I need to update my previous post as I have noticed a bug. The code that I
posted does not distinguish between validation errors in the W3C schema and
errors in the XML request entity submitted by the user. Obviously the first
problem should return an HTTP response with a 5XX error status and the latter a
4XX error status.
I have have changed the file named Base.java that I posted above. I replaced
the try/catch block that contained the line "requestEntity.validate(xsd);" with
the following:
final StreamSource schemaSource = new StreamSource(xsd.getStream());
schemaSource.setSystemId("schema");
final InputSource entitySource = new
InputSource(requestEntity.getStream());
entitySource.setSystemId("entity");
try {
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaSource)
.newValidator().validate(new SAXSource(entitySource));
} catch (Exception e) {
if (e instanceof org.xml.sax.SAXParseException) {
SAXParseException spe = (SAXParseException)e;
StringBuffer message = new StringBuffer("Invalid XML at ");
message.append("line " + spe.getLineNumber() + " column of "+
spe.getColumnNumber());
if (spe.getSystemId().matches(".*schema")) {
message.append(" schema");
throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
message.toString(), e);
} else {
message.append(" HTTP request entity");
throw new
ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY,
message.toString(), e);
}
} else {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "XML
validation FAIL", e);
}
}
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2973914