source: endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java @ 671:24a6e7d423eb

Revision 671:24a6e7d423eb, 2.2 KB checked in by Babis Nikolaou <charnik@…>, 6 years ago (diff)

added interface (EndpointResult?) for the return type of the answer of an endpoint. Added a prototypical implementation for StrabonEndpoint?

Line 
1/**
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright (C) 2012, Pyravlos Team
7 *
8 * http://www.strabon.di.uoa.gr/
9 */
10package eu.earthobservatory.org.StrabonEndpoint.client;
11
12import java.io.IOException;
13import java.net.URL;
14
15import org.apache.commons.httpclient.HttpMethod;
16import org.apache.commons.httpclient.methods.PostMethod;
17import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
18import org.openrdf.rio.RDFFormat;
19
20/**
21 * This class is the implementation of a java client for accessing
22 * StrabonEndpoint instances.
23 *
24 * @author Charalampos Nikolaou <charnik@di.uoa.gr>
25 */
26public class StrabonEndpoint extends SpatialEndpointImpl {
27
28        public StrabonEndpoint(String host, int port) {
29                super(host, port);
30        }
31
32        @Override
33        public EndpointResult query(String sparqlQuery, stSPARQLQueryResultFormat format) throws IOException {
34                // create a post method to execute
35                HttpMethod method = new PostMethod(getConnectionURL());
36               
37                // set the query parameter
38                method.getParams().setParameter("query", sparqlQuery);
39               
40                // set the accept format
41                method.setRequestHeader("Accept", format.getDefaultMIMEType());
42               
43                try {
44                        // execute the method
45                        int statusCode = hc.executeMethod(method);
46
47                        // Read the response body.
48                        byte[] responseBody = method.getResponseBody();
49
50                        // Deal with the response.
51                        // Use caution: ensure correct character encoding and is not binary
52                        // data
53                        String response = new String(responseBody);
54                       
55                        return new StrabonEndpointResult(statusCode, method.getStatusText(), response);
56
57                } catch (IOException e) {
58                        throw e;
59                       
60                } finally {
61                        // release the connection.
62                        method.releaseConnection();
63                }
64        }
65
66        @Override
67        public boolean store(String data, RDFFormat format) {
68                return false;
69        }
70
71        @Override
72        public boolean store(URL data, RDFFormat format) {
73                return false;
74        }
75
76        @Override
77        public boolean update(String sparqlUpdate) {
78                return false;
79        }
80
81        @Override
82        public EndpointResult describe(String sparqlDescribe) {
83                return null;
84        }
85
86        @Override
87        public EndpointResult construct(String sparqlConstruct) {
88                return null;
89        }
90
91}
Note: See TracBrowser for help on using the repository browser.