13 changed files with 330 additions and 104 deletions
@ -1,14 +1,16 @@ |
|||||||
package com.fr.plugin.file.submit.oss; |
package com.fr.plugin.file.submit.oss; |
||||||
|
|
||||||
|
import com.fr.decision.fun.impl.BaseHttpHandler; |
||||||
import com.fr.decision.webservice.plugin.handler.AbstractHttpHandlerProvider; |
import com.fr.decision.webservice.plugin.handler.AbstractHttpHandlerProvider; |
||||||
import com.fr.decision.webservice.plugin.handler.HttpHandler; |
import com.fr.plugin.file.submit.oss.fun.OssDownloadHttpHandler; |
||||||
import com.fr.plugin.file.submit.oss.fun.OssHttpHandler; |
import com.fr.plugin.file.submit.oss.fun.OssUploadHttpHandler; |
||||||
|
|
||||||
public class OssRequestHandler extends AbstractHttpHandlerProvider { |
public class OssRequestHandler extends AbstractHttpHandlerProvider { |
||||||
@Override |
@Override |
||||||
public HttpHandler[] registerHandlers() { |
public BaseHttpHandler[] registerHandlers() { |
||||||
return new HttpHandler[] { |
return new BaseHttpHandler[] { |
||||||
new OssHttpHandler() |
new OssDownloadHttpHandler(), |
||||||
|
new OssUploadHttpHandler() |
||||||
}; |
}; |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -0,0 +1,70 @@ |
|||||||
|
package com.fr.plugin.file.submit.oss.fun; |
||||||
|
|
||||||
|
import com.aliyun.oss.OSSClient; |
||||||
|
import com.fr.cache.Attachment; |
||||||
|
import com.fr.general.FArray; |
||||||
|
import com.fr.general.GeneralUtils; |
||||||
|
import com.fr.script.Calculator; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
import com.fr.stable.xml.FRFile; |
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream; |
||||||
|
|
||||||
|
public class OssUpload { |
||||||
|
|
||||||
|
public static void upload(Calculator calculator, OSSClient ossClient, OssSubmitTargetConfig submitTargetConfig) throws Exception { |
||||||
|
OssSubmitTarget[] submitTargets = submitTargetConfig.getSubmitTargets(); |
||||||
|
for (OssSubmitTarget target : submitTargets) { |
||||||
|
OssVariableValue fileObject = target.getFile(); |
||||||
|
Object file = calculator.evalValue(GeneralUtils.objectToString(fileObject)); |
||||||
|
|
||||||
|
|
||||||
|
uploadFile(ossClient, target.getBucket(), file, |
||||||
|
target.getDirectory() == null ? null : target.getDirectory().getResult(calculator), |
||||||
|
target.getName() == null ? null : target.getName().getResult(calculator)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static void uploadFile(OSSClient ossClient, String bucket, Object file, String directory, String newFileName) { |
||||||
|
if (file instanceof FRFile) { |
||||||
|
ossClient.putObject(bucket, directory + "/" + combineName(((FRFile) file).getFileName(), newFileName), new ByteArrayInputStream(((FRFile) file).getBytes())); |
||||||
|
} else if (file instanceof FArray && ((FArray) file).length() != 0) { |
||||||
|
FArray array = (FArray)file; |
||||||
|
for (int i = 0; i < array.length(); i++) { |
||||||
|
Object element = array.elementAt(i); |
||||||
|
if (!(element instanceof Attachment)) { |
||||||
|
if (element instanceof FArray) { |
||||||
|
uploadFile(ossClient, bucket, element, directory, newFileName); |
||||||
|
} |
||||||
|
} else { |
||||||
|
ossClient.putObject(bucket, directory + "/" + combineName(((Attachment) element).getFilename(), newFileName), new ByteArrayInputStream(((Attachment) element).getBytes())); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
* 这里的逻辑是: |
||||||
|
* 1、如果没有设置文件名,就用上传的文件的名字; |
||||||
|
* 2、如果设置了文件名,且带有文件后缀,就用设置的文件名; |
||||||
|
* 3、其他情况都用原文件名。 |
||||||
|
*/ |
||||||
|
private static String combineName(String fileName, String myFileName) { |
||||||
|
String realFileName; |
||||||
|
if (StringUtils.isBlank(fileName)) { |
||||||
|
fileName = "unnamed.png"; |
||||||
|
} |
||||||
|
if (StringUtils.isNotEmpty(myFileName)) { |
||||||
|
int index = myFileName.lastIndexOf("."); |
||||||
|
if (index == -1) { |
||||||
|
realFileName = fileName; |
||||||
|
} else { |
||||||
|
realFileName = myFileName; |
||||||
|
} |
||||||
|
} else { |
||||||
|
realFileName = fileName; |
||||||
|
} |
||||||
|
return realFileName; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,102 @@ |
|||||||
|
package com.fr.plugin.file.submit.oss.fun; |
||||||
|
|
||||||
|
import com.aliyun.oss.OSSClient; |
||||||
|
import com.fr.base.ParameterMapNameSpace; |
||||||
|
import com.fr.data.NetworkHelper; |
||||||
|
import com.fr.decision.fun.impl.BaseHttpHandler; |
||||||
|
import com.fr.general.xml.GeneralXMLTools; |
||||||
|
import com.fr.json.JSON; |
||||||
|
import com.fr.json.JSONArray; |
||||||
|
import com.fr.json.JSONFactory; |
||||||
|
import com.fr.json.JSONObject; |
||||||
|
import com.fr.log.FineLoggerFactory; |
||||||
|
import com.fr.plugin.file.submit.oss.conf.FileSubmitOssServerConfig; |
||||||
|
import com.fr.plugin.file.submit.oss.script.OssSubmitJavaScript; |
||||||
|
import com.fr.script.Calculator; |
||||||
|
import com.fr.stable.ColumnRow; |
||||||
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
||||||
|
import com.fr.web.core.ReportSessionIDInfor; |
||||||
|
import com.fr.web.core.SessionPoolManager; |
||||||
|
import com.fr.web.session.SessionIDInfo; |
||||||
|
import com.fr.web.utils.WebUtils; |
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
import java.io.PrintWriter; |
||||||
|
|
||||||
|
public class OssUploadHttpHandler extends BaseHttpHandler { |
||||||
|
@Override |
||||||
|
public RequestMethod getMethod() { |
||||||
|
return RequestMethod.POST; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String getPath() { |
||||||
|
return "/oss/upload"; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isPublic() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void handle(HttpServletRequest req, HttpServletResponse res) throws Exception { |
||||||
|
final String httpRequestEncodeParameter = NetworkHelper.getHTTPRequestEncodeParameter(req, "xmlconf", true); |
||||||
|
if (httpRequestEncodeParameter != null) { |
||||||
|
String sessionID = NetworkHelper.getHTTPRequestParameter(req, "sessionID"); |
||||||
|
final ReportSessionIDInfor reportSessionIDInfor = SessionPoolManager.getSessionIDInfor(sessionID, ReportSessionIDInfor.class); |
||||||
|
if (reportSessionIDInfor == null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
final OssSubmitJavaScript javaScript = (OssSubmitJavaScript) GeneralXMLTools.readStringAsXMLable(httpRequestEncodeParameter); |
||||||
|
if (javaScript == null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
final Calculator calculator = Calculator.createCalculator(); |
||||||
|
final ParameterMapNameSpace create = ParameterMapNameSpace.create(WebUtils.parameters4SessionIDInfor(req)); |
||||||
|
calculator.pushNameSpace(create); |
||||||
|
this.initCalAttrAndNS(calculator, reportSessionIDInfor, req); |
||||||
|
boolean b = true; |
||||||
|
final JSONObject jsonObject = JSONObject.create(); |
||||||
|
OSSClient ossClient = new OSSClient( |
||||||
|
FileSubmitOssServerConfig.getInstance().getEndPoint(), |
||||||
|
FileSubmitOssServerConfig.getInstance().getAccessKeyId(), |
||||||
|
FileSubmitOssServerConfig.getInstance().getAccessKeySecret()); |
||||||
|
try { |
||||||
|
this.process(ossClient, javaScript, calculator); |
||||||
|
} catch (Exception ex) { |
||||||
|
FineLoggerFactory.getLogger().error(ex.getMessage(), ex); |
||||||
|
b = false; |
||||||
|
jsonObject.put("errorMsg", ex.getMessage()); |
||||||
|
} finally { |
||||||
|
ossClient.shutdown(); |
||||||
|
} |
||||||
|
calculator.removeNameSpace(create); |
||||||
|
final PrintWriter printWriter = WebUtils.createPrintWriter(res); |
||||||
|
final JSONArray jsonArray = JSONFactory.createJSON(JSON.ARRAY); |
||||||
|
final JSONObject jsonObject2 = JSONFactory.createJSON(JSON.OBJECT); |
||||||
|
jsonObject.put("success", b); |
||||||
|
jsonObject2.put("oss_submit_info", jsonObject); |
||||||
|
jsonArray.put(jsonObject2); |
||||||
|
printWriter.print(jsonArray); |
||||||
|
printWriter.flush(); |
||||||
|
printWriter.close(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void process(OSSClient ossClient, OssSubmitJavaScript javaScript, Calculator calculator) throws Exception { |
||||||
|
OssUpload.upload(calculator, ossClient, javaScript.getSubmitTargetConfig()); |
||||||
|
} |
||||||
|
|
||||||
|
private void initCalAttrAndNS(final Calculator calculator, final ReportSessionIDInfor reportSessionIDInfor, final HttpServletRequest httpServletRequest) { |
||||||
|
calculator.setAttribute(Calculator.SHEET_NUMBER_KEY, WebUtils.getHTTPRequestParameter(httpServletRequest, "sheetNum")); |
||||||
|
reportSessionIDInfor.setUpAttribute4dbCommit(calculator); |
||||||
|
calculator.pushNameSpace(ParameterMapNameSpace.create(reportSessionIDInfor.getParameterMap4Execute())); |
||||||
|
final ColumnRow value = ColumnRow.valueOf(WebUtils.getHTTPRequestParameter(httpServletRequest, "location")); |
||||||
|
if (value != ColumnRow.ERROR) { |
||||||
|
calculator.setAttribute(ColumnRow.CURRENT_CR_KEY, value); |
||||||
|
} |
||||||
|
calculator.pushNameSpace(SessionIDInfo.asNameSpace(reportSessionIDInfor)); |
||||||
|
} |
||||||
|
} |
@ -1,34 +1,65 @@ |
|||||||
package com.fr.plugin.file.submit.oss.script; |
package com.fr.plugin.file.submit.oss.script; |
||||||
|
|
||||||
|
import com.fr.general.xml.GeneralXMLTools; |
||||||
import com.fr.js.AbstractJavaScript; |
import com.fr.js.AbstractJavaScript; |
||||||
|
import com.fr.json.JSONObject; |
||||||
|
import com.fr.plugin.file.submit.oss.fun.OssSubmitTargetConfig; |
||||||
|
import com.fr.stable.AssistUtils; |
||||||
import com.fr.stable.web.Repository; |
import com.fr.stable.web.Repository; |
||||||
import com.fr.stable.xml.XMLPrintWriter; |
import com.fr.stable.xml.XMLPrintWriter; |
||||||
import com.fr.stable.xml.XMLableReader; |
import com.fr.stable.xml.XMLableReader; |
||||||
|
|
||||||
public class OssSubmitJavaScript extends AbstractJavaScript { |
public class OssSubmitJavaScript extends AbstractJavaScript { |
||||||
|
|
||||||
|
private OssSubmitTargetConfig submitTargetConfig; |
||||||
|
|
||||||
|
public OssSubmitTargetConfig getSubmitTargetConfig() { |
||||||
|
return submitTargetConfig; |
||||||
|
} |
||||||
|
|
||||||
|
public void setSubmitTargetConfig(OssSubmitTargetConfig submitTargetConfig) { |
||||||
|
this.submitTargetConfig = submitTargetConfig; |
||||||
|
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
protected String actionJS(Repository repository) { |
protected String actionJS(Repository repository) { |
||||||
return null; |
return "var fm = new FR.OssForm();fm.ossCommit({xmlconf:" + JSONObject.quote(GeneralXMLTools.writeXMLableAsString(this)) + "},this)"; |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void writeXML(XMLPrintWriter xmlPrintWriter) { |
public void readXML(XMLableReader reader) { |
||||||
super.writeXML(xmlPrintWriter); |
super.readXML(reader); |
||||||
|
if (reader.isChildNode()) { |
||||||
|
String tagName = reader.getTagName(); |
||||||
|
if (OssSubmitTargetConfig.XML_TAG.equals(tagName)) { |
||||||
|
OssSubmitTargetConfig newConfig = new OssSubmitTargetConfig(); |
||||||
|
newConfig.readXML(reader); |
||||||
|
submitTargetConfig = newConfig; |
||||||
|
} |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void readXML(XMLableReader xmLableReader) { |
public void writeXML(XMLPrintWriter writer) { |
||||||
super.readXML(xmLableReader); |
super.writeXML(writer); |
||||||
|
if (submitTargetConfig != null) { |
||||||
|
GeneralXMLTools.writeXMLable(writer, submitTargetConfig, OssSubmitTargetConfig.XML_TAG); |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
public boolean equals(Object o) { |
public boolean equals(Object o) { |
||||||
return super.equals(o); |
return o instanceof OssSubmitJavaScript |
||||||
|
&& super.equals(o) |
||||||
|
&& AssistUtils.equals(((OssSubmitJavaScript) o).submitTargetConfig, submitTargetConfig); |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
public Object clone() throws CloneNotSupportedException { |
public Object clone() throws CloneNotSupportedException { |
||||||
return super.clone(); |
OssSubmitJavaScript cloned = (OssSubmitJavaScript) super.clone(); |
||||||
|
if (submitTargetConfig != null) { |
||||||
|
cloned.submitTargetConfig = (OssSubmitTargetConfig) submitTargetConfig.clone(); |
||||||
|
} |
||||||
|
return cloned; |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -1,17 +0,0 @@ |
|||||||
package com.fr.plugin.file.submit.oss.ui.tools; |
|
||||||
|
|
||||||
import javax.swing.*; |
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
public class RewriteWorker extends SwingWorker<Void, Void> { |
|
||||||
|
|
||||||
@Override |
|
||||||
protected Void doInBackground() throws Exception { |
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
protected void process(List<Void> chunks) { |
|
||||||
super.process(chunks); |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue