2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.portal.nonrtric.controlpanel.eiproducerapi;
22 import com.google.gson.GsonBuilder;
24 import java.lang.invoke.MethodHandles;
26 import javax.net.ssl.SSLException;
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Component;
37 import org.springframework.web.client.HttpClientErrorException;
38 import org.springframework.web.client.HttpServerErrorException;
40 @Component("EiProducerApi")
41 public class EiProducerApiImpl implements EiProducerApi {
42 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
44 private static final String EI_TYPES = "/eitypes";
45 private static final String EI_PRODUCERS = "/eiproducers";
46 private static final String EI_JOBS = "/eijobs";
47 private static final String STATUS = "/status";
49 private final AsyncRestClient webClient;
51 private static com.google.gson.Gson gson = new GsonBuilder() //
56 public EiProducerApiImpl(
57 @org.springframework.beans.factory.annotation.Value("${enrichmentcontroller.url.prefix}") final String urlPrefix) {
58 this(new AsyncRestClient(urlPrefix));
59 logger.debug("enrichment controller url prefix '{}'", urlPrefix);
62 public EiProducerApiImpl(AsyncRestClient webClient) {
63 this.webClient = webClient;
67 public ResponseEntity<String> getAllEiTypeIds() {
68 return getResponseArray(EI_TYPES);
72 public ResponseEntity<String> getEiType(String eiTypeId) {
73 return getResponseObject(EI_TYPES + "/" + eiTypeId);
77 public ResponseEntity<String> getAllEiProducerIds() {
78 return getResponseArray(EI_PRODUCERS);
82 public ResponseEntity<String> getEiProducer(String eiProducerId) {
83 return getResponseObject(EI_PRODUCERS + "/" + eiProducerId);
87 public ResponseEntity<String> getEiJobsForOneEiProducer(String eiProducerId) {
88 return getResponseArray(EI_PRODUCERS + "/" + eiProducerId + EI_JOBS);
92 public ResponseEntity<String> getEiProducerStatus(String eiProducerId) {
93 return getResponseObject(EI_PRODUCERS + "/" + eiProducerId + STATUS);
96 private ResponseEntity<String> getResponseArray(String url) {
98 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
99 if (!rsp.getStatusCode().is2xxSuccessful()) {
102 return new ResponseEntity<>(new JSONArray(rsp.getBody()).toString(), rsp.getStatusCode());
103 } catch (Exception e) {
104 return handleException(e);
108 private ResponseEntity<String> getResponseObject(String url) {
110 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
111 if (!rsp.getStatusCode().is2xxSuccessful()) {
114 return new ResponseEntity<>(new JSONObject(rsp.getBody()).toString(), rsp.getStatusCode());
115 } catch (Exception e) {
116 return handleException(e);
120 private ResponseEntity<String> handleException(Exception throwable) {
121 if (throwable instanceof HttpClientErrorException) {
122 HttpClientErrorException e = (HttpClientErrorException) throwable;
123 return new ResponseEntity<>(e.getMessage(), e.getStatusCode());
124 } else if (throwable instanceof HttpServerErrorException) {
125 HttpServerErrorException e = (HttpServerErrorException) throwable;
126 return new ResponseEntity<>(e.getResponseBodyAsString(), e.getStatusCode());
127 } else if (throwable instanceof SSLException) {
128 SSLException e = (SSLException) throwable;
129 return new ResponseEntity<>("Could not create WebClient " + e.getMessage(),
130 HttpStatus.INTERNAL_SERVER_ERROR);
132 return new ResponseEntity<>(throwable.getClass().getName() + ": " + throwable.getMessage(),
133 HttpStatus.INTERNAL_SERVER_ERROR);