-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: publish the files as internal files
- Loading branch information
Showing
7 changed files
with
247 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
287 changes: 151 additions & 136 deletions
287
portal/src/main/java/edu/isi/wings/portal/classes/config/Config.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
portal/src/main/java/edu/isi/wings/portal/servlets/ProvenanceFileServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package edu.isi.wings.portal.servlets; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import edu.isi.wings.portal.classes.config.Config; | ||
|
||
public class ProvenanceFileServlet extends HttpServlet { | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
Config config = new Config(request, null, null); | ||
String provenanceDirectoryPath = config.getProvenanceDirectory(); | ||
File provenanceDirectory = new File(provenanceDirectoryPath); | ||
|
||
String requestFile = request.getPathInfo(); | ||
String filePath = provenanceDirectoryPath + File.separator + requestFile; | ||
System.out.println("FileServlet: " + filePath); | ||
|
||
File file = new File(filePath); | ||
if (!file.exists()) { | ||
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. | ||
return; | ||
} | ||
// check if filepath is in allowed directory | ||
if (!isInsideDirectory(file, provenanceDirectory)) { | ||
response.sendError(HttpServletResponse.SC_FORBIDDEN); | ||
return; | ||
} | ||
// Set content type and headers for the response | ||
response.setContentType("application/octet-stream"); | ||
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); | ||
|
||
// Read the file and write its contents to the response output stream | ||
try (FileInputStream fis = new FileInputStream(filePath); | ||
OutputStream out = response.getOutputStream()) { | ||
byte[] buffer = new byte[4096]; | ||
int bytesRead; | ||
while ((bytesRead = fis.read(buffer)) != -1) { | ||
out.write(buffer, 0, bytesRead); | ||
} | ||
} catch (IOException e) { | ||
// Handle exception | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static boolean isInsideDirectory(File file, File directory) { | ||
File parent = file.getParentFile(); | ||
if (parent == null) { | ||
return false; | ||
} else if (parent.equals(directory)) { | ||
return true; | ||
} else { | ||
return isInsideDirectory(parent, directory); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters