List of usage examples for org.apache.http.client.methods CloseableHttpResponse getFirstHeader
Header getFirstHeader(String str);
From source file:org.apache.maven.wagon.providers.http.AbstractHttpClientWagonFixed.java
private void fillInputData(int wait, InputData inputData) throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException { Resource resource = inputData.getResource(); String repositoryUrl = getRepository().getUrl(); String url = repositoryUrl + (repositoryUrl.endsWith("/") ? "" : "/") + resource.getName(); HttpGet getMethod = new HttpGet(url); long timestamp = resource.getLastModified(); if (timestamp > 0) { SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd-MMM-yy HH:mm:ss zzz", Locale.US); fmt.setTimeZone(GMT_TIME_ZONE);// w w w .j av a 2 s. c om Header hdr = new BasicHeader("If-Modified-Since", fmt.format(new Date(timestamp))); fireTransferDebug("sending ==> " + hdr + "(" + timestamp + ")"); getMethod.addHeader(hdr); } try { CloseableHttpResponse response = execute(getMethod); closeable = response; int statusCode = response.getStatusLine().getStatusCode(); String reasonPhrase = ", ReasonPhrase:" + response.getStatusLine().getReasonPhrase() + "."; fireTransferDebug(url + " - Status code: " + statusCode + reasonPhrase); switch (statusCode) { case HttpStatus.SC_OK: break; case HttpStatus.SC_NOT_MODIFIED: // return, leaving last modified set to original value so getIfNewer should return unmodified return; case HttpStatus.SC_FORBIDDEN: fireSessionConnectionRefused(); throw new AuthorizationException("Access denied to: " + url + " " + reasonPhrase); case HttpStatus.SC_UNAUTHORIZED: fireSessionConnectionRefused(); throw new AuthorizationException("Not authorized " + reasonPhrase); case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED: fireSessionConnectionRefused(); throw new AuthorizationException("Not authorized by proxy " + reasonPhrase); case HttpStatus.SC_NOT_FOUND: throw new ResourceDoesNotExistException("File: " + url + " " + reasonPhrase); case SC_TOO_MANY_REQUESTS: fillInputData(backoff(wait, url), inputData); break; // add more entries here default: cleanupGetTransfer(resource); TransferFailedException e = new TransferFailedException( "Failed to transfer file: " + url + ". Return code is: " + statusCode + " " + reasonPhrase); fireTransferError(resource, e, TransferEvent.REQUEST_GET); throw e; } Header contentLengthHeader = response.getFirstHeader("Content-Length"); if (contentLengthHeader != null) { try { long contentLength = Long.parseLong(contentLengthHeader.getValue()); resource.setContentLength(contentLength); } catch (NumberFormatException e) { fireTransferDebug( "error parsing content length header '" + contentLengthHeader.getValue() + "' " + e); } } Header lastModifiedHeader = response.getFirstHeader("Last-Modified"); if (lastModifiedHeader != null) { Date lastModified = DateUtils.parseDate(lastModifiedHeader.getValue()); if (lastModified != null) { resource.setLastModified(lastModified.getTime()); fireTransferDebug("last-modified = " + lastModifiedHeader.getValue() + " (" + lastModified.getTime() + ")"); } } HttpEntity entity = response.getEntity(); if (entity != null) { inputData.setInputStream(entity.getContent()); } } catch (IOException e) { fireTransferError(resource, e, TransferEvent.REQUEST_GET); throw new TransferFailedException(e.getMessage(), e); } catch (HttpException e) { fireTransferError(resource, e, TransferEvent.REQUEST_GET); throw new TransferFailedException(e.getMessage(), e); } catch (InterruptedException e) { fireTransferError(resource, e, TransferEvent.REQUEST_GET); throw new TransferFailedException(e.getMessage(), e); } }
From source file:org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.java
private void fillInputData(int wait, InputData inputData) throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException { Resource resource = inputData.getResource(); String repositoryUrl = getRepository().getUrl(); String url = repositoryUrl + (repositoryUrl.endsWith("/") ? "" : "/") + resource.getName(); HttpGet getMethod = new HttpGet(url); long timestamp = resource.getLastModified(); if (timestamp > 0) { SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd-MMM-yy HH:mm:ss zzz", Locale.US); fmt.setTimeZone(GMT_TIME_ZONE);/* ww w .j a va 2s .co m*/ Header hdr = new BasicHeader("If-Modified-Since", fmt.format(new Date(timestamp))); fireTransferDebug("sending ==> " + hdr + "(" + timestamp + ")"); getMethod.addHeader(hdr); } try { CloseableHttpResponse response = execute(getMethod); closeable = response; int statusCode = response.getStatusLine().getStatusCode(); String reasonPhrase = ", ReasonPhrase:" + response.getStatusLine().getReasonPhrase() + "."; fireTransferDebug(url + " - Status code: " + statusCode + reasonPhrase); switch (statusCode) { case HttpStatus.SC_OK: break; case HttpStatus.SC_NOT_MODIFIED: // return, leaving last modified set to original value so getIfNewer should return unmodified return; case HttpStatus.SC_FORBIDDEN: fireSessionConnectionRefused(); throw new AuthorizationException("Access denied to: " + url + " " + reasonPhrase); case HttpStatus.SC_UNAUTHORIZED: fireSessionConnectionRefused(); throw new AuthorizationException("Not authorized " + reasonPhrase); case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED: fireSessionConnectionRefused(); throw new AuthorizationException("Not authorized by proxy " + reasonPhrase); case HttpStatus.SC_NOT_FOUND: throw new ResourceDoesNotExistException("File: " + url + " " + reasonPhrase); case SC_TOO_MANY_REQUESTS: fillInputData(backoff(wait, url), inputData); break; // add more entries here default: { cleanupGetTransfer(resource); TransferFailedException e = new TransferFailedException( "Failed to transfer file: " + url + ". Return code is: " + statusCode + " " + reasonPhrase); fireTransferError(resource, e, TransferEvent.REQUEST_GET); throw e; } } Header contentLengthHeader = response.getFirstHeader("Content-Length"); if (contentLengthHeader != null) { try { long contentLength = Long.parseLong(contentLengthHeader.getValue()); resource.setContentLength(contentLength); } catch (NumberFormatException e) { fireTransferDebug( "error parsing content length header '" + contentLengthHeader.getValue() + "' " + e); } } Header lastModifiedHeader = response.getFirstHeader("Last-Modified"); if (lastModifiedHeader != null) { Date lastModified = DateUtils.parseDate(lastModifiedHeader.getValue()); if (lastModified != null) { resource.setLastModified(lastModified.getTime()); fireTransferDebug("last-modified = " + lastModifiedHeader.getValue() + " (" + lastModified.getTime() + ")"); } } HttpEntity entity = response.getEntity(); if (entity != null) { inputData.setInputStream(entity.getContent()); } } catch (IOException e) { fireTransferError(resource, e, TransferEvent.REQUEST_GET); throw new TransferFailedException(e.getMessage(), e); } catch (HttpException e) { fireTransferError(resource, e, TransferEvent.REQUEST_GET); throw new TransferFailedException(e.getMessage(), e); } catch (InterruptedException e) { fireTransferError(resource, e, TransferEvent.REQUEST_GET); throw new TransferFailedException(e.getMessage(), e); } }
From source file:nl.architolk.ldt.processors.HttpClientProcessor.java
public void generateData(PipelineContext context, ContentHandler contentHandler) throws SAXException { try {// w ww.ja va2 s .c o m CloseableHttpClient httpclient = HttpClientProperties.createHttpClient(); try { // Read content of config pipe Document configDocument = readInputAsDOM4J(context, INPUT_CONFIG); Node configNode = configDocument.selectSingleNode("//config"); URL theURL = new URL(configNode.valueOf("url")); if (configNode.valueOf("auth-method").equals("basic")) { HttpHost targetHost = new HttpHost(theURL.getHost(), theURL.getPort(), theURL.getProtocol()); //Authentication support CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials( configNode.valueOf("username"), configNode.valueOf("password"))); // logger.info("Credentials: "+configNode.valueOf("username")+"/"+configNode.valueOf("password")); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); authCache.put(targetHost, new BasicScheme()); // Add AuthCache to the execution context httpContext = HttpClientContext.create(); httpContext.setCredentialsProvider(credsProvider); httpContext.setAuthCache(authCache); } else if (configNode.valueOf("auth-method").equals("form")) { //Sign in. Cookie will be remembered bij httpclient HttpPost authpost = new HttpPost(configNode.valueOf("auth-url")); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("userName", configNode.valueOf("username"))); nameValuePairs.add(new BasicNameValuePair("password", configNode.valueOf("password"))); authpost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); CloseableHttpResponse httpResponse = httpclient.execute(authpost); // logger.info("Signin response:"+Integer.toString(httpResponse.getStatusLine().getStatusCode())); } CloseableHttpResponse response; if (configNode.valueOf("method").equals("post")) { // POST HttpPost httpRequest = new HttpPost(configNode.valueOf("url")); setBody(httpRequest, context, configNode); response = executeRequest(httpRequest, httpclient); } else if (configNode.valueOf("method").equals("put")) { // PUT HttpPut httpRequest = new HttpPut(configNode.valueOf("url")); setBody(httpRequest, context, configNode); response = executeRequest(httpRequest, httpclient); } else if (configNode.valueOf("method").equals("delete")) { //DELETE HttpDelete httpRequest = new HttpDelete(configNode.valueOf("url")); response = executeRequest(httpRequest, httpclient); } else if (configNode.valueOf("method").equals("head")) { //HEAD HttpHead httpRequest = new HttpHead(configNode.valueOf("url")); response = executeRequest(httpRequest, httpclient); } else if (configNode.valueOf("method").equals("options")) { //OPTIONS HttpOptions httpRequest = new HttpOptions(configNode.valueOf("url")); response = executeRequest(httpRequest, httpclient); } else { //Default = GET HttpGet httpRequest = new HttpGet(configNode.valueOf("url")); String acceptHeader = configNode.valueOf("accept"); if (!acceptHeader.isEmpty()) { httpRequest.addHeader("accept", configNode.valueOf("accept")); } //Add proxy route if needed HttpClientProperties.setProxy(httpRequest, theURL.getHost()); response = executeRequest(httpRequest, httpclient); } try { contentHandler.startDocument(); int status = response.getStatusLine().getStatusCode(); AttributesImpl statusAttr = new AttributesImpl(); statusAttr.addAttribute("", "status", "status", "CDATA", Integer.toString(status)); contentHandler.startElement("", "response", "response", statusAttr); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); Header contentType = response.getFirstHeader("Content-Type"); if (entity != null && contentType != null) { //logger.info("Contenttype: " + contentType.getValue()); //Read content into inputstream InputStream inStream = entity.getContent(); // output-type = json means: response is json, convert to xml if (configNode.valueOf("output-type").equals("json")) { //TODO: net.sf.json.JSONObject might nog be the correct JSONObject. javax.json.JsonObject might be better!!! //javax.json contains readers to read from an inputstream StringWriter writer = new StringWriter(); IOUtils.copy(inStream, writer, "UTF-8"); JSONObject json = JSONObject.fromObject(writer.toString()); parseJSONObject(contentHandler, json); // output-type = xml means: response is xml, keep it } else if (configNode.valueOf("output-type").equals("xml")) { try { XMLReader saxParser = XMLParsing .newXMLReader(new XMLParsing.ParserConfiguration(false, false, false)); saxParser.setContentHandler(new ParseHandler(contentHandler)); saxParser.parse(new InputSource(inStream)); } catch (Exception e) { throw new OXFException(e); } // output-type = jsonld means: reponse is json-ld, (a) convert to nquads; (b) convert to xml } else if (configNode.valueOf("output-type").equals("jsonld")) { try { Object jsonObject = JsonUtils.fromInputStream(inStream, "UTF-8"); //TODO: UTF-8 should be read from response! Object nquads = JsonLdProcessor.toRDF(jsonObject, new NQuadTripleCallback()); Any23 runner = new Any23(); DocumentSource source = new StringDocumentSource((String) nquads, configNode.valueOf("url")); ByteArrayOutputStream out = new ByteArrayOutputStream(); TripleHandler handler = new RDFXMLWriter(out); try { runner.extract(source, handler); } finally { handler.close(); } ByteArrayInputStream inJsonStream = new ByteArrayInputStream(out.toByteArray()); XMLReader saxParser = XMLParsing .newXMLReader(new XMLParsing.ParserConfiguration(false, false, false)); saxParser.setContentHandler(new ParseHandler(contentHandler)); saxParser.parse(new InputSource(inJsonStream)); } catch (Exception e) { throw new OXFException(e); } // output-type = rdf means: response is some kind of rdf (except json-ld...), convert to xml } else if (configNode.valueOf("output-type").equals("rdf")) { try { Any23 runner = new Any23(); DocumentSource source; //If contentType = text/html than convert from html to xhtml to handle non-xml style html! logger.info("Contenttype: " + contentType.getValue()); if (configNode.valueOf("tidy").equals("yes") && contentType.getValue().startsWith("text/html")) { org.jsoup.nodes.Document doc = Jsoup.parse(inStream, "UTF-8", configNode.valueOf("url")); //TODO UTF-8 should be read from response! RDFCleaner cleaner = new RDFCleaner(); org.jsoup.nodes.Document cleandoc = cleaner.clean(doc); cleandoc.outputSettings().escapeMode(Entities.EscapeMode.xhtml); cleandoc.outputSettings() .syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml); cleandoc.outputSettings().charset("UTF-8"); source = new StringDocumentSource(cleandoc.html(), configNode.valueOf("url"), contentType.getValue()); } else { source = new ByteArrayDocumentSource(inStream, configNode.valueOf("url"), contentType.getValue()); } ByteArrayOutputStream out = new ByteArrayOutputStream(); TripleHandler handler = new RDFXMLWriter(out); try { runner.extract(source, handler); } finally { handler.close(); } ByteArrayInputStream inAnyStream = new ByteArrayInputStream(out.toByteArray()); XMLReader saxParser = XMLParsing .newXMLReader(new XMLParsing.ParserConfiguration(false, false, false)); saxParser.setContentHandler(new ParseHandler(contentHandler)); saxParser.parse(new InputSource(inAnyStream)); } catch (Exception e) { throw new OXFException(e); } } else { CharArrayWriter writer = new CharArrayWriter(); IOUtils.copy(inStream, writer, "UTF-8"); contentHandler.characters(writer.toCharArray(), 0, writer.size()); } } } contentHandler.endElement("", "response", "response"); contentHandler.endDocument(); } finally { response.close(); } } finally { httpclient.close(); } } catch (Exception e) { throw new OXFException(e); } }
From source file:org.apache.http.impl.client.cache.CachingExec.java
CloseableHttpResponse negotiateResponseFromVariants(final HttpRoute route, final HttpRequestWrapper request, final HttpClientContext context, final HttpExecutionAware execAware, final Map<String, Variant> variants) throws IOException, HttpException { final HttpRequestWrapper conditionalRequest = conditionalRequestBuilder .buildConditionalRequestFromVariants(request, variants); final Date requestDate = getCurrentDate(); final CloseableHttpResponse backendResponse = backend.execute(route, conditionalRequest, context, execAware);/* w ww . j av a 2 s. com*/ try { final Date responseDate = getCurrentDate(); backendResponse.addHeader("Via", generateViaHeader(backendResponse)); if (backendResponse.getStatusLine().getStatusCode() != HttpStatus.SC_NOT_MODIFIED) { return handleBackendResponse(route, request, context, execAware, requestDate, responseDate, backendResponse); } final Header resultEtagHeader = backendResponse.getFirstHeader(HeaderConstants.ETAG); if (resultEtagHeader == null) { log.warn("304 response did not contain ETag"); IOUtils.consume(backendResponse.getEntity()); backendResponse.close(); return callBackend(route, request, context, execAware); } final String resultEtag = resultEtagHeader.getValue(); final Variant matchingVariant = variants.get(resultEtag); if (matchingVariant == null) { log.debug("304 response did not contain ETag matching one sent in If-None-Match"); IOUtils.consume(backendResponse.getEntity()); backendResponse.close(); return callBackend(route, request, context, execAware); } final HttpCacheEntry matchedEntry = matchingVariant.getEntry(); if (revalidationResponseIsTooOld(backendResponse, matchedEntry)) { IOUtils.consume(backendResponse.getEntity()); backendResponse.close(); return retryRequestUnconditionally(route, request, context, execAware, matchedEntry); } recordCacheUpdate(context); final HttpCacheEntry responseEntry = getUpdatedVariantEntry(context.getTargetHost(), conditionalRequest, requestDate, responseDate, backendResponse, matchingVariant, matchedEntry); backendResponse.close(); final CloseableHttpResponse resp = responseGenerator.generateResponse(responseEntry); tryToUpdateVariantMap(context.getTargetHost(), request, matchingVariant); if (shouldSendNotModifiedResponse(request, responseEntry)) { return responseGenerator.generateNotModifiedResponse(responseEntry); } return resp; } catch (final IOException ex) { backendResponse.close(); throw ex; } catch (final RuntimeException ex) { backendResponse.close(); throw ex; } }
From source file:org.artifactory.bintray.BintrayServiceImpl.java
@Override public BintrayItemSearchResults<BintrayItemInfo> searchByName(String query, @Nullable Map<String, String> headersMap) throws IOException, BintrayException { String requestUrl = getBaseBintrayApiUrl() + "search/file/?subject=bintray&repo=jcenter&name=" + query; log.debug("requestUrl=\"" + requestUrl + "\""); try (CloseableHttpClient client = createHTTPClient(new UsernamePasswordCredentials("", ""))) { HttpGet getMethod = createGetMethod(requestUrl, headersMap); CloseableHttpResponse response = client.execute(getMethod); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { throw new BintrayException(response.getStatusLine().getReasonPhrase(), statusCode); } else {/*from w w w . j av a 2s . c om*/ try { int rangeLimitTotal = Integer.parseInt(response.getFirstHeader(RANGE_LIMIT_TOTAL).getValue()); InputStream responseStream = response.getEntity().getContent(); List<BintrayItemInfo> listResult = JacksonReader.streamAsValueTypeReference(responseStream, new TypeReference<List<BintrayItemInfo>>() { }); List<BintrayItemInfo> distinctResults = listResult.stream().distinct() .collect(Collectors.toList()); BintrayItemSearchResults<BintrayItemInfo> results = new BintrayItemSearchResults<>( distinctResults, rangeLimitTotal); fillLocalRepoPaths(distinctResults); fixDateFormat(distinctResults); return results; } finally { IOUtils.closeQuietly(response); } } } }
From source file:org.artifactory.bintray.BintrayServiceImpl.java
private BintrayItemInfo getBintrayItemInfoByChecksum(final String sha1, @Nullable Map<String, String> headersMap) { String itemInfoRequest = String.format("%ssearch/file/?sha1=%s&subject=bintray&repo=jcenter", getBaseBintrayApiUrl(), sha1); BintrayItemInfo result = null;/* w ww .j av a 2 s . com*/ CloseableHttpClient client = getUserOrSystemApiKeyHttpClient(); CloseableHttpResponse response = null; try { log.debug("Bintray item request:{}", itemInfoRequest); HttpGet getMethod = createGetMethod(itemInfoRequest, headersMap); response = client.execute(getMethod); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { if (statusCode == HttpStatus.SC_UNAUTHORIZED) { String userName = getCurrentUser().getUsername(); log.info("Bintray authentication failure: item {}, user {}", sha1, userName); } else { log.info("Bintray request info failure for item {}", sha1); } } else { int rangeLimitTotal = Integer.parseInt(response.getFirstHeader(RANGE_LIMIT_TOTAL).getValue()); InputStream responseStream = response.getEntity().getContent(); List<BintrayItemInfo> listResult = JacksonReader.streamAsValueTypeReference(responseStream, new TypeReference<List<BintrayItemInfo>>() { }); BintrayItemSearchResults<BintrayItemInfo> results = new BintrayItemSearchResults<>(listResult, rangeLimitTotal); if (results.getResults().size() > 0) { result = results.getResults().get(0); } else { log.debug("No item found for request: {}", itemInfoRequest); } } } catch (Exception e) { log.warn("Failure during Bintray fetching package {}: {}", sha1, e.getMessage()); log.debug("Failure during Bintray fetching package {}: {}", sha1, e); } finally { IOUtils.closeQuietly(response); IOUtils.closeQuietly(client); } return result; }
From source file:org.artifactory.rest.common.service.ResearchService.java
/** * Fetches ARTIFACTORY_ID header from {@link CloseableHttpResponse} * * @param response//w w w . jav a 2 s . c o m * * @return ArtifactoryId if present or null */ @Nullable private String getArtifactoryId(CloseableHttpResponse response) { assert response != null : "HttpResponse cannot be empty"; Header artifactoryIdHeader = response.getFirstHeader(ArtifactoryResponse.ARTIFACTORY_ID); if (artifactoryIdHeader != null && !Strings.isNullOrEmpty(artifactoryIdHeader.getValue())) { return artifactoryIdHeader.getValue(); } return null; }
From source file:org.fcrepo.integration.http.api.FedoraVersioningIT.java
private static void assertNoMementoDatetimeHeaderPresent(final CloseableHttpResponse response) { assertNull("No memento datetime header set in response", response.getFirstHeader(MEMENTO_DATETIME_HEADER)); }