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