Project

General

Profile

1
package eu.dnetlib.repo.manager.controllers;
2

    
3
import eu.dnetlib.repo.manager.exception.EndPointException;
4
import org.springframework.http.HttpStatus;
5
import org.springframework.http.client.ClientHttpResponse;
6
import org.springframework.stereotype.Component;
7
import org.springframework.web.client.ResponseErrorHandler;
8

    
9
import java.io.IOException;
10

    
11
import static org.springframework.http.HttpStatus.Series.CLIENT_ERROR;
12
import static org.springframework.http.HttpStatus.Series.SERVER_ERROR;
13

    
14
@Component
15
public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {
16

    
17
    @Override
18
    public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
19
        return (httpResponse.getStatusCode().series() == CLIENT_ERROR
20
                || httpResponse.getStatusCode().series() == SERVER_ERROR);
21
    }
22

    
23
    @Override
24
    public void handleError(ClientHttpResponse httpResponse) throws IOException {
25

    
26
        if (httpResponse.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR) {
27
            throw new EndPointException();
28
        } else if (httpResponse.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR) {
29
            if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
30
                throw new IOException();
31
            }
32
        }
33
    }
34
}
(7-7/12)