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

The following examples show how to use org.apache.http.client.methods.HttpUriRequest#getAllHeaders() . 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: ConfigurableDispatchTest.java    From knox with Apache License 2.0 6 votes vote down vote up
@Test( timeout = TestUtils.SHORT_TIMEOUT )
public void testRequestExcludeHeadersDefault() {
  ConfigurableDispatch dispatch = new ConfigurableDispatch();

  Map<String, String> headers = new HashMap<>();
  headers.put(HttpHeaders.AUTHORIZATION, "Basic ...");
  headers.put(HttpHeaders.ACCEPT, "abc");
  headers.put("TEST", "test");

  HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
  EasyMock.expect(inboundRequest.getHeaderNames()).andReturn(Collections.enumeration(headers.keySet())).anyTimes();
  Capture<String> capturedArgument = Capture.newInstance();
  EasyMock.expect(inboundRequest.getHeader(EasyMock.capture(capturedArgument)))
      .andAnswer(() -> headers.get(capturedArgument.getValue())).anyTimes();
  EasyMock.replay(inboundRequest);

  HttpUriRequest outboundRequest = new HttpGet();
  dispatch.copyRequestHeaderFields(outboundRequest, inboundRequest);

  Header[] outboundRequestHeaders = outboundRequest.getAllHeaders();
  assertThat(outboundRequestHeaders.length, is(2));
  assertThat(outboundRequestHeaders[0].getName(), is(HttpHeaders.ACCEPT));
  assertThat(outboundRequestHeaders[1].getName(), is("TEST"));
}
 
Example 2
Source File: ConfigurableDispatchTest.java    From knox with Apache License 2.0 6 votes vote down vote up
@Test( timeout = TestUtils.SHORT_TIMEOUT )
public void testRequestExcludeHeadersConfig() {
  ConfigurableDispatch dispatch = new ConfigurableDispatch();
  dispatch.setRequestExcludeHeaders(String.join(",", Arrays.asList(HttpHeaders.ACCEPT, "TEST")));

  Map<String, String> headers = new HashMap<>();
  headers.put(HttpHeaders.AUTHORIZATION, "Basic ...");
  headers.put(HttpHeaders.ACCEPT, "abc");
  headers.put("TEST", "test");

  HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
  EasyMock.expect(inboundRequest.getHeaderNames()).andReturn(Collections.enumeration(headers.keySet())).anyTimes();
  Capture<String> capturedArgument = Capture.newInstance();
  EasyMock.expect(inboundRequest.getHeader(EasyMock.capture(capturedArgument)))
      .andAnswer(() -> headers.get(capturedArgument.getValue())).anyTimes();
  EasyMock.replay(inboundRequest);

  HttpUriRequest outboundRequest = new HttpGet();
  dispatch.copyRequestHeaderFields(outboundRequest, inboundRequest);

  Header[] outboundRequestHeaders = outboundRequest.getAllHeaders();
  assertThat(outboundRequestHeaders.length, is(1));
  assertThat(outboundRequestHeaders[0].getName(), is(HttpHeaders.AUTHORIZATION));
}
 
Example 3
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private void log(HttpUriRequest request) {
  if (logger != null) {
    List<String> headers = new ArrayList<>();
    for (Header h : request.getAllHeaders()) {
      headers.add(h.toString());
    }
    logger.logRequest(request.getMethod(), request.getURI().toString(), headers, null);
  }    
}
 
Example 4
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private void log(HttpUriRequest request) {
  if (logger != null) {
    List<String> headers = new ArrayList<>();
    for (Header h : request.getAllHeaders()) {
      headers.add(h.toString());
    }
    logger.logRequest(request.getMethod(), request.getURI().toString(), headers, null);
  }    
}
 
Example 5
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private void log(HttpUriRequest request) {
  if (logger != null) {
    List<String> headers = new ArrayList<>();
    for (Header h : request.getAllHeaders()) {
      headers.add(h.toString());
    }
    logger.logRequest(request.getMethod(), request.getURI().toString(), headers, null);
  }    
}
 
Example 6
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private void log(HttpUriRequest request) {
  if (logger != null) {
    List<String> headers = new ArrayList<>();
    for (Header h : request.getAllHeaders()) {
      headers.add(h.toString());
    }
    logger.logRequest(request.getMethod(), request.getURI().toString(), headers, null);
  }    
}
 
Example 7
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private void log(HttpUriRequest request) {
  if (logger != null) {
    List<String> headers = new ArrayList<>();
    for (Header h : request.getAllHeaders()) {
      headers.add(h.toString());
    }
    logger.logRequest(request.getMethod(), request.getURI().toString(), headers, null);
  }    
}
 
Example 8
Source File: ImmersionHttpClient.java    From letv with Apache License 2.0 5 votes vote down vote up
private HttpResponse b0449щ0449щщ0449(HttpUriRequest httpUriRequest, Map map, int i) throws Exception {
    URI uri = httpUriRequest.getURI();
    String trim = uri.getHost() != null ? uri.getHost().trim() : "";
    if (trim.length() > 0) {
        httpUriRequest.setHeader("Host", trim);
    }
    if (map != null) {
        for (Object next : map.entrySet()) {
            if (((b04170417041704170417З + b0417ЗЗЗЗ0417) * b04170417041704170417З) % bЗ0417ЗЗЗ0417 != bЗЗЗЗЗ0417) {
                b04170417041704170417З = 81;
                bЗЗЗЗЗ0417 = 31;
            }
            Entry entry = (Entry) next;
            httpUriRequest.setHeader((String) entry.getKey(), (String) entry.getValue());
        }
    }
    Header[] allHeaders = httpUriRequest.getAllHeaders();
    Log.d(b043D043Dнн043Dн, "request URI [" + httpUriRequest.getURI() + "]");
    for (Object obj : allHeaders) {
        Log.d(b043D043Dнн043Dн, "request header [" + obj.toString() + "]");
    }
    HttpConnectionParams.setSoTimeout(this.bнн043Dн043Dн.getParams(), i);
    HttpResponse execute = this.bнн043Dн043Dн.execute(httpUriRequest);
    if (execute != null) {
        return execute;
    }
    throw new RuntimeException("Null response returned.");
}
 
Example 9
Source File: ClientLoginHelper.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private static void removeGoogleAuthHeaders(HttpUriRequest request) {
  for (Header header : request.getAllHeaders()) {
    if (header.getName().equalsIgnoreCase("Authorization") &&
            header.getValue().startsWith(AUTHORIZATION_HEADER_PREFIX)) {
      Log.i(LOG_TAG, "Removing header:" + header);
      request.removeHeader(header);
    }
  }
}
 
Example 10
Source File: DcRestAdapter.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * デバッグ用.
 * @param req デバッグ出力するRequestオブジェクト
 * @param body デバッグ出力するリクエストボディ
 */
private void debugHttpRequest(final HttpUriRequest req, final String body) {
    log.debug(req.getURI());
    if (log.isDebugEnabled()) {
        log.debug("【Request】 " + req.getMethod() + "  " + req.getURI());
        Header[] headers = req.getAllHeaders();
        for (int i = 0; i < headers.length; i++) {
            log.debug("RequestHeader[" + headers[i].getName() + "] : " + headers[i].getValue());
        }
        log.debug("RequestBody:  " + body);
    }
}
 
Example 11
Source File: HttpTestUtils.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
public static void assertEqual(RequestBuilder actual, RequestBuilder expect)
    throws IOException {
  // Check entity
  HttpEntity actualEntity = actual.getEntity();
  HttpEntity expectedEntity = expect.getEntity();
  if (actualEntity == null) {
    Assert.assertTrue(expectedEntity == null);
  } else {
    Assert.assertEquals(actualEntity.getContentLength(), expectedEntity.getContentLength());
    String actualContent = IOUtils.toString(actualEntity.getContent(), StandardCharsets.UTF_8);
    String expectedContent = IOUtils.toString(expectedEntity.getContent(), StandardCharsets.UTF_8);
    Assert.assertEquals(actualContent, expectedContent);
  }

  // Check request
  HttpUriRequest actualRequest = actual.build();
  HttpUriRequest expectedRequest = expect.build();
  Assert.assertEquals(actualRequest.getMethod(), expectedRequest.getMethod());
  Assert.assertEquals(actualRequest.getURI().toString(), expectedRequest.getURI().toString());

  Header[] actualHeaders = actualRequest.getAllHeaders();
  Header[] expectedHeaders = expectedRequest.getAllHeaders();
  Assert.assertEquals(actualHeaders.length, expectedHeaders.length);
  for (int i = 0; i < actualHeaders.length; i++) {
    Assert.assertEquals(actualHeaders[i].toString(), expectedHeaders[i].toString());
  }
}
 
Example 12
Source File: HttpRequestParcel.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public HttpRequestParcel(HttpUriRequest request) {
    RequestLine line = request.getRequestLine();
    uri = line.getUri();
    method = line.getMethod();
    headers = new Bundle();
    for (Header header : request.getAllHeaders()) {
        headers.putString(header.getName(), header.getValue());
    }
    params = new Bundle();
    entity = null;
    files = new Bundle();
}
 
Example 13
Source File: AndroidHttpClient.java    From travelguide with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a cURL command equivalent to the given request.
 */
private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
    StringBuilder builder = new StringBuilder();

    builder.append("curl ");

    for (Header header: request.getAllHeaders()) {
        if (!logAuthToken
                && (header.getName().equals("Authorization") ||
                    header.getName().equals("Cookie"))) {
            continue;
        }
        builder.append("--header \"");
        builder.append(header.toString().trim());
        builder.append("\" ");
    }

    URI uri = request.getURI();

    // If this is a wrapped request, use the URI from the original
    // request instead. getURI() on the wrapper seems to return a
    // relative URI. We want an absolute URI.
    if (request instanceof RequestWrapper) {
        HttpRequest original = ((RequestWrapper) request).getOriginal();
        if (original instanceof HttpUriRequest) {
            uri = ((HttpUriRequest) original).getURI();
        }
    }

    builder.append("\"");
    builder.append(uri);
    builder.append("\"");

    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityRequest =
                (HttpEntityEnclosingRequest) request;
        HttpEntity entity = entityRequest.getEntity();
        if (entity != null && entity.isRepeatable()) {
            if (entity.getContentLength() < 1024) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                entity.writeTo(stream);
                String entityString = stream.toString();

                // TODO: Check the content type, too.
                builder.append(" --data-ascii \"")
                        .append(entityString)
                        .append("\"");
            } else {
                builder.append(" [TOO MUCH DATA TO INCLUDE]");
            }
        }
    }

    return builder.toString();
}
 
Example 14
Source File: AndroidHttpClient.java    From Alite with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Generates a cURL command equivalent to the given request.
 */
private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
    StringBuilder builder = new StringBuilder();

    builder.append("curl ");

    for (Header header: request.getAllHeaders()) {
        if (!logAuthToken
                && (header.getName().equals("Authorization") ||
                    header.getName().equals("Cookie"))) {
            continue;
        }
        builder.append("--header \"");
        builder.append(header.toString().trim());
        builder.append("\" ");
    }

    URI uri = request.getURI();

    // If this is a wrapped request, use the URI from the original
    // request instead. getURI() on the wrapper seems to return a
    // relative URI. We want an absolute URI.
    if (request instanceof RequestWrapper) {
        HttpRequest original = ((RequestWrapper) request).getOriginal();
        if (original instanceof HttpUriRequest) {
            uri = ((HttpUriRequest) original).getURI();
        }
    }

    builder.append("\"");
    builder.append(uri);
    builder.append("\"");

    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityRequest =
                (HttpEntityEnclosingRequest) request;
        HttpEntity entity = entityRequest.getEntity();
        if (entity != null && entity.isRepeatable()) {
            if (entity.getContentLength() < 1024) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                entity.writeTo(stream);
                String entityString = stream.toString();

                // TODO: Check the content type, too.
                builder.append(" --data-ascii \"")
                        .append(entityString)
                        .append("\"");
            } else {
                builder.append(" [TOO MUCH DATA TO INCLUDE]");
            }
        }
    }

    return builder.toString();
}
 
Example 15
Source File: AndroidHttpClient.java    From android-download-manager with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a cURL command equivalent to the given request.
 */
private static String toCurl(HttpUriRequest request, boolean logAuthToken)
		throws IOException {
	StringBuilder builder = new StringBuilder();

	builder.append("curl ");

	for (Header header : request.getAllHeaders()) {
		if (!logAuthToken
				&& (header.getName().equals("Authorization") || header
						.getName().equals("Cookie"))) {
			continue;
		}
		builder.append("--header \"");
		builder.append(header.toString().trim());
		builder.append("\" ");
	}

	URI uri = request.getURI();

	// If this is a wrapped request, use the URI from the original
	// request instead. getURI() on the wrapper seems to return a
	// relative URI. We want an absolute URI.
	if (request instanceof RequestWrapper) {
		HttpRequest original = ((RequestWrapper) request).getOriginal();
		if (original instanceof HttpUriRequest) {
			uri = ((HttpUriRequest) original).getURI();
		}
	}

	builder.append("\"");
	builder.append(uri);
	builder.append("\"");

	if (request instanceof HttpEntityEnclosingRequest) {
		HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
		HttpEntity entity = entityRequest.getEntity();
		if (entity != null && entity.isRepeatable()) {
			if (entity.getContentLength() < 1024) {
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				entity.writeTo(stream);
				String entityString = stream.toString();

				// TODO: Check the content type, too.
				builder.append(" --data-ascii \"").append(entityString)
						.append("\"");
			} else {
				builder.append(" [TOO MUCH DATA TO INCLUDE]");
			}
		}
	}

	return builder.toString();
}