Projekt

Obecné

Profil

« Předchozí | Další » 

Revize c1ad4a51

Přidáno uživatelem stepanekp před asi 1 rok

Metrics integrated into UI

Zobrazit rozdíly:

src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/detecting/DetectingController.java
5 5
import org.springframework.beans.factory.annotation.Value;
6 6
import org.springframework.http.*;
7 7
import org.springframework.web.bind.annotation.*;
8
import org.springframework.web.client.HttpStatusCodeException;
9
import org.springframework.web.client.RestClientException;
8 10
import org.springframework.web.client.RestTemplate;
9 11
import org.springframework.web.servlet.HandlerMapping;
10 12

  
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/errorHandler/CustomErrorHandler.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.errorHandler;
2

  
3
import org.springframework.http.HttpStatus;
4
import org.springframework.http.client.ClientHttpResponse;
5
import org.springframework.web.client.DefaultResponseErrorHandler;
6
import org.springframework.web.client.HttpClientErrorException;
7

  
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.nio.charset.StandardCharsets;
11

  
12
/**
13
 * @author Jiri Trefil
14
 * Custom HTTP error handler for RestTemplate class
15
 *
16
 */
17
public class CustomErrorHandler extends DefaultResponseErrorHandler {
18
    /**
19
     *
20
     * Override default custom behavior of Response Handler to be able to read body of error responses
21
     * because default error handle can only parse body of generic errors.
22
     * @param response Http response containing error code
23
     * @throws IOException if file descriptor (socket) is no longer valid in the response
24
     */
25
    @Override
26
    public void handleError(ClientHttpResponse response) throws IOException {
27
        if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {
28
            final InputStream inputStream = response.getBody();
29
            byte[] responseBody = inputStream.readAllBytes();
30
            inputStream.close();
31
            //String responseBody = StreamUtils.copyToString(response.getBody(),StandardCharsets.UTF_8);
32
            throw new HttpClientErrorException(response.getStatusCode(),response.getStatusText(),responseBody,StandardCharsets.UTF_8);
33
        } else {
34
            super.handleError(response);
35
        }
36
    }
37

  
38

  
39
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/errorHandler/CustomResponseErrorHandler.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.errorHandler;
2

  
3

  
4
import org.springframework.http.HttpStatus;
5
import org.springframework.http.client.ClientHttpResponse;
6
import org.springframework.web.client.HttpClientErrorException;
7
import org.springframework.web.client.ResponseErrorHandler;
8

  
9
import java.io.BufferedReader;
10
import java.io.IOException;
11
import java.io.InputStream;
12
import java.io.InputStreamReader;
13
import java.util.stream.Collectors;
14

  
15
public class CustomResponseErrorHandler implements ResponseErrorHandler {
16
    @Override
17
    public void handleError(ClientHttpResponse response) throws IOException {
18
        HttpStatus statusCode = response.getStatusCode();
19

  
20
        if (statusCode == HttpStatus.CONFLICT) {
21
            throw new HttpClientErrorException(statusCode, "Conflict error.");
22
        } else if (statusCode == HttpStatus.BAD_REQUEST) {
23
            throw new HttpClientErrorException(statusCode, "Bad Request.");
24
        } else {
25
            throw new HttpClientErrorException(statusCode, "Unexpected error occurred.");
26
        }
27
    }
28

  
29
    @Override
30
    public boolean hasError(ClientHttpResponse response) throws IOException {
31
        return response.getStatusCode().isError();
32
    }
33
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/httpErrorHandler/CustomErrorHandler.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.httpErrorHandler;
2

  
3
import org.springframework.http.HttpStatus;
4
import org.springframework.http.client.ClientHttpResponse;
5
import org.springframework.util.StreamUtils;
6
import org.springframework.web.client.DefaultResponseErrorHandler;
7
import org.springframework.web.client.HttpClientErrorException;
8

  
9
import java.io.IOException;
10
import java.io.InputStream;
11
import java.nio.charset.StandardCharsets;
12

  
13
/**
14
 * @author Jiri Trefil
15
 * Custom HTTP error handler for RestTemplate class
16
 *
17
 */
18
public class CustomErrorHandler extends DefaultResponseErrorHandler {
19
    /**
20
     *
21
     * Override default custom behavior of Response Handler to be able to read body of error responses
22
     * because default error handle can only parse body of generic errors.
23
     * @param response Http response containing error code
24
     * @throws IOException if file descriptor (socket) is no longer valid in the response
25
     */
26
    @Override
27
    public void handleError(ClientHttpResponse response) throws IOException {
28
        if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {
29
            final InputStream inputStream = response.getBody();
30
            byte[] responseBody = inputStream.readAllBytes();
31
            inputStream.close();
32
            //String responseBody = StreamUtils.copyToString(response.getBody(),StandardCharsets.UTF_8);
33
            throw new HttpClientErrorException(response.getStatusCode(),response.getStatusText(),responseBody,StandardCharsets.UTF_8);
34
        } else {
35
            super.handleError(response);
36
        }
37
    }
38

  
39

  
40
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/detecting/DetectingService.java
3 3
import org.springframework.beans.factory.annotation.Autowired;
4 4
import org.springframework.http.*;
5 5
import org.springframework.stereotype.Service;
6
import org.springframework.web.client.HttpClientErrorException;
7
import org.springframework.web.client.HttpStatusCodeException;
8
import org.springframework.web.client.RestClientResponseException;
6 9
import org.springframework.web.client.RestTemplate;
7 10

  
8 11
@Service
......
15 18
        HttpHeaders headers = new HttpHeaders();
16 19
        headers.setContentType(MediaType.APPLICATION_JSON);
17 20

  
18
        HttpEntity<String> requestEntity = null;
19
        if (requestBody != null) {
20
            requestEntity = new HttpEntity<>(requestBody, headers);
21
        } else {
22
            requestEntity = new HttpEntity<>(headers);
23
        }
21
        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
24 22

  
25
        return restTemplate.exchange(url, method, requestEntity, String.class);
23
        try {
24
            return restTemplate.exchange(url, method, requestEntity, String.class);
25
        } catch (RestClientResponseException e) {
26
                return ResponseEntity.status(e.getRawStatusCode()).body(e.getStatusText());
27
        }
26 28
    }
27 29
}
28

  
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/spring/AppConfig.java
1 1
package cz.zcu.fav.kiv.antipatterndetectionapp.spring;
2 2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.errorHandler.CustomResponseErrorHandler;
3 4
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4 5
import org.springframework.context.annotation.Bean;
5 6
import org.springframework.context.annotation.ComponentScan;
......
57 58

  
58 59
    @Bean
59 60
    public RestTemplate restTemplate() {
60
        return new RestTemplate();
61
        RestTemplate restTemplate = new RestTemplate();
62
        restTemplate.setErrorHandler(new CustomResponseErrorHandler());
63
        return restTemplate;
61 64
    }
62 65
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/utils/RequestBuilder.java
1 1
package cz.zcu.fav.kiv.antipatterndetectionapp.utils;
2 2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.httpErrorHandler.CustomErrorHandler;
3
import cz.zcu.fav.kiv.antipatterndetectionapp.errorHandler.CustomErrorHandler;
4 4
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
5 5
import org.springframework.stereotype.Component;
6 6
import org.springframework.util.StreamUtils;

Také k dispozici: Unified diff