Java Code Examples for com.squareup.okhttp.Request#header()

The following examples show how to use com.squareup.okhttp.Request#header() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SendFeedBack.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Response intercept(Chain chain) throws IOException {
    Request originalRequest = chain.request();
    if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
        return chain.proceed(originalRequest);
    }

    Request compressedRequest = originalRequest.newBuilder()
            .header("Content-Encoding", "gzip")
            .method(originalRequest.method(), forceContentLength(gzip(originalRequest.body())))
            .build();
    return chain.proceed(compressedRequest);
}
 
Example 2
Source File: SendFeedBack.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Response intercept(Chain chain) throws IOException {
    Request originalRequest = chain.request();
    if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
        return chain.proceed(originalRequest);
    }

    Request compressedRequest = originalRequest.newBuilder()
            .header("Content-Encoding", "gzip")
            .method(originalRequest.method(), forceContentLength(gzip(originalRequest.body())))
            .build();
    return chain.proceed(compressedRequest);
}
 
Example 3
Source File: TokenRefreshInterceptorTest.java    From wear-notify-for-reddit with Apache License 2.0 5 votes vote down vote up
private Matcher<Request> hasAuthorisationHeader(String authHeader) {
    return new FeatureMatcher<Request, String>(equalTo(authHeader),
            "Authorisation header",
            "Unexpected auth header") {
        @Override protected String featureValueOf(Request actual) {
            return actual.header(AUTHORIZATION);
        }
    };
}
 
Example 4
Source File: MetricsInterceptor.java    From kork with Apache License 2.0 4 votes vote down vote up
protected final Object doIntercept(Object chainObject) throws IOException {
  long start = System.nanoTime();
  boolean wasSuccessful = false;
  int statusCode = -1;

  Interceptor.Chain chain =
      (chainObject instanceof Interceptor.Chain) ? (Interceptor.Chain) chainObject : null;
  okhttp3.Interceptor.Chain chain3 =
      (chainObject instanceof okhttp3.Interceptor.Chain)
          ? (okhttp3.Interceptor.Chain) chainObject
          : null;

  Request request = (chain != null) ? chain.request() : null;
  okhttp3.Request request3 = (chain3 != null) ? chain3.request() : null;

  List<String> missingHeaders = new ArrayList<>();
  String method = null;
  URL url = null;

  try {
    String xSpinAnonymous = MDC.get(Header.XSpinnakerAnonymous);

    if (xSpinAnonymous == null && !skipHeaderCheck) {
      for (Header header : Header.values()) {
        String headerValue =
            (request != null)
                ? request.header(header.getHeader())
                : request3.header(header.getHeader());

        if (header.isRequired() && StringUtils.isEmpty(headerValue)) {
          missingHeaders.add(header.getHeader());
        }
      }
    }

    Object response;

    if (chain != null) {
      method = request.method();
      url = request.url();
      response = chain.proceed(request);
      statusCode = ((Response) response).code();
    } else {
      method = request3.method();
      url = request3.url().url();
      response = chain3.proceed(request3);
      statusCode = ((okhttp3.Response) response).code();
    }

    wasSuccessful = true;
    return response;
  } finally {
    boolean missingAuthHeaders = missingHeaders.size() > 0;

    if (missingAuthHeaders) {
      List<String> stack =
          Arrays.stream(Thread.currentThread().getStackTrace())
              .map(StackTraceElement::toString)
              .filter(x -> x.contains("com.netflix.spinnaker"))
              .collect(Collectors.toList());

      String stackTrace = String.join("\n\tat ", stack);
      log.warn(
          String.format(
              "Request %s:%s is missing %s authentication headers and will be treated as anonymous.\nRequest from: %s",
              method, url, missingHeaders, stackTrace));
    }

    recordTimer(
        registry.get(),
        url,
        System.nanoTime() - start,
        statusCode,
        wasSuccessful,
        !missingAuthHeaders);
  }
}