001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.validator;
018    
019    import java.io.InputStream;
020    import java.util.Map;
021    
022    import org.w3c.dom.ls.LSResourceResolver;
023    
024    import org.apache.camel.Endpoint;
025    import org.apache.camel.converter.IOConverter;
026    import org.apache.camel.impl.DefaultComponent;
027    import org.apache.camel.impl.ProcessorEndpoint;
028    import org.apache.camel.processor.validation.ValidatingProcessor;
029    import org.apache.camel.util.IOHelper;
030    import org.apache.camel.util.ResourceHelper;
031    import org.slf4j.Logger;
032    import org.slf4j.LoggerFactory;
033    
034    /**
035     * The <a href="http://camel.apache.org/validation.html">Validator Component</a>
036     * for validating XML against some schema
037     */
038    public class ValidatorComponent extends DefaultComponent {
039    
040        private static final Logger LOG = LoggerFactory.getLogger(ValidatorComponent.class);
041    
042        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
043            final String resourceUri = remaining;
044            InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), resourceUri);
045            byte[] bytes = null;
046            try {
047                bytes = IOConverter.toBytes(is);
048            } finally {
049                // and make sure to close the input stream after the schema has been loaded
050                IOHelper.close(is);
051            }
052    
053            ValidatingProcessor validator = new ValidatingProcessor();
054            validator.setSchemaAsByteArray(bytes);
055            LOG.debug("{} using schema resource: {}", this, resourceUri);
056            configureValidator(validator, uri, remaining, parameters);
057    
058            // force loading of schema at create time otherwise concurrent
059            // processing could cause thread safe issues for the javax.xml.validation.SchemaFactory
060            validator.loadSchema();
061    
062            return new ProcessorEndpoint(uri, this, validator);
063        }
064    
065        protected void configureValidator(ValidatingProcessor validator, String uri, String remaining, Map<String, Object> parameters) throws Exception {
066            LSResourceResolver resourceResolver = resolveAndRemoveReferenceParameter(parameters, "resourceResolver", LSResourceResolver.class);
067            if (resourceResolver != null) {
068                validator.setResourceResolver(resourceResolver);
069            } else {
070                validator.setResourceResolver(new DefaultLSResourceResolver(getCamelContext(), remaining));
071            }
072    
073            setProperties(validator, parameters);
074        }
075    }