List of usage examples for javax.servlet AsyncContext getRequest
public ServletRequest getRequest();
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 {/* w w w .j a va2s .c om*/ d2jsRunner.run((HttpServletRequest) asyncContext.getRequest(), (HttpServletResponse) asyncContext.getResponse(), "modify"); } finally { asyncContext.complete(); } }
From source file:com.kurento.kmf.content.internal.ControlProtocolManager.java
/** * Receiver method for JSON throw a request. * /*from w w w. java2 s . c om*/ * @param asyncCtx * Asynchronous context * @return Received JSON encapsulated as a Java class */ public JsonRpcRequest receiveJsonRequest(AsyncContext asyncCtx) { HttpServletRequest request = (HttpServletRequest) asyncCtx.getRequest(); // Received character encoding should be UTF-8. In order to check this, // the method detectJsonEncoding will be used. Before that, the // InputStream read from request.getInputStream() should be cloned // (using a ByteArrayOutputStream) to be used on detectJsonEncoding and // then for reading the JSON message try { InputStream inputStream = request.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[BUFF]; int len; while ((len = inputStream.read(buffer)) > -1) { baos.write(buffer, 0, len); } baos.flush(); String encoding = detectJsonEncoding(new ByteArrayInputStream(baos.toByteArray())); log.debug("Detected JSON encoding: " + encoding); if (encoding == null) { throw new KurentoMediaFrameworkException( "Invalid or unsupported charset encondig in received JSON request", 10018); } InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()), encoding); JsonRpcRequest jsonRequest = gson.fromJson(isr, JsonRpcRequest.class); Assert.notNull(jsonRequest.getMethod()); log.info("Received JsonRpc request ...\n " + jsonRequest.toString()); return jsonRequest; } catch (IOException e) { // TODO: trace this exception and double check appropriate JsonRpc // answer is sent throw new KurentoMediaFrameworkException( "IOException reading JsonRPC request. Cause: " + e.getMessage(), e, 20016); } }
From source file:com.boylesoftware.web.AsynchronousExecutor.java
@Override public void onTimeout(final AsyncEvent event) { final boolean debug = this.log.isDebugEnabled(); if (debug)// w w w . j av a2 s. c om this.log.debug("async event: timeout"); this.timedOut = true; this.cleanup(); if (debug) this.log.debug("recycling router request " + this.routerReq); RouterRequestLifecycle.recycle(this.routerReq); this.routerReq = null; this.asyncContext = null; this.webapp = null; if (this.executorThread != null) this.executorThread.interrupt(); final AsyncContext asyncCtx = event.getAsyncContext(); Router.setAsyncException(asyncCtx.getRequest(), new ServiceUnavailableException()); if (debug) this.log.debug("dispatching service unavailable exception back to" + " the router"); asyncCtx.dispatch(); }
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 {/*from www . j ava2s.c om*/ d2jsRunner.run((HttpServletRequest) asyncContext.getRequest(), (HttpServletResponse) asyncContext.getResponse(), method); } finally { asyncContext.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 {//from w ww . jav a2 s. c o m d2jsRunner.run((HttpServletRequest) asyncContext.getRequest(), (HttpServletResponse) asyncContext.getResponse(), method); } finally { asyncContext.complete(); } }
From source file:com.kurento.kmf.content.internal.ControlProtocolManager.java
/** * Internal implementation for sending JSON (called from sendJsonAnswer and * sendJsonError methods).//from w w w. jav a 2 s. c o m * * @param asyncCtx * Asynchronous context * @param message * JSON message (as a Java class) * @throws IOException * Exception while parsing operating with asynchronous context */ private void internalSendJsonAnswer(AsyncContext asyncCtx, JsonRpcResponse message) { try { if (asyncCtx == null) { throw new KurentoMediaFrameworkException("Null asyncCtx found", 20017); } synchronized (asyncCtx) { if (!asyncCtx.getRequest().isAsyncStarted()) { throw new KurentoMediaFrameworkException("Cannot send message in completed asyncCtx", 1); // TODO } HttpServletResponse response = (HttpServletResponse) asyncCtx.getResponse(); response.setContentType("application/json"); OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream(), UTF8); osw.write(gson.toJson(message)); osw.flush(); log.info("Sent JsonRpc answer ...\n" + message); asyncCtx.complete(); } } catch (IOException ioe) { throw new KurentoMediaFrameworkException(ioe.getMessage(), ioe, 20018); } }
From source file:org.apache.vysper.xmpp.extension.xep0124.BoshBackedSessionContextTest.java
@Test public void testRequestExpired() throws IOException { Stanza emtpyStanza = BoshStanzaUtils.EMPTY_BOSH_RESPONSE; // addRequest HttpServletRequest httpServletRequest = mocksControl.createMock(HttpServletRequest.class); AsyncContext asyncContext = mocksControl.createMock(AsyncContext.class); expect(httpServletRequest.startAsync()).andReturn(asyncContext).atLeastOnce(); expect(httpServletRequest.getAsyncContext()).andReturn(asyncContext).atLeastOnce(); asyncContext.setTimeout(anyLong());/*from www .j a va 2 s. com*/ httpServletRequest.setAttribute(eq(BOSH_REQUEST_ATTRIBUTE), EasyMock.<BoshRequest>notNull()); expect(asyncContext.getRequest()).andReturn(httpServletRequest).atLeastOnce(); asyncContext.dispatch(); expectLastCall().atLeastOnce(); Capture<AsyncListener> listenerCaptured = new Capture<AsyncListener>(); asyncContext.addListener(EasyMock.<AsyncListener>capture(listenerCaptured)); AsyncEvent asyncEvent = mocksControl.createMock(AsyncEvent.class); BoshRequest br = new BoshRequest(httpServletRequest, emtpyStanza, 1L); // requestExpired expect(httpServletRequest.getAttribute(BOSH_REQUEST_ATTRIBUTE)).andReturn(br); Capture<BoshResponse> responseCaptured = new Capture<BoshResponse>(); httpServletRequest.setAttribute(eq(BOSH_RESPONSE_ATTRIBUTE), EasyMock.<BoshResponse>capture(responseCaptured)); // write0 mocksControl.replay(); BoshBackedSessionContext boshBackedSessionContext = new BoshBackedSessionContext(serverRuntimeContext, null, inactivityChecker); boshBackedSessionContext.insertRequest(br); listenerCaptured.getValue().onTimeout(asyncEvent); mocksControl.verify(); assertEquals(new Renderer(emtpyStanza).getComplete(), new String(responseCaptured.getValue().getContent())); assertEquals(BoshServlet.XML_CONTENT_TYPE, responseCaptured.getValue().getContentType()); }
From source file:com.kurento.kmf.content.internal.base.AbstractContentHandlerServlet.java
/** * Generic processor of HTTP request when not using JSON control procotol. * //from www . j a v a 2s . co m * @param asyncCtx * Asynchronous context * @param contentId * Content unique identifier * @param resp * HTTP response * @throws ServletException * Exception in Servlet * @throws IOException * Input/Ouput Exception */ private void doRequest4SimpleHttpProtocol(AsyncContext asyncCtx, String contentId, HttpServletResponse resp) throws ServletException, IOException { try { AbstractContentSession contentRequest = createContentSession(asyncCtx, contentId); Future<?> future = executor.getExecutor() .submit(createAsyncRequestProcessor(contentRequest, null, asyncCtx)); // Store future and request for using it in case of error asyncCtx.getRequest().setAttribute(ContentAsyncListener.FUTURE_REQUEST_PROCESSOR_ATT_NAME, future); asyncCtx.getRequest().setAttribute(ContentAsyncListener.CONTENT_REQUEST_ATT_NAME, contentRequest); } catch (KurentoMediaFrameworkException ke) { getLogger().error(ke.getMessage(), ke); ServletUtils.sendHttpError((HttpServletRequest) asyncCtx.getRequest(), resp, ExceptionUtils.getHttpErrorCode(ke.getCode()), ke.getMessage()); } }
From source file:org.apache.vysper.xmpp.extension.xep0124.BoshBackedSessionContext.java
private void requestExpired(final AsyncContext context) { final BoshRequest req = (BoshRequest) context.getRequest().getAttribute(BOSH_REQUEST_ATTRIBUTE); if (req == null) { LOGGER.warn("SID = " + getSessionId() + " - Continuation expired without having " + "an associated request!"); return;/*from w w w .ja v a 2 s . c o m*/ } LOGGER.debug("SID = " + getSessionId() + " - rid = {} - BOSH request expired", req.getRid()); while (!requestsWindow.isEmpty() && requestsWindow.firstRid() <= req.getRid()) { writeBoshResponse(BoshStanzaUtils.EMPTY_BOSH_RESPONSE); } }