Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSITE-945] Remove dependency on Commons IO #174

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,6 @@ under the License.
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
<scope>test</scope>
</dependency>

<!-- test -->
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
package org.apache.maven.plugins.site.deploy;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.apache.maven.bridge.MavenRepositorySystem;
import org.apache.maven.doxia.tools.SiteTool;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
Expand Down Expand Up @@ -56,7 +55,7 @@ public void setUp() throws Exception {
super.setUp();
if (!siteTargetPath.exists()) {
siteTargetPath.mkdirs();
FileUtils.cleanDirectory(siteTargetPath);
cleanDirectory(siteTargetPath);
}
}

Expand All @@ -66,7 +65,7 @@ public void setUp() throws Exception {

@Test
public void noAuthzDavDeploy() throws Exception {
FileUtils.cleanDirectory(siteTargetPath);
cleanDirectory(siteTargetPath);
SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler(siteTargetPath);

try {
Expand Down Expand Up @@ -102,7 +101,7 @@ public void noAuthzDavDeploy() throws Exception {
@Test
public void davDeployThruProxyWithoutAuthzInProxy() throws Exception {

FileUtils.cleanDirectory(siteTargetPath);
cleanDirectory(siteTargetPath);
SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler(siteTargetPath);
try {
File pluginXmlFile = getTestFile("src/test/resources/unit/deploy-dav/pom.xml");
Expand Down Expand Up @@ -149,7 +148,7 @@ public void davDeployThruProxyWithoutAuthzInProxy() throws Exception {
@Test
public void davDeployThruProxyWitAuthzInProxy() throws Exception {

FileUtils.cleanDirectory(siteTargetPath);
cleanDirectory(siteTargetPath);
// SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler( siteTargetPath );

Map<String, String> authentications = new HashMap<>();
Expand Down Expand Up @@ -217,12 +216,12 @@ public void davDeployThruProxyWitAuthzInProxy() throws Exception {
private void assertContentInFiles() throws Exception {
File fileToTest = new File(siteTargetPath, "site" + File.separator + "index.html");
assertTrue(fileToTest.exists());
String fileContent = FileUtils.readFileToString(fileToTest);
String fileContent = readFileToString(fileToTest);
assertTrue(fileContent.contains("Welcome to Apache Maven"));

fileToTest = new File(siteTargetPath, "site" + File.separator + "css" + File.separator + "maven-base.css");
assertTrue(fileToTest.exists());
fileContent = FileUtils.readFileToString(fileToTest);
fileContent = readFileToString(fileToTest);
assertTrue(fileContent.contains("background-image: url(../images/collapsed.gif);"));
}

Expand All @@ -243,4 +242,26 @@ private boolean assertAtLeastOneRequestContainsHeader(List<HttpRequest> requests
}
return false;
}

private void cleanDirectory(File dir) throws IOException {
Files.list(dir.toPath())
.flatMap(p -> {
try {
return Files.walk(p);
} catch (IOException e) {
return Stream.empty();
}
})
.sorted(Comparator.reverseOrder())
.forEach(p -> {
try {
Files.delete(p);
} catch (IOException e) {
}
});
}

private String readFileToString(File fileToTest) throws IOException {
return Files.lines(fileToTest.toPath()).collect(Collectors.joining(System.lineSeparator()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
package org.apache.maven.plugins.site.deploy;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.proxy.AsyncProxyServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -127,7 +128,7 @@ public void service(ServletRequest req, ServletResponse res) throws ServletExcep
if (request.getMethod().equalsIgnoreCase("PUT") && targetPath != null) {
File targetFile = new File(siteTargetPath, targetPath);
log.info("writing file " + targetFile.getPath());
FileUtils.writeByteArrayToFile(targetFile, IOUtils.toByteArray(request.getInputStream()));
writeByteArrayToFile(targetFile, request.getInputStream());
}

response.setStatus(HttpServletResponse.SC_OK);
Expand All @@ -143,4 +144,16 @@ public void service(ServletRequest req, ServletResponse res) throws ServletExcep

super.service(req, res);
}

private void writeByteArrayToFile(File targetFile, ServletInputStream inputStream) throws IOException {
int nRead;
byte[] data = new byte[512];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
targetFile.getParentFile().mkdirs();
Files.write(targetFile.toPath(), buffer.toByteArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
Expand Down Expand Up @@ -78,7 +77,7 @@ public void handle(String target, Request r, HttpServletRequest request, HttpSer
if (request.getMethod().equalsIgnoreCase("PUT")) {
File targetFile = new File(siteTargetPath, targetPath);
log.info("writing file " + targetFile.getPath());
FileUtils.writeByteArrayToFile(targetFile, IOUtils.toByteArray(request.getInputStream()));
writeByteArrayToFile(targetFile, request.getInputStream());
}

// PrintWriter writer = response.getWriter();
Expand Down Expand Up @@ -111,4 +110,17 @@ public int getPort() {
public void stop() throws Exception {
server.stop();
}

private void writeByteArrayToFile(File targetFile, ServletInputStream inputStream) throws IOException {
int nRead;
byte[] data = new byte[512];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}

buffer.flush();
targetFile.getParentFile().mkdirs();
Files.write(targetFile.toPath(), buffer.toByteArray());
}
}