001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.test.web;
017
018import com.jfinal.core.JFinalFilter;
019
020import javax.servlet.FilterChain;
021import javax.servlet.ServletException;
022import javax.servlet.ServletRequest;
023import javax.servlet.ServletResponse;
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026import java.io.IOException;
027
028public class MockJFinalFilter extends JFinalFilter {
029
030    @Override
031    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
032        HttpServletRequest request = (HttpServletRequest)req;
033        HttpServletResponse response = (HttpServletResponse)res;
034        request.setCharacterEncoding(encoding);
035
036        String target = request.getRequestURI();
037        if (contextPathLength != 0) {
038            target = target.substring(contextPathLength);
039        }
040
041        boolean[] isHandled = {false};
042        try {
043            handler.handle(target, request, response, isHandled);
044        }
045        catch (Exception e) {
046            if (log.isErrorEnabled()) {
047                String qs = request.getQueryString();
048                log.error(qs == null ? target : target + "?" + qs, e);
049            }
050            throw new AssertionError(e.getMessage(),e);
051        }
052
053        if (isHandled[0] == false) {
054            // 默认拒绝直接访问 jsp 文件,加固 tomcat、jetty 安全性
055//            if (constants.getDenyAccessJsp() && isJsp(target)) {
056//                com.jfinal.kit.HandlerKit.renderError404(request, response, isHandled);
057//                return ;
058//            }
059
060            chain.doFilter(request, response);
061        }
062    }
063}