Search in sources :

Example 1 with AttributeHolder

use of org.glassfish.grizzly.attributes.AttributeHolder in project openmq by eclipse-ee4j.

the class StompMessageFilter method handleRead.

/**
 * @param ctx Context of {@link FilterChainContext} processing
 * @return the next action
 */
@Override
public NextAction handleRead(final FilterChainContext ctx) throws IOException {
    BridgeContext bc = null;
    synchronized (this) {
        if (_bc == null || _jmsprop == null || logger == null) {
            if (logger != null) {
                logger.logWarn("Stomp Service not ready yet", null);
            }
            throw new IOException("Stomp service not ready yet");
        }
        bc = _bc;
    }
    final Connection c = ctx.getConnection();
    StompProtocolHandler sph = sphAttr.get(c);
    if (logger.isFinestLoggable()) {
        logger.logFinest(this + ", conn=@" + c.hashCode() + ", sph=@" + (sph == null ? "null" : sph.hashCode()), null);
    }
    AttributeHolder ah = ctx.getAttributes();
    ah.setAttribute(STOMP_PROTOCOL_HANDLER, sph);
    final Buffer input = ctx.getMessage();
    final PacketParseState parsestate = parsestateAttr.get(c);
    int pos = input.position();
    StompFrameMessageImpl _message = null;
    try {
        if (logger.isFinestLoggable()) {
            logger.logFinest(this + ", position=" + pos + ", input=" + input, null);
        }
        if (parsestate.message == null) {
            if (input.remaining() >= StompFrameMessage.MIN_COMMAND_LEN) {
                parsestate.message = StompFrameMessageImpl.parseCommand(input, logger);
                if (logger.isFinestLoggable()) {
                    logger.logFinest("returned from parseCommand with " + parsestate.message, null);
                }
            }
            if (parsestate.message == null) {
                input.position(pos);
                return ctx.getStopAction(input);
            }
        }
        _message = parsestate.message;
        if (_message.getNextParseStage() == StompFrameMessage.ParseStage.HEADER) {
            _message.parseHeader(input);
            if (logger.isFinestLoggable()) {
                logger.logFinest("returned from parseHeader", null);
            }
        }
        if (_message.getNextParseStage() == StompFrameMessage.ParseStage.BODY) {
            _message.readBody(input);
        }
        if (_message.getNextParseStage() == StompFrameMessage.ParseStage.NULL) {
            _message.readNULL(input);
        }
        if (logger.isFinestLoggable()) {
            logger.logFinest("position=" + input.position() + ", input=" + input + ", nextParseState=" + _message.getNextParseStage(), null);
        }
        if (_message.getNextParseStage() != StompFrameMessage.ParseStage.DONE) {
            if (logger.isFinestLoggable()) {
                logger.logFinest("StopAction with position=" + input.position() + ", hasRemaining=" + input.hasRemaining(), null);
            }
            return ctx.getStopAction((input.hasRemaining() ? input : null));
        }
        ctx.setMessage(_message);
        Exception pex = _message.getParseException();
        if (pex != null) {
            if (pex instanceof StompFrameParseException) {
                _message = (StompFrameMessageImpl) ((StompFrameParseException) pex).getStompMessageERROR(StompFrameMessageImpl.getFactory(), logger);
            } else {
                _message = (StompFrameMessageImpl) (new StompFrameParseException(pex.getMessage(), pex)).getStompMessageERROR(StompFrameMessageImpl.getFactory(), logger);
            }
            ctx.setMessage(_message);
            parsestate.reset();
            return ctx.getInvokeAction();
        }
        final Buffer remainder = input.split(input.position());
        parsestate.reset();
        return ctx.getInvokeAction(remainder.hasRemaining() ? remainder : null);
    } catch (Throwable t) {
        if (t instanceof OutOfMemoryError) {
            logger.logSevere(_OOMMSG, null);
            bc.handleGlobalError(t, _OOMMSG);
        } else {
            logger.logSevere(StompServer.getStompBridgeResources().getKString(StompBridgeResources.E_PARSE_INCOMING_DATA_FAILED, t.getMessage()), t);
        }
        try {
            if (t instanceof StompFrameParseException) {
                _message = (StompFrameMessageImpl) ((StompFrameParseException) t).getStompMessageERROR(StompFrameMessageImpl.getFactory(), logger);
                _message.setFatalERROR();
            } else {
                _message = (StompFrameMessageImpl) (new StompFrameParseException(t.getMessage(), t, true)).getStompMessageERROR(StompFrameMessageImpl.getFactory(), logger);
            }
        } catch (Throwable tt) {
            if (t instanceof OutOfMemoryError) {
                _message = (StompFrameMessageImpl) StompFrameParseException.OOMMSG;
            } else {
                logger.logSevere(StompServer.getStompBridgeResources().getKString(StompBridgeResources.E_UNABLE_CREATE_ERROR_MSG, t.getMessage()), tt);
                RuntimeException re = new RuntimeException(tt.getMessage());
                re.initCause(tt);
                throw re;
            }
        }
        ctx.setMessage(_message);
        parsestate.reset();
        return ctx.getInvokeAction();
    }
}
Also used : Buffer(org.glassfish.grizzly.Buffer) AttributeHolder(org.glassfish.grizzly.attributes.AttributeHolder) Connection(org.glassfish.grizzly.Connection) BridgeContext(com.sun.messaging.bridge.api.BridgeContext) IOException(java.io.IOException) IOException(java.io.IOException) StompFrameParseException(com.sun.messaging.bridge.api.StompFrameParseException) StompProtocolHandler(com.sun.messaging.bridge.api.StompProtocolHandler) StompFrameParseException(com.sun.messaging.bridge.api.StompFrameParseException)

Aggregations

BridgeContext (com.sun.messaging.bridge.api.BridgeContext)1 StompFrameParseException (com.sun.messaging.bridge.api.StompFrameParseException)1 StompProtocolHandler (com.sun.messaging.bridge.api.StompProtocolHandler)1 IOException (java.io.IOException)1 Buffer (org.glassfish.grizzly.Buffer)1 Connection (org.glassfish.grizzly.Connection)1 AttributeHolder (org.glassfish.grizzly.attributes.AttributeHolder)1