JFinal整合CKFinder

    使用JFinal對以前的系統進行重構,老系統使用了CKEditor+CKFinder編輯器,發如今web.xml中配置ConnectorServlet會被JFinalFilter過濾處理,並且使用JFinal以後也不想在web.xml中增長額外的配置,因此嘗試的寫了JFinal整合CKFinder的Handler,因爲我對CKEditor+CKFinder不熟悉,可能整合的並不完整,這裏只當參考,歡迎提供更好的解決方案~~ java

package com.jfinal.ext.handler;

import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ckfinder.connector.ConnectorServlet;
import com.hikvision.util.base.PropertiesKit;
import com.jfinal.handler.Handler;

public class CKFinderHandler extends Handler
{
    private String visitPath = "/ckfinder/core/connector/java/connector";
    
    private ConnectorServlet servlet = new ConnectorServlet();
    
    private Properties properties;
    
    public CKFinderHandler() {}
    
    public CKFinderHandler(String cfgPath) 
    {
        this.properties = PropertiesKit.loadPropertyFile(cfgPath);
    }
    
    public CKFinderHandler(String visitPath, String cfgPath)
    {
        this.visitPath = visitPath;
        this.properties = PropertiesKit.loadPropertyFile(cfgPath);
    }
    
    @Override
    public void handle(String target, final HttpServletRequest request, HttpServletResponse response, boolean[] isHandled)
    {
        if (target.startsWith(visitPath)) {
            isHandled[0] = true;
            
            try {
                servlet.init(new ServletConfig()
                {
                    @Override
                    public String getServletName() {return null; }
                    
                    @Override
                    public ServletContext getServletContext() {return request.getSession().getServletContext(); }
                    
                    @SuppressWarnings("rawtypes")
                    @Override
                    public Enumeration getInitParameterNames() {return null; }
                    
                    @Override
                    public String getInitParameter(String name)
                    {
                        return properties.getProperty(name);
                    }
                });
                servlet.service(request, response);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        else {
            nextHandler.handle(target, request, response, isHandled);
        }
    }
}
@Override
public void configHandler(Handlers me)
{
    me.add(new CKFinderHandler("classes/ckfinder.properties"));
}

ckfinder.properties web

debug=false
XMLConfig=/WEB-INF/config.xml
相關文章
相關標籤/搜索