Sonar Fixes, Formatting
[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.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
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(HttpServletRequest req, HttpServletResponse resp) {
47         AuthzTrans trans = env.newTrans();
48         trans.set(req, resp);
49         return trans;
50     }
51
52     @Override
53     protected TimeTaken start(AuthzTrans trans) {
54         return trans.start("Trans " + //(context==null?"n/a":context.toString()) +
55         " IP: " + trans.ip() +
56         " Port: " + trans.port()
57         , Env.SUB);
58     }
59
60     @Override
61     protected void authenticated(AuthzTrans trans, TaggedPrincipal p) {
62         trans.setUser(p);
63     }
64
65     @Override
66     protected void tallyHo(AuthzTrans trans) {
67         // Transaction is done, now post
68         StringBuilder sb = new StringBuilder("AuditTrail\n");
69         // We'll grab sub-metrics for Remote Calls and JSON
70         // IMPORTANT!!! if you add more entries here, change "BUCKETSIZE"!!!
71         Metric m = trans.auditTrail(1, sb, Env.REMOTE,Env.JSON);
72         // Add current Metrics to total metrics
73         serviceMetric.total+= m.total;
74         for (int i=0;i<serviceMetric.buckets.length;++i) {
75             serviceMetric.buckets[i]+=m.buckets[i];
76         }
77         // Log current info
78         sb.append("  Total: ");
79         sb.append(m.total);
80         sb.append(" Remote: ");
81         sb.append(m.buckets[0]);
82         sb.append(" JSON: ");
83         sb.append(m.buckets[1]);
84         trans.info().log(sb);
85     }
86
87 }