├── .gitignore ├── AUTHORS ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── yandex │ │ └── disk │ │ └── rest │ │ └── example │ │ ├── Application.java │ │ ├── ContextMenuDialogFragment.java │ │ ├── Credentials.java │ │ ├── DeleteItemFragment.java │ │ ├── DownloadFileFragment.java │ │ ├── ExampleActivity.java │ │ ├── IODialogFragment.java │ │ ├── IODialogRetainedFragment.java │ │ ├── ListExampleFragment.java │ │ ├── ListExampleLoader.java │ │ ├── ListItem.java │ │ ├── MakeFolderFragment.java │ │ ├── MakeItemPublicFragment.java │ │ ├── RenameMoveItemFragment.java │ │ ├── RestClientUtil.java │ │ └── UploadFileFragment.java │ └── res │ ├── drawable-hdpi │ ├── add_folder_icon.png │ ├── add_icon.png │ └── ic_launcher.png │ ├── drawable-ldpi │ ├── add_folder_icon.png │ ├── add_icon.png │ └── ic_launcher.png │ ├── drawable-mdpi │ ├── add_folder_icon.png │ ├── add_icon.png │ └── ic_launcher.png │ ├── drawable-xhdpi │ ├── add_folder_icon.png │ ├── add_icon.png │ └── ic_launcher.png │ ├── drawable │ └── icon.png │ ├── layout │ └── make_folder_layout.xml │ ├── menu │ ├── example_action_bar.xml │ └── example_context_menu.xml │ └── values │ └── strings.xml ├── build.gradle ├── disk-restapi-sdk ├── .gitignore ├── build.gradle └── src │ ├── main │ └── java │ │ └── com │ │ └── yandex │ │ └── disk │ │ └── rest │ │ ├── Credentials.java │ │ ├── CustomHeader.java │ │ ├── DownloadListener.java │ │ ├── FileDownloadListener.java │ │ ├── OkHttpClientFactory.java │ │ ├── ProgressListener.java │ │ ├── QueryBuilder.java │ │ ├── RequestBodyProgress.java │ │ ├── ResourcesArgs.java │ │ ├── ResourcesHandler.java │ │ ├── RestClient.java │ │ ├── RestClientIO.java │ │ ├── exceptions │ │ ├── CancelledDownloadException.java │ │ ├── CancelledUploadingException.java │ │ ├── DownloadNoSpaceAvailableException.java │ │ ├── RetrofitConversionException.java │ │ ├── ServerException.java │ │ ├── ServerIOException.java │ │ ├── WrongMethodException.java │ │ └── http │ │ │ ├── BadGatewayException.java │ │ │ ├── BadRequestException.java │ │ │ ├── ConflictException.java │ │ │ ├── FileNotModifiedException.java │ │ │ ├── FileTooBigException.java │ │ │ ├── ForbiddenException.java │ │ │ ├── GoneException.java │ │ │ ├── HttpCodeException.java │ │ │ ├── InsufficientStorageException.java │ │ │ ├── InternalServerException.java │ │ │ ├── LockedException.java │ │ │ ├── MethodNotAllowedException.java │ │ │ ├── NotAcceptableException.java │ │ │ ├── NotFoundException.java │ │ │ ├── NotImplementedException.java │ │ │ ├── PreconditionFailedException.java │ │ │ ├── RangeNotSatisfiableException.java │ │ │ ├── ServiceUnavailableException.java │ │ │ ├── TooManyRequestsException.java │ │ │ ├── UnauthorizedException.java │ │ │ ├── UnprocessableEntityException.java │ │ │ └── UnsupportedMediaTypeException.java │ │ ├── json │ │ ├── ApiError.java │ │ ├── ApiVersion.java │ │ ├── DiskInfo.java │ │ ├── Link.java │ │ ├── Operation.java │ │ ├── Resource.java │ │ └── ResourceList.java │ │ ├── retrofit │ │ ├── CloudApi.java │ │ ├── ErrorHandlerImpl.java │ │ └── RequestInterceptorImpl.java │ │ └── util │ │ ├── Hash.java │ │ ├── ISO8601.java │ │ └── ResourcePath.java │ └── test │ └── java │ └── com │ └── yandex │ └── disk │ └── rest │ ├── LoggingInterceptor.java │ ├── QueryBuilderTest.java │ ├── RestClientTest.java │ └── util │ ├── ISO8601Test.java │ └── ResourcePathTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── testResources └── test-upload-001.bin /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/* 4 | /build 5 | *.iml 6 | .DS_Store 7 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | The following authors have created the source code of 2 | "Java SDK for Yandex.Disk" published and distributed 3 | by YANDEX LLC as the owner: 4 | Oleg Okhotnikov 5 | 6 | The following authors have licensed their contributions 7 | to YANDEX LLC and everyone who uses "Java SDK for Yandex.Disk" 8 | under the licensing terms detailed in LICENSE available 9 | at http://www.apache.org/licenses/LICENSE-2.0. 10 | no one yet 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (C) YANDEX LLC, 2015 2 | 3 | The Source Code called "Java SDK for Yandex.Disk" available at 4 | https://github.com/yandex-disk/yandex-disk-restapi-java is subject 5 | to the terms of the Apache License, Version 2.0 (hereinafter 6 | referred to as the "License"). The text of the License is the following: 7 | 8 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 9 | 10 | 1. Definitions. 11 | 12 | "License" shall mean the terms and conditions for use, reproduction, 13 | and distribution as defined by Sections 1 through 9 of this document. 14 | 15 | "Licensor" shall mean the copyright owner or entity authorized by 16 | the copyright owner that is granting the License. 17 | 18 | "Legal Entity" shall mean the union of the acting entity and all 19 | other entities that control, are controlled by, or are under common 20 | control with that entity. For the purposes of this definition, 21 | "control" means (i) the power, direct or indirect, to cause the 22 | direction or management of such entity, whether by contract or 23 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 24 | outstanding shares, or (iii) beneficial ownership of such entity. 25 | 26 | "You" (or "Your") shall mean an individual or Legal Entity 27 | exercising permissions granted by this License. 28 | 29 | "Source" form shall mean the preferred form for making modifications, 30 | including but not limited to software source code, documentation 31 | source, and configuration files. 32 | 33 | "Object" form shall mean any form resulting from mechanical 34 | transformation or translation of a Source form, including but 35 | not limited to compiled object code, generated documentation, 36 | and conversions to other media types. 37 | 38 | "Work" shall mean the work of authorship, whether in Source or 39 | Object form, made available under the License, as indicated by a 40 | copyright notice that is included in or attached to the work 41 | (an example is provided in the Appendix below). 42 | 43 | "Derivative Works" shall mean any work, whether in Source or Object 44 | form, that is based on (or derived from) the Work and for which the 45 | editorial revisions, annotations, elaborations, or other modifications 46 | represent, as a whole, an original work of authorship. For the purposes 47 | of this License, Derivative Works shall not include works that remain 48 | separable from, or merely link (or bind by name) to the interfaces of, 49 | the Work and Derivative Works thereof. 50 | 51 | "Contribution" shall mean any work of authorship, including 52 | the original version of the Work and any modifications or additions 53 | to that Work or Derivative Works thereof, that is intentionally 54 | submitted to Licensor for inclusion in the Work by the copyright owner 55 | or by an individual or Legal Entity authorized to submit on behalf of 56 | the copyright owner. For the purposes of this definition, "submitted" 57 | means any form of electronic, verbal, or written communication sent 58 | to the Licensor or its representatives, including but not limited to 59 | communication on electronic mailing lists, source code control systems, 60 | and issue tracking systems that are managed by, or on behalf of, the 61 | Licensor for the purpose of discussing and improving the Work, but 62 | excluding communication that is conspicuously marked or otherwise 63 | designated in writing by the copyright owner as "Not a Contribution." 64 | 65 | "Contributor" shall mean Licensor and any individual or Legal Entity 66 | on behalf of whom a Contribution has been received by Licensor and 67 | subsequently incorporated within the Work. 68 | 69 | 2. Grant of Copyright License. Subject to the terms and conditions of 70 | this License, each Contributor hereby grants to You a perpetual, 71 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 72 | copyright license to reproduce, prepare Derivative Works of, 73 | publicly display, publicly perform, sublicense, and distribute the 74 | Work and such Derivative Works in Source or Object form. 75 | 76 | 3. Grant of Patent License. Subject to the terms and conditions of 77 | this License, each Contributor hereby grants to You a perpetual, 78 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 79 | (except as stated in this section) patent license to make, have made, 80 | use, offer to sell, sell, import, and otherwise transfer the Work, 81 | where such license applies only to those patent claims licensable 82 | by such Contributor that are necessarily infringed by their 83 | Contribution(s) alone or by combination of their Contribution(s) 84 | with the Work to which such Contribution(s) was submitted. If You 85 | institute patent litigation against any entity (including a 86 | cross-claim or counterclaim in a lawsuit) alleging that the Work 87 | or a Contribution incorporated within the Work constitutes direct 88 | or contributory patent infringement, then any patent licenses 89 | granted to You under this License for that Work shall terminate 90 | as of the date such litigation is filed. 91 | 92 | 4. Redistribution. You may reproduce and distribute copies of the 93 | Work or Derivative Works thereof in any medium, with or without 94 | modifications, and in Source or Object form, provided that You 95 | meet the following conditions: 96 | 97 | (a) You must give any other recipients of the Work or 98 | Derivative Works a copy of this License; and 99 | 100 | (b) You must cause any modified files to carry prominent notices 101 | stating that You changed the files; and 102 | 103 | (c) You must retain, in the Source form of any Derivative Works 104 | that You distribute, all copyright, patent, trademark, and 105 | attribution notices from the Source form of the Work, 106 | excluding those notices that do not pertain to any part of 107 | the Derivative Works; and 108 | 109 | (d) If the Work includes a "NOTICE" text file as part of its 110 | distribution, then any Derivative Works that You distribute must 111 | include a readable copy of the attribution notices contained 112 | within such NOTICE file, excluding those notices that do not 113 | pertain to any part of the Derivative Works, in at least one 114 | of the following places: within a NOTICE text file distributed 115 | as part of the Derivative Works; within the Source form or 116 | documentation, if provided along with the Derivative Works; or, 117 | within a display generated by the Derivative Works, if and 118 | wherever such third-party notices normally appear. The contents 119 | of the NOTICE file are for informational purposes only and 120 | do not modify the License. You may add Your own attribution 121 | notices within Derivative Works that You distribute, alongside 122 | or as an addendum to the NOTICE text from the Work, provided 123 | that such additional attribution notices cannot be construed 124 | as modifying the License. 125 | 126 | You may add Your own copyright statement to Your modifications and 127 | may provide additional or different license terms and conditions 128 | for use, reproduction, or distribution of Your modifications, or 129 | for any such Derivative Works as a whole, provided Your use, 130 | reproduction, and distribution of the Work otherwise complies with 131 | the conditions stated in this License. 132 | 133 | 5. Submission of Contributions. Unless You explicitly state otherwise, 134 | any Contribution intentionally submitted for inclusion in the Work 135 | by You to the Licensor shall be under the terms and conditions of 136 | this License, without any additional terms or conditions. 137 | Notwithstanding the above, nothing herein shall supersede or modify 138 | the terms of any separate license agreement you may have executed 139 | with Licensor regarding such Contributions. 140 | 141 | 6. Trademarks. This License does not grant permission to use the trade 142 | names, trademarks, service marks, or product names of the Licensor, 143 | except as required for reasonable and customary use in describing the 144 | origin of the Work and reproducing the content of the NOTICE file. 145 | 146 | 7. Disclaimer of Warranty. Unless required by applicable law or 147 | agreed to in writing, Licensor provides the Work (and each 148 | Contributor provides its Contributions) on an "AS IS" BASIS, 149 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 150 | implied, including, without limitation, any warranties or conditions 151 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 152 | PARTICULAR PURPOSE. You are solely responsible for determining the 153 | appropriateness of using or redistributing the Work and assume any 154 | risks associated with Your exercise of permissions under this License. 155 | 156 | 8. Limitation of Liability. In no event and under no legal theory, 157 | whether in tort (including negligence), contract, or otherwise, 158 | unless required by applicable law (such as deliberate and grossly 159 | negligent acts) or agreed to in writing, shall any Contributor be 160 | liable to You for damages, including any direct, indirect, special, 161 | incidental, or consequential damages of any character arising as a 162 | result of this License or out of the use or inability to use the 163 | Work (including but not limited to damages for loss of goodwill, 164 | work stoppage, computer failure or malfunction, or any and all 165 | other commercial damages or losses), even if such Contributor 166 | has been advised of the possibility of such damages. 167 | 168 | 9. Accepting Warranty or Additional Liability. While redistributing 169 | the Work or Derivative Works thereof, You may choose to offer, 170 | and charge a fee for, acceptance of support, warranty, indemnity, 171 | or other liability obligations and/or rights consistent with this 172 | License. However, in accepting such obligations, You may act only 173 | on Your own behalf and on Your sole responsibility, not on behalf 174 | of any other Contributor, and only if You agree to indemnify, 175 | defend, and hold each Contributor harmless for any liability 176 | incurred by, or claims asserted against, such Contributor by reason 177 | of your accepting any such warranty or additional liability. 178 | 179 | END OF TERMS AND CONDITIONS 180 | 181 | A copy of the License is also available 182 | at http://www.apache.org/licenses/LICENSE-2.0. 183 | ``` 184 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Java SDK for Yandex.Disk REST API 2 | =================================== 3 | 4 | 5 | The Yandex.Disk SDK is intended for applications that work with the files of Yandex.Disk users or store 6 | their own files and settings on Yandex.Disk. 7 | Please follow the rules and recommendations in the REST API Terms of Use and brand guide. 8 | 9 | Usage 10 | 11 | dependencies { 12 | compile 'com.yandex.android:disk-restapi-sdk:1.0' 13 | } 14 | 15 | 16 | Links 17 | 18 | https://api.yandex.com/disk/api/concepts/about.xml 19 | http://legal.yandex.ru/disk_api/ 20 | 21 | 22 | License 23 | ------- 24 | 25 | (C) 2015 Yandex LLC (https://yandex.com/) 26 | 27 | The source code of Java SDK for Yandex.Disk REST API 28 | is available to use under terms of Apache License, Version 2.0. 29 | See the file LICENSE for the details. 30 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'idea' 3 | 4 | android { 5 | compileSdkVersion 21 6 | buildToolsVersion "21.1.2" 7 | 8 | defaultConfig { 9 | applicationId "com.yandex.disk.rest.example" 10 | minSdkVersion 15 11 | targetSdkVersion 21 12 | versionCode 1 13 | versionName "1.0" 14 | } 15 | 16 | compileOptions { 17 | sourceCompatibility JavaVersion.VERSION_1_7 18 | targetCompatibility JavaVersion.VERSION_1_7 19 | } 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | } 27 | 28 | repositories { 29 | mavenCentral() 30 | } 31 | 32 | dependencies { 33 | compile 'com.android.support:support-v4:21.0.3' 34 | compile 'org.slf4j:slf4j-android:1.7.10' 35 | compile 'com.facebook.stetho:stetho:1.0.0' 36 | compile 'com.facebook.stetho:stetho-okhttp:1.0.0' 37 | compile project (':disk-restapi-sdk') 38 | } 39 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /usr/local/android-sdk-macosx/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 13 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | 12 | import java.util.ArrayList; 13 | 14 | import android.content.Context; 15 | 16 | import com.facebook.stetho.DumperPluginsProvider; 17 | import com.facebook.stetho.Stetho; 18 | import com.facebook.stetho.dumpapp.DumperPlugin; 19 | 20 | public class Application extends android.app.Application { 21 | 22 | @Override 23 | public void onCreate() { 24 | super.onCreate(); 25 | 26 | final Context context = this; 27 | Stetho.initialize( 28 | Stetho.newInitializerBuilder(context) 29 | .enableDumpapp(new SampleDumperPluginsProvider(context)) 30 | .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(context)) 31 | .build()); 32 | } 33 | 34 | private static class SampleDumperPluginsProvider implements DumperPluginsProvider { 35 | private final Context mContext; 36 | 37 | public SampleDumperPluginsProvider(Context context) { 38 | mContext = context; 39 | } 40 | 41 | @Override 42 | public Iterable get() { 43 | ArrayList plugins = new ArrayList(); 44 | for (DumperPlugin defaultPlugin : Stetho.defaultDumperPluginsProvider(mContext).get()) { 45 | plugins.add(defaultPlugin); 46 | } 47 | // plugins.add(new HelloWorldDumperPlugin()); 48 | return plugins; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/ContextMenuDialogFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.os.Bundle; 12 | import android.support.v4.app.DialogFragment; 13 | 14 | abstract class ContextMenuDialogFragment extends DialogFragment { 15 | 16 | protected static final String CREDENTIALS = "example.credentials"; 17 | protected static final String LIST_ITEM = "example.list.item"; 18 | 19 | protected Credentials credentials; 20 | protected ListItem listItem; 21 | 22 | protected static T newInstance(T fragment, Credentials credentials, ListItem listItem) { 23 | Bundle args = new Bundle(); 24 | args.putParcelable(CREDENTIALS, credentials); 25 | args.putParcelable(LIST_ITEM, listItem); 26 | fragment.setArguments(args); 27 | return fragment; 28 | } 29 | 30 | @Override 31 | public void onCreate(Bundle savedInstanceState) { 32 | super.onCreate(savedInstanceState); 33 | 34 | credentials = getArguments().getParcelable(CREDENTIALS); 35 | listItem = getArguments().getParcelable(LIST_ITEM); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/Credentials.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.os.Parcel; 12 | import android.os.Parcelable; 13 | 14 | public class Credentials extends com.yandex.disk.rest.Credentials implements Parcelable { 15 | 16 | public Credentials(String user, String token) { 17 | super(user, token); 18 | } 19 | 20 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 21 | 22 | public Credentials createFromParcel(Parcel parcel) { 23 | return new Credentials(parcel.readString(), parcel.readString()); 24 | } 25 | 26 | public Credentials[] newArray(int size) { 27 | return new Credentials[size]; 28 | } 29 | }; 30 | 31 | @Override 32 | public int describeContents() { 33 | return 0; 34 | } 35 | 36 | @Override 37 | public void writeToParcel(Parcel parcel, int flags) { 38 | parcel.writeString(user); 39 | parcel.writeString(token); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/DeleteItemFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.app.Dialog; 12 | import android.app.ProgressDialog; 13 | import android.content.DialogInterface; 14 | import android.os.AsyncTask; 15 | import android.os.Bundle; 16 | import android.support.v4.app.FragmentManager; 17 | import android.util.Log; 18 | import android.widget.Toast; 19 | 20 | import com.yandex.disk.rest.RestClient; 21 | import com.yandex.disk.rest.exceptions.ServerException; 22 | import com.yandex.disk.rest.exceptions.http.HttpCodeException; 23 | 24 | import java.io.IOException; 25 | 26 | public class DeleteItemFragment extends IODialogFragment { 27 | 28 | private static final String TAG = "DeleteItemFragment"; 29 | 30 | private static final String WORK_FRAGMENT_TAG = "DeleteItemFragment.Background"; 31 | 32 | private static final String DELETE_PATH = "example.delete.path"; 33 | private static final String DELETE_DISPLAY_NAME = "example.delete.display.name"; 34 | private static final String DELETE_IS_COLLECTION = "example.delete.is.collection"; 35 | 36 | private Credentials credentials; 37 | private String path, displayName; 38 | private boolean isCollection; 39 | 40 | private DeleteItemRetainedFragment workFragment; 41 | 42 | public static DeleteItemFragment newInstance(Credentials credentials, String path, String displayName, boolean isCollection) { 43 | DeleteItemFragment fragment = new DeleteItemFragment(); 44 | 45 | Bundle args = new Bundle(); 46 | args.putParcelable(CREDENTIALS, credentials); 47 | args.putString(DELETE_PATH, path); 48 | args.putString(DELETE_DISPLAY_NAME, displayName); 49 | args.putBoolean(DELETE_IS_COLLECTION, isCollection); 50 | fragment.setArguments(args); 51 | 52 | return fragment; 53 | } 54 | 55 | @Override 56 | public void onCreate(Bundle savedInstanceState) { 57 | super.onCreate(savedInstanceState); 58 | 59 | credentials = getArguments().getParcelable(CREDENTIALS); 60 | path = getArguments().getString(DELETE_PATH); 61 | displayName = getArguments().getString(DELETE_DISPLAY_NAME); 62 | isCollection = getArguments().getBoolean(DELETE_IS_COLLECTION); 63 | } 64 | 65 | @Override 66 | public void onActivityCreated (Bundle savedInstanceState) { 67 | super.onActivityCreated(savedInstanceState); 68 | 69 | FragmentManager fragmentManager = getFragmentManager(); 70 | workFragment = (DeleteItemRetainedFragment) fragmentManager.findFragmentByTag(WORK_FRAGMENT_TAG); 71 | if (workFragment == null || workFragment.getTargetFragment() == null) { 72 | workFragment = new DeleteItemRetainedFragment(); 73 | fragmentManager.beginTransaction().add(workFragment, WORK_FRAGMENT_TAG).commit(); 74 | workFragment.deleteItem(credentials, path); 75 | } 76 | workFragment.setTargetFragment(this, 0); 77 | } 78 | 79 | @Override 80 | public void onDetach() { 81 | super.onDetach(); 82 | 83 | if (workFragment != null) { 84 | workFragment.setTargetFragment(null, 0); 85 | } 86 | } 87 | 88 | @Override 89 | public Dialog onCreateDialog(Bundle savedInstanceState) { 90 | dialog = new ProgressDialog(getActivity()); 91 | dialog.setTitle(isCollection ? R.string.example_delete_folder_title : R.string.example_delete_file_title); 92 | dialog.setMessage(displayName); 93 | dialog.setIndeterminate(true); 94 | dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 95 | dialog.setButton(ProgressDialog.BUTTON_NEUTRAL, getText(R.string.example_make_folder_negative_button), new DialogInterface.OnClickListener() { 96 | @Override 97 | public void onClick (DialogInterface dialog, int which) { 98 | dialog.dismiss(); 99 | onCancel(); 100 | } 101 | }); 102 | return dialog; 103 | } 104 | 105 | @Override 106 | public void onCancel(DialogInterface dialog) { 107 | super.onCancel(dialog); 108 | onCancel(); 109 | } 110 | 111 | private void onCancel() { 112 | } 113 | 114 | public void onComplete() { 115 | dialog.dismiss(); 116 | Toast.makeText(getActivity(), isCollection ? R.string.example_delete_folder_done : R.string.example_delete_file_done, Toast.LENGTH_LONG).show(); 117 | ((ExampleActivity) getActivity()).reloadContent(); 118 | } 119 | 120 | public static class DeleteItemRetainedFragment extends IODialogRetainedFragment { 121 | 122 | public void deleteItem(final Credentials credentials, final String path) { 123 | 124 | new AsyncTask() { 125 | 126 | @Override 127 | protected Void doInBackground(Void... params) { 128 | try { 129 | RestClient client = RestClientUtil.getInstance(credentials); 130 | client.delete(path, false); 131 | } catch (HttpCodeException ex) { 132 | Log.d(TAG, "deleteItem", ex); 133 | sendException(ex.getResponse().getDescription()); 134 | } catch (IOException | ServerException ex) { 135 | Log.d(TAG, "deleteItem", ex); 136 | sendException(ex); 137 | } 138 | return null; 139 | } 140 | 141 | @Override 142 | protected void onPostExecute(Void aVoid) { 143 | DeleteItemFragment targetFragment = (DeleteItemFragment) getTargetFragment(); 144 | if (targetFragment != null) { 145 | targetFragment.onComplete(); 146 | } 147 | } 148 | }.execute(); 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/DownloadFileFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.app.Dialog; 12 | import android.app.ProgressDialog; 13 | import android.content.Context; 14 | import android.content.DialogInterface; 15 | import android.content.Intent; 16 | import android.net.Uri; 17 | import android.os.Bundle; 18 | import android.support.v4.app.FragmentManager; 19 | import android.util.Log; 20 | 21 | import com.yandex.disk.rest.ProgressListener; 22 | import com.yandex.disk.rest.RestClient; 23 | import com.yandex.disk.rest.exceptions.ServerException; 24 | import com.yandex.disk.rest.exceptions.http.HttpCodeException; 25 | 26 | import java.io.File; 27 | import java.io.IOException; 28 | 29 | public class DownloadFileFragment extends IODialogFragment { 30 | 31 | private static final String TAG = "LoadFileFragment"; 32 | 33 | private static final String WORK_FRAGMENT_TAG = "LoadFileFragment.Background"; 34 | 35 | private static final String FILE_ITEM = "example.file.item"; 36 | 37 | private static final int PROGRESS_DIV = 1024 * 1024; 38 | 39 | private Credentials credentials; 40 | private ListItem item; 41 | 42 | private DownloadFileRetainedFragment workFragment; 43 | 44 | public static DownloadFileFragment newInstance(Credentials credentials, ListItem item) { 45 | DownloadFileFragment fragment = new DownloadFileFragment(); 46 | 47 | Bundle args = new Bundle(); 48 | args.putParcelable(CREDENTIALS, credentials); 49 | args.putParcelable(FILE_ITEM, item); 50 | fragment.setArguments(args); 51 | 52 | return fragment; 53 | } 54 | 55 | @Override 56 | public void onCreate(Bundle savedInstanceState) { 57 | super.onCreate(savedInstanceState); 58 | 59 | credentials = getArguments().getParcelable(CREDENTIALS); 60 | item = getArguments().getParcelable(FILE_ITEM); 61 | } 62 | 63 | @Override 64 | public void onActivityCreated (Bundle savedInstanceState) { 65 | super.onActivityCreated(savedInstanceState); 66 | 67 | FragmentManager fragmentManager = getFragmentManager(); 68 | workFragment = (DownloadFileRetainedFragment) fragmentManager.findFragmentByTag(WORK_FRAGMENT_TAG); 69 | if (workFragment == null || workFragment.getTargetFragment() == null) { 70 | workFragment = new DownloadFileRetainedFragment(); 71 | fragmentManager.beginTransaction().add(workFragment, WORK_FRAGMENT_TAG).commit(); 72 | workFragment.loadFile(getActivity(), credentials, item); 73 | } 74 | workFragment.setTargetFragment(this, 0); 75 | } 76 | 77 | @Override 78 | public void onDetach() { 79 | super.onDetach(); 80 | 81 | if (workFragment != null) { 82 | workFragment.setTargetFragment(null, 0); 83 | } 84 | } 85 | 86 | @Override 87 | public Dialog onCreateDialog(Bundle savedInstanceState) { 88 | dialog = new ProgressDialog(getActivity()); 89 | dialog.setTitle(R.string.example_loading_file_title); 90 | dialog.setMessage(item.getName()); 91 | dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 92 | dialog.setIndeterminate(true); 93 | dialog.setButton(ProgressDialog.BUTTON_NEUTRAL, getString(R.string.example_loading_file_cancel_button), new DialogInterface.OnClickListener() { 94 | @Override 95 | public void onClick(DialogInterface dialog, int which) { 96 | dialog.dismiss(); 97 | onCancel(); 98 | } 99 | }); 100 | dialog.setOnCancelListener(this); 101 | dialog.show(); 102 | return dialog; 103 | } 104 | 105 | @Override 106 | public void onCancel(DialogInterface dialog) { 107 | super.onCancel(dialog); 108 | onCancel(); 109 | } 110 | 111 | private void onCancel() { 112 | workFragment.cancelDownload(); 113 | } 114 | 115 | public void onDownloadComplete(File file) { 116 | dialog.dismiss(); 117 | makeWorldReadableAndOpenFile(file); 118 | } 119 | 120 | private void makeWorldReadableAndOpenFile(File file) { 121 | file.setReadable(true, false); 122 | Intent intent = new Intent(); 123 | intent.setAction(Intent.ACTION_VIEW); 124 | intent.setDataAndType(Uri.fromFile(file), item.getContentType()); 125 | startActivity(Intent.createChooser(intent, getText(R.string.example_loading_file_chooser_title))); 126 | } 127 | 128 | public void setDownloadProgress(long loaded, long total) { 129 | if (dialog != null) { 130 | if (dialog.isIndeterminate()) { 131 | dialog.setIndeterminate(false); 132 | } 133 | if (total > Integer.MAX_VALUE) { 134 | dialog.setProgress((int)(loaded / PROGRESS_DIV)); 135 | dialog.setMax((int)(total / PROGRESS_DIV)); 136 | } else { 137 | dialog.setProgress((int)loaded); 138 | dialog.setMax((int)total); 139 | } 140 | } 141 | } 142 | 143 | public static class DownloadFileRetainedFragment extends IODialogRetainedFragment implements ProgressListener { 144 | 145 | private boolean cancelled; 146 | private File result; 147 | 148 | public void loadFile(final Context context, final Credentials credentials, final ListItem item) { 149 | result = new File(context.getFilesDir(), new File(item.getPath()).getName()); 150 | 151 | new Thread(new Runnable() { 152 | @Override 153 | public void run () { 154 | try { 155 | RestClient client = RestClientUtil.getInstance(credentials); 156 | client.downloadFile(item.getPath(), result, DownloadFileRetainedFragment.this); 157 | downloadComplete(); 158 | } catch (HttpCodeException ex) { 159 | Log.d(TAG, "loadFile", ex); 160 | sendException(ex.getResponse().getDescription()); 161 | } catch (IOException | ServerException ex) { 162 | Log.d(TAG, "loadFile", ex); 163 | sendException(ex); 164 | } 165 | } 166 | }).start(); 167 | } 168 | 169 | @Override 170 | public void updateProgress (final long loaded, final long total) { 171 | handler.post(new Runnable() { 172 | @Override 173 | public void run() { 174 | DownloadFileFragment targetFragment = (DownloadFileFragment) getTargetFragment(); 175 | if (targetFragment != null) { 176 | targetFragment.setDownloadProgress(loaded, total); 177 | } 178 | } 179 | }); 180 | } 181 | 182 | @Override 183 | public boolean hasCancelled () { 184 | return cancelled; 185 | } 186 | 187 | public void downloadComplete() { 188 | handler.post(new Runnable() { 189 | @Override 190 | public void run() { 191 | DownloadFileFragment targetFragment = (DownloadFileFragment) getTargetFragment(); 192 | if (targetFragment != null) { 193 | targetFragment.onDownloadComplete(result); 194 | } 195 | } 196 | }); 197 | } 198 | 199 | public void cancelDownload() { 200 | cancelled = true; 201 | } 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/ExampleActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.app.AlertDialog; 12 | import android.app.Dialog; 13 | import android.content.DialogInterface; 14 | import android.content.Intent; 15 | import android.content.SharedPreferences; 16 | import android.net.Uri; 17 | import android.os.Bundle; 18 | import android.preference.PreferenceManager; 19 | import android.support.v4.app.DialogFragment; 20 | import android.support.v4.app.FragmentActivity; 21 | import android.text.TextUtils; 22 | import android.util.Log; 23 | 24 | import java.util.regex.Matcher; 25 | import java.util.regex.Pattern; 26 | 27 | public class ExampleActivity extends FragmentActivity { 28 | 29 | private static final String TAG = "ExampleActivity"; 30 | 31 | public static final String FRAGMENT_TAG = "list"; 32 | 33 | // create your own client id/secret pair with callback url on oauth.yandex.ru 34 | public static final String CLIENT_ID = "82e97a6ea25547478f7824c572e7c625"; 35 | 36 | public static final String AUTH_URL = "https://oauth.yandex.ru/authorize?response_type=token&client_id="+CLIENT_ID; 37 | 38 | public static final String USERNAME = "example.username"; 39 | public static final String TOKEN = "example.token"; 40 | 41 | @Override 42 | public void onCreate (Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | 45 | if (getIntent() != null && getIntent().getData() != null) { 46 | onLogin(); 47 | } 48 | 49 | SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 50 | String token = preferences.getString(TOKEN, null); 51 | if (token == null) { 52 | startLogin(); 53 | return; 54 | } 55 | 56 | if (savedInstanceState == null) { 57 | startFragment(); 58 | } 59 | } 60 | 61 | private void startFragment() { 62 | getSupportFragmentManager().beginTransaction() 63 | .replace(android.R.id.content, new ListExampleFragment(), FRAGMENT_TAG) 64 | .commit(); 65 | } 66 | 67 | private void onLogin () { 68 | Uri data = getIntent().getData(); 69 | setIntent(null); 70 | Pattern pattern = Pattern.compile("access_token=(.*?)(&|$)"); 71 | Matcher matcher = pattern.matcher(data.toString()); 72 | if (matcher.find()) { 73 | final String token = matcher.group(1); 74 | if (!TextUtils.isEmpty(token)) { 75 | Log.d(TAG, "onLogin: token: "+token); 76 | saveToken(token); 77 | } else { 78 | Log.w(TAG, "onRegistrationSuccess: empty token"); 79 | } 80 | } else { 81 | Log.w(TAG, "onRegistrationSuccess: token not found in return url"); 82 | } 83 | } 84 | 85 | private void saveToken(String token) { 86 | SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit(); 87 | editor.putString(USERNAME, ""); 88 | editor.putString(TOKEN, token); 89 | editor.apply(); 90 | } 91 | 92 | public void reloadContent() { 93 | ListExampleFragment fragment = (ListExampleFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG); 94 | fragment.restartLoader(); 95 | } 96 | 97 | public void startLogin() { 98 | new AuthDialogFragment().show(getSupportFragmentManager(), "auth"); 99 | } 100 | 101 | public static class AuthDialogFragment extends DialogFragment { 102 | 103 | public AuthDialogFragment () { 104 | super(); 105 | } 106 | 107 | @Override 108 | public Dialog onCreateDialog(Bundle savedInstanceState) { 109 | return new AlertDialog.Builder(getActivity()) 110 | .setTitle(R.string.example_auth_title) 111 | .setMessage(R.string.example_auth_message) 112 | .setPositiveButton(R.string.example_auth_positive_button, new DialogInterface.OnClickListener() { 113 | @Override 114 | public void onClick (DialogInterface dialog, int which) { 115 | dialog.dismiss(); 116 | startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(AUTH_URL))); 117 | } 118 | }) 119 | .setNegativeButton(R.string.example_auth_negative_button, new DialogInterface.OnClickListener() { 120 | @Override 121 | public void onClick (DialogInterface dialog, int which) { 122 | dialog.dismiss(); 123 | getActivity().finish(); 124 | } 125 | }) 126 | .create(); 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/IODialogFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.app.ProgressDialog; 12 | import android.support.v4.app.DialogFragment; 13 | import android.widget.Toast; 14 | 15 | public class IODialogFragment extends DialogFragment { 16 | 17 | protected static final String CREDENTIALS = "example.credentials"; 18 | 19 | protected ProgressDialog dialog; 20 | 21 | public void sendException(final String message) { 22 | dialog.dismiss(); 23 | Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/IODialogRetainedFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.os.Bundle; 12 | import android.os.Handler; 13 | import android.support.v4.app.Fragment; 14 | 15 | public class IODialogRetainedFragment extends Fragment { 16 | 17 | protected Handler handler; 18 | 19 | @Override 20 | public void onCreate(Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | 23 | setRetainInstance(true); 24 | handler = new Handler(); 25 | } 26 | 27 | protected void sendException(final Exception ex) { 28 | sendException(ex.getMessage()); 29 | } 30 | 31 | protected void sendException(final String message) { 32 | handler.post(new Runnable() { 33 | @Override 34 | public void run () { 35 | IODialogFragment targetFragment = (IODialogFragment) getTargetFragment(); 36 | if (targetFragment != null) { 37 | targetFragment.sendException(message); 38 | } 39 | } 40 | }); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/ListExampleFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.app.Activity; 12 | import android.app.AlertDialog; 13 | import android.app.Dialog; 14 | import android.content.Context; 15 | import android.content.DialogInterface; 16 | import android.content.Intent; 17 | import android.content.SharedPreferences; 18 | import android.net.Uri; 19 | import android.os.Bundle; 20 | import android.os.Handler; 21 | import android.preference.PreferenceManager; 22 | import android.support.v4.app.DialogFragment; 23 | import android.support.v4.app.ListFragment; 24 | import android.support.v4.app.LoaderManager; 25 | import android.support.v4.content.Loader; 26 | import android.util.Log; 27 | import android.view.ContextMenu; 28 | import android.view.LayoutInflater; 29 | import android.view.Menu; 30 | import android.view.MenuInflater; 31 | import android.view.MenuItem; 32 | import android.view.View; 33 | import android.view.ViewGroup; 34 | import android.widget.AdapterView; 35 | import android.widget.ArrayAdapter; 36 | import android.widget.EditText; 37 | import android.widget.ListView; 38 | import android.widget.TextView; 39 | import android.widget.Toast; 40 | 41 | import com.yandex.disk.rest.exceptions.http.HttpCodeException; 42 | import com.yandex.disk.rest.exceptions.http.UnauthorizedException; 43 | 44 | import java.io.File; 45 | import java.util.List; 46 | 47 | public class ListExampleFragment extends ListFragment implements LoaderManager.LoaderCallbacks> { 48 | 49 | private static final String TAG = "ListExampleFragment"; 50 | 51 | private static final String CURRENT_DIR_KEY = "example.current.dir"; 52 | 53 | private static final int GET_FILE_TO_UPLOAD = 100; 54 | 55 | private static final String ROOT = "/"; 56 | 57 | private Credentials credentials; 58 | private String currentDir; 59 | 60 | private ListExampleAdapter adapter; 61 | private Handler handler; 62 | 63 | @Override 64 | public void onActivityCreated(Bundle savedInstanceState) { 65 | super.onActivityCreated(savedInstanceState); 66 | 67 | setDefaultEmptyText(); 68 | 69 | setHasOptionsMenu(true); 70 | 71 | registerForContextMenu(getListView()); 72 | 73 | SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 74 | String username = preferences.getString(ExampleActivity.USERNAME, null); 75 | String token = preferences.getString(ExampleActivity.TOKEN, null); 76 | 77 | handler = new Handler(); 78 | credentials = new Credentials(username, token); 79 | 80 | Bundle args = getArguments(); 81 | if (args != null) { 82 | currentDir = args.getString(CURRENT_DIR_KEY); 83 | } 84 | if (currentDir == null) { 85 | currentDir = ROOT; 86 | } 87 | getActivity().getActionBar().setDisplayHomeAsUpEnabled(!ROOT.equals(currentDir)); 88 | 89 | adapter = new ListExampleAdapter(getActivity()); 90 | setListAdapter(adapter); 91 | setListShown(false); 92 | getLoaderManager().initLoader(0, null, this); 93 | } 94 | 95 | public void restartLoader() { 96 | getLoaderManager().restartLoader(0, null, this); 97 | } 98 | 99 | @Override 100 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 101 | super.onCreateContextMenu(menu, v, menuInfo); 102 | 103 | menu.setHeaderTitle(getListItem(menuInfo).getName()); 104 | 105 | MenuInflater inflater = getActivity().getMenuInflater(); 106 | inflater.inflate(R.menu.example_context_menu, menu); 107 | } 108 | 109 | @Override 110 | public boolean onContextItemSelected(MenuItem item) { 111 | ListItem listItem = getListItem(item.getMenuInfo()); 112 | switch (item.getItemId()) { 113 | case R.id.example_context_publish: 114 | Log.d(TAG, "onContextItemSelected: publish: listItem=" + listItem); 115 | if (listItem.getPublicUrl() != null) { 116 | ShowPublicUrlDialogFragment.newInstance(credentials, listItem).show(getFragmentManager(), "showPublicUrlDialog"); 117 | } else { 118 | MakeItemPublicFragment.newInstance(credentials, listItem.getPath(), true).show(getFragmentManager(), "makeItemPublic"); 119 | } 120 | return true; 121 | case R.id.example_context_move: 122 | RenameMoveDialogFragment.newInstance(credentials, listItem).show(getFragmentManager(), "renameMoveDialog"); 123 | return true; 124 | case R.id.example_context_delete: 125 | DeleteItemDialogFragment.newInstance(credentials, listItem).show(getFragmentManager(), "deleteItemDialog"); 126 | return true; 127 | default: 128 | return super.onContextItemSelected(item); 129 | } 130 | } 131 | 132 | private ListItem getListItem(ContextMenu.ContextMenuInfo menuInfo) { 133 | AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; 134 | return (ListItem) getListAdapter().getItem(info.position); 135 | } 136 | 137 | @Override 138 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 139 | super.onCreateOptionsMenu(menu, inflater); 140 | 141 | inflater.inflate(R.menu.example_action_bar, menu); 142 | } 143 | 144 | @Override 145 | public boolean onOptionsItemSelected(MenuItem item) { 146 | switch(item.getItemId()) { 147 | case android.R.id.home: 148 | getFragmentManager().popBackStack(); 149 | break; 150 | case R.id.example_add_file: 151 | openAddFileDialog(); 152 | break; 153 | case R.id.example_make_folder: 154 | MakeFolderDialogFragment.newInstance(credentials, currentDir).show(getFragmentManager(), "makeFolderName"); 155 | break; 156 | default: 157 | return super.onOptionsItemSelected(item); 158 | } 159 | return true; 160 | } 161 | 162 | private void openAddFileDialog() { 163 | Intent intent = new Intent(); 164 | intent.setAction(Intent.ACTION_GET_CONTENT); 165 | intent.setType("*/*"); 166 | intent.addCategory(Intent.CATEGORY_OPENABLE); 167 | startActivityForResult(Intent.createChooser(intent, getText(R.string.example_loading_get_file_to_upload_chooser_title)), GET_FILE_TO_UPLOAD); 168 | } 169 | 170 | @Override 171 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 172 | switch (requestCode) { 173 | case GET_FILE_TO_UPLOAD: 174 | if (resultCode == Activity.RESULT_OK) { 175 | Uri uri = data.getData(); 176 | if ("file".equalsIgnoreCase(uri.getScheme())) { 177 | uploadFile(uri.getPath()); 178 | } else { 179 | Toast.makeText(getActivity(), R.string.example_get_file_unsupported_scheme, Toast.LENGTH_LONG).show(); 180 | } 181 | } 182 | break; 183 | } 184 | super.onActivityResult(requestCode, resultCode, data); 185 | } 186 | 187 | @Override 188 | public Loader> onCreateLoader(int i, Bundle bundle) { 189 | return new ListExampleLoader(getActivity(), credentials, currentDir); 190 | } 191 | 192 | @Override 193 | public void onLoadFinished(final Loader> loader, List data) { 194 | if (isResumed()) { 195 | setListShown(true); 196 | } else { 197 | setListShownNoAnimation(true); 198 | } 199 | if (data.isEmpty()) { 200 | Exception ex = ((ListExampleLoader) loader).getException(); 201 | if (ex != null) { 202 | handleException(ex); 203 | } else { 204 | setDefaultEmptyText(); 205 | } 206 | } else { 207 | adapter.setData(data); 208 | } 209 | } 210 | 211 | private void handleException(Exception ex) { 212 | if (ex instanceof HttpCodeException) { 213 | setEmptyText(((HttpCodeException)ex).getResponse().getDescription()); 214 | if (ex instanceof UnauthorizedException) { 215 | final ExampleActivity activity = ((ExampleActivity) getActivity()); 216 | handler.post(new Runnable() { 217 | @Override 218 | public void run() { 219 | activity.startLogin(); 220 | } 221 | }); 222 | } 223 | } else { 224 | setEmptyText(ex.getMessage()); 225 | } 226 | } 227 | 228 | @Override 229 | public void onLoaderReset(Loader> loader) { 230 | adapter.setData(null); 231 | } 232 | 233 | @Override 234 | public void onListItemClick(ListView listView, View view, int position, long id) { 235 | ListItem item = (ListItem) getListAdapter().getItem(position); 236 | Log.d(TAG, "onListItemClick(): "+item); 237 | if (item.isDir()) { 238 | changeDir(item.getPath()); 239 | } else { 240 | downloadFile(item); 241 | } 242 | } 243 | 244 | protected void changeDir(String dir) { 245 | Bundle args = new Bundle(); 246 | args.putString(CURRENT_DIR_KEY, dir); 247 | 248 | ListExampleFragment fragment = new ListExampleFragment(); 249 | fragment.setArguments(args); 250 | 251 | getFragmentManager().beginTransaction() 252 | .replace(android.R.id.content, fragment, ExampleActivity.FRAGMENT_TAG) 253 | .addToBackStack(null) 254 | .commit(); 255 | } 256 | 257 | private void downloadFile(ListItem item) { 258 | DownloadFileFragment.newInstance(credentials, item).show(getFragmentManager(), "download"); 259 | } 260 | 261 | private void uploadFile(String path) { 262 | UploadFileFragment.newInstance(credentials, currentDir+new File(path).getName(), path).show(getFragmentManager(), "upload"); 263 | } 264 | 265 | private void setDefaultEmptyText() { 266 | setEmptyText(getString(R.string.example_no_files)); 267 | } 268 | 269 | public static class ListExampleAdapter extends ArrayAdapter { 270 | private final LayoutInflater inflater; 271 | 272 | public ListExampleAdapter(Context context) { 273 | super(context, android.R.layout.simple_list_item_2); 274 | inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 275 | } 276 | 277 | public void setData(List data) { 278 | clear(); 279 | if (data != null) { 280 | addAll(data); 281 | } 282 | } 283 | 284 | @Override 285 | public View getView(int position, View convertView, ViewGroup parent) { 286 | View view; 287 | 288 | if (convertView == null) { 289 | view = inflater.inflate(android.R.layout.simple_list_item_2, parent, false); 290 | } else { 291 | view = convertView; 292 | } 293 | 294 | ListItem item = getItem(position); 295 | ((TextView)view.findViewById(android.R.id.text1)).setText(item.getName()); 296 | ((TextView)view.findViewById(android.R.id.text2)).setText(item.isDir() ? "" : ""+item.getContentLength()); 297 | 298 | return view; 299 | } 300 | } 301 | 302 | public static class MakeFolderDialogFragment extends DialogFragment { 303 | 304 | private static final String CREDENTIALS = "example.credentials"; 305 | private static final String CURRENT_DIR = "example.current.dir"; 306 | 307 | private Credentials credentials; 308 | private String currentDir; 309 | 310 | public static MakeFolderDialogFragment newInstance(Credentials credentials, String currentDir) { 311 | MakeFolderDialogFragment fragment = new MakeFolderDialogFragment(); 312 | 313 | Bundle args = new Bundle(); 314 | args.putParcelable(CREDENTIALS, credentials); 315 | args.putString(CURRENT_DIR, currentDir); 316 | fragment.setArguments(args); 317 | 318 | return fragment; 319 | } 320 | 321 | public MakeFolderDialogFragment() { 322 | super(); 323 | } 324 | 325 | @Override 326 | public void onCreate(Bundle savedInstanceState) { 327 | super.onCreate(savedInstanceState); 328 | 329 | credentials = getArguments().getParcelable(CREDENTIALS); 330 | currentDir = getArguments().getString(CURRENT_DIR); 331 | } 332 | 333 | @Override 334 | public Dialog onCreateDialog(Bundle savedInstanceState) { 335 | final View view = getActivity().getLayoutInflater().inflate(R.layout.make_folder_layout, null); 336 | ((EditText) view.findViewById(R.id.example_edit_text_in_dialog)).setHint(R.string.example_make_folder_hint); 337 | return new AlertDialog.Builder(getActivity()) 338 | .setView(view) 339 | .setTitle(R.string.example_make_folder_title) 340 | .setPositiveButton(R.string.example_make_folder_positive_button, new DialogInterface.OnClickListener() { 341 | @Override 342 | public void onClick (DialogInterface dialog, int which) { 343 | String name = ((EditText) view.findViewById(R.id.example_edit_text_in_dialog)).getText().toString(); 344 | dialog.dismiss(); 345 | MakeFolderFragment.newInstance(credentials, currentDir, name).show(getFragmentManager(), "makeFolder"); 346 | } 347 | }) 348 | .setNegativeButton(R.string.example_make_folder_negative_button, null) 349 | .create(); 350 | } 351 | } 352 | 353 | public static class DeleteItemDialogFragment extends ContextMenuDialogFragment { 354 | 355 | public static DeleteItemDialogFragment newInstance(Credentials credentials, ListItem listItem) { 356 | return newInstance(new DeleteItemDialogFragment(), credentials, listItem); 357 | } 358 | 359 | @Override 360 | public Dialog onCreateDialog(Bundle savedInstanceState) { 361 | return new AlertDialog.Builder(getActivity()) 362 | .setTitle(listItem.isDir() ? R.string.example_delete_folder_title : R.string.example_delete_file_title) 363 | .setMessage(listItem.getName()) 364 | .setPositiveButton(R.string.example_delete_item_positive_button, new DialogInterface.OnClickListener() { 365 | @Override 366 | public void onClick(DialogInterface dialog, int which) { 367 | dialog.dismiss(); 368 | DeleteItemFragment.newInstance(credentials, listItem.getPath(), listItem.getName(), 369 | listItem.isDir()).show(getFragmentManager(), "deleteItem"); 370 | } 371 | }) 372 | .setNegativeButton(R.string.example_delete_item_negative_button, null) 373 | .create(); 374 | } 375 | } 376 | 377 | public static class RenameMoveDialogFragment extends ContextMenuDialogFragment { 378 | 379 | public static RenameMoveDialogFragment newInstance(Credentials credentials, ListItem listItem) { 380 | return newInstance(new RenameMoveDialogFragment(), credentials, listItem); 381 | } 382 | 383 | @Override 384 | public Dialog onCreateDialog(Bundle savedInstanceState) { 385 | final View view = getActivity().getLayoutInflater().inflate(R.layout.make_folder_layout, null); 386 | ((EditText) view.findViewById(R.id.example_edit_text_in_dialog)).setHint(R.string.example_move_rename_item_hint); 387 | return new AlertDialog.Builder(getActivity()) 388 | .setView(view) 389 | .setTitle(R.string.example_move_rename_item_title) 390 | .setPositiveButton(R.string.example_move_rename_positive_button, new DialogInterface.OnClickListener() { 391 | @Override 392 | public void onClick (DialogInterface dialog, int which) { 393 | String name = ((EditText) view.findViewById(R.id.example_edit_text_in_dialog)).getText().toString(); 394 | dialog.dismiss(); 395 | RenameMoveItemFragment.newInstance(credentials, listItem.getPath(), 396 | name /* assume this is full path */ ).show(getFragmentManager(), "renameMoveItem"); 397 | } 398 | }) 399 | .setNegativeButton(R.string.example_move_rename_negative_button, null) 400 | .create(); 401 | } 402 | } 403 | 404 | public static class ShowPublicUrlDialogFragment extends ContextMenuDialogFragment { 405 | 406 | public static ShowPublicUrlDialogFragment newInstance(Credentials credentials, ListItem listItem) { 407 | return newInstance(new ShowPublicUrlDialogFragment(), credentials, listItem); 408 | } 409 | 410 | @Override 411 | public Dialog onCreateDialog(Bundle savedInstanceState) { 412 | return new AlertDialog.Builder(getActivity()) 413 | .setTitle(R.string.example_publish_show_url_title) 414 | .setMessage(listItem.getPublicUrl()) 415 | .setPositiveButton(R.string.example_publish_show_url_positive_button, new DialogInterface.OnClickListener() { 416 | @Override 417 | public void onClick (DialogInterface dialog, int which) { 418 | dialog.dismiss(); 419 | MakeItemPublicFragment.newInstance(credentials, listItem.getPath(), false).show(getFragmentManager(), "makeItemPublic"); 420 | } 421 | }) 422 | .setNegativeButton(R.string.example_publish_show_url_negative_button, null) 423 | .create(); 424 | } 425 | } 426 | } 427 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/ListExampleLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.content.Context; 12 | import android.os.Handler; 13 | import android.support.v4.content.AsyncTaskLoader; 14 | import android.util.Log; 15 | 16 | import com.yandex.disk.rest.ResourcesArgs; 17 | import com.yandex.disk.rest.ResourcesHandler; 18 | import com.yandex.disk.rest.RestClient; 19 | import com.yandex.disk.rest.exceptions.ServerException; 20 | import com.yandex.disk.rest.json.Resource; 21 | 22 | import java.io.IOException; 23 | import java.text.Collator; 24 | import java.util.ArrayList; 25 | import java.util.Collections; 26 | import java.util.Comparator; 27 | import java.util.List; 28 | 29 | public class ListExampleLoader extends AsyncTaskLoader> { 30 | 31 | private static String TAG = "ListExampleLoader"; 32 | 33 | private Credentials credentials; 34 | private String dir; 35 | private Handler handler; 36 | 37 | private List fileItemList; 38 | private Exception exception; 39 | private boolean hasCancelled; 40 | 41 | private static final int ITEMS_PER_REQUEST = 20; 42 | 43 | private static Collator collator = Collator.getInstance(); 44 | static { 45 | collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION); 46 | } 47 | private final Comparator FILE_ITEM_COMPARATOR = new Comparator() { 48 | @Override 49 | public int compare(ListItem f1, ListItem f2) { 50 | if (f1.isDir() && !f2.isDir()) { 51 | return -1; 52 | } else if (f2.isDir() && !f1.isDir()) { 53 | return 1; 54 | } else { 55 | return collator.compare(f1.getName(), f2.getName()); 56 | } 57 | } 58 | }; 59 | 60 | public ListExampleLoader(Context context, Credentials credentials, String dir) { 61 | super(context); 62 | handler = new Handler(); 63 | this.credentials = credentials; 64 | this.dir = dir; 65 | } 66 | 67 | @Override 68 | protected void onStartLoading() { 69 | forceLoad(); 70 | } 71 | 72 | @Override 73 | protected void onReset() { 74 | super.onReset(); 75 | hasCancelled = true; 76 | } 77 | 78 | @Override 79 | public List loadInBackground() { 80 | fileItemList = new ArrayList<>(); 81 | hasCancelled = false; 82 | int offset = 0; 83 | RestClient client = null; 84 | try { 85 | client = RestClientUtil.getInstance(credentials); 86 | int size; 87 | do { 88 | Resource resource = client.getResources(new ResourcesArgs.Builder() 89 | .setPath(dir) 90 | .setSort(ResourcesArgs.Sort.name) 91 | .setLimit(ITEMS_PER_REQUEST) 92 | .setOffset(offset) 93 | .setParsingHandler(new ResourcesHandler() { 94 | @Override 95 | public void handleItem(Resource item) { 96 | fileItemList.add(new ListItem(item)); 97 | } 98 | }) 99 | .build()); 100 | offset += ITEMS_PER_REQUEST; 101 | handler.post(new Runnable() { 102 | @Override 103 | public void run() { 104 | Collections.sort(fileItemList, FILE_ITEM_COMPARATOR); 105 | deliverResult(new ArrayList<>(fileItemList)); 106 | } 107 | }); 108 | size = resource.getResourceList().getItems().size(); 109 | } while (!hasCancelled && size >= ITEMS_PER_REQUEST); 110 | return fileItemList; 111 | } catch (IOException | ServerException ex) { 112 | Log.d(TAG, "loadInBackground", ex); 113 | exception = ex; 114 | } 115 | return fileItemList; 116 | } 117 | 118 | public Exception getException() { 119 | return exception; 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/ListItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.os.Parcel; 12 | import android.os.Parcelable; 13 | 14 | import com.yandex.disk.rest.json.Resource; 15 | 16 | public class ListItem implements Parcelable { 17 | 18 | private String name, path, etag, contentType, publicUrl, mediaType; 19 | private boolean dir; 20 | private long contentLength, lastUpdated; 21 | 22 | public ListItem(Resource resource) { 23 | this.name = resource.getName(); 24 | this.path = resource.getPath() != null ? resource.getPath().getPath() : null; // Must throw an exception in real life code 25 | this.etag = resource.getMd5(); 26 | this.contentType = resource.getMimeType(); 27 | this.publicUrl = resource.getPublicUrl(); 28 | this.mediaType = resource.getMediaType(); 29 | this.dir = resource.isDir(); 30 | this.contentLength = resource.getSize(); 31 | this.lastUpdated = resource.getModified() != null ? resource.getModified().getTime() : 0; 32 | } 33 | 34 | private ListItem(String path, String name, long contentLength, long lastUpdated, boolean dir, 35 | String etag, String contentType, String publicUrl, String mediaType) { 36 | this.path = path; 37 | this.name = name; 38 | this.contentLength = contentLength; 39 | this.lastUpdated = lastUpdated; 40 | this.dir = dir; 41 | this.etag = etag; 42 | this.contentType = contentType; 43 | this.publicUrl = publicUrl; 44 | this.mediaType = mediaType; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return "ListItem{" + 50 | "name='" + name + '\'' + 51 | ", path='" + path + '\'' + 52 | ", etag='" + etag + '\'' + 53 | ", contentType='" + contentType + '\'' + 54 | ", publicUrl='" + publicUrl + '\'' + 55 | ", mediaType='" + mediaType + '\'' + 56 | ", dir=" + dir + 57 | ", contentLength=" + contentLength + 58 | ", lastUpdated=" + lastUpdated + 59 | '}'; 60 | } 61 | 62 | @Override 63 | public int describeContents() { 64 | return 0; 65 | } 66 | 67 | @Override 68 | public void writeToParcel(Parcel parcel, int i) { 69 | parcel.writeString(path); 70 | parcel.writeString(name); 71 | parcel.writeLong(contentLength); 72 | parcel.writeLong(lastUpdated); 73 | parcel.writeByte((byte) (dir ? 1 : 0)); 74 | parcel.writeString(etag); 75 | parcel.writeString(contentType); 76 | parcel.writeString(publicUrl); 77 | parcel.writeString(mediaType); 78 | } 79 | 80 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 81 | 82 | public ListItem createFromParcel(Parcel parcel) { 83 | return new ListItem(parcel.readString(), parcel.readString(), parcel.readLong(), 84 | parcel.readLong(), parcel.readByte() > 0, parcel.readString(), 85 | parcel.readString(), parcel.readString(), parcel.readString()); 86 | } 87 | 88 | public ListItem[] newArray(int size) { 89 | return new ListItem[size]; 90 | } 91 | }; 92 | 93 | public String getPath() { 94 | return path; 95 | } 96 | 97 | public String getName() { 98 | return name; 99 | } 100 | 101 | public long getContentLength() { 102 | return contentLength; 103 | } 104 | 105 | /** 106 | * @return time in milliseconds 107 | */ 108 | public long getLastUpdated() { 109 | return lastUpdated; 110 | } 111 | 112 | public boolean isDir() { 113 | return dir; 114 | } 115 | 116 | public String getContentType() { 117 | return contentType; 118 | } 119 | 120 | public String getEtag() { 121 | return etag; 122 | } 123 | 124 | public String getPublicUrl() { 125 | return publicUrl; 126 | } 127 | 128 | public String getMediaType() { 129 | return mediaType; 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/MakeFolderFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.app.Dialog; 12 | import android.app.ProgressDialog; 13 | import android.content.Context; 14 | import android.content.DialogInterface; 15 | import android.os.AsyncTask; 16 | import android.os.Bundle; 17 | import android.support.v4.app.FragmentManager; 18 | import android.util.Log; 19 | import android.widget.Toast; 20 | 21 | import com.yandex.disk.rest.RestClient; 22 | import com.yandex.disk.rest.exceptions.ServerException; 23 | import com.yandex.disk.rest.exceptions.http.HttpCodeException; 24 | 25 | import java.io.IOException; 26 | 27 | public class MakeFolderFragment extends IODialogFragment { 28 | 29 | private static final String TAG = "MakeFolderFragment"; 30 | 31 | private static final String WORK_FRAGMENT_TAG = "MakeFolderFragment.Background"; 32 | 33 | private static final String MKDIR_PATH = "example.mkdir.path"; 34 | private static final String MKDIR_NAME = "example.mkdir.name"; 35 | 36 | private Credentials credentials; 37 | private String path, name; 38 | 39 | private MakeFolderRetainedFragment workFragment; 40 | 41 | public static MakeFolderFragment newInstance(Credentials credentials, String path, String name) { 42 | MakeFolderFragment fragment = new MakeFolderFragment(); 43 | 44 | Bundle args = new Bundle(); 45 | args.putParcelable(CREDENTIALS, credentials); 46 | args.putString(MKDIR_PATH, path); 47 | args.putString(MKDIR_NAME, name); 48 | fragment.setArguments(args); 49 | 50 | return fragment; 51 | } 52 | 53 | @Override 54 | public void onCreate(Bundle savedInstanceState) { 55 | super.onCreate(savedInstanceState); 56 | 57 | credentials = getArguments().getParcelable(CREDENTIALS); 58 | path = getArguments().getString(MKDIR_PATH); 59 | name = getArguments().getString(MKDIR_NAME); 60 | } 61 | 62 | @Override 63 | public void onActivityCreated (Bundle savedInstanceState) { 64 | super.onActivityCreated(savedInstanceState); 65 | 66 | FragmentManager fragmentManager = getFragmentManager(); 67 | workFragment = (MakeFolderRetainedFragment) fragmentManager.findFragmentByTag(WORK_FRAGMENT_TAG); 68 | if (workFragment == null || workFragment.getTargetFragment() == null) { 69 | workFragment = new MakeFolderRetainedFragment(); 70 | fragmentManager.beginTransaction().add(workFragment, WORK_FRAGMENT_TAG).commit(); 71 | workFragment.makeFolder(credentials, path+"/"+name); 72 | } 73 | workFragment.setTargetFragment(this, 0); 74 | } 75 | 76 | @Override 77 | public void onDetach() { 78 | super.onDetach(); 79 | 80 | if (workFragment != null) { 81 | workFragment.setTargetFragment(null, 0); 82 | } 83 | } 84 | 85 | @Override 86 | public Dialog onCreateDialog(Bundle savedInstanceState) { 87 | dialog = new ProgressDialog(getActivity()); 88 | dialog.setTitle(R.string.example_progress_mkfolder_title); 89 | dialog.setIndeterminate(true); 90 | dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 91 | dialog.setButton(ProgressDialog.BUTTON_NEUTRAL, getText(R.string.example_make_folder_negative_button), new DialogInterface.OnClickListener() { 92 | @Override 93 | public void onClick (DialogInterface dialog, int which) { 94 | dialog.dismiss(); 95 | onCancel(); 96 | } 97 | }); 98 | return dialog; 99 | } 100 | 101 | @Override 102 | public void onCancel(DialogInterface dialog) { 103 | super.onCancel(dialog); 104 | onCancel(); 105 | } 106 | 107 | private void onCancel() { 108 | } 109 | 110 | public void onComplete() { 111 | dialog.dismiss(); 112 | Toast.makeText(getActivity(), R.string.example_progress_mkfolder_done, Toast.LENGTH_LONG).show(); 113 | ((ExampleActivity) getActivity()).reloadContent(); 114 | } 115 | 116 | public static class MakeFolderRetainedFragment extends IODialogRetainedFragment { 117 | 118 | public void makeFolder(final Credentials credentials, final String path) { 119 | 120 | new AsyncTask() { 121 | 122 | @Override 123 | protected Void doInBackground(Void... params) { 124 | try { 125 | RestClient client = RestClientUtil.getInstance(credentials); 126 | client.makeFolder(path); 127 | } catch (HttpCodeException ex) { 128 | Log.d(TAG, "makeFolder", ex); 129 | sendException(ex.getResponse().getDescription()); 130 | } catch (IOException | ServerException ex) { 131 | Log.d(TAG, "makeFolder", ex); 132 | sendException(ex); 133 | } 134 | return null; 135 | } 136 | 137 | @Override 138 | protected void onPostExecute(Void aVoid) { 139 | MakeFolderFragment targetFragment = (MakeFolderFragment) getTargetFragment(); 140 | if (targetFragment != null) { 141 | targetFragment.onComplete(); 142 | } 143 | } 144 | }.execute(); 145 | } 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/MakeItemPublicFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.app.AlertDialog; 12 | import android.app.Dialog; 13 | import android.app.ProgressDialog; 14 | import android.content.Context; 15 | import android.content.DialogInterface; 16 | import android.os.AsyncTask; 17 | import android.os.Bundle; 18 | import android.support.v4.app.DialogFragment; 19 | import android.support.v4.app.FragmentManager; 20 | import android.util.Log; 21 | import android.widget.Toast; 22 | 23 | import com.yandex.disk.rest.RestClient; 24 | import com.yandex.disk.rest.exceptions.ServerException; 25 | import com.yandex.disk.rest.exceptions.http.HttpCodeException; 26 | 27 | import java.io.IOException; 28 | 29 | public class MakeItemPublicFragment extends IODialogFragment { 30 | 31 | private static final String TAG = "MakeFolderFragment"; 32 | 33 | private static final String WORK_FRAGMENT_TAG = "MakeFolderFragment.Background"; 34 | 35 | private static final String URL_TO_PUBLIC_PATH = "example.url.to.public.path"; 36 | private static final String MAKE_PUBLIC_OR_EXPIRE = "example.make.public.or.expire"; 37 | 38 | private Credentials credentials; 39 | private String path; 40 | private boolean makePublicOrExpire; 41 | 42 | private MakeItemPublicRetainedFragment workFragment; 43 | 44 | public static MakeItemPublicFragment newInstance(Credentials credentials, String path, boolean makePublicOrExpire) { 45 | MakeItemPublicFragment fragment = new MakeItemPublicFragment(); 46 | 47 | Bundle args = new Bundle(); 48 | args.putParcelable(CREDENTIALS, credentials); 49 | args.putString(URL_TO_PUBLIC_PATH, path); 50 | args.putBoolean(MAKE_PUBLIC_OR_EXPIRE, makePublicOrExpire); 51 | fragment.setArguments(args); 52 | 53 | return fragment; 54 | } 55 | 56 | @Override 57 | public void onCreate(Bundle savedInstanceState) { 58 | super.onCreate(savedInstanceState); 59 | 60 | credentials = getArguments().getParcelable(CREDENTIALS); 61 | path = getArguments().getString(URL_TO_PUBLIC_PATH); 62 | makePublicOrExpire = getArguments().getBoolean(MAKE_PUBLIC_OR_EXPIRE); 63 | } 64 | 65 | @Override 66 | public void onActivityCreated (Bundle savedInstanceState) { 67 | super.onActivityCreated(savedInstanceState); 68 | 69 | FragmentManager fragmentManager = getFragmentManager(); 70 | workFragment = (MakeItemPublicRetainedFragment) fragmentManager.findFragmentByTag(WORK_FRAGMENT_TAG); 71 | if (workFragment == null || workFragment.getTargetFragment() == null) { 72 | workFragment = new MakeItemPublicRetainedFragment(); 73 | fragmentManager.beginTransaction().add(workFragment, WORK_FRAGMENT_TAG).commit(); 74 | workFragment.changePublicState(credentials, path, makePublicOrExpire); 75 | } 76 | workFragment.setTargetFragment(this, 0); 77 | } 78 | 79 | @Override 80 | public void onDetach() { 81 | super.onDetach(); 82 | 83 | if (workFragment != null) { 84 | workFragment.setTargetFragment(null, 0); 85 | } 86 | } 87 | 88 | @Override 89 | public Dialog onCreateDialog(Bundle savedInstanceState) { 90 | dialog = new ProgressDialog(getActivity()); 91 | dialog.setTitle(R.string.example_progress_mkfolder_title); 92 | dialog.setIndeterminate(true); 93 | dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 94 | dialog.setButton(ProgressDialog.BUTTON_NEUTRAL, getText(R.string.example_make_folder_negative_button), new DialogInterface.OnClickListener() { 95 | @Override 96 | public void onClick (DialogInterface dialog, int which) { 97 | dialog.dismiss(); 98 | onCancel(); 99 | } 100 | }); 101 | return dialog; 102 | } 103 | 104 | @Override 105 | public void onCancel(DialogInterface dialog) { 106 | super.onCancel(dialog); 107 | onCancel(); 108 | } 109 | 110 | private void onCancel() { 111 | // workFragment.cancelDownload(); 112 | } 113 | 114 | public void onComplete(String url) { 115 | dialog.dismiss(); 116 | if (url != null) { 117 | ShowNewPublicUrlDialogFragment.newInstance(url).show(getFragmentManager(), "showNewPublicUrlDialog"); 118 | } else { 119 | Toast.makeText(getActivity(), R.string.example_publish_show_url_revoked, Toast.LENGTH_LONG).show(); 120 | } 121 | ((ExampleActivity) getActivity()).reloadContent(); 122 | } 123 | 124 | public static class MakeItemPublicRetainedFragment extends IODialogRetainedFragment { 125 | 126 | public void changePublicState(final Credentials credentials, final String path, final boolean makePublicOrExpire) { 127 | 128 | new AsyncTask() { 129 | 130 | @Override 131 | protected String doInBackground(Void... params) { 132 | try { 133 | RestClient client = RestClientUtil.getInstance(credentials); 134 | if (makePublicOrExpire) { 135 | return client.publish(path).getHref(); 136 | } else { 137 | client.unpublish(path); 138 | } 139 | } catch (HttpCodeException ex) { 140 | Log.d(TAG, "makePublicOrExpire", ex); 141 | sendException(ex.getResponse().getDescription()); 142 | } catch (IOException | ServerException ex) { 143 | Log.d(TAG, "makePublicOrExpire", ex); 144 | sendException(ex); 145 | } 146 | return null; 147 | } 148 | 149 | @Override 150 | protected void onPostExecute(String url) { 151 | MakeItemPublicFragment targetFragment = (MakeItemPublicFragment) getTargetFragment(); 152 | if (targetFragment != null) { 153 | targetFragment.onComplete(url); 154 | } 155 | } 156 | }.execute(); 157 | } 158 | } 159 | 160 | public static class ShowNewPublicUrlDialogFragment extends DialogFragment { 161 | 162 | protected static final String NEW_PUBLIC_URL = "example.new.public.url"; 163 | 164 | protected String url; 165 | 166 | public static ShowNewPublicUrlDialogFragment newInstance(String url) { 167 | ShowNewPublicUrlDialogFragment fragment = new ShowNewPublicUrlDialogFragment(); 168 | 169 | Bundle args = new Bundle(); 170 | args.putString(NEW_PUBLIC_URL, url); 171 | fragment.setArguments(args); 172 | 173 | return fragment; 174 | } 175 | 176 | @Override 177 | public void onCreate(Bundle savedInstanceState) { 178 | super.onCreate(savedInstanceState); 179 | 180 | url = getArguments().getString(NEW_PUBLIC_URL); 181 | } 182 | 183 | @Override 184 | public Dialog onCreateDialog(Bundle savedInstanceState) { 185 | return new AlertDialog.Builder(getActivity()) 186 | .setTitle(R.string.example_publish_show_url_title) 187 | .setMessage(url) 188 | .setNegativeButton(R.string.example_publish_show_url_negative_button, null) 189 | .create(); 190 | } 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/RenameMoveItemFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.app.Dialog; 12 | import android.app.ProgressDialog; 13 | import android.content.Context; 14 | import android.content.DialogInterface; 15 | import android.os.AsyncTask; 16 | import android.os.Bundle; 17 | import android.support.v4.app.FragmentManager; 18 | import android.util.Log; 19 | import android.widget.Toast; 20 | 21 | import com.yandex.disk.rest.RestClient; 22 | import com.yandex.disk.rest.exceptions.ServerException; 23 | import com.yandex.disk.rest.exceptions.http.HttpCodeException; 24 | 25 | import java.io.IOException; 26 | 27 | public class RenameMoveItemFragment extends IODialogFragment { 28 | 29 | private static final String TAG = "RenameMoveItemFragment"; 30 | 31 | private static final String WORK_FRAGMENT_TAG = "RenameMoveItemFragment.Background"; 32 | 33 | private static final String MOVE_SRC_PATH = "example.move.src.path"; 34 | private static final String MOVE_DST_PATH = "example.move.dst.path"; 35 | 36 | private Credentials credentials; 37 | private String srcPath, dstPath; 38 | 39 | private RenameMoveItemRetainedFragment workFragment; 40 | 41 | public static RenameMoveItemFragment newInstance(Credentials credentials, String srcPath, String dstPath) { 42 | RenameMoveItemFragment fragment = new RenameMoveItemFragment(); 43 | 44 | Bundle args = new Bundle(); 45 | args.putParcelable(CREDENTIALS, credentials); 46 | args.putString(MOVE_SRC_PATH, srcPath); 47 | args.putString(MOVE_DST_PATH, dstPath); 48 | fragment.setArguments(args); 49 | 50 | return fragment; 51 | } 52 | 53 | @Override 54 | public void onCreate(Bundle savedInstanceState) { 55 | super.onCreate(savedInstanceState); 56 | 57 | credentials = getArguments().getParcelable(CREDENTIALS); 58 | srcPath = getArguments().getString(MOVE_SRC_PATH); 59 | dstPath = getArguments().getString(MOVE_DST_PATH); 60 | } 61 | 62 | @Override 63 | public void onActivityCreated (Bundle savedInstanceState) { 64 | super.onActivityCreated(savedInstanceState); 65 | 66 | FragmentManager fragmentManager = getFragmentManager(); 67 | workFragment = (RenameMoveItemRetainedFragment) fragmentManager.findFragmentByTag(WORK_FRAGMENT_TAG); 68 | if (workFragment == null || workFragment.getTargetFragment() == null) { 69 | workFragment = new RenameMoveItemRetainedFragment(); 70 | fragmentManager.beginTransaction().add(workFragment, WORK_FRAGMENT_TAG).commit(); 71 | workFragment.renameMoveItem(credentials, srcPath, dstPath); 72 | } 73 | workFragment.setTargetFragment(this, 0); 74 | } 75 | 76 | @Override 77 | public void onDetach() { 78 | super.onDetach(); 79 | 80 | if (workFragment != null) { 81 | workFragment.setTargetFragment(null, 0); 82 | } 83 | } 84 | 85 | @Override 86 | public Dialog onCreateDialog(Bundle savedInstanceState) { 87 | dialog = new ProgressDialog(getActivity()); 88 | dialog.setTitle(R.string.example_move_rename_item_title); 89 | dialog.setMessage(dstPath); 90 | dialog.setIndeterminate(true); 91 | dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 92 | dialog.setButton(ProgressDialog.BUTTON_NEUTRAL, getText(R.string.example_make_folder_negative_button), new DialogInterface.OnClickListener() { 93 | @Override 94 | public void onClick (DialogInterface dialog, int which) { 95 | dialog.dismiss(); 96 | onCancel(); 97 | } 98 | }); 99 | return dialog; 100 | } 101 | 102 | @Override 103 | public void onCancel(DialogInterface dialog) { 104 | super.onCancel(dialog); 105 | onCancel(); 106 | } 107 | 108 | private void onCancel() { 109 | } 110 | 111 | public void onComplete() { 112 | dialog.dismiss(); 113 | Toast.makeText(getActivity(), R.string.example_move_rename_item_done, Toast.LENGTH_LONG).show(); 114 | ((ExampleActivity) getActivity()).reloadContent(); 115 | } 116 | 117 | public static class RenameMoveItemRetainedFragment extends IODialogRetainedFragment { 118 | 119 | public void renameMoveItem(final Credentials credentials, final String srcPath, final String dstPath) { 120 | 121 | new AsyncTask() { 122 | 123 | @Override 124 | protected Void doInBackground(Void... params) { 125 | RestClient client = null; 126 | try { 127 | client = RestClientUtil.getInstance(credentials); 128 | client.move(srcPath, dstPath, false); 129 | } catch (HttpCodeException ex) { 130 | Log.d(TAG, "renameMoveItem", ex); 131 | sendException(ex.getResponse().getDescription()); 132 | } catch (IOException | ServerException ex) { 133 | Log.d(TAG, "renameMoveItem", ex); 134 | sendException(ex); 135 | } 136 | return null; 137 | } 138 | 139 | @Override 140 | protected void onPostExecute(Void aVoid) { 141 | RenameMoveItemFragment targetFragment = (RenameMoveItemFragment) getTargetFragment(); 142 | if (targetFragment != null) { 143 | targetFragment.onComplete(); 144 | } 145 | } 146 | }.execute(); 147 | } 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/RestClientUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import com.facebook.stetho.okhttp.StethoInterceptor; 12 | import com.squareup.okhttp.OkHttpClient; 13 | import com.yandex.disk.rest.OkHttpClientFactory; 14 | import com.yandex.disk.rest.RestClient; 15 | 16 | public class RestClientUtil { 17 | 18 | public static RestClient getInstance(final Credentials credentials) { 19 | OkHttpClient client = OkHttpClientFactory.makeClient(); 20 | client.networkInterceptors().add(new StethoInterceptor()); 21 | return new RestClient(credentials, client); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/yandex/disk/rest/example/UploadFileFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.example; 10 | 11 | import android.app.Dialog; 12 | import android.app.ProgressDialog; 13 | import android.content.Context; 14 | import android.content.DialogInterface; 15 | import android.os.Bundle; 16 | import android.support.v4.app.FragmentManager; 17 | import android.util.Log; 18 | import android.widget.Toast; 19 | 20 | import com.yandex.disk.rest.ProgressListener; 21 | import com.yandex.disk.rest.RestClient; 22 | import com.yandex.disk.rest.exceptions.CancelledUploadingException; 23 | import com.yandex.disk.rest.exceptions.ServerException; 24 | import com.yandex.disk.rest.exceptions.http.HttpCodeException; 25 | import com.yandex.disk.rest.json.Link; 26 | 27 | import java.io.File; 28 | import java.io.IOException; 29 | 30 | public class UploadFileFragment extends IODialogFragment { 31 | 32 | private static final String TAG = "LoadFileFragment"; 33 | 34 | private static final String WORK_FRAGMENT_TAG = "UploadFileFragment.Background"; 35 | 36 | private static final String CREDENTIALS = "example.credentials"; 37 | private static final String SERVER_PATH = "example.server.path"; 38 | private static final String LOCAL_FILE = "example.local.file"; 39 | 40 | private static final int PROGRESS_DIV = 1024 * 1024; 41 | 42 | private Credentials credentials; 43 | private String serverPath, localFile; 44 | 45 | private UploadFileRetainedFragment workFragment; 46 | 47 | public static UploadFileFragment newInstance(Credentials credentials, String serverPath, String localFile) { 48 | UploadFileFragment fragment = new UploadFileFragment(); 49 | 50 | Bundle args = new Bundle(); 51 | args.putParcelable(CREDENTIALS, credentials); 52 | args.putString(SERVER_PATH, serverPath); 53 | args.putString(LOCAL_FILE, localFile); 54 | fragment.setArguments(args); 55 | 56 | return fragment; 57 | } 58 | 59 | @Override 60 | public void onCreate(Bundle savedInstanceState) { 61 | super.onCreate(savedInstanceState); 62 | 63 | credentials = getArguments().getParcelable(CREDENTIALS); 64 | serverPath = getArguments().getString(SERVER_PATH); 65 | localFile = getArguments().getString(LOCAL_FILE); 66 | } 67 | 68 | @Override 69 | public void onActivityCreated (Bundle savedInstanceState) { 70 | super.onActivityCreated(savedInstanceState); 71 | 72 | FragmentManager fragmentManager = getFragmentManager(); 73 | workFragment = (UploadFileRetainedFragment) fragmentManager.findFragmentByTag(WORK_FRAGMENT_TAG); 74 | if (workFragment == null || workFragment.getTargetFragment() == null) { 75 | workFragment = new UploadFileRetainedFragment(); 76 | fragmentManager.beginTransaction().add(workFragment, WORK_FRAGMENT_TAG).commit(); 77 | workFragment.uploadFile(credentials, serverPath, localFile); 78 | } 79 | workFragment.setTargetFragment(this, 0); 80 | } 81 | 82 | @Override 83 | public void onDetach() { 84 | super.onDetach(); 85 | 86 | if (workFragment != null) { 87 | workFragment.setTargetFragment(null, 0); 88 | } 89 | } 90 | 91 | @Override 92 | public Dialog onCreateDialog(Bundle savedInstanceState) { 93 | dialog = new ProgressDialog(getActivity()); 94 | dialog.setTitle(R.string.example_uploading_file_title); 95 | dialog.setMessage(localFile); 96 | dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 97 | dialog.setIndeterminate(true); 98 | dialog.setButton(ProgressDialog.BUTTON_NEUTRAL, getString(R.string.example_loading_file_cancel_button), new DialogInterface.OnClickListener() { 99 | @Override 100 | public void onClick (DialogInterface dialog, int which) { 101 | dialog.dismiss(); 102 | onCancel(); 103 | } 104 | }); 105 | dialog.setOnCancelListener(this); 106 | dialog.show(); 107 | return dialog; 108 | } 109 | 110 | @Override 111 | public void onCancel(DialogInterface dialog) { 112 | super.onCancel(dialog); 113 | onCancel(); 114 | } 115 | 116 | private void onCancel() { 117 | workFragment.cancelUpload(); 118 | } 119 | 120 | public void onUploadComplete() { 121 | dialog.dismiss(); 122 | Toast.makeText(getActivity(), R.string.example_file_uploaded_toast, Toast.LENGTH_LONG).show(); 123 | } 124 | 125 | public void setDownloadProgress(long loaded, long total) { 126 | if (dialog != null) { 127 | if (dialog.isIndeterminate()) { 128 | dialog.setIndeterminate(false); 129 | } 130 | if (total > Integer.MAX_VALUE) { 131 | dialog.setProgress((int)(loaded / PROGRESS_DIV)); 132 | dialog.setMax((int)(total / PROGRESS_DIV)); 133 | } else { 134 | dialog.setProgress((int)loaded); 135 | dialog.setMax((int)total); 136 | } 137 | } 138 | } 139 | 140 | public static class UploadFileRetainedFragment extends IODialogRetainedFragment implements ProgressListener { 141 | 142 | private boolean cancelled; 143 | 144 | public void uploadFile(final Credentials credentials, final String serverPath, final String localFile) { 145 | 146 | new Thread(new Runnable() { 147 | @Override 148 | public void run () { 149 | try { 150 | RestClient client = RestClientUtil.getInstance(credentials); 151 | Link link = client.getUploadLink(serverPath, true); 152 | client.uploadFile(link, true, new File(localFile), UploadFileRetainedFragment.this); 153 | uploadComplete(); 154 | } catch (CancelledUploadingException ex) { 155 | // cancelled by user 156 | } catch (HttpCodeException ex) { 157 | Log.d(TAG, "uploadFile", ex); 158 | sendException(ex.getResponse().getDescription()); 159 | } catch (IOException | ServerException ex) { 160 | Log.d(TAG, "uploadFile", ex); 161 | sendException(ex); 162 | } 163 | } 164 | }).start(); 165 | } 166 | 167 | @Override 168 | public void updateProgress (final long loaded, final long total) { 169 | handler.post(new Runnable() { 170 | @Override 171 | public void run () { 172 | UploadFileFragment targetFragment = (UploadFileFragment) getTargetFragment(); 173 | if (targetFragment != null) { 174 | targetFragment.setDownloadProgress(loaded, total); 175 | } 176 | } 177 | }); 178 | } 179 | 180 | @Override 181 | public boolean hasCancelled () { 182 | return cancelled; 183 | } 184 | 185 | public void uploadComplete() { 186 | handler.post(new Runnable() { 187 | @Override 188 | public void run () { 189 | UploadFileFragment targetFragment = (UploadFileFragment) getTargetFragment(); 190 | if (targetFragment != null) { 191 | targetFragment.onUploadComplete(); 192 | } 193 | } 194 | }); 195 | } 196 | 197 | public void cancelUpload() { 198 | cancelled = true; 199 | } 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/add_folder_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-hdpi/add_folder_icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/add_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-hdpi/add_icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-ldpi/add_folder_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-ldpi/add_folder_icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-ldpi/add_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-ldpi/add_icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-ldpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-ldpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/add_folder_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-mdpi/add_folder_icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/add_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-mdpi/add_icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/add_folder_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-xhdpi/add_folder_icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/add_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-xhdpi/add_icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/app/src/main/res/drawable/icon.png -------------------------------------------------------------------------------- /app/src/main/res/layout/make_folder_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/menu/example_action_bar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 11 | 12 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/menu/example_context_menu.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 12 | 13 | 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | sdk-example 4 | No files 5 | Auth 6 | Start browser to authorize? 7 | OK 8 | Exit 9 | Loading file 10 | Cancel 11 | Open file 12 | Select file to upload 13 | Add file 14 | Create folder 15 | Unsupported scheme 16 | Uploading file 17 | File uploaded 18 | Create folder 19 | Create 20 | Cancel 21 | Folder name 22 | Creating folder 23 | Folder created 24 | Delete 25 | Rename / Move 26 | Publish / unpublish 27 | Delete file 28 | Delete folder 29 | Delete 30 | Cancel 31 | File deleted 32 | Folder deleted 33 | New name or path to move 34 | Rename / Move 35 | Rename or move completed 36 | Rename / move 37 | Cancel 38 | Public url 39 | Revoke 40 | Close 41 | Public url has been revoked 42 | 43 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'com.android.tools.build:gradle:1.0.0' 7 | } 8 | } 9 | 10 | allprojects { 11 | repositories { 12 | mavenCentral() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /disk-restapi-sdk/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /disk-restapi-sdk/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'maven' 3 | apply plugin: 'signing' 4 | 5 | buildscript { 6 | repositories { 7 | mavenCentral() 8 | } 9 | } 10 | 11 | repositories { 12 | mavenCentral() 13 | } 14 | 15 | sourceCompatibility = 1.7 16 | targetCompatibility = 1.7 17 | 18 | dependencies { 19 | compile 'com.squareup.retrofit:retrofit:1.9.0' 20 | compile 'com.google.code.gson:gson:2.3.1' 21 | compile 'com.squareup.okhttp:okhttp:2.3.0' 22 | compile 'com.squareup.okio:okio:1.6.0' 23 | compile 'org.slf4j:slf4j-api:1.7.10' 24 | 25 | testCompile 'org.slf4j:slf4j-simple:1.7.10' 26 | testCompile 'junit:junit:4.11' 27 | testCompile 'org.hamcrest:hamcrest-all:1.3' 28 | testCompile 'com.google.guava:guava:18.0' 29 | } 30 | 31 | group = 'com.yandex.android' 32 | version = '1.03' 33 | 34 | ext.getBooleanWithDefault = { String propertyName, boolean defaultValue -> 35 | boolean result = defaultValue 36 | if (project.hasProperty(propertyName)) { 37 | result = (project[propertyName] == 'true') 38 | } 39 | return result 40 | } 41 | 42 | def isMavenCentralBuild = getBooleanWithDefault('mavenCentral', false) 43 | def isTeamcityBuild = project.properties.containsKey('teamcity') 44 | 45 | if (isMavenCentralBuild) { 46 | 47 | if (isTeamcityBuild) { 48 | signing { 49 | project.ext["signing.keyId"] = teamcity['signing.keyId'] 50 | project.ext["signing.password"] = teamcity['signing.password'] 51 | project.ext["signing.secretKeyRingFile"] = teamcity['signing.secretKeyRingFile'] 52 | 53 | sign configurations.archives 54 | } 55 | } else { 56 | task signArchives { 57 | // do nothing 58 | } 59 | } 60 | 61 | uploadArchives { 62 | repositories { 63 | mavenDeployer { 64 | pom.project { 65 | 66 | parent { 67 | groupId 'org.sonatype.oss' 68 | artifactId 'oss-parent' 69 | version '7' 70 | } 71 | 72 | modelVersion '4.0.0' 73 | name 'Yandex.Disk Java SDK for REST API' 74 | description 'The Java SDK for Yandex.Disk REST API is intended for applications that work with the files of Yandex.Disk users or store their own files and settings on Yandex.Disk.' 75 | url 'http://disk.yandex.com/' 76 | 77 | licenses { 78 | license { 79 | name 'Terms of Use of Yandex.Disk service' 80 | url 'http://legal.yandex.com/disk_termsofuse/' 81 | distribution 'repo' 82 | } 83 | } 84 | 85 | scm { 86 | connection 'scm:git://github.com/yandex-disk/yandex-disk-restapi-java.git' 87 | developerConnection 'scm:git:git@github.com/yandex-disk/yandex-disk-restapi-java.git' 88 | url 'https://github.com/yandex-disk/yandex-disk-restapi-java.git' 89 | } 90 | 91 | developers { 92 | developer { 93 | name 'Yandex' 94 | url 'http://api.yandex.com/disk/' 95 | roles { 96 | role 'Developer' 97 | role 'Contributor' 98 | } 99 | } 100 | } 101 | } 102 | 103 | if (isTeamcityBuild) { 104 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 105 | 106 | ext.auth = { 107 | authentication(userName: teamcity['maven.username'], password: teamcity['maven.password']) 108 | } 109 | 110 | repository(url: teamcity['maven.repository'], auth) 111 | snapshotRepository(url: teamcity['maven.snapshotRepository'], auth) 112 | } else { 113 | def userHome = System.properties['user.home'] 114 | repository(url: "file:///$userHome/.m2/repository/") 115 | } 116 | } 117 | } 118 | } 119 | 120 | } else { 121 | 122 | uploadArchives { 123 | repositories { 124 | if (isTeamcityBuild) { 125 | mavenDeployer { 126 | ext.auth = { 127 | authentication(userName: teamcity['maven.username'], password: teamcity['maven.password']) 128 | } 129 | repository(url: 'http://artifactory.yandex.net/artifactory/yandex_mobile_releases/', auth) 130 | snapshotRepository(url: 'http://artifactory.yandex.net/artifactory/yandex_mobile_snapshots/', auth) 131 | } 132 | } else { 133 | mavenDeployer { 134 | def userHome = System.properties['user.home'] 135 | repository(url: "file:///$userHome/.m2/repository/") 136 | } 137 | } 138 | } 139 | } 140 | 141 | } 142 | 143 | uploadArchives.doFirst { 144 | // Debug printing all artifacts 145 | configurations.archives.artifacts.each { 146 | println "-------" 147 | println "name: $it.name" 148 | println "file: $it.file" 149 | println "type: $it.type" 150 | println "extension: $it.extension" 151 | println "classifier: $it.classifier" 152 | } 153 | } 154 | 155 | task sourcesJar(type: Jar, dependsOn: classes) { 156 | classifier = 'sources' 157 | from sourceSets.main.allSource 158 | } 159 | 160 | task javadocJar(type: Jar, dependsOn: javadoc) { 161 | classifier = 'javadoc' 162 | from javadoc.destinationDir 163 | } 164 | 165 | artifacts { 166 | archives sourcesJar 167 | archives javadocJar 168 | } 169 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/Credentials.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | import java.util.ArrayList; 12 | import java.util.Collections; 13 | import java.util.List; 14 | 15 | public class Credentials { 16 | 17 | /* package */ static final String AUTHORIZATION_HEADER = "Authorization"; 18 | /* package */ static final String USER_AGENT_HEADER = "User-Agent"; 19 | private static final String USER_AGENT = "Cloud API Android Client Example/1.0"; 20 | 21 | protected String user, token; 22 | 23 | public Credentials(final String user, final String token) { 24 | this.user = user; 25 | this.token = token; 26 | } 27 | 28 | public String getUser() { 29 | return user; 30 | } 31 | 32 | public String getToken() { 33 | return token; 34 | } 35 | 36 | public List getHeaders() { 37 | List list = new ArrayList<>(); 38 | list.add(new CustomHeader(USER_AGENT_HEADER, USER_AGENT)); 39 | list.add(new CustomHeader(AUTHORIZATION_HEADER, "OAuth " + getToken())); 40 | return Collections.unmodifiableList(list); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/CustomHeader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | 10 | package com.yandex.disk.rest; 11 | 12 | public class CustomHeader { 13 | 14 | private final String name, value; 15 | 16 | /* package */ CustomHeader(String name, String value) { 17 | this.name = name; 18 | this.value = value; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public String getValue() { 26 | return value; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/DownloadListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | import com.yandex.disk.rest.exceptions.DownloadNoSpaceAvailableException; 12 | 13 | import java.io.IOException; 14 | import java.io.OutputStream; 15 | 16 | public abstract class DownloadListener implements ProgressListener { 17 | 18 | /** 19 | * Local file length for resuming download. 0 if local file not exist 20 | */ 21 | public long getLocalLength() { 22 | return 0; 23 | } 24 | 25 | /** 26 | * Used for If-None-Match or If-Range. MD5 or null if not applicable or not known 27 | * @see rfc 2616 28 | */ 29 | public String getETag() { 30 | return null; 31 | } 32 | 33 | /** 34 | * Start position after server response 35 | */ 36 | public void setStartPosition(long position) { 37 | } 38 | 39 | /** 40 | * Content length after server response. 0 if not known 41 | * @throws DownloadNoSpaceAvailableException if no local space for content 42 | */ 43 | public void setContentLength(long length) 44 | throws DownloadNoSpaceAvailableException { 45 | } 46 | 47 | public abstract OutputStream getOutputStream(boolean append) 48 | throws IOException; 49 | 50 | @Override 51 | public void updateProgress(long loaded, long total) { 52 | } 53 | 54 | @Override 55 | public boolean hasCancelled() { 56 | return false; 57 | } 58 | 59 | public void setETag(String etag) { 60 | } 61 | 62 | public void setContentType(String contentType) { 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/FileDownloadListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | import java.io.File; 12 | import java.io.FileNotFoundException; 13 | import java.io.FileOutputStream; 14 | import java.io.OutputStream; 15 | 16 | public class FileDownloadListener extends DownloadListener { 17 | 18 | private final File saveTo; 19 | private final ProgressListener progressListener; 20 | 21 | public FileDownloadListener(File saveTo, ProgressListener progressListener) { 22 | this.saveTo = saveTo; 23 | this.progressListener = progressListener; 24 | } 25 | 26 | @Override 27 | public OutputStream getOutputStream(boolean append) 28 | throws FileNotFoundException { 29 | return new FileOutputStream(saveTo, append); 30 | } 31 | 32 | @Override 33 | public long getLocalLength() { 34 | return saveTo.length(); 35 | } 36 | 37 | @Override 38 | public void updateProgress(long loaded, long total) { 39 | if (progressListener != null) { 40 | progressListener.updateProgress(loaded, total); 41 | } 42 | } 43 | 44 | @Override 45 | public boolean hasCancelled() { 46 | return progressListener != null && progressListener.hasCancelled(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/OkHttpClientFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | import com.squareup.okhttp.OkHttpClient; 12 | 13 | import java.util.concurrent.TimeUnit; 14 | 15 | public class OkHttpClientFactory { 16 | 17 | private static final int CONNECT_TIMEOUT_MILLIS = 30 * 1000; 18 | private static final int READ_TIMEOUT_MILLIS = 30 * 1000; 19 | private static final int WRITE_TIMEOUT_MILLIS = 30 * 1000; 20 | 21 | public static OkHttpClient makeClient() { 22 | OkHttpClient client = new OkHttpClient(); 23 | 24 | client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 25 | client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 26 | client.setWriteTimeout(WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 27 | 28 | client.setFollowSslRedirects(true); 29 | client.setFollowRedirects(true); 30 | 31 | return client; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/ProgressListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | public interface ProgressListener { 12 | void updateProgress(long loaded, long total); 13 | 14 | boolean hasCancelled(); 15 | } 16 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/QueryBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | import java.io.UnsupportedEncodingException; 12 | import java.net.URLEncoder; 13 | import java.util.LinkedHashMap; 14 | import java.util.Map; 15 | 16 | /* package */ class QueryBuilder { 17 | 18 | private static final String UTF8 = "UTF-8"; 19 | 20 | private final Map queryMap; 21 | private final String url; 22 | 23 | /* package */ QueryBuilder(String url) { 24 | this.url = url; 25 | this.queryMap = new LinkedHashMap<>(); 26 | } 27 | 28 | /* package */ String build() { 29 | StringBuilder sb = new StringBuilder(); 30 | for (Map.Entry entry : queryMap.entrySet()) { 31 | Object value = entry.getValue(); 32 | if (value != null) { 33 | if (sb.length() > 0) { 34 | sb.append("&"); 35 | } 36 | sb.append(encode(entry.getKey())) 37 | .append("=") 38 | .append(encode(value.toString())); 39 | } 40 | } 41 | return url + "?" + sb.toString(); 42 | } 43 | 44 | private static String encode(String key) { 45 | try { 46 | return URLEncoder.encode(key, UTF8); 47 | } catch (UnsupportedEncodingException e) { 48 | throw new UnsupportedOperationException(e); 49 | } 50 | } 51 | 52 | /* package */ QueryBuilder add(String key, String value) { 53 | queryMap.put(key, value); 54 | return this; 55 | } 56 | 57 | /* package */ QueryBuilder add(String key, Boolean value) { 58 | queryMap.put(key, value); 59 | return this; 60 | } 61 | 62 | /* package */ QueryBuilder add(String key, Integer value) { 63 | queryMap.put(key, value); 64 | return this; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/RequestBodyProgress.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | import com.squareup.okhttp.MediaType; 12 | import com.squareup.okhttp.RequestBody; 13 | import com.squareup.okhttp.internal.Util; 14 | import com.yandex.disk.rest.exceptions.CancelledUploadingException; 15 | 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | 19 | import java.io.File; 20 | import java.io.FileInputStream; 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | 24 | import okio.Buffer; 25 | import okio.BufferedSink; 26 | import okio.Okio; 27 | import okio.Source; 28 | 29 | /* package */ abstract class RequestBodyProgress { 30 | 31 | private static final Logger logger = LoggerFactory.getLogger(RequestBodyProgress.class); 32 | 33 | private static final int SIZE = 2048; 34 | 35 | /** 36 | * Returns a new request body that transmits the content of {@code file}. 37 | *
38 | * Based on {@link RequestBody#create(com.squareup.okhttp.MediaType, java.io.File)} 39 | * 40 | * @see RequestBody#create(com.squareup.okhttp.MediaType, java.io.File) 41 | */ 42 | /* package */ static RequestBody create(final MediaType contentType, final File file, final long startOffset, 43 | final ProgressListener listener) { 44 | if (file == null) { 45 | throw new NullPointerException("content == null"); 46 | } 47 | 48 | if (listener == null && startOffset == 0) { 49 | return RequestBody.create(contentType, file); 50 | } 51 | 52 | return new RequestBody() { 53 | 54 | private void updateProgress(long loaded) 55 | throws CancelledUploadingException { 56 | if (listener != null) { 57 | if (listener.hasCancelled()) { 58 | throw new CancelledUploadingException(); 59 | } 60 | listener.updateProgress(loaded + startOffset, file.length()); 61 | } 62 | } 63 | 64 | @Override 65 | public MediaType contentType() { 66 | return contentType; 67 | } 68 | 69 | @Override 70 | public long contentLength() { 71 | return file.length() - startOffset; 72 | } 73 | 74 | @Override 75 | public void writeTo(BufferedSink sink) throws IOException { 76 | Source source = null; 77 | InputStream inputStream = new FileInputStream(file); 78 | try { 79 | if (startOffset > 0) { 80 | long skipped = inputStream.skip(startOffset); 81 | if (skipped != startOffset) { 82 | throw new IOException("RequestBodyProgress: inputStream.skip() failed"); 83 | } 84 | } 85 | long loaded = 0; 86 | updateProgress(loaded); 87 | source = Okio.source(inputStream); 88 | Buffer buffer = new Buffer(); 89 | for (long readCount; (readCount = source.read(buffer, SIZE)) != -1; ) { 90 | sink.write(buffer, readCount); 91 | loaded += readCount; 92 | updateProgress(loaded); 93 | } 94 | logger.debug("loaded: " + loaded); 95 | } finally { 96 | Util.closeQuietly(source); 97 | Util.closeQuietly(inputStream); 98 | } 99 | } 100 | }; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/ResourcesArgs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | import retrofit.mime.TypedOutput; 12 | 13 | public class ResourcesArgs { 14 | 15 | public enum Sort { 16 | name, path, created, modified, size, deleted 17 | } 18 | 19 | private String path, fields, sort, previewSize, publicKey, mediaType; 20 | private Integer limit, offset; 21 | private Boolean previewCrop; 22 | private ResourcesHandler parsingHandler; 23 | private TypedOutput body; 24 | 25 | private ResourcesArgs(String path, String fields, String sort, String previewSize, 26 | Integer limit, Integer offset, Boolean previewCrop, String publicKey, 27 | String mediaType, TypedOutput body, ResourcesHandler parsingHandler) { 28 | this.path = path; 29 | this.fields = fields; 30 | this.sort = sort; 31 | this.previewSize = previewSize; 32 | this.limit = limit; 33 | this.offset = offset; 34 | this.previewCrop = previewCrop; 35 | this.publicKey = publicKey; 36 | this.mediaType = mediaType; 37 | this.body = body; 38 | this.parsingHandler = parsingHandler; 39 | } 40 | 41 | public String getPath() { 42 | return path; 43 | } 44 | 45 | public String getFields() { 46 | return fields; 47 | } 48 | 49 | public String getSort() { 50 | return sort; 51 | } 52 | 53 | public String getPreviewSize() { 54 | return previewSize; 55 | } 56 | 57 | public String getPublicKey() { 58 | return publicKey; 59 | } 60 | 61 | public String getMediaType() { 62 | return mediaType; 63 | } 64 | 65 | public Integer getLimit() { 66 | return limit; 67 | } 68 | 69 | public Integer getOffset() { 70 | return offset; 71 | } 72 | 73 | public Boolean getPreviewCrop() { 74 | return previewCrop; 75 | } 76 | 77 | public TypedOutput getBody() { 78 | return body; 79 | } 80 | 81 | public ResourcesHandler getParsingHandler() { 82 | return parsingHandler; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "ResourcesArgs{" + 88 | "path='" + path + '\'' + 89 | ", fields='" + fields + '\'' + 90 | ", limit=" + limit + 91 | ", offset=" + offset + 92 | ", sort='" + sort + '\'' + 93 | ", previewSize='" + previewSize + '\'' + 94 | ", previewCrop=" + previewCrop + 95 | ", publicKey=" + publicKey + 96 | ", mediaType=" + mediaType + 97 | ", body=" + body + 98 | ", parsingHandler=" + (parsingHandler != null) + 99 | '}'; 100 | } 101 | 102 | public static class Builder { 103 | private String path, fields, sort, previewSize, publicKey, mediaType; 104 | private Integer limit, offset; 105 | private Boolean previewCrop; 106 | private ResourcesHandler parsingHandler; 107 | private TypedOutput body; 108 | 109 | public ResourcesArgs build() { 110 | return new ResourcesArgs(path, fields, sort, previewSize, limit, offset, previewCrop, 111 | publicKey, mediaType, body, parsingHandler); 112 | } 113 | 114 | public Builder setPath(String path) { 115 | this.path = path; 116 | return this; 117 | } 118 | 119 | public Builder setFields(String fields) { 120 | this.fields = fields; 121 | return this; 122 | } 123 | 124 | public Builder setSort(String sort) { 125 | this.sort = sort; 126 | return this; 127 | } 128 | 129 | public Builder setSort(Sort sort) { 130 | this.sort = sort.name(); 131 | return this; 132 | } 133 | 134 | public Builder setPreviewSize(String previewSize) { 135 | this.previewSize = previewSize; 136 | return this; 137 | } 138 | 139 | public Builder setLimit(Integer limit) { 140 | this.limit = limit; 141 | return this; 142 | } 143 | 144 | public Builder setOffset(Integer offset) { 145 | this.offset = offset; 146 | return this; 147 | } 148 | 149 | public Builder setPreviewCrop(Boolean previewCrop) { 150 | this.previewCrop = previewCrop; 151 | return this; 152 | } 153 | 154 | public Builder setPublicKey(String publicKey) { 155 | this.publicKey = publicKey; 156 | return this; 157 | } 158 | 159 | public Builder setMediaType(String mediaType) { 160 | this.mediaType = mediaType; 161 | return this; 162 | } 163 | 164 | public Builder setParsingHandler(ResourcesHandler parsingHandler) { 165 | this.parsingHandler = parsingHandler; 166 | return this; 167 | } 168 | 169 | public Builder setBody(TypedOutput body) { 170 | this.body = body; 171 | return this; 172 | } 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/ResourcesHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | import com.yandex.disk.rest.json.Resource; 12 | 13 | public abstract class ResourcesHandler { 14 | 15 | public void handleSelf(Resource item) { 16 | } 17 | 18 | public void handleItem(Resource item) { 19 | } 20 | 21 | public void onFinished(int itemsOnPage) { 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/RestClientIO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | import com.google.gson.Gson; 12 | import com.squareup.okhttp.MediaType; 13 | import com.squareup.okhttp.OkHttpClient; 14 | import com.squareup.okhttp.Request; 15 | import com.squareup.okhttp.RequestBody; 16 | import com.squareup.okhttp.Response; 17 | import com.squareup.okhttp.ResponseBody; 18 | import com.yandex.disk.rest.exceptions.CancelledDownloadException; 19 | import com.yandex.disk.rest.exceptions.DownloadNoSpaceAvailableException; 20 | import com.yandex.disk.rest.exceptions.ServerIOException; 21 | import com.yandex.disk.rest.exceptions.http.ConflictException; 22 | import com.yandex.disk.rest.exceptions.http.FileNotModifiedException; 23 | import com.yandex.disk.rest.exceptions.http.FileTooBigException; 24 | import com.yandex.disk.rest.exceptions.http.HttpCodeException; 25 | import com.yandex.disk.rest.exceptions.http.InsufficientStorageException; 26 | import com.yandex.disk.rest.exceptions.http.NotFoundException; 27 | import com.yandex.disk.rest.exceptions.http.PreconditionFailedException; 28 | import com.yandex.disk.rest.exceptions.http.RangeNotSatisfiableException; 29 | import com.yandex.disk.rest.exceptions.http.ServiceUnavailableException; 30 | import com.yandex.disk.rest.json.Link; 31 | import com.yandex.disk.rest.json.Operation; 32 | import com.yandex.disk.rest.retrofit.ErrorHandlerImpl; 33 | import com.yandex.disk.rest.util.Hash; 34 | import org.slf4j.Logger; 35 | import org.slf4j.LoggerFactory; 36 | 37 | import java.io.File; 38 | import java.io.IOException; 39 | import java.io.InputStream; 40 | import java.io.OutputStream; 41 | import java.util.List; 42 | import java.util.regex.Matcher; 43 | import java.util.regex.Pattern; 44 | 45 | /* package */ class RestClientIO { 46 | 47 | private static final Logger logger = LoggerFactory.getLogger(RestClientIO.class); 48 | 49 | private static final String ETAG_HEADER = "Etag"; 50 | private static final String SHA256_HEADER = "Sha256"; 51 | private static final String SIZE_HEADER = "Size"; 52 | private static final String CONTENT_LENGTH_HEADER = "Content-Length"; 53 | private static final String CONTENT_RANGE_HEADER = "Content-Range"; 54 | 55 | private static final String METHOD_GET = "GET"; 56 | private static final String METHOD_DELETE = "DELETE"; 57 | private static final String METHOD_PUT = "PUT"; 58 | 59 | private static final Pattern CONTENT_RANGE_HEADER_PATTERN = Pattern.compile("bytes\\D+(\\d+)-\\d+/(\\d+)"); 60 | 61 | private OkHttpClient client; 62 | private List commonHeaders; 63 | 64 | /* package */ RestClientIO(OkHttpClient client, List commonHeaders) { 65 | this.client = client; 66 | this.commonHeaders = commonHeaders; 67 | } 68 | 69 | private Request.Builder buildRequest() { 70 | Request.Builder request = new Request.Builder(); 71 | for (CustomHeader header : commonHeaders) { 72 | request.addHeader(header.getName(), header.getValue()); 73 | } 74 | return request; 75 | } 76 | 77 | /* package */ void downloadUrl(final String url, final DownloadListener downloadListener) 78 | throws IOException, CancelledDownloadException, DownloadNoSpaceAvailableException, 79 | HttpCodeException { 80 | 81 | Request.Builder req = buildRequest() 82 | .url(url); 83 | 84 | long length = downloadListener.getLocalLength(); 85 | String ifTag = "If-None-Match"; 86 | if (length >= 0) { 87 | ifTag = "If-Range"; 88 | StringBuilder contentRange = new StringBuilder(); 89 | contentRange.append("bytes=").append(length).append("-"); 90 | logger.debug("Range: " + contentRange); 91 | req.addHeader("Range", contentRange.toString()); 92 | } 93 | 94 | String etag = downloadListener.getETag(); 95 | if (etag != null) { 96 | logger.debug(ifTag + ": " + etag); 97 | req.addHeader(ifTag, etag); 98 | } 99 | 100 | Request request = req.build(); 101 | Response response = client 102 | .newCall(request) 103 | .execute(); 104 | 105 | boolean partialContent = false; 106 | int code = response.code(); 107 | switch (code) { 108 | case 200: 109 | // OK 110 | break; 111 | case 206: 112 | partialContent = true; 113 | break; 114 | case 304: 115 | throw new FileNotModifiedException(code); 116 | case 404: 117 | throw new NotFoundException(code); 118 | case 416: 119 | throw new RangeNotSatisfiableException(code); 120 | default: 121 | throw new HttpCodeException(code); 122 | } 123 | 124 | ResponseBody responseBody = response.body(); 125 | long contentLength = responseBody.contentLength(); 126 | logger.debug("download: contentLength=" + contentLength); 127 | 128 | long loaded; 129 | if (partialContent) { 130 | ContentRangeResponse contentRangeResponse = parseContentRangeHeader(response.header("Content-Range")); 131 | logger.debug("download: contentRangeResponse=" + contentRangeResponse); 132 | if (contentRangeResponse != null) { 133 | loaded = contentRangeResponse.getStart(); 134 | contentLength = contentRangeResponse.getSize(); 135 | } else { 136 | loaded = length; 137 | } 138 | } else { 139 | loaded = 0; 140 | if (contentLength < 0) { 141 | contentLength = 0; 142 | } 143 | } 144 | 145 | OutputStream os = null; 146 | try { 147 | downloadListener.setStartPosition(loaded); 148 | MediaType contentTypeHeader = responseBody.contentType(); 149 | if (contentTypeHeader != null) { 150 | downloadListener.setContentType(contentTypeHeader.toString()); 151 | } 152 | downloadListener.setContentLength(contentLength); 153 | 154 | int count; 155 | InputStream content = responseBody.byteStream(); 156 | os = downloadListener.getOutputStream(partialContent); 157 | final byte[] downloadBuffer = new byte[1024]; 158 | while ((count = content.read(downloadBuffer)) != -1) { 159 | if (downloadListener.hasCancelled()) { 160 | logger.info("Downloading " + url + " canceled"); 161 | client.cancel(request.tag()); 162 | throw new CancelledDownloadException(); 163 | } 164 | os.write(downloadBuffer, 0, count); 165 | loaded += count; 166 | downloadListener.updateProgress(loaded, contentLength); 167 | } 168 | } catch (CancelledDownloadException ex) { 169 | throw ex; 170 | } catch (Exception e) { 171 | logger.warn(e.getMessage(), e); 172 | client.cancel(request.tag()); 173 | if (e instanceof IOException) { 174 | throw (IOException) e; 175 | } else if (e instanceof RuntimeException) { 176 | throw (RuntimeException) e; 177 | } else if (e instanceof DownloadNoSpaceAvailableException) { 178 | throw (DownloadNoSpaceAvailableException) e; 179 | } else { 180 | // never happen 181 | throw new RuntimeException(e); 182 | } 183 | } finally { 184 | try { 185 | if (os != null) { 186 | os.close(); 187 | } 188 | } catch (IOException ex) { 189 | // nothing 190 | } 191 | try { 192 | response.body().close(); 193 | } catch (IOException | NullPointerException ex) { 194 | logger.warn(ex.getMessage(), ex); 195 | } 196 | } 197 | } 198 | 199 | private ContentRangeResponse parseContentRangeHeader(String header) { 200 | if (header == null) { 201 | return null; 202 | } 203 | Matcher matcher = CONTENT_RANGE_HEADER_PATTERN.matcher(header); 204 | if (!matcher.matches()) { 205 | return null; 206 | } 207 | try { 208 | return new ContentRangeResponse(Long.parseLong(matcher.group(1)), Long.parseLong(matcher.group(2))); 209 | } catch (IllegalStateException ex) { 210 | logger.error("parseContentRangeHeader: " + header, ex); 211 | return null; 212 | } catch (NumberFormatException ex) { 213 | logger.error("parseContentRangeHeader: " + header, ex); 214 | return null; 215 | } 216 | } 217 | 218 | /* package */ void uploadFile(final String url, final File file, final long startOffset, 219 | final ProgressListener progressListener) 220 | throws IOException, HttpCodeException { 221 | logger.debug("uploadFile: put to url: "+url); 222 | MediaType mediaType = MediaType.parse("application/octet-stream"); 223 | RequestBody requestBody = RequestBodyProgress.create(mediaType, file, startOffset, 224 | progressListener); 225 | Request.Builder requestBuilder = buildRequest() 226 | .removeHeader(Credentials.AUTHORIZATION_HEADER) 227 | .url(url) 228 | .put(requestBody); 229 | if (startOffset > 0) { 230 | StringBuilder contentRange = new StringBuilder(); 231 | contentRange.append("bytes ").append(startOffset).append("-").append(file.length() - 1) 232 | .append("/").append(file.length()); 233 | logger.debug(CONTENT_RANGE_HEADER + ": " + contentRange); 234 | requestBuilder.addHeader(CONTENT_RANGE_HEADER, contentRange.toString()); 235 | } 236 | Request request = requestBuilder.build(); 237 | 238 | Response response = client 239 | .newCall(request) 240 | .execute(); 241 | 242 | String statusLine = response.message(); 243 | logger.debug("headUrl: " + statusLine + " for url " + url); 244 | 245 | int code = response.code(); 246 | 247 | ResponseBody responseBody = response.body(); 248 | responseBody.close(); 249 | 250 | switch (code) { 251 | case 201: 252 | case 202: 253 | logger.debug("uploadFile: file uploaded successfully: "+file); 254 | break; 255 | case 404: 256 | throw new NotFoundException(code, null); 257 | case 409: 258 | throw new ConflictException(code, null); 259 | case 412: 260 | throw new PreconditionFailedException(code, null); 261 | case 413: 262 | throw new FileTooBigException(code, null); 263 | case 503: 264 | throw new ServiceUnavailableException(code, null); 265 | case 507: 266 | throw new InsufficientStorageException(code, null); 267 | default: 268 | throw new HttpCodeException(code); 269 | } 270 | } 271 | 272 | /* package */ long getUploadedSize(String url, Hash hash) 273 | throws IOException { 274 | 275 | Request request = buildRequest() 276 | .removeHeader(Credentials.AUTHORIZATION_HEADER) 277 | .url(url) 278 | .head() 279 | .addHeader(ETAG_HEADER, hash.getMd5()) 280 | .addHeader(SHA256_HEADER, hash.getSha256()) 281 | .addHeader(SIZE_HEADER, String.valueOf(hash.getSize())) 282 | .build(); 283 | 284 | Response response = client 285 | .newCall(request) 286 | .execute(); 287 | 288 | int code = response.code(); 289 | ResponseBody responseBody = response.body(); 290 | responseBody.close(); 291 | switch (code) { 292 | case 200: 293 | return Long.valueOf(response.header(CONTENT_LENGTH_HEADER, "0")); 294 | default: 295 | return 0; 296 | } 297 | } 298 | 299 | /* package */ Operation getOperation(String url) 300 | throws IOException, HttpCodeException { 301 | Response response = call(METHOD_GET, url); 302 | int code = response.code(); 303 | if (!response.isSuccessful()) { 304 | throw new HttpCodeException(code); 305 | } 306 | return parseJson(response, Operation.class); 307 | } 308 | 309 | /* package */ Link delete(String url) 310 | throws IOException, ServerIOException { 311 | Response response = null; 312 | try { 313 | response = call(METHOD_DELETE, url); 314 | switch (response.code()) { 315 | case 202: 316 | Link result = parseJson(response, Link.class); 317 | result.setHttpStatus(Link.HttpStatus.inProgress); 318 | return result; 319 | case 204: 320 | close(response); 321 | return Link.DONE; 322 | default: 323 | throw ErrorHandlerImpl.createHttpCodeException(response.code(), 324 | response.body().byteStream()); 325 | } 326 | } finally { 327 | close(response); 328 | } 329 | } 330 | 331 | /* package */ Link put(String url) 332 | throws IOException, ServerIOException { 333 | Response response = null; 334 | try { 335 | response = call(METHOD_PUT, url); 336 | switch (response.code()) { 337 | case 201: 338 | Link done = parseJson(response, Link.class); 339 | done.setHttpStatus(Link.HttpStatus.done); 340 | return done; 341 | case 202: 342 | Link inProgress = parseJson(response, Link.class); 343 | inProgress.setHttpStatus(Link.HttpStatus.inProgress); 344 | return inProgress; 345 | default: 346 | throw ErrorHandlerImpl.createHttpCodeException(response.code(), 347 | response.body().byteStream()); 348 | } 349 | } finally { 350 | close(response); 351 | } 352 | } 353 | 354 | private void close(Response response) throws IOException { 355 | if (response == null) { 356 | return; 357 | } 358 | ResponseBody responseBody = response.body(); 359 | if (responseBody == null) { 360 | return; 361 | } 362 | 363 | responseBody.close(); 364 | } 365 | 366 | private Response call(String method, String url) 367 | throws IOException { 368 | Request request = buildRequest() 369 | .method(method, null) 370 | .url(url) 371 | .build(); 372 | return client.newCall(request) 373 | .execute(); 374 | } 375 | 376 | private T parseJson(Response response, Class classOfT) 377 | throws IOException { 378 | ResponseBody responseBody = null; 379 | try { 380 | responseBody = response.body(); 381 | Gson gson = new Gson(); 382 | return gson.fromJson(responseBody.charStream(), classOfT); 383 | } finally { 384 | if (responseBody != null) { 385 | responseBody.close(); 386 | } 387 | } 388 | } 389 | 390 | private static class ContentRangeResponse { 391 | 392 | private final long start, size; 393 | 394 | ContentRangeResponse(long start, long size) { 395 | this.start = start; 396 | this.size = size; 397 | } 398 | 399 | long getStart() { 400 | return start; 401 | } 402 | 403 | long getSize() { 404 | return size; 405 | } 406 | } 407 | } 408 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/CancelledDownloadException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions; 10 | 11 | public class CancelledDownloadException extends ServerException { 12 | } 13 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/CancelledUploadingException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions; 10 | 11 | import java.io.InterruptedIOException; 12 | 13 | public class CancelledUploadingException extends InterruptedIOException { 14 | } 15 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/DownloadNoSpaceAvailableException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions; 10 | 11 | public class DownloadNoSpaceAvailableException extends ServerException { 12 | 13 | private final String destinationFolder; 14 | private final long length; 15 | 16 | public DownloadNoSpaceAvailableException(String destinationFolder, long length) { 17 | super(); 18 | this.destinationFolder = destinationFolder; 19 | this.length = length; 20 | } 21 | 22 | public String getDestinationFolder() { 23 | return destinationFolder; 24 | } 25 | 26 | public long getLength() { 27 | return length; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/RetrofitConversionException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions; 10 | 11 | /** 12 | * {@link retrofit.RetrofitError.Kind#CONVERSION} 13 | */ 14 | public class RetrofitConversionException extends ServerIOException { 15 | public RetrofitConversionException(Throwable ex) { 16 | super(ex); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/ServerException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions; 10 | 11 | public abstract class ServerException extends Exception { 12 | 13 | public ServerException() { 14 | super(); 15 | } 16 | 17 | public ServerException(String detailMessage) { 18 | super(detailMessage); 19 | } 20 | 21 | public ServerException(String detailMessage, Throwable throwable) { 22 | super(detailMessage, throwable); 23 | } 24 | 25 | public ServerException(Throwable throwable) { 26 | super(throwable); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/ServerIOException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions; 10 | 11 | public class ServerIOException extends ServerException { 12 | 13 | public ServerIOException() { 14 | super(); 15 | } 16 | 17 | public ServerIOException(String message) { 18 | super(message); 19 | } 20 | 21 | public ServerIOException(Throwable e) { 22 | super(e); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/WrongMethodException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions; 10 | 11 | public class WrongMethodException extends ServerException { 12 | public WrongMethodException(String message) { 13 | super(message); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/BadGatewayException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class BadGatewayException extends HttpCodeException { 14 | public BadGatewayException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/BadRequestException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class BadRequestException extends HttpCodeException { 14 | public BadRequestException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/ConflictException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class ConflictException extends HttpCodeException { 14 | public ConflictException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/FileNotModifiedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | /** 12 | * 304 on GET with If-None-Match 13 | */ 14 | public class FileNotModifiedException extends HttpCodeException { 15 | public FileNotModifiedException(int code) { 16 | super(code); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/FileTooBigException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class FileTooBigException extends HttpCodeException { 14 | public FileTooBigException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/ForbiddenException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class ForbiddenException extends HttpCodeException { 14 | public ForbiddenException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/GoneException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class GoneException extends HttpCodeException { 14 | public GoneException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/HttpCodeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.exceptions.ServerIOException; 12 | import com.yandex.disk.rest.json.ApiError; 13 | 14 | /** 15 | * 4xx and 5xx http codes
16 | * {@link retrofit.RetrofitError.Kind#HTTP}
17 | *
18 | * Basic rules:
19 | * Replace Error in the error name from {@link ApiError#getError()} 20 | * to Exception to get new exception name
21 | * New exception must extends HttpCodeException 22 | */ 23 | public class HttpCodeException extends ServerIOException { 24 | 25 | protected final int code; 26 | protected final ApiError response; 27 | 28 | public HttpCodeException(int code, ApiError response) { 29 | super(); 30 | this.code = code; 31 | this.response = response; 32 | } 33 | 34 | public HttpCodeException(int code) { 35 | this(code, null); 36 | } 37 | 38 | public int getCode() { 39 | return code; 40 | } 41 | 42 | public ApiError getResponse() { 43 | return response; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return "HttpCodeException{" + 49 | "code=" + code + 50 | ", response=" + response + 51 | '}'; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/InsufficientStorageException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class InsufficientStorageException extends HttpCodeException { 14 | public InsufficientStorageException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/InternalServerException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class InternalServerException extends HttpCodeException { 14 | public InternalServerException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/LockedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class LockedException extends HttpCodeException { 14 | public LockedException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/MethodNotAllowedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class MethodNotAllowedException extends HttpCodeException { 14 | public MethodNotAllowedException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/NotAcceptableException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class NotAcceptableException extends HttpCodeException { 14 | public NotAcceptableException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/NotFoundException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class NotFoundException extends HttpCodeException { 14 | public NotFoundException(int code) { 15 | super(code); 16 | } 17 | 18 | public NotFoundException(int code, ApiError response) { 19 | super(code, response); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/NotImplementedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class NotImplementedException extends HttpCodeException { 14 | public NotImplementedException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/PreconditionFailedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class PreconditionFailedException extends HttpCodeException { 14 | public PreconditionFailedException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/RangeNotSatisfiableException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | public class RangeNotSatisfiableException extends HttpCodeException { 12 | 13 | public RangeNotSatisfiableException(int code) { 14 | super(code); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/ServiceUnavailableException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class ServiceUnavailableException extends HttpCodeException { 14 | public ServiceUnavailableException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/TooManyRequestsException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class TooManyRequestsException extends HttpCodeException { 14 | public TooManyRequestsException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/UnauthorizedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class UnauthorizedException extends HttpCodeException { 14 | public UnauthorizedException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/UnprocessableEntityException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class UnprocessableEntityException extends HttpCodeException { 14 | public UnprocessableEntityException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/exceptions/http/UnsupportedMediaTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.exceptions.http; 10 | 11 | import com.yandex.disk.rest.json.ApiError; 12 | 13 | public class UnsupportedMediaTypeException extends HttpCodeException { 14 | public UnsupportedMediaTypeException(int code, ApiError response) { 15 | super(code, response); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/json/ApiError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.json; 10 | 11 | import com.google.gson.annotations.SerializedName; 12 | 13 | /** 14 | * @see API reference 15 | */ 16 | public class ApiError { 17 | 18 | @SerializedName("description") 19 | String description; 20 | 21 | @SerializedName("error") 22 | String error; 23 | 24 | public String getDescription() { 25 | return description; 26 | } 27 | 28 | public String getError() { 29 | return error; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "ApiError{" + 35 | "description='" + description + '\'' + 36 | ", error='" + error + '\'' + 37 | '}'; 38 | } 39 | } -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/json/ApiVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.json; 10 | 11 | import com.google.gson.annotations.SerializedName; 12 | 13 | public class ApiVersion { 14 | 15 | @SerializedName("build") 16 | String build; 17 | 18 | @SerializedName("api_version") 19 | String apiVersion; 20 | 21 | public String getBuild() { 22 | return build; 23 | } 24 | 25 | public String getApiVersion() { 26 | return apiVersion; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "ApiVersion{" + 32 | "build='" + build + '\'' + 33 | ", apiVersion='" + apiVersion + '\'' + 34 | '}'; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/json/DiskInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.json; 10 | 11 | import com.google.gson.annotations.SerializedName; 12 | 13 | import java.util.Map; 14 | 15 | /** 16 | * @see

API reference english, 17 | * russian

18 | */ 19 | public class DiskInfo { 20 | 21 | @SerializedName("trash_size") 22 | long trashSize; 23 | 24 | @SerializedName("total_space") 25 | long totalSpace; 26 | 27 | @SerializedName("used_space") 28 | long usedSpace; 29 | 30 | @SerializedName("system_folders") 31 | Map systemFolders; 32 | 33 | public long getTrashSize() { 34 | return trashSize; 35 | } 36 | 37 | public long getTotalSpace() { 38 | return totalSpace; 39 | } 40 | 41 | public long getUsedSpace() { 42 | return usedSpace; 43 | } 44 | 45 | public Map getSystemFolders() { 46 | return systemFolders; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return "DiskCapacity{" + 52 | "trashSize=" + trashSize + 53 | ", totalSpace=" + totalSpace + 54 | ", usedSpace=" + usedSpace + 55 | ", systemFolders=" + systemFolders + 56 | '}'; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/json/Link.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.json; 10 | 11 | import com.google.gson.annotations.SerializedName; 12 | 13 | /** 14 | * @see

API reference english, 15 | * russian

16 | */ 17 | public class Link { 18 | 19 | public enum HttpStatus { 20 | done, inProgress, error 21 | } 22 | 23 | HttpStatus httpStatus; 24 | 25 | public final static Link DONE = new Link() { 26 | { 27 | httpStatus = HttpStatus.done; 28 | } 29 | }; 30 | 31 | @SerializedName("href") 32 | String href; 33 | 34 | @SerializedName("method") 35 | String method; 36 | 37 | @SerializedName("templated") 38 | boolean templated; 39 | 40 | public String getHref() { 41 | return href; 42 | } 43 | 44 | public String getMethod() { 45 | return method; 46 | } 47 | 48 | public boolean isTemplated() { 49 | return templated; 50 | } 51 | 52 | public HttpStatus getHttpStatus() { 53 | return httpStatus; 54 | } 55 | 56 | public void setHttpStatus(HttpStatus httpStatus) { 57 | this.httpStatus = httpStatus; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "Link{" + 63 | "href='" + href + '\'' + 64 | ", method='" + method + '\'' + 65 | ", templated=" + templated + 66 | ", httpStatus=" + httpStatus + 67 | '}'; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/json/Operation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.json; 10 | 11 | import com.google.gson.annotations.SerializedName; 12 | 13 | /** 14 | * @see

API reference english, 15 | * russian

16 | */ 17 | public class Operation { 18 | 19 | private static final String IN_PROGRESS = "in-progress"; 20 | private static final String SUCCESS = "success"; 21 | 22 | @SerializedName("status") 23 | String status; 24 | 25 | public String getStatus() { 26 | return status; 27 | } 28 | 29 | public boolean isInProgress() { 30 | return IN_PROGRESS.equals(status); 31 | } 32 | 33 | public boolean isSuccess() { 34 | return SUCCESS.equals(status); 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "Operation{" + 40 | "status='" + status + '\'' + 41 | '}'; 42 | } 43 | } -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/json/Resource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.json; 10 | 11 | import com.google.gson.annotations.SerializedName; 12 | import com.yandex.disk.rest.util.ISO8601; 13 | import com.yandex.disk.rest.util.ResourcePath; 14 | 15 | import java.util.Date; 16 | 17 | /** 18 | * @see

API reference english, 19 | * russian

20 | */ 21 | public class Resource { 22 | 23 | @SerializedName("public_key") 24 | String publicKey; 25 | 26 | @SerializedName("_embedded") 27 | ResourceList resourceList; 28 | 29 | @SerializedName("name") 30 | String name; 31 | 32 | @SerializedName("created") 33 | String created; 34 | 35 | @SerializedName("public_url") 36 | String publicUrl; 37 | 38 | @SerializedName("origin_path") 39 | String originPath; 40 | 41 | @SerializedName("modified") 42 | String modified; 43 | 44 | @SerializedName("deleted") 45 | String deleted; 46 | 47 | @SerializedName("path") 48 | String path; 49 | 50 | @SerializedName("md5") 51 | String md5; 52 | 53 | @SerializedName("type") 54 | String type; 55 | 56 | @SerializedName("mime_type") 57 | String mimeType; 58 | 59 | @SerializedName("media_type") 60 | String mediaType; 61 | 62 | @SerializedName("preview") 63 | String preview; 64 | 65 | @SerializedName("size") 66 | long size; 67 | 68 | @SerializedName("custom_properties") 69 | Object properties; 70 | 71 | public String getPublicKey() { 72 | return publicKey; 73 | } 74 | 75 | public ResourceList getResourceList() { 76 | return resourceList; 77 | } 78 | 79 | public String getName() { 80 | return name; 81 | } 82 | 83 | public Date getCreated() { 84 | return created != null ? ISO8601.parse(created) : null; 85 | } 86 | 87 | public String getPublicUrl() { 88 | return publicUrl; 89 | } 90 | 91 | public ResourcePath getOriginPath() { 92 | return originPath != null ? new ResourcePath(originPath) : null; 93 | } 94 | 95 | public Date getModified() { 96 | return modified != null ? ISO8601.parse(modified) : null; 97 | } 98 | 99 | public Date getDeleted() { 100 | return deleted != null ? ISO8601.parse(deleted) : null; 101 | } 102 | 103 | public ResourcePath getPath() { 104 | return path != null ? new ResourcePath(path) : null; 105 | } 106 | 107 | public String getMd5() { 108 | return md5; 109 | } 110 | 111 | public String getType() { 112 | return type; 113 | } 114 | 115 | public boolean isDir() { 116 | return "dir".equalsIgnoreCase(type); 117 | } 118 | 119 | public String getMimeType() { 120 | return mimeType; 121 | } 122 | 123 | public String getMediaType() { 124 | return mediaType; 125 | } 126 | 127 | public String getPreview() { 128 | return preview; 129 | } 130 | 131 | public long getSize() { 132 | return size; 133 | } 134 | 135 | public Object getProperties() { 136 | return properties; 137 | } 138 | 139 | @Override 140 | public String toString() { 141 | return "Resource{" + 142 | "publicKey='" + publicKey + '\'' + 143 | ", resourceList=" + resourceList + 144 | ", name='" + name + '\'' + 145 | ", created='" + getCreated() + '\'' + 146 | ", publicUrl='" + publicUrl + '\'' + 147 | ", originPath='" + getOriginPath() + '\'' + 148 | ", modified='" + getModified() + '\'' + 149 | ", deleted='" + getDeleted() + '\'' + 150 | ", path='" + getPath() + '\'' + 151 | ", md5='" + md5 + '\'' + 152 | ", type='" + type + '\'' + 153 | ", mimeType='" + mimeType + '\'' + 154 | ", mediaType='" + mediaType + '\'' + 155 | ", preview='" + preview + '\'' + 156 | ", size=" + size + 157 | ", properties=" + properties + 158 | '}'; 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/json/ResourceList.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.json; 10 | 11 | import com.google.gson.annotations.SerializedName; 12 | 13 | import java.util.List; 14 | 15 | /** 16 | * @see

API reference english, 17 | * russian

18 | */ 19 | public class ResourceList { 20 | 21 | @SerializedName("sort") 22 | String sort; 23 | 24 | @SerializedName("public_key") 25 | String publicKey; 26 | 27 | @SerializedName("items") 28 | List items; 29 | 30 | @SerializedName("path") 31 | String path; 32 | 33 | @SerializedName("limit") 34 | int limit; 35 | 36 | @SerializedName("offset") 37 | int offset; 38 | 39 | @SerializedName("total") 40 | int total; 41 | 42 | public String getSort() { 43 | return sort; 44 | } 45 | 46 | public String getPublicKey() { 47 | return publicKey; 48 | } 49 | 50 | public List getItems() { 51 | return items; 52 | } 53 | 54 | public String getPath() { 55 | return path; 56 | } 57 | 58 | public int getLimit() { 59 | return limit; 60 | } 61 | 62 | public int getOffset() { 63 | return offset; 64 | } 65 | 66 | public int getTotal() { 67 | return total; 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return "ResourceList{" + 73 | "sort='" + sort + '\'' + 74 | ", publicKey='" + publicKey + '\'' + 75 | ", items=" + items + 76 | ", path='" + path + '\'' + 77 | ", limit=" + limit + 78 | ", offset=" + offset + 79 | ", total=" + total + 80 | '}'; 81 | } 82 | } -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/retrofit/CloudApi.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.retrofit; 10 | 11 | import com.yandex.disk.rest.exceptions.ServerIOException; 12 | import com.yandex.disk.rest.json.ApiVersion; 13 | import com.yandex.disk.rest.json.DiskInfo; 14 | import com.yandex.disk.rest.json.Link; 15 | import com.yandex.disk.rest.json.Operation; 16 | import com.yandex.disk.rest.json.Resource; 17 | import com.yandex.disk.rest.json.ResourceList; 18 | 19 | import retrofit.http.GET; 20 | import retrofit.http.Body; 21 | import retrofit.http.PATCH; 22 | import retrofit.http.POST; 23 | import retrofit.http.PUT; 24 | import retrofit.http.Path; 25 | import retrofit.http.Query; 26 | import retrofit.mime.TypedOutput; 27 | 28 | import java.io.IOException; 29 | 30 | public interface CloudApi { 31 | 32 | @GET("/") 33 | ApiVersion getApiVersion() 34 | throws IOException, ServerIOException; 35 | 36 | @GET("/v1/disk/operations/{operation_id}") 37 | Operation getOperation(@Path("operation_id") String operationId) 38 | throws IOException, ServerIOException; 39 | 40 | @GET("/v1/disk") 41 | DiskInfo getDiskInfo(@Query("fields") String fields) 42 | throws IOException, ServerIOException; 43 | 44 | @GET("/v1/disk/resources") 45 | Resource getResources(@Query("path") String path, @Query("fields") String fields, 46 | @Query("limit") Integer limit, @Query("offset") Integer offset, 47 | @Query("sort") String sort, @Query("preview_size") String previewSize, 48 | @Query("preview_crop") Boolean previewCrop) 49 | throws IOException, ServerIOException; 50 | 51 | @GET("/v1/disk/resources/files") 52 | ResourceList getFlatResourceList(@Query("limit") Integer limit, @Query("media_type") String mediaType, 53 | @Query("offset") Integer offset, @Query("fields") String fields, 54 | @Query("preview_size") String previewSize, 55 | @Query("preview_crop") Boolean previewCrop) 56 | throws IOException, ServerIOException; 57 | 58 | @GET("/v1/disk/resources/last-uploaded") 59 | ResourceList getLastUploadedResources(@Query("limit") Integer limit, @Query("media_type") String mediaType, 60 | @Query("offset") Integer offset, @Query("fields") String fields, 61 | @Query("preview_size") String previewSize, 62 | @Query("preview_crop") Boolean previewCrop) 63 | throws IOException, ServerIOException; 64 | 65 | @PATCH("/v1/disk/resources/") 66 | Resource patchResource(@Query("path") String path, @Query("fields") String fields, 67 | @Body TypedOutput body) 68 | throws IOException, ServerIOException; 69 | 70 | @GET("/v1/disk/resources/download") 71 | Link getDownloadLink(@Query("path") String path) 72 | throws IOException, ServerIOException; 73 | 74 | @POST("/v1/disk/resources/upload") 75 | Link saveFromUrl(@Query("url") String url, @Query("path") String path) 76 | throws IOException, ServerIOException; 77 | 78 | @GET("/v1/disk/resources/upload") 79 | Link getUploadLink(@Query("path") String path, @Query("overwrite") Boolean overwrite) 80 | throws IOException, ServerIOException; 81 | 82 | @POST("/v1/disk/resources/copy") 83 | Link copy(@Query("from") String from, @Query("path") String path, 84 | @Query("overwrite") Boolean overwrite) 85 | throws IOException, ServerIOException; 86 | 87 | @POST("/v1/disk/resources/move") 88 | Link move(@Query("from") String from, @Query("path") String path, 89 | @Query("overwrite") Boolean overwrite) 90 | throws IOException, ServerIOException; 91 | 92 | @PUT("/v1/disk/resources") 93 | Link makeFolder(@Query("path") String path) 94 | throws IOException, ServerIOException; 95 | 96 | @PUT("/v1/disk/resources/publish") 97 | Link publish(@Query("path") String path) 98 | throws IOException, ServerIOException; 99 | 100 | @PUT("/v1/disk/resources/unpublish") 101 | Link unpublish(@Query("path") String path) 102 | throws IOException, ServerIOException; 103 | 104 | @GET("/v1/disk/public/resources") 105 | Resource listPublicResources(@Query("public_key") String publicKey, @Query("path") String path, 106 | @Query("fields") String fields, @Query("limit") Integer limit, 107 | @Query("offset") Integer offset, @Query("sort") String sort, 108 | @Query("preview_size") String previewSize, 109 | @Query("preview_crop") Boolean previewCrop) 110 | throws IOException, ServerIOException; 111 | 112 | @GET("/v1/disk/public/resources/download") 113 | Link getPublicResourceDownloadLink(@Query("public_key") String publicKey, 114 | @Query("path") String path) 115 | throws IOException, ServerIOException; 116 | 117 | @POST("/v1/disk/public/resources/save-to-disk/") 118 | Link savePublicResource(@Query("public_key") String publicKey, @Query("path") String path, 119 | @Query("name") String name) 120 | throws IOException, ServerIOException; 121 | 122 | @GET("/v1/disk/trash/resources") 123 | Resource getTrashResources(@Query("path") String path, @Query("fields") String fields, 124 | @Query("limit") Integer limit, @Query("offset") Integer offset, 125 | @Query("sort") String sort, @Query("preview_size") String previewSize, 126 | @Query("preview_crop") Boolean previewCrop) 127 | throws IOException, ServerIOException; 128 | } 129 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/retrofit/ErrorHandlerImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.retrofit; 10 | 11 | import com.google.gson.Gson; 12 | import com.yandex.disk.rest.exceptions.RetrofitConversionException; 13 | import com.yandex.disk.rest.exceptions.ServerIOException; 14 | import com.yandex.disk.rest.exceptions.http.BadGatewayException; 15 | import com.yandex.disk.rest.exceptions.http.BadRequestException; 16 | import com.yandex.disk.rest.exceptions.http.ConflictException; 17 | import com.yandex.disk.rest.exceptions.http.FileTooBigException; 18 | import com.yandex.disk.rest.exceptions.http.ForbiddenException; 19 | import com.yandex.disk.rest.exceptions.http.GoneException; 20 | import com.yandex.disk.rest.exceptions.http.HttpCodeException; 21 | import com.yandex.disk.rest.exceptions.http.InsufficientStorageException; 22 | import com.yandex.disk.rest.exceptions.http.InternalServerException; 23 | import com.yandex.disk.rest.exceptions.http.LockedException; 24 | import com.yandex.disk.rest.exceptions.http.MethodNotAllowedException; 25 | import com.yandex.disk.rest.exceptions.http.NotAcceptableException; 26 | import com.yandex.disk.rest.exceptions.http.NotFoundException; 27 | import com.yandex.disk.rest.exceptions.http.NotImplementedException; 28 | import com.yandex.disk.rest.exceptions.http.PreconditionFailedException; 29 | import com.yandex.disk.rest.exceptions.http.ServiceUnavailableException; 30 | import com.yandex.disk.rest.exceptions.http.TooManyRequestsException; 31 | import com.yandex.disk.rest.exceptions.http.UnauthorizedException; 32 | import com.yandex.disk.rest.exceptions.http.UnprocessableEntityException; 33 | import com.yandex.disk.rest.exceptions.http.UnsupportedMediaTypeException; 34 | import com.yandex.disk.rest.json.ApiError; 35 | import org.slf4j.Logger; 36 | import org.slf4j.LoggerFactory; 37 | import retrofit.ErrorHandler; 38 | import retrofit.RetrofitError; 39 | import retrofit.client.Response; 40 | 41 | import java.io.IOException; 42 | import java.io.InputStream; 43 | import java.io.InputStreamReader; 44 | import java.io.Reader; 45 | 46 | public class ErrorHandlerImpl implements ErrorHandler { 47 | 48 | private static final Logger logger = LoggerFactory.getLogger(ErrorHandlerImpl.class); 49 | 50 | @Override 51 | public Throwable handleError(RetrofitError retrofitError) { 52 | RetrofitError.Kind kind = retrofitError.getKind(); 53 | switch (kind) { 54 | case NETWORK: 55 | Throwable th = retrofitError.getCause(); 56 | return th instanceof IOException ? th : new IOException(th); 57 | 58 | case CONVERSION: 59 | return new RetrofitConversionException(retrofitError.getCause()); 60 | 61 | case HTTP: 62 | try { 63 | Response response = retrofitError.getResponse(); 64 | int httpCode = response.getStatus(); 65 | return createHttpCodeException(httpCode, response.getBody().in()); 66 | } catch (IOException ex) { 67 | logger.debug("errorHandler", retrofitError); 68 | return ex; 69 | } 70 | 71 | case UNEXPECTED: 72 | return new ServerIOException(retrofitError.getCause()); 73 | 74 | default: 75 | return new ServerIOException("ErrorHandler: unhandled error " + kind.name()); 76 | } 77 | } 78 | 79 | public static HttpCodeException createHttpCodeException(int httpCode, InputStream in) { 80 | return createHttpCodeException(httpCode, readApiError(in)); 81 | } 82 | 83 | private static ApiError readApiError(InputStream in) { 84 | Reader reader = new InputStreamReader(in); 85 | return new Gson().fromJson(reader, ApiError.class); 86 | } 87 | 88 | private static HttpCodeException createHttpCodeException(int httpCode, ApiError apiError) { 89 | logger.debug("getStatus=" + httpCode); 90 | switch (httpCode) { 91 | case 400: 92 | return new BadRequestException(httpCode, apiError); 93 | case 401: 94 | return new UnauthorizedException(httpCode, apiError); 95 | case 403: 96 | return new ForbiddenException(httpCode, apiError); 97 | case 404: 98 | return new NotFoundException(httpCode, apiError); 99 | case 405: 100 | return new MethodNotAllowedException(httpCode, apiError); 101 | case 406: 102 | return new NotAcceptableException(httpCode, apiError); 103 | case 409: 104 | return new ConflictException(httpCode, apiError); 105 | case 410: 106 | return new GoneException(httpCode, apiError); 107 | case 412: 108 | return new PreconditionFailedException(httpCode, apiError); 109 | case 413: 110 | return new FileTooBigException(httpCode, apiError); 111 | case 415: 112 | return new UnsupportedMediaTypeException(httpCode, apiError); 113 | case 422: 114 | return new UnprocessableEntityException(httpCode, apiError); 115 | case 423: 116 | return new LockedException(httpCode, apiError); 117 | case 429: 118 | return new TooManyRequestsException(httpCode, apiError); 119 | case 500: 120 | return new InternalServerException(httpCode, apiError); 121 | case 501: 122 | return new NotImplementedException(httpCode, apiError); 123 | case 502: 124 | return new BadGatewayException(httpCode, apiError); 125 | case 503: 126 | return new ServiceUnavailableException(httpCode, apiError); 127 | case 507: 128 | return new InsufficientStorageException(httpCode, apiError); 129 | default: 130 | return new HttpCodeException(httpCode, apiError); 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/retrofit/RequestInterceptorImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.retrofit; 10 | 11 | import com.yandex.disk.rest.CustomHeader; 12 | 13 | import java.util.List; 14 | 15 | import retrofit.RequestInterceptor; 16 | 17 | public class RequestInterceptorImpl implements RequestInterceptor { 18 | 19 | private final List headers; 20 | 21 | public RequestInterceptorImpl(final List headers) { 22 | this.headers = headers; 23 | } 24 | 25 | @Override 26 | public void intercept(final RequestFacade request) { 27 | for (CustomHeader header : headers) { 28 | addHeader(request, header); 29 | } 30 | } 31 | 32 | private static void addHeader(final RequestFacade request, final CustomHeader header) { 33 | request.addHeader(header.getName(), header.getValue()); 34 | } 35 | } -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/util/Hash.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.util; 10 | 11 | import java.io.File; 12 | import java.io.FileInputStream; 13 | import java.io.IOException; 14 | import java.io.InputStream; 15 | import java.security.MessageDigest; 16 | import java.security.NoSuchAlgorithmException; 17 | 18 | public class Hash { 19 | 20 | private enum HashType { 21 | 22 | MD5("MD5"), SHA256("SHA-256"); 23 | 24 | private final String value; 25 | 26 | HashType(String value) { 27 | this.value = value; 28 | } 29 | 30 | String getValue() { 31 | return value; 32 | } 33 | } 34 | 35 | private final String md5, sha256; 36 | private final long size; 37 | 38 | public Hash(String md5, String sha256, long size) { 39 | this.md5 = md5; 40 | this.sha256 = sha256; 41 | this.size = size; 42 | } 43 | 44 | public static Hash getHash(File file) 45 | throws IOException { 46 | InputStream is = new FileInputStream(file); 47 | try { 48 | return getHash(is, file.length()); 49 | } finally { 50 | close(is); 51 | } 52 | } 53 | 54 | private static Hash getHash(InputStream is, long size) 55 | throws IOException { 56 | MessageDigest md5Digest, sha256Digest; 57 | try { 58 | md5Digest = MessageDigest.getInstance(HashType.MD5.getValue()); 59 | sha256Digest = MessageDigest.getInstance(HashType.SHA256.getValue()); 60 | } catch (NoSuchAlgorithmException ex) { 61 | throw new RuntimeException(ex); 62 | } 63 | byte[] buf = new byte[8192]; 64 | int count; 65 | while ((count = is.read(buf)) > 0) { 66 | md5Digest.update(buf, 0, count); 67 | sha256Digest.update(buf, 0, count); 68 | } 69 | return new Hash(toString(md5Digest.digest()), toString(sha256Digest.digest()), size); 70 | } 71 | 72 | private static void close(InputStream is) 73 | throws IOException { 74 | if (is != null) { 75 | is.close(); 76 | } 77 | } 78 | 79 | public String getMd5() { 80 | return md5; 81 | } 82 | 83 | public String getSha256() { 84 | return sha256; 85 | } 86 | 87 | public long getSize() { 88 | return size; 89 | } 90 | 91 | public static String toString(byte[] bytes) { 92 | if (bytes == null) { 93 | return null; 94 | } 95 | StringBuilder out = new StringBuilder(); 96 | for (byte b : bytes) { 97 | String n = Integer.toHexString(b & 0x000000FF); 98 | if (n.length() == 1) { 99 | out.append('0'); 100 | } 101 | out.append(n); 102 | } 103 | return out.toString(); 104 | } 105 | 106 | @Override 107 | public String toString() { 108 | return "Hash{" + 109 | "md5='" + md5 + '\'' + 110 | ", sha256='" + sha256 + '\'' + 111 | ", size=" + size + 112 | '}'; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/util/ISO8601.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.util; 10 | 11 | import java.text.ParseException; 12 | import java.text.SimpleDateFormat; 13 | import java.util.Calendar; 14 | import java.util.Date; 15 | import java.util.GregorianCalendar; 16 | 17 | public class ISO8601 { 18 | 19 | private static final String FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; 20 | 21 | public static String fromCalendar(final Calendar calendar) { 22 | Date date = calendar.getTime(); 23 | String formatted = new SimpleDateFormat(FORMAT).format(date); 24 | return formatted.substring(0, 22) + ":" + formatted.substring(22); 25 | } 26 | 27 | public static Calendar toCalendar(final String iso8601string) 28 | throws ParseException { 29 | Calendar calendar = GregorianCalendar.getInstance(); 30 | String s = iso8601string.replace("Z", "+00:00"); 31 | try { 32 | s = s.substring(0, 22) + s.substring(23); 33 | } catch (IndexOutOfBoundsException e) { 34 | throw new ParseException("Invalid length", 0); 35 | } 36 | Date date = new SimpleDateFormat(FORMAT).parse(s); 37 | calendar.setTime(date); 38 | return calendar; 39 | } 40 | 41 | public static Date parse(final String iso8601string) { 42 | try { 43 | return toCalendar(iso8601string).getTime(); 44 | } catch (ParseException e) { 45 | return null; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/main/java/com/yandex/disk/rest/util/ResourcePath.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.util; 10 | 11 | public class ResourcePath { 12 | 13 | private final static char SEPARATOR = ':'; 14 | 15 | private final String prefix, path; 16 | 17 | public ResourcePath(String str) { 18 | if (str == null) { 19 | throw new IllegalArgumentException(); 20 | } 21 | int index = str.indexOf(SEPARATOR); 22 | if (index == -1) { 23 | prefix = null; 24 | path = str; 25 | } else { 26 | prefix = str.substring(0, index); 27 | path = str.substring(index + 1); 28 | if (prefix.length() == 0) { 29 | throw new IllegalArgumentException(); 30 | } 31 | } 32 | if (path.length() == 0) { 33 | throw new IllegalArgumentException(); 34 | } 35 | } 36 | 37 | public ResourcePath(String prefix, String path) { 38 | if (prefix == null || path == null || prefix.length() == 0 || path.length() == 0) { 39 | throw new IllegalArgumentException(); 40 | } 41 | this.prefix = prefix; 42 | this.path = path; 43 | } 44 | 45 | public String getPrefix() { 46 | return prefix; 47 | } 48 | 49 | public String getPath() { 50 | return path; 51 | } 52 | 53 | @Override 54 | public boolean equals(Object o) { 55 | if (this == o) return true; 56 | if (o == null || getClass() != o.getClass()) return false; 57 | 58 | ResourcePath that = (ResourcePath) o; 59 | 60 | if (path != null ? !path.equals(that.path) : that.path != null) return false; 61 | if (prefix != null ? !prefix.equals(that.prefix) : that.prefix != null) return false; 62 | 63 | return true; 64 | } 65 | 66 | @Override 67 | public int hashCode() { 68 | int result = prefix != null ? prefix.hashCode() : 0; 69 | result = 31 * result + (path != null ? path.hashCode() : 0); 70 | return result; 71 | } 72 | 73 | @Override 74 | public String toString() { 75 | return (prefix != null ? prefix + SEPARATOR : "") + path; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/test/java/com/yandex/disk/rest/LoggingInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | import com.google.common.io.ByteStreams; 12 | import com.squareup.okhttp.Headers; 13 | import com.squareup.okhttp.Interceptor; 14 | import com.squareup.okhttp.Request; 15 | import com.squareup.okhttp.RequestBody; 16 | import com.squareup.okhttp.Response; 17 | import com.squareup.okhttp.ResponseBody; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | 22 | import java.io.IOException; 23 | import java.util.List; 24 | 25 | import okio.Buffer; 26 | 27 | public class LoggingInterceptor implements Interceptor { 28 | 29 | private static final Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class); 30 | 31 | private static final String SEND_PREFIX = " >> "; 32 | private static final String RECEIVE_PREFIX = " << "; 33 | 34 | private boolean logWire; 35 | 36 | public LoggingInterceptor(boolean logWire) { 37 | this.logWire = logWire; 38 | logger.info("REST API logging started: logWire=" + logWire); 39 | } 40 | 41 | @Override 42 | public Response intercept(Chain chain) throws IOException { 43 | Request request = chain.request(); 44 | String hash = Integer.toHexString(chain.hashCode()); 45 | String sendPrefix = hash + SEND_PREFIX; 46 | String receivePrefix = hash + RECEIVE_PREFIX; 47 | 48 | if (logWire) { 49 | RequestBody requestBody = request.body(); 50 | if (requestBody != null) { 51 | logger.info(sendPrefix + "request: " + requestBody); 52 | Buffer buffer = new Buffer(); 53 | requestBody.writeTo(buffer); 54 | byte[] requestBuffer = ByteStreams.toByteArray(buffer.inputStream()); 55 | logBuffer(sendPrefix, requestBuffer); 56 | } 57 | request = request.newBuilder() 58 | .removeHeader("Accept-Encoding") 59 | .addHeader("Accept-Encoding", "") 60 | .build(); 61 | } 62 | 63 | logger.info(sendPrefix + request.method() + " " + request.url()); 64 | logger.info(sendPrefix + "on " + chain.connection()); 65 | logHeaders(sendPrefix, request.headers()); 66 | 67 | Response response = chain.proceed(request); 68 | logger.info(receivePrefix + response.protocol() + " " + response.code() 69 | + " " + response.message()); 70 | logHeaders(receivePrefix, response.headers()); 71 | 72 | if (logWire) { 73 | ResponseBody body = response.body(); 74 | byte[] responseBuffer = ByteStreams.toByteArray(body.byteStream()); 75 | response = response.newBuilder() 76 | .body(ResponseBody.create(body.contentType(), responseBuffer)) 77 | .build(); 78 | logBuffer(receivePrefix, responseBuffer); 79 | } 80 | 81 | return response; 82 | } 83 | 84 | private void logBuffer(String prefix, byte[] buf) { 85 | if (buf.length < 10240) { // assume binary output: magic number from RestClientTest 86 | logger.info(prefix + new String(buf)); 87 | } else { 88 | logger.info(prefix + "[" + buf.length + " bytes]"); 89 | } 90 | } 91 | 92 | private void logHeaders(String prefix, Headers headers) { 93 | for (String name : headers.names()) { 94 | List values = headers.values(name); 95 | for (String value : values) { 96 | logger.info(prefix + name + ": " + value); 97 | } 98 | } 99 | } 100 | } -------------------------------------------------------------------------------- /disk-restapi-sdk/src/test/java/com/yandex/disk/rest/QueryBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest; 10 | 11 | import com.yandex.disk.rest.QueryBuilder; 12 | 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | import org.junit.runners.JUnit4; 16 | 17 | import java.io.UnsupportedEncodingException; 18 | import java.net.URLDecoder; 19 | 20 | import static org.junit.Assert.assertTrue; 21 | 22 | @RunWith(JUnit4.class) 23 | public class QueryBuilderTest { 24 | 25 | @Test 26 | public void testQueryBuilder() throws UnsupportedEncodingException { 27 | String str = new QueryBuilder("https://cloud-api.yandex.net/v1/disk/resources") 28 | .add("path", "\u20ac\u00a3\u0024") 29 | .add("name", (Integer) null) 30 | .add("overwrite", true) 31 | .build(); 32 | assertTrue(str.equals( 33 | "https://cloud-api.yandex.net/v1/disk/resources?path=%E2%82%AC%C2%A3%24&overwrite=true")); 34 | assertTrue(URLDecoder.decode(str, "UTF-8").equals( 35 | "https://cloud-api.yandex.net/v1/disk/resources?path=\u20ac\u00a3\u0024&overwrite=true")); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/test/java/com/yandex/disk/rest/util/ISO8601Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.util; 10 | 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.junit.runners.JUnit4; 14 | 15 | import java.util.Calendar; 16 | import java.util.Date; 17 | import java.util.GregorianCalendar; 18 | 19 | import static org.junit.Assert.assertFalse; 20 | import static org.junit.Assert.assertNull; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | @RunWith(JUnit4.class) 24 | public class ISO8601Test { 25 | 26 | @Test 27 | public void testFromTo() throws Exception { 28 | Calendar calendar = GregorianCalendar.getInstance(); 29 | assertTrue(calendar.getTimeInMillis() / 1000L 30 | == ISO8601.toCalendar(ISO8601.fromCalendar(calendar)).getTimeInMillis() / 1000L); 31 | } 32 | 33 | @Test 34 | public void testParse() throws Exception { 35 | assertNull(ISO8601.parse("")); 36 | assertFalse(ISO8601.parse("2014-07-07T10:03:04+00:00").equals(new Date(1404727384001L))); 37 | assertTrue(ISO8601.parse("2014-12-09T20:45:57+04:00").equals(new Date(1418143557000L))); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /disk-restapi-sdk/src/test/java/com/yandex/disk/rest/util/ResourcePathTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * (C) 2015 Yandex LLC (https://yandex.com/) 3 | * 4 | * The source code of Java SDK for Yandex.Disk REST API 5 | * is available to use under terms of Apache License, 6 | * Version 2.0. See the file LICENSE for the details. 7 | */ 8 | 9 | package com.yandex.disk.rest.util; 10 | 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.junit.runners.JUnit4; 14 | 15 | import static org.junit.Assert.assertEquals; 16 | import static org.junit.Assert.assertTrue; 17 | 18 | @RunWith(JUnit4.class) 19 | public class ResourcePathTest { 20 | 21 | @Test 22 | public void testPositive() throws Exception { 23 | String src = "prefix:/long/path"; 24 | ResourcePath resourcePath = new ResourcePath(src); 25 | assertTrue("prefix".equals(resourcePath.getPrefix())); 26 | assertTrue("/long/path".equals(resourcePath.getPath())); 27 | assertTrue(src.equals(resourcePath.toString())); 28 | assertEquals(resourcePath, new ResourcePath("prefix", "/long/path")); 29 | } 30 | 31 | @Test(expected = IllegalArgumentException.class) 32 | public void testNullArg() throws Exception { 33 | new ResourcePath(null); 34 | } 35 | 36 | @Test(expected = IllegalArgumentException.class) 37 | public void testNullArg2() throws Exception { 38 | new ResourcePath(null, ""); 39 | } 40 | 41 | @Test(expected = IllegalArgumentException.class) 42 | public void testNullArg3() throws Exception { 43 | new ResourcePath("", null); 44 | } 45 | 46 | @Test 47 | public void testNoPrefix() throws Exception { 48 | assertEquals(new ResourcePath("/long/path").toString(), "/long/path"); 49 | } 50 | 51 | @Test(expected = IllegalArgumentException.class) 52 | public void testNoPath() throws Exception { 53 | new ResourcePath("1:"); 54 | } 55 | 56 | @Test(expected = IllegalArgumentException.class) 57 | public void testWrongPrefix() throws Exception { 58 | new ResourcePath(":2"); 59 | } 60 | 61 | @Test 62 | public void testSecondColon() throws Exception { 63 | new ResourcePath("1:2:3"); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':disk-restapi-sdk' 2 | include ':app' 3 | -------------------------------------------------------------------------------- /testResources/test-upload-001.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yandex-disk/yandex-disk-restapi-java/48ad74c754a42222de80693eb8edb234a5faf9a6/testResources/test-upload-001.bin --------------------------------------------------------------------------------