View Javadoc

1   //-------------------------------------------------------------------------
2   // FILE: AceService.java
3   // PACKAGE: org.astrogrid.ace.webservice.axis
4   //
5   // DATE       AUTHOR    NOTES
6   // ----       ------    -----
7   // 11/11/02   KEA       Initial prototype
8   //
9   //
10  // NOTE: This class is developed for use with Apache Axis 1.0.
11  //-------------------------------------------------------------------------
12  
13  package org.astrogrid.ace.webservice.axis;
14  
15  import javax.xml.parsers.DocumentBuilderFactory;
16  import javax.xml.parsers.DocumentBuilder;
17  import javax.xml.soap.SOAPMessage;
18  
19  import org.w3c.dom.Document;
20  import org.w3c.dom.DocumentType;
21  import org.w3c.dom.Element;
22  import org.w3c.dom.NodeList;
23  import org.w3c.dom.Node;
24  import org.w3c.dom.CDATASection;
25  import org.w3c.dom.Text;
26  import org.w3c.dom.Attr;
27  import org.w3c.dom.NamedNodeMap;
28  
29  import org.apache.axis.AxisFault;
30  import org.apache.axis.utils.XMLUtils;
31  import org.apache.axis.message.SOAPBodyElement;
32  
33  import org.astrogrid.ace.web.Ace;
34  import org.astrogrid.ace.service.AceContext;
35  //import org.astrogrid.ace.AceParameterBundle;
36  
37  //import org.astrogrid.service.ParameterExtractor;
38  
39  import org.astrogrid.xmlutils.XmlValidatorIfc;
40  import org.astrogrid.xmlutils.XmlValidatorXercesImpl;
41  
42  // FOR LOGGING
43  import org.astrogrid.log.Log;
44  
45  // MISC
46  import java.io.StringReader;
47  import java.io.IOException;
48  import java.util.Vector;
49  
50  
51  // FOR PRINT AND DEBUG FUNC ONLY - REMOVE LATER
52  //import org.astrogrid.ace.webservice.axis.ParameterBundle;
53  import java.io.BufferedWriter;
54  import java.io.FileWriter;
55  
56  /***
57   * <p>???
58   *
59   * <p>???
60   *
61   * <p>See ???.java for a ???
62   *
63   * <p>TO DO:  <ul>
64   * <li>Nicer exception-throwing and handling?</li>
65   * </ul>
66   *
67   *
68   * @see org.astrogrid.ace.webservice.axis.???
69   * @see AceService_deploy.wsdd
70   * @see AceService_undeploy.wsdd
71   *
72   * @author Kona Andrews,
73   * <a href="mailto:kea@ast.cam.ac.uk">kea@ast.cam.ac.uk</a>
74   * @version 1.0
75  */
76  
77  
78  public class AceService
79  {
80     /***
81      * Location of logfile - each instance should have a separate logfile -
82        fix this when policy is decided re instance working directories etc.
83      */
84     //static final String LOGFILE =
85     //    "/data/cass123a/kea/AvoDemo/WORKING/serverlog.txt";
86  
87     // Dump log info unless needed for debugging - otherwise log files get huge
88     static final String LOGFILE = "/dev/null";
89  
90  
91     /***
92      * Dummy constructor - does nothing.
93      */
94     public AceService()
95     {
96     }
97  
98     /***
99      * This function is the Ace web-service's public interface.
100     * It accepts a DOM Element representing the root of an XML
101     * document n
102     *
103     * @param xmlIn  A (pre-initialised) reader for the input XML
104     *
105     * @return  A Document containing the DOM tree for the input XML.
106     */
107 
108    public Element[] processParams(Element[] soapBodyElements) throws AxisFault
109    {
110       try
111       {
112          net.mchill.log.Log.addHandler(new net.mchill.log.Log2File(LOGFILE));
113 
114          //Element soapBody = (Element) soapBodyElements.get(0);
115          Element soapBody = soapBodyElements[0];
116 
117          Log.trace("Webservice: Validating XML input against its schema");
118 
119          XmlValidatorXercesImpl validator = new XmlValidatorXercesImpl();
120          StringReader reader =
121                new StringReader(XMLUtils.ElementToString(soapBody));
122 
123          // Throws exception if XML doesn't validate against schema
124          try
125          {
126             validator.validate(reader);
127          }
128          catch (Exception e)
129          {
130             AxisFault fault = AxisFault.makeFault(e);
131             fault.setFaultCode("Client.InvalidXMLDocument");
132             throw fault;
133          }
134 
135          // INPUT XML OK - NO VALIDATION ERROR AGAINST SCHEMA
136 
137          // Make the SExtractor call
138          //
139          Log.trace("Webservice: Doing Ace wrapper call");
140          Element result = doSExtractorCall(soapBody);
141          Log.trace("Webservice: Done SExtractor call");
142 
143          return makeVOTableReturn(result);
144          //return makeStatusReturn(result);
145 
146       }
147       // CATCH ANY OTHER EXCEPTIONS AND MAKE A PROPER AXIS FAULT FROM THEM
148       catch (Exception e)
149       {
150          throw AxisFault.makeFault(e);
151       }
152    }
153 
154 
155    protected Element[] makeVOTableReturn(Element result) throws Exception
156    {
157          // Build output doc for return SOAP msg
158          //
159          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
160          factory.setNamespaceAware(false);
161          DocumentBuilder builder = factory.newDocumentBuilder();
162 
163          Document responseDoc = builder.newDocument();
164 
165          Element[] wsResult = new Element[1];
166          wsResult[0] = result;
167          return wsResult;
168    }
169 
170    protected Element[] makeStatusReturn(Element result) throws Exception
171    {
172          // Build output doc for return SOAP msg
173          //
174          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
175          factory.setNamespaceAware(true);
176          DocumentBuilder builder = factory.newDocumentBuilder();
177 
178          Document responseDoc = builder.newDocument();
179 
180          Element resRoot = responseDoc.createElementNS(
181                "http://www.astrogrid.org/namespace/SimpleReturnDoc",
182                "ReturnString");
183 
184          String status = "";
185          if (result != null)
186          {
187             status = "SExtractor run complete - results in sexResults.xml";
188          }
189          else
190          {
191             status = "SExtractor run FAILED - null results node received";
192          }
193          // PUT OUR OUTPUT INTO THE DOCUMENT
194          Text eltText = responseDoc.createTextNode(status);
195          resRoot.appendChild(eltText);
196 
197          Element[] wsResult = new Element[1];
198          wsResult[0] = resRoot;
199          return wsResult;
200    }
201 
202 
203    protected Element doSExtractorCall(Element soapBody) throws IOException
204    {
205       Ace ace = new Ace();
206       return ace.runApplication(soapBody);
207    }
208 
209 /* DEBUG HARNESS ONLY - NOT NEEDED NOW
210    protected String getBodyString(Element soapBody) throws Exception
211    {
212       //SExConfigBundle bundle = new SExConfigBundle();
213       ParameterBundle bundle = new ParameterBundle();
214       String outString = "\n";
215 
216       NodeList nodeList = soapBody.getChildNodes();
217 
218       for (int i = 0; i < nodeList.getLength(); i++) {
219          if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
220 
221             Element paramNode = (Element)(nodeList.item(i));
222 
223             // SEE IF THIS IS A MULTI-ARGUMENT PARAMETER
224             NodeList args =
225                paramNode.getElementsByTagName("arg");
226 
227             if (args.getLength() == 0) {
228                throw new Exception("Error - missing <arg> for parameter "
229                      + paramNode.getTagName());
230             }
231 
232             String tagString =
233                args.item(0).getFirstChild().getNodeValue();
234 
235             for (int j = 1; j < args.getLength(); j++) {
236                tagString =
237                   tagString + ", " +
238                   args.item(j).getFirstChild().getNodeValue();
239             }
240             //bundle.setDictionary(paramNode.getTagName(),tagString);
241             bundle.addParameter(paramNode.getTagName(),tagString);
242          }
243       }
244       //outString = outString + bundle.getStringOutput();
245 
246       String[] parameters = bundle.getParameters();
247       int maxLen = 0;
248       for (int i=0;i<parameters.length;i++) {
249          if (maxLen < parameters[i].length()) {
250             maxLen = parameters[i].length();
251          }
252       }
253 
254       for (int i=0;i<parameters.length;i++) {
255          int numSpaces = maxLen - parameters[i].length();
256          outString = outString + parameters[i] + "   ";
257          for (int j = 0; j < numSpaces; j++) {
258             outString = outString + " ";
259          }
260          // BOOLEANS ARE A SPECIAL CASE
261          String paramVal =
262             (bundle.getParameter(parameters[i])).trim();
263          if (paramVal.equalsIgnoreCase("true")) {
264             outString = outString + "Y" + '\n';
265          }
266          else if (paramVal.equalsIgnoreCase("false")) {
267             outString = outString + "N" + '\n';
268          }
269          else {
270             outString = outString +
271                bundle.getParameter(parameters[i]) + '\n';
272          }
273       }
274       return outString;
275    }
276    */
277 }
278 //-------------------------------------------------------------------------