Browse Source

无JIRA任务 OSS按钮事件

pull/1/head
richie 6 years ago
parent
commit
743e522f7d
  1. 3
      plugin.xml
  2. 8
      src/main/java/com/fr/plugin/file/submit/oss/OssJavaScriptActionBridge.java
  3. 12
      src/main/java/com/fr/plugin/file/submit/oss/OssRequestHandler.java
  4. 3
      src/main/java/com/fr/plugin/file/submit/oss/OssURLAliasProvider.java
  5. 8
      src/main/java/com/fr/plugin/file/submit/oss/fun/OssDownloadHttpHandler.java
  6. 70
      src/main/java/com/fr/plugin/file/submit/oss/fun/OssUpload.java
  7. 102
      src/main/java/com/fr/plugin/file/submit/oss/fun/OssUploadHttpHandler.java
  8. 64
      src/main/java/com/fr/plugin/file/submit/oss/job/OssSubmitJob.java
  9. 45
      src/main/java/com/fr/plugin/file/submit/oss/script/OssSubmitJavaScript.java
  10. 39
      src/main/java/com/fr/plugin/file/submit/oss/ui/OssSubmitJavaScriptPane.java
  11. 17
      src/main/java/com/fr/plugin/file/submit/oss/ui/tools/RewriteWorker.java
  12. 2
      src/main/resources/com/fr/plugin/file/submit/oss/submit_zh_CN.properties
  13. 61
      src/main/resources/com/fr/plugin/file/submit/oss/web/oss.js

3
plugin.xml

@ -2,12 +2,13 @@
<id>com.fr.plugin.file.submit.oss</id>
<name><![CDATA[文件上传下载(OSS)]]></name>
<active>yes</active>
<version>1.2</version>
<version>1.3</version>
<env-version>10.0</env-version>
<jartime>2018-11-02</jartime>
<vendor>author</vendor>
<description><![CDATA[文件上传至阿里云的OSS中。]]></description>
<change-notes><![CDATA[
[2018-11-05]JavaScript事件中可以使用改插件。<br/>
[2018-11-05]下载无法使用$$$的问题。<br/>
[2018-10-31]初始化插件。<br/>
]]></change-notes>

8
src/main/java/com/fr/plugin/file/submit/oss/OssJavaScriptActionBridge.java

@ -2,12 +2,18 @@ package com.fr.plugin.file.submit.oss;
import com.fr.design.beans.FurtherBasicBeanPane;
import com.fr.design.fun.impl.AbstractJavaScriptActionProvider;
import com.fr.design.mainframe.DesignerContext;
import com.fr.js.JavaScript;
import com.fr.plugin.file.submit.oss.ui.OssSubmitJavaScriptPane;
public class OssJavaScriptActionBridge extends AbstractJavaScriptActionProvider {
@Override
public FurtherBasicBeanPane<? extends JavaScript> getJavaScriptActionPane() {
return null;
return new OssSubmitJavaScriptPane();
}
public boolean isSupportType() {
return DesignerContext.getDesignerFrame().getSelectedJTemplate().isJWorkBook();
}
}

12
src/main/java/com/fr/plugin/file/submit/oss/OssRequestHandler.java

@ -1,14 +1,16 @@
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.HttpHandler;
import com.fr.plugin.file.submit.oss.fun.OssHttpHandler;
import com.fr.plugin.file.submit.oss.fun.OssDownloadHttpHandler;
import com.fr.plugin.file.submit.oss.fun.OssUploadHttpHandler;
public class OssRequestHandler extends AbstractHttpHandlerProvider {
@Override
public HttpHandler[] registerHandlers() {
return new HttpHandler[] {
new OssHttpHandler()
public BaseHttpHandler[] registerHandlers() {
return new BaseHttpHandler[] {
new OssDownloadHttpHandler(),
new OssUploadHttpHandler()
};
}
}

3
src/main/java/com/fr/plugin/file/submit/oss/OssURLAliasProvider.java

@ -8,7 +8,8 @@ public class OssURLAliasProvider extends AbstractURLAliasProvider {
@Override
public URLAlias[] registerAlias() {
return new URLAlias[]{
URLAliasFactory.createPluginAlias("/oss/download", "/oss/download", true)
URLAliasFactory.createPluginAlias("/oss/download", "/oss/download", true),
URLAliasFactory.createPluginAlias("/oss/upload", "/oss/upload", true)
};
}
}

8
src/main/java/com/fr/plugin/file/submit/oss/fun/OssHttpHandler.java → src/main/java/com/fr/plugin/file/submit/oss/fun/OssDownloadHttpHandler.java

@ -2,11 +2,10 @@ package com.fr.plugin.file.submit.oss.fun;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.DateUtil;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.fr.data.NetworkHelper;
import com.fr.decision.webservice.plugin.handler.HttpHandler;
import com.fr.general.DateUtils;
import com.fr.decision.fun.HttpHandler;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.plugin.file.submit.oss.conf.FileSubmitOssServerConfig;
import com.fr.stable.StableUtils;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
@ -18,9 +17,8 @@ import java.io.PrintWriter;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class OssHttpHandler implements HttpHandler {
public class OssDownloadHttpHandler extends BaseHttpHandler {
@Override
public RequestMethod getMethod() {
return RequestMethod.POST;

70
src/main/java/com/fr/plugin/file/submit/oss/fun/OssUpload.java

@ -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;
}
}

102
src/main/java/com/fr/plugin/file/submit/oss/fun/OssUploadHttpHandler.java

@ -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));
}
}

64
src/main/java/com/fr/plugin/file/submit/oss/job/OssSubmitJob.java

@ -1,28 +1,19 @@
package com.fr.plugin.file.submit.oss.job;
import com.aliyun.oss.OSSClient;
import com.fr.cache.Attachment;
import com.fr.data.AbstractSubmitTask;
import com.fr.general.FArray;
import com.fr.general.GeneralUtils;
import com.fr.general.xml.GeneralXMLTools;
import com.fr.intelli.record.Focus;
import com.fr.intelli.record.Original;
import com.fr.plugin.file.submit.oss.conf.FileSubmitOssServerConfig;
import com.fr.plugin.file.submit.oss.fun.OssConstants;
import com.fr.plugin.file.submit.oss.fun.OssSubmitTarget;
import com.fr.plugin.file.submit.oss.fun.OssSubmitTargetConfig;
import com.fr.plugin.file.submit.oss.fun.OssVariableValue;
import com.fr.plugin.file.submit.oss.fun.OssUpload;
import com.fr.record.analyzer.EnableMetrics;
import com.fr.script.Calculator;
import com.fr.stable.StringUtils;
import com.fr.stable.xml.FRFile;
import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLableReader;
import java.io.ByteArrayInputStream;
import java.io.File;
/**
* 提交到OSS的任务
*/
@ -53,16 +44,7 @@ public class OssSubmitJob extends AbstractSubmitTask {
FileSubmitOssServerConfig.getInstance().getEndPoint(),
FileSubmitOssServerConfig.getInstance().getAccessKeyId(),
FileSubmitOssServerConfig.getInstance().getAccessKeySecret());
OssSubmitTarget[] submitTargets = submitTargetConfig.getSubmitTargets();
for (OssSubmitTarget target : submitTargets) {
OssVariableValue fileObject = target.getFile();
Object file = calculator.evalValue(GeneralUtils.objectToString(fileObject));
uploadFile(target.getBucket(), file,
target.getDirectory() == null ? null : target.getDirectory().getResult(calculator),
target.getName() == null ? null : target.getName().getResult(calculator));
}
OssUpload.upload(calculator, ossClient, submitTargetConfig);
}
@Override
@ -70,48 +52,6 @@ public class OssSubmitJob extends AbstractSubmitTask {
ossClient.shutdown();
}
private void uploadFile(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(bucket, element, directory, newFileName);
}
} else {
ossClient.putObject(bucket, directory + "/" + combineName(((Attachment) element).getFilename(), newFileName), new ByteArrayInputStream(((Attachment) element).getBytes()));
}
}
}
}
/*
* 这里的逻辑是
* 1如果没有设置文件名就用上传的文件的名字
* 2如果设置了文件名且带有文件后缀就用设置的文件名
* 3其他情况都用原文件名
*/
private 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;
}
@Override
public void readXML(final XMLableReader reader) {
if (reader.isChildNode()) {

45
src/main/java/com/fr/plugin/file/submit/oss/script/OssSubmitJavaScript.java

@ -1,34 +1,65 @@
package com.fr.plugin.file.submit.oss.script;
import com.fr.general.xml.GeneralXMLTools;
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.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLableReader;
public class OssSubmitJavaScript extends AbstractJavaScript {
private OssSubmitTargetConfig submitTargetConfig;
public OssSubmitTargetConfig getSubmitTargetConfig() {
return submitTargetConfig;
}
public void setSubmitTargetConfig(OssSubmitTargetConfig submitTargetConfig) {
this.submitTargetConfig = submitTargetConfig;
}
@Override
protected String actionJS(Repository repository) {
return null;
return "var fm = new FR.OssForm();fm.ossCommit({xmlconf:" + JSONObject.quote(GeneralXMLTools.writeXMLableAsString(this)) + "},this)";
}
@Override
public void writeXML(XMLPrintWriter xmlPrintWriter) {
super.writeXML(xmlPrintWriter);
public void readXML(XMLableReader reader) {
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
public void readXML(XMLableReader xmLableReader) {
super.readXML(xmLableReader);
public void writeXML(XMLPrintWriter writer) {
super.writeXML(writer);
if (submitTargetConfig != null) {
GeneralXMLTools.writeXMLable(writer, submitTargetConfig, OssSubmitTargetConfig.XML_TAG);
}
}
@Override
public boolean equals(Object o) {
return super.equals(o);
return o instanceof OssSubmitJavaScript
&& super.equals(o)
&& AssistUtils.equals(((OssSubmitJavaScript) o).submitTargetConfig, submitTargetConfig);
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
OssSubmitJavaScript cloned = (OssSubmitJavaScript) super.clone();
if (submitTargetConfig != null) {
cloned.submitTargetConfig = (OssSubmitTargetConfig) submitTargetConfig.clone();
}
return cloned;
}
}

39
src/main/java/com/fr/plugin/file/submit/oss/ui/OssSubmitJavaScriptPane.java

@ -1,11 +1,29 @@
package com.fr.plugin.file.submit.oss.ui;
import com.fr.design.beans.FurtherBasicBeanPane;
import com.fr.design.gui.itableeditorpane.UITableEditorPane;
import com.fr.design.i18n.Toolkit;
import com.fr.plugin.file.submit.oss.fun.OssSubmitTarget;
import com.fr.plugin.file.submit.oss.fun.OssSubmitTargetConfig;
import com.fr.plugin.file.submit.oss.script.OssSubmitJavaScript;
import java.awt.*;
public class OssSubmitJavaScriptPane extends FurtherBasicBeanPane<OssSubmitJavaScript> {
private UITableEditorPane<OssSubmitTarget> tableEditorPane;
public OssSubmitJavaScriptPane() {
initComponents();
}
private void initComponents() {
setLayout(new BorderLayout());
this.tableEditorPane = new UITableEditorPane<OssSubmitTarget>(new OssSubmitPathTableModel());
this.add(tableEditorPane, BorderLayout.CENTER);
}
@Override
public boolean accept(Object o) {
return o instanceof OssSubmitJavaScript;
@ -17,17 +35,30 @@ public class OssSubmitJavaScriptPane extends FurtherBasicBeanPane<OssSubmitJavaS
}
@Override
public void populateBean(OssSubmitJavaScript ossSubmitJavaScript) {
public void populateBean(OssSubmitJavaScript javaScript) {
if (javaScript != null) {
OssSubmitTargetConfig submitTargetConfig = javaScript.getSubmitTargetConfig();
if (submitTargetConfig != null) {
OssSubmitTarget[] targets = submitTargetConfig.getSubmitTargets();
this.tableEditorPane.populate(targets);
}
}
}
@Override
public OssSubmitJavaScript updateBean() {
return null;
OssSubmitJavaScript javaScript = new OssSubmitJavaScript();
OssSubmitTargetConfig submitTargetConfig = new OssSubmitTargetConfig();
java.util.List<OssSubmitTarget> targetList = tableEditorPane.update();
for (OssSubmitTarget target : targetList) {
submitTargetConfig.addSubmitTarget(target);
}
javaScript.setSubmitTargetConfig(submitTargetConfig);
return javaScript;
}
@Override
public String title4PopupWindow() {
return Toolkit.i18nText("Plugin-File_Submit_OSS");
return Toolkit.i18nText("Plugin-File_Submit_Oss");
}
}

17
src/main/java/com/fr/plugin/file/submit/oss/ui/tools/RewriteWorker.java

@ -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);
}
}

2
src/main/resources/com/fr/plugin/file/submit/oss/submit_zh_CN.properties

@ -1,4 +1,4 @@
Plugin-File_Submit_Oss=\u4E0A\u4F20\u81F3OSS
Plugin-File_Submit_Oss=\u6587\u4EF6\u4E0A\u4F20\uFF08OSS\uFF09
Plugin-File_Submit_Oss_Bucket_Name=Bucket\u540D\u5B57
Plugin-File_Submit_Oss_Directory=\u76EE\u5F55
Plugin-File_Submit_Oss_File=\u6587\u4EF6

61
src/main/resources/com/fr/plugin/file/submit/oss/web/oss.js

@ -1,3 +1,64 @@
FR.OssForm = FR.extend(FR.Form, {
/**
* 上传到OSS服务器
*/
ossCommit: function (o, target) {
var self = this;
if (_g().saveReport) {
_g().saveReport((function () {
this.formSubmit({
url: FR.fineServletURL + "/url/oss/upload",
data: {
cmd: o.cmd,
location: target
? target.options.location
: null,
sheetNum: _g().selectedIndex,
xmlconf: o.xmlconf,
sessionID: _g().currentSessionID || _g().sessionID
},
callback: function (res) {
self.showResult(res);
}
});
}).createDelegate(this));
} else {
this.formSubmit({
url: FR.fineServletURL + "/url/oss/upload",
data: {
xmlconf: o.xmlconf,
cmd: o.cmd,
sessionID: _g().currentSessionID || _g().sessionID
},
callback: function (res) {
self.showResult(res);
}
});
}
},
showResult: function (res) {
var submitInfo;
var json_array = FR.jsonDecode(res.responseText);
if (json_array.length > 0) {
$.each(json_array, function (idx, item) {
if (item.oss_submit_info) {
submitInfo = item.oss_submit_info;
}
});
}
var success = submitInfo.success;
if (success !== true && success !== false) {
FR.Msg.toast(FR.i18nText("Fine-Engine_Report") + FR.i18nText("Fine-Engine_Report_Write_Attributes_Msg"));
} else if (success === true) {
FR.Msg.toast(FR.i18nText("Fine-Engine_Report_Successfully"));
} else if (success === false) {
FR.Msg.toast(FR.i18nText("Fine-Engine_Failed") + ":" + submitInfo.errorMsg);
}
}
});
$.extend(FR, {
doHyperlinkByOssDownload: function (data) {

Loading…
Cancel
Save