    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.fileupload.servlet;
    018
    019import static java.lang.String.format;
    020
    021import java.io.IOException;
    022import java.io.InputStream;
    023
    024import javax.servlet.http.HttpServletRequest;
    025
    026import org.apache.commons.fileupload.FileUploadBase;
    027import org.apache.commons.fileupload.UploadContext;
    028
    029/**
    030 * <p>Provides access to the request information needed for a request made to
    031 * an HTTP servlet.</p>
    032 *
    033 * @since FileUpload 1.1
    034 */
    035public class ServletRequestContext implements UploadContext {
    036
    037    // ----------------------------------------------------- Instance Variables
    038
    039    /**
    040     * The request for which the context is being provided.
    041     */
    042    private final HttpServletRequest request;
    043
    044    // ----------------------------------------------------------- Constructors
    045
    046    /**
    047     * Construct a context for this request.
    048     *
    049     * @param request The request to which this context applies.
    050     */
    051    public ServletRequestContext(HttpServletRequest request) {
    052        this.request = request;
    053    }
    054
    055    // --------------------------------------------------------- Public Methods
    056
    057    /**
    058     * Retrieve the character encoding for the request.
    059     *
    060     * @return The character encoding for the request.
    061     */
    062    public String getCharacterEncoding() {
    063        return request.getCharacterEncoding();
    064    }
    065
    066    /**
    067     * Retrieve the content type of the request.
    068     *
    069     * @return The content type of the request.
    070     */
    071    public String getContentType() {
    072        return request.getContentType();
    073    }
    074
    075    /**
    076     * Retrieve the content length of the request.
    077     *
    078     * @return The content length of the request.
    079     * @deprecated 1.3 Use {@link #contentLength()} instead
    080     */
    081    @Deprecated
    082    public int getContentLength() {
    083        return request.getContentLength();
    084    }
    085
    086    /**
    087     * Retrieve the content length of the request.
    088     *
    089     * @return The content length of the request.
    090     * @since 1.3
    091     */
    092    public long contentLength() {
    093        long size;
    094        try {
    095            size = Long.parseLong(request.getHeader(FileUploadBase.CONTENT_LENGTH));
    096        } catch (NumberFormatException e) {
    097            size = request.getContentLength();
    098        }
    099        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}