@GET @Path("/uninstalled") @Produces({MediaType.APPLICATION_JSON}) public String getUninstalledConnectors() { Guest guest = AuthHelper.getGuest(); // If no guest is logged in, return empty array if (guest == null) return "[]"; try { List<ConnectorInfo> allConnectors = sysService.getConnectors(); List<ConnectorInfo> connectors = new ArrayList<ConnectorInfo>(); for (ConnectorInfo connector : allConnectors) { if (connector.enabled && !connector.connectorName.equals("facebook")) connectors.add(connector); } for (int i = 0; i < connectors.size(); i++) { if (guestService.hasApiKey(guest.getId(), connectors.get(i).getApi())) connectors.remove(i--); } StringBuilder sb = new StringBuilder("module=API component=connectorStore action=getUninstalledConnectors") .append(" guestId=") .append(guest.getId()); logger.info(sb.toString()); return gson.toJson(connectors); } catch (Exception e) { StringBuilder sb = new StringBuilder("module=API component=connectorStore action=getUninstalledConnectors") .append(" guestId=") .append(guest.getId()) .append(" stackTrace=<![CDATA[") .append(Utils.stackTrace(e)) .append("]]>"); logger.warn(sb.toString()); return gson.toJson( new StatusModel(false, "Failed to get uninstalled connectors: " + e.getMessage())); } }
@GET @Path("/installed") @Produces({MediaType.APPLICATION_JSON}) public String getInstalledConnectors() { Guest guest = AuthHelper.getGuest(); // If no guest is logged in, return empty array if (guest == null) return "[]"; ResourceBundle res = ResourceBundle.getBundle("messages/connectors"); try { List<ConnectorInfo> connectors = sysService.getConnectors(); JSONArray connectorsArray = new JSONArray(); for (int i = 0; i < connectors.size(); i++) { final ConnectorInfo connectorInfo = connectors.get(i); final Connector api = connectorInfo.getApi(); if (api == null) { StringBuilder sb = new StringBuilder( "module=API component=connectorStore action=getInstalledConnectors "); logger.warn("message=\"null connector for " + connectorInfo.getName() + "\""); continue; } if (!guestService.hasApiKey(guest.getId(), api) || api.getName().equals("facebook") /*HACK*/) { connectors.remove(i--); } else { ConnectorInfo connector = connectorInfo; JSONObject connectorJson = new JSONObject(); Connector conn = Connector.fromValue(connector.api); ApiKey apiKey = guestService.getApiKey(guest.getId(), conn); connectorJson.accumulate("prettyName", conn.prettyName()); List<String> facetTypes = new ArrayList<String>(); ObjectType[] objTypes = conn.objectTypes(); if (objTypes != null) { for (ObjectType obj : objTypes) { facetTypes.add(connector.connectorName + "-" + obj.getName()); } } connectorJson.accumulate("facetTypes", facetTypes); connectorJson.accumulate( "status", apiKey.status != null ? apiKey.status.toString() : "NA"); connectorJson.accumulate("name", connector.name); connectorJson.accumulate("connectUrl", connector.connectUrl); connectorJson.accumulate("image", connector.image); connectorJson.accumulate("connectorName", connector.connectorName); connectorJson.accumulate("enabled", connector.enabled); connectorJson.accumulate("manageable", connector.manageable); connectorJson.accumulate("text", connector.text); connectorJson.accumulate("api", connector.api); connectorJson.accumulate("apiKeyId", apiKey.getId()); connectorJson.accumulate( "lastSync", connector.supportsSync ? getLastSync(apiKey) : Long.MAX_VALUE); connectorJson.accumulate("latestData", getLatestData(apiKey)); final String auditTrail = checkForErrors(apiKey); connectorJson.accumulate("errors", auditTrail != null); connectorJson.accumulate("auditTrail", auditTrail != null ? auditTrail : ""); connectorJson.accumulate("syncing", checkIfSyncInProgress(guest.getId(), conn)); connectorJson.accumulate( "channels", settingsService.getChannelsForConnector(guest.getId(), conn)); connectorJson.accumulate("sticky", connector.connectorName.equals("fluxtream_capture")); connectorJson.accumulate("supportsRenewToken", connector.supportsRenewTokens); connectorJson.accumulate("supportsSync", connector.supportsSync); connectorJson.accumulate("supportsFileUpload", connector.supportsFileUpload); connectorJson.accumulate("prettyName", conn.prettyName()); final String uploadMessageKey = conn.getName() + ".upload"; if (res.containsKey(uploadMessageKey)) { final String uploadMessage = res.getString(uploadMessageKey); connectorJson.accumulate("uploadMessage", uploadMessage); } connectorsArray.add(connectorJson); } } StringBuilder sb = new StringBuilder("module=API component=connectorStore action=getInstalledConnectors") .append(" guestId=") .append(guest.getId()); logger.info(sb.toString()); return connectorsArray.toString(); } catch (Exception e) { StringBuilder sb = new StringBuilder("module=API component=connectorStore action=getInstalledConnectors") .append(" guestId=") .append(guest.getId()) .append(" stackTrace=<![CDATA[") .append(Utils.stackTrace(e)) .append("]]>"); System.out.println(sb.toString()); logger.warn(sb.toString()); return gson.toJson( new StatusModel(false, "Failed to get installed connectors: " + e.getMessage())); } }