View Javadoc

1   /*
2    * $Id: ProxyResourceSupport.java,v 1.4 2005/03/21 18:45:55 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
5    */
6   
7   package org.astrogrid.dataservice.metadata.v0_10;
8   
9   import java.io.FileNotFoundException;
10  import java.io.IOException;
11  import java.io.InputStream;
12  import java.net.URLConnection;
13  import java.util.Date;
14  import javax.xml.parsers.ParserConfigurationException;
15  import org.astrogrid.cfg.ConfigFactory;
16  import org.astrogrid.dataservice.metadata.MetadataException;
17  import org.astrogrid.dataservice.metadata.v0_10.VoResourceSupport;
18  import org.astrogrid.xml.DomHelper;
19  import org.w3c.dom.Document;
20  import org.w3c.dom.Element;
21  import org.w3c.dom.NodeList;
22  import org.xml.sax.SAXException;
23  
24  /***
25   * Used to serve metadata generated from disk, or a URL, or some other service.
26   * Provides methods for ensuring identifiers etc are correct for *this* service.
27   * NB Expect to extend from this to localise special values, such as the CEA's
28   * application <-> service links.
29   *
30   * <p>
31   * @author M Hill
32   */
33  
34  public class ProxyResourceSupport extends VoResourceSupport {
35     
36     /*** As makeLocal(Document) but takes stream to parse from
37      */
38     public String makeLocal(InputStream in) throws IOException {
39        try {
40           Document doc = DomHelper.newDocument(in);
41           return makeLocal(doc);
42        }
43        catch (SAXException e) {
44           log.error("Invalid metadata",e);
45           throw new MetadataException("Server not configured properly - invalid metadata: "+e,e);
46        }
47        finally  {
48           try  {
49              in.close();
50           }
51           catch (IOException e)  {
52              // not bothered
53           }
54        }
55     }
56     
57     /*** Takes the given resource document and sets IDs etc to reflect *this* service.  The
58      * given doc may contain a list of <Resource>
59      * elements or a Description element.  Returns the transformed document.
60      */
61     public String makeLocal(Document doc) throws IOException {
62        
63        String rootElementName = doc.getDocumentElement().getNodeName();
64        
65        //if the root element is a resource, return that
66        if (rootElementName.equals("Resource"))  {
67           checkResource( doc.getDocumentElement());
68           return DomHelper.ElementToString(doc.getDocumentElement()) ;
69        }
70        //if the root element is a vodescription, return all resource elements
71        else if (rootElementName.equals("VODescription"))  {
72           StringBuffer localRes = new StringBuffer();
73           NodeList voResources = doc.getElementsByTagName("Resource");
74           if (voResources.getLength()==0)  {
75              throw new MetadataException("No <Resource> elements in document ");
76           }
77           
78           for (int i = 0; i < voResources.getLength(); i++)  {
79              Element resource = (Element) voResources.item(i);
80              
81              checkResource(resource);
82              
83              localRes.append(DomHelper.ElementToString(resource));
84           }
85           return localRes.toString();
86        }
87        else  {
88           throw new MetadataException("No 'Resource' or 'VODescription' element in metadata");
89        }
90     }
91     
92     
93     /*** Checks that a resource is OK for this datacenter, and sets appropriately if not */
94     public void checkResource(Element remoteResource) throws IOException  {
95  
96        /* The remoteResource DOM will be modified using values from the locally
97         * generated VoResource core metadata */
98        remoteResource.setAttribute("status", "active");
99        remoteResource.setAttribute("updated", toRegistryForm(new Date()));
100 
101       try {
102          //make local core and parse
103          Element localCore = DomHelper.newDocument("<"+VORESOURCE_ELEMENT+">"+makeCore("")+"</"+VORESOURCE_ELEMENT+">").getDocumentElement();
104          
105          //set id to local 'core' id + last /-seperated token of the remote id
106          Element remoteIdNode = DomHelper.getSingleChildByTagName(remoteResource, "identifier");
107          if (remoteIdNode == null) {
108             throw new MetadataException("No <identifier> element");
109          }
110          String localStem = DomHelper.getValueOf(localCore, "identifier");
111          String remoteId = DomHelper.getValueOf(remoteIdNode);
112          String newId = localStem;
113          int i = remoteId.lastIndexOf("/");
114          if (i>-1) newId = newId + remoteId.substring(i);
115          DomHelper.setElementValue(remoteIdNode, newId);
116          
117          //set reference url
118          Element contentNode = DomHelper.getSingleChildByTagName(remoteResource, "content");
119          if (contentNode != null) {
120             Element refUrlNode = DomHelper.getSingleChildByTagName(contentNode, "referenceURL");
121             String localRefUrl = DomHelper.getValueOf(DomHelper.getSingleChildByTagName(localCore, "content"), "referenceURL");
122             DomHelper.setElementValue(refUrlNode, localRefUrl);
123          }
124          
125          
126       }
127       catch (SAXException e) {
128          throw new MetadataException(e+" in Core Generated VoResource: "+makeCore(""),e);
129       }
130    }
131    
132 
133    
134    
135 }
136 
137