Skip to content

Commit 8be3269

Browse files
authored
Don't throw in low level client (#1253)
* Revert "JCL-402: RDF4J body handlers http error handling (#1162)" This reverts commit e03eb1e. * Revert "JCL-402: JSONB test coverage (#1184)" This reverts commit 52d485b. * Revert "JCL-402: HTTP error handling in JenaBodyHandlers (#1159)" This reverts commit 89534b4. * Move ProblemDetails to solid module, remove ClientHttpException * ProblemDetails to interface in API module
1 parent 2da19cc commit 8be3269

File tree

17 files changed

+353
-1131
lines changed

17 files changed

+353
-1131
lines changed

api/src/main/java/com/inrupt/client/ClientHttpException.java

Lines changed: 0 additions & 98 deletions
This file was deleted.

api/src/main/java/com/inrupt/client/Headers.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.inrupt.client.auth.Challenge;
2424
import com.inrupt.client.spi.ServiceProvider;
2525

26+
import java.io.Serializable;
2627
import java.net.URI;
2728
import java.util.Arrays;
2829
import java.util.Collections;
@@ -39,7 +40,9 @@
3940
/**
4041
* A read-only view of a collection of HTTP headers.
4142
*/
42-
public final class Headers {
43+
public final class Headers implements Serializable {
44+
45+
private static final long serialVersionUID = 3845207335727836025L;
4346

4447
private final NavigableMap<String, List<String>> data = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
4548

api/src/main/java/com/inrupt/client/ProblemDetails.java

Lines changed: 9 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -20,191 +20,52 @@
2020
*/
2121
package com.inrupt.client;
2222

23-
import com.inrupt.client.spi.JsonService;
24-
import com.inrupt.client.spi.ServiceProvider;
25-
26-
import java.io.ByteArrayInputStream;
27-
import java.io.IOException;
28-
import java.io.Serializable;
2923
import java.net.URI;
30-
import java.util.Optional;
3124

3225
/**
3326
* A data class representing a structured problem description sent by the server on error response.
3427
*
3528
* @see <a href="https://www.rfc-editor.org/rfc/rfc9457">RFC 9457 Problem Details for HTTP APIs</a>
3629
*/
37-
public class ProblemDetails implements Serializable {
38-
39-
private static final long serialVersionUID = -4597170432270957765L;
30+
public interface ProblemDetails {
4031

4132
/**
4233
* The <a href="https://www.rfc-editor.org/rfc/rfc9457">RFC9457</a> default MIME type.
4334
*/
44-
public static final String MIME_TYPE = "application/problem+json";
45-
/**
46-
* The <a href="https://www.rfc-editor.org/rfc/rfc9457">RFC9457</a> default problem type.
47-
*/
48-
public static final String DEFAULT_TYPE = "about:blank";
49-
private final URI type;
50-
private final String title;
51-
private final String details;
52-
private final int status;
53-
private final URI instance;
54-
private static JsonService jsonService;
55-
private static boolean isJsonServiceInitialized;
35+
String MIME_TYPE = "application/problem+json";
5636

5737
/**
58-
* Build a ProblemDetails instance providing the expected fields as described in
59-
* <a href="https://www.rfc-editor.org/rfc/rfc9457">RFC9457</a>.
60-
* @param type the problem type
61-
* @param title the problem title
62-
* @param details the problem details
63-
* @param status the error response status code
64-
* @param instance the problem instance
38+
* The <a href="https://www.rfc-editor.org/rfc/rfc9457">RFC9457</a> default problem type.
6539
*/
66-
public ProblemDetails(
67-
final URI type,
68-
final String title,
69-
final String details,
70-
final int status,
71-
final URI instance
72-
) {
73-
this.type = type;
74-
this.title = title;
75-
this.details = details;
76-
this.status = status;
77-
this.instance = instance;
78-
}
40+
String DEFAULT_TYPE = "about:blank";
7941

8042
/**
8143
* The problem type.
8244
* @return the type
8345
*/
84-
public URI getType() {
85-
return this.type;
86-
}
46+
URI getType();
8747

8848
/**
8949
* The problem title.
9050
* @return the title
9151
*/
92-
public String getTitle() {
93-
return this.title;
94-
}
52+
String getTitle();
9553

9654
/**
9755
* The problem details.
9856
* @return the details
9957
*/
100-
public String getDetails() {
101-
return this.details;
102-
}
58+
String getDetails();
10359

10460
/**
10561
* The problem status code.
10662
* @return the status code
10763
*/
108-
public int getStatus() {
109-
return this.status;
110-
}
64+
int getStatus();
11165

11266
/**
11367
* The problem instance.
11468
* @return the instance
11569
*/
116-
public URI getInstance() {
117-
return this.instance;
118-
}
119-
120-
/**
121-
* This inner class is only ever used for JSON deserialization. Please do not use in any other context.
122-
*/
123-
public static class Data {
124-
/**
125-
* The problem type.
126-
*/
127-
public URI type;
128-
/**
129-
* The problem title.
130-
*/
131-
public String title;
132-
/**
133-
* The problem details.
134-
*/
135-
public String details;
136-
/**
137-
* The problem status code.
138-
*/
139-
public int status;
140-
/**
141-
* The problem instance.
142-
*/
143-
public URI instance;
144-
}
145-
146-
private static JsonService getJsonService() {
147-
if (ProblemDetails.isJsonServiceInitialized) {
148-
return ProblemDetails.jsonService;
149-
}
150-
// It is a legitimate use case that this is loaded in a context where no implementation of the JSON service is
151-
// available. On SPI lookup failure, the ProblemDetails exceptions will fall back to default and not be parsed.
152-
try {
153-
ProblemDetails.jsonService = ServiceProvider.getJsonService();
154-
} catch (IllegalStateException e) {
155-
ProblemDetails.jsonService = null;
156-
}
157-
ProblemDetails.isJsonServiceInitialized = true;
158-
return ProblemDetails.jsonService;
159-
}
160-
161-
/**
162-
* Builds a {@link ProblemDetails} instance from an HTTP error response.
163-
* @param statusCode the HTTP error response status code
164-
* @param headers the HTTP error response headers
165-
* @param body the HTTP error response body
166-
* @return a {@link ProblemDetails} instance
167-
*/
168-
public static ProblemDetails fromErrorResponse(
169-
final int statusCode,
170-
final Headers headers,
171-
final byte[] body
172-
) {
173-
final JsonService jsonService = getJsonService();
174-
if (jsonService == null
175-
|| (headers != null && !headers.allValues("Content-Type").contains(ProblemDetails.MIME_TYPE))) {
176-
return new ProblemDetails(
177-
URI.create(ProblemDetails.DEFAULT_TYPE),
178-
null,
179-
null,
180-
statusCode,
181-
null
182-
);
183-
}
184-
try {
185-
final Data pdData = jsonService.fromJson(
186-
new ByteArrayInputStream(body),
187-
Data.class
188-
);
189-
final URI type = Optional.ofNullable(pdData.type)
190-
.orElse(URI.create(ProblemDetails.DEFAULT_TYPE));
191-
// JSON mappers map invalid integers to 0, which is an invalid value in our case anyway.
192-
final int status = Optional.of(pdData.status).filter(s -> s != 0).orElse(statusCode);
193-
return new ProblemDetails(
194-
type,
195-
pdData.title,
196-
pdData.details,
197-
status,
198-
pdData.instance
199-
);
200-
} catch (IOException e) {
201-
return new ProblemDetails(
202-
URI.create(ProblemDetails.DEFAULT_TYPE),
203-
null,
204-
null,
205-
statusCode,
206-
null
207-
);
208-
}
209-
}
70+
URI getInstance();
21071
}

api/src/main/java/com/inrupt/client/Response.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,6 @@ interface ResponseInfo {
9595
ByteBuffer body();
9696
}
9797

98-
/**
99-
* Indicates whether a status code reflects a successful HTTP response.
100-
* @param statusCode the HTTP response status code
101-
* @return true if the status code is in the success range, namely [200, 299].
102-
*/
103-
static boolean isSuccess(final int statusCode) {
104-
return statusCode >= 200 && statusCode < 300;
105-
}
106-
10798
/**
10899
* An interface for mapping an HTTP response into a specific Java type.
109100
* @param <T> the body type

0 commit comments

Comments
 (0)