Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-aaf-auth / src / main / java / org / onap / aai / aaf / filters / AafFilter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * <p>
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * <p>
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.aai.aaf.filters;
22
23 import java.io.IOException;
24
25 import javax.servlet.FilterChain;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.onap.aaf.cadi.PropAccess;
31 import org.onap.aaf.cadi.filter.CadiFilter;
32 import org.onap.aai.aaf.auth.ResponseFormatter;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.boot.web.servlet.filter.OrderedRequestContextFilter;
37 import org.springframework.context.annotation.Profile;
38 import org.springframework.stereotype.Component;
39
40 /**
41  * AAF authentication filter
42  */
43
44 @Component
45 @Profile(AafProfiles.AAF_AUTHENTICATION)
46 public class AafFilter extends OrderedRequestContextFilter {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(AafCertFilter.class);
49
50     private final CadiFilter cadiFilter;
51
52     @Autowired
53     public AafFilter(CadiProps cadiProps) throws IOException, ServletException {
54         cadiFilter = new CadiFilter(new PropAccess((level, element) -> {
55             switch (level) {
56                 case DEBUG:
57                     LOGGER.debug(buildMsg(element));
58                     break;
59                 case INFO:
60                 case AUDIT:
61                     LOGGER.info(buildMsg(element));
62                     break;
63                 case WARN:
64                     LOGGER.warn(buildMsg(element));
65                     break;
66                 case ERROR:
67                     LOGGER.error(buildMsg(element));
68                     break;
69                 case INIT:
70                     LOGGER.info(buildMsg(element));
71                     break;
72                 case TRACE:
73                     LOGGER.trace(buildMsg(element));
74                     break;
75                 case NONE:
76                     break;
77             }
78         }, new String[] {"cadi_prop_files=" + cadiProps.getCadiFileName()}));
79         this.setOrder(FilterPriority.AAF_AUTHENTICATION.getPriority());
80     }
81
82     @Override
83     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
84             throws IOException, ServletException {
85         if (!request.getRequestURI().matches("^.*/util/echo$")) {
86             cadiFilter.doFilter(request, response, filterChain);
87             if (response.getStatus() == 401 || response.getStatus() == 403) {
88                 ResponseFormatter.errorResponse(request, response);
89             }
90         } else {
91             filterChain.doFilter(request, response);
92         }
93     }
94
95     private String buildMsg(Object[] objects) {
96         StringBuilder sb = new StringBuilder();
97         boolean first = true;
98         for (Object o : objects) {
99             if (first) {
100                 first = false;
101             } else {
102                 sb.append(' ');
103             }
104             sb.append(o.toString());
105         }
106         return (sb.toString());
107     }
108 }