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