1
2
3
4
5
6
7 package org.astrogrid.dataservice.queriers.pal;
8
9 import java.io.DataOutputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.net.URL;
13 import java.net.URLConnection;
14 import java.net.URLEncoder;
15 import java.security.Principal;
16 import java.util.Date;
17 import org.astrogrid.cfg.ConfigFactory;
18 import org.astrogrid.dataservice.metadata.VoResourcePlugin;
19 import org.astrogrid.dataservice.metadata.v0_10.ProxyResourceSupport;
20 import org.astrogrid.dataservice.queriers.DefaultPlugin;
21 import org.astrogrid.dataservice.queriers.Querier;
22 import org.astrogrid.dataservice.queriers.VotableInResults;
23 import org.astrogrid.dataservice.queriers.status.QuerierQuerying;
24 import org.astrogrid.query.Query;
25
26 import org.astrogrid.slinger.sourcetargets.UrlSourceTarget;
27
28 /***
29 * A plugin that passes the query on to a remote PAL installation
30 *
31 * * @author M Hill
32 */
33
34 public class PalProxyPlugin extends DefaultPlugin implements VoResourcePlugin {
35
36
37 public static final String PAL_TARGET = "datacenter.palproxy.targetstem";
38
39 /*** false if forwardable targets are given to target; true if results must come back through the proxy */
40 public static final String PROXY_RESULTS = "datacenter.palproxy.proxyresults";
41
42 private boolean proxyResults = true;
43
44 public PalProxyPlugin() {
45 proxyResults = ConfigFactory.getCommonConfig().getBoolean(PROXY_RESULTS, proxyResults);
46 }
47
48 /*** performs a synchronous call to the database, submitting the given query
49 *
50 */
51 public void askQuery(Principal user, Query query, Querier querier) throws IOException {
52
53 querier.setStatus(new QuerierQuerying(querier.getStatus(), query.toString()));
54 useServlet(query, querier);
55 }
56
57 /*** Use the Datacenter delegate
58 public void useDelegate(Query query, Querier querier) throws IOException {
59
60 proxyResults = true; //need an asynch way of specifying target. Or just use servlets
61
62 String endpoint = ConfigFactory.getCommonConfig().getUrl(PAL_TARGET)+"/services/AxisDataService05";
63
64 QuerySearcher delegate = null;
65 try {
66 delegate = DatacenterDelegateFactory.makeQuerySearcher(
67 querier.getUser(),
68 endpoint,
69 DatacenterDelegateFactory.ASTROGRID_WEB_SERVICE);
70
71 }
72 catch (ServiceException e) {
73 throw new IOException(e+", connecting to "+endpoint);
74 }
75
76 delegate.setTimeout(60000);
77 InputStream in = delegate.askQuery(query);
78
79 if (proxyResults) {
80 VotableInResults vot = new VotableInResults(querier, in);
81 vot.send(query.getResultsDef(), querier.getUser());
82 }
83
84 }
85
86
87 /** Use the remote PAL's servlet interface */
88 public void useServlet(Query query, Querier querier) throws IOException {
89 URL targetPal = new URL(ConfigFactory.getCommonConfig().getUrl(PAL_TARGET)+"/SubmitAdql");
90
91 querier.getStatus().addDetail("Proxying to: "+targetPal);
92 log.info("Proxying Query to: "+targetPal);
93
94 URLConnection connection = targetPal.openConnection();
95
96
97 connection.setDoInput(true);
98 connection.setDoOutput(true);
99 connection.setUseCaches(false);
100 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
101
102
103 DataOutputStream out = new DataOutputStream(connection.getOutputStream());
104
105
106 out.writeBytes( "AdqlXml="+URLEncoder.encode(query.getAdqlString(), "UTF-8")+
107 "&Format="+URLEncoder.encode("VOTABLE", "UTF-8")
108 );
109
110 if (!(query.getTarget() instanceof UrlSourceTarget)) {
111 proxyResults = true;
112 }
113
114 if (proxyResults) {
115
116 out.writeBytes("&TargetResponse="+URLEncoder.encode("true", "UTF-8"));
117 }
118 else {
119 out.writeBytes("&TargetURI="+URLEncoder.encode( ((UrlSourceTarget) query.getTarget()).toURI().toString(), "UTF-8"));
120 }
121 out.flush();
122 out.close();
123
124
125 querier.getStatus().addDetail("Submitted to target at "+new Date());
126
127 InputStream in = connection.getInputStream();
128
129 if (proxyResults) {
130 VotableInResults vot = new VotableInResults(querier, in);
131 vot.send(query.getResultsDef(), querier.getUser());
132 }
133
134 }
135
136 /*** Returns just the number of matches rather than the list of matches */
137 public long getCount(Principal user, Query query, Querier querier) throws IOException {
138 throw new UnsupportedOperationException("Not done yet");
139 }
140
141 /***
142 * Returns the VOResource elements of the remote service.
143 * @deprecated - use UrlResourcePlugin to get the rdbms resource, and CeaResourceServer to
144 * build the righr CEA stuff for *this* service
145 */
146 public String getVoResource() throws IOException {
147 URL targetPal = new URL(ConfigFactory.getCommonConfig().getUrl(PAL_TARGET)+"/GetMetadata");
148 log.info("Proxying VoResources to: "+targetPal);
149
150 ProxyResourceSupport proxyer = new ProxyResourceSupport();
151 return proxyer.makeLocal(targetPal.openStream());
152 }
153
154 /*** Returns the formats that this plugin can provide. Asks the results class; override in subclasse if nec */
155 public String[] getFormats() {
156 return VotableInResults.listFormats();
157 }
158
159
160 }
161
162
163
164