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      @Override
    65      public String getCharacterEncoding() {
    66          return request.getCharacterEncoding();
    67      }
    68
    69      /**
    70       * Retrieve the content type of the request.
    71       *
    72       * @return The content type of the request.
    73       */
    74      @Override
    75      public String getContentType() {
    76          return request.getContentType();
    77      }
    78
    79      /**
    80       * Retrieve the content length of the request.
    81       *
    82       * @return The content length of the request.
    83       * @deprecated 1.3 Use {@link #contentLength()} instead
    84       */
    85      @Override
    86      @Deprecated
    87      public int getContentLength() {
    88          return request.getContentLength();
    89      }
    90
    91      /**
    92       * Retrieve the content length of the request.
    93       *
    94       * @return The content length of the request.
    95       * @since 1.3
    96       */
    97      @Override
    98      public long contentLength() {
    99          long size;
    100         try {
    101             size = Long.parseLong(request.getProperty(FileUploadBase.CONTENT_LENGTH));
    102         } catch (NumberFormatException e) {
    103             size = request.getContentLength();
    104         }
    105         return size;
    106     }
    107
    108     /**
    109      * Retrieve the input stream for the request.
    110      *
    111      * @return The input stream for the request.
    112      *
    113      * @throws IOException if a problem occurs.
    114      */
    115     @Override
    116     public InputStream getInputStream() throws IOException {
    117         return request.getPortletInputStream();
    118     }
    119
    120     /**
    121      * Returns a string representation of this object.
    122      *
    123      * @return a string representation of this object.
    124      */
    125     @Override
    126     public String toString() {
    127         return format("ContentLength=%s, ContentType=%s",
    128                       Long.valueOf(this.contentLength()),
    129                       this.getContentType());
    130     }
    131
    132 }