1d6486b0b434e3301332fd971a19573d1d84fd29
[aai/aai-common.git] / aai-rest / src / main / java / org / onap / aai / restclient / RestClientResponseErrorHandler.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright Â© 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.restclient;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import org.springframework.http.HttpStatus;
24 import org.springframework.http.client.ClientHttpResponse;
25 import org.springframework.web.client.ResponseErrorHandler;
26
27 import java.io.IOException;
28
29 public class RestClientResponseErrorHandler implements ResponseErrorHandler {
30
31     private EELFLogger logger;
32
33     public RestClientResponseErrorHandler(EELFLogger logger) {
34         this.logger = logger;
35     }
36
37     @Override
38     public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
39         if (!clientHttpResponse.getStatusCode().is2xxSuccessful()) {
40
41             logger.debug("Status code: " + clientHttpResponse.getStatusCode());
42
43             if (clientHttpResponse.getStatusCode() == HttpStatus.FORBIDDEN) {
44                 logger.debug("Call returned a error 403 forbidden resposne ");
45                 return true;
46             }
47
48             if (clientHttpResponse.getRawStatusCode() % 100 == 5) {
49                 logger.debug("Call returned a error " + clientHttpResponse.getStatusText());
50                 return true;
51             }
52         }
53         return false;
54     }
55
56     @Override
57     public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
58     }
59 }