Пример #1
0
 @DELETE
 @Path("/{connector}")
 @Produces({MediaType.APPLICATION_JSON})
 public String deleteConnector(@PathParam("connector") String connector) {
   StatusModel result;
   Guest guest = AuthHelper.getGuest();
   // If no guest is logged in, return empty array
   if (guest == null) return "{}";
   try {
     Connector apiToRemove = Connector.fromString(connector);
     guestService.removeApiKeys(guest.getId(), apiToRemove);
     result = new StatusModel(true, "Successfully removed " + connector + ".");
     StringBuilder sb =
         new StringBuilder("module=API component=connectorStore action=deleteConnector")
             .append(" connector=")
             .append(connector)
             .append(" guestId=")
             .append(guest.getId());
     logger.info(sb.toString());
   } catch (Exception e) {
     StringBuilder sb =
         new StringBuilder("module=API component=connectorStore action=deleteConnector")
             .append(" connector=")
             .append(connector)
             .append(" guestId=")
             .append(guest.getId())
             .append(" stackTrace=<![CDATA[")
             .append(Utils.stackTrace(e))
             .append("]]>");
     logger.warn(sb.toString());
     result = new StatusModel(false, "Failed to remove " + connector + ".");
   }
   return gson.toJson(result);
 }
Пример #2
0
 @POST
 @Path("/renew/{apiKeyId}")
 @Produces({MediaType.APPLICATION_JSON})
 public String renewConnectorTokens(@PathParam("apiKeyId") long apiKeyId) throws Exception {
   final ApiKey apiKey = guestService.getApiKey(apiKeyId);
   ConnectorInfo connectorInfo = sysService.getConnectorInfo(apiKey.getConnector().getName());
   JSONObject renewInfo = new JSONObject();
   final String renewTokensUrlTemplate = connectorInfo.renewTokensUrlTemplate;
   final String renewTokensUrl = String.format(renewTokensUrlTemplate, apiKey.getId());
   renewInfo.accumulate("redirectTo", env.get("homeBaseUrl") + renewTokensUrl);
   return renewInfo.toString();
 }
Пример #3
0
 @GET
 @Path("/settings/{apiKeyId}")
 @Produces({MediaType.APPLICATION_JSON})
 public String getConnectorSettings(@PathParam("apiKeyId") long apiKeyId)
     throws UpdateFailedException, IOException {
   final ApiKey apiKey = guestService.getApiKey(apiKeyId);
   final long guestId = AuthHelper.getGuestId();
   if (apiKey.getGuestId() != guestId)
     throw new RuntimeException("attempt to retrieve ApiKey from another guest!");
   final Object settings = settingsService.getConnectorSettings(apiKey.getId());
   String json = mapper.writeValueAsString(settings);
   return json;
 }
Пример #4
0
  @GET
  @Path("/{objectTypeName}/data")
  @Produces({MediaType.APPLICATION_JSON})
  public String getData(
      @PathParam("objectTypeName") String objectTypeName,
      @QueryParam("start") long start,
      @QueryParam("end") long end,
      @QueryParam("value") String value) {
    Guest guest = AuthHelper.getGuest();
    if (guest == null) return "[]";

    CoachingBuddy coachee;
    try {
      coachee = AuthHelper.getCoachee();
    } catch (CoachRevokedException e) {
      return gson.toJson(
          new StatusModel(
              false,
              "Sorry, permission to access this data has been revoked. Please reload your browser window"));
    }
    if (coachee != null) {
      guest = guestService.getGuestById(coachee.guestId);
    }

    String[] objectTypeNameParts = objectTypeName.split("-");
    ApiKey apiKey =
        guestService
            .getApiKeys(guest.getId(), Connector.getConnector(objectTypeNameParts[0]))
            .get(0);
    Connector connector = apiKey.getConnector();

    final AbstractBodytrackResponder bodytrackResponder =
        connector.getBodytrackResponder(beanFactory);
    return gson.toJson(
        bodytrackResponder.getFacetVOs(
            settingsService.getSettings(guest.getId()), apiKey, objectTypeName, start, end, value));
  }
Пример #5
0
 private long getLatestData(ApiKey apiKey) {
   if (!apiKey.getConnector().hasFacets()) return Long.MAX_VALUE;
   final ObjectType[] objectTypes = apiKey.getConnector().objectTypes();
   if (objectTypes == null || objectTypes.length == 0) {
     final String maxTimeAtt =
         guestService.getApiKeyAttribute(apiKey, ApiKeyAttribute.MAX_TIME_KEY);
     // only return the ApiKey's maxTime if we have it cached as an attribute
     if (maxTimeAtt != null && StringUtils.isNotEmpty(maxTimeAtt)) {
       final DateTime dateTime =
           ISODateTimeFormat.dateHourMinuteSecondFraction()
               .withZoneUTC()
               .parseDateTime(maxTimeAtt);
       return dateTime.getMillis();
     }
   } else {
     long maxTime = Long.MIN_VALUE;
     for (ObjectType objectType : objectTypes) {
       final String maxTimeAtt =
           guestService.getApiKeyAttribute(
               apiKey, objectType.getApiKeyAttributeName(ApiKeyAttribute.MAX_TIME_KEY));
       if (maxTimeAtt != null && StringUtils.isNotEmpty(maxTimeAtt)) {
         final DateTime dateTime =
             ISODateTimeFormat.dateHourMinuteSecondFraction()
                 .withZoneUTC()
                 .parseDateTime(maxTimeAtt);
         final long maxObjectTypeTime = dateTime.getMillis();
         if (maxObjectTypeTime > maxTime) maxTime = maxObjectTypeTime;
       }
     }
     // only return the ApiKey's maxTime if we have it cached as an attribute for any its
     // connector's objectTypes
     if (maxTime > Long.MIN_VALUE) return maxTime;
   }
   // fall back to old method of querying the facets table
   AbstractFacet facet = null; // apiDataService.getLatestApiDataFacet(apiKey, null);
   return facet == null ? Long.MAX_VALUE : facet.end;
 }
Пример #6
0
 private long getLastSync(ApiKey apiKey) {
   if (!apiKey.getConnector().hasFacets()) return Long.MAX_VALUE;
   final String lastSyncTimeAtt =
       guestService.getApiKeyAttribute(apiKey, ApiKeyAttribute.LAST_SYNC_TIME_KEY);
   // only return the ApiKey's lastSyncTime if we have it cached as an attribute
   if (lastSyncTimeAtt != null && StringUtils.isNotEmpty(lastSyncTimeAtt)) {
     final DateTime dateTime =
         ISODateTimeFormat.dateHourMinuteSecondFraction()
             .withZoneUTC()
             .parseDateTime(lastSyncTimeAtt);
     return dateTime.getMillis();
   }
   // fall back to old method of querying the ApiUpdates table
   ApiUpdate update = null; // connectorUpdateService.getLastSuccessfulUpdate(apiKey);
   return update != null ? update.ts : Long.MAX_VALUE;
 }
Пример #7
0
 @POST
 @Path("/settings/{apiKeyId}")
 @Produces({MediaType.APPLICATION_JSON})
 public StatusModel saveConnectorSettings(
     @PathParam("apiKeyId") long apiKeyId, @FormParam("json") String json) {
   final ApiKey apiKey = guestService.getApiKey(apiKeyId);
   final long guestId = AuthHelper.getGuestId();
   try {
     if (apiKey.getGuestId() != guestId)
       throw new RuntimeException("attempt to retrieve ApiKey from another guest!");
     settingsService.saveConnectorSettings(apiKey.getId(), json);
   } catch (Throwable e) {
     return new StatusModel(false, e.getMessage());
   }
   return new StatusModel(true, "saved connector settings");
 }
Пример #8
0
 @POST
 @Path("/{connector}/channels")
 @Produces({MediaType.APPLICATION_JSON})
 public String setConnectorChannels(
     @PathParam("connector") String connectorName, @FormParam("channels") String channels) {
   StatusModel result;
   Guest guest = AuthHelper.getGuest();
   // If no guest is logged in, return empty array
   if (guest == null) return "{}";
   try {
     ApiKey apiKey = guestService.getApiKey(guest.getId(), Connector.getConnector(connectorName));
     settingsService.setChannelsForConnector(
         guest.getId(), apiKey.getConnector(), channels.split(","));
     result = new StatusModel(true, "Successfully updated channels for " + connectorName + ".");
     StringBuilder sb =
         new StringBuilder("module=API component=connectorStore action=setConnectorChannels")
             .append(" connector=")
             .append(connectorName)
             .append(" channels=")
             .append(channels)
             .append(" guestId=")
             .append(guest.getId());
     logger.info(sb.toString());
   } catch (Exception e) {
     StringBuilder sb =
         new StringBuilder("module=API component=connectorStore action=setConnectorChannels")
             .append(" connector=")
             .append(connectorName)
             .append(" guestId=")
             .append(guest.getId())
             .append(" stackTrace=<![CDATA[")
             .append(Utils.stackTrace(e))
             .append("]]>");
     logger.warn(sb.toString());
     result = new StatusModel(false, "Failed to set channels for " + connectorName + ".");
   }
   return gson.toJson(result);
 }
Пример #9
0
 @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()));
   }
 }
Пример #10
0
 private boolean checkIfSyncInProgress(long guestId, Connector connector) {
   final ApiKey apiKey = guestService.getApiKey(guestId, connector);
   return (apiKey.synching);
 }
Пример #11
0
  @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()));
    }
  }