Example usage for org.apache.http.client.methods CloseableHttpResponse getFirstHeader

List of usage examples for org.apache.http.client.methods CloseableHttpResponse getFirstHeader

Introduction

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

Prototype

Header getFirstHeader(String str);

Source Link

Usage

From source file:edu.harvard.hul.ois.drs.pdfaconvert.clients.HttpClientIntegrationTest.java

@Test
public void doGetTest() throws URISyntaxException {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL fileUrl = loader.getResource(INPUT_FILENAME);
    File inputFile = new File(fileUrl.toURI());
    assertNotNull(inputFile);//from   w w w . j  a v a  2s  . c om
    assertTrue(inputFile.exists());
    assertTrue(inputFile.isFile());
    assertTrue(inputFile.canRead());

    CloseableHttpClient httpclient = HttpClients.createDefault();
    String fileLocation = fileUrl.getPath();
    String url = LOCAL_TOMCAT_SERVICE_URL + "?file=" + fileLocation;
    HttpGet httpGet = new HttpGet(url);

    CloseableHttpResponse response = null;
    try {
        logger.debug("executing request " + httpGet.getRequestLine());
        response = httpclient.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        logger.debug("Response status line : " + statusLine);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            long len = entity.getContentLength();
            if (len != -1 && len < 2048) {
                logger.debug("len: " + len);
                logger.debug(EntityUtils.toString(entity));
            } else {
                logger.debug("len: " + len);
                Header[] allHeaders = response.getAllHeaders();
                for (Header h : allHeaders) {
                    logger.debug("Header: name:" + h.getName() + " -- value: " + h.getValue()
                            + " -- toString(): " + h);
                }
                Header header = entity.getContentEncoding();
                header = entity.getContentType();
                logger.debug("header content type: " + header.toString());
                header = response.getFirstHeader("filename");
                String filename = header.getValue();
                String savedFilename = filename == null ? "file.pdf" : filename;
                InputStream is = entity.getContent();
                OutputStream out = new FileOutputStream("target/" + savedFilename);
                int bytesCnt;
                while ((bytesCnt = is.read()) != -1) {
                    out.write(bytesCnt);
                }
                out.close();
            }
        }
    } catch (IOException e) {
        logger.error("Something went wrong...", e);
        fail(e.getMessage());
    } finally {
        if (response != null) {
            try {
                response.close();
                httpclient.close();
            } catch (IOException e) {
                // nothing to do
                ;
            }
        }
    }
    logger.debug("DONE");
}

From source file:edu.harvard.hul.ois.drs.pdfaconvert.clients.HttpClientIntegrationTest.java

@Test
public void doPostTest() throws URISyntaxException {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL fileUrl = loader.getResource(INPUT_FILENAME);
    File inputFile = new File(fileUrl.toURI());
    assertNotNull(inputFile);/* w ww  .  j  a  v a  2  s .c o  m*/
    assertTrue(inputFile.exists());
    assertTrue(inputFile.isFile());
    assertTrue(inputFile.canRead());

    CloseableHttpClient httpclient = HttpClients.createDefault();

    HttpPost httpPost = new HttpPost(LOCAL_TOMCAT_SERVICE_URL);
    FileBody fileContent = new FileBody(inputFile);
    HttpEntity reqEntity = MultipartEntityBuilder.create().addPart(FORM_FIELD_DATAFILE, fileContent).build();
    httpPost.setEntity(reqEntity);

    CloseableHttpResponse response = null;
    try {
        logger.debug("executing request " + httpPost.getRequestLine());
        response = httpclient.execute(httpPost);
        StatusLine statusLine = response.getStatusLine();
        logger.debug("Response status line : " + statusLine);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            long len = entity.getContentLength();
            if (len != -1 && len < 2048) {
                logger.debug("len: " + len);
                logger.debug(EntityUtils.toString(entity));
            } else {
                logger.debug("len: " + len);
                Header[] allHeaders = response.getAllHeaders();
                for (Header h : allHeaders) {
                    logger.debug("Header: name:" + h.getName() + " -- value: " + h.getValue()
                            + " -- toString(): " + h);
                }
                Header header = entity.getContentEncoding();
                // logger.debug("header encoding: " + header.toString());
                header = entity.getContentType();
                logger.debug("header content type: " + header.toString());
                header = response.getFirstHeader("filename");
                String filename = header.getValue();
                String savedFilename = filename == null ? "file.pdf" : filename;
                InputStream is = entity.getContent();
                OutputStream out = new FileOutputStream("target/" + savedFilename);
                int bytesCnt;
                while ((bytesCnt = is.read()) != -1) {
                    out.write(bytesCnt);
                }
                out.close();
            }
        }
    } catch (IOException e) {
        logger.error("Something went wrong...", e);
        fail(e.getMessage());
    } finally {
        if (response != null) {
            try {
                response.close();
                httpclient.close();
            } catch (IOException e) {
                // nothing to do
                ;
            }
        }
    }
    logger.debug("DONE");

}

From source file:org.votingsystem.util.HttpHelper.java

public ResponseVS sendObjectMap(Map<String, Object> fileMap, String serverURL) throws Exception {
    log.info("sendObjectMap - serverURL: " + serverURL);
    ResponseVS responseVS = null;/*from   w  w  w  .  j a va2  s  .  c o  m*/
    if (fileMap == null || fileMap.isEmpty())
        throw new Exception(ContextVS.getInstance().getMessage("requestWithoutFileMapErrorMsg"));
    HttpPost httpPost = null;
    CloseableHttpResponse response = null;
    ContentTypeVS responseContentType = null;
    try {
        httpPost = new HttpPost(serverURL);
        Set<String> fileNames = fileMap.keySet();
        MultipartEntity reqEntity = new MultipartEntity();
        for (String objectName : fileNames) {
            Object objectToSend = fileMap.get(objectName);
            if (objectToSend instanceof File) {
                File file = (File) objectToSend;
                log.info("sendObjectMap - fileName: " + objectName + " - filePath: " + file.getAbsolutePath());
                FileBody fileBody = new FileBody(file);
                reqEntity.addPart(objectName, fileBody);
            } else if (objectToSend instanceof byte[]) {
                byte[] byteArray = (byte[]) objectToSend;
                reqEntity.addPart(objectName, new ByteArrayBody(byteArray, objectName));
            }
        }
        httpPost.setEntity(reqEntity);
        response = httpClient.execute(httpPost);
        Header header = response.getFirstHeader("Content-Type");
        if (header != null)
            responseContentType = ContentTypeVS.getByName(header.getValue());
        log.info("----------------------------------------");
        log.info(response.getStatusLine().toString() + " - contentTypeVS: " + responseContentType
                + " - connManager stats: " + connManager.getTotalStats().toString());
        log.info("----------------------------------------");
        byte[] responseBytes = EntityUtils.toByteArray(response.getEntity());
        responseVS = new ResponseVS(response.getStatusLine().getStatusCode(), responseBytes,
                responseContentType);
        EntityUtils.consume(response.getEntity());
    } catch (Exception ex) {
        String statusLine = null;
        if (response != null)
            statusLine = response.getStatusLine().toString();
        log.log(Level.SEVERE, ex.getMessage() + " - StatusLine: " + statusLine, ex);
        responseVS = new ResponseVS(ResponseVS.SC_ERROR, ex.getMessage());
        if (httpPost != null)
            httpPost.abort();
    } finally {
        try {
            if (response != null)
                response.close();
        } catch (Exception ex) {
            log.log(Level.SEVERE, ex.getMessage(), ex);
        }
        return responseVS;
    }
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.SteeringTest.java

License:asdf

@Test
public void itUsesMultiLocationFormatWithMoreThanTwoEntries() throws Exception {
    final String path = "/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78";
    HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + path);
    httpGet.addHeader("Host", "tr.client-steering-test-2.thecdn.example.com");

    CloseableHttpResponse response = null;

    try {/*  w w w.  j  a v  a  2 s  . c om*/
        response = httpClient.execute(httpGet);
        String location1 = ".steering-target-2.thecdn.example.com:8090" + path;
        String location2 = ".steering-target-1.thecdn.example.com:8090" + path;
        String location3 = ".client-steering-target-2.thecdn.example.com:8090" + path;
        String location4 = ".client-steering-target-4.thecdn.example.com:8090" + path;
        String location5 = ".client-steering-target-3.thecdn.example.com:8090" + path;
        String location6 = ".client-steering-target-1.thecdn.example.com:8090" + path;
        String location7 = ".steering-target-4.thecdn.example.com:8090" + path;
        String location8 = ".steering-target-3.thecdn.example.com:8090" + path;

        HttpEntity entity = response.getEntity();
        assertThat("Failed getting 302 for request " + httpGet.getFirstHeader("Host").getValue(),
                response.getStatusLine().getStatusCode(), equalTo(302));
        assertThat(response.getFirstHeader("Location").getValue(), endsWith(location1));

        ObjectMapper objectMapper = new ObjectMapper(new JsonFactory());

        assertThat(entity.getContent(), not(nullValue()));

        JsonNode json = objectMapper.readTree(entity.getContent());

        assertThat(json.has("locations"), equalTo(true));
        assertThat(json.get("locations").size(), equalTo(8));
        assertThat(json.get("locations").get(0).asText(),
                equalTo(response.getFirstHeader("Location").getValue()));
        assertThat(json.get("locations").get(1).asText(), endsWith(location2));
        assertThat(json.get("locations").get(2).asText(), endsWith(location3));
        assertThat(json.get("locations").get(3).asText(), endsWith(location4));
        assertThat(json.get("locations").get(4).asText(), endsWith(location5));
        assertThat(json.get("locations").get(5).asText(), endsWith(location6));
        assertThat(json.get("locations").get(6).asText(), endsWith(location7));
        assertThat(json.get("locations").get(7).asText(), endsWith(location8));
    } finally {
        if (response != null) {
            response.close();
        }
    }
}

From source file:it.tidalwave.bluemarine2.downloader.impl.DefaultDownloader.java

/*******************************************************************************************************************
 *
 *
 *
 ******************************************************************************************************************/
/* VisibleForTesting */ void onDownloadRequest(final @ListensTo @Nonnull DownloadRequest request)
        throws URISyntaxException {
    try {//from  w w w.  j  a v  a2  s.c om
        log.info("onDownloadRequest({})", request);

        URL url = request.getUrl();

        for (;;) {
            final HttpCacheContext context = HttpCacheContext.create();
            @Cleanup
            final CloseableHttpResponse response = httpClient.execute(new HttpGet(url.toURI()), context);
            final byte[] bytes = bytesFrom(response);
            final CacheResponseStatus cacheResponseStatus = context.getCacheResponseStatus();
            log.debug(">>>> cacheResponseStatus: {}", cacheResponseStatus);

            final Origin origin = cacheResponseStatus.equals(CacheResponseStatus.CACHE_HIT) ? Origin.CACHE
                    : Origin.NETWORK;

            // FIXME: shouldn't do this by myself
            // FIXME: upon configuration, everything should be cached (needed for supporting integration tests)
            if (!origin.equals(Origin.CACHE)
                    && Arrays.asList(200, 303).contains(response.getStatusLine().getStatusCode())) {
                final Date date = new Date();
                final Resource resource = new HeapResource(bytes);
                cacheStorage.putEntry(url.toExternalForm(), new HttpCacheEntry(date, date,
                        response.getStatusLine(), response.getAllHeaders(), resource));
            }

            // FIXME: if the redirect were enabled, we could drop this check
            if (request.isOptionPresent(DownloadRequest.Option.FOLLOW_REDIRECT)
                    && response.getStatusLine().getStatusCode() == 303) // SEE_OTHER FIXME
            {
                url = new URL(response.getFirstHeader("Location").getValue());
                log.info(">>>> following 'see also' to {} ...", url);
            } else {
                messageBus.publish(new DownloadComplete(request.getUrl(),
                        response.getStatusLine().getStatusCode(), bytes, origin));
                return;
            }
        }
    } catch (IOException e) {
        log.error("{}: {}", request.getUrl(), e.toString());
        messageBus.publish(new DownloadComplete(request.getUrl(), -1, new byte[0], Origin.NETWORK));
    }
}

From source file:org.aksw.dice.eaglet.uri.impl.HttpBasedUriChecker.java

protected Model requestModel(String uri) {
    HttpGet request = null;//from w  ww  .j  a v a  2s.  c o  m
    try {
        request = createGetRequest(uri);
    } catch (IllegalArgumentException e) {
        LOGGER.error("Exception while sending request for \"" + uri + "\". Returning null.", e);
        return null;
    }
    request.addHeader(HttpHeaders.ACCEPT, REQUEST_ACCEPT_HEADER_VALUE);
    request.addHeader(HttpHeaders.ACCEPT_CHARSET, "UTF-8");

    HttpEntity entity = null;
    CloseableHttpResponse response = null;
    Model model = null;
    try {

        try {
            response = client.execute(request);
        } catch (java.net.SocketException e) {
            if (e.getMessage().contains(CONNECTION_ABORT_INDICATING_EXCPETION_MSG)) {
                LOGGER.error("It seems like requesting the model of \"" + uri
                        + "\" needed too much time and was interrupted. Returning null.");
                return null;
            } else {
                LOGGER.error("Exception while sending request to \"" + uri + "\". Returning null.", e);
                return null;
            }
        } catch (UnknownHostException e) {
            LOGGER.info("Couldn't find host of \"" + uri + "\". Returning null.");
            return null;
        } catch (Exception e) {
            LOGGER.error("Exception while sending request to \"" + uri + "\". Returning null.", e);
            return null;
        }
        StatusLine status = response.getStatusLine();
        if ((status.getStatusCode() < 200) || (status.getStatusCode() >= 300)) {
            LOGGER.warn("Response of \"{}\" has the wrong status ({}). Returning null.", uri,
                    status.toString());
            return null;
        }
        // receive NIF document
        entity = response.getEntity();
        Header contentTypeHeader = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
        if (contentTypeHeader == null) {
            LOGGER.error("The response did not contain a content type header. Returning null.");
            return null;
        }
        ContentType contentType = ContentType.create(contentTypeHeader.getValue());
        Lang language = RDFLanguages.contentTypeToLang(contentType);
        if (language == null) {
            LOGGER.error(
                    "Couldn't find an RDF language for the content type header value \"{}\". Returning null.",
                    contentTypeHeader.getValue());
            return null;
        }
        // read response and parse NIF
        try {
            model = ModelFactory.createDefaultModel();
            RDFDataMgr.read(model, entity.getContent(), language);
        } catch (Exception e) {
            LOGGER.error("Couldn't parse the response for the URI \"" + uri + "\". Returning null", e);
        }
    } finally {
        if (entity != null) {
            try {
                EntityUtils.consume(entity);
            } catch (IOException e1) {
            }
        }
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
        closeRequest(request);
    }
    return model;
}

From source file:org.apache.hive.jdbc.TestActivePassiveHA.java

private String sendAuthMethod(HttpRequestBase method, boolean enableAuth, boolean enableCORS) throws Exception {
    CloseableHttpResponse httpResponse = null;

    try (CloseableHttpClient client = HttpClients.createDefault();) {

        // CORS check
        if (enableCORS) {
            String origin = "http://example.com";

            HttpOptions optionsMethod = new HttpOptions(method.getURI().toString());

            optionsMethod.addHeader("Origin", origin);

            if (enableAuth) {
                setupAuthHeaders(optionsMethod);
            }//from w  ww.j  av a 2s  .  c om

            httpResponse = client.execute(optionsMethod);

            if (httpResponse != null) {
                StatusLine statusLine = httpResponse.getStatusLine();
                if (statusLine != null) {
                    String response = httpResponse.getStatusLine().getReasonPhrase();
                    int statusCode = httpResponse.getStatusLine().getStatusCode();

                    if (statusCode == 200) {
                        Header originHeader = httpResponse.getFirstHeader("Access-Control-Allow-Origin");
                        assertNotNull(originHeader);
                        assertEquals(origin, originHeader.getValue());
                    } else {
                        fail("CORS returned: " + statusCode + " Error: " + response);
                    }
                } else {
                    fail("Status line is null");
                }
            } else {
                fail("No http Response");
            }
        }

        if (enableAuth) {
            setupAuthHeaders(method);
        }

        httpResponse = client.execute(method);

        return EntityUtils.toString(httpResponse.getEntity());
    } finally {
        httpResponse.close();
    }
}

From source file:eu.fusepool.p3.proxy.ProxyHandler.java

@Override
public void handle(String target, Request baseRequest, final HttpServletRequest inRequest,
        final HttpServletResponse outResponse) throws IOException, ServletException {
    final String targetUriString = targetBaseUri + inRequest.getRequestURI();
    final String requestUri = getFullRequestUrl(inRequest);
    //System.out.println(targetUriString);
    final URI targetUri;
    try {/*from  w ww  .java2  s .c  o  m*/
        targetUri = new URI(targetUriString);
    } catch (URISyntaxException ex) {
        throw new IOException(ex);
    }
    final String method = inRequest.getMethod();
    final HttpEntityEnclosingRequestBase outRequest = new HttpEntityEnclosingRequestBase() {

        @Override
        public String getMethod() {
            return method;
        }

    };
    outRequest.setURI(targetUri);
    String transformerUri = null;
    if (method.equals("POST")) {
        if (!"no-transform".equals(inRequest.getHeader("X-Fusepool-Proxy"))) {
            transformerUri = getTransformerUrl(requestUri);
        }
    }
    final Enumeration<String> headerNames = baseRequest.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        final String headerName = headerNames.nextElement();
        if (headerName.equalsIgnoreCase("Content-Length") || headerName.equalsIgnoreCase("X-Fusepool-Proxy")
                || headerName.equalsIgnoreCase("Transfer-Encoding")) {
            continue;
        }
        final Enumeration<String> headerValues = baseRequest.getHeaders(headerName);
        if (headerValues.hasMoreElements()) {
            final String headerValue = headerValues.nextElement();
            outRequest.setHeader(headerName, headerValue);
        }
        while (headerValues.hasMoreElements()) {
            final String headerValue = headerValues.nextElement();
            outRequest.addHeader(headerName, headerValue);
        }
    }
    final Header[] outRequestHeaders = outRequest.getAllHeaders();
    //slow: outRequest.setEntity(new InputStreamEntity(inRequest.getInputStream()));
    final byte[] inEntityBytes = IOUtils.toByteArray(inRequest.getInputStream());
    if (inEntityBytes.length > 0) {
        outRequest.setEntity(new ByteArrayEntity(inEntityBytes));
    }
    final CloseableHttpResponse inResponse = httpclient.execute(outRequest);
    try {
        outResponse.setStatus(inResponse.getStatusLine().getStatusCode());
        final Header[] inResponseHeaders = inResponse.getAllHeaders();
        final Set<String> setHeaderNames = new HashSet();
        for (Header header : inResponseHeaders) {
            if (setHeaderNames.add(header.getName())) {
                outResponse.setHeader(header.getName(), header.getValue());
            } else {
                outResponse.addHeader(header.getName(), header.getValue());
            }
        }
        final HttpEntity entity = inResponse.getEntity();
        final ServletOutputStream os = outResponse.getOutputStream();
        if (entity != null) {
            //outResponse.setContentType(target);
            final InputStream instream = entity.getContent();
            try {
                IOUtils.copy(instream, os);
            } finally {
                instream.close();
            }
        }
        //without flushing this and no or too little byte jetty return 404
        os.flush();
    } finally {
        inResponse.close();
    }
    if (transformerUri != null) {
        Header locationHeader = inResponse.getFirstHeader("Location");
        if (locationHeader == null) {
            log.warn("Response to POST request to LDPC contains no Location header. URI: " + targetUriString);
        } else {
            startTransformation(locationHeader.getValue(), requestUri, transformerUri, inEntityBytes,
                    outRequestHeaders);
        }
    }
}

From source file:com.addthis.hydra.job.web.resources.JobsResource.java

@GET
@Path("/{jobID}/log")
@Produces(MediaType.APPLICATION_JSON)/*  w  ww  .jav a  2s. co  m*/
public Response getJobTaskLog(@PathParam("jobID") String jobID,
        @QueryParam("lines") @DefaultValue("50") int lines,
        @QueryParam("runsAgo") @DefaultValue("0") int runsAgo,
        @QueryParam("offset") @DefaultValue("-1") int offset, @QueryParam("out") @DefaultValue("1") int out,
        @QueryParam("minion") String minion, @QueryParam("port") String port, @QueryParam("node") String node) {
    JSONObject body = new JSONObject();
    try {
        if (minion == null) {
            body.put("error", "Missing required query parameter 'minion'");
            return Response.status(Response.Status.BAD_REQUEST).entity(body.toString()).build();
        } else if (node == null) {
            body.put("error", "Missing required query parameter 'node'");
            return Response.status(Response.Status.BAD_REQUEST).entity(body.toString()).build();
        } else if (port == null) {
            body.put("error", "Missing required query parameter 'port'");
            return Response.status(Response.Status.BAD_REQUEST).entity(body.toString()).build();
        } else if (lines > maxLogFileLines) {
            body.put("error",
                    "Number of log lines requested " + lines + " is greater than max " + maxLogFileLines);
            return Response.status(Response.Status.BAD_REQUEST).entity(body.toString()).build();
        } else {
            URI uri = UriBuilder.fromUri("http://" + minion + ":" + port + "/job.log").queryParam("id", jobID)
                    .queryParam("node", node).queryParam("runsAgo", runsAgo).queryParam("lines", lines)
                    .queryParam("out", out).queryParam("offset", offset).build();

            HttpGet httpGet = new HttpGet(uri);
            CloseableHttpResponse response = httpClient.execute(httpGet);

            try {
                HttpEntity entity = response.getEntity();
                String encoding = entity.getContentEncoding() != null ? entity.getContentEncoding().getValue()
                        : "UTF-8";
                String responseBody = IOUtils.toString(entity.getContent(), encoding);

                return Response.status(response.getStatusLine().getStatusCode())
                        .header("Content-type", response.getFirstHeader("Content-type")).entity(responseBody)
                        .build();
            } catch (IOException ex) {
                return buildServerError(ex);
            } finally {
                try {
                    response.close();
                } catch (IOException ex) {
                    log.warn("Error while closing response: ", ex);
                }
            }
        }

    } catch (Exception ex) {
        return buildServerError(ex);
    }

}