-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
304 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.github.voxxin.web; | ||
|
||
import com.github.voxxin.web.request.FormattedRequest; | ||
import com.github.voxxin.web.request.FormattedResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
|
||
import java.io.*; | ||
|
||
class FilePathRoute extends AbstractRoute { | ||
private final Logger LOGGER = LoggerFactory.getLogger(WebServer.class); | ||
private final File file; | ||
|
||
public FilePathRoute(File file, String route) { | ||
super(route + file.getName()); | ||
this.file = file; | ||
} | ||
|
||
@Override | ||
public OutputStream handleRequests(FormattedRequest request, OutputStream outputStream) throws IOException { | ||
try (FileInputStream fileInputStream = new FileInputStream(file)) { | ||
outputStream.write(new FormattedResponse() | ||
.contentType(Files.probeContentType(file.toPath())) | ||
.content(fileInputStream.readAllBytes()) | ||
.statusCode(200) | ||
.statusMessage("OK").build()); | ||
} catch (IOException e) { | ||
LOGGER.error("Error occurred while handling file request: {}", e.getMessage()); | ||
} | ||
|
||
return outputStream; | ||
} | ||
} | ||
|
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
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
Oops, something went wrong.