List of usage examples for javax.servlet ServletResponse getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:info.magnolia.module.servletsanity.support.ServletAssert.java
private static void appendResponseChain(HttpServletResponse response) throws IOException { append("Response Chain:"); ServletResponse rs = response; do {//from w ww . j a v a 2 s. co m append(" " + rs.getClass().getName() + " @ " + System.identityHashCode(rs) + " - " + rs.toString()); rs = rs instanceof HttpServletResponseWrapper ? ((HttpServletResponseWrapper) rs).getResponse() : null; } while (rs != null); append(""); }
From source file:edu.utah.further.i2b2.hook.further.FurtherInterceptionFilter.java
/** * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, * javax.servlet.ServletResponse, javax.servlet.FilterChain) *///from w w w . ja v a2 s .c om public void doFilter(final ServletRequest req, final ServletResponse resp, final FilterChain chain) throws IOException, ServletException { // Cast to friendler versions if (!HttpServletRequest.class.isAssignableFrom(req.getClass())) { throw new ServletException("ServletRequest must be an instance of HttpServletRequest"); } if (!HttpServletResponse.class.isAssignableFrom(resp.getClass())) { throw new ServletException("ServletResponse must be an instance of HttpServletResponse"); } final HttpServletRequest request = (HttpServletRequest) req; final HttpServletResponse response = (HttpServletResponse) resp; // Decorate the i2b2 processing chain. Decorate the HTTP request so that input // stream can be read twice - once by this filter's methods, and one by the // i2b2 processing chain call chain.doFilter(). final HttpServletRequest bufferedRequest = new BufferedRequestWrapper(request); final CapturedResponseWrapper capturedResponse = new CapturedResponseWrapper(response); beforeRequest(bufferedRequest); chain.doFilter(bufferedRequest, capturedResponse); afterRequest(bufferedRequest, response, capturedResponse); }
From source file:org.solmix.wmix.web.servlet.AbstractWmixFilter.java
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; String method = httpRequest.getMethod(); if ("HEAD".equalsIgnoreCase(method)) { httpResponse = new NoBodyResponse(httpResponse); }//from ww w.j a va 2 s . com try { doFilter(httpRequest, httpResponse, chain); } finally { if (httpResponse instanceof NoBodyResponse) { ((NoBodyResponse) httpResponse).setContentLength(); } } } else { LOG.debug("Skipped filtering due to the unknown request/response types: {}, {}", request.getClass().getName(), response.getClass().getName()); chain.doFilter(request, response); } }
From source file:org.xmlactions.web.conceal.HttpPager.java
public void processPage(ServletRequest request, ServletResponse response, String page) throws ServletException, IOException { IExecContext execContext = null;/*ww w .j a v a2 s.c o m*/ if (response instanceof HttpServletResponse && request instanceof HttpServletRequest) { try { HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpServletResponse httpServletResponse = (HttpServletResponse) response; execContext = setupExecContext(httpServletRequest, httpServletResponse); String pageName = httpServletRequest.getServletPath(); if (pageName.indexOf("axelconfig") > 0) { PrintWriter out = response.getWriter(); out.print(buildInfo(httpServletRequest, httpServletResponse)); out.close(); return; } if (!pageName.endsWith(".ajax")) { String alternatePage = processPrePages(execContext, httpServletRequest, httpServletResponse); if ("stop".equals(execContext.getString("pre.page.stop"))) { execContext.put("pre.page.stop", ""); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print(alternatePage); out.close(); } } log.debug("contextPath:" + httpServletRequest.getContextPath()); log.debug("URI:" + httpServletRequest.getRequestURI()); log.debug("root:" + realPath + " page:" + pageName + " wrapperPage:" + wrapperPage); log.debug(HttpSessionInfo.sysInfo(httpServletRequest)); Action action; if (pageName.endsWith(".ajax")) { page = processAjaxCall(httpServletRequest, httpServletResponse, pageName.substring(1, pageName.length() - ".ajax".length()), execContext); page = StrSubstitutor.replace(page, execContext); } else if (pageName.endsWith(".bin") || pageName.endsWith(".pdfbin")) { String pn = null; if (pageName.endsWith(".pdfbin")) { pn = pageName.substring(1, pageName.length() - ".pdfbin".length()); } else { pn = pageName.substring(1, pageName.length() - ".bin".length()); } page = processAjaxCall(httpServletRequest, httpServletResponse, pn, execContext); if (page.startsWith("EX:")) { PrintWriter out = response.getWriter(); out.print(page); out.close(); } else { byte[] image = (byte[]) execContext.get("image"); if (pageName.endsWith(".pdfbin")) { String outputPdfFileName = execContext.getString("outputFileName"); String ex = serviceJasperPdfRequest(httpServletResponse, image, outputPdfFileName); if (ex != null) { PrintWriter out = response.getWriter(); out.print(page); out.close(); } return; } else { String responseType = execContext.getString("response_type"); if (StringUtils.isEmpty(responseType)) { responseType = "image/png"; } response.setContentType(responseType); processPostPages(execContext, httpServletRequest, httpServletResponse); InputStream in = new ByteArrayInputStream(image); OutputStream out = response.getOutputStream(); // Copy the contents of the file to the output // stream byte[] buf = new byte[1024]; int count = 0; while ((count = in.read(buf)) >= 0) { out.write(buf, 0, count); } in.close(); out.close(); } return; } } else { if (pageName.startsWith("/:")) { String wrapperPageName = null; int secondColon = pageName.indexOf(':', 2); if (secondColon >= 0) { wrapperPageName = pageName.substring(2, secondColon); execContext.put("inner_page", "/" + pageName.substring(secondColon + 1)); } else { if (StringUtils.isEmpty(wrapperPage)) { throw new ServletException("No wrapper page set, why use :"); } wrapperPageName = wrapperPage; execContext.put("inner_page", "/" + pageName.substring(2)); } // insert the requested page into the wrapper page action = new Action(realPath, wrapperPageName, nameSpace); } else { if (StringUtils.isNotEmpty(wrapperPage)) { // we have a base wrapper page defined execContext.put("inner_page", "/" + pageName); action = new Action(realPath, wrapperPage, nameSpace); } else { if (pageName.indexOf("show_axel_config") >= 0) { page = AxelConfig.getAxelConfig(execContext); log.info(page); page = "Config Copied to Log"; action = null; } else { // if we don't have a wrapper page we show the requested page action = new Action(realPath, pageName, nameSpace); } } } if (action != null) { if (StringUtils.isNotEmpty(page)) { page = action.processPage(execContext, page); } else { page = action.processPage(execContext); } } } log.debug("URI:" + httpServletRequest.getRequestURI()); // log.debug("page:" + page); Object object = execContext.get(ActionConst.NO_STR_SUBST); // log.debug(ActionConst.NO_STR_SUBST + ":[" + object + "]"); execContext.put(ActionConst.NO_STR_SUBST, null); if (pageName.toLowerCase().endsWith("soap")) { response.setContentType(IExecContext.CONTENT_TYPE_XML); } else if (pageName.toLowerCase().endsWith("json")) { response.setContentType(IExecContext.CONTENT_TYPE_JSON); } else { response.setContentType(IExecContext.CONTENT_TYPE_HTML); } processPostPages(execContext, httpServletRequest, httpServletResponse); // = = = // check if there is a contentType value stored in the execContext. This could have been set by one of the actions such as an action that wants to return a JSON data. // = = = String contentType = execContext.getString(IExecContext.CONTENT_TYPE_KEY); if (StringUtils.isNotBlank(contentType)) { response.setContentType(contentType); // if (contentType.equals(IExecContext.CONTENT_TYPE_JSON) || contentType.equals(IExecContext.CONTENT_TYPE_XML)) { // if (response instanceof HttpServletResponse) { // ((HttpServletResponse)response).setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1 // ((HttpServletResponse)response).setHeader("Pragma", "no-cache"); // HTTP 1.0 // ((HttpServletResponse)response).setDateHeader("Expires", 0); // Proxies. // } // } } execContext.remove("content_type"); PrintWriter out = response.getWriter(); out.print(page); out.close(); } catch (Throwable t) { // TODO review this, use a better way for reporting the error. log.info(t.getMessage(), t); throw new ServletException(t); } finally { if (execContext != null) { execContext.saveToPersistence(); RequestExecContext.remove(); } } } else { String msg = "Not processing page. Must be HttpServletRequest not [" + request.getClass().getName() + "] and HttpServletResponse not [" + response.getClass().getName() + "]"; log.info(msg); throw new ServletException(msg); } }
From source file:org.apache.catalina.core.ApplicationDispatcher.java
private void doForward(ServletRequest request, ServletResponse response) throws ServletException, IOException { // Reset any output that has been buffered, but keep headers/cookies if (response.isCommitted()) { if (log.isDebugEnabled()) log.debug(" Forward on committed response --> ISE"); throw new IllegalStateException(sm.getString("applicationDispatcher.forward.ise")); }/*from w ww . j a va 2 s .co m*/ try { response.resetBuffer(); } catch (IllegalStateException e) { if (log.isDebugEnabled()) log.debug(" Forward resetBuffer() returned ISE: " + e); throw e; } // Set up to handle the specified request and response setup(request, response, false); // Identify the HTTP-specific request and response objects (if any) HttpServletRequest hrequest = null; if (request instanceof HttpServletRequest) hrequest = (HttpServletRequest) request; HttpServletResponse hresponse = null; if (response instanceof HttpServletResponse) hresponse = (HttpServletResponse) response; // Handle a non-HTTP forward by passing the existing request/response if ((hrequest == null) || (hresponse == null)) { if (log.isDebugEnabled()) log.debug(" Non-HTTP Forward"); processRequest(hrequest, hresponse); } // Handle an HTTP named dispatcher forward else if ((servletPath == null) && (pathInfo == null)) { if (log.isDebugEnabled()) log.debug(" Named Dispatcher Forward"); processRequest(request, response); } // Handle an HTTP path-based forward else { if (log.isDebugEnabled()) log.debug(" Path Based Forward"); ApplicationHttpRequest wrequest = (ApplicationHttpRequest) wrapRequest(); String contextPath = context.getPath(); wrequest.setContextPath(contextPath); wrequest.setRequestURI(requestURI); wrequest.setServletPath(servletPath); wrequest.setPathInfo(pathInfo); wrequest.setAttribute(Globals.FORWARD_REQUEST_URI_ATTR, hrequest.getRequestURI()); wrequest.setAttribute(Globals.FORWARD_CONTEXT_PATH_ATTR, hrequest.getContextPath()); wrequest.setAttribute(Globals.FORWARD_SERVLET_PATH_ATTR, hrequest.getServletPath()); wrequest.setAttribute(Globals.FORWARD_PATH_INFO_ATTR, hrequest.getPathInfo()); wrequest.setAttribute(Globals.FORWARD_QUERY_STRING_ATTR, hrequest.getQueryString()); if (queryString != null) { wrequest.setQueryString(queryString); wrequest.setQueryParams(queryString); } processRequest(request, response); unwrapRequest(); } // This is not a real close in order to support error processing if (log.isDebugEnabled()) log.debug(" Disabling the response for futher output"); if (response instanceof ResponseFacade) { ((ResponseFacade) response).finish(); } else { // Servlet SRV.6.2.2. The Resquest/Response may have been wrapped // and may no longer be instance of RequestFacade if (log.isDebugEnabled()) { log.debug(" The Response is vehiculed using a wrapper: " + response.getClass().getName()); } // Close anyway try { PrintWriter writer = response.getWriter(); writer.close(); } catch (IllegalStateException e) { try { ServletOutputStream stream = response.getOutputStream(); stream.close(); } catch (IllegalStateException f) { ; } catch (IOException f) { ; } } catch (IOException e) { ; } } }
From source file:org.mifos.servlet.filters.CharacterEncodingFilter.java
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(CharEncoding.UTF_8); response.setCharacterEncoding(CharEncoding.UTF_8); if (!CharEncoding.UTF_8.equals(response.getCharacterEncoding())) { // MIFOS-5435 - the character encoding was not set because the connection is in including state try {/*from w ww. j a v a2s .co m*/ Method getResponse = response.getClass().getMethod("getResponse"); ServletResponse servletResponse = (ServletResponse) getResponse.invoke(response); getResponse = servletResponse.getClass().getMethod("getResponse"); ServletResponse jettyResponse = (ServletResponse) getResponse.invoke(servletResponse); Field _characterEncoding = jettyResponse.getClass().getDeclaredField("_characterEncoding"); _characterEncoding.setAccessible(true); _characterEncoding.set(jettyResponse, CharEncoding.UTF_8); } catch (NoSuchFieldException e) { logger.debug(e); } catch (IllegalAccessException e) { logger.debug(e); } catch (NoSuchMethodException e) { logger.debug(e); } catch (InvocationTargetException e) { logger.debug(e); } } chain.doFilter(request, response); }
From source file:org.unitedinternet.cosmo.dav.acegisecurity.DavAccessDeniedHandler.java
public void handle(ServletRequest request, ServletResponse response, AccessDeniedException exception) throws IOException, ServletException { if (!(response instanceof HttpServletResponse)) { throw new IllegalStateException("Expected response of type: [" + HttpServletResponse.class.getName() + "], received :[" + response.getClass().getName() + "]"); }/*from w w w . j a va2 s .com*/ StandardDavResponse sdr = new StandardDavResponse((HttpServletResponse) response); NeedsPrivilegesException toSend = null; if (exception instanceof DavAccessDeniedException) { DavAccessDeniedException e = (DavAccessDeniedException) exception; toSend = new NeedsPrivilegesException(e.getHref(), e.getPrivilege()); } else { toSend = new NeedsPrivilegesException(exception.getMessage()); } sdr.sendDavError(toSend); }