1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-08 16:36:13 +00:00

Use new wrapper in GetRemoteUserAvatar operation

This commit is contained in:
davigonz 2018-06-06 17:56:38 +02:00
parent 82b7bef113
commit 944a1186d9
4 changed files with 58 additions and 59 deletions

View File

@ -38,6 +38,8 @@ import org.apache.jackrabbit.webdav.property.DavPropertyName;
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet; import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
import org.apache.jackrabbit.webdav.xml.Namespace; import org.apache.jackrabbit.webdav.xml.Namespace;
import okhttp3.Response;
public class WebdavUtils { public class WebdavUtils {
public static final SimpleDateFormat DISPLAY_DATE_FORMAT = new SimpleDateFormat( public static final SimpleDateFormat DISPLAY_DATE_FORMAT = new SimpleDateFormat(
"dd.MM.yyyy hh:mm"); "dd.MM.yyyy hh:mm");
@ -169,28 +171,26 @@ public class WebdavUtils {
return rawEtag; return rawEtag;
} }
/** /**
* *
* @param method * @param response from which to get the etag
* @return * @return etag from response
*/ */
public static String getEtagFromResponse(HttpMethod method) { public static String getEtagFromResponse(Response response) {
Header eTag = method.getResponseHeader("OC-ETag"); String eTag = response.header("OC-ETag");
if (eTag == null) { if (eTag == null) {
eTag = method.getResponseHeader("oc-etag"); eTag = response.header("oc-etag");
} }
if (eTag == null) { if (eTag == null) {
eTag = method.getResponseHeader("ETag"); eTag = response.header("ETag");
} }
if (eTag == null) { if (eTag == null) {
eTag = method.getResponseHeader("etag"); eTag = response.header("etag");
} }
String result = ""; String result = "";
if (eTag != null) { if (eTag != null) {
result = parseEtag(eTag.getValue()); result = eTag;
} }
return result; return result;
} }
} }

View File

@ -152,8 +152,8 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
} else { } else {
Log_OC.e(TAG, "Could not read modification time from response downloading " + mRemotePath); Log_OC.e(TAG, "Could not read modification time from response downloading " + mRemotePath);
} }
// TODO
mEtag = WebdavUtils.getEtagFromResponse(mGet); // mEtag = WebdavUtils.getEtagFromResponse(mGet);
if (mEtag.length() == 0) { if (mEtag.length() == 0) {
Log_OC.e(TAG, "Could not read eTag from response downloading " + mRemotePath); Log_OC.e(TAG, "Could not read eTag from response downloading " + mRemotePath);
} }

View File

@ -24,6 +24,7 @@
* *
*/ */
package com.owncloud.android.lib.resources.status; package com.owncloud.android.lib.resources.status;
import android.net.Uri; import android.net.Uri;

View File

@ -1,7 +1,6 @@
/* ownCloud Android Library is available under MIT license /* ownCloud Android Library is available under MIT license
* *
* @author David A. Velasco * Copyright (C) 2018 ownCloud GmbH.
* Copyright (C) 2016 ownCloud GmbH.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -26,25 +25,31 @@
package com.owncloud.android.lib.resources.users; package com.owncloud.android.lib.resources.users;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.http.nonwebdav.GetMethod;
import com.owncloud.android.lib.common.network.WebdavUtils;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import org.apache.commons.httpclient.HttpStatus;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import org.apache.commons.httpclient.Header; import okhttp3.Request;
import org.apache.commons.httpclient.HttpStatus; import okhttp3.Response;
import org.apache.commons.httpclient.methods.GetMethod;
import com.owncloud.android.lib.common.OwnCloudClient; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
import com.owncloud.android.lib.common.network.WebdavUtils;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
/** /**
* Gets avatar about the user logged in, if available * Gets avatar about the user logged in, if available
* @author David A. Velasco
* @author David González Verdugo
*/ */
public class GetRemoteUserAvatarOperation extends RemoteOperation { public class GetRemoteUserAvatarOperation extends RemoteOperation {
@ -71,47 +76,40 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
@Override @Override
protected RemoteOperationResult run(OwnCloudClient client) { protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null; RemoteOperationResult result;
GetMethod get = null;
InputStream inputStream = null; InputStream inputStream = null;
BufferedInputStream bis = null; BufferedInputStream bis = null;
ByteArrayOutputStream bos = null; ByteArrayOutputStream bos = null;
Response response = null;
try { try {
String uri = String url =
client.getBaseUri() + NON_OFFICIAL_AVATAR_PATH + client.getBaseUri() + NON_OFFICIAL_AVATAR_PATH +
client.getCredentials().getUsername() + "/" + mDimension; client.getCredentials().getUsername() + "/" + mDimension;
; ;
Log_OC.d(TAG, "avatar URI: " + uri); Log_OC.d(TAG, "avatar URI: " + url);
get = new GetMethod(uri);
/* Conditioned call is corrupting the input stream of the connection.
Seems that response with 304 is also including the avatar in the response body,
though it's forbidden by HTTPS specification. Besides, HTTPClient library
assumes there is nothing in the response body, but the bytes are read
by the next request, resulting in an exception due to a corrupt status line
Maybe when we have a real API we can enable this again. final Request request = new Request.Builder()
.url(url)
.build();
if (mCurrentEtag != null && mCurrentEtag.length() > 0) { GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request);
get.addRequestHeader(IF_NONE_MATCH_HEADER, "\"" + mCurrentEtag + "\""); response = client.executeHttpMethod(getMethod);
}
*/
//get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
int status = client.executeMethod(get);
if (isSuccess(status)) {
if (isSuccess(response.code())) {
// find out size of file to read // find out size of file to read
int totalToTransfer = 0; int totalToTransfer = 0;
Header contentLength = get.getResponseHeader("Content-Length"); String contentLength = response.header("Content-Length");
if (contentLength != null && contentLength.getValue().length() > 0) {
totalToTransfer = Integer.parseInt(contentLength.getValue()); if (contentLength != null && contentLength.length() > 0) {
totalToTransfer = Integer.parseInt(contentLength);
} }
// find out MIME-type! // find out MIME-type!
String mimeType; String mimeType;
Header contentType = get.getResponseHeader("Content-Type"); String contentType =response.header("Content-Type");
if (contentType == null || !contentType.getValue().startsWith("image")) {
if (contentType == null || !contentType.startsWith("image")) {
Log_OC.e( Log_OC.e(
TAG, "Not an image, failing with no avatar" TAG, "Not an image, failing with no avatar"
); );
@ -120,10 +118,11 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
); );
return result; return result;
} }
mimeType = contentType.getValue();
mimeType = contentType;
/// download will be performed to a buffer /// download will be performed to a buffer
inputStream = get.getResponseBodyAsStream(); inputStream = response.body().byteStream();
bis = new BufferedInputStream(inputStream); bis = new BufferedInputStream(inputStream);
bos = new ByteArrayOutputStream(totalToTransfer); bos = new ByteArrayOutputStream(totalToTransfer);
@ -137,21 +136,21 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
// TODO check total bytes transferred? // TODO check total bytes transferred?
// find out etag // find out etag
String etag = WebdavUtils.getEtagFromResponse(get); String etag = WebdavUtils.getEtagFromResponse(response);
if (etag.length() == 0) { if (etag.length() == 0) {
Log_OC.w(TAG, "Could not read Etag from avatar"); Log_OC.w(TAG, "Could not read Etag from avatar");
} }
// Result // Result
result = new RemoteOperationResult(true, get); result = new RemoteOperationResult(OK);
ResultData resultData = new ResultData(bos.toByteArray(), mimeType, etag); ResultData resultData = new ResultData(bos.toByteArray(), mimeType, etag);
ArrayList<Object> data = new ArrayList<Object>(); ArrayList<Object> data = new ArrayList<Object>();
data.add(resultData); data.add(resultData);
result.setData(data); result.setData(data);
} else { } else {
result = new RemoteOperationResult(false, get); result = new RemoteOperationResult(false, getMethod.getRequest(), response);
client.exhaustResponse(get.getResponseBodyAsStream()); client.exhaustResponse(response.body().byteStream());
} }
} catch (Exception e) { } catch (Exception e) {
@ -159,7 +158,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
Log_OC.e(TAG, "Exception while getting OC user avatar", e); Log_OC.e(TAG, "Exception while getting OC user avatar", e);
} finally { } finally {
if (get != null) { if (response != null) {
try { try {
if (inputStream != null) { if (inputStream != null) {
client.exhaustResponse(inputStream); client.exhaustResponse(inputStream);
@ -179,7 +178,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
} catch (IOException o) { } catch (IOException o) {
Log_OC.e(TAG, "Unexpected exception closing output stream ", o); Log_OC.e(TAG, "Unexpected exception closing output stream ", o);
} }
get.releaseConnection(); response.body().close();
} }
} }
@ -213,5 +212,4 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
return mAvatarData; return mAvatarData;
} }
} }
} }