1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts.connector;
20
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileReader;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.dom4j.Document;
32 import org.dom4j.Element;
33 import org.dom4j.io.OutputFormat;
34 import org.dom4j.io.XMLWriter;
35 import org.springframework.extensions.config.RemoteConfigElement.EndpointDescriptor;
36 import org.springframework.extensions.surf.util.StringBuilderWriter;
37
38
39
40
41
42
43
44
45
46
47
48
49 public class XMLCredentialVault extends AbstractPersistentCredentialVault
50 {
51 private static Log logger = LogFactory.getLog(XMLCredentialVault.class);
52
53 protected String location;
54
55
56
57
58
59
60 public XMLCredentialVault(String id)
61 {
62 super(id);
63 }
64
65 public void setLocation(String location)
66 {
67 this.location = location;
68 }
69
70 public String getLocation()
71 {
72 return this.location;
73 }
74
75
76
77
78 public boolean load()
79 {
80 boolean success = false;
81
82 File file = new File(getLocation() + "/" + this.id + ".xml");
83 if(file.exists())
84 {
85 StringBuilder contents = new StringBuilder();
86 try
87 {
88 BufferedReader input = new BufferedReader(new FileReader(file));
89 try
90 {
91 String line = null;
92
93 while (( line = input.readLine()) != null)
94 {
95 contents.append(line);
96 contents.append(System.getProperty("line.separator"));
97 }
98
99
100 String xml = contents.toString();
101
102
103 deserialize(xml);
104
105
106 success = true;
107 }
108 finally
109 {
110 input.close();
111 }
112 }
113 catch (IOException ex)
114 {
115 ex.printStackTrace();
116 }
117 }
118 else
119 {
120 if(logger.isDebugEnabled())
121 {
122 logger.debug("Unable to load XML Credential Vault");
123 logger.debug("Not found: " + file.getAbsolutePath());
124 }
125 }
126
127 return success;
128 }
129
130
131
132
133 public boolean save()
134 {
135 boolean success = false;
136
137 String xml = serialize();
138
139 File file = new File(getLocation() + "/" + this.id + ".xml");
140 if(file.exists())
141 {
142 file.delete();
143 }
144
145 FileWriter fw = null;
146 try
147 {
148 fw = new FileWriter(file, true);
149 fw.write(xml);
150
151
152 success = true;
153 }
154 catch(Exception ex)
155 {
156 ex.printStackTrace();
157 }
158 finally
159 {
160 if(fw != null)
161 {
162 try { fw.close(); } catch(Exception ex) { }
163 }
164 }
165
166 return success;
167 }
168
169
170
171
172
173
174 protected String serialize()
175 {
176 Element rootElement = org.dom4j.DocumentHelper.createElement("vault");
177 rootElement.addAttribute("id", this.id);
178
179 Document d = org.dom4j.DocumentHelper.createDocument(rootElement);
180
181
182 Iterator it = credentialsMap.keySet().iterator();
183 while(it.hasNext())
184 {
185 String endpointId = (String) it.next();
186
187 EndpointDescriptor descriptor = getRemoteConfig().getEndpointDescriptor(endpointId);
188 if(descriptor.getPersistent())
189 {
190
191 Element endpointElement = rootElement.addElement("endpoint");
192 endpointElement.addAttribute("id", endpointId);
193
194 Credentials credentials = retrieve(endpointId);
195
196 String[] keys = credentials.getPropertyKeys();
197 for(int i = 0; i < keys.length; i++)
198 {
199 String value = (String) credentials.getProperty(keys[i]);
200
201 Element credentialElement = endpointElement.addElement("credential");
202 credentialElement.addAttribute("id", keys[i]);
203 credentialElement.setText(value);
204 }
205 }
206 }
207
208 return toXML(d, true);
209 }
210
211
212
213
214
215
216 protected void deserialize(String xml)
217 {
218
219
220
221
222 Document d = null;
223 try
224 {
225 d = org.dom4j.DocumentHelper.parseText(xml);
226
227 Element rootElement = d.getRootElement();
228 this.id = rootElement.attributeValue("id");
229
230 List endpoints = rootElement.elements("endpoint");
231 for(int i = 0; i < endpoints.size(); i++)
232 {
233 Element endpointElement = (Element) endpoints.get(i);
234 String endpointId = endpointElement.attributeValue("id");
235
236
237 EndpointDescriptor descriptor = getRemoteConfig().getEndpointDescriptor(endpointId);
238 if(descriptor.getPersistent())
239 {
240
241 Credentials credz = newCredentials(endpointId);
242
243
244 List credentialsList = endpointElement.elements("credential");
245 for(int k = 0; k < credentialsList.size(); k++)
246 {
247 Element credential = (Element) credentialsList.get(k);
248
249 String credentialId = credential.attributeValue("id");
250 String credentialValue = credential.getTextTrim();
251
252 credz.setProperty(credentialId, credentialValue);
253 }
254
255
256 store(credz);
257 }
258 }
259 }
260 catch(Exception ex)
261 {
262 ex.printStackTrace();
263 }
264 }
265
266
267
268
269 @Override
270 public String toString()
271 {
272 return "XMLCredentialVault - " + location;
273 }
274
275
276
277
278
279
280
281
282
283
284 public static String toXML(Document document, boolean pretty)
285 {
286 String xml = null;
287
288 if (pretty)
289 {
290 OutputFormat format = OutputFormat.createPrettyPrint();
291 format.setSuppressDeclaration(false);
292
293 StringBuilderWriter writer = new StringBuilderWriter(256);
294 XMLWriter xmlWriter = new XMLWriter(writer, format);
295 try
296 {
297 xmlWriter.write(document);
298 xmlWriter.flush();
299 xml = writer.toString();
300 }
301 catch (IOException ioe)
302 {
303
304
305
306
307 logger.debug(ioe);
308 }
309 }
310
311
312
313
314
315 if (xml == null)
316 {
317 xml = document.asXML();
318 }
319
320 return xml;
321 }
322
323 }