1
2
3
4
5
6
7 package org.astrogrid.dataservice.queriers.nvocone;
8
9 import java.io.IOException;
10 import java.net.URL;
11 import java.security.Principal;
12 import java.util.Enumeration;
13 import java.util.Hashtable;
14 import java.util.Vector;
15 import org.astrogrid.dataservice.queriers.DefaultPlugin;
16 import org.astrogrid.dataservice.queriers.Querier;
17 import org.astrogrid.dataservice.queriers.VotableInResults;
18 import org.astrogrid.dataservice.queriers.status.QuerierQuerying;
19 import org.astrogrid.dataservice.DatacenterException;
20 import org.astrogrid.query.Query;
21 import org.astrogrid.query.keyword.KeywordMaker;
22 import org.astrogrid.geom.Angle;
23
24 /***
25 * The National Virtual Observatory, an American effort, defined a simple
26 * cone search service:
27 * @see http://www.us-vo.org/metadata/conesearch/
28 * <p>
29 * This plugin gives us the capability to query such datacenters by mapping table
30 * names to the URLs.
31 * <p>
32 * Cunning plan - there is no real need for this plugin to connect to the URL
33 * unless it needs to stream the results back to the front end. The URL can
34 * just be given to the StoreClient to connect to. No status info though...
35 *
36 * @author M Hill
37 */
38
39 public class NvoConePlugin extends DefaultPlugin
40 {
41
42 /*** index of table names and the corresponding cone search */
43 private static Hashtable tableUrls = null;
44
45 /*** populates the tableUrls with virtual table names and corresponding URLs.
46 * At the moment this is hardwired, but should be loaded from config or even
47 * better got from the registry. @see http://nvo.stsci.edu/voregistry/QueryRegistry.aspx */
48 public static synchronized void initialise() {
49 if (tableUrls != null) {
50 tableUrls.put("VIRTUALSKY_MESSIER", "http://virtualsky.org/servlet/cover?CAT=messier");
51 tableUrls.put("COPERNICUS", "http://archive.stsci.edu/copernicus/search.php?");
52 tableUrls.put("HIPPARCOS", "http://chart.stsci.edu/GSCVO/HIPVO.jsp?");
53 }
54 }
55
56 /*** returns the list of virtual tables that correspond to the services */
57 public static String[] getTables() {
58 if (tableUrls == null) initialise();
59 Vector tables = new Vector();
60 Enumeration keys = tableUrls.keys();
61 while (keys.hasMoreElements()) {
62 Object key = keys.nextElement();
63 tables.add(tableUrls.get(key));
64 }
65 return (String[]) tables.toArray(new String[] {} );
66 }
67
68 /***
69 * Sends the query to the nvo cone search. NB this routes the results through
70 * this server, which is not necesssarily the best thing. Ideally the URL should
71 * be passed to the storepoint to upload directly.
72 */
73 public void askQuery(Principal user, Query query, Querier querier) throws IOException {
74
75 querier.setStatus(new QuerierQuerying(querier.getStatus(), query.toString()));
76
77 if (tableUrls == null) initialise();
78
79 KeywordMaker maker = new KeywordMaker(query);
80
81 Angle ra = Angle.parseAngle(maker.getRequiredValue(maker.RA_KEYWORD).toString());
82 Angle dec = Angle.parseAngle(maker.getRequiredValue(maker.DEC_KEYWORD).toString());
83 Angle radius = Angle.parseAngle(maker.getRequiredValue(maker.RADIUS_KEYWORD).toString());
84
85 String[] tables = query.getScope();
86 if (tables.length>1) {
87 throw new DatacenterException("Datacenters can only proxy to one NVO cone search at a time; just give one table in the FROM");
88 }
89 if (tables.length==0) {
90 throw new DatacenterException("No table given in scope; check the metadata for valid tables");
91 }
92
93 String queryUrl = tableUrls.get(tables[0]).toString();
94 if (queryUrl == null) {
95 throw new DatacenterException("Table "+tables[0]+" is unknown - check the metadata for valid tables");
96 }
97
98
99 if (queryUrl.indexOf("?")>-1) {
100 queryUrl = queryUrl + "&";
101 }
102 else {
103 queryUrl = queryUrl + "?";
104 }
105
106 URL url = new URL(queryUrl+"RA="+ra.asDegrees()+"&DEC="+dec.asDegrees()+"&SR="+radius.asDegrees());
107
108
109 VotableInResults results = new VotableInResults(querier, url.openStream());
110
111 if (!aborted) {
112 results.send(query.getResultsDef(), querier.getUser());
113 }
114 }
115
116 /*** Returns just the number of matches rather than the list of matches */
117 public long getCount(Principal user, Query query, Querier querier) throws IOException {
118 return getCountFromResults(user, query, querier);
119 }
120
121 /*** Returns the formats that this plugin can provide. Asks the results class; override in subclasse if nec */
122 public String[] getFormats() {
123 return VotableInResults.listFormats();
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
173
174
175
176
177
178
179
180
181
182
183
184
185