Java Code Examples for org.apache.cxf.jaxrs.model.OperationResourceInfo#getParameters()
The following examples show how to use
org.apache.cxf.jaxrs.model.OperationResourceInfo#getParameters() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WadlGenerator.java From cxf with Apache License 2.0 | 6 votes |
private void handleFormRepresentation(StringBuilder sb, Set<Class<?>> jaxbTypes, ElementQNameResolver qnameResolver, Map<Class<?>, QName> clsMap, OperationResourceInfo ori, Class<?> type, boolean isJson) { if (type != null) { handleRepresentation(sb, jaxbTypes, qnameResolver, clsMap, ori, type, false, true); } else { List<MediaType> types = ori.getConsumeTypes(); MediaType formType = isWildcard(types) ? MediaType.APPLICATION_FORM_URLENCODED_TYPE : types .get(0); sb.append("<representation"); sb.append(" mediaType=\"").append(formType).append('"'); if (isJson) { sb.append("/>"); } else { sb.append('>'); List<Parameter> params = ori.getParameters(); for (int i = 0; i < params.size(); i++) { if (isFormParameter(params.get(i), getMethod(ori).getParameterTypes()[i], getMethod(ori) .getParameterAnnotations()[i])) { writeParam(sb, params.get(i), ori, isJson); } } sb.append("</representation>"); } } }
Example 2
Source File: JAXRSServiceImpl.java From cxf with Apache License 2.0 | 5 votes |
public List<ServiceInfo> getServiceInfos() { if (!createServiceModel) { return Collections.emptyList(); } // try to convert to WSDL-centric model so that CXF DataBindings can get initialized // might become useful too if we support wsdl2 // make databindings to use databinding-specific information // like @XmlRootElement for ex to select a namespace this.put("org.apache.cxf.databinding.namespace", "true"); List<ServiceInfo> infos = new ArrayList<>(); for (ClassResourceInfo cri : classResourceInfos) { ServiceInfo si = new ServiceInfo(); infos.add(si); QName qname = JAXRSUtils.getClassQName(cri.getServiceClass()); si.setName(qname); InterfaceInfo inf = new InterfaceInfo(si, qname); si.setInterface(inf); for (OperationResourceInfo ori : cri.getMethodDispatcher().getOperationResourceInfos()) { Method m = ori.getMethodToInvoke(); QName oname = new QName(qname.getNamespaceURI(), m.getName()); OperationInfo oi = inf.addOperation(oname); createMessagePartInfo(oi, m.getReturnType(), qname, m, false); for (Parameter pm : ori.getParameters()) { if (pm.getType() == ParameterType.REQUEST_BODY) { createMessagePartInfo(oi, ori.getMethodToInvoke().getParameterTypes()[pm.getIndex()], qname, m, true); } } } } return infos; }
Example 3
Source File: ResourceUtilsTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testClassResourceInfoUserResource() throws Exception { UserResource ur = new UserResource(); ur.setName(HashMap.class.getName()); //NOPMD ur.setPath("/hashmap"); UserOperation op = new UserOperation(); op.setPath("/key/{id}"); op.setName("get"); op.setVerb("POST"); op.setParameters(Collections.singletonList(new Parameter(ParameterType.PATH, "id"))); ur.setOperations(Collections.singletonList(op)); Map<String, UserResource> resources = new HashMap<>(); resources.put(ur.getName(), ur); ClassResourceInfo cri = ResourceUtils.createClassResourceInfo(resources, ur, null, true, true, BusFactory.getDefaultBus()); assertNotNull(cri); assertEquals("/hashmap", cri.getURITemplate().getValue()); Method method = HashMap.class.getMethod("get", new Class[]{Object.class}); //NOPMD OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(method); assertNotNull(ori); assertEquals("/key/{id}", ori.getURITemplate().getValue()); List<Parameter> params = ori.getParameters(); assertNotNull(params); Parameter p = params.get(0); assertEquals("id", p.getName()); }
Example 4
Source File: ClientProxyImpl.java From cxf with Apache License 2.0 | 5 votes |
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; }
Example 5
Source File: WadlGenerator.java From cxf with Apache License 2.0 | 5 votes |
protected void handleParams(StringBuilder sb, OperationResourceInfo ori, ParameterType type, boolean isJson) { for (Parameter pm : ori.getParameters()) { if (pm.getType() == type) { writeParam(sb, pm, ori, isJson); } } }
Example 6
Source File: WadlGenerator.java From cxf with Apache License 2.0 | 5 votes |
private Annotation[] getBodyAnnotations(OperationResourceInfo ori, boolean inbound) { Method opMethod = getMethod(ori); if (inbound) { for (Parameter pm : ori.getParameters()) { if (pm.getType() == ParameterType.REQUEST_BODY) { return opMethod.getParameterAnnotations()[pm.getIndex()]; } } return new Annotation[] {}; } return opMethod.getDeclaredAnnotations(); }
Example 7
Source File: WadlGenerator.java From cxf with Apache License 2.0 | 5 votes |
private void doWriteJaxrsBeanParams(StringBuilder sb, OperationResourceInfo ori, boolean isJson, ParameterType ...parameterTypes) { for (Parameter p : ori.getParameters()) { if (p.getType() == ParameterType.BEAN) { Method method = getMethod(ori); Class<?> type = method.getParameterTypes()[p.getIndex()]; doWriteJaxrsBeanParam(sb, ori, type, isJson, parameterTypes); } } }
Example 8
Source File: WadlGenerator.java From cxf with Apache License 2.0 | 5 votes |
private Parameter getRequestBodyParam(OperationResourceInfo ori) { for (Parameter p : ori.getParameters()) { if (p.getType() == ParameterType.REQUEST_BODY) { return p; } } throw new IllegalStateException(); }
Example 9
Source File: WadlGenerator.java From cxf with Apache License 2.0 | 5 votes |
private boolean isFormRequest(OperationResourceInfo ori) { for (Parameter p : ori.getParameters()) { if (p.getType() == ParameterType.FORM || p.getType() == ParameterType.REQUEST_BODY && (getMethod(ori).getParameterTypes()[p.getIndex()] == MultivaluedMap.class || AnnotationUtils .getAnnotation(getMethod(ori).getParameterAnnotations()[p.getIndex()], Multipart.class) != null)) { return true; } } return false; }
Example 10
Source File: WadlGenerator.java From cxf with Apache License 2.0 | 5 votes |
private Class<?> getFormClass(OperationResourceInfo ori) { List<Parameter> params = ori.getParameters(); for (int i = 0; i < params.size(); i++) { if (isFormParameter(params.get(i), getMethod(ori).getParameterTypes()[i], getMethod(ori) .getParameterAnnotations()[i])) { return null; } } return MultivaluedMap.class; }
Example 11
Source File: ResourceUtils.java From cxf with Apache License 2.0 | 4 votes |
private static void getAllTypesForResource(ClassResourceInfo resource, ResourceTypes types, boolean jaxbOnly, MessageBodyWriter<?> jaxbWriter) { Class<?> jaxbElement = null; try { jaxbElement = ClassLoaderUtils.loadClass("javax.xml.bind.JAXBElement", ResourceUtils.class); } catch (final ClassNotFoundException e) { // no-op } for (OperationResourceInfo ori : resource.getMethodDispatcher().getOperationResourceInfos()) { Method method = ori.getAnnotatedMethod() == null ? ori.getMethodToInvoke() : ori.getAnnotatedMethod(); Class<?> realReturnType = method.getReturnType(); Class<?> cls = realReturnType; if (cls == Response.class || ori.isAsync()) { cls = getActualJaxbType(cls, method, false); } Type type = method.getGenericReturnType(); if (jaxbOnly) { checkJaxbType(resource.getServiceClass(), cls, realReturnType == Response.class || ori.isAsync() ? cls : type, types, method.getAnnotations(), jaxbWriter, jaxbElement); } else { types.getAllTypes().put(cls, type); } for (Parameter pm : ori.getParameters()) { if (pm.getType() == ParameterType.REQUEST_BODY) { Class<?> inType = method.getParameterTypes()[pm.getIndex()]; if (inType != AsyncResponse.class) { Type paramType = method.getGenericParameterTypes()[pm.getIndex()]; if (jaxbOnly) { checkJaxbType(resource.getServiceClass(), inType, paramType, types, method.getParameterAnnotations()[pm.getIndex()], jaxbWriter, jaxbElement); } else { types.getAllTypes().put(inType, paramType); } } } } } for (ClassResourceInfo sub : resource.getSubResources()) { if (!isRecursiveSubResource(resource, sub)) { getAllTypesForResource(sub, types, jaxbOnly, jaxbWriter); } } }
Example 12
Source File: JAXRSUtils.java From cxf with Apache License 2.0 | 4 votes |
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")); final int parameterTypesLength = preferModelParams ? paramsInfo.size() : parameterTypes.length; if (parameterTypesLength < 1) { return Collections.emptyList(); } Type[] genericParameterTypes = ori.getInGenericParameterTypes(); Annotation[][] anns = ori.getInParameterAnnotations(); Object[] params = new Object[parameterTypesLength]; // Ensure we process all request-body parameters first, then all @*Params, etc. ParamTuple[] tuple = new ParamTuple[parameterTypesLength]; for (int i = 0; i < parameterTypesLength; i++) { tuple[i] = new ParamTuple(); if (!preferModelParams) { tuple[i].param = parameterTypes[i]; tuple[i].genericParam = InjectionUtils.processGenericTypeIfNeeded( ori.getClassResourceInfo().getServiceClass(), tuple[i].param, genericParameterTypes[i]); tuple[i].param = InjectionUtils.updateParamClassToTypeIfNeeded(tuple[i].param, tuple[i].genericParam); tuple[i].paramAnns = anns == null ? EMPTY_ANNOTATIONS : anns[i]; } else { tuple[i].param = paramsInfo.get(i).getJavaType(); tuple[i].genericParam = tuple[i].param; tuple[i].paramAnns = EMPTY_ANNOTATIONS; } if (paramsInfo.get(i).getType() == ParameterType.REQUEST_BODY) { params[i] = processRequestBodyParameter(tuple[i].param, tuple[i].genericParam, tuple[i].paramAnns, message, ori); } } for (int i = 0; i < parameterTypesLength; i++) { if (paramsInfo.get(i).getType() != ParameterType.REQUEST_BODY) { params[i] = processParameter(tuple[i].param, tuple[i].genericParam, tuple[i].paramAnns, paramsInfo.get(i), values, message, ori); } } return Arrays.asList(params); }
Example 13
Source File: WadlGenerator.java From cxf with Apache License 2.0 | 4 votes |
protected boolean handleOperation(StringBuilder sb, Set<Class<?>> jaxbTypes, //NOPMD ElementQNameResolver qnameResolver, Map<Class<?>, QName> clsMap, OperationResourceInfo ori, Map<Parameter, Object> classParams, OperationResourceInfo nextOp, boolean resourceTagOpened, boolean isJson, int index) { Annotation[] anns = getMethod(ori).getAnnotations(); // CHECKSTYLE:ON boolean samePathOperationFollows = singleResourceMultipleMethods && compareOperations(ori, nextOp); String path = ori.getURITemplate().getValue(); if (!resourceTagOpened && openResource(path)) { resourceTagOpened = true; URITemplate template = ori.getClassResourceInfo().getURITemplate(); if (template != null) { String parentPath = template.getValue(); if (parentPath.endsWith("/") && path.startsWith("/") && path.length() > 1) { path = path.substring(1); } } startResourceTag(sb, ori, getPath(path)); handleDocs(anns, sb, DocTarget.RESOURCE, false, isJson); handlePathAndMatrixClassParams(ori, sb, classParams, isJson); handlePathAndMatrixParams(sb, ori, isJson); } else if (index == 0) { handlePathAndMatrixClassParams(ori, sb, classParams, isJson); handlePathAndMatrixParams(sb, ori, isJson); } startMethodTag(sb, ori); if (!handleDocs(anns, sb, DocTarget.METHOD, true, isJson)) { handleOperJavaDocs(ori, sb); } int numOfParams = getMethod(ori).getParameterTypes().length; if ((numOfParams > 1 || numOfParams == 1 && !ori.isAsync()) || !classParams.isEmpty()) { startMethodRequestTag(sb, ori); handleDocs(anns, sb, DocTarget.REQUEST, false, isJson); boolean isForm = isFormRequest(ori); doHandleClassParams(ori, sb, classParams, isJson, ParameterType.QUERY, ParameterType.HEADER); doHandleJaxrsBeanParamClassParams(ori, sb, classParams, isJson, ParameterType.QUERY, ParameterType.HEADER); for (Parameter p : ori.getParameters()) { if (isForm && p.getType() == ParameterType.REQUEST_BODY) { continue; } handleParameter(sb, jaxbTypes, qnameResolver, clsMap, ori, p, isJson); } if (isForm) { handleFormRepresentation(sb, jaxbTypes, qnameResolver, clsMap, ori, getFormClass(ori), isJson); } endMethodRequestTag(sb, ori); } startMethodResponseTag(sb, ori); Class<?> returnType = getMethod(ori).getReturnType(); boolean isVoid = void.class == returnType && !ori.isAsync(); ResponseStatus responseStatus = getMethod(ori).getAnnotation(ResponseStatus.class); if (responseStatus != null) { setResponseStatus(sb, responseStatus.value()); } else if (isVoid) { boolean oneway = getMethod(ori).getAnnotation(Oneway.class) != null; setResponseStatus(sb, oneway ? Response.Status.ACCEPTED : Response.Status.NO_CONTENT); } sb.append('>'); handleDocs(anns, sb, DocTarget.RESPONSE, false, isJson); if (!isVoid) { handleRepresentation(sb, jaxbTypes, qnameResolver, clsMap, ori, returnType, isJson, false); } endMethodResponseTag(sb, ori); endMethodTag(sb, ori); if (resourceTagOpened && !samePathOperationFollows) { endResourceTag(sb, ori); resourceTagOpened = false; } return resourceTagOpened; }
Example 14
Source File: QueryResourceInfoComparator.java From cxf-fediz with Apache License 2.0 | 4 votes |
/** * This method calculates a number indicating a good or bad match between values provided within the request and * expected method parameters. A higher number means a better match. * * @param operation The operation to be rated, based on contained parameterInfo values. * @param message A message containing query and header values from user request * @return A positive or negative number, indicating a good match between query and method */ protected int getMatchingRate(final OperationResourceInfo operation, final Message message) { List<Parameter> params = operation.getParameters(); if (params == null || params.isEmpty()) { return 0; } // Get Request QueryParams String query = (String) message.get(Message.QUERY_STRING); String path = (String) message.get(Message.REQUEST_URI); Map<String, List<String>> qParams = JAXRSUtils.getStructuredParams(query, "&", true, false); Map<String, List<String>> mParams = JAXRSUtils.getMatrixParams(path, true); // Get Request Headers Map<?, ?> qHeader = (java.util.Map<?, ?>) message.get(Message.PROTOCOL_HEADERS); int rate = 0; for (Parameter p : params) { switch (p.getType()) { case QUERY: if (qParams.containsKey(p.getName())) { rate += 2; } else if (p.getDefaultValue() == null) { rate -= 1; } break; case MATRIX: if (mParams.containsKey(p.getName())) { rate += 2; } else if (p.getDefaultValue() == null) { rate -= 1; } break; case HEADER: if (qHeader.containsKey(p.getName())) { rate += 2; } else if (p.getDefaultValue() == null) { rate -= 1; } break; default: break; } } return rate; }