use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class InjectionUtils method getParametersFromBeanClass.
public static Map<Parameter, Class<?>> getParametersFromBeanClass(Class<?> beanClass, ParameterType type, boolean checkIgnorable) {
Map<Parameter, Class<?>> params = new LinkedHashMap<Parameter, Class<?>>();
for (Method m : beanClass.getMethods()) {
String methodName = m.getName();
boolean startsFromGet = methodName.startsWith("get");
if ((startsFromGet || isBooleanType(m.getReturnType()) && methodName.startsWith("is")) && m.getParameterTypes().length == 0) {
int minLen = startsFromGet ? 3 : 2;
if (methodName.length() <= minLen) {
continue;
}
String propertyName = StringUtils.uncapitalize(methodName.substring(minLen));
if (m.getReturnType() == Class.class || checkIgnorable && canPropertyBeIgnored(m, propertyName)) {
continue;
}
params.put(new Parameter(type, propertyName), m.getReturnType());
}
}
return params;
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class JAXRSUtils method processParameters.
// Message contains following information: PATH, HTTP_REQUEST_METHOD, CONTENT_TYPE, InputStream.
public static List<Object> processParameters(OperationResourceInfo ori, MultivaluedMap<String, String> values, Message message) throws IOException, WebApplicationException {
Class<?>[] parameterTypes = ori.getInParameterTypes();
List<Parameter> paramsInfo = ori.getParameters();
boolean preferModelParams = paramsInfo.size() > parameterTypes.length && !PropertyUtils.isTrue(message.getContextualProperty("org.apache.cxf.preferMethodParameters"));
int parameterTypesLengh = preferModelParams ? paramsInfo.size() : parameterTypes.length;
Type[] genericParameterTypes = ori.getInGenericParameterTypes();
Annotation[][] anns = ori.getInParameterAnnotations();
List<Object> params = new ArrayList<>(parameterTypesLengh);
for (int i = 0; i < parameterTypesLengh; i++) {
Class<?> param = null;
Type genericParam = null;
Annotation[] paramAnns = null;
if (!preferModelParams) {
param = parameterTypes[i];
genericParam = InjectionUtils.processGenericTypeIfNeeded(ori.getClassResourceInfo().getServiceClass(), param, genericParameterTypes[i]);
param = InjectionUtils.updateParamClassToTypeIfNeeded(param, genericParam);
paramAnns = anns == null ? EMPTY_ANNOTATIONS : anns[i];
} else {
param = paramsInfo.get(i).getJavaType();
genericParam = param;
paramAnns = EMPTY_ANNOTATIONS;
}
Object paramValue = processParameter(param, genericParam, paramAnns, paramsInfo.get(i), values, message, ori);
params.add(paramValue);
}
return params;
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class JAXRSUtils method injectParameters.
@SuppressWarnings("unchecked")
public static void injectParameters(OperationResourceInfo ori, BeanResourceInfo bri, Object requestObject, Message message) {
if (bri.isSingleton() && (!bri.getParameterMethods().isEmpty() || !bri.getParameterFields().isEmpty())) {
LOG.fine("Injecting request parameters into singleton resource is not thread-safe");
}
// Param methods
MultivaluedMap<String, String> values = (MultivaluedMap<String, String>) message.get(URITemplate.TEMPLATE_PARAMETERS);
for (Method m : bri.getParameterMethods()) {
Parameter p = ResourceUtils.getParameter(0, m.getAnnotations(), m.getParameterTypes()[0]);
Object o;
if (p.getType() == ParameterType.BEAN) {
o = createBeanParamValue(message, m.getParameterTypes()[0], ori);
} else {
o = createHttpParameterValue(p, m.getParameterTypes()[0], m.getGenericParameterTypes()[0], m.getParameterAnnotations()[0], message, values, ori);
}
InjectionUtils.injectThroughMethod(requestObject, m, o, message);
}
// Param fields
for (Field f : bri.getParameterFields()) {
Parameter p = ResourceUtils.getParameter(0, f.getAnnotations(), f.getType());
Object o = null;
if (p.getType() == ParameterType.BEAN) {
o = createBeanParamValue(message, f.getType(), ori);
} else {
o = createHttpParameterValue(p, f.getType(), f.getGenericType(), f.getAnnotations(), message, values, ori);
}
InjectionUtils.injectFieldValue(f, requestObject, o);
}
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class ResourceUtils method getParameter.
// CHECKSTYLE:OFF
public static Parameter getParameter(int index, Annotation[] anns, Class<?> type) {
Context ctx = AnnotationUtils.getAnnotation(anns, Context.class);
if (ctx != null) {
return new Parameter(ParameterType.CONTEXT, index, null);
}
boolean isEncoded = AnnotationUtils.getAnnotation(anns, Encoded.class) != null;
BeanParam bp = AnnotationUtils.getAnnotation(anns, BeanParam.class);
if (bp != null) {
return new Parameter(ParameterType.BEAN, index, null, isEncoded, null);
}
String dValue = AnnotationUtils.getDefaultParameterValue(anns);
PathParam a = AnnotationUtils.getAnnotation(anns, PathParam.class);
if (a != null) {
return new Parameter(ParameterType.PATH, index, a.value(), isEncoded, dValue);
}
QueryParam q = AnnotationUtils.getAnnotation(anns, QueryParam.class);
if (q != null) {
return new Parameter(ParameterType.QUERY, index, q.value(), isEncoded, dValue);
}
MatrixParam m = AnnotationUtils.getAnnotation(anns, MatrixParam.class);
if (m != null) {
return new Parameter(ParameterType.MATRIX, index, m.value(), isEncoded, dValue);
}
FormParam f = AnnotationUtils.getAnnotation(anns, FormParam.class);
if (f != null) {
return new Parameter(ParameterType.FORM, index, f.value(), isEncoded, dValue);
}
HeaderParam h = AnnotationUtils.getAnnotation(anns, HeaderParam.class);
if (h != null) {
return new Parameter(ParameterType.HEADER, index, h.value(), isEncoded, dValue);
}
CookieParam c = AnnotationUtils.getAnnotation(anns, CookieParam.class);
if (c != null) {
return new Parameter(ParameterType.COOKIE, index, c.value(), isEncoded, dValue);
}
return new Parameter(ParameterType.REQUEST_BODY, index, null);
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class ResourceUtils method getOperationFromElement.
private static UserOperation getOperationFromElement(Element e) {
UserOperation op = new UserOperation();
op.setName(e.getAttribute("name"));
op.setVerb(e.getAttribute("verb"));
op.setPath(e.getAttribute("path"));
op.setOneway(Boolean.parseBoolean(e.getAttribute("oneway")));
op.setConsumes(e.getAttribute("consumes"));
op.setProduces(e.getAttribute("produces"));
List<Element> paramEls = DOMUtils.findAllElementsByTagNameNS(e, "http://cxf.apache.org/jaxrs", "param");
List<Parameter> params = new ArrayList<>(paramEls.size());
for (int i = 0; i < paramEls.size(); i++) {
Element paramEl = paramEls.get(i);
Parameter p = new Parameter(paramEl.getAttribute("type"), i, paramEl.getAttribute("name"));
p.setEncoded(Boolean.valueOf(paramEl.getAttribute("encoded")));
p.setDefaultValue(paramEl.getAttribute("defaultValue"));
String pClass = paramEl.getAttribute("class");
if (!StringUtils.isEmpty(pClass)) {
try {
p.setJavaType(ClassLoaderUtils.loadClass(pClass, ResourceUtils.class));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
params.add(p);
}
op.setParameters(params);
return op;
}
Aggregations