2 * Copyright © 2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.sdc.workflow.server.resolvers;
19 import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER;
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;
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.
38 public class UserIdResolver implements HandlerMethodArgumentResolver {
40 private static final String ERROR_MESSAGE = "Missing mandatory request header '" + USER_ID_HEADER + "'";
43 public boolean supportsParameter(MethodParameter methodParameter) {
45 if (!methodParameter.hasParameterAnnotation(UserId.class)) {
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());
59 public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer,
60 NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory)
61 throws ServletRequestBindingException {
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);