mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Refactoed operations on Shares
This commit is contained in:
		
							parent
							
								
									ad60bc9e8e
								
							
						
					
					
						commit
						4d9ff3e022
					
				| @ -1,4 +1,6 @@ | ||||
| /* ownCloud Android Library is available under MIT license | ||||
|  *   @author masensio | ||||
|  *   @author David A. Velasco | ||||
|  *   Copyright (C) 2015 ownCloud Inc. | ||||
|  *    | ||||
|  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
| @ -24,24 +26,16 @@ | ||||
| 
 | ||||
| package com.owncloud.android.lib.resources.shares; | ||||
| 
 | ||||
| import java.io.ByteArrayInputStream; | ||||
| import java.io.InputStream; | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.methods.PostMethod; | ||||
| import org.apache.http.HttpStatus; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.OwnCloudClient; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperation; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; | ||||
| import com.owncloud.android.lib.common.utils.Log_OC; | ||||
| 
 | ||||
| /** | ||||
|  * Creates a new share.  This allows sharing with a user or group or as a link. | ||||
|  *  | ||||
|  * @author masensio | ||||
|  * | ||||
|  */ | ||||
| public class CreateRemoteShareOperation extends RemoteOperation { | ||||
| 
 | ||||
| @ -54,8 +48,6 @@ public class CreateRemoteShareOperation extends RemoteOperation { | ||||
| 	private static final String PARAM_PASSWORD = "password"; | ||||
| 	private static final String PARAM_PERMISSIONS = "permissions"; | ||||
| 
 | ||||
| 	private ArrayList<OCShare> mShares;  // List of shares for result, one share in this case | ||||
| 	 | ||||
| 	private String mRemoteFilePath; | ||||
| 	private ShareType mShareType; | ||||
| 	private String mShareWith; | ||||
| @ -118,7 +110,6 @@ public class CreateRemoteShareOperation extends RemoteOperation { | ||||
| 		try { | ||||
| 			// Post Method | ||||
| 			post = new PostMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); | ||||
| 			//Log_OC.d(TAG, "URL ------> " + client.getBaseUri() + ShareUtils.SHARING_API_PATH); | ||||
| 
 | ||||
| 			post.setRequestHeader( "Content-Type", | ||||
|                     "application/x-www-form-urlencoded; charset=utf-8"); // necessary for special characters | ||||
| @ -127,7 +118,7 @@ public class CreateRemoteShareOperation extends RemoteOperation { | ||||
| 			post.addParameter(PARAM_SHARE_TYPE, Integer.toString(mShareType.getValue())); | ||||
| 			post.addParameter(PARAM_SHARE_WITH, mShareWith); | ||||
| 			if (mPublicUpload) { | ||||
| 				post.addParameter(PARAM_PUBLIC_UPLOAD, Boolean.toString(mPublicUpload)); | ||||
| 				post.addParameter(PARAM_PUBLIC_UPLOAD, Boolean.toString(true)); | ||||
| 			} | ||||
| 			if (mPassword != null && mPassword.length() > 0) { | ||||
| 				post.addParameter(PARAM_PASSWORD, mPassword); | ||||
| @ -140,52 +131,23 @@ public class CreateRemoteShareOperation extends RemoteOperation { | ||||
| 
 | ||||
| 			if(isSuccess(status)) { | ||||
| 				String response = post.getResponseBodyAsString(); | ||||
| 				ArrayList<Object> resultData = new ArrayList<Object>(); | ||||
| 
 | ||||
| 				// Parse xml response --> obtain the response in ShareFiles ArrayList | ||||
| 				// convert String into InputStream | ||||
| 				InputStream is = new ByteArrayInputStream(response.getBytes()); | ||||
| 				ShareXMLParser xmlParser = new ShareXMLParser(); | ||||
| 				mShares = xmlParser.parseXMLResponse(is); | ||||
| 				if (xmlParser.isSuccess()) { | ||||
| 					if (mShares != null && mShares.size() > 0) { | ||||
| 						Log_OC.d(TAG, "Created " + mShares.size() + " share(s)"); | ||||
| 						result = new RemoteOperationResult(ResultCode.OK); | ||||
| 						for (OCShare share: mShares) { | ||||
| 							resultData.add(share); | ||||
| 						} | ||||
| 						result.setData(resultData); | ||||
| 				ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( | ||||
| 						new ShareXMLParser() | ||||
| 				); | ||||
| 				parser.setOneOrMoreSharesRequired(true); | ||||
| 				parser.setOwnCloudVersion(client.getOwnCloudVersion()); | ||||
| 				parser.setServerBaseUri(client.getBaseUri()); | ||||
| 				result = parser.parse(response); | ||||
| 
 | ||||
| 						if (mGetShareDetails) { | ||||
| 							// retrieve more info | ||||
| 							OCShare emptyShare = (OCShare) resultData.get(0); | ||||
| 
 | ||||
| 							GetRemoteShareOperation getInfo = new GetRemoteShareOperation(emptyShare.getIdRemoteShared()); | ||||
| 							result = getInfo.execute(client); | ||||
| 						} | ||||
| 
 | ||||
| 					} else { | ||||
| 						result = new RemoteOperationResult(ResultCode.WRONG_SERVER_RESPONSE); | ||||
| 						Log_OC.e(TAG, "Successful status with no share in it"); | ||||
| 				if (result.isSuccess()) { | ||||
| 					Log_OC.d(TAG, "Created " + result.getData().size() + " share(s)");	// should be one | ||||
| 					if (mGetShareDetails) { | ||||
| 						// retrieve more info - POST operation only returns the index of the new share | ||||
| 						OCShare emptyShare = (OCShare) result.getData().get(0); | ||||
| 						GetRemoteShareOperation getInfo = new GetRemoteShareOperation(emptyShare.getIdRemoteShared()); | ||||
| 						result = getInfo.execute(client); | ||||
| 					} | ||||
| 
 | ||||
| 				} else if (xmlParser.isWrongParameter()){ | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_WRONG_PARAMETER); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else if (xmlParser.isNotFound()){ | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else if (xmlParser.isForbidden()) { | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_FORBIDDEN); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else { | ||||
| 					result = new RemoteOperationResult(ResultCode.WRONG_SERVER_RESPONSE); | ||||
| 				} | ||||
| 
 | ||||
| 			} else { | ||||
|  | ||||
| @ -28,17 +28,11 @@ package com.owncloud.android.lib.resources.shares; | ||||
| import com.owncloud.android.lib.common.OwnCloudClient; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperation; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; | ||||
| import com.owncloud.android.lib.common.utils.Log_OC; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.methods.GetMethod; | ||||
| import org.apache.http.HttpStatus; | ||||
| 
 | ||||
| import java.io.ByteArrayInputStream; | ||||
| import java.io.InputStream; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| 
 | ||||
| /**  | ||||
|  * Get the data about a Share resource, known its remote ID. | ||||
| @ -67,47 +61,21 @@ public class GetRemoteShareOperation extends RemoteOperation { | ||||
| 		// Get the response | ||||
| 		try{ | ||||
| 			get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH + "/" + Long.toString(mRemoteId)); | ||||
| 			//get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||
| 			get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||
| 
 | ||||
| 			status = client.executeMethod(get); | ||||
| 
 | ||||
| 			if(isSuccess(status)) { | ||||
| 				String response = get.getResponseBodyAsString(); | ||||
| 				ArrayList<Object> resultData = new ArrayList<Object>(); | ||||
| 
 | ||||
| 				// Parse xml response --> obtain the response in ShareFiles ArrayList | ||||
| 				// convert String into InputStream | ||||
| 				InputStream is = new ByteArrayInputStream(response.getBytes()); | ||||
| 				ShareXMLParser xmlParser = new ShareXMLParser(); | ||||
| 				List<OCShare> shares = xmlParser.parseXMLResponse(is); | ||||
| 				if (xmlParser.isSuccess()) { | ||||
| 					if (shares != null && shares.size() > 0) { | ||||
| 						Log_OC.d(TAG, "Got " + shares.size() + " shares"); | ||||
| 						result = new RemoteOperationResult(ResultCode.OK); | ||||
| 						resultData.add(shares.get(0)); | ||||
| 						result.setData(resultData); | ||||
| 					} else { | ||||
| 						result = new RemoteOperationResult(ResultCode.WRONG_SERVER_RESPONSE); | ||||
| 						Log_OC.e(TAG, "Successful status with no share in it"); | ||||
| 					} | ||||
| 
 | ||||
| 				} else if (xmlParser.isWrongParameter()){ | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_WRONG_PARAMETER); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else if (xmlParser.isNotFound()){ | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else if (xmlParser.isForbidden()) { | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_FORBIDDEN); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else { | ||||
| 					result = new RemoteOperationResult(ResultCode.WRONG_SERVER_RESPONSE); | ||||
| 				} | ||||
| 				// Parse xml response and obtain the list of shares | ||||
| 				ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( | ||||
| 						new ShareXMLParser() | ||||
| 				); | ||||
| 				parser.setOneOrMoreSharesRequired(true); | ||||
| 				parser.setOwnCloudVersion(client.getOwnCloudVersion()); | ||||
| 				parser.setServerBaseUri(client.getBaseUri()); | ||||
| 				result = parser.parse(response); | ||||
| 
 | ||||
| 			} else { | ||||
| 				result = new RemoteOperationResult(false, status, get.getResponseHeaders()); | ||||
|  | ||||
| @ -1,4 +1,6 @@ | ||||
| /* ownCloud Android Library is available under MIT license | ||||
|  *   @author masensio | ||||
|  *   @author David A. Velasco | ||||
|  *   Copyright (C) 2015 ownCloud Inc. | ||||
|  *    | ||||
|  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
| @ -24,10 +26,6 @@ | ||||
| 
 | ||||
| package com.owncloud.android.lib.resources.shares; | ||||
| 
 | ||||
| import java.io.ByteArrayInputStream; | ||||
| import java.io.InputStream; | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.NameValuePair; | ||||
| import org.apache.commons.httpclient.methods.GetMethod; | ||||
| import org.apache.http.HttpStatus; | ||||
| @ -35,18 +33,14 @@ import org.apache.http.HttpStatus; | ||||
| import com.owncloud.android.lib.common.OwnCloudClient; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperation; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; | ||||
| import com.owncloud.android.lib.common.utils.Log_OC; | ||||
| 
 | ||||
| 
 | ||||
| /** | ||||
|  * Provide a list shares for a specific file.   | ||||
|  * The input is the full path of the desired file.   | ||||
|  * The output is a list of everyone who has the file shared with them. | ||||
|  *  | ||||
|  * @author masensio | ||||
|  * | ||||
|  */ | ||||
| 
 | ||||
| public class GetRemoteSharesForFileOperation extends RemoteOperation { | ||||
| 
 | ||||
| 	private static final String TAG = GetRemoteSharesForFileOperation.class.getSimpleName(); | ||||
| @ -55,8 +49,6 @@ public class GetRemoteSharesForFileOperation extends RemoteOperation { | ||||
| 	private static final String PARAM_RESHARES = "reshares"; | ||||
| 	private static final String PARAM_SUBFILES = "subfiles"; | ||||
| 
 | ||||
| 	private ArrayList<OCShare> mShares;  // List of shares for result, one share in this case | ||||
| 	 | ||||
| 	private String mRemoteFilePath; | ||||
| 	private boolean mReshares; | ||||
| 	private boolean mSubfiles; | ||||
| @ -102,53 +94,17 @@ public class GetRemoteSharesForFileOperation extends RemoteOperation { | ||||
| 
 | ||||
| 			if(isSuccess(status)) { | ||||
| 				String response = get.getResponseBodyAsString(); | ||||
| 				ArrayList<Object> resultData = new ArrayList<Object>(); | ||||
| 
 | ||||
| 				// Parse xml response --> obtain the response in ShareFiles ArrayList | ||||
| 				// convert String into InputStream | ||||
| 				InputStream is = new ByteArrayInputStream(response.getBytes()); | ||||
| 				ShareXMLParser xmlParser = new ShareXMLParser(); | ||||
| 				mShares = xmlParser.parseXMLResponse(is); | ||||
| 				if (xmlParser.isSuccess()) { | ||||
| 					if (mShares != null) {	// 0 shares is a right response | ||||
| 						Log_OC.d(TAG, "Got " + mShares.size() + " shares"); | ||||
| 						result = new RemoteOperationResult(ResultCode.OK); | ||||
| 						for (OCShare share: mShares) { | ||||
| 							// Build the link | ||||
| 							if (	share.getShareType() == ShareType.PUBLIC_LINK && | ||||
| 									share.getShareLink() == null && | ||||
| 									share.getToken().length() > 0 | ||||
| 									) { | ||||
| 								String linkToken = ShareUtils.getSharingToken( | ||||
| 										client.getOwnCloudVersion()); | ||||
| 								share.setShareLink(client.getBaseUri() + linkToken + | ||||
| 										share.getToken()); | ||||
| 							} | ||||
| 							resultData.add(share); | ||||
| 						} | ||||
| 						result.setData(resultData); | ||||
| 					} else { | ||||
| 						result = new RemoteOperationResult(ResultCode.WRONG_SERVER_RESPONSE); | ||||
| 						Log_OC.e(TAG, "Successful status with no share in it"); | ||||
| 					} | ||||
| 				// Parse xml response and obtain the list of shares | ||||
| 				ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( | ||||
| 						new ShareXMLParser() | ||||
| 				); | ||||
| 				parser.setOwnCloudVersion(client.getOwnCloudVersion()); | ||||
| 				parser.setServerBaseUri(client.getBaseUri()); | ||||
| 				result = parser.parse(response); | ||||
| 
 | ||||
| 				} else if (xmlParser.isWrongParameter()){ | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_WRONG_PARAMETER); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else if (xmlParser.isNotFound()){ | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else if (xmlParser.isForbidden()) { | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_FORBIDDEN); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else { | ||||
| 					result = new RemoteOperationResult(ResultCode.WRONG_SERVER_RESPONSE); | ||||
| 				if (result.isSuccess()) { | ||||
| 					Log_OC.d(TAG, "Got " + result.getData().size() + " shares"); | ||||
| 				} | ||||
| 
 | ||||
| 			} else { | ||||
|  | ||||
| @ -1,4 +1,6 @@ | ||||
| /* ownCloud Android Library is available under MIT license | ||||
|  *   @author masensio | ||||
|  *   @author David A. Velasco | ||||
|  *   Copyright (C) 2015 ownCloud Inc. | ||||
|  *    | ||||
|  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
| @ -24,33 +26,24 @@ | ||||
| 
 | ||||
| package com.owncloud.android.lib.resources.shares; | ||||
| 
 | ||||
| import java.io.ByteArrayInputStream; | ||||
| import java.io.InputStream; | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.methods.GetMethod; | ||||
| import org.apache.http.HttpStatus; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.OwnCloudClient; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperation; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; | ||||
| import com.owncloud.android.lib.common.utils.Log_OC; | ||||
| 
 | ||||
| 
 | ||||
| /**  | ||||
|  * Get the data from the server about ALL the known shares owned by the requester. | ||||
|  *  | ||||
|  * @author masensio | ||||
|  * | ||||
|  */ | ||||
| 
 | ||||
| public class GetRemoteSharesOperation extends RemoteOperation { | ||||
| 
 | ||||
| 	private static final String TAG = GetRemoteSharesOperation.class.getSimpleName(); | ||||
| 
 | ||||
| 	private ArrayList<OCShare> mShares;  // List of shares for result | ||||
| 
 | ||||
| 
 | ||||
| 	public GetRemoteSharesOperation() { | ||||
| 	} | ||||
| @ -71,41 +64,14 @@ public class GetRemoteSharesOperation extends RemoteOperation { | ||||
| 
 | ||||
| 			if(isSuccess(status)) { | ||||
| 				String response = get.getResponseBodyAsString(); | ||||
| 				ArrayList<Object> resultData = new ArrayList<Object>(); | ||||
| 
 | ||||
| 				// Parse xml response --> obtain the response in ShareFiles ArrayList | ||||
| 				// convert String into InputStream | ||||
| 				InputStream is = new ByteArrayInputStream(response.getBytes()); | ||||
| 				ShareXMLParser xmlParser = new ShareXMLParser(); | ||||
| 				mShares = xmlParser.parseXMLResponse(is); | ||||
| 				if (xmlParser.isSuccess()) { | ||||
| 					if (mShares != null) {		// 0 shares is a right response | ||||
| 						Log_OC.d(TAG, "Got " + mShares.size() + " shares"); | ||||
| 						result = new RemoteOperationResult(ResultCode.OK); | ||||
| 						for (OCShare share : mShares) { | ||||
| 							resultData.add(share); | ||||
| 						} | ||||
| 						result.setData(resultData); | ||||
| 					} | ||||
| 
 | ||||
| 				} else if (xmlParser.isWrongParameter()){ | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_WRONG_PARAMETER); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else if (xmlParser.isNotFound()){ | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else if (xmlParser.isForbidden()) { | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_FORBIDDEN); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else { | ||||
| 					result = new RemoteOperationResult(ResultCode.WRONG_SERVER_RESPONSE); | ||||
| 				} | ||||
| 				// Parse xml response and obtain the list of shares | ||||
| 				ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( | ||||
| 						new ShareXMLParser() | ||||
| 				); | ||||
| 				parser.setOwnCloudVersion(client.getOwnCloudVersion()); | ||||
| 				parser.setServerBaseUri(client.getBaseUri()); | ||||
| 				result = parser.parse(response); | ||||
| 
 | ||||
| 			} else { | ||||
| 				result = new RemoteOperationResult(false, status, get.getResponseHeaders()); | ||||
|  | ||||
| @ -1,4 +1,6 @@ | ||||
| /* ownCloud Android Library is available under MIT license | ||||
|  *   @author masensio | ||||
|  *   @author David A. Velasco | ||||
|  *   Copyright (C) 2015 ownCloud Inc. | ||||
|  *    | ||||
|  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
| @ -24,24 +26,16 @@ | ||||
| 
 | ||||
| package com.owncloud.android.lib.resources.shares; | ||||
| 
 | ||||
| import java.io.ByteArrayInputStream; | ||||
| import java.io.InputStream; | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.HttpStatus; | ||||
| import org.apache.jackrabbit.webdav.client.methods.DeleteMethod; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.OwnCloudClient; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperation; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; | ||||
| import com.owncloud.android.lib.common.utils.Log_OC; | ||||
| 
 | ||||
| /** | ||||
|  * Remove a share | ||||
|  *  | ||||
|  * @author masensio | ||||
|  * | ||||
|  */ | ||||
| 
 | ||||
| public class RemoveRemoteShareOperation extends RemoteOperation { | ||||
| @ -78,35 +72,15 @@ public class RemoveRemoteShareOperation extends RemoteOperation { | ||||
| 
 | ||||
| 			if(isSuccess(status)) { | ||||
| 				String response = delete.getResponseBodyAsString(); | ||||
| 				ArrayList<Object> resultData = new ArrayList<Object>(); | ||||
| 
 | ||||
| 				// Parse xml response | ||||
| 				// convert String into InputStream | ||||
| 				InputStream is = new ByteArrayInputStream(response.getBytes()); | ||||
| 				ShareXMLParser xmlParser = new ShareXMLParser(); | ||||
| 				xmlParser.parseXMLResponse(is); | ||||
| 				if (xmlParser.isSuccess()) { | ||||
| 					result = new RemoteOperationResult(ResultCode.OK); | ||||
| 				} else if (xmlParser.isWrongParameter()){ | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_WRONG_PARAMETER); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else if (xmlParser.isNotFound()){ | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else if (xmlParser.isForbidden()) { | ||||
| 					result = new RemoteOperationResult(ResultCode.SHARE_FORBIDDEN); | ||||
| 					resultData.add(xmlParser.getMessage()); | ||||
| 					result.setData(resultData); | ||||
| 
 | ||||
| 				} else { | ||||
| 					result = new RemoteOperationResult(ResultCode.WRONG_SERVER_RESPONSE); | ||||
| 				} | ||||
| 				// Parse xml response and obtain the list of shares | ||||
| 				ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( | ||||
| 						new ShareXMLParser() | ||||
| 				); | ||||
| 				result = parser.parse(response); | ||||
| 
 | ||||
| 				Log_OC.d(TAG, "Unshare " + id + ": " + result.getLogMessage()); | ||||
| 
 | ||||
| 			} else { | ||||
| 				result = new RemoteOperationResult(false, status, delete.getResponseHeaders()); | ||||
| 			} | ||||
|  | ||||
| @ -0,0 +1,144 @@ | ||||
| /* ownCloud Android Library is available under MIT license | ||||
|  *   @author David A. Velasco | ||||
|  *   Copyright (C) 2015 ownCloud Inc. | ||||
|  * | ||||
|  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  *   of this software and associated documentation files (the "Software"), to deal | ||||
|  *   in the Software without restriction, including without limitation the rights | ||||
|  *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  *   copies of the Software, and to permit persons to whom the Software is | ||||
|  *   furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  *   The above copyright notice and this permission notice shall be included in | ||||
|  *   all copies or substantial portions of the Software. | ||||
|  * | ||||
|  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
|  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||
|  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||
|  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||||
|  *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||||
|  *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||
|  *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  *   THE SOFTWARE. | ||||
|  * | ||||
|  */ | ||||
| 
 | ||||
| package com.owncloud.android.lib.resources.shares; | ||||
| 
 | ||||
| import android.net.Uri; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||
| import com.owncloud.android.lib.common.utils.Log_OC; | ||||
| import com.owncloud.android.lib.resources.status.OwnCloudVersion; | ||||
| 
 | ||||
| import org.xmlpull.v1.XmlPullParserException; | ||||
| 
 | ||||
| import java.io.ByteArrayInputStream; | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class ShareToRemoteOperationResultParser { | ||||
| 
 | ||||
|     private static final String TAG = ShareToRemoteOperationResultParser.class.getSimpleName(); | ||||
| 
 | ||||
|     private ShareXMLParser mShareXmlParser = null; | ||||
|     private boolean mOneOrMoreSharesRequired = false; | ||||
|     private OwnCloudVersion mOwnCloudVersion = null; | ||||
|     private Uri mServerBaseUri = null; | ||||
| 
 | ||||
| 
 | ||||
|     public ShareToRemoteOperationResultParser(ShareXMLParser shareXmlParser) { | ||||
|         mShareXmlParser = shareXmlParser; | ||||
|     } | ||||
| 
 | ||||
|     public void setOneOrMoreSharesRequired(boolean oneOrMoreSharesRequired) { | ||||
|         mOneOrMoreSharesRequired = oneOrMoreSharesRequired; | ||||
|     } | ||||
| 
 | ||||
|     public void setOwnCloudVersion(OwnCloudVersion ownCloudVersion) { | ||||
|         mOwnCloudVersion = ownCloudVersion; | ||||
|     } | ||||
| 
 | ||||
|     public void setServerBaseUri(Uri serverBaseURi) { | ||||
|         mServerBaseUri = serverBaseURi; | ||||
|     } | ||||
| 
 | ||||
|     public RemoteOperationResult parse(String serverResponse) { | ||||
|         if (serverResponse == null || serverResponse.length() == 0) { | ||||
|             return new RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE); | ||||
|         } | ||||
| 
 | ||||
|         RemoteOperationResult result = null; | ||||
|         ArrayList<Object> resultData = new ArrayList<Object>(); | ||||
| 
 | ||||
|         try { | ||||
|             // Parse xml response and obtain the list of shares | ||||
|             InputStream is = new ByteArrayInputStream(serverResponse.getBytes()); | ||||
|             if (mShareXmlParser == null) { | ||||
|                 Log_OC.w(TAG, "No ShareXmlParser provided, creating new instance "); | ||||
|                 mShareXmlParser = new ShareXMLParser(); | ||||
|             } | ||||
|             List<OCShare> shares = mShareXmlParser.parseXMLResponse(is); | ||||
| 
 | ||||
|             if (mShareXmlParser.isSuccess()) { | ||||
|                 if ((shares != null && shares.size() > 0) || !mOneOrMoreSharesRequired) { | ||||
|                     result = new RemoteOperationResult(RemoteOperationResult.ResultCode.OK); | ||||
|                     if (shares != null) { | ||||
|                         for (OCShare share : shares) { | ||||
|                             resultData.add(share); | ||||
|                             // build the share link if not in the response (only received when the share is created) | ||||
|                             if (share.getShareType() == ShareType.PUBLIC_LINK && | ||||
|                                     share.getShareLink() == null && | ||||
|                                     share.getToken().length() > 0 | ||||
|                                     ) { | ||||
|                                 if (mServerBaseUri != null) { | ||||
|                                     String sharingLinkPath = ShareUtils.getSharingLinkPath(mOwnCloudVersion); | ||||
|                                     share.setShareLink(mServerBaseUri + sharingLinkPath + share.getToken()); | ||||
|                                 } else { | ||||
|                                     Log_OC.e(TAG, "Couldn't build link for public share"); | ||||
|                                 } | ||||
|                             } | ||||
|                         } | ||||
|                     } | ||||
|                     result.setData(resultData); | ||||
| 
 | ||||
|                 } else { | ||||
|                     result = new RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE); | ||||
|                     Log_OC.e(TAG, "Successful status with no share in the response"); | ||||
|                 } | ||||
| 
 | ||||
|             } else if (mShareXmlParser.isWrongParameter()){ | ||||
|                 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_WRONG_PARAMETER); | ||||
|                 resultData.add(mShareXmlParser.getMessage()); | ||||
|                 result.setData(resultData); | ||||
| 
 | ||||
|             } else if (mShareXmlParser.isNotFound()){ | ||||
|                 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND); | ||||
|                 resultData.add(mShareXmlParser.getMessage()); | ||||
|                 result.setData(resultData); | ||||
| 
 | ||||
|             } else if (mShareXmlParser.isForbidden()) { | ||||
|                 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_FORBIDDEN); | ||||
|                 resultData.add(mShareXmlParser.getMessage()); | ||||
|                 result.setData(resultData); | ||||
| 
 | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE); | ||||
| 
 | ||||
|             } | ||||
| 
 | ||||
|         } catch (XmlPullParserException e) { | ||||
|             Log_OC.e(TAG, "Error parsing response from server ", e); | ||||
|             result = new RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE); | ||||
| 
 | ||||
|         } catch (IOException e) { | ||||
|             Log_OC.e(TAG, "Error reading response from server ", e); | ||||
|             result = new RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE); | ||||
|         } | ||||
| 
 | ||||
|         return result; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -39,17 +39,15 @@ public class ShareUtils { | ||||
| 	public static final String SHARING_API_PATH ="/ocs/v1.php/apps/files_sharing/api/v1/shares";  | ||||
| 
 | ||||
|     // String to build the link with the token of a share: | ||||
|     // server address + "/public.php?service=files&t=" + token | ||||
|     public static final String SHARING_LINK_TOKEN_BEFORE_VERSION_8 = "/public.php?service=files&t="; | ||||
|     public static final String SHARING_LINK_TOKEN_AFTER_VERSION_8= "/index.php/s/"; | ||||
|     public static final String SHARING_LINK_PATH_BEFORE_VERSION_8 = "/public.php?service=files&t="; | ||||
|     public static final String SHARING_LINK_PATH_AFTER_VERSION_8 = "/index.php/s/"; | ||||
| 
 | ||||
|     public static String getSharingToken(OwnCloudVersion version){ | ||||
|     public static String getSharingLinkPath(OwnCloudVersion version){ | ||||
|         if (version!= null && version.isAfter8Version()){ | ||||
|             return SHARING_LINK_TOKEN_AFTER_VERSION_8; | ||||
|             return SHARING_LINK_PATH_AFTER_VERSION_8; | ||||
|         } else { | ||||
|             return SHARING_LINK_TOKEN_BEFORE_VERSION_8; | ||||
|             return SHARING_LINK_PATH_BEFORE_VERSION_8; | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user