Remove unused method
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / eiproducerapi / EiProducerApiImpl.java
index ca45e27..921d586 100644 (file)
 package org.oransc.portal.nonrtric.controlpanel.eiproducerapi;
 
 import com.google.gson.GsonBuilder;
+import com.google.gson.JsonSyntaxException;
+
 import java.lang.invoke.MethodHandles;
-import javax.net.ssl.SSLException;
+import java.util.List;
+
 import org.json.JSONArray;
 import org.json.JSONObject;
+import org.oransc.portal.nonrtric.controlpanel.model.JobInfo;
+import org.oransc.portal.nonrtric.controlpanel.model.ProducerRegistrationInfo;
+import org.oransc.portal.nonrtric.controlpanel.model.ProducerStatusInfo;
 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
+import org.oransc.portal.nonrtric.controlpanel.util.ErrorResponseHandler;
+import org.oransc.portal.nonrtric.controlpanel.util.JsonArrayHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
-import org.springframework.web.client.HttpClientErrorException;
-import org.springframework.web.client.HttpServerErrorException;
 
 @Component("EiProducerApi")
 public class EiProducerApiImpl implements EiProducerApi {
     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
-    private static final String EI_TYPES = "/eitypes";
     private static final String EI_PRODUCERS = "/eiproducers";
     private static final String EI_JOBS = "/eijobs";
     private static final String STATUS = "/status";
 
     private final AsyncRestClient webClient;
-
-    private static com.google.gson.Gson gson = new GsonBuilder() //
-        .serializeNulls() //
-        .create(); //
+    private static com.google.gson.Gson gson = new GsonBuilder().create();
 
     @Autowired
     public EiProducerApiImpl(
@@ -60,34 +61,31 @@ public class EiProducerApiImpl implements EiProducerApi {
         this.webClient = webClient;
     }
 
-    @Override
-    public ResponseEntity<String> getAllEiTypeIds() {
-        return getResponseArray(EI_TYPES);
-    }
-
-    @Override
-    public ResponseEntity<String> getEiType(String eiTypeId) {
-        return getResponseObject(EI_TYPES + "/" + eiTypeId);
-    }
-
     @Override
     public ResponseEntity<String> getAllEiProducerIds() {
         return getResponseArray(EI_PRODUCERS);
     }
 
     @Override
-    public ResponseEntity<String> getEiProducer(String eiProducerId) {
-        return getResponseObject(EI_PRODUCERS + "/" + eiProducerId);
+    public ResponseEntity<ProducerRegistrationInfo> getEiProducer(String eiProducerId) throws JsonSyntaxException {
+        ResponseEntity<String> resp = getResponseObject(EI_PRODUCERS + "/" + eiProducerId);
+        ProducerRegistrationInfo info = gson.fromJson(resp.getBody(), ProducerRegistrationInfo.class);
+        return new ResponseEntity<>(info, resp.getStatusCode());
     }
 
     @Override
-    public ResponseEntity<String> getEiJobsForOneEiProducer(String eiProducerId) {
-        return getResponseArray(EI_PRODUCERS + "/" + eiProducerId + EI_JOBS);
+    public ResponseEntity<List<JobInfo>> getEiJobsForOneEiProducer(String eiProducerId)
+        throws JsonSyntaxException, IllegalStateException {
+        ResponseEntity<String> resp = getResponseArray(EI_PRODUCERS + "/" + eiProducerId + EI_JOBS);
+        List<JobInfo> jobs = JsonArrayHandler.parseJsonArray(resp.getBody(), JobInfo.class);
+        return new ResponseEntity<>(jobs, resp.getStatusCode());
     }
 
     @Override
-    public ResponseEntity<String> getEiProducerStatus(String eiProducerId) {
-        return getResponseObject(EI_PRODUCERS + "/" + eiProducerId + STATUS);
+    public ResponseEntity<ProducerStatusInfo> getEiProducerStatus(String eiProducerId) throws JsonSyntaxException {
+        ResponseEntity<String> resp = getResponseObject(EI_PRODUCERS + "/" + eiProducerId + STATUS);
+        ProducerStatusInfo status = gson.fromJson(resp.getBody(), ProducerStatusInfo.class);
+        return new ResponseEntity<>(status, resp.getStatusCode());
     }
 
     private ResponseEntity<String> getResponseArray(String url) {
@@ -98,7 +96,7 @@ public class EiProducerApiImpl implements EiProducerApi {
             }
             return new ResponseEntity<>(new JSONArray(rsp.getBody()).toString(), rsp.getStatusCode());
         } catch (Exception e) {
-            return handleException(e);
+            return ErrorResponseHandler.handleException(e);
         }
     }
 
@@ -110,22 +108,7 @@ public class EiProducerApiImpl implements EiProducerApi {
             }
             return new ResponseEntity<>(new JSONObject(rsp.getBody()).toString(), rsp.getStatusCode());
         } catch (Exception e) {
-            return handleException(e);
-        }
-    }
-
-    private ResponseEntity<String> handleException(Exception throwable) {
-        if (throwable instanceof HttpClientErrorException) {
-            HttpClientErrorException e = (HttpClientErrorException) throwable;
-            return new ResponseEntity<>(e.getMessage(), e.getStatusCode());
-        } else if (throwable instanceof HttpServerErrorException) {
-            HttpServerErrorException e = (HttpServerErrorException) throwable;
-            return new ResponseEntity<>(e.getResponseBodyAsString(), e.getStatusCode());
-        } else if (throwable instanceof SSLException) {
-            SSLException e = (SSLException) throwable;
-            return new ResponseEntity<>("Could not create WebClient " + e.getMessage(),
-                HttpStatus.INTERNAL_SERVER_ERROR);
+            return ErrorResponseHandler.handleException(e);
         }
-        return new ResponseEntity<>(throwable.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
     }
 }