AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-hello / src / main / java / org / onap / aaf / auth / hello / AAF_Hello.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
23 package org.onap.aaf.auth.hello;
24
25 import java.util.Map;
26
27 import javax.servlet.Filter;
28
29 import org.onap.aaf.auth.cache.Cache.Dated;
30 import org.onap.aaf.auth.env.AuthzEnv;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.env.AuthzTransFilter;
33 import org.onap.aaf.auth.rserv.HttpCode;
34 import org.onap.aaf.auth.rserv.HttpMethods;
35 import org.onap.aaf.auth.server.AbsService;
36 import org.onap.aaf.auth.server.JettyServiceStarter;
37 import org.onap.aaf.cadi.CadiException;
38 import org.onap.aaf.cadi.LocatorException;
39 import org.onap.aaf.cadi.PropAccess;
40 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
41 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
42 import org.onap.aaf.cadi.aaf.v2_0.AAFTrustChecker;
43 import org.onap.aaf.cadi.config.Config;
44 import org.onap.aaf.cadi.register.Registrant;
45 import org.onap.aaf.cadi.register.RemoteRegistrant;
46 import org.onap.aaf.misc.env.APIException;
47 import org.onap.aaf.misc.env.Env;
48
49 public class AAF_Hello extends AbsService<AuthzEnv,AuthzTrans> {
50         public enum API{TOKEN_REQ, TOKEN,INTROSPECT, ERROR,VOID};
51         public Map<String, Dated> cacheUser;
52         public AAFAuthn<?> aafAuthn;
53         public AAFLurPerm aafLurPerm;
54         
55         /**
56          * Construct AuthzAPI with all the Context Supporting Routes that Authz needs
57          * 
58          * @param env
59          * @param si 
60          * @param dm 
61          * @param decryptor 
62          * @throws APIException 
63          */
64         public AAF_Hello(final AuthzEnv env) throws Exception {
65                 super(env.access(), env);
66                 
67                 aafLurPerm = aafCon().newLur();
68                 // Note: If you need both Authn and Authz construct the following:
69                 aafAuthn = aafCon().newAuthn(aafLurPerm);
70
71                 String aaf_env = env.getProperty(Config.AAF_ENV);
72                 if(aaf_env==null) {
73                         throw new APIException("aaf_env needs to be set");
74                 }
75                 
76                 // Initialize Facade for all uses
77                 AuthzTrans trans = env.newTrans();
78                 StringBuilder sb = new StringBuilder();
79                 trans.auditTrail(2, sb);
80                 trans.init().log(sb);
81                 
82                 API_Hello.init(this);
83 }
84         
85         /**
86          * Setup XML and JSON implementations for each supported Version type
87          * 
88          * We do this by taking the Code passed in and creating clones of these with the appropriate Facades and properties
89          * to do Versions and Content switches
90          * 
91          */
92         public void route(HttpMethods meth, String path, API api, HttpCode<AuthzTrans, AAF_Hello> code) throws Exception {
93                 String version = "1.0";
94                 // Get Correct API Class from Mapper
95                 route(env,meth,path,code,"text/plain;version="+version,"*/*");
96         }
97         
98         @Override
99         public Filter[] filters() throws CadiException, LocatorException {
100                 try {
101                         return new Filter[] {
102                                         new AuthzTransFilter(env,aafCon(),
103                                                 new AAFTrustChecker((Env)env))
104                                 };
105                 } catch (NumberFormatException e) {
106                         throw new CadiException("Invalid Property information", e);
107                 }
108         }
109
110         @SuppressWarnings("unchecked")
111         @Override
112         public Registrant<AuthzEnv>[] registrants(final int port) throws CadiException, LocatorException {
113                 return new Registrant[] {
114                         new RemoteRegistrant<AuthzEnv>(aafCon(),app_name,app_version,port)
115                 };
116         }
117
118         public static void main(final String[] args) {
119                 PropAccess propAccess = new PropAccess(args);
120                 try {
121                         AAF_Hello service = new AAF_Hello(new AuthzEnv(propAccess));
122 //                      env.setLog4JNames("log4j.properties","authz","hello","audit","init","trace");
123                         JettyServiceStarter<AuthzEnv,AuthzTrans> jss = new JettyServiceStarter<AuthzEnv,AuthzTrans>(service);
124                         jss.start();
125                 } catch (Exception e) {
126                         e.printStackTrace();
127                 }
128         }
129 }