diff --git a/domino-rest-processor/src/main/java/org/dominokit/rest/processor/RequestFactorySourceWriter.java b/domino-rest-processor/src/main/java/org/dominokit/rest/processor/RequestFactorySourceWriter.java index 3d21660..62be8e4 100644 --- a/domino-rest-processor/src/main/java/org/dominokit/rest/processor/RequestFactorySourceWriter.java +++ b/domino-rest-processor/src/main/java/org/dominokit/rest/processor/RequestFactorySourceWriter.java @@ -56,6 +56,7 @@ public class RequestFactorySourceWriter extends AbstractSourceBuilder { private final Element serviceElement; + private final Set metadataAnnotationTypes; private final String requestsServiceRoot; private Map methodCount; @@ -64,6 +65,7 @@ public RequestFactorySourceWriter( super(processingEnvironment); this.serviceElement = serviceElement; this.requestsServiceRoot = serviceElement.getAnnotation(RequestFactory.class).serviceRoot(); + this.metadataAnnotationTypes = new HashSet<>(); ObjectMapperProcessor.elementUtils = elements; ObjectMapperProcessor.typeUtils = types; @@ -72,10 +74,14 @@ public RequestFactorySourceWriter( } public RequestFactorySourceWriter( - Element serviceElement, String serviceRoot, ProcessingEnvironment processingEnvironment) { + Element serviceElement, + String serviceRoot, + Set metadataAnnotationTypes, + ProcessingEnvironment processingEnvironment) { super(processingEnvironment); this.serviceElement = serviceElement; this.requestsServiceRoot = serviceRoot; + this.metadataAnnotationTypes = metadataAnnotationTypes; ObjectMapperProcessor.elementUtils = elements; ObjectMapperProcessor.typeUtils = types; @@ -792,6 +798,8 @@ private MethodSpec constructor(TypeMirror requestBean, ServiceMethod serviceMeth .addStatement("setAccept(new String[]{$L})", getAcceptResponse(serviceMethod)) .addStatement("setPath($S)", getPath(serviceMethod)) .addStatement("setServiceRoot($S)", getServiceRoot(serviceMethod.method)); + Optional metaDataAnnotations = getMetaDataAnnotations(serviceMethod.method); + metaDataAnnotations.ifPresent(annotation -> constructorBuilder.addStatement("setMetaParameter($S, $S)", annotation, "true")); if (!consumesMultipartForm(serviceMethod)) { constructorBuilder.addStatement( @@ -1094,6 +1102,20 @@ private Optional getNullQueryParamStrategy( return Optional.empty(); } + private Optional getMetaDataAnnotations(ExecutableElement method) { + Optional metadata = Optional.empty(); + for (AnnotationMirror annotatedOnMethod : method.getAnnotationMirrors()) { + for (TypeMirror metadataAnnotation : this.metadataAnnotationTypes) { + if (annotatedOnMethod.toString().equals("@" + metadataAnnotation.toString())) { + metadata = + Optional.of( + annotatedOnMethod.getAnnotationType().asElement().getSimpleName().toString()); + } + } + } + return metadata; + } + private String getHttpMethod(ServiceMethod serviceMethod) { ExecutableElement method = serviceMethod.method; diff --git a/domino-rest-processor/src/main/java/org/dominokit/rest/processor/ResourceListProcessingStep.java b/domino-rest-processor/src/main/java/org/dominokit/rest/processor/ResourceListProcessingStep.java index ca09e93..a1594a4 100644 --- a/domino-rest-processor/src/main/java/org/dominokit/rest/processor/ResourceListProcessingStep.java +++ b/domino-rest-processor/src/main/java/org/dominokit/rest/processor/ResourceListProcessingStep.java @@ -23,6 +23,7 @@ import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.Element; import javax.lang.model.type.TypeMirror; +import org.dominokit.rest.shared.request.service.annotations.MetaDataAnnotations; import org.dominokit.rest.shared.request.service.annotations.RequestFactory; import org.dominokit.rest.shared.request.service.annotations.ResourceList; @@ -65,12 +66,17 @@ private void generateFactory(Element resourceListElement) { new HashSet<>( processorUtil.getClassArrayValueFromAnnotation( resourceListElement, ResourceList.class, "value")); + Set metadataAnnotationTypes = + new HashSet<>( + processorUtil.getClassArrayValueFromAnnotation( + resourceListElement, MetaDataAnnotations.class, "value")); resourceTypes.forEach( typeMirror -> { Element element = types.asElement(typeMirror); writeSource( - new RequestFactorySourceWriter(element, resourceList.serviceRoot(), processingEnv) + new RequestFactorySourceWriter( + element, resourceList.serviceRoot(), metadataAnnotationTypes, processingEnv) .asTypeBuilder(), elements.getPackageOf(resourceListElement).getQualifiedName().toString()); }); diff --git a/domino-rest-processor/src/test/java/org/dominokit/rest/processor/JwtRequired.java b/domino-rest-processor/src/test/java/org/dominokit/rest/processor/JwtRequired.java new file mode 100644 index 0000000..253ac48 --- /dev/null +++ b/domino-rest-processor/src/test/java/org/dominokit/rest/processor/JwtRequired.java @@ -0,0 +1,18 @@ +/* + * Copyright © 2019 Dominokit + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dominokit.rest.processor; + +public @interface JwtRequired {} diff --git a/domino-rest-processor/src/test/java/org/dominokit/rest/processor/NoFactoryAnnotationSampleService.java b/domino-rest-processor/src/test/java/org/dominokit/rest/processor/NoFactoryAnnotationSampleService.java index b088d93..c02de57 100644 --- a/domino-rest-processor/src/test/java/org/dominokit/rest/processor/NoFactoryAnnotationSampleService.java +++ b/domino-rest-processor/src/test/java/org/dominokit/rest/processor/NoFactoryAnnotationSampleService.java @@ -27,6 +27,7 @@ public interface NoFactoryAnnotationSampleService { @GET + @JwtRequired @Path("someService/{id}") SampleResponse getById(@PathParam("id") @RequestBody int id, int count); diff --git a/domino-rest-processor/src/test/java/org/dominokit/rest/processor/package-info.java b/domino-rest-processor/src/test/java/org/dominokit/rest/processor/package-info.java index 4546b0a..5745ea1 100644 --- a/domino-rest-processor/src/test/java/org/dominokit/rest/processor/package-info.java +++ b/domino-rest-processor/src/test/java/org/dominokit/rest/processor/package-info.java @@ -14,6 +14,8 @@ * limitations under the License. */ @ResourceList({NoFactoryAnnotationSampleService.class, NoFactoryAnnotationSampleService.IntX.class}) +@MetaDataAnnotations({JwtRequired.class}) package org.dominokit.rest.processor; +import org.dominokit.rest.shared.request.service.annotations.MetaDataAnnotations; import org.dominokit.rest.shared.request.service.annotations.ResourceList; diff --git a/domino-rest-shared/src/main/java/org/dominokit/rest/shared/request/service/annotations/MetaDataAnnotations.java b/domino-rest-shared/src/main/java/org/dominokit/rest/shared/request/service/annotations/MetaDataAnnotations.java new file mode 100644 index 0000000..06f24d3 --- /dev/null +++ b/domino-rest-shared/src/main/java/org/dominokit/rest/shared/request/service/annotations/MetaDataAnnotations.java @@ -0,0 +1,38 @@ +/* + * Copyright © 2019 Dominokit + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dominokit.rest.shared.request.service.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation is used to locate the services without annotating them with {@link + * RequestFactory}. + * + *

This is helpful when services interfaces not part of the project and they don't have {@link + * RequestFactory} annotation. Including these services are done by providing them inside this + * annotation and define it on package-info.java + * + * @see RequestFactory + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.PACKAGE) +public @interface MetaDataAnnotations { + + Class[] value() default {}; +}