Project

General

Profile

1
package eu.dnetlib.openaire.exporter;
2

    
3
import java.util.List;
4
import java.util.stream.Collectors;
5

    
6
import com.fasterxml.jackson.annotation.JsonAutoDetect;
7
import eu.dnetlib.enabling.datasources.common.DsmException;
8
import eu.dnetlib.enabling.datasources.common.DsmForbiddenException;
9
import eu.dnetlib.enabling.datasources.common.DsmNotFoundException;
10
import org.apache.commons.lang3.StringUtils;
11
import org.apache.commons.lang3.exception.ExceptionUtils;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.springframework.http.HttpStatus;
15
import org.springframework.web.bind.MethodArgumentNotValidException;
16
import org.springframework.web.bind.annotation.ExceptionHandler;
17
import org.springframework.web.bind.annotation.ResponseBody;
18
import org.springframework.web.bind.annotation.ResponseStatus;
19

    
20
/**
21
 * Created by claudio on 18/07/2017.
22
 */
23
public abstract class AbstractExporterController {
24

    
25
	private static final Log log = LogFactory.getLog(AbstractExporterController.class); // NOPMD by marko on 11/24/08 5:02 PM
26

    
27
	/*
28
	Tags used to group the operations on the swagger UI
29
	 */
30
	protected final static String DS = "datasource";
31
	protected final static String API = "API";
32
	protected final static String R = "read";
33
	protected final static String W = "write";
34

    
35
	protected final static String M = "Management";
36

    
37
	protected final static String DSPACE = "DSpace";
38
	protected final static String EPRINT = "EPrints";
39
	protected final static String TSV = "TSV";
40
	protected final static String STREAMING = "Streaming";
41

    
42
	@ResponseBody
43
	@ExceptionHandler(DsmException.class)
44
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
45
	public ErrorMessage handleDSMException(final Exception e) {
46
		return _handleError(e);
47
	}
48

    
49
	@ResponseBody
50
	@ExceptionHandler(DsmForbiddenException.class)
51
	@ResponseStatus(value = HttpStatus.FORBIDDEN)
52
	public ErrorMessage handleForbiddenException(final Exception e) {
53
		return _handleError(e);
54
	}
55

    
56
	@ResponseBody
57
	@ExceptionHandler(DsmNotFoundException.class)
58
	@ResponseStatus(value = HttpStatus.FORBIDDEN)
59
	public ErrorMessage handleNotFoundException(final Exception e) {
60
		return _handleError(e);
61
	}
62

    
63
	@ResponseBody
64
	@ExceptionHandler(MethodArgumentNotValidException.class)
65
	@ResponseStatus(HttpStatus.BAD_REQUEST)
66
	public List<ErrorMessage> processValidationError(final MethodArgumentNotValidException e) {
67
		return e.getBindingResult()
68
				.getFieldErrors().stream()
69
					.map(fe -> new ErrorMessage(
70
							String.format("field '%s'", fe.getField()),
71
							String.format("rejected value '%s'", fe.getRejectedValue()),
72
							fe.getDefaultMessage()))
73
					.collect(Collectors.toList());
74
	}
75

    
76
	private ErrorMessage _handleError(final Exception e) {
77
		log.debug(e.getMessage());
78
		if (StringUtils.containsIgnoreCase(ExceptionUtils.getRootCauseMessage(e), "Broken pipe")) {
79
			return null;        //socket is closed, cannot return any response
80
		} else {
81
			return new ErrorMessage(e);
82
		}
83
	}
84

    
85
	@JsonAutoDetect
86
	public class ErrorMessage {
87

    
88
		private final String message;
89
		private final String details;
90
		private final String stacktrace;
91

    
92
		public ErrorMessage(final Exception e) {
93
			this(e.getMessage(), "", ExceptionUtils.getStackTrace(e));
94
		}
95

    
96
		public ErrorMessage(final String message, final String details, final String stacktrace) {
97
			this.message = message;
98
			this.details = details;
99
			this.stacktrace = stacktrace;
100
		}
101

    
102
		public String getMessage() {
103
			return this.message;
104
		}
105

    
106
		public String getStacktrace() {
107
			return this.stacktrace;
108
		}
109

    
110
		public String getDetails() {
111
			return details;
112
		}
113
	}
114

    
115
}
(1-1/2)