Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRESOLVER-600] - RFC9457 Implementation #576

Merged
merged 9 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class looks awkward to me in SPI (not a -1 but...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I kind of agree but I was trying to ensure that the original exceptions (which are different for each transporter) are not lost and thought the wrapped solution is more convenient (albeit a little ugly) as it is a single call rather than the if/else that would have to replace it.

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 {
michael-o marked this conversation as resolved.
Show resolved Hide resolved
public static final RFC9457Payload INSTANCE = new RFC9457Payload();

private final URI type;
michael-o marked this conversation as resolved.
Show resolved Hide resolved

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