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: }
|