Project

General

Profile

« Previous | Next » 

Revision 60958

[Trunk | Notification Service]:
NotificationServiceCheckDeployController: [NEW] Controller added and
a. "hello()" method (@RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET)) which just prints and returns a greeting message.
b. "checkEverything()" method @RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET) only accessible by PORTAL ADMINS which checks connection with db and prints properties.

View differences:

modules/uoa-notification-service/trunk/src/main/java/eu/dnetlib/uoanotificationservice/controllers/NotificationServiceCheckDeployController.java
1
package eu.dnetlib.uoanotificationservice.controllers;
2

  
3
import com.mongodb.BasicDBObject;
4
import com.mongodb.CommandResult;
5
import com.mongodb.DBObject;
6
import eu.dnetlib.uoanotificationservice.configuration.MongoConfig;
7
import eu.dnetlib.uoanotificationservice.configuration.NotificationMongoConnection;
8
import org.apache.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.data.mongodb.core.MongoTemplate;
11
import org.springframework.security.access.prepost.PreAuthorize;
12
import org.springframework.web.bind.annotation.CrossOrigin;
13
import org.springframework.web.bind.annotation.RequestMapping;
14
import org.springframework.web.bind.annotation.RequestMethod;
15
import org.springframework.web.bind.annotation.RestController;
16

  
17
import java.util.HashMap;
18
import java.util.Map;
19

  
20
@RestController
21
@CrossOrigin(origins = "*")
22
@RequestMapping("/notification-service")
23
public class NotificationServiceCheckDeployController {
24
    private final Logger log = Logger.getLogger(this.getClass());
25

  
26
    @Autowired
27
    private NotificationMongoConnection mongoConnection;
28

  
29
    @Autowired
30
    private MongoConfig mongoConfig;
31

  
32
    @RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET)
33
    public String hello() {
34
        log.debug("Hello from uoa-notification-service!");
35
        return "Hello from uoa-notification-service!";
36
    }
37

  
38
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
39
    @RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET)
40
    public Map<String, String> checkEverything() {
41
        Map<String, String> response = new HashMap<>();
42

  
43
        MongoTemplate mt = mongoConnection.getNotificationTemplate();
44
        DBObject ping = new BasicDBObject("ping", "1");
45
        try {
46
            CommandResult answer = mt.getDb().command(ping);
47
            response.put("Mongo try: error", answer.getErrorMessage());
48
        } catch (Exception e) {
49
            response.put("Mongo catch: error", e.getMessage());
50
        }
51

  
52
        response.put("notification.mongodb.database", mongoConfig.getDatabase());
53
        response.put("notification.mongodb.host", mongoConfig.getHost());
54
        response.put("notification.mongodb.port", mongoConfig.getPort()+"");
55
        response.put("notification.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]");
56
        response.put("notification.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]");
57

  
58
//        response.put("Define also", "notification.mongodb.username, notification.mongodb.password");
59

  
60
        return response;
61
    }
62
}

Also available in: Unified diff