1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.dataservice.impl.cds;
12
13 import java.io.DataInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.StringWriter;
17 import java.net.URL;
18 import java.security.Principal;
19 import javax.xml.parsers.ParserConfigurationException;
20 import org.astrogrid.account.LoginAccount;
21 import org.astrogrid.cfg.ConfigFactory;
22 import org.astrogrid.dataservice.queriers.DefaultPlugin;
23 import org.astrogrid.dataservice.queriers.Querier;
24 import org.astrogrid.dataservice.queriers.QuerierPluginFactory;
25 import org.astrogrid.dataservice.queriers.RawPipeResults;
26 import org.astrogrid.dataservice.queriers.status.QuerierQuerying;
27 import org.astrogrid.io.ProgressMonitorInputStream;
28 import org.astrogrid.io.StreamProgressListener;
29 import org.astrogrid.query.Query;
30 import org.astrogrid.query.QueryException;
31 import org.astrogrid.query.SimpleQueryMaker;
32 import org.astrogrid.query.sql.SqlParser;
33 import org.astrogrid.slinger.mime.MimeTypes;
34 import org.astrogrid.slinger.targets.TargetMaker;
35 import org.xml.sax.SAXException;
36
37 /*** Datacenter querier SPI that performs queries against ASU-compatible services -
38 * which might only be CDS's Vizier data at the moment.
39 * <p>
40 * @see http://vizier.u-strasbg.fr/doc/asu.html
41 * <p>
42 * @author M Hill
43 */
44 public class AsuQuerierPlugin extends DefaultPlugin
45 {
46
47
48 public final static String DEFAULT_ASU_URL = "http://vizier.u-strasbg.fr/cgi-bin/votable";
49
50
51 /*** default constructor */
52 public AsuQuerierPlugin() throws IOException {
53 super();
54 }
55
56
57
58 public void askQuery(Principal user, Query query, final Querier querier) throws IOException {
59
60 String stemurl = ConfigFactory.getCommonConfig().getString("datacenter.asuplugin.dataurl", DEFAULT_ASU_URL);
61
62 AsuTwigMaker twigmaker = new AsuTwigMaker();
63 query.acceptVisitor(twigmaker);
64
65 String url = stemurl+twigmaker.getAsuTwig();
66
67 querier.setStatus(new QuerierQuerying(querier.getStatus(), url));
68 querier.getStatus().addDetail("Calling vizier via Url "+url+"...");
69
70 log.info("Querying using "+url);
71
72
73 InputStream in = new URL(url).openStream();
74
75 StreamProgressListener listener = new StreamProgressListener() {
76 public void setStreamProgress(long bytesRead) {
77 querier.getStatus().setProgress(bytesRead);
78 }
79 public void setStreamClosed() { }
80 };
81
82 in = new ProgressMonitorInputStream(in, 1000, listener);
83
84 RawPipeResults results = new RawPipeResults(querier, in, query.getResultsDef().getFormat());
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 if (aborted) return;
102
103 if (results != null) {
104 results.send(query.getResultsDef(), user);
105 } else {
106 throw new IOException("No results from any of the Vizier services (see query status for details)\n");
107 }
108
109 }
110
111
112 /*** Returns just the number of matches rather than the list of matches. Since there's no way to do this yet
113 * directly with Vizier (I don't think?), we just do a normal query then count the rows */
114 public long getCount(Principal user, Query query, Querier querier) throws IOException {
115 String stemurl = ConfigFactory.getCommonConfig().getString("datacenter.asuplugin.dataurl", DEFAULT_ASU_URL);
116
117 AsuTwigMaker twigmaker = new AsuCountTwigMaker();
118 query.acceptVisitor(twigmaker);
119
120 String url = stemurl+twigmaker.getAsuTwig();
121
122 querier.setStatus(new QuerierQuerying(querier.getStatus(), url));
123 querier.getStatus().addDetail("Calling vizier via Url "+url+"...");
124
125 log.info("Querying using "+url);
126
127
128 DataInputStream in = new DataInputStream(new URL(url).openStream());
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 return in.readLong();
146 }
147
148 /*** Returns the formats that this plugin can provide. Asks the results class; override in subclasse if nec */
149 public String[] getFormats() {
150 return new String[] { MimeTypes.HTML, MimeTypes.FITS, MimeTypes.VOTABLE, MimeTypes.TSV };
151 }
152
153
154
155
156 /*** Quick & dirty tester
157 *
158 */
159 public static void main(String[] args) throws IOException, QueryException, ParserConfigurationException, IOException, SAXException
160 {
161 ConfigFactory.getCommonConfig().setProperty(QuerierPluginFactory.QUERIER_PLUGIN_KEY,"org.astrogrid.datacenter.impl.cds.AsuQuerierPlugin");
162
163 {
164 StringWriter sw = new StringWriter();
165 Query query = SqlParser.makeQuery(
166 "SELECT * FROM ELAIS WHERE CIRCLE('J2000',7.5,-42,1)",
167 TargetMaker.makeTarget(sw),
168 "VOTABLE");
169 Querier querier = Querier.makeQuerier(LoginAccount.ANONYMOUS, query, "dirtytest");
170 querier.ask();
171 System.out.println(sw.toString());
172 }
173
174 {
175 StringWriter sw = new StringWriter();
176 Query query = SimpleQueryMaker.makeConeQuery(20,30,6, TargetMaker.makeTarget(sw));
177 Querier querier = Querier.makeQuerier(LoginAccount.ANONYMOUS, query, "dirtytest");
178 querier.ask();
179 System.out.println(sw.toString());
180 }
181
182 {
183 StringWriter sw = new StringWriter();
184 Query query = SqlParser.makeQuery(
185 "SELECT ELAIS.Jmag, ELAIS.Kmag FROM ELAIS WHERE CIRCLE('J2000',7.5,-42,1) AND ((ELAIS.Jmag >= 5 OR ELAIS.Kmag > 5) )",
186 TargetMaker.makeTarget(sw),
187 "VOTABLE");
188 Querier querier = Querier.makeQuerier(LoginAccount.ANONYMOUS, query, "dirtytest");
189 querier.ask();
190
191 System.out.println(sw.toString());
192 }
193 }
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219