6077b39d03825d9569254933c9b96016c7e5f51c
[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.IOException;
27
28 import javax.servlet.Filter;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.onap.aaf.auth.env.AuthzEnv;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.env.AuthzTransOnlyFilter;
35 import org.onap.aaf.auth.rserv.CachingFileAccess;
36 import org.onap.aaf.auth.rserv.HttpCode;
37 import org.onap.aaf.auth.server.AbsService;
38 import org.onap.aaf.auth.server.JettyServiceStarter;
39 import org.onap.aaf.auth.server.Log4JLogIt;
40 import org.onap.aaf.cadi.Access.Level;
41 import org.onap.aaf.cadi.CadiException;
42 import org.onap.aaf.cadi.LocatorException;
43 import org.onap.aaf.cadi.PropAccess;
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
48 public class AAF_FS extends AbsService<AuthzEnv, AuthzTrans>  {
49
50     public AAF_FS(final AuthzEnv env) throws IOException, CadiException {
51         super(env.access(),env);
52         try {
53             ///////////////////////
54             // File Server
55             ///////////////////////
56             // creates StaticSlot, needed for CachingFileAccess, and sets to public Dir
57             env.staticSlot(CachingFileAccess.CFA_WEB_PATH,"aaf_public_dir");
58
59             CachingFileAccess<AuthzTrans> cfa = new CachingFileAccess<>(env);
60             route(env,GET,"/:key*", cfa);
61             final String aaf_locate_url = Config.getAAFLocateUrl(access);
62             if (aaf_locate_url == null) {
63                 access.printf(Level.WARN, "Redirection requires property %s",Config.AAF_LOCATE_URL);
64             } else {
65                 route(env,GET,"/", new Redirect(this,aaf_locate_url));
66             }
67         } catch (Exception e) {
68             e.printStackTrace();
69         }
70     }
71
72     private static class Redirect extends HttpCode<AuthzTrans, AAF_FS> {
73         private final String url;
74
75         public Redirect(AAF_FS context,String url) {
76             super(context, "Redirect to HTTP/S");
77             this.url = url;
78         }
79
80         @Override
81         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
82             trans.info().printf("Redirecting %s to HTTP/S %s", req.getRemoteAddr(), req.getLocalAddr());
83             resp.sendRedirect(url);
84         }
85     };
86
87     @Override
88     public Filter[] _filters(Object ... additionalTafLurs) throws CadiException, LocatorException {
89         // Note: No TAFs and Lurs on FileServer
90         return new Filter[] {
91             new AuthzTransOnlyFilter(env)
92         };
93     }
94
95     @SuppressWarnings("unchecked")
96     @Override
97     public Registrant<AuthzEnv>[] registrants(final int port) throws CadiException, LocatorException {
98         return new Registrant[] {
99             new RemoteRegistrant<AuthzEnv>(aafCon(),port)
100         };
101     }
102
103     public static void main(final String[] args) {
104         try {
105             Log4JLogIt logIt = new Log4JLogIt(args, "fs");
106             PropAccess propAccess = new PropAccess(logIt,args);
107             try {
108                 new JettyServiceStarter<AuthzEnv,AuthzTrans>(
109                     new AAF_FS(new AuthzEnv(propAccess)),false)
110                         .start();
111             } catch (Exception e) {
112                 propAccess.log(e);
113             }
114         } catch (Exception e) {
115             e.printStackTrace();
116         }
117     }
118 }