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.http.RFC9457;
020
021import java.io.IOException;
022import java.nio.charset.StandardCharsets;
023
024import org.apache.http.Header;
025import org.apache.http.HttpHeaders;
026import org.apache.http.client.methods.CloseableHttpResponse;
027import org.apache.http.util.EntityUtils;
028
029public class RFC9457Reporter {
030    public static final RFC9457Reporter INSTANCE = new RFC9457Reporter();
031
032    public boolean isRFC9457Message(CloseableHttpResponse response) {
033        Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
034        if (headers.length > 0) {
035            String contentType = headers[0].getValue();
036            return hasRFC9457ContentType(contentType);
037        }
038        return false;
039    }
040
041    public void generateException(CloseableHttpResponse response) throws HttpRFC9457Exception {
042        int statusCode = getStatusCode(response);
043        String reasonPhrase = getReasonPhrase(response);
044
045        String body;
046        try {
047            body = getBody(response);
048        } catch (IOException ignore) {
049            // No body found but it is representing a RFC 9457 message due to the content type.
050            throw new HttpRFC9457Exception(statusCode, reasonPhrase, RFC9457Payload.INSTANCE);
051        }
052
053        if (body != null && !body.isEmpty()) {
054            RFC9457Payload rfc9457Payload = RFC9457Parser.parse(body);
055            throw new HttpRFC9457Exception(statusCode, reasonPhrase, rfc9457Payload);
056        }
057        throw new HttpRFC9457Exception(statusCode, reasonPhrase, RFC9457Payload.INSTANCE);
058    }
059
060    private String getBody(final CloseableHttpResponse response) throws IOException {
061        return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
062    }
063
064    private int getStatusCode(final CloseableHttpResponse response) {
065        return response.getStatusLine().getStatusCode();
066    }
067
068    private String getReasonPhrase(final CloseableHttpResponse response) {
069        String reasonPhrase = response.getStatusLine().getReasonPhrase();
070        if (reasonPhrase == null || reasonPhrase.isEmpty()) {
071            return "";
072        }
073        int statusCode = getStatusCode(response);
074        return reasonPhrase + " (" + statusCode + ")";
075    }
076
077    private boolean hasRFC9457ContentType(String contentType) {
078        return "application/problem+json".equals(contentType);
079    }
080}