719daaae0276e98d4031d6597f93d48ff8c7cc2c
[aaf/authz.git] / auth / auth-fs / src / main / java / org / onap / aaf / auth / fs / AAF_FS.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.fs;
23
24 import static org.onap.aaf.auth.rserv.HttpMethods.GET;
25
26 import java.io.File;
27 import java.io.IOException;
28
29 import javax.servlet.Filter;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.onap.aaf.auth.env.AuthzEnv;
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.auth.env.AuthzTransOnlyFilter;
36 import org.onap.aaf.auth.rserv.CachingFileAccess;
37 import org.onap.aaf.auth.rserv.HttpCode;
38 import org.onap.aaf.auth.server.AbsService;
39 import org.onap.aaf.auth.server.JettyServiceStarter;
40 import org.onap.aaf.auth.server.Log4JLogIt;
41 import org.onap.aaf.cadi.Access.Level;
42 import org.onap.aaf.cadi.CadiException;
43 import org.onap.aaf.cadi.LocatorException;
44 import org.onap.aaf.cadi.PropAccess;
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
50
51 public class AAF_FS extends AbsService<AuthzEnv, AuthzTrans>  {
52
53         public AAF_FS(final AuthzEnv env) throws APIException, IOException, CadiException {
54                 super(env.access(),env);
55                 try {
56                         ///////////////////////  
57                         // File Server 
58                         ///////////////////////
59                         // creates StaticSlot, needed for CachingFileAccess, and sets to public Dir
60                         env.staticSlot(CachingFileAccess.CFA_WEB_PATH,"aaf_public_dir");
61
62                         CachingFileAccess<AuthzTrans> cfa = new CachingFileAccess<AuthzTrans>(env);
63                         route(env,GET,"/:key", cfa); 
64                         route(env,GET,"/:key/:cmd", cfa);
65                         final String aaf_locate_url = access.getProperty(Config.AAF_LOCATE_URL, null);
66                         if(aaf_locate_url == null) {
67                                 access.printf(Level.WARN, "Redirection requires property %s",Config.AAF_LOCATE_URL);
68                         } else {
69                                 route(env,GET,"/", new Redirect(this,aaf_locate_url));
70                         }
71                 } catch (Exception e) {
72                         e.printStackTrace();
73                 }
74         }
75         
76         private static class Redirect extends HttpCode<AuthzTrans, AAF_FS> {
77                 private final String url;
78
79                 public Redirect(AAF_FS context,String url) {
80                         super(context, "Redirect to HTTP/S");
81                         this.url = url;
82                 }
83
84                 @Override
85                 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
86                         trans.info().printf("Redirecting %s to HTTP/S %s", req.getRemoteAddr(), req.getLocalAddr());
87                         resp.sendRedirect(url);
88                 }
89         };
90         
91         @Override
92         public Filter[] filters() throws CadiException, LocatorException {
93                 return new Filter[] {
94                         new AuthzTransOnlyFilter(env)
95                 };
96         }
97
98         @SuppressWarnings("unchecked")
99         @Override
100         public Registrant<AuthzEnv>[] registrants(final int port) throws CadiException, LocatorException {
101                 return new Registrant[] {
102                         new RemoteRegistrant<AuthzEnv>(aafCon(),app_name,app_version,port)
103                 };
104         }
105         
106         public static void main(final String[] args) {
107                 try {
108                         String propsFile = getArg(AAF_LOG4J_PREFIX, args, "org.osaaf")+".log4j.props";
109                         String log_dir = getArg(Config.CADI_LOGDIR,args,"./logs");
110                         String log_level = getArg(Config.CADI_LOGLEVEL,args,"INFO");
111                         File logs = new File(log_dir);
112                         if(!logs.isDirectory()) {
113                                 logs.delete();
114                         }
115                         if(!logs.exists()) {
116                                 logs.mkdirs();
117                         }
118                         Log4JLogIt logIt = new Log4JLogIt(log_dir,log_level,propsFile, "fs");
119                         PropAccess propAccess = new PropAccess(logIt,args);
120                 
121                         AAF_FS service = new AAF_FS(new AuthzEnv(propAccess));
122                         JettyServiceStarter<AuthzEnv,AuthzTrans> jss = new JettyServiceStarter<AuthzEnv,AuthzTrans>(service);
123                         jss.insecure().start();
124                 } catch (Exception e) {
125                         e.printStackTrace();
126                 }
127         }
128 }