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   */
44  public interface Http {
45  
46      public static final String HTTP = "http";
47  
48      public static final int HTTP_PORT = 80;
49  
50      public static final String HTTP_WITH_SLASH = "http://";
51  
52      public static final String HTTPS = "https";
53  
54      public static final int HTTPS_PORT = 443;
55  
56      public static final String HTTPS_WITH_SLASH = "https://";
57  
58      public static final String PROTOCOL_DELIMITER = "://";
59  
60      public String addParameter(String url, String name, boolean value);
61  
62      public String addParameter(String url, String name, double value);
63  
64      public String addParameter(String url, String name, int value);
65  
66      public String addParameter(String url, String name, long value);
67  
68      public String addParameter(String url, String name, short value);
69  
70      public String addParameter(String url, String name, String value);
71  
72      public String decodeURL(String url);
73  
74      public String decodeURL(String url, boolean unescapeSpace);
75  
76      public String encodeURL(String url);
77  
78      public String encodeURL(String url, boolean escapeSpaces);
79  
80      public String getCompleteURL(HttpServletRequest request);
81  
82      public Cookie[] getCookies();
83  
84      public String getDomain(String url);
85  
86      public String getParameter(String url, String name);
87  
88      public String getParameter(String url, String name, boolean escaped);
89  
90      public Map<String, String[]> getParameterMap(String queryString);
91  
92      public String getProtocol(ActionRequest actionRequest);
93  
94      public String getProtocol(boolean secure);
95  
96      public String getProtocol(HttpServletRequest request);
97  
98      public String getProtocol(RenderRequest renderRequest);
99  
100     public String getProtocol(String url);
101 
102     public String getQueryString(String url);
103 
104     public String getRequestURL(HttpServletRequest request);
105 
106     public boolean hasDomain(String url);
107 
108     public boolean hasProtocol(String url);
109 
110     public boolean hasProxyConfig();
111 
112     public boolean isNonProxyHost(String host);
113 
114     public boolean isProxyHost(String host);
115 
116     public Map<String, String[]> parameterMapFromString(String queryString);
117 
118     public String parameterMapToString(Map<String, String[]> parameterMap);
119 
120     public String parameterMapToString(
121         Map<String, String[]> parameterMap, boolean addQuestion);
122 
123     public String protocolize(String url, ActionRequest actionRequest);
124 
125     public String protocolize(String url, boolean secure);
126 
127     public String protocolize(String url, HttpServletRequest request);
128 
129     public String protocolize(String url, RenderRequest renderRequest);
130 
131     public String removeDomain(String url);
132 
133     public String removeParameter(String url, String name);
134 
135     public String removeProtocol(String url);
136 
137     public String setParameter(String url, String name, boolean value);
138 
139     public String setParameter(String url, String name, double value);
140 
141     public String setParameter(String url, String name, int value);
142 
143     public String setParameter(String url, String name, long value);
144 
145     public String setParameter(String url, String name, short value);
146 
147     public String setParameter(String url, String name, String value);
148 
149     public byte[] URLtoByteArray(Http.Options options) throws IOException;
150 
151     public byte[] URLtoByteArray(String location) throws IOException;
152 
153     public byte[] URLtoByteArray(String location, boolean post)
154         throws IOException;
155 
156     /**
157      * @deprecated
158      */
159     public byte[] URLtoByteArray(
160             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
161             boolean post)
162         throws IOException;
163 
164     /**
165      * @deprecated
166      */
167     public byte[] URLtoByteArray(
168             String location, Cookie[] cookies, Http.Auth auth,
169             Map<String, String> parts, boolean post)
170         throws IOException;
171 
172     public String URLtoString(Http.Options options) throws IOException;
173 
174     public String URLtoString(String location) throws IOException;
175 
176     public String URLtoString(String location, boolean post) throws IOException;
177 
178     /**
179      * @deprecated
180      */
181     public String URLtoString(
182             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
183             boolean post)
184         throws IOException;
185 
186     /**
187      * @deprecated
188      */
189     public String URLtoString(
190             String location, Cookie[] cookies, Http.Auth auth,
191             Map<String, String> parts, boolean post)
192         throws IOException;
193 
194     /**
195      * This method only uses the default Commons HttpClient implementation when
196      * the URL object represents a HTTP resource. The URL object could also
197      * represent a file or some JNDI resource. In that case, the default Java
198      * implementation is used.
199      *
200      * @param       url URL object
201      * @return      A string representation of the resource referenced by the
202      *              URL object
203      * @throws      IOException
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 }