7e3ed562d081fabea715df3dbaa7dd8189564fd9
[aai/sparky-be.git] / sparkybe-onap-application / src / main / java / org / onap / aai / sparky / ExceptionConfiguration.java
1 /**
2  * ============LICENSE_START=======================================================
3  * SPARKY (AAI UI service)
4  * ================================================================================
5  * Copyright � 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright � 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky;
22
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import org.onap.aai.sparky.exception.GenericServiceException;
29 import org.onap.aai.sparky.exception.ProxyErrorDetails;
30 import org.onap.aai.sparky.exception.ProxyServiceException;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.web.bind.annotation.ControllerAdvice;
34 import org.springframework.web.bind.annotation.ExceptionHandler;
35 import org.springframework.web.bind.annotation.RestController;
36 import org.springframework.web.context.request.WebRequest;
37 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
38
39
40 @ControllerAdvice
41 @RestController
42 public class ExceptionConfiguration extends ResponseEntityExceptionHandler {
43
44   @ExceptionHandler(ProxyServiceException.class)
45   public final ResponseEntity<ProxyErrorDetails> handleProxyException(ProxyServiceException ex, WebRequest request) {
46           Date curDate = new Date();
47       SimpleDateFormat format = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
48           String DateToStr = format.format(curDate);
49           ProxyErrorDetails errorDetails = new ProxyErrorDetails(
50                         DateToStr, ex.getMessage(),request.getDescription(false),"REQUEST PROCESSING ERROR","500");   
51     return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
52   }
53   
54   @ExceptionHandler(GenericServiceException.class)
55   public final ResponseEntity<ProxyErrorDetails> handleGenericException(GenericServiceException ex, WebRequest request) {
56           Date curDate = new Date();
57       SimpleDateFormat format = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
58           String DateToStr = format.format(curDate);
59           String[] msg = ex.getMessage().split("resultCode:");
60           int statusCode = Integer.valueOf(msg[1]);
61           HttpStatus hs = HttpStatus.valueOf(statusCode);
62           Matcher m = Pattern.compile("\\[\"(.*?)\"\\,").matcher(msg[0]);
63           
64           String message="";
65           while(m.find()) {
66                   message=m.group(0);
67           }
68           message=message.replaceAll("\\[\\\"", "").replaceAll("\"\\,","");
69           ProxyErrorDetails errorDetails = new ProxyErrorDetails(
70                 DateToStr,message,request.getDescription(false),hs.name().toString().toUpperCase(),msg[1]);    
71     return new ResponseEntity<>(errorDetails, hs);
72   }
73 }