Package org.mortbay.util.ajax

Examples of org.mortbay.util.ajax.Continuation


        long timeout = getReadTimeout(request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("doMessage timeout=" + timeout);
        }

        Continuation continuation = ContinuationSupport.getContinuation(request, client);
        Listener listener = getListener(request);
        if (listener != null && continuation != null && !continuation.isPending()) {
            listener.access();
        }

        Message message = null;
        synchronized (client) {

            List consumers = client.getConsumers();
            MessageAvailableConsumer consumer = null;

            // Look for a message that is ready to go
            for (int i = 0; message == null && i < consumers.size(); i++) {
                consumer = (MessageAvailableConsumer)consumers.get(i);
                if (consumer.getAvailableListener() == null) {
                    continue;
                }

                // Look for any available messages
                message = consumer.receiveNoWait();
                if (LOG.isDebugEnabled()) {
                    LOG.debug("received " + message + " from " + consumer);
                }
            }

            // Get an existing Continuation or create a new one if there are no
            // messages

            if (message == null) {
                // register this continuation with our listener.
                listener.setContinuation(continuation);

                // Get the continuation object (may wait and/or retry
                // request here).
                continuation.suspend(timeout);
            }
            listener.setContinuation(null);

            // prepare the responds
            response.setContentType("text/xml");
View Full Code Here


            if (LOG.isDebugEnabled()) {
                LOG.debug("Receiving message(s) from: " + destination + " with timeout: " + timeout);
            }

            MessageAvailableConsumer consumer = (MessageAvailableConsumer)client.getConsumer(destination);
            Continuation continuation = null;
            Listener listener = null;
            Message message = null;

            synchronized (consumer) {
                // Fetch the listeners
                listener = (Listener)consumer.getAvailableListener();
                if (listener == null) {
                    listener = new Listener(consumer);
                    consumer.setAvailableListener(listener);
                }
                // Look for any available messages
                message = consumer.receiveNoWait();

                // Get an existing Continuation or create a new one if there are
                // no events.
                if (message == null) {
                    continuation = ContinuationSupport.getContinuation(request, consumer);

                    // register this continuation with our listener.
                    listener.setContinuation(continuation);

                    // Get the continuation object (may wait and/or retry
                    // request here).
                    continuation.suspend(timeout);
                }

                // Try again now
                if (message == null) {
                    message = consumer.receiveNoWait();
View Full Code Here

                }

                setCurrentConnection(this);
                long io = 0;

                Continuation continuation = _request.getContinuation();
                if (continuation != null && continuation.isPending())
                {
                    Log.debug("resume continuation {}",continuation);
                    if (_request.getMethod() == null)
                        throw new IllegalStateException();
                    handleRequest();
                }
                else
                {
                    // If we are not ended then parse available
                    if (!_parser.isComplete())
                        io = _parser.parseAvailable();

                    // Do we have more generating to do?
                    // Loop here because some writes may take multiple steps and
                    // we need to flush them all before potentially blocking in
                    // the
                    // next loop.
                    while (_generator.isCommitted() && !_generator.isComplete())
                    {
                        long written = _generator.flush();
                        io += written;
                        if (written <= 0)
                            break;
                        if (_endp.isBufferingOutput())
                            _endp.flush();
                    }

                    // Flush buffers
                    if (_endp.isBufferingOutput())
                    {
                        _endp.flush();
                        if (!_endp.isBufferingOutput())
                            no_progress = 0;
                    }

                    if (io > 0)
                        no_progress = 0;
                    else if (no_progress++ >= 2)
                        return;
                }
            }
            catch (HttpException e)
            {
                if (Log.isDebugEnabled())
                {
                    Log.debug("uri=" + _uri);
                    Log.debug("fields=" + _requestFields);
                    Log.debug(e);
                }
                _generator.sendError(e.getStatus(),e.getReason(),null,true);

                _parser.reset(true);
                _endp.close();
                throw e;
            }
            finally
            {
                setCurrentConnection(null);

                more_in_buffer = _parser.isMoreInBuffer() || _endp.isBufferingInput();

                synchronized (this)
                {
                    _handling = false;

                    if (_destroy)
                    {
                        destroy();
                        return;
                    }
                }

                if (_parser.isComplete() && _generator.isComplete() && !_endp.isBufferingOutput())
                {
                    if (!_generator.isPersistent())
                    {
                        _parser.reset(true);
                        more_in_buffer = false;
                    }

                    if (more_in_buffer)
                    {
                        reset(false);
                        more_in_buffer = _parser.isMoreInBuffer() || _endp.isBufferingInput();
                    }
                    else
                        reset(true);

                    no_progress = 0;
                }

                Continuation continuation = _request.getContinuation();
                if (continuation != null && continuation.isPending())
                {
                    break;
                }
                else if (_generator.isCommitted() && !_generator.isComplete() && _endp instanceof SelectChannelEndPoint) // TODO
                                                                                                                         // remove
View Full Code Here

    }
   
    @Test
    public void testRetrieveFromContinuation() throws Exception {
       
        Continuation continuation = EasyMock.createMock(Continuation.class);
       
        Message m = new MessageImpl();
        ContinuationInfo ci = new ContinuationInfo(m);
        Object userObject = new Object();
        ci.setUserObject(userObject);
        continuation.getObject();
        EasyMock.expectLastCall().andReturn(ci);
        continuation.setObject(ci.getUserObject());
        EasyMock.expectLastCall();
        EasyMock.replay(continuation);
       
        HttpServletRequest httpRequest = EasyMock.createMock(HttpServletRequest.class);
        httpRequest.getAttribute("org.mortbay.jetty.ajax.Continuation");
View Full Code Here

    }
   
    @Test
    public void testContinuationsIgnored() throws Exception {
       
        Continuation continuation = EasyMock.createMock(Continuation.class);
        HttpServletRequest httpRequest = EasyMock.createMock(HttpServletRequest.class);
        httpRequest.getAttribute("org.mortbay.jetty.ajax.Continuation");
        EasyMock.expectLastCall().andReturn(continuation);
        EasyMock.replay(httpRequest);
       
View Full Code Here

    public String getAuthMethod() {
        return this.endpoint.getAuthMethod();
    }
   
    public void process(MessageExchange exchange) throws Exception {
        Continuation cont = (Continuation) locks.remove(exchange.getExchangeId());
        if (cont != null) {
            synchronized (cont) {
                if (log.isDebugEnabled()) {
                    log.debug("Resuming continuation for exchange: " + exchange.getExchangeId());
                }
                exchanges.put(exchange.getExchangeId(), exchange);
                cont.resume();
            }
        } else {
            throw new IllegalStateException("Exchange not found");
        }
    }
View Full Code Here

        if (!"POST".equals(request.getMethod())) {
            response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, request.getMethod() + " not supported");
            return;
        }
        // Not giving a specific mutex will synchronize on the contination itself
        Continuation cont = ContinuationSupport.getContinuation(request, null);
        MessageExchange exchange;
        // If the continuation is not a retry
        if (!cont.isPending()) {
            try {
                SoapMessage message = soapHelper.getSoapMarshaler().createReader().read(
                                            request.getInputStream(),
                                            request.getHeader(Constants.HEADER_CONTENT_TYPE));
                Context context = soapHelper.createContext(message);
                if (request.getUserPrincipal() != null) {
                    if (request.getUserPrincipal() instanceof JaasJettyPrincipal) {
                        Subject subject = ((JaasJettyPrincipal) request.getUserPrincipal()).getSubject();
                        context.getInMessage().setSubject(subject);
                    } else {
                        context.getInMessage().addPrincipal(request.getUserPrincipal());
                    }
                }
                request.setAttribute(Context.class.getName(), context);
                exchange = soapHelper.onReceive(context);
                NormalizedMessage inMessage = exchange.getMessage("in");
                if (getConfiguration().isWantHeadersFromHttpIntoExchange()) {
                    inMessage.setProperty(JbiConstants.PROTOCOL_HEADERS, getHeaders(request));
                }
                locks.put(exchange.getExchangeId(), cont);
                request.setAttribute(MessageExchange.class.getName(), exchange.getExchangeId());
                synchronized (cont) {
                    channel.send(exchange);
                    if (log.isDebugEnabled()) {
                        log.debug("Suspending continuation for exchange: " + exchange.getExchangeId());
                    }
                    boolean result = cont.suspend(suspentionTime);
                    exchanges.remove(exchange.getExchangeId());
                    if (!result) {
                        throw new Exception("Error sending exchange: aborted");
                    }
                    request.removeAttribute(MessageExchange.class.getName());
                }
            } catch (RetryRequest retry) {
                throw retry;
            } catch (SoapFault fault) {
                sendFault(fault, request, response);
                return;
            } catch (Exception e) {
                SoapFault fault = new SoapFault(e);
                sendFault(fault, request, response);
                return;
            }
        } else {
            String id = (String) request.getAttribute(MessageExchange.class.getName());
            exchange = (MessageExchange) exchanges.remove(id);
            request.removeAttribute(MessageExchange.class.getName());
            boolean result = cont.suspend(0);
            // Check if this is a timeout
            if (exchange == null) {
                throw new IllegalStateException("Exchange not found");
            }
            if (!result) {
View Full Code Here

    }
   
    @Test
    public void testRetrieveFromContinuation() throws Exception {
       
        Continuation continuation = EasyMock.createMock(Continuation.class);
       
        Message m = new MessageImpl();
        ContinuationInfo ci = new ContinuationInfo(m);
        Object userObject = new Object();
        ci.setUserObject(userObject);
        continuation.getObject();
        EasyMock.expectLastCall().andReturn(ci);
        continuation.setObject(ci.getUserObject());
        EasyMock.expectLastCall();
        EasyMock.replay(continuation);
       
        HttpServletRequest httpRequest = EasyMock.createMock(HttpServletRequest.class);
        httpRequest.getAttribute("org.mortbay.jetty.ajax.Continuation");
View Full Code Here

    }
   
    @Test
    public void testContinuationsIgnored() throws Exception {
       
        Continuation continuation = EasyMock.createMock(Continuation.class);
        HttpServletRequest httpRequest = EasyMock.createMock(HttpServletRequest.class);
        httpRequest.getAttribute("org.mortbay.jetty.ajax.Continuation");
        EasyMock.expectLastCall().andReturn(continuation);
        EasyMock.replay(httpRequest);
       
View Full Code Here

       
        if (!engine.getContinuationsEnabled()) {
            return null;
        }
       
        Continuation cont = ContinuationSupport.getContinuation(req, null);
        synchronized (cont) {
            Object o = cont.getObject();
            if (o instanceof ContinuationInfo) {
                ContinuationInfo ci = (ContinuationInfo)o;
                m = (MessageImpl)ci.getMessage();
               
                // now that we got the message we don't need ContinuationInfo
                // as we don't know how continuation was suspended, by jetty wrapper
                // or directly in which (latter) case we need to ensure that an original user object
                // if any, need to be restored
                cont.setObject(ci.getUserObject());
            }
            if (m == null && (cont.isPending() || cont.isResumed())) {
                String message = "No message for existing continuation, status : "
                    + (cont.isPending() ? "Pending" : "Resumed");
                if (!(o instanceof ContinuationInfo)) {
                    message += ", ContinuationInfo object is unavailable";
                }
                LOG.warning(message);
            }
View Full Code Here

TOP

Related Classes of org.mortbay.util.ajax.Continuation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.