done task 5

This commit is contained in:
2026-01-26 22:15:05 +01:00
parent 100dec28bc
commit b7a3190bff
49 changed files with 97 additions and 27 deletions

View File

@@ -0,0 +1,40 @@
## Start Glass fish
```bash
# start glassfish server
glassfish7/bin/asadmin start-domain
# deploy app on it
glassfish7/bin/asadmin deploy target/shop-1.0-SNAPSHOT.war
```
we can browse server at [http://localhost:8080/shop-1.0-SNAPSHOT/api/items](http://localhost:8080/shop-1.0-SNAPSHOT/api/items)
## Start client
```bash
# cd to clients project directory
cd client
# compile and run project
mvn clean compile exec:java
```
The client class handles HTTP operations, while the Delegator class handles user interaction, input validation, and adapts the input into the correct format. Delegator then calls the appropriate Client methods, acting as a proxy in a liberal sense.
## Ending the program
Clent program can be ended by typing `done` or `exit` in the interactive terminal
Glassfish can be stoped using the following command in terminal
```bash
# first check which domains are running
glassfish7/bin/asadmin list-domains
# you will get somehing like the below lines
# domain1 running
# Command list-domains executed successfully.
# we have domain1 running. so we stop it with
glassfish7/bin/asadmin stop-domain domain1
```
## DONE

View File

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

View File

@@ -1,6 +0,0 @@
net/tinsae/shop/App.class
net/tinsae/shop/Item.class
net/tinsae/shop/Util.class
net/tinsae/shop/Factory.class
net/tinsae/shop/InvalidItemException.class
net/tinsae/shop/ItemDao.class

View File

@@ -1,6 +0,0 @@
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/App.java
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/Factory.java
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/InvalidItemException.java
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/Item.java
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/ItemDao.java
/home/tgk/Repos/Trusted/DistributedSystems/week5_TinsaeGhilay/shop/src/main/java/net/tinsae/shop/Util.java

Binary file not shown.

View File

@@ -0,0 +1,52 @@
#include <iostream>
#include <string>
#include <curl/curl.h>
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
std::string httpRequest(const std::string& url,
const std::string& method,
const std::string& body = "") {
CURL* curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if (curl) {
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
if (!body.empty()) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return readBuffer;
}
int main() {
std::string base = "http://localhost:8080/shop-1.0-SNAPSHOT/api/items";
// GET all items
std::cout << "GET all items:\n" << httpRequest(base, "GET") << "\n\n";
// GET item by ID
std::cout << "GET item 1:\n" << httpRequest(base + "/1", "GET") << "\n\n";
// POST new item
std::string newItem = R"({"name":"Mouse","price":1.25, "description":"Logitek gaming mouse"})";
std::cout << "POST new item:\n" << httpRequest(base, "POST", newItem) << "\n\n";
return 0;
}

BIN
week5_TinsaeGhilay/Task 4/client Executable file

Binary file not shown.

View File

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

View File

@@ -1,3 +0,0 @@
/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

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_1.xsd">
</beans>

View File

@@ -0,0 +1,5 @@
Exercise 1: not mandatory
Exercise 2: done 100%
Exercise 3: done 100%
Exercise 4: done 100%
Exercise 5: not mandatory