001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.transport.jetty;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.nio.charset.StandardCharsets;
024import java.util.concurrent.ExecutionException;
025import java.util.concurrent.TimeUnit;
026import java.util.concurrent.TimeoutException;
027
028import org.eclipse.aether.spi.connector.transport.http.HttpTransporterException;
029import org.eclipse.aether.spi.connector.transport.http.RFC9457.RFC9457Reporter;
030import org.eclipse.jetty.client.api.Response;
031import org.eclipse.jetty.client.util.InputStreamResponseListener;
032import org.eclipse.jetty.http.HttpHeader;
033
034public class JettyRFC9457Reporter extends RFC9457Reporter<InputStreamResponseListener, HttpTransporterException> {
035    public static final JettyRFC9457Reporter INSTANCE = new JettyRFC9457Reporter();
036
037    private JettyRFC9457Reporter() {}
038
039    @Override
040    protected boolean isRFC9457Message(final InputStreamResponseListener listener) {
041        try {
042            Response response = listener.get(1, TimeUnit.SECONDS);
043            String contentType = response.getHeaders().get(HttpHeader.CONTENT_TYPE);
044            return hasRFC9457ContentType(contentType);
045        } catch (InterruptedException | TimeoutException | ExecutionException e) {
046            return false;
047        }
048    }
049
050    @Override
051    protected int getStatusCode(final InputStreamResponseListener listener) {
052        try {
053            Response response = listener.get(1, TimeUnit.SECONDS);
054            return response.getStatus();
055        } catch (InterruptedException | TimeoutException | ExecutionException e) {
056            return -1;
057        }
058    }
059
060    @Override
061    protected String getReasonPhrase(final InputStreamResponseListener listener) {
062        try {
063            Response response = listener.get(1, TimeUnit.SECONDS);
064            return response.getReason();
065        } catch (InterruptedException | TimeoutException | ExecutionException e) {
066            return null;
067        }
068    }
069
070    @Override
071    protected String getBody(final InputStreamResponseListener listener) throws IOException {
072        try (InputStream is = listener.getInputStream()) {
073            return new String(is.readAllBytes(), StandardCharsets.UTF_8);
074        }
075    }
076}