Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
jerry-http - Common Java Functionality Copyright (c) 2012-2014, Sandeep Gupta http://sangupta.com/projects/jerry-http Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package com.sangupta.jerry.http;
 
 
 
org.apache.http.client.ResponseHandler class that handles the extract the WebResponse object from the provided org.apache.http.HttpResponse object.

Author(s):
sangupta
Since:
0.3
 
 public class WebResponseHandler implements HttpResponseHandler<WebResponse> {

 
 	public WebResponse handleResponse(HttpResponse responseHttpContext localHttpContextthrows ClientProtocolExceptionIOException {
 		StatusLine statusLine = response.getStatusLine();
         HttpEntity entity = response.getEntity();
         
         byte[] bytes = null;
         if(entity != null) {
         	bytes = EntityUtils.toByteArray(entity);
         }
 		final WebResponse webResponse = new WebResponse(bytes);
 		
 		// decipher from status line
 		webResponse.responseCode = statusLine.getStatusCode();
 		webResponse.message = statusLine.getReasonPhrase();
 		
 		// set size
 		if(entity != null) {
 			webResponse.size = entity.getContentLength();
         } else {
     		long value = 0;
         	Header header = response.getFirstHeader(.);
         	if(header != null) {
         		String headerValue = header.getValue();
             	if(AssertUtils.isNotEmpty(headerValue)) {
             		try {
             			value = Long.parseLong(headerValue);
             		} catch(Exception e) {
             			// eat the exception
             		}
             	}
         	}
         	
         	webResponse.size = value;
         }
 		
 		// content type
 		if(entity != null && entity.getContentType() != null) {
         	webResponse.contentType = entity.getContentType().getValue();
         }
 		
 		// response headers
 		final Header[] responseHeaders = response.getAllHeaders();
         if(AssertUtils.isNotEmpty(responseHeaders)) {
         	for(Header header : responseHeaders) {
         		webResponse.headers.put(header.getName(), header.getValue());
        	}
        }
		// charset
        try {
			ContentType type = ContentType.get(entity);
			if(type != null) {
				webResponse.charSet = type.getCharset();
			}
        } catch(UnsupportedCharsetException e) {
        	// we are unable to find the charset for the content
        	// let's leave it to be considered binary
        }
        
        // fill in the redirect uri chain
        RedirectLocations locations = (RedirectLocationslocalHttpContext.getAttribute(.);
        if(AssertUtils.isNotEmpty(locations)) {
        	webResponse.setRedirectChain(locations.getAll());
        }
        
		// return the object finally
        return webResponse;
	}
New to GrepCode? Check out our FAQ X