Skip to content

Commit

Permalink
Add support for downloading important files xwikisas#8
Browse files Browse the repository at this point in the history
* Refactored functions
* Edited comments
* Moved modals from templates to AdminTools.WebHome
* Edited translations
* Refactored templates
  • Loading branch information
ChiuchiuSorin committed Oct 9, 2023
1 parent f8acc5f commit f7cae01
Show file tree
Hide file tree
Showing 19 changed files with 248 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public interface ServerIdentifier
*
* @return {@link String} path to server log file.
*/
String getLogFilePath();
String getLastLogFilePath();

/**
* Get server pattern for identifying log files date.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package com.xwiki.admintools.configuration;

import java.util.List;

import org.xwiki.component.annotation.Role;
import org.xwiki.stability.Unstable;

Expand All @@ -42,7 +44,7 @@ public interface AdminToolsConfiguration
/**
* Get the lines that are to be excluded from xwiki.cfg and xwiki.properties files.
*
* @return {@link String} with the lines to be excluded, separated by ",".
* @return {@link List} with the lines to be excluded.
*/
String getExcludedLines();
List<String> getExcludedLines();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public interface DataResource
* Retrieves the content of a system file and adds it as an entry inside a {@link ZipOutputStream}.
*
* @param zipOutputStream {@link ZipOutputStream} represents the zip archive in which the entry is written.
* @param filters optional parameter. Store filters that can be used for file selection.
* @param filters store filters that can be used for file selection.
* @throws IOException
*/
void addZipEntry(ZipOutputStream zipOutputStream, Map<String, String> filters) throws IOException;

/**
* Retrieve the content of a system file.
*
* @param input Optional. Used to send additional info to the component.
* @param input Can be used to send additional info to the component.
* @return the content of the file as an {@link Byte} array.
* @throws IOException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,31 @@
public interface AdminToolsResource extends XWikiRestComponent
{
/**
* Access the XWiki configuration or properties files.
* Access the content of a specific file, based on received type.
*
* @param type {link String} specifies the XWiki searched file.
* @return {@link Response} with the content of the file, or specific error code.
* @throws XWikiRestException
* @param type identifier of the requested file.
* @return the content of this file.
* @throws XWikiRestException if an error occurred while accessing the file.
*/
@GET
@Path("/files/{fileType}")
Response getFileView(@PathParam("fileType") String type) throws XWikiRestException;
Response getFile(@PathParam("fileType") String type) throws XWikiRestException;

/**
* Download files from system server.
* Download an archive with the requested files.
*
* @return {@link Response} with a configured header for zip download, or specific error code.
* @throws XWikiRestException
* @return an archive with files.
* @throws XWikiRestException if an error occurred while getting the archive.
*/
@POST
@Path("/files")
Response getFiles() throws XWikiRestException;

/**
* Retrieve last logs from server.
* Get last n lines of server logs.
*
* @return {@link Response} with the content of the last logs, or specific error code.
* @throws XWikiRestException
* @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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

import org.xwiki.component.annotation.Component;

import com.xpn.xwiki.XWikiContext;
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;
Expand Down Expand Up @@ -59,8 +57,6 @@ public class AdminToolsManager
@Inject
private DownloadManager downloadManager;

@Inject
private Provider<XWikiContext> xcontextProvider;
/**
* Get data generated in a specific format, using a template, by each provider and merge it.
*
Expand Down Expand Up @@ -100,7 +96,7 @@ public String generateData(String hint)
*/
public List<String> getSupportedDBs()
{
return new ArrayList<>(this.currentServer.getSupportedDBs().values());
return new ArrayList<>(this.currentServer.getSupportedDBs().values());
}

/**
Expand All @@ -113,19 +109,6 @@ public List<String> getSupportedServers()
return this.currentServer.getSupportedServers();
}

/**
* Get the context path for the current XWiki request.
*
* @return a String representing the xwiki context path.
*/
public String getContextPath()
{
XWikiContext xWikiContext = this.xcontextProvider.get();
XWikiRequest xWikiRequest = xWikiContext.getRequest();

return xWikiRequest.getContextPath();
}

/**
* Get the rendered template for accessing the downloads UI.
*
Expand All @@ -135,5 +118,4 @@ public String getDownloadTemplate()
{
return this.downloadManager.renderTemplate();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
package com.xwiki.admintools.internal.configuration;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
Expand Down Expand Up @@ -55,9 +58,9 @@ public String getServerPath()
}

@Override
public String getExcludedLines()
public List<String> getExcludedLines()
{
return this.getProperty(EXCLUDED_LINES, "NO_EXCLUDED_LINE");
return new ArrayList<>(List.of(this.getProperty(EXCLUDED_LINES, "NO_EXCLUDED_LINE").split(",")));
}

private <T> T getProperty(String key, T defaultValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public String getLogsFolderPath()
}

@Override
public String getLogFilePath()
public String getLastLogFilePath()
{
return this.serverPath + "/logs/catalina.out";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class DownloadManager
* @return filtered file content as a {@link Byte} array
* @throws IOException
*/
public byte[] getFileView(String input, String hint) throws IOException
public byte[] getFileView(String hint, String input) throws IOException
{
DataResource fileViewerProvider = findDataResource(hint);
if (fileViewerProvider != null) {
Expand Down Expand Up @@ -137,7 +137,7 @@ public String renderTemplate()
}
ScriptContext scriptContext = this.scriptContextManager.getScriptContext();
scriptContext.setAttribute("found", found, ScriptContext.ENGINE_SCOPE);
return this.templateManager.render("downloadTemplate.vm");
return this.templateManager.render("filesSectionTemplate.vm");
} catch (Exception e) {
this.logger.warn("Failed to render custom template. Root cause is: [{}]",
ExceptionUtils.getRootCauseMessage(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,16 @@ public class DataProvidersDataResource implements DataResource
@Override
public void addZipEntry(ZipOutputStream zipOutputStream, Map<String, String> filters) throws IOException
{
if (filters == null) {
createArchiveEntry(zipOutputStream);
}
createZipEntry(zipOutputStream);
}

@Override
public byte[] getByteData(String input) throws IOException
public byte[] getByteData(String input)
{
Map<String, String> providersResults = new HashMap<>();
Map<String, Map<String, String>> providersResults = new HashMap<>();
for (DataProvider dataProvider : dataProviders.get()) {
try {
providersResults.putAll(dataProvider.getDataAsJSON());
providersResults.put(dataProvider.getIdentifier(), dataProvider.getDataAsJSON());
} catch (Exception e) {
logger.warn(String.format("Error getting json from DataProvider %s", dataProvider.getIdentifier()),
ExceptionUtils.getRootCauseMessage(e));
Expand All @@ -89,7 +87,7 @@ public String getIdentifier()
return HINT;
}

private void createArchiveEntry(ZipOutputStream zipOutputStream) throws IOException
private void createZipEntry(ZipOutputStream zipOutputStream) throws IOException
{
ZipEntry zipEntry = new ZipEntry("configuration_json.txt");
zipOutputStream.putNextEntry(zipEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,28 @@ public class LogsDataResource implements DataResource
*/
public static final String HINT = "logsDataResource";

private static final String FROM_DATE_FILTER_KEY = "from";

private static final String TO_DATE_FILTER_KEY = "to";

@Inject
private Logger logger;

@Inject
private CurrentServer currentServer;

@Override
public void addZipEntry(ZipOutputStream zipOutputStream, Map<String, String> filters) throws IOException
public void addZipEntry(ZipOutputStream zipOutputStream, Map<String, String> filters)
{
if (filters != null) {
createArchiveEntry(zipOutputStream, filters);
}
createZipEntry(zipOutputStream, filters);
}

@Override
public byte[] getByteData(String input) throws IOException
{
File file = new File(currentServer.getCurrentServer().getLogFilePath());
File file = new File(currentServer.getCurrentServer().getLastLogFilePath());
if (!file.exists() || !file.isFile()) {
throw new FileNotFoundException("File not found: " + currentServer.getCurrentServer().getLogFilePath());
throw new FileNotFoundException("File not found: " + currentServer.getCurrentServer().getLastLogFilePath());
}
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
int lines = parseInt(input);
Expand Down Expand Up @@ -118,7 +120,7 @@ public String getIdentifier()
return HINT;
}

private void createArchiveEntry(ZipOutputStream zipOutputStream, Map<String, String> filters)
private void createZipEntry(ZipOutputStream zipOutputStream, Map<String, String> filters)
{
byte[] buffer = new byte[2048];
try {
Expand All @@ -127,7 +129,7 @@ private void createArchiveEntry(ZipOutputStream zipOutputStream, Map<String, Str
// Go through all the files in the list.
for (File file : listOfFiles != null ? listOfFiles : new File[0]) {
// Check if the selected file is of file type and check filters.
if (file.isFile() && checkFilters(filters, currentServer.getCurrentServer().getLogsPattern(), file)) {
if (file.isFile() && checkFilters(filters, file)) {
// Create a new zip entry and add the content.
ZipEntry zipEntry = new ZipEntry("logs/" + file.getName());
zipOutputStream.putNextEntry(zipEntry);
Expand All @@ -151,28 +153,27 @@ private void createArchiveEntry(ZipOutputStream zipOutputStream, Map<String, Str
* Check that the file date is in the filter range. Returns {@code true} if no filter is provided.
*
* @param filters represents the date range of the search.
* @param pattern server specific {@link Pattern} used to identify the log date from the log name.
* @param file current {@link File} that is to be checked.
* @return {@code true} if the file is between the provided dates or there is no filter, {@code false} otherwise
*/
private boolean checkFilters(Map<String, String> filters, Pattern pattern, File file)
private boolean checkFilters(Map<String, String> filters, File file)
{
// Get the server specific Pattern used to identify the log date from the log name.
Pattern pattern = currentServer.getCurrentServer().getLogsPattern();
Matcher matcher = pattern.matcher(file.getName());
if (matcher.find()) {
String fromDateFilterKey = "from";
String toDateFilterKey = "to";
String fileDateString = matcher.group();
LocalDate fileDate = LocalDate.parse(fileDateString);

if (filters.get(fromDateFilterKey) != null && filters.get(toDateFilterKey) != null) {
LocalDate fromDate = LocalDate.parse(filters.get(fromDateFilterKey));
LocalDate toDate = LocalDate.parse(filters.get(toDateFilterKey));
if (filters.get(FROM_DATE_FILTER_KEY) != null && filters.get(TO_DATE_FILTER_KEY) != null) {
LocalDate fromDate = LocalDate.parse(filters.get(FROM_DATE_FILTER_KEY));
LocalDate toDate = LocalDate.parse(filters.get(TO_DATE_FILTER_KEY));
return fileDate.isAfter(fromDate.minusDays(1)) && fileDate.isBefore(toDate.plusDays(1));
} else if (filters.get(fromDateFilterKey) != null) {
LocalDate fromDate = LocalDate.parse(filters.get(fromDateFilterKey));
} else if (filters.get(FROM_DATE_FILTER_KEY) != null) {
LocalDate fromDate = LocalDate.parse(filters.get(FROM_DATE_FILTER_KEY));
return fileDate.isAfter(fromDate.minusDays(1));
} else if (filters.get(toDateFilterKey) != null) {
LocalDate toDate = LocalDate.parse(filters.get(toDateFilterKey));
} else if (filters.get(TO_DATE_FILTER_KEY) != null) {
LocalDate toDate = LocalDate.parse(filters.get(TO_DATE_FILTER_KEY));
return fileDate.isBefore(toDate.plusDays(1));
} else {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -55,11 +54,9 @@ public class XWikiConfigFileDataResource implements DataResource
/**
* Component identifier.
*/
public static final String HINT = "xwikiConfigFileDataResource";
public static final String HINT = "xwikiConfig";

private List<String> excludedLinesHints = new ArrayList<>();

private final String xwikiCfg = "xwiki.cfg";
private static final String XWIKI_CFG = "xwiki.cfg";

@Inject
@Named("default")
Expand All @@ -74,16 +71,14 @@ public class XWikiConfigFileDataResource implements DataResource
@Override
public void addZipEntry(ZipOutputStream zipOutputStream, Map<String, String> filters) throws IOException
{
if (filters == null) {
createArchiveEntry(zipOutputStream);
}
createZipEntry(zipOutputStream);
}

@Override
public byte[] getByteData(String input) throws IOException
public byte[] getByteData(String input)
{
getExcludedLines();
String filePath = currentServer.getCurrentServer().getXwikiCfgFolderPath() + xwikiCfg;
List<String> excludedLinesHints = adminToolsConfig.getExcludedLines();
String filePath = currentServer.getCurrentServer().getXwikiCfgFolderPath() + XWIKI_CFG;
File inputFile = new File(filePath);
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
StringBuilder stringBuilder = new StringBuilder();
Expand Down Expand Up @@ -111,15 +106,9 @@ public String getIdentifier()
return HINT;
}

private void getExcludedLines()
{
String content = adminToolsConfig.getExcludedLines();
excludedLinesHints = new ArrayList<>(List.of(content.split(",")));
}

private void createArchiveEntry(ZipOutputStream zipOutputStream) throws IOException
private void createZipEntry(ZipOutputStream zipOutputStream) throws IOException
{
ZipEntry zipEntry = new ZipEntry(xwikiCfg);
ZipEntry zipEntry = new ZipEntry(XWIKI_CFG);

zipOutputStream.putNextEntry(zipEntry);

Expand Down
Loading

0 comments on commit f7cae01

Please sign in to comment.