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.fileupload.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.fileupload.FileUploadBase;
41  
42  /**
43   * Mock class for tests. Implements an {@link ActionRequest}.
44   *
45   * @see PortletFileUploadTest
46   * @since 1.4
47   */
48  @SuppressWarnings("rawtypes") // because of the portlet ActionRequest API does not use generics
49  public class MockPortletActionRequest implements ActionRequest {
50  
51      private final Hashtable<String, Object> attributes = new Hashtable<String, Object>();
52  
53      private final Map<String, String> parameters = new HashMap<String, String>();
54  
55      private String characterEncoding;
56      private int length;
57      private final String contentType;
58      private final InputStream requestData;
59  
60      public MockPortletActionRequest(final byte[] requestData, final String contentType) {
61          this(new ByteArrayInputStream(requestData), requestData.length, contentType);
62      }
63  
64      public MockPortletActionRequest(ByteArrayInputStream byteArrayInputStream, int requestLength, String contentType) {
65          this.requestData = byteArrayInputStream;
66          length = requestLength;
67          this.contentType = contentType;
68          attributes.put(FileUploadBase.CONTENT_TYPE, contentType);
69      }
70  
71      @Override
72      public Object getAttribute(String key) {
73          return attributes.get(key);
74      }
75  
76      @Override
77      public Enumeration getAttributeNames() {
78          return attributes.keys();
79      }
80  
81      @Override
82      public String getAuthType() {
83          return null;
84      }
85  
86      @Override
87      public String getContextPath() {
88          return null;
89      }
90  
91      @Override
92      public Locale getLocale() {
93          return Locale.getDefault();
94      }
95  
96      @Override
97      public Enumeration getLocales() {
98          return Collections.enumeration(Arrays.asList(Locale.getAvailableLocales()));
99      }
100 
101     @Override
102     public String getParameter(String key) {
103         return parameters.get(key);
104     }
105 
106     @Override
107     public Map getParameterMap() {
108         return Collections.unmodifiableMap(parameters);
109     }
110 
111     @Override
112     public Enumeration getParameterNames() {
113         return Collections.enumeration(parameters.keySet());
114     }
115 
116     @Override
117     public String[] getParameterValues(String arg0) {
118         return null;
119     }
120 
121     @Override
122     public PortalContext getPortalContext() {
123         return null;
124     }
125 
126     @Override
127     public PortletMode getPortletMode() {
128         return null;
129     }
130 
131     @Override
132     public PortletSession getPortletSession() {
133         return null;
134     }
135 
136     @Override
137     public PortletSession getPortletSession(boolean arg0) {
138         return null;
139     }
140 
141     @Override
142     public PortletPreferences getPreferences() {
143         return null;
144     }
145 
146     @Override
147     public Enumeration getProperties(String arg0) {
148         return null;
149     }
150 
151     @Override
152     public String getProperty(String arg0) {
153         return null;
154     }
155 
156     @Override
157     public Enumeration getPropertyNames() {
158         return null;
159     }
160 
161     @Override
162     public String getRemoteUser() {
163         return null;
164     }
165 
166     @Override
167     public String getRequestedSessionId() {
168         return null;
169     }
170 
171     @Override
172     public String getResponseContentType() {
173         return null;
174     }
175 
176     @Override
177     public Enumeration getResponseContentTypes() {
178         return null;
179     }
180 
181     @Override
182     public String getScheme() {
183         return null;
184     }
185 
186     @Override
187     public String getServerName() {
188         return null;
189     }
190 
191     @Override
192     public int getServerPort() {
193         return 0;
194     }
195 
196     @Override
197     public Principal getUserPrincipal() {
198         return null;
199     }
200 
201     @Override
202     public WindowState getWindowState() {
203         return null;
204     }
205 
206     @Override
207     public boolean isPortletModeAllowed(PortletMode arg0) {
208         return false;
209     }
210 
211     @Override
212     public boolean isRequestedSessionIdValid() {
213         return false;
214     }
215 
216     @Override
217     public boolean isSecure() {
218         return false;
219     }
220 
221     @Override
222     public boolean isUserInRole(String arg0) {
223         return false;
224     }
225 
226     @Override
227     public boolean isWindowStateAllowed(WindowState arg0) {
228         return false;
229     }
230 
231     @Override
232     public void removeAttribute(String key) {
233         attributes.remove(key);
234     }
235 
236     @Override
237     public void setAttribute(String key, Object value) {
238         attributes.put(key, value);
239     }
240 
241     @Override
242     public String getCharacterEncoding() {
243         return characterEncoding;
244     }
245 
246     @Override
247     public int getContentLength() {
248         return length;
249     }
250 
251     @Override
252     public String getContentType() {
253         return contentType;
254     }
255 
256     @Override
257     public InputStream getPortletInputStream() throws IOException {
258         return requestData;
259     }
260 
261     @Override
262     public BufferedReader getReader() throws UnsupportedEncodingException, IOException {
263         return null;
264     }
265 
266     @Override
267     public void setCharacterEncoding(String characterEncoding) throws UnsupportedEncodingException {
268         this.characterEncoding = characterEncoding;
269     }
270 
271 }