View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.fileupload2.portlet;
18  
19  import java.io.BufferedReader;
20  import java.io.ByteArrayInputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.UnsupportedEncodingException;
24  import java.security.Principal;
25  import java.util.Arrays;
26  import java.util.Collections;
27  import java.util.Enumeration;
28  import java.util.HashMap;
29  import java.util.Hashtable;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.PortalContext;
35  import javax.portlet.PortletMode;
36  import javax.portlet.PortletPreferences;
37  import javax.portlet.PortletSession;
38  import javax.portlet.WindowState;
39  
40  import org.apache.commons.fileupload2.core.AbstractFileUpload;
41  
42  /**
43   * Mock class for tests. Implements an {@link ActionRequest}.
44   *
45   * @see JavaxPortletFileUploadTest
46   */
47  @SuppressWarnings("rawtypes") // because of the portlet ActionRequest API does not use generics
48  public class JavaxPortletMockActionRequest implements ActionRequest {
49  
50      private final Hashtable<String, Object> attributes = new Hashtable<>();
51  
52      private final Map<String, String> parameters = new HashMap<>();
53  
54      private String characterEncoding;
55      private final int length;
56      private final String contentType;
57      private final InputStream requestData;
58  
59      public JavaxPortletMockActionRequest(final byte[] requestData, final String contentType) {
60          this(new ByteArrayInputStream(requestData), requestData.length, contentType);
61      }
62  
63      public JavaxPortletMockActionRequest(final ByteArrayInputStream requestData, final int length, final String contentType) {
64          this.requestData = requestData;
65          this.length = length;
66          this.contentType = contentType;
67          this.attributes.put(AbstractFileUpload.CONTENT_TYPE, contentType);
68      }
69  
70      @Override
71      public Object getAttribute(final String key) {
72          return attributes.get(key);
73      }
74  
75      @Override
76      public Enumeration getAttributeNames() {
77          return attributes.keys();
78      }
79  
80      @Override
81      public String getAuthType() {
82          return null;
83      }
84  
85      @Override
86      public String getCharacterEncoding() {
87          return characterEncoding;
88      }
89  
90      @Override
91      public int getContentLength() {
92          return length;
93      }
94  
95      @Override
96      public String getContentType() {
97          return contentType;
98      }
99  
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 }