source: endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/StrabonEndpoint.java @ 670:f443dfe30f78

Revision 670:f443dfe30f78, 2.3 KB checked in by Babis Nikolaou <charnik@…>, 6 years ago (diff)

completed implementation of query() method 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.HttpStatus;
17import org.apache.commons.httpclient.methods.PostMethod;
18import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
19import org.openrdf.rio.RDFFormat;
20
21/**
22 * This class is the implementation of a java client for accessing
23 * StrabonEndpoint instances.
24 *
25 * @author Charalampos Nikolaou <charnik@di.uoa.gr>
26 */
27public class StrabonEndpoint extends SpatialEndpointImpl {
28
29        public StrabonEndpoint(String host, int port) {
30                super(host, port);
31        }
32
33        @Override
34        public String query(String sparqlQuery, stSPARQLQueryResultFormat format) throws IOException {
35                // create a post method to execute
36                HttpMethod method = new PostMethod(getConnectionURL());
37               
38                // set the query parameter
39                method.getParams().setParameter("query", sparqlQuery);
40               
41                // set the accept format
42                method.setRequestHeader("Accept", format.getDefaultMIMEType());
43               
44                try {
45                        // execute the method
46                        int statusCode = hc.executeMethod(method);
47
48                        // check the status code
49                        if (statusCode != HttpStatus.SC_OK) {
50                                System.err.println("Method failed: " + method.getStatusLine());
51                        }
52
53                        // Read the response body.
54                        byte[] responseBody = method.getResponseBody();
55
56                        // Deal with the response.
57                        // Use caution: ensure correct character encoding and is not binary
58                        // data
59                        return new String(responseBody);
60
61                } catch (IOException e) {
62                        throw e;
63                       
64                } finally {
65                        // release the connection.
66                        method.releaseConnection();
67                }
68        }
69
70        @Override
71        public boolean store(String data, RDFFormat format) {
72                return false;
73        }
74
75        @Override
76        public boolean store(URL data, RDFFormat format) {
77                return false;
78        }
79
80        @Override
81        public boolean update(String sparqlUpdate) {
82                return false;
83        }
84
85        @Override
86        public boolean describe(String sparqlDescribe) {
87                return false;
88        }
89
90        @Override
91        public boolean construct(String sparqlConstruct) {
92                return false;
93        }
94
95}
Note: See TracBrowser for help on using the repository browser.