1
2
3
4
5
6
7 package gov.noaa.edsd.sessionlessRemoteXmlDb;
8
9 import javax.servlet.ServletContext;
10 import javax.xml.rpc.server.ServiceLifecycle;
11 import javax.xml.rpc.server.ServletEndpointContext;
12 import org.apache.log4j.Logger;
13
14 import org.xmldb.api.base.Database;
15 import org.xmldb.api.base.Collection;
16 import org.xmldb.api.base.ResourceSet;
17 import org.xmldb.api.modules.XMLResource;
18 import org.xmldb.api.modules.XPathQueryService;
19 import org.xmldb.api.base.XMLDBException;
20 import org.w3c.dom.Document;
21
22 /***
23 * Provides a really simple interface to an XAPI database.
24 * @author tns
25 * @version $Id$
26 */
27 public class SessionlessXmldbService implements ServiceLifecycle {
28
29 public static String DATABASE_KEY = "org.xmldb.api.base.Database";
30
31 private static Logger log = Logger.getLogger(SessionlessXmldbService.class);
32
33 private Database database = null;
34
35 /*** Creates a new instance of Service */
36 public SessionlessXmldbService() {
37 }
38 /*** Grabs the Database from the servlet context with attribute "org.xmldb.api.base.Database".
39 *@param context The ServletEndpiontContext
40 */
41 public void init(Object context) throws javax.xml.rpc.ServiceException {
42 ServletEndpointContext soapContext = (ServletEndpointContext) context;
43 ServletContext servletContext = soapContext.getServletContext();
44 database = (Database) servletContext.getAttribute(DATABASE_KEY);
45 if (database == null){
46 throw new javax.xml.rpc.ServiceException("Database not found " +
47 "using attribute " + DATABASE_KEY );
48 }
49 }
50
51 /***removes reference to database */
52 public void destroy() {
53 this.database = null;
54 }
55
56 /***Checks to see if the collection is available
57 *@param collectionName
58 *@returns true if the collection is available, false otherwise.
59 */
60 public boolean isCollectionAvailable(String collectionName) throws SessionlessException {
61 try {
62 return this.database.acceptsURI(collectionName);
63 } catch (XMLDBException e){
64 throw new SessionlessException(e);
65 }
66 }
67
68 /***Retrieves a resource using xpath
69 *@param collectionName The name of the collection to user
70 *@param xpathQuery The xpath to use to find a resource
71 *@param userName The user name used for verification
72 *@param password The password used for verification
73 *@return String the XML as a String or null if there is not a resource
74 *that satisfies the query.
75 */
76 public String getXmlResourceWithXpath(String collectionName, String xpathQuery,
77 String userName, String password) throws SessionlessException {
78 Collection collection = null;
79 try {
80 collection = this.database.getCollection(collectionName, userName, password);
81 XPathQueryService service = (XPathQueryService) collection.getService("XPathQueryService","1.0");
82 ResourceSet resourceSet = service.query(xpathQuery);
83 if (resourceSet.getSize() == 0){
84 return null;
85 }
86 XMLResource resource = (XMLResource) resourceSet.getResource(0);
87 org.w3c.dom.Node node = resource.getContentAsDOM();
88 return this.serializeDom(node);
89
90 } catch (XMLDBException e){
91 throw new SessionlessException(e);
92 } finally {
93 try {
94 collection.close();
95 } catch (XMLDBException e){
96 log.debug("Error while closing collection",e);
97 }
98 }
99
100 }
101
102 /***Retrieves a resource by id
103 *@param collectionName The name of the collection
104 *@param resourceId The id of the resource to retrieve
105 *@returns The XML as a string
106 */
107 public String getXmlResource(String collectionName, String resourceId,
108 String userName, String password) throws SessionlessException {
109 Collection collection = null;
110 try {
111 collection = this.database.getCollection(collectionName, userName, password);
112 XMLResource resource = (XMLResource) collection.getResource(resourceId);
113 org.w3c.dom.Node node = resource.getContentAsDOM();
114 return this.serializeDom(node);
115
116 } catch (XMLDBException e){
117 throw new SessionlessException(e);
118 } finally {
119 try {
120 collection.close();
121 } catch (XMLDBException e){
122 log.debug("Error while closing collection",e);
123 }
124 }
125 }
126
127 /***Updates the specified resource by replaceing the entire record
128 * with the specified record.
129 *@param collectionName The collection which contains the current resource
130 *@param resourceId The id of the resource
131 *@param xml The xml string to replace
132 *@param userName The user name to use for verification
133 *@param password The password to use for verification
134 */
135 public void updateResource(String collectionName, String resourceId,
136 String xml, String userName, String password) throws SessionlessException {
137 Document doc = this.parseXml(xml);
138 Collection collection = null;
139 try {
140 collection = this.database.getCollection(collectionName,userName,password);
141 String id = collection.createId();
142 XMLResource resource = (XMLResource) collection.getResource(resourceId);
143 resource.setContentAsDOM(doc);
144 collection.storeResource(resource);
145 } catch (XMLDBException e){
146 throw new SessionlessException(e);
147 } finally {
148
149 if (collection != null){
150 try {
151 collection.close();
152 } catch (XMLDBException e){
153 log.warn(e);
154 }
155 }
156 }
157
158 }
159
160 /***Transforms a node into a String */
161 private String serializeDom(org.w3c.dom.Node node) throws SessionlessException {
162 java.io.StringWriter writer = new java.io.StringWriter();
163 try {
164 javax.xml.parsers.DocumentBuilderFactory factory =
165 new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
166 javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
167 javax.xml.transform.stream.StreamResult result =
168 new javax.xml.transform.stream.StreamResult(writer);
169 javax.xml.transform.dom.DOMSource source =
170 new javax.xml.transform.dom.DOMSource(node);
171 javax.xml.transform.TransformerFactory transFactory =
172 new org.apache.xalan.processor.TransformerFactoryImpl();
173 javax.xml.transform.Transformer transformer =
174 transFactory.newTransformer();
175 transformer.transform(source,result);
176 return writer.getBuffer().toString();
177 } catch (javax.xml.transform.TransformerException e){
178 throw new SessionlessException(e);
179 } catch (javax.xml.parsers.ParserConfigurationException e){
180 throw new SessionlessException(e);
181 }
182 }
183
184 /***Does a quick parse of XML
185 *@param xml
186 */
187 private Document parseXml(String xml) throws SessionlessException {
188 java.io.Reader reader = null;
189 try {
190
191 javax.xml.parsers.DocumentBuilderFactory factory =
192 new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
193 javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
194 reader = new java.io.StringReader(xml);
195 org.xml.sax.InputSource is = new org.xml.sax.InputSource(reader);
196 Document doc = builder.parse(is);
197 return doc;
198 } catch (java.io.IOException e){
199 throw new SessionlessException(e);
200 } catch (org.xml.sax.SAXException e){
201 throw new SessionlessException(e);
202 } catch (javax.xml.parsers.ParserConfigurationException e){
203 throw new SessionlessException(e);
204 } finally {
205 if (reader != null){
206 try {
207 reader.close();
208 } catch (java.io.IOException e){
209 log.warn(e);
210 }
211 }
212
213 }
214 }
215
216 /***Saves the specified XML String in the collection.
217 *@param collectionName The name of the collection to insert the record in.
218 *@param xml The xml to insert.
219 *@param userName The user name to use for verification
220 *@param password The password to use for verification
221 *@returns The id of the inserted resource
222 */
223 public String insertResource(String collectionName, String xml,
224 String userName, String password) throws SessionlessException {
225
226 Document doc = this.parseXml(xml);
227 Collection collection = null;
228 try {
229 collection = this.database.getCollection(collectionName,userName,password);
230 String id = collection.createId();
231 XMLResource resource = (XMLResource) collection.createResource(id,"XMLResource");
232 resource.setContentAsDOM(doc);
233 collection.storeResource(resource);
234 return resource.getId();
235 } catch (XMLDBException e){
236 throw new SessionlessException(e);
237 } finally {
238
239 if (collection != null){
240 try {
241 collection.close();
242 } catch (XMLDBException e){
243 log.warn(e);
244 }
245 }
246 }
247
248 }
249
250 }