source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/ModuleWrapper.java@ 3621

Last change on this file since 3621 was 3621, checked in by say1, 21 years ago

added XSLTServices.java

  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 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.apache.xindice.xml.TextWriter;
29import org.w3c.dom.Document;
30import org.w3c.dom.Element;
31import org.w3c.dom.Node;
32import org.w3c.dom.Node;
33import org.xml.sax.InputSource;
34import org.xml.sax.SAXException;
35import org.xml.sax.XMLReader;
36
37// java classes
38import java.io.StringReader;
39import java.io.File;
40import java.io.FileWriter;
41import java.io.IOException;
42import java.security.SecureRandom;
43
44/**
45 * A classes for logging and checking all traffic to and from a ModuleInterface
46 *
47 * if logCounter hasn't been initialised, initialise it to a random
48 * value. This has to use secure random rather than a normal random
49 * or every time the application is run the same numbers will be
50 * generated and the search for a free range will become a linear
51 * search rather than a hash.
52 *
53 *
54 * @author <a href="mailto:[email protected]">Stuart Yeates</a>
55 * @version $Revision: 3621 $
56 */
57public class ModuleWrapper
58 implements ModuleInterface
59{
60 /** the module we're wrapping */
61 protected ModuleInterface inner = null;
62 /** the module we're wrapping */
63 public ModuleInterface getInner(){return inner;};
64 /** the module we're wrapping */
65 public void setInner(ModuleInterface i){inner=i;};
66
67 /** The schema of the IN XML */
68 protected String fromSchema = null;
69 /** The schema of the IN XML */
70 public String getFromSchema(){return fromSchema;};
71 /** The schema of the IN XML */
72 public void setFromSchema(String s){fromSchema=s;};
73
74 /** The schema of the OUT XML */
75 protected String toSchema = null;
76 /** The schema of the OUT XML */
77 public String getToSchema(){return toSchema;};
78 /** The schema of the OUT XML */
79 public void setToSchema(String s){toSchema=s;};
80
81 /** Are we logging the XML ? */
82 protected boolean logging = true;
83 /** Are we logging the XML ? */
84 public boolean getLogging(){return logging;};
85 /** Are we logging the XML ? */
86 public void setLogging(boolean b){logging=b;};
87
88 /** Where we are logging the XML */
89 protected String logDirectory = "/tmp/";
90 /** Where we are logging the XML */
91 public String getLogDirectory(){return logDirectory;};
92 /** Where we are logging the XML */
93 public void setLogDirectory(String s){logDirectory=s;};
94
95 /** The number of the previous log file */
96 protected static long logCounter = 0;
97
98 /** The no-args constructor */
99 public ModuleWrapper(){
100
101 }
102 /** The all-args constructor */
103 public ModuleWrapper(String in,String out, ModuleInterface inner){
104 this.setFromSchema(in);
105 this.setToSchema(out);
106 this.setInner(inner);
107 }
108
109 /**
110 * Process an XML request - as a String
111 *
112 * @param xmlIn the request to process
113 * @return the response - contains any error messages
114 * @see java.lang.String
115 */
116 public String process(String xmlIn) {
117
118 long logNumber = 0;
119 String xmlOut = "";
120 if (getLogging()) {
121 if (logCounter == 0)
122 logCounter = new SecureRandom().nextLong();
123 try {
124 logNumber = logCounter++;
125 String filename = getLogDirectory() + File.separator + logNumber + ".in";
126 File file = new File(filename);
127
128 while (file.exists()){
129 logCounter = new SecureRandom().nextLong();
130 logNumber = logCounter++;
131 filename = getLogDirectory() + File.separator + logNumber + ".in";
132 file = new File(filename);
133 }
134
135 FileWriter writer = new FileWriter(file);
136 writer.write(xmlIn);
137 } catch (IOException e) {
138 System.err.println("ModuleWrapper: caught exception: " +e );
139 }
140 }
141 xmlOut = processInner(xmlIn);
142
143 try {
144 String filename = getLogDirectory() + File.separator + logNumber + ".out";
145 File file = new File(filename);
146 FileWriter writer = new FileWriter(file);
147 writer.write(xmlOut);
148 } catch (IOException e) {
149 System.err.println("ModuleWrapper: caught exception: " +e );
150 }
151 return xmlOut;
152 }
153
154 /**
155 * Process an XML request - as a String
156 *
157 * @param xmlIn the request to process
158 * @return the response - contains any error messages
159 * @see java.lang.String
160 */
161 protected String processInner(String xmlIn)
162 {
163 try {
164 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
165 DocumentBuilder builder= factory.newDocumentBuilder();
166 DOMParser parser = new DOMParser();
167 try {
168
169 parser.setFeature("http://xml.org/sax/features/validation",true);
170 parser.setFeature("http://apache.org/xml/features/validation/schema",true);
171 parser.setFeature("http://apache.org/xml/features/validation/dynamic", true);
172 parser.setFeature("http://apache.org/xml/features/validation/schema", true);
173 parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
174 parser.setFeature("http://apache.org/xml/features/validation/schema/normalized-value", true);
175 parser.setFeature("http://apache.org/xml/features/validation/warn-on-duplicate-attdef", true);
176 parser.setFeature("http://apache.org/xml/features/validation/warn-on-undeclared-elemdef", true);
177 parser.setFeature("http://apache.org/xml/features/warn-on-duplicate-entitydef", true);
178 } catch (Exception a) {
179 System.err.println("unable to set feature:" +a);
180 a.printStackTrace();
181 }
182
183 // do the pre-call checking
184 InputSource xml_source = new InputSource(new StringReader(xmlIn));
185 try {
186 parser.parse(xml_source);
187 } catch (Exception e){
188 System.err.println("parsing error:" +e);
189 e.printStackTrace();
190 return "<response> <error class=\"ModuleWrapper\" code=1> Error: supplied string contained the parse error: " + e + " </error> </response>";
191 }
192
193 String xmlOut = inner.process(xmlIn);
194
195 // do the post-call checking
196 InputSource xmlResult = new InputSource(new StringReader(xmlIn));
197 try {
198 parser.parse(xmlResult);
199 } catch (Exception e){
200 System.err.println("parsing error:" +e);
201 e.printStackTrace();
202 return "<response> <error class=\"ModuleWrapper\" code=2> Error: returned string contained the parse error: " + e + "</error></response>";
203 }
204
205 return xmlOut;
206
207 } catch (Exception e){
208 System.err.println("other error:" +e);
209 e.printStackTrace();
210 return "<response> <error class=\"ModuleWrapper\" code=3>Error: Unknown error or warning: " + e + " </error></response>";
211 }
212 }
213
214 /**
215 * Process an XML request - as a DOM Element
216 *
217 * @param in the request to process
218 * @return the response - contains any error messages
219 * @see org.w3c.dom.Element
220 */
221 public Element process(Element xmlIn) {
222 throw new Error("Not implmented yet. Should be faked by stringizing the node.");
223 }
224}
225
226
227
228
229
230
231
232
233
Note: See TracBrowser for help on using the repository browser.