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.tomcate;
25 import java.io.IOException;
26 import java.net.URISyntaxException;
28 import org.apache.catalina.Service;
29 import org.apache.catalina.connector.Connector;
30 import org.apache.catalina.startup.Tomcat;
31 import org.apache.log4j.chainsaw.Main;
32 import org.onap.aaf.cadi.Access;
33 import org.onap.aaf.cadi.Access.Level;
34 import org.onap.aaf.cadi.PropAccess;
37 * @author JonathanGathman
40 public class TomcatEmbedded {
42 public static void main(String[] args) throws Exception {
43 System.setProperty("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE", "true");
44 Tomcat tomcat = new Tomcat();
46 Service service = tomcat.getService();
47 service.addConnector(getSslConnector(new PropAccess(args), 8081));
49 tomcat.addWebapp("/caditest", getRootFolder().getAbsolutePath());
52 tomcat.getServer().await();
56 private static Connector getSslConnector(PropAccess access, int port) throws IOException {
57 Connector connector = new Connector();
58 connector.setPort(port);
59 connector.setSecure(true);
60 connector.setScheme("https");
61 setAttr(connector,access,"keyAlias","cadi_alias");
62 setAttr(connector,access,"keystoreFile","cadi_keystore");
63 connector.setAttribute("keystoreType", "PKCS12");
64 setAttr(connector,access,"keystorePass","cadi_keystore_password");
65 setAttr(connector,access,"truststoreFile","cadi_truststore");
66 connector.setAttribute("truststoreType", "JKS");
67 setAttr(connector,access,"truststorePass","cadi_truststore_password");
68 connector.setAttribute("clientAuth", "want");
69 connector.setAttribute("protocol", "HTTP/1.1");
70 connector.setAttribute("sslProtocol", "TLS");
71 connector.setAttribute("maxThreads", "200");
72 connector.setAttribute("protocol", "org.apache.coyote.http11.Http11AprProtocol");
73 connector.setAttribute("SSLEnabled", true);
77 private static void setAttr(Connector connector, Access access, String ctag, String atag) throws IOException {
78 String value = access.getProperty(atag, null);
80 access.log(Level.ERROR, atag, "is null");
82 if (value.startsWith("enc:")) {
83 access.log(Level.INIT,atag,"=enc:************");
84 value = access.decrypt(value, false);
86 access.log(Level.INIT,atag,"=",value);
88 connector.setAttribute(ctag, value);
92 private static File getRootFolder() {
95 String runningJarPath = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath().replaceAll("\\\\", "/");
96 int lastIndexOf = runningJarPath.lastIndexOf("/target/");
97 if (lastIndexOf < 0) {
100 root = new File(runningJarPath.substring(0, lastIndexOf));
102 System.out.println("application resolved root folder: " + root.getAbsolutePath());
104 } catch (URISyntaxException ex) {
105 throw new RuntimeException(ex);