Skip to content

Commit

Permalink
Merge pull request #27 from ONLYOFFICE/develop
Browse files Browse the repository at this point in the history
Release/2.4.0
  • Loading branch information
LinneyS authored Jan 18, 2021
2 parents efc01bb + 6fbe365 commit c506eff
Show file tree
Hide file tree
Showing 34 changed files with 770 additions and 69 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "src/main/resources/app_data"]
path = src/main/resources/app_data
url = https://github.com/ONLYOFFICE/document-templates
branch = main/new
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Change Log

# 2.4.0
## Added
- Ability to create documents
- Added connection to a demo document server
- Button to hide/show Secret key
- Button to open file location

## Changed
- Moved jwt-header parameter to confluence.cfg.xml file

## 2.3.0
## Changed
- Apache license
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>onlyoffice</groupId>
<artifactId>onlyoffice-confluence-plugin</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>

<organization>
<name>Ascensio System SIA</name>
Expand Down
79 changes: 78 additions & 1 deletion src/main/java/onlyoffice/ConfigurationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,38 @@

package onlyoffice;

import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;

import javax.inject.Inject;
import javax.inject.Named;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Named
public class ConfigurationManager {
@ComponentImport
private final PluginSettingsFactory pluginSettingsFactory;

private final PluginSettings pluginSettings;
private Map<String, String> demoData;

@Inject
public ConfigurationManager(PluginSettingsFactory pluginSettingsFactory) {
this.pluginSettingsFactory = pluginSettingsFactory;
pluginSettings = pluginSettingsFactory.createGlobalSettings();

demoData = new HashMap<String, String>();
demoData.put("url", "https://onlinedocs.onlyoffice.com/");
demoData.put("header", "AuthorizationJWT");
demoData.put("secret", "sn2puSUF7muF5Jas");
demoData.put("trial", "30");
}

public Properties GetProperties() throws IOException {
Properties properties = new Properties();
Expand All @@ -32,4 +59,54 @@ public Properties GetProperties() throws IOException {
}
return properties;
}

public boolean selectDemo(Boolean demo) {
pluginSettings.put("onlyoffice.demo", demo.toString());
if (demo) {
String demoStart = (String) pluginSettings.get("onlyoffice.demoStart");
if (demoStart == null || demoStart.isEmpty()) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
pluginSettings.put("onlyoffice.demoStart", dateFormat.format(date));
}
return true;
}
return false;
}

public Boolean demoEnabled() {
String demo = (String) pluginSettings.get("onlyoffice.demo");
if (demo == null || demo.isEmpty()) {
demo = "false";
}
return Boolean.parseBoolean(demo);
}

public Boolean demoAvailable(Boolean forActivate) {
String demoStart = (String) pluginSettings.get("onlyoffice.demoStart");
if (demoStart != null && !demoStart.isEmpty()) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
Calendar date = Calendar.getInstance();
date.setTime(dateFormat.parse(demoStart));
date.add(Calendar.DATE, Integer.parseInt(demoData.get("trial")));
if (date.after(Calendar.getInstance())) {
return true;
}else {
return false;
}
} catch (ParseException e) {
e.printStackTrace();
}
}
return forActivate;
}

public Boolean demoActive() {
return demoEnabled() && demoAvailable(false);
}

public String getDemo(String key) {
return demoData.get(key);
}
}
11 changes: 7 additions & 4 deletions src/main/java/onlyoffice/ConvertManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ public class ConvertManager {

private final UrlManager urlManager;
private final JwtManager jwtManager;
private final ConfigurationManager configurationManager;

@Inject
public ConvertManager(UrlManager urlManager, JwtManager jwtManager) {
public ConvertManager(UrlManager urlManager, JwtManager jwtManager, ConfigurationManager configurationManager) {
this.urlManager = urlManager;
this.jwtManager = jwtManager;
this.configurationManager = configurationManager;
}

public static boolean isConvertable(String ext) {
Expand All @@ -58,7 +60,7 @@ public String convertsTo(String ext) {
return convertableDict.getOrDefault(trimDot(ext), null);
}

public String getMimeType(String ext) {
public static String getMimeType(String ext) {
return mimeTypes.getOrDefault(trimDot(ext), null);
}

Expand Down Expand Up @@ -101,7 +103,7 @@ public JSONObject convert(Long attachmentId, String ext) throws Exception {

StringEntity requestEntity = new StringEntity(body.toString(), ContentType.APPLICATION_JSON);
HttpPost request = new HttpPost(urlManager.getInnerDocEditorUrl()
+ new ConfigurationManager().GetProperties().getProperty("files.docservice.url.convert"));
+ configurationManager.GetProperties().getProperty("files.docservice.url.convert"));
request.setEntity(requestEntity);
request.setHeader("Accept", "application/json");

Expand All @@ -111,7 +113,8 @@ public JSONObject convert(Long attachmentId, String ext) throws Exception {
payloadBody.put("payload", body);
String headerToken = jwtManager.createToken(body);
body.put("token", token);
request.setHeader("Authorization", "Bearer " + headerToken);
String header = jwtManager.getJwtHeader();
request.setHeader(header, "Bearer " + headerToken);
}

log.debug("Sending POST to Docserver: " + body.toString());
Expand Down
113 changes: 100 additions & 13 deletions src/main/java/onlyoffice/DocumentManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,47 @@

package onlyoffice;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.*;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Properties;

import java.util.*;
import com.atlassian.confluence.core.ContentEntityManager;
import com.atlassian.confluence.core.ContentEntityObject;
import com.atlassian.confluence.languages.LocaleManager;
import com.atlassian.confluence.pages.Attachment;
import com.atlassian.confluence.pages.AttachmentManager;
import com.atlassian.confluence.pages.Page;
import com.atlassian.confluence.pages.PageManager;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.confluence.user.ConfluenceUser;
import com.atlassian.plugin.PluginAccessor;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.sal.api.message.I18nResolver;
import com.atlassian.spring.container.ContainerManager;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.commons.codec.binary.Hex;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class DocumentManager {
@ComponentImport
private static I18nResolver i18n;

private static ConfigurationManager configurationManager;

@Inject
public DocumentManager(I18nResolver i18n, ConfigurationManager configurationManager) {
this.i18n = i18n;
this.configurationManager = configurationManager;
}

private static final Logger log = LogManager.getLogger("onlyoffice.DocumentManager");

public static long GetMaxFileSize() {
long size;
try {
ConfigurationManager configurationManager = new ConfigurationManager();
Properties properties = configurationManager.GetProperties();
String filesizeMax = properties.getProperty("filesize-max");
size = Long.parseLong(filesizeMax);
Expand All @@ -51,7 +71,6 @@ public static long GetMaxFileSize() {

public static List<String> GetEditedExts() {
try {
ConfigurationManager configurationManager = new ConfigurationManager();
Properties properties = configurationManager.GetProperties();
String exts = properties.getProperty("files.docservice.edited-docs");

Expand Down Expand Up @@ -83,7 +102,6 @@ private static String GenerateRevisionId(String expectedKey) {

public static String CreateHash(String str) {
try {
ConfigurationManager configurationManager = new ConfigurationManager();
Properties properties = configurationManager.GetProperties();
String secret = properties.getProperty("files.docservice.secret");

Expand All @@ -101,7 +119,6 @@ public static String ReadHash(String base64) {
try {
String str = new String(Base64.getDecoder().decode(base64), "UTF-8");

ConfigurationManager configurationManager = new ConfigurationManager();
Properties properties = configurationManager.GetProperties();
String secret = properties.getProperty("files.docservice.secret");

Expand Down Expand Up @@ -129,4 +146,74 @@ private static String GetHashHex(String str) {
}
return "";
}

private static String GetCorrectName(String fileName, String fileExt, Long pageID) {
ContentEntityManager contentEntityManager = (ContentEntityManager) ContainerManager.getComponent("contentEntityManager");
AttachmentManager attachmentManager = (AttachmentManager) ContainerManager.getComponent("attachmentManager");
ContentEntityObject contentEntityObject = contentEntityManager.getById(pageID);

List<Attachment> Attachments = attachmentManager.getLatestVersionsOfAttachments(contentEntityObject);
String name = (fileName + "." + fileExt).replaceAll("[*?:\"<>/|\\\\]","_");
int count = 0;
Boolean flag = true;

while(flag) {
flag = false;
for (Attachment attachment : Attachments) {
if (attachment.getFileName().equals(name)) {
count++;
name = fileName + " (" + count + ")." + fileExt;
flag = true;
break;
}
}
}

return name;
}

private static InputStream GetDemoFile(ConfluenceUser user, String fileExt) {
LocaleManager localeManager = (LocaleManager) ContainerManager.getComponent("localeManager");
PluginAccessor pluginAccessor = (PluginAccessor) ContainerManager.getComponent("pluginAccessor");

String pathToDemoFile = "app_data/" + localeManager.getLocale(user).toString().replace("_", "-");

if (pluginAccessor.getDynamicResourceAsStream(pathToDemoFile) == null) {
pathToDemoFile = "app_data/en-US";
}

return pluginAccessor.getDynamicResourceAsStream(pathToDemoFile + "/new." + fileExt);
}

public static Long createDemo(String fileName, String fileExt, Long pageID) {
Attachment attachment = null;
try {
ConfluenceUser confluenceUser = AuthenticatedUserThreadLocal.get();
PageManager pageManager = (PageManager) ContainerManager.getComponent("pageManager");
AttachmentManager attachmentManager = (AttachmentManager) ContainerManager.getComponent("attachmentManager");

fileExt = fileExt == null || !fileExt.equals("xlsx") && !fileExt.equals("pptx") ? "docx" : fileExt.trim();
fileName = fileName == null || fileName.equals("") ? i18n.getText("onlyoffice.connector.dialog-filecreate." + fileExt) : fileName;

Date date = Calendar.getInstance().getTime();

InputStream demoFile = GetDemoFile(confluenceUser, fileExt);

fileName = GetCorrectName(fileName, fileExt, pageID);

Page page = pageManager.getPage(pageID);
attachment = new Attachment(fileName, ConvertManager.getMimeType(fileExt), demoFile.available(), "");
attachment.setCreator(confluenceUser);
attachment.setCreationDate(date);
attachment.setLastModificationDate(date);
attachment.setContainer(pageManager.getPage(pageID));

attachmentManager.saveAttachment(attachment, null, demoFile);
page.addAttachment(attachment);
} catch (Exception ex) {
log.error(ex);
}

return attachment.getContentId().asLong();
}
}
66 changes: 66 additions & 0 deletions src/main/java/onlyoffice/IsOfficePageAttachments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
*
* (c) Copyright Ascensio System SIA 2020
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package onlyoffice;

import com.atlassian.confluence.pages.Page;
import com.atlassian.confluence.pages.PageManager;
import com.atlassian.confluence.security.Permission;
import com.atlassian.confluence.security.PermissionManager;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.confluence.user.ConfluenceUser;
import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.Condition;
import com.atlassian.spring.container.ContainerManager;
import com.opensymphony.webwork.ServletActionContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IsOfficePageAttachments implements Condition {
private String pageAttachments = "viewpageattachments.action";

public void init(Map<String, String> map) throws PluginParseException {

}

public boolean shouldDisplay(Map<String, Object> context) {
HttpServletRequest request = ServletActionContext.getRequest();

if (request != null){
String uri = request.getServletPath();
Pattern pattern = Pattern.compile(".*/" + pageAttachments + ".*");
Matcher matcher = pattern.matcher(uri);

String pageId = request.getParameter("pageId");
boolean access = false;
if (pageId != null){
ConfluenceUser user = AuthenticatedUserThreadLocal.get();
PermissionManager permissionManager = (PermissionManager) ContainerManager.getComponent("permissionManager");
PageManager pageManager = (PageManager) ContainerManager.getComponent("pageManager");

Page page = pageManager.getPage(Long.parseLong(pageId));
access = permissionManager.hasPermission(user, Permission.EDIT, page);
}
return matcher.matches() && access;
}else {
return false;
}
}
}
Loading

0 comments on commit c506eff

Please sign in to comment.