View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.servlet.impl;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.apache.http.HttpHeaders;
25  import org.apache.http.entity.AbstractHttpEntity;
26  import org.esigate.http.HttpResponseUtils;
27  
28  public class HttpServletRequestEntity extends AbstractHttpEntity {
29      private final HttpServletRequest request;
30      private final long length;
31  
32      HttpServletRequestEntity(HttpServletRequest request) {
33          this.request = request;
34          String contentLengthHeader = request.getHeader(HttpHeaders.CONTENT_LENGTH);
35          if (contentLengthHeader != null) {
36              length = Long.parseLong(contentLengthHeader);
37          } else {
38              length = -1;
39          }
40          String contentTypeHeader = request.getHeader(HttpHeaders.CONTENT_TYPE);
41          if (contentTypeHeader != null) {
42              this.setContentType(contentTypeHeader);
43          }
44          String contentEncodingHeader = request.getHeader(HttpHeaders.CONTENT_ENCODING);
45          if (contentEncodingHeader != null) {
46              this.setContentEncoding(contentEncodingHeader);
47          }
48  
49      }
50  
51      @Override
52      public boolean isRepeatable() {
53          return false;
54      }
55  
56      @Override
57      public long getContentLength() {
58          return length;
59      }
60  
61      @Override
62      public InputStream getContent() throws IOException, IllegalStateException {
63          return request.getInputStream();
64      }
65  
66      @Override
67      public void writeTo(OutputStream outstream) throws IOException {
68          HttpResponseUtils.writeTo(this, outstream);
69      }
70  
71      @Override
72      public boolean isStreaming() {
73          return true;
74      }
75  
76  }