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      // ----------------------------------------------------- Instance Variables
    38
    39      /**
    40       * The request for which the context is being provided.
    41       */
    42      private final ActionRequest request;
    43
    44
    45      // ----------------------------------------------------------- Constructors
    46
    47      /**
    48       * Construct a context for this request.
    49       *
    50       * @param request The request to which this context applies.
    51       */
    52      public PortletRequestContext(ActionRequest request) {
    53          this.request = request;
    54      }
    55
    56
    57      // --------------------------------------------------------- Public Methods
    58
    59      /**
    60       * Retrieve the character encoding for the request.
    61       *
    62       * @return The character encoding for the request.
    63       */
    64      public String getCharacterEncoding() {
    65          return request.getCharacterEncoding();
    66      }
    67
    68      /**
    69       * Retrieve the content type of the request.
    70       *
    71       * @return The content type of the request.
    72       */
    73      public String getContentType() {
    74          return request.getContentType();
    75      }
    76
    77      /**
    78       * Retrieve the content length of the request.
    79       *
    80       * @return The content length of the request.
    81       * @deprecated 1.3 Use {@link #contentLength()} instead
    82       */
    83      @Deprecated
    84      public int getContentLength() {
    85          return request.getContentLength();
    86      }
    87
    88      /**
    89       * Retrieve the content length of the request.
    90       *
    91       * @return The content length of the request.
    92       * @since 1.3
    93       */
    94      public long contentLength() {
    95          long size;
    96          try {
    97              size = Long.parseLong(request.getProperty(FileUploadBase.CONTENT_LENGTH));
    98          } catch (NumberFormatException e) {
    99              size = request.getContentLength();
    100         }
    101         return size;
    102     }
    103
    104     /**
    105      * Retrieve the input stream for the request.
    106      *
    107      * @return The input stream for the request.
    108      *
    109      * @throws IOException if a problem occurs.
    110      */
    111     public InputStream getInputStream() throws IOException {
    112         return request.getPortletInputStream();
    113     }
    114
    115     /**
    116      * Returns a string representation of this object.
    117      *
    118      * @return a string representation of this object.
    119      */
    120     @Override
    121     public String toString() {
    122         return format("ContentLength=%s, ContentType=%s",
    123                       Long.valueOf(this.contentLength()),
    124                       this.getContentType());
    125     }
    126
    127 }