-
Notifications
You must be signed in to change notification settings - Fork 128
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
cstamas
merged 9 commits into
apache:master
from
doddi:MRESOLVER-600-RFC9457-implementation
Oct 12, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0b936f4
[MRESOLVER-600] - RFC9457 Implementation
doddi 2159e1f
Added parsing of the RFC 9457 content
doddi 620e357
Deserializer to establish behaviour of defaults as recommended in RFC…
doddi 53176c9
Renaming RFC
doddi a50eabb
Add toString so the payload bubbles up to the cli
doddi 1c7138e
Updated gson version
doddi cf8c504
Rename payload
doddi 156b095
Add UTF-8
doddi 9f3391e
As recommended opted to use static instance.
doddi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
.../main/java/org/eclipse/aether/spi/connector/transport/http/RFC9457/BiConsumerChecked.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
55 changes: 55 additions & 0 deletions
55
...in/java/org/eclipse/aether/spi/connector/transport/http/RFC9457/HttpRFC9457Exception.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
.../src/main/java/org/eclipse/aether/spi/connector/transport/http/RFC9457/RFC9457Parser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...src/main/java/org/eclipse/aether/spi/connector/transport/http/RFC9457/RFC9457Payload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '}'; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...rc/main/java/org/eclipse/aether/spi/connector/transport/http/RFC9457/RFC9457Reporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
.../test/java/org/eclipse/aether/spi/connector/transport/http/RFC9457/RFC9457ParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...)
There was a problem hiding this comment.
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.