1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.transport.jetty;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.charset.StandardCharsets;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
27
28 import org.eclipse.aether.spi.connector.transport.http.HttpTransporterException;
29 import org.eclipse.aether.spi.connector.transport.http.RFC9457.RFC9457Reporter;
30 import org.eclipse.jetty.client.InputStreamResponseListener;
31 import org.eclipse.jetty.client.Request;
32 import org.eclipse.jetty.client.Response;
33 import org.eclipse.jetty.http.HttpHeader;
34
35 public class JettyRFC9457Reporter
36 extends RFC9457Reporter<InputStreamResponseListener, HttpTransporterException, Request> {
37 public static final JettyRFC9457Reporter INSTANCE = new JettyRFC9457Reporter();
38
39 private JettyRFC9457Reporter() {}
40
41 @Override
42 public void prepareRequest(Request request) {
43 request.headers(h -> h.add(HttpHeader.ACCEPT.asString(), CONTENT_TYPE_PROBLEM_DETAILS_JSON));
44 }
45
46 @Override
47 protected boolean isRFC9457Message(final InputStreamResponseListener listener) {
48 try {
49 Response response = listener.get(1, TimeUnit.SECONDS);
50 String contentType = response.getHeaders().get(HttpHeader.CONTENT_TYPE);
51 return hasRFC9457ContentType(contentType);
52 } catch (InterruptedException | TimeoutException | ExecutionException e) {
53 return false;
54 }
55 }
56
57 @Override
58 protected int getStatusCode(final InputStreamResponseListener listener) {
59 try {
60 Response response = listener.get(1, TimeUnit.SECONDS);
61 return response.getStatus();
62 } catch (InterruptedException | TimeoutException | ExecutionException e) {
63 return -1;
64 }
65 }
66
67 @Override
68 protected String getReasonPhrase(final InputStreamResponseListener listener) {
69 try {
70 Response response = listener.get(1, TimeUnit.SECONDS);
71 return response.getReason();
72 } catch (InterruptedException | TimeoutException | ExecutionException e) {
73 return null;
74 }
75 }
76
77 @Override
78 protected String getBody(final InputStreamResponseListener listener) throws IOException {
79 try (InputStream is = listener.getInputStream()) {
80 return new String(is.readAllBytes(), StandardCharsets.UTF_8);
81 }
82 }
83 }