From 41b8259f1a9931ef19419d988805dad0cf26909d Mon Sep 17 00:00:00 2001
From: Moritzjenny <moritzjenny@hotmail.com>
Date: Mon, 13 Jul 2020 14:42:46 +0200
Subject: [PATCH] OO-4803: Expand doceditor REST webservice

---
 .../restapi/DocEditorSessionWebService.java   |  74 +++++++++++++
 .../restapi/DocEditorStatisticsVO.java        |  73 +++++++++++++
 .../restapi/DocEditorWebService.java          |  51 +++++++++
 .../olat/restapi/DocEditorWebServiceTest.java | 100 ++++++++++++++++++
 .../java/org/olat/test/AllTestsJunit4.java    |   1 +
 5 files changed, 299 insertions(+)
 create mode 100644 src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorSessionWebService.java
 create mode 100644 src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorStatisticsVO.java
 create mode 100644 src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorWebService.java
 create mode 100644 src/test/java/org/olat/restapi/DocEditorWebServiceTest.java

diff --git a/src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorSessionWebService.java b/src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorSessionWebService.java
new file mode 100644
index 00000000000..da582797eec
--- /dev/null
+++ b/src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorSessionWebService.java
@@ -0,0 +1,74 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+
+package org.olat.core.commons.services.doceditor.restapi;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.olat.core.CoreSpringFactory;
+import org.olat.core.commons.services.doceditor.DocEditor.Mode;
+import org.olat.core.commons.services.doceditor.wopi.WopiService;
+import org.springframework.stereotype.Component;
+
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.media.Content;
+import io.swagger.v3.oas.annotations.media.Schema;
+import io.swagger.v3.oas.annotations.responses.ApiResponse;
+
+/**
+ * Description:<br>
+ * This serves information about the document editors.
+ * 
+ * Initial date: 09 Jul. 2020<br>
+ * @author morjen, moritz.jenny@frentix.com, http://www.frentix.com
+ *
+ */
+
+@Component
+public class DocEditorSessionWebService {
+
+	@GET
+	@Path("{app}")
+	@Operation(summary = "Returns information about doceditor", description = "Returns information about the doceditor given as parameter")
+	@ApiResponse(responseCode = "200", description = "Information about the doceditor", content = {
+			@Content(mediaType = "application/json", schema = @Schema(implementation = DocEditorStatisticsVO.class)),
+			@Content(mediaType = "application/xml", schema = @Schema(implementation = DocEditorStatisticsVO.class)) })
+	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
+		
+	public Response getStatistics(@PathParam("app") String app) {
+		DocEditorStatisticsVO stats = getDocEditorStatistics(app);
+		return Response.ok(stats).build();
+	}	
+	
+	private DocEditorStatisticsVO getDocEditorStatistics(String app) {
+		DocEditorStatisticsVO stats = new DocEditorStatisticsVO();
+		WopiService wopiService = CoreSpringFactory.getImpl(WopiService.class);		
+		stats.setAppName(app);
+		stats.setOpenDocumentsWrite(wopiService.getAccessCount(app, Mode.EDIT));
+		stats.setOpenDocumentsRead(wopiService.getAccessCount(app, Mode.VIEW));
+		return stats;
+	}	
+
+}
diff --git a/src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorStatisticsVO.java b/src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorStatisticsVO.java
new file mode 100644
index 00000000000..0abd75f7cdf
--- /dev/null
+++ b/src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorStatisticsVO.java
@@ -0,0 +1,73 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+
+
+package org.olat.core.commons.services.doceditor.restapi;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+/**
+ * Description:<br>
+ * This VO holds statistics about a doceditor.
+ * 
+ * Initial date: 13 Jul. 2020<br>
+ * @author morjen, moritz.jenny@frentix.com, http://www.frentix.com
+ *
+ */
+
+@XmlAccessorType(XmlAccessType.FIELD)
+@XmlRootElement(name = "DocEditorStatisticsVO")
+public class DocEditorStatisticsVO {
+
+	@XmlAttribute(name="appName", required=false)
+	private String appName;
+	
+	@XmlAttribute(name="openDocumentsRead", required=false)
+	private long openDocumentsRead;
+	
+	@XmlAttribute(name="openDocumentsWrite", required=false)
+	private long openDocumentsWrite;
+
+	
+	public String getAppName() {
+		return appName;
+	}
+	public void setAppName(String appName) {
+		this.appName = appName;
+	}
+	public long getOpenDocumentsRead() {
+		return openDocumentsRead;
+	}
+	public void setOpenDocumentsRead(long openDocumentsRead) {
+		this.openDocumentsRead = openDocumentsRead;
+	}
+	public long getOpenDocumentsWrite() {
+		return openDocumentsWrite;
+	}
+	public void setOpenDocumentsWrite(long openDocumentsWrite) {
+		this.openDocumentsWrite = openDocumentsWrite;
+	}
+	
+	
+
+}
diff --git a/src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorWebService.java b/src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorWebService.java
new file mode 100644
index 00000000000..edaf8a9c7f4
--- /dev/null
+++ b/src/main/java/org/olat/core/commons/services/doceditor/restapi/DocEditorWebService.java
@@ -0,0 +1,51 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+
+package org.olat.core.commons.services.doceditor.restapi;
+
+import javax.ws.rs.Path;
+
+import org.springframework.stereotype.Component;
+
+import io.swagger.v3.oas.annotations.tags.Tag;
+
+/**
+ * Description:<br>
+ * This serves information about the document editors.
+ * 
+ * Initial date: 09 Jul. 2020<br>
+ * @author morjen, moritz.jenny@frentix.com, http://www.frentix.com
+ *
+ */
+@Tag(name = "Document Editor")
+@Path("doceditor")
+@Component
+public class DocEditorWebService {
+	
+	private static final DocEditorSessionWebService docEditorSessionWebService = new DocEditorSessionWebService();
+
+	
+	@Path("sessions")
+	public DocEditorSessionWebService getStatus() {
+		return docEditorSessionWebService;
+	}
+	
+	
+}
diff --git a/src/test/java/org/olat/restapi/DocEditorWebServiceTest.java b/src/test/java/org/olat/restapi/DocEditorWebServiceTest.java
new file mode 100644
index 00000000000..0d4f36f8214
--- /dev/null
+++ b/src/test/java/org/olat/restapi/DocEditorWebServiceTest.java
@@ -0,0 +1,100 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.restapi;
+
+import static org.olat.test.JunitTestHelper.random;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Date;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.UriBuilder;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.junit.Assert;
+import org.junit.Test;
+import org.olat.core.commons.persistence.DB;
+import org.olat.core.commons.services.doceditor.DocEditor.Mode;
+import org.olat.core.commons.services.doceditor.DocEditorSecurityCallback;
+import org.olat.core.commons.services.doceditor.DocEditorSecurityCallbackBuilder;
+import org.olat.core.commons.services.doceditor.restapi.DocEditorStatisticsVO;
+import org.olat.core.commons.services.doceditor.wopi.Access;
+import org.olat.core.commons.services.doceditor.wopi.WopiService;
+import org.olat.core.commons.services.vfs.VFSMetadata;
+import org.olat.core.commons.services.vfs.manager.VFSMetadataDAO;
+import org.olat.core.id.Identity;
+import org.olat.test.JunitTestHelper;
+import org.olat.test.OlatRestTestCase;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * 
+ * Initial date: 13 jul. 2020<br>
+ * @author mjenny, moritz.jenny@frentix.com, http://www.frentix.com
+ *
+ */
+public class DocEditorWebServiceTest extends OlatRestTestCase {
+	
+	@Autowired
+	private DB dbInstance;
+	@Autowired
+	private VFSMetadataDAO vfsMetadataDAO;
+	@Autowired
+	private WopiService sut;
+
+	@Test
+	public void docEditorSessionQuery()
+	throws IOException, URISyntaxException {
+		
+		RestConnection conn = new RestConnection();
+		Assert.assertTrue(conn.login("administrator", "openolat"));
+		
+		VFSMetadata metadata = randomMetadata();
+		Identity identity = JunitTestHelper.createAndPersistIdentityAsRndUser("wopi");
+		DocEditorSecurityCallback secCallback = DocEditorSecurityCallbackBuilder.builder().withMode(Mode.EDIT).build();
+		VFSMetadata metadata2 = randomMetadata();
+		Identity identity2 = JunitTestHelper.createAndPersistIdentityAsRndUser("wopi2");
+		DocEditorSecurityCallback secCallback2 = DocEditorSecurityCallbackBuilder.builder().withMode(Mode.VIEW).build();
+		Access access1 = sut.getOrCreateAccess(metadata, identity, secCallback, "App1", null);
+		dbInstance.commitAndCloseSession();	
+		Access access2 = sut.getOrCreateAccess(metadata2, identity2, secCallback2, "App1", null);
+		dbInstance.commitAndCloseSession();	
+		
+		Assert.assertNotNull(access1);
+		Assert.assertNotNull(access2);
+		
+		URI request = UriBuilder.fromUri(getContextURI()).path("doceditor").path("sessions").path("App1").build();
+		HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
+		HttpResponse response = conn.execute(method);
+		Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+		
+		DocEditorStatisticsVO docEditorStatisticsVO = conn.parse(response, DocEditorStatisticsVO.class);
+		
+		Assert.assertEquals(1, docEditorStatisticsVO.getOpenDocumentsRead());
+		Assert.assertEquals(1, docEditorStatisticsVO.getOpenDocumentsWrite());
+	}
+	
+	private VFSMetadata randomMetadata() {
+		return vfsMetadataDAO.createMetadata(random(), random(), random(), new Date(), 1000l, false, "file://" + random(), "file", null);
+	}
+}
diff --git a/src/test/java/org/olat/test/AllTestsJunit4.java b/src/test/java/org/olat/test/AllTestsJunit4.java
index fcbbdfe5ceb..511e9622556 100644
--- a/src/test/java/org/olat/test/AllTestsJunit4.java
+++ b/src/test/java/org/olat/test/AllTestsJunit4.java
@@ -413,6 +413,7 @@ import org.junit.runners.Suite;
 	org.olat.restapi.CurriculumsWebServiceTest.class,
 	org.olat.restapi.CurriculumElementsWebServiceTest.class,
 	org.olat.restapi.CurriculumElementTypesWebServiceTest.class,
+	org.olat.restapi.DocEditorWebServiceTest.class,
 	org.olat.restapi.EfficiencyStatementTest.class,
 	org.olat.restapi.FolderTest.class,
 	org.olat.restapi.ForumTest.class,
-- 
GitLab