List of usage examples for org.apache.commons.httpclient HttpMethod getResponseHeaders
public abstract Header[] getResponseHeaders(String paramString);
From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java
/** * @param method Method that got a 401./* w w w . j a va2s . c om*/ * @param curi CrawlURI that got a 401. * @return Returns first wholesome authscheme found else null. */ protected AuthScheme getAuthScheme(final HttpMethod method, final CrawlURI curi) { Header[] headers = method.getResponseHeaders("WWW-Authenticate"); if (headers == null || headers.length <= 0) { logger.info("We got a 401 but no WWW-Authenticate challenge: " + curi.toString()); return null; } Map authschemes = null; try { authschemes = AuthChallengeParser.parseChallenges(headers); } catch (MalformedChallengeException e) { logger.info("Failed challenge parse: " + e.getMessage()); } if (authschemes == null || authschemes.size() <= 0) { logger.info("We got a 401 and WWW-Authenticate challenge" + " but failed parse of the header " + curi.toString()); return null; } AuthScheme result = null; // Use the first auth found. for (Iterator i = authschemes.keySet().iterator(); result == null && i.hasNext();) { String key = (String) i.next(); String challenge = (String) authschemes.get(key); if (key == null || key.length() <= 0 || challenge == null || challenge.length() <= 0) { logger.warn("Empty scheme: " + curi.toString() + ": " + headers); } AuthScheme authscheme = null; if (key.equals("basic")) { authscheme = new BasicScheme(); } else if (key.equals("digest")) { authscheme = new DigestScheme(); } else { logger.info("Unsupported scheme: " + key); continue; } try { authscheme.processChallenge(challenge); } catch (MalformedChallengeException e) { logger.info(e.getMessage() + " " + curi + " " + headers); continue; } if (authscheme.isConnectionBased()) { logger.info("Connection based " + authscheme); continue; } if (authscheme.getRealm() == null || authscheme.getRealm().length() <= 0) { logger.info("Empty realm " + authscheme + " for " + curi); continue; } result = authscheme; } return result; }
From source file:org.openhab.binding.nest.internal.messages.AbstractRequest.java
/** * Executes the given <code>url</code> with the given <code>httpMethod</code>. In the case of httpMethods that do * not support automatic redirection, manually handle the HTTP temporary redirect (307) and retry with the new URL. * //from w w w.j a v a2 s .c o m * @param httpMethod * the HTTP method to use * @param url * the url to execute (in milliseconds) * @param contentString * the content to be sent to the given <code>url</code> or <code>null</code> if no content should be * sent. * @param contentType * the content type of the given <code>contentString</code> * @return the response body or <code>NULL</code> when the request went wrong */ protected final String executeUrl(final String httpMethod, final String url, final String contentString, final String contentType) { HttpClient client = new HttpClient(); HttpMethod method = HttpUtil.createHttpMethod(httpMethod, url); method.getParams().setSoTimeout(HTTP_REQUEST_TIMEOUT); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); for (String httpHeaderKey : HTTP_HEADERS.stringPropertyNames()) { method.addRequestHeader(new Header(httpHeaderKey, HTTP_HEADERS.getProperty(httpHeaderKey))); } // add content if a valid method is given ... if (method instanceof EntityEnclosingMethod && contentString != null) { EntityEnclosingMethod eeMethod = (EntityEnclosingMethod) method; InputStream content = new ByteArrayInputStream(contentString.getBytes()); eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType)); } if (logger.isDebugEnabled()) { try { logger.trace("About to execute '" + method.getURI().toString() + "'"); } catch (URIException e) { logger.trace(e.getMessage()); } } try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_ACCEPTED) { // perfectly fine but we cannot expect any answer... return null; } // Manually handle 307 redirects with a little tail recursion if (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) { Header[] headers = method.getResponseHeaders("Location"); String newUrl = headers[headers.length - 1].getValue(); return executeUrl(httpMethod, newUrl, contentString, contentType); } if (statusCode != HttpStatus.SC_OK) { logger.warn("Method failed: " + method.getStatusLine()); } InputStream tmpResponseStream = method.getResponseBodyAsStream(); Header encodingHeader = method.getResponseHeader("Content-Encoding"); if (encodingHeader != null) { for (HeaderElement ehElem : encodingHeader.getElements()) { if (ehElem.toString().matches(".*gzip.*")) { tmpResponseStream = new GZIPInputStream(tmpResponseStream); logger.trace("GZipped InputStream from {}", url); } else if (ehElem.toString().matches(".*deflate.*")) { tmpResponseStream = new InflaterInputStream(tmpResponseStream); logger.trace("Deflated InputStream from {}", url); } } } String responseBody = IOUtils.toString(tmpResponseStream); if (!responseBody.isEmpty()) { logger.trace(responseBody); } return responseBody; } catch (HttpException he) { logger.error("Fatal protocol violation: {}", he.toString()); } catch (IOException ioe) { logger.error("Fatal transport error: {}", ioe.toString()); } finally { method.releaseConnection(); } return null; }
From source file:org.openrdf.http.client.HTTPClient.java
/** * Gets the MIME type specified in the response headers of the supplied * method, if any. For example, if the response headers contain * <tt>Content-Type: application/xml;charset=UTF-8</tt>, this method will * return <tt>application/xml</tt> as the MIME type. * // w w w. ja v a 2 s .c o m * @param method * The method to get the reponse MIME type from. * @return The response MIME type, or <tt>null</tt> if not available. */ protected String getResponseMIMEType(HttpMethod method) throws IOException { Header[] headers = method.getResponseHeaders("Content-Type"); for (Header header : headers) { HeaderElement[] headerElements = header.getElements(); for (HeaderElement headerEl : headerElements) { String mimeType = headerEl.getName(); if (mimeType != null) { logger.debug("reponse MIME type is {}", mimeType); return mimeType; } } } return null; }
From source file:org.openrdf.repository.sparql.query.SPARQLGraphQuery.java
private RDFParser getParser(HttpMethod response) { for (Header header : response.getResponseHeaders("Content-Type")) { for (HeaderElement headerEl : header.getElements()) { String mimeType = headerEl.getName(); if (mimeType != null) { RDFFormat format = registry.getFileFormatForMIMEType(mimeType); RDFParserFactory factory = registry.get(format); if (factory != null) return factory.getParser(); }// w w w . j a v a 2 s . co m } } throw new UnsupportedQueryResultFormatException( "No parser factory available for this graph query result format"); }