Skip to content

Commit

Permalink
Download important files xwikisas#8 - DRAFT
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiuchiuSorin committed Aug 31, 2023
1 parent e9bc0f4 commit 7439f99
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 0 deletions.
10 changes: 10 additions & 0 deletions application-admintools-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,15 @@
<artifactId>xwiki-commons-component-api</artifactId>
<version>${commons.version}</version>
</dependency>
<dependency>
<groupId>org.xwiki.contrib</groupId>
<artifactId>xwiki-restserver-example</artifactId>
<version>4.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.xwiki.platform</groupId>
<artifactId>xwiki-platform-rest-server</artifactId>
<version>${platform.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/
package com.xwiki.admintools;

import java.util.Map;

import org.xwiki.component.annotation.Role;

/**
* Allows data gathering. CREATE AN ABSTRACT CLASS CONTAINING THE SAME FEATURES (adminToolsCfgProvider, templateManager,
* logger)?
*
* @version $Id$
* @since 1.0
*/
@Role
public interface FilesDownloader
{
/**
* TBC.
*
* @param filter
* @param path
* @return TBC
*/
Object getLogs(Map<String, String> filter, String path);

/**
* Extract the hint of a component.
*
* @return component hint
*/
String getIdentifier();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.
*/
package com.xwiki.admintools.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import org.xwiki.rest.XWikiRestComponent;
import org.xwiki.rest.XWikiRestException;

/**
* Provides the APIs needed by the collabora server in order to access a file and it's content, but also to save it.
*
* @version $Id$
* @since 1.0
*/
@Path("/admintools/download")
public interface AdminToolsRestApi extends XWikiRestComponent
{
/**
* TBC.
* @param type
* @param token
* @return TBC
* @throws XWikiRestException
*/
@GET
@Path("/configs/{fileType}")
Response getConfigs(@PathParam("fileType") String type, @QueryParam("access_token") String token)
throws XWikiRestException;

/**
* TBC.
*
* @param type
* @param token
* @return TBC
* @throws XWikiRestException
*/
@GET
@Path("/logs")
Response getLogs(@PathParam("fileType") String type, @QueryParam("access_token") String token)
throws XWikiRestException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* 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.
*/
package com.xwiki.admintools.internal.downloads;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.phase.Initializable;
import org.xwiki.component.phase.InitializationException;

import com.xwiki.admintools.FilesDownloader;
import com.xwiki.admintools.internal.data.identifiers.CurrentServer;

/**
* Encapsulates functions used for downloading configuration files.
*
* @version $Id$
* @since 1.0
*/
@Component(roles = DownloadsManager.class)
@Singleton
public class DownloadsManager implements Initializable
{
/**
* TBC.
*/
@Inject
private CurrentServer currentServer;

/**
* A list of all the data providers for Admin Tools.
*/
@Inject
private Provider<List<FilesDownloader>> filesDownloader;

private String serverPath;

private String serverType;

/**
* TBC.
*
* @throws InitializationException
*/
@Override
public void initialize() throws InitializationException
{
Map<String, String> identifiers = currentServer.getServerIdentifiers();
serverPath = identifiers.get("serverPath");
serverType = identifiers.get("serverType");
}

/**
* TBC.
*
* @param fileType TBC.
* @return TBC.
* @throws IOException
*/
public byte[] downloadXWikiFile(String fileType) throws IOException
{
return prepareFile(fileType);
}

/**
* TBC.
*
* @param filters TBC
* @return TBC
*/
public Object downloadLogs(Map<String, String> filters)
{
return callLogsDownloader(serverType + "Logs", filters);
}

/**
* As all servers have the same path to the xwiki properties and configuration files, it is not needed to call this
* function in a server specific class.
*
* @param type
* @return
* @throws IOException
*/
private byte[] prepareFile(String type) throws IOException
{
String filePath = serverPath;
if (Objects.equals(type, "properties")) {
filePath += "/webapps/xwiki/WEB-INF/xwiki.properties";
} else {
filePath += "/webapps/xwiki/WEB-INF/xwiki.cfg";
}
File inputFile = new File(filePath);

BufferedReader reader = new BufferedReader(new FileReader(inputFile));
StringBuilder stringBuilder = new StringBuilder();

String currentLine;
List<String> wordsList = new ArrayList<>(
Arrays.asList("xwiki.authentication.validationKey", "xwiki.authentication.encryptionKey",
"xwiki.superadminpassword", "extension.repositories.privatemavenid.auth", "mail.sender.password"));
while ((currentLine = reader.readLine()) != null) {
String trimmedLine = currentLine.trim();
if (wordsList.stream().anyMatch(trimmedLine::contains)) {
continue;
}
stringBuilder.append(currentLine).append(System.getProperty("line.separator"));
}
reader.close();
return stringBuilder.toString().getBytes();
}

private Object callLogsDownloader(String hint, Map<String, String> filter)
{
for (FilesDownloader specificFilesDownloader : this.filesDownloader.get()) {
if (specificFilesDownloader.getIdentifier().equals(hint)) {
return specificFilesDownloader.getLogs(filter, serverPath);
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.
*/
package com.xwiki.admintools.internal.downloads;

import java.util.Map;

import javax.inject.Named;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;

import com.xwiki.admintools.FilesDownloader;

/**
* Encapsulates functions used for downloading configuration files.
*
* @version $Id$
* @since 1.0
*/
@Component(roles = TomcatFilesDownloader.class)
@Named(TomcatFilesDownloader.HINT)
@Singleton
public class TomcatFilesDownloader implements FilesDownloader
{
/**
* The hint for the component.
*/
public static final String HINT = "tomcatLogs";

/**
* @param filter
* @return
*/
@Override
public Object getLogs(Map<String, String> filter, String path)
{
return null;
}

@Override
public String getIdentifier()
{
return HINT;
}
}
Loading

0 comments on commit 7439f99

Please sign in to comment.