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 String HTTPS = "https";
45  
46      public static final String HTTP_WITH_SLASH = "http://";
47  
48      public static final String HTTPS_WITH_SLASH = "https://";
49  
50      public static final int HTTP_PORT = 80;
51  
52      public static final int HTTPS_PORT = 443;
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(boolean secure);
87  
88      public String getProtocol(String url);
89  
90      public String getProtocol(HttpServletRequest request);
91  
92      public String getProtocol(ActionRequest actionRequest);
93  
94      public String getProtocol(RenderRequest renderRequest);
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, boolean secure);
116 
117     public String protocolize(String url, HttpServletRequest request);
118 
119     public String protocolize(String url, ActionRequest actionRequest);
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 void submit(String location) throws IOException;
142 
143     public void submit(String location, Cookie[] cookies) throws IOException;
144 
145     public void submit(String location, boolean post) throws IOException;
146 
147     public void submit(String location, Cookie[] cookies, boolean post)
148         throws IOException;
149 
150     public void submit(
151             String location, Cookie[] cookies, Http.Body body, boolean post)
152         throws IOException;
153 
154     public void submit(
155             String location, Cookie[] cookies, Map<String, String> parts,
156             boolean post)
157         throws IOException;
158 
159     public byte[] URLtoByteArray(String location) throws IOException;
160 
161     public byte[] URLtoByteArray(String location, Cookie[] cookies)
162         throws IOException;
163 
164     public byte[] URLtoByteArray(String location, boolean post)
165         throws IOException;
166 
167     public byte[] URLtoByteArray(
168             String location, Cookie[] cookies, boolean post)
169         throws IOException;
170 
171     public byte[] URLtoByteArray(
172             String location, Cookie[] cookies, Http.Body body, boolean post)
173         throws IOException;
174 
175     public byte[] URLtoByteArray(
176             String location, Cookie[] cookies, Map<String, String> parts,
177             boolean post)
178         throws IOException;
179 
180     public String URLtoString(String location) throws IOException;
181 
182     public String URLtoString(String location, Cookie[] cookies)
183         throws IOException;
184 
185     public String URLtoString(String location, boolean post) throws IOException;
186 
187     public String URLtoString(
188             String location, Cookie[] cookies, boolean post)
189         throws IOException;
190 
191     public String URLtoString(
192             String location, Cookie[] cookies, Http.Body body, boolean post)
193         throws IOException;
194 
195     public String URLtoString(
196             String location, Cookie[] cookies, Map<String, String> parts,
197             boolean post)
198         throws IOException;
199 
200     public String URLtoString(
201             String location, String host, int port, String realm,
202             String username, String password)
203         throws IOException;
204 
205     /**
206      * This method only uses the default Commons HttpClient implementation when
207      * the URL object represents a HTTP resource. The URL object could also
208      * represent a file or some JNDI resource. In that case, the default Java
209      * implementation is used.
210      *
211      * @param       url URL object
212      * @return      A string representation of the resource referenced by the
213      *              URL object
214      * @throws      IOException
215      */
216     public String URLtoString(URL url) throws IOException;
217 
218     public class Body {
219 
220         public Body(String content, String contentType, String charset) {
221             _content = content;
222             _contentType = contentType;
223             _charset = charset;
224         }
225 
226         public String getContent() {
227             return _content;
228         }
229 
230         public String getContentType() {
231             return _contentType;
232         }
233 
234         public String getCharset() {
235             return _charset;
236         }
237 
238         private String _content;
239         private String _contentType;
240         private String _charset;
241 
242     }
243 
244 }