Search in sources :

Example 1 with SimpleMessage

use of com.sun.messaging.ums.simple.SimpleMessage in project openmq by eclipse-ee4j.

the class UMSServlet method doSimpleMessaging.

/**
 * Send/Receive a simple text message. Default domain is queue. To specify domain, add domain=queue/topic in the query
 * string.
 *
 * Example:
 *
 * http://host:port/xmlprotocol/service?clientId=chiaming&service=receive&destination=testQ
 * http://host:port/xmlprotocol/service?clientId=chiaming&service=send&destination=testQ&text=myMessage
 */
public void doSimpleMessaging(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String respMsg = null;
    int status = resp.SC_BAD_REQUEST;
    boolean isSend = false;
    boolean isReceive = false;
    boolean isAdmin = false;
    boolean isLogin = false;
    boolean isClose = false;
    boolean isCommit = false;
    boolean isRollback = false;
    boolean isValidRequest = true;
    // String separator = ",";
    Map map = this.getHeadersAsMap(req);
    // String text = this.readHttpBody(req);
    InputStream in = req.getInputStream();
    SimpleMessage msg = SimpleMessageFactory.createMessage(map, in);
    in.close();
    // String text = msg.getText();
    String destName = msg.getMessageProperty(Constants.DESTINATION_NAME);
    boolean isTopic = msg.isTopicDomain();
    String clientId = msg.getMessageProperty(Constants.CLIENT_ID);
    if (UMSServiceImpl.debug) {
        logger.info("Simple messaging sid=" + clientId);
    }
    if (msg.isSendService()) {
        isSend = true;
    } else if (msg.isReceiveService()) {
        isReceive = true;
    } else if (msg.isLoginService()) {
        isLogin = true;
    } else if (msg.isCloseService()) {
        isClose = true;
    } else if (msg.isAdminService()) {
        isAdmin = true;
    } else if (msg.isCommitService()) {
        isCommit = true;
    } else if (msg.isRollbackService()) {
        isRollback = true;
    } else {
        isValidRequest = false;
    }
    try {
        if (isValidRequest == false) {
            status = resp.SC_BAD_REQUEST;
            // throw exception
            respMsg = "Invalid query string., see http://host:port/<context>/";
        } else {
            if (isSend) {
                // String text = req.getParameter(Constants.HTTP_GET_SEND_TEXT);
                String text = msg.getText();
                if (UMSServiceImpl.debug) {
                    logger.info("Simple messaging, sending text=" + text);
                }
                this.JMSService.sendText(clientId, isTopic, destName, text, map);
                resp.setHeader(UMS_SERVICE, Constants.SERVICE_VALUE_SEND_MESSAGE_REPLY);
                resp.setHeader(UMS_DESTINATION, destName);
                resp.setHeader(UMS_DOMAIN, getDomain(isTopic));
                resp.setHeader(UMS_MOM, JMSService.getProvider(map));
                resp.setHeader(UMS_STATUS, Constants.SERVICE_STATUS_VALUE_OK);
            } else if (isReceive) {
                long timeout = this.getServiceTimeout(msg);
                // receive message
                String text = this.JMSService.receiveText(clientId, destName, isTopic, timeout, map);
                resp.setHeader(UMS_SERVICE, Constants.SERVICE_VALUE_RECEIVE_MESSAGE_REPLY);
                resp.setHeader(UMS_DESTINATION, destName);
                resp.setHeader(UMS_DOMAIN, getDomain(isTopic));
                resp.setHeader(UMS_MOM, JMSService.getProvider(map));
                respMsg = text;
                if (respMsg == null) {
                    // respMsg = "null";
                    resp.setHeader(UMS_STATUS, Constants.SERVICE_STATUS_VALUE_NO_MESSAGE);
                } else {
                    resp.setHeader(UMS_STATUS, Constants.SERVICE_STATUS_VALUE_OK);
                }
            } else if (isLogin) {
                String sid = this.JMSService.authenticate(map);
                resp.setHeader(UMS_SERVICE, Constants.SERVICE_VALUE_LOGIN_REPLY);
                resp.setHeader(UMS_MOM, JMSService.getProvider(map));
                resp.setHeader(UMS_STATUS, Constants.SERVICE_STATUS_VALUE_OK);
                respMsg = sid;
            } else if (isClose) {
                this.JMSService.closeClient2(map);
                resp.setHeader(UMS_SERVICE, Constants.SERVICE_VALUE_CLOSE_REPLY);
                resp.setHeader(UMS_MOM, JMSService.getProvider(map));
                resp.setHeader(UMS_STATUS, Constants.SERVICE_STATUS_VALUE_OK);
            } else if (isCommit) {
                this.JMSService.commit(msg);
                resp.setHeader(UMS_SERVICE, Constants.SERVICE_VALUE_COMMIT_REPLY);
                resp.setHeader(UMS_MOM, JMSService.getProvider(map));
                resp.setHeader(UMS_STATUS, Constants.SERVICE_STATUS_VALUE_OK);
            } else if (isRollback) {
                this.JMSService.rollback(msg);
                resp.setHeader(UMS_SERVICE, Constants.SERVICE_VALUE_ROLLBACK_REPLY);
                resp.setHeader(UMS_MOM, JMSService.getProvider(map));
                resp.setHeader(UMS_STATUS, Constants.SERVICE_STATUS_VALUE_OK);
            } else if (isAdmin) {
                String user = req.getParameter(Constants.USER);
                String pass = req.getParameter(Constants.PASSWORD);
                ProviderDestinationService pds = DestinationService.getProviderDestinationService(null);
                pds.authenticate(user, pass);
                String flag = req.getParameter(ADMIN_DEBUG);
                boolean debug = Boolean.parseBoolean(flag);
                UMSServiceImpl.debug = debug;
                respMsg = SERVICE + "admin, " + ADMIN_DEBUG + "=" + debug;
            }
            status = resp.SC_OK;
        }
        resp.setStatus(status);
        // resp.setHeader("Content-Type", "text/plain;charset=UTF-8");
        resp.setHeader(CONTENT_TYPE, PLAIN_TEXT_CONTENT_TYPE);
        // resp.setCharacterEncoding("UTF-8");
        byte[] data = null;
        if (respMsg != null) {
            data = respMsg.getBytes(UTF8);
        } else {
            // data = "".getBytes(UTF8);
            data = EMPTY_STRING.getBytes(UTF8);
        }
        DataOutputStream dos = new DataOutputStream(resp.getOutputStream());
        resp.setContentLength(data.length);
        dos.write(data, 0, data.length);
        // dos.writeUTF(respMsg);
        dos.flush();
        dos.close();
    } catch (Exception e) {
        logger.log(Level.WARNING, e.getMessage(), e);
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        resp.setHeader(CONTENT_TYPE, PLAIN_TEXT_CONTENT_TYPE);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        e.printStackTrace(ps);
        byte[] data = baos.toString().getBytes(UTF8);
        DataOutputStream dos = new DataOutputStream(resp.getOutputStream());
        resp.setContentLength(data.length);
        dos.write(data, 0, data.length);
        dos.flush();
        dos.close();
        resp.flushBuffer();
    }
}
Also used : PrintStream(java.io.PrintStream) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) ProviderDestinationService(com.sun.messaging.ums.provider.openmq.ProviderDestinationService) DataOutputStream(java.io.DataOutputStream) SimpleMessage(com.sun.messaging.ums.simple.SimpleMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map) ServletException(jakarta.servlet.ServletException) SOAPException(jakarta.xml.soap.SOAPException) IOException(java.io.IOException)

Aggregations

ProviderDestinationService (com.sun.messaging.ums.provider.openmq.ProviderDestinationService)1 SimpleMessage (com.sun.messaging.ums.simple.SimpleMessage)1 ServletException (jakarta.servlet.ServletException)1 SOAPException (jakarta.xml.soap.SOAPException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 Map (java.util.Map)1