1
2
3
4
5
6
7 package org.astrogrid.dataservice.service.soap;
8
9 import java.io.IOException;
10 import java.io.StringWriter;
11 import java.net.MalformedURLException;
12 import java.rmi.RemoteException;
13 import javax.xml.rpc.ServiceException;
14 import javax.xml.rpc.server.ServiceLifecycle;
15 import org.apache.axis.AxisFault;
16 import org.apache.axis.MessageContext;
17 import org.astrogrid.dataservice.queriers.status.QuerierStatus;
18 import org.astrogrid.dataservice.service.AxisDataServer;
19 import org.astrogrid.query.Query;
20 import org.astrogrid.query.adql.Adql074Writer;
21 import org.astrogrid.query.adql.AdqlQueryMaker;
22 import org.astrogrid.query.sql.SqlParser;
23 import org.astrogrid.slinger.targets.TargetMaker;
24 import org.astrogrid.status.DefaultTaskStatus;
25 import org.astrogrid.status.ServiceStatus;
26 import org.astrogrid.xml.DomHelper;
27 import org.w3c.dom.Element;
28
29 /***
30 * The implementation of the Datacenter axis web service for end of Itn06
31 * <p>
32 * When Axis receives a SOAP message from the client it is routed to this class for processing.
33 * It is a singleton; state comes from the Queriers.
34
35 * @author M Hill
36 *
37 */
38
39 public class AxisDataService_v06 implements ServiceLifecycle {
40
41
42 AxisDataServer server = new AxisDataServer();
43
44 /*** Called by Axis when the service needs to be initialised. Not actuall of
45 * much use, as it only gets called when Axis is called, and other servlet/JSP access
46 * won't do that
47 */
48 public void init(Object context) throws ServiceException {
49 if (context instanceof javax.xml.rpc.server.ServletEndpointContext) {
50 javax.xml.rpc.server.ServletEndpointContext secontext = (javax.xml.rpc.server.ServletEndpointContext) context;
51
52 }
53 }
54
55 public void destroy() {
56 }
57
58 /***
59 * Ask adql query (blocking request). Returns results.
60 */
61 public String askAdql(Element adql, String requestedFormat) throws AxisFault {
62 try {
63
64 if (MessageContext.getCurrentContext() != null) {
65 MessageContext.getCurrentContext().getMessage().writeTo(System.out);
66 }
67 StringWriter sw = new StringWriter();
68 Query query = AdqlQueryMaker.makeQuery(adql);
69 query.getResultsDef().setFormat(requestedFormat);
70 query.getResultsDef().setTarget(TargetMaker.makeTarget(sw));
71 server.askQuery(server.getUser(), query, server.getSource()+" via AxisDataService06.askAdql");
72 return sw.toString();
73 }
74 catch (Throwable e) {
75 throw server.makeFault(server.SERVERFAULT, e+", asking Query("+adql+", "+requestedFormat+")", e);
76 }
77 }
78
79 /***
80 * Ask adql query (blocking request) sending results to given target, returning
81 * the target uri
82 */
83 public String askAdqlTo(Element adql, String requestedFormat, String target) throws AxisFault {
84 try {
85
86 if (MessageContext.getCurrentContext() != null) {
87 MessageContext.getCurrentContext().getMessage().writeTo(System.out);
88 }
89 if (target == null) {
90 throw new IllegalArgumentException("No target given");
91 }
92 Query query = AdqlQueryMaker.makeQuery(adql);
93 query.getResultsDef().setFormat(requestedFormat);
94 query.getResultsDef().setTarget(TargetMaker.makeTarget(target));
95 server.askQuery(server.getUser(), query, server.getSource()+" via AxisDataService06.askAdql");
96 return target.toString();
97 }
98 catch (Throwable e) {
99 throw server.makeFault(server.SERVERFAULT, e+", asking Query("+adql+", "+requestedFormat+", ->"+target+")", e);
100 }
101 }
102
103 /***
104 * Translation service - Converts SQL-like ADQL into XML ADQL
105 */
106 public Element adqlSql2xml(String sql) throws AxisFault {
107 try {
108 if ((sql == null) || (sql.trim().length()==0)) {
109 throw new IllegalArgumentException("No SQL given");
110 }
111 return DomHelper.newDocument(Adql074Writer.makeAdql(SqlParser.makeQuery(sql), "From SQL: "+sql)).getDocumentElement();
112 }
113 catch (Throwable e) {
114 throw server.makeFault(server.SERVERFAULT, e+", converting SQL "+sql, e);
115 }
116 }
117
118 /*** Returns the number of matches - this ought to be part of the select statement but
119 * it will do for temporary use */
120 public long askCount(Element adql) throws AxisFault {
121 try {
122 Query query = AdqlQueryMaker.makeQuery(adql);
123 return server.askCount(server.getUser(), query, server.getSource()+" via AxisDataService06.askCount");
124 }
125 catch (Throwable e) {
126 throw server.makeFault(server.SERVERFAULT, e+", asking Query("+adql+")", e);
127 }
128 }
129
130 /***
131 * Ask raw sql for blocking operation - returns the results
132 *
133 public String askSql(String sql, String requestedFormat) throws AxisFault {
134 try {
135 if (!ConfigFactory.getCommonConfig().getBoolean(DataServer.SQL_PASSTHROUGH_ENABLED, false)) {
136 throw new UnsupportedOperationException("This server does not support raw SQL queries - use ADQL");
137 }
138
139 StringWriter sw = new StringWriter();
140 Query query = SqlParser.makeQuery(sql);
141 query.getResultsDef().setFormat(requestedFormat);
142 query.getResultsDef().setTarget(TargetIndicator.makeIndicator(sw));
143 server.askQuery(getUser(), query);
144 return sw.toString();
145 }
146 catch (Throwable e) {
147 throw server.makeFault(server.SERVERFAULT, "Error asking Query("+sql+", "+requestedFormat+")", e);
148 }
149 }
150
151 /**
152 * Submit query for asynchronous operation - returns id of query
153 */
154 public String submitAdql(Element adql, String requestedFormat, String resultsTarget) throws AxisFault {
155 try {
156 Query query = AdqlQueryMaker.makeQuery(adql);
157 query.getResultsDef().setFormat(requestedFormat);
158 query.getResultsDef().setTarget(TargetMaker.makeTarget(resultsTarget));
159 return server.submitQuery(server.getUser(), query, server.getSource()+" via AxisDataService06.submitAdql");
160 }
161 catch (MalformedURLException mue) {
162 throw server.makeFault(server.CLIENTFAULT, "malformed resultsTarget", mue);
163 }
164 catch (Throwable th) {
165 throw server.makeFault(server.SERVERFAULT, th+", submitting Adql Query", th);
166 }
167 }
168
169
170 /***
171 * Aborts the query specified by the given id. Returns
172 * nothing - if the server can't stop it it's a server-end problem.
173 */
174 public void abortQuery(String queryId) throws AxisFault {
175 try {
176 server.abortQuery(server.getUser(), queryId);
177 }
178 catch (IOException ioe) {
179 throw server.makeFault(server.SERVERFAULT, ioe+", aborting query "+queryId, ioe);
180 }
181 }
182
183 /***
184 * Returns the state of the query with the given id
185 */
186 public QueryStatusSoapyBean getQueryStatus(String queryId) throws AxisFault {
187 try {
188 QueryStatusSoapyBean soapyStatus = new QueryStatusSoapyBean();
189 soapyStatus.setQueryID(queryId);
190 QuerierStatus status = server.getQueryStatus(server.getUser(), queryId);
191 soapyStatus.setState(status.getState().toString());
192 soapyStatus.setNote(status.getMessage());
193 return soapyStatus;
194 }
195 catch (IOException ioe) {
196 throw server.makeFault(server.SERVERFAULT, ioe+", getting status of query "+queryId, ioe);
197 }
198 }
199
200 /***
201 * Returns the state of the query with the given id
202 */
203 public DefaultTaskStatus getTaskStatus(String queryId) throws AxisFault {
204 try {
205 return server.getQueryStatus(server.getUser(), queryId);
206 }
207 catch (IOException ioe) {
208 throw server.makeFault(server.SERVERFAULT, ioe+", getting status of query "+queryId, ioe);
209 }
210 }
211
212 /***
213 * Returns the state of the service
214 */
215 public ServiceStatus getServiceStatus() throws AxisFault {
216 return server.getStatus().getServiceStatus();
217 }
218
219 /***
220 * Returns a simple text string indicating the state of the service
221 */
222 public String getSimpleServiceStatus() throws AxisFault {
223 return server.getStatus().toString();
224 }
225
226
227 /*** Returns the Registry entries for this service. Same as getMetadata */
228 public String getRegistration() throws RemoteException {
229 try {
230 return server.getMetadata();
231 }
232 catch (IOException ioe) {
233 throw server.makeFault(server.SERVERFAULT, ioe+", getting metadata", ioe);
234 }
235 }
236
237 /*** Returns a document describing the service */
238 public Element getMetadata() throws RemoteException {
239 try {
240 return DomHelper.newDocument(server.getMetadata()).getDocumentElement();
241 }
242 catch (Exception ioe) {
243 throw server.makeFault(server.SERVERFAULT, ioe+", getting metadata", ioe);
244 }
245 }
246
247 /***
248 * Useful mechanism for testing that clients can receive and process faults.
249 * The useful bit of stack trace will be limited to this method but ho hum
250 */
251 public void throwFault() throws AxisFault {
252 throw server.makeFault(server.CLIENTFAULT, "Client asked for this", new IOException("Client deliberately threw test fault "));
253 }
254
255
256
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378