2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.sample.cadi.jetty;
24 import java.net.Inet4Address;
25 import java.util.concurrent.ArrayBlockingQueue;
27 import javax.servlet.Servlet;
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;
45 public abstract class JettyServletServer implements Servlet {
47 public static Server run(PropAccess access, String context, Class<? extends Servlet> servletCls, int port, String ...args) throws Exception {
49 int blockingQueueSize = 10;
50 int corePoolSize = 10;
52 int keepAliveTime = 3000;
53 String hostname = access.getProperty(Config.HOSTNAME, null);
55 hostname = Inet4Address.getLocalHost().getHostName();
58 // Add your own Properties to override defaults
60 ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(blockingQueueSize);
61 QueuedThreadPool pool = new QueuedThreadPool(maxPoolSize,corePoolSize,keepAliveTime,queue);
62 Server server = new Server(pool);
65 if (access.getProperty(Config.CADI_KEYSTORE_PASSWORD,null)==null) {
66 ServerConnector conn = new ServerConnector(server);
67 conn.setHost(hostname);
69 server.addConnector(conn);
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);
84 // Setup Sample Servlet
85 CadiFilter cf = new CadiFilter(true,access);
86 FilterHolder cfh = new FilterHolder(cf);
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,"/*");
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);
108 access.log(Level.INFO,"TestServlet is running at " + protocol + "://"+hostname+':'+port+context);