Example usage for javax.servlet.http HttpServletRequest startAsync

List of usage examples for javax.servlet.http HttpServletRequest startAsync

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest startAsync.

Prototype

public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
        throws IllegalStateException;

Source Link

Document

Puts this request into asynchronous mode, and initializes its AsyncContext with the given request and response objects.

Usage

From source file:com.ifactory.service.weather.photo.PhotoServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("application/json");
    final AsyncContext aCtx = req.startAsync(req, resp);
    aCtx.start(new GetPhotoService(aCtx, this.photo));
}

From source file:com.ifactory.service.weather.photo.PhotoServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("application/json");
    final AsyncContext aCtx = req.startAsync(req, resp);
    aCtx.start(new PostPhotoService(aCtx, this.photo));
}

From source file:com.ibm.amc.feedback.FeedbackHandler.java

public void handleRequest(final HttpServletRequest request, final HttpServletResponse response) {
    if (logger.isEntryEnabled())
        logger.entry("handleRequest", request, response);

    final String user = request.getRemoteUser();
    final Queue<ActionStatusResponse> statuses = getActionStatuses(user);
    if (statuses.isEmpty()) {
        // No updates pending - register listener
        final AsyncContext asyncContext = request.startAsync(request, response);
        asyncContext.setTimeout(900000000L);
        logger.debug("handleRequest", "Registering new listener for user " + user);
        synchronized (feedbackListeners) {
            Set<AsyncContext> contexts = feedbackListeners.get(user);
            if (contexts == null) {
                contexts = new HashSet<AsyncContext>();
                feedbackListeners.put(user, contexts);
            }//w  w  w  . j  a  v  a2s .  c  om
            contexts.add(asyncContext);
        }

        // Timeout listener
        executor.schedule(new Runnable() {

            @Override
            public void run() {
                synchronized (feedbackListeners) {
                    final Set<AsyncContext> contexts = feedbackListeners.get(user);
                    if (contexts.remove(asyncContext)) {
                        if (logger.isDebugEnabled())
                            logger.debug("handleRequest$Runnable.run", "Timing out listener for user " + user);
                        writeResponse(asyncContext.getResponse(), new LinkedList<ActionStatusResponse>());
                        asyncContext.complete();
                        if (contexts.isEmpty())
                            feedbackListeners.remove(user);
                    }
                }
            }

        }, POLLING_TIMEOUT, TimeUnit.SECONDS);
    } else {
        // Update pending - send response immediately
        writeResponse(response, statuses);
    }

    if (logger.isEntryEnabled())
        logger.exit("handleRequest");
}

From source file:byps.http.HHttpServlet.java

private HRequestContext createRequestContext(HttpServletRequest request, HttpServletResponse response,
        boolean async) {
    final HRequestContext rctxt = async ? new HAsyncContext(request.startAsync(request, response))
            : new HSyncContext(request, response);
    return rctxt;
}

From source file:jetbrains.buildServer.clouds.azure.arm.web.SettingsController.java

private void doPost(@NotNull final HttpServletRequest request, @NotNull final HttpServletResponse response) {
    final Element xmlResponse = XmlResponseUtil.newXmlResponse();
    final ActionErrors errors = new ActionErrors();
    final String[] resources = request.getParameterValues("resource");
    final List<Promise<Content, Throwable, Void>> promises = new ArrayList<>(resources.length);

    for (final String resource : resources) {
        final ResourceHandler handler = HANDLERS.get(resource);
        if (handler == null)
            continue;

        try {/* w  w  w  .j av  a 2  s  . c  om*/
            final Promise<Content, Throwable, Void> promise = handler.handle(request)
                    .fail(new FailCallback<Throwable>() {
                        @Override
                        public void onFail(Throwable result) {
                            LOG.debug(result);
                            errors.addError(resource, result.getMessage());
                        }
                    });
            promises.add(promise);
        } catch (Throwable t) {
            LOG.debug(t);
            errors.addError(resource, t.getMessage());
        }
    }

    if (promises.size() == 0) {
        if (errors.hasErrors()) {
            errors.serialize(xmlResponse);
        }

        writeResponse(xmlResponse, response);
        return;
    }

    final AsyncContext context = request.startAsync(request, response);
    myManager.when(promises.toArray(new Promise[] {})).always(new AlwaysCallback<MultipleResults, OneReject>() {
        @Override
        public void onAlways(Promise.State state, MultipleResults resolved, OneReject rejected) {
            if (errors.hasErrors()) {
                errors.serialize(xmlResponse);
            } else {
                for (OneResult oneResult : resolved) {
                    xmlResponse.addContent((Content) oneResult.getResult());
                }
            }

            writeResponse(xmlResponse, context.getResponse());
            context.complete();
        }
    });
}

From source file:org.siphon.d2js.DispatchServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
    AsyncContext asyncContext = request.startAsync(request, response);
    D2jsRunner d2jsRunner = this.getD2jsRunner();
    String method = StringUtils.defaultIfEmpty(request.getParameter("_m"), "fetch");

    try {/* w  w w . jav a 2s. c o m*/
        d2jsRunner.run((HttpServletRequest) asyncContext.getRequest(),
                (HttpServletResponse) asyncContext.getResponse(), method);
    } finally {
        asyncContext.complete();
    }
}

From source file:org.siphon.d2js.DispatchServlet.java

@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
    AsyncContext asyncContext = request.startAsync(request, response);
    D2jsRunner d2jsRunner = this.getD2jsRunner();
    try {//from  www.j  av  a2s.  com
        d2jsRunner.run((HttpServletRequest) asyncContext.getRequest(),
                (HttpServletResponse) asyncContext.getResponse(), "delete");
    } finally {
        asyncContext.complete();
    }
}

From source file:org.siphon.d2js.DispatchServlet.java

@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
    AsyncContext asyncContext = request.startAsync(request, response);
    D2jsRunner d2jsRunner = this.getD2jsRunner();
    try {/*  ww w .ja va 2s  . c  o m*/
        d2jsRunner.run((HttpServletRequest) asyncContext.getRequest(),
                (HttpServletResponse) asyncContext.getResponse(), "modify");
    } finally {
        asyncContext.complete();
    }
}

From source file:org.siphon.d2js.DispatchServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
    AsyncContext asyncContext = request.startAsync(request, response);
    D2jsRunner d2jsRunner = this.getD2jsRunner();
    String method = StringUtils.defaultIfEmpty(request.getParameter("_m"), "create");
    try {// w w w.j  ava 2 s  .  c  om
        d2jsRunner.run((HttpServletRequest) asyncContext.getRequest(),
                (HttpServletResponse) asyncContext.getResponse(), method);
    } finally {
        asyncContext.complete();
    }
}