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.fao.oaipmh.requests;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.IOException;
028: import java.io.UnsupportedEncodingException;
029: import java.net.URL;
030: import java.util.ArrayList;
031: import org.apache.commons.httpclient.Cookie;
032: import org.apache.commons.httpclient.Credentials;
033: import org.apache.commons.httpclient.Header;
034: import org.apache.commons.httpclient.HttpClient;
035: import org.apache.commons.httpclient.HttpMethodBase;
036: import org.apache.commons.httpclient.HttpState;
037: import org.apache.commons.httpclient.NameValuePair;
038: import org.apache.commons.httpclient.UsernamePasswordCredentials;
039: import org.apache.commons.httpclient.auth.AuthScope;
040: import org.apache.commons.httpclient.cookie.CookiePolicy;
041: import org.apache.commons.httpclient.methods.GetMethod;
042: import org.apache.commons.httpclient.methods.PostMethod;
043: import org.fao.oaipmh.util.Xml;
044: import org.jdom.Element;
045: import org.jdom.JDOMException;
046:
047: //=============================================================================
048:
049: public class Transport {
050: public enum Method {
051: GET, POST
052: }
053:
054: //---------------------------------------------------------------------------
055: //---
056: //--- Constructor
057: //---
058: //---------------------------------------------------------------------------
059:
060: public Transport() {
061: this (null);
062: }
063:
064: //---------------------------------------------------------------------------
065:
066: public Transport(String host) {
067: this (host, 80);
068: }
069:
070: //---------------------------------------------------------------------------
071:
072: public Transport(String host, int port) {
073: this .host = host;
074: this .port = port;
075:
076: setMethod(Method.POST);
077: state.addCookie(cookie);
078: client.setState(state);
079: client.getParams().setCookiePolicy(
080: CookiePolicy.BROWSER_COMPATIBILITY);
081: }
082:
083: //---------------------------------------------------------------------------
084: //---
085: //--- API methods
086: //---
087: //---------------------------------------------------------------------------
088:
089: public String getHost() {
090: return host;
091: }
092:
093: public int getPort() {
094: return port;
095: }
096:
097: public String getAddress() {
098: return address;
099: }
100:
101: public Method getMethod() {
102: return method;
103: }
104:
105: public String getSentData() {
106: return sentData;
107: }
108:
109: public String getReceivedData() {
110: return receivedData;
111: }
112:
113: //---------------------------------------------------------------------------
114:
115: public void setHost(String host) {
116: this .host = host;
117: }
118:
119: //---------------------------------------------------------------------------
120:
121: public void setPort(int port) {
122: this .port = port;
123: }
124:
125: //---------------------------------------------------------------------------
126:
127: public void setAddress(String address) {
128: this .address = address;
129: }
130:
131: //---------------------------------------------------------------------------
132:
133: public void setMethod(Method m) {
134: method = m;
135: }
136:
137: //---------------------------------------------------------------------------
138:
139: public void setUrl(URL url) {
140: host = url.getHost();
141: port = url.getPort();
142: address = url.getPath();
143:
144: if (port == -1)
145: port = 80;
146: }
147:
148: //---------------------------------------------------------------------------
149:
150: public void setCredentials(String username, String password) {
151: this .useAuthent = (username != null);
152:
153: if (username != null) {
154: Credentials cred = new UsernamePasswordCredentials(
155: username, password);
156: AuthScope scope = new AuthScope(AuthScope.ANY_HOST,
157: AuthScope.ANY_PORT, AuthScope.ANY_REALM);
158:
159: client.getState().setCredentials(scope, cred);
160: }
161: }
162:
163: //---------------------------------------------------------------------------
164:
165: public Element execute() throws IOException, JDOMException {
166: HttpMethodBase httpMethod = setupHttpMethod();
167:
168: return doExecute(httpMethod);
169: }
170:
171: //---------------------------------------------------------------------------
172:
173: /* package */void clearParameters() {
174: alParams.clear();
175: }
176:
177: //---------------------------------------------------------------------------
178:
179: /* package */void addParameter(String name, String value) {
180: alParams.add(new NameValuePair(name, value));
181: }
182:
183: //---------------------------------------------------------------------------
184: //---
185: //--- Private methods
186: //---
187: //---------------------------------------------------------------------------
188:
189: private HttpMethodBase setupHttpMethod() {
190: HttpMethodBase httpMethod;
191:
192: if (method == Method.GET) {
193: httpMethod = new GetMethod();
194: httpMethod.setQueryString(alParams
195: .toArray(new NameValuePair[1]));
196: } else {
197: PostMethod pm = new PostMethod();
198: pm.setRequestBody(alParams.toArray(new NameValuePair[1]));
199:
200: httpMethod = pm;
201: }
202:
203: httpMethod.setPath(address);
204: httpMethod.setDoAuthentication(useAuthent);
205:
206: return httpMethod;
207: }
208:
209: //---------------------------------------------------------------------------
210:
211: private Element doExecute(HttpMethodBase httpMethod)
212: throws IOException, JDOMException {
213: client.getHostConfiguration().setHost(host, port, "http");
214:
215: byte[] data = null;
216:
217: try {
218: client.executeMethod(httpMethod);
219: data = httpMethod.getResponseBody();
220:
221: Element response = Xml.loadStream(new ByteArrayInputStream(
222: data));
223:
224: setupSentData(httpMethod);
225: setupReceivedData(httpMethod, data);
226:
227: return response;
228: } finally {
229: httpMethod.releaseConnection();
230: }
231: }
232:
233: //---------------------------------------------------------------------------
234:
235: private void setupSentData(HttpMethodBase httpMethod) {
236: sentData = httpMethod.getName() + " " + httpMethod.getPath();
237:
238: if (httpMethod.getQueryString() != null)
239: sentData += "?" + httpMethod.getQueryString();
240:
241: sentData += "\r\n";
242:
243: for (Header h : httpMethod.getRequestHeaders())
244: sentData += h;
245:
246: sentData += "\r\n";
247: }
248:
249: //---------------------------------------------------------------------------
250:
251: private void setupReceivedData(HttpMethodBase httpMethod,
252: byte[] response) {
253: receivedData = httpMethod.getStatusText() + "\r\r";
254:
255: for (Header h : httpMethod.getResponseHeaders())
256: receivedData += h;
257:
258: receivedData += "\r\n";
259:
260: try {
261: if (response != null)
262: receivedData += new String(response, "UTF8");
263: } catch (UnsupportedEncodingException e) {
264: }
265: }
266:
267: //---------------------------------------------------------------------------
268: //---
269: //--- Variables
270: //---
271: //---------------------------------------------------------------------------
272:
273: private String host;
274: private int port;
275: private String address;
276: private Method method;
277: private boolean useAuthent;
278:
279: private HttpClient client = new HttpClient();
280: private HttpState state = new HttpState();
281: private Cookie cookie = new Cookie();
282:
283: private ArrayList<NameValuePair> alParams = new ArrayList<NameValuePair>();
284:
285: //--- transient vars
286:
287: private String sentData;
288: private String receivedData;
289: }
290:
291: //=============================================================================
|