com.pureinfo.tgirls.servlet.SendSystemMsgServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.tgirls.servlet.SendSystemMsgServlet.java

Source

/**
 * PureInfo TGirls
 * @(#)SendSystemMsgServlet.java   1.0 2009-3-23
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.tgirls.servlet;

import java.io.IOException;
import java.util.Arrays;

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.apache.log4j.Logger;

import com.pureinfo.tgirls.json.model.JsonBase;
import com.pureinfo.tgirls.json.model.JsonBase.ErrorCode;
import com.pureinfo.tgirls.sns.TOPAPI;
import com.pureinfo.tgirls.sns.constants.SessionConstants;
import com.pureinfo.tgirls.utils.servlet.CookieUtils;
import com.taobao.api.sns.response.ObjResponse;

public class SendSystemMsgServlet extends HttpServlet {
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 1L;
    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String[] uids = request.getParameterValues("uids");
        String msg = request.getParameter("msg");
        JsonBase result = new JsonBase();
        response.setContentType("text/json; charset=utf-8");

        logger.debug("to send msg[" + msg + "] to uids[" + Arrays.toString(uids) + "]");

        try {
            if (uids == null || uids.length == 0 || StringUtils.isEmpty(msg)) {
                throw new Exception("??");
            }
            String sessionId = (String) CookieUtils.getRequestCookieValue(request,
                    SessionConstants.TAOBAO_SESSION_ID);
            for (int i = 0; i < uids.length; i++) {
                String uid = uids[i];

                try {
                    ObjResponse<Boolean> or = TOPAPI.getInstance().sendSysMsg(Long.parseLong(uid), msg, sessionId);
                    if (or.isSuccess()) {
                        logger.info("msg send success for[" + uid + "]");
                    } else {
                        logger.info("msg send error for[" + uid + "]");
                        logger.info(or.getMsg());
                    }
                } catch (Exception e) {
                    logger.error("error when send msg.skip this msg.", e);
                }

            }
        } catch (Exception e) {
            logger.error("error when send msg.", e);
            result.setErrorCode(ErrorCode.ERROR.getCode());
            if (StringUtils.isNotEmpty(e.getMessage())) {
                result.setErrorMsg(e.getMessage());
            } else {
                result.setErrorMsg("");
            }
        }

        response.getWriter().write(result.toString());
        return;
    }

    @Override
    protected void doGet(HttpServletRequest _req, HttpServletResponse _resp) throws ServletException, IOException {
        this.doPost(_req, _resp);
    }

}