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;
018
019import java.lang.reflect.Field;
020
021import org.apache.camel.spi.UriParam;
022import org.apache.camel.util.CollectionStringBuffer;
023import org.apache.camel.util.JsonSchemaHelper;
024
025import static org.apache.camel.util.StringQuoteHelper.doubleQuote;
026
027/**
028 * Represents the configuration of a URI query parameter value to allow type conversion
029 * and better validation of the configuration of URIs and Endpoints
030 */
031@Deprecated
032public class ParameterConfiguration {
033    private final String name;
034    private final Class<?> parameterType;
035
036    public ParameterConfiguration(String name, Class<?> parameterType) {
037        this.name = name;
038        this.parameterType = parameterType;
039    }
040
041    @Override
042    public String toString() {
043        return "ParameterConfiguration[" + name + " on " + parameterType + "]";
044    }
045
046    /**
047     * Returns the name of the parameter value
048     */
049    public String getName() {
050        return name;
051    }
052
053    /**
054     * Returns the type of the parameter value
055     */
056    public Class<?> getParameterType() {
057        return parameterType;
058    }
059
060    /**
061     * Factory method to create a new ParameterConfiguration from a field
062     */
063    public static ParameterConfiguration newInstance(String name, Field field, UriParam uriParam) {
064        return new AnnotatedParameterConfiguration(name, field.getType(), field);
065    }
066
067    /**
068     * Returns the JSON format of this parameter configuration
069     */
070    public String toJson() {
071        if (parameterType.isEnum()) {
072            String typeName = "string";
073            CollectionStringBuffer sb = new CollectionStringBuffer();
074            for (Object value : parameterType.getEnumConstants()) {
075                sb.append(doubleQuote(value.toString()));
076            }
077            return doubleQuote(name) + ": { \"type\": " + doubleQuote(typeName)
078                    + ", \"javaType\": \"" + parameterType.getCanonicalName() + "\""
079                    + ", \"enum\": [ " + sb.toString() + " ] }";
080        } else if (parameterType.isArray()) {
081            String typeName = "array";
082            return doubleQuote(name) + ": { \"type\": " + doubleQuote(typeName)
083                    + ", \"javaType\": \"" + parameterType.getCanonicalName() + "\" }";
084        } else {
085            String typeName = JsonSchemaHelper.getType(parameterType);
086            return doubleQuote(name) + ": { \"type\": " + doubleQuote(typeName)
087                    + ", \"javaType\": \"" + parameterType.getCanonicalName() + "\" }";
088        }
089    }
090}