/* Copyright 2013 Nationale-Nederlanden Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package nl.nn.adapterframework.senders; import nl.nn.adapterframework.configuration.ConfigurationException; import nl.nn.adapterframework.core.IPipeLineSession; import nl.nn.adapterframework.core.ISenderWithParameters; import nl.nn.adapterframework.core.SenderException; import nl.nn.adapterframework.core.TimeOutException; import nl.nn.adapterframework.parameters.Parameter; import nl.nn.adapterframework.parameters.ParameterResolutionContext; import nl.nn.adapterframework.validation.AbstractXmlValidator; import nl.nn.adapterframework.validation.XercesXmlValidator; /** *<code>Sender</code> that validates the input message against a XML-Schema. * * <p><b>Configuration:</b> * <table border="1"> * <tr><th>attributes</th><th>description</th><th>default</th></tr> * <tr><td>className</td><td>nl.nn.adapterframework.pipes.XmlValidator</td><td> </td></tr> * <tr><td>{@link #setSchema(String) schema}</td><td>The filename of the schema on the classpath. See doc on the method. (effectively the same as noNamespaceSchemaLocation)</td><td> </td></tr> * <tr><td>{@link #setNoNamespaceSchemaLocation(String) noNamespaceSchemaLocation}</td><td>A URI reference as a hint as to the location of a schema document with no target namespace. See doc on the method.</td><td> </td></tr> * <tr><td>{@link #setSchemaLocation(String) schemaLocation}</td><td>Pairs of URI references (one for the namespace name, and one for a hint as to the location of a schema document defining names for that namespace name). See doc on the method.</td><td> </td></tr> * <tr><td>{@link #setSchemaSessionKey(String) schemaSessionKey}</td><td> </td><td> </td></tr> * <tr><td>{@link #setFullSchemaChecking(boolean) fullSchemaChecking}</td><td>Perform addional memory intensive checks</td><td><code>false</code></td></tr> * <tr><td>{@link #setThrowException(boolean) throwException}</td><td>Should the XmlValidator throw a PipeRunException on a validation error (if not, a forward with name "failure" should be defined.</td><td><code>false</code></td></tr> * <tr><td>{@link #setReasonSessionKey(String) reasonSessionKey}</td><td>if set: key of session variable to store reasons of mis-validation in</td><td>failureReason</td></tr> * <tr><td>{@link #setXmlReasonSessionKey(String) xmlReasonSessionKey}</td><td>like <code>reasonSessionKey</code> but stores reasons in xml format and more extensive</td><td>xmlFailureReason</td></tr> * <tr><td>{@link #setRoot(String) root}</td><td>name of the root element</td><td> </td></tr> * <tr><td>{@link #setValidateFile(boolean) validateFile}</td><td>when set <code>true</code>, the input is assumed to be the name of the file to be validated. Otherwise the input itself is validated</td><td><code>false</code></td></tr> * <tr><td>{@link #setCharset(String) charset}</td><td>characterset used for reading file, only used when {@link #setValidateFile(boolean) validateFile} is <code>true</code></td><td>UTF-8</td></tr> * </table> * <br> * N.B. noNamespaceSchemaLocation may contain spaces, but not if the schema is stored in a .jar or .zip file on the class path. * * @author Gerrit van Brakel * @since */ public class XmlValidatorSender extends XercesXmlValidator implements ISenderWithParameters { private String name; public void configure() throws ConfigurationException { configure(getLogPrefix()); } public void close() throws SenderException { } public void open() throws SenderException { } public String sendMessage(String correlationID, String message) throws SenderException, TimeOutException { return sendMessage(correlationID,message,null); } public void addParameter(Parameter p) { } public String sendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException { IPipeLineSession session=prc.getSession(); String fullReasons="tja"; try { String resultEvent = validate(message, session, getLogPrefix()); if (AbstractXmlValidator.XML_VALIDATOR_VALID_MONITOR_EVENT.equals(resultEvent)) { return message; } fullReasons = resultEvent; // TODO: find real fullReasons if (isThrowException()) { throw new SenderException(fullReasons); } log.warn(fullReasons); return message; } catch (Exception e) { if (isThrowException()) { throw new SenderException(e); } log.warn(fullReasons, e); return message; } } public boolean isSynchronous() { return true; } protected String getLogPrefix() { return "["+this.getClass().getName()+"] ["+getName()+"] "; } public String getName() { return name; } public void setName(String name) { this.name=name; } }