org.apigw.authserver.web.controller.AlertController.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.authserver.web.controller.AlertController.java

Source

/**
 *   Copyright 2013 Stockholm County Council
 *
 *   This file is part of APIGW
 *
 *   APIGW is free software; you can redistribute it and/or modify
 *   it under the terms of version 2.1 of the GNU Lesser General Public
 *   License as published by the Free Software Foundation.
 *
 *   APIGW is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with APIGW; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *   Boston, MA 02111-1307  USA
 *
 */
package org.apigw.authserver.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apigw.appmanagement.ApplicationManagementService;
import org.apigw.appmanagement.domain.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

//TODO: Work in progress? Commenting annotations for now to disable this web service.

//@Controller
//@SessionAttributes
//@RequestMapping(value = "alert")
public class AlertController {

    private static final Logger log = LoggerFactory.getLogger(AlertController.class);

    @Autowired
    private ApplicationManagementService appManagement;

    @Autowired
    private JavaMailSender sender;

    //    @RequestMapping(method = RequestMethod.POST)
    public @ResponseBody String alert(@RequestBody Map<String, Object> alert) {

        String clientId, state;

        if (alert.containsKey("categories") && alert.get("categories") instanceof List) {
            List<String> categories = (List<String>) alert.get("categories");
            log.info("cat: " + categories);
            clientId = categories.get(0);
        } else {
            throw new RuntimeException("No categories found");
        }

        for (String k : alert.keySet()) {
            log.info(k + ": " + alert.get(k));
        }

        Application app = appManagement.getApplicationByClientId(clientId);

        if (app != null) {
            String email = app.getEmail();

            try {
                MimeMessage message = sender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message);

                helper.setTo(email);
                helper.setSubject("alert");
                helper.setText("text");
                sender.send(message);
            } catch (Exception e) {
                log.error("Caught exception while trying to send an email", e);
            }
        }

        return "";
    }

    //    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody Map<String, Object> alert() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("hej", "Hej");
        return map;
    }
}