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.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  }