public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; String contextPath = req.getContextPath(); if (contextPath.equals("/")) { contextPath = ""; } String path = RequestUtil.getPath(req); if (!processDirectAccess(request, response, chain, path)) { return; } reloadRoutes(); if (path.indexOf('.') < 0) { // If the request pass via reverse proxy, the original path must be gotten from HTTP header. if (!contextSensitive) { path = getOriginalPath(req); } try { Options options = Routes.recognizePath(path); String controller = options.getString("controller"); String action = options.getString("action"); Options params = options.except("controller", "action"); String actionPath = ControllerUtil.fromClassNameToPath(controller); S2Container container = SingletonS2ContainerFactory.getContainer(); if (container.hasComponentDef(actionPath.replace('/', '_').concat("Action"))) { S2ExecuteConfig executeConfig; if (StringUtil.equals(action, "index")) { executeConfig = S2ExecuteConfigUtil.findExecuteConfig("/" + actionPath, req); action = executeConfig.getMethod().getName(); } else { executeConfig = S2ExecuteConfigUtil.findExecuteConfig("/" + actionPath, action); } if (executeConfig != null) { StringBuilder forwardPath = new StringBuilder(256); forwardPath .append("/") .append(actionPath) .append(".do?SAStruts.method=") .append(URLEncoderUtil.encode(action)); for (String key : params.keySet()) { forwardPath .append("&") .append(URLEncoderUtil.encode(key)) .append("=") .append(URLEncoderUtil.encode(params.getString(key))); } logger.debug(String.format("recognize route %s as %s#%s.", path, actionPath, action)); req.getRequestDispatcher(forwardPath.toString()).forward(req, res); return; } } } catch (RoutingException e) { if (!fallThrough) throw e; } } chain.doFilter(request, response); }
@Test(expected = RoutingException.class) public void testPut() throws RoutingException { Routes.load(ResourceUtil.getResourceAsFile("routes/via.xml")); System.out.println(Routes.getRouteSet().toString()); MockHttpServletRequest request = ((MockHttpServletRequest) RequestUtil.getRequest()); request.setMethod("PUT"); Routes.recognizePath("/methods/"); }
/** AbstractInterceptorを継承する際に、実装する必要のあるメソッド。 割り込ませる処理を記述。 */ @Override public Object invoke(MethodInvocation invocation) throws Throwable { // Map<String, Object> sessionScope = SingletonS2Container.getComponent("sessionScope"); // LoginUserDto loginDto = (LoginUserDto) sessionScope.get("loginUserDto"); // // loginDtoがNULLだったり、NULLでなくてもコードがNULLの場合タイムアウトと見なす。 // if (loginDto == null || loginDto.userId == null) { // // タイムアウト画面 // return "/"; // } String loginCookieValue = cookieService.getCookieValue("_coupon_island_login_"); String fbLoginCookieValue = cookieService.getCookieValue("_coupon_island_fb_login_"); IUserLogin userLogin = null; if (StringUtils.isEmpty(loginCookieValue) && StringUtils.isEmpty(fbLoginCookieValue)) { return "/"; } else if (StringUtils.isNotEmpty(loginCookieValue)) { userLogin = loginService.getIUserLogin(loginCookieValue); if (userLogin == null) { Cookie c = new Cookie("_coupon_island_login_", null); c.setMaxAge(0); // 即死にする c.setPath(RequestUtil.getRequest().getContextPath()); ResponseUtil.getResponse().addCookie(c); } } else if (StringUtils.isNotEmpty(fbLoginCookieValue)) { userLogin = loginService.getIUserLogin(fbLoginCookieValue); if (userLogin == null) { Cookie c = new Cookie("_coupon_island_fb_login_", null); c.setMaxAge(0); // 即死にする c.setPath(RequestUtil.getRequest().getContextPath()); ResponseUtil.getResponse().addCookie(c); } } if (userLogin == null) { return "/"; } loginUserDto.userId = userLogin.userId; return invocation.proceed(); }
@Test public void testPost() { Routes.load(ResourceUtil.getResourceAsFile("routes/via.xml")); System.out.println(Routes.getRouteSet().toString()); MockHttpServletRequest request = ((MockHttpServletRequest) RequestUtil.getRequest()); request.setMethod("POST"); Options options = Routes.recognizePath("/methods/"); System.out.println(options); assertThat(options.getString("action"), is("getAndPost")); }