Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework5. Тяпуев Дмитрий. Магистратура. Политех #189

Merged
merged 49 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
d742016
added my DAO and http rest api
typuichik123 Feb 20, 2024
eccd9ba
some fixes for codeclimate
typuichik123 Feb 20, 2024
a3ac664
all done
typuichik123 Feb 21, 2024
aa4837c
Merge branch 'main' into homework2
typuichik123 Feb 24, 2024
c17c0af
fix code after hw1 review
typuichik123 Feb 27, 2024
3e754c2
fix report after hw1 review
typuichik123 Feb 28, 2024
dbf04ff
homework №2 done
typuichik123 Feb 28, 2024
7e8af1b
Merge branch 'main' into homework2
typuichik123 Feb 28, 2024
3ce2242
fixes
typuichik123 Feb 28, 2024
7e4ab6b
fixe codeclimate
typuichik123 Feb 28, 2024
0544e1e
all done
typuichik123 Mar 5, 2024
991b7c3
Merge branch 'main' into homework3
typuichik123 Mar 7, 2024
3c8aca9
hw3 done
typuichik123 Mar 11, 2024
7c329b0
fix codeclimate
typuichik123 Mar 11, 2024
d071de9
Merge branch 'main' into homework3
typuichik123 Mar 20, 2024
92b7a93
add report
typuichik123 Mar 20, 2024
d51d05e
fix old file
typuichik123 Mar 20, 2024
f8d0a34
fix report
typuichik123 Mar 20, 2024
261265a
Merge branch 'main' into homework4
typuichik123 Mar 24, 2024
598124e
hw4 done
typuichik123 Mar 28, 2024
2940e04
Merge branch 'main' into homework4
typuichik123 Mar 28, 2024
638f643
fix codeclimate
typuichik123 Mar 28, 2024
243fc96
Merge remote-tracking branch 'origin/homework4' into homework4
typuichik123 Mar 28, 2024
7ad6100
add report
typuichik123 Apr 3, 2024
800387f
Merge branch 'main' into homework5
typuichik123 Apr 9, 2024
c1adde7
hw5 done
typuichik123 Apr 11, 2024
2e2dd90
fix codeclimate
typuichik123 Apr 11, 2024
73288e2
fix codeclimate
typuichik123 Apr 11, 2024
2d49d1f
fix codeclimate
typuichik123 Apr 11, 2024
d898779
fix codeclimate
typuichik123 Apr 11, 2024
1fcca52
Merge branch 'main' into homework5
incubos Apr 13, 2024
9eca94a
fixes after review and report
typuichik123 Apr 16, 2024
1971c10
Merge remote-tracking branch 'origin/homework5' into homework5
typuichik123 Apr 16, 2024
97a0adb
Merge branch 'main' into homework5
typuichik123 Apr 16, 2024
4622e77
fixes
typuichik123 Apr 16, 2024
2256ba0
empty commit
typuichik123 Apr 16, 2024
0f4b415
empty commit
typuichik123 Apr 17, 2024
8ce65a6
try to fix
typuichik123 Apr 17, 2024
5518965
try to fix
typuichik123 Apr 17, 2024
9b6e6eb
try to fix
typuichik123 Apr 17, 2024
fbcfca9
check old realization
typuichik123 Apr 17, 2024
5d9aaf8
try to fix
typuichik123 Apr 17, 2024
360e4f5
fixes
typuichik123 Apr 17, 2024
f320645
Merge branch 'main' into homework5
typuichik123 May 9, 2024
62aa9f9
fix
typuichik123 May 9, 2024
7c58e12
Merge remote-tracking branch 'origin/homework5' into homework5
typuichik123 May 9, 2024
7245b9d
fix
typuichik123 May 9, 2024
43e2fe0
Merge branch 'main' into homework5
typuichik123 May 18, 2024
f18f692
Merge branch 'main' into homework5
incubos May 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions src/main/java/ru/vk/itmo/test/tyapuevdmitrij/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package ru.vk.itmo.test.tyapuevdmitrij;

import one.nio.http.Request;
import one.nio.http.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.HttpURLConnection;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

public class Client {
private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);

public static final String PROXY_TIMESTAMP_HEADER = "proxy";

public static final String NODE_TIMESTAMP_HEADER = "node";

private static final byte[] EMPTY_BODY = new byte[0];
private final HttpClient httpClient;

private String url;

private long timeStamp;

private final ExecutorService proxyExecutor;

Client(ExecutorService proxyExecutor) {
this.httpClient = HttpClient.newBuilder().build();
this.proxyExecutor = proxyExecutor;
}

public CompletableFuture<Response> handleProxyRequest(Request request) {
CompletableFuture<HttpResponse<byte[]>> responseFuture = getProxyResponse(request);
return responseFuture.thenApplyAsync(response -> {
if (response == null) {
return new Response(Response.INTERNAL_ERROR, Response.EMPTY);
}
String statusCode = switch (response.statusCode()) {
case HttpURLConnection.HTTP_OK -> Response.OK;
case HttpURLConnection.HTTP_CREATED -> Response.CREATED;
case HttpURLConnection.HTTP_ACCEPTED -> Response.ACCEPTED;
case HttpURLConnection.HTTP_BAD_REQUEST -> Response.BAD_REQUEST;
case HttpURLConnection.HTTP_NOT_FOUND -> Response.NOT_FOUND;
case HttpURLConnection.HTTP_INTERNAL_ERROR -> Response.INTERNAL_ERROR;
default -> throw new IllegalStateException("Unexpected value: " + response.statusCode());
};
Optional<String> nodeHeader = response.headers().firstValue(NODE_TIMESTAMP_HEADER);
Response finalResponse = new Response(statusCode, response.body());
if (nodeHeader.isPresent()) {
long time = Long.parseLong(nodeHeader.get());
finalResponse.addHeader(Client.NODE_TIMESTAMP_HEADER + ":" + time);
}
return finalResponse;
}, proxyExecutor).exceptionally(throwable -> {
LOGGER.error("can't reach target node", throwable);
return new Response(Response.INTERNAL_ERROR, Response.EMPTY);
});
}

public void setUrl(String url) {
this.url = url;
}

public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}

private byte[] getRequestBody(Request request) {
return request.getBody() == null ? EMPTY_BODY : request.getBody();
}

private HttpRequest getProxyRequest(Request request) {
return HttpRequest.newBuilder(URI.create(url + request.getURI()))
.method(request.getMethodName(),
HttpRequest.BodyPublishers.ofByteArray(getRequestBody(request)))
.header(PROXY_TIMESTAMP_HEADER, String.valueOf(timeStamp))
.build();
}

private CompletableFuture<HttpResponse<byte[]>> getProxyResponse(Request request) {
return httpClient.sendAsync(getProxyRequest(request),
HttpResponse.BodyHandlers.ofByteArray());
}
}
Loading
Loading