Example usage for org.apache.http.client.methods HttpRequestBase getHeaders

List of usage examples for org.apache.http.client.methods HttpRequestBase getHeaders

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpRequestBase getHeaders.

Prototype

public Header[] getHeaders(String str) 

Source Link

Usage

From source file:ca.uhn.fhir.rest.client.apache.GZipContentInterceptor.java

@Override
public void interceptRequest(IHttpRequest theRequestInterface) {
    HttpRequestBase theRequest = ((ApacheHttpRequest) theRequestInterface).getApacheRequest();
    if (theRequest instanceof HttpEntityEnclosingRequest) {
        Header[] encodingHeaders = theRequest.getHeaders(Constants.HEADER_CONTENT_ENCODING);
        if (encodingHeaders == null || encodingHeaders.length == 0) {
            HttpEntityEnclosingRequest req = (HttpEntityEnclosingRequest) theRequest;

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            GZIPOutputStream gos;
            try {
                gos = new GZIPOutputStream(bos);
                req.getEntity().writeTo(gos);
                gos.finish();/*w  w w.  j a v  a  2s.  c  o m*/
            } catch (IOException e) {
                ourLog.warn("Failed to GZip outgoing content", e);
                return;
            }

            byte[] byteArray = bos.toByteArray();
            ByteArrayEntity newEntity = new ByteArrayEntity(byteArray);
            req.setEntity(newEntity);
            req.addHeader(Constants.HEADER_CONTENT_ENCODING, "gzip");
        }
    }

}

From source file:com.ksc.http.apache.client.impl.ApacheDefaultHttpRequestFactoryTest.java

@Test
public void apache_request_has_content_type_set_when_not_explicitly_set()
        throws IOException, URISyntaxException {

    final Request<Object> request = newDefaultRequest(HttpMethodName.POST);
    final String testContentype = "testContentType";
    request.addHeader(HttpHeaders.CONTENT_TYPE, testContentype);
    request.setContent(new StringInputStream("dummy string stream"));
    HttpRequestBase requestBase = requestFactory.create(request, settings);
    assertContentTypeContains(testContentype, requestBase.getHeaders(CONTENT_TYPE));

}

From source file:com.ksc.http.apache.client.impl.ApacheDefaultHttpRequestFactoryTest.java

@Test
public void request_has_default_content_type_set_when_not_explicitly_set()
        throws IOException, URISyntaxException {
    final Request<Object> request = newDefaultRequest(HttpMethodName.POST);
    request.setContent(new StringInputStream("dummy string stream"));
    HttpRequestBase requestBase = requestFactory.create(request, settings);
    assertContentTypeContains("application/x-www-form-urlencoded", requestBase.getHeaders(CONTENT_TYPE));
}

From source file:eionet.webq.xforms.XFormsHTTPRequestAuthHandlerImplTest.java

private void assertThatRequestIsNotChanged(HttpRequestBase httpRequest, String requestUri) {
    assertThat(httpRequest.getURI().toString(), startsWith(requestUri));
    assertThat(httpRequest.getHeaders("Authorization").length, equalTo(0));
}

From source file:com.gistlabs.mechanize.PageRequest.java

private void assertHeaders(final HttpRequestBase request) throws ArrayComparisonFailure {
    for (Header header : headers) {
        org.apache.http.Header[] requestHeaders = request.getHeaders(header.getName());
        boolean foundMatch = false;
        for (org.apache.http.Header requestHeader : requestHeaders)
            if (header.getValues().contains(requestHeader.getValue()))
                foundMatch = true;/*from  www .j  a  va2 s  .  c o  m*/
        if (!foundMatch)
            Assert.fail(String.format("Could not find request header matching: %s", header));
    }
    if (request instanceof HttpPost) {
        HttpPost post = (HttpPost) request;
        HttpEntity entity = post.getEntity();
        Parameters actualParameters = extractParameters(entity);

        String[] expectedNames = parameters.getNames();
        String[] actualNames = actualParameters.getNames();
        Arrays.sort(expectedNames);
        Arrays.sort(actualNames);
        Assert.assertArrayEquals("Expected and actual parameters should equal by available parameter names",
                expectedNames, actualNames);

        for (String name : expectedNames) {
            String[] expectedValue = parameters.get(name);
            String[] actualValue = actualParameters.get(name);
            Assert.assertArrayEquals("Expected parameter of next PageRequest '" + uri + "' must match",
                    expectedValue, actualValue);
        }
    }
}

From source file:com.aliyun.oss.common.comm.HttpRequestFactory.java

private void configureRequestHeaders(ServiceClient.Request request, ExecutionContext context,
        HttpRequestBase httpRequest) {
    for (Entry<String, String> entry : request.getHeaders().entrySet()) {
        if (entry.getKey().equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)
                || entry.getKey().equalsIgnoreCase(HttpHeaders.HOST)) {
            continue;
        }// w  w  w  .j a va  2  s .c  o m

        httpRequest.addHeader(entry.getKey(), entry.getValue());
    }

    if (!request.isUseUrlSignature() && (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null
            || httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE).length == 0)) {
        httpRequest.addHeader(HttpHeaders.CONTENT_TYPE,
                "application/x-www-form-urlencoded; " + "charset=" + context.getCharset().toLowerCase());
    }
}

From source file:com.android.exchange.EasSyncServiceTests.java

public void testAddHeaders() {
    HttpRequestBase method = new HttpPost();
    EasSyncService svc = new EasSyncService();
    svc.mAuthString = "auth";
    svc.mProtocolVersion = "12.1";
    svc.mDeviceType = "android";
    svc.mAccount = null;//from  ww  w . ja  v a 2  s . co  m
    // With second argument false, there should be no header
    svc.setHeaders(method, false);
    Header[] headers = method.getHeaders("X-MS-PolicyKey");
    assertEquals(0, headers.length);
    // With second argument true, there should always be a header
    // The value will be "0" without an account
    method.removeHeaders("X-MS-PolicyKey");
    svc.setHeaders(method, true);
    headers = method.getHeaders("X-MS-PolicyKey");
    assertEquals(1, headers.length);
    assertEquals("0", headers[0].getValue());
    // With an account, but null security key, the header's value should be "0"
    Account account = new Account();
    account.mSecuritySyncKey = null;
    svc.mAccount = account;
    method.removeHeaders("X-MS-PolicyKey");
    svc.setHeaders(method, true);
    headers = method.getHeaders("X-MS-PolicyKey");
    assertEquals(1, headers.length);
    assertEquals("0", headers[0].getValue());
    // With an account and security key, the header's value should be the security key
    account.mSecuritySyncKey = "key";
    svc.mAccount = account;
    method.removeHeaders("X-MS-PolicyKey");
    svc.setHeaders(method, true);
    headers = method.getHeaders("X-MS-PolicyKey");
    assertEquals(1, headers.length);
    assertEquals("key", headers[0].getValue());
}

From source file:com.magnet.plugin.api.requests.abs.BaseRequest.java

@Override
public void doWork() {
    try {//from  www  . j a  v  a  2  s.  co m
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpRequestBase request = getRequest(requestModel);

        List<RequestHeaderModel> requestModelHeaders = requestModel.getHeaders();

        for (RequestHeaderModel header : requestModelHeaders) {
            request.setHeader(header.getName(), header.getValue());
        }

        // If not specified as a header argument, Content-Type is inferred from the request body
        // Add the header here so the request is successful.
        if (request.getHeaders(ContentTypeHelper.CONTENT_TYPE_HEADER) == null
                || request.getHeaders(ContentTypeHelper.CONTENT_TYPE_HEADER).length == 0) {
            RestContentType type = ExampleParser.guessContentType(requestModel.getRequest());
            if (null != type) {
                request.setHeader(ContentTypeHelper.CONTENT_TYPE_HEADER, type.getName());
            }
        }
        // check if content-type is parameterized
        HttpResponse httpResponse = httpClient.execute(request);
        ApiMethodModel methodModel = new ApiMethodModel();
        methodModel.setRequestHeaders(request.getAllHeaders());
        methodModel.setHttpResponse(httpResponse);
        methodModel.setRequestModel(requestModel);
        onSuccess(methodModel);
    } catch (Exception e) {
        e.printStackTrace();
        Logger.info(getClass(), e.toString());
        onError(e);
    }

}

From source file:com.flipkart.poseidon.handlers.http.impl.HttpConnectionPool.java

/** Method to execute a request */
public HttpResponse execute(HttpRequestBase request) throws Exception {
    if (processQueue.tryAcquire()) {
        HttpResponse response;//from w  ww . j a v  a  2s. c o  m
        try {
            // Inject timestamp in milliseconds just before sending request on wire.
            // This will help in measuring latencies between client and server.
            if (request.getHeaders(TIMESTAMP_HEADER).length == 0) {
                request.addHeader(TIMESTAMP_HEADER, String.valueOf(System.currentTimeMillis()));
            }
            response = client.execute(request);
        } catch (Exception e) {
            logger.error("Connections: {} AvailableRequests: {}",
                    ((PoolingClientConnectionManager) this.client.getConnectionManager()).getTotalStats(),
                    processQueue.availablePermits());
            throw e;
        } finally {
            processQueue.release();
        }
        return response;
    } else {
        throw new Exception("PROCESS_QUEUE_FULL POOL:" + name);
    }
}