b6ed37d80b204cbd3e4d7c2612989b0244255f24
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.deployment.rest;
22
23 import org.glassfish.grizzly.http.server.HttpServer;
24 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
25 import org.glassfish.jersey.media.multipart.MultiPartFeature;
26 import org.glassfish.jersey.server.ResourceConfig;
27 import org.onap.policy.apex.model.utilities.Assertions;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
30
31 /**
32  * This class is used to launch the services. It creates a Grizzly embedded web server and runs the services.
33  */
34 public class ApexDeploymentRest {
35     // Logger for this class
36     private static final XLogger logger = XLoggerFactory.getXLogger(ApexDeploymentRest.class);
37
38     // The HTTP server exposing JAX-RS resources defined in this application.
39     private HttpServer server;
40
41     /**
42      * Starts the HTTP server for the Apex services client on the default base URI and with the default REST packages
43      */
44     public ApexDeploymentRest() {
45         this(new ApexDeploymentRestParameters());
46     }
47
48     /**
49      * Starts the HTTP server for the Apex services client
50      *
51      * @param parameters: The Apex parameters to use to start the server
52      */
53     public ApexDeploymentRest(final ApexDeploymentRestParameters parameters) {
54         Assertions.argumentNotNull(parameters, "parameters may not be null");
55
56         logger.debug("Apex services RESTful client starting . . .");
57
58         // Create a resource configuration that scans for JAX-RS resources and providers
59         // in org.onap.policy.apex.client.deployment.rest package
60         final ResourceConfig rc = new ResourceConfig().packages(parameters.getRESTPackages());
61
62         // Add MultiPartFeature class for jersey-media-multipart
63         rc.register(MultiPartFeature.class);
64
65         // create and start a new instance of grizzly http server
66         // exposing the Jersey application at BASE_URI
67         server = GrizzlyHttpServerFactory.createHttpServer(parameters.getBaseURI(), rc);
68
69         // Add static content
70         server.getServerConfiguration().addHttpHandler(new org.glassfish.grizzly.http.server.CLStaticHttpHandler(
71                 ApexDeploymentRestMain.class.getClassLoader(), "/webapp/"), parameters.getStaticPath());
72
73         logger.debug("Apex services RESTful client started");
74     }
75
76     /**
77      * Shut down the web server
78      */
79     public void shutdown() {
80         logger.debug("Apex services RESTful client shutting down . . .");
81         server.shutdown();
82         logger.debug("Apex services RESTful client shut down");
83     }
84 }