Source Code Cross Referenced for HttpClient.java in  » GIS » geonetwork » org » wfp » vam » intermap » kernel » map » mapServices » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » geonetwork » org.wfp.vam.intermap.kernel.map.mapServices 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //=============================================================================
002:        //===	Copyright (C) 2001-2007 Food and Agriculture Organization of the
003:        //===	United Nations (FAO-UN), United Nations World Food Programme (WFP)
004:        //===	and United Nations Environment Programme (UNEP)
005:        //===
006:        //===	This program is free software; you can redistribute it and/or modify
007:        //===	it under the terms of the GNU General Public License as published by
008:        //===	the Free Software Foundation; either version 2 of the License, or (at
009:        //===	your option) any later version.
010:        //===
011:        //===	This program is distributed in the hope that it will be useful, but
012:        //===	WITHOUT ANY WARRANTY; without even the implied warranty of
013:        //===	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014:        //===	General Public License for more details.
015:        //===
016:        //===	You should have received a copy of the GNU General Public License
017:        //===	along with this program; if not, write to the Free Software
018:        //===	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019:        //===
020:        //===	Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021:        //===	Rome - Italy. email: geonetwork@osgeo.org
022:        //==============================================================================
023:
024:        package org.wfp.vam.intermap.kernel.map.mapServices;
025:
026:        import java.io.*;
027:        import java.util.*;
028:        import java.net.*;
029:
030:        import org.jdom.*;
031:
032:        import jeeves.utils.Xml;
033:
034:        public class HttpClient {
035:            private static final int BUF_LEN = 1024;
036:
037:            private URL url;
038:            private String user; // User id for HTTP authentication
039:            private String password; // Password for HTTP authentication
040:            private String request;
041:            private HttpURLConnection conn;
042:            private Hashtable htHeaders = new Hashtable(); // Contains all the response headers
043:
044:            // Cache management
045:            private static Vector vUrl = new Vector();
046:            private static Vector vReq = new Vector();
047:            private static Vector vTime = new Vector();
048:
049:            private boolean cached = false;
050:
051:            public HttpClient(String url, String request)
052:                    throws MalformedURLException {
053:                int pos = vUrl.indexOf(url);
054:                if (pos != -1 && vReq.get(pos).equals(request)) // DEBUG: Aggiungere il controllo sulla data!!!!!!!!!!
055:                    cached = true;
056:
057:                this .url = new URL(url);
058:                this .request = request;
059:            }
060:
061:            public HttpClient(String url) throws MalformedURLException {
062:                this (url, null);
063:            }
064:
065:            /** Sets the user id for HTTP basic authentication */
066:            public void setUser(String user) {
067:                this .user = user;
068:            }
069:
070:            /** Set the password for HTTP basic authentication */
071:            public void setPassword(String password) {
072:                this .password = password;
073:            }
074:
075:            /** Connexts to the remote server */
076:            private void connect() throws IOException {
077:                conn = (HttpURLConnection) url.openConnection();
078:                getResponseHeaders();
079:                //		if (conn.getResponseCode() == HttpURLConnection.HTTP_OK);
080:            }
081:
082:            /** Disconnects from the server */
083:            public void disconnect() {
084:                conn.disconnect();
085:            }
086:
087:            /** Returns the response code from the server */
088:            public int getResponseCode() throws IOException {
089:                return conn.getResponseCode();
090:            }
091:
092:            /** Sets a request property */
093:            public void setRequestHeader(String key, String value) {
094:                conn.setRequestProperty(key, value);
095:            }
096:
097:            /** Returns the HTTP response header with the given key */
098:            public String getResponseHeader(String key) {
099:                return (String) htHeaders.get(key.toLowerCase());
100:            }
101:
102:            /**
103:             * Sends the request and gets the response as a JDOM Element
104:             *
105:             * @return  the response as a JDOM Element
106:             *
107:             * @throws   IOException if a connection failure occurs
108:             * @throws   JDOMException if a xml parsing error occurs
109:             *
110:             */
111:            public Element getElement() throws Exception {
112:                return Xml.loadString(getString(), false); // DEBUG
113:            }
114:
115:            /**
116:             * Sends the request and gets the response as a String
117:             *
118:             * @return   the response as a String
119:             *
120:             * @throws   IOException if a connection failure occurs
121:             *
122:             */
123:            public String getString() throws IOException {
124:                sendRequest();
125:
126:                // Get the response from the server
127:                InputStream is = conn.getInputStream();
128:                String response = "";
129:                byte[] buf = new byte[BUF_LEN];
130:                for (int n; (n = is.read(buf, 0, BUF_LEN)) > 0;)
131:                    response += new String(buf, 0, n);
132:
133:                try {
134:                    is.close();
135:                } catch (Exception e) {
136:                }
137:
138:                return response;
139:            }
140:
141:            /**
142:             * Gets the image from the remote server, and save in the given file
143:             *
144:             * @return   the response as a String
145:             *
146:             * @throws   IOException if a connection failure occurs
147:             *
148:             */
149:            public void getFile(File f) throws IOException {
150:                sendRequest();
151:
152:                BufferedInputStream is = new BufferedInputStream(conn
153:                        .getInputStream());
154:                BufferedOutputStream os = new BufferedOutputStream(
155:                        new FileOutputStream(f));
156:
157:                byte[] buf = new byte[BUF_LEN];
158:                for (int n; (n = is.read(buf, 0, BUF_LEN)) > 0;)
159:                    os.write(buf, 0, n);
160:
161:                // Close the streams
162:                try {
163:                    is.close();
164:                } catch (Exception e) {
165:                }
166:                try {
167:                    os.close();
168:                } catch (Exception e) {
169:                }
170:            }
171:
172:            /**
173:             * Send the request to the ArcIMS map server
174:             *
175:             * @throws   IOException if a connection failure occurs
176:             *
177:             */
178:            private void sendRequest() throws IOException {
179:                // Connect to the server if not already connected
180:                if (conn == null)
181:                    connect();
182:
183:                // Set the authentication parameters
184:                if (user != null && password != null) {
185:                    sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
186:                    String userPwd = encoder.encode((user + ":" + password)
187:                            .getBytes());
188:                    conn.setRequestProperty("Authentication", "Basic "
189:                            + userPwd);
190:                }
191:
192:                if (request != null) {
193:                    // Send the request to to the server
194:                    conn.setDoOutput(true);
195:                    BufferedOutputStream os = new BufferedOutputStream(conn
196:                            .getOutputStream());
197:
198:                    os.write(request.getBytes());
199:
200:                    os.flush();
201:                    try {
202:                        os.close();
203:                    } catch (Exception e) {
204:                    }
205:                }
206:            }
207:
208:            /** Save headers in an Hashtable */
209:            private void getResponseHeaders() {
210:                String h;
211:                for (int i = 1; (h = conn.getHeaderField(i)) != null; i++)
212:                    htHeaders.put(conn.getHeaderFieldKey(i).toLowerCase(), h);
213:            }
214:
215:        }
w_ww__.j__a_v___a_2s_.__c__o___m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.