Package org.fluxtream.core.connectors.updaters

Examples of org.fluxtream.core.connectors.updaters.UpdateFailedException


            JSONObject errorJson = JSONObject.fromObject(content);
            if (errorJson.has("meta")) {
                JSONObject meta = errorJson.getJSONObject("meta");
                if (meta.has("error_type")) {
                    String details = meta.has("error_detail") ? meta.getString("error_details") : "Unknown Error (no details provided)";
                    throw new UpdateFailedException(message + " - " + details, true, ApiKey.PermanentFailReason.unknownReason(details));
                }
            }
        } catch (Throwable t) {
            // just ignore any potential problems here
        }
View Full Code Here


                                                 "scroll to the " + apiKey.getConnector().getName() + " connector, and renew your tokens (look for the <i class=\"icon-resize-small icon-large\"></i> icon)");

            // Record permanent failure since this connector won't work again until
            // it is reauthenticated
            guestService.setApiKeyStatus(apiKey.getId(), ApiKey.Status.STATUS_PERMANENT_FAILURE, null, ApiKey.PermanentFailReason.NEEDS_REAUTH);
            throw new UpdateFailedException("requires token reauthorization", true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
        }

        // We're not on a mirrored test server.  Try to swap the expired
        // access token for a fresh one.
        String swapTokenUrl = "https://accounts.google.com/o/oauth2/token";

        final String refreshToken = guestService.getApiKeyAttribute(apiKey, "refreshToken");
        Map<String,String> params = new HashMap<String,String>();
        params.put("refresh_token", refreshToken);
        params.put("client_id", env.get("google.client.id"));
        params.put("client_secret", env.get("google.client.secret"));
        params.put("grant_type", "refresh_token");

        String fetched;
        try {
            fetched = HttpUtils.fetch(swapTokenUrl, params);
            logger.info("component=background_updates action=refreshToken" +
                        " connector="
                        + apiKey.getConnector().getName()
                        + " guestId=" + apiKey.getGuestId()
                        + " status=success");
            // Record that this connector is now up
            guestService.setApiKeyStatus(apiKey.getId(), ApiKey.Status.STATUS_UP, null, null);
        } catch (IOException e) {
            logger.warn("component=background_updates action=refreshToken" +
                        " connector=" + apiKey.getConnector().getName() + " guestId=" + apiKey.getGuestId() + " status=failed");
            // Notify the user that the tokens need to be manually renewed
            notificationsService.addNotification(apiKey.getGuestId(), Notification.Type.WARNING,
                                                 "Heads Up. We failed in our attempt to automatically refresh your authentication tokens.<br>" +
                                                 "Please head to <a href=\"javascript:App.manageConnectors()\">Manage Connectors</a>,<br>" +
                                                 "scroll to the " + apiKey.getConnector().getName() + " connector, and renew your tokens (look for the <i class=\"icon-resize-small icon-large\"></i> icon)");

            // Record permanent update failure since this connector is never
            // going to succeed
            guestService.setApiKeyStatus(apiKey.getId(), ApiKey.Status.STATUS_PERMANENT_FAILURE, Utils.stackTrace(e), ApiKey.PermanentFailReason.NEEDS_REAUTH);
            throw new UpdateFailedException("refresh token attempt failed", e, true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
        }

        JSONObject token = JSONObject.fromObject(fetched);
        final long expiresIn = token.getLong("expires_in");
        final String access_token = token.getString("access_token");
View Full Code Here

                if (stat.equalsIgnoreCase("fail")) {
                    String message = feed.getString("message");
                    if (message.indexOf("Invalid auth token")!=-1) {
                        throw new AuthExpiredException();
                    } else
                        throw new UpdateFailedException("Could not retrieve flickr recently updated photos: " + message,
                                                        true,
                                                        ApiKey.PermanentFailReason.unknownReason(message));
                }
            }
            JSONObject photosWrapper = feed.getJSONObject("photos");
View Full Code Here

        } catch (UnexpectedHttpResponseCodeException e) {
            countFailedApiCall(updateInfo.apiKey, updateInfo.objectTypes,
                               then, searchPhotosUrl, Utils.stackTrace(e),
                               e.getHttpResponseCode(), e.getHttpResponseMessage());
            if (e.getHttpResponseCode()>=400 && e.getHttpResponseCode()<500)
                throw new UpdateFailedException("Unexpected response code: " + e.getHttpResponseCode(), new Exception(), true,
                                                ApiKey.PermanentFailReason. clientError(e.getHttpResponseCode(), e.getHttpResponseMessage()));
            throw new UpdateFailedException(e, false, null);
    } catch (IOException e) {
      reportFailedApiCall(updateInfo.apiKey, updateInfo.objectTypes, then, searchPhotosUrl,
                                Utils.stackTrace(e), "I/O");
      throw e;
    }
View Full Code Here

        } catch (UnexpectedHttpResponseCodeException e) {
            countFailedApiCall(updateInfo.apiKey, updateInfo.objectTypes,
                               then, searchPhotosUrl, Utils.stackTrace(e),
                               e.getHttpResponseCode(), e.getHttpResponseMessage());
            if (e.getHttpResponseCode()>=400 && e.getHttpResponseCode()<500)
                throw new UpdateFailedException("Unexpected response code: " + e.getHttpResponseCode(), new Exception(), true,
                                                ApiKey.PermanentFailReason.clientError(e.getHttpResponseCode(), e.getHttpResponseMessage()));
            throw new UpdateFailedException(e, false, null);
        } catch (IOException e) {
            reportFailedApiCall(updateInfo.apiKey, updateInfo.objectTypes,
                               then, searchPhotosUrl, Utils.stackTrace(e), "I/O");
            throw e;
        }
View Full Code Here

        notificationsService.addNamedNotification(updateInfo.getGuestId(), Notification.Type.WARNING, connector().statusNotificationName(),
                                                  "Heads Up. This server has recently been upgraded to a version that supports<br>" +
                                                  "oauth 2 with Google APIs. Please head to <a href=\"javascript:App.manageConnectors()\">Manage Connectors</a>,<br>" +
                                                  "scroll to the Google Calendar connector, and renew your tokens (look for the <i class=\"icon-resize-small icon-large\"></i> icon)");
        // Report this connector as having failed permanently
        throw new UpdateFailedException("requires token reauthorization", true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
    }
View Full Code Here

                                                      "scroll to the Google Calendar connector, and renew your tokens (look for the <i class=\"icon-resize-small icon-large\"></i> icon)");

            // Record permanent update failure since this connector is never
            // going to succeed
            guestService.setApiKeyStatus(apiKey.getId(), ApiKey.Status.STATUS_PERMANENT_FAILURE, Utils.stackTrace(e), ApiKey.PermanentFailReason.NEEDS_REAUTH);
            throw new UpdateFailedException("refresh token attempt permanently failed due to a bad token refresh response", e, true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
        }
        catch (IOException e) {
            logger.warn("module=GoogleCalendarUpdater component=background_updates action=refreshToken" +
                        " connector=" + apiKey.getConnector().getName() + " guestId=" + apiKey.getGuestId() + " status=permanently failed");
            // Notify the user that the tokens need to be manually renewed
            throw new UpdateFailedException("refresh token attempt failed", e, true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
        }
        final Calendar.Builder calendarBuilder = new Calendar.Builder(httpTransport, jsonFactory, credential);
        final Calendar calendar = calendarBuilder.build();
        return calendar;
    }
View Full Code Here

                                                      "scroll to the BodyMedia connector, and renew your tokens (look for the <i class=\"icon-resize-small icon-large\"></i> icon)");

            // Record permanent failure since this connector won't work again until
            // it is reauthenticated
            guestService.setApiKeyStatus(updateInfo.apiKey.getId(), ApiKey.Status.STATUS_PERMANENT_FAILURE, null, ApiKey.PermanentFailReason.NEEDS_REAUTH);
            throw new UpdateFailedException("requires token reauthorization",true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
        }

        // We're not on a mirrored test server.  Try to swap the expired
        // access token for a fresh one.

        // First, retrieve the OAuth server keys used when this key was created.  These are automatically stored
        // in the ApiKeyAttribute table at the time of creation based on the values present in
        // oauth.properties.
        String bodymediaConsumerKey = guestService.getApiKeyAttribute(updateInfo.apiKey, "bodymediaConsumerKey");
        String bodymediaConsumerSecret = guestService.getApiKeyAttribute(updateInfo.apiKey, "bodymediaConsumerSecret");

        OAuthConsumer consumer = new DefaultOAuthConsumer(
                bodymediaConsumerKey,
                bodymediaConsumerSecret);

        String accessToken = guestService.getApiKeyAttribute(updateInfo.apiKey, "accessToken");
        consumer.setTokenWithSecret(accessToken,
                guestService.getApiKeyAttribute(updateInfo.apiKey, "tokenSecret"));
        HttpParameters additionalParameter = new HttpParameters();
        additionalParameter.put("api_key", bodymediaConsumerKey);
        additionalParameter.put("oauth_token",
                                accessToken);
        consumer.setAdditionalParameters(additionalParameter);

        HttpClient httpClient = env.getHttpClient();

        OAuthProvider provider = new CommonsHttpOAuthProvider(
                "https://api.bodymedia.com/oauth/request_token?api_key="+bodymediaConsumerKey,
                "https://api.bodymedia.com/oauth/access_token?api_key="+bodymediaConsumerKey,
                "https://api.bodymedia.com/oauth/authorize?api_key="+bodymediaConsumerKey, httpClient);

        try {
            provider.retrieveAccessToken(consumer, null);

            guestService.setApiKeyAttribute(updateInfo.apiKey,
                                            "accessToken", consumer.getToken());
            guestService.setApiKeyAttribute(updateInfo.apiKey,
                                            "tokenSecret", consumer.getTokenSecret());
            guestService.setApiKeyAttribute(updateInfo.apiKey,
                                            "tokenExpiration", provider.getResponseParameters().get("xoauth_token_expiration_time").first());

            // Record this connector as having status up
            guestService.setApiKeyStatus(updateInfo.apiKey.getId(), ApiKey.Status.STATUS_UP, null, null);
            // Schedule an update for this connector
            connectorUpdateService.updateConnector(updateInfo.apiKey, false);

        } catch (Throwable t) {
            // Notify the user that the tokens need to be manually renewed
            notificationsService.addNamedNotification(updateInfo.getGuestId(), Notification.Type.WARNING, connector().statusNotificationName(),
                                                      "Heads Up. We failed in our attempt to automatically refresh your authentication tokens.<br>" +
                                                      "Please head to <a href=\"javascript:App.manageConnectors()\">Manage Connectors</a>,<br>" +
                                                      "scroll to the BodyMedia connector, and renew your tokens (look for the <i class=\"icon-resize-small icon-large\"></i> icon)");
            // Record permanent failure since this connector won't work again until
            // it is reauthenticated
            guestService.setApiKeyStatus(updateInfo.apiKey.getId(), ApiKey.Status.STATUS_PERMANENT_FAILURE, null, ApiKey.PermanentFailReason.NEEDS_REAUTH);
            throw new UpdateFailedException("refresh token attempt failed", t, true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
        }
    }
View Full Code Here

                                   e.getHttpResponseCode(), e.getHttpResponseMessage());

                // The update failed.  We don't know if this is permanent or temporary.
                // let's assume that it is permanent if it's our fault (4xx)
                if (e.getHttpResponseCode()>=400&&e.getHttpResponseCode()<500)
                    throw new UpdateFailedException("Unexpected response code: " + e.getHttpResponseCode(), new Exception(), true,
                                                    ApiKey.PermanentFailReason.clientError(e.getHttpResponseCode(), e.getHttpResponseMessage()));
                throw new UpdateFailedException(e);

            } catch (RateLimitReachedException e) {
                // Couldn't get user registration date, rate limit reached
                StringBuilder sb = new StringBuilder("module=updateQueue component=updater action=MovesUpdater.getUserRegistrationDate")
                        .append(" message=\"rate limit reached while retrieving UserRegistrationDate\" connector=")
                        .append(updateInfo.apiKey.getConnector().toString()).append(" guestId=")
                        .append(updateInfo.apiKey.getGuestId())
                        .append(" stackTrace=<![CDATA[").append(Utils.stackTrace(e)).append("]]>");;
                logger.info(sb.toString());

                countFailedApiCall(updateInfo.apiKey, updateInfo.objectTypes, currentTime, query, Utils.stackTrace(e),
                                   429, "Rate limit reached");

                // Rethrow the rate limit reached exception
                throw e;

            } catch (IOException e) {
                // Couldn't get user registration date
                StringBuilder sb = new StringBuilder("module=updateQueue component=updater action=MovesUpdater.getUserRegistrationDate")
                        .append(" message=\"exception while retrieving UserRegistrationDate\" connector=")
                        .append(updateInfo.apiKey.getConnector().toString()).append(" guestId=")
                        .append(updateInfo.apiKey.getGuestId())
                        .append(" stackTrace=<![CDATA[").append(Utils.stackTrace(e)).append("]]>");
                logger.info(sb.toString());

                reportFailedApiCall(updateInfo.apiKey, updateInfo.objectTypes, currentTime, query, Utils.stackTrace(e), "I/O");

                // The update failed.  We don't know if this is permanent or temporary.
                // Throw the appropriate exception.
                throw new UpdateFailedException(e);
            }
        }
        return userRegistrationDate;
    }
View Full Code Here

            System.out.println("MOVES: guestId=" + updateInfo.getGuestId() + ", UPDATE FAILED (don't know why)");

            // The update failed.  We don't know if this is permanent or temporary.
            // Throw the appropriate exception.
            throw new UpdateFailedException(e);
        }

    }
View Full Code Here

TOP

Related Classes of org.fluxtream.core.connectors.updaters.UpdateFailedException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.