Search in sources :

Example 21 with ResourceMethod

use of org.glassfish.jersey.server.model.ResourceMethod in project jersey by jersey.

the class IntrospectionModellerTest method testCreateResource.

@Test
public /**
     * Test of createResource method, of class IntrospectionModeller.
     */
void testCreateResource() {
    Class<?> resourceClass;
    Resource result;
    List<ResourceMethod> resourceMethods;
    ResourceMethod resourceMethod;
    // HelloWorldResource
    resourceClass = HelloWorldResource.class;
    result = Resource.builder(resourceClass).build();
    resourceMethods = result.getResourceMethods();
    assertEquals("Unexpected number of resource methods in the resource model.", 2, resourceMethods.size());
    resourceMethod = find(resourceMethods, "postA");
    assertEquals("Unexpected number of produced media types in the resource method model", 3, resourceMethod.getProducedTypes().size());
    assertEquals("Unexpected number of consumed media types in the resource method model", 2, resourceMethod.getConsumedTypes().size());
    assertEquals("Unexpected number of handler parameters", 0, resourceMethod.getInvocable().getHandler().getParameters().size());
    resourceMethod = find(resourceMethods, "postB");
    assertEquals("Unexpected number of inherited produced media types in the resource method model", 2, resourceMethod.getProducedTypes().size());
    assertEquals("Unexpected number of inherited consumed media types in the resource method model", 3, resourceMethod.getConsumedTypes().size());
    assertEquals("Unexpected number of handler parameters", 0, resourceMethod.getInvocable().getHandler().getParameters().size());
    // OtherResource
    resourceClass = OtherResource.class;
    result = Resource.builder(resourceClass).build();
    resourceMethods = result.getResourceMethods();
    assertEquals("Unexpected number of resource methods in the resource model.", 2, resourceMethods.size());
    resourceMethod = find(resourceMethods, "get");
    assertEquals("Unexpected number of produced media types in the resource method model", 0, resourceMethod.getProducedTypes().size());
    assertEquals("Unexpected number of consumed media types in the resource method model", 0, resourceMethod.getConsumedTypes().size());
    assertEquals("Unexpected number of handler parameters", 5, resourceMethod.getInvocable().getHandler().getParameters().size());
    assertSources(resourceMethod.getInvocable().getHandler().getParameters(), Parameter.Source.CONTEXT, Parameter.Source.PATH, Parameter.Source.QUERY, // @Inject on field
    Parameter.Source.UNKNOWN, // @Inject on setter
    Parameter.Source.UNKNOWN);
    resourceMethod = find(resourceMethods, "post");
    assertEquals("Unexpected number of inherited produced media types in the resource method model", 0, resourceMethod.getProducedTypes().size());
    assertEquals("Unexpected number of inherited consumed media types in the resource method model", 0, resourceMethod.getConsumedTypes().size());
    assertEquals("Unexpected number of handler parameters", 5, resourceMethod.getInvocable().getHandler().getParameters().size());
    assertSources(resourceMethod.getInvocable().getHandler().getParameters(), Parameter.Source.CONTEXT, Parameter.Source.PATH, Parameter.Source.QUERY, // @Inject on field
    Parameter.Source.UNKNOWN, // @Inject on setter
    Parameter.Source.UNKNOWN);
}
Also used : Resource(org.glassfish.jersey.server.model.Resource) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Test(org.junit.Test)

Example 22 with ResourceMethod

use of org.glassfish.jersey.server.model.ResourceMethod in project graylog2-server by Graylog2.

the class AuditEventModelProcessor method checkResources.

private void checkResources(List<Resource> resources) {
    for (Resource resource : resources) {
        for (ResourceMethod method : resource.getResourceMethods()) {
            final Method m = method.getInvocable().getDefinitionMethod();
            if (m.isAnnotationPresent(POST.class) || m.isAnnotationPresent(PUT.class) || m.isAnnotationPresent(DELETE.class)) {
                if (!m.isAnnotationPresent(AuditEvent.class) && !m.isAnnotationPresent(NoAuditEvent.class)) {
                    LOG.warn("REST endpoint not included in audit trail: {}", String.format(Locale.US, "%6s %s", method.getHttpMethod(), getPathFromResource(resource)));
                    LOG.debug("Missing @AuditEvent or @NoAuditEvent annotation: {}#{}", m.getDeclaringClass().getCanonicalName(), m.getName());
                } else {
                    if (m.isAnnotationPresent(AuditEvent.class)) {
                        final AuditEvent annotation = m.getAnnotation(AuditEvent.class);
                        if (!auditEventTypes.contains(annotation.type())) {
                            LOG.warn("REST endpoint does not use a registered audit type: {} (type: \"{}\")", String.format(Locale.US, "%6s %s", method.getHttpMethod(), getPathFromResource(resource)), annotation.type());
                            LOG.debug("Make sure the audit event types are registered in a class that implements PluginAuditEventTypes: {}#{}", m.getDeclaringClass().getCanonicalName(), m.getName());
                        }
                    } else if (m.isAnnotationPresent(NoAuditEvent.class)) {
                        final NoAuditEvent annotation = m.getAnnotation(NoAuditEvent.class);
                        if (isNullOrEmpty(annotation.value())) {
                            LOG.warn("REST endpoint uses @NoAuditEvent annotation with an empty value: {}", String.format(Locale.US, "%6s %s", method.getHttpMethod(), getPathFromResource(resource)));
                        }
                    }
                }
            }
        }
        // Make sure to also check all child resources! Otherwise some resources will not be checked.
        checkResources(resource.getChildResources());
    }
}
Also used : DELETE(javax.ws.rs.DELETE) RestTools.getPathFromResource(org.graylog2.rest.RestTools.getPathFromResource) Resource(org.glassfish.jersey.server.model.Resource) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Aggregations

ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)22 Resource (org.glassfish.jersey.server.model.Resource)9 Method (java.lang.reflect.Method)6 Invocable (org.glassfish.jersey.server.model.Invocable)5 MediaType (javax.ws.rs.core.MediaType)4 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)3 HashSet (java.util.HashSet)3 AcceptableMediaType (org.glassfish.jersey.message.internal.AcceptableMediaType)3 RuntimeResource (org.glassfish.jersey.server.model.RuntimeResource)3 Metered (com.codahale.metrics.annotation.Metered)2 Timed (com.codahale.metrics.annotation.Timed)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 HttpMethod (javax.ws.rs.HttpMethod)2 ExtendedUriInfo (org.glassfish.jersey.server.ExtendedUriInfo)2 ResourceModelComponent (org.glassfish.jersey.server.model.ResourceModelComponent)2 ResourceMethodStatistics (org.glassfish.jersey.server.monitoring.ResourceMethodStatistics)2 Test (org.junit.Test)2