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.api.Response;
31 import org.eclipse.jetty.client.util.InputStreamResponseListener;
32 import org.eclipse.jetty.http.HttpHeader;
33
34 public class JettyRFC9457Reporter extends RFC9457Reporter<InputStreamResponseListener, HttpTransporterException> {
35 public static final JettyRFC9457Reporter INSTANCE = new JettyRFC9457Reporter();
36
37 private JettyRFC9457Reporter() {}
38
39 @Override
40 protected boolean isRFC9457Message(final InputStreamResponseListener listener) {
41 try {
42 Response response = listener.get(1, TimeUnit.SECONDS);
43 String contentType = response.getHeaders().get(HttpHeader.CONTENT_TYPE);
44 return hasRFC9457ContentType(contentType);
45 } catch (InterruptedException | TimeoutException | ExecutionException e) {
46 return false;
47 }
48 }
49
50 @Override
51 protected int getStatusCode(final InputStreamResponseListener listener) {
52 try {
53 Response response = listener.get(1, TimeUnit.SECONDS);
54 return response.getStatus();
55 } catch (InterruptedException | TimeoutException | ExecutionException e) {
56 return -1;
57 }
58 }
59
60 @Override
61 protected String getReasonPhrase(final InputStreamResponseListener listener) {
62 try {
63 Response response = listener.get(1, TimeUnit.SECONDS);
64 return response.getReason();
65 } catch (InterruptedException | TimeoutException | ExecutionException e) {
66 return null;
67 }
68 }
69
70 @Override
71 protected String getBody(final InputStreamResponseListener listener) throws IOException {
72 try (InputStream is = listener.getInputStream()) {
73 return new String(is.readAllBytes(), StandardCharsets.UTF_8);
74 }
75 }
76 }