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 C = "community";
31
	protected final static String C_CP = "community content providers";
32
	protected final static String C_PJ = "community projects";
33

    
34
	protected final static String DS = "datasource";
35
	protected final static String API = "API";
36
	protected final static String R = "read";
37
	protected final static String W = "write";
38

    
39
	protected final static String D = "deprecated";
40
	protected final static String M = "Management";
41

    
42
	protected final static String DSPACE = "DSpace";
43
	protected final static String EPRINT = "EPrints";
44
	protected final static String TSV = "TSV";
45
	protected final static String STREAMING = "Streaming";
46

    
47
	@ResponseBody
48
	@ExceptionHandler(DsmException.class)
49
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
50
	public ErrorMessage handleDSMException(final Exception e) {
51
		return _handleError(e);
52
	}
53

    
54
	@ResponseBody
55
	@ExceptionHandler(DsmForbiddenException.class)
56
	@ResponseStatus(value = HttpStatus.FORBIDDEN)
57
	public ErrorMessage handleForbiddenException(final Exception e) {
58
		return _handleError(e);
59
	}
60

    
61
	@ResponseBody
62
	@ExceptionHandler(DsmNotFoundException.class)
63
	@ResponseStatus(value = HttpStatus.FORBIDDEN)
64
	public ErrorMessage handleNotFoundException(final Exception e) {
65
		return _handleError(e);
66
	}
67

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

    
81
	private ErrorMessage _handleError(final Exception e) {
82
		log.debug(e.getMessage());
83
		if (StringUtils.containsIgnoreCase(ExceptionUtils.getRootCauseMessage(e), "Broken pipe")) {
84
			return null;        //socket is closed, cannot return any response
85
		} else {
86
			return new ErrorMessage(e);
87
		}
88
	}
89

    
90
	@JsonAutoDetect
91
	public class ErrorMessage {
92

    
93
		private final String message;
94
		private final String details;
95
		private final String stacktrace;
96

    
97
		public ErrorMessage(final Exception e) {
98
			this(e.getMessage(), "", ExceptionUtils.getStackTrace(e));
99
		}
100

    
101
		public ErrorMessage(final String message, final String details, final String stacktrace) {
102
			this.message = message;
103
			this.details = details;
104
			this.stacktrace = stacktrace;
105
		}
106

    
107
		public String getMessage() {
108
			return this.message;
109
		}
110

    
111
		public String getStacktrace() {
112
			return this.stacktrace;
113
		}
114

    
115
		public String getDetails() {
116
			return details;
117
		}
118
	}
119

    
120
}
(1-1/2)