Statistics
| Branch: | Revision:

root / extensions / httpnio / src / main / java / org / jclouds / http / httpnio / util / NioHttpUtils.java @ 35e7942d

History | View | Annotate | Download (5.2 kB)

1
/**
2
 *
3
 * Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
4
 *
5
 * ====================================================================
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 * ====================================================================
18
 */
19
package org.jclouds.http.httpnio.util;
20

    
21
import java.io.File;
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.io.UnsupportedEncodingException;
25

    
26
import javax.ws.rs.core.HttpHeaders;
27

    
28
import org.apache.http.Header;
29
import org.apache.http.HttpEntityEnclosingRequest;
30
import org.apache.http.HttpVersion;
31
import org.apache.http.entity.InputStreamEntity;
32
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
33
import org.apache.http.nio.entity.NByteArrayEntity;
34
import org.apache.http.nio.entity.NFileEntity;
35
import org.apache.http.nio.entity.NStringEntity;
36
import org.jclouds.http.HttpRequest;
37
import org.jclouds.http.HttpResponse;
38
import org.jclouds.http.Payload;
39

    
40
/**
41
 * 
42
 * @author Adrian Cole
43
 */
44
public class NioHttpUtils {
45
   public static final String USER_AGENT = "jclouds/1.0 httpcore-nio/4.1-alpha1";
46

    
47
   public static HttpEntityEnclosingRequest convertToApacheRequest(HttpRequest request) {
48

    
49
      String path = request.getEndpoint().getRawPath();
50
      if (request.getEndpoint().getQuery() != null)
51
         path += "?" + request.getEndpoint().getQuery();
52
      BasicHttpEntityEnclosingRequest apacheRequest = new BasicHttpEntityEnclosingRequest(request
53
               .getMethod(), path, HttpVersion.HTTP_1_1);
54

    
55
      Payload payload = request.getPayload();
56

    
57
      // Since we may remove headers, ensure they are added to the apache
58
      // request after this block
59
      if (payload != null) {
60
         String lengthString = request.getFirstHeaderOrNull(HttpHeaders.CONTENT_LENGTH);
61
         if (lengthString == null) {
62
            throw new IllegalStateException("no Content-Length header on request: " + apacheRequest);
63
         }
64
         long contentLength = Long.parseLong(lengthString);
65
         String contentType = request.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE);
66
         addEntityForContent(apacheRequest, payload.getRawContent(), contentType, contentLength);
67
      }
68

    
69
      for (String header : request.getHeaders().keySet()) {
70
         for (String value : request.getHeaders().get(header))
71
            // apache automatically tries to add content length header
72
            if (!header.equals(HttpHeaders.CONTENT_LENGTH))
73
               apacheRequest.addHeader(header, value);
74
      }
75
      apacheRequest.addHeader(HttpHeaders.USER_AGENT, USER_AGENT);
76

    
77
      return apacheRequest;
78
   }
79

    
80
   public static void addEntityForContent(BasicHttpEntityEnclosingRequest apacheRequest,
81
            Object content, String contentType, long length) {
82
      if (content instanceof InputStream) {
83
         InputStream inputStream = (InputStream) content;
84
         if (length == -1)
85
            throw new IllegalArgumentException(
86
                     "you must specify size when content is an InputStream");
87
         InputStreamEntity Entity = new InputStreamEntity(inputStream, length);
88
         Entity.setContentType(contentType);
89
         apacheRequest.setEntity(Entity);
90
      } else if (content instanceof String) {
91
         NStringEntity nStringEntity = null;
92
         try {
93
            nStringEntity = new NStringEntity((String) content);
94
         } catch (UnsupportedEncodingException e) {
95
            throw new UnsupportedOperationException("Encoding not supported", e);
96
         }
97
         nStringEntity.setContentType(contentType);
98
         apacheRequest.setEntity(nStringEntity);
99
      } else if (content instanceof File) {
100
         apacheRequest.setEntity(new NFileEntity((File) content, contentType, true));
101
      } else if (content instanceof byte[]) {
102
         NByteArrayEntity Entity = new NByteArrayEntity((byte[]) content);
103
         Entity.setContentType(contentType);
104
         apacheRequest.setEntity(Entity);
105
      } else {
106
         throw new UnsupportedOperationException("Content class not supported: "
107
                  + content.getClass().getName());
108
      }
109
      assert (apacheRequest.getEntity() != null);
110
   }
111

    
112
   public static HttpResponse convertToJCloudsResponse(org.apache.http.HttpResponse apacheResponse)
113
            throws IOException {
114
      HttpResponse response = new HttpResponse();
115
      if (apacheResponse.getEntity() != null) {
116
         response.setContent(apacheResponse.getEntity().getContent());
117
      }
118
      for (Header header : apacheResponse.getAllHeaders()) {
119
         response.getHeaders().put(header.getName(), header.getValue());
120
      }
121
      response.setStatusCode(apacheResponse.getStatusLine().getStatusCode());
122
      response.setMessage(apacheResponse.getStatusLine().getReasonPhrase());
123
      return response;
124
   }
125
}