Skip to content

Commit

Permalink
Merge pull request #16666 from iterate-ch/bugfix/MD-22730
Browse files Browse the repository at this point in the history
Only attempt to parse response body for matching content type.
  • Loading branch information
dkocher authored Dec 13, 2024
2 parents 121a094 + 4f60ac9 commit 7b4340d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,24 +40,29 @@ public abstract class OcsResponseHandler<R> extends AbstractResponseHandler<R> {
@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);
}
Expand Down

0 comments on commit 7b4340d

Please sign in to comment.