Search in sources :

Example 6 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.

the class ClientProxyImpl method getBodyIndex.

private static int getBodyIndex(MultivaluedMap<ParameterType, Parameter> map, OperationResourceInfo ori) {
    List<Parameter> list = map.get(ParameterType.REQUEST_BODY);
    int index = list == null || list.size() > 1 ? -1 : list.get(0).getIndex();
    if (ori.isSubResourceLocator() && index != -1) {
        reportInvalidResourceMethod(ori.getMethodToInvoke(), "NO_BODY_IN_SUBRESOURCE");
    }
    return index;
}
Also used : Parameter(org.apache.cxf.jaxrs.model.Parameter) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 7 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.

the class ClientProxyImpl method handleForm.

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;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Parameter(org.apache.cxf.jaxrs.model.Parameter) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 8 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.

the class ClientProxyImpl method getParametersInfo.

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.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;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) Parameter(org.apache.cxf.jaxrs.model.Parameter) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 9 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter in project cxf 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
 */
public Object invoke(Object o, Method m, Object[] params) throws Throwable {
    Class<?> declaringClass = m.getDeclaringClass();
    if (Client.class == declaringClass || InvocationHandlerAware.class == declaringClass || Object.class == declaringClass) {
        return m.invoke(this, params);
    }
    resetResponse();
    OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(m);
    if (ori == null) {
        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<String, String>();
    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);
    }
    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();
    }
}
Also used : ParameterType(org.apache.cxf.jaxrs.model.ParameterType) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URI(java.net.URI) Endpoint(org.apache.cxf.endpoint.Endpoint) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Parameter(org.apache.cxf.jaxrs.model.Parameter) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) UriBuilder(javax.ws.rs.core.UriBuilder)

Example 10 with Parameter

use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.

the class SwaggerParseUtilsTest method testConvertSimpleDocToUserApp.

@Test
public void testConvertSimpleDocToUserApp() {
    UserApplication ap = SwaggerParseUtils.getUserApplication("/swagger20.json");
    assertNotNull(ap);
    assertEquals("/services/helloservice", ap.getBasePath());
    Map<String, UserResource> map = ap.getResourcesAsMap();
    assertEquals(2, map.size());
    UserResource ur = map.get("sayHello");
    assertNotNull(ur);
    assertEquals("/", ur.getPath());
    assertEquals(1, ur.getOperations().size());
    UserOperation op = ur.getOperations().get(0);
    assertEquals("sayHello", op.getName());
    assertEquals("/sayHello/{a}", op.getPath());
    assertEquals("GET", op.getVerb());
    assertEquals("text/plain", op.getProduces());
    assertEquals(1, op.getParameters().size());
    Parameter param1 = op.getParameters().get(0);
    assertEquals("a", param1.getName());
    assertEquals(ParameterType.PATH, param1.getType());
    assertEquals(String.class, param1.getJavaType());
    UserResource ur2 = map.get("sayHello2");
    assertNotNull(ur2);
    assertEquals("/", ur2.getPath());
    assertEquals(1, ur2.getOperations().size());
    UserOperation op2 = ur2.getOperations().get(0);
    assertEquals("sayHello", op2.getName());
    assertEquals("/sayHello2/{a}", op2.getPath());
    assertEquals("GET", op2.getVerb());
    assertEquals("text/plain", op2.getProduces());
    assertEquals(1, op2.getParameters().size());
    Parameter param2 = op.getParameters().get(0);
    assertEquals("a", param2.getName());
    assertEquals(ParameterType.PATH, param2.getType());
    assertEquals(String.class, param2.getJavaType());
}
Also used : UserApplication(org.apache.cxf.jaxrs.model.UserApplication) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) UserResource(org.apache.cxf.jaxrs.model.UserResource) Parameter(org.apache.cxf.jaxrs.model.Parameter) Test(org.junit.Test)

Aggregations

Parameter (org.apache.cxf.jaxrs.model.Parameter)38 UserOperation (org.apache.cxf.jaxrs.model.UserOperation)13 Method (java.lang.reflect.Method)12 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)10 Endpoint (org.apache.cxf.endpoint.Endpoint)9 UserResource (org.apache.cxf.jaxrs.model.UserResource)9 LinkedHashMap (java.util.LinkedHashMap)8 HashMap (java.util.HashMap)7 UserApplication (org.apache.cxf.jaxrs.model.UserApplication)7 MediaType (javax.ws.rs.core.MediaType)6 Annotation (java.lang.annotation.Annotation)5 Type (java.lang.reflect.Type)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)5 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)5 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)5 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)5 LinkedList (java.util.LinkedList)4 Test (org.junit.Test)4