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 javax.xml.transform.stream.StreamSource;
023    
024    import org.w3c.dom.ls.LSResourceResolver;
025    
026    import org.apache.camel.Endpoint;
027    import org.apache.camel.impl.DefaultComponent;
028    import org.apache.camel.impl.ProcessorEndpoint;
029    import org.apache.camel.processor.validation.ValidatingProcessor;
030    import org.apache.camel.util.IOHelper;
031    import org.apache.camel.util.ResourceHelper;
032    import org.slf4j.Logger;
033    import org.slf4j.LoggerFactory;
034    
035    /**
036     * The <a href="http://camel.apache.org/validation.html">Validator Component</a>
037     * for validating XML against some schema
038     */
039    public class ValidatorComponent extends DefaultComponent {
040    
041        private static final transient Logger LOG = LoggerFactory.getLogger(ValidatorComponent.class);
042    
043        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
044            final String resourceUri = remaining;
045            InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), resourceUri);
046            StreamSource source = new StreamSource(is);
047    
048            ValidatingProcessor validator = new ValidatingProcessor();
049            validator.setSchemaSource(source);
050            LOG.debug("{} using schema resource: {}", this, resourceUri);
051            configureValidator(validator, uri, remaining, parameters);
052    
053            // force loading of schema at create time otherwise concurrent
054            // processing could cause thread safe issues for the javax.xml.validation.SchemaFactory
055            validator.loadSchema();
056            // and make sure to close the input stream after the schema has been loaded
057            IOHelper.close(is);
058    
059            return new ProcessorEndpoint(uri, this, validator);
060        }
061    
062        protected void configureValidator(ValidatingProcessor validator, String uri, String remaining, Map<String, Object> parameters) throws Exception {
063            LSResourceResolver resourceResolver = resolveAndRemoveReferenceParameter(parameters, "resourceResolver", LSResourceResolver.class);
064            if (resourceResolver != null) {
065                validator.setResourceResolver(resourceResolver);
066            } else {
067                validator.setResourceResolver(new DefaultLSResourceResolver(getCamelContext(), remaining));
068            }
069    
070            setProperties(validator, parameters);
071        }
072    }