aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/main/java/org/onap/policy/pap/main
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/main/java/org/onap/policy/pap/main')
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/PolicyPapApplication.java14
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/config/PapAafConfig.java6
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/exception/ControllerExceptionHandler.java (renamed from main/src/main/java/org/onap/policy/pap/main/PapExceptionHandler.java)6
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/exception/ServiceExceptionHandler.java64
4 files changed, 83 insertions, 7 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/PolicyPapApplication.java b/main/src/main/java/org/onap/policy/pap/main/PolicyPapApplication.java
index ede40126..edb6ff6d 100644
--- a/main/src/main/java/org/onap/policy/pap/main/PolicyPapApplication.java
+++ b/main/src/main/java/org/onap/policy/pap/main/PolicyPapApplication.java
@@ -22,12 +22,15 @@ package org.onap.policy.pap.main;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonSerializer;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;
+import springfox.documentation.spring.web.json.Json;
@SpringBootApplication(exclude = {JacksonAutoConfiguration.class})
@EntityScan(
@@ -39,8 +42,17 @@ public class PolicyPapApplication {
SpringApplication.run(PolicyPapApplication.class, args);
}
+ /**
+ * Configure gson with the required adapters to be used in the application.
+ *
+ * @return the gson bean
+ */
@Bean
public Gson gson() {
- return GsonMessageBodyHandler.configBuilder(new GsonBuilder()).create();
+ GsonBuilder gsonBuilder = GsonMessageBodyHandler.configBuilder(new GsonBuilder());
+ JsonSerializer<Json> serializer =
+ (json, type, jsonSerializationContext) -> JsonParser.parseString(json.value());
+ gsonBuilder.registerTypeAdapter(Json.class, serializer);
+ return gsonBuilder.create();
}
}
diff --git a/main/src/main/java/org/onap/policy/pap/main/config/PapAafConfig.java b/main/src/main/java/org/onap/policy/pap/main/config/PapAafConfig.java
index d73a37d8..4bbf29f6 100644
--- a/main/src/main/java/org/onap/policy/pap/main/config/PapAafConfig.java
+++ b/main/src/main/java/org/onap/policy/pap/main/config/PapAafConfig.java
@@ -1,6 +1,6 @@
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2021 Bell Canada. All rights reserved.
+ * Copyright (C) 2021-2022 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,12 +22,12 @@ package org.onap.policy.pap.main.config;
import javax.servlet.Filter;
import org.onap.policy.pap.main.rest.PapAafFilter;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.Profile;
@Configuration
-@Profile(value = {"aaf-auth"})
+@ConditionalOnProperty(value = "pap.aaf", havingValue = "true")
public class PapAafConfig {
/**
diff --git a/main/src/main/java/org/onap/policy/pap/main/PapExceptionHandler.java b/main/src/main/java/org/onap/policy/pap/main/exception/ControllerExceptionHandler.java
index 54418123..2b4217c0 100644
--- a/main/src/main/java/org/onap/policy/pap/main/PapExceptionHandler.java
+++ b/main/src/main/java/org/onap/policy/pap/main/exception/ControllerExceptionHandler.java
@@ -18,7 +18,7 @@
* ============LICENSE_END=========================================================
*/
-package org.onap.policy.pap.main;
+package org.onap.policy.pap.main.exception;
import java.util.UUID;
import org.onap.policy.models.base.PfModelException;
@@ -34,9 +34,9 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
@RestControllerAdvice
-public class PapExceptionHandler {
+public class ControllerExceptionHandler {
- private static final Logger logger = LoggerFactory.getLogger(PapExceptionHandler.class);
+ private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);
/**
* Handle PfModelException.
diff --git a/main/src/main/java/org/onap/policy/pap/main/exception/ServiceExceptionHandler.java b/main/src/main/java/org/onap/policy/pap/main/exception/ServiceExceptionHandler.java
new file mode 100644
index 00000000..80887961
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/pap/main/exception/ServiceExceptionHandler.java
@@ -0,0 +1,64 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Bell Canada. All rights reserved.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pap.main.exception;
+
+import javax.ws.rs.core.Response;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.AfterThrowing;
+import org.aspectj.lang.annotation.Aspect;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.TransactionException;
+
+@Aspect
+@Component
+public class ServiceExceptionHandler {
+
+ /**
+ * Handle any exceptions that are not already handled.
+ * For e.g., runtime exceptions that could happen during SQL query execution related to data integrity etc.
+ *
+ * @param joinPoint the point of execution
+ * @param exception the exception
+ */
+ @AfterThrowing(pointcut = "execution(* org.onap.policy.pap.main.service.*.*(..))", throwing = "exception")
+ public void handleServiceException(JoinPoint joinPoint, RuntimeException exception) {
+ if (exception instanceof PfModelRuntimeException) {
+ throw (PfModelRuntimeException) exception;
+ } else {
+ throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, exception.getMessage(), exception);
+ }
+ }
+
+ /**
+ * Handle DB Transaction related exceptions.
+ * All service classes in org.onap.policy.pap.main.service are transactional now,
+ * Autowiring these service classes can cause TransactionException.
+ * For e.g., JDBC connection failure occurs and failed to open transaction at service level
+ *
+ * @param joinPoint the point of execution
+ * @param exception the exception
+ */
+ @AfterThrowing(pointcut = "execution(* org.onap.policy.pap.main.*.*.*(..))", throwing = "exception")
+ public void handleTransactionException(JoinPoint joinPoint, TransactionException exception) {
+ throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, exception.getMessage(), exception);
+ }
+}