Skip to content
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
5 changes: 5 additions & 0 deletions maven-resolver-transport-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-util</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.13.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
import org.eclipse.aether.spi.connector.transport.TransportTask;
import org.eclipse.aether.transfer.NoTransporterException;
import org.eclipse.aether.transfer.TransferCancelledException;
import org.eclipse.aether.transport.http.RFC9457.HttpRFC9457Exception;
import org.eclipse.aether.transport.http.RFC9457.RFC9457Reporter;
import org.eclipse.aether.util.ConfigUtils;
import org.eclipse.aether.util.FileUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -624,9 +626,12 @@ private <T extends HttpUriRequest> void resume(T request, GetTask task) {
}
}

private void handleStatus(CloseableHttpResponse response) throws HttpResponseException {
private void handleStatus(CloseableHttpResponse response) throws HttpResponseException, HttpRFC9457Exception {
int status = response.getStatusLine().getStatusCode();
if (status >= 300) {
if (RFC9457Reporter.INSTANCE.isRFC9457Message(response)) {
RFC9457Reporter.INSTANCE.generateException(response);
}
throw new HttpResponseException(status, response.getStatusLine().getReasonPhrase() + " (" + status + ")");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.transport.http.RFC9457;

import java.io.IOException;

/**
* Exception thrown by {@link HttpTransporter} in case of errors.
*
* @since 1.9.24
*/
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.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 final 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.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,80 @@
/*
* 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.transport.http.RFC9457;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

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

public boolean isRFC9457Message(CloseableHttpResponse response) {
Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);
if (headers.length > 0) {
String contentType = headers[0].getValue();
return hasRFC9457ContentType(contentType);
}
return false;
}

public void generateException(CloseableHttpResponse response) throws HttpRFC9457Exception {
int statusCode = getStatusCode(response);
String reasonPhrase = getReasonPhrase(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);
}

private String getBody(final CloseableHttpResponse response) throws IOException {
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
}

private int getStatusCode(final CloseableHttpResponse response) {
return response.getStatusLine().getStatusCode();
}

private String getReasonPhrase(final CloseableHttpResponse response) {
String reasonPhrase = response.getStatusLine().getReasonPhrase();
if (reasonPhrase == null || reasonPhrase.isEmpty()) {
return "";
}
int statusCode = getStatusCode(response);
return reasonPhrase + " (" + statusCode + ")";
}

private boolean hasRFC9457ContentType(String contentType) {
return "application/problem+json".equals(contentType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
Expand All @@ -35,6 +38,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.gson.Gson;
import org.eclipse.aether.transport.http.RFC9457.RFC9457Payload;
import org.eclipse.aether.util.ChecksumUtils;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
Expand Down Expand Up @@ -229,6 +234,7 @@ public HttpServer start() throws Exception {
handlers.addHandler(new AuthHandler());
handlers.addHandler(new RedirectHandler());
handlers.addHandler(new RepoHandler());
handlers.addHandler(new RFC9457Handler());

server = new Server();
httpConnector = new ServerConnector(server);
Expand Down Expand Up @@ -478,6 +484,46 @@ public void handle(String target, Request req, HttpServletRequest request, HttpS
}
}

private class RFC9457Handler extends AbstractHandler {
@Override
public void handle(
final String target,
final Request req,
final HttpServletRequest request,
final HttpServletResponse response)
throws IOException {
String path = req.getPathInfo().substring(1);

if (!path.startsWith("rfc9457/")) {
return;
}
req.setHandled(true);

if (HttpMethod.GET.is(req.getMethod())) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.setHeader(HttpHeader.CONTENT_TYPE.asString(), "application/problem+json");
RFC9457Payload rfc9457Payload;
if (path.endsWith("missing_fields.txt")) {
rfc9457Payload = new RFC9457Payload(null, null, null, null, null);
} else {
rfc9457Payload = new RFC9457Payload(
URI.create("https://example.com/probs/out-of-credit"),
HttpServletResponse.SC_FORBIDDEN,
"You do not have enough credit.",
"Your current balance is 30, but that costs 50.",
URI.create("/account/12345/msgs/abc"));
}
try (OutputStream outputStream = response.getOutputStream()) {
outputStream.write(buildRFC9457Message(rfc9457Payload).getBytes(StandardCharsets.UTF_8));
}
}
}
}

private String buildRFC9457Message(RFC9457Payload payload) {
return new Gson().toJson(payload, RFC9457Payload.class);
}

static boolean checkBasicAuth(String credentials, String username, String password) {
if (credentials != null) {
int space = credentials.indexOf(' ');
Expand Down
Loading