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