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 template for downloads dashboard section
* Created modal for files download
* Created modal for log view
* Added hyperlinks to view xwiki.cfg and xwiki.properties files in browser
* Small CSS modifications
* Added translations
  • Loading branch information
ChiuchiuSorin committed Oct 6, 2023
1 parent 238f9e4 commit 562de33
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.xpn.xwiki.web.XWikiRequest;
import com.xwiki.admintools.DataProvider;
import com.xwiki.admintools.internal.data.identifiers.CurrentServer;
import com.xwiki.admintools.internal.download.DownloadManager;

/**
* Manages the data providers.
Expand All @@ -55,6 +56,9 @@ public class AdminToolsManager
@Inject
private CurrentServer currentServer;

@Inject
private DownloadManager downloadManager;

@Inject
private Provider<XWikiContext> xcontextProvider;
/**
Expand Down Expand Up @@ -122,4 +126,14 @@ public String getContextPath()
return xWikiRequest.getContextPath();
}

/**
* Get the rendered template for accessing the downloads UI.
*
* @return a {@link String} representation of the template.
*/
public String getDownloadTemplate()
{
return this.downloadManager.renderTemplate();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.script.ScriptContext;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.script.ScriptContextManager;
import org.xwiki.template.TemplateManager;

import com.xwiki.admintools.download.DataResource;
import com.xwiki.admintools.internal.data.identifiers.CurrentServer;
import com.xwiki.admintools.internal.download.resources.LogsDataResource;

/**
Expand All @@ -55,6 +59,15 @@ public class DownloadManager
@Inject
private Provider<List<DataResource>> dataResources;

@Inject
private TemplateManager templateManager;

@Inject
private ScriptContextManager scriptContextManager;

@Inject
private CurrentServer currentServer;

@Inject
private Logger logger;

Expand Down Expand Up @@ -110,6 +123,28 @@ public byte[] downloadMultipleFiles(Map<String, String[]> request)
}
}

/**
* Get the data in a format given by the associated template.
*
* @return the rendered template as a {@link String}.
*/
public String renderTemplate()
{
try {
String found = "false";
if (currentServer.getCurrentServer() != null) {
found = "true";
}
ScriptContext scriptContext = this.scriptContextManager.getScriptContext();
scriptContext.setAttribute("found", found, ScriptContext.ENGINE_SCOPE);
return this.templateManager.render("downloadTemplate.vm");
} catch (Exception e) {
this.logger.warn("Failed to render custom template. Root cause is: [{}]",
ExceptionUtils.getRootCauseMessage(e));
return null;
}
}

private DataResource findDataResource(String hint)
{
for (DataResource archiverDataResource : dataResources.get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,14 @@ public String getContextPath()
{
return this.adminToolsManager.getContextPath();
}

/**
* Get the rendered template for accessing the downloads UI.
*
* @return a {@link String} representation of the template.
*/
public String getDownloadTemplate()
{
return this.adminToolsManager.getDownloadTemplate();
}
}
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.
## ---------------------------------------------------------------------------



{{html clean='false'}}

#if ($configuration['serverFound'] == 'true')
Expand All @@ -33,8 +35,8 @@
</li>
<ul>
<li>
<a href="Show-latest-logs">$escapetool.xml(
$services.localization.render('adminTools.dashboard.section.backend.showLogs'))</a>
<a href="#viewLogsModal" data-toggle="modal" data-target="#viewLogsModal">
$escapetool.xml($services.localization.render('adminTools.dashboard.showLogs'))</a>
</li>
</ul>
<li>
Expand Down Expand Up @@ -80,4 +82,33 @@
#warning($warningMessage)
#end

<div class="modal fade" id="viewLogsModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span></button>
<h1 class="modal-title" id="myModalLabel" style="text-align: center;">
$escapetool.xml($services.localization.render('adminTools.dashboard.modal.logs.title'))</h1>
</div>
<div class="modal-body" style="text-align: center;">
<form id="downloadForm" action="$services.admintools.getContextPath()/rest/admintools/files/logs"
method="POST" target="_blank">
<br>
<label>$escapetool.xml($services.localization.render('adminTools.dashboard.modal.logs.selection')) </label>
<input type="number" id="noLines" name="noLines" placeholder="100">
<br>
<br>
<br>
<input class="btn btn-primary btn-lg" type="submit"
value="$escapetool.xml($services.localization.render('adminTools.dashboard.modal.logs.button'))">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
$escapetool.xml($services.localization.render('adminTools.dashboard.modal.button.close'))</button>
</div>
</div>
</div>
</div>
{{/html}}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,123 @@
## 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 ($found == 'true')
<div class="adminToolsDashboardItem">
<h2>$services.icon.renderHTML('download')
$escapetool.xml($services.localization.render('adminTools.dashboard.section.download.title'))</h2>
<p>$escapetool.xml($services.localization.render('adminTools.dashboard.section.download.description'))</p>
<hr/>
<ul>
<li>
<a href="#downloadFilesModal" data-toggle="modal" data-target="#downloadFilesModal">
$escapetool.xml($services.localization.render('adminTools.dashboard.section.download.modal.open'))</a>
</li>
<li>
$escapetool.xml($services.localization.render('adminTools.dashboard.section.download.view.description'))
<ul>
<li>
<a href="$services.admintools.getContextPath()/rest/admintools/files/xwikiConfigFileDataResource"
target="_blank">
$escapetool.xml($services.localization.render('adminTools.dashboard.section.download.view.configuration'))
</a>
</li>
<li>
<a href="$services.admintools.getContextPath()/rest/admintools/files/xwikiPropertiesFileDataResource"
target="_blank">
$escapetool.xml($services.localization.render('adminTools.dashboard.section.download.view.properties'))
</a>
</li>
<li>
<a href="#viewLogsModal" data-toggle="modal" data-target="#viewLogsModal">
$escapetool.xml($services.localization.render('adminTools.dashboard.showLogs'))</a>
</li>
</ul>
</li>
</ul>

#warning($escapetool.xml($services.localization.render('adminTools.dashboard.section.download.warning')))

</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'))
+ $stringtool.join($services.admintools.getSupportedServers(), ', '))

#warning($warningMessage)
#end

<div class="modal fade" id="downloadFilesModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span></button>
<h1 class="modal-title" id="myModalLabel" style="text-align: center;">
$escapetool.xml($services.localization.render('adminTools.dashboard.modal.download.title'))</h1>
</div>
<div class="modal-body" style="text-align: center;">
<form id="downloadForm" action="$services.admintools.getContextPath()/rest/admintools/files" method="POST">
<label><input type="checkbox" name="files" value="xwikiConfigFileDataResource" checked> xwiki.cfg</label><br>
<label><input type="checkbox" name="files" value="xwikiPropertiesFileDataResource" checked>
xwiki.properties</label><br>
<label><input type="checkbox" name="files" value="dataProviderResource" checked>
$escapetool.xml($services.localization.render('adminTools.dashboard.modal.download.provided'))</label><br>
<label><input type="checkbox" name="files" value="logsDataResource" checked> Logs</label><br>
<div id="dateFields">
<label>$escapetool.xml($services.localization.render('adminTools.dashboard.modal.download.date.from'))
</label> <input type="date" id="from" name="from">
<label>$escapetool.xml($services.localization.render('adminTools.dashboard.modal.download.date.to'))
</label> <input type="date" id="to" name="to">
</div>
<br>
<br>
<input class="btn btn-primary btn-lg" type="submit"
value="$escapetool.xml($services.localization.render('adminTools.dashboard.modal.download.button'))">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
$escapetool.xml($services.localization.render('adminTools.dashboard.modal.button.close'))</button>
</div>
</div>
</div>
</div>

<div class="modal fade" id="viewLogsModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span></button>
<h1 class="modal-title" id="myModalLabel" style="text-align: center;">
$escapetool.xml($services.localization.render('adminTools.dashboard.modal.logs.title'))</h1>
</div>
<div class="modal-body" style="text-align: center;">
<form id="downloadForm" action="$services.admintools.getContextPath()/rest/admintools/files/logs"
method="POST" target="_blank">
<br>
<label>$escapetool.xml($services.localization.render('adminTools.dashboard.modal.logs.selection')) </label>
<input type="number" id="noLines" name="noLines" placeholder="100">
<br>
<br>
<br>
<input class="btn btn-primary btn-lg" type="submit"
value="$escapetool.xml($services.localization.render('adminTools.dashboard.modal.logs.button'))">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
$escapetool.xml($services.localization.render('adminTools.dashboard.modal.button.close'))</button>
</div>
</div>
</div>
</div>
{{/html}}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,30 @@
AdminTools.Code.ConfigurationClass_serverLocation=Server location
AdminTools.Code.ConfigurationClass_serverLocation.hint=Path to the server location
admin.admintools.description=Configure Admin Tools Application
adminTools.dashboard.modal.button.close=Close
adminTools.dashboard.modal.logs.title=Show the latest logs
adminTools.dashboard.modal.logs.selection=Insert the number of lines to show:
adminTools.dashboard.modal.logs.button=View logs
adminTools.dashboard.modal.download.title=Select Files to Download
adminTools.dashboard.modal.download.provided=Configuration info
adminTools.dashboard.modal.download.date.from=From:
adminTools.dashboard.modal.download.date.to=To:
adminTools.dashboard.modal.download.button=Download selected files
adminTools.dashboard.section.download.description=Get the files you need. View them directly in browser or download them as an archive.
adminTools.dashboard.section.download.title=Important files
adminTools.dashboard.section.download.modal.open=Export files needed for getting support
adminTools.dashboard.section.download.view.description=View files content:
adminTools.dashboard.section.download.view.configuration=XWiki configuration file
adminTools.dashboard.section.download.view.properties=XWiki properties file
adminTools.dashboard.section.download.warning=Before downloading and sending the files, please make sure you introduced the lines that may contain sensitive info in Admin Tools configuration page!
adminTools.dashboard.section.backend.configuration.error=Server path was not found. Please manually add the path to the server on the Admin Tools configuration page.
adminTools.dashboard.section.backend.description=Get info about your backend
adminTools.dashboard.section.backend.java=Detected java version:
adminTools.dashboard.section.backend.os.legend=OS info:
adminTools.dashboard.section.backend.os.architecture=OS architecture:
adminTools.dashboard.section.backend.os.name=OS name:
adminTools.dashboard.section.backend.os.version=OS version:
adminTools.dashboard.section.backend.showLogs=Show latest logs
adminTools.dashboard.showLogs=Show the latest logs
adminTools.dashboard.section.backend.supportedDB.error=Database not found. Currently supported databases:
adminTools.dashboard.section.backend.supportedServers.error=Currently supported servers:
adminTools.dashboard.section.backend.title=Backend
Expand Down
Loading

0 comments on commit 562de33

Please sign in to comment.