1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.io.IOException;
26  
27  import java.net.URL;
28  
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.RenderRequest;
34  
35  import javax.servlet.http.Cookie;
36  import javax.servlet.http.HttpServletRequest;
37  
38  /**
39   * <a href="Http.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public interface Http {
44  
45      public static final String HTTP = "http";
46  
47      public static final int HTTP_PORT = 80;
48  
49      public static final String HTTP_WITH_SLASH = "http://";
50  
51      public static final String HTTPS = "https";
52  
53      public static final int HTTPS_PORT = 443;
54  
55      public static final String HTTPS_WITH_SLASH = "https://";
56  
57      public static final String PROTOCOL_DELIMITER = "://";
58  
59      public String addParameter(String url, String name, boolean value);
60  
61      public String addParameter(String url, String name, double value);
62  
63      public String addParameter(String url, String name, int value);
64  
65      public String addParameter(String url, String name, long value);
66  
67      public String addParameter(String url, String name, short value);
68  
69      public String addParameter(String url, String name, String value);
70  
71      public String decodeURL(String url);
72  
73      public String decodeURL(String url, boolean unescapeSpace);
74  
75      public String encodeURL(String url);
76  
77      public String encodeURL(String url, boolean escapeSpaces);
78  
79      public String getCompleteURL(HttpServletRequest request);
80  
81      public Cookie[] getCookies();
82  
83      public String getDomain(String url);
84  
85      public String getIpAddress(String url);
86  
87      public String getParameter(String url, String name);
88  
89      public String getParameter(String url, String name, boolean escaped);
90  
91      public Map<String, String[]> getParameterMap(String queryString);
92  
93      public String getProtocol(ActionRequest actionRequest);
94  
95      public String getProtocol(boolean secure);
96  
97      public String getProtocol(HttpServletRequest request);
98  
99      public String getProtocol(RenderRequest renderRequest);
100 
101     public String getProtocol(String url);
102 
103     public String getQueryString(String url);
104 
105     public String getRequestURL(HttpServletRequest request);
106 
107     public boolean hasDomain(String url);
108 
109     public boolean hasProtocol(String url);
110 
111     public boolean hasProxyConfig();
112 
113     public boolean isNonProxyHost(String host);
114 
115     public boolean isProxyHost(String host);
116 
117     public Map<String, String[]> parameterMapFromString(String queryString);
118 
119     public String parameterMapToString(Map<String, String[]> parameterMap);
120 
121     public String parameterMapToString(
122         Map<String, String[]> parameterMap, boolean addQuestion);
123 
124     public String protocolize(String url, ActionRequest actionRequest);
125 
126     public String protocolize(String url, boolean secure);
127 
128     public String protocolize(String url, HttpServletRequest request);
129 
130     public String protocolize(String url, RenderRequest renderRequest);
131 
132     public String removeDomain(String url);
133 
134     public String removeParameter(String url, String name);
135 
136     public String removeProtocol(String url);
137 
138     public String setParameter(String url, String name, boolean value);
139 
140     public String setParameter(String url, String name, double value);
141 
142     public String setParameter(String url, String name, int value);
143 
144     public String setParameter(String url, String name, long value);
145 
146     public String setParameter(String url, String name, short value);
147 
148     public String setParameter(String url, String name, String value);
149 
150     public byte[] URLtoByteArray(Http.Options options) throws IOException;
151 
152     public byte[] URLtoByteArray(String location) throws IOException;
153 
154     public byte[] URLtoByteArray(String location, boolean post)
155         throws IOException;
156 
157     /**
158      * @deprecated
159      */
160     public byte[] URLtoByteArray(
161             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
162             boolean post)
163         throws IOException;
164 
165     /**
166      * @deprecated
167      */
168     public byte[] URLtoByteArray(
169             String location, Cookie[] cookies, Http.Auth auth,
170             Map<String, String> parts, boolean post)
171         throws IOException;
172 
173     public String URLtoString(Http.Options options) throws IOException;
174 
175     public String URLtoString(String location) throws IOException;
176 
177     public String URLtoString(String location, boolean post) throws IOException;
178 
179     /**
180      * @deprecated
181      */
182     public String URLtoString(
183             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
184             boolean post)
185         throws IOException;
186 
187     /**
188      * @deprecated
189      */
190     public String URLtoString(
191             String location, Cookie[] cookies, Http.Auth auth,
192             Map<String, String> parts, boolean post)
193         throws IOException;
194 
195     /**
196      * This method only uses the default Commons HttpClient implementation when
197      * the URL object represents a HTTP resource. The URL object could also
198      * represent a file or some JNDI resource. In that case, the default Java
199      * implementation is used.
200      *
201      * @param  url URL object
202      * @return A string representation of the resource referenced by the
203      *         URL object
204      */
205     public String URLtoString(URL url) throws IOException;
206 
207     public class Auth {
208 
209         public Auth(
210             String host, int port, String realm, String username,
211             String password) {
212 
213             _host = host;
214             _port = port;
215             _realm = realm;
216             _username = username;
217             _password = password;
218         }
219 
220         public String getHost() {
221             return _host;
222         }
223 
224         public String getPassword() {
225             return _password;
226         }
227 
228         public int getPort() {
229             return _port;
230         }
231 
232         public String getRealm() {
233             return _realm;
234         }
235 
236         public String getUsername() {
237             return _username;
238         }
239 
240         private String _host;
241         private String _password;
242         private int _port;
243         private String _realm;
244         private String _username;
245 
246     }
247 
248     public class Body {
249 
250         public Body(String content, String contentType, String charset) {
251             _content = content;
252             _contentType = contentType;
253             _charset = charset;
254         }
255 
256         public String getCharset() {
257             return _charset;
258         }
259 
260         public String getContent() {
261             return _content;
262         }
263 
264         public String getContentType() {
265             return _contentType;
266         }
267 
268         private String _charset;
269         private String _content;
270         private String _contentType;
271 
272     }
273 
274     public enum Method {
275 
276         DELETE, GET, POST, PUT
277     }
278 
279     public class Options {
280 
281         public void addHeader(String name, String value) {
282             if (_headers == null) {
283                 _headers = new HashMap<String, String>();
284             }
285 
286             _headers.put(name, value);
287         }
288 
289         public void addPart(String name, String value) {
290             if (_body != null) {
291                 throw new IllegalArgumentException(
292                     "Part cannot be added because a body has already been set");
293             }
294 
295             if (_parts == null) {
296                 _parts = new HashMap<String, String>();
297             }
298 
299             _parts.put(name, value);
300         }
301 
302         public Auth getAuth() {
303             return _auth;
304         }
305 
306         public Body getBody() {
307             return _body;
308         }
309 
310         public Cookie[] getCookies() {
311             return _cookies;
312         }
313 
314         public Map<String, String> getHeaders() {
315             return _headers;
316         }
317 
318         public String getLocation() {
319             return _location;
320         }
321 
322         public Method getMethod() {
323             return _method;
324         }
325 
326         public Map<String, String> getParts() {
327             return _parts;
328         }
329 
330         public boolean isDelete() {
331             if (_method == Method.DELETE) {
332                 return true;
333             }
334             else {
335                 return false;
336             }
337         }
338 
339         public boolean isGet() {
340             if (_method == Method.GET) {
341                 return true;
342             }
343             else {
344                 return false;
345             }
346         }
347 
348         public boolean isPost() {
349             if (_method == Method.POST) {
350                 return true;
351             }
352             else {
353                 return false;
354             }
355         }
356 
357         public boolean isPut() {
358             if (_method == Method.PUT) {
359                 return true;
360             }
361             else {
362                 return false;
363             }
364         }
365 
366         public void setAuth(Http.Auth auth) {
367             setAuth(
368                 auth.getHost(), auth.getPort(), auth.getRealm(),
369                 auth.getUsername(), auth.getPassword());
370         }
371 
372         public void setAuth(
373             String host, int port, String realm, String username,
374             String password) {
375 
376             _auth = new Auth(host, port, realm, username, password);
377         }
378 
379         public void setBody(Http.Body body) {
380             setBody(
381                 body.getContent(), body.getContentType(), body.getCharset());
382         }
383 
384         public void setBody(
385             String content, String contentType, String charset) {
386 
387             if (_parts != null) {
388                 throw new IllegalArgumentException(
389                     "Body cannot be set because a part has already been added");
390             }
391 
392             _body = new Body(content, contentType, charset);
393         }
394 
395         public void setCookies(Cookie[] cookies) {
396             _cookies = cookies;
397         }
398 
399         public void setDelete(boolean delete) {
400             if (delete) {
401                 _method = Method.DELETE;
402             }
403             else {
404                 _method = Method.GET;
405             }
406         }
407 
408         public void setHeaders(Map<String, String> headers) {
409             _headers = headers;
410         }
411 
412         public void setLocation(String location) {
413             _location = location;
414         }
415 
416         public void setParts(Map<String, String> parts) {
417             _parts = parts;
418         }
419 
420         public void setPost(boolean post) {
421             if (post) {
422                 _method = Method.POST;
423             }
424             else {
425                 _method = Method.GET;
426             }
427         }
428 
429         public void setPut(boolean put) {
430             if (put) {
431                 _method = Method.PUT;
432             }
433             else {
434                 _method = Method.GET;
435             }
436         }
437 
438         private Auth _auth;
439         private Body _body;
440         private Cookie[] _cookies;
441         private Map<String, String> _headers;
442         private String _location;
443         private Map<String, String> _parts;
444         private Method _method = Method.GET;
445 
446     }
447 
448 }