AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / env / AuthzTransOnlyFilter.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.env;
23
24 import javax.servlet.ServletRequest;
25 import javax.servlet.http.HttpServletRequest;
26
27 import org.onap.aaf.auth.rserv.TransOnlyFilter;
28 import org.onap.aaf.cadi.principal.TaggedPrincipal;
29 import org.onap.aaf.misc.env.Env;
30 import org.onap.aaf.misc.env.TimeTaken;
31 import org.onap.aaf.misc.env.Trans.Metric;
32
33 public class AuthzTransOnlyFilter extends TransOnlyFilter<AuthzTrans> {
34         private AuthzEnv env;
35         public Metric serviceMetric;
36
37         public static final int BUCKETSIZE = 2;
38
39         public AuthzTransOnlyFilter(AuthzEnv env) {
40                 this.env = env;
41                 serviceMetric = new Metric();
42                 serviceMetric.buckets = new float[BUCKETSIZE]; 
43         }
44         
45         @Override
46         protected AuthzTrans newTrans() {
47                 return env.newTrans();
48         }
49
50         @Override
51         protected TimeTaken start(AuthzTrans trans, ServletRequest request) {
52                 trans.set((HttpServletRequest)request);
53                 return trans.start("Trans " + //(context==null?"n/a":context.toString()) +
54                 " IP: " + trans.ip() +
55                 " Port: " + trans.port()
56                 , Env.SUB);
57         }
58
59         @Override
60         protected void authenticated(AuthzTrans trans, TaggedPrincipal p) {
61                 trans.setUser(p);
62         }
63
64         @Override
65         protected void tallyHo(AuthzTrans trans) {
66                 // Transaction is done, now post
67                 StringBuilder sb = new StringBuilder("AuditTrail\n");
68                 // We'll grab sub-metrics for Remote Calls and JSON
69                 // IMPORTANT!!! if you add more entries here, change "BUCKETSIZE"!!!
70                 Metric m = trans.auditTrail(1, sb, Env.REMOTE,Env.JSON);
71                 // Add current Metrics to total metrics
72                 serviceMetric.total+= m.total;
73                 for(int i=0;i<serviceMetric.buckets.length;++i) {
74                         serviceMetric.buckets[i]+=m.buckets[i];
75                 }
76                 // Log current info
77                 sb.append("  Total: ");
78                 sb.append(m.total);
79                 sb.append(" Remote: ");
80                 sb.append(m.buckets[0]);
81                 sb.append(" JSON: ");
82                 sb.append(m.buckets[1]);
83                 trans.info().log(sb);
84         }
85
86 }