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;
 
 import java.net.URI;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
Class that encapsulates the response information obtained from invoking a HTTP URL.

Author(s):
sangupta
Since:
0.9.0
 
 public class WebResponse implements SerializableCloseable {

Generated using Eclipse
 
 	private static final long serialVersionUID = 5896497905018410580L;

The response code returned by the webservice invocation.
 
     int responseCode;
    
    
The response message returned by the webservice invocation.
 
     String message;
    
    
The response body returned by the webservice invocation.
 
     private byte[] bytes;
    
    
The charset of the content received
 
     Charset charSet;
    
    
The content-type as specified by the server
 
     String contentType;
    
    
The response headers received
 
     final Map<StringStringheaders = new HashMap<StringString>();
    
    
The redirect chain of java.net.URIs
 
     List<URIredirectChain;
    
    
The size of the response
 
     long size;
     
     public WebResponse(byte[] bytes) {
     	this. = bytes;
     }
    
    
Set the redirect chain

Parameters:
redirectChain
	void setRedirectChain(List<URIredirectChain) {
		this. = redirectChain;
	}
    @Override
    public void close() {
    	try {
    		InputStream stream = this.asStream();
    		if(stream != null) {
    			stream.close();
    		}
    	} catch(Exception e) {
    		// eat up
    	}
    }
    
    
Read the response stream as a java.lang.String object considering UTF-8 encoding.

Returns:
    public String getContent() {
    	return asString(.);
    }
    
    
Read the response stream as a java.lang.String object considering the given java.nio.charset.Charset encoding

Parameters:
charset
Returns:
    public String asContent(Charset charset) {
    	return asString(charset);
    }
    
    
Returns the fetched response as a java.lang.String parsed using the response java.nio.charset.Charset or the provided java.nio.charset.Charset if none is specified.

Parameters:
charset the java.nio.charset.Charset to be used for deconding the response
Returns:
the string representation of the string
    public String asString(Charset charset) {
    	if(this. != null) {
            try {
                if (this. != null) {
	                return new String(this.this..name());
                }
                
                return new String(this.charset);
            } catch (UnsupportedEncodingException ex) {
                // eat up
            }
            
            return new String(this.);
    	}
    	
    	return null;
    }
    
    
Returns the fetched response as an java.io.InputStream.

Returns:
the java.io.InputStream of the response
    public InputStream asStream() {
    	if(this. != null) {
    		return new ByteArrayInputStream(this.);
    	}
    	
    	return null;
    }
    
    
Return the fetched response as a byte-array. The returned byte-array is the actual byte-array. Any modification to the same, will cause a change in the subsequent call to asStream(), getContent() or asString(java.nio.charset.Charset) methods.

Returns:
the byte array representation of the response
    public byte[] asBytes() {
    	return this.;
    }
    
    
Return the fetched response as a byte-array. The returned byte-array is a clone of the response array.

Returns:
the byte array representation of the response
    public byte[] asClonedBytes() {
    	if(this. == null) {
    		return null;
    	}
    	
    	return this..clone();
    }
    
    
Return the last modified date as a long value

Returns:
the millis timestamp representing the last modified header, or -1 if the header is not present or cannot be parsed successfully
    public long getLastModified() {
    	String headerValue = getHeader(.);
    	if(headerValue != null) {
    		try {
    			return Date.parse(headerValue);
    		} catch(Exception e) {
    			// eat this up
    		}
    	}
    	
    	return -1;
    }
    
    
Check if the request returned a successful response. All response codes between HTTP 200 and HTTP 299 are considered to be successful.

Returns:
true for success, false otherwise
    public boolean isSuccess() {
    	if(this. >= 200 && this. <= 299) {
    		return true;
    	}
    	
    	return false;
    }
    
    
Check if the response indicates a redirected request. All response codes between HTTP 300 and HTTP 399 are considered to be a redirect.

Returns:
true if redirect response, false otherwise
    public boolean isRedirect() {
    	if(this. >= 300 && this. <= 399) {
    		return true;
    	}
    	
    	return false;
    }
    
    
Check if the response indicates an erroneous client request. All response codes between HTTP 400 and HTTP 499 are considered to be a client error.

Returns:
true if client error is indicated, false otherwise
    public boolean isClientError() {
    	if(this. >= 400 && this. <= 499) {
    		return true;
    	}
    	
    	return false;
    }
    
    
Check if the response indicates an error on the server. All response codes between HTTP 500 and HTTP 599 are considered to be a server error.

Returns:
true if server error is indicated, false otherwise
    public boolean isServerError() {
    	if(this. >= 500 && this. <= 599) {
    		return true;
    	}
    	
    	return false;
    }
    
    
Return the value of the header if present, or null

Parameters:
headerName the name of the header
Returns:
the value of the header, or null if header is not present
    private String getHeader(String headerName) {
    	if( == null) {
    		return null;
    	}
    	
    	return .get(headerName);
    }
    
    
    @Override
    public String toString() {
    	return this. + " " + this.;
    }
    
    
Utility function that returns a string representation of this response which can be used for debugging purposes. Should not be used in production code as is slow.

Returns:
debug trace information represented as string
    public String trace() {
    	StringBuilder builder = new StringBuilder();
    	
    	builder.append("[Response: code=");
    	builder.append(this.);
    	
    	builder.append(", message=");
    	builder.append(this.);
    	
    	builder.append(", contentType=");
    	builder.append(this.);
    	
    	builder.append(", size=");
    	builder.append(this.);
    	
    	builder.append("]");
    	return builder.toString();
    }
    
    // Usual accessor's follow

    
Returns the responseCode.

Returns:
the responseCode.
    public int getResponseCode() {
        return this.;
    }

Returns:
the size
	public long getSize() {
		return ;
	}

Returns:
the bytes
	public byte[] getBytes() {
		return ;
	}

Returns:
the message
	public String getMessage() {
		return ;
	}

Returns:
the headers
	public Map<StringStringgetHeaders() {
		return Collections.unmodifiableMap();
	}

Returns:
the contentType
	public String getContentType() {
		return ;
	}

Returns:
the charSet
	public Charset getCharSet() {
		return ;
	}

Returns:
the redirectChain
	public List<URIgetRedirectChain() {
	}

Indicates if the response was fetched from a redirected resource.

Returns:
	public boolean hasRedirects() {
		return !this..isEmpty();
	}

Returns the final URL that was used to hit a resource.

Returns:
	public URI getFinalURI() {
		if(!this.hasRedirects()) {
			return null;
		}
		return this..get(this..size() - 1);
	}
New to GrepCode? Check out our FAQ X