Add PTL as committer
[clamp.git] / src / main / java / org / onap / clamp / clds / filter / ClampDefaultUserFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.clamp.clds.filter;
24
25 import java.io.IOException;
26 import java.util.Arrays;
27
28 import javax.servlet.Filter;
29 import javax.servlet.FilterChain;
30 import javax.servlet.FilterConfig;
31 import javax.servlet.ServletException;
32 import javax.servlet.ServletRequest;
33 import javax.servlet.ServletResponse;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.onap.clamp.clds.config.ClampProperties;
39 import org.onap.clamp.clds.config.ClampUserWrap;
40 import org.onap.clamp.clds.config.CldsUserJsonDecoder;
41 import org.onap.clamp.clds.exception.CldsUsersException;
42 import org.onap.clamp.clds.service.CldsUser;
43
44
45 public class ClampDefaultUserFilter  implements Filter {
46     private CldsUser defaultUser;
47     @Autowired
48     private ClampProperties refProp;
49
50     // Load the default user
51     public void init(FilterConfig cfg) throws ServletException {
52         try { 
53             CldsUser[] users = CldsUserJsonDecoder.decodeJson(refProp.getFileContent("files.cldsUsers"));
54             defaultUser = users[0];
55         } catch (IOException e) {
56             // not able to load default user
57                 throw new CldsUsersException("Exception occurred during the decoding of the clds-users.json", e);
58         }
59   }
60
61     // Call the ClampUserWrapper
62     @Override
63     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
64         HttpServletRequest hreq = (HttpServletRequest)req;      
65         chain.doFilter(new ClampUserWrap(hreq, defaultUser.getUser(), Arrays.asList(defaultUser.getPermissionsString())), res);
66     }
67
68     public void destroy() {
69     }
70 }