Modify ONAP PAP REST classes basic checkstyle
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / restAuth / PAPAuthenticationFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.restAuth;
22
23 import java.io.IOException;
24
25 import javax.servlet.Filter;
26 import javax.servlet.FilterChain;
27 import javax.servlet.FilterConfig;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.annotation.WebFilter;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /**
39  * Servlet Filter implementation class PAPAuthenticationFilter
40  */
41 @WebFilter("/*")
42 public class PAPAuthenticationFilter implements Filter {
43
44     private static final Log logger     = LogFactory.getLog(PAPAuthenticationFilter.class);
45     public static final String AUTHENTICATION_HEADER = "Authorization";
46
47     @Override
48     public void doFilter(ServletRequest request, ServletResponse response,
49             FilterChain filter) throws IOException, ServletException {
50
51
52         if (request instanceof HttpServletRequest) {
53             HttpServletRequest httpServletRequest = (HttpServletRequest) request;
54
55             String authCredentials = null;
56             String url = httpServletRequest.getRequestURI();
57
58             logger.info("Request URI: " + url);
59
60             //getting authentication credentials
61             authCredentials = httpServletRequest.getHeader(AUTHENTICATION_HEADER);
62
63             // Check Authentication credentials
64             AuthenticationService authenticationService = new AuthenticationService();
65             boolean authenticationStatus = authenticationService.authenticate(authCredentials);
66
67             if (authenticationStatus) {
68                 //indicates the request comes from Traditional Admin Console or PolicyEngineAPI
69                 if ("/pap/".equals(url)){
70                     logger.info("Request comes from Traditional Admin Console or PolicyEngineAPI");
71                     //forward request to the XACMLPAPServlet if authenticated
72                     request.getRequestDispatcher("/pap/pap/").forward(request, response);
73                 }else if (url.startsWith("/pap/onap/") && response instanceof HttpServletResponse){
74                     //indicates the request comes from the ONAP Portal onap-sdk-app
75                     HttpServletResponse alteredResponse = ((HttpServletResponse)response);
76                     addCorsHeader(alteredResponse);
77                     logger.info("Request comes from Onap Portal");
78                     //Spring dispatcher servlet is at the end of the filter chain at /pap/onap/ path
79                     filter.doFilter(request, response);
80                 }
81             } else {
82                 if (response instanceof HttpServletResponse) {
83                     HttpServletResponse httpServletResponse = (HttpServletResponse) response;
84                     httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
85                 }
86             }
87
88         }
89     }
90
91     //method to add CorsHeaders for onap portal rest call
92     private void addCorsHeader(HttpServletResponse response) {
93         logger.info("Adding Cors Response Headers!!!");
94         response.addHeader("Access-Control-Allow-Origin", "*");
95         response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
96         response.addHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
97         response.addHeader("Access-Control-Max-Age", "1728000");        
98     }
99
100     @Override
101     public void destroy() {
102         //Empty
103     }
104
105     @Override
106     public void init(FilterConfig arg0) throws ServletException {
107         //Empty
108     }
109 }