1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import java.io.IOException;
23  
24  import java.net.URL;
25  
26  import java.util.Map;
27  
28  import javax.portlet.ActionRequest;
29  import javax.portlet.RenderRequest;
30  
31  import javax.servlet.http.Cookie;
32  import javax.servlet.http.HttpServletRequest;
33  
34  /**
35   * <a href="Http.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public interface Http {
41  
42      public static final String HTTP = "http";
43  
44      public static final int HTTP_PORT = 80;
45  
46      public static final String HTTP_WITH_SLASH = "http://";
47  
48      public static final String HTTPS = "https";
49  
50      public static final int HTTPS_PORT = 443;
51  
52      public static final String HTTPS_WITH_SLASH = "https://";
53  
54      public static final String PROTOCOL_DELIMITER = "://";
55  
56      public String addParameter(String url, String name, boolean value);
57  
58      public String addParameter(String url, String name, double value);
59  
60      public String addParameter(String url, String name, int value);
61  
62      public String addParameter(String url, String name, long value);
63  
64      public String addParameter(String url, String name, short value);
65  
66      public String addParameter(String url, String name, String value);
67  
68      public String decodeURL(String url);
69  
70      public String decodeURL(String url, boolean unescapeSpace);
71  
72      public String encodeURL(String url);
73  
74      public String encodeURL(String url, boolean escapeSpaces);
75  
76      public String getCompleteURL(HttpServletRequest request);
77  
78      public String getDomain(String url);
79  
80      public String getParameter(String url, String name);
81  
82      public String getParameter(String url, String name, boolean escaped);
83  
84      public Map<String, String[]> getParameterMap(String queryString);
85  
86      public String getProtocol(ActionRequest actionRequest);
87  
88      public String getProtocol(boolean secure);
89  
90      public String getProtocol(HttpServletRequest request);
91  
92      public String getProtocol(RenderRequest renderRequest);
93  
94      public String getProtocol(String url);
95  
96      public String getQueryString(String url);
97  
98      public String getRequestURL(HttpServletRequest request);
99  
100     public boolean hasDomain(String url);
101 
102     public boolean hasProxyConfig();
103 
104     public boolean isNonProxyHost(String host);
105 
106     public boolean isProxyHost(String host);
107 
108     public Map<String, String[]> parameterMapFromString(String queryString);
109 
110     public String parameterMapToString(Map<String, String[]> parameterMap);
111 
112     public String parameterMapToString(
113         Map<String, String[]> parameterMap, boolean addQuestion);
114 
115     public String protocolize(String url, ActionRequest actionRequest);
116 
117     public String protocolize(String url, boolean secure);
118 
119     public String protocolize(String url, HttpServletRequest request);
120 
121     public String protocolize(String url, RenderRequest renderRequest);
122 
123     public String removeDomain(String url);
124 
125     public String removeParameter(String url, String name);
126 
127     public String removeProtocol(String url);
128 
129     public String setParameter(String url, String name, boolean value);
130 
131     public String setParameter(String url, String name, double value);
132 
133     public String setParameter(String url, String name, int value);
134 
135     public String setParameter(String url, String name, long value);
136 
137     public String setParameter(String url, String name, short value);
138 
139     public String setParameter(String url, String name, String value);
140 
141     public byte[] URLtoByteArray(String location) throws IOException;
142 
143     public byte[] URLtoByteArray(String location, boolean post)
144         throws IOException;
145 
146     public byte[] URLtoByteArray(
147             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
148             boolean post)
149         throws IOException;
150 
151     public byte[] URLtoByteArray(
152             String location, Cookie[] cookies, Http.Auth auth,
153             Map<String, String> parts, boolean post)
154         throws IOException;
155 
156     public String URLtoString(String location) throws IOException;
157 
158     public String URLtoString(String location, boolean post) throws IOException;
159 
160     public String URLtoString(
161             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
162             boolean post)
163         throws IOException;
164 
165     public String URLtoString(
166             String location, Cookie[] cookies, Http.Auth auth,
167             Map<String, String> parts, boolean post)
168         throws IOException;
169 
170     /**
171      * This method only uses the default Commons HttpClient implementation when
172      * the URL object represents a HTTP resource. The URL object could also
173      * represent a file or some JNDI resource. In that case, the default Java
174      * implementation is used.
175      *
176      * @param       url URL object
177      * @return      A string representation of the resource referenced by the
178      *              URL object
179      * @throws      IOException
180      */
181     public String URLtoString(URL url) throws IOException;
182 
183     public class Auth {
184 
185         public Auth(
186             String host, int port, String realm, String username,
187             String password) {
188 
189             _host = host;
190             _port = port;
191             _realm = realm;
192             _username = username;
193             _password = password;
194         }
195 
196         public String getHost() {
197             return _host;
198         }
199 
200         public String getPassword() {
201             return _password;
202         }
203 
204         public int getPort() {
205             return _port;
206         }
207 
208         public String getRealm() {
209             return _realm;
210         }
211 
212         public String getUsername() {
213             return _username;
214         }
215 
216         private String _host;
217         private String _password;
218         private int _port;
219         private String _realm;
220         private String _username;
221 
222     }
223 
224     public class Body {
225 
226         public Body(String content, String contentType, String charset) {
227             _content = content;
228             _contentType = contentType;
229             _charset = charset;
230         }
231 
232         public String getCharset() {
233             return _charset;
234         }
235 
236         public String getContent() {
237             return _content;
238         }
239 
240         public String getContentType() {
241             return _contentType;
242         }
243 
244         private String _charset;
245         private String _content;
246         private String _contentType;
247 
248     }
249 
250 }