2020 */
2121package com .inrupt .client .solid ;
2222
23- import static java .nio .charset .StandardCharsets .UTF_8 ;
24-
25- import com .inrupt .client .Client ;
26- import com .inrupt .client .ClientProvider ;
27- import com .inrupt .client .Headers ;
28- import com .inrupt .client .RDFSource ;
29- import com .inrupt .client .Request ;
30- import com .inrupt .client .Resource ;
31- import com .inrupt .client .Response ;
32- import com .inrupt .client .ValidationResult ;
23+ import com .inrupt .client .*;
3324import com .inrupt .client .auth .Session ;
3425
3526import java .io .ByteArrayInputStream ;
3627import java .io .IOException ;
3728import java .io .InputStream ;
3829import java .net .URI ;
30+ import java .nio .charset .StandardCharsets ;
3931import java .util .Collections ;
4032import java .util .List ;
4133import java .util .Map ;
@@ -131,36 +123,43 @@ public <T extends Resource> CompletionStage<T> read(final URI identifier, final
131123 headers .firstValue (USER_AGENT ).ifPresent (agent -> builder .setHeader (USER_AGENT , agent ));
132124
133125 final Request request = builder .build ();
134- return client .send (request , Response .BodyHandlers .ofByteArray ())
135- .thenApply (response -> {
136- if (response .statusCode () >= ERROR_STATUS ) {
137- throw SolidClientException .handle ("Unable to read resource at " + request .uri (), request .uri (),
138- response .statusCode (), response .headers (), new String (response .body ()));
139- } else {
140- final String contentType = response .headers ().firstValue (CONTENT_TYPE )
141- .orElse ("application/octet-stream" );
142- try {
143- // Check that this is an RDFSoure
144- if (RDFSource .class .isAssignableFrom (clazz )) {
145- final Dataset dataset = SolidResourceHandlers .buildDataset (contentType , response .body (),
146- request .uri ().toString ()).orElse (null );
147- final T obj = construct (request .uri (), clazz , dataset , response .headers ());
148- final ValidationResult res = RDFSource .class .cast (obj ).validate ();
149- if (!res .isValid ()) {
150- throw new DataMappingException (
151- "Unable to map resource into type: [" + clazz .getSimpleName () + "] " ,
152- res .getResults ());
153- }
154- return obj ;
155- // Otherwise, create a non-RDF-bearing resource
156- } else {
157- return construct (request .uri (), clazz , contentType ,
158- new ByteArrayInputStream (response .body ()), response .headers ());
126+ return client .send (
127+ request ,
128+ Response .BodyHandlers .ofByteArray ()
129+ ).thenApply (response -> {
130+ if (!Response .isSuccess (response .statusCode ())) {
131+ throw SolidClientException .handle (
132+ "Reading resource failed." ,
133+ response .uri (),
134+ response .statusCode (),
135+ response .headers (),
136+ new String (response .body (), StandardCharsets .UTF_8 )
137+ );
138+ }
139+
140+ final String contentType = response .headers ().firstValue (CONTENT_TYPE )
141+ .orElse ("application/octet-stream" );
142+ try {
143+ // Check that this is an RDFSoure
144+ if (RDFSource .class .isAssignableFrom (clazz )) {
145+ final Dataset dataset = SolidResourceHandlers .buildDataset (contentType , response .body (),
146+ request .uri ().toString ()).orElse (null );
147+ final T obj = construct (request .uri (), clazz , dataset , response .headers ());
148+ final ValidationResult res = RDFSource .class .cast (obj ).validate ();
149+ if (!res .isValid ()) {
150+ throw new DataMappingException (
151+ "Unable to map resource into type: [" + clazz .getSimpleName () + "] " ,
152+ res .getResults ());
159153 }
160- } catch (final ReflectiveOperationException ex ) {
161- throw new SolidResourceException ("Unable to read resource into type " + clazz .getName (),
162- ex );
154+ return obj ;
155+ // Otherwise, create a non-RDF-bearing resource
156+ } else {
157+ return construct (request .uri (), clazz , contentType ,
158+ new ByteArrayInputStream (response .body ()), response .headers ());
163159 }
160+ } catch (final ReflectiveOperationException ex ) {
161+ throw new SolidResourceException ("Unable to read resource into type " + clazz .getName (),
162+ ex );
164163 }
165164 });
166165 }
@@ -280,13 +279,21 @@ public <T extends Resource> CompletionStage<Void> delete(final T resource, final
280279 defaultHeaders .firstValue (USER_AGENT ).ifPresent (agent -> builder .setHeader (USER_AGENT , agent ));
281280 headers .firstValue (USER_AGENT ).ifPresent (agent -> builder .setHeader (USER_AGENT , agent ));
282281
283- return client .send (builder .build (), Response .BodyHandlers .ofByteArray ()).thenApply (res -> {
284- if (isSuccess (res .statusCode ())) {
285- return null ;
286- } else {
287- throw SolidClientException .handle ("Unable to delete resource" , resource .getIdentifier (),
288- res .statusCode (), res .headers (), new String (res .body (), UTF_8 ));
282+
283+ return client .send (
284+ builder .build (),
285+ Response .BodyHandlers .ofByteArray ()
286+ ).thenApply (response -> {
287+ if (!Response .isSuccess (response .statusCode ())) {
288+ throw SolidClientException .handle (
289+ "Deleting resource failed." ,
290+ response .uri (),
291+ response .statusCode (),
292+ response .headers (),
293+ new String (response .body (), StandardCharsets .UTF_8 )
294+ );
289295 }
296+ return null ;
290297 });
291298 }
292299
@@ -370,9 +377,14 @@ public SolidClient build() {
370377 <T extends Resource > Function <Response <byte []>, CompletionStage <T >> handleResponse (final T resource ,
371378 final Headers headers , final String message ) {
372379 return res -> {
373- if (!isSuccess (res .statusCode ())) {
374- throw SolidClientException .handle (message , resource .getIdentifier (),
375- res .statusCode (), res .headers (), new String (res .body (), UTF_8 ));
380+ if (!Response .isSuccess (res .statusCode ())) {
381+ throw SolidClientException .handle (
382+ message ,
383+ resource .getIdentifier (),
384+ res .statusCode (),
385+ res .headers (),
386+ new String (res .body (), StandardCharsets .UTF_8 )
387+ );
376388 }
377389
378390 if (!fetchAfterWrite ) {
@@ -382,7 +394,6 @@ <T extends Resource> Function<Response<byte[]>, CompletionStage<T>> handleRespon
382394 @ SuppressWarnings ("unchecked" )
383395 final Class <T > clazz = (Class <T >) resource .getClass ();
384396 return read (resource .getIdentifier (), headers , clazz );
385-
386397 };
387398 }
388399
@@ -445,10 +456,6 @@ static void decorateHeaders(final Request.Builder builder, final Headers headers
445456 }
446457 }
447458
448- static boolean isSuccess (final int statusCode ) {
449- return statusCode >= 200 && statusCode < 300 ;
450- }
451-
452459 static Request .BodyPublisher cast (final Resource resource ) {
453460 try {
454461 return Request .BodyPublishers .ofInputStream (resource .getEntity ());
0 commit comments