@@ -218,9 +218,9 @@ public void setMaxDelay(int maxDelay) {
218218 }
219219
220220 private FailedRequest extractErrorFields (Response response ) {
221- if ( response == null ) return null ;
221+ if (response == null ) return null ;
222222 try {
223- if ( response .code () == STATUS_UNAUTHORIZED ) {
223+ if (response .code () == STATUS_UNAUTHORIZED ) {
224224 FailedRequest failure = new FailedRequest ();
225225 failure .setMessageString ("Unauthorized" );
226226 failure .setStatusString ("Failed Auth" );
@@ -229,7 +229,7 @@ private FailedRequest extractErrorFields(Response response) {
229229 String responseBody = getEntity (response .body (), String .class );
230230 InputStream is = new ByteArrayInputStream (responseBody .getBytes ("UTF-8" ));
231231 FailedRequest handler = FailedRequest .getFailedRequest (response .code (), response .header (HEADER_CONTENT_TYPE ), is );
232- if ( handler .getMessage () == null ) {
232+ if (handler .getMessage () == null ) {
233233 handler .setMessageString (responseBody );
234234 }
235235 return handler ;
@@ -5411,33 +5411,36 @@ private <T> T getEntity(ResponseBody body, Class<T> as) {
54115411 boolean isBinary = true ;
54125412 MediaType mediaType = body .contentType ();
54135413 if (mediaType != null ) {
5414- String subtype = mediaType .subtype ().toLowerCase ();
5415- if (subtype == "json" || subtype .endsWith ("+json" )) {
5416- suffix = ".json" ;
5417- isBinary = false ;
5418- } else if (subtype == "xml" || subtype .endsWith ("+xml" )) {
5419- suffix = ".xml" ;
5420- isBinary = false ;
5421- } else if (subtype == "vnd.marklogic-js-module" ) {
5422- suffix = ".mjs" ;
5423- isBinary = false ;
5424- } else if (subtype == "vnd.marklogic-javascript" || subtype == "sjs" ) {
5425- suffix = ".sjs" ;
5426- isBinary = false ;
5427- } else if (subtype == "vnd.marklogic-xdmp" || subtype == "xquery" ) {
5428- suffix = ".xqy" ;
5429- isBinary = false ;
5430- } else if (subtype == "javascript" ) {
5431- suffix = ".js" ;
5432- isBinary = false ;
5433- } else if (subtype == "html" ) {
5434- suffix = ".html" ;
5435- isBinary = false ;
5436- } else if (mediaType .type ().toLowerCase () == "text" ) {
5437- suffix = ".txt" ;
5438- isBinary = false ;
5439- } else {
5440- suffix = "." + subtype ;
5414+ String subtype = mediaType .subtype ();
5415+ if (subtype != null ) {
5416+ subtype = subtype .toLowerCase ();
5417+ if (subtype .endsWith ("json" )) {
5418+ suffix = ".json" ;
5419+ isBinary = false ;
5420+ } else if (subtype .endsWith ("xml" )) {
5421+ suffix = ".xml" ;
5422+ isBinary = false ;
5423+ } else if (subtype .equals ("vnd.marklogic-js-module" )) {
5424+ suffix = ".mjs" ;
5425+ isBinary = false ;
5426+ } else if (subtype .equals ("vnd.marklogic-javascript" )) {
5427+ suffix = ".sjs" ;
5428+ isBinary = false ;
5429+ } else if (subtype .equals ("vnd.marklogic-xdmp" ) || subtype .endsWith ("xquery" )) {
5430+ suffix = ".xqy" ;
5431+ isBinary = false ;
5432+ } else if (subtype .endsWith ("javascript" )) {
5433+ suffix = ".js" ;
5434+ isBinary = false ;
5435+ } else if (subtype .endsWith ("html" )) {
5436+ suffix = ".html" ;
5437+ isBinary = false ;
5438+ } else if (mediaType .type ().toLowerCase () == "text" ) {
5439+ suffix = ".txt" ;
5440+ isBinary = false ;
5441+ } else {
5442+ suffix = "." + subtype ;
5443+ }
54415444 }
54425445 }
54435446 Path path = Files .createTempFile ("tmp" , suffix );
@@ -5678,26 +5681,29 @@ private void checkStatus(Response response) {
56785681 if (statusCode >= 300 ) {
56795682 FailedRequest failure = null ;
56805683 MediaType mediaType = MediaType .parse (response .header (HEADER_CONTENT_TYPE ));
5681- if ( "json" .equals (mediaType .subtype ()) ) {
5682- failure = extractErrorFields (response );
5683- } else if ( statusCode == STATUS_UNAUTHORIZED ) {
5684- failure = new FailedRequest ();
5685- failure .setMessageString ("Unauthorized" );
5686- failure .setStatusString ("Failed Auth" );
5687- } else if (statusCode == STATUS_NOT_FOUND ) {
5688- ResourceNotFoundException ex = failure == null ? new ResourceNotFoundException ("Could not " + method + " at " + endpoint ) :
5689- new ResourceNotFoundException ("Could not " + method + " at " + endpoint , failure );
5690- throw ex ;
5691- } else if (statusCode == STATUS_FORBIDDEN ) {
5692- ForbiddenUserException ex = failure == null ? new ForbiddenUserException ("User is not allowed to " + method + " at " + endpoint ) :
5693- new ForbiddenUserException ("User is not allowed to " + method + " at " + endpoint , failure );
5694- throw ex ;
5695- } else {
5696- failure = new FailedRequest ();
5697- failure .setStatusCode (statusCode );
5698- failure .setMessageCode ("UNKNOWN" );
5699- failure .setMessageString ("Server did not respond with an expected Error message." );
5700- failure .setStatusString ("UNKNOWN" );
5684+ String subtype = mediaType .subtype ();
5685+ if (subtype != null ) {
5686+ subtype = subtype .toLowerCase ();
5687+ if (subtype .endsWith ("json" ) || subtype .endsWith ("xml" )) {
5688+ failure = extractErrorFields (response );
5689+ }
5690+ }
5691+ if (failure == null ) {
5692+ if (statusCode == STATUS_UNAUTHORIZED ) {
5693+ failure = new FailedRequest ();
5694+ failure .setMessageString ("Unauthorized" );
5695+ failure .setStatusString ("Failed Auth" );
5696+ } else if (statusCode == STATUS_NOT_FOUND ) {
5697+ throw new ResourceNotFoundException ("Could not " + method + " at " + endpoint );
5698+ } else if (statusCode == STATUS_FORBIDDEN ) {
5699+ throw new ForbiddenUserException ("User is not allowed to " + method + " at " + endpoint );
5700+ } else {
5701+ failure = new FailedRequest ();
5702+ failure .setStatusCode (statusCode );
5703+ failure .setMessageCode ("UNKNOWN" );
5704+ failure .setMessageString ("Server did not respond with an expected Error message." );
5705+ failure .setStatusString ("UNKNOWN" );
5706+ }
57015707 }
57025708 FailedRequestException ex = failure == null ? new FailedRequestException ("failed to " + method + " at " + endpoint + ": "
57035709 + getReasonPhrase (response )) : new FailedRequestException ("failed to " + method + " at " + endpoint + ": "
@@ -5946,8 +5952,8 @@ public String getErrorBody() {
59465952 if (errorBody .contentLength () > 0 ) {
59475953 MediaType errorType = errorBody .contentType ();
59485954 if (errorType != null ) {
5949- String errorContentType = errorType .toString ();
5950- if (errorContentType != null && errorContentType . startsWith ( "application/" ) && errorContentType . contains ("json" )) {
5955+ String subtype = errorType .subtype ();
5956+ if (subtype != null && subtype . toLowerCase (). endsWith ("json" )) {
59515957 return errorBody .string ();
59525958 }
59535959 }
0 commit comments