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.io.File;
 
A wrapper over the org.apache.http.HttpResponse object that is returned as part of org.apache.http.HttpRequest execution. This helps in extracting the final WebResponse object.

Author(s):
sangupta
Since:
0.3
 
 public class WebRawResponse {

 
 	private final HttpResponse response;

 
 	private final HttpContext localHttpContext;

Flag that signifies if the response stream has been consumed or not.
 
     private boolean consumed;

    
Constructor that takes a org.apache.http.HttpResponse object and stores it internally for processing.

Parameters:
response the actual org.apache.http.HttpResponse object that was returned from the server
localHttpContext the local org.apache.http.protocol.HttpContext as applicable to this request
 
     WebRawResponse(final HttpResponse responseHttpContext localHttpContext) {
         super();
         this. = response;
         this. = localHttpContext;
     }

    
Check if the response stream has not already been consumed.
 
     private void assertNotConsumed() {
         if (this.) {
             throw new IllegalStateException("Response content has been already consumed");
         }
     }

    
Dispose off the response, after consuming.
 
    private void dispose() {
        if (this.) {
            return;
        }
        try {
            EntityUtils.consume(this..getEntity());
        } catch (Exception ignore) {
        } finally {
            this. = true;
        }
    }

    
Discard any content off the response stream, if pending.
    public void discardContent() {
        dispose();
    }

    
Handle the response using the given response handler.

Parameters:
handler the handler to use
Returns:
the handled response return
Throws:
org.apache.http.client.ClientProtocolException if something fails
java.io.IOException if something fails
    public <T> T handleResponse(final HttpResponseHandler<T> handlerthrows ClientProtocolExceptionIOException {
        assertNotConsumed();
        try {
            return handler.handleResponse(this.this.);
        } finally {
            dispose();
        }
    }

    
Convert the response to a WebResponse object. The method will never return a null.

Returns:
the WebResponse object
Throws:
org.apache.http.client.ClientProtocolException if something fails
java.io.IOException if something fails
        return handleResponse(new WebResponseHandler());
    }

    
Return the org.apache.http.HttpResponse object by reading the entire response stream as byte-array.

Returns:
the org.apache.http.HttpResponse object
Throws:
java.io.IOException if something fails
    public HttpResponse httpResponse() throws IOException {
        assertNotConsumed();
        try {
            HttpEntity entity = this..getEntity();
            if (entity != null) {
                this..setEntity(new ByteArrayEntity(EntityUtils.toByteArray(entity), ContentType.getOrDefault(entity)));
            }
            return this.;
        } finally {
            this. = true;
        }
    }
    
    
Write the response stream to the given file. If the HTTP status code is greater than or equal to HTTP 300, an org.apache.http.client.HttpResponseException is thrown.

Parameters:
file the file to write the response to.
Throws:
java.io.IOException if something fails during HTTP connection
org.apache.http.client.HttpResponseException if the HTTP status code is greater than or equal to HTTP 300
java.lang.NullPointerException if the file to which the response needs to be written is null
    public void writeToFile(final File filethrows IOException {
        assertNotConsumed();
        StatusLine statusLine = .getStatusLine();
        if (statusLine.getStatusCode() >= 300) {
            throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
        }
        
        BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(file));
        try {
            HttpEntity entity = this..getEntity();
            if (entity != null) {
                entity.writeTo(bout);
            }
        } finally {
            this. = true;
            bout.close();
        }
    }
New to GrepCode? Check out our FAQ X