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.arcims;
025:
026: import java.net.*;
027: import java.io.*;
028:
029: import org.jdom.*;
030:
031: import jeeves.utils.Xml;
032:
033: public class ArcIMSClient {
034: private static final int BUF_SIZE = 1024;
035:
036: private String server; // Server name
037: private String service; // Service name
038: private String user = null;
039: private String password = null;
040: private String customService = null;
041: private Element request;
042:
043: private HttpURLConnection conn;
044:
045: public ArcIMSClient(String server, String service,
046: String customService, Element request) {
047: this .server = server;
048: this .service = service;
049: this .request = request;
050: this .customService = customService;
051: }
052:
053: public ArcIMSClient(String server, String service, Element request) {
054: this (server, service, null, request);
055: }
056:
057: /**
058: * Sets the user id for authentication
059: *
060: * @param user a String
061: *
062: */
063: public void setUser(String user) {
064: this .user = user;
065: }
066:
067: /**
068: * Set the password for authentication
069: *
070: * @param password a String
071: *
072: */
073: public void setPassword(String password) {
074: this .password = password;
075: }
076:
077: /**
078: * Sends the request and gets the response as a Jdom Element
079: *
080: * @return the response as a Jdom Element
081: *
082: * @throws IOException if a connection failure occurs
083: * @throws JDOMException if a xml parsing error occurs
084: *
085: */
086: public Element getElement() throws Exception {
087: // Get the response from the server
088: return Xml.loadString(getString(), false); // DEBUG
089: }
090:
091: /**
092: * Sends the request and gets the response as a String
093: *
094: * @return the response as a String
095: *
096: * @throws IOException if a connection failure occurs
097: *
098: */
099: public String getString() throws IOException {
100: sendRequest();
101:
102: // Get the response from the server
103: InputStream is = conn.getInputStream();
104: String response = "";
105: byte[] buf = new byte[BUF_SIZE];
106: for (int n; (n = is.read(buf, 0, BUF_SIZE)) > 0;)
107: response += new String(buf, 0, n);
108: conn.disconnect();
109:
110: return response;
111: }
112:
113: /**
114: * Send the request to the ArcIMS map server
115: *
116: * @throws IOException if a connection failure occurs
117: *
118: */
119: private void sendRequest() throws IOException {
120: try {
121: String stUrl = server + "?ServiceName=" + service
122: + "&ClientVersion=4.0";
123: if (customService != null) {
124: stUrl += "&CustomService=" + customService;
125: }
126: URL url = new URL(stUrl);
127:
128: // Connect to the server
129: conn = (HttpURLConnection) url.openConnection();
130:
131: // Set the authentication parameters
132: if (user != null && password != null) {
133: sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
134: String userPwd = encoder.encode((user + ":" + password)
135: .getBytes());
136: conn.setRequestProperty("Authentication", "Basic "
137: + userPwd);
138: }
139:
140: // Send the request to the ArcIMS server
141: conn.setDoOutput(true);
142: BufferedOutputStream os = new BufferedOutputStream(conn
143: .getOutputStream());
144:
145: // Send the request to the server
146: os.write(Xml.getString(request).getBytes());
147: os.flush();
148: os.close();
149: } catch (IOException e) {
150: e.printStackTrace();
151: throw (e);
152: }
153: }
154:
155: }
|