List of usage examples for javax.servlet AsyncContext getRequest
public ServletRequest getRequest();
From source file:org.forumj.web.requesthandler.handler.ForumThreadHandler.java
@Override protected void doHandle(AsyncContext context) throws FJWebException { try {/*from w w w .j a va 2 s. c om*/ HttpServletRequest request = (HttpServletRequest) context.getRequest(); HttpServletResponse response = (HttpServletResponse) context.getResponse(); long startTime = new Date().getTime(); StringBuffer buffer = new StringBuffer(); HttpSession session = request.getSession(); cache(response); // ? ?? ? , Integer pageNumber = request.getParameter("page") == null ? 1 : Integer.valueOf(request.getParameter("page")); // id Long threadId = request.getParameter("id") == null ? 1 : Long.valueOf(request.getParameter("id")); // ? ?, String replyPostParameter = request.getParameter("reply"); String end = request.getParameter("end"); // ? ?? String msg = request.getParameter("msg"); VoiceService voiceService = FJServiceHolder.getVoiceService(); PostService postService = FJServiceHolder.getPostService(); SubscribeService subscribeService = FJServiceHolder.getSubscribeService(); ThreadService treadService = FJServiceHolder.getThreadService(); IFJThread thread = treadService.readThread(threadId); boolean isAnswer = replyPostParameter != null && !"".equals(replyPostParameter.trim()); LocaleString locale = (LocaleString) session.getAttribute("locale"); IUser user = (IUser) session.getAttribute("user"); IgnorService ignorService = FJServiceHolder.getIgnorService(); List<IIgnor> ignorList = ignorService.readUserIgnor(user.getId()); // ?? Integer count = thread.getPcount(); Integer couP = (int) (Math.floor((double) count / user.getPt()) + 2); // ? ? ?, ? boolean lastPost = false; if (isAnswer || end != null) { pageNumber = couP - 1; lastPost = true; } int nfirstpost = (pageNumber - 1) * user.getPt(); List<IFJPost> posts = postService.readPosts(user, threadId, nfirstpost, user.getPt(), pageNumber, lastPost); int postsAmount = posts.size(); // ?? ? session.setAttribute("page", pageNumber); session.setAttribute("id", threadId); session.setAttribute("where", request.getContextPath() + "?id=" + threadId + "&page=" + pageNumber); int countPosts = 0; if (msg != null && !"".equals(msg.trim())) { try { Long msgId = new Long(msg); countPosts = postService.getPostsCountInThread(threadId, msgId); } catch (NumberFormatException e) { e.printStackTrace(); msg = null; } pageNumber = (int) (Math.floor(countPosts / user.getPt()) + 1); } // ? ? // ? if (!isRobot(request)) { // ? treadService.setSeen(user, threadId); } // ? ? colspan! buffer.append("<div>"); buffer.append("<table width=100%>"); buffer.append("<tr>"); buffer.append("<td>"); buffer.append("<table>"); buffer.append("<tr>"); buffer.append("<td class='page'>"); buffer.append("<font class=mnuforum><b>" + locale.getString("mess22") + " </b></font>"); buffer.append("</td>"); int i2 = 0; for (int i1 = 1; i1 < couP; i1++) { i2 = i2 + 1; if ((i1 > (pageNumber - 5) && i1 < (pageNumber + 5)) || i2 == 10 || i1 == 1 || i1 == (couP - 1)) { if (i2 == 10) i2 = 0; if (i1 == pageNumber) { buffer.append("<td class='pagecurrent'>"); buffer.append("<span class=mnuforum><b>" + i1 + "</b></span>"); buffer.append("</td>"); } else { buffer.append("<td class='page'>"); buffer.append("<a class=mnuforum href='" + FJUrl.VIEW_THREAD + "?page=" + i1 + "&id=" + threadId + "'>" + i1 + "</a>"); buffer.append("</td>"); } } } buffer.append("</tr>"); buffer.append("</table>"); buffer.append("</td>"); buffer.append("<td align=right>"); // ? :) buffer.append("<span class=posthead>" + locale.getString("mess91") + "</span>"); buffer.append("</td>"); buffer.append("</tr></table>"); buffer.append("</td>"); buffer.append("</tr></table></td></tr>"); // ?? ? // ? buffer.append("<tr><td height='400' valign='top'>"); // buffer.append("<table border='0' cellpadding='2' cellspacing='0' width='100%'>"); // ? - ? if (postsAmount > count) { postsAmount = count - (pageNumber - 1) * user.getPt(); } else { postsAmount = user.getPt(); } // // ? for (int postIndex = 0; postIndex < posts.size(); postIndex++) { IFJPost post = posts.get(postIndex); buffer.append(writePost(post, ignorList, user, pageNumber, locale, thread, voiceService)); } // / buffer.append("</table>"); // "" buffer.append("</td>"); buffer.append("</tr>"); // ?? // ? ? buffer.append("<tr>"); buffer.append("<td width='100%'>"); buffer.append("<table border='0' style='border-collapse: collapse' width='100%'>"); buffer.append("<tr>"); buffer.append("<td colspan='5'>"); buffer.append("<table>"); buffer.append("<tr>"); buffer.append("<td class='page'>"); buffer.append("<font class=mnuforum><b>" + locale.getString("mess22") + " </b></font>"); buffer.append("</td>"); i2 = 0; for (int i1 = 1; i1 < couP; i1++) { i2 = i2 + 1; if ((i1 > (pageNumber - 5) && i1 < (pageNumber + 5)) || i2 == 10 || i1 == 1 || i1 == (couP - 1)) { if (i2 == 10) i2 = 0; if (i1 == pageNumber) { buffer.append("<td class='pagecurrent'>"); buffer.append("<span class=mnuforum><b>" + i1 + "</b></span>"); buffer.append("</td>"); } else { buffer.append("<td class='page'>"); buffer.append("<a class=mnuforum href='" + FJUrl.VIEW_THREAD + "?page=" + i1 + "&id=" + threadId + "'>" + i1 + "</a>"); buffer.append("</td>"); } } } buffer.append("</tr>"); buffer.append("</table>"); buffer.append("</td>"); buffer.append("</tr>"); // "" buffer.append("</table></td></tr>"); if (user.isLogined() && !user.isBanned() && !thread.isClosed()) { // ?/? // ?? String action = ""; String mess = ""; if (subscribeService.isUserSubscribed(threadId, user.getId())) { //? ?, ?? action = FJUrl.DELETE_SUBSCRIBE + "?pg=" + pageNumber; mess = locale.getString("mess90"); } else { //? - ??? action = FJUrl.ADD_SUBSCRIBE + "?pg=" + pageNumber; mess = locale.getString("mess89"); } buffer.append("<tr>"); buffer.append("<td align=right>"); buffer.append("<form id='subs' action='" + action + "' method='post'>"); buffer.append("<table>"); buffer.append("<tr>"); buffer.append("<td>"); buffer.append(fd_button(mess, "subscribe();", "btn_subs", "1")); // ... buffer.append("<input type=hidden name='IDT' value='" + threadId + "'>"); buffer.append(fd_form_add(user)); buffer.append("</td>"); buffer.append("</tr>"); buffer.append("</table>"); buffer.append("</form>"); buffer.append("</td>"); buffer.append("</tr>"); String re = ""; String head = thread.getHead(); IFJPost replyPost = null; // ? / if (isAnswer) { replyPost = postService.read(Long.valueOf(replyPostParameter)); head = removeSlashes(replyPost.getHead().getTitle()); } // ? // ? buffer.append("<tr>"); buffer.append("<td>"); buffer.append("<a name='edit'> "); buffer.append("</a>"); buffer.append("<table>"); buffer.append("<tr>"); buffer.append("<td>"); buffer.append("<form name='post' action='" + FJUrl.ADD_POST + "' method='post'>"); buffer.append("<table width='100%'>"); // buffer.append("<tr>"); buffer.append("<td colspan='2' align='CENTER'>"); buffer.append("<table>"); buffer.append("<tr>"); buffer.append("<td>"); buffer.append(locale.getString("mess59") + ": "); buffer.append("</td>"); buffer.append("<td>"); buffer.append(fd_input("NHEAD", re + HtmlChars.convertHtmlSymbols(head), "70", "1")); buffer.append("</td>"); buffer.append("</tr>"); buffer.append("</table>"); buffer.append("</td>"); buffer.append("</tr>"); buffer.append("<tr>"); // buffer.append("<td width='400' align='CENTER'>"); buffer.append("<p>"); buffer.append(locale.getString("mess21") + ":"); buffer.append("</p>"); buffer.append("</td>"); // buffer.append("<td align='CENTER'>"); buffer.append("<p>"); buffer.append(locale.getString("mess12")); buffer.append("</p>"); buffer.append("</td>"); buffer.append("</tr>"); //? buffer.append("<tr>"); buffer.append("<td valign='TOP' width='100%' height='100%'>"); // buffer.append(smiles_add(locale.getString("mess11"))); buffer.append("</td>"); buffer.append("<td width='500' align='CENTER' valign='top'>"); //? buffer.append(autotags_add()); // ? String textarea = ""; if (isAnswer) { String ans = request.getParameter("ans"); if (replyPost.getHead().getAuth().equals(user.getId())) { textarea += HtmlChars.convertHtmlSymbols(removeSlashes(replyPost.getBody().getBody())); } else if (ans == null) { textarea += "[quote][b]" + HtmlChars.convertHtmlSymbols( removeSlashes(replyPost.getHead().getAuthor().getNick())) + "[/b] "; textarea += locale.getString("mess14") + String.valueOf((char) 13); textarea += HtmlChars.convertHtmlSymbols(removeSlashes(replyPost.getBody().getBody())) + "[/quote]"; } else { textarea += "[b]" + removeSlashes(replyPost.getHead().getAuthor().getNick()) + "[/b]"; textarea += ", "; } } buffer.append("<textarea rows='20' class='mnuforumSm' id='ed1' name='A2' cols='55'>" + textarea + "</textarea>"); buffer.append("<br>"); buffer.append("<input type='checkbox' name='no_exit' value='1'>"); buffer.append(locale.getString("mess123")); // buffer.append("<table>"); buffer.append("<tr>"); buffer.append("<td>"); if (isAnswer && (replyPost.getHead().getAuth().equals(user.getId()))) { buffer.append(fd_button(locale.getString("mess13"), "post_submit(\"write_edit\");", "B1", "1")); } else { buffer.append(fd_button(locale.getString("mess13"), "post_submit(\"write_new\");", "B1", "1")); } buffer.append("</td>"); buffer.append("<td>"); if (isAnswer && (replyPost.getHead().getAuth().equals(user.getId()))) { buffer.append(fd_button(locale.getString("mess63"), "post_submit(\"view_edit\");", "B1", "1")); } else { buffer.append(fd_button(locale.getString("mess63"), "post_submit(\"view_new\");", "B3", "1")); } buffer.append("</td>"); buffer.append("</tr>"); buffer.append("</table>"); //? if (isAnswer && (replyPost.getHead().getAuth().equals(user.getId()))) { buffer.append("<input type=hidden name='IDB' size='20' value='" + replyPostParameter + "'>"); buffer.append( "<input type=hidden name='IDTbl' size='20' value='" + replyPost.getTablePost() + "'>"); buffer.append("<input type=hidden name='IDPst' size='20' value='" + replyPost.getId().toString() + "'>"); buffer.append("<input type=hidden name='IDTblHead' size='20' value='" + replyPost.getTableHead() + "'>"); buffer.append("<input type=hidden name='IDHead' size='20' value='" + replyPost.getId().toString() + "'>"); } //id buffer.append("<input type=hidden name='IDT' size='20' value='" + threadId + "'>"); if (thread.isQuest()) { buffer.append("<input type=hidden name='ISQUEST' size='20' value='true'>"); } buffer.append(fd_form_add(user)); buffer.append("</td>"); buffer.append("</tr>"); buffer.append("</table>"); buffer.append("</form>"); buffer.append("</td>"); buffer.append("</tr>"); buffer.append("</table>"); buffer.append("</td>"); buffer.append("</tr>"); } Double allTime = (double) ((new Date().getTime() - startTime)); DecimalFormat format = new DecimalFormat("##0.###"); response.setContentType("text/html; charset=UTF-8"); PrintWriter writer = response.getWriter(); String out = buffer.toString(); writer.write(out.replace("_", format.format(allTime / 1000))); } catch (Throwable e) { throw new FJWebException(e); } }
From source file:com.kurento.kmf.content.internal.base.AbstractContentHandlerServlet.java
/** * Generic processor of HTTP request when using JSON control protocol. * /*from w ww. j av a 2 s . c o 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 doRequest4JsonControlProtocol(AsyncContext asyncCtx, String contentId, HttpServletResponse resp) throws ServletException, IOException { JsonRpcRequest message = null; try { message = protocolManager.receiveJsonRequest(asyncCtx); if (message == null) { throw new KurentoMediaFrameworkException("Null json message received", 10020); } AbstractContentSession contentSession = null; String sessionId = message.getParams() != null ? message.getParams().getSessionId() : null; if (sessionId == null && message.getMethod().equals(METHOD_START)) { // Session is created by a start request, we need to fill // asyncCtx associated to start requests. contentSession = createContentSession(asyncCtx, contentId); contentSessionManager.put(contentSession); } else if (sessionId == null && message.getMethod().equals(METHOD_EXECUTE)) { // Session is created by an execute request, the asyncCtx for // start requests must be set to null contentSession = createContentSession(null, contentId); contentSessionManager.put(contentSession); } else if (sessionId != null) { contentSession = contentSessionManager.get(sessionId); if (contentSession == null) { throw new KurentoMediaFrameworkException( "Cloud not find contentRequest object associated to sessionId " + sessionId, 10021); } } else { throw new KurentoMediaFrameworkException("Cloud not find required sessionId field in request", 10022); } Future<?> future = executor.getExecutor() .submit(createAsyncRequestProcessor(contentSession, message, asyncCtx)); // Store future for using it in ContentAsyncListener in case of // error asyncCtx.getRequest().setAttribute(ContentAsyncListener.FUTURE_REQUEST_PROCESSOR_ATT_NAME, future); asyncCtx.getRequest().setAttribute(ContentAsyncListener.CONTENT_REQUEST_ATT_NAME, contentSession); asyncCtx.getRequest().setAttribute(ContentAsyncListener.CONTROL_PROTOCOL_REQUEST_MESSAGE_ATT_NAME, message); } catch (KurentoMediaFrameworkException ke) { int reqId = message != null ? message.getId() : 0; protocolManager.sendJsonError(asyncCtx, JsonRpcResponse .newError(ExceptionUtils.getJsonErrorCode(ke.getCode()), ke.getMessage(), reqId)); } catch (Throwable t) { int reqId = message != null ? message.getId() : 0; protocolManager.sendJsonError(asyncCtx, JsonRpcResponse.newError(ExceptionUtils.getJsonErrorCode(1), t.getMessage(), reqId)); } }