Skip to content

Commit

Permalink
[MRESOLVER-600] RFC9457 Implementation (#576)
Browse files Browse the repository at this point in the history
Added an RFC9457Reporter for each of the 3  Transporters.

At the moment I tried to keep the original exception messages if the error response is not an [RFC9457](https://www.rfc-editor.org/rfc/rfc9457) type. This did create a little more complexity in the implementation than is perhaps necessary.
If the exceptions to be thrown can be consisted throughout the transporters then this PR could also be simplified considerably. At the moment I have taken the stance not to make any potential breaking changes in messaging.

---

https://issues.apache.org/jira/browse/MRESOLVER-600
  • Loading branch information
doddi authored Oct 12, 2024
1 parent 548ee0b commit 92b46ba
Show file tree
Hide file tree
Showing 19 changed files with 757 additions and 8 deletions.
4 changes: 4 additions & 0 deletions maven-resolver-spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.spi.connector.transport.http.RFC9457;

@FunctionalInterface
public interface BiConsumerChecked<T, U, E extends Exception> {
void accept(T t, U u) throws E;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.spi.connector.transport.http.RFC9457;

import java.io.IOException;

import org.eclipse.aether.spi.connector.transport.http.HttpTransporter;

/**
* Exception thrown by {@link HttpTransporter} in case of errors.
*
* @since 2.0.2
*/
public class HttpRFC9457Exception extends IOException {
private final int statusCode;

private final String reasonPhrase;

private final RFC9457Payload payload;

public HttpRFC9457Exception(int statusCode, String reasonPhrase, RFC9457Payload payload) {
super(payload.toString());
this.statusCode = statusCode;
this.reasonPhrase = reasonPhrase;
this.payload = payload;
}

public int getStatusCode() {
return statusCode;
}

public String getReasonPhrase() {
return reasonPhrase;
}

public RFC9457Payload getPayload() {
return payload;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.spi.connector.transport.http.RFC9457;

import java.lang.reflect.Type;
import java.net.URI;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

public class RFC9457Parser {
private static Gson gson = new GsonBuilder()
.registerTypeAdapter(RFC9457Payload.class, new RFC9457PayloadAdapter())
.create();

public static RFC9457Payload parse(String data) {
return gson.fromJson(data, RFC9457Payload.class);
}

private static class RFC9457PayloadAdapter implements JsonDeserializer<RFC9457Payload> {
@Override
public RFC9457Payload deserialize(
final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
throws JsonParseException {
JsonObject asJsonObject = json.getAsJsonObject();
URI type = parseNullableURI(asJsonObject, "type", "about:blank");
Integer status = parseStatus(asJsonObject);
String title = parseNullableString(asJsonObject, "title");
String detail = parseNullableString(asJsonObject, "detail");
URI instance = parseNullableURI(asJsonObject, "instance", null);
return new RFC9457Payload(type, status, title, detail, instance);
}
}

private static Integer parseStatus(JsonObject jsonObject) {
return jsonObject.get("status") == null || jsonObject.get("status").isJsonNull()
? null
: jsonObject.get("status").getAsInt();
}

private static String parseNullableString(JsonObject jsonObject, String key) {
return jsonObject.get(key) == null || jsonObject.get(key).isJsonNull()
? null
: jsonObject.get(key).getAsString();
}

private static URI parseNullableURI(JsonObject jsonObject, String key, String defaultValue) {
return !jsonObject.has(key) || jsonObject.get(key).isJsonNull()
? defaultValue != null ? URI.create(defaultValue) : null
: URI.create(jsonObject.get(key).getAsString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.spi.connector.transport.http.RFC9457;

import java.net.URI;

public class RFC9457Payload {
public static final RFC9457Payload INSTANCE = new RFC9457Payload();

private final URI type;

private final Integer status;

private final String title;

private final String detail;

private final URI instance;

private RFC9457Payload() {
this(null, null, null, null, null);
}

public RFC9457Payload(
final URI type, final Integer status, final String title, final String detail, final URI instance) {
this.type = type;
this.status = status;
this.title = title;
this.detail = detail;
this.instance = instance;
}

public URI getType() {
return type;
}

public Integer getStatus() {
return status;
}

public String getTitle() {
return title;
}

public String getDetail() {
return detail;
}

public URI getInstance() {
return instance;
}

@Override
public String toString() {
return "RFC9457Payload {" + "type="
+ type + ", status="
+ status + ", title='"
+ title + ", detail='"
+ detail + ", instance="
+ instance + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.spi.connector.transport.http.RFC9457;

import java.io.IOException;

/**
* A reporter for RFC 9457 messages.
* RFC 9457 is a standard for reporting problems in HTTP responses as a JSON object.
* There are members specified in the RFC but none of those appear to be required,
* @see <a href=https://www.rfc-editor.org/rfc/rfc9457#section-3-7>rfc9457 section 3.7</a>
* Given the JSON fields are not mandatory, this reporter simply extracts the body of the
* response without validation.
* A RFC 9457 message is detected by the content type "application/problem+json".
*
* @param <T> The type of the response.
* @param <E> The base exception type to throw if the response is not a RFC9457 message.
*/
public abstract class RFC9457Reporter<T, E extends Exception> {
protected abstract boolean isRFC9457Message(T response);

protected abstract int getStatusCode(T response);

protected abstract String getReasonPhrase(T response);

protected abstract String getBody(T response) throws IOException;

protected boolean hasRFC9457ContentType(String contentType) {
return "application/problem+json".equals(contentType);
}

/**
* Generates a {@link HttpRFC9457Exception} if the response type is a RFC 9457 message.
* Otherwise, it throws the base exception
*
* @param response The response to check for RFC 9457 messages.
* @param baseException The base exception to throw if the response is not a RFC 9457 message.
*/
public void generateException(T response, BiConsumerChecked<Integer, String, E> baseException)
throws E, HttpRFC9457Exception {
int statusCode = getStatusCode(response);
String reasonPhrase = getReasonPhrase(response);

if (isRFC9457Message(response)) {
String body;
try {
body = getBody(response);
} catch (IOException ignore) {
// No body found but it is representing a RFC 9457 message due to the content type.
throw new HttpRFC9457Exception(statusCode, reasonPhrase, RFC9457Payload.INSTANCE);
}

if (body != null && !body.isEmpty()) {
RFC9457Payload rfc9457Payload = RFC9457Parser.parse(body);
throw new HttpRFC9457Exception(statusCode, reasonPhrase, rfc9457Payload);
}
throw new HttpRFC9457Exception(statusCode, reasonPhrase, RFC9457Payload.INSTANCE);
}
baseException.accept(statusCode, reasonPhrase);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.spi.connector.transport.http.RFC9457;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class RFC9457ParserTest {
@Test
void testParseAllFields() {
String data =
"{\"type\":\"https://example.com/error\",\"status\":400,\"title\":\"Bad Request\",\"detail\":\"The request could not be understood by the server due to malformed syntax.\",\"instance\":\"https://example.com/error/400\"}";
RFC9457Payload payload = RFC9457Parser.parse(data);

assertEquals("https://example.com/error", payload.getType().toString());
assertEquals(400, payload.getStatus());
assertEquals("Bad Request", payload.getTitle());
assertEquals("The request could not be understood by the server due to malformed syntax.", payload.getDetail());
assertEquals("https://example.com/error/400", payload.getInstance().toString());
}

@Test
void testParseWithMissingFields() {
String data = "{\"type\":\"https://example.com/other_error\",\"status\":403}";
RFC9457Payload payload = RFC9457Parser.parse(data);

assertEquals("https://example.com/other_error", payload.getType().toString());
assertEquals(403, payload.getStatus());
assertNull(payload.getTitle());
assertNull(payload.getDetail());
assertNull(payload.getInstance());
}

@Test
void testParseWithNoFields() {
String data = "{}";
RFC9457Payload payload = RFC9457Parser.parse(data);

assertEquals("about:blank", payload.getType().toString());
assertNull(payload.getStatus());
assertNull(payload.getTitle());
assertNull(payload.getDetail());
assertNull(payload.getInstance());
}

@Test
void testParseWithNullFields() {
String data = "{\"type\":null,\"status\":null,\"title\":null,\"detail\":null,\"instance\":null}";
RFC9457Payload payload = RFC9457Parser.parse(data);

assertEquals("about:blank", payload.getType().toString());
assertNull(payload.getStatus());
assertNull(payload.getTitle());
assertNull(payload.getDetail());
assertNull(payload.getInstance());
}
}
Loading

0 comments on commit 92b46ba

Please sign in to comment.