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 */ 017package org.apache.camel.impl.validator; 018 019import org.apache.camel.CamelContext; 020import org.apache.camel.Exchange; 021import org.apache.camel.Message; 022import org.apache.camel.Processor; 023import org.apache.camel.ValidationException; 024import org.apache.camel.impl.DefaultExchange; 025import org.apache.camel.spi.DataType; 026import org.apache.camel.spi.Validator; 027import org.apache.camel.util.ObjectHelper; 028import org.apache.camel.util.ServiceHelper; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032/** 033 * A {@link Validator} implementation which leverages {@link Processor} to perform validation. 034 * 035 * {@see Validator} 036 */ 037public class ProcessorValidator extends Validator { 038 private static final Logger LOG = LoggerFactory.getLogger(ProcessorValidator.class); 039 040 private Processor processor; 041 private String validatorString; 042 043 public ProcessorValidator(CamelContext context) { 044 setCamelContext(context); 045 } 046 047 /** 048 * Perform content validation with specified type using Processor. 049 * 050 * @param message message to apply validation 051 * @param type 'from' data type 052 */ 053 @Override 054 public void validate(Message message, DataType type) throws ValidationException { 055 Exchange exchange = message.getExchange(); 056 057 LOG.debug("Sending to validate processor '{}'", processor); 058 // create a new exchange to use during validation to avoid side-effects on original exchange 059 DefaultExchange validateExchange = new DefaultExchange(exchange); 060 validateExchange.setIn(message); 061 validateExchange.setProperties(exchange.getProperties()); 062 try { 063 processor.process(validateExchange); 064 065 // if the validation failed then propagate the exception 066 if (validateExchange.getException() != null) { 067 exchange.setException(validateExchange.getException()); 068 } 069 070 } catch (Exception e) { 071 if (e instanceof ValidationException) { 072 throw (ValidationException)e; 073 } else { 074 throw new ValidationException(String.format("Validation failed for '%s'", type), exchange, e); 075 } 076 } 077 } 078 079 /** 080 * Set processor to use 081 * 082 * @param processor Processor 083 * @return this ProcessorTransformer instance 084 */ 085 public ProcessorValidator setProcessor(Processor processor) { 086 this.processor = processor; 087 this.validatorString = null; 088 return this; 089 } 090 091 @Override 092 public String toString() { 093 if (validatorString == null) { 094 validatorString = 095 String.format("ProcessorValidator[type='%s', processor='%s']", getType(), processor); 096 } 097 return validatorString; 098 } 099 100 @Override 101 protected void doStart() throws Exception { 102 ObjectHelper.notNull(processor, "processor", this); 103 ServiceHelper.startService(this.processor); 104 } 105 106 @Override 107 protected void doStop() throws Exception { 108 ServiceHelper.stopService(this.processor); 109 } 110}