Skip to content

Commit

Permalink
Explicit File to URL conversions, Fixes #46 (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmsq authored and smecsia committed Jul 27, 2018
1 parent 30fd533 commit aaacedf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
17 changes: 13 additions & 4 deletions src/main/java/io/qameta/allure/bamboo/AllureArtifactsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@
import com.atlassian.sal.api.ApplicationProperties;
import com.atlassian.sal.api.UrlMode;
import com.google.common.collect.ImmutableList;

import org.apache.log4j.Logger;
import org.apache.tools.ant.types.FileSet;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import javax.ws.rs.core.UriBuilder;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -135,10 +138,16 @@ Optional<String> getArtifactUrl(final String planKeyString, final String buildNu
}

@Nullable
private String getLocalStorageURL(String planKeyString, String buildNumber, String filePath) {
final File file = getLocalStoragePath(planKeyString, buildNumber).resolve(filePath).toFile();
final String fullPath = (file.isDirectory()) ? new File(file, "index.html").getAbsolutePath() : file.getAbsolutePath();
return String.format("file://%s", fullPath);
private String getLocalStorageURL(String planKeyString, String buildNumber, String filePath) {
try {
final File file = getLocalStoragePath(planKeyString, buildNumber).resolve(filePath).toFile();
final String fullPath = (file.isDirectory()) ? new File(file, "index.html").getAbsolutePath() : file.getAbsolutePath();
return new File(fullPath).toURI().toURL().toString();

} catch (MalformedURLException e) {
// should never happen
throw new RuntimeException(e);
}
}

/**
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/io/qameta/allure/bamboo/AllureReportServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.atlassian.bamboo.plan.PlanResultKey;
import com.atlassian.bamboo.resultsummary.ResultsSummary;
import com.atlassian.bamboo.resultsummary.ResultsSummaryManager;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -12,8 +13,10 @@
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -70,14 +73,21 @@ protected void doHead(HttpServletRequest request, HttpServletResponse response)
}

private void setResponseHeaders(HttpServletResponse response, String fileUrl) throws IOException {
response.setStatus(HttpServletResponse.SC_OK);
final String file = new URL(fileUrl).getPath();
final String mimeType = ofNullable(getServletContext().getMimeType(fileUrl)).orElse(
Files.probeContentType(Paths.get(file))
);
final String charsetPostfix = of("application", "text").anyMatch(mimeType::contains) ? ";charset=utf-8" : "";
response.setHeader("Content-Type", mimeType + charsetPostfix);
response.setHeader("Content-Disposition", "inline; filename=\"" + Paths.get(fileUrl).getFileName().toString() + "\"");

try {
response.setStatus(HttpServletResponse.SC_OK);
final URI file = new URL(fileUrl).toURI();
final String mimeType = ofNullable(getServletContext().getMimeType(fileUrl)).orElse(
Files.probeContentType(Paths.get(file))
);
final String charsetPostfix = of("application", "text").anyMatch(mimeType::contains) ? ";charset=utf-8" : "";
response.setHeader("Content-Type", mimeType + charsetPostfix);
response.setHeader("Content-Disposition", "inline; filename=\"" + Paths.get(file).getFileName().toString() + "\"");

} catch (URISyntaxException e) {
// should never happen
throw new RuntimeException(e);
}
}

private Optional<String> getArtifactUrl(HttpServletRequest request, HttpServletResponse response) {
Expand Down

0 comments on commit aaacedf

Please sign in to comment.