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.InputStreamResponseListener;
031import org.eclipse.jetty.client.Request;
032import org.eclipse.jetty.client.Response;
033import org.eclipse.jetty.http.HttpHeader;
034
035public class JettyRFC9457Reporter
036        extends RFC9457Reporter<InputStreamResponseListener, HttpTransporterException, Request> {
037    public static final JettyRFC9457Reporter INSTANCE = new JettyRFC9457Reporter();
038
039    private JettyRFC9457Reporter() {}
040
041    @Override
042    public void prepareRequest(Request request) {
043        request.headers(h -> h.add(HttpHeader.ACCEPT.asString(), CONTENT_TYPE_PROBLEM_DETAILS_JSON));
044    }
045
046    @Override
047    protected boolean isRFC9457Message(final InputStreamResponseListener listener) {
048        try {
049            Response response = listener.get(1, TimeUnit.SECONDS);
050            String contentType = response.getHeaders().get(HttpHeader.CONTENT_TYPE);
051            return hasRFC9457ContentType(contentType);
052        } catch (InterruptedException | TimeoutException | ExecutionException e) {
053            return false;
054        }
055    }
056
057    @Override
058    protected int getStatusCode(final InputStreamResponseListener listener) {
059        try {
060            Response response = listener.get(1, TimeUnit.SECONDS);
061            return response.getStatus();
062        } catch (InterruptedException | TimeoutException | ExecutionException e) {
063            return -1;
064        }
065    }
066
067    @Override
068    protected String getReasonPhrase(final InputStreamResponseListener listener) {
069        try {
070            Response response = listener.get(1, TimeUnit.SECONDS);
071            return response.getReason();
072        } catch (InterruptedException | TimeoutException | ExecutionException e) {
073            return null;
074        }
075    }
076
077    @Override
078    protected String getBody(final InputStreamResponseListener listener) throws IOException {
079        try (InputStream is = listener.getInputStream()) {
080            return new String(is.readAllBytes(), StandardCharsets.UTF_8);
081        }
082    }
083}