List of usage examples for org.apache.http.client.methods CloseableHttpResponse getFirstHeader
Header getFirstHeader(String str);
From source file:org.brutusin.rpc.client.http.HttpEndpoint.java
/** * * @param serviceId/*from www .j av a2 s. c o m*/ * @param input supports inputstreams * @param progressCallback * @return * @throws IOException */ public final HttpResponse exec(final String serviceId, final JsonNode input, final ProgressCallback progressCallback) throws IOException { if (!loaded) { synchronized (this) { if (!loaded) { loadServices(); loaded = true; } } } final HttpMethod method = getMethod(services.get(serviceId)); if (method == null) { throw new IllegalArgumentException("Invalid service id " + serviceId); } HttpResponse ret = new HttpResponse(); try { CloseableHttpResponse resp = doExec(serviceId, input, method, progressCallback); if (resp.getEntity() == null) { throw new RuntimeException(resp.getStatusLine().toString()); } Header cacheControl = resp.getFirstHeader("Cache-Control"); if (cacheControl != null) { HeaderElement[] elements = cacheControl.getElements(); if (elements != null) { for (HeaderElement element : elements) { if (element.getName().equals("no-cache")) { break; } if (ret.getCachingInfo() == null) { ret.setCachingInfo(new CachingInfo(0, true, false)); } if (element.getName().equals("max-age")) { ret.getCachingInfo().setMaxAge(Integer.valueOf(element.getValue())); } if (element.getName().equals("public")) { ret.getCachingInfo().setShared(true); } if (element.getName().equals("private")) { ret.getCachingInfo().setShared(false); } if (element.getName().equals("no-store")) { ret.getCachingInfo().setStore(false); } } } } if (!resp.getEntity().getContentType().getValue().startsWith(JSON_CONTENT_TYPE)) { MetaDataInputStream is = new MetaDataInputStream(resp.getEntity().getContent(), getAttachmentFileName(resp.getFirstHeader("Content-Disposition")), resp.getEntity().getContentType().getValue(), resp.getEntity().getContentLength(), null); ret.setInputStream(is); } else { JsonNode responseNode = JsonCodec.getInstance() .parse(Miscellaneous.toString(resp.getEntity().getContent(), "UTF-8")); RpcResponse<JsonNode> rpcResponse = new RpcResponse<JsonNode>(); if (responseNode.get("error") != null) { rpcResponse.setError( JsonCodec.getInstance().load(responseNode.get("error"), RpcResponse.Error.class)); } rpcResponse.setResult(responseNode.get("result")); ret.setRpcResponse(rpcResponse); } } catch (ConnectException ex) { RpcResponse<JsonNode> rpcResponse = new RpcResponse<JsonNode>(); rpcResponse.setError(new RpcResponse.Error(RpcErrorCode.connectionError, ex.getMessage())); ret.setRpcResponse(rpcResponse); } catch (Throwable t) { RpcResponse<JsonNode> rpcResponse = new RpcResponse<JsonNode>(); rpcResponse.setError(new RpcResponse.Error(RpcErrorCode.internalError, t.getMessage())); ret.setRpcResponse(rpcResponse); } return ret; }
From source file:de.jetwick.snacktory.HtmlFetcher.java
public String fetchAsString(String urlAsString, int timeout, boolean includeSomeGooseOptions) throws MalformedURLException, IOException { urlAsString = urlAsString.replace("https", "http"); CloseableHttpResponse response = createUrlConnection(urlAsString, timeout, includeSomeGooseOptions, false); if (response.getStatusLine().getStatusCode() > 399) { throw new MalformedURLException(response.getStatusLine().toString()); }//w w w.j a v a2 s . c o m Header header = response.getFirstHeader("Content-Type"); String encoding = null; if (header == null) { encoding = "utf-8"; } else { encoding = header.getValue(); if (encoding == null || !encoding.startsWith("text")) { throw new MalformedURLException("Not an HTML content!"); } } String res = null; try { final HttpEntity body = response.getEntity(); InputStream is; if (encoding != null && encoding.equalsIgnoreCase("gzip")) { is = new GZIPInputStream(body.getContent()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { is = new InflaterInputStream(body.getContent(), new Inflater(true)); } else { is = body.getContent(); } String enc = Converter.extractEncoding(encoding); res = createConverter(urlAsString).streamToString(is, enc); EntityUtils.consume(body); if (logger.isDebugEnabled()) logger.debug(res.length() + " FetchAsString:" + urlAsString); } finally { response.close(); } return res; }
From source file:org.hawk.http.HTTPManager.java
protected String getRevision(CloseableHttpResponse response) { if (response.getStatusLine().getStatusCode() == 304) { // Not-Modified, as told by the server return lastETag; } else if (response.getStatusLine().getStatusCode() != 200) { // Request failed for some reason (4xx, 5xx: we already // handle 3xx redirects) return FIRST_REV; }/*from w w w. j a va 2s . c om*/ final Header etagHeader = response.getFirstHeader(HEADER_ETAG); if (etagHeader != null) { lastETag = etagHeader.getValue(); return lastETag; } final Header lmHeader = response.getFirstHeader(HEADER_LAST_MODIFIED); if (lmHeader != null) { final Date lmDate = DateUtils.parseDate(lmHeader.getValue()); return lmDate.getTime() + ""; } final Header clHeader = response.getFirstHeader(HEADER_CONTENT_LENGTH); if (clHeader != null) { return clHeader.getValue(); } final HttpEntity entity = response.getEntity(); if (entity != null) { final long cLength = entity.getContentLength(); if (cLength >= 0) { return cLength + ""; } } return null; }
From source file:io.joynr.messaging.bounceproxy.controller.RemoteBounceProxyFacade.java
private URI sendCreateChannelHttpRequest(ControlledBounceProxyInformation bpInfo, String ccid, String trackingId) throws IOException, JoynrProtocolException { // TODO jsessionid handling final String url = bpInfo.getLocationForBpc().toString() + "channels/?ccid=" + ccid; logger.debug("Using bounce proxy channel service URL: {}", url); HttpPost postCreateChannel = new HttpPost(url.trim()); postCreateChannel.addHeader(ChannelServiceConstants.X_ATMOSPHERE_TRACKING_ID, trackingId); CloseableHttpResponse response = null; try {/*from ww w .j av a 2 s .c o m*/ response = httpclient.execute(postCreateChannel); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == HttpURLConnection.HTTP_CREATED) { // the channel was created successfully // check if bounce proxy ID header was sent correctly if (response.containsHeader("bp")) { String bounceProxyId = response.getFirstHeader("bp").getValue(); if (bounceProxyId == null || !bounceProxyId.equals(bpInfo.getId())) { throw new JoynrProtocolException("Bounce proxy ID '" + bounceProxyId + "' returned by bounce proxy '" + bpInfo.getId() + "' does not match."); } } else { throw new JoynrProtocolException( "No bounce proxy ID returned by bounce proxy '" + bpInfo.getId() + "'"); } // get URI of newly created channel if (!response.containsHeader("Location")) { throw new JoynrProtocolException( "No channel location returned by bounce proxy '" + bpInfo.getId() + "'"); } String locationValue = response.getFirstHeader("Location").getValue(); if (locationValue == null || locationValue.isEmpty()) { throw new JoynrProtocolException( "Bounce proxy '" + bpInfo.getId() + "' didn't return a channel location."); } try { URI channelLocation = new URI(locationValue); logger.info("Successfully created channel '{}' on bounce proxy '{}'", ccid, bpInfo.getId()); return channelLocation; } catch (Exception e) { throw new JoynrProtocolException("Cannot parse channel location '" + locationValue + "' returned by bounce proxy '" + bpInfo.getId() + "'", e); } } // the bounce proxy is not excepted to reject this call as it was // chosen based on performance measurements sent by it logger.error("Failed to create channel on bounce proxy '{}'. Response: {}", bpInfo.getId(), response); throw new JoynrProtocolException( "Bounce Proxy " + bpInfo.getId() + " rejected channel creation (Response: " + response + ")"); } finally { if (response != null) { response.close(); } } }
From source file:qhindex.controller.SearchAuthorWorksController.java
private String resolvePublisher(String urlCitationWork, String publisherNameIncomplete) throws IOException { String publisher = publisherNameIncomplete; if (urlCitationWork.contains(".pdf") == false) { // Get the header and determine if the resource is in text format (html or plain) // to be able to extract the publisher name final RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(AppHelper.connectionTimeOut) .setConnectionRequestTimeout(AppHelper.connectionTimeOut) .setSocketTimeout(AppHelper.connectionTimeOut).setStaleConnectionCheckEnabled(true).build(); final CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig) .build();//from ww w. j a va 2s . co m HttpHead httpHead = new HttpHead(urlCitationWork); try { CloseableHttpResponse responseHead = httpclient.execute(httpHead); StatusLine statusLineHead = responseHead.getStatusLine(); responseHead.close(); String contentType = responseHead.getFirstHeader("Content-Type").toString().toLowerCase(); if (statusLineHead.getStatusCode() < 300 && contentType.contains("text/html") || contentType.contains("text/plain")) { HttpGet httpGet = new HttpGet(urlCitationWork); CloseableHttpResponse responsePost = httpclient.execute(httpGet); StatusLine statusLine = responsePost.getStatusLine(); if (statusLine.getStatusCode() < 300) { //AppHelper.waitBeforeNewRequest(); BufferedReader br = new BufferedReader( new InputStreamReader((responsePost.getEntity().getContent()))); String content = new String(); String line; while ((line = br.readLine()) != null) { content += line; } int bodyStartIndex = content.indexOf("<body"); if (bodyStartIndex < 0) bodyStartIndex = 0; try { publisherNameIncomplete = formatRegExSpecialCharsInString(publisherNameIncomplete); Pattern pattern = Pattern.compile(publisherNameIncomplete + "(\\w|\\d|-|\\s)+"); Matcher matcher = pattern.matcher(content); if (matcher.find(bodyStartIndex)) { publisher = content.substring(matcher.start(), matcher.end()); } else { publisher = publisherNameIncomplete; } } catch (Exception ex) { Debug.print( "Exception while resolving publisher for citing work - extrating pattern from citation web resource: " + ex.toString()); resultsMsg += "Exception while resolving publisher for citing work - extrating pattern from citation web resource.\n"; } } responsePost.close(); } } catch (IOException ioEx) { Debug.print("Exception while resolving publisher for citing work: " + ioEx.toString()); resultsMsg += "Exception while resolving publisher for citing work.\n"; } } publisher = publisher.trim(); return publisher; }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.RouterTest.java
@Test public void itRedirectsValidHttpRequests() throws IOException, InterruptedException { HttpGet httpGet = new HttpGet( "http://localhost:" + routerHttpPort + "/stuff?fakeClientIpAddress=12.34.56.78"); httpGet.addHeader("Host", "tr." + deliveryServiceId + ".bar"); CloseableHttpResponse response = null; try {/* w w w . ja va 2 s . c om*/ response = httpClient.execute(httpGet); assertThat(response.getStatusLine().getStatusCode(), equalTo(302)); Header header = response.getFirstHeader("Location"); assertThat(header.getValue(), isIn(validLocations)); assertThat(header.getValue(), Matchers.startsWith("http://")); } finally { if (response != null) response.close(); } }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.RouterTest.java
@Test public void itRedirectsHttpsRequests() throws Exception { HttpGet httpGet = new HttpGet( "https://localhost:" + routerSecurePort + "/stuff?fakeClientIpAddress=12.34.56.78"); httpGet.addHeader("Host", "tr." + httpsOnlyId + ".thecdn.example.com"); CloseableHttpResponse response = null; try {/* ww w. j a v a 2s .co m*/ response = httpClient.execute(httpGet); assertThat(response.getStatusLine().getStatusCode(), equalTo(302)); Header header = response.getFirstHeader("Location"); assertThat(header.getValue(), isIn(httpsOnlyLocations)); assertThat(header.getValue(), startsWith("https://")); assertThat(header.getValue(), containsString(httpsOnlyId + ".thecdn.example.com/stuff")); } finally { if (response != null) response.close(); } }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.RouterTest.java
@Test public void itConsistentlyRedirectsValidRequests() throws IOException, InterruptedException { HttpGet httpGet = new HttpGet( "http://localhost:" + routerHttpPort + "/stuff?fakeClientIpAddress=12.34.56.78"); httpGet.addHeader("Host", "tr." + deliveryServiceId + ".bar"); CloseableHttpResponse response = null; try {/*from w w w . jav a 2s. com*/ response = httpClient.execute(httpGet); assertThat(response.getStatusLine().getStatusCode(), equalTo(302)); String location = response.getFirstHeader("Location").getValue(); response.close(); for (int i = 0; i < 100; i++) { response = httpClient.execute(httpGet); assertThat(response.getFirstHeader("Location").getValue(), equalTo(location)); response.close(); } } finally { if (response != null) response.close(); } }
From source file:org.apache.activemq.artemis.tests.integration.rest.util.RestMessageContext.java
public String pullMessage() throws IOException { String message = null;//from w w w. j ava 2 s . co m String msgPullUri = null; if (autoAck) { msgPullUri = contextMap.get(KEY_MSG_CONSUME_NEXT); if (msgPullUri == null) { initPullConsumers(); msgPullUri = contextMap.get(KEY_MSG_CONSUME_NEXT); } } else { msgPullUri = contextMap.get(KEY_MSG_ACK_NEXT); if (msgPullUri == null) { initPullConsumers(); msgPullUri = contextMap.get(KEY_MSG_ACK_NEXT); } } CloseableHttpResponse response = connection.post(msgPullUri); int code = ResponseUtil.getHttpCode(response); try { if (code == 200) { // success HttpEntity entity = response.getEntity(); long len = entity.getContentLength(); if (len != -1 && len < 1024000) { message = EntityUtils.toString(entity); } else { // drop message System.err.println("Message too large, drop it " + len); } Header header = response.getFirstHeader(KEY_MSG_CONSUMER); contextMap.put(KEY_MSG_CONSUMER, header.getValue()); if (!autoAck) { header = response.getFirstHeader(KEY_MSG_ACK); contextMap.put(KEY_MSG_ACK, header.getValue()); } else { header = response.getFirstHeader(KEY_MSG_CONSUME_NEXT); contextMap.put(KEY_MSG_CONSUME_NEXT, header.getValue()); } } else if (code == 503) { if (autoAck) { contextMap.put(KEY_MSG_CONSUME_NEXT, response.getFirstHeader(KEY_MSG_CONSUME_NEXT).getValue()); } else { contextMap.put(KEY_MSG_ACK_NEXT, response.getFirstHeader(KEY_MSG_ACK_NEXT).getValue()); } Header header = response.getFirstHeader("Retry-After"); if (header != null) { long retryDelay = Long.valueOf(response.getFirstHeader("Retry-After").getValue()); try { Thread.sleep(retryDelay); } catch (InterruptedException e) { e.printStackTrace(); } message = pullMessage(); } } else { throw new IllegalStateException("error: " + ResponseUtil.getDetails(response)); } } finally { response.close(); } return message; }