List of usage examples for java.lang ClassCastException printStackTrace
public void printStackTrace(PrintStream s)
From source file:net.kungfoo.grizzly.proxy.impl.ConnectingHandler.java
public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) { System.out.println(conn + " [proxy<-origin] input ready"); HttpContext context = conn.getContext(); ProxyProcessingInfo proxyTask = (ProxyProcessingInfo) context.getAttribute(ProxyProcessingInfo.ATTRIB); synchronized (proxyTask) { ConnState connState = proxyTask.getOriginState(); if (connState != ConnState.RESPONSE_RECEIVED && connState != ConnState.RESPONSE_BODY_STREAM) { throw new IllegalStateException("Illegal target connection state: " + connState); }/*from ww w . j a v a2 s .co m*/ final Response response = proxyTask.getResponse(); try { ByteBuffer dst = proxyTask.getOutBuffer(); int bytesRead = decoder.read(dst); if (bytesRead > 0) { dst.flip(); final ByteChunk chunk = new ByteChunk(bytesRead); final byte[] buf = new byte[bytesRead]; dst.get(buf); chunk.setBytes(buf, 0, bytesRead); dst.compact(); try { response.doWrite(chunk); } catch (ClassCastException e) { System.err.println("gone bad: " + e.getMessage()); e.printStackTrace(System.err); } response.flush(); System.out.println(conn + " [proxy<-origin] " + bytesRead + " bytes read"); System.out.println(conn + " [proxy<-origin] " + decoder); } if (!dst.hasRemaining()) { // Output buffer is full. Suspend origin input until // the client handler frees up some space in the buffer conn.suspendInput(); } /* // If there is some content in the buffer make sure client output // is active if (dst.position() > 0) { proxyTask.getClientIOControl().requestOutput(); } */ if (decoder.isCompleted()) { System.out.println(conn + " [proxy<-origin] response body received"); proxyTask.setOriginState(ConnState.RESPONSE_BODY_DONE); if (!this.connStrategy.keepAlive(conn.getHttpResponse(), context)) { System.out.println(conn + " [proxy<-origin] close connection"); proxyTask.setOriginState(ConnState.CLOSING); conn.close(); } proxyTask.getCompletion().run(); } else { proxyTask.setOriginState(ConnState.RESPONSE_BODY_STREAM); } } catch (IOException ex) { shutdownConnection(conn); } } }
From source file:org.openmrs.module.uiframework.PageController.java
/** * @param path should be of the form "provider/optional/subdirectories/pageName" * @param request// w w w .j av a 2s .c o m * @param response * @param model * @param httpSession * @return */ public String handlePath(String path, HttpServletRequest request, HttpServletResponse response, Model model, HttpSession httpSession) { // handle the case where the url has two slashes, e.g. host/openmrs//emr/patient.page if (path.startsWith("/")) { path = path.substring(1); } int index = path.indexOf("/"); if (index < 0) { throw new IllegalArgumentException( "page request must have at least provider/pageName, but this does not: " + request.getRequestURI()); } String providerName = path.substring(0, index); String pageName = path.substring(index + 1); Session session; try { session = sessionFactory.getSession(httpSession); } catch (ClassCastException ex) { // this means that the UI Framework module was reloaded sessionFactory.destroySession(httpSession); session = sessionFactory.getSession(httpSession); } PageRequest pageRequest = new PageRequest(providerName, pageName, request, response, session); try { String html = pageFactory.handle(pageRequest); model.addAttribute("html", html); return SHOW_HTML_VIEW; } catch (Redirect redirect) { String ret = ""; if (!redirect.getUrl().startsWith("/")) ret += "/"; ret += redirect.getUrl(); if (ret.startsWith("/" + WebConstants.CONTEXT_PATH)) { ret = ret.substring(WebConstants.CONTEXT_PATH.length() + 1); } return "redirect:" + ret; } catch (FileDownload download) { response.setContentType(download.getContentType()); response.setHeader("Content-Disposition", "attachment; filename=" + download.getFilename()); try { IOUtils.copy(new ByteArrayInputStream(download.getFileContent()), response.getOutputStream()); response.flushBuffer(); } catch (IOException ex) { throw new UiFrameworkException("Error trying to write file content to response", ex); } return null; } catch (PageAction action) { throw new RuntimeException("Not Yet Implemented: " + action.getClass(), action); } catch (RuntimeException ex) { // special-case if this is due to the user not being logged in APIAuthenticationException authEx = ExceptionUtil.findExceptionInChain(ex, APIAuthenticationException.class); if (authEx != null) { throw authEx; } // The following should go in an @ExceptionHandler. I tried this, and it isn't getting invoked for some reason. // And it's not worth debugging that. StringWriter sw = new StringWriter(); ex.printStackTrace(new PrintWriter(sw)); model.addAttribute("fullStacktrace", sw.toString()); Throwable t = ex; while (t.getCause() != null && !t.equals(t.getCause())) t = t.getCause(); sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw)); model.addAttribute("rootStacktrace", sw.toString()); return "/module/uiframework/uiError"; } }
From source file:org.openmrs.module.xforms.page.controller.PatientRegUrlHandler.java
/** * @param path should be of the form "provider/optional/subdirectories/pageName" * @param request/*w ww .j a v a 2 s .c o m*/ * @param response * @param model * @param httpSession * @return */ public String handlePath(String path, HttpServletRequest request, HttpServletResponse response, Model model, HttpSession httpSession) { // handle the case where the url has two slashes, e.g. host/openmrs//emr/patient.page if (path.startsWith("/")) { path = path.substring(1); } int index = path.indexOf("/"); if (index < 0) { throw new IllegalArgumentException( "page request must have at least provider/pageName, but this does not: " + request.getRequestURI()); } String providerName = path.substring(0, index); String pageName = path.substring(index + 1); Session session; try { session = sessionFactory.getSession(httpSession); } catch (ClassCastException ex) { // this means that the UI Framework module was reloaded sessionFactory.destroySession(httpSession); session = sessionFactory.getSession(httpSession); } PageRequest pageRequest = new PageRequest(providerName, pageName, request, response, session); try { String html = pageFactory.handle(pageRequest); model.addAttribute("html", html); return SHOW_HTML_VIEW; } catch (Redirect redirect) { String ret = ""; if (!redirect.getUrl().startsWith("/")) ret += "/"; ret += redirect.getUrl(); if (ret.startsWith("/" + WebConstants.CONTEXT_PATH + "/")) { ret = ret.substring(WebConstants.CONTEXT_PATH.length() + 1); } return "redirect:" + ret; } catch (FileDownload download) { response.setContentType(download.getContentType()); response.setHeader("Content-Disposition", "attachment; filename=" + download.getFilename()); try { IOUtils.copy(new ByteArrayInputStream(download.getFileContent()), response.getOutputStream()); response.flushBuffer(); } catch (IOException ex) { throw new UiFrameworkException("Error trying to write file content to response", ex); } return null; } catch (PageAction action) { throw new RuntimeException("Not Yet Implemented: " + action.getClass(), action); } catch (RuntimeException ex) { // special-case if this is due to the user not being logged in APIAuthenticationException authEx = ExceptionUtil.findExceptionInChain(ex, APIAuthenticationException.class); if (authEx != null) { throw authEx; } // The following should go in an @ExceptionHandler. I tried this, and it isn't getting invoked for some reason. // And it's not worth debugging that. StringWriter sw = new StringWriter(); ex.printStackTrace(new PrintWriter(sw)); model.addAttribute("fullStacktrace", sw.toString()); Throwable t = ex; while (t.getCause() != null && !t.equals(t.getCause())) t = t.getCause(); sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw)); model.addAttribute("rootStacktrace", sw.toString()); return "/module/uiframework/uiError"; } }