1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.Map;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.RenderRequest;
33  
34  import javax.servlet.http.Cookie;
35  import javax.servlet.http.HttpServletRequest;
36  
37  /**
38   * <a href="Http.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
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 String getDomain(String url);
82  
83      public String getParameter(String url, String name);
84  
85      public String getParameter(String url, String name, boolean escaped);
86  
87      public Map<String, String[]> getParameterMap(String queryString);
88  
89      public String getProtocol(ActionRequest actionRequest);
90  
91      public String getProtocol(boolean secure);
92  
93      public String getProtocol(HttpServletRequest request);
94  
95      public String getProtocol(RenderRequest renderRequest);
96  
97      public String getProtocol(String url);
98  
99      public String getQueryString(String url);
100 
101     public String getRequestURL(HttpServletRequest request);
102 
103     public boolean hasDomain(String url);
104 
105     public boolean hasProxyConfig();
106 
107     public boolean isNonProxyHost(String host);
108 
109     public boolean isProxyHost(String host);
110 
111     public Map<String, String[]> parameterMapFromString(String queryString);
112 
113     public String parameterMapToString(Map<String, String[]> parameterMap);
114 
115     public String parameterMapToString(
116         Map<String, String[]> parameterMap, boolean addQuestion);
117 
118     public String protocolize(String url, ActionRequest actionRequest);
119 
120     public String protocolize(String url, boolean secure);
121 
122     public String protocolize(String url, HttpServletRequest request);
123 
124     public String protocolize(String url, RenderRequest renderRequest);
125 
126     public String removeDomain(String url);
127 
128     public String removeParameter(String url, String name);
129 
130     public String removeProtocol(String url);
131 
132     public String setParameter(String url, String name, boolean value);
133 
134     public String setParameter(String url, String name, double value);
135 
136     public String setParameter(String url, String name, int value);
137 
138     public String setParameter(String url, String name, long value);
139 
140     public String setParameter(String url, String name, short value);
141 
142     public String setParameter(String url, String name, String value);
143 
144     public byte[] URLtoByteArray(String location) throws IOException;
145 
146     public byte[] URLtoByteArray(String location, boolean post)
147         throws IOException;
148 
149     public byte[] URLtoByteArray(
150             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
151             boolean post)
152         throws IOException;
153 
154     public byte[] URLtoByteArray(
155             String location, Cookie[] cookies, Http.Auth auth,
156             Map<String, String> parts, boolean post)
157         throws IOException;
158 
159     public String URLtoString(String location) throws IOException;
160 
161     public String URLtoString(String location, boolean post) throws IOException;
162 
163     public String URLtoString(
164             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
165             boolean post)
166         throws IOException;
167 
168     public String URLtoString(
169             String location, Cookie[] cookies, Http.Auth auth,
170             Map<String, String> parts, boolean post)
171         throws IOException;
172 
173     /**
174      * This method only uses the default Commons HttpClient implementation when
175      * the URL object represents a HTTP resource. The URL object could also
176      * represent a file or some JNDI resource. In that case, the default Java
177      * implementation is used.
178      *
179      * @param       url URL object
180      * @return      A string representation of the resource referenced by the
181      *              URL object
182      * @throws      IOException
183      */
184     public String URLtoString(URL url) throws IOException;
185 
186     public class Auth {
187 
188         public Auth(
189             String host, int port, String realm, String username,
190             String password) {
191 
192             _host = host;
193             _port = port;
194             _realm = realm;
195             _username = username;
196             _password = password;
197         }
198 
199         public String getHost() {
200             return _host;
201         }
202 
203         public String getPassword() {
204             return _password;
205         }
206 
207         public int getPort() {
208             return _port;
209         }
210 
211         public String getRealm() {
212             return _realm;
213         }
214 
215         public String getUsername() {
216             return _username;
217         }
218 
219         private String _host;
220         private String _password;
221         private int _port;
222         private String _realm;
223         private String _username;
224 
225     }
226 
227     public class Body {
228 
229         public Body(String content, String contentType, String charset) {
230             _content = content;
231             _contentType = contentType;
232             _charset = charset;
233         }
234 
235         public String getCharset() {
236             return _charset;
237         }
238 
239         public String getContent() {
240             return _content;
241         }
242 
243         public String getContentType() {
244             return _contentType;
245         }
246 
247         private String _charset;
248         private String _content;
249         private String _contentType;
250 
251     }
252 
253 }