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 */ 031public class ParameterConfiguration { 032 private final String name; 033 private final Class<?> parameterType; 034 035 public ParameterConfiguration(String name, Class<?> parameterType) { 036 this.name = name; 037 this.parameterType = parameterType; 038 } 039 040 @Override 041 public String toString() { 042 return "ParameterConfiguration[" + name + " on " + parameterType + "]"; 043 } 044 045 /** 046 * Returns the name of the parameter value 047 */ 048 public String getName() { 049 return name; 050 } 051 052 /** 053 * Returns the type of the parameter value 054 */ 055 public Class<?> getParameterType() { 056 return parameterType; 057 } 058 059 /** 060 * Factory method to create a new ParameterConfiguration from a field 061 */ 062 public static ParameterConfiguration newInstance(String name, Field field, UriParam uriParam) { 063 return new AnnotatedParameterConfiguration(name, field.getType(), field); 064 } 065 066 /** 067 * Returns the JSON format of this parameter configuration 068 */ 069 public String toJson() { 070 if (parameterType.isEnum()) { 071 String typeName = "string"; 072 CollectionStringBuffer sb = new CollectionStringBuffer(); 073 for (Object value : parameterType.getEnumConstants()) { 074 sb.append(doubleQuote(value.toString())); 075 } 076 return doubleQuote(name) + ": { \"type\": " + doubleQuote(typeName) + ", \"enum\": [ " + sb.toString() + " ] }"; 077 } else if (parameterType.isArray()) { 078 String typeName = "array"; 079 return doubleQuote(name) + ": { \"type\": " + doubleQuote(typeName) + " }"; 080 } else { 081 String typeName = JsonSchemaHelper.getType(parameterType); 082 if ("object".equals(typeName)) { 083 // for object then include the javaType as a description so we know that 084 return doubleQuote(name) + ": { \"type\": " + doubleQuote(typeName) 085 + ", \"properties\": { \"javaType\": { \"description\": \"" + parameterType.getCanonicalName() + "\", \"type\": \"string\" } } }"; 086 } else { 087 return doubleQuote(name) + ": { \"type\": " + doubleQuote(typeName) + " }"; 088 } 089 } 090 } 091}