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

The following examples show how to use com.squareup.okhttp.Request#url() . 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: OkHttp2Probe.java    From pre-dem-android with MIT License 6 votes vote down vote up
@Around("call(* com.squareup.okhttp.OkHttpClient+.newCall(..))")
public Object onOkHttpNew(ProceedingJoinPoint joinPoint) throws Throwable {
    if (!Configuration.httpMonitorEnable || joinPoint.getArgs().length != 1) {
        return joinPoint.proceed();
    }
    Object[] args = joinPoint.getArgs();
    Request request = (Request) args[0];

    //url
    URL url = request.url();
    if (GlobalConfig.isExcludeHost(url.getHost())) {
        return joinPoint.proceed();
    }
    RespBean bean = new RespBean();
    bean.setUrl(url.toString());
    bean.setStartTimestamp(System.currentTimeMillis());
    startTimeStamp.add(bean);
    return joinPoint.proceed();
}
 
Example 2
Source File: OkHttpClientAsyncInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
private static void onBeforeEnqueue(@Advice.Origin Class<? extends Call> clazz,
                                    @Advice.FieldValue(value = "originalRequest", typing = Assigner.Typing.DYNAMIC, readOnly = false) @Nullable Request originalRequest,
                                    @Advice.Argument(value = 0, readOnly = false) @Nullable Callback callback,
                                    @Advice.Local("span") Span span) {
    if (tracer == null || tracer.getActive() == null || callbackWrapperCreator == null) {
        return;
    }

    final WrapperCreator<Callback> wrapperCreator = callbackWrapperCreator.getForClassLoaderOfClass(clazz);
    if (originalRequest == null || callback == null || wrapperCreator == null) {
        return;
    }

    final AbstractSpan<?> parent = tracer.getActive();

    Request request = originalRequest;
    URL url = request.url();
    span = HttpClientHelper.startHttpClientSpan(parent, request.method(), url.toString(), url.getProtocol(),
        OkHttpClientHelper.computeHostName(url.getHost()), url.getPort());
    if (span != null) {
        span.activate();
        if (headerSetterHelperManager != null) {
            TextHeaderSetter<Request.Builder> headerSetter = headerSetterHelperManager.getForClassLoaderOfClass(Request.class);
            if (headerSetter != null) {
                Request.Builder builder = originalRequest.newBuilder();
                span.propagateTraceContext(builder, headerSetter);
                originalRequest = builder.build();
            }
        }
        callback = wrapperCreator.wrap(callback, span);
    }
}
 
Example 3
Source File: OkHttpClientRequestAdaptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public String getDestinationId(Request request) {
    final URL httpUrl = request.url();
    if (httpUrl == null || httpUrl.getHost() == null) {
        return "Unknown";
    }
    final int port = EndPointUtils.getPort(httpUrl.getPort(), httpUrl.getDefaultPort());
    return HostAndPort.toHostAndPortString(httpUrl.getHost(), port);
}
 
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);
  }
}
 
Example 5
Source File: HttpURLConnectionImpl.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * Aggressively tries to get the final HTTP response, potentially making
 * many HTTP requests in the process in order to cope with redirects and
 * authentication.
 */
private HttpEngine getResponse() throws IOException {
  initHttpEngine();

  if (httpEngine.hasResponse()) {
    return httpEngine;
  }

  while (true) {
    if (!execute(true)) {
      continue;
    }

    Response response = httpEngine.getResponse();
    Request followUp = httpEngine.followUpRequest();

    if (followUp == null) {
      httpEngine.releaseConnection();
      return httpEngine;
    }

    if (++followUpCount > HttpEngine.MAX_FOLLOW_UPS) {
      throw new ProtocolException("Too many follow-up requests: " + followUpCount);
    }

    // The first request was insufficient. Prepare for another...
    url = followUp.url();
    requestHeaders = followUp.headers().newBuilder();

    // Although RFC 2616 10.3.2 specifies that a HTTP_MOVED_PERM redirect
    // should keep the same method, Chrome, Firefox and the RI all issue GETs
    // when following any redirect.
    Sink requestBody = httpEngine.getRequestBody();
    if (!followUp.method().equals(method)) {
      requestBody = null;
    }

    if (requestBody != null && !(requestBody instanceof RetryableSink)) {
      throw new HttpRetryException("Cannot retry streamed HTTP body", responseCode);
    }

    if (!httpEngine.sameConnection(followUp.url())) {
      httpEngine.releaseConnection();
    }

    Connection connection = httpEngine.close();
    httpEngine = newHttpEngine(followUp.method(), connection, (RetryableSink) requestBody,
        response);
  }
}