希望在导出操作之前,加入一些自定义操作,可以用这个接口实现。比如导出的文件进行自定义加密。
注:该接口为流式接口,数据量较大,应避免将数据放入内存,否则存在宕机风险!!
package com.finebi.dashboard.impl.service.export; import com.finebi.dashboard.api.service.export.ExportContext; import com.fr.stable.fun.mark.Mutable; import java.io.OutputStream; /** * 提供导出流修改接口 * Created by Hiram on 2018/11/14. */ public interface ExportHandleProvider extends Mutable { String XML_TAG = "ExportHandleProvider"; int CURRENT_LEVEL = 1; /** * 导出流修改 * * @param originalOutputStream 原始导出流 * @param type 导出类型 * @return 处理后的流 */ OutputStream handleStream(OutputStream originalOutputStream, ExportType type); /** * 导出流修改 * * @param originalOutputStream 原始导出流 * @param exportContext 导出上下文 * @return */ OutputStream handleStream(OutputStream originalOutputStream, ExportContext exportContext); } |
<extra-core> <ExportHandleProvider class="插件自定义实现包全名"/> </extra-core> |
package com.finebi.provider.api.export; import com.finebi.dashboard.impl.service.export.ExportHandleProvider; import com.finebi.dashboard.impl.service.export.ExportType; import com.fr.stable.fun.mark.API; import java.io.OutputStream; /** * 计算导出excel的大小 * Created by Hiram on 2018/11/15. */ @API(level = ExportHandleProvider.CURRENT_LEVEL) public abstract class AbstractExportHandleProvider implements ExportHandleProvider { @Override public int currentAPILevel() { return CURRENT_LEVEL; } @Override public String mark4Provider() { return getClass().getName(); } @Override @Deprecated public OutputStream handleStream(OutputStream originalOutputStream, ExportType type) { return originalOutputStream; } } |
接口内容主要接口
|
<extra-core> <ExportHandleProvider class="com.fr.plugin.demo.export.CountExportHandle"/> </extra-core> |
简单的统计一下导出文件的大小,输出在日志里面。
写一个拦截处理的CountExportHandle,返回一个CountOutputStream,在write时计数,最后在close的时候输出大小。
import com.finebi.stable.fun.ExportType; import com.finebi.stable.fun.impl.AbstractExportHandleProvider; import java.io.OutputStream; public class CountExportHandle extends AbstractExportHandleProvider { @Override public OutputStream handleStream(OutputStream originalOutputStream, ExportType type) { return new CountOutputStream(originalOutputStream); } } |
import com.fr.log.FineLoggerFactory; import java.io.IOException; import java.io.OutputStream; public class CountOutputStream extends OutputStream { private OutputStream out; private int count; public CountOutputStream(OutputStream out) { this.out = out; } @Override public void write(int b) throws IOException { count++; out.write(b); } @Override public void write(byte[] b) throws IOException { count += b.length; out.write(b); } @Override public void write(byte[] b, int off, int len) throws IOException { count += len; out.write(b, off, len); } @Override public void flush() throws IOException { out.flush(); } @Override public void close() throws IOException { FineLoggerFactory.getLogger().info("===== export length: {} ======", count); out.close(); } } |