Cenralized handling of USER_ID header
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / main / java / org / onap / sdc / workflow / server / resolvers / UserIdResolver.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.server.resolvers;
18
19 import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER;
20
21 import java.util.Objects;
22 import javax.servlet.http.HttpServletRequest;
23 import org.onap.sdc.workflow.services.annotations.UserId;
24 import org.springframework.core.MethodParameter;
25 import org.springframework.web.bind.ServletRequestBindingException;
26 import org.springframework.web.bind.support.WebDataBinderFactory;
27 import org.springframework.web.context.request.NativeWebRequest;
28 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
29 import org.springframework.web.method.support.ModelAndViewContainer;
30
31 /**
32  * Resolves a user ID from an HTTP header and injects it into a parameter of type {@link String} annotated with {@link
33  * UserId}. The header is considered mandatory, therefore an error is returned to the client if no user ID was sent.
34  *
35  * @author evitaliy
36  * @since 21 Aug 2018
37  */
38 public class UserIdResolver implements HandlerMethodArgumentResolver {
39
40     private static final String ERROR_MESSAGE = "Missing mandatory request header '" + USER_ID_HEADER + "'";
41
42     @Override
43     public boolean supportsParameter(MethodParameter methodParameter) {
44
45         if (!methodParameter.hasParameterAnnotation(UserId.class)) {
46             return false;
47         }
48
49         Class<?> parameterType = methodParameter.getParameterType();
50         if (!parameterType.equals(String.class)) {
51             throw new IllegalStateException("Cannot inject user ID into a parameter of type "
52                                                     + parameterType.getTypeName());
53         }
54
55         return true;
56     }
57
58     @Override
59     public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer,
60             NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory)
61             throws ServletRequestBindingException {
62
63         HttpServletRequest httpServletRequest = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
64         String userHeader = Objects.requireNonNull(httpServletRequest).getHeader(USER_ID_HEADER);
65         if (userHeader == null) {
66             throw new ServletRequestBindingException(ERROR_MESSAGE);
67         }
68
69         return userHeader;
70     }
71 }