Search in sources :

Example 36 with Parameter

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

the class JAXRSParameterNameProvider method getParameterNames.

@Override
public List<String> getParameterNames(final Method method) {
    final List<Parameter> parameters = ResourceUtils.getParameters(method);
    final List<String> parameterNames = new ArrayList<String>();
    for (int i = 0; i < parameters.size(); ++i) {
        final StringBuilder sb = new StringBuilder();
        sb.append("arg" + i);
        sb.append("(");
        Parameter parameter = parameters.get(i);
        if (parameter.getName() != null) {
            sb.append(parameter.getType().toString());
            sb.append("(\"" + parameter.getName() + "\")");
            sb.append(" ");
        }
        sb.append(method.getParameterTypes()[i].getSimpleName());
        sb.append(")");
        parameterNames.add(sb.toString());
    }
    return parameterNames;
}
Also used : ArrayList(java.util.ArrayList) Parameter(org.apache.cxf.jaxrs.model.Parameter)

Example 37 with Parameter

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

the class AbstractSwagger2ServiceDescriptionTest method doTestApiListingIsProperlyReturnedJSON.

protected static void doTestApiListingIsProperlyReturnedJSON(final WebClient client, boolean useXForwarded) throws Exception {
    if (useXForwarded) {
        client.header("USE_XFORWARDED", true);
    }
    try {
        String swaggerJson = client.get(String.class);
        UserApplication ap = SwaggerParseUtils.getUserApplicationFromJson(swaggerJson);
        assertNotNull(ap);
        assertEquals(useXForwarded ? "/reverse" : "/", ap.getBasePath());
        List<UserResource> urs = ap.getResources();
        assertNotNull(urs);
        assertEquals(1, urs.size());
        UserResource r = urs.get(0);
        String basePath = "";
        if (!"/".equals(r.getPath())) {
            basePath = r.getPath();
        }
        Map<String, UserOperation> map = r.getOperationsAsMap();
        assertEquals(3, map.size());
        UserOperation getBooksOp = map.get("getBooks");
        assertEquals(HttpMethod.GET, getBooksOp.getVerb());
        assertEquals("/bookstore", basePath + getBooksOp.getPath());
        assertEquals(MediaType.APPLICATION_JSON, getBooksOp.getProduces());
        List<Parameter> getBooksOpParams = getBooksOp.getParameters();
        assertEquals(1, getBooksOpParams.size());
        assertEquals(ParameterType.QUERY, getBooksOpParams.get(0).getType());
        UserOperation getBookOp = map.get("getBook");
        assertEquals(HttpMethod.GET, getBookOp.getVerb());
        assertEquals("/bookstore/{id}", basePath + getBookOp.getPath());
        assertEquals(MediaType.APPLICATION_JSON, getBookOp.getProduces());
        List<Parameter> getBookOpParams = getBookOp.getParameters();
        assertEquals(1, getBookOpParams.size());
        assertEquals(ParameterType.PATH, getBookOpParams.get(0).getType());
        UserOperation deleteOp = map.get("delete");
        assertEquals(HttpMethod.DELETE, deleteOp.getVerb());
        assertEquals("/bookstore/{id}", basePath + deleteOp.getPath());
        List<Parameter> delOpParams = deleteOp.getParameters();
        assertEquals(1, delOpParams.size());
        assertEquals(ParameterType.PATH, delOpParams.get(0).getType());
        assertThat(swaggerJson, CoreMatchers.containsString(CONTACT));
        assertThat(swaggerJson, CoreMatchers.containsString(TITLE));
        assertThat(swaggerJson, CoreMatchers.containsString(DESCRIPTION));
        assertThat(swaggerJson, CoreMatchers.containsString(LICENSE));
        assertThat(swaggerJson, CoreMatchers.containsString(LICENSE_URL));
        assertThat(swaggerJson, CoreMatchers.containsString(SECURITY_DEFINITION_NAME));
    } finally {
        client.close();
    }
}
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) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 38 with Parameter

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

the class JAXRSServiceImpl method getServiceInfos.

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;
}
Also used : ServiceInfo(org.apache.cxf.service.model.ServiceInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Parameter(org.apache.cxf.jaxrs.model.Parameter) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) Method(java.lang.reflect.Method)

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