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(
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();
}
}
/*-
* ============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.
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 {
/**
* ============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;
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.
--- /dev/null
+/*-
+ * ============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);
+ }
+}
pap:
name: PapGroup
+ aaf: false
pdpParameters:
heartBeatMs: 120000
updateParameters:
import org.onap.policy.common.utils.network.NetworkUtil;
import org.onap.policy.common.utils.security.SelfSignedKeyStore;
import org.onap.policy.common.utils.services.Registry;
+import org.onap.policy.pap.main.PapConstants;
import org.onap.policy.pap.main.PolicyPapApplication;
import org.onap.policy.pap.main.parameters.CommonTestData;
import org.onap.policy.pap.main.startstop.PapActivator;
import org.powermock.reflect.Whitebox;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.annotation.DirtiesContext;
@LocalServerPort
private int port;
- @Autowired
private PapActivator papActivator;
/**
@Before
public void setUp() throws Exception {
httpsPrefix = "https://localhost:" + port + "/";
+ papActivator = Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class);
activatorWasAlive = papActivator.isAlive();
}
pap:
name: "PapGroup"
+ aaf: false
pdpParameters:
updateParameters:
maxRetryCount: 1
pap:
name: PapGroup
+ aaf: false
pdpParameters:
heartBeatMs: 120000
updateParameters: