View Javadoc

1   /*$Id: AsuQuerierPlugin.java,v 1.2 2005/03/21 18:45:55 mch Exp $
2    * Created on 13-Nov-2003
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid
7    * Software License version 1.2, a copy of which has been included
8    * with this distribution in the LICENSE.txt file.
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     //note bug on Vizier that -mime doesn't seem to work at "http://vizier.u-strasbg.fr/cgi-bin/VizieR" - just returns html
47     //every time.  Sebastian says use the appropriate script
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     /* Runs Query on an ASU server
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  //    URLConnection connection = new URL(url).openConnection();
73        InputStream in = new URL(url).openStream();
74        //create quick little stream listener to update status as the stream runs
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        //try each of the vizier services until one works
88        QueryResults results = null;
89        int site=0;
90        while ((results == null) && (site<mirrorUrls.length) && (!aborted)) {
91           try {
92  //            results = useSoap(mirrorUrls[site], querier, target, radius, unit, text, wavelength);
93              results = useHttp(mirrorUrls[site], querier, target, radius, unit, text, wavelength);
94           }
95           catch (IOException ioe) {
96              querier.getStatus().addDetail(ioe+" accessing "+mirrorUrls[site]);
97           }
98           site++;
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 //    URLConnection connection = new URL(url).openConnection();
128       DataInputStream in = new DataInputStream(new URL(url).openStream());
129       
130       /*
131       //try each of the vizier services until one works
132       QueryResults results = null;
133       int site=0;
134       while ((results == null) && (site<mirrorUrls.length) && (!aborted)) {
135          try {
136 //            results = useSoap(mirrorUrls[site], querier, target, radius, unit, text, wavelength);
137             results = useHttp(mirrorUrls[site], querier, target, radius, unit, text, wavelength);
138          }
139          catch (IOException ioe) {
140             querier.getStatus().addDetail(ioe+" accessing "+mirrorUrls[site]);
141          }
142          site++;
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) )",//OR ELAIS.COOKED LIKE 'TRUE')",
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  $Log: AsuQuerierPlugin.java,v $
199  Revision 1.2  2005/03/21 18:45:55  mch
200  Naughty big lump of changes
201 
202  Revision 1.1.1.1  2005/02/17 18:37:34  mch
203  Initial checkin
204 
205  Revision 1.1.1.1  2005/02/16 17:11:24  mch
206  Initial checkin
207 
208  Revision 1.1.2.4  2005/01/13 18:57:31  mch
209  Fixes to metadata mostly
210 
211  Revision 1.1.2.3  2004/12/10 12:37:13  mch
212  Cone searches to look in metadata, lots of metadata interpreterrs
213 
214  */
215 
216 
217 
218 
219