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.spi; 018 019import org.apache.camel.CamelContext; 020import org.apache.camel.CamelContextAware; 021import org.apache.camel.Message; 022import org.apache.camel.ValidationException; 023import org.apache.camel.support.service.ServiceSupport; 024 025/** 026 * <a href="http://camel.apache.org/validator.html">Validator</a> 027 * performs message content validation according to the declared data type. 028 * {@link org.apache.camel.processor.ContractAdvice} applies Validator if input/output type is declared on 029 * a route with validation enabled. 030 * 031 * @see {@link org.apache.camel.processor.ContractAdvice} 032 * {@link org.apache.camel.model.InputTypeDefinition} {@link org.apache.camel.model.OutputTypeDefinition} 033 */ 034public abstract class Validator extends ServiceSupport implements CamelContextAware { 035 036 private CamelContext camelContext; 037 private DataType type; 038 039 /** 040 * Perform data validation with specified type. 041 * 042 * @param message message to apply validation 043 * @param type the data type 044 * @throws ValidationException thrown if any validation error is detected 045 */ 046 public abstract void validate(Message message, DataType type) throws ValidationException; 047 048 /** 049 * Get 'from' data type. 050 */ 051 public DataType getType() { 052 return type; 053 } 054 055 /** 056 * Set data type. 057 * 058 * @param type data type 059 */ 060 public Validator setType(String type) { 061 this.type = new DataType(type); 062 return this; 063 } 064 065 @Override 066 public CamelContext getCamelContext() { 067 return this.camelContext; 068 } 069 070 @Override 071 public void setCamelContext(CamelContext context) { 072 this.camelContext = context; 073 } 074 075 @Override 076 public String toString() { 077 return String.format("%s[type='%s']", this.getClass().getSimpleName(), type); 078 } 079 080 @Override 081 protected void doStart() throws Exception { 082 // no-op 083 } 084 085 @Override 086 protected void doStop() throws Exception { 087 // no-op 088 } 089}