Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / rserv / TransOnlyFilter.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 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
22 package org.onap.aaf.auth.rserv;
23
24 import java.io.IOException;
25
26 import javax.servlet.Filter;
27 import javax.servlet.FilterChain;
28 import javax.servlet.FilterConfig;
29 import javax.servlet.ServletException;
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.onap.aaf.cadi.principal.TaggedPrincipal;
36 import org.onap.aaf.misc.env.TimeTaken;
37 import org.onap.aaf.misc.env.TransStore;
38
39 /**
40  * Create a new Transaction Object for each and every incoming Transaction
41  *
42  * Attach to Request.  User "FilterHolder" mechanism to retain single instance.
43  *
44  * TransFilter includes CADIFilter as part of the package, so that it can
45  * set User Data, etc, as necessary.
46  *
47  * @author Jonathan
48  *
49  */
50 public abstract class TransOnlyFilter<TRANS extends TransStore> implements Filter {
51     @Override
52     public void init(FilterConfig filterConfig) throws ServletException {
53     }
54
55
56
57     protected abstract TRANS newTrans(HttpServletRequest req, HttpServletResponse resp);
58     protected abstract TimeTaken start(TRANS trans);
59     protected abstract void authenticated(TRANS trans, TaggedPrincipal p);
60     protected abstract void tallyHo(TRANS trans);
61
62     @Override
63     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
64         TRANS trans = newTrans((HttpServletRequest)request,(HttpServletResponse)response);
65         TimeTaken overall = start(trans);
66         try {
67             request.setAttribute(TransFilter.TRANS_TAG, trans);
68             chain.doFilter(request, response);
69         } finally {
70             overall.done();
71         }
72         tallyHo(trans);
73     }
74
75     @Override
76     public void destroy() {
77     };
78 }