Strabon
changeset 764:91c47ca1dd39
updated endpoint client to use the newest version of httpclient (4.2) and added a main method to StrabonEndpoint, thus the respective jar in endpoint-client/target can be used in command line as well
author | Babis Nikolaou <charnik@di.uoa.gr> |
---|---|
date | Tue Dec 04 22:40:26 2012 +0200 (2012-12-04) |
parents | 9d2553759226 |
children | ca4221c776e2 |
files | endpoint-client/pom.xml endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpointImpl.java endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java |
line diff
1.1 --- a/endpoint-client/pom.xml Fri Nov 30 15:23:53 2012 +0200 1.2 +++ b/endpoint-client/pom.xml Tue Dec 04 22:40:26 2012 +0200 1.3 @@ -23,31 +23,12 @@ 1.4 <groupId>org.openrdf.sesame</groupId> 1.5 <artifactId>sesame-queryresultio-spatial-api</artifactId> 1.6 </dependency> 1.7 - <!-- 1.8 + 1.9 <dependency> 1.10 - <groupId>org.openrdf.sesame</groupId> 1.11 - <artifactId>sesame-queryresultio-spatial-api</artifactId> 1.12 - </dependency> 1.13 - --> 1.14 - <dependency> 1.15 - <groupId>commons-httpclient</groupId> 1.16 - <artifactId>commons-httpclient</artifactId> 1.17 - <exclusions> 1.18 - <exclusion> 1.19 - <groupId>commons-logging</groupId> 1.20 - <artifactId>commons-logging</artifactId> 1.21 - </exclusion> 1.22 - <exclusion> 1.23 - <!-- httpclient includes older codec --> 1.24 - <groupId>commons-codec</groupId> 1.25 - <artifactId>commons-codec</artifactId> 1.26 - </exclusion> 1.27 - </exclusions> 1.28 - </dependency> 1.29 - <dependency> 1.30 - <groupId>commons-codec</groupId> 1.31 - <artifactId>commons-codec</artifactId> 1.32 - <scope>runtime</scope> 1.33 + <groupId>org.apache.httpcomponents</groupId> 1.34 + <artifactId>httpclient</artifactId> 1.35 + <!-- version was put in purpose! do not remove it; --> 1.36 + <version>4.2</version> 1.37 </dependency> 1.38 1.39 <!-- Testing: JUnit -->
2.1 --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpointImpl.java Fri Nov 30 15:23:53 2012 +0200 2.2 +++ b/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpointImpl.java Tue Dec 04 22:40:26 2012 +0200 2.3 @@ -9,8 +9,10 @@ 2.4 */ 2.5 package eu.earthobservatory.org.StrabonEndpoint.client; 2.6 2.7 -import org.apache.commons.httpclient.HttpClient; 2.8 -import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 2.9 +import org.apache.http.client.HttpClient; 2.10 +import org.apache.http.conn.ClientConnectionManager; 2.11 +import org.apache.http.impl.client.DefaultHttpClient; 2.12 +import org.apache.http.impl.conn.PoolingClientConnectionManager; 2.13 2.14 /** 2.15 * @author Charalampos Nikolaou <charnik@di.uoa.gr> 2.16 @@ -50,7 +52,7 @@ 2.17 * The connection manager that manages sharing of connections to endpoints 2.18 * among several threads. 2.19 */ 2.20 - private MultiThreadedHttpConnectionManager connectionManager; 2.21 + private ClientConnectionManager connectionManager; 2.22 2.23 /** 2.24 * The HttpClient to be used for connecting to an endpoint. 2.25 @@ -68,10 +70,10 @@ 2.26 this.endpointName = (endpointName == null ? "":endpointName); 2.27 2.28 // create a connection manager for allowing the users of this class use threads 2.29 - connectionManager = new MultiThreadedHttpConnectionManager(); 2.30 + connectionManager = new PoolingClientConnectionManager(); 2.31 2.32 // create an HttpClient instance that establishes connections based on the connection manager 2.33 - hc = new HttpClient(connectionManager); 2.34 + hc = new DefaultHttpClient(connectionManager); 2.35 } 2.36 2.37 public String getHost() {
3.1 --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java Fri Nov 30 15:23:53 2012 +0200 3.2 +++ b/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java Tue Dec 04 22:40:26 2012 +0200 3.3 @@ -9,10 +9,21 @@ 3.4 */ 3.5 package eu.earthobservatory.org.StrabonEndpoint.client; 3.6 3.7 +import java.io.BufferedReader; 3.8 import java.io.IOException; 3.9 +import java.io.InputStream; 3.10 +import java.io.InputStreamReader; 3.11 import java.net.URL; 3.12 +import java.nio.charset.Charset; 3.13 +import java.util.ArrayList; 3.14 +import java.util.List; 3.15 3.16 -import org.apache.commons.httpclient.methods.PostMethod; 3.17 +import org.apache.http.HttpEntity; 3.18 +import org.apache.http.HttpResponse; 3.19 +import org.apache.http.NameValuePair; 3.20 +import org.apache.http.client.entity.UrlEncodedFormEntity; 3.21 +import org.apache.http.client.methods.HttpPost; 3.22 +import org.apache.http.message.BasicNameValuePair; 3.23 import org.openrdf.query.resultio.stSPARQLQueryResultFormat; 3.24 import org.openrdf.rio.RDFFormat; 3.25 3.26 @@ -37,20 +48,70 @@ 3.27 assert(format != null); 3.28 3.29 // create a post method to execute 3.30 - PostMethod method = new PostMethod(getConnectionURL() + "/Query"); 3.31 + HttpPost method = new HttpPost(getConnectionURL() + "/Query"); 3.32 3.33 // set the query parameter 3.34 - method.setParameter("query", sparqlQuery); 3.35 + List<NameValuePair> params = new ArrayList<NameValuePair>(); 3.36 + params.add(new BasicNameValuePair("query", sparqlQuery)); 3.37 + UrlEncodedFormEntity encodedEntity = new UrlEncodedFormEntity(params, Charset.defaultCharset()); 3.38 + method.setEntity(encodedEntity); 3.39 + 3.40 + // set the content type 3.41 + method.setHeader("Content-Type", "application/x-www-form-urlencoded"); 3.42 3.43 // set the accept format 3.44 - method.addRequestHeader("Accept", format.getDefaultMIMEType()); 3.45 - //System.out.println(method.getRequestHeader("Accept")); 3.46 + method.addHeader("Accept", format.getDefaultMIMEType()); 3.47 3.48 try { 3.49 + // response that will be filled next 3.50 + String responseBody = ""; 3.51 + 3.52 // execute the method 3.53 - int statusCode = hc.executeMethod(method); 3.54 + HttpResponse response = hc.execute(method); 3.55 + int statusCode = response.getStatusLine().getStatusCode(); 3.56 3.57 - return new StrabonEndpointResult(statusCode, method.getStatusText(), method.getResponseBodyAsString()); 3.58 + // If the response does not enclose an entity, there is no need 3.59 + // to worry about connection release 3.60 + HttpEntity entity = response.getEntity(); 3.61 + if (entity != null) { 3.62 + InputStream instream = entity.getContent(); 3.63 + try { 3.64 + 3.65 + BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); 3.66 + StringBuffer strBuf = new StringBuffer(); 3.67 + 3.68 + // do something useful with the response 3.69 + String nextLine; 3.70 + while ((nextLine = reader.readLine()) != null) { 3.71 + strBuf.append(nextLine + "\n"); 3.72 + } 3.73 + 3.74 + // remove last newline character 3.75 + if (strBuf.length() > 0) { 3.76 + strBuf.setLength(strBuf.length() - 1); 3.77 + } 3.78 + 3.79 + responseBody = strBuf.toString(); 3.80 + 3.81 + } catch (IOException ex) { 3.82 + // In case of an IOException the connection will be released 3.83 + // back to the connection manager automatically 3.84 + throw ex; 3.85 + 3.86 + } catch (RuntimeException ex) { 3.87 + // In case of an unexpected exception you may want to abort 3.88 + // the HTTP request in order to shut down the underlying 3.89 + // connection and release it back to the connection manager. 3.90 + method.abort(); 3.91 + throw ex; 3.92 + 3.93 + } finally { 3.94 + // Closing the input stream will trigger connection release 3.95 + instream.close(); 3.96 + } 3.97 + } 3.98 + 3.99 + return new StrabonEndpointResult(statusCode, response.getStatusLine().getReasonPhrase(), responseBody); 3.100 3.101 } catch (IOException e) { 3.102 throw e; 3.103 @@ -85,5 +146,44 @@ 3.104 public EndpointResult construct(String sparqlConstruct) { 3.105 return null; 3.106 } 3.107 - 3.108 + 3.109 + public static void main(String args[]) { 3.110 + if (args.length < 4) { 3.111 + System.err.println("Usage: eu.earthobservatory.org.StrabonEndpoint.client.StrabonEndpoint <HOST> <PORT> <APPNAME> [<FORMAT>]"); 3.112 + System.err.println(" where <HOST> is the hostname of the Strabon Endpoint"); 3.113 + System.err.println(" <PORT> is the port to connect to on the host"); 3.114 + System.err.println(" <APPNAME> is the application name of Strabon Endpoint as deployed in the Tomcat container"); 3.115 + System.err.println(" <QUERY> is the query to execute on the endpoint"); 3.116 + System.err.println(" [<FORMAT>] is the format of your results. Should be one of XML (default), KML, KMZ, GeoJSON, TSV, or HTML."); 3.117 + System.exit(1); 3.118 + } 3.119 + 3.120 + String host = args[0]; 3.121 + Integer port = new Integer(args[1]); 3.122 + String appName = args[2]; 3.123 + String query = args[3]; 3.124 + String format = ""; 3.125 + 3.126 + if (args.length == 5) { 3.127 + format = args[4]; 3.128 + 3.129 + } else { 3.130 + format = "XML"; 3.131 + } 3.132 + 3.133 + StrabonEndpoint endpoint = new StrabonEndpoint(host, port, appName); 3.134 + 3.135 + try { 3.136 + EndpointResult result = endpoint.query(query, stSPARQLQueryResultFormat.valueOf(format)); 3.137 + 3.138 + System.out.println("Status code: " + result.getStatusCode()); 3.139 + System.out.println("Status text: " + result.getStatusText()); 3.140 + System.out.println("<----- Result ----->"); 3.141 + System.out.println(result.getResponse().replaceAll("\n", "\n\t")); 3.142 + System.out.println("<----- Result ----->"); 3.143 + 3.144 + } catch (IOException e) { 3.145 + e.printStackTrace(); 3.146 + } 3.147 + } 3.148 }