Adding client editor module to apex-pdp
[policy/apex-pdp.git] / client / client-editor / src / main / java / org / onap / policy / apex / client / editor / rest / ApexEditor.java
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.editor.rest;
22
23 import org.glassfish.grizzly.http.server.HttpServer;
24 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
25 import org.glassfish.jersey.server.ResourceConfig;
26 import org.onap.policy.apex.model.utilities.Assertions;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * This class is used to launch the editor. It creates a Grizzly embedded web server and runs the editor.
32  */
33 public class ApexEditor {
34     // Logger for this class
35     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditor.class);
36
37     // The HTTP server exposing JAX-RS resources defined in this application.
38     private final HttpServer server;
39
40     /**
41      * Starts the HTTP server for the Apex editor on the default base URI and with the default REST packages.
42      */
43     public ApexEditor() {
44         this(new ApexEditorParameters());
45     }
46
47     /**
48      * Starts the HTTP server for the Apex editor.
49      *
50      * @param parameters the parameters
51      */
52     public ApexEditor(final ApexEditorParameters parameters) {
53         Assertions.argumentNotNull(parameters, "parameters may not be null");
54
55         LOGGER.debug("Apex RESTful editor starting . . .");
56
57         // Create a resource configuration that scans for JAX-RS resources and providers
58         // in org.onap.policy.apex.client.editor.rest package
59         final ResourceConfig rc = new ResourceConfig().packages(parameters.getRESTPackages());
60
61         // create and start a new instance of grizzly http server
62         // exposing the Jersey application at BASE_URI
63         server = GrizzlyHttpServerFactory.createHttpServer(parameters.getBaseURI(), rc);
64
65         // Add static content
66         server.getServerConfiguration().addHttpHandler(new org.glassfish.grizzly.http.server.CLStaticHttpHandler(
67                 ApexEditorMain.class.getClassLoader(), "/webapp/"), parameters.getStaticPath());
68
69         LOGGER.debug("Apex RESTful editor started");
70     }
71
72     /**
73      * Shut down the web server.
74      */
75     public void shutdown() {
76         LOGGER.debug("Apex RESTful editor shutting down . . .");
77         server.shutdown();
78         LOGGER.debug("Apex RESTful editor shut down");
79     }
80 }