use of org.apache.cxf.jaxrs.model.ParameterType in project tomee by apache.
the class ClientProxyImpl method getParametersInfo.
protected MultivaluedMap<ParameterType, Parameter> getParametersInfo(Method m, Object[] params, OperationResourceInfo ori) {
MultivaluedMap<ParameterType, Parameter> map = new MetadataMap<>();
List<Parameter> parameters = ori.getParameters();
if (parameters.isEmpty()) {
return map;
}
int requestBodyParam = 0;
int multipartParam = 0;
for (Parameter p : parameters) {
if (isIgnorableParameter(m, p)) {
continue;
}
if (p.getType() == ParameterType.REQUEST_BODY) {
requestBodyParam++;
if (getMultipart(ori, p.getIndex()) != null) {
multipartParam++;
}
}
map.add(p.getType(), p);
}
if (map.containsKey(ParameterType.REQUEST_BODY)) {
if (requestBodyParam > 1 && requestBodyParam != multipartParam) {
reportInvalidResourceMethod(ori.getMethodToInvoke(), "SINGLE_BODY_ONLY");
}
if (map.containsKey(ParameterType.FORM)) {
reportInvalidResourceMethod(ori.getMethodToInvoke(), "ONLY_FORM_ALLOWED");
}
}
return map;
}
use of org.apache.cxf.jaxrs.model.ParameterType in project tomee by apache.
the class ClientProxyImpl method invoke.
/**
* Updates the current state if Client method is invoked, otherwise
* does the remote invocation or returns a new proxy if subresource
* method is invoked. Can throw an expected exception if ResponseExceptionMapper
* is registered
*/
@Override
public Object invoke(Object o, Method m, Object[] params) throws Throwable {
checkClosed();
Class<?> declaringClass = m.getDeclaringClass();
if (Client.class == declaringClass || InvocationHandlerAware.class == declaringClass || Object.class == declaringClass || Closeable.class == declaringClass || AutoCloseable.class == declaringClass) {
return m.invoke(this, params);
}
resetResponse();
OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(m);
if (ori == null) {
if (m.isDefault()) {
return invokeDefaultMethod(declaringClass, o, m, params);
}
reportInvalidResourceMethod(m, "INVALID_RESOURCE_METHOD");
}
MultivaluedMap<ParameterType, Parameter> types = getParametersInfo(m, params, ori);
List<Parameter> beanParamsList = getParameters(types, ParameterType.BEAN);
int bodyIndex = getBodyIndex(types, ori);
List<Object> pathParams = getPathParamValues(m, params, types, beanParamsList, ori, bodyIndex);
UriBuilder builder = getCurrentBuilder().clone();
if (isRoot) {
addNonEmptyPath(builder, ori.getClassResourceInfo().getURITemplate().getValue());
}
addNonEmptyPath(builder, ori.getURITemplate().getValue());
handleMatrixes(m, params, types, beanParamsList, builder);
handleQueries(m, params, types, beanParamsList, builder);
URI uri = builder.buildFromEncoded(pathParams.toArray()).normalize();
MultivaluedMap<String, String> headers = getHeaders();
MultivaluedMap<String, String> paramHeaders = new MetadataMap<>();
handleHeaders(m, params, paramHeaders, beanParamsList, types);
handleCookies(m, params, paramHeaders, beanParamsList, types);
if (ori.isSubResourceLocator()) {
ClassResourceInfo subCri = cri.getSubResource(m.getReturnType(), m.getReturnType());
if (subCri == null) {
reportInvalidResourceMethod(m, "INVALID_SUBRESOURCE");
}
MultivaluedMap<String, String> subHeaders = paramHeaders;
if (inheritHeaders) {
subHeaders.putAll(headers);
}
ClientState newState = getState().newState(uri, subHeaders, getTemplateParametersMap(ori.getURITemplate(), pathParams));
ClientProxyImpl proxyImpl = new ClientProxyImpl(newState, proxyLoader, subCri, false, inheritHeaders);
proxyImpl.setConfiguration(getConfiguration());
return JAXRSClientFactory.createProxy(m.getReturnType(), proxyLoader, proxyImpl);
}
headers.putAll(paramHeaders);
getState().setTemplates(getTemplateParametersMap(ori.getURITemplate(), pathParams));
Object body = null;
if (bodyIndex != -1) {
body = params[bodyIndex];
if (body == null) {
bodyIndex = -1;
}
} else if (types.containsKey(ParameterType.FORM)) {
body = handleForm(m, params, types, beanParamsList);
} else if (types.containsKey(ParameterType.REQUEST_BODY)) {
body = handleMultipart(types, ori, params);
} else if (hasFormParams(params, beanParamsList)) {
body = handleForm(m, params, types, beanParamsList);
}
setRequestHeaders(headers, ori, types.containsKey(ParameterType.FORM), body == null ? null : body.getClass(), m.getReturnType());
try {
return doChainedInvocation(uri, headers, ori, params, body, bodyIndex, null, null);
} finally {
resetResponseStateImmediatelyIfNeeded();
}
}
use of org.apache.cxf.jaxrs.model.ParameterType in project tomee 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;
}
use of org.apache.cxf.jaxrs.model.ParameterType in project tomee by apache.
the class InjectionUtils method injectIntoCollectionOrArray.
// CHECKSTYLE:OFF
private static Object injectIntoCollectionOrArray(Class<?> rawType, Type genericType, Annotation[] paramAnns, MultivaluedMap<String, String> values, boolean isbean, boolean decoded, ParameterType pathParam, Message message) {
// CHECKSTYLE:ON
Class<?> type = getCollectionType(rawType);
final Class<?> realType;
final Type realGenericType;
if (rawType.isArray()) {
realType = rawType.getComponentType();
realGenericType = realType;
} else {
Type[] types = getActualTypes(genericType);
if (types == null || types.length == 0 || !(types[0] instanceof ParameterizedType)) {
realType = getActualType(genericType);
realGenericType = realType;
} else {
realType = getRawType(types[0]);
realGenericType = types[0];
}
}
Object theValues = null;
if (type != null) {
try {
theValues = type.newInstance();
} catch (IllegalAccessException ex) {
reportServerError("CLASS_ACCESS_FAILURE", type.getName());
} catch (Exception ex) {
reportServerError("CLASS_INSTANTIATION_FAILURE", type.getName());
}
} else {
theValues = Array.newInstance(realType, isbean ? 1 : values.values().iterator().next().size());
}
if (isbean) {
Object o = InjectionUtils.handleBean(realType, paramAnns, values, pathParam, message, decoded);
addToCollectionValues(theValues, o, 0);
} else {
List<String> valuesList = values.values().iterator().next();
valuesList = checkPathSegment(valuesList, realType, pathParam);
for (int ind = 0; ind < valuesList.size(); ind++) {
Object o = InjectionUtils.handleParameter(valuesList.get(ind), decoded, realType, realGenericType, paramAnns, pathParam, message);
addToCollectionValues(theValues, o, ind);
}
}
return theValues;
}
use of org.apache.cxf.jaxrs.model.ParameterType in project cxf by apache.
the class OpenApiParseUtils 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();
app.setBasePath("/");
List<Map<String, Object>> servers = CastUtils.cast((List<?>) map.get("servers"));
if (servers != null && !servers.isEmpty()) {
final String url = (String) servers.get(0).get("url");
if (url != null) {
app.setBasePath(url);
}
}
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());
userOp.setPath(operPath);
userOp.setName((String) oper.get("operationId"));
Map<String, Object> responses = CastUtils.cast((Map<?, ?>) oper.get("responses"));
if (responses != null) {
userOp.setProduces(listToString(responses.entrySet().stream().map(entry -> CastUtils.cast((Map<?, ?>) entry.getValue())).map(value -> CastUtils.cast((Map<?, ?>) value.get("content"))).filter(Objects::nonNull).flatMap(content -> content.keySet().stream().map(type -> (String) type)).collect(Collectors.toList())));
}
Map<String, Object> payloads = CastUtils.cast((Map<?, ?>) oper.get("requestBody"));
if (payloads != null) {
userOp.setConsumes(listToString(payloads.entrySet().stream().map(entry -> CastUtils.cast((Map<?, ?>) entry.getValue())).map(value -> CastUtils.cast((Map<?, ?>) value.get("content"))).filter(Objects::nonNull).flatMap(content -> content.keySet().stream().map(type -> (String) type)).collect(Collectors.toList())));
}
List<Parameter> userOpParams = new LinkedList<>();
List<Map<String, Object>> params = CastUtils.cast((List<?>) oper.get("parameters"));
if (params != null) {
for (Map<String, Object> param : params) {
String name = (String) param.get("name");
// "query", "header", "path" or "cookie".
String paramType = (String) param.get("in");
final ParameterType pType;
if ("query".equals(paramType)) {
pType = ParameterType.QUERY;
} else if ("header".equals(paramType)) {
pType = ParameterType.HEADER;
} else if ("path".equals(paramType)) {
pType = ParameterType.PATH;
} else if ("cookie".equals(paramType)) {
pType = ParameterType.COOKIE;
} else {
pType = ParameterType.REQUEST_BODY;
}
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.putIfAbsent(opTag, new LinkedList<UserOperation>());
userOpsMap.get(opTag).add(userOp);
}
}
}
List<UserResource> resources = new LinkedList<>();
for (Map.Entry<String, List<UserOperation>> entry : userOpsMap.entrySet()) {
if (!entry.getValue().isEmpty()) {
UserResource ur = new UserResource();
ur.setPath("/");
ur.setOperations(entry.getValue());
ur.setName(entry.getKey());
resources.add(ur);
}
}
app.setResources(resources);
return app;
}
Aggregations