View Javadoc

1   /*
2    * $Id: NvoConePlugin.java,v 1.2 2005/03/21 18:45:55 mch Exp $
3    *
4    * (C) Copyright AstroGrid...
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        //add query stuff - there might already be query stuff in the base url
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       //start query & pick up handle to results
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 $Log: NvoConePlugin.java,v $
130 Revision 1.2  2005/03/21 18:45:55  mch
131 Naughty big lump of changes
132 
133 Revision 1.1.1.1  2005/02/17 18:37:35  mch
134 Initial checkin
135 
136 Revision 1.1.1.1  2005/02/16 17:11:24  mch
137 Initial checkin
138 
139 Revision 1.7.2.4  2004/12/08 18:36:40  mch
140 Added Vizier, rationalised SqlWriters etc, separated out TableResults from QueryResults
141 
142 Revision 1.7.2.3  2004/11/25 00:30:56  mch
143 that fiddly sky package
144 
145 Revision 1.7.2.2  2004/11/24 20:59:37  mch
146 doc fixes and added slinger browser
147 
148 Revision 1.7.2.1  2004/11/22 00:57:16  mch
149 New interfaces for SIAP etc and new slinger package
150 
151 Revision 1.7  2004/11/12 13:49:12  mch
152 Fix where keyword maker might not have had keywords made
153 
154 Revision 1.6  2004/11/12 10:44:54  mch
155 More resources, siap stuff, ssap stuff, SSS
156 
157 Revision 1.5  2004/11/11 23:23:29  mch
158 Prepared framework for SSAP and SIAP
159 
160 Revision 1.4  2004/11/03 00:17:56  mch
161 PAL_MCH Candidate 2 merge
162 
163 Revision 1.3.6.1  2004/10/27 00:43:39  mch
164 Started adding getCount, some resource fixes, some jsps
165 
166 Revision 1.3  2004/10/18 13:11:30  mch
167 Lumpy Merge
168 
169 Revision 1.2.2.1  2004/10/15 19:59:06  mch
170 Lots of changes during trip to CDS to improve int test pass rate
171 
172 Revision 1.2  2004/10/06 21:12:17  mch
173 Big Lump of changes to pass Query OM around instead of Query subclasses, and TargetIndicator mixed into Slinger
174 
175 Revision 1.1  2004/09/28 15:02:13  mch
176 Merged PAL and server packages
177 
178 Revision 1.2  2004/08/02 11:34:33  mch
179 Completed the askQuery
180 
181 
182 */
183 
184 
185