javaxsdjax-rsgetresourceweb-content

Access xsd file in WebContent folder from a MessageBodyReader class


Inside a JAX-RS service I wan't to provide a XmlValidatonReader which validates incoming XML documents against an exising xsd file which is located in the WebContent directory.

enter image description here

The xsd file needs to be inside the WebContent folder because it should be accessible to the client. In order to validate the incoming XML file I need the xsd resource: /WebContent/Category.xsd

How can I load this from src/at/fhj/ase/XmlValidationReader.java


Solution

  • So I brought it to run.

    @Provider
    @Consumes(MediaType.APPLICATION_XML)
    public class XmlValidationReader implements MessageBodyReader<Category> {
    
        protected Providers providers;
        protected ServletContext servletContext;
        private final String xsdFileName = "/Category.xsd";
        private Schema schema;
    
        public XmlValidationReader(@Context Providers providers,
                @Context ServletContext servletContext) {
            this.providers = providers;
            this.servletContext = servletContext;
    
            try {
                SchemaFactory sf = SchemaFactory
                        .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                File xsd = new File(servletContext.getRealPath(xsdFileName));
                schema = sf.newSchema(xsd);
            } catch (Exception e) {
                throw new RuntimeException(
                        "Unable to create XSD validation schema", e);
            }
        }
    
        @Override
        public boolean isReadable(Class<?> type, Type genericType,
                Annotation[] annotations, MediaType mediaType) {
            ...
        }
    
    
        @Override
        public Category readFrom(Class<Category> type, Type genericType,
                Annotation[] annotations, MediaType mediaType,
                MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
                throws IOException, WebApplicationException {
            ...
        }