Update for SNI checking
[policy/models.git] / models-sim / models-sim-dmaap / src / main / java / org / onap / policy / models / sim / dmaap / parameters / RestServerParameters.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019,2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.sim.dmaap.parameters;
23
24 import java.util.Properties;
25 import lombok.Getter;
26 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
27 import org.onap.policy.common.gson.GsonMessageBodyHandler;
28 import org.onap.policy.common.parameters.ParameterGroupImpl;
29 import org.onap.policy.common.parameters.annotations.Min;
30 import org.onap.policy.common.parameters.annotations.NotBlank;
31 import org.onap.policy.common.parameters.annotations.NotNull;
32 import org.onap.policy.models.sim.dmaap.rest.CambriaMessageBodyHandler;
33 import org.onap.policy.models.sim.dmaap.rest.DmaapSimRestControllerV1;
34 import org.onap.policy.models.sim.dmaap.rest.TextMessageBodyHandler;
35
36 /**
37  * Class to hold all parameters needed for rest server.
38  */
39 @NotNull
40 @NotBlank
41 @Getter
42 public class RestServerParameters extends ParameterGroupImpl {
43     private String host;
44
45     @Min(value = 1)
46     private int port;
47
48     private String userName;
49
50     private String password;
51
52     private boolean useHttps;
53
54     private boolean sniHostCheck;
55
56     public RestServerParameters() {
57         super(RestServerParameters.class.getSimpleName());
58     }
59
60     /**
61      * Creates a set of properties, suitable for building a REST server, from the
62      * parameters.
63      *
64      * @return a set of properties representing the given parameters
65      */
66     public Properties getServerProperties() {
67         final var props = new Properties();
68         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, getName());
69
70         final String svcpfx =
71             PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + getName();
72
73         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, getHost());
74         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
75             Integer.toString(getPort()));
76         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
77             DmaapSimRestControllerV1.class.getName());
78         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
79         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "false");
80         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, Boolean.toString(isUseHttps()));
81         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SNI_HOST_CHECK_SUFFIX,
82             Boolean.toString(isSniHostCheck()));
83
84         if (getUserName() != null && getPassword() != null) {
85             props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, getUserName());
86             props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, getPassword());
87         }
88
89         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
90             String.join(",", CambriaMessageBodyHandler.class.getName(),
91                 GsonMessageBodyHandler.class.getName(),
92                 TextMessageBodyHandler.class.getName()));
93         return props;
94     }
95 }