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