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.portlet;
018
019import static java.lang.String.format;
020
021import java.io.IOException;
022import java.io.InputStream;
023
024import javax.portlet.ActionRequest;
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 * a portlet.</p>
032 *
033 * @since FileUpload 1.1
034 *
035 * @version $Id: PortletRequestContext.java 1564788 2014-02-05 14:36:41Z markt $
036 */
037public class PortletRequestContext implements UploadContext {
038
039    // ----------------------------------------------------- Instance Variables
040
041    /**
042     * The request for which the context is being provided.
043     */
044    private final ActionRequest request;
045
046
047    // ----------------------------------------------------------- Constructors
048
049    /**
050     * Construct a context for this request.
051     *
052     * @param request The request to which this context applies.
053     */
054    public PortletRequestContext(ActionRequest request) {
055        this.request = request;
056    }
057
058
059    // --------------------------------------------------------- Public Methods
060
061    /**
062     * Retrieve the character encoding for the request.
063     *
064     * @return The character encoding for the request.
065     */
066    public String getCharacterEncoding() {
067        return request.getCharacterEncoding();
068    }
069
070    /**
071     * Retrieve the content type of the request.
072     *
073     * @return The content type of the request.
074     */
075    public String getContentType() {
076        return request.getContentType();
077    }
078
079    /**
080     * Retrieve the content length of the request.
081     *
082     * @return The content length of the request.
083     * @deprecated 1.3 Use {@link #contentLength()} instead
084     */
085    @Deprecated
086    public int getContentLength() {
087        return request.getContentLength();
088    }
089
090    /**
091     * Retrieve the content length of the request.
092     *
093     * @return The content length of the request.
094     * @since 1.3
095     */
096    public long contentLength() {
097        long size;
098        try {
099            size = Long.parseLong(request.getProperty(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    public InputStream getInputStream() throws IOException {
114        return request.getPortletInputStream();
115    }
116
117    /**
118     * Returns a string representation of this object.
119     *
120     * @return a string representation of this object.
121     */
122    @Override
123    public String toString() {
124        return format("ContentLength=%s, ContentType=%s",
125                      Long.valueOf(this.contentLength()),
126                      this.getContentType());
127    }
128
129}