    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.fileupload2.portlet;
    018
    019import java.io.BufferedReader;
    020import java.io.ByteArrayInputStream;
    021import java.io.IOException;
    022import java.io.InputStream;
    023import java.io.UnsupportedEncodingException;
    024import java.security.Principal;
    025import java.util.Arrays;
    026import java.util.Collections;
    027import java.util.Enumeration;
    028import java.util.HashMap;
    029import java.util.Hashtable;
    030import java.util.Locale;
    031import java.util.Map;
    032
    033import javax.portlet.ActionRequest;
    034import javax.portlet.PortalContext;
    035import javax.portlet.PortletMode;
    036import javax.portlet.PortletPreferences;
    037import javax.portlet.PortletSession;
    038import javax.portlet.WindowState;
    039
    040import org.apache.commons.fileupload2.core.AbstractFileUpload;
    041
    042/**
    043 * Mock class for tests. Implements an {@link ActionRequest}.
    044 *
    045 * @see JavaxPortletFileUploadTest
    046 */
    047@SuppressWarnings("rawtypes") // because of the portlet ActionRequest API does not use generics
    048public class JavaxPortletMockActionRequest implements ActionRequest {
    049
    050    private final Hashtable<String, Object> attributes = new Hashtable<>();
    051
    052    private final Map<String, String> parameters = new HashMap<>();
    053
    054    private String characterEncoding;
    055    private final int length;
    056    private final String contentType;
    057    private final InputStream requestData;
    058
    059    public JavaxPortletMockActionRequest(final byte[] requestData, final String contentType) {
    060        this(new ByteArrayInputStream(requestData), requestData.length, contentType);
    061    }
    062
    063    public JavaxPortletMockActionRequest(final ByteArrayInputStream requestData, final int length, final String contentType) {
    064        this.requestData = requestData;
    065        this.length = length;
    066        this.contentType = contentType;
    067        this.attributes.put(AbstractFileUpload.CONTENT_TYPE, contentType);
    068    }
    069
    070    @Override
    071    public Object getAttribute(final String key) {
    072        return attributes.get(key);
    073    }
    074
    075    @Override
    076    public Enumeration getAttributeNames() {
    077        return attributes.keys();
    078    }
    079
    080    @Override
    081    public String getAuthType() {
    082        return null;
    083    }
    084
    085    @Override
    086    public String getCharacterEncoding() {
    087        return characterEncoding;
    088    }
    089
    090    @Override
    091    public int getContentLength() {
    092        return length;
    093    }
    094
    095    @Override
    096    public String getContentType() {
    097        return contentType;
    098    }
    099
    100    @Override
    101    public String getContextPath() {
    102        return null;
    103    }
    104
    105    @Override
    106    public Locale getLocale() {
    107        return Locale.getDefault();
    108    }
    109
    110    @Override
    111    public Enumeration getLocales() {
    112        return Collections.enumeration(Arrays.asList(Locale.getAvailableLocales()));
    113    }
    114
    115    @Override
    116    public String getParameter(final String key) {
    117        return parameters.get(key);
    118    }
    119
    120    @Override
    121    public Map getParameterMap() {
    122        return Collections.unmodifiableMap(parameters);
    123    }
    124
    125    @Override
    126    public Enumeration getParameterNames() {
    127        return Collections.enumeration(parameters.keySet());
    128    }
    129
    130    @Override
    131    public String[] getParameterValues(final String arg0) {
    132        return null;
    133    }
    134
    135    @Override
    136    public PortalContext getPortalContext() {
    137        return null;
    138    }
    139
    140    @Override
    141    public InputStream getPortletInputStream() throws IOException {
    142        return requestData;
    143    }
    144
    145    @Override
    146    public PortletMode getPortletMode() {
    147        return null;
    148    }
    149
    150    @Override
    151    public PortletSession getPortletSession() {
    152        return null;
    153    }
    154
    155    @Override
    156    public PortletSession getPortletSession(final boolean arg0) {
    157        return null;
    158    }
    159
    160    @Override
    161    public PortletPreferences getPreferences() {
    162        return null;
    163    }
    164
    165    @Override
    166    public Enumeration getProperties(final String arg0) {
    167        return null;
    168    }
    169
    170    @Override
    171    public String getProperty(final String arg0) {
    172        return null;
    173    }
    174
    175    @Override
    176    public Enumeration getPropertyNames() {
    177        return null;
    178    }
    179
    180    @Override
    181    public BufferedReader getReader() throws UnsupportedEncodingException, IOException {
    182        return null;
    183    }
    184
    185    @Override
    186    public String getRemoteUser() {
    187        return null;
    188    }
    189
    190    @Override
    191    public String getRequestedSessionId() {
    192        return null;
    193    }
    194
    195    @Override
    196    public String getResponseContentType() {
    197        return null;
    198    }
    199
    200    @Override
    201    public Enumeration getResponseContentTypes() {
    202        return null;
    203    }
    204
    205    @Override
    206    public String getScheme() {
    207        return null;
    208    }
    209
    210    @Override
    211    public String getServerName() {
    212        return null;
    213    }
    214
    215    @Override
    216    public int getServerPort() {
    217        return 0;
    218    }
    219
    220    @Override
    221    public Principal getUserPrincipal() {
    222        return null;
    223    }
    224
    225    @Override
    226    public WindowState getWindowState() {
    227        return null;
    228    }
    229
    230    @Override
    231    public boolean isPortletModeAllowed(final PortletMode arg0) {
    232        return false;
    233    }
    234
    235    @Override
    236    public boolean isRequestedSessionIdValid() {
    237        return false;
    238    }
    239
    240    @Override
    241    public boolean isSecure() {
    242        return false;
    243    }
    244
    245    @Override
    246    public boolean isUserInRole(final String arg0) {
    247        return false;
    248    }
    249
    250    @Override
    251    public boolean isWindowStateAllowed(final WindowState arg0) {
    252        return false;
    253    }
    254
    255    @Override
    256    public void removeAttribute(final String key) {
    257        attributes.remove(key);
    258    }
    259
    260    @Override
    261    public void setAttribute(final String key, final Object value) {
    262        attributes.put(key, value);
    263    }
    264
    265    @Override
    266    public void setCharacterEncoding(final String characterEncoding) throws UnsupportedEncodingException {
    267        this.characterEncoding = characterEncoding;
    268    }
    269
    270}