View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }