Java Code Examples for org.apache.http.client.methods.HttpUriRequest#getHeaders()

The following examples show how to use org.apache.http.client.methods.HttpUriRequest#getHeaders() . 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: GatewayCorsEnabledTest.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Test
void givenCorsIsEnabled_whenRequestWithOriginComes_thenOriginIsntPassedToSouthbound() throws Exception {
    // There is request to the southbound server and the CORS headers are properly set on the response
    mockValid200HttpResponseWithAddedCors();
    applicationRegistry.setCurrentApplication(serviceWithCustomConfiguration.getId());
    discoveryClient.createRefreshCacheEvent();

    // Simple request
    given()
        .header(new Header("Origin", "https://foo.bar.org"))
    .when()
        .get(basePath + serviceWithCustomConfiguration.getPath())
    .then()
        .statusCode(is(SC_OK))
        .header("Access-Control-Allow-Origin", is("https://foo.bar.org"));

    // The actual request is passed to the southbound service
    verify(mockClient, times(1)).execute(ArgumentMatchers.any(HttpUriRequest.class));

    ArgumentCaptor<HttpUriRequest> captor = ArgumentCaptor.forClass(HttpUriRequest.class);
    verify(mockClient, times(1)).execute(captor.capture());

    HttpUriRequest toVerify = captor.getValue();
    org.apache.http.Header[] originHeaders = toVerify.getHeaders("Origin");
    assertThat(originHeaders, arrayWithSize(0));
}
 
Example 2
Source File: HTTPUtils.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Returns value of a given header from HTTP request
 *
 * @param headerKey name of header to get value for
 * @param request HttpUriRequest
 * @return value of header as a String
 */
public static String getHeaderValue(String headerKey, HttpUriRequest request) {
    String headerVal = null;
    Header[] encodingHeader = request.getHeaders(headerKey);
    if(encodingHeader != null && encodingHeader.length > 0) {
        HeaderElement[] headerElements = encodingHeader[0].getElements();
        headerVal =  headerElements[0].getValue();
    }
    return headerVal;
}
 
Example 3
Source File: HTTPUtils.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Returns value of a given header from HTTP request
 *
 * @param headerKey name of header to get value for
 * @param request HttpUriRequest
 * @return value of header as a String
 */
public static String getHeaderValue(String headerKey, HttpUriRequest request) {
    String headerVal = null;
    Header[] encodingHeader = request.getHeaders(headerKey);
    if(encodingHeader != null && encodingHeader.length > 0) {
        HeaderElement[] headerElements = encodingHeader[0].getElements();
        headerVal =  headerElements[0].getValue();
    }
    return headerVal;
}
 
Example 4
Source File: NiFiRequestUtil.java    From knox with Apache License 2.0 4 votes vote down vote up
static HttpUriRequest modifyOutboundRequest(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest) throws IOException {
  // preserve trailing slash from inbound request in the outbound request
  if (inboundRequest.getPathInfo().endsWith("/")) {
    String[] split = outboundRequest.getURI().toString().split("\\?");
    if (!split[0].endsWith("/")) {
      outboundRequest = RequestBuilder.copy(outboundRequest).setUri(split[0] + "/" + (split.length == 2 ? "?" + split[1] : "")).build();
    }
  }
  // update the X-Forwarded-Context header to include the Knox-specific context path
  final Header originalXForwardedContextHeader = outboundRequest.getFirstHeader(NiFiHeaders.X_FORWARDED_CONTEXT);
  if (originalXForwardedContextHeader != null) {
    String xForwardedContextHeaderValue = originalXForwardedContextHeader.getValue();
    if (xForwardedContextHeaderValue != null && !xForwardedContextHeaderValue.isEmpty()) {
      // Inspect the inbound request and outbound request to determine the additional context path from the rewrite
      // rules that needs to be added to the X-Forwarded-Context header to allow proper proxying to NiFi.
      //
      // NiFi does its own URL rewriting, and will not work with the context path provided by Knox
      // (ie, "/gateway/sandbox").
      //
      // For example, if Knox has a rewrite rule "*://*:*/**/nifi-app/{**}?{**}", "/nifi-app" needs to be added
      // to the existing value of the X-Forwarded-Context header, which ends up being "/gateway/sandbox/nifi-app".
      String inboundRequestPathInfo = inboundRequest.getPathInfo();
      String outboundRequestUriPath = outboundRequest.getURI().getPath();
      String outboundRequestUriPathNoTrailingSlash = StringUtils.removeEnd(outboundRequestUriPath, "/");
      String knoxRouteContext = null;
      int index = inboundRequestPathInfo.lastIndexOf(outboundRequestUriPathNoTrailingSlash);
      if (index >= 0) {
        knoxRouteContext = inboundRequestPathInfo.substring(0, index);
      } else {
        Logger.getLogger(NiFiRequestUtil.class.getName()).error(String.format(Locale.ROOT, "Unable to find index of %s in %s", outboundRequestUriPathNoTrailingSlash, inboundRequestPathInfo));
      }
      outboundRequest.setHeader(NiFiHeaders.X_FORWARDED_CONTEXT, xForwardedContextHeaderValue + knoxRouteContext);
    }
  }

  // NiFi requires the header "X-ProxiedEntitiesChain" to be set with the identity or identities of the authenticated requester.
  // The effective principal (identity) in the requester subject must be added to "X-ProxiedEntitiesChain".
  // If the request already has a populated "X-ProxiedEntitiesChain" header, the identities must be appended to it.
  // If the user proxied through Knox is anonymous, the "Anonymous" identity needs to be represented in X-ProxiedEntitiesChain
  // as empty angle brackets "<>".
  final Subject subject = SubjectUtils.getCurrentSubject();
  String effectivePrincipalName = SubjectUtils.getEffectivePrincipalName(subject);
  String proxiedEntitesChainHeader = inboundRequest.getHeader(NiFiHeaders.X_PROXIED_ENTITIES_CHAIN);
  if(proxiedEntitesChainHeader == null) {
    proxiedEntitesChainHeader = "";
  }
  outboundRequest.setHeader(NiFiHeaders.X_PROXIED_ENTITIES_CHAIN, proxiedEntitesChainHeader +
      String.format(Locale.ROOT, "<%s>", "anonymous".equalsIgnoreCase(effectivePrincipalName) ? "" : effectivePrincipalName));

  // Make sure headers named "Cookie" are removed from the request to NiFi, since NiFi does not use cookies.
  Header[] cookieHeaders = outboundRequest.getHeaders("Cookie");
  for (Header cookieHeader : cookieHeaders) {
    outboundRequest.removeHeader(cookieHeader);
  }
  return outboundRequest;
}