Subversion Repositories XServices

Rev

Rev 177 | Rev 198 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 177 Rev 185
Line 24... Line 24...
24
import java.io.UnsupportedEncodingException;
24
import java.io.UnsupportedEncodingException;
25
import java.nio.charset.Charset;
25
import java.nio.charset.Charset;
26
import java.nio.charset.IllegalCharsetNameException;
26
import java.nio.charset.IllegalCharsetNameException;
27
import java.util.Iterator;
27
import java.util.Iterator;
28
import java.util.List;
28
import java.util.List;
29
import java.util.StringTokenizer;
-
 
30
 
-
 
31
import javax.jws.WebService;
29
import javax.jws.WebService;
32
import javax.ws.rs.NotAuthorizedException;
-
 
33
import javax.xml.namespace.QName;
30
import javax.xml.namespace.QName;
34
import javax.xml.stream.XMLOutputFactory;
31
import javax.xml.stream.XMLOutputFactory;
35
import javax.xml.stream.XMLStreamException;
32
import javax.xml.stream.XMLStreamException;
36
import javax.xml.stream.XMLStreamReader;
-
 
37
import javax.xml.stream.XMLStreamWriter;
33
import javax.xml.stream.XMLStreamWriter;
Line 38... Line -...
38
 
-
 
39
import net.brutex.xservices.security.DirectoryPermission;
34
 
40
import net.brutex.xservices.types.AttributeType;
35
import net.brutex.xservices.types.AttributeType;
41
import net.brutex.xservices.types.NamespaceListType;
36
import net.brutex.xservices.types.NamespaceListType;
42
import net.brutex.xservices.types.NamespaceType;
37
import net.brutex.xservices.types.NamespaceType;
43
import net.brutex.xservices.types.StringSplitType;
38
import net.brutex.xservices.types.StringSplitType;
Line 56... Line 51...
56
import org.apache.axiom.om.OMNode;
51
import org.apache.axiom.om.OMNode;
57
import org.apache.axiom.om.OMProcessingInstruction;
52
import org.apache.axiom.om.OMProcessingInstruction;
58
import org.apache.axiom.om.OMText;
53
import org.apache.axiom.om.OMText;
59
import org.apache.axiom.om.OMXMLBuilderFactory;
54
import org.apache.axiom.om.OMXMLBuilderFactory;
60
import org.apache.axiom.om.xpath.AXIOMXPath;
55
import org.apache.axiom.om.xpath.AXIOMXPath;
-
 
56
 
61
import org.apache.log4j.Logger;
57
import org.apache.logging.log4j.LogManager;
62
import org.apache.shiro.SecurityUtils;
58
import org.apache.logging.log4j.Logger;
63
import org.apache.shiro.authz.UnauthorizedException;
-
 
64
import org.apache.shiro.authz.annotation.RequiresPermissions;
59
import org.apache.shiro.authz.annotation.RequiresPermissions;
65
import org.jaxen.JaxenException;
60
import org.jaxen.JaxenException;
66
import org.jaxen.SimpleNamespaceContext;
61
import org.jaxen.SimpleNamespaceContext;
Line 67... Line 62...
67
 
62
 
68
/**
63
/**
69
 * @author Brian Rosenberger, bru(at)brutex.de
64
 * @author Brian Rosenberger, bru(at)brutex.de
70
 * 
65
 * 
71
 */
66
 */
72
@WebService(targetNamespace = "http://ws.xservices.brutex.net", endpointInterface = "net.brutex.xservices.ws.XmlService", serviceName = "XmlService")
67
@WebService(targetNamespace = "http://ws.xservices.brutex.net", endpointInterface = "net.brutex.xservices.ws.XmlService", serviceName = "XmlService")
73
public class XmlServiceImpl implements XmlService {
68
public class XmlServiceImpl implements XmlService {
Line 74... Line 69...
74
	final Logger logger = Logger.getLogger(XmlServiceImpl.class);
69
	private static final Logger logger = LogManager.getLogger();
-
 
70
 
75
 
71
	public String insertNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment)
76
	public String insertNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
72
			throws XServicesFault {
77
		try {
73
		try {
78
			AXIOMXPath axp = new AXIOMXPath(xpath);
74
			AXIOMXPath axp = new AXIOMXPath(xpath);
79
			InputStream is = res.getAntResource(null).getInputStream();
-
 
80
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is)
75
			InputStream is = res.getAntResource(null).getInputStream();
81
					.getDocument();
76
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
82
			OMDocument fragdoc = null;
-
 
83
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
77
			OMDocument fragdoc = null;
84
				fragdoc = OMXMLBuilderFactory.createOMBuilder(
78
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
85
						new StringReader("<XS>" + xmlFragment + "</XS>"))
79
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
86
						.getDocument();
80
						.getDocument();
87
			} else {
81
			} else {
Line 112... Line 106...
112
			e.printStackTrace();
106
			e.printStackTrace();
113
			throw new XServicesFault(e);
107
			throw new XServicesFault(e);
114
		}
108
		}
115
	}
109
	}
Line 116... Line 110...
116
 
110
 
-
 
111
	public String replaceNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment)
117
	public String replaceNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
112
			throws XServicesFault {
118
		try {
113
		try {
119
				AXIOMXPath axp = new AXIOMXPath(xpath);
114
			AXIOMXPath axp = new AXIOMXPath(xpath);
120
				InputStream is = res.getAntResource(null).getInputStream();
115
			InputStream is = res.getAntResource(null).getInputStream();
121
				OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
116
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
122
				OMDocument fragdoc = null;
117
			OMDocument fragdoc = null;
123
				if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
-
 
124
					fragdoc = OMXMLBuilderFactory.createOMBuilder(
118
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
125
							new StringReader("<XS>" + xmlFragment + "</XS>"))
119
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
126
							.getDocument();
120
						.getDocument();
127
				} else {
121
			} else {
128
					throw new XServicesFault("No xmldata to insert.");
122
				throw new XServicesFault("No xmldata to insert.");
Line 152... Line 146...
152
				e.printStackTrace();
146
			e.printStackTrace();
153
				throw new XServicesFault(e);
147
			throw new XServicesFault(e);
154
			}
148
		}
155
	}
149
	}
Line 156... Line 150...
156
 
150
 
-
 
151
	public String replaceNodes(String source, String encoding, NamespaceListType nsList, String xpath,
157
	public String replaceNodes(String source, String encoding, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
152
			String xmlFragment) throws XServicesFault {
158
		encoding = validateEncoding(encoding);
153
		encoding = validateEncoding(encoding);
159
		try {
154
		try {
160
			AXIOMXPath axp = new AXIOMXPath(xpath);
155
			AXIOMXPath axp = new AXIOMXPath(xpath);
161
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
156
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
162
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is)
-
 
163
					.getDocument();
157
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
164
			OMDocument fragdoc = null;
158
			OMDocument fragdoc = null;
165
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
-
 
166
				fragdoc = OMXMLBuilderFactory.createOMBuilder(
159
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
167
						new StringReader("<XS>" + xmlFragment + "</XS>"))
160
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
168
						.getDocument();
161
						.getDocument();
169
			} else {
162
			} else {
170
				throw new XServicesFault("No xmldata to insert.");
163
				throw new XServicesFault("No xmldata to insert.");
Line 194... Line 187...
194
			throw new XServicesFault(e);
187
			throw new XServicesFault(e);
195
		}
188
		}
196
	}
189
	}
Line 197... Line 190...
197
	
190
 
198
	@RequiresPermissions("insertNodes")
191
	@RequiresPermissions("insertNodes")
-
 
192
	public String insertNodes(String source, String encoding, NamespaceListType nsList, String xpath,
199
	public String insertNodes(String source, String encoding, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
193
			String xmlFragment) throws XServicesFault {
200
		encoding = validateEncoding(encoding);
194
		encoding = validateEncoding(encoding);
201
		try {
195
		try {
202
			AXIOMXPath axp = new AXIOMXPath(xpath);
196
			AXIOMXPath axp = new AXIOMXPath(xpath);
203
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
197
			InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
204
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is)
-
 
205
					.getDocument();
198
			OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
206
			OMDocument fragdoc = null;
199
			OMDocument fragdoc = null;
207
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
-
 
208
				fragdoc = OMXMLBuilderFactory.createOMBuilder(
200
			if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
209
						new StringReader("<XS>" + xmlFragment + "</XS>"))
201
				fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
210
						.getDocument();
202
						.getDocument();
211
			} else {
203
			} else {
212
				throw new XServicesFault("No xmldata to insert.");
204
				throw new XServicesFault("No xmldata to insert.");
Line 241... Line 233...
241
		String result ="";
233
		String result = "";
242
		String[] tokens = data.split("\\]\\]>", -1);
234
		String[] tokens = data.split("\\]\\]>", -1);
Line 243... Line 235...
243
		
235
 
244
		for(int i=0; i<tokens.length; i++) {
236
		for (int i = 0; i < tokens.length; i++) {
-
 
237
			result += tokens[i];
245
			result +=  tokens[i];
238
			if (i + 1 < tokens.length)
246
			if (i+1 < tokens.length ) result += "]]]]><![CDATA[>";
239
				result += "]]]]><![CDATA[>";
Line 247... Line 240...
247
		}
240
		}
248
		
241
 
249
		result = "<![CDATA[" + result + "]]>";
242
		result = "<![CDATA[" + result + "]]>";
Line 250... Line 243...
250
		return result;
243
		return result;
-
 
244
	}
251
	}
245
 
252
 
246
	public StringSplitType selectXPath(String source, String encoding, NamespaceListType nsList, String xpath)
253
	public StringSplitType selectXPath(String source, String encoding, NamespaceListType nsList, String xpath) throws XServicesFault {
247
			throws XServicesFault {
254
		encoding = validateEncoding(encoding);
248
		encoding = validateEncoding(encoding);
255
		try {
249
		try {
Line 291... Line 285...
291
					text = String.valueOf(o);
285
					text = String.valueOf(o);
292
				}
286
				}
293
				rarray.addStringMatch(text);
287
				rarray.addStringMatch(text);
294
			}
288
			}
Line 295... Line -...
295
			
-
 
296
 
289
 
297
			return rarray;
290
			return rarray;
298
			} catch (JaxenException e) {
291
		} catch (JaxenException e) {
299
			e.printStackTrace();
292
			e.printStackTrace();
300
			throw new XServicesFault(e);
293
			throw new XServicesFault(e);
Line 304... Line 297...
304
			} catch (UnsupportedEncodingException e) {
297
		} catch (UnsupportedEncodingException e) {
305
				throw new XServicesFault(e);
298
			throw new XServicesFault(e);
306
		}
299
		}
307
	}
300
	}
Line 308... Line 301...
308
	
301
 
-
 
302
	public String setAttribute(String source, String encoding, NamespaceListType nsList, String xpath,
309
	public String setAttribute(String source, String encoding, NamespaceListType nsList, String xpath, AttributeType attr) throws XServicesFault {
303
			AttributeType attr) throws XServicesFault {
310
		encoding = validateEncoding(encoding);
304
		encoding = validateEncoding(encoding);
311
		try {
305
		try {
312
			StringSplitType rarray = new StringSplitType();
306
			StringSplitType rarray = new StringSplitType();
313
			AXIOMXPath axp = new AXIOMXPath(xpath);
307
			AXIOMXPath axp = new AXIOMXPath(xpath);
Line 356... Line 350...
356
			} catch (UnsupportedEncodingException e) {
350
		} catch (UnsupportedEncodingException e) {
357
				throw new XServicesFault(e);
351
			throw new XServicesFault(e);
358
		}
352
		}
359
	}
353
	}
Line 360... Line 354...
360
	
354
 
-
 
355
	private OMDocument insertNodes(OMDocument xmldocument, AXIOMXPath axp, OMDocument xmlfragment)
361
	private OMDocument insertNodes(OMDocument xmldocument, AXIOMXPath axp,OMDocument xmlfragment) throws XServicesFault {
356
			throws XServicesFault {
362
		List<?> olist = null;
357
		List<?> olist = null;
363
		try {
358
		try {
364
			olist = axp.selectNodes(xmldocument.getOMDocumentElement());
359
			olist = axp.selectNodes(xmldocument.getOMDocumentElement());
365
			this.logger.debug("XPath '" + axp.toString() + "' has "
-
 
366
					+ olist.size() + " matches.");
360
			this.logger.debug("XPath '" + axp.toString() + "' has " + olist.size() + " matches.");
367
			this.logger.trace("XPath root expression is: '" + axp.debug()
-
 
368
					+ "'.");
361
			this.logger.trace("XPath root expression is: '" + axp.debug() + "'.");
369
		} catch (JaxenException e) {
362
		} catch (JaxenException e) {
370
			throw new XServicesFault(e.getMessage(), e);
363
			throw new XServicesFault(e.getMessage(), e);
371
		}
364
		}
372
		if (olist.size() == 0)
365
		if (olist.size() == 0)
373
			throw new XServicesFault(Messages.getString("XmlService.no_match",
-
 
Line 374... Line 366...
374
					new Object[] { axp.toString() }));
366
			throw new XServicesFault(Messages.getString("XmlService.no_match", new Object[] { axp.toString() }));
375
 
367
 
Line 376... Line 368...
376
		// Prepare children to insert
368
		// Prepare children to insert
377
		xmlfragment.build();
369
		xmlfragment.build();
378
 
370
 
379
		// Determine what has been matched
371
		// Determine what has been matched
380
		OMContainer match = null;
-
 
381
		for (Object o : olist) {
372
		OMContainer match = null;
382
			Iterator<?> children = xmlfragment.getOMDocumentElement()
373
		for (Object o : olist) {
383
					.getChildren();
374
			Iterator<?> children = xmlfragment.getOMDocumentElement().getChildren();
384
			if ((o instanceof OMNode)) {
375
			if ((o instanceof OMNode)) {
385
				OMNode node = (OMNode) o;
376
				OMNode node = (OMNode) o;
Line 399... Line 390...
399
					// match = node.getParent();
390
					// match = node.getParent();
400
					match = (OMContainer) node;
391
					match = (OMContainer) node;
401
					this.logger.debug(Messages.getString("XmlService.10"));
392
					this.logger.debug(Messages.getString("XmlService.10"));
402
					break;
393
					break;
403
				default:
394
				default:
404
					this.logger.error("XPath matched "
395
					this.logger
405
							+ o.getClass().getCanonicalName() + " Node Type:"
396
							.error("XPath matched " + o.getClass().getCanonicalName() + " Node Type:" + node.getType());
406
							+ node.getType());
-
 
407
					this.logger.error(Messages.getString("XmlService.11"));
397
					this.logger.error(Messages.getString("XmlService.11"));
408
					throw new XServicesFault(
-
 
409
							Messages.getString("XmlService.12"));
398
					throw new XServicesFault(Messages.getString("XmlService.12"));
410
				}
399
				}
411
			} else {
400
			} else {
412
				this.logger.error("XPath matched "
-
 
413
						+ o.getClass().getCanonicalName());
401
				this.logger.error("XPath matched " + o.getClass().getCanonicalName());
414
				this.logger.error(Messages.getString("XmlService.11"));
402
				this.logger.error(Messages.getString("XmlService.11"));
415
				throw new XServicesFault(Messages.getString("XmlService.12"));
403
				throw new XServicesFault(Messages.getString("XmlService.12"));
416
			}
404
			}
Line 417... Line 405...
417
 
405
 
Line 423... Line 411...
423
 
411
 
424
		xmldocument.build();
412
		xmldocument.build();
425
		return xmldocument;
413
		return xmldocument;
Line 426... Line -...
426
	}
-
 
427
 
414
	}
-
 
415
 
Line 428... Line 416...
428
 
416
	private OMDocument replaceNodes(OMDocument xmldocument, AXIOMXPath axp, OMDocument xmlfragment)
429
	private OMDocument replaceNodes(OMDocument xmldocument, AXIOMXPath axp, OMDocument xmlfragment) throws XServicesFault {
417
			throws XServicesFault {
430
		
418
 
431
		List<?> olist = null;
419
		List<?> olist = null;
432
		try {
-
 
433
			olist = axp.selectNodes(xmldocument.getOMDocumentElement());
420
		try {
434
			this.logger.debug("XPath '" + axp.toString() + "' has "
-
 
435
					+ olist.size() + " matches.");
421
			olist = axp.selectNodes(xmldocument.getOMDocumentElement());
436
			this.logger.trace("XPath root expression is: '" + axp.debug()
422
			this.logger.debug("XPath '" + axp.toString() + "' has " + olist.size() + " matches.");
437
					+ "'.");
423
			this.logger.trace("XPath root expression is: '" + axp.debug() + "'.");
438
		} catch (JaxenException e) {
424
		} catch (JaxenException e) {
439
			throw new XServicesFault(e.getMessage(), e);
425
			throw new XServicesFault(e.getMessage(), e);
440
		}
-
 
Line 441... Line 426...
441
		if (olist.size() == 0)
426
		}
442
			throw new XServicesFault(Messages.getString("XmlService.no_match",
427
		if (olist.size() == 0)
Line 443... Line 428...
443
					new Object[] { axp.toString() }));
428
			throw new XServicesFault(Messages.getString("XmlService.no_match", new Object[] { axp.toString() }));
444
 
429
 
445
		// Prepare children to insert
430
		// Prepare children to insert
446
		xmlfragment.build();
431
		xmlfragment.build();
447
 
-
 
448
		// Determine what has been matched
432
 
449
		OMNode match = null;
433
		// Determine what has been matched
450
		for (Object o : olist) {
434
		OMNode match = null;
451
			Iterator<?> children = xmlfragment.getOMDocumentElement()
435
		for (Object o : olist) {
452
					.getChildren();
436
			Iterator<?> children = xmlfragment.getOMDocumentElement().getChildren();
Line 459... Line 443...
459
					if ((o instanceof OMDocument))
443
					if ((o instanceof OMDocument))
460
						match = ((OMDocument) o).getOMDocumentElement();
444
						match = ((OMDocument) o).getOMDocumentElement();
461
					this.logger.debug(Messages.getString("XmlService.8"));
445
					this.logger.debug(Messages.getString("XmlService.8"));
462
					break;
446
					break;
463
				default:
447
				default:
464
					this.logger.error("XPath matched "
448
					this.logger
465
							+ o.getClass().getCanonicalName() + " Node Type:"
449
							.error("XPath matched " + o.getClass().getCanonicalName() + " Node Type:" + node.getType());
466
							+ node.getType());
-
 
467
					this.logger.error(Messages.getString("XmlService.11"));
450
					this.logger.error(Messages.getString("XmlService.11"));
468
					throw new XServicesFault(
-
 
469
							Messages.getString("XmlService.12"));
451
					throw new XServicesFault(Messages.getString("XmlService.12"));
470
				}
452
				}
471
			} else {
453
			} else {
472
				this.logger.error("XPath matched "
-
 
473
						+ o.getClass().getCanonicalName());
454
				this.logger.error("XPath matched " + o.getClass().getCanonicalName());
474
				this.logger.error(Messages.getString("XmlService.11"));
455
				this.logger.error(Messages.getString("XmlService.11"));
475
				throw new XServicesFault(Messages.getString("XmlService.12"));
456
				throw new XServicesFault(Messages.getString("XmlService.12"));
476
			}
457
			}
Line 477... Line 458...
477
 
458
 
Line 489... Line 470...
489
		// Initialize XPath context
470
		// Initialize XPath context
490
		SimpleNamespaceContext context = new SimpleNamespaceContext();
471
		SimpleNamespaceContext context = new SimpleNamespaceContext();
491
		if(nsList != null) {
472
		if (nsList != null) {
492
			for (NamespaceType ns : nsList.getNamespaces()) {
473
			for (NamespaceType ns : nsList.getNamespaces()) {
493
				context.addNamespace(ns.getPrefix(), ns.getUri().toString());
474
				context.addNamespace(ns.getPrefix(), ns.getUri().toString());
494
				this.logger.debug(Messages.getString("XmlService.0")
475
				this.logger.debug(
495
						+ ns.getPrefix() + "=\"" + ns.getUri().toString()
476
						Messages.getString("XmlService.0") + ns.getPrefix() + "=\"" + ns.getUri().toString() + "\"'");
496
						+ "\"'");
-
 
497
			}
477
			}
498
		} else {
478
		} else {
499
			logger.debug("No namespaces defined.");
479
			logger.debug("No namespaces defined.");
500
		}
480
		}
501
		return context;
481
		return context;
502
	}
482
	}
Line 503... Line 483...
503
 
483
 
-
 
484
	private String validateEncoding(String encoding) throws XServicesFault {
504
	private String validateEncoding(String encoding) throws XServicesFault {
485
		if (encoding == null || encoding.equals("")) {
-
 
486
			encoding = Charset.defaultCharset().displayName();
505
		if(encoding == null || encoding.equals("")) { encoding=Charset.defaultCharset().displayName(); }
487
		}
506
		try { 
488
		try {
507
			Charset.isSupported(encoding);
489
			Charset.isSupported(encoding);
508
		} catch (IllegalCharsetNameException e) {
490
		} catch (IllegalCharsetNameException e) {
509
			throw new XServicesFault("Endcoding '"+encoding+"' is not supported by this JRE.");
491
			throw new XServicesFault("Endcoding '" + encoding + "' is not supported by this JRE.");
510
		}
492
		}
511
		logger.debug("Setting source xml string encoding to '"+encoding+"'");
493
		logger.debug("Setting source xml string encoding to '" + encoding + "'");
512
		return encoding;
494
		return encoding;
Line 513... Line -...
513
	}
-
 
514
	
495
	}