Java Code Examples for org.apache.http.HttpRequest#getRequestLine()

The following examples show how to use org.apache.http.HttpRequest#getRequestLine() . 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: AopHttpClient.java    From ArgusAPM with Apache License 2.0 6 votes vote down vote up
private static HttpRequest handleRequest(HttpHost host, HttpRequest request, NetInfo data) {
    RequestLine requestLine = request.getRequestLine();
    if (requestLine != null) {
        String uri = requestLine.getUri();
        int i = (uri != null) && (uri.length() >= 10) && (uri.substring(0, 10).indexOf("://") >= 0) ? 1 : 0;
        if ((i == 0) && (uri != null) && (host != null)) {
            String uriFromHost = host.toURI().toString();
            data.setURL(uriFromHost + ((uriFromHost.endsWith("/")) || (uri.startsWith("/")) ? "" : "/") + uri);
        } else if (i != 0) {
            data.setURL(uri);
        }
    }
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
        if (entityRequest.getEntity() != null) {
            entityRequest.setEntity(new AopHttpRequestEntity(entityRequest.getEntity(), data));
        }
        return entityRequest;
    }
    return request;
}
 
Example 2
Source File: InstrHttpClientBuilderProvider.java    From knox with Apache License 2.0 6 votes vote down vote up
@Override
public String getNameFor(String name, HttpRequest request) {
  try {
    String context = "";
    Header header = request.getFirstHeader("X-Forwarded-Context");
    if (header != null) {
      context = header.getValue();
    }
    RequestLine requestLine = request.getRequestLine();
    URIBuilder uriBuilder = new URIBuilder(requestLine.getUri());
    String resourcePath = InstrUtils.getResourcePath(uriBuilder.removeQuery().build().toString());
    return MetricRegistry.name("service", name, context + resourcePath, methodNameString(request));
  } catch (URISyntaxException e) {
    throw new IllegalArgumentException(e);
  }
}
 
Example 3
Source File: ApacheHttpClientAspect.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@OnBefore
public static @Nullable TraceEntry onBefore(ThreadContext context,
        @BindParameter @Nullable HttpHost hostObj,
        @BindParameter @Nullable HttpRequest request) {
    if (request == null) {
        return null;
    }
    RequestLine requestLine = request.getRequestLine();
    if (requestLine == null) {
        return null;
    }
    String method = requestLine.getMethod();
    if (method == null) {
        method = "";
    } else {
        method += " ";
    }
    String host = hostObj == null ? "" : hostObj.toURI();
    String uri = requestLine.getUri();
    if (uri == null) {
        uri = "";
    }
    return context.startServiceCallEntry("HTTP", method + Uris.stripQueryString(uri),
            MessageSupplier.create("http client request: {}{}{}", method, host, uri),
            timerName);
}
 
Example 4
Source File: TestHttpHandler.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
	RequestLine requestLine = request.getRequestLine();
	this.method = requestLine.getMethod();
	this.uri = requestLine.getUri();

	HttpEntity entity = null;
	if (request instanceof HttpEntityEnclosingRequest) {
		entity = ((HttpEntityEnclosingRequest) request).getEntity();
		body = EntityUtils.toString(entity, StandardCharsets.UTF_8);
	}
}
 
Example 5
Source File: HttpClientInterceptor.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Override
public void process(HttpRequest request, HttpContext context)
{
    byte[] body = null;
    String mimeType = null;
    if (request instanceof HttpEntityEnclosingRequest)
    {
        HttpEntityEnclosingRequest requestWithBody = (HttpEntityEnclosingRequest) request;
        HttpEntity entity = requestWithBody.getEntity();
        if (entity != null)
        {
            mimeType = Optional.ofNullable(ContentType.getLenient(entity))
                    .map(ContentType::getMimeType)
                    .orElseGet(() -> getMimeType(requestWithBody.getAllHeaders()));
            try (ByteArrayOutputStream baos = new ByteArrayOutputStream((int) entity.getContentLength()))
            {
                // https://github.com/apache/httpcomponents-client/commit/09cefc2b8970eea56d81b1a886d9bb769a48daf3
                entity.writeTo(baos);
                body = baos.toByteArray();
            }
            catch (IOException e)
            {
                LOGGER.error("Error is occurred at HTTP message parsing", e);
            }
        }
    }
    RequestLine requestLine = request.getRequestLine();
    String attachmentTitle = String.format("Request: %s %s", requestLine.getMethod(), requestLine.getUri());
    attachApiMessage(attachmentTitle, request.getAllHeaders(), body, mimeType, -1);
}
 
Example 6
Source File: HttpAsyncRequestProducerWrapper.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Override
public HttpRequest generateRequest() throws IOException, HttpException {
    // first read the volatile, span will become visible as a result
    HttpRequest request = delegate.generateRequest();

    if (request != null) {
        RequestLine requestLine = request.getRequestLine();
        if (requestLine != null) {
            String method = requestLine.getMethod();
            span.withName(method).appendToName(" ");
            span.getContext().getHttp().withMethod(method).withUrl(requestLine.getUri());
        }
        span.propagateTraceContext(request, headerSetter);
    }

    HttpHost host = getTarget();
    //noinspection ConstantConditions
    if (host != null) {
        String hostname = host.getHostName();
        if (hostname != null) {
            span.appendToName(hostname);
            HttpClientHelper.setDestinationServiceDetails(span, host.getSchemeName(), hostname, host.getPort());
        }
    }

    //noinspection ConstantConditions
    return request;
}
 
Example 7
Source File: AbstractHttpRequestInterceptor.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
public void appendHttpClientRequestSpanTags(HttpRequest httpRequest,
                                            SofaTracerSpan httpClientSpan) {
    if (httpClientSpan == null) {
        return;
    }
    if (this.appName == null) {
        this.appName = SofaTracerConfiguration.getProperty(
            SofaTracerConfiguration.TRACER_APPNAME_KEY, StringUtils.EMPTY_STRING);
    }
    //lazy init
    RequestLine requestLine = httpRequest.getRequestLine();
    String methodName = requestLine.getMethod();
    //appName
    httpClientSpan.setTag(CommonSpanTags.LOCAL_APP,
        this.appName == null ? StringUtils.EMPTY_STRING : this.appName);
    //targetAppName
    httpClientSpan.setTag(CommonSpanTags.REMOTE_APP,
        this.targetAppName == null ? StringUtils.EMPTY_STRING : this.targetAppName);
    if (httpRequest instanceof HttpRequestWrapper) {
        HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) httpRequest;
        httpClientSpan.setTag(CommonSpanTags.REQUEST_URL, httpRequestWrapper.getOriginal()
            .getRequestLine().getUri());
    } else {
        httpClientSpan.setTag(CommonSpanTags.REQUEST_URL, requestLine.getUri());
    }
    //method
    httpClientSpan.setTag(CommonSpanTags.METHOD, methodName);
    //length
    if (httpRequest instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest;
        HttpEntity httpEntity = httpEntityEnclosingRequest.getEntity();
        httpClientSpan.setTag(CommonSpanTags.REQ_SIZE,
            httpEntity == null ? -1 : httpEntity.getContentLength());
    }
    //carrier
    this.processHttpClientRequestCarrier(httpRequest, httpClientSpan);
}
 
Example 8
Source File: HttpRequestWarcRecord.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
private HttpRequestWarcRecord(final HeaderGroup warcHeaders, final URI targetURI, final HttpRequest request) {
	super(targetURI, warcHeaders);
	getWarcTargetURI(); // Check correct initialization
	this.warcHeaders.updateHeader(Type.warcHeader(Type.REQUEST));
	this.warcHeaders.updateHeader(new WarcHeader(WarcHeader.Name.CONTENT_TYPE, HTTP_REQUEST_MSGTYPE));
	this.protocolVersion = request.getProtocolVersion();
	this.requestLine = request.getRequestLine();
	this.setHeaders(request.getAllHeaders());
}
 
Example 9
Source File: HttpConnector.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Builds a string from given request.
 * 
 * @param message
 * @return a string from given request.
 */
private String buildRequestLogString(HttpRequest message) {
	if (message == null) {
		return "<null>";
	}

	StringBuilder result = new StringBuilder();
	if (message.getRequestLine() != null) {
		result.append(message.getRequestLine().toString()).append("\n");
	}

	if (message.getAllHeaders() != null) {
		for (Header header : message.getAllHeaders()) {
			result.append(header.toString()).append("\n");
		}
	}

	if (message instanceof HttpEntityEnclosingRequest) {
		HttpEntity entity = ((HttpEntityEnclosingRequest) message).getEntity();
		if (entity != null) {
			if (entity.isRepeatable()) {
				try {
					ByteArrayOutputStream entityBytes = new ByteArrayOutputStream();
					entity.writeTo(entityBytes);
					result.append(new String(entityBytes.toByteArray(), entity.getContentEncoding() == null ? charsetToUse : entity.getContentEncoding().getValue()));
				} catch (IOException e) {
					// ignore
				}
			} else {
				result.append("\n<content not repeatable>\n");
			}
		}
	}

	return result.toString();
}
 
Example 10
Source File: ApacheHttpAsyncClientAspect.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@OnBefore
public static @Nullable AsyncTraceEntry onBefore(ThreadContext context,
        @BindParameter @Nullable HttpHost hostObj,
        @BindParameter @Nullable HttpRequest request,
        @BindParameter ParameterHolder<FutureCallback<HttpResponse>> callback) {
    if (request == null) {
        return null;
    }
    RequestLine requestLine = request.getRequestLine();
    if (requestLine == null) {
        return null;
    }
    String method = requestLine.getMethod();
    if (method == null) {
        method = "";
    } else {
        method += " ";
    }
    String host = hostObj == null ? "" : hostObj.toURI();
    String uri = requestLine.getUri();
    if (uri == null) {
        uri = "";
    }
    AsyncTraceEntry asyncTraceEntry = context.startAsyncServiceCallEntry("HTTP",
            method + Uris.stripQueryString(uri),
            MessageSupplier.create("http client request: {}{}{}", method, host, uri),
            timerName);
    callback.set(ExecuteAdvice.createWrapper(context, callback, asyncTraceEntry));
    return asyncTraceEntry;
}
 
Example 11
Source File: CachingHttpClient.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
boolean clientRequestsOurOptions(HttpRequest request) {
	RequestLine line = request.getRequestLine();

	if (!HeaderConstants.OPTIONS_METHOD.equals(line.getMethod()))
		return false;

	if (!"*".equals(line.getUri()))
		return false;

	if (!"0".equals(request.getFirstHeader(HeaderConstants.MAX_FORWARDS)
			.getValue()))
		return false;

	return true;
}
 
Example 12
Source File: WingtipsApacheHttpClientUtil.java    From wingtips with Apache License 2.0 3 votes vote down vote up
/**
 * Returns span name that should be used for the subspan surrounding the given request when {@link
 * com.nike.wingtips.tags.HttpTagAndSpanNamingStrategy#getInitialSpanName(Object, HttpTagAndSpanNamingAdapter)}
 * returns null and you need a fallback.
 *
 * <p>This method returns {@code apachehttpclient_downstream_call-[HTTP_METHOD]}, e.g. for a GET
 * call, this would return {@code "apachehttpclient_downstream_call-GET"}.
 *
 * @param request The request that is about to be executed.
 * @return The fallback span name that should be used for the subspan surrounding the request.
 */
public static String getFallbackSubspanSpanName(HttpRequest request) {
    RequestLine requestLine = request.getRequestLine();

    return HttpRequestTracingUtils.getFallbackSpanNameForHttpRequest(
        "apachehttpclient_downstream_call", requestLine.getMethod()
    );
}