View Javadoc

1   package org.astrogrid.registry.client.harvest;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   
6   
7   import java.net.URL; 
8   import java.util.Vector; 
9   import javax.xml.parsers.DocumentBuilder; 
10  import javax.xml.parsers.DocumentBuilderFactory; 
11  import javax.xml.parsers.ParserConfigurationException;
12  import javax.xml.rpc.ServiceException;
13  import java.rmi.RemoteException; 
14  import org.apache.axis.client.Call; 
15  import org.apache.axis.client.Service; 
16  import org.apache.axis.message.SOAPBodyElement; 
17  import org.apache.axis.utils.XMLUtils; 
18  import org.w3c.dom.Document; 
19  import org.w3c.dom.Node;
20  import org.w3c.dom.Element;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import java.text.ParseException;
24  import java.util.Calendar;
25  import java.util.Set;
26  import java.util.Iterator;
27  
28  /***
29   * 
30   * The RegistryService class is a delegate to a web service that submits an XML formatted
31   * registry query to the to the server side web service also named the same RegistryService.
32   * This delegate helps the user browse the registry.  Queries should be formatted according to
33   * the schema at IVOA schema version 0.9.  This class also uses the common RegistryInterface for
34   * knowing the web service methods to call on the server side.
35   * @todo replace null-returns with thrown exceptions, declared in interface.
36   * @see org.astrogrid.registry.common.RegistryInterface
37   * @link http://www.ivoa.net/twiki/bin/view/IVOA/IVOARegWp03
38   * @author Kevin Benson
39   * @deprecated Being deprecated, harvests should only be started automatically by the server or manually by 
40   * going through the server.
41   */
42  public class RegistryHarvestService {
43      /***
44       * Commons Logger for this class
45       */
46      private static final Log logger = LogFactory
47              .getLog(RegistryHarvestService.class); 
48   
49     /***
50      * target end point  is the location of the webservice. 
51      */
52     private URL endPoint = null;
53     
54     private static final String NAMESPACE_URI =  "http://harvest.server.registry.astrogrid.org";   
55     
56       
57  
58     /***
59      * Empty constructor that defaults the end point to local host.
60      * @author Kevin Benson
61      */
62     public RegistryHarvestService() {
63        this(null);
64     }
65      
66     /***
67      * Main constructor to allocate the endPoint variable.
68      * @todo fail on null being passed in - throw an illegalArgumentException.
69      * @param endPoint location to the web service.
70      * @author Kevin Benson
71      */     
72     public RegistryHarvestService(URL endPoint) {
73        this.endPoint = endPoint;
74     }
75      
76  
77     /***
78      * Method to establish a Service and a Call to the server side web service.
79      * @return Call object which has the necessary properties set for an Axis message style.
80      * @throws Exception
81      * @author Kevin Benson
82      */     
83     private Call getCall() {
84        Call _call = null;
85        try {
86           Service  service = new Service();
87           _call = (Call) service.createCall();
88           _call.setTargetEndpointAddress(endPoint);
89           _call.setSOAPActionURI("");
90           _call.setOperationStyle(org.apache.axis.enum.Style.MESSAGE);
91           _call.setOperationUse(org.apache.axis.enum.Use.LITERAL);        
92           _call.setEncodingStyle(null);
93        }catch(ServiceException se) {
94          logger.error("getCall()", se);
95           _call = null;            
96        }finally {
97            //@todo don't return a null on error - throw the exception instead.
98           return _call;   
99        }       
100    }
101    
102    public Document harvest(Document query) {
103       try {
104          //get a call object operation to the web service.
105          Call call = getCall();
106           
107          DocumentBuilder registryBuilder = null;
108          registryBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
109          Document doc = registryBuilder.newDocument();
110          Element root = doc.createElementNS(NAMESPACE_URI,"harvest");
111          doc.appendChild(root);
112          SOAPBodyElement sbeRequest = new SOAPBodyElement(doc.getDocumentElement());      
113          sbeRequest.setName("harvest");
114          sbeRequest.setNamespaceURI(NAMESPACE_URI);
115          Vector result = (Vector) call.invoke (new Object[] {sbeRequest});
116          SOAPBodyElement sbe = (SOAPBodyElement) result.get(0);
117          return sbe.getAsDocument();
118       }catch(ParserConfigurationException pce) {
119         logger.error("harvest(Document)", pce);   
120       }catch(RemoteException re) {
121         logger.error("harvest(Document)", re);   
122       }catch(Exception e) {
123         logger.error("harvest(Document)", e);            
124       }
125       //@todo same here - this idiom is wrong.
126       return null;
127    }
128    
129    public Document harvestFrom(Document query) {
130       /*
131        * see if it s a valid document.
132        */
133       try {
134          //get a call object operation to the web service.
135          Call call = getCall();
136           
137          DocumentBuilder registryBuilder = null;
138          registryBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
139          Document doc = registryBuilder.newDocument();
140          Element root = doc.createElementNS(NAMESPACE_URI,"harvestFrom");
141          doc.appendChild(root);
142          Node nd = doc.importNode(query.getDocumentElement(),true);
143          root.appendChild(nd);
144 
145          SOAPBodyElement sbeRequest = new SOAPBodyElement(doc.getDocumentElement());      
146          sbeRequest.setName("harvestFrom");
147          sbeRequest.setNamespaceURI(NAMESPACE_URI);
148          Vector result = (Vector) call.invoke (new Object[] {sbeRequest});
149          SOAPBodyElement sbe = (SOAPBodyElement) result.get(0);
150 
151         logger.info("harvestFrom(Document) - received "
152                 + XMLUtils.DocumentToString(sbe.getAsDocument()));
153          return sbe.getAsDocument();       
154          
155       }catch(ParserConfigurationException pce) {
156         logger.error("harvestFrom(Document)", pce);   
157       }catch(RemoteException re) {
158         logger.error("harvestFrom(Document)", re);   
159       }catch(Exception e) {
160         logger.error("harvestFrom(Document)", e);            
161       }
162       return null;  
163    }
164    
165    public void harvestAll(Document query) {
166       try {
167          //get a call object operation to the web service.
168          Call call = getCall();
169           
170          DocumentBuilder registryBuilder = null;
171          registryBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
172          Document doc = registryBuilder.newDocument();
173          Element root = doc.createElementNS(NAMESPACE_URI,"harvestAll");
174          doc.appendChild(root);
175          if(query != null) { 
176             Node nd = doc.importNode(query.getDocumentElement(),true);
177             root.appendChild(nd);
178          }
179 
180          SOAPBodyElement sbeRequest = new SOAPBodyElement(doc.getDocumentElement());      
181          sbeRequest.setName("harvestAll");
182          sbeRequest.setNamespaceURI(NAMESPACE_URI);
183          call.invokeOneWay(new Object[] {sbeRequest});
184 
185       }catch(ParserConfigurationException pce) {
186         logger.error("harvestAll(Document)", pce);   
187       }catch(Exception e) {
188         logger.error("harvestAll(Document)", e);            
189       }
190    }
191    
192    
193    
194    public void harvestResource(Document query) {
195       try {
196          //get a call object operation to the web service.
197         logger
198                 .info("harvestResource(Document) - okay doing Call and endpoint = "
199                         + endPoint.toString());
200          Call call = getCall();
201 
202          DocumentBuilder registryBuilder = null;
203          registryBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
204          Document doc = registryBuilder.newDocument();
205          Element root = doc.createElementNS(NAMESPACE_URI,"harvestResource");
206          doc.appendChild(root);
207          Node nd = doc.importNode(query.getDocumentElement(),true);
208          root.appendChild(nd);
209 
210          SOAPBodyElement sbeRequest = new SOAPBodyElement(doc.getDocumentElement());      
211          //SOAPBodyElement sbeRequest = new SOAPBodyElement(query.getDocumentElement());
212          sbeRequest.setName("harvestResource");
213          sbeRequest.setNamespaceURI(NAMESPACE_URI);
214         logger.info("harvestResource(Document) - Invoking this doc = "
215                 + XMLUtils.DocumentToString(query));
216          call.invokeOneWay (new Object[] {sbeRequest});
217       }catch(ParserConfigurationException pce) {
218         logger.error("harvestResource(Document)", pce);   
219       }catch(Exception e) {
220         logger.error("harvestResource(Document)", e);            
221       }
222    }
223    
224    public void harvestFromResource(Document query) {
225       try {
226          //get a call object operation to the web service.
227          Call call = getCall();
228           
229          DocumentBuilder registryBuilder = null;
230          registryBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
231          Document doc = registryBuilder.newDocument();
232          Element root = doc.createElementNS(NAMESPACE_URI,"harvestFromResource");
233          doc.appendChild(root);
234          Node nd = doc.importNode(query.getDocumentElement(),true);
235          root.appendChild(nd);
236 
237          SOAPBodyElement sbeRequest = new SOAPBodyElement(doc.getDocumentElement());      
238          sbeRequest.setName("harvestFromResource");
239          sbeRequest.setNamespaceURI(NAMESPACE_URI);
240          call.invokeOneWay (new Object[] {sbeRequest});
241       }catch(ParserConfigurationException pce) {
242         logger.error("harvestFromResource(Document)", pce);   
243       }catch(Exception e) {
244         logger.error("harvestFromResource(Document)", e);            
245       }
246    }
247 
248 }