View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.fileupload.servlet;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.apache.commons.fileupload.FileUploadBase;
25  import org.apache.commons.fileupload.UploadContext;
26  
27  /**
28   * <p>Provides access to the request information needed for a request made to
29   * an HTTP servlet.</p>
30   *
31   * @since FileUpload 1.1
32   */
33  public class ServletRequestContext implements UploadContext {
34  
35      /**
36       * The request for which the context is being provided.
37       */
38      private final HttpServletRequest request;
39  
40      /**
41       * Construct a context for this request.
42       *
43       * @param request The request to which this context applies.
44       */
45      public ServletRequestContext(final HttpServletRequest request) {
46          this.request = request;
47      }
48  
49      /**
50       * Retrieve the content length of the request.
51       *
52       * @return The content length of the request.
53       * @since 1.3
54       */
55      @Override
56      public long contentLength() {
57          long size;
58          try {
59              size = Long.parseLong(request.getHeader(FileUploadBase.CONTENT_LENGTH));
60          } catch (final NumberFormatException e) {
61              size = request.getContentLength();
62          }
63          return size;
64      }
65  
66      /**
67       * Retrieve the character encoding for the request.
68       *
69       * @return The character encoding for the request.
70       */
71      @Override
72      public String getCharacterEncoding() {
73          return request.getCharacterEncoding();
74      }
75  
76      /**
77       * Retrieve the content length of the request.
78       *
79       * @return The content length of the request.
80       * @deprecated 1.3 Use {@link #contentLength()} instead
81       */
82      @Override
83      @Deprecated
84      public int getContentLength() {
85          return request.getContentLength();
86      }
87  
88      /**
89       * Retrieve the content type of the request.
90       *
91       * @return The content type of the request.
92       */
93      @Override
94      public String getContentType() {
95          return request.getContentType();
96      }
97  
98      /**
99       * Retrieve the input stream for the request.
100      *
101      * @return The input stream for the request.
102      * @throws IOException if a problem occurs.
103      */
104     @Override
105     public InputStream getInputStream() throws IOException {
106         return request.getInputStream();
107     }
108 
109     /**
110      * Returns a string representation of this object.
111      *
112      * @return a string representation of this object.
113      */
114     @Override
115     public String toString() {
116         return String.format("ContentLength=%s, ContentType=%s",
117                 Long.valueOf(contentLength()), getContentType());
118     }
119 
120 }