use of org.apache.cxf.jaxrs.model.ParameterType in project cxf by apache.
the class SwaggerParseUtils method getUserApplicationFromJson.
public static UserApplication getUserApplicationFromJson(String json, ParseConfiguration cfg) {
JsonMapObjectReaderWriter reader = new JsonMapObjectReaderWriter();
Map<String, Object> map = reader.fromJson(json);
UserApplication app = new UserApplication();
String relativePath = (String) map.get("basePath");
app.setBasePath(StringUtils.isEmpty(relativePath) ? "/" : relativePath);
Map<String, List<UserOperation>> userOpsMap = new LinkedHashMap<>();
Set<String> tags = new HashSet<>();
List<Map<String, Object>> tagsProp = CastUtils.cast((List<?>) map.get("tags"));
if (tagsProp != null) {
for (Map<String, Object> tagProp : tagsProp) {
tags.add((String) tagProp.get("name"));
}
} else {
tags.add("");
}
for (String tag : tags) {
userOpsMap.put(tag, new LinkedList<UserOperation>());
}
Map<String, Map<String, Object>> paths = CastUtils.cast((Map<?, ?>) map.get("paths"));
for (Map.Entry<String, Map<String, Object>> pathEntry : paths.entrySet()) {
String operPath = pathEntry.getKey();
Map<String, Object> operations = pathEntry.getValue();
for (Map.Entry<String, Object> operEntry : operations.entrySet()) {
UserOperation userOp = new UserOperation();
userOp.setVerb(operEntry.getKey().toUpperCase());
Map<String, Object> oper = CastUtils.cast((Map<?, ?>) operEntry.getValue());
// operations but Swagger may still include it.
if (oper != null) {
userOp.setPath(operPath);
userOp.setName((String) oper.get("operationId"));
List<String> opProduces = CastUtils.cast((List<?>) oper.get("produces"));
userOp.setProduces(listToString(opProduces));
List<String> opConsumes = CastUtils.cast((List<?>) oper.get("consumes"));
userOp.setConsumes(listToString(opConsumes));
List<Parameter> userOpParams = new LinkedList<>();
List<Map<String, Object>> params = CastUtils.cast((List<?>) oper.get("parameters"));
for (Map<String, Object> param : params) {
String name = (String) param.get("name");
// "query", "header", "path", "formData" or "body"
String paramType = (String) param.get("in");
ParameterType pType = "body".equals(paramType) ? ParameterType.REQUEST_BODY : "formData".equals(paramType) ? ParameterType.FORM : ParameterType.valueOf(paramType.toUpperCase());
Parameter userParam = new Parameter(pType, name);
setJavaType(userParam, (String) param.get("type"));
userOpParams.add(userParam);
}
if (!userOpParams.isEmpty()) {
userOp.setParameters(userOpParams);
}
List<String> opTags = CastUtils.cast((List<?>) oper.get("tags"));
if (opTags == null) {
opTags = Collections.singletonList("");
}
for (String opTag : opTags) {
userOpsMap.get(opTag).add(userOp);
}
}
}
}
List<UserResource> resources = new LinkedList<>();
for (Map.Entry<String, List<UserOperation>> entry : userOpsMap.entrySet()) {
UserResource ur = new UserResource();
ur.setPath("/");
ur.setOperations(entry.getValue());
ur.setName(entry.getKey());
resources.add(ur);
}
app.setResources(resources);
return app;
}
use of org.apache.cxf.jaxrs.model.ParameterType in project cxf by apache.
the class WadlGenerator method doWriteParam.
// CHECKSTYLE:OFF
protected void doWriteParam(OperationResourceInfo ori, StringBuilder sb, Parameter pm, Class<?> type, Type genericType, String paramName, Annotation[] anns, boolean isJson) {
// CHECKSTYLE:ON
ParameterType pType = pm.getType();
boolean isForm = isFormParameter(pm, type, anns);
if (paramName == null && isForm) {
Multipart m = AnnotationUtils.getAnnotation(anns, Multipart.class);
if (m != null) {
paramName = m.value();
}
}
sb.append("<param name=\"").append(paramName).append("\" ");
String style = ParameterType.PATH == pType ? "template" : isForm ? "query" : ParameterType.REQUEST_BODY == pType ? "plain" : pType.toString().toLowerCase();
sb.append("style=\"").append(style).append('"');
if (pm.getDefaultValue() != null) {
sb.append(" default=\"").append(xmlEncodeIfNeeded(pm.getDefaultValue())).append('"');
}
if (InjectionUtils.isSupportedCollectionOrArray(type)) {
type = InjectionUtils.getActualType(genericType);
sb.append(" repeating=\"true\"");
}
String value = XmlSchemaPrimitiveUtils.getSchemaRepresentation(type);
if (value == null) {
if (type.isEnum()) {
value = "xs:string";
} else if (type == InputStream.class) {
value = "xs:anyType";
}
}
if (value != null) {
if (isJson) {
value = value.substring(3);
}
sb.append(" type=\"").append(value).append('"');
}
if (type.isEnum()) {
sb.append('>');
handleDocs(anns, sb, DocTarget.PARAM, true, isJson);
setEnumOptions(sb, type);
sb.append("</param>");
} else {
addDocsAndCloseElement(ori, pm.getIndex(), sb, anns, "param", DocTarget.PARAM, true, isJson);
}
}
use of org.apache.cxf.jaxrs.model.ParameterType in project cxf by apache.
the class WadlGenerator method doHandleClassParams.
protected void doHandleClassParams(OperationResourceInfo ori, StringBuilder sb, Map<Parameter, Object> params, boolean isJson, ParameterType... pType) {
Set<ParameterType> pTypes = new LinkedHashSet<>(Arrays.asList(pType));
for (Map.Entry<Parameter, Object> entry : params.entrySet()) {
Parameter pm = entry.getKey();
Object obj = entry.getValue();
if (pTypes.contains(pm.getType())) {
Class<?> cls = obj instanceof Method ? ((Method) obj).getParameterTypes()[0] : ((Field) obj).getType();
Type type = obj instanceof Method ? ((Method) obj).getGenericParameterTypes()[0] : ((Field) obj).getGenericType();
Annotation[] ann = obj instanceof Method ? ((Method) obj).getParameterAnnotations()[0] : ((Field) obj).getAnnotations();
doWriteParam(ori, sb, pm, cls, type, pm.getName(), ann, isJson);
}
}
}
use of org.apache.cxf.jaxrs.model.ParameterType in project cxf by apache.
the class InjectionUtils method injectIntoMap.
private static Object injectIntoMap(Type genericType, Annotation[] paramAnns, MultivaluedMap<String, String> processedValues, boolean decoded, ParameterType pathParam, Message message) {
ParameterizedType paramType = (ParameterizedType) genericType;
Class<?> keyType = (Class<?>) paramType.getActualTypeArguments()[0];
Type secondType = InjectionUtils.getType(paramType.getActualTypeArguments(), 1);
if (secondType instanceof ParameterizedType) {
MultivaluedMap<Object, Object> theValues = new MetadataMap<>();
ParameterizedType valueParamType = (ParameterizedType) secondType;
Class<?> valueType = (Class<?>) InjectionUtils.getType(valueParamType.getActualTypeArguments(), 0);
for (Map.Entry<String, List<String>> processedValuesEntry : processedValues.entrySet()) {
List<String> valuesList = processedValuesEntry.getValue();
for (String value : valuesList) {
Object o = InjectionUtils.handleParameter(value, decoded, valueType, valueType, paramAnns, pathParam, message);
theValues.add(convertStringToPrimitive(processedValuesEntry.getKey(), keyType), o);
}
}
return theValues;
}
Map<Object, Object> theValues = new HashMap<>();
Class<?> valueType = (Class<?>) InjectionUtils.getType(paramType.getActualTypeArguments(), 1);
for (Map.Entry<String, List<String>> processedValuesEntry : processedValues.entrySet()) {
List<String> valuesList = processedValuesEntry.getValue();
for (String value : valuesList) {
Object o = InjectionUtils.handleParameter(value, decoded, valueType, valueType, paramAnns, pathParam, message);
theValues.put(convertStringToPrimitive(processedValuesEntry.getKey(), keyType), o);
}
}
return theValues;
}
use of org.apache.cxf.jaxrs.model.ParameterType in project cxf by apache.
the class InjectionUtils method handleBean.
public static Object handleBean(Class<?> paramType, Annotation[] paramAnns, MultivaluedMap<String, String> values, ParameterType pType, Message message, boolean decoded) {
Object bean = null;
try {
if (paramType.isInterface()) {
paramType = org.apache.cxf.jaxrs.utils.JAXBUtils.getValueTypeFromAdapter(paramType, paramType, paramAnns);
}
bean = paramType.newInstance();
} catch (IllegalAccessException ex) {
reportServerError("CLASS_ACCESS_FAILURE", paramType.getName());
} catch (Exception ex) {
reportServerError("CLASS_INSTANTIATION_FAILURE", paramType.getName());
}
Map<String, MultivaluedMap<String, String>> parsedValues = new HashMap<>();
for (Map.Entry<String, List<String>> entry : values.entrySet()) {
String memberKey = entry.getKey();
final String beanKey;
int idx = memberKey.indexOf('.');
if (idx == -1) {
beanKey = '.' + memberKey;
} else {
beanKey = memberKey.substring(0, idx);
memberKey = memberKey.substring(idx + 1);
}
MultivaluedMap<String, String> value = parsedValues.get(beanKey);
if (value == null) {
value = new MetadataMap<>();
parsedValues.put(beanKey, value);
}
value.put(memberKey, entry.getValue());
}
if (!parsedValues.isEmpty()) {
for (Map.Entry<String, MultivaluedMap<String, String>> entry : parsedValues.entrySet()) {
String memberKey = entry.getKey();
boolean isbean = !memberKey.startsWith(".");
if (!isbean) {
memberKey = memberKey.substring(1);
}
Object setter = null;
Object getter = null;
for (Method m : paramType.getMethods()) {
if (m.getName().equalsIgnoreCase("set" + memberKey) && m.getParameterTypes().length == 1) {
setter = m;
} else if (m.getName().equalsIgnoreCase("get" + memberKey) || isBooleanType(m.getReturnType()) && m.getName().equalsIgnoreCase("is" + memberKey)) {
getter = m;
}
if (setter != null && getter != null) {
break;
}
}
if (setter == null) {
for (Field f : paramType.getFields()) {
if (f.getName().equalsIgnoreCase(memberKey)) {
setter = f;
getter = f;
break;
}
}
}
if (setter != null && getter != null) {
final Class<?> type;
final Type genericType;
Object paramValue;
if (setter instanceof Method) {
type = Method.class.cast(setter).getParameterTypes()[0];
genericType = Method.class.cast(setter).getGenericParameterTypes()[0];
paramValue = InjectionUtils.extractFromMethod(bean, (Method) getter);
} else {
type = Field.class.cast(setter).getType();
genericType = Field.class.cast(setter).getGenericType();
paramValue = InjectionUtils.extractFieldValue((Field) getter, bean);
}
List<MultivaluedMap<String, String>> processedValuesList = processValues(type, genericType, entry.getValue(), isbean);
for (MultivaluedMap<String, String> processedValues : processedValuesList) {
if (InjectionUtils.isSupportedCollectionOrArray(type)) {
Object appendValue = InjectionUtils.injectIntoCollectionOrArray(type, genericType, paramAnns, processedValues, isbean, true, pType, message);
paramValue = InjectionUtils.mergeCollectionsOrArrays(paramValue, appendValue, genericType);
} else if (isSupportedMap(genericType)) {
Object appendValue = injectIntoMap(genericType, paramAnns, processedValues, true, pType, message);
paramValue = mergeMap(paramValue, appendValue);
} else if (isbean) {
paramValue = InjectionUtils.handleBean(type, paramAnns, processedValues, pType, message, decoded);
} else {
paramValue = InjectionUtils.handleParameter(processedValues.values().iterator().next().get(0), decoded, type, type, paramAnns, pType, message);
}
if (paramValue != null) {
if (setter instanceof Method) {
InjectionUtils.injectThroughMethod(bean, (Method) setter, paramValue);
} else {
InjectionUtils.injectFieldValue((Field) setter, bean, paramValue);
}
}
}
}
}
}
return bean;
}
Aggregations