1
2
3
4
5
6
7 package org.astrogrid.xdbserver.xql;
8
9 import java.io.DataOutputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.net.HttpURLConnection;
13 import java.net.URL;
14 import java.security.Principal;
15 import javax.xml.parsers.ParserConfigurationException;
16 import org.astrogrid.cfg.ConfigFactory;
17 import org.astrogrid.dataservice.queriers.DefaultPlugin;
18 import org.astrogrid.dataservice.queriers.Querier;
19 import org.astrogrid.dataservice.queriers.status.QuerierQuerying;
20 import org.astrogrid.query.Query;
21 import org.astrogrid.query.QueryException;
22 import org.astrogrid.query.xql.XqlMaker;
23 import org.astrogrid.xml.DomHelper;
24 import org.xml.sax.SAXException;
25
26 /***
27 * Plugin to use an eXist database that is colocated with this PAL (ie, as another
28 * webapp on the same machine)
29 *
30 * @author Kevin B, Martin H
31 */
32
33 public class ExistPlugin extends DefaultPlugin {
34
35 /*** Plugin implementation - carries out query.
36 */
37 public void askQuery(Principal user, Query query, Querier querier) throws IOException {
38
39 querier.setStatus(new QuerierQuerying(querier.getStatus(), query.toString()));
40
41 InputStream eXistIn = askExist(query);
42
43 if (!aborted) {
44 XmlResults results = new XmlResults(querier, eXistIn);
45 results.send(query.getResultsDef(), querier.getUser());
46 }
47 }
48
49 /*** Returns just the number of matches rather than the list of matches */
50 public long getCount(Principal user, Query query, Querier querier) throws IOException {
51 throw new UnsupportedOperationException("To do");
52 }
53
54
55 /*** Returns the limit configured for this application */
56 public long getLocalLimit() {
57 return ConfigFactory.getCommonConfig().getInt("datacenter.results.limit",300);
58 }
59
60 /*** queries eXist with the given query */
61 public InputStream askExist(Query query) throws IOException {
62 XqlMaker maker = new XqlMaker();
63 String xql = maker.getXql(query);
64 long limit = query.getLimit();
65 if ((limit <=0) ||
66 ((limit > getLocalLimit()) && (getLocalLimit() >0))) {
67 limit = getLocalLimit();
68 }
69
70 return askExist(xql, limit);
71 }
72
73 /*** queries exist with the given Xql and max limit number of returned values,
74 * returning a stream ot the results */
75 public InputStream askExist(String xql, long limit) throws IOException {
76
77 String query = "<query xmlns='http://exist.sourceforge.net/NS/exist'" +
78 " start='1' max='" + limit + "\">" +
79 "<text><![CDATA[" + xql + "]]></text></query>";
80
81 try {
82 DomHelper.newDocument(query);
83 }
84 catch(SAXException se) {
85 throw new QueryException(se+" in generated XQL (Internal Server Error)",se);
86 }
87 URL postUrl = new URL(ConfigFactory.getCommonConfig().getUrl("exist.db.url")+
88 "/servlet/db/");
89
90 HttpURLConnection huc = (HttpURLConnection) postUrl.openConnection();
91 huc.setRequestProperty("Content-Type", "text/xml");
92 huc.setDoOutput(true);
93 huc.setDoInput(true);
94 huc.connect();
95
96 DataOutputStream dos = new DataOutputStream(huc.getOutputStream());
97 dos.writeChars(query);
98 dos.flush();
99 dos.close();
100
101 if (!aborted) {
102 return huc.getInputStream();
103 }
104 else {
105 return null;
106 }
107
108
109 }
110
111
112
113 /*** Returns the formats that this plugin can provide. Asks the results class; override in subclasse if nec */
114 public String[] getFormats() {
115 return XmlResults.listFormats();
116 }
117
118
119
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172