1
2
3
4
5 package org.astrogrid.dataservice.service.servlet;
6 import java.io.IOException;
7 import java.net.URISyntaxException;
8 import java.net.URL;
9 import java.security.Principal;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.xml.parsers.ParserConfigurationException;
14 import org.apache.commons.logging.LogFactory;
15 import org.astrogrid.dataservice.service.DataServer;
16 import org.astrogrid.dataservice.service.ServletHelper;
17 import org.astrogrid.query.Query;
18 import org.astrogrid.query.QueryException;
19 import org.astrogrid.slinger.sources.SourceIdentifier;
20 import org.astrogrid.slinger.sourcetargets.URISourceTargetMaker;
21 import org.astrogrid.webapp.DefaultServlet;
22 import org.xml.sax.SAXException;
23
24 /***
25 * A servlet for submitting a query asynchronously.
26 * The parameters are:
27 * AdqlUrl, AdqlSql or AdqlXml that specify the query to be run.
28 * Target: where the results are to be sent
29 * Format: what format the results should be sent in (eg VOTABLE, CSV - defaults to VOTABLE)
30 *
31 * @author mch
32 */
33 public class SubmitQuery extends DefaultServlet {
34
35 DataServer server = new DataServer();
36
37 public void doGet(HttpServletRequest request,
38 HttpServletResponse response) throws ServletException, IOException {
39
40 Query query = null;
41
42 try {
43 query = makeQuery(request);
44
45 if (query.getTarget() == null) {
46
47 throw new QueryException("Must specify a Target argument for the results to be sent to when submitting Queries (otherwise use AskQuery)", null);
48 }
49
50 response.setContentType("text/plain");
51 String id = server.submitQuery(ServletHelper.getUser(request), query, request.getRemoteHost()+" ("+request.getRemoteAddr()+") via SubmitQuery servlet");
52 response.getWriter().write(id);
53 }
54 catch (Throwable th) {
55 LogFactory.getLog(request.getContextPath()).error(th+" submitting query",th);
56 doError(response, "Submitting "+query+" -> "+ServletHelper.makeReturnSpec(request),th);
57 }
58 }
59
60 /*** Creates a query from the parameters in the given request. Static public so other servlets can use it */
61 public static Query makeQuery(HttpServletRequest request) throws IOException, ParserConfigurationException, QueryException, SAXException, URISyntaxException {
62
63 String adqlUri = request.getParameter("AdqlUri");
64 String adqlSql = request.getParameter("AdqlSql");
65 String adqlXml = request.getParameter("AdqlXml");
66
67 Principal user = ServletHelper.getUser(request);
68
69 Query query = null;
70
71
72 if (adqlUri != null) {
73 SourceIdentifier source = URISourceTargetMaker.makeSourceTarget(adqlUri);
74 query = new Query(source.openInputStream());
75 }
76 else if (adqlSql != null) {
77 throw new QueryException("Query creation from adql/sql is not supported");
78 }
79 else if (adqlXml != null) {
80 query = new Query(adqlXml);
81 }
82 else {
83 throw new QueryException("Must specify AdqlUri or AdqlXml parameter");
84 }
85 if (!ServletHelper.isCountReq(request)) {
86 ServletHelper.fillReturnSpec(query.getResultsDef(), request );
87 }
88 return query;
89 }
90 }