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    @Override
063    public String getCharacterEncoding() {
064        return request.getCharacterEncoding();
065    }
066
067    /**
068     * Retrieve the content type of the request.
069     *
070     * @return The content type of the request.
071     */
072    @Override
073    public String getContentType() {
074        return request.getContentType();
075    }
076
077    /**
078     * Retrieve the content length of the request.
079     *
080     * @return The content length of the request.
081     * @deprecated 1.3 Use {@link #contentLength()} instead
082     */
083    @Override
084    @Deprecated
085    public int getContentLength() {
086        return request.getContentLength();
087    }
088
089    /**
090     * Retrieve the content length of the request.
091     *
092     * @return The content length of the request.
093     * @since 1.3
094     */
095    @Override
096    public long contentLength() {
097        long size;
098        try {
099            size = Long.parseLong(request.getHeader(FileUploadBase.CONTENT_LENGTH));
100        } catch (NumberFormatException e) {
101            size = request.getContentLength();
102        }
103        return size;
104    }
105
106    /**
107     * Retrieve the input stream for the request.
108     *
109     * @return The input stream for the request.
110     *
111     * @throws IOException if a problem occurs.
112     */
113    @Override
114    public InputStream getInputStream() throws IOException {
115        return request.getInputStream();
116    }
117
118    /**
119     * Returns a string representation of this object.
120     *
121     * @return a string representation of this object.
122     */
123    @Override
124    public String toString() {
125        return format("ContentLength=%s, ContentType=%s",
126                Long.valueOf(this.contentLength()),
127                this.getContentType());
128    }
129
130}