1
2
3
4 package org.astrogrid.xmldb.eXist.server;
5
6 import org.astrogrid.config.Config;
7
8 import java.io.IOException;
9 import java.io.UnsupportedEncodingException;
10 import javax.xml.parsers.ParserConfigurationException;
11 import org.xml.sax.SAXException;
12 import java.net.MalformedURLException;
13
14 import java.net.URL;
15 import java.net.HttpURLConnection;
16 import java.net.URLEncoder;
17 import org.astrogrid.util.DomHelper;
18 import org.apache.axis.AxisFault;
19
20 import java.io.DataOutputStream;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.w3c.dom.Document;
26 import org.w3c.dom.NodeList;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.Node;
29
30 import javax.xml.namespace.QName;
31
32 import org.apache.commons.httpclient.HttpClient;
33 import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
34 import org.apache.commons.httpclient.methods.PostMethod;
35 import org.apache.commons.httpclient.UsernamePasswordCredentials;
36
37
38
39 public class QueryDBService {
40
41 private static final Log log = LogFactory.getLog(QueryDBService.class);
42
43 public static Config conf = null;
44 private static String existLocation = null;
45
46 static {
47 if(conf == null) {
48 conf = org.astrogrid.config.SimpleConfig.getSingleton();
49 existLocation = conf.getString("exist.db.url");
50 }
51 }
52
53
54 public Document query(String collectionName, String xql) throws AxisFault {
55 log.debug("start query");
56
57 try {
58 return runQuery(collectionName,xql);
59 }catch(MalformedURLException mue) {
60 log.error(mue);
61 throw new AxisFault("URL incorrect", mue);
62 }catch(ParserConfigurationException pce) {
63 log.error(pce);
64 throw new AxisFault("XML Parser Configuration error", pce);
65 }catch(IOException ioe) {
66 log.error(ioe);
67 throw new AxisFault("IO Excepiton error", ioe);
68 }catch(SAXException se) {
69 log.error(se);
70 throw new AxisFault("SAX Exception - could not parse the result into xml", se);
71 }finally {
72 log.debug("end query");
73 }
74 }
75
76 private String getQueryUrl(String collectionName) throws MalformedURLException, UnsupportedEncodingException
77 {
78 log.debug("start getQueryUrl");
79 String location = existLocation;
80 if(collectionName == null || collectionName.trim().length() == 0) {
81 collectionName = "";
82 }
83 location += "/servlet/db/" + collectionName;
84 log.debug("end getQueryUrl");
85 return location;
86
87 }
88
89 public Document getCollection(String collectionName)
90 throws MalformedURLException, ParserConfigurationException,
91 IOException, UnsupportedEncodingException, SAXException {
92 return DomHelper.newDocument(new URL(getQueryUrl(collectionName)));
93 }
94
95
96 public Document getResource(String collectionName,String resource)
97 throws MalformedURLException, ParserConfigurationException,
98 IOException, UnsupportedEncodingException, SAXException
99 {
100
101 String resourceUniqueName = resource.replaceAll("[^//w*]","_");
102 return DomHelper.newDocument(new URL(getQueryUrl(collectionName + "/" + resourceUniqueName + ".xml")));
103 }
104
105
106 public Document runQuery(String collectionName, String xql)
107 throws MalformedURLException, ParserConfigurationException,
108 IOException, UnsupportedEncodingException, SAXException {
109 log.debug("start runQuery");
110 Document queryDoc = null;
111 int numberOfResourcesReturned = conf.getInt("exist.query.returncount", 25);
112 log.info("max number of resources to be returned = " + numberOfResourcesReturned);
113
114 String query = "<query xmlns=\"http://exist.sourceforge.net/NS/exist\"" +
115 " start=\"1\" max=\"" + numberOfResourcesReturned + "\">" +
116 "<text><![CDATA[" + xql + "]]></text></query>";
117 queryDoc = DomHelper.newDocument(query);
118
119 log.info("query to be posted = " + query);
120 long beginQ = System.currentTimeMillis();
121 log.info("Query begin time: " + beginQ);
122
123 HttpClient httpclient = new HttpClient();
124
125
126
127
128
129
130
131
132
133
134
135
136
137 String queryURL = getQueryUrl(collectionName);
138 PostMethod post = new PostMethod(queryURL);
139
140
141
142
143
144
145
146
147
148
149
150
151
152 String xml = DomHelper.DocumentToString(queryDoc);
153 post.setRequestBody(xml);
154 if (xml.length() < Integer.MAX_VALUE) {
155 post.setRequestContentLength((int)xml.length());
156 } else {
157 post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
158 }
159
160
161
162 post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
163
164
165
166 int result = httpclient.executeMethod(post);
167 log.info("result of status code of query = " + result);
168 if(result != 200) {
169 String error = post.getResponseBodyAsString();
170 log.error("Seems to be a problem with query = " + error);
171 throw new IOException(error);
172 }
173
174 Document resultDoc = DomHelper.newDocument(post.getResponseBodyAsStream());
175
176 post.releaseConnection();
177
178
179
180 long endQ = System.currentTimeMillis();
181 log.info("Query end time: " + endQ);
182 log.info("Query total time: " + (endQ - beginQ));
183 log.debug("end runQuery");
184 return resultDoc;
185 }
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212