|
| 1 | +package com.xwintop.xTransfer.filter.service.impl; |
| 2 | + |
| 3 | +import com.xwintop.xTransfer.filter.bean.FilterConfig; |
| 4 | +import com.xwintop.xTransfer.filter.bean.FilterConfigUnicodeTransformation; |
| 5 | +import com.xwintop.xTransfer.filter.service.Filter; |
| 6 | +import com.xwintop.xTransfer.messaging.IContext; |
| 7 | +import com.xwintop.xTransfer.messaging.IMessage; |
| 8 | +import com.xwintop.xTransfer.util.Common; |
| 9 | +import lombok.Data; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.apache.commons.lang3.StringUtils; |
| 12 | +import org.springframework.context.annotation.Scope; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | + |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +/** |
| 18 | + * @ClassName: FilterUnicodeTransformationImpl |
| 19 | + * @Description: 编码转换工具类 |
| 20 | + * @author: xufeng |
| 21 | + * @date: 2019/8/21 18:07 |
| 22 | + */ |
| 23 | + |
| 24 | +@Service("filterUnicodeTransformation") |
| 25 | +@Scope("prototype") |
| 26 | +@Data |
| 27 | +@Slf4j |
| 28 | +public class FilterUnicodeTransformationImpl implements Filter { |
| 29 | + private FilterConfigUnicodeTransformation filterConfigUnicodeTransformation; |
| 30 | + |
| 31 | + @Override |
| 32 | + public void doFilter(IContext ctx, Map params) throws Exception { |
| 33 | + for (IMessage iMessage : ctx.getMessages()) { |
| 34 | + if (StringUtils.isNotBlank(filterConfigUnicodeTransformation.getFileNameFilterRegex())) { |
| 35 | + String fileNameFilterRegexGroup = filterConfigUnicodeTransformation.getFileNameFilterRegexGroup(); |
| 36 | + if (StringUtils.isEmpty(fileNameFilterRegexGroup)) { |
| 37 | + fileNameFilterRegexGroup = "defaultRegexGroup"; |
| 38 | + } |
| 39 | + if ("?!".equals(filterConfigUnicodeTransformation.getFileNameFilterRegex())) { |
| 40 | + if (iMessage.checkFileNameFilterRegexGroup(fileNameFilterRegexGroup)) { |
| 41 | + log.info("Filter:" + filterConfigUnicodeTransformation.getId() + "跳过fileName:" + iMessage.getFileName()); |
| 42 | + continue; |
| 43 | + } |
| 44 | + } else { |
| 45 | + if (!iMessage.getFileName().matches(filterConfigUnicodeTransformation.getFileNameFilterRegex())) { |
| 46 | + log.info("Filter:" + filterConfigUnicodeTransformation.getId() + "跳过fileName:" + iMessage.getFileName()); |
| 47 | + continue; |
| 48 | + } |
| 49 | + iMessage.addFileNameFilterRegexGroup(fileNameFilterRegexGroup); |
| 50 | + } |
| 51 | + } |
| 52 | + doFilter(iMessage); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public void doFilter(IMessage msg) throws Exception { |
| 57 | + if (StringUtils.isNotEmpty(filterConfigUnicodeTransformation.getOldEncoding())) { |
| 58 | + if ("AUTO".equalsIgnoreCase(filterConfigUnicodeTransformation.getOldEncoding())) { |
| 59 | + String charset = Common.detectFileCharset(msg.getMessage()); |
| 60 | + log.info("检测到文件:" + msg.getFileName() + "编码:" + charset); |
| 61 | + msg.setEncoding(charset); |
| 62 | + } else { |
| 63 | + msg.setEncoding(filterConfigUnicodeTransformation.getOldEncoding()); |
| 64 | + } |
| 65 | + } |
| 66 | + if (StringUtils.isNotEmpty(filterConfigUnicodeTransformation.getNewEncoding()) && !filterConfigUnicodeTransformation.getNewEncoding().equalsIgnoreCase(msg.getEncoding())) { |
| 67 | + byte[] newBytes = msg.getMessageByString().getBytes(filterConfigUnicodeTransformation.getNewEncoding()); |
| 68 | + msg.setMessage(newBytes); |
| 69 | + msg.setEncoding(filterConfigUnicodeTransformation.getNewEncoding()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void setFilterConfig(FilterConfig filterConfig) throws Exception { |
| 75 | + this.filterConfigUnicodeTransformation = (FilterConfigUnicodeTransformation) filterConfig; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void destroy() { |
| 80 | + } |
| 81 | +} |
0 commit comments