org.projectforge.web.meb.SMSReceiverServlet.java Source code

Java tutorial

Introduction

Here is the source code for org.projectforge.web.meb.SMSReceiverServlet.java

Source

/////////////////////////////////////////////////////////////////////////////
//
// Project ProjectForge Community Edition
//         www.projectforge.org
//
// Copyright (C) 2001-2013 Kai Reinhard (k.reinhard@micromata.de)
//
// ProjectForge is dual-licensed.
//
// This community edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 3 of the License.
//
// This community edition 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 General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see http://www.gnu.org/licenses/.
//
/////////////////////////////////////////////////////////////////////////////

package org.projectforge.web.meb;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.projectforge.core.Configuration;
import org.projectforge.core.ConfigXml;
import org.projectforge.meb.MebDao;
import org.projectforge.meb.MebEntryDO;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

/**
 * Servlet which supports receiving SMS via http get:<br/>
 * https://www.projectforge.org/secure/SMSReceiver?key=5fcs2lp&date=20101105171233&sender=0170123456&msg=Hello... <br/>
 * Parameter should be given in Format yyyymmddhhMMss or as milliseconds since 1970/01/01.
 * @author Kai Reinhard (k.reinhard@micromata.de)
 * 
 */
public class SMSReceiverServlet extends HttpServlet {
    public static final String URL = "secure/SMSReceiver";

    private static final long serialVersionUID = 5382567744977121735L;

    // Should not be final because this logger is replaced by a Mockito spy object in test class.
    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(SMSReceiverServlet.class);

    private MebDao mebDao;

    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
            throws ServletException, IOException {
        log.debug("Start doPost");
        // https://projectforge.micromata.de/secure/SMSReceiver?key=<key>&date=20101105171233&sender=01701891142&msg=Hallo...
        req.setCharacterEncoding("UTF-8");
        final String key = req.getParameter("key");
        final String expectedKey = ConfigXml.getInstance().getReceiveSmsKey();
        if (StringUtils.isBlank(expectedKey) == true) {
            log.warn(
                    "Servlet call for receiving sms ignored because receiveSmsKey is not given in config.xml file.");
            response(resp, "NOT YET CONFIGURED");
            return;
        }
        if (expectedKey.equals(key) == false) {
            log.warn("Servlet call for receiving sms ignored because receiveSmsKey does not match given key: "
                    + key);
            response(resp, "DENIED");
            return;
        }
        final String dateString = req.getParameter("date");
        if (StringUtils.isBlank(dateString) == true) {
            log.warn("Servlet call for receiving sms ignored because parameter 'date' is not given.");
            response(resp, "Missing parameter 'date'.");
            return;
        }
        final String sender = req.getParameter("sender");
        if (StringUtils.isBlank(sender) == true) {
            log.warn("Servlet call for receiving sms ignored because parameter 'sender' is not given.");
            response(resp, "Missing parameter 'sender'.");
            return;
        }
        final String msg = req.getParameter("msg");
        if (StringUtils.isBlank(msg) == true) {
            log.warn("Servlet call for receiving sms ignored because parameter 'msg' is not given.");
            response(resp, "Missing parameter 'msg'.");
            return;
        }
        final Date date = MebDao.parseDate(dateString);
        if (date == null) {
            log.warn("Servlet call for receiving sms ignored because couln't parse parameter 'date'.");
            response(resp, "Unsupported date format.");
            return;
        }
        final ConfigurableListableBeanFactory beanFactory = Configuration.getInstance().getBeanFactory();
        beanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
        MebEntryDO entry = new MebEntryDO();
        entry.setDate(date);
        entry.setSender(sender);
        entry.setMessage(msg);
        log.info("Servlet-call: date=[" + date + "], sender=[" + sender + "]");
        mebDao.checkAndAddEntry(entry, "SERVLET");
        response(resp, "OK");
    }

    private void response(final HttpServletResponse resp, final String result) {
        try {
            resp.getWriter().print(result);
        } catch (IOException ex) {
            log.error("Exception encountered while writing servlet response: " + ex, ex);
        }
    }

    public void setMebDao(MebDao mebDao) {
        this.mebDao = mebDao;
    }
}