Search in sources :

Example 41 with Resource

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

the class PrintModelProcessor method findChildResources.

private List<Resource> findChildResources(Resource parentResource) {
    final List<Resource> childResources = new ArrayList<>();
    for (Resource resource : parentResource.getChildResources()) {
        childResources.add(resource);
        childResources.addAll(findChildResources(resource));
    }
    return childResources;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.glassfish.jersey.server.model.Resource)

Example 42 with Resource

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

the class PrefixAddingModelProcessorTest method processResourceModelAddsPrefixToResourceClassInCorrectPackage.

@Test
public void processResourceModelAddsPrefixToResourceClassInCorrectPackage() throws Exception {
    final ImmutableMap<String, String> packagePrefixes = ImmutableMap.of(PACKAGE_NAME, "/test/prefix");
    final PrefixAddingModelProcessor modelProcessor = new PrefixAddingModelProcessor(packagePrefixes);
    final ResourceModel originalResourceModel = new ResourceModel.Builder(false).addResource(Resource.from(TestResource.class)).build();
    final ResourceModel resourceModel = modelProcessor.processResourceModel(originalResourceModel, new ResourceConfig());
    assertThat(resourceModel.getResources()).hasSize(1);
    final Resource resource = resourceModel.getResources().get(0);
    assertThat(resource.getPath()).isEqualTo("/test/prefix/foobar/{test}");
}
Also used : Resource(org.glassfish.jersey.server.model.Resource) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 43 with Resource

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

the class PrefixAddingModelProcessorTest method processResourceModelDoesNotAddPrefixToResourceClassInOtherPackage.

@Test
public void processResourceModelDoesNotAddPrefixToResourceClassInOtherPackage() throws Exception {
    final ImmutableMap<String, String> packagePrefixes = ImmutableMap.of("org.example", "/test/prefix");
    final PrefixAddingModelProcessor modelProcessor = new PrefixAddingModelProcessor(packagePrefixes);
    final ResourceModel originalResourceModel = new ResourceModel.Builder(false).addResource(Resource.from(TestResource.class)).build();
    final ResourceModel resourceModel = modelProcessor.processResourceModel(originalResourceModel, new ResourceConfig());
    assertThat(resourceModel.getResources()).hasSize(1);
    final Resource resource = resourceModel.getResources().get(0);
    assertThat(resource.getPath()).isEqualTo("/foobar/{test}");
}
Also used : Resource(org.glassfish.jersey.server.model.Resource) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 44 with Resource

use of org.glassfish.jersey.server.model.Resource 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

Resource (org.glassfish.jersey.server.model.Resource)44 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)12 ResourceModel (org.glassfish.jersey.server.model.ResourceModel)12 Test (org.junit.Test)10 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)9 ArrayList (java.util.ArrayList)7 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)7 Response (javax.ws.rs.core.Response)6 HashSet (java.util.HashSet)4 RuntimeResource (org.glassfish.jersey.server.model.RuntimeResource)4 Inflector (org.glassfish.jersey.process.Inflector)3 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)3 Type (java.lang.reflect.Type)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 Configuration (javax.ws.rs.core.Configuration)2 ComponentBag (org.glassfish.jersey.model.internal.ComponentBag)2 ModelProcessor (org.glassfish.jersey.server.model.ModelProcessor)2 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)1