d0c97c9157bc61fa9187da50b6b6b1b6c6abe12a
[policy/gui.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.gui.editors.apex.rest.handling.plugin.upload;
21
22 import javax.ws.rs.client.Client;
23 import javax.ws.rs.client.ClientBuilder;
24 import javax.ws.rs.client.Entity;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import org.onap.policy.common.parameters.ParameterService;
28 import org.onap.policy.gui.editors.apex.rest.UploadPluginConfigParameters;
29
30 /**
31  * Client for the Policy Model upload endpoint.
32  */
33 public class UploadPluginClient {
34
35     private final Client client;
36     private final UploadPluginConfigParameters uploadConfigParam;
37
38     /**
39      * Create a upload plugin client.
40      */
41     public UploadPluginClient() {
42         this(ClientBuilder.newClient(), ParameterService.get(UploadPluginConfigParameters.GROUP_NAME));
43     }
44
45     /**
46      * Create a upload plugin client.
47      * @param client the http client
48      * @param uploadConfigParam the upload configuration parameters
49      */
50     UploadPluginClient(final Client client,
51                        final UploadPluginConfigParameters uploadConfigParam) {
52         this.client = client;
53         this.uploadConfigParam = uploadConfigParam;
54     }
55
56     /**
57      * Uploads the policy to the configured endpoint.
58      *
59      * @param uploadPolicyRequestDto the policy DTO to upload
60      * @return the request response
61      */
62     public Response upload(final UploadPolicyRequestDto uploadPolicyRequestDto) {
63         return client
64             .target(uploadConfigParam.getUrl())
65             .request(MediaType.APPLICATION_JSON)
66             .post(Entity.entity(uploadPolicyRequestDto, MediaType.APPLICATION_JSON));
67     }
68
69 }