Skip to content

Commit

Permalink
Add support for downloading important files xwikisas#8
Browse files Browse the repository at this point in the history
 * Created a JS listener for button events
 * UI optimizations
 * Moved the modals from AdminTools.WebHome to Macro.vm
 * Created macros for the modals
 * Modified translations
  • Loading branch information
ChiuchiuSorin committed Oct 12, 2023
1 parent b5499b7 commit 07628bf
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*/
package com.xwiki.admintools.rest;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
Expand Down Expand Up @@ -54,17 +54,18 @@ public interface AdminToolsResource extends XWikiRestComponent
* @return an archive with files.
* @throws XWikiRestException if an error occurred while getting the archive.
*/
@POST
@GET
@Path("/files")
Response getFiles() throws XWikiRestException;

/**
* Get last n lines of server logs.
*
* @param noLines number of lines to be retrieved from the log file.
* @return a file containing the last n lines of server logs.
* @throws XWikiRestException if an error occurred while retrieving the logs file .
*/
@POST
@Path("/files/logs")
Response getLastLogs() throws XWikiRestException;
@GET
@Path("/files/logs/{noLines}")
Response getLastLogs(@PathParam("noLines") @DefaultValue("1000") String noLines) throws XWikiRestException;
}
2 changes: 1 addition & 1 deletion application-admintools-default/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@
</dependency>
</dependencies>
<properties>
<xwiki.jacoco.instructionRatio>0.66</xwiki.jacoco.instructionRatio>
<xwiki.jacoco.instructionRatio>0.81</xwiki.jacoco.instructionRatio>
</properties>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,14 @@ public Response getFiles()
}

@Override
public Response getLastLogs()
public Response getLastLogs(String input)
{
if (!isAdmin()) {
logger.warn("Failed to get the logs due to restricted rights.");
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
try {
XWikiContext wikiContext = xcontextProvider.get();
XWikiRequest xWikiRequest = wikiContext.getRequest();
String noLines = xWikiRequest.getParameter("noLines");
String noLines = input;
if (noLines == null || noLines.equals("")) {
noLines = "1000";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
## Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
## 02110-1301 USA, or see the FSF site: http://www.fsf.org.
## ---------------------------------------------------------------------------
#viewLastNLinesMoldal("configuration")
{{html clean='false'}}

#if ($configuration['serverFound'] == 'true')
<div class="adminToolsDashboardItem">
<h2>$services.icon.renderHTML('world')
Expand All @@ -27,39 +27,39 @@
<hr/>
<ul>
<li>
$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.xwiki.version'))
$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.xwiki.version')):
$configuration['xwikiVersion']
</li>
<ul>
<li>
<a href="#viewLastNLinesModal" data-toggle="modal" data-target="#viewLastNLinesModal">
<a href="#configurationViewLastNLinesModal" data-toggle="modal" data-target="#configurationViewLastNLinesModal">
$escapetool.xml($services.localization.render('adminTools.dashboard.showLogs'))</a>
</li>
</ul>
<li>
$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.java'))
$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.java')):
$configuration['javaVersion']
</li>
<li>
$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.usedServer'))
$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.usedServer')):
$configuration['usedServer']
</li>
<li>$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.os.legend'))
<ul>
<li>$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.os.name'))
<li>$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.os.name')):
$configuration['osName']</li>
<li>$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.os.version'))
<li>$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.os.version')):
$configuration['osVersion']</li>
<li>$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.os.architecture'))
<li>$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.os.architecture')):
$configuration['osArch']</li>
</ul>
</li>
#if ($configuration['database'] != $null)
<li>$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.usedDB'))
<li>$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.usedDB')):
$configuration['database']</li>
#else
#set($warningDatabaseMessage = $escapetool.xml(
$services.localization.render('adminTools.dashboard.section.backend.supportedDB.error')))
$services.localization.render('adminTools.dashboard.section.backend.supportedDB.error')) + ':')
#set($warningDatabaseMessage = $warningDatabaseMessage +
$stringtool.join($services.admintools.getSupportedDatabases(), ', '))
<li>$escapetool.xml(
Expand All @@ -68,15 +68,13 @@
#end
</ul>
</div>

#else
#set($warningMessage =
$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.configuration.error'))+ '<br>')
#set($warningMessage = $warningMessage +
$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.supportedServers.error'))
$escapetool.xml($services.localization.render('adminTools.dashboard.section.backend.supportedServers.error')) + ':'
+ $stringtool.join($services.admintools.getSupportedServers(), ', '))

#warning($warningMessage)
#end

{{/html}}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
## 02110-1301 USA, or see the FSF site: http://www.fsf.org.
## ---------------------------------------------------------------------------
#set($adminToolsFilesPath = "$request.getContextPath()/rest/admintools/files")
#viewLastNLinesMoldal("files")
#downloadFiles("files")
{{html clean='false'}}
#if ($found == 'true')
<div class="adminToolsDashboardItem">
Expand All @@ -27,7 +29,7 @@
<hr/>
<ul>
<li>
<a href="#downloadFilesModal" data-toggle="modal" data-target="#downloadFilesModal">
<a href="#filesDownloadFilesModal" data-toggle="modal" data-target="#filesDownloadFilesModal">
$escapetool.xml($services.localization.render('adminTools.dashboard.section.download.modal.open'))</a>
</li>
<li>
Expand All @@ -46,7 +48,7 @@
</a>
</li>
<li>
<a href="#viewLastNLinesModal" data-toggle="modal" data-target="#viewLastNLinesModal">
<a href="#filesViewLastNLinesModal" data-toggle="modal" data-target="#filesViewLastNLinesModal">
$escapetool.xml($services.localization.render('adminTools.dashboard.showLogs'))</a>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
## Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
## 02110-1301 USA, or see the FSF site: http://www.fsf.org.
## ---------------------------------------------------------------------------

{{html clean='false'}}

#if($security['serverFound'] == 'true')
<div class="adminToolsDashboardItem">
<h2>$services.icon.renderHTML('lock')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void getLastLogs() throws Exception
{
when(downloadManager.getFile(LogsDataResource.HINT, "30")).thenReturn(new byte[] { 2 });
when(xWikiRequest.getParameter("noLines")).thenReturn("30");
assertEquals(200, defaultAdminToolsResource.getLastLogs().getStatus());
assertEquals(200, defaultAdminToolsResource.getLastLogs("30").getStatus());
}

@Test
Expand All @@ -208,7 +208,7 @@ void getLastDownloadManagerError() throws Exception

when(downloadManager.getFile(LogsDataResource.HINT, "1000")).thenThrow(new IOException("FILE NOT FOUND"));

assertEquals(404, defaultAdminToolsResource.getLastLogs().getStatus());
assertEquals(404, defaultAdminToolsResource.getLastLogs("").getStatus());
verify(logger).warn("Could not retrieve logs from server. Root cause: [{}]", "IOException: FILE NOT FOUND");
}

Expand All @@ -220,7 +220,7 @@ void getLastLogsDownloadManagerError() throws Exception

when(downloadManager.getFile(LogsDataResource.HINT, "1000")).thenThrow(new Exception("INTERNAL ERROR"));
Exception exception = assertThrows(Exception.class, () -> {
this.defaultAdminToolsResource.getLastLogs();
this.defaultAdminToolsResource.getLastLogs(null);
});
assertEquals("HTTP 500 Internal Server Error", exception.getMessage());
verify(logger).warn("Failed to get logs. Root cause: [{}]", "Exception: INTERNAL ERROR");
Expand All @@ -234,7 +234,7 @@ void getLastLogsNotAdmin()

when(authorizationManager.hasAccess(Right.ADMIN, user, wikiReference)).thenReturn(false);
Exception exception = assertThrows(Exception.class, () -> {
this.defaultAdminToolsResource.getLastLogs();
this.defaultAdminToolsResource.getLastLogs("1000");
});
assertEquals("HTTP 401 Unauthorized", exception.getMessage());
verify(logger).warn("Failed to get the logs due to restricted rights.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?xml version='1.1' encoding='UTF-8'?>

<!--
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
<xwikidoc version="1.4" reference="AdminTools.Code.Macros" locale="">
<web>AdminTools.Code</web>
<name>Macros</name>
<language/>
<defaultLanguage/>
<translation>0</translation>
<creator>xwiki:XWiki.Admin</creator>
<parent>AdminTools.WebHome</parent>
<author>xwiki:XWiki.Admin</author>
<contentAuthor>xwiki:XWiki.Admin</contentAuthor>
<version>1.1</version>
<title>Macros</title>
<comment/>
<minorEdit>false</minorEdit>
<syntaxId>xwiki/2.1</syntaxId>
<hidden>true</hidden>
<content>{{velocity output="false"}}
#macro (viewLastNLinesMoldal $id)
#set ($modalId = $id + "ViewLastNLinesModal")
#set ($formId = $id + "LastNLogLinesForm")
#set ($inputId = $id + "NoLines")
{{html clean='false'}}
&lt;div class="modal fade" id="$modalId" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
&lt;div class="modal-dialog modal-sm" role="document">
&lt;div class="modal-content">
&lt;div class="logs-modal-content">
&lt;div class="modal-header">
&lt;button type="button" class="close" data-dismiss="modal" aria-label="Close">
&lt;span aria-hidden="true">&amp;times;&lt;/span>&lt;/button>
&lt;h1 class="modal-title" id="viewLastNLinesModalLabel">
$escapetool.xml($services.localization.render('adminTools.dashboard.logs.modal.title'))
&lt;/h1>
&lt;/div>
&lt;div class="modal-body">
&lt;form class="xform" id="$formId" action="$request.getContextPath()/rest/admintools/files/logs">
&lt;dl>
&lt;dt>
&lt;label for="noLines">$escapetool.xml($services.localization.render('adminTools.dashboard.logs.modal.nLines.label')) &lt;/label>
&lt;span class="xHint">max 50000&lt;/span>
&lt;/dt>
&lt;dd>&lt;input type="number" id="$inputId" name="noLines" placeholder="1000">&lt;/dd>
&lt;/dl>
&lt;/form>
&lt;/div>
&lt;div class="modal-footer">
&lt;button type="button" class="btn btn-primary">
$escapetool.xml($services.localization.render('adminTools.dashboard.logs.modal.submit'))&lt;/button>
&lt;button type="button" class="btn btn-default" data-dismiss="modal">
$escapetool.xml($services.localization.render('cancel'))&lt;/button>
&lt;/div>
&lt;/div>
&lt;/div>
&lt;/div>
&lt;/div>
{{/html}}
#end

#macro (downloadFiles $id)
#set ($modalId = $id + "DownloadFilesModal")
#set ($formId = $id + "DownloadForm")

{{html clean='false'}}
&lt;div class="modal fade" id="$modalId" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
&lt;div class="modal-dialog" role="document">
&lt;div class="modal-content">
&lt;div class="modal-header">
&lt;button type="button" class="close" data-dismiss="modal" aria-label="Close">
&lt;span aria-hidden="true">&amp;times;&lt;/span>&lt;/button>
&lt;h1 class="modal-title downloadFilesModalTitle">
$escapetool.xml($services.localization.render('adminTools.dashboard.download.modal.title'))
&lt;/h1>
&lt;/div>
&lt;div class="modal-body">
&lt;form id="$formId" action="$request.getContextPath()/rest/admintools/files">
&lt;div class="download-modal-content">
&lt;dl>
&lt;dt>&lt;label>&lt;input type="checkbox" name="files" value="xwikiConfig" checked>
$escapetool.xml($services.localization.render('adminTools.dashboard.download.modal.xwikiConfig.title'))
&lt;/label&gt;
&lt;/dt>
&lt;dt>&lt;label>&lt;input type="checkbox" name="files" value="xwikiProperties" checked>
$escapetool.xml($services.localization.render('adminTools.dashboard.download.modal.xwikiProperties.title'))
&lt;/label>
&lt;/dt>
&lt;dt>&lt;label>&lt;input type="checkbox" name="files" value="dataProviderResource" checked>
$escapetool.xml($services.localization.render('adminTools.dashboard.download.modal.provided'))&lt;/label>
&lt;/dt>
&lt;dt>&lt;label>&lt;input type="checkbox" name="files" value="logsDataResource" checked> Logs&lt;/label>&lt;/dt>
&lt;dt>
&lt;div class="dateFields">
&lt;label for="from">$escapetool.xml($services.localization.render('adminTools.dashboard.download.modal.date.from'))
&lt;/label> &lt;input type="date" id="from" name="from">
&lt;label for="to">$escapetool.xml($services.localization.render('adminTools.dashboard.download.modal.date.to'))
&lt;/label> &lt;input type="date" id="to" name="to">
&lt;/div>
&lt;/dt>
&lt;/dl>
&lt;/div>
&lt;/form>
&lt;/div>
&lt;div class="modal-footer">
&lt;button type="button" class="btn btn-primary">
$escapetool.xml($services.localization.render('adminTools.dashboard.download.modal.button'))&lt;/button>
&lt;button type="button" class="btn btn-default" data-dismiss="modal">
$escapetool.xml($services.localization.render('cancel'))&lt;/button>
&lt;/div>
&lt;/div>
&lt;/div>
&lt;/div>
{{/html}}
#end
{{/velocity}}</content>
</xwikidoc>
Loading

0 comments on commit 07628bf

Please sign in to comment.