2d1fc0977c79b9f6b836a5fb9a141d411b674336
[aaf/authz.git] / cadi / servlet-sample / src / test / java / org / onap / aaf / sample / cadi / jetty / JettyServletServer.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.sample.cadi.jetty;
23
24 import java.net.Inet4Address;
25 import java.util.concurrent.ArrayBlockingQueue;
26
27 import javax.servlet.Servlet;
28
29 import org.eclipse.jetty.server.Server;
30 import org.eclipse.jetty.server.ServerConnector;
31 import org.eclipse.jetty.server.handler.ContextHandler;
32 import org.eclipse.jetty.servlet.FilterHolder;
33 import org.eclipse.jetty.servlet.FilterMapping;
34 import org.eclipse.jetty.servlet.ServletContextHandler;
35 import org.eclipse.jetty.servlet.ServletHandler;
36 import org.eclipse.jetty.servlet.ServletHolder;
37 import org.eclipse.jetty.util.ssl.SslContextFactory;
38 import org.eclipse.jetty.util.thread.QueuedThreadPool;
39 import org.onap.aaf.cadi.Access.Level;
40 import org.onap.aaf.cadi.PropAccess;
41 import org.onap.aaf.cadi.config.Config;
42 import org.onap.aaf.cadi.config.SecurityInfo;
43 import org.onap.aaf.cadi.filter.CadiFilter;
44
45 public abstract class JettyServletServer implements Servlet {
46         
47         public static Server run(PropAccess access, String context, Class<? extends Servlet> servletCls, int port, String ...args) throws Exception {
48                 // Defaults:
49                 int blockingQueueSize = 10;
50         int corePoolSize = 10;
51         int maxPoolSize = 10;
52         int keepAliveTime  = 3000;
53                 String hostname = access.getProperty(Config.HOSTNAME, null);
54                 if(hostname==null) {
55                         hostname = Inet4Address.getLocalHost().getHostName();
56                 }
57         
58         // Add your own Properties to override defaults
59
60         ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(blockingQueueSize);
61         QueuedThreadPool pool = new QueuedThreadPool(maxPoolSize,corePoolSize,keepAliveTime,queue);
62                 Server server = new Server(pool);                       
63
64                 String protocol;
65                 if(access.getProperty(Config.CADI_KEYSTORE_PASSWORD,null)==null) {
66                         ServerConnector conn = new ServerConnector(server);
67                         conn.setHost(hostname);
68                         conn.setPort(port);
69                         server.addConnector(conn);
70                         protocol = "http";
71                 } else {
72                         // Setup Security
73                         SecurityInfo securityInfo = new SecurityInfo(access);
74                         SslContextFactory scf = new SslContextFactory();
75                         scf.setSslContext(securityInfo.getSSLContext());
76                         scf.setWantClientAuth(true);
77                         ServerConnector sslConnector = new ServerConnector(server,scf); 
78                 sslConnector.setHost(hostname);
79                 sslConnector.setPort(port);
80                 server.addConnector(sslConnector);
81                         protocol = "https";
82                 }
83         
84         // Setup Sample Servlet
85         CadiFilter cf = new CadiFilter(true,access);
86                 FilterHolder cfh = new FilterHolder(cf);
87                 
88                 ServletHandler shand = new ServletHandler();
89                 shand.addFilterWithMapping(cfh, "/*", FilterMapping.ALL);
90                 // To use normal Servlets, just add the class here... Actually, bug in Jetty... need to add with ServletHolder
91                 ServletHolder sh = new ServletHolder();
92                 sh.setServlet(servletCls.newInstance());
93                 shand.addServletWithMapping(sh,"/*");
94                 
95                 // To use JASPI Authorization Style to protect the servlet, wrap the Servlet
96                 // with the "MiniJSAPIWrap class, as shown here.  Then add "@RolesAllowed" on your 
97                 // servlet (see sample).  Use Pipe delimited Permissions, not AAF Roles in the line
98                 // shand.addServletWithMapping(new MiniJASPIWrap(MyServlet.class),"/*");
99                 // call initialize after start
100                 ContextHandler ch = new ServletContextHandler();
101                 ch.setContextPath(context);
102                 ch.setHandler(shand);
103                 server.setHandler(ch);
104                 // Startup the Server
105         server.setStopAtShutdown(true);
106         server.start();
107    
108         access.log(Level.INFO,"TestServlet is running at " + protocol + "://"+hostname+':'+port+context);
109         return server;
110         }
111
112 }