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="HttpUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class HttpUtil {
41  
42      public static String addParameter(String url, String name, boolean value) {
43          return getHttp().addParameter(url, name, value);
44      }
45  
46      public static String addParameter(String url, String name, double value) {
47          return getHttp().addParameter(url, name, value);
48      }
49  
50      public static String addParameter(String url, String name, int value) {
51          return getHttp().addParameter(url, name, value);
52      }
53  
54      public static String addParameter(String url, String name, long value) {
55          return getHttp().addParameter(url, name, value);
56      }
57  
58      public static String addParameter(String url, String name, short value) {
59          return getHttp().addParameter(url, name, value);
60      }
61  
62      public static String addParameter(String url, String name, String value) {
63          return getHttp().addParameter(url, name, value);
64      }
65  
66      public static String decodeURL(String url) {
67          return getHttp().decodeURL(url);
68      }
69  
70      public static String decodeURL(String url, boolean unescapeSpace) {
71          return getHttp().decodeURL(url, unescapeSpace);
72      }
73  
74      public static String encodeURL(String url) {
75          return getHttp().encodeURL(url);
76      }
77  
78      public static String encodeURL(String url, boolean escapeSpaces) {
79          return getHttp().encodeURL(url, escapeSpaces);
80      }
81  
82      public static String getCompleteURL(HttpServletRequest request) {
83          return getHttp().getCompleteURL(request);
84      }
85  
86      public static String getDomain(String url) {
87          return getHttp().getDomain(url);
88      }
89  
90      public static Http getHttp() {
91          return _http;
92      }
93  
94      public static String getParameter(String url, String name) {
95          return getHttp().getParameter(url, name);
96      }
97  
98      public static String getParameter(
99          String url, String name, boolean escaped) {
100 
101         return getHttp().getParameter(url, name, escaped);
102     }
103 
104     public static Map<String, String[]> getParameterMap(String queryString) {
105         return getHttp().getParameterMap(queryString);
106     }
107 
108     public static String getProtocol(ActionRequest actionRequest) {
109         return getHttp().getProtocol(actionRequest);
110     }
111 
112     public static String getProtocol(boolean secure) {
113         return getHttp().getProtocol(secure);
114     }
115 
116     public static String getProtocol(HttpServletRequest request) {
117         return getHttp().getProtocol(request);
118     }
119 
120     public static String getProtocol(RenderRequest renderRequest) {
121         return getHttp().getProtocol(renderRequest);
122     }
123 
124     public static String getProtocol(String url) {
125         return getHttp().getProtocol(url);
126     }
127 
128     public static String getQueryString(String url) {
129         return getHttp().getQueryString(url);
130     }
131 
132     public static String getRequestURL(HttpServletRequest request) {
133         return getHttp().getRequestURL(request);
134     }
135 
136     public static boolean hasDomain(String url) {
137         return getHttp().hasDomain(url);
138     }
139 
140     public static boolean hasProxyConfig() {
141         return getHttp().hasProxyConfig();
142     }
143 
144     public static boolean isNonProxyHost(String host) {
145         return getHttp().isNonProxyHost(host);
146     }
147 
148     public static boolean isProxyHost(String host) {
149         return getHttp().isProxyHost(host);
150     }
151 
152     public static Map<String, String[]> parameterMapFromString(
153         String queryString) {
154 
155         return getHttp().parameterMapFromString(queryString);
156     }
157 
158     public static String parameterMapToString(
159         Map<String, String[]> parameterMap) {
160 
161         return getHttp().parameterMapToString(parameterMap);
162     }
163 
164     public static String parameterMapToString(
165         Map<String, String[]> parameterMap, boolean addQuestion) {
166 
167         return getHttp().parameterMapToString(parameterMap, addQuestion);
168     }
169 
170     public static String protocolize(String url, ActionRequest actionRequest) {
171         return getHttp().protocolize(url, actionRequest);
172     }
173 
174     public static String protocolize(String url, boolean secure) {
175         return getHttp().protocolize(url, secure);
176     }
177 
178     public static String protocolize(String url, HttpServletRequest request) {
179         return getHttp().protocolize(url, request);
180     }
181 
182     public static String protocolize(String url, RenderRequest renderRequest) {
183         return getHttp().protocolize(url, renderRequest);
184     }
185 
186     public static String removeDomain(String url) {
187         return getHttp().removeDomain(url);
188     }
189 
190     public static String removeParameter(String url, String name) {
191         return getHttp().removeParameter(url, name);
192     }
193 
194     public static String removeProtocol(String url) {
195         return getHttp().removeProtocol(url);
196     }
197 
198     public static String setParameter(String url, String name, boolean value) {
199         return getHttp().setParameter(url, name, value);
200     }
201 
202     public static String setParameter(String url, String name, double value) {
203         return getHttp().setParameter(url, name, value);
204     }
205 
206     public static String setParameter(String url, String name, int value) {
207         return getHttp().setParameter(url, name, value);
208     }
209 
210     public static String setParameter(String url, String name, long value) {
211         return getHttp().setParameter(url, name, value);
212     }
213 
214     public static String setParameter(String url, String name, short value) {
215         return getHttp().setParameter(url, name, value);
216     }
217 
218     public static String setParameter(String url, String name, String value) {
219         return getHttp().setParameter(url, name, value);
220     }
221 
222     public static byte[] URLtoByteArray(String location) throws IOException {
223         return getHttp().URLtoByteArray(location);
224     }
225 
226     public static byte[] URLtoByteArray(String location, boolean post)
227         throws IOException {
228 
229         return getHttp().URLtoByteArray(location, post);
230     }
231 
232     public static byte[] URLtoByteArray(
233             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
234             boolean post)
235         throws IOException {
236 
237         return getHttp().URLtoByteArray(location, cookies, auth, body, post);
238     }
239 
240     public static byte[] URLtoByteArray(
241             String location, Cookie[] cookies, Http.Auth auth,
242             Map<String, String> parts, boolean post)
243         throws IOException {
244 
245         return getHttp().URLtoByteArray(location, cookies, auth, parts, post);
246     }
247 
248     public static String URLtoString(String location) throws IOException {
249         return getHttp().URLtoString(location);
250     }
251 
252     public static String URLtoString(String location, boolean post)
253         throws IOException {
254 
255         return getHttp().URLtoString(location, post);
256     }
257 
258     public static String URLtoString(
259             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
260             boolean post)
261         throws IOException {
262 
263         return getHttp().URLtoString(location, cookies, auth, body, post);
264     }
265 
266     public static String URLtoString(
267             String location, Cookie[] cookies, Http.Auth auth,
268             Map<String, String> parts, boolean post)
269         throws IOException {
270 
271         return getHttp().URLtoString(location, cookies, auth, parts, post);
272     }
273 
274     /**
275      * This method only uses the default Commons HttpClient implementation when
276      * the URL object represents a HTTP resource. The URL object could also
277      * represent a file or some JNDI resource. In that case, the default Java
278      * implementation is used.
279      *
280      * @param       url URL object
281      * @return      A string representation of the resource referenced by the
282      *              URL object
283      * @throws      IOException
284      */
285     public static String URLtoString(URL url) throws IOException {
286         return getHttp().URLtoString(url);
287     }
288 
289     public void setHttp(Http http) {
290         _http = http;
291     }
292 
293     private static Http _http;
294 
295 }