List of usage examples for org.apache.commons.httpclient HttpMethod getResponseHeader
public abstract Header getResponseHeader(String paramString);
From source file:org.apache.axis2.transport.http.impl.httpclient3.HTTPSenderImpl.java
/** * Collect the HTTP header information and set them in the message context * /*from w w w .j a va 2s. c om*/ * @param method * HttpMethodBase from which to get information * @param msgContext * the MessageContext in which to place the information... OR * NOT! * @throws AxisFault * if problems occur */ protected void obtainHTTPHeaderInformation(Object httpMethodBase, MessageContext msgContext) throws AxisFault { HttpMethod method; if (httpMethodBase instanceof HttpMethodBase) { method = (HttpMethod) httpMethodBase; } else { return; } // Set RESPONSE properties onto the REQUEST message context. They will // need to be copied off the request context onto // the response context elsewhere, for example in the // OutInOperationClient. Map transportHeaders = new HTTPTransportHeaders(method.getResponseHeaders()); msgContext.setProperty(MessageContext.TRANSPORT_HEADERS, transportHeaders); msgContext.setProperty(HTTPConstants.MC_HTTP_STATUS_CODE, new Integer(method.getStatusCode())); Header header = method.getResponseHeader(HTTPConstants.HEADER_CONTENT_TYPE); if (header != null) { HeaderElement[] headers = header.getElements(); MessageContext inMessageContext = msgContext.getOperationContext() .getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE); Object contentType = header.getValue(); Object charSetEnc = null; for (int i = 0; i < headers.length; i++) { NameValuePair charsetEnc = headers[i].getParameterByName(HTTPConstants.CHAR_SET_ENCODING); if (charsetEnc != null) { charSetEnc = charsetEnc.getValue(); } } if (inMessageContext != null) { inMessageContext.setProperty(Constants.Configuration.CONTENT_TYPE, contentType); inMessageContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc); } else { // Transport details will be stored in a HashMap so that anybody // interested can // retrieve them HashMap transportInfoMap = new HashMap(); transportInfoMap.put(Constants.Configuration.CONTENT_TYPE, contentType); transportInfoMap.put(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc); // the HashMap is stored in the outgoing message. msgContext.setProperty(Constants.Configuration.TRANSPORT_INFO_MAP, transportInfoMap); } } String sessionCookie = null; // Process old style headers first Header[] cookieHeaders = method.getResponseHeaders(HTTPConstants.HEADER_SET_COOKIE); String customCoookiId = (String) msgContext.getProperty(Constants.CUSTOM_COOKIE_ID); for (int i = 0; i < cookieHeaders.length; i++) { HeaderElement[] elements = cookieHeaders[i].getElements(); for (int e = 0; e < elements.length; e++) { HeaderElement element = elements[e]; if (Constants.SESSION_COOKIE.equalsIgnoreCase(element.getName()) || Constants.SESSION_COOKIE_JSESSIONID.equalsIgnoreCase(element.getName())) { sessionCookie = processCookieHeader(element); } if (customCoookiId != null && customCoookiId.equalsIgnoreCase(element.getName())) { sessionCookie = processCookieHeader(element); } } } // Overwrite old style cookies with new style ones if present cookieHeaders = method.getResponseHeaders(HTTPConstants.HEADER_SET_COOKIE2); for (int i = 0; i < cookieHeaders.length; i++) { HeaderElement[] elements = cookieHeaders[i].getElements(); for (int e = 0; e < elements.length; e++) { HeaderElement element = elements[e]; if (Constants.SESSION_COOKIE.equalsIgnoreCase(element.getName()) || Constants.SESSION_COOKIE_JSESSIONID.equalsIgnoreCase(element.getName())) { sessionCookie = processCookieHeader(element); } if (customCoookiId != null && customCoookiId.equalsIgnoreCase(element.getName())) { sessionCookie = processCookieHeader(element); } } } if (sessionCookie != null) { msgContext.getServiceContext().setProperty(HTTPConstants.COOKIE_STRING, sessionCookie); } }
From source file:org.apache.camel.component.http.HttpProducer.java
protected Exception populateHttpOperationFailedException(Exchange exchange, HttpMethod method, int responseCode) throws IOException, ClassNotFoundException { Exception answer;//w w w . ja v a 2s.c o m String uri = method.getURI().toString(); String statusText = method.getStatusLine() != null ? method.getStatusLine().getReasonPhrase() : null; Map<String, String> headers = extractResponseHeaders(method.getResponseHeaders()); Object responseBody = extractResponseBody(method, exchange); if (transferException && responseBody != null && responseBody instanceof Exception) { // if the response was a serialized exception then use that return (Exception) responseBody; } // make a defensive copy of the response body in the exception so its detached from the cache String copy = null; if (responseBody != null) { copy = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, responseBody); } if (responseCode >= 300 && responseCode < 400) { String redirectLocation; Header locationHeader = method.getResponseHeader("location"); if (locationHeader != null) { redirectLocation = locationHeader.getValue(); answer = new HttpOperationFailedException(uri, responseCode, statusText, redirectLocation, headers, copy); } else { // no redirect location answer = new HttpOperationFailedException(uri, responseCode, statusText, null, headers, copy); } } else { // internal server error (error code 500) answer = new HttpOperationFailedException(uri, responseCode, statusText, null, headers, copy); } return answer; }
From source file:org.apache.camel.component.http.HttpProducer.java
/** * Extracts the response from the method as a InputStream. * * @param method the method that was executed * @return the response either as a stream, or as a deserialized java object * @throws IOException can be thrown//w w w. j a v a 2 s . com */ protected static Object extractResponseBody(HttpMethod method, Exchange exchange) throws IOException, ClassNotFoundException { InputStream is = method.getResponseBodyAsStream(); if (is == null) { return null; } Header header = method.getResponseHeader(Exchange.CONTENT_ENCODING); String contentEncoding = header != null ? header.getValue() : null; if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { is = GZIPHelper.uncompressGzip(contentEncoding, is); } // Honor the character encoding String contentType = null; header = method.getResponseHeader("content-type"); if (header != null) { contentType = header.getValue(); // find the charset and set it to the Exchange HttpHelper.setCharsetFromContentType(contentType, exchange); } InputStream response = doExtractResponseBodyAsStream(is, exchange); // if content type is a serialized java object then de-serialize it back to a Java object if (contentType != null && contentType.equals(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT)) { return HttpHelper.deserializeJavaObjectFromStream(response); } else { return response; } }
From source file:org.apache.camel.component.jetty.EnableCORSTest.java
@Test public void testCORSdisabled() throws Exception { HttpClient httpclient = new HttpClient(); HttpMethod httpMethod = new GetMethod("http://localhost:" + getPort() + "/test1"); httpMethod.addRequestHeader("Origin", "http://localhost:9000"); httpMethod.addRequestHeader("Referer", "http://localhost:9000"); int status = httpclient.executeMethod(httpMethod); assertEquals("Get a wrong response status", 200, status); Header responseHeader = httpMethod.getResponseHeader("Access-Control-Allow-Credentials"); assertNull("Access-Control-Allow-Credentials HEADER should not be set", responseHeader); }
From source file:org.apache.camel.component.jetty.EnableCORSTest.java
@Test public void testCORSenabled() throws Exception { HttpClient httpclient = new HttpClient(); HttpMethod httpMethod = new GetMethod("http://localhost:" + getPort2() + "/test2"); httpMethod.addRequestHeader("Origin", "http://localhost:9000"); httpMethod.addRequestHeader("Referer", "http://localhost:9000"); int status = httpclient.executeMethod(httpMethod); assertEquals("Get a wrong response status", 200, status); Header responseHeader = httpMethod.getResponseHeader("Access-Control-Allow-Credentials"); assertTrue("CORS not enabled", Boolean.valueOf(responseHeader.getValue())); }
From source file:org.apache.camel.component.jetty.HttpHeaderCaseTest.java
@Test public void testHttpHeaderCase() throws Exception { HttpClient client = new HttpClient(); HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest"); method.setRequestHeader("clientHeader", "fooBAR"); method.setRequestHeader("OTHER", "123"); method.setRequestHeader("beer", "Carlsberg"); client.executeMethod(method);/* w ww.j a v a2s .c o m*/ assertEquals("Bye World", method.getResponseBodyAsString()); assertEquals("aBc123", method.getResponseHeader("MyCaseHeader").getValue()); assertEquals("456DEf", method.getResponseHeader("otherCaseHeader").getValue()); }
From source file:org.apache.excalibur.source.factories.HTTPClientSource.java
/** * Method to update the mime type of a resource after * executing a particular {@link HttpMethod}. * * @param method {@link HttpMethod} executed *//*from w w w. ja v a 2s .c o m*/ private void updateMimeType(final HttpMethod method) { // REVISIT: should this be the mime-type, or the content-type -> URLSource // returns the Content-Type, so we'll follow that for now. final Header header = method.getResponseHeader(CONTENT_TYPE); this.m_mimeType = header == null ? null : header.getValue(); }
From source file:org.apache.excalibur.source.factories.HTTPClientSource.java
/** * Method to update the content length of a resource after * executing a particular {@link HttpMethod}. * * @param method {@link HttpMethod} executed *//*ww w .j a v a 2s . co m*/ private void updateContentLength(final HttpMethod method) { try { final Header length = method.getResponseHeader(CONTENT_LENGTH); this.m_contentLength = length == null ? -1 : Long.parseLong(length.getValue()); } catch (final NumberFormatException e) { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("Unable to determine content length, returning -1", e); } this.m_contentLength = -1; } }
From source file:org.apache.excalibur.source.factories.HTTPClientSource.java
/** * Method to update the last modified date of a resource after * executing a particular {@link HttpMethod}. * * @param method {@link HttpMethod} executed *//* w ww. ja v a2 s .com*/ private void updateLastModified(final HttpMethod method) { final Header lastModified = method.getResponseHeader(LAST_MODIFIED); try { this.m_lastModified = lastModified == null ? 0 : DateUtil.parseDate(lastModified.getValue()).getTime(); } catch (DateParseException e) { // we ignore this exception and simply set last modified to 0 this.m_lastModified = 0; } }
From source file:org.apache.jmeter.protocol.http.control.CacheManager.java
/** * Helper method to deal with missing headers - Commons HttpClient * @param method Http method/*from w w w .j a v a2 s . c o m*/ * @param name Header name * @return Header value * @deprecated HC3.1 will be dropped in upcoming version */ @Deprecated private String getHeader(HttpMethod method, String name) { org.apache.commons.httpclient.Header hdr = method.getResponseHeader(name); return hdr != null ? hdr.getValue() : null; }