b20bf1681017a66e2cb6013336d0530e4644bb08
[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.io.File;
26 import java.util.Map;
27
28 import javax.servlet.Filter;
29
30 import org.onap.aaf.auth.cache.Cache.Dated;
31 import org.onap.aaf.auth.env.AuthzEnv;
32 import org.onap.aaf.auth.env.AuthzTrans;
33 import org.onap.aaf.auth.env.AuthzTransFilter;
34 import org.onap.aaf.auth.rserv.HttpCode;
35 import org.onap.aaf.auth.rserv.HttpMethods;
36 import org.onap.aaf.auth.server.AbsService;
37 import org.onap.aaf.auth.server.JettyServiceStarter;
38 import org.onap.aaf.auth.server.Log4JLogIt;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.cadi.LocatorException;
41 import org.onap.aaf.cadi.PropAccess;
42 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
43 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
44 import org.onap.aaf.cadi.aaf.v2_0.AAFTrustChecker;
45 import org.onap.aaf.cadi.config.Config;
46 import org.onap.aaf.cadi.register.Registrant;
47 import org.onap.aaf.cadi.register.RemoteRegistrant;
48 import org.onap.aaf.misc.env.APIException;
49 import org.onap.aaf.misc.env.Env;
50
51 public class AAF_Hello extends AbsService<AuthzEnv,AuthzTrans> {
52         public enum API{TOKEN_REQ, TOKEN,INTROSPECT, ERROR,VOID};
53         public Map<String, Dated> cacheUser;
54         public AAFAuthn<?> aafAuthn;
55         public AAFLurPerm aafLurPerm;
56         
57         /**
58          * Construct AuthzAPI with all the Context Supporting Routes that Authz needs
59          * 
60          * @param env
61          * @param si 
62          * @param dm 
63          * @param decryptor 
64          * @throws APIException 
65          */
66         public AAF_Hello(final AuthzEnv env) throws Exception {
67                 super(env.access(), env);
68                 
69                 aafLurPerm = aafCon().newLur();
70                 // Note: If you need both Authn and Authz construct the following:
71                 aafAuthn = aafCon().newAuthn(aafLurPerm);
72
73                 String aaf_env = env.getProperty(Config.AAF_ENV);
74                 if(aaf_env==null) {
75                         throw new APIException("aaf_env needs to be set");
76                 }
77                 
78                 // Initialize Facade for all uses
79                 AuthzTrans trans = env.newTrans();
80                 StringBuilder sb = new StringBuilder();
81                 trans.auditTrail(2, sb);
82                 trans.init().log(sb);
83                 
84                 API_Hello.init(this);
85 }
86         
87         /**
88          * Setup XML and JSON implementations for each supported Version type
89          * 
90          * We do this by taking the Code passed in and creating clones of these with the appropriate Facades and properties
91          * to do Versions and Content switches
92          * 
93          */
94         public void route(HttpMethods meth, String path, API api, HttpCode<AuthzTrans, AAF_Hello> code) throws Exception {
95                 String version = "1.0";
96                 // Get Correct API Class from Mapper
97                 route(env,meth,path,code,"text/plain;version="+version,"*/*");
98         }
99         
100         @Override
101         public Filter[] filters() throws CadiException, LocatorException {
102                 try {
103                         return new Filter[] {
104                                         new AuthzTransFilter(env,aafCon(),
105                                                 new AAFTrustChecker((Env)env))
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                         String propsFile = getArg(AAF_LOG4J_PREFIX, args, "org.osaaf")+".log4j.props";
123                         String log_dir = getArg(Config.CADI_LOGDIR,args,"./logs");
124                         String log_level = getArg(Config.CADI_LOGLEVEL,args,"INFO");
125                         File logs = new File(log_dir);
126                         if(!logs.isDirectory()) {
127                                 logs.delete();
128                         }
129                         if(!logs.exists()) {
130                                 logs.mkdirs();
131                         }
132                         Log4JLogIt logIt = new Log4JLogIt(log_dir,log_level,propsFile, "hello");
133                         PropAccess propAccess = new PropAccess(logIt,args);
134                 
135                         AAF_Hello service = new AAF_Hello(new AuthzEnv(propAccess));
136                         JettyServiceStarter<AuthzEnv,AuthzTrans> jss = new JettyServiceStarter<AuthzEnv,AuthzTrans>(service);
137                         jss.start();
138                 } catch (Exception e) {
139                         e.printStackTrace();
140                 }
141         }
142 }