You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
8.2 KiB
206 lines
8.2 KiB
package com.fr.plugin.xx.zyjn.decode.decode; |
|
|
|
import cn.net.drm.edi.client.DrmAgent; |
|
import cn.net.drm.edi.client.DrmAgentInf; |
|
import cn.net.drm.edi.exception.DrmException; |
|
import com.fr.base.ServerConfig; |
|
import com.fr.base.core.IgnoreBytesInputStream; |
|
import com.fr.base.core.ParseResult; |
|
import com.fr.base.core.PostParseUtils; |
|
import com.fr.data.NetworkHelper; |
|
import com.fr.decision.authority.data.User; |
|
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
|
import com.fr.decision.mobile.terminal.TerminalHandler; |
|
import com.fr.decision.webservice.v10.config.ConfigService; |
|
import com.fr.decision.webservice.v10.login.LoginService; |
|
import com.fr.decision.webservice.v10.login.TokenResource; |
|
import com.fr.decision.webservice.v10.user.UserService; |
|
import com.fr.general.CommonIOUtils; |
|
import com.fr.io.utils.ResourceIOUtils; |
|
import com.fr.plugin.context.PluginContexts; |
|
import com.fr.plugin.xx.zyjn.decode.LogUtils; |
|
import com.fr.plugin.xx.zyjn.decode.PluginConstants; |
|
import com.fr.plugin.transform.FunctionRecorder; |
|
import com.fr.stable.fun.Authorize; |
|
import com.fr.stable.web.Device; |
|
import com.fr.third.org.apache.commons.io.IOUtils; |
|
|
|
import javax.servlet.FilterChain; |
|
import javax.servlet.ServletException; |
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import java.io.*; |
|
import java.util.UUID; |
|
|
|
/** |
|
* @author xx |
|
* @date 2022/03/01 |
|
*/ |
|
@FunctionRecorder |
|
@Authorize(callSignKey = PluginConstants.PLUGIN_ID) |
|
public class DecodeFilter extends AbstractGlobalRequestFilterProvider { |
|
|
|
private static final byte[] NEW_LINE_BYTES = new byte[]{13, 10}; |
|
private static final byte[] BOUNDARY_END = new byte[]{45, 45}; |
|
|
|
private static final String TMP_PATH = System.getProperty("java.io.tmpdir"); |
|
|
|
@Override |
|
public String filterName() { |
|
return "decode"; |
|
} |
|
|
|
@Override |
|
public String[] urlPatterns() { |
|
String servletPathName = "decision"; |
|
try { |
|
servletPathName = ConfigService.getInstance().getBasicParam().getServletPathName(); |
|
} catch (Exception e) { |
|
LogUtils.error(e.getMessage(), e); |
|
} |
|
if (PluginContexts.currentContext().isAvailable()) { |
|
return new String[]{ |
|
"/" + servletPathName + "/v10/attach/upload", |
|
"/" + servletPathName + "/v10/dataset/upload", |
|
"/" + servletPathName + "/view/report", |
|
|
|
}; |
|
} else { |
|
return new String[]{"/aaaa"}; |
|
} |
|
|
|
} |
|
|
|
@Override |
|
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) { |
|
BodyReaderHttpServletRequestWrapper wrapper = null; |
|
try { |
|
DrmAgentInf instance; |
|
instance = DrmAgent.getInstance(); |
|
wrapper = new BodyReaderHttpServletRequestWrapper(req); |
|
User user = getUser(req); |
|
if (user == null) { |
|
LogUtils.debug4plugin("current user is null,not decrypted"); |
|
filter(wrapper, res, filterChain); |
|
return; |
|
} |
|
InputStream inputStream = wrapper.getInputStream(); |
|
ParseResult parseResult = PostParseUtils.parse(inputStream, req.getCharacterEncoding()); |
|
InputStream fis = new IgnoreBytesInputStream(inputStream, this.concat(this.concat(NEW_LINE_BYTES, parseResult.getBoundary().getBytes()), BOUNDARY_END)); |
|
LogUtils.debug4plugin("record file request url is {}", req.getRequestURI() + req.getQueryString()); |
|
File file = saveTmp(fis, parseResult.getFileName()); |
|
boolean encrypted = instance.isEncrypted(file); |
|
LogUtils.debug4plugin("current files encrypted is {}", encrypted); |
|
if (!encrypted) { |
|
filter(wrapper, res, filterChain); |
|
return; |
|
} |
|
decode(req, wrapper, parseResult, file, instance); |
|
} catch (Exception e) { |
|
LogUtils.error(e.getMessage(), e); |
|
} |
|
if (wrapper != null) { |
|
filter(wrapper, res, filterChain); |
|
return; |
|
} |
|
} |
|
|
|
private void decode(HttpServletRequest req, BodyReaderHttpServletRequestWrapper wrapper, ParseResult parseResult, File file, DrmAgentInf instance) throws DrmException, UnsupportedEncodingException, FileNotFoundException { |
|
//File file = saveTmp(fis,parseResult.getFileName()); |
|
long decrypt = instance.decrypt(file); |
|
LogUtils.debug4plugin("decode file size is {}", decrypt); |
|
wrapper.setBody(handleBody(req, CommonIOUtils.inputStream2Bytes(new FileInputStream(file)), parseResult, req.getCharacterEncoding() == null ? ServerConfig.getInstance().getServerCharset() : req.getCharacterEncoding())); |
|
file.delete(); |
|
LogUtils.debug4plugin("delete temp file"); |
|
} |
|
|
|
private User getUser(HttpServletRequest req) { |
|
try { |
|
return UserService.getInstance().getUserByRequestCookie(req); |
|
} catch (Exception e) { |
|
|
|
} |
|
return null; |
|
} |
|
|
|
private File saveTmp(InputStream input, String filename) { |
|
if (!ResourceIOUtils.isDirectoryExist(TMP_PATH)) { |
|
ResourceIOUtils.createDirectory(TMP_PATH); |
|
} |
|
String path = TMP_PATH + File.separator + UUID.randomUUID() + filename.substring(filename.indexOf(".")); |
|
File file = new File(path); |
|
FileOutputStream outputStream = null; |
|
if (!file.exists()) { |
|
try { |
|
file.createNewFile(); |
|
outputStream = new FileOutputStream(file.getPath()); |
|
IOUtils.copyLarge(input, outputStream); |
|
LogUtils.debug4plugin("save tmp file path is {}", path); |
|
} catch (IOException e) { |
|
LogUtils.error(e.getMessage(), e); |
|
} finally { |
|
try { |
|
input.close(); |
|
outputStream.flush(); |
|
outputStream.close(); |
|
} catch (IOException e) { |
|
LogUtils.error(e.getMessage(), e); |
|
} |
|
} |
|
} |
|
return new File(path); |
|
} |
|
|
|
|
|
private void filter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) { |
|
try { |
|
filterChain.doFilter(req, res); |
|
} catch (IOException e) { |
|
LogUtils.error(e.getMessage(), e); |
|
} catch (ServletException e) { |
|
LogUtils.error(e.getMessage(), e); |
|
} |
|
} |
|
|
|
private boolean isLogin(HttpServletRequest request) { |
|
String oldToken = TokenResource.COOKIE.getToken(request); |
|
return oldToken != null && checkTokenValid(request, (String) oldToken); |
|
} |
|
|
|
private boolean checkTokenValid(HttpServletRequest req, String token) { |
|
try { |
|
Device device = NetworkHelper.getDevice(req); |
|
LoginService.getInstance().loginStatusValid(token, TerminalHandler.getTerminal(req, device)); |
|
return true; |
|
} catch (Exception ignore) { |
|
} |
|
return false; |
|
} |
|
|
|
private byte[] concat(byte[] a, byte[] b) { |
|
byte[] c = new byte[a.length + b.length]; |
|
System.arraycopy(a, 0, c, 0, a.length); |
|
System.arraycopy(b, 0, c, a.length, b.length); |
|
return c; |
|
} |
|
|
|
private byte[] handleBody(HttpServletRequest req, byte[] body, ParseResult parseResult, String charsetName) throws UnsupportedEncodingException { |
|
byte[] newBody; |
|
byte[] start; |
|
byte[] end; |
|
StringBuilder stringBuilder = new StringBuilder(); |
|
stringBuilder.append(parseResult.getBoundary()).append("\r\n") |
|
.append("Content-Disposition:").append(parseResult.getDispline()) |
|
.append("; name=\"").append(parseResult.getFieldName()) |
|
.append("\"; filename=\"").append(parseResult.getFileName()).append("\"\r\n") |
|
.append("Content-Type: ").append(req.getContentType()).append("\r\n").append("\r\n"); |
|
start = stringBuilder.toString().getBytes(charsetName); |
|
StringBuilder stringBuilder1 = new StringBuilder(); |
|
stringBuilder1.append("\r\n").append(parseResult.getBoundary()).append("--\r\n"); |
|
end = stringBuilder1.toString().getBytes(charsetName); |
|
|
|
newBody = this.concat(this.concat(start, body), end); |
|
return newBody; |
|
} |
|
|
|
}
|
|
|