source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/ModuleWrapper.java@ 29318

Last change on this file since 29318 was 16688, checked in by davidb, 16 years ago

Changed 'Element process(Element)' in ModuleInterface to 'Node process(Node)'. After some deliberation is was decided this is a more useful (generic) layer of the DOM to pass information around in. Helps with the DocType problem when producing XSL Transformed pages, for example. When this was an Element, it would loose track of its DocType. Supporting method provided in XMLConverter 'Element nodeToElement(Node)' which checks a nodes docType and casts to Element if appropriate, or if a Document, typecasts to that and then extracts the top-level Element. With this fundamental change in ModuleInterface, around 20 files needed to be updated (Actions, Services, etc) that build on top of 'process()' to reflect this change, and use nodeToElement where necessary.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/*
2 * ModuleWrapper.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.service;
20import org.greenstone.gsdl3.core.ModuleInterface;
21
22// XML classes
23import javax.xml.parsers.DocumentBuilderFactory;
24import javax.xml.parsers.DocumentBuilder;
25import javax.xml.parsers.SAXParser;
26import org.apache.xerces.dom.TextImpl;
27import org.apache.xerces.parsers.DOMParser;
28import org.w3c.dom.Document;
29import org.w3c.dom.Element;
30import org.w3c.dom.Node;
31import org.w3c.dom.Node;
32import org.xml.sax.InputSource;
33import org.xml.sax.SAXException;
34import org.xml.sax.XMLReader;
35
36// java classes
37import java.io.StringReader;
38import java.io.File;
39import java.io.FileWriter;
40import java.io.IOException;
41import java.security.SecureRandom;
42
43import org.apache.log4j.*;
44
45/**
46 * A classes for logging and checking all traffic to and from a ModuleInterface
47 *
48 * if logCounter hasn't been initialised, initialise it to a random
49 * value. This has to use secure random rather than a normal random
50 * or every time the application is run the same numbers will be
51 * generated and the search for a free range will become a linear
52 * search rather than a hash.
53 *
54 *
55 * @author <a href="mailto:[email protected]">Stuart Yeates</a>
56 * @version $Revision: 16688 $
57 */
58public class ModuleWrapper
59 implements ModuleInterface
60{
61
62 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service. ModuleWrapper.class.getName());
63
64 /** the module we're wrapping */
65 protected ModuleInterface inner = null;
66 /** the module we're wrapping */
67 public ModuleInterface getInner(){return inner;};
68 /** the module we're wrapping */
69 public void setInner(ModuleInterface i){inner=i;};
70
71 /** The schema of the IN XML */
72 protected String fromSchema = null;
73 /** The schema of the IN XML */
74 public String getFromSchema(){return fromSchema;};
75 /** The schema of the IN XML */
76 public void setFromSchema(String s){fromSchema=s;};
77
78 /** The schema of the OUT XML */
79 protected String toSchema = null;
80 /** The schema of the OUT XML */
81 public String getToSchema(){return toSchema;};
82 /** The schema of the OUT XML */
83 public void setToSchema(String s){toSchema=s;};
84
85 /** Are we logging the XML ? */
86 protected boolean logging = true;
87 /** Are we logging the XML ? */
88 public boolean getLogging(){return logging;};
89 /** Are we logging the XML ? */
90 public void setLogging(boolean b){logging=b;};
91
92 /** Where we are logging the XML */
93 protected String logDirectory = "/tmp/";
94 /** Where we are logging the XML */
95 public String getLogDirectory(){return logDirectory;};
96 /** Where we are logging the XML */
97 public void setLogDirectory(String s){logDirectory=s;};
98
99 /** The number of the previous log file */
100 protected static long logCounter = 0;
101
102 /** The no-args constructor */
103 public ModuleWrapper(){
104
105 }
106
107 public void cleanUp() {}
108
109 /** The all-args constructor */
110 public ModuleWrapper(String in,String out, ModuleInterface inner){
111 this.setFromSchema(in);
112 this.setToSchema(out);
113 this.setInner(inner);
114 }
115
116 /**
117 * Process an XML request - as a String
118 *
119 * @param xmlIn the request to process
120 * @return the response - contains any error messages
121 * @see java.lang.String
122 */
123 public String process(String xmlIn) {
124
125 long logNumber = 0;
126 String xmlOut = "";
127 if (getLogging()) {
128 if (logCounter == 0)
129 logCounter = new SecureRandom().nextLong();
130 try {
131 logNumber = logCounter++;
132 String filename = getLogDirectory() + File.separator + logNumber + ".in";
133 File file = new File(filename);
134
135 while (file.exists()){
136 logCounter = new SecureRandom().nextLong();
137 logNumber = logCounter++;
138 filename = getLogDirectory() + File.separator + logNumber + ".in";
139 file = new File(filename);
140 }
141
142 FileWriter writer = new FileWriter(file);
143 writer.write(xmlIn);
144 } catch (IOException e) {
145 logger.error("caught exception: " +e );
146 }
147 }
148 xmlOut = processInner(xmlIn);
149
150 try {
151 String filename = getLogDirectory() + File.separator + logNumber + ".out";
152 File file = new File(filename);
153 FileWriter writer = new FileWriter(file);
154 writer.write(xmlOut);
155 } catch (IOException e) {
156 logger.error("caught exception: " +e );
157 }
158 return xmlOut;
159 }
160
161 /**
162 * Process an XML request - as a String
163 *
164 * @param xmlIn the request to process
165 * @return the response - contains any error messages
166 * @see java.lang.String
167 */
168 protected String processInner(String xmlIn)
169 {
170 try {
171 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
172 DocumentBuilder builder= factory.newDocumentBuilder();
173 DOMParser parser = new DOMParser();
174 try {
175
176 parser.setFeature("http://xml.org/sax/features/validation",true);
177 parser.setFeature("http://apache.org/xml/features/validation/schema",true);
178 parser.setFeature("http://apache.org/xml/features/validation/dynamic", true);
179 parser.setFeature("http://apache.org/xml/features/validation/schema", true);
180 parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
181 parser.setFeature("http://apache.org/xml/features/validation/schema/normalized-value", true);
182 parser.setFeature("http://apache.org/xml/features/validation/warn-on-duplicate-attdef", true);
183 parser.setFeature("http://apache.org/xml/features/validation/warn-on-undeclared-elemdef", true);
184 parser.setFeature("http://apache.org/xml/features/warn-on-duplicate-entitydef", true);
185 } catch (Exception a) {
186 logger.error("unable to set feature:" +a);
187 a.printStackTrace();
188 }
189
190 // do the pre-call checking
191 InputSource xml_source = new InputSource(new StringReader(xmlIn));
192 try {
193 parser.parse(xml_source);
194 } catch (Exception e){
195 logger.error("parsing error:" +e);
196 e.printStackTrace();
197 return "<response> <error class=\"ModuleWrapper\" code=1> Error: supplied string contained the parse error: " + e + " </error> </response>";
198 }
199
200 String xmlOut = inner.process(xmlIn);
201
202 // do the post-call checking
203 InputSource xmlResult = new InputSource(new StringReader(xmlIn));
204 try {
205 parser.parse(xmlResult);
206 } catch (Exception e){
207 logger.error("parsing error:" +e);
208 e.printStackTrace();
209 return "<response> <error class=\"ModuleWrapper\" code=2> Error: returned string contained the parse error: " + e + "</error></response>";
210 }
211
212 return xmlOut;
213
214 } catch (Exception e){
215 logger.error("other error:" +e);
216 e.printStackTrace();
217 return "<response> <error class=\"ModuleWrapper\" code=3>Error: Unknown error or warning: " + e + " </error></response>";
218 }
219 }
220
221 /**
222 * Process an XML request - as a DOM Element
223 *
224 * @param in the request to process
225 * @return the response - contains any error messages
226 * @see org.w3c.dom.Element
227 */
228 public Node process(Node xmlIn) {
229 throw new Error("Not implmented yet. Should be faked by stringizing the node.");
230 }
231}
232
233
234
235
236
237
238
239
240
Note: See TracBrowser for help on using the repository browser.