AT&T 2.0.19 Code drop, stage 3
[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
33 import org.onap.aaf.cadi.principal.TaggedPrincipal;
34 import org.onap.aaf.misc.env.TimeTaken;
35 import org.onap.aaf.misc.env.TransStore;
36
37 /**
38  * Create a new Transaction Object for each and every incoming Transaction
39  * 
40  * Attach to Request.  User "FilterHolder" mechanism to retain single instance.
41  * 
42  * TransFilter includes CADIFilter as part of the package, so that it can
43  * set User Data, etc, as necessary.
44  * 
45  * @author Jonathan
46  *
47  */
48 public abstract class TransOnlyFilter<TRANS extends TransStore> implements Filter {
49         @Override
50         public void init(FilterConfig filterConfig) throws ServletException {
51         }
52         
53
54
55         protected abstract TRANS newTrans();
56         protected abstract TimeTaken start(TRANS trans, ServletRequest request);
57         protected abstract void authenticated(TRANS trans, TaggedPrincipal p);
58         protected abstract void tallyHo(TRANS trans);
59         
60         @Override
61         public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
62                 TRANS trans = newTrans();
63                 
64                 TimeTaken overall = start(trans,request);
65                 try {
66                         request.setAttribute(TransFilter.TRANS_TAG, trans);
67                         chain.doFilter(request, response);
68                 } finally {
69                         overall.done();
70                 }
71                 tallyHo(trans);
72         }
73
74         @Override
75         public void destroy() {
76         };
77 }