Search in sources :

Example 11 with HttpMethod

use of javax.ws.rs.HttpMethod in project opencast by opencast.

the class RestDocsAnnotationTest method testRestQueryDocs.

/**
 * This tests the functionality of @RestQuery, @RestParameter, @RestResponse, @Path, @Produces, @Consumes annotation
 * type.
 */
@Test
public void testRestQueryDocs() {
    Method testMethod;
    try {
        testMethod = TestServletSample.class.getMethod("methodA");
        if (testMethod != null) {
            RestQuery restQueryAnnotation = (RestQuery) testMethod.getAnnotation(RestQuery.class);
            Path pathAnnotation = (Path) testMethod.getAnnotation(Path.class);
            Produces producesAnnotation = (Produces) testMethod.getAnnotation(Produces.class);
            Consumes consumesAnnotation = (Consumes) testMethod.getAnnotation(Consumes.class);
            assertEquals(1, producesAnnotation.value().length);
            assertEquals(MediaType.TEXT_XML, producesAnnotation.value()[0]);
            assertEquals(1, consumesAnnotation.value().length);
            assertEquals(MediaType.MULTIPART_FORM_DATA, consumesAnnotation.value()[0]);
            assertEquals("addTrack", pathAnnotation.value());
            // we cannot hard code the GET.class or POST.class because we don't know which one is used.
            for (Annotation a : testMethod.getAnnotations()) {
                HttpMethod method = (HttpMethod) a.annotationType().getAnnotation(HttpMethod.class);
                if (method != null) {
                    assertEquals("POST", a.annotationType().getSimpleName());
                    assertEquals("POST", method.value());
                }
            }
            // name, description and return description
            assertEquals("addTrackInputStream", restQueryAnnotation.name());
            assertEquals("Add a media track to a given media package using an input stream", restQueryAnnotation.description());
            assertEquals("augmented media package", restQueryAnnotation.returnDescription());
            // path parameter
            assertTrue(restQueryAnnotation.pathParameters().length == 1);
            assertEquals("wdID", restQueryAnnotation.pathParameters()[0].name());
            assertEquals("Workflow definition id", restQueryAnnotation.pathParameters()[0].description());
            assertTrue(restQueryAnnotation.pathParameters()[0].isRequired());
            assertEquals("", restQueryAnnotation.pathParameters()[0].defaultValue());
            assertEquals(RestParameter.Type.STRING, restQueryAnnotation.pathParameters()[0].type());
            // query parameters
            assertTrue(restQueryAnnotation.restParameters().length == 2);
            // #1
            assertEquals("flavor", restQueryAnnotation.restParameters()[0].name());
            assertEquals("The kind of media track", restQueryAnnotation.restParameters()[0].description());
            assertTrue(restQueryAnnotation.restParameters()[0].isRequired());
            assertEquals("Default", restQueryAnnotation.restParameters()[0].defaultValue());
            assertEquals(RestParameter.Type.STRING, restQueryAnnotation.restParameters()[0].type());
            // #2
            assertEquals("mediaPackage", restQueryAnnotation.restParameters()[1].name());
            assertEquals("The media package as XML", restQueryAnnotation.restParameters()[1].description());
            assertFalse(restQueryAnnotation.restParameters()[1].isRequired());
            assertEquals("", restQueryAnnotation.restParameters()[1].defaultValue());
            assertEquals(RestParameter.Type.TEXT, restQueryAnnotation.restParameters()[1].type());
            // body parameter
            assertEquals("BODY", restQueryAnnotation.bodyParameter().name());
            assertEquals("The media track file", restQueryAnnotation.bodyParameter().description());
            assertTrue(restQueryAnnotation.bodyParameter().isRequired());
            assertEquals("", restQueryAnnotation.bodyParameter().defaultValue());
            assertEquals(RestParameter.Type.FILE, restQueryAnnotation.bodyParameter().type());
            // responses
            assertTrue(restQueryAnnotation.reponses().length == 3);
            assertEquals(HttpServletResponse.SC_OK, restQueryAnnotation.reponses()[0].responseCode());
            assertEquals("Returns augmented media package", restQueryAnnotation.reponses()[0].description());
            assertEquals(HttpServletResponse.SC_BAD_REQUEST, restQueryAnnotation.reponses()[1].responseCode());
            assertEquals("", restQueryAnnotation.reponses()[1].description());
            assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, restQueryAnnotation.reponses()[2].responseCode());
            assertEquals("", restQueryAnnotation.reponses()[2].description());
        }
    } catch (SecurityException e) {
        fail();
    } catch (NoSuchMethodException e) {
        fail();
    }
}
Also used : Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) RestQuery(org.opencastproject.util.doc.rest.RestQuery) HttpMethod(javax.ws.rs.HttpMethod) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) HttpMethod(javax.ws.rs.HttpMethod) Test(org.junit.Test)

Example 12 with HttpMethod

use of javax.ws.rs.HttpMethod in project cxf by apache.

the class Validator method checkMethodsForMultipleHTTPMethodAnnotations.

private static void checkMethodsForMultipleHTTPMethodAnnotations(Method[] clientMethods) throws RestClientDefinitionException {
    Map<String, Class<? extends Annotation>> httpMethods = new HashMap<>();
    for (Method method : clientMethods) {
        for (Annotation anno : method.getAnnotations()) {
            Class<? extends Annotation> annoClass = anno.annotationType();
            HttpMethod verb = annoClass.getAnnotation(HttpMethod.class);
            if (verb != null) {
                httpMethods.put(verb.value(), annoClass);
            }
        }
        if (httpMethods.size() > 1) {
            throwException("VALIDATION_METHOD_WITH_MULTIPLE_VERBS", method, httpMethods.values());
        }
        httpMethods.clear();
    }
}
Also used : HashMap(java.util.HashMap) HttpMethod(javax.ws.rs.HttpMethod) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) HttpMethod(javax.ws.rs.HttpMethod)

Example 13 with HttpMethod

use of javax.ws.rs.HttpMethod in project java-chassis by ServiceComb.

the class GetAnnotationProcessor method process.

@Override
public void process(SwaggerGenerator swaggerGenerator, OperationGenerator operationGenerator, Annotation annotation) {
    HttpMethod httpMethod = annotation.annotationType().getAnnotation(HttpMethod.class);
    operationGenerator.setHttpMethod(httpMethod.value());
}
Also used : HttpMethod(javax.ws.rs.HttpMethod)

Example 14 with HttpMethod

use of javax.ws.rs.HttpMethod in project typescript-generator by vojtechhabarta.

the class JaxrsApplicationParser method parseResourceMethod.

private void parseResourceMethod(Result result, ResourceContext context, Class<?> resourceClass, Method method) {
    final Path pathAnnotation = method.getAnnotation(Path.class);
    // subContext
    context = context.subPath(pathAnnotation);
    final Map<String, Type> pathParamTypes = new LinkedHashMap<>();
    for (Parameter parameter : method.getParameters()) {
        final PathParam pathParamAnnotation = parameter.getAnnotation(PathParam.class);
        if (pathParamAnnotation != null) {
            pathParamTypes.put(pathParamAnnotation.value(), parameter.getParameterizedType());
        }
    }
    context = context.subPathParamTypes(pathParamTypes);
    // JAX-RS specification - 3.3 Resource Methods
    final HttpMethod httpMethod = getHttpMethod(method);
    if (httpMethod != null) {
        // swagger
        final SwaggerOperation swaggerOperation = settings.ignoreSwaggerAnnotations ? new SwaggerOperation() : Swagger.parseSwaggerAnnotations(method);
        if (swaggerOperation.possibleResponses != null) {
            for (SwaggerResponse response : swaggerOperation.possibleResponses) {
                if (response.responseType != null) {
                    foundType(result, response.responseType, resourceClass, method.getName());
                }
            }
        }
        if (swaggerOperation.hidden) {
            return;
        }
        // path parameters
        final List<MethodParameterModel> pathParams = new ArrayList<>();
        final PathTemplate pathTemplate = PathTemplate.parse(context.path);
        for (PathTemplate.Part part : pathTemplate.getParts()) {
            if (part instanceof PathTemplate.Parameter) {
                final PathTemplate.Parameter parameter = (PathTemplate.Parameter) part;
                final Type type = context.pathParamTypes.get(parameter.getName());
                pathParams.add(new MethodParameterModel(parameter.getName(), type != null ? type : String.class));
            }
        }
        // query parameters
        final List<MethodParameterModel> queryParams = new ArrayList<>();
        for (Parameter param : method.getParameters()) {
            final QueryParam queryParamAnnotation = param.getAnnotation(QueryParam.class);
            if (queryParamAnnotation != null) {
                queryParams.add(new MethodParameterModel(queryParamAnnotation.value(), param.getParameterizedType()));
            }
        }
        // JAX-RS specification - 3.3.2.1 Entity Parameters
        final MethodParameterModel entityParameter = getEntityParameter(method);
        if (entityParameter != null) {
            foundType(result, entityParameter.getType(), resourceClass, method.getName());
        }
        // JAX-RS specification - 3.3.3 Return Type
        final Class<?> returnType = method.getReturnType();
        final Type genericReturnType = method.getGenericReturnType();
        final Type modelReturnType;
        if (returnType == void.class) {
            modelReturnType = returnType;
        } else if (returnType == Response.class) {
            if (swaggerOperation.responseType != null) {
                modelReturnType = swaggerOperation.responseType;
                foundType(result, modelReturnType, resourceClass, method.getName());
            } else {
                modelReturnType = Object.class;
            }
        } else if (genericReturnType instanceof ParameterizedType && returnType == GenericEntity.class) {
            final ParameterizedType parameterizedReturnType = (ParameterizedType) genericReturnType;
            modelReturnType = parameterizedReturnType.getActualTypeArguments()[0];
            foundType(result, modelReturnType, resourceClass, method.getName());
        } else {
            modelReturnType = genericReturnType;
            foundType(result, modelReturnType, resourceClass, method.getName());
        }
        // comments
        final List<String> comments = Swagger.getOperationComments(swaggerOperation);
        // create method
        model.getMethods().add(new JaxrsMethodModel(resourceClass, method.getName(), modelReturnType, context.rootResource, httpMethod.value(), context.path, pathParams, queryParams, entityParameter, comments));
    }
    // JAX-RS specification - 3.4.1 Sub Resources
    if (pathAnnotation != null && httpMethod == null) {
        parseResource(result, context, method.getReturnType());
    }
}
Also used : Path(javax.ws.rs.Path) ApplicationPath(javax.ws.rs.ApplicationPath) Response(javax.ws.rs.core.Response) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) QueryParam(javax.ws.rs.QueryParam) GenericEntity(javax.ws.rs.core.GenericEntity) Parameter(java.lang.reflect.Parameter) PathParam(javax.ws.rs.PathParam) HttpMethod(javax.ws.rs.HttpMethod)

Aggregations

HttpMethod (javax.ws.rs.HttpMethod)14 Annotation (java.lang.annotation.Annotation)8 Method (java.lang.reflect.Method)7 Path (javax.ws.rs.Path)7 Produces (javax.ws.rs.Produces)5 Consumes (javax.ws.rs.Consumes)4 Test (org.junit.Test)3 RestQuery (org.opencastproject.util.doc.rest.RestQuery)3 Parameter (java.lang.reflect.Parameter)2 HashMap (java.util.HashMap)2 PathParam (javax.ws.rs.PathParam)2 QueryParam (javax.ws.rs.QueryParam)2 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)1 AnnotatedParameter (com.fasterxml.jackson.databind.introspect.AnnotatedParameter)1 Api (io.swagger.annotations.Api)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponse (io.swagger.annotations.ApiResponse)1