KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > HttpUtils


1 //
2
// HttpUtils
3
// EV 19.12.2000
4
//
5
// getRemoteFileContents
6
//
7

8 package org.jahia.utils;
9
10 import java.io.BufferedReader JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import org.jahia.exceptions.JahiaException;
18 ;
19
20
21
22 public class HttpUtils {
23
24     private static org.apache.log4j.Logger logger =
25         org.apache.log4j.Logger.getLogger(HttpUtils.class);
26
27     private static HttpUtils theObject = null;
28
29
30
31     /***
32         * constructor
33         * EV 19.12.2000
34         *
35         */

36     private HttpUtils()
37     {
38         JahiaConsole.println( "Utils", "***** Starting HttpUtils *****" );
39     } // end constructor
40

41
42
43     /***
44         * getInstance
45         * EV 19.12.2000
46         *
47         */

48     public static HttpUtils getInstance()
49     {
50         if (theObject == null) {
51             theObject = new HttpUtils();
52         }
53         return theObject;
54     } // end getInstance
55

56
57
58     /***
59         * getRemoteFileContents
60         * EV 03.12.2000
61         * called by NewsFeedServices.getNewsFeed(), DataSourceServices
62         *
63         */

64     public String JavaDoc getRemoteFileContents( String JavaDoc urlPath )
65     {
66         try {
67             URL JavaDoc url = new URL JavaDoc( urlPath );
68             InputStreamReader JavaDoc input = new InputStreamReader JavaDoc( (InputStream JavaDoc) url.openStream() );
69             BufferedReader JavaDoc in = new BufferedReader JavaDoc( (Reader JavaDoc) input );
70             String JavaDoc contents = "";
71             String JavaDoc buffer = "";
72             while ((buffer = in.readLine()) != null) {
73                 contents += buffer + "\n";
74             }
75             return contents;
76         } catch (IOException JavaDoc ie) {
77             String JavaDoc errorMsg = "Error in reading newsfeed " + urlPath + " : " + ie.getMessage();
78             JahiaConsole.println( "NewsFeedManager", errorMsg + " -> Error !" );
79             JahiaException je = new JahiaException( "Cannot access to newsfeed",
80                                                 errorMsg, JahiaException.FILE_ERROR, JahiaException.ERROR_SEVERITY, ie );
81             logger.error("Error:", je);
82             return "- No NewsFeed ! -";
83         }
84     } // end getRemoteFileContents
85

86
87
88 } // end HttpUtils
89
Popular Tags