public void testCharacterEncodingSetBeforeRequestWrappingAndActionService() throws Exception { MockServletContext servletContext = new MockServletContext(); MockFilterConfig filterConfig = new MockFilterConfig(servletContext); MockHttpServletRequest req = new MockHttpServletRequest(servletContext); MockHttpServletResponse res = new MockHttpServletResponse(); MockFilterChain chain = new MockFilterChain(); final InnerDispatcher _dispatcher = new InnerDispatcher(servletContext); Dispatcher.setInstance(null); _dispatcher.setDefaultEncoding("UTF-16_DUMMY"); FilterDispatcher filter = new FilterDispatcher() { protected Dispatcher createDispatcher(FilterConfig filterConfig) { return _dispatcher; } }; filter.init(filterConfig); // set ActionMapper after init() as all dependencies will be injected in init() filter.setActionMapper(new InnerActionMapper()); _dispatcher.setDefaultEncoding("UTF-16_DUMMY"); filter.doFilter(req, res, chain); assertTrue(_dispatcher.wrappedRequest); assertTrue(_dispatcher.serviceRequest); }
public void testIfActionMapperIsNullDontServiceAction() throws Exception { MockServletContext servletContext = new MockServletContext(); MockFilterConfig filterConfig = new MockFilterConfig(servletContext); MockHttpServletRequest req = new MockHttpServletRequest(servletContext); MockHttpServletResponse res = new MockHttpServletResponse(); MockFilterChain chain = new MockFilterChain(); final NoOpDispatcher _dispatcher = new NoOpDispatcher(servletContext); ConfigurationManager confManager = new ConfigurationManager(); confManager.setConfiguration(new DefaultConfiguration()); _dispatcher.setConfigurationManager(confManager); Dispatcher.setInstance(_dispatcher); FilterDispatcher filter = new FilterDispatcher() { protected Dispatcher createDispatcher() { return _dispatcher; } }; filter.init(filterConfig); filter.setActionMapper(null); filter.doFilter(req, res, chain); assertFalse(_dispatcher.serviceRequest); }
/** * Send an HTTP error response code. * * @param request the HttpServletRequest object. * @param response the HttpServletResponse object. * @param code the HttpServletResponse error code (see {@link * javax.servlet.http.HttpServletResponse} for possible error codes). * @param e the Exception that is reported. * @param ctx the ServletContext object. */ public void sendError( HttpServletRequest request, HttpServletResponse response, ServletContext ctx, int code, Exception e) { Boolean devModeOverride = FilterDispatcher.getDevModeOverride(); if (devModeOverride != null ? devModeOverride : devMode) { if (LOG.isDebugEnabled()) { LOG.debug("Exception occurred during processing request: #0", e, e.getMessage()); } try { FreemarkerManager mgr = getContainer().getInstance(FreemarkerManager.class); freemarker.template.Configuration config = mgr.getConfiguration(ctx); Template template = config.getTemplate("/org/apache/struts2/dispatcher/error.ftl"); List<Throwable> chain = new ArrayList<Throwable>(); Throwable cur = e; chain.add(cur); while ((cur = cur.getCause()) != null) { chain.add(cur); } HashMap<String, Object> data = new HashMap<String, Object>(); data.put("exception", e); data.put("unknown", Location.UNKNOWN); data.put("chain", chain); data.put("locator", new Locator()); Writer writer = new StringWriter(); template.process(data, writer); response.setContentType("text/html"); response.getWriter().write(writer.toString()); response.getWriter().close(); } catch (Exception exp) { try { if (LOG.isDebugEnabled()) { LOG.debug("Cannot show problem report!", exp); } response.sendError( code, "Unable to show problem report:\n" + exp + "\n\n" + LocationUtils.getLocation(exp)); } catch (IOException ex) { // we're already sending an error, not much else we can do if more stuff breaks } } } else { try { if (LOG.isErrorEnabled()) { LOG.error("Exception occurred during processing request: #0", e, e.getMessage()); } // WW-1977: Only put errors in the request when code is a 500 error if (code == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) { // send a http error response to use the servlet defined error handler // make the exception availible to the web.xml defined error page request.setAttribute("javax.servlet.error.exception", e); // for compatibility request.setAttribute("javax.servlet.jsp.jspException", e); } // send the error response response.sendError(code, e.getMessage()); } catch (IOException e1) { // we're already sending an error, not much else we can do if more stuff breaks } } }