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 java.io.IOException;
020import java.io.InputStream;
021
022import javax.servlet.http.HttpServletRequest;
023
024import org.apache.commons.fileupload.FileUploadBase;
025import org.apache.commons.fileupload.UploadContext;
026
027/**
028 * <p>Provides access to the request information needed for a request made to
029 * an HTTP servlet.</p>
030 *
031 * @since FileUpload 1.1
032 */
033public class ServletRequestContext implements UploadContext {
034
035    /**
036     * The request for which the context is being provided.
037     */
038    private final HttpServletRequest request;
039
040    /**
041     * Construct a context for this request.
042     *
043     * @param request The request to which this context applies.
044     */
045    public ServletRequestContext(final HttpServletRequest request) {
046        this.request = request;
047    }
048
049    /**
050     * Retrieve the content length of the request.
051     *
052     * @return The content length of the request.
053     * @since 1.3
054     */
055    @Override
056    public long contentLength() {
057        long size;
058        try {
059            size = Long.parseLong(request.getHeader(FileUploadBase.CONTENT_LENGTH));
060        } catch (final NumberFormatException e) {
061            size = request.getContentLength();
062        }
063        return size;
064    }
065
066    /**
067     * Retrieve the character encoding for the request.
068     *
069     * @return The character encoding for the request.
070     */
071    @Override
072    public String getCharacterEncoding() {
073        return request.getCharacterEncoding();
074    }
075
076    /**
077     * Retrieve the content length of the request.
078     *
079     * @return The content length of the request.
080     * @deprecated 1.3 Use {@link #contentLength()} instead
081     */
082    @Override
083    @Deprecated
084    public int getContentLength() {
085        return request.getContentLength();
086    }
087
088    /**
089     * Retrieve the content type of the request.
090     *
091     * @return The content type of the request.
092     */
093    @Override
094    public String getContentType() {
095        return request.getContentType();
096    }
097
098    /**
099     * Retrieve the input stream for the request.
100     *
101     * @return The input stream for the request.
102     * @throws IOException if a problem occurs.
103     */
104    @Override
105    public InputStream getInputStream() throws IOException {
106        return request.getInputStream();
107    }
108
109    /**
110     * Returns a string representation of this object.
111     *
112     * @return a string representation of this object.
113     */
114    @Override
115    public String toString() {
116        return String.format("ContentLength=%s, ContentType=%s",
117                Long.valueOf(contentLength()), getContentType());
118    }
119
120}