mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Merge pull request #10 from owncloud/share_link__new_share
Additions in library to share files with public links
This commit is contained in:
		
						commit
						815fbba486
					
				| @ -24,6 +24,8 @@ | |||||||
| 
 | 
 | ||||||
| package com.owncloud.android.lib.operations.common; | package com.owncloud.android.lib.operations.common; | ||||||
| 
 | 
 | ||||||
|  | import java.io.Serializable; | ||||||
|  | 
 | ||||||
| import com.owncloud.android.lib.utils.FileUtils; | import com.owncloud.android.lib.utils.FileUtils; | ||||||
| 
 | 
 | ||||||
| import android.os.Parcel; | import android.os.Parcel; | ||||||
| @ -37,7 +39,10 @@ import android.util.Log; | |||||||
|  * @author masensio |  * @author masensio | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| public class OCShare implements Parcelable{ | public class OCShare implements Parcelable, Serializable { | ||||||
|  | 	 | ||||||
|  | 	/** Generated - should be refreshed every time the class changes!! */ | ||||||
|  | 	private static final long serialVersionUID = 4124975224281327921L; | ||||||
| 
 | 
 | ||||||
| 	private static final String TAG = OCShare.class.getSimpleName(); | 	private static final String TAG = OCShare.class.getSimpleName(); | ||||||
|      |      | ||||||
| @ -52,9 +57,10 @@ public class OCShare implements Parcelable{ | |||||||
|     private long mExpirationDate; |     private long mExpirationDate; | ||||||
|     private String mToken; |     private String mToken; | ||||||
|     private String mSharedWithDisplayName; |     private String mSharedWithDisplayName; | ||||||
|     private boolean mIsDirectory; |     private boolean mIsFolder; | ||||||
|     private long mUserId; |     private long mUserId; | ||||||
|     private long mIdRemoteShared; |     private long mIdRemoteShared; | ||||||
|  |     private String mShareLink; | ||||||
|      |      | ||||||
|     public OCShare() { |     public OCShare() { | ||||||
|     	super(); |     	super(); | ||||||
| @ -85,9 +91,10 @@ public class OCShare implements Parcelable{ | |||||||
|         mExpirationDate = 0; |         mExpirationDate = 0; | ||||||
|         mToken = null; |         mToken = null; | ||||||
|         mSharedWithDisplayName = null; |         mSharedWithDisplayName = null; | ||||||
|         mIsDirectory = false; |         mIsFolder = false; | ||||||
|         mUserId = -1; |         mUserId = -1; | ||||||
|         mIdRemoteShared = -1; |         mIdRemoteShared = -1; | ||||||
|  |         mShareLink = null; | ||||||
|     }	 |     }	 | ||||||
|      |      | ||||||
|     /// Getters and Setters |     /// Getters and Setters | ||||||
| @ -180,12 +187,12 @@ public class OCShare implements Parcelable{ | |||||||
|         this.mSharedWithDisplayName = sharedWithDisplayName; |         this.mSharedWithDisplayName = sharedWithDisplayName; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public boolean isDirectory() { |     public boolean isFolder() { | ||||||
|         return mIsDirectory; |         return mIsFolder; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setIsDirectory(boolean isDirectory) { |     public void setIsFolder(boolean isFolder) { | ||||||
|         this.mIsDirectory = isDirectory; |         this.mIsFolder = isFolder; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public long getUserId() { |     public long getUserId() { | ||||||
| @ -204,6 +211,14 @@ public class OCShare implements Parcelable{ | |||||||
|         this.mIdRemoteShared = idRemoteShared; |         this.mIdRemoteShared = idRemoteShared; | ||||||
|     } |     } | ||||||
|      |      | ||||||
|  |     public String getShareLink() { | ||||||
|  |     	return this.mShareLink; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     public void setShareLink(String shareLink) { | ||||||
|  |     	this.mShareLink = shareLink; | ||||||
|  |     } | ||||||
|  |      | ||||||
|     /**  |     /**  | ||||||
|      * Parcelable Methods |      * Parcelable Methods | ||||||
|      */ |      */ | ||||||
| @ -245,9 +260,10 @@ public class OCShare implements Parcelable{ | |||||||
|         mExpirationDate = source.readLong(); |         mExpirationDate = source.readLong(); | ||||||
|         mToken = source.readString(); |         mToken = source.readString(); | ||||||
|         mSharedWithDisplayName = source.readString(); |         mSharedWithDisplayName = source.readString(); | ||||||
|         mIsDirectory = source.readInt() == 0; |         mIsFolder = source.readInt() == 0; | ||||||
|         mUserId = source.readLong(); |         mUserId = source.readLong(); | ||||||
|         mIdRemoteShared = source.readLong(); |         mIdRemoteShared = source.readLong(); | ||||||
|  |         mShareLink = source.readString(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -270,9 +286,10 @@ public class OCShare implements Parcelable{ | |||||||
|         dest.writeLong(mExpirationDate); |         dest.writeLong(mExpirationDate); | ||||||
|         dest.writeString(mToken); |         dest.writeString(mToken); | ||||||
|         dest.writeString(mSharedWithDisplayName); |         dest.writeString(mSharedWithDisplayName); | ||||||
|         dest.writeInt(mIsDirectory ? 1 : 0); |         dest.writeInt(mIsFolder ? 1 : 0); | ||||||
|         dest.writeLong(mUserId); |         dest.writeLong(mUserId); | ||||||
|         dest.writeLong(mIdRemoteShared); |         dest.writeLong(mIdRemoteShared); | ||||||
|  |         dest.writeString(mShareLink); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -0,0 +1,172 @@ | |||||||
|  | /* ownCloud Android Library is available under MIT license | ||||||
|  |  *   Copyright (C) 2014 ownCloud (http://www.owncloud.org/) | ||||||
|  |  *    | ||||||
|  |  *   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.operations.remote; | ||||||
|  | 
 | ||||||
|  | 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 android.util.Log; | ||||||
|  | 
 | ||||||
|  | import com.owncloud.android.lib.network.OwnCloudClient; | ||||||
|  | import com.owncloud.android.lib.operations.common.OCShare; | ||||||
|  | import com.owncloud.android.lib.operations.common.RemoteOperation; | ||||||
|  | import com.owncloud.android.lib.operations.common.RemoteOperationResult; | ||||||
|  | import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode; | ||||||
|  | import com.owncloud.android.lib.operations.common.ShareType; | ||||||
|  | import com.owncloud.android.lib.utils.ShareUtils; | ||||||
|  | import com.owncloud.android.lib.utils.ShareXMLParser; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Creates a new share.  This allows sharing with a user or group or as a link. | ||||||
|  |  *  | ||||||
|  |  * @author masensio | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | public class CreateShareRemoteOperation extends RemoteOperation { | ||||||
|  | 
 | ||||||
|  | 	private static final String TAG = CreateShareRemoteOperation.class.getSimpleName(); | ||||||
|  | 
 | ||||||
|  | 	private static final String PARAM_PATH = "path"; | ||||||
|  | 	private static final String PARAM_SHARE_TYPE = "shareType"; | ||||||
|  | 	private static final String PARAM_SHARE_WITH = "shareWith"; | ||||||
|  | 	private static final String PARAM_PUBLIC_UPLOAD = "publicUpload"; | ||||||
|  | 	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 mPath; | ||||||
|  | 	private ShareType mShareType; | ||||||
|  | 	private String mShareWith; | ||||||
|  | 	private boolean mPublicUpload; | ||||||
|  | 	private String mPassword; | ||||||
|  | 	private int mPermissions; | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 	 * Constructor | ||||||
|  | 	 * @param path			Full path of the file/folder being shared. Mandatory argument | ||||||
|  | 	 * @param shareType		‘0’ = user, ‘1’ = group, ‘3’ = Public link. Mandatory argument | ||||||
|  | 	 * @param shareWith		User/group ID with who the file should be shared.  This is mandatory for shareType of 0 or 1 | ||||||
|  | 	 * @param publicUpload	If ‘false’ (default) public cannot upload to a public shared folder.  | ||||||
|  | 	 * 						If ‘true’ public can upload to a shared folder. Only available for public link shares | ||||||
|  | 	 * @param password		Password to protect a public link share. Only available for public link shares | ||||||
|  | 	 * @param permissions	1 - Read only – Default for “public” shares | ||||||
|  | 	 * 						2 - Update | ||||||
|  | 	 * 						4 - Create | ||||||
|  | 	 * 						8 - Delete | ||||||
|  | 	 * 						16- Re-share | ||||||
|  | 	 * 						31- All above – Default for “private” shares | ||||||
|  | 	 * 						For user or group shares. | ||||||
|  | 	 * 						To obtain combinations, add the desired values together.   | ||||||
|  | 	 * 						For instance, for “Re-Share”, “delete”, “read”, “update”, add 16+8+2+1 = 27. | ||||||
|  | 	 */ | ||||||
|  | 	public CreateShareRemoteOperation(String path, ShareType shareType, String shareWith, boolean publicUpload,  | ||||||
|  | 			String password, int permissions) { | ||||||
|  | 
 | ||||||
|  | 		mPath = path; | ||||||
|  | 		mShareType = shareType; | ||||||
|  | 		mShareWith = shareWith; | ||||||
|  | 		mPublicUpload = publicUpload; | ||||||
|  | 		mPassword = password; | ||||||
|  | 		mPermissions = permissions; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	@Override | ||||||
|  | 	protected RemoteOperationResult run(OwnCloudClient client) { | ||||||
|  | 		RemoteOperationResult result = null; | ||||||
|  | 		int status = -1; | ||||||
|  | 
 | ||||||
|  | 		PostMethod post = null; | ||||||
|  | 
 | ||||||
|  | 		try { | ||||||
|  | 			// Post Method | ||||||
|  | 			post = new PostMethod(client.getBaseUri() + ShareUtils.SHAREAPI_ROUTE); | ||||||
|  | 			Log.d(TAG, "URL ------> " + client.getBaseUri() + ShareUtils.SHAREAPI_ROUTE); | ||||||
|  | 
 | ||||||
|  | 			post.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); // necessary for special characters | ||||||
|  | 			post.addParameter(PARAM_PATH, mPath); | ||||||
|  | 			post.addParameter(PARAM_SHARE_TYPE, Integer.toString(mShareType.getValue())); | ||||||
|  | 			post.addParameter(PARAM_SHARE_WITH, mShareWith); | ||||||
|  | 			post.addParameter(PARAM_PUBLIC_UPLOAD, Boolean.toString(mPublicUpload)); | ||||||
|  | 			if (mPassword != null && mPassword.length() > 0) { | ||||||
|  | 				post.addParameter(PARAM_PASSWORD, mPassword); | ||||||
|  | 			} | ||||||
|  | 			post.addParameter(PARAM_PERMISSIONS, Integer.toString(mPermissions)); | ||||||
|  | 
 | ||||||
|  | 			status = client.executeMethod(post); | ||||||
|  | 
 | ||||||
|  | 			if(isSuccess(status)) { | ||||||
|  | 				String response = post.getResponseBodyAsString(); | ||||||
|  | 				Log.d(TAG, "Successful response: " + response); | ||||||
|  | 
 | ||||||
|  | 				result = new RemoteOperationResult(ResultCode.OK); | ||||||
|  | 				 | ||||||
|  | 				// 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) { | ||||||
|  | 						Log.d(TAG, "Shares: " + mShares.size()); | ||||||
|  | 						result = new RemoteOperationResult(ResultCode.OK); | ||||||
|  | 						ArrayList<Object> sharesObjects = new ArrayList<Object>(); | ||||||
|  | 						for (OCShare share: mShares) { | ||||||
|  | 							sharesObjects.add(share); | ||||||
|  | 						} | ||||||
|  | 						result.setData(sharesObjects); | ||||||
|  | 					} | ||||||
|  | 				} else if (xmlParser.isFileNotFound()){ | ||||||
|  | 					result = new RemoteOperationResult(ResultCode.FILE_NOT_FOUND); | ||||||
|  | 					 | ||||||
|  | 				} else { | ||||||
|  | 					result = new RemoteOperationResult(false, status, post.getResponseHeaders());	 | ||||||
|  | 				} | ||||||
|  | 
 | ||||||
|  | 			} else { | ||||||
|  | 				result = new RemoteOperationResult(false, status, post.getResponseHeaders()); | ||||||
|  | 			} | ||||||
|  | 			 | ||||||
|  | 		} catch (Exception e) { | ||||||
|  | 			result = new RemoteOperationResult(e); | ||||||
|  | 			Log.e(TAG, "Exception while Creating New Share", e); | ||||||
|  | 			 | ||||||
|  | 		} finally { | ||||||
|  | 			if (post != null) { | ||||||
|  | 				post.releaseConnection(); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return result; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	private boolean isSuccess(int status) { | ||||||
|  | 		return (status == HttpStatus.SC_OK); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -36,6 +36,7 @@ import com.owncloud.android.lib.operations.common.RemoteOperation; | |||||||
| import com.owncloud.android.lib.operations.common.RemoteOperationResult; | import com.owncloud.android.lib.operations.common.RemoteOperationResult; | ||||||
| import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode; | import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode; | ||||||
| import com.owncloud.android.lib.operations.common.OCShare; | import com.owncloud.android.lib.operations.common.OCShare; | ||||||
|  | import com.owncloud.android.lib.utils.ShareUtils; | ||||||
| import com.owncloud.android.lib.utils.ShareXMLParser; | import com.owncloud.android.lib.utils.ShareXMLParser; | ||||||
| 
 | 
 | ||||||
| import android.util.Log; | import android.util.Log; | ||||||
| @ -52,9 +53,6 @@ public class GetRemoteSharesOperation extends RemoteOperation { | |||||||
| 
 | 
 | ||||||
| 	private static final String TAG = GetRemoteSharesOperation.class.getSimpleName(); | 	private static final String TAG = GetRemoteSharesOperation.class.getSimpleName(); | ||||||
| 
 | 
 | ||||||
| 	// OCS Route |  | ||||||
| 	private static final String SHAREAPI_ROUTE ="/ocs/v1.php/apps/files_sharing/api/v1/shares";  |  | ||||||
| 
 |  | ||||||
| 	private ArrayList<OCShare> mShares;  // List of shares for result | 	private ArrayList<OCShare> mShares;  // List of shares for result | ||||||
| 
 | 
 | ||||||
| 	 | 	 | ||||||
| @ -71,8 +69,8 @@ public class GetRemoteSharesOperation extends RemoteOperation { | |||||||
| 
 | 
 | ||||||
| 		// Get the response | 		// Get the response | ||||||
| 		try{ | 		try{ | ||||||
| 			get = new GetMethod(client.getBaseUri() + SHAREAPI_ROUTE); | 			get = new GetMethod(client.getBaseUri() + ShareUtils.SHAREAPI_ROUTE); | ||||||
| 			Log.d(TAG, "URL ------> " + client.getBaseUri() + SHAREAPI_ROUTE); | 			Log.d(TAG, "URL ------> " + client.getBaseUri() + ShareUtils.SHAREAPI_ROUTE); | ||||||
| 			status = client.executeMethod(get); | 			status = client.executeMethod(get); | ||||||
| 			if(isSuccess(status)) { | 			if(isSuccess(status)) { | ||||||
| 				Log.d(TAG, "Obtain RESPONSE"); | 				Log.d(TAG, "Obtain RESPONSE"); | ||||||
|  | |||||||
| @ -0,0 +1,145 @@ | |||||||
|  | /* ownCloud Android Library is available under MIT license | ||||||
|  |  *   Copyright (C) 2014 ownCloud (http://www.owncloud.org/) | ||||||
|  |  *    | ||||||
|  |  *   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.operations.remote; | ||||||
|  | 
 | ||||||
|  | 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; | ||||||
|  | 
 | ||||||
|  | import android.util.Log; | ||||||
|  | 
 | ||||||
|  | import com.owncloud.android.lib.network.OwnCloudClient; | ||||||
|  | import com.owncloud.android.lib.operations.common.OCShare; | ||||||
|  | import com.owncloud.android.lib.operations.common.RemoteOperation; | ||||||
|  | import com.owncloud.android.lib.operations.common.RemoteOperationResult; | ||||||
|  | import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode; | ||||||
|  | import com.owncloud.android.lib.utils.ShareUtils; | ||||||
|  | import com.owncloud.android.lib.utils.ShareXMLParser; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 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 GetSharesForFileRemoteOperation extends RemoteOperation { | ||||||
|  | 
 | ||||||
|  | 	private static final String TAG = GetSharesForFileRemoteOperation.class.getSimpleName(); | ||||||
|  | 	 | ||||||
|  | 	private static final String PARAM_PATH = "path"; | ||||||
|  | 	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 mPath; | ||||||
|  | 	private boolean mReshares; | ||||||
|  | 	private boolean mSubfiles; | ||||||
|  | 	 | ||||||
|  | 	/** | ||||||
|  | 	 * Constructor | ||||||
|  | 	 *  | ||||||
|  | 	 * @param path		Path to file or folder | ||||||
|  | 	 * @param reshares	If set to ‘false’ (default), only shares from the current user are returned | ||||||
|  | 	 * 					If set to ‘true’, all shares from the given file are returned | ||||||
|  | 	 * @param subfiles	If set to ‘false’ (default), lists only the folder being shared | ||||||
|  | 	 * 					If set to ‘true’, all shared files within the folder are returned. | ||||||
|  | 	 */ | ||||||
|  | 	public GetSharesForFileRemoteOperation(String path, boolean reshares, boolean subfiles) { | ||||||
|  | 		mPath = path; | ||||||
|  | 		mReshares = reshares; | ||||||
|  | 		mSubfiles = subfiles; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	@Override | ||||||
|  | 	protected RemoteOperationResult run(OwnCloudClient client) { | ||||||
|  | 		RemoteOperationResult result = null; | ||||||
|  | 		int status = -1; | ||||||
|  | 
 | ||||||
|  | 		GetMethod get = null; | ||||||
|  | 
 | ||||||
|  | 		try { | ||||||
|  | 			// Get Method | ||||||
|  | 			get = new GetMethod(client.getBaseUri() + ShareUtils.SHAREAPI_ROUTE); | ||||||
|  | 			Log.d(TAG, "URL ------> " + client.getBaseUri() + ShareUtils.SHAREAPI_ROUTE); | ||||||
|  | 
 | ||||||
|  | 			// Add Parameters to Get Method | ||||||
|  | 			get.setQueryString(new NameValuePair[] {  | ||||||
|  | 				    new NameValuePair(PARAM_PATH, mPath), | ||||||
|  | 				    new NameValuePair(PARAM_RESHARES, String.valueOf(mReshares)), | ||||||
|  | 				    new NameValuePair(PARAM_SUBFILES, String.valueOf(mSubfiles)) | ||||||
|  | 				});  | ||||||
|  | 
 | ||||||
|  | 			status = client.executeMethod(get); | ||||||
|  | 
 | ||||||
|  | 			if(isSuccess(status)) { | ||||||
|  | 				String response = get.getResponseBodyAsString(); | ||||||
|  | 				Log.d(TAG, "Successful response: " + response); | ||||||
|  | 
 | ||||||
|  | 				result = new RemoteOperationResult(ResultCode.OK); | ||||||
|  | 				 | ||||||
|  | 				// 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 (mShares != null) { | ||||||
|  | 					Log.d(TAG, "Shares: " + mShares.size()); | ||||||
|  | 					result = new RemoteOperationResult(ResultCode.OK); | ||||||
|  | 					ArrayList<Object> sharesObjects = new ArrayList<Object>(); | ||||||
|  | 					for (OCShare share: mShares) { | ||||||
|  | 						sharesObjects.add(share); | ||||||
|  | 					} | ||||||
|  | 					result.setData(sharesObjects); | ||||||
|  | 				} | ||||||
|  | 
 | ||||||
|  | 			} else { | ||||||
|  | 				result = new RemoteOperationResult(false, status, get.getResponseHeaders()); | ||||||
|  | 			} | ||||||
|  | 			 | ||||||
|  | 		} catch (Exception e) { | ||||||
|  | 			result = new RemoteOperationResult(e); | ||||||
|  | 			Log.e(TAG, "Exception while Creating New Share", e); | ||||||
|  | 			 | ||||||
|  | 		} finally { | ||||||
|  | 			if (get != null) { | ||||||
|  | 				get.releaseConnection(); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return result; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	private boolean isSuccess(int status) { | ||||||
|  | 		return (status == HttpStatus.SC_OK); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,353 @@ | |||||||
|  | /* ownCloud Android Library is available under MIT license | ||||||
|  |  *   Copyright (C) 2014 ownCloud (http://www.owncloud.org/) | ||||||
|  |  *    | ||||||
|  |  *   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.utils; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | 
 | ||||||
|  | import org.xmlpull.v1.XmlPullParser; | ||||||
|  | import org.xmlpull.v1.XmlPullParserException; | ||||||
|  | import org.xmlpull.v1.XmlPullParserFactory; | ||||||
|  | 
 | ||||||
|  | import android.util.Log; | ||||||
|  | import android.util.Xml; | ||||||
|  | 
 | ||||||
|  | import com.owncloud.android.lib.operations.common.OCShare; | ||||||
|  | import com.owncloud.android.lib.operations.common.ShareType; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Parser for Share API Response: GetSharesForFile Operation | ||||||
|  |  * @author masensio | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | public class GetSharesForFileXMLParser { | ||||||
|  | 
 | ||||||
|  | 	private static final String TAG = GetSharesForFileXMLParser.class.getSimpleName(); | ||||||
|  | 
 | ||||||
|  | 	// No namespaces | ||||||
|  | 	private static final String ns = null; | ||||||
|  | 
 | ||||||
|  | 	// NODES for XML Parser | ||||||
|  | 	private static final String NODE_OCS = "ocs"; | ||||||
|  | 
 | ||||||
|  | 	private static final String NODE_META = "meta"; | ||||||
|  | 	private static final String NODE_STATUS = "status"; | ||||||
|  | 	private static final String NODE_STATUS_CODE = "statuscode"; | ||||||
|  | 	//private static final String NODE_MESSAGE = "message"; | ||||||
|  | 
 | ||||||
|  | 	private static final String NODE_DATA = "data"; | ||||||
|  | 	private static final String NODE_ELEMENT = "element"; | ||||||
|  | 	private static final String NODE_ID = "id"; | ||||||
|  | 	private static final String NODE_ITEM_TYPE = "item_type"; | ||||||
|  | 	private static final String NODE_ITEM_SOURCE = "item_source"; | ||||||
|  | 	private static final String NODE_PARENT = "parent"; | ||||||
|  | 	private static final String NODE_SHARE_TYPE = "share_type"; | ||||||
|  | 	private static final String NODE_SHARE_WITH = "share_with"; | ||||||
|  | 	private static final String NODE_FILE_SOURCE = "file_source"; | ||||||
|  | 	private static final String NODE_PATH = "path"; | ||||||
|  | 	private static final String NODE_PERMISSIONS = "permissions"; | ||||||
|  | 	private static final String NODE_STIME = "stime"; | ||||||
|  | 	private static final String NODE_EXPIRATION = "expiration"; | ||||||
|  | 	private static final String NODE_TOKEN = "token"; | ||||||
|  | 	private static final String NODE_STORAGE = "storage"; | ||||||
|  | 	private static final String NODE_MAIL_SEND = "mail_send"; | ||||||
|  | 	private static final String NODE_SHARE_WITH_DISPLAY_NAME = "share_with_display_name"; | ||||||
|  | 	 | ||||||
|  | 
 | ||||||
|  | 	private static final String TYPE_FOLDER = "folder"; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 	private String mStatus; | ||||||
|  | 	private int mStatusCode; | ||||||
|  | 
 | ||||||
|  | 	// Getters and Setters | ||||||
|  | 	public String getStatus() { | ||||||
|  | 		return mStatus; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	public void setStatus(String status) { | ||||||
|  | 		this.mStatus = status; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	public int getStatusCode() { | ||||||
|  | 		return mStatusCode; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	public void setStatusCode(int statusCode) { | ||||||
|  | 		this.mStatusCode = statusCode; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Constructor | ||||||
|  | 	public GetSharesForFileXMLParser() { | ||||||
|  | 		// TODO Auto-generated constructor stub | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 	 * Parse is as response of Share API | ||||||
|  | 	 * @param is | ||||||
|  | 	 * @return List of ShareRemoteFiles | ||||||
|  | 	 * @throws XmlPullParserException | ||||||
|  | 	 * @throws IOException | ||||||
|  | 	 */ | ||||||
|  | 	public ArrayList<OCShare> parseXMLResponse(InputStream is) throws XmlPullParserException, IOException { | ||||||
|  | 
 | ||||||
|  | 		try { | ||||||
|  | 			// XMLPullParser | ||||||
|  | 			XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); | ||||||
|  | 			factory.setNamespaceAware(true); | ||||||
|  | 
 | ||||||
|  | 			XmlPullParser parser = Xml.newPullParser(); | ||||||
|  | 			parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); | ||||||
|  | 			parser.setInput(is, null); | ||||||
|  | 			parser.nextTag(); | ||||||
|  | 			return readOCS(parser); | ||||||
|  | 
 | ||||||
|  | 		} finally { | ||||||
|  | 			is.close(); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 	 * Parse OCS node | ||||||
|  | 	 * @param parser | ||||||
|  | 	 * @return List of ShareRemoteFiles | ||||||
|  | 	 * @throws XmlPullParserException | ||||||
|  | 	 * @throws IOException | ||||||
|  | 	 */ | ||||||
|  | 	private ArrayList<OCShare> readOCS (XmlPullParser parser) throws XmlPullParserException, IOException { | ||||||
|  | 		ArrayList<OCShare> shares = new ArrayList<OCShare>(); | ||||||
|  | 		parser.require(XmlPullParser.START_TAG,  ns , NODE_OCS); | ||||||
|  | 		while (parser.next() != XmlPullParser.END_TAG) { | ||||||
|  | 			if (parser.getEventType() != XmlPullParser.START_TAG) { | ||||||
|  | 				continue; | ||||||
|  | 			} | ||||||
|  | 			String name = parser.getName(); | ||||||
|  | 			// read NODE_META and NODE_DATA | ||||||
|  | 			if (name.equalsIgnoreCase(NODE_META)) { | ||||||
|  | 				readMeta(parser); | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_DATA)) { | ||||||
|  | 				shares = readData(parser); | ||||||
|  | 			} else { | ||||||
|  | 				skip(parser); | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 		} | ||||||
|  | 		return shares; | ||||||
|  | 
 | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 	 * Parse Meta node | ||||||
|  | 	 * @param parser | ||||||
|  | 	 * @throws XmlPullParserException | ||||||
|  | 	 * @throws IOException | ||||||
|  | 	 */ | ||||||
|  | 	private void readMeta(XmlPullParser parser) throws XmlPullParserException, IOException { | ||||||
|  | 		parser.require(XmlPullParser.START_TAG, ns, NODE_META); | ||||||
|  | 		Log.d(TAG, "---- NODE META ---"); | ||||||
|  | 		while (parser.next() != XmlPullParser.END_TAG) { | ||||||
|  | 			if (parser.getEventType() != XmlPullParser.START_TAG) { | ||||||
|  | 				continue; | ||||||
|  | 			} | ||||||
|  | 			String name = parser.getName(); | ||||||
|  | 
 | ||||||
|  | 			if  (name.equalsIgnoreCase(NODE_STATUS)) { | ||||||
|  | 				setStatus(readNode(parser, NODE_STATUS)); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_STATUS_CODE)) { | ||||||
|  | 				setStatusCode(Integer.parseInt(readNode(parser, NODE_STATUS_CODE))); | ||||||
|  | 
 | ||||||
|  | 			} else { | ||||||
|  | 				skip(parser); | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 	 * Parse Data node | ||||||
|  | 	 * @param parser | ||||||
|  | 	 * @return | ||||||
|  | 	 * @throws XmlPullParserException | ||||||
|  | 	 * @throws IOException | ||||||
|  | 	 */ | ||||||
|  | 	private ArrayList<OCShare> readData(XmlPullParser parser) throws XmlPullParserException, IOException { | ||||||
|  | 		ArrayList<OCShare> shares = new ArrayList<OCShare>(); | ||||||
|  | 
 | ||||||
|  | 		parser.require(XmlPullParser.START_TAG, ns, NODE_DATA);		 | ||||||
|  | 		Log.d(TAG, "---- NODE DATA ---"); | ||||||
|  | 		while (parser.next() != XmlPullParser.END_TAG) { | ||||||
|  | 			if (parser.getEventType() != XmlPullParser.START_TAG) { | ||||||
|  | 				continue; | ||||||
|  | 			} | ||||||
|  | 			String name = parser.getName(); | ||||||
|  | 			if (name.equalsIgnoreCase(NODE_ELEMENT)) { | ||||||
|  | 				shares.add(readElement(parser)); | ||||||
|  | 			} else { | ||||||
|  | 				skip(parser); | ||||||
|  | 				 | ||||||
|  | 			}  | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 		return shares; | ||||||
|  | 
 | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 	/**  | ||||||
|  | 	 * Parse Element node | ||||||
|  | 	 * @param parser | ||||||
|  | 	 * @return | ||||||
|  | 	 * @throws XmlPullParserException | ||||||
|  | 	 * @throws IOException | ||||||
|  | 	 */ | ||||||
|  | 	private OCShare readElement(XmlPullParser parser) throws XmlPullParserException, IOException { | ||||||
|  | 		parser.require(XmlPullParser.START_TAG, ns, NODE_ELEMENT); | ||||||
|  | 		 | ||||||
|  | 		OCShare share = new OCShare(); | ||||||
|  | 		 | ||||||
|  | 		Log.d(TAG, "---- NODE ELEMENT ---"); | ||||||
|  | 		while (parser.next() != XmlPullParser.END_TAG) { | ||||||
|  | 			if (parser.getEventType() != XmlPullParser.START_TAG) { | ||||||
|  | 				continue; | ||||||
|  | 			} | ||||||
|  | 			 | ||||||
|  | 			String name = parser.getName(); | ||||||
|  | 			 | ||||||
|  | 			if (name.equalsIgnoreCase(NODE_ELEMENT)) { | ||||||
|  | 				share = readElement(parser); | ||||||
|  | 				 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_ID)) { | ||||||
|  | 				share.setIdRemoteShared(Integer.parseInt(readNode(parser, NODE_ID))); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_ITEM_TYPE)) { | ||||||
|  | 				share.setIsFolder(readNode(parser, NODE_ITEM_TYPE).equalsIgnoreCase(TYPE_FOLDER)); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_ITEM_SOURCE)) { | ||||||
|  | 				share.setItemSource(Long.parseLong(readNode(parser, NODE_ITEM_SOURCE))); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_PARENT)) { | ||||||
|  | 				readNode(parser, NODE_PARENT); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_SHARE_TYPE)) { | ||||||
|  | 				int value = Integer.parseInt(readNode(parser, NODE_SHARE_TYPE)); | ||||||
|  | 				share.setShareType(ShareType.fromValue(value)); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_SHARE_WITH)) { | ||||||
|  | 				share.setShareWith(readNode(parser, NODE_SHARE_WITH)); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_FILE_SOURCE)) { | ||||||
|  | 				share.setFileSource(Long.parseLong(readNode(parser, NODE_FILE_SOURCE))); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_PATH)) { | ||||||
|  | 				share.setPath(readNode(parser, NODE_PATH)); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_PERMISSIONS)) { | ||||||
|  | 				share.setPermissions(Integer.parseInt(readNode(parser, NODE_PERMISSIONS))); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_STIME)) { | ||||||
|  | 				share.setSharedDate(Long.parseLong(readNode(parser, NODE_STIME))); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_EXPIRATION)) { | ||||||
|  | 				String value = readNode(parser, NODE_EXPIRATION); | ||||||
|  | 				if (!value.isEmpty()) { | ||||||
|  | 					share.setExpirationDate(Long.parseLong(readNode(parser, NODE_EXPIRATION))); // check if expiration is in long format or date format | ||||||
|  | 				} | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_TOKEN)) { | ||||||
|  | 				share.setToken(readNode(parser, NODE_TOKEN)); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_STORAGE)) { | ||||||
|  | 				readNode(parser, NODE_STORAGE); | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_MAIL_SEND)) { | ||||||
|  | 				readNode(parser, NODE_MAIL_SEND); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_SHARE_WITH_DISPLAY_NAME)) { | ||||||
|  | 				share.setSharedWithDisplayName(readNode(parser, NODE_SHARE_WITH_DISPLAY_NAME)); | ||||||
|  | 
 | ||||||
|  | 			} else { | ||||||
|  | 				skip(parser); | ||||||
|  | 			}  | ||||||
|  | 		}		 | ||||||
|  | 
 | ||||||
|  | 		return share; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 	 * Parse a node, to obtain its text. Needs readText method | ||||||
|  | 	 * @param parser | ||||||
|  | 	 * @param node | ||||||
|  | 	 * @return Text of the node | ||||||
|  | 	 * @throws XmlPullParserException | ||||||
|  | 	 * @throws IOException | ||||||
|  | 	 */ | ||||||
|  | 	private String readNode (XmlPullParser parser, String node) throws XmlPullParserException, IOException{ | ||||||
|  | 		parser.require(XmlPullParser.START_TAG, ns, node); | ||||||
|  | 		String value = readText(parser); | ||||||
|  | 		Log.d(TAG, "node= " + node + ", value= " + value); | ||||||
|  | 		parser.require(XmlPullParser.END_TAG, ns, node); | ||||||
|  | 		return value; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	/** | ||||||
|  | 	 * Read the text from a node | ||||||
|  | 	 * @param parser | ||||||
|  | 	 * @return Text of the node | ||||||
|  | 	 * @throws IOException | ||||||
|  | 	 * @throws XmlPullParserException | ||||||
|  | 	 */ | ||||||
|  | 	private String readText(XmlPullParser parser) throws IOException, XmlPullParserException { | ||||||
|  | 		String result = ""; | ||||||
|  | 		if (parser.next() == XmlPullParser.TEXT) { | ||||||
|  | 			result = parser.getText(); | ||||||
|  | 			parser.nextTag(); | ||||||
|  | 		} | ||||||
|  | 		return result; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/** | ||||||
|  | 	 * Skip tags in parser procedure | ||||||
|  | 	 * @param parser | ||||||
|  | 	 * @throws XmlPullParserException | ||||||
|  | 	 * @throws IOException | ||||||
|  | 	 */ | ||||||
|  | 	private void skip(XmlPullParser parser) throws XmlPullParserException, IOException { | ||||||
|  | 		if (parser.getEventType() != XmlPullParser.START_TAG) { | ||||||
|  | 			throw new IllegalStateException(); | ||||||
|  | 		} | ||||||
|  | 		int depth = 1; | ||||||
|  | 		while (depth != 0) { | ||||||
|  | 			switch (parser.next()) { | ||||||
|  | 			case XmlPullParser.END_TAG: | ||||||
|  | 				depth--; | ||||||
|  | 				break; | ||||||
|  | 			case XmlPullParser.START_TAG: | ||||||
|  | 				depth++; | ||||||
|  | 				break; | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										39
									
								
								src/com/owncloud/android/lib/utils/ShareUtils.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								src/com/owncloud/android/lib/utils/ShareUtils.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,39 @@ | |||||||
|  | /* ownCloud Android Library is available under MIT license | ||||||
|  |  *   Copyright (C) 2014 ownCloud (http://www.owncloud.org/) | ||||||
|  |  *    | ||||||
|  |  *   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.utils; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Contains Constants for Share Operation | ||||||
|  |  *  | ||||||
|  |  * @author masensio | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | public class ShareUtils { | ||||||
|  | 
 | ||||||
|  | 	// OCS Route | ||||||
|  | 	public static final String SHAREAPI_ROUTE ="/ocs/v1.php/apps/files_sharing/api/v1/shares";  | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -77,8 +77,14 @@ public class ShareXMLParser { | |||||||
| 	private static final String NODE_MAIL_SEND = "mail_send"; | 	private static final String NODE_MAIL_SEND = "mail_send"; | ||||||
| 	private static final String NODE_SHARE_WITH_DISPLAY_NAME = "share_with_display_name"; | 	private static final String NODE_SHARE_WITH_DISPLAY_NAME = "share_with_display_name"; | ||||||
| 	 | 	 | ||||||
|  | 	private static final String NODE_URL = "url"; | ||||||
|  | 
 | ||||||
| 	private static final String TYPE_FOLDER = "folder"; | 	private static final String TYPE_FOLDER = "folder"; | ||||||
| 	 | 	 | ||||||
|  | 	private static final int SUCCESS = 100; | ||||||
|  | 	private static final int FAILURE = 403; | ||||||
|  | 	private static final int FILE_NOT_FOUND = 404; | ||||||
|  | 
 | ||||||
| 	private String mStatus; | 	private String mStatus; | ||||||
| 	private int mStatusCode; | 	private int mStatusCode; | ||||||
| 
 | 
 | ||||||
| @ -98,10 +104,19 @@ public class ShareXMLParser { | |||||||
| 	public void setStatusCode(int statusCode) { | 	public void setStatusCode(int statusCode) { | ||||||
| 		this.mStatusCode = statusCode; | 		this.mStatusCode = statusCode; | ||||||
| 	} | 	} | ||||||
| 
 |  | ||||||
| 	// Constructor | 	// Constructor | ||||||
| 	public ShareXMLParser() { | 	public ShareXMLParser() { | ||||||
|  | 		mStatusCode = 100; | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
|  | 	public boolean isSuccess() { | ||||||
|  | 		return mStatusCode == SUCCESS; | ||||||
|  | 	} | ||||||
|  | 	public boolean isFailure() { | ||||||
|  | 		return mStatusCode == FAILURE; | ||||||
|  | 	} | ||||||
|  | 	public boolean isFileNotFound() { | ||||||
|  | 		return mStatusCode == FILE_NOT_FOUND; | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	/** | 	/** | ||||||
| @ -196,6 +211,7 @@ public class ShareXMLParser { | |||||||
| 	 */ | 	 */ | ||||||
| 	private ArrayList<OCShare> readData(XmlPullParser parser) throws XmlPullParserException, IOException { | 	private ArrayList<OCShare> readData(XmlPullParser parser) throws XmlPullParserException, IOException { | ||||||
| 		ArrayList<OCShare> shares = new ArrayList<OCShare>(); | 		ArrayList<OCShare> shares = new ArrayList<OCShare>(); | ||||||
|  | 		OCShare share = null; | ||||||
| 
 | 
 | ||||||
| 		parser.require(XmlPullParser.START_TAG, ns, NODE_DATA);		 | 		parser.require(XmlPullParser.START_TAG, ns, NODE_DATA);		 | ||||||
| 		Log.d(TAG, "---- NODE DATA ---"); | 		Log.d(TAG, "---- NODE DATA ---"); | ||||||
| @ -206,15 +222,35 @@ public class ShareXMLParser { | |||||||
| 			String name = parser.getName(); | 			String name = parser.getName(); | ||||||
| 			if (name.equalsIgnoreCase(NODE_ELEMENT)) { | 			if (name.equalsIgnoreCase(NODE_ELEMENT)) { | ||||||
| 				shares.add(readElement(parser)); | 				shares.add(readElement(parser)); | ||||||
|  | 			}  else if (name.equalsIgnoreCase(NODE_ID)) {// Parse Create XML Response | ||||||
|  | 				share = new OCShare(); | ||||||
|  | 				String value = readNode(parser, NODE_ID); | ||||||
|  | 				share.setIdRemoteShared(Integer.parseInt(value)); | ||||||
|  | 
 | ||||||
|  | 			} else if (name.equalsIgnoreCase(NODE_URL)) { | ||||||
|  | 				share.setShareType(ShareType.PUBLIC_LINK); | ||||||
|  | 				String value = readNode(parser, NODE_URL); | ||||||
|  | 				share.setShareLink(value); | ||||||
|  | 
 | ||||||
|  | 			}  else if (name.equalsIgnoreCase(NODE_TOKEN)) { | ||||||
|  | 				share.setToken(readNode(parser, NODE_TOKEN)); | ||||||
|  | 
 | ||||||
| 			} else { | 			} else { | ||||||
| 				skip(parser); | 				skip(parser); | ||||||
|  | 				 | ||||||
| 			}  | 			}  | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
|  | 		if (share != null) { | ||||||
|  | 			shares.add(share); | ||||||
|  | 		} | ||||||
|  | 			 | ||||||
|  | 
 | ||||||
| 		return shares; | 		return shares; | ||||||
| 
 | 
 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| 	/**  | 	/**  | ||||||
| 	 * Parse Element node | 	 * Parse Element node | ||||||
| 	 * @param parser | 	 * @param parser | ||||||
| @ -239,7 +275,8 @@ public class ShareXMLParser { | |||||||
| 				share.setIdRemoteShared(Integer.parseInt(readNode(parser, NODE_ID))); | 				share.setIdRemoteShared(Integer.parseInt(readNode(parser, NODE_ID))); | ||||||
| 
 | 
 | ||||||
| 			} else if (name.equalsIgnoreCase(NODE_ITEM_TYPE)) { | 			} else if (name.equalsIgnoreCase(NODE_ITEM_TYPE)) { | ||||||
| 				share.setIsDirectory(readNode(parser, NODE_ITEM_TYPE).equalsIgnoreCase(TYPE_FOLDER)); | 				share.setIsFolder(readNode(parser, NODE_ITEM_TYPE).equalsIgnoreCase(TYPE_FOLDER)); | ||||||
|  | 				fixPathForFolder(share); | ||||||
| 
 | 
 | ||||||
| 			} else if (name.equalsIgnoreCase(NODE_ITEM_SOURCE)) { | 			} else if (name.equalsIgnoreCase(NODE_ITEM_SOURCE)) { | ||||||
| 				share.setItemSource(Long.parseLong(readNode(parser, NODE_ITEM_SOURCE))); | 				share.setItemSource(Long.parseLong(readNode(parser, NODE_ITEM_SOURCE))); | ||||||
| @ -259,6 +296,7 @@ public class ShareXMLParser { | |||||||
| 
 | 
 | ||||||
| 			} else if (name.equalsIgnoreCase(NODE_PATH)) { | 			} else if (name.equalsIgnoreCase(NODE_PATH)) { | ||||||
| 				share.setPath(readNode(parser, NODE_PATH)); | 				share.setPath(readNode(parser, NODE_PATH)); | ||||||
|  | 				fixPathForFolder(share); | ||||||
| 
 | 
 | ||||||
| 			} else if (name.equalsIgnoreCase(NODE_PERMISSIONS)) { | 			} else if (name.equalsIgnoreCase(NODE_PERMISSIONS)) { | ||||||
| 				share.setPermissions(Integer.parseInt(readNode(parser, NODE_PERMISSIONS))); | 				share.setPermissions(Integer.parseInt(readNode(parser, NODE_PERMISSIONS))); | ||||||
| @ -291,6 +329,12 @@ public class ShareXMLParser { | |||||||
| 		return share; | 		return share; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	private void fixPathForFolder(OCShare share) { | ||||||
|  | 		if (share.isFolder() && share.getPath() != null && share.getPath().length() > 0 && !share.getPath().endsWith(FileUtils.PATH_SEPARATOR)) { | ||||||
|  | 			share.setPath(share.getPath() + FileUtils.PATH_SEPARATOR); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	/** | 	/** | ||||||
| 	 * Parse a node, to obtain its text. Needs readText method | 	 * Parse a node, to obtain its text. Needs readText method | ||||||
| 	 * @param parser | 	 * @param parser | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user