task 2 in progress

This commit is contained in:
2026-01-24 22:24:15 +01:00
parent ea8436e399
commit db262112e1
13 changed files with 244 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.tinsae</groupId>
<artifactId>client</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<junit.version>5.10.0</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>4.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,78 @@
package net.tinsae;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Client {
private String url;
private String table = "/items";
public Client(String url){
this.url = url+table;
}
private static final HttpClient client = HttpClient.newHttpClient();
public static void main(String[] args) throws Exception {
}
public int postItem(String json) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode();
}
public int putItem(int id, String json) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url + "/" + id))
.header("Content-Type", "application/json")
.PUT(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode();
}
public int deleteItem(int id) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url + "/" + id))
.DELETE()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode();
}
public int getItems() throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
return response.statusCode();
}
public int getItem(int id) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url+"/"+id))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
return response.statusCode();
}
}

View File

@@ -0,0 +1,89 @@
package net.tinsae;
import java.util.Scanner;
public class Delegator {
private final Client client;
private final Scanner sc = new Scanner(System.in);
public Delegator(String path) {
client = new Client(path);
}
public void executeCommand(String command) {
switch (command) {
case "delete" -> delegateDelete();
case "insert" -> delegateInsert();
case "read" -> delegateRead();
case "update" -> delegateUpdate();
default -> System.err.println("Unknown command, please repeat");
}
}
private void delegateDelete() {
try {
System.out.print("ID of the item you want to delete: ");
int id = sc.nextInt();
sc.nextLine(); // consume newline
int response = client.deleteItem(id);
System.out.println("Command executed with response code " + response);
} catch (Exception e) {
System.err.println("Error in executing delete command: " + e.getMessage());
}
}
private void delegateInsert() {
try {
System.out.print("Name: ");
String name = sc.nextLine().strip();
System.out.print("Description: ");
String description = sc.nextLine();
System.out.print("Price: ");
float price = sc.nextFloat();
sc.nextLine(); // consume newline
int response = client.postItem(makeJson(name, description, price));
System.out.println("Command executed with response code " + response);
} catch (Exception e) {
System.err.println("Error in executing insert command: " + e.getMessage());
}
}
private String makeJson(String name, String desc, float price) {
return String.format("""
{
"name": "%s",
"description": "%s",
"price": %s
}
""", name, desc, price);
}
private void delegateRead() {
try {
System.out.print("Read All or One? (A/O): ");
String input = sc.nextLine();
if (input.equalsIgnoreCase("A")) {
client.getItems();
return;
}
System.out.print("Enter ID: ");
int id = sc.nextInt();
sc.nextLine(); // consume newline
client.getItem(id);
} catch (Exception e) {
System.err.println("Error in executing read command: " + e.getMessage());
}
}
private void delegateUpdate() {
System.out.println("Update not implemented yet");
}
}

View File

@@ -0,0 +1,24 @@
package net.tinsae;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String path = "http://localhost:8080/shop-1.0-SNAPSHOT/api";
Delegator delegator = new Delegator(path);
System.out.println("Hello! What do you want to do? allowed actions are \n"+
"Insert, update, delete and read");
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String response = sc.nextLine().strip();
if(response.equalsIgnoreCase("exit")){
return;
}
delegator.executeCommand(response);
}
sc.close();
}
}

View File

@@ -0,0 +1,3 @@
artifactId=client
groupId=net.tinsae
version=1.0-SNAPSHOT

View File

@@ -0,0 +1,3 @@
net/tinsae/Client.class
net/tinsae/Main.class
net/tinsae/Delegator.class

View File

@@ -0,0 +1,3 @@
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/client/src/main/java/net/tinsae/Client.java
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/client/src/main/java/net/tinsae/Delegator.java
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/client/src/main/java/net/tinsae/Main.java