1 package org.astrogrid.xmldb.eXist.server;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.net.HttpURLConnection;
7 import java.net.URLEncoder;
8 import org.astrogrid.util.DomHelper;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import org.w3c.dom.Document;
14 import org.w3c.dom.NodeList;
15 import org.w3c.dom.Element;
16 import org.w3c.dom.Node;
17
18 import java.io.DataOutputStream;
19
20 import org.astrogrid.config.Config;
21 import org.apache.commons.httpclient.HttpClient;
22 import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
23 import org.apache.commons.httpclient.methods.PutMethod;
24 import org.apache.commons.httpclient.UsernamePasswordCredentials;
25
26 import org.apache.axis.AxisFault;
27
28
29 public class UpdateDBService {
30
31 private static final Log log = LogFactory.getLog(UpdateDBService.class);
32
33 public static Config conf = null;
34 private static String existLocation = null;
35
36 static {
37 if(conf == null) {
38 conf = org.astrogrid.config.SimpleConfig.getSingleton();
39 existLocation = conf.getString("exist.db.url");
40 }
41 }
42
43
44 public void update(String uniqueName, String type,
45 String collectionName, Document update) throws AxisFault {
46 log.debug("start update");
47 try {
48 updateQuery(uniqueName, type, collectionName, update.getDocumentElement());
49 }catch(MalformedURLException mue) {
50 log.error(mue);
51 throw new AxisFault("URL incorrect", mue);
52 }catch(IOException ioe) {
53 log.error(ioe);
54 throw new AxisFault("IO Excepiton error", ioe);
55 }finally {
56 log.debug("end update");
57 }
58 }
59
60 private String getUpdateURL(String collectionName, String xmlDocName) throws MalformedURLException
61 {
62 log.debug("start getUpdateURL");
63 String location = existLocation;
64 location += "/servlet/db/" + collectionName + "/" + xmlDocName;
65 URL fullQueryURL = null;
66 log.info("the full update put url = " + location);
67 log.debug("end getUpdateURL");
68 return location;
69
70
71
72
73
74
75
76
77
78
79
80 }
81
82
83 public void updateQuery(String uniqueName, String type,
84 String collectionName, Node updateNode)
85 throws MalformedURLException, IOException
86 {
87 log.debug("start updateQuery");
88 log.info("THE IDENTIFIER = " + uniqueName);
89 String xmlDocName = uniqueName.replaceAll("[^//w*]","_") + "." + type;
90 log.info("THE REPLACED DOC NAME = " + xmlDocName);
91
92 HttpClient httpclient = new HttpClient();
93
94
95
96
97 PutMethod put = new PutMethod(getUpdateURL(collectionName, xmlDocName));
98
99
100
101
102
103
104
105
106
107
108
109
110
111 String xml = null;
112 if(updateNode instanceof Element) {
113
114 xml = DomHelper.ElementToString((Element)updateNode);
115 }else if(updateNode instanceof Document) {
116
117 xml = DomHelper.DocumentToString((Document)updateNode);
118 }else {
119 throw new IOException("The Node was not of an instance of Element or Document which is required for updating the eXist db");
120 }
121 if(xml == null || xml.trim().length() <= 0) {
122 throw new IOException("Nothing was present to send; empty xml content");
123 }
124 put.setRequestBody(xml);
125 if (xml.length() < Integer.MAX_VALUE) {
126 put.setRequestContentLength((int)xml.length());
127 } else {
128 put.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
129 }
130
131 put.setRequestHeader("Content-type", "text/xml");
132
133 int result = httpclient.executeMethod(put);
134 log.info("Status code of the resulting post = " + result);
135 if(result != 200) {
136 String error = put.getResponseBodyAsString();
137 log.error("Seems to be a problem with query = " + error);
138
139 }
140 log.info("Response of the http put = " + put.getResponseBodyAsString());
141 put.releaseConnection();
142
143
144
145
146
147
148
149
150
151
152
153 }
154 }