Subversion Repositories XServices

Compare Revisions

Regard whitespace Rev 177 → Rev 185

/xservices/trunk/src/java/net/brutex/xservices/ws/impl/XmlServiceImpl.java
26,17 → 26,12
import java.nio.charset.IllegalCharsetNameException;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
 
import javax.jws.WebService;
import javax.ws.rs.NotAuthorizedException;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
 
import net.brutex.xservices.security.DirectoryPermission;
import net.brutex.xservices.types.AttributeType;
import net.brutex.xservices.types.NamespaceListType;
import net.brutex.xservices.types.NamespaceType;
58,9 → 53,9
import org.apache.axiom.om.OMText;
import org.apache.axiom.om.OMXMLBuilderFactory;
import org.apache.axiom.om.xpath.AXIOMXPath;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthorizedException;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jaxen.JaxenException;
import org.jaxen.SimpleNamespaceContext;
71,18 → 66,17
*/
@WebService(targetNamespace = "http://ws.xservices.brutex.net", endpointInterface = "net.brutex.xservices.ws.XmlService", serviceName = "XmlService")
public class XmlServiceImpl implements XmlService {
final Logger logger = Logger.getLogger(XmlServiceImpl.class);
private static final Logger logger = LogManager.getLogger();
 
public String insertNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
public String insertNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment)
throws XServicesFault {
try {
AXIOMXPath axp = new AXIOMXPath(xpath);
InputStream is = res.getAntResource(null).getInputStream();
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is)
.getDocument();
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
OMDocument fragdoc = null;
if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
fragdoc = OMXMLBuilderFactory.createOMBuilder(
new StringReader("<XS>" + xmlFragment + "</XS>"))
fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
.getDocument();
} else {
throw new XServicesFault("No xmldata to insert.");
114,7 → 108,8
}
}
 
public String replaceNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
public String replaceNodesFromFile(FileResource res, NamespaceListType nsList, String xpath, String xmlFragment)
throws XServicesFault {
try {
AXIOMXPath axp = new AXIOMXPath(xpath);
InputStream is = res.getAntResource(null).getInputStream();
121,8 → 116,7
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
OMDocument fragdoc = null;
if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
fragdoc = OMXMLBuilderFactory.createOMBuilder(
new StringReader("<XS>" + xmlFragment + "</XS>"))
fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
.getDocument();
} else {
throw new XServicesFault("No xmldata to insert.");
154,17 → 148,16
}
}
 
public String replaceNodes(String source, String encoding, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
public String replaceNodes(String source, String encoding, NamespaceListType nsList, String xpath,
String xmlFragment) throws XServicesFault {
encoding = validateEncoding(encoding);
try {
AXIOMXPath axp = new AXIOMXPath(xpath);
InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is)
.getDocument();
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
OMDocument fragdoc = null;
if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
fragdoc = OMXMLBuilderFactory.createOMBuilder(
new StringReader("<XS>" + xmlFragment + "</XS>"))
fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
.getDocument();
} else {
throw new XServicesFault("No xmldata to insert.");
196,17 → 189,16
}
@RequiresPermissions("insertNodes")
public String insertNodes(String source, String encoding, NamespaceListType nsList, String xpath, String xmlFragment) throws XServicesFault {
public String insertNodes(String source, String encoding, NamespaceListType nsList, String xpath,
String xmlFragment) throws XServicesFault {
encoding = validateEncoding(encoding);
try {
AXIOMXPath axp = new AXIOMXPath(xpath);
InputStream is = new ByteArrayInputStream(source.getBytes(encoding));
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is)
.getDocument();
OMDocument sourcedoc = OMXMLBuilderFactory.createOMBuilder(is).getDocument();
OMDocument fragdoc = null;
if ((xmlFragment != null) && (new String(xmlFragment).length() > 0)) {
fragdoc = OMXMLBuilderFactory.createOMBuilder(
new StringReader("<XS>" + xmlFragment + "</XS>"))
fragdoc = OMXMLBuilderFactory.createOMBuilder(new StringReader("<XS>" + xmlFragment + "</XS>"))
.getDocument();
} else {
throw new XServicesFault("No xmldata to insert.");
243,7 → 235,8
for(int i=0; i<tokens.length; i++) {
result += tokens[i];
if (i+1 < tokens.length ) result += "]]]]><![CDATA[>";
if (i + 1 < tokens.length)
result += "]]]]><![CDATA[>";
}
result = "<![CDATA[" + result + "]]>";
250,7 → 243,8
return result;
}
 
public StringSplitType selectXPath(String source, String encoding, NamespaceListType nsList, String xpath) throws XServicesFault {
public StringSplitType selectXPath(String source, String encoding, NamespaceListType nsList, String xpath)
throws XServicesFault {
encoding = validateEncoding(encoding);
try {
StringSplitType rarray = new StringSplitType();
293,7 → 287,6
rarray.addStringMatch(text);
}
 
return rarray;
} catch (JaxenException e) {
e.printStackTrace();
306,7 → 299,8
}
}
public String setAttribute(String source, String encoding, NamespaceListType nsList, String xpath, AttributeType attr) throws XServicesFault {
public String setAttribute(String source, String encoding, NamespaceListType nsList, String xpath,
AttributeType attr) throws XServicesFault {
encoding = validateEncoding(encoding);
try {
StringSplitType rarray = new StringSplitType();
358,20 → 352,18
}
}
private OMDocument insertNodes(OMDocument xmldocument, AXIOMXPath axp,OMDocument xmlfragment) throws XServicesFault {
private OMDocument insertNodes(OMDocument xmldocument, AXIOMXPath axp, OMDocument xmlfragment)
throws XServicesFault {
List<?> olist = null;
try {
olist = axp.selectNodes(xmldocument.getOMDocumentElement());
this.logger.debug("XPath '" + axp.toString() + "' has "
+ olist.size() + " matches.");
this.logger.trace("XPath root expression is: '" + axp.debug()
+ "'.");
this.logger.debug("XPath '" + axp.toString() + "' has " + olist.size() + " matches.");
this.logger.trace("XPath root expression is: '" + axp.debug() + "'.");
} catch (JaxenException e) {
throw new XServicesFault(e.getMessage(), e);
}
if (olist.size() == 0)
throw new XServicesFault(Messages.getString("XmlService.no_match",
new Object[] { axp.toString() }));
throw new XServicesFault(Messages.getString("XmlService.no_match", new Object[] { axp.toString() }));
 
// Prepare children to insert
xmlfragment.build();
379,8 → 371,7
// Determine what has been matched
OMContainer match = null;
for (Object o : olist) {
Iterator<?> children = xmlfragment.getOMDocumentElement()
.getChildren();
Iterator<?> children = xmlfragment.getOMDocumentElement().getChildren();
if ((o instanceof OMNode)) {
OMNode node = (OMNode) o;
switch (node.getType()) {
401,16 → 392,13
this.logger.debug(Messages.getString("XmlService.10"));
break;
default:
this.logger.error("XPath matched "
+ o.getClass().getCanonicalName() + " Node Type:"
+ node.getType());
this.logger
.error("XPath matched " + o.getClass().getCanonicalName() + " Node Type:" + node.getType());
this.logger.error(Messages.getString("XmlService.11"));
throw new XServicesFault(
Messages.getString("XmlService.12"));
throw new XServicesFault(Messages.getString("XmlService.12"));
}
} else {
this.logger.error("XPath matched "
+ o.getClass().getCanonicalName());
this.logger.error("XPath matched " + o.getClass().getCanonicalName());
this.logger.error(Messages.getString("XmlService.11"));
throw new XServicesFault(Messages.getString("XmlService.12"));
}
425,22 → 413,19
return xmldocument;
}
 
private OMDocument replaceNodes(OMDocument xmldocument, AXIOMXPath axp, OMDocument xmlfragment)
throws XServicesFault {
 
private OMDocument replaceNodes(OMDocument xmldocument, AXIOMXPath axp, OMDocument xmlfragment) throws XServicesFault {
List<?> olist = null;
try {
olist = axp.selectNodes(xmldocument.getOMDocumentElement());
this.logger.debug("XPath '" + axp.toString() + "' has "
+ olist.size() + " matches.");
this.logger.trace("XPath root expression is: '" + axp.debug()
+ "'.");
this.logger.debug("XPath '" + axp.toString() + "' has " + olist.size() + " matches.");
this.logger.trace("XPath root expression is: '" + axp.debug() + "'.");
} catch (JaxenException e) {
throw new XServicesFault(e.getMessage(), e);
}
if (olist.size() == 0)
throw new XServicesFault(Messages.getString("XmlService.no_match",
new Object[] { axp.toString() }));
throw new XServicesFault(Messages.getString("XmlService.no_match", new Object[] { axp.toString() }));
 
// Prepare children to insert
xmlfragment.build();
448,8 → 433,7
// Determine what has been matched
OMNode match = null;
for (Object o : olist) {
Iterator<?> children = xmlfragment.getOMDocumentElement()
.getChildren();
Iterator<?> children = xmlfragment.getOMDocumentElement().getChildren();
if ((o instanceof OMNode)) {
OMNode node = (OMNode) o;
switch (node.getType()) {
461,16 → 445,13
this.logger.debug(Messages.getString("XmlService.8"));
break;
default:
this.logger.error("XPath matched "
+ o.getClass().getCanonicalName() + " Node Type:"
+ node.getType());
this.logger
.error("XPath matched " + o.getClass().getCanonicalName() + " Node Type:" + node.getType());
this.logger.error(Messages.getString("XmlService.11"));
throw new XServicesFault(
Messages.getString("XmlService.12"));
throw new XServicesFault(Messages.getString("XmlService.12"));
}
} else {
this.logger.error("XPath matched "
+ o.getClass().getCanonicalName());
this.logger.error("XPath matched " + o.getClass().getCanonicalName());
this.logger.error(Messages.getString("XmlService.11"));
throw new XServicesFault(Messages.getString("XmlService.12"));
}
491,9 → 472,8
if(nsList != null) {
for (NamespaceType ns : nsList.getNamespaces()) {
context.addNamespace(ns.getPrefix(), ns.getUri().toString());
this.logger.debug(Messages.getString("XmlService.0")
+ ns.getPrefix() + "=\"" + ns.getUri().toString()
+ "\"'");
this.logger.debug(
Messages.getString("XmlService.0") + ns.getPrefix() + "=\"" + ns.getUri().toString() + "\"'");
}
} else {
logger.debug("No namespaces defined.");
502,7 → 482,9
}
 
private String validateEncoding(String encoding) throws XServicesFault {
if(encoding == null || encoding.equals("")) { encoding=Charset.defaultCharset().displayName(); }
if (encoding == null || encoding.equals("")) {
encoding = Charset.defaultCharset().displayName();
}
try {
Charset.isSupported(encoding);
} catch (IllegalCharsetNameException e) {
512,5 → 494,4
return encoding;
}
 
}