Example #1
0
 @SuppressWarnings("deprecation")
 /* package */ static void setConnectionParametersForRequest(
     HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError {
   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.");
   }
 }
Example #2
0
 static HttpUriRequest createMultiPartRequest(
     Request<?> request, Map<String, String> additionalHeaders) throws AuthFailureError {
   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.getBody();
         if (postBody != null) {
           HttpPost postRequest = new HttpPost(request.getUrl());
           if (request.getBodyContentType() != null)
             postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
           HttpEntity entity;
           entity = new ByteArrayEntity(postBody);
           postRequest.setEntity(entity);
           return postRequest;
         } else {
           return new HttpGet(request.getUrl());
         }
       }
     case Method.GET:
       return new HttpGet(request.getUrl());
     case Method.DELETE:
       return new HttpDelete(request.getUrl());
     case Method.POST:
       {
         HttpPost postRequest = new HttpPost(request.getUrl());
         if (request.getBodyContentType() != null) {
           postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
         }
         setMultiPartBody(postRequest, request);
         return postRequest;
       }
     case Method.PUT:
       {
         HttpPut putRequest = new HttpPut(request.getUrl());
         if (request.getBodyContentType() != null)
           putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
         setMultiPartBody(putRequest, request);
         return putRequest;
       }
       // Added in source code of Volley libray.
     case Method.PATCH:
       {
         HttpPatch patchRequest = new HttpPatch(request.getUrl());
         if (request.getBodyContentType() != null)
           patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
         return patchRequest;
       }
     default:
       throw new IllegalStateException("Unknown request method.");
   }
 }
Example #3
0
 private static void dispatchToQueue(Request pRequest, Context pContext) {
   if (!isNetworkConnected(pContext) && pRequest.getMethod() != Request.Method.GET) {
     // non-GET request should not return cached response in case of no network
     pRequest.deliverError(new VolleyError(RESPONSE_NO_INTERNET));
     return;
   }
   switch (pRequest.getMethod()) {
     case Request.Method.POST:
       {
         pRequest.setRetryPolicy(RETRY_POLICY_POST);
         break;
       }
     case Request.Method.GET:
     default:
       {
         pRequest.setRetryPolicy(RETRY_POLICY_GET);
         break;
       }
   }
   mRequestQueue.add(pRequest);
 }
  @SuppressWarnings("deprecation")
  private static void setConnectionParametersForRequest(
      com.squareup.okhttp.Request.Builder builder, Request<?> request)
      throws IOException, AuthFailureError {
    switch (request.getMethod()) {
      case Request.Method.DEPRECATED_GET_OR_POST:
        // Ensure backwards compatibility.
        // Volley assumes a request with a null body is a GET.
        byte[] postBody = request.getPostBody();

        if (postBody != null) {
          builder.post(
              RequestBody.create(MediaType.parse(request.getPostBodyContentType()), postBody));
        }
        break;

      case Request.Method.GET:
        builder.get();
        break;

      case Request.Method.DELETE:
        builder.delete();
        break;

      case Request.Method.POST:
        builder.post(createRequestBody(request));
        break;

      case Request.Method.PUT:
        builder.put(createRequestBody(request));
        break;

      case Request.Method.HEAD:
        builder.head();
        break;

      case Request.Method.OPTIONS:
        builder.method("OPTIONS", null);
        break;

      case Request.Method.TRACE:
        builder.method("TRACE", null);
        break;

      case Request.Method.PATCH:
        builder.patch(createRequestBody(request));
        break;

      default:
        throw new IllegalStateException("Unknown method type.");
    }
  }
Example #5
0
 @Override
 public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
     throws IOException, AuthFailureError {
   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);
   if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
     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;
 }