Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / cadi / servlet-sample / src / test / java / org / onap / aaf / sample / cadi / tomcate / TomcatEmbedded.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.tomcate;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.net.URISyntaxException;
27
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;
35
36 /** 
37  * @author JonathanGathman
38  *
39  */
40 public class TomcatEmbedded {
41
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();
45         
46         Service service = tomcat.getService();
47         service.addConnector(getSslConnector(new PropAccess(args), 8081));
48         
49         tomcat.addWebapp("/caditest", getRootFolder().getAbsolutePath());
50         
51         tomcat.start();
52         tomcat.getServer().await();
53
54     }
55     
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);
74         return connector;
75      }
76     
77     private static void setAttr(Connector connector, Access access, String ctag, String atag) throws IOException {
78         String value = access.getProperty(atag, null);
79         if(value==null) {
80             access.log(Level.ERROR, atag, "is null");
81         } else {
82             if(value.startsWith("enc:")) {
83                 access.log(Level.INIT,atag,"=enc:************");
84                 value = access.decrypt(value, false);
85             } else {
86                 access.log(Level.INIT,atag,"=",value);
87             }
88             connector.setAttribute(ctag, value);
89         }
90     }
91
92     private static File getRootFolder() {
93         try {
94             File root;
95             String runningJarPath = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath().replaceAll("\\\\", "/");
96             int lastIndexOf = runningJarPath.lastIndexOf("/target/");
97             if (lastIndexOf < 0) {
98                 root = new File("");
99             } else {
100                 root = new File(runningJarPath.substring(0, lastIndexOf));
101             }
102             System.out.println("application resolved root folder: " + root.getAbsolutePath());
103             return root;
104         } catch (URISyntaxException ex) {
105             throw new RuntimeException(ex);
106         }
107     }
108 }