FileDocCategorySizeDatePackage
HurlStack.javaAPI DocAndroid 5.1 API9636Thu Mar 12 22:22:56 GMT 2015com.android.volley.toolbox

HurlStack

public class HurlStack extends Object implements HttpStack
An {@link HttpStack} based on {@link HttpURLConnection}.

Fields Summary
private static final String
HEADER_CONTENT_TYPE
private final UrlRewriter
mUrlRewriter
private final SSLSocketFactory
mSslSocketFactory
Constructors Summary
public HurlStack()


      
        this(null);
    
public HurlStack(UrlRewriter urlRewriter)

param
urlRewriter Rewriter to use for request URLs

        this(urlRewriter, null);
    
public HurlStack(UrlRewriter urlRewriter, SSLSocketFactory sslSocketFactory)

param
urlRewriter Rewriter to use for request URLs
param
sslSocketFactory SSL factory to use for HTTPS connections

        mUrlRewriter = urlRewriter;
        mSslSocketFactory = sslSocketFactory;
    
Methods Summary
private static voidaddBodyIfExists(java.net.HttpURLConnection connection, com.android.volley.Request request)

        byte[] body = request.getBody();
        if (body != null) {
            connection.setDoOutput(true);
            connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(body);
            out.close();
        }
    
protected java.net.HttpURLConnectioncreateConnection(java.net.URL url)
Create an {@link HttpURLConnection} for the specified {@code url}.

        return (HttpURLConnection) url.openConnection();
    
private static org.apache.http.HttpEntityentityFromConnection(java.net.HttpURLConnection connection)
Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.

param
connection
return
an HttpEntity populated with data from connection.

        BasicHttpEntity entity = new BasicHttpEntity();
        InputStream inputStream;
        try {
            inputStream = connection.getInputStream();
        } catch (IOException ioe) {
            inputStream = connection.getErrorStream();
        }
        entity.setContent(inputStream);
        entity.setContentLength(connection.getContentLength());
        entity.setContentEncoding(connection.getContentEncoding());
        entity.setContentType(connection.getContentType());
        return entity;
    
private java.net.HttpURLConnectionopenConnection(java.net.URL url, com.android.volley.Request request)
Opens an {@link HttpURLConnection} with parameters.

param
url
return
an open connection
throws
IOException

        HttpURLConnection connection = createConnection(url);

        int timeoutMs = request.getTimeoutMs();
        connection.setConnectTimeout(timeoutMs);
        connection.setReadTimeout(timeoutMs);
        connection.setUseCaches(false);
        connection.setDoInput(true);

        // use caller-provided custom SslSocketFactory, if any, for HTTPS
        if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
            ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory);
        }

        return connection;
    
public org.apache.http.HttpResponseperformRequest(com.android.volley.Request request, java.util.Map additionalHeaders)

        String url = request.getUrl();
        HashMap<String, String> map = new HashMap<String, String>();
        map.putAll(request.getHeaders());
        map.putAll(additionalHeaders);
        if (mUrlRewriter != null) {
            String rewritten = mUrlRewriter.rewriteUrl(url);
            if (rewritten == null) {
                throw new IOException("URL blocked by rewriter: " + url);
            }
            url = rewritten;
        }
        URL parsedUrl = new URL(url);
        HttpURLConnection connection = openConnection(parsedUrl, request);
        for (String headerName : map.keySet()) {
            connection.addRequestProperty(headerName, map.get(headerName));
        }
        setConnectionParametersForRequest(connection, request);
        // Initialize HttpResponse with data from the HttpURLConnection.
        ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
        int responseCode = connection.getResponseCode();
        if (responseCode == -1) {
            // -1 is returned by getResponseCode() if the response code could not be retrieved.
            // Signal to the caller that something was wrong with the connection.
            throw new IOException("Could not retrieve response code from HttpUrlConnection.");
        }
        StatusLine responseStatus = new BasicStatusLine(protocolVersion,
                connection.getResponseCode(), connection.getResponseMessage());
        BasicHttpResponse response = new BasicHttpResponse(responseStatus);
        response.setEntity(entityFromConnection(connection));
        for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
            if (header.getKey() != null) {
                Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
                response.addHeader(h);
            }
        }
        return response;
    
static voidsetConnectionParametersForRequest(java.net.HttpURLConnection connection, com.android.volley.Request request)

        switch (request.getMethod()) {
            case Method.DEPRECATED_GET_OR_POST:
                // This is the deprecated way that needs to be handled for backwards compatibility.
                // If the request's post body is null, then the assumption is that the request is
                // GET.  Otherwise, it is assumed that the request is a POST.
                byte[] postBody = request.getPostBody();
                if (postBody != null) {
                    // Prepare output. There is no need to set Content-Length explicitly,
                    // since this is handled by HttpURLConnection using the size of the prepared
                    // output stream.
                    connection.setDoOutput(true);
                    connection.setRequestMethod("POST");
                    connection.addRequestProperty(HEADER_CONTENT_TYPE,
                            request.getPostBodyContentType());
                    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
                    out.write(postBody);
                    out.close();
                }
                break;
            case Method.GET:
                // Not necessary to set the request method because connection defaults to GET but
                // being explicit here.
                connection.setRequestMethod("GET");
                break;
            case Method.DELETE:
                connection.setRequestMethod("DELETE");
                break;
            case Method.POST:
                connection.setRequestMethod("POST");
                addBodyIfExists(connection, request);
                break;
            case Method.PUT:
                connection.setRequestMethod("PUT");
                addBodyIfExists(connection, request);
                break;
            case Method.HEAD:
                connection.setRequestMethod("HEAD");
                break;
            case Method.OPTIONS:
                connection.setRequestMethod("OPTIONS");
                break;
            case Method.TRACE:
                connection.setRequestMethod("TRACE");
                break;
            case Method.PATCH:
                connection.setRequestMethod("PATCH");
                addBodyIfExists(connection, request);
                break;
            default:
                throw new IllegalStateException("Unknown method type.");
        }