1 | // Copyright (C) 2008 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package com.google.caja.service; |
16 | |
17 | import com.google.caja.SomethingWidgyHappenedError; |
18 | import com.google.caja.util.Maps; |
19 | import com.google.caja.util.Strings; |
20 | import java.io.BufferedReader; |
21 | import java.io.ByteArrayInputStream; |
22 | import java.io.UnsupportedEncodingException; |
23 | import java.io.IOException; |
24 | import java.net.URLDecoder; |
25 | import java.security.Principal; |
26 | import java.util.ArrayList; |
27 | import java.util.Collections; |
28 | import java.util.Date; |
29 | import java.util.Enumeration; |
30 | import java.util.Iterator; |
31 | import java.util.List; |
32 | import java.util.Locale; |
33 | import java.util.Map; |
34 | |
35 | import javax.servlet.RequestDispatcher; |
36 | import javax.servlet.ServletInputStream; |
37 | import javax.servlet.http.Cookie; |
38 | import javax.servlet.http.HttpServletRequest; |
39 | import javax.servlet.http.HttpSession; |
40 | |
41 | /** |
42 | * @author jasvir@google.com (Jasvir Nagra) |
43 | */ |
44 | public final class TestHttpServletRequest implements HttpServletRequest { |
45 | private final String queryString; |
46 | private final Map<String, List<String>> params = Maps.newHashMap(); |
47 | private final byte[] content; |
48 | private final String contentType; |
49 | private final String characterEncoding; |
50 | private final Map<String, String> headers = Maps.newHashMap(); |
51 | |
52 | public TestHttpServletRequest(String queryString) { |
53 | this.queryString = queryString; |
54 | this.content = new byte[0]; |
55 | this.contentType = null; |
56 | this.characterEncoding = null; |
57 | decodeParams(); |
58 | } |
59 | |
60 | TestHttpServletRequest(String queryString, |
61 | byte[] content, |
62 | String contentType, |
63 | String characterEncoding) { |
64 | this.queryString = queryString; |
65 | this.content = content; |
66 | this.contentType = contentType; |
67 | this.characterEncoding = characterEncoding; |
68 | decodeParams(); |
69 | } |
70 | |
71 | private void decodeParams() { |
72 | for (String pair : queryString.split("[?&]")) { |
73 | int eq = pair.indexOf('='); |
74 | String k = decode(eq >= 0 ? pair.substring(0, eq) : pair); |
75 | String v = decode(eq >= 0 ? pair.substring(eq + 1) : ""); |
76 | if (!params.containsKey(k)) { |
77 | params.put(k, new ArrayList<String>()); |
78 | } |
79 | params.get(k).add(v); |
80 | } |
81 | } |
82 | |
83 | public String getAuthType() { throw new UnsupportedOperationException(); } |
84 | public Cookie[] getCookies() { throw new UnsupportedOperationException(); } |
85 | @SuppressWarnings("deprecation") |
86 | public long getDateHeader(String a) { |
87 | String h = headers.get(a); |
88 | return h != null ? new Date(h).getTime() : -1; |
89 | } |
90 | public String getHeader(String a) { |
91 | return headers.get(a); |
92 | } |
93 | public Enumeration<String> getHeaderNames() { |
94 | return enumeration(headers.keySet().iterator()); |
95 | } |
96 | public int getIntHeader(String arg0) { |
97 | String h = headers.get(arg0); |
98 | return h != null ? Integer.valueOf(h) : -1; |
99 | } |
100 | public String getMethod() { throw new UnsupportedOperationException(); } |
101 | public String getPathInfo() { throw new UnsupportedOperationException(); } |
102 | public String getPathTranslated() { |
103 | throw new UnsupportedOperationException(); |
104 | } |
105 | public String getQueryString() { return queryString; } |
106 | public String getRemoteUser() { throw new UnsupportedOperationException(); } |
107 | public String getRequestURI() { return "/proxy"; } |
108 | public String getRequestedSessionId() { |
109 | throw new UnsupportedOperationException(); |
110 | } |
111 | public String getServletPath() { throw new UnsupportedOperationException(); } |
112 | public HttpSession getSession() { throw new UnsupportedOperationException(); } |
113 | public HttpSession getSession(boolean a) { |
114 | throw new UnsupportedOperationException(); |
115 | } |
116 | public boolean isRequestedSessionIdFromCookie() { |
117 | throw new UnsupportedOperationException(); |
118 | } |
119 | public boolean isRequestedSessionIdFromURL() { |
120 | throw new UnsupportedOperationException(); |
121 | } |
122 | @Deprecated |
123 | public boolean isRequestedSessionIdFromUrl() { |
124 | throw new UnsupportedOperationException(); |
125 | } |
126 | public boolean isRequestedSessionIdValid() { |
127 | throw new UnsupportedOperationException(); |
128 | } |
129 | public Object getAttribute(String arg0) { |
130 | throw new UnsupportedOperationException(); |
131 | } |
132 | public Enumeration<String> getAttributeNames() { |
133 | throw new UnsupportedOperationException(); |
134 | } |
135 | public String getCharacterEncoding() { return characterEncoding; } |
136 | public int getContentLength() { return content.length; } |
137 | public String getContentType() { return contentType; } |
138 | public ServletInputStream getInputStream() { |
139 | final ByteArrayInputStream bais = new ByteArrayInputStream(content); |
140 | return new ServletInputStream() { |
141 | @Override |
142 | public int read() { |
143 | return bais.read(); |
144 | } |
145 | @Override |
146 | public void close() throws IOException { |
147 | bais.close(); |
148 | } |
149 | }; |
150 | } |
151 | public String getParameter(String k) { |
152 | return params.containsKey(k) ? params.get(k).get(0) : null; |
153 | } |
154 | public Enumeration<String> getParameterNames() { |
155 | return enumeration(params.keySet().iterator()); |
156 | } |
157 | public String[] getParameterValues(String k) { |
158 | List<String> vals = params.get(k); |
159 | return vals != null ? vals.toArray(new String[0]) : null; |
160 | } |
161 | public String getProtocol() { return "http"; } |
162 | public BufferedReader getReader() { |
163 | throw new UnsupportedOperationException(); |
164 | } |
165 | @Deprecated |
166 | public String getRealPath(String arg0) { |
167 | throw new UnsupportedOperationException(); |
168 | } |
169 | public String getRemoteAddr() { throw new UnsupportedOperationException(); } |
170 | public String getRemoteHost() { throw new UnsupportedOperationException(); } |
171 | public String getScheme() { return getProtocol(); } |
172 | public String getServerName() { return "test.proxy"; } |
173 | public int getServerPort() { return -1; } |
174 | public void setAttribute(String arg0, Object arg1) { |
175 | throw new UnsupportedOperationException(); |
176 | } |
177 | |
178 | private static String decode(String mimeEncoded) { |
179 | try { |
180 | return URLDecoder.decode(mimeEncoded, "UTF-8"); |
181 | } catch (UnsupportedEncodingException ex) { |
182 | throw new SomethingWidgyHappenedError(ex); |
183 | } |
184 | } |
185 | |
186 | public String getContextPath() { |
187 | throw new UnsupportedOperationException(); |
188 | } |
189 | |
190 | public Enumeration<?> getHeaders(String arg0) { |
191 | throw new UnsupportedOperationException(); |
192 | } |
193 | |
194 | public StringBuffer getRequestURL() { |
195 | throw new UnsupportedOperationException(); |
196 | } |
197 | |
198 | public Principal getUserPrincipal() { |
199 | throw new UnsupportedOperationException(); |
200 | } |
201 | |
202 | public boolean isUserInRole(String arg0) { |
203 | throw new UnsupportedOperationException(); |
204 | } |
205 | |
206 | public Locale getLocale() { |
207 | return Locale.ENGLISH; |
208 | } |
209 | |
210 | public Enumeration<Locale> getLocales() { |
211 | throw new UnsupportedOperationException(); |
212 | } |
213 | |
214 | public Map<String, List<String>> getParameterMap() { |
215 | return Collections.unmodifiableMap(this.getParameterMap()); |
216 | } |
217 | |
218 | public RequestDispatcher getRequestDispatcher(String arg0) { |
219 | throw new UnsupportedOperationException(); |
220 | } |
221 | |
222 | public boolean isSecure() { |
223 | return Strings.toLowerCase(this.getRequestURI()).startsWith("https://"); |
224 | } |
225 | |
226 | public void removeAttribute(String arg0) { |
227 | throw new UnsupportedOperationException(); |
228 | } |
229 | |
230 | public void setCharacterEncoding(String encodingName) { |
231 | throw new UnsupportedOperationException(); |
232 | } |
233 | |
234 | private static <T> Enumeration<T> enumeration(final Iterator<T> it) { |
235 | return new Enumeration<T>() { |
236 | public boolean hasMoreElements() { return it.hasNext(); } |
237 | public T nextElement() { return it.next(); } |
238 | }; |
239 | } |
240 | |
241 | public String getLocalAddr() { throw new UnsupportedOperationException(); } |
242 | public String getLocalName() { throw new UnsupportedOperationException(); } |
243 | public int getLocalPort() { throw new UnsupportedOperationException(); } |
244 | public int getRemotePort() { throw new UnsupportedOperationException(); } |
245 | } |