Skip to content

Commit 01c7eec

Browse files
committed
Resources compliance
1 parent 98f6d20 commit 01c7eec

File tree

3 files changed

+8
-16
lines changed

3 files changed

+8
-16
lines changed

reverse-proxy-file-server/src/main/java/com/ufscar/psor/Main.java

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.ufscar.psor;
22

3-
import java.io.File;
4-
import java.io.FileInputStream;
53
import java.io.IOException;
64
import java.io.InputStream;
75
import java.io.OutputStream;
@@ -13,7 +11,6 @@ public class Main {
1311
private static final int PORT = 8080;
1412
private static final String PROXY_ADRESS = "localhost";
1513
private static final int PROXY_PORT = 8081;
16-
private static final String ROOT = "reverse-proxy-file-server/public"; // diretorio base para os arquivos
1714
private static final String INDEX = "index.html"; // nome do arquivo
1815

1916
public static void main(String[] args) {
@@ -60,16 +57,15 @@ public static void handleRequest(Socket clientSocket) throws IOException {
6057
requestedFile = "/" + INDEX;
6158
}
6259

63-
// caminho do arquivo solicitado parseado
64-
File file = new File(ROOT + requestedFile);
60+
InputStream fileStream = Main.class.getClassLoader().getResourceAsStream(requestedFile.substring(1));
6561

6662
//debug
6763
//System.out.println("arquivo solicitado: " + file.getAbsolutePath());
6864

6965
if (requestedFile.equals("/proxy")) {
7066
handleProxyRequest(request, outputStream);
71-
} else if (file.exists() && !file.isDirectory()) {
72-
handleLocalRequest(file, outputStream);
67+
} else if (fileStream != null) {
68+
handleLocalRequest(fileStream, outputStream);
7369
} else {
7470
String notFound = "HTTP/1.1 404 Not Found\r\n\r\n<h1>404 File Not Found locally</h1>";
7571
outputStream.write(notFound.getBytes());
@@ -81,19 +77,15 @@ public static void handleRequest(Socket clientSocket) throws IOException {
8177
}
8278
}
8379

84-
public static void handleLocalRequest(File file, OutputStream outputStream) throws IOException {
80+
public static void handleLocalRequest(InputStream fileStream, OutputStream outputStream) throws IOException {
8581

8682
outputStream.write("HTTP/1.1 200 OK\r\n".getBytes());
8783
outputStream.write("Content-Type: text/html\r\n\r\n".getBytes());
8884

89-
try (FileInputStream fileInputStream = new FileInputStream(file)) {
90-
byte[] buffer = new byte[2048];
91-
int bytesRead;
92-
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
93-
outputStream.write(buffer, 0, bytesRead);
94-
}
95-
} catch (IOException e) {
96-
e.printStackTrace(System.out);
85+
byte[] buffer = new byte[2048];
86+
int bytesRead;
87+
while ((bytesRead = fileStream.read(buffer)) != -1) {
88+
outputStream.write(buffer, 0, bytesRead);
9789
}
9890
}
9991

0 commit comments

Comments
 (0)