Update vulnerable package dependencies
[sdc/sdc-workflow-designer.git] / sdc-workflow-designer-be / src / main / java / org / onap / sdc / workflow / api / swagger / UserIdReader.java
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.sdc.workflow.api.swagger;
18
19 import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER;
20
21 import com.fasterxml.classmate.TypeResolver;
22 import java.util.Optional;
23 import org.onap.sdc.workflow.services.annotations.UserId;
24 import org.springframework.core.annotation.Order;
25 import org.springframework.stereotype.Component;
26 import springfox.documentation.service.ResolvedMethodParameter;
27 import springfox.documentation.spi.DocumentationType;
28 import springfox.documentation.spi.service.ParameterBuilderPlugin;
29 import springfox.documentation.spi.service.contexts.ParameterContext;
30 import springfox.documentation.swagger.common.SwaggerPluginSupport;
31
32 /**
33  * This component is neccesary because swagger cannot find a custom annotations in API.
34  * This is needed to find specifically the {@link UserId} annotation
35  */
36 @Component
37 @Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1000)
38 public class UserIdReader implements ParameterBuilderPlugin {
39
40     private static final String HEADER = "header";
41     private final TypeResolver resolver;
42
43     public UserIdReader(TypeResolver resolver) {
44         this.resolver = resolver;
45     }
46
47     @Override
48     public void apply(ParameterContext parameterContext) {
49         ResolvedMethodParameter methodParameter = parameterContext.resolvedMethodParameter();
50         Optional<UserId> requestParam = methodParameter.findAnnotation(UserId.class);
51         if (requestParam.isPresent()) {
52             parameterContext.parameterBuilder().parameterType(HEADER).name(USER_ID_HEADER)
53                             .type(resolver.resolve(String.class));
54         }
55     }
56
57     @Override
58     public boolean supports(DocumentationType documentationType) {
59         return true;
60     }
61 }