KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > HttpUtilities


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.util;
25
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.BufferedWriter JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.InputStreamReader JavaDoc;
32 import java.io.OutputStreamWriter JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.net.URLConnection JavaDoc;
36 import java.net.URLEncoder JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import java.util.Hashtable JavaDoc;
39
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41
42 import org.apache.log4j.Logger;
43
44
45
46 /**
47  * URL utility class. Used for tasks involving getting url-contents from remote addresses
48  * within a program.
49  *
50  * @author Mattias Bogeblad
51  * @version 1
52  * @since 1999-10-05
53  */

54
55
56 public class HttpUtilities
57 {
58     private final static Logger logger = Logger.getLogger(HttpUtilities.class.getName());
59
60     /*
61      *
62      */

63     public HttpUtilities()
64     {
65     }
66
67     /**
68      * This method post information to an URL and returns a string.It throws
69      * an exception if anything goes wrong.
70      * (Works like most 'doPost' methods)
71      *
72      * @param urlAddress The address of the URL you would like to post to.
73      * @param inHash The parameters you would like to post to the URL.
74      * @return The result of the postToUrl method as a string.
75      * @exception java.lang.Exception
76      */

77     
78     public static String JavaDoc postToUrl(String JavaDoc urlAddress, Hashtable JavaDoc inHash) throws Exception JavaDoc
79     {
80         URL JavaDoc url = new URL JavaDoc(urlAddress);
81         URLConnection JavaDoc urlConn = url.openConnection();
82         urlConn.setAllowUserInteraction(false);
83         urlConn.setDoOutput (true);
84         urlConn.setDoInput (true);
85         urlConn.setUseCaches (false);
86         urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
87         PrintWriter JavaDoc printout = new PrintWriter JavaDoc(urlConn.getOutputStream(), true);
88         String JavaDoc argString = "";
89         if(inHash != null)
90         {
91             argString = toEncodedString(inHash);
92         }
93         printout.print(argString);
94         printout.flush();
95         printout.close ();
96         InputStream JavaDoc inStream = null;
97         inStream = urlConn.getInputStream();
98         InputStreamReader JavaDoc inStreamReader = new InputStreamReader JavaDoc(inStream);
99         BufferedReader JavaDoc buffer = new BufferedReader JavaDoc(inStreamReader);
100         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
101         String JavaDoc line;
102         while((line = buffer.readLine()) != null)
103         {
104             strbuf.append(line);
105         }
106         String JavaDoc readData = strbuf.toString();
107         buffer.close();
108         return readData;
109     }
110
111
112     /**
113      * This method post information to an URL and returns a string.It throws
114      * an exception if anything goes wrong.
115      * (Works like most 'doPost' methods)
116      *
117      * @param urlAddress The address of the URL you would like to post to.
118      * @param inHash The parameters you would like to post to the URL.
119      * @return The result of the postToUrl method as a string.
120      * @exception java.lang.Exception
121      */

122     
123     public static String JavaDoc postToUrl(String JavaDoc urlAddress, Hashtable JavaDoc inHash, String JavaDoc userName, String JavaDoc password, boolean shouldEncode) throws Exception JavaDoc
124     {
125         String JavaDoc encodedPassword = HTUU.encode(userName + ":" + password);
126         
127         URL JavaDoc url = new URL JavaDoc(urlAddress);
128         URLConnection JavaDoc urlConn = url.openConnection();
129         urlConn.setAllowUserInteraction(false);
130         urlConn.setDoOutput (true);
131         urlConn.setDoInput (true);
132         urlConn.setUseCaches (false);
133         urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
134         urlConn.setRequestProperty("Authorization", "Basic " + encodedPassword);
135         PrintWriter JavaDoc printout = new PrintWriter JavaDoc(urlConn.getOutputStream(), true);
136         String JavaDoc argString = "";
137         if(inHash != null)
138         {
139             if (shouldEncode)
140                 argString = toEncodedString(inHash);
141             else
142                 argString = toString(inHash);
143         }
144         printout.print(argString);
145         printout.flush();
146         printout.close ();
147         InputStream JavaDoc inStream = null;
148         inStream = urlConn.getInputStream();
149         InputStreamReader JavaDoc inStreamReader = new InputStreamReader JavaDoc(inStream);
150         BufferedReader JavaDoc buffer = new BufferedReader JavaDoc(inStreamReader);
151         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
152         String JavaDoc line;
153         while((line = buffer.readLine()) != null)
154         {
155             strbuf.append(line);
156         }
157         String JavaDoc readData = strbuf.toString();
158         buffer.close();
159         return readData;
160     }
161
162      
163     public static String JavaDoc getUrlContent(String JavaDoc urlAddress, HttpServletRequest JavaDoc request, boolean includeRequest) throws Exception JavaDoc
164     {
165         if(includeRequest)
166             return getUrlContent(urlAddress, requestToHashtable(request));
167         else
168             return getUrlContent(urlAddress);
169     }
170
171     public static String JavaDoc getUrlContent(String JavaDoc urlAddress, HttpServletRequest JavaDoc request, boolean includeRequest, String JavaDoc encoding) throws Exception JavaDoc
172     {
173         if(includeRequest)
174             return getUrlContent(urlAddress, requestToHashtable(request), encoding);
175         else
176             return getUrlContent(urlAddress, encoding);
177     }
178
179     public static String JavaDoc getUrlContent(String JavaDoc urlAddress, Hashtable JavaDoc inHash) throws Exception JavaDoc
180     {
181         String JavaDoc argString = "";
182         if(inHash != null)
183         {
184             if(urlAddress.indexOf("?") > -1)
185                 argString = "&" + toEncodedString(inHash);
186             else
187                 argString = "?" + toEncodedString(inHash);
188         }
189
190         logger.info("Getting content from url: " + urlAddress + argString);
191         
192         URL JavaDoc url = new URL JavaDoc(urlAddress + argString);
193         URLConnection JavaDoc connection = url.openConnection();
194         connection.setUseCaches(false);
195         InputStream JavaDoc inStream = null;
196         inStream = connection.getInputStream();
197         InputStreamReader JavaDoc inStreamReader = new InputStreamReader JavaDoc(inStream, "ISO-8859-1");
198         BufferedReader JavaDoc buffer = new BufferedReader JavaDoc(inStreamReader);
199         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
200         String JavaDoc line;
201         while((line = buffer.readLine()) != null)
202         {
203             strbuf.append(line);
204         }
205         String JavaDoc readData = strbuf.toString();
206         buffer.close();
207                                                 
208         return readData;
209     }
210
211
212     public static String JavaDoc getUrlContent(String JavaDoc urlAddress, Hashtable JavaDoc inHash, String JavaDoc encoding) throws Exception JavaDoc
213     {
214         String JavaDoc argString = "";
215         if(inHash != null)
216         {
217             if(urlAddress.indexOf("?") > -1)
218                 argString = "&" + toEncodedString(inHash);
219             else
220                 argString = "?" + toEncodedString(inHash);
221         }
222
223         logger.info("Getting content from url: " + urlAddress + argString);
224         
225         URL JavaDoc url = new URL JavaDoc(urlAddress + argString);
226         URLConnection JavaDoc connection = url.openConnection();
227         connection.setUseCaches(false);
228         InputStream JavaDoc inStream = null;
229         inStream = connection.getInputStream();
230         InputStreamReader JavaDoc inStreamReader = new InputStreamReader JavaDoc(inStream, encoding);
231         BufferedReader JavaDoc buffer = new BufferedReader JavaDoc(inStreamReader);
232         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
233         String JavaDoc line;
234         while((line = buffer.readLine()) != null)
235         {
236             strbuf.append(line);
237         }
238         String JavaDoc readData = strbuf.toString();
239         buffer.close();
240
241         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
242         BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(baos, "UTF-8"));
243         writer.write(readData);
244
245         baos.flush();
246         baos.close();
247         writer.flush();
248         writer.close();
249         
250         readData = new String JavaDoc(baos.toString(encoding));
251                                                 
252         return readData;
253     }
254
255     
256     public static String JavaDoc getUrlContent(String JavaDoc urlAddress) throws Exception JavaDoc
257     {
258         URL JavaDoc url = new URL JavaDoc(urlAddress);
259         URLConnection JavaDoc connection = url.openConnection();
260         connection.setUseCaches(false);
261         InputStream JavaDoc inStream = null;
262         inStream = connection.getInputStream();
263         InputStreamReader JavaDoc inStreamReader = new InputStreamReader JavaDoc(inStream);
264         BufferedReader JavaDoc buffer = new BufferedReader JavaDoc(inStreamReader);
265         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
266         String JavaDoc line;
267         while((line = buffer.readLine()) != null)
268         {
269             strbuf.append(line);
270         }
271         String JavaDoc readData = strbuf.toString();
272         buffer.close();
273         return readData;
274     }
275     
276     
277     public static String JavaDoc getUrlContent(String JavaDoc urlAddress, String JavaDoc encoding) throws Exception JavaDoc
278     {
279         URL JavaDoc url = new URL JavaDoc(urlAddress);
280         URLConnection JavaDoc connection = url.openConnection();
281         connection.setUseCaches(false);
282         InputStream JavaDoc inStream = null;
283         inStream = connection.getInputStream();
284         InputStreamReader JavaDoc inStreamReader = new InputStreamReader JavaDoc(inStream, encoding);
285         BufferedReader JavaDoc buffer = new BufferedReader JavaDoc(inStreamReader);
286         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
287         String JavaDoc line;
288         while((line = buffer.readLine()) != null)
289         {
290             strbuf.append(line);
291         }
292         String JavaDoc readData = strbuf.toString();
293         buffer.close();
294         
295         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
296         BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(baos, "UTF-8"));
297         writer.write(readData);
298
299         baos.flush();
300         baos.close();
301         writer.flush();
302         writer.close();
303
304         readData = new String JavaDoc(baos.toString(encoding));
305         
306         return readData;
307     }
308     
309     /**
310      * This method gets information form an URL and returns a string.It throws
311      * an exception if anything goes wrong.
312      * (Works like most 'doGet' methods)
313      *
314      * @param urlAddress The address of the URL you would like to get information from.
315      * @param inHash The parameters you would like to get from the URL.
316      * @return The result of the getUrlContent method as a string.
317      * @exception java.lang.Exception
318      */

319               
320     public static String JavaDoc getUrlContent(String JavaDoc urlAddress, Hashtable JavaDoc inHash, String JavaDoc userName, String JavaDoc password, boolean shouldEncode) throws Exception JavaDoc
321     {
322         String JavaDoc encodedPassword = HTUU.encode(userName + ":" + password);
323         
324         String JavaDoc argString = "";
325         if(inHash != null)
326         {
327             if (shouldEncode)
328                 argString = "?" + toEncodedString(inHash);
329             else
330                 argString = "?" + toString(inHash);
331         }
332         URL JavaDoc url = new URL JavaDoc(urlAddress + argString);
333         URLConnection JavaDoc connection = url.openConnection();
334         connection.setUseCaches(false);
335         connection.setRequestProperty("Authorization", "Basic " + encodedPassword);
336         InputStream JavaDoc inStream = null;
337         inStream = connection.getInputStream();
338         InputStreamReader JavaDoc inStreamReader = new InputStreamReader JavaDoc(inStream);
339         BufferedReader JavaDoc buffer = new BufferedReader JavaDoc(inStreamReader);
340         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
341         String JavaDoc line;
342         while((line = buffer.readLine()) != null)
343         {
344             strbuf.append(line);
345         }
346         String JavaDoc readData = strbuf.toString();
347         buffer.close();
348         return readData;
349     }
350     
351     
352     
353     public static String JavaDoc getUrlContent(String JavaDoc urlAddress, String JavaDoc data, String JavaDoc userName, String JavaDoc password) throws Exception JavaDoc
354     {
355         String JavaDoc encodedPassword = HTUU.encode(userName + ":" + password);
356         
357         String JavaDoc argString = "?" + data;
358         
359         URL JavaDoc url = new URL JavaDoc(urlAddress + argString);
360         URLConnection JavaDoc connection = url.openConnection();
361         connection.setUseCaches(false);
362         connection.setRequestProperty("Authorization", "Basic " + encodedPassword);
363         InputStream JavaDoc inStream = null;
364         inStream = connection.getInputStream();
365         InputStreamReader JavaDoc inStreamReader = new InputStreamReader JavaDoc(inStream);
366         BufferedReader JavaDoc buffer = new BufferedReader JavaDoc(inStreamReader);
367         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
368         String JavaDoc line;
369         while((line = buffer.readLine()) != null)
370         {
371             strbuf.append(line);
372         }
373         String JavaDoc readData = strbuf.toString();
374         buffer.close();
375         return readData;
376     }
377     
378     
379     
380     /**
381      * This method gets information form an URL and returns an input stream.It
382      * throws an exception if anything goes wrong.
383      * It returns a input stream so that you can return objects.
384      * (Works like most 'doGet' methods)
385      *
386      * @param urlAddress The address of the URL you would like to get information from.
387      * @param inHash The parameters you would like to get from the URL.
388      * @return A input stream.
389      * @exception java.lang.Exception
390      */

391     
392     public static InputStream JavaDoc getURLStream(String JavaDoc urlAddress, Hashtable JavaDoc inHash) throws Exception JavaDoc
393     {
394         String JavaDoc argString = "";
395         if(inHash != null)
396         {
397             argString = "?" + toEncodedString(inHash);
398         }
399         URL JavaDoc url = new URL JavaDoc(urlAddress + argString);
400         URLConnection JavaDoc connection = url.openConnection();
401         connection.setUseCaches(false);
402         InputStream JavaDoc inStream = null;
403         inStream = connection.getInputStream();
404         return inStream;
405     }
406     
407     
408
409     /**
410      * This method converts the request-object to a hashtable instead.
411      */

412     
413     public static Hashtable JavaDoc requestToHashtable(HttpServletRequest JavaDoc request)
414     {
415         Hashtable JavaDoc parameters = new Hashtable JavaDoc();
416         
417         if(request != null)
418         {
419             for (Enumeration JavaDoc e = request.getParameterNames(); e.hasMoreElements() ;)
420             {
421                 String JavaDoc name = (String JavaDoc)e.nextElement();
422                 String JavaDoc value = (String JavaDoc)request.getParameter(name);
423                 parameters.put(name, value);
424             }
425         }
426                 
427         return parameters;
428         
429     }
430
431     
432     /**
433      * Encodes a hash table to an URL encoded string.
434      *
435      * @param inHash The hash table you would like to encode
436      * @return A URL encoded string.
437      */

438         
439     private static String JavaDoc toEncodedString(Hashtable JavaDoc inHash) throws Exception JavaDoc
440     {
441         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
442         Enumeration JavaDoc names = inHash.keys();
443         while(names.hasMoreElements())
444         {
445             String JavaDoc name = names.nextElement().toString();
446             String JavaDoc value = inHash.get(name).toString();
447             buffer.append(URLEncoder.encode(name, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8"));
448             if(names.hasMoreElements())
449             {
450                 buffer.append("&");
451             }
452         }
453         return buffer.toString();
454     }
455     
456     private static String JavaDoc toString(Hashtable JavaDoc inHash)
457     {
458         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
459         Enumeration JavaDoc names = inHash.keys();
460         while(names.hasMoreElements())
461         {
462             String JavaDoc name = names.nextElement().toString();
463             String JavaDoc value = inHash.get(name).toString();
464             buffer.append(name + "=" + value);
465             if(names.hasMoreElements())
466             {
467                 buffer.append("&");
468             }
469         }
470         return buffer.toString();
471     }
472
473 }
474
Popular Tags