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.portlet;
18  
19  import static java.lang.String.format;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import javax.portlet.ActionRequest;
25  
26  import org.apache.commons.fileupload.FileUploadBase;
27  import org.apache.commons.fileupload.UploadContext;
28  
29  /**
30   * <p>Provides access to the request information needed for a request made to
31   * a portlet.</p>
32   *
33   * @since FileUpload 1.1
34   */
35  public class PortletRequestContext implements UploadContext {
36  
37      /**
38       * The request for which the context is being provided.
39       */
40      private final ActionRequest request;
41  
42      /**
43       * Construct a context for this request.
44       *
45       * @param request The request to which this context applies.
46       */
47      public PortletRequestContext(final ActionRequest request) {
48          this.request = request;
49      }
50  
51      /**
52       * Retrieve the content length of the request.
53       *
54       * @return The content length of the request.
55       * @since 1.3
56       */
57      @Override
58      public long contentLength() {
59          long size;
60          try {
61              size = Long.parseLong(request.getProperty(FileUploadBase.CONTENT_LENGTH));
62          } catch (final NumberFormatException e) {
63              size = request.getContentLength();
64          }
65          return size;
66      }
67  
68      /**
69       * Retrieve the character encoding for the request.
70       *
71       * @return The character encoding for the request.
72       */
73      @Override
74      public String getCharacterEncoding() {
75          return request.getCharacterEncoding();
76      }
77  
78      /**
79       * Retrieve the content length of the request.
80       *
81       * @return The content length of the request.
82       * @deprecated 1.3 Use {@link #contentLength()} instead
83       */
84      @Override
85      @Deprecated
86      public int getContentLength() {
87          return request.getContentLength();
88      }
89  
90      /**
91       * Retrieve the content type of the request.
92       *
93       * @return The content type of the request.
94       */
95      @Override
96      public String getContentType() {
97          return request.getContentType();
98      }
99  
100     /**
101      * Retrieve the input stream for the request.
102      *
103      * @return The input stream for the request.
104      * @throws IOException if a problem occurs.
105      */
106     @Override
107     public InputStream getInputStream() throws IOException {
108         return request.getPortletInputStream();
109     }
110 
111     /**
112      * Returns a string representation of this object.
113      *
114      * @return a string representation of this object.
115      */
116     @Override
117     public String toString() {
118         return format("ContentLength=%s, ContentType=%s",
119                       Long.valueOf(contentLength()),
120                       getContentType());
121     }
122 
123 }