Esempio n. 1
0
 private static void logRequest(Request request) {
   LOGGER.fine("---> HTTP " + request.getMethod() + " " + request.getUrl());
   for (Header header : request.getHeaders()) {
     LOGGER.fine(header.getName() + ": " + header.getValue());
   }
   LOGGER.fine("---> END HTTP");
 }
Esempio n. 2
0
  private static Profiler.RequestInformation getRequestInfo(
      Server server, RestMethodInfo methodDetails, Request request) {
    long contentLength = 0;
    String contentType = null;

    TypedOutput body = request.getBody();
    if (body != null) {
      contentLength = body.length();
      contentType = body.mimeType();
    }

    return new Profiler.RequestInformation(
        methodDetails.restMethod.value(),
        server.getUrl(),
        methodDetails.path,
        contentLength,
        contentType);
  }
Esempio n. 3
0
    /**
     * Execute an HTTP request.
     *
     * @return HTTP response object of specified {@code type}.
     * @throws RetrofitError Thrown if any error occurs during the HTTP request.
     */
    private Object invokeRequest(RestMethodInfo methodDetails, Object[] args) {
      methodDetails.init(); // Ensure all relevant method information has been loaded.

      String url = server.getUrl();
      try {
        Request request =
            new RequestBuilder(converter) //
                .setApiUrl(server.getUrl())
                .setArgs(args)
                .setHeaders(headers.get())
                .setMethodInfo(methodDetails)
                .build();
        url = request.getUrl();

        if (!methodDetails.isSynchronous) {
          // If we are executing asynchronously then update the current thread with a useful name.
          Thread.currentThread().setName(THREAD_PREFIX + url);
        }

        if (LOGGER.isLoggable(Level.FINE)) {
          logRequest(request);
        }

        Object profilerObject = null;
        if (profiler != null) {
          profilerObject = profiler.beforeCall();
        }

        long start = System.nanoTime();
        Response response = clientProvider.get().execute(request);
        long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);

        int statusCode = response.getStatus();
        if (profiler != null) {
          RequestInformation requestInfo = getRequestInfo(server, methodDetails, request);
          profiler.afterCall(requestInfo, elapsedTime, statusCode, profilerObject);
        }

        TypedInput body = response.getBody();
        if (LOGGER.isLoggable(Level.FINE)) {
          // Replace the response since the logger needs to consume the entire input stream.
          body = logResponse(url, response.getStatus(), body, elapsedTime);
        }

        List<Header> headers = response.getHeaders();
        for (Header header : headers) {
          if (HTTP.CONTENT_TYPE.equalsIgnoreCase(header.getName()) //
              && !UTF_8.equalsIgnoreCase(Utils.parseCharset(header.getValue()))) {
            throw new IOException("Only UTF-8 charset supported.");
          }
        }

        Type type = methodDetails.type;
        if (statusCode >= 200 && statusCode < 300) { // 2XX == successful request
          if (type.equals(Response.class)) {
            return response;
          }
          if (body == null) {
            return null;
          }
          try {
            return converter.fromBody(body, type);
          } catch (ConversionException e) {
            throw RetrofitError.conversionError(url, response, converter, type, e);
          }
        }
        throw RetrofitError.httpError(url, response, converter, type);
      } catch (RetrofitError e) {
        throw e; // Pass through our own errors.
      } catch (IOException e) {
        throw RetrofitError.networkError(url, e);
      } catch (Throwable t) {
        throw RetrofitError.unexpectedError(url, t);
      }
    }