From 4f60ac9557b31ac6d6f9ad01a0ecc280ff1334d3 Mon Sep 17 00:00:00 2001 From: David Kocher Date: Thu, 12 Dec 2024 15:57:58 +0100 Subject: [PATCH] Only attempt to parse response body for matching content type. --- .../ocs/OcsCapabilitiesResponseHandler.java | 45 ++++++++++--------- .../core/ocs/OcsResponseHandler.java | 37 ++++++++------- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/nextcloud/src/main/java/ch/cyberduck/core/ocs/OcsCapabilitiesResponseHandler.java b/nextcloud/src/main/java/ch/cyberduck/core/ocs/OcsCapabilitiesResponseHandler.java index ee1a5c22c88..37462e799f0 100644 --- a/nextcloud/src/main/java/ch/cyberduck/core/ocs/OcsCapabilitiesResponseHandler.java +++ b/nextcloud/src/main/java/ch/cyberduck/core/ocs/OcsCapabilitiesResponseHandler.java @@ -17,7 +17,9 @@ import ch.cyberduck.core.ocs.model.Capabilities; +import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; +import org.apache.http.entity.ContentType; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -36,28 +38,31 @@ public OcsCapabilitiesResponseHandler(final OcsCapabilities capabilities) { @Override public OcsCapabilities handleEntity(final HttpEntity entity) throws IOException { - final XmlMapper mapper = new XmlMapper(); - final Capabilities value = mapper.readValue(entity.getContent(), Capabilities.class); - if(value.data != null) { - if(value.data.capabilities != null) { - if(value.data.capabilities.core != null) { - capabilities.withWebdav(value.data.capabilities.core.webdav); - } - if(value.data.capabilities.files != null) { - if(value.data.capabilities.files.locking != null) { - try { - capabilities.withLocking(1 == Double.parseDouble(value.data.capabilities.files.locking)); - } - catch(NumberFormatException e) { - log.warn("Failure parsing {}", value.data.capabilities.files.locking); - } + if(StringUtils.equals(ContentType.APPLICATION_XML.getMimeType(), + ContentType.parse(entity.getContentType().getValue()).getMimeType())) { + final XmlMapper mapper = new XmlMapper(); + final Capabilities value = mapper.readValue(entity.getContent(), Capabilities.class); + if(value.data != null) { + if(value.data.capabilities != null) { + if(value.data.capabilities.core != null) { + capabilities.withWebdav(value.data.capabilities.core.webdav); } - if(value.data.capabilities.files.versioning != null) { - try { - capabilities.withVersioning(1 == Integer.parseInt(value.data.capabilities.files.versioning)); + if(value.data.capabilities.files != null) { + if(value.data.capabilities.files.locking != null) { + try { + capabilities.withLocking(1 == Double.parseDouble(value.data.capabilities.files.locking)); + } + catch(NumberFormatException e) { + log.warn("Failure parsing {}", value.data.capabilities.files.locking); + } } - catch(NumberFormatException e) { - log.warn("Failure parsing {}", value.data.capabilities.files.versioning); + if(value.data.capabilities.files.versioning != null) { + try { + capabilities.withVersioning(1 == Integer.parseInt(value.data.capabilities.files.versioning)); + } + catch(NumberFormatException e) { + log.warn("Failure parsing {}", value.data.capabilities.files.versioning); + } } } } diff --git a/nextcloud/src/main/java/ch/cyberduck/core/ocs/OcsResponseHandler.java b/nextcloud/src/main/java/ch/cyberduck/core/ocs/OcsResponseHandler.java index 85009dc6f80..a8db0b74b82 100644 --- a/nextcloud/src/main/java/ch/cyberduck/core/ocs/OcsResponseHandler.java +++ b/nextcloud/src/main/java/ch/cyberduck/core/ocs/OcsResponseHandler.java @@ -19,10 +19,12 @@ import ch.cyberduck.core.nextcloud.NextcloudShareFeature; import ch.cyberduck.core.ocs.model.Share; +import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.HttpResponseException; import org.apache.http.entity.BufferedHttpEntity; +import org.apache.http.entity.ContentType; import org.apache.http.impl.client.AbstractResponseHandler; import org.apache.http.util.EntityUtils; import org.apache.logging.log4j.LogManager; @@ -38,24 +40,29 @@ public abstract class OcsResponseHandler extends AbstractResponseHandler { @Override public R handleResponse(final HttpResponse response) throws IOException { final StatusLine statusLine = response.getStatusLine(); - EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity())); - if(statusLine.getStatusCode() >= 300) { - final StringAppender message = new StringAppender(); - message.append(statusLine.getReasonPhrase()); - final Share error = new XmlMapper().readValue(response.getEntity().getContent(), Share.class); - message.append(error.meta.message); - throw new HttpResponseException(statusLine.getStatusCode(), message.toString()); - } - final Share error = new XmlMapper().readValue(response.getEntity().getContent(), Share.class); - try { - if(Integer.parseInt(error.meta.statuscode) > 100) { + if(response.getEntity() != null) { + EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity())); + if(statusLine.getStatusCode() >= 300) { final StringAppender message = new StringAppender(); + message.append(statusLine.getReasonPhrase()); + final Share error = new XmlMapper().readValue(response.getEntity().getContent(), Share.class); message.append(error.meta.message); - throw new HttpResponseException(Integer.parseInt(error.meta.statuscode), message.toString()); + throw new HttpResponseException(statusLine.getStatusCode(), message.toString()); + } + if(StringUtils.equals(ContentType.APPLICATION_XML.getMimeType(), + ContentType.parse(response.getEntity().getContentType().getValue()).getMimeType())) { + final Share error = new XmlMapper().readValue(response.getEntity().getContent(), Share.class); + try { + if(Integer.parseInt(error.meta.statuscode) > 100) { + final StringAppender message = new StringAppender(); + message.append(error.meta.message); + throw new HttpResponseException(Integer.parseInt(error.meta.statuscode), message.toString()); + } + } + catch(NumberFormatException e) { + log.warn("Failure parsing status code in response {}", error); + } } - } - catch(NumberFormatException e) { - log.warn("Failure parsing status code in response {}", error); } return super.handleResponse(response); }