private static boolean isIgnorableParameter(Method m, Parameter p) { if (p.getType() == ParameterType.CONTEXT) { return true; } return p.getType() == ParameterType.REQUEST_BODY && m.getParameterTypes()[p.getIndex()] == AsyncResponse.class; }
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<Parameter>(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; }
private static MultivaluedMap<ParameterType, Parameter> getParametersInfo( Method m, Object[] params, OperationResourceInfo ori) { MultivaluedMap<ParameterType, Parameter> map = new MetadataMap<ParameterType, Parameter>(); List<Parameter> parameters = ori.getParameters(); if (parameters.size() == 0) { 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; }
private static void getAllTypesForResource( ClassResourceInfo resource, ResourceTypes types, boolean jaxbOnly, MessageBodyWriter<?> jaxbWriter) { for (OperationResourceInfo ori : resource.getMethodDispatcher().getOperationResourceInfos()) { Method method = ori.getMethodToInvoke(); Class<?> realReturnType = method.getReturnType(); Class<?> cls = realReturnType; if (cls == Response.class) { cls = getActualJaxbType(cls, method, false); } Type type = method.getGenericReturnType(); if (jaxbOnly) { checkJaxbType( resource.getServiceClass(), cls, realReturnType == Response.class ? cls : type, types, method.getAnnotations(), jaxbWriter); } else { types.getAllTypes().put(cls, type); } for (Parameter pm : ori.getParameters()) { if (pm.getType() == ParameterType.REQUEST_BODY) { Class<?> inType = method.getParameterTypes()[pm.getIndex()]; Type paramType = method.getGenericParameterTypes()[pm.getIndex()]; if (jaxbOnly) { checkJaxbType( resource.getServiceClass(), inType, paramType, types, method.getParameterAnnotations()[pm.getIndex()], jaxbWriter); } else { types.getAllTypes().put(inType, paramType); } } } } for (ClassResourceInfo sub : resource.getSubResources()) { if (!isRecursiveSubResource(resource, sub)) { getAllTypesForResource(sub, types, jaxbOnly, jaxbWriter); } } }
private List<Attachment> handleMultipart( MultivaluedMap<ParameterType, Parameter> map, OperationResourceInfo ori, Object[] params) { List<Attachment> atts = new LinkedList<Attachment>(); List<Parameter> fm = getParameters(map, ParameterType.REQUEST_BODY); for (Parameter p : fm) { Multipart part = getMultipart(ori, p.getIndex()); if (part != null) { Object partObject = params[p.getIndex()]; if (partObject != null) { atts.add(new Attachment(part.value(), part.type(), partObject)); } } } return atts; }
private MultivaluedMap<String, String> handleForm( Method m, Object[] params, MultivaluedMap<ParameterType, Parameter> map, List<Parameter> beanParams) { MultivaluedMap<String, String> form = new MetadataMap<String, String>(); List<Parameter> fm = getParameters(map, ParameterType.FORM); for (Parameter p : fm) { addFormValue(form, p.getName(), params[p.getIndex()], getParamAnnotations(m, p)); } for (Parameter p : beanParams) { Map<String, BeanPair> values = getValuesFromBeanParam(params[p.getIndex()], FormParam.class); for (Map.Entry<String, BeanPair> entry : values.entrySet()) { addFormValue(form, entry.getKey(), entry.getValue().getValue(), entry.getValue().getAnns()); } } return form; }
private void handleCookies( Method m, Object[] params, MultivaluedMap<String, String> headers, List<Parameter> beanParams, MultivaluedMap<ParameterType, Parameter> map) { List<Parameter> cs = getParameters(map, ParameterType.COOKIE); for (Parameter p : cs) { if (params[p.getIndex()] != null) { headers.add( HttpHeaders.COOKIE, p.getName() + '=' + convertParamValue(params[p.getIndex()].toString(), getParamAnnotations(m, p))); } } for (Parameter p : beanParams) { Map<String, BeanPair> values = getValuesFromBeanParam(params[p.getIndex()], CookieParam.class); for (Map.Entry<String, BeanPair> entry : values.entrySet()) { if (entry.getValue() != null) { headers.add( HttpHeaders.COOKIE, entry.getKey() + "=" + convertParamValue(entry.getValue().getValue(), entry.getValue().getAnns())); } } } }
private void handleHeaders( Method m, Object[] params, MultivaluedMap<String, String> headers, List<Parameter> beanParams, MultivaluedMap<ParameterType, Parameter> map) { List<Parameter> hs = getParameters(map, ParameterType.HEADER); for (Parameter p : hs) { if (params[p.getIndex()] != null) { headers.add( p.getName(), convertParamValue(params[p.getIndex()], getParamAnnotations(m, p))); } } for (Parameter p : beanParams) { Map<String, BeanPair> values = getValuesFromBeanParam(params[p.getIndex()], HeaderParam.class); for (Map.Entry<String, BeanPair> entry : values.entrySet()) { if (entry.getValue() != null) { headers.add( entry.getKey(), convertParamValue(entry.getValue().getValue(), entry.getValue().getAnns())); } } } }
private void handleMatrixes( Method m, Object[] params, MultivaluedMap<ParameterType, Parameter> map, List<Parameter> beanParams, UriBuilder ub) { List<Parameter> mx = getParameters(map, ParameterType.MATRIX); for (Parameter p : mx) { if (params[p.getIndex()] != null) { addMatrixQueryParamsToBuilder( ub, p.getName(), ParameterType.MATRIX, getParamAnnotations(m, p), params[p.getIndex()]); } } for (Parameter p : beanParams) { Map<String, BeanPair> values = getValuesFromBeanParam(params[p.getIndex()], MatrixParam.class); for (Map.Entry<String, BeanPair> entry : values.entrySet()) { if (entry.getValue() != null) { addMatrixQueryParamsToBuilder( ub, entry.getKey(), ParameterType.MATRIX, entry.getValue().getAnns(), entry.getValue().getValue()); } } } }
private static Annotation[] getParamAnnotations(Method m, Parameter p) { return m.getParameterAnnotations()[p.getIndex()]; }
private List<Object> getPathParamValues( Method m, Object[] params, MultivaluedMap<ParameterType, Parameter> map, List<Parameter> beanParams, OperationResourceInfo ori, int bodyIndex) { List<Object> list = new LinkedList<Object>(); List<String> methodVars = ori.getURITemplate().getVariables(); List<Parameter> paramsList = getParameters(map, ParameterType.PATH); Map<String, BeanPair> beanParamValues = new HashMap<String, BeanPair>(beanParams.size()); for (Parameter p : beanParams) { beanParamValues.putAll(getValuesFromBeanParam(params[p.getIndex()], PathParam.class)); } if (!beanParamValues.isEmpty() && !methodVars.containsAll(beanParamValues.keySet())) { List<String> classVars = ori.getClassResourceInfo().getURITemplate().getVariables(); for (String classVar : classVars) { BeanPair pair = beanParamValues.get(classVar); if (pair != null) { Object paramValue = convertParamValue(pair.getValue(), pair.getAnns()); if (isRoot) { valuesMap.put(classVar, paramValue); } else { list.add(paramValue); } } } } if (isRoot) { list.addAll(valuesMap.values()); } Map<String, Parameter> paramsMap = new LinkedHashMap<String, Parameter>(); for (Parameter p : paramsList) { if (p.getName().length() == 0) { MultivaluedMap<String, Object> values = InjectionUtils.extractValuesFromBean(params[p.getIndex()], ""); for (String var : methodVars) { list.addAll(values.get(var)); } } else { paramsMap.put(p.getName(), p); } } Object requestBody = bodyIndex == -1 ? null : params[bodyIndex]; for (String varName : methodVars) { Parameter p = paramsMap.remove(varName); if (p != null) { list.add(convertParamValue(params[p.getIndex()], getParamAnnotations(m, p))); } else if (beanParamValues.containsKey(varName)) { BeanPair pair = beanParamValues.get(varName); list.add(convertParamValue(pair.getValue(), pair.getAnns())); } else if (requestBody != null) { try { Method getter = requestBody .getClass() .getMethod("get" + StringUtils.capitalize(varName), new Class<?>[] {}); list.add(getter.invoke(requestBody, new Object[] {})); } catch (Exception ex) { // continue } } } for (Parameter p : paramsMap.values()) { if (valuesMap.containsKey(p.getName())) { int index = 0; for (Iterator<String> it = valuesMap.keySet().iterator(); it.hasNext(); index++) { if (it.next().equals(p.getName()) && index < list.size()) { list.remove(index); list.add(index, convertParamValue(params[p.getIndex()], null)); break; } } } } return list; }