Skip to content
Snippets Groups Projects
Commit fa49f347 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Prepared for excercise

Resolves: #
parent 1d6e2443
No related merge requests found
package lab07;
import static jakarta.ws.rs.core.Response.Status.NOT_FOUND;
import static jakarta.ws.rs.core.Response.Status.OK;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
......@@ -31,14 +25,7 @@ class HttpHandler {
try {
request = getRequest(channel);
String resource = getResource(request);
CompletableFuture<Void> getFileTransfer = transferFile(resource, channel);
getFileTransfer.thenRun(() -> {
try {
channel.close();
} catch (IOException e) {
log.error("close", channel);
}
});
} catch (IOException e) {
writeStatus(Status.INTERNAL_SERVER_ERROR, channel);
writeString("", channel);
......@@ -48,38 +35,25 @@ class HttpHandler {
}
}
private void writeStatus(Status status,SocketChannel channel) {
writeString("HTTP/1.1 %d %s".formatted(status.getStatusCode(), status.getReasonPhrase()), channel);
writeString("", channel);
}
private CompletableFuture<Void> transferFile(String resource, SocketChannel channel) {
return CompletableFuture.runAsync(() -> doTransferFile(resource, channel));
return null;
}
private void doTransferFile(final String resource, SocketChannel channel) {
try {
String resourcePath;
if (resource.length() > 1) {
resourcePath = resource.substring(1);
} else {
resourcePath = resource;
}
RandomAccessFile raf = new RandomAccessFile(root.resolve(Paths.get(resourcePath)).toFile(), "r");
try(FileChannel fileChannel = raf.getChannel()) {
writeStatus(OK, channel);
fileChannel.transferTo(0, raf.length(), channel);
}
} catch (FileNotFoundException e) {
writeStatus(NOT_FOUND, channel);
writeString("%s %s".formatted(resource, NOT_FOUND.getReasonPhrase()), channel);
} catch (IOException e) {
log.error("transfer", e);
}
@SuppressWarnings("unused")
Path filePath = getFilePath(resource);
//TODO implement data transfer from file to socket
}
private Path getFilePath(String resource) {
//remove trailing '/' to relative path
String resourcePath;
if (resource.length() > 1) {
resourcePath = resource.substring(1);
} else {
resourcePath = resource;
}
return root.resolve(Paths.get(resourcePath));
}
private String getRequest(SocketChannel channel) throws IOException {
......@@ -90,18 +64,11 @@ class HttpHandler {
return result;
}
private static String getResource(String readedData) {
try (Scanner scanner = new Scanner(readedData)) {
scanner.useDelimiter("\\s");
String method = scanner.next();
if (!method.equals("GET")) {
throw new UnsupportedOperationException(method);
}
return scanner.next();
}
private void writeStatus(Status status,SocketChannel channel) {
writeString("HTTP/1.1 %d %s".formatted(status.getStatusCode(), status.getReasonPhrase()), channel);
writeString("", channel);
}
private void writeString(String value, SocketChannel socketChannel) {
buffer.put(value.getBytes());
buffer.put("\n".getBytes());
......@@ -113,4 +80,15 @@ class HttpHandler {
}
buffer.clear();
}
private static String getResource(String readedData) {
try (Scanner scanner = new Scanner(readedData)) {
scanner.useDelimiter("\\s");
String method = scanner.next();
if (!method.equals("GET")) {
throw new UnsupportedOperationException(method);
}
return scanner.next();
}
}
}
......@@ -2,10 +2,10 @@ package lab07;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.extern.log4j.Log4j2;
......@@ -13,6 +13,10 @@ import lombok.extern.log4j.Log4j2;
@Log4j2
public class Main {
public static void main(String[] args) throws IOException, URISyntaxException {
int port = 8000;
String basePathName = "..";
Path basePath = Paths.get(basePathName).toRealPath();
log.info("Started server on http://localhost:{} with base path {}", port, basePath);
HttpHandler handler = new HttpHandler(Paths.get("."));
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.bind(new InetSocketAddress(8000));
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment