2020 */
2121package com .inrupt .client .jena ;
2222
23+ import com .inrupt .client .ClientHttpException ;
2324import com .inrupt .client .Response ;
2425
2526import java .io .ByteArrayInputStream ;
2627import java .io .IOException ;
2728import java .io .UncheckedIOException ;
29+ import java .nio .charset .StandardCharsets ;
2830
2931import org .apache .jena .atlas .web .ContentType ;
3032import org .apache .jena .graph .Graph ;
@@ -44,6 +46,18 @@ public final class JenaBodyHandlers {
4446
4547 private static final String CONTENT_TYPE = "Content-Type" ;
4648
49+ private static void throwOnError (final Response .ResponseInfo responseInfo ) {
50+ if (!Response .isSuccess (responseInfo .statusCode ())) {
51+ throw new ClientHttpException (
52+ "Could not map to a Jena entity." ,
53+ responseInfo .uri (),
54+ responseInfo .statusCode (),
55+ responseInfo .headers (),
56+ new String (responseInfo .body ().array (), StandardCharsets .UTF_8 )
57+ );
58+ }
59+ }
60+
4761 private static Model responseToModel (final Response .ResponseInfo responseInfo ) {
4862 return responseInfo .headers ().firstValue (CONTENT_TYPE )
4963 .map (JenaBodyHandlers ::toJenaLang ).map (lang -> {
@@ -75,10 +89,10 @@ public static Response.BodyHandler<Model> ofModel() {
7589 * @return an HTTP body handler
7690 */
7791 public static Response .BodyHandler <Model > ofJenaModel () {
78- return Response . BodyHandlers . throwOnError (
79- JenaBodyHandlers :: responseToModel ,
80- ( r ) -> Response . isSuccess ( r . statusCode ())
81- ) ;
92+ return responseInfo -> {
93+ JenaBodyHandlers . throwOnError ( responseInfo );
94+ return JenaBodyHandlers . responseToModel ( responseInfo );
95+ } ;
8296 }
8397
8498 private static Graph responseToGraph (final Response .ResponseInfo responseInfo ) {
@@ -112,10 +126,10 @@ public static Response.BodyHandler<Graph> ofGraph() {
112126 * @return an HTTP body handler
113127 */
114128 public static Response .BodyHandler <Graph > ofJenaGraph () {
115- return Response . BodyHandlers . throwOnError (
116- JenaBodyHandlers :: responseToGraph ,
117- ( r ) -> Response . isSuccess ( r . statusCode ())
118- ) ;
129+ return responseInfo -> {
130+ JenaBodyHandlers . throwOnError ( responseInfo );
131+ return JenaBodyHandlers . responseToGraph ( responseInfo );
132+ } ;
119133 }
120134
121135 private static Dataset responseToDataset (final Response .ResponseInfo responseInfo ) {
@@ -149,10 +163,10 @@ public static Response.BodyHandler<Dataset> ofDataset() {
149163 * @return an HTTP body handler
150164 */
151165 public static Response .BodyHandler <Dataset > ofJenaDataset () {
152- return Response . BodyHandlers . throwOnError (
153- JenaBodyHandlers :: responseToDataset ,
154- ( r ) -> Response . isSuccess ( r . statusCode ())
155- ) ;
166+ return responseInfo -> {
167+ JenaBodyHandlers . throwOnError ( responseInfo );
168+ return JenaBodyHandlers . responseToDataset ( responseInfo );
169+ } ;
156170 }
157171
158172 static Lang toJenaLang (final String mediaType ) {
0 commit comments