Project

General

Profile

1
package eu.dnetlib.msro.openaireplus.workflows.nodes.dhp.message;
2

    
3
import java.util.Arrays;
4
import java.util.List;
5

    
6
import javax.annotation.Resource;
7

    
8
import org.apache.commons.lang3.exception.ExceptionUtils;
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.springframework.http.HttpStatus;
12
import org.springframework.stereotype.Controller;
13
import org.springframework.web.bind.annotation.ExceptionHandler;
14
import org.springframework.web.bind.annotation.RequestBody;
15
import org.springframework.web.bind.annotation.RequestMapping;
16
import org.springframework.web.bind.annotation.RequestMethod;
17
import org.springframework.web.bind.annotation.ResponseBody;
18
import org.springframework.web.bind.annotation.ResponseStatus;
19

    
20
import eu.dnetlib.dhp.message.Message;
21
import eu.dnetlib.msro.rmi.MSROException;
22

    
23
@Controller
24
public class DnetMessageManagerController {
25

    
26
	@Resource
27
	private DnetMessageManager dnetMessageManager;
28

    
29
	private static final Log log = LogFactory.getLog(DnetMessageManagerController.class);
30

    
31
	@RequestMapping(value = "/dhp/message", method = {
32
		RequestMethod.PUT, RequestMethod.POST
33
	})
34
	public @ResponseBody List<String> registerMessage(@RequestBody(required = true) final Message message) throws MSROException {
35
		dnetMessageManager.registerMessage(message);
36
		return Arrays.asList("done");
37
	}
38

    
39
	@ExceptionHandler(Exception.class)
40
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
41
	public @ResponseBody ErrorMessage handleException(final Exception e) {
42
		log.error("Error processing http method", e);
43
		return new ErrorMessage(e);
44
	}
45

    
46
	public class ErrorMessage {
47

    
48
		private final String message;
49
		private final String stacktrace;
50

    
51
		public ErrorMessage(final Exception e) {
52
			this(e.getMessage(), ExceptionUtils.getStackTrace(e));
53
		}
54

    
55
		public ErrorMessage(final String message, final String stacktrace) {
56
			this.message = message;
57
			this.stacktrace = stacktrace;
58
		}
59

    
60
		public String getMessage() {
61
			return this.message;
62
		}
63

    
64
		public String getStacktrace() {
65
			return this.stacktrace;
66
		}
67

    
68
	}
69
}
(2-2/2)