1edfad3bbc5f3359d38d0e6f0fd903a6865df16d
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 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.openecomp.sdc.itempermissions.servlet;
18
19 import java.io.IOException;
20 import javax.servlet.Filter;
21 import javax.servlet.FilterChain;
22 import javax.servlet.FilterConfig;
23 import javax.servlet.ServletException;
24 import javax.servlet.ServletRequest;
25 import javax.servlet.ServletResponse;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.ws.rs.HttpMethod;
29 import javax.ws.rs.core.Response;
30
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import org.openecomp.sdc.common.errors.ErrorCode;
33 import org.openecomp.sdc.common.errors.ErrorCodeAndMessage;
34 import org.openecomp.sdc.common.errors.Messages;
35 import org.openecomp.sdc.itempermissions.PermissionsServices;
36 import org.openecomp.sdc.itempermissions.PermissionsServicesFactory;
37 import org.openecomp.sdc.logging.api.Logger;
38 import org.openecomp.sdc.logging.api.LoggerFactory;
39
40 /**
41  * Created by ayalaben on 6/27/2017.
42  */
43 public class PermissionsFilter implements Filter {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(PermissionsFilter.class);
46     private final PermissionsServices permissionsServices;
47     private static final String IRRELEVANT_REQUEST = "Irrelevant_Request";
48     private static final String EDIT_ITEM = "Edit_Item";
49
50     public PermissionsFilter() {
51         this(PermissionsServicesFactory.getInstance().createInterface());
52     }
53
54     PermissionsFilter(PermissionsServices permissionsServices) {
55         this.permissionsServices = permissionsServices;
56     }
57
58     @Override
59     public void init(FilterConfig filterConfig) {
60         // required by servlet API
61     }
62
63     @Override
64     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
65             throws IOException, ServletException {
66
67         if ((servletRequest instanceof HttpServletRequest)
68                     && isRelevant((HttpServletRequest) servletRequest, servletResponse)) {
69             filterChain.doFilter(servletRequest, servletResponse);
70         }
71     }
72
73     private boolean isRelevant(HttpServletRequest servletRequest, ServletResponse servletResponse) throws IOException {
74         String method = servletRequest.getMethod();
75         if (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT) || method.equals(HttpMethod.DELETE)) {
76
77             String userId = servletRequest.getHeader("USER_ID");
78             String itemId = parseItemIdFromPath(servletRequest.getPathInfo());
79
80             if (!itemId.equals(IRRELEVANT_REQUEST) && !permissionsServices.isAllowed(itemId, userId, EDIT_ITEM)) {
81                 ((HttpServletResponse) servletResponse).setStatus(HttpServletResponse.SC_FORBIDDEN);
82                 servletResponse.getWriter().print(buildResponse(Response.Status.FORBIDDEN,
83                         Messages.PERMISSIONS_ERROR.getErrorMessage(),
84                         Messages.PERMISSIONS_ERROR.name()));
85                 return false;
86             }
87         }
88
89         return true;
90     }
91
92     private String parseItemIdFromPath(String pathInfo) {
93         String[] tokens = pathInfo.split("/");
94         if (tokens.length < 4) {
95             return IRRELEVANT_REQUEST;
96         } else {
97             return tokens[3];
98         }
99     }
100
101     @Override
102     public void destroy() {
103         // required by serlvet API
104     }
105
106     private String buildResponse(Response.Status status, String message, String id) {
107         ErrorCode errorCode = new ErrorCode.ErrorCodeBuilder()
108                                       .withId(id)
109                                       .withMessage(message).build();
110         return objectToJsonString(new ErrorCodeAndMessage(status, errorCode));
111     }
112
113     private String objectToJsonString(Object obj) {
114         try {
115             return new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(obj);
116         } catch (Exception e) {
117             LOGGER.error(e.getMessage(), e);
118             return "An internal error has occurred. Please contact support.";
119         }
120     }
121 }