bd5ae2423455a48129e01bba6f708aca1f460462
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-endpoints
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.openecomp.policy.drools.http.server;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Properties;
27
28 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
29 import org.openecomp.policy.common.logging.flexlogger.Logger;
30 import org.openecomp.policy.drools.http.server.internal.JettyJerseyServer;
31 import org.openecomp.policy.drools.properties.PolicyProperties;
32
33 /**
34  * Jetty Server Factory
35  */
36 public interface HttpServletServerFactory {
37
38         public HttpServletServer build(String name, String host, int port, String contextPath, boolean managed)
39                 throws IllegalArgumentException;
40         
41         public ArrayList<HttpServletServer> build(Properties properties) throws IllegalArgumentException;
42         
43         public HttpServletServer get(int port);
44         public List<HttpServletServer> inventory();
45         public void destroy(int port);
46         public void destroy();
47 }
48
49 class IndexedHttpServletServerFactory implements HttpServletServerFactory {
50         
51         protected static Logger  logger = FlexLogger.getLogger(IndexedHttpServletServerFactory.class);  
52         
53         protected HashMap<Integer, JettyJerseyServer> servers = new HashMap<Integer, JettyJerseyServer>();
54
55         @Override
56         public synchronized HttpServletServer build(String name, String host, int port, 
57                                                             String contextPath, boolean managed) 
58                 throws IllegalArgumentException {       
59                 
60                 if (servers.containsKey(port))
61                         return servers.get(port);
62                 
63                 JettyJerseyServer server = new JettyJerseyServer(name, host, port, contextPath);
64                 if (managed)
65                         servers.put(port, server);
66                 
67                 return server;
68         }
69         
70         @Override
71         public synchronized ArrayList<HttpServletServer> build(Properties properties) 
72                 throws IllegalArgumentException {       
73                 
74                 ArrayList<HttpServletServer> serviceList = new ArrayList<HttpServletServer>();
75                 
76                 String serviceNames = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES);
77                 if (serviceNames == null || serviceNames.isEmpty()) {
78                         logger.warn("No topic for HTTP Service " + properties);
79                         return serviceList;
80                 }
81                 
82                 List<String> serviceNameList = 
83                                 new ArrayList<String>(Arrays.asList(serviceNames.split("\\s*,\\s*")));
84                 
85                 for (String serviceName : serviceNameList) {
86                         String servicePortString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + 
87                                                                       serviceName + 
88                                                                       PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX);
89                         
90                         int servicePort;
91                         try {
92                                 if (servicePortString == null || servicePortString.isEmpty()) {
93                                         if (logger.isWarnEnabled())
94                                                 logger.warn("No HTTP port for service in " + serviceName);
95                                         continue;
96                                 }
97                                 servicePort = Integer.parseInt(servicePortString);
98                         } catch (NumberFormatException nfe) {
99                                 if (logger.isWarnEnabled())
100                                         logger.warn("No HTTP port for service in " + serviceName);
101                                 continue;
102                         }
103                         
104                         String hostName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
105                                                      serviceName + 
106                                                      PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX);
107                         
108                         String contextUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
109                                                         serviceName + 
110                                                         PolicyProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX);
111                         
112                         String userName = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
113                                                                          serviceName + 
114                                                      PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX);
115
116                         String password = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
117                                                      serviceName + 
118                                                      PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX);
119                         
120                         String authUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
121                                                         serviceName + 
122                                                         PolicyProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX);
123                         
124                         String restClasses = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
125                                                       serviceName + 
126                                                       PolicyProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX);
127                         
128                         String restPackages = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
129                                                         serviceName + 
130                                                         PolicyProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX);
131                         String restUriPath = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
132                                                          serviceName + 
133                                                          PolicyProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX);
134                         
135                         String managedString = properties.getProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." +
136                                                           serviceName + 
137                                                           PolicyProperties.PROPERTY_MANAGED_SUFFIX);            
138                         boolean managed = true;
139                         if (managedString != null && !managedString.isEmpty()) {
140                                 managed = Boolean.parseBoolean(managedString);
141                         }
142                         
143                         HttpServletServer service = build(serviceName, hostName, servicePort, contextUriPath, managed);
144                         if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
145                                 service.setBasicAuthentication(userName, password, authUriPath);
146                         }
147                         
148                         if (restClasses != null && !restClasses.isEmpty()) {
149                                 List<String> restClassesList = 
150                                                 new ArrayList<String>(Arrays.asList(restClasses.split("\\s*,\\s*")));
151                                 for (String restClass : restClassesList)
152                                         service.addServletClass(restUriPath, restClass);
153                         }
154                         
155                         if (restPackages != null && !restPackages.isEmpty()) {
156                                 List<String> restPackageList = 
157                                                 new ArrayList<String>(Arrays.asList(restPackages.split("\\s*,\\s*")));
158                                 for (String restPackage : restPackageList)
159                                         service.addServletPackage(restUriPath, restPackage);
160                         }
161                         
162                         serviceList.add(service);
163                 }
164                 
165                 return serviceList;
166         }
167
168         @Override
169         public synchronized HttpServletServer get(int port) throws IllegalArgumentException {
170                 
171                 if (servers.containsKey(port)) {
172                         return servers.get(port);
173                 } 
174                 
175                 throw new IllegalArgumentException("Http Server for " + port + " not found");
176         }
177
178         @Override
179         public synchronized List<HttpServletServer> inventory() {
180                  return new ArrayList<HttpServletServer>(this.servers.values());
181         }
182         
183         @Override
184         public synchronized void destroy(int port) throws IllegalArgumentException, IllegalStateException {
185                 
186                 if (!servers.containsKey(port)) {
187                         return;
188                 }
189                 
190                 HttpServletServer server = servers.remove(port);
191                 server.shutdown();
192         }
193
194         @Override
195         public synchronized void destroy() throws IllegalArgumentException, IllegalStateException {
196                 List<HttpServletServer> servers = this.inventory();
197                 for (HttpServletServer server: servers) {
198                         server.shutdown();
199                 }
200                 
201                 synchronized(this) {
202                         this.servers.clear();
203                 }
204         }
205         
206 }