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