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

The following examples show how to use org.apache.http.HttpRequest#getFirstHeader() . 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: TestWebServer.java    From curly with Apache License 2.0 6 votes vote down vote up
void handleHttpRequest(HttpRequest request, HttpResponse response, HttpContext context) throws UnsupportedEncodingException {
    lastRequest = request;
    try {
        if (requireLogin && request.getFirstHeader("Authorization") == null) {
            response.setStatusCode(401);
            response.setHeader("WWW-Authenticate", "Basic realm=\"test\"");
        } else {
            Thread.sleep(processingDelay);
            if (request.getRequestLine().getUri().contains("failure")) {
                response.setStatusCode(403);
            } else {
                response.setEntity(new StringEntity(responseMessage));
            }
        }
    } catch (InterruptedException ex) {
        Logger.getLogger(TestWebServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
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: RequestProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 6 votes vote down vote up
private RequestProtocolError requestHasWeakETagAndRange(HttpRequest request) {
	// TODO: Should these be looking at all the headers marked as Range?
	String method = request.getRequestLine().getMethod();
	if (!(HeaderConstants.GET_METHOD.equals(method))) {
		return null;
	}

	Header range = request.getFirstHeader(HeaderConstants.RANGE);
	if (range == null)
		return null;

	Header ifRange = request.getFirstHeader(HeaderConstants.IF_RANGE);
	if (ifRange == null)
		return null;

	String val = ifRange.getValue();
	if (val.startsWith("W/")) {
		return RequestProtocolError.WEAK_ETAG_AND_RANGE_ERROR;
	}

	return null;
}
 
Example 4
Source File: RequestProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 6 votes vote down vote up
private void decrementOPTIONSMaxForwardsIfGreaterThen0(HttpRequest request) {
	if (!HeaderConstants.OPTIONS_METHOD.equals(request.getRequestLine()
			.getMethod())) {
		return;
	}

	Header maxForwards = request
			.getFirstHeader(HeaderConstants.MAX_FORWARDS);
	if (maxForwards == null) {
		return;
	}

	request.removeHeaders(HeaderConstants.MAX_FORWARDS);
	int currentMaxForwards = Integer.parseInt(maxForwards.getValue());

	request.setHeader(HeaderConstants.MAX_FORWARDS,
			Integer.toString(currentMaxForwards - 1));
}
 
Example 5
Source File: HttpRequestHelper.java    From esigate with Apache License 2.0 5 votes vote down vote up
public static String getFirstHeader(String name, HttpRequest request) {
    final Header header = request.getFirstHeader(name);
    String headerValue = null;
    if (header != null) {
        headerValue = header.getValue();
    }
    return headerValue;
}
 
Example 6
Source File: IsCreateChannelHttpRequest.java    From joynr with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(Object argument) {

    HttpRequest request = (HttpRequest) argument;

    // check if tracking ID is sent in header
    Header trackingIdHeader = request.getFirstHeader("X-Atmosphere-tracking-id");
    if (trackingIdHeader == null) {
        // no tracking ID header set at all
        return false;
    } else {
        if (!trackingIdHeader.getValue().equals(trackingId)) {
            // wrong tracking ID header set
            return false;
        }
    }

    // check if channel ID is sent as query parameter ccid
    List<NameValuePair> queryParameters = URLEncodedUtils.parse(URI.create(request.getRequestLine().getUri()),
                                                                "UTF-8");
    for (NameValuePair queryParameter : queryParameters) {
        if (queryParameter.getName().equals("ccid") && queryParameter.getValue().equals(ccid)) {
            // right channel ID sent
            return true;
        } else {
            // wrong channel ID sent
            return false;
        }
    }
    // no query parameter with ccid sent at all
    return false;
}
 
Example 7
Source File: ResponseProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
private void ensurePartialContentIsNotSentToAClientThatDidNotRequestIt(
		HttpRequest request, HttpResponse response)
		throws ClientProtocolException {
	if (request.getFirstHeader(HeaderConstants.RANGE) != null)
		return;

	if (response.getFirstHeader(HeaderConstants.CONTENT_RANGE) != null) {
		throw new ClientProtocolException(
				"Content-Range was returned for a request that did not ask for a Content-Range.");
	}

}
 
Example 8
Source File: RequestProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
private RequestProtocolError requestHasWeekETagForPUTOrDELETEIfMatch(
		HttpRequest request) {
	// TODO: Should these be looking at all the headers marked as
	// If-Match/If-None-Match?

	String method = request.getRequestLine().getMethod();
	if (!(HeaderConstants.PUT_METHOD.equals(method) || HeaderConstants.DELETE_METHOD
			.equals(method))) {
		return null;
	}

	Header ifMatch = request.getFirstHeader(HeaderConstants.IF_MATCH);
	if (ifMatch != null) {
		String val = ifMatch.getValue();
		if (val.startsWith("W/")) {
			return RequestProtocolError.WEAK_ETAG_ON_PUTDELETE_METHOD_ERROR;
		}
	} else {
		Header ifNoneMatch = request
				.getFirstHeader(HeaderConstants.IF_NONE_MATCH);
		if (ifNoneMatch == null)
			return null;

		String val2 = ifNoneMatch.getValue();
		if (val2.startsWith("W/")) {
			return RequestProtocolError.WEAK_ETAG_ON_PUTDELETE_METHOD_ERROR;
		}
	}

	return null;
}
 
Example 9
Source File: AwsSigner4Request.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private static String getSigningDateTime(HttpRequest request, Date signingTime) {
    Header dateHeader = request.getFirstHeader("X-Amz-Date");
    if (dateHeader != null) {
        return dateHeader.getValue();
    }
    final SimpleDateFormat timeFormat= new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
    timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    return timeFormat.format(signingTime);
}
 
Example 10
Source File: UserAgentHttpRequestInterceptor.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Override
public void process(final HttpRequest request, final HttpContext context) {
    Header existingUserAgent = request.getFirstHeader(HttpHeaders.USER_AGENT);
    String userAgent = String.format("gerrit-rest-java-client/%s using %s",
        Version.get(), existingUserAgent.getValue());
    request.setHeader(HttpHeaders.USER_AGENT, userAgent);
}
 
Example 11
Source File: AwsSigner4Request.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
private static String getSigningDateTime(HttpRequest request, Date signingTime) {
    Header dateHeader = request.getFirstHeader("X-Amz-Date");
    if (dateHeader != null) {
        return dateHeader.getValue();
    }
    synchronized (TIME_FORMAT) {
        return TIME_FORMAT.format(signingTime);
    }
}
 
Example 12
Source File: ApacheHttpClientTagAdapter.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Override
public @Nullable String getHeaderSingleValue(@Nullable HttpRequest request, @NotNull String headerKey) {
    if (request == null) {
        return null;
    }

    Header matchingHeader = request.getFirstHeader(headerKey);

    if (matchingHeader == null) {
        return null;
    }

    return matchingHeader.getValue();
}
 
Example 13
Source File: HttpBasicPassTicketScheme.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void applyToRequest(HttpRequest request) {
    request.setHeader(
        new BasicHeader(HttpHeaders.AUTHORIZATION, authorizationValue)
    );
    Header header = request.getFirstHeader(COOKIE_HEADER);
    if (header != null) {
        request.setHeader(COOKIE_HEADER,
            CookieUtil.removeCookie(
                header.getValue(),
                cookieName
            )
        );
    }
}
 
Example 14
Source File: RequestHeaderAccessor.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public String getFirstHeader(String headerName, HttpRequest request) {
    Header header = request.getFirstHeader(headerName);
    if (header != null) {
        return header.getValue();
    }
    return null;
}
 
Example 15
Source File: CachedResponseSuitabilityChecker.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
private boolean hasUnsupportedConditionalHeaders(HttpRequest request) {
	return (request.getFirstHeader("If-Range") != null
			|| request.getFirstHeader("If-Match") != null || hasValidDateField(
			request, "If-Unmodified-Since"));
}
 
Example 16
Source File: HttpDownHandler.java    From AndroidWebServ with Apache License 2.0 2 votes vote down vote up
/**
 * 判断请求是否接受GBK
 * @param request Http请求
 * @return 接受与否
 */
private boolean isGBKAccepted(HttpRequest request) {
    Header header = request.getFirstHeader("Accept-Charset");
    return ((header != null) && header.getValue().toLowerCase().indexOf("gbk") != -1);
}
 
Example 17
Source File: GzipUtil.java    From AndroidWebServ with Apache License 2.0 2 votes vote down vote up
/**
 * 判断请求是否支持Gzip
 * @param request Http请求
 * @return 支持与否
 */
public boolean isGZipSupported(HttpRequest request) {
    Header header = request.getFirstHeader("Accept-Encoding");
    return ((header != null) && header.getValue().toLowerCase().indexOf("gzip") != -1);
}