Remove duplicated code for exception handling
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / eiproducerapi / EiProducerApiImpl.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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.oransc.portal.nonrtric.controlpanel.eiproducerapi;
21
22 import java.lang.invoke.MethodHandles;
23 import org.json.JSONArray;
24 import org.json.JSONObject;
25 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
26 import org.oransc.portal.nonrtric.controlpanel.util.ErrorResponseHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.stereotype.Component;
32
33 @Component("EiProducerApi")
34 public class EiProducerApiImpl implements EiProducerApi {
35     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
36
37     private static final String EI_TYPES = "/eitypes";
38     private static final String EI_PRODUCERS = "/eiproducers";
39     private static final String EI_JOBS = "/eijobs";
40     private static final String STATUS = "/status";
41
42     private final AsyncRestClient webClient;
43
44     @Autowired
45     public EiProducerApiImpl(
46         @org.springframework.beans.factory.annotation.Value("${enrichmentcontroller.url.prefix}") final String urlPrefix) {
47         this(new AsyncRestClient(urlPrefix));
48         logger.debug("enrichment controller url prefix '{}'", urlPrefix);
49     }
50
51     public EiProducerApiImpl(AsyncRestClient webClient) {
52         this.webClient = webClient;
53     }
54
55     @Override
56     public ResponseEntity<String> getAllEiTypeIds() {
57         return getResponseArray(EI_TYPES);
58     }
59
60     @Override
61     public ResponseEntity<String> getEiType(String eiTypeId) {
62         return getResponseObject(EI_TYPES + "/" + eiTypeId);
63     }
64
65     @Override
66     public ResponseEntity<String> getAllEiProducerIds() {
67         return getResponseArray(EI_PRODUCERS);
68     }
69
70     @Override
71     public ResponseEntity<String> getEiProducer(String eiProducerId) {
72         return getResponseObject(EI_PRODUCERS + "/" + eiProducerId);
73     }
74
75     @Override
76     public ResponseEntity<String> getEiJobsForOneEiProducer(String eiProducerId) {
77         return getResponseArray(EI_PRODUCERS + "/" + eiProducerId + EI_JOBS);
78     }
79
80     @Override
81     public ResponseEntity<String> getEiProducerStatus(String eiProducerId) {
82         return getResponseObject(EI_PRODUCERS + "/" + eiProducerId + STATUS);
83     }
84
85     private ResponseEntity<String> getResponseArray(String url) {
86         try {
87             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
88             if (!rsp.getStatusCode().is2xxSuccessful()) {
89                 return rsp;
90             }
91             return new ResponseEntity<>(new JSONArray(rsp.getBody()).toString(), rsp.getStatusCode());
92         } catch (Exception e) {
93             return ErrorResponseHandler.handleException(e);
94         }
95     }
96
97     private ResponseEntity<String> getResponseObject(String url) {
98         try {
99             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
100             if (!rsp.getStatusCode().is2xxSuccessful()) {
101                 return rsp;
102             }
103             return new ResponseEntity<>(new JSONObject(rsp.getBody()).toString(), rsp.getStatusCode());
104         } catch (Exception e) {
105             return ErrorResponseHandler.handleException(e);
106         }
107     }
108 }