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

Initial commit

parents
Branches
No related merge requests found
**/target/
/target/
*/target/*
.classpath
.project
.settings
# Package Files #
*.jar
*.war
*.ear
*.iml
*.idea
\ No newline at end of file
pom.xml 0 → 100644
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>koz01.java2</groupId>
<artifactId>lab07</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>15.0.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>15.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.ws.rs/jakarta.ws.rs-api -->
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</project>
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;
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;
import jakarta.ws.rs.core.Response.Status;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
@Log4j2
@RequiredArgsConstructor
class HttpHandler {
private final ByteBuffer buffer = ByteBuffer.allocate(1024*1024);
private final Path root;
void handle(SocketChannel channel) {
String request;
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);
} catch (UnsupportedOperationException exc) {
writeStatus(Status.METHOD_NOT_ALLOWED, channel);
writeString("Method %s is not allowed".formatted(exc.getMessage()), channel);
}
}
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));
}
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);
}
}
private String getRequest(SocketChannel channel) throws IOException {
channel.read(buffer);
buffer.flip();
String result = new String(buffer.array(), 0, buffer.limit());
buffer.clear();
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 writeString(String value, SocketChannel socketChannel) {
buffer.put(value.getBytes());
buffer.put("\n".getBytes());
buffer.flip();
try {
socketChannel.write(buffer);
} catch (IOException e) {
log.error("write: " + value, e);
}
buffer.clear();
}
}
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.Paths;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class Main {
public static void main(String[] args) throws IOException, URISyntaxException {
HttpHandler handler = new HttpHandler(Paths.get("."));
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.bind(new InetSocketAddress(8000));
while(true) {
SocketChannel sc = ssc.accept();
handler.handle(sc);
}
//ssc.close();
}
}
module koz01.java2.lab07 {
requires transitive javafx.controls;
requires javafx.fxml;
requires jakarta.ws.rs;
requires lombok;
requires org.apache.logging.log4j;
}
\ No newline at end of file
/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
\ No newline at end of file
<Configuration>
<Appenders>
<Console name="Console">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"></AppenderRef>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
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