コード例 #1
0
 private static <T> String requestHolderToString(RequestHolder<T> requestHolder) {
   try {
     return requestHolder.getMethod()
         + " "
         + requestHolder.getUrl()
         + (Strings.isNullOrEmpty(requestHolder.getBody())
             ? ""
             : "\n" + Util.prettyPrintJsonStringSecure(requestHolder.getBody()));
   } catch (IOException e) {
     throw Util.toSphereException(e);
   }
 }
コード例 #2
0
 /** Executes request and parses JSON response as given type. */
 public static <T> ListenableFuture<SphereResultRaw<T>> execute(
     final RequestHolder<T> requestHolder, final TypeReference<T> jsonParserTypeRef) {
   try {
     return requestHolder.executeRequest(
         new AsyncCompletionHandler<SphereResultRaw<T>>() {
           public SphereResultRaw<T> onCompleted(Response response) throws Exception {
             int status = response.getStatusCode();
             String body = response.getResponseBody(Charsets.UTF_8.name());
             if (status / 100 != 2) {
               SphereErrorResponse errorResponse = null;
               try {
                 errorResponse = jsonParser.readValue(body, errorResponseJsonTypeRef);
               } catch (Exception e) {
                 // This can only happen when the backend and SDK don't match.
                 Log.error(
                     "Can't parse backend response: \n["
                         + status
                         + "]\n"
                         + body
                         + "\n\nRequest: "
                         + requestHolderToString(requestHolder));
                 throw new SphereException("Can't parse backend response.", e);
               }
               if (Log.isErrorEnabled()) {
                 Log.error(errorResponse + "\n\nRequest: " + requestHolderToString(requestHolder));
               }
               return SphereResultRaw.<T>error(
                   new SphereBackendException(requestHolder.getUrl(), errorResponse));
             } else {
               if (Log.isTraceEnabled()) {
                 Log.trace(
                     requestHolderToString(requestHolder)
                         + "\n"
                         + "=> "
                         + response.getStatusCode()
                         + "\n"
                         + Util.prettyPrintJsonStringSecure(body)
                         + "\n");
               } else if (Log.isDebugEnabled()) {
                 Log.debug(requestHolderToString(requestHolder));
               }
               return SphereResultRaw.<T>success(jsonParser.<T>readValue(body, jsonParserTypeRef));
             }
           }
         });
   } catch (Exception e) {
     throw Util.toSphereException(e);
   }
 }
コード例 #3
0
 private void update(Tokens tokens, Exception e) {
   synchronized (accessTokenLock) {
     try {
       if (e == null) {
         AccessToken newToken =
             new AccessToken(
                 tokens.getAccessToken(), tokens.getExpiresIn(), System.currentTimeMillis());
         this.accessTokenResult = Optional.of(ValidationE.<AccessToken>success(newToken));
         Log.debug("[oauth] Refreshed access token.");
         scheduleNextRefresh(tokens);
       } else {
         this.accessTokenResult =
             Optional.of(ValidationE.<AccessToken>error(Util.toSphereException(e)));
         Log.error("[oauth] Failed to refresh access token.", e);
       }
     } finally {
       accessTokenLock.notifyAll();
     }
   }
 }