From 2ab5761d22fa27fe90922331cb7019a156ed7e4f Mon Sep 17 00:00:00 2001 From: RehanRaza Date: Thu, 15 Oct 2020 14:50:33 +0200 Subject: [PATCH] Remove duplicated code for exception handling Change-Id: I83f8aa61158678f8f9b98e2a5d62fa9eccfaaa3f Issue-ID: NONRTRIC-295 Signed-off-by: RehanRaza --- .../eiproducerapi/EiProducerApiImpl.java | 33 ++------------- .../policyagentapi/PolicyAgentApiImpl.java | 34 ++++----------- .../controlpanel/util/ErrorResponseHandler.java | 49 ++++++++++++++++++++++ .../eiproducerapi/EiProducerApiImplTest.java | 2 +- 4 files changed, 60 insertions(+), 58 deletions(-) create mode 100644 webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/util/ErrorResponseHandler.java diff --git a/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/eiproducerapi/EiProducerApiImpl.java b/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/eiproducerapi/EiProducerApiImpl.java index a4f4270..381a35a 100644 --- a/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/eiproducerapi/EiProducerApiImpl.java +++ b/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/eiproducerapi/EiProducerApiImpl.java @@ -19,23 +19,16 @@ */ package org.oransc.portal.nonrtric.controlpanel.eiproducerapi; -import com.google.gson.GsonBuilder; - import java.lang.invoke.MethodHandles; - -import javax.net.ssl.SSLException; - import org.json.JSONArray; import org.json.JSONObject; import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient; +import org.oransc.portal.nonrtric.controlpanel.util.ErrorResponseHandler; 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 { @@ -48,10 +41,6 @@ public class EiProducerApiImpl implements EiProducerApi { private final AsyncRestClient webClient; - private static com.google.gson.Gson gson = new GsonBuilder() // - .serializeNulls() // - .create(); // - @Autowired public EiProducerApiImpl( @org.springframework.beans.factory.annotation.Value("${enrichmentcontroller.url.prefix}") final String urlPrefix) { @@ -101,7 +90,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); } } @@ -113,23 +102,7 @@ public class EiProducerApiImpl implements EiProducerApi { } return new ResponseEntity<>(new JSONObject(rsp.getBody()).toString(), rsp.getStatusCode()); } catch (Exception e) { - return handleException(e); - } - } - - private ResponseEntity 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.getClass().getName() + ": " + throwable.getMessage(), - HttpStatus.INTERNAL_SERVER_ERROR); } } diff --git a/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java b/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java index 613c801..25e604a 100644 --- a/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java +++ b/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/policyagentapi/PolicyAgentApiImpl.java @@ -26,15 +26,11 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.reflect.TypeToken; - import java.lang.invoke.MethodHandles; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; import java.util.List; - -import javax.net.ssl.SSLException; - import org.immutables.gson.Gson; import org.immutables.value.Value; import org.oransc.portal.nonrtric.controlpanel.model.ImmutablePolicyInfo; @@ -43,14 +39,13 @@ import org.oransc.portal.nonrtric.controlpanel.model.PolicyInstances; import org.oransc.portal.nonrtric.controlpanel.model.PolicyType; import org.oransc.portal.nonrtric.controlpanel.model.PolicyTypes; import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient; +import org.oransc.portal.nonrtric.controlpanel.util.ErrorResponseHandler; 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("PolicyAgentApi") public class PolicyAgentApiImpl implements PolicyAgentApi { @@ -96,7 +91,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi { } return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode()); } catch (Exception e) { - return handleException(e); + return ErrorResponseHandler.handleException(e); } } @@ -117,7 +112,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi { } return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode()); } catch (Exception e) { - return handleException(e); + return ErrorResponseHandler.handleException(e); } } @@ -130,7 +125,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi { String str = obj.toString(); return new ResponseEntity<>(str, rsp.getStatusCode()); } catch (Exception e) { - ResponseEntity rsp = handleException(e); + ResponseEntity rsp = ErrorResponseHandler.handleException(e); return new ResponseEntity<>(rsp.getBody(), rsp.getStatusCode()); } } @@ -146,7 +141,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi { webClient.putForEntity(url, jsonStr).block(); return new ResponseEntity<>(HttpStatus.OK); } catch (Exception e) { - return handleException(e); + return ErrorResponseHandler.handleException(e); } } @@ -157,7 +152,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi { webClient.deleteForEntity(url).block(); return new ResponseEntity<>(HttpStatus.OK); } catch (Exception e) { - return handleException(e); + return ErrorResponseHandler.handleException(e); } } @@ -186,22 +181,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi { String json = gson.toJson(result); return new ResponseEntity<>(json, HttpStatus.OK); } catch (Exception e) { - return handleException(e); - } - } - - private ResponseEntity 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); } } diff --git a/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/util/ErrorResponseHandler.java b/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/util/ErrorResponseHandler.java new file mode 100644 index 0000000..3dbfd04 --- /dev/null +++ b/webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/util/ErrorResponseHandler.java @@ -0,0 +1,49 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2020 Nordix Foundation + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ +package org.oransc.portal.nonrtric.controlpanel.util; + +import javax.net.ssl.SSLException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.HttpServerErrorException; + +public class ErrorResponseHandler { + + private ErrorResponseHandler() { + throw new IllegalStateException("ErrorResponseHandler is a utility class and not meant to be instantiated"); + } + + public static ResponseEntity 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 new ResponseEntity<>(throwable.getClass().getName() + ": " + throwable.getMessage(), + HttpStatus.INTERNAL_SERVER_ERROR); + } +} diff --git a/webapp-backend/src/test/java/org/oransc/portal/nonrtric/controlpanel/eiproducerapi/EiProducerApiImplTest.java b/webapp-backend/src/test/java/org/oransc/portal/nonrtric/controlpanel/eiproducerapi/EiProducerApiImplTest.java index b368f0e..d478d45 100644 --- a/webapp-backend/src/test/java/org/oransc/portal/nonrtric/controlpanel/eiproducerapi/EiProducerApiImplTest.java +++ b/webapp-backend/src/test/java/org/oransc/portal/nonrtric/controlpanel/eiproducerapi/EiProducerApiImplTest.java @@ -32,7 +32,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.client.HttpServerErrorException; import reactor.core.publisher.Mono; -public class EiProducerApiImplTest { +class EiProducerApiImplTest { private static final String URL_EI_TYPES = "/eitypes"; private static final String EI_TYPE_1 = "eitype1"; private static final String EI_TYPE_2 = "eitype2"; -- 2.16.6