Exemplo n.º 1
0
 @SuppressWarnings("deprecation")
 /* package */ static void setConnectionParametersForRequest(
     HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError {
   switch (request.getMethod()) {
     case Request.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 Request.Method.GET:
       // Not necessary to set the request method because connection defaults to GET but
       // being explicit here.
       connection.setRequestMethod("GET");
       break;
     case Request.Method.DELETE:
       connection.setRequestMethod("DELETE");
       break;
     case Request.Method.POST:
       connection.setRequestMethod("POST");
       addBodyIfExists(connection, request);
       break;
     case Request.Method.PUT:
       connection.setRequestMethod("PUT");
       addBodyIfExists(connection, request);
       break;
     case Request.Method.HEAD:
       connection.setRequestMethod("HEAD");
       break;
     case Request.Method.OPTIONS:
       connection.setRequestMethod("OPTIONS");
       break;
     case Request.Method.TRACE:
       connection.setRequestMethod("TRACE");
       break;
     case Request.Method.PATCH:
       connection.setRequestMethod("PATCH");
       addBodyIfExists(connection, request);
       break;
     default:
       throw new IllegalStateException("Unknown method type.");
   }
 }