View Javadoc

1   /*
2    * $Id: ExistPlugin.java,v 1.2 2005/03/21 18:45:55 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
5    */
6   
7   package org.astrogrid.xdbserver.xql;
8   
9   import java.io.DataOutputStream;
10  import java.io.IOException;
11  import java.io.InputStream;
12  import java.net.HttpURLConnection;
13  import java.net.URL;
14  import java.security.Principal;
15  import javax.xml.parsers.ParserConfigurationException;
16  import org.astrogrid.cfg.ConfigFactory;
17  import org.astrogrid.dataservice.queriers.DefaultPlugin;
18  import org.astrogrid.dataservice.queriers.Querier;
19  import org.astrogrid.dataservice.queriers.status.QuerierQuerying;
20  import org.astrogrid.query.Query;
21  import org.astrogrid.query.QueryException;
22  import org.astrogrid.query.xql.XqlMaker;
23  import org.astrogrid.xml.DomHelper;
24  import org.xml.sax.SAXException;
25  
26  /***
27   * Plugin to use an eXist database that is colocated with this PAL (ie, as another
28   * webapp on the same machine)
29   *
30   * @author Kevin B, Martin H
31   */
32  
33  public class ExistPlugin extends DefaultPlugin {
34     
35     /*** Plugin implementation - carries out query.
36      */
37     public void askQuery(Principal user, Query query, Querier querier) throws IOException {
38  
39        querier.setStatus(new QuerierQuerying(querier.getStatus(), query.toString()));
40  
41        InputStream eXistIn = askExist(query);
42        
43        if (!aborted) {
44           XmlResults results = new XmlResults(querier, eXistIn);
45           results.send(query.getResultsDef(), querier.getUser());
46        }
47     }
48     
49     /*** Returns just the number of matches rather than the list of matches */
50     public long getCount(Principal user, Query query, Querier querier) throws IOException {
51        throw new UnsupportedOperationException("To do");
52     }
53     
54  
55     /*** Returns the limit configured for this application */
56     public long getLocalLimit() {
57        return ConfigFactory.getCommonConfig().getInt("datacenter.results.limit",300);
58     }
59     
60     /*** queries eXist with the given query */
61     public InputStream askExist(Query query) throws IOException {
62        XqlMaker maker = new XqlMaker();
63        String xql = maker.getXql(query);
64        long limit = query.getLimit();
65        if ((limit <=0) ||
66               ((limit > getLocalLimit()) && (getLocalLimit() >0))) {
67           limit = getLocalLimit();
68        }
69        
70        return askExist(xql, limit);
71     }
72     
73     /*** queries exist with the given Xql and max limit number of returned values,
74      * returning a stream ot the results */
75     public InputStream askExist(String xql, long limit) throws IOException {
76  
77           String query = "<query xmlns='http://exist.sourceforge.net/NS/exist'" +
78              " start='1' max='" + limit + "\">" +
79              "<text><![CDATA[" + xql + "]]></text></query>";
80  
81           try {
82              DomHelper.newDocument(query); //check that it's valid
83           }
84           catch(SAXException se) {
85              throw new QueryException(se+" in generated XQL (Internal Server Error)",se);
86           }
87           URL postUrl = new URL(ConfigFactory.getCommonConfig().getUrl("exist.db.url")+
88                                    "/servlet/db/");
89           
90           HttpURLConnection huc = (HttpURLConnection) postUrl.openConnection();
91           huc.setRequestProperty("Content-Type", "text/xml");
92           huc.setDoOutput(true);
93           huc.setDoInput(true);
94           huc.connect();
95           //write query
96           DataOutputStream dos = new DataOutputStream(huc.getOutputStream());
97           dos.writeChars(query);
98           dos.flush();
99           dos.close();
100          //read results
101          if (!aborted) {
102             return huc.getInputStream();
103          }
104          else {
105             return null;
106          }
107       
108       
109    }
110 
111    
112  
113    /*** Returns the formats that this plugin can provide.  Asks the results class; override in subclasse if nec */
114    public String[] getFormats() {
115       return XmlResults.listFormats();
116    }
117    
118    
119  
120 }
121 
122 /*
123  $Log: ExistPlugin.java,v $
124  Revision 1.2  2005/03/21 18:45:55  mch
125  Naughty big lump of changes
126 
127  Revision 1.1  2005/03/10 16:42:55  mch
128  Split fits, sql and xdb
129 
130  Revision 1.1.1.1  2005/02/17 18:37:35  mch
131  Initial checkin
132 
133  Revision 1.1.1.1  2005/02/16 17:11:24  mch
134  Initial checkin
135 
136  Revision 1.5.2.4  2004/12/08 18:36:40  mch
137  Added Vizier, rationalised SqlWriters etc, separated out TableResults from QueryResults
138 
139  Revision 1.5.2.3  2004/11/24 20:59:37  mch
140  doc fixes and added slinger browser
141 
142  Revision 1.5.2.2  2004/11/22 00:57:16  mch
143  New interfaces for SIAP etc and new slinger package
144 
145  Revision 1.5.2.1  2004/11/17 11:15:46  mch
146  Changes for serving images
147 
148  Revision 1.5  2004/11/12 13:49:12  mch
149  Fix where keyword maker might not have had keywords made
150 
151  Revision 1.4  2004/11/11 23:23:29  mch
152  Prepared framework for SSAP and SIAP
153 
154  Revision 1.3  2004/11/03 00:17:56  mch
155  PAL_MCH Candidate 2 merge
156 
157  Revision 1.2.6.1  2004/10/27 00:43:40  mch
158  Started adding getCount, some resource fixes, some jsps
159 
160  Revision 1.2  2004/10/18 13:11:30  mch
161  Lumpy Merge
162 
163  Revision 1.1.2.1  2004/10/15 19:59:06  mch
164  Lots of changes during trip to CDS to improve int test pass rate
165 
166 
167  */
168 
169 
170 
171 
172