Update AAF Version 1.0.0
[aaf/authz.git] / authz-fs / src / main / java / com / att / authz / fs / FileServer.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.authz.fs;\r
24 \r
25 import static com.att.cssa.rserv.HttpMethods.GET;\r
26 \r
27 import java.io.IOException;\r
28 import java.io.InputStream;\r
29 import java.net.URL;\r
30 import java.util.ArrayList;\r
31 import java.util.EnumSet;\r
32 import java.util.List;\r
33 import java.util.Properties;\r
34 \r
35 import com.att.aft.dme2.api.DME2Manager;\r
36 import com.att.aft.dme2.api.DME2Server;\r
37 import com.att.aft.dme2.api.DME2ServerProperties;\r
38 import com.att.aft.dme2.api.DME2ServiceHolder;\r
39 import com.att.aft.dme2.api.util.DME2FilterHolder;\r
40 import com.att.aft.dme2.api.util.DME2FilterHolder.RequestDispatcherType;\r
41 import com.att.aft.dme2.api.util.DME2ServletHolder;\r
42 import com.att.authz.env.AuthzEnv;\r
43 import com.att.authz.env.AuthzTrans;\r
44 import com.att.authz.env.AuthzTransOnlyFilter;\r
45 import com.att.cssa.rserv.CachingFileAccess;\r
46 import com.att.cssa.rserv.RServlet;\r
47 import com.att.inno.env.APIException;\r
48 \r
49 \r
50 public class FileServer extends RServlet<AuthzTrans>  {\r
51         public FileServer(final AuthzEnv env) throws APIException, IOException {\r
52                 try {\r
53                         ///////////////////////  \r
54                         // File Server \r
55                         ///////////////////////\r
56                         \r
57                         CachingFileAccess<AuthzTrans> cfa = new CachingFileAccess<AuthzTrans>(env);\r
58                         route(env,GET,"/:key", cfa); \r
59                         route(env,GET,"/:key/:cmd", cfa); \r
60                         ///////////////////////\r
61         \r
62         \r
63                 } catch (Exception e) {\r
64                         e.printStackTrace();\r
65                 }\r
66         }\r
67         \r
68         public static void main(String[] args) {\r
69                 try {\r
70                         // Load Properties from authFramework.properties.  Needed for DME2 and AuthzEnv\r
71                         Properties props = new Properties();\r
72                         URL rsrc = ClassLoader.getSystemResource("FileServer.props");\r
73                         if(rsrc==null) {\r
74                                 System.err.println("Folder containing FileServer.props must be on Classpath");\r
75                                 System.exit(1);\r
76                         }\r
77                         InputStream is = rsrc.openStream();\r
78                         try {\r
79                                 props.load(is);\r
80                         } finally {\r
81                                 is.close();\r
82                         }\r
83                         \r
84                         // Load Properties into AuthzEnv\r
85                         AuthzEnv env = new AuthzEnv(props); \r
86                         env.setLog4JNames("log4j.properties","authz","fs","audit","init",null);\r
87                         \r
88                         // AFT Discovery Libraries only read System Props\r
89                         env.loadToSystemPropsStartsWith("AFT_","DME2_");\r
90                         env.init().log("DME2 using " + env.getProperty("DMEServiceName","unknown") + " URI");\r
91                         \r
92                         // Start DME2 (DME2 needs Properties form of props)\r
93                     DME2Manager dme2 = new DME2Manager("RServDME2Manager",props);\r
94                     \r
95                     DME2ServiceHolder svcHolder;\r
96                     List<DME2ServletHolder> slist = new ArrayList<DME2ServletHolder>();\r
97                     svcHolder = new DME2ServiceHolder();\r
98                     String serviceName = env.getProperty("DMEServiceName",null);\r
99                         if(serviceName!=null) {\r
100                         svcHolder.setServiceURI(serviceName);\r
101                         svcHolder.setManager(dme2);\r
102                         svcHolder.setContext("/");\r
103                         \r
104                         FileServer fs = new FileServer(env);\r
105                         DME2ServletHolder srvHolder = new DME2ServletHolder(fs);\r
106                         srvHolder.setContextPath("/*");\r
107                         slist.add(srvHolder);\r
108                         \r
109                         EnumSet<RequestDispatcherType> edlist = EnumSet.of(\r
110                                         RequestDispatcherType.REQUEST,\r
111                                         RequestDispatcherType.FORWARD,\r
112                                         RequestDispatcherType.ASYNC\r
113                                         );\r
114 \r
115                         ///////////////////////\r
116                         // Apply Filters\r
117                         ///////////////////////\r
118                         List<DME2FilterHolder> flist = new ArrayList<DME2FilterHolder>();\r
119                         \r
120                         // Need TransFilter\r
121                         flist.add(new DME2FilterHolder(new AuthzTransOnlyFilter(env),"/*",edlist));\r
122                         svcHolder.setFilters(flist);\r
123                         svcHolder.setServletHolders(slist);\r
124                         \r
125                         DME2Server dme2svr = dme2.getServer();\r
126                         DME2ServerProperties dsprops = dme2svr.getServerProperties();\r
127                         dsprops.setGracefulShutdownTimeMs(1000);\r
128 \r
129                         env.init().log("Starting AAF FileServer with Jetty/DME2 server...");\r
130                         dme2svr.start();\r
131                         try {\r
132 //                              if(env.getProperty("NO_REGISTER",null)!=null)\r
133                                 dme2.bindService(svcHolder);\r
134                                 env.init().log("DME2 is available as HTTP"+(dsprops.isSslEnable()?"/S":""),"on port:",dsprops.getPort());\r
135 \r
136                             while(true) { // Per DME2 Examples...\r
137                                 Thread.sleep(5000);\r
138                             }\r
139                         } catch(InterruptedException e) {\r
140                             env.init().log("AAF Jetty Server interrupted!");\r
141                         } catch(Exception e) { // Error binding service doesn't seem to stop DME2 or Process\r
142                             env.init().log(e,"DME2 Initialization Error");\r
143                                 dme2svr.stop();\r
144                                 System.exit(1);\r
145                         }\r
146                         } else {\r
147                                 env.init().log("Properties must contain DMEServiceName");\r
148                         }\r
149 \r
150                 } catch (Exception e) {\r
151                         e.printStackTrace(System.err);\r
152                         System.exit(1);\r
153                 }\r
154         }\r
155 }\r