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 DefaultExchange validateExchange = new DefaultExchange(exchange); 059 validateExchange.setIn(message); 060 validateExchange.setProperties(exchange.getProperties()); 061 try { 062 processor.process(validateExchange); 063 } catch (Exception e) { 064 if (e instanceof ValidationException) { 065 throw (ValidationException)e; 066 } else { 067 throw new ValidationException(String.format("Validation failed for '%s'", type), validateExchange, e); 068 } 069 } 070 } 071 072 /** 073 * Set processor to use 074 * 075 * @param processor Processor 076 * @return this ProcessorTransformer instance 077 */ 078 public ProcessorValidator setProcessor(Processor processor) { 079 this.processor = processor; 080 this.validatorString = null; 081 return this; 082 } 083 084 @Override 085 public String toString() { 086 if (validatorString == null) { 087 validatorString = 088 String.format("ProcessorValidator[type='%s', processor='%s']", getType(), processor); 089 } 090 return validatorString; 091 } 092 093 @Override 094 protected void doStart() throws Exception { 095 ObjectHelper.notNull(processor, "processor", this); 096 ServiceHelper.startService(this.processor); 097 } 098 099 @Override 100 protected void doStop() throws Exception { 101 ServiceHelper.stopService(this.processor); 102 } 103}