diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml deleted file mode 100644 index 9c54ecc..0000000 --- a/.github/workflows/android.yml +++ /dev/null @@ -1,17 +0,0 @@ -name: Android CI - -on: [push] - -jobs: - build: - - runs-on: windows-latest - - steps: - - uses: actions/checkout@v1 - - name: set up JDK 1.8 - uses: actions/setup-java@v1 - with: - java-version: 1.8 - - name: Build with Gradle - run: mvn -Dmaven.test.skip=true -U -e -X -B package --file pom-android.xml diff --git a/.github/workflows/esdk-obs-java.yml b/.github/workflows/esdk-obs-java.yml new file mode 100644 index 0000000..d67dab2 --- /dev/null +++ b/.github/workflows/esdk-obs-java.yml @@ -0,0 +1,49 @@ +name: ci esdk-obs-java + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - name: Set up JDK 1.8 + uses: actions/setup-java@v1 + with: + java-version: 1.8 + - name: 编译常规的 Java SDK + run: mvn clean install -Dmaven.test.skip=true -f pom-java.xml + - name: 创建存放Java SDK的目录(esdk-obs-java-en-on-github) + run: mkdir esdk-obs-java-en-on-github + - name: 拷贝Java SDK的编译结果到目录(esdk-obs-java-en-on-github) + run: cp /home/runner/work/huaweicloud-sdk-java-obs/huaweicloud-sdk-java-obs/target/esdk-obs-java-* ./esdk-obs-java-en-on-github + #- name: 编译性能优化后的 Java SDK + # run: mvn clean install -Dmaven.test.skip=true -f pom-java-optimization.xml + #- name: 拷贝性能优化后的Java SDK的编译结果到目录(esdk-obs-java-cn-on-github) + # run: cp /home/runner/work/huaweicloud-sdk-java-obs/huaweicloud-sdk-java-obs/target/esdk-obs-java-optimization-*.jar ./esdk-obs-java-en-on-github + + - name: 上传Java SDK输出件 + if: success() + uses: actions/upload-artifact@master + with: + # Artifact name + name: esdk-obs-java-en-on-github + # Destination path + path: ./esdk-obs-java-en-on-github/ + + - name: 编译常规的 Android SDK + run: mvn clean install -Dmaven.test.skip=true -f pom-android.xml + - name: 创建存放Android SDK的目录(esdk-obs-android-en-on-github) + run: mkdir esdk-obs-android-en-on-github + - name: 拷贝Java SDK的编译结果到目录(esdk-obs-android-en-on-github) + run: cp /home/runner/work/huaweicloud-sdk-java-obs/huaweicloud-sdk-java-obs/target/esdk-obs-android-* ./esdk-obs-android-en-on-github + - name: 上传Android SDK输出件 + if: success() + uses: actions/upload-artifact@master + with: + # Artifact name + name: esdk-obs-android-en-on-github + # Destination path + path: ./esdk-obs-android-en-on-github/ diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml deleted file mode 100644 index 08c401d..0000000 --- a/.github/workflows/maven.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: OBS Java SDK CI - -on: [push] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v1 - - name: Set up JDK 1.8 - uses: actions/setup-java@v1 - with: - java-version: 1.8 - - name: Build with Maven - run: mvn -B package --file pom-java.xml - - name: Upload artifact - if: success() - uses: actions/upload-artifact@v1.0.0 - with: - # Artifact name - name: esdk-obs-java - # Destination path - path: /home/runner/work/huaweicloud-sdk-java-obs/huaweicloud-sdk-java-obs/target/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..bc7c364 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,22 @@ +language: java + +jdk: + - openjdk8 + +#cache: +# directories: +# - "$HOME/.m2" + +#before_install: +# - cp .maven.settings.xml $HOME/.m2/settings.xml + +jobs: + include: +# - stage: findbugs +# script: +# - mvn clean package findbugs:findbugs -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dfindbugs.excludeFilterFile=./app/src/test -f pom-java.xml + - stage: install + script: + - mvn install -Dmaven.javadoc.skip=true -Dcheckstyle.skip=false -Drat.skip=false -f pom-java.xml +after_success: + - bash <(curl -s https://codecov.io/bash) diff --git a/Help on License b/Help on License new file mode 100644 index 0000000..1e4325b --- /dev/null +++ b/Help on License @@ -0,0 +1,35 @@ +This help guides Huawei engineers to add or modify the license announcement +during the development. + +In the case that you add a new file, the text below should be added in the head. + Copyright 2019 Huawei Technologies Co.,Ltd. + + Licensed under the Apache License, Version 2.0 (the "License"); you may not use + this file except in compliance with the License. You may obtain a copy of the + License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software distributed + under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. See the License for the + specific language governing permissions and limitations under the License. + + +In the case that you modify one existing file, the text below should be added after +the exiting license. + Huawei has modified this source file. + + Copyright 2019 Huawei Technologies Co., Ltd. + + Licensed under the Apache License, Version 2.0 (the "License"); you may not + use this file except in compliance with the License. You may obtain a copy of + the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + License for the specific language governing permissions and limitations under + the License. diff --git a/LICENSE b/LICENSE index 4cd54f9..2a00b14 100644 --- a/LICENSE +++ b/LICENSE @@ -1,201 +1,13 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ +## OPEN SOURCE SOFTWARE NOTICE +This document contains open source software notice for this product. And this document is confidential information of copyright holder. Recipient shall protect it in due care and shall not disseminate it without permission. - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION +### Warranty Disclaimer +THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. - 1. Definitions. - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright 2019 Huawei Technologies Co., Ltd. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. +### Copyright Notice and License Texts +### Written Offer +This product contains software whose rights holders license it on the terms of the GNU General Public License, version 2 (GPLv2) or other open source software license. We will provide you with the source code of the software licensed under related license if you send us a written request by mail or email to the following addresses: +foss@huawei.com +detailing the name of the product and the firmware version for which you need the source code and indicating how we can contact you. +**PLEASE NOTE THAT WE WILL ASK YOU TO PAY US FOR THE COSTS OF A DATA CARRIER AND THE POSTAL CHARGES TO SEND THE DATA CARRIER TO YOU. THIS OFFER IS VALID FOR THREE YEARS FROM THE MOMENT WE DISTRIBUTED THE PRODUCT AND VALID FOR AS LONG AS WE OFFER SPARE PARTS OR CUSTOMER SUPPORT FOR THAT PRODUCT MODEL.** diff --git a/Notice.MD b/Notice.MD index 607c5f4..aca5a12 100644 --- a/Notice.MD +++ b/Notice.MD @@ -6,17 +6,6 @@ Warranty Disclaimer THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. Copyright Notice and License Texts -Software: JetS3t 0.9.4 -Copyright notice: -Copyright 1999-2005 The Apache Software Foundation -Copyright 1999-2015 The Apache Software Foundation -Copyright (c) 2000 - 2015 The Legion of the Bouncy Castle Inc. (http:www.bouncycastle.org) -Copyright (c) 2003-2004 David Vignoni. -Copyright 2002-2013 The Apache Software Foundation -Copyright (C) 2002 Kevin Atkinson (kevina@gnu.org) -Copyright (c) 2001 The Apache Software Foundation. All rights reserved. -Copyright (c) 2001-2004 by the MX4J contributors. All rights reserved. - License: Apache License V2.0 Apache License @@ -189,57 +178,14 @@ License: Apache License V2.0 See the License for the specific language governing permissions and limitations under the License. - -Software: aws-sdk-java 1.11.490 -Copyright notice: -Copyright 2013-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. -- XML parsing and utility functions from JetS3t - Copyright 2006-2009 James Murty. -- PKCS#1 PEM encoded private key parsing and utility functions from oauth.googlecode.com - Copyright 1998-2010 AOL Inc. - -License: Apache License V2.0 -Please see above. - -Software: aws-sdk-java 1.10.30 -Copyright notice: -Copyright 2013-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. -Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. -Amazon Modifications: Copyright 2014 Amazon.com, Inc. or its affiliates. -Copyright (c) 1998-2010 AOL Inc. -Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. -Copyright 2013-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. -Copyright 2010-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved. -- XML parsing and utility functions from JetS3t - Copyright 2006-2009 James Murty. -- JSON parsing and utility functions from JSON.org - Copyright 2002 JSON.org. -- PKCS1 PEM encoded private key parsing and utility functions from oauth.googlecode.com - Copyright 1998-2010 AOL Inc. -Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. - -License: Apache License V2.0 -Please see above. - -Software: com.android.tools.build:gradle 3.2.1 -Copyright notice: -Copyright 2011 the original author or authors. -Copyright (c) 2012, The Android Open Source Project - -License: Apache License V2.0 -Please see above. - -Software: aliyun-oss-sdk 2.6.1 -Copyright notice: -Copyright (C) Alibaba Cloud Computing - - -License: Apache License V2.0 -Please see above. - -Software: jackson-annotations-2.10.0 +Software: jackson-annotations-2.11.1 Copyright notice: Copyright 2007-2019 the FasterXML.com. License: Apache License V2.0 Please see above. -Software: jackson-core-2.10.0 +Software: jackson-core-2.11.1 Copyright notice: Copyright 2007-2019 Tatu Saloranta (tatu.saloranta@iki.fi). Copyright 2007-2019 the FasterXML.com. @@ -247,32 +193,29 @@ Copyright 2007-2019 the FasterXML.com. License: Apache License V2.0 Please see above. -Software: jackson-databind-2.10.0 +Software: jackson-databind-2.11.1 Copyright notice: Copyright 2007-2019 the FasterXML.com. License: Apache License V2.0 Please see above. - -License: Apache License V2.0 -Please see above. - -Software: java-xmlbuilder-1.2 +Software: java-xmlbuilder-1.3 Copyright notice: Copyright 2008-2014 James Murty (www.jamesmurty.com) License: Apache License V2.0 Please see above. -Software: log4j-api-2.12.0 +Software: log4j-api-2.13.2 Copyright notice: Copyright 1999-2017 The Apache Software Foundation Copyright 1999-2017 Apache Software Foundation License: Apache License V2.0 Please see above. -Software: log4j-core-2.12.0 + +Software: log4j-core-2.13.2 Copyright notice: Copyright 1999-2017 The Apache Software Foundation Copyright 1999-2012 Apache Software Foundation @@ -281,8 +224,7 @@ Copyright 2005-2006 Tim Fennell License: Apache License V2.0 Please see above. - -Software: okhttp-3.11.0 +Software: okhttp-3.14.9 Copyright notice: Copyright (C) 2012 Google Inc. Copyright (C) 2011 The Android Open Source Project @@ -310,7 +252,7 @@ Copyright (C) 2018 Square, Inc. License: Apache License V2.0 Please see above. -Software: okio-1.17.2 +Software: okio-1.17.5 Copyright notice: Copyright (C) 2014 Square, Inc. Copyright 2014 Square Inc. diff --git a/README-Android.md b/README-Android.md new file mode 100644 index 0000000..029770c --- /dev/null +++ b/README-Android.md @@ -0,0 +1,140 @@ +Version 3.22.3 +Third-party dependence: +1. Replace log4j2 2.17.0 with log4j2 2.17.1 +2. Replace okhttp 4.9.1 with okhttp 4.9.3 +3. Replace okio 2.7.0 with okio 2.10.0 +4. Replace jackson-core 2.12.5 with jackson-core 2.13.0 +5. Replace jackson-databind 2.12.5 with jackson-databind 2.13.0 +6. Replace jackson-annotations 2.12.5 with jackson-annotations 2.13.0 +----------------------------------------------------------------------------------- + +Version 3.21.12 +Third-party dependence: +1. Replace log4j2 2.16.0 with log4j2 2.17.0 +----------------------------------------------------------------------------------- + +Version 3.21.11 +New features: +1. Allowed you to add any custom header field in a request. +Third-party dependence: +1. Replace jackson-core 2.11.1 with jackson-core 2.12.5 +2. Replace jackson-databind 2.11.1 with jackson-databind 2.12.5 +3. Replace jackson-annotations 2.11.1 with jackson-annotations 2.12.5 +4. Replace okhttp 4.8.0 with okhttp 4.9.1 +5. Replace log4j2 2.14.1 with log4j2 2.16.0 +----------------------------------------------------------------------------------- + +Version 3.21.8 + +1. Replace okhttp 3.14.9 with okhttp 4.8.0. +2. Adding original headers to Response object + +----------------------------------------------------------------------------------- + +Version 3.21.4 + +1. Fixed a known issue + +----------------------------------------------------------------------------------- + +Version 3.20.1 +Third-party dependence: +1. Replace okhttp 3.11.0 with okhttp 3.14.2. + +----------------------------------------------------------------------------------- + +Version 3.19.11 +New features: +1. Request authentication supports obtaining access keys by searching in sequence. +2. Download requests are supported. If status code 302 Found is returned, authentication information is not required for redirection. + +Third-party dependence: +1. Replace log4j-core 2.8.2 with log4j-core 2.12.0. +2. Replace jackson-databind 2.8.2 with log4j-api 2.12.0. +3. Replace java-xmlbuilder 1.1 with java-xmlbuilder 1.2. + +----------------------------------------------------------------------------------- + +Version 3.19.9 + +Third-party dependencies: +1. replace jackson-core 2.9.9 with jackson-core 2.9.10 +2. replace jackson-databind 2.9.9 with jackson-databind 2.9.10 +3. replace jackson-annotations 2.9.9 with jackson-core 2.9.10 + +----------------------------------------------------------------------------------- + +Version 3.19.7.1 + +New features: +1. Added the IObsCredentialsProvider interface to provide methods for obtaining AK/SK methods and actively refreshing AK/SK. +2. Added three implementations of the IObsCredentialsProvider interface: +   a. User provide AK/SK: BasicObsCredentialsProvider; +   b. Get AK/SK from the environment variable: EnvironmentVariableObsCredentialsProvider; +   c. Get AK/SK from the ECS service: EcsObsCredentialsProvider. +3. Support the content-type setting of the svp format file, and set the value to image/svg+xml. + +----------------------------------------------------------------------------------- + +Version 3.19.5.x + +Third-party dependencies: +1. replace okhttp 3.10.0 with okhttp 3.11.0 +2. replace jackson-core 2.9.8 with jackson-core 2.9.9 +3. replace jackson-databind 2.9.8 with jackson-databind 2.9.9 +4. replace jackson-annotations 2.9.8 with jackson-core 2.9.9 + +----------------------------------------------------------------------------------- + +Version 3.19.5 + +New features: +1. Added the project ID parameter to SSE-KMS. +2. Added the detailed error flag returned by the OBS server when an exception occurs on ObsException.getErrorIndicator. + +Resolved issues: +1. [Function] Fixed the issue that upload and download progress bar cannot be displayed when content-length is not set. +2. [Function] Fixed the issue that content-type is automatically set to video/mp4 for uploaded files with file name extension of .mp4. +3. [Function] Fixed the issue of object download failure in the data retrieval scenario. + +----------------------------------------------------------------------------------- + +Version 3.1.3 +New features: +1. Added bucket encryption APIs: ObsClient.setBucketEncryption, ObsClient.getBucketEncryption, and ObsClient.deleteBucketEncryption. Currently, only the SSE-KMS encryption is supported. +2. Added the enumerated type (SSEAlgorithmEnum) for server-side encryption. The server-side encryption model ServerAlgorithm and ServerEncryption are marked as Deprecated. + +Documentation & Demo +1. Modified the encryption sample code in the section describing server-side encryption in the Developer Guide. + +Resolved issues: +1. Optimized the logging when exceptions occur. +2. Fixed the issue that errors may occur when the ByteArrayInputStream data flow is transferred during object upload. +3. Optimized the level of access logs to avoid ambiguity. +4. Changed the lower limit of resumable upload API on a part size from 5 MB to 100KB. + +----------------------------------------------------------------------------------- + +Version 3.1.2 + +New features: +1. FunctionGraph configuration and query are supported in the bucket event notification APIs: ObsClient.setBucketNotification and ObsClient.getBucketNotification. + +Documentation & Demo +1. Added the description of FunctionGraph configuration in the section about event notification in the Developer Guide. + +Resolved issues: +1. Fixed the issue that the error information reported by the bucket creation API ObsClient.createBucket is incorrect due to protocol negotiation. +2. Fix the bugs at the bottom layer of okhttp3.Dispatcher. This bug causes the issue that the maximum number of concurrent requests exceeds the upper limit. + +----------------------------------------------------------------------------------- + +Version 3.1.1 + +New features: +1. Integrated log4j 1.x as the log component. +2. Added the temporary authentication access API (ObsClient.createGetTemporarySignature) that supports policy setting. +3. The API for object upload (ObsClient.putObject) can automatically identify a wider MIME type. + +Resolved issues: +1. Fixed the issue that multiple TopicConfigurations cannot be set for the bucket event notification API (ObsClient.setBucketNotification). diff --git a/README-Java.md b/README-Java.md new file mode 100644 index 0000000..eceee0d --- /dev/null +++ b/README-Java.md @@ -0,0 +1,171 @@ +Version 3.22.3 +Third-party dependence: +1. Replace log4j2 2.17.0 with log4j2 2.17.1 +2. Replace okhttp 4.9.1 with okhttp 4.9.3 +3. Replace okio 2.7.0 with okio 2.10.0 +4. Replace jackson-core 2.12.5 with jackson-core 2.13.0 +5. Replace jackson-databind 2.12.5 with jackson-databind 2.13.0 +6. Replace jackson-annotations 2.12.5 with jackson-annotations 2.13.0 +----------------------------------------------------------------------------------- + +Version 3.21.12 +Third-party dependence: +1. Replace log4j2 2.16.0 with log4j2 2.17.0 +----------------------------------------------------------------------------------- + +Version 3.21.11 +New features: +1. Allowed you to add any custom header field in a request. +Third-party dependence: +1. Replace jackson-core 2.11.1 with jackson-core 2.12.5 +2. Replace jackson-databind 2.11.1 with jackson-databind 2.12.5 +3. Replace jackson-annotations 2.11.1 with jackson-annotations 2.12.5 +4. Replace okhttp 4.8.0 with okhttp 4.9.1 +5. Replace log4j2 2.14.1 with log4j2 2.16.0 +----------------------------------------------------------------------------------- +Version 3.21.8 + +1. Replace okhttp 3.14.9 with okhttp 4.8.0. +2. Adding original headers to Response object + +----------------------------------------------------------------------------------- + +Version 3.21.4 + +1. Fixed a known issue + +----------------------------------------------------------------------------------- + +Version 3.20.6 +Fixed issues: +1. Fixed the issue that logs cannot be dynamically updated. +2. Fixed the issue that automatic depression is not executed when gzip files are being downloaded. + +Third-party dependencies: +1. Replaced okhttp 3.14.2 with okhttp 3.14.4. +2. Replaced log4j-core 2.12.0 with log4j-core 2.13.2. +3. Replaced log4j-api 2.12.0 with log4j-api 2.13.2. + +----------------------------------------------------------------------------------- + +Version 3.20.3 +New Features: +1. Supports Requester Pays. +2. Deleted the default log4j configuration. + +----------------------------------------------------------------------------------- + +Version 3.20.1 +Third-party dependence: +1. Replace okhttp 3.11.0 with okhttp 3.14.2. + +----------------------------------------------------------------------------------- + +Version 3.19.11 +New features: +1. Request authentication supports obtaining access keys by searching in sequence. +2. Download requests are supported. If status code 302 Found is returned, authentication information is not required for redirection. + +Third-party dependence: +1. Replace log4j-core 2.8.2 with log4j-core 2.12.0. +2. Replace jackson-databind 2.8.2 with log4j-api 2.12.0. +3. Replace java-xmlbuilder 1.1 with java-xmlbuilder 1.2. + +----------------------------------------------------------------------------------- + +Version 3.19.9 + +Third-party dependencies: +1. replace jackson-core 2.9.9 with jackson-core 2.9.10 +2. replace jackson-databind 2.9.9 with jackson-databind 2.9.10 +3. replace jackson-annotations 2.9.9 with jackson-core 2.9.10 + +----------------------------------------------------------------------------------- + +Version 3.19.7.1 + +New features: +1. Added the IObsCredentialsProvider interface to provide methods for obtaining AK/SK methods and actively refreshing AK/SK. +2. Added three implementations of the IObsCredentialsProvider interface: +   a. User provide AK/SK: BasicObsCredentialsProvider; +   b. Get AK/SK from the environment variable: EnvironmentVariableObsCredentialsProvider; +   c. Get AK/SK from the ECS service: EcsObsCredentialsProvider. +3. Support the content-type setting of the svp format file, and set the value to image/svg+xml. + +----------------------------------------------------------------------------------- + +Version 3.19.5.x + +Third-party dependencies: +1. replace okhttp 3.10.0 with okhttp 3.11.0 +2. replace jackson-core 2.9.8 with jackson-core 2.9.9 +3. replace jackson-databind 2.9.8 with jackson-databind 2.9.9 +4. replace jackson-annotations 2.9.8 with jackson-core 2.9.9 + +----------------------------------------------------------------------------------- + +Version 3.19.5 + +New features: +1. Added the project ID parameter to SSE-KMS. +2. Added the detailed error flag returned by the OBS server when an exception occurs on ObsException.getErrorIndicator. + +Resolved issues: +1. [Function] Fixed the issue that upload and download progress bar cannot be displayed when content-length is not set. +2. [Function] Fixed the issue that content-type is automatically set to video/mp4 for uploaded files with file name extension of .mp4. +3. [Function] Fixed the issue of object download failure in the data retrieval scenario. + +----------------------------------------------------------------------------------- + +Version 3.1.3 +New features: +1. Added bucket encryption APIs: ObsClient.setBucketEncryption, ObsClient.getBucketEncryption, and ObsClient.deleteBucketEncryption. Currently, only the SSE-KMS encryption is supported. +2. Added the enumerated type (SSEAlgorithmEnum) for server-side encryption. The server-side encryption model ServerAlgorithm and ServerEncryption are marked as Deprecated. + +Documentation & Demo +1. Modified the encryption sample code in the section describing server-side encryption in the Developer Guide. + +Resolved issues: +1. Optimized the logging when exceptions occur. +2. Fixed the issue that errors may occur when the ByteArrayInputStream data flow is transferred during object upload. +3. Optimized the level of access logs to avoid ambiguity. +4. Changed the lower limit of resumable upload API on a part size from 5 MB to 100KB. + +----------------------------------------------------------------------------------- + +Version 3.1.2.1 + +Resolved issues: +1. Modified the default value of maxIdleConnections in ObsConfiguration to 1000. + +----------------------------------------------------------------------------------- + +Version 3.1.2 + +New features: +1. FunctionGraph configuration and query are supported in the bucket event notification APIs: ObsClient.setBucketNotification and ObsClient.getBucketNotification. + +Documentation & Demo +1. Added the description of FunctionGraph configuration in the section about event notification in the Developer Guide. + +Resolved issues: +1. Fixed the issue that the error information reported by the bucket creation API ObsClient.createBucket is incorrect due to protocol negotiation. +2. Fix the bugs at the bottom layer of okhttp3.Dispatcher. This bug causes the issue that the maximum number of concurrent requests exceeds the upper limit. + +----------------------------------------------------------------------------------- + +Version 3.1.1 + +New features: +1. Integrated log4j 1.x as the log component. +2. Added the temporary authentication access API (ObsClient.createGetTemporarySignature) that supports policy setting. +3. The API for object upload (ObsClient.putObject) can automatically identify a wider MIME type. + +Resolved issues: +1. Fixed the issue that multiple TopicConfigurations cannot be set for the bucket event notification API (ObsClient.setBucketNotification). +2. Fixed the issue that the SDK is incompatible with JDK 9 or later versions. + + + + + diff --git a/README.MD b/README.MD index ffa15de..eceee0d 100644 --- a/README.MD +++ b/README.MD @@ -1,123 +1,171 @@ -Version 3.19.11 -New features: -1. Request authentication supports obtaining access keys by searching in sequence. -2. Download requests are supported. If status code 302 Found is returned, authentication information is not required for redirection. - -Third-party dependence: -1. Replace log4j-core 2.8.2 with log4j-core 2.12.0. -2. Replace jackson-databind 2.8.2 with log4j-api 2.12.0. -3. Replace java-xmlbuilder 1.1 with java-xmlbuilder 1.2. - ------------------------------------------------------------------------------------ - -Version 3.19.9 - -Third-party dependencies: -1. replace jackson-core 2.9.9 with jackson-core 2.9.10 -2. replace jackson-databind 2.9.9 with jackson-databind 2.9.10 -3. replace jackson-annotations 2.9.9 with jackson-core 2.9.10 - ------------------------------------------------------------------------------------ - -Version 3.19.7 - -New features: -1. Added the IObsCredentialsProvider interface to provide methods for obtaining AK/SK methods and actively refreshing AK/SK. -2. Added three implementations of the IObsCredentialsProvider interface: -   a. User provide AK/SK: BasicObsCredentialsProvider; -   b. Get AK/SK from the environment variable: EnvironmentVariableObsCredentialsProvider; -   c. Get AK/SK from the ECS service: EcsObsCredentialsProvider. -3. Support the content-type setting of the svp format file, and set the value to image/svg+xml. -4. Update the third-party dependencies: - a. replace jackson-core 2.9.8 with jackson-core 2.9.9 - b. replace jackson-databind 2.9.8 with jackson-databind 2.9.9 - c. replace jackson-annotations 2.9.8 with jackson-core 2.9.9 - -Resolved issues: - ------------------------------------------------------------------------------------ - -Version 3.19.5.3 - -New features: - - -Resolved issues: - ------------------------------------------------------------------------------------ - -Version 3.19.5.2 - -New features: - - -Resolved issues: -1. Updated the copyright notice. - ------------------------------------------------------------------------------------ - -Version 3.19.5 - -New features: -1. Added the project ID parameter to SSE-KMS. -2. Added the detailed error flag returned by the OBS server when an exception occurs on ObsException.getErrorIndicator. - -Resolved issues: -1. [Function] Fixed the issue that upload and download progress bar cannot be displayed when content-length is not set. -2. [Function] Fixed the issue that content-type is automatically set to video/mp4 for uploaded files with file name extension of .mp4. -3. [Function] Fixed the issue of object download failure in the data retrieval scenario. - ------------------------------------------------------------------------------------ - -Version 3.1.3 -New features: -1. Added bucket encryption APIs: ObsClient.setBucketEncryption, ObsClient.getBucketEncryption, and ObsClient.deleteBucketEncryption. Currently, only the SSE-KMS encryption is supported. -2. Added the enumerated type (SSEAlgorithmEnum) for server-side encryption. The server-side encryption model ServerAlgorithm and ServerEncryption are marked as Deprecated. - -Documentation & Demo -1. Modified the encryption sample code in the section describing server-side encryption in the Developer Guide. - -Resolved issues: -1. Optimized the logging when exceptions occur. -2. Fixed the issue that errors may occur when the ByteArrayInputStream data flow is transferred during object upload. -3. Optimized the level of access logs to avoid ambiguity. -4. Changed the lower limit of resumable upload API on a part size from 5 MB to 100KB. - ------------------------------------------------------------------------------------ - -Version 3.1.2.1 - -Resolved issues: -1. Modified the default value of maxIdleConnections in ObsConfiguration to 1000. - ------------------------------------------------------------------------------------ - -Version 3.1.2 - -New features: -1. FunctionGraph configuration and query are supported in the bucket event notification APIs: ObsClient.setBucketNotification and ObsClient.getBucketNotification. - -Documentation & Demo -1. Added the description of FunctionGraph configuration in the section about event notification in the Developer Guide. - -Resolved issues: -1. Fixed the issue that the error information reported by the bucket creation API ObsClient.createBucket is incorrect due to protocol negotiation. -2. Fix the bugs at the bottom layer of okhttp3.Dispatcher. This bug causes the issue that the maximum number of concurrent requests exceeds the upper limit. - ------------------------------------------------------------------------------------ - -Version 3.1.1 - -New features: -1. Integrated log4j 1.x as the log component. -2. Added the temporary authentication access API (ObsClient.createGetTemporarySignature) that supports policy setting. -3. The API for object upload (ObsClient.putObject) can automatically identify a wider MIME type. - -Resolved issues: -1. Fixed the issue that multiple TopicConfigurations cannot be set for the bucket event notification API (ObsClient.setBucketNotification). -2. Fixed the issue that the SDK is incompatible with JDK 9 or later versions. - - - - - +Version 3.22.3 +Third-party dependence: +1. Replace log4j2 2.17.0 with log4j2 2.17.1 +2. Replace okhttp 4.9.1 with okhttp 4.9.3 +3. Replace okio 2.7.0 with okio 2.10.0 +4. Replace jackson-core 2.12.5 with jackson-core 2.13.0 +5. Replace jackson-databind 2.12.5 with jackson-databind 2.13.0 +6. Replace jackson-annotations 2.12.5 with jackson-annotations 2.13.0 +----------------------------------------------------------------------------------- + +Version 3.21.12 +Third-party dependence: +1. Replace log4j2 2.16.0 with log4j2 2.17.0 +----------------------------------------------------------------------------------- + +Version 3.21.11 +New features: +1. Allowed you to add any custom header field in a request. +Third-party dependence: +1. Replace jackson-core 2.11.1 with jackson-core 2.12.5 +2. Replace jackson-databind 2.11.1 with jackson-databind 2.12.5 +3. Replace jackson-annotations 2.11.1 with jackson-annotations 2.12.5 +4. Replace okhttp 4.8.0 with okhttp 4.9.1 +5. Replace log4j2 2.14.1 with log4j2 2.16.0 +----------------------------------------------------------------------------------- +Version 3.21.8 + +1. Replace okhttp 3.14.9 with okhttp 4.8.0. +2. Adding original headers to Response object + +----------------------------------------------------------------------------------- + +Version 3.21.4 + +1. Fixed a known issue + +----------------------------------------------------------------------------------- + +Version 3.20.6 +Fixed issues: +1. Fixed the issue that logs cannot be dynamically updated. +2. Fixed the issue that automatic depression is not executed when gzip files are being downloaded. + +Third-party dependencies: +1. Replaced okhttp 3.14.2 with okhttp 3.14.4. +2. Replaced log4j-core 2.12.0 with log4j-core 2.13.2. +3. Replaced log4j-api 2.12.0 with log4j-api 2.13.2. + +----------------------------------------------------------------------------------- + +Version 3.20.3 +New Features: +1. Supports Requester Pays. +2. Deleted the default log4j configuration. + +----------------------------------------------------------------------------------- + +Version 3.20.1 +Third-party dependence: +1. Replace okhttp 3.11.0 with okhttp 3.14.2. + +----------------------------------------------------------------------------------- + +Version 3.19.11 +New features: +1. Request authentication supports obtaining access keys by searching in sequence. +2. Download requests are supported. If status code 302 Found is returned, authentication information is not required for redirection. + +Third-party dependence: +1. Replace log4j-core 2.8.2 with log4j-core 2.12.0. +2. Replace jackson-databind 2.8.2 with log4j-api 2.12.0. +3. Replace java-xmlbuilder 1.1 with java-xmlbuilder 1.2. + +----------------------------------------------------------------------------------- + +Version 3.19.9 + +Third-party dependencies: +1. replace jackson-core 2.9.9 with jackson-core 2.9.10 +2. replace jackson-databind 2.9.9 with jackson-databind 2.9.10 +3. replace jackson-annotations 2.9.9 with jackson-core 2.9.10 + +----------------------------------------------------------------------------------- + +Version 3.19.7.1 + +New features: +1. Added the IObsCredentialsProvider interface to provide methods for obtaining AK/SK methods and actively refreshing AK/SK. +2. Added three implementations of the IObsCredentialsProvider interface: +   a. User provide AK/SK: BasicObsCredentialsProvider; +   b. Get AK/SK from the environment variable: EnvironmentVariableObsCredentialsProvider; +   c. Get AK/SK from the ECS service: EcsObsCredentialsProvider. +3. Support the content-type setting of the svp format file, and set the value to image/svg+xml. + +----------------------------------------------------------------------------------- + +Version 3.19.5.x + +Third-party dependencies: +1. replace okhttp 3.10.0 with okhttp 3.11.0 +2. replace jackson-core 2.9.8 with jackson-core 2.9.9 +3. replace jackson-databind 2.9.8 with jackson-databind 2.9.9 +4. replace jackson-annotations 2.9.8 with jackson-core 2.9.9 + +----------------------------------------------------------------------------------- + +Version 3.19.5 + +New features: +1. Added the project ID parameter to SSE-KMS. +2. Added the detailed error flag returned by the OBS server when an exception occurs on ObsException.getErrorIndicator. + +Resolved issues: +1. [Function] Fixed the issue that upload and download progress bar cannot be displayed when content-length is not set. +2. [Function] Fixed the issue that content-type is automatically set to video/mp4 for uploaded files with file name extension of .mp4. +3. [Function] Fixed the issue of object download failure in the data retrieval scenario. + +----------------------------------------------------------------------------------- + +Version 3.1.3 +New features: +1. Added bucket encryption APIs: ObsClient.setBucketEncryption, ObsClient.getBucketEncryption, and ObsClient.deleteBucketEncryption. Currently, only the SSE-KMS encryption is supported. +2. Added the enumerated type (SSEAlgorithmEnum) for server-side encryption. The server-side encryption model ServerAlgorithm and ServerEncryption are marked as Deprecated. + +Documentation & Demo +1. Modified the encryption sample code in the section describing server-side encryption in the Developer Guide. + +Resolved issues: +1. Optimized the logging when exceptions occur. +2. Fixed the issue that errors may occur when the ByteArrayInputStream data flow is transferred during object upload. +3. Optimized the level of access logs to avoid ambiguity. +4. Changed the lower limit of resumable upload API on a part size from 5 MB to 100KB. + +----------------------------------------------------------------------------------- + +Version 3.1.2.1 + +Resolved issues: +1. Modified the default value of maxIdleConnections in ObsConfiguration to 1000. + +----------------------------------------------------------------------------------- + +Version 3.1.2 + +New features: +1. FunctionGraph configuration and query are supported in the bucket event notification APIs: ObsClient.setBucketNotification and ObsClient.getBucketNotification. + +Documentation & Demo +1. Added the description of FunctionGraph configuration in the section about event notification in the Developer Guide. + +Resolved issues: +1. Fixed the issue that the error information reported by the bucket creation API ObsClient.createBucket is incorrect due to protocol negotiation. +2. Fix the bugs at the bottom layer of okhttp3.Dispatcher. This bug causes the issue that the maximum number of concurrent requests exceeds the upper limit. + +----------------------------------------------------------------------------------- + +Version 3.1.1 + +New features: +1. Integrated log4j 1.x as the log component. +2. Added the temporary authentication access API (ObsClient.createGetTemporarySignature) that supports policy setting. +3. The API for object upload (ObsClient.putObject) can automatically identify a wider MIME type. + +Resolved issues: +1. Fixed the issue that multiple TopicConfigurations cannot be set for the bucket event notification API (ObsClient.setBucketNotification). +2. Fixed the issue that the SDK is incompatible with JDK 9 or later versions. + + + + + diff --git a/README_CN.MD b/README_CN.MD index cee6093..2f47983 100644 --- a/README_CN.MD +++ b/README_CN.MD @@ -1,124 +1,184 @@ -Version 3.19.11 -新特性: -1. 请求鉴权支持链式访问模式 -2. 支持下载请求,返回302请求的情况下,重定向的时候不带鉴权信息 - -三方依赖: -1. 使用 log4j-core 2.12.0 替代 log4j-core 2.8.2 -2. 使用 log4j-api 2.12.0 替代 jackson-databind 2.8.2 -3. 使用 java-xmlbuilder 1.2 替代 java-xmlbuilder 1.1 - ------------------------------------------------------------------------------------ - -Version 3.19.9 - -三方依赖: -1. 使用 jackson-core 2.9.10 替代 jackson-core 2.9.9 -2. 使用 jackson-databind 2.9.10 替代 jackson-databind 2.9.9 -3. 使用 jackson-annotations 2.9.10 替代 jackson-core 2.9.9 - ------------------------------------------------------------------------------------ - -Version 3.19.7 - -新特性: - -1. 添加IObsCredentialsProvider接口,提供获取AK/SK方法和主动刷新AK/SK的方法; -2. 添加IObsCredentialsProvider接口的三种实现方式: - a. 用户传入AK/SK:BasicObsCredentialsProvider; - b. 从环境变量读取AK/SK:EnvironmentVariableObsCredentialsProvider; - c. 从ECS服务获取AK/SK:EcsObsCredentialsProvider。 -3. 支持svp格式文件的content-type设置,设置值为image/svg+xml; -4. 更新第三方依赖: - a. 使用 jackson-core 2.9.9 替代 jackson-core 2.9.8 - b. 使用 jackson-databind 2.9.9 替代 jackson-databind 2.9.8 - c. 使用 jackson-annotations 2.9.9 替代 jackson-core 2.9.8 - -修复问题: - ------------------------------------------------------------------------------------ - -Version 3.19.5.3 - -新特性: - - -修复问题: - ------------------------------------------------------------------------------------ - -Version 3.19.5.2 - -新特性: - - -修复问题: -1. 更新版本声明; - ------------------------------------------------------------------------------------ - -Version 3.19.5 - -新特性: -1. sse-kms加密方式新增支持project id; -2. 新增ObsException.getErrorIndicator获取异常时OBS服务端返回的详细错误标识; - -修复问题: -1. 【功能】修复上传下载进度条在未设置content-length时无法显示的问题; -2. 【功能】修正以.mp4后缀上传的文件自动设置的content-type为video/mp4; -3. 【功能】修复在数据回源情况下的对象下载失败的问题; - ------------------------------------------------------------------------------------ - -Version 3.1.3 -新特性: -1. 新增桶加密接口(ObsClient.setBucketEncryption/ObsClient.getBucketEncryption/ObsClient.deleteBucketEncryption),目前仅支持SSE-KMS的服务端加密方式; -2. 新增服务端加密方式枚举类型(SSEAlgorithmEnum),将服务端加密相关模型 ServerAlgorithm,ServerEncryption 标记为 Deprecated; - -资料&demo: -1. 开发指南服务端加密章节,修改加密示例代码; - -修复问题: -1. 优化异常情况下的日志记录; -2. 修复上传对象时,传入ByteArrayInputStream数据流可能导致报错的问题; -3. 优化access日志的级别,避免产生歧义; -4. 修改断点续传上传接口对段大小限制,从最小5MB改为最小100KB; - ------------------------------------------------------------------------------------ - -Version 3.1.2.1 - -修复问题: -1. 修改ObsConfiguration中maxIdleConnections参数的默认值为1000; - ------------------------------------------------------------------------------------ - -Version 3.1.2 - -新特性: -1. 桶事件通知接口(ObsClient.setBucketNotification/ObsClient.getBucketNotification)新增对函数工作流服务配置和查询的支持; - -资料&demo: -1. 开发指南事件通知章节,新增对函数工作流服务配置的介绍; - -修复问题: -1. 修复创建桶接口(ObsClient.createBucket)由于协议协商导致报错信息不准确的问题; -2. 修复okhttp3.Dispatcher底层的BUG,该BUG会导致最大并发数超限; - ------------------------------------------------------------------------------------ - -Version 3.1.1 - -新特性: -1. 支持集成 log4j 1.x作为日志组件; -2. 新增支持以Policy设置权限的临时鉴权访问接口(ObsClient.createGetTemporarySignature); -3. 上传对象(ObsClient.putObject)支持自动识别更广泛的MIME类型; - -修复问题: -1. 修复设置桶事件通知接口(ObsClient.setBucketNotification)无法设置多个TopicConfiguration的问题; -2. 修复SDK对JDK 9 及以上版本不兼容的问题; - - - - - +Version 3.22.3 +Third-party dependence: +1. 使用 log4j2 2.17.1 替代 log4j2 2.17.0 +2. 使用 okhttp 4.9.3 替代 okhttp 4.9.1 +3. 使用 okio 2.10.0 替代 okio 2.7.0 +4. 使用 jackson-core 2.13.0 替代 jackson-core 2.12.5 +5. 使用 jackson-databind 2.13.0 替代 jackson-databind 2.12.5 +6. 使用 jackson-annotations 2.13.0 替代 jackson-annotations 2.12.5 +----------------------------------------------------------------------------------- + +Version 3.21.12 +三方依赖: +1. 使用 log4j2 2.17.0 替代 log4j2 2.16.0 +----------------------------------------------------------------------------------- + +Version 3.21.11 +新特性: +1. 用户可以在发送请求时添加任意自定义头域 + +三方依赖: +1. 使用jackson-core 2.12.5 替代jackson-core 2.11.1 +2. 使用 jackson-databind 2.12.5 替代jackson-databind 2.11.1 +3. 使用 jackson-annotations 2.12.5 替代jackson-annotations 2.11.1 +4. 使用 okhttp 4.9.1 替代okhttp 4.8.0 +5. 使用 log4j2 2.16.0 替代 log4j2 2.14.1 +----------------------------------------------------------------------------------- +Version 3.21.8 + +1. okhttp 由 3.14.9 升级至 4.8.0 +2. Response 对象添加原始头域字段 + +----------------------------------------------------------------------------------- +Version 3.21.4 + +1. 修复了一个已知问题 + +----------------------------------------------------------------------------------- + +Version 3.20.7 +新特性: +1. 上传对象时,新增支持指定3个请求头:Content-Disposition,Cache-Control, Expires; + +三方依赖: +1. 使用jackson-core 2.11.1 替代jackson-core 2.10.0 +2. 使用 jackson-databind 2.11.1 替代jackson-databind 2.10.0 +3. 使用 jackson-annotations 2.11.1 替代jackson-annotations 2.10.0 +4. 使用java-xmlbuilder 1.3 替代java-xmlbuilder 1.2 +5. 使用 okhttp 3.14.9 替代okhttp 3.14.4 +5. 使用 okio 1.17.5 替代okio 1.17.2 + +----------------------------------------------------------------------------------- + +Version 3.20.6 +修复问题: +1. 修复日志级别无法动态刷新的问题; +2. 优化下载gzip文件时,默认不自动解压; + +三方依赖: +1. 使用 okhttp 3.14.4 替代okhttp 3.14.2 +2. 使用 log4j-core 2.13.2 替代 log4j-core 2.12.0 +3. 使用 log4j-api 2.13.2 替代 log4j-api 2.12.0 + +----------------------------------------------------------------------------------- + +Version 3.20.3 +新特性: +1. 支持请求者付费 +2. 去掉默认的log4j配置 + +----------------------------------------------------------------------------------- + +Version 3.20.1 +三方依赖: +1. 使用 okhttp 3.14.2 替代okhttp 3.11.0 + +----------------------------------------------------------------------------------- + +Version 3.19.11 +新特性: +1. 请求鉴权支持链式访问模式 +2. 支持下载请求,返回302请求的情况下,重定向的时候不带鉴权信息 + +三方依赖: +1. 使用 log4j-core 2.12.0 替代 log4j-core 2.8.2 +2. 使用 log4j-api 2.12.0 替代 log4j-api 2.8.2 +3. 使用 java-xmlbuilder 1.2 替代 java-xmlbuilder 1.1 + +----------------------------------------------------------------------------------- + +Version 3.19.9 + +三方依赖: +1. 使用 jackson-core 2.9.10 替代 jackson-core 2.9.9 +2. 使用 jackson-databind 2.9.10 替代 jackson-databind 2.9.9 +3. 使用 jackson-annotations 2.9.10 替代 jackson-core 2.9.9 + +----------------------------------------------------------------------------------- + +Version 3.19.7.1 + +新特性: +1. 添加IObsCredentialsProvider接口,提供获取AK/SK方法和主动刷新AK/SK的方法; +2. 添加IObsCredentialsProvider接口的三种实现方式: + a. 用户传入AK/SK:BasicObsCredentialsProvider; + b. 从环境变量读取AK/SK:EnvironmentVariableObsCredentialsProvider; + c. 从ECS服务获取AK/SK:EcsObsCredentialsProvider。 +3. 支持svp格式文件的content-type设置,设置值为image/svg+xml。 + +----------------------------------------------------------------------------------- +Version 3.19.5.x + +三方依赖: +1. 使用okhttp 3.11.0替代okhttp 3.10.0 +2. 使用 jackson-core 2.9.9 替代 jackson-core 2.9.8 +3. 使用 jackson-databind 2.9.9 替代 jackson-databind 2.9.8 +4. 使用 jackson-annotations 2.9.9 替代 jackson-core 2.9.8 + +----------------------------------------------------------------------------------- + +Version 3.19.5 + +新特性: +1. sse-kms加密方式新增支持project id; +2. 新增ObsException.getErrorIndicator获取异常时OBS服务端返回的详细错误标识; + +修复问题: +1. 【功能】修复上传下载进度条在未设置content-length时无法显示的问题; +2. 【功能】修正以.mp4后缀上传的文件自动设置的content-type为video/mp4; +3. 【功能】修复在数据回源情况下的对象下载失败的问题; + +----------------------------------------------------------------------------------- + +Version 3.1.3 +新特性: +1. 新增桶加密接口(ObsClient.setBucketEncryption/ObsClient.getBucketEncryption/ObsClient.deleteBucketEncryption),目前仅支持SSE-KMS的服务端加密方式; +2. 新增服务端加密方式枚举类型(SSEAlgorithmEnum),将服务端加密相关模型 ServerAlgorithm,ServerEncryption 标记为 Deprecated; + +资料&demo: +1. 开发指南服务端加密章节,修改加密示例代码; + +修复问题: +1. 优化异常情况下的日志记录; +2. 修复上传对象时,传入ByteArrayInputStream数据流可能导致报错的问题; +3. 优化access日志的级别,避免产生歧义; +4. 修改断点续传上传接口对段大小限制,从最小5MB改为最小100KB; + +----------------------------------------------------------------------------------- + +Version 3.1.2.1 + +修复问题: +1. 修改ObsConfiguration中maxIdleConnections参数的默认值为1000; + +----------------------------------------------------------------------------------- + +Version 3.1.2 + +新特性: +1. 桶事件通知接口(ObsClient.setBucketNotification/ObsClient.getBucketNotification)新增对函数工作流服务配置和查询的支持; + +资料&demo: +1. 开发指南事件通知章节,新增对函数工作流服务配置的介绍; + +修复问题: +1. 修复创建桶接口(ObsClient.createBucket)由于协议协商导致报错信息不准确的问题; +2. 修复okhttp3.Dispatcher底层的BUG,该BUG会导致最大并发数超限; + +----------------------------------------------------------------------------------- + +Version 3.1.1 + +新特性: +1. 支持集成 log4j 1.x作为日志组件; +2. 新增支持以Policy设置权限的临时鉴权访问接口(ObsClient.createGetTemporarySignature); +3. 上传对象(ObsClient.putObject)支持自动识别更广泛的MIME类型; + +修复问题: +1. 修复设置桶事件通知接口(ObsClient.setBucketNotification)无法设置多个TopicConfiguration的问题; +2. 修复SDK对JDK 9 及以上版本不兼容的问题; + + + + + diff --git a/app/.gitignore b/app/.gitignore deleted file mode 100644 index fcad875..0000000 --- a/app/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/build -/libs/*.jar diff --git a/app/assembly.xml b/app/assembly.xml new file mode 100644 index 0000000..f0ed704 --- /dev/null +++ b/app/assembly.xml @@ -0,0 +1,53 @@ + + + assembly_jar_with_third_party + + zip + + + false + + + ${project.build.directory}/${project.artifactId}-${project.version}.jar + / + + + ${project.build.directory}/${project.artifactId}-${project.version}-javadoc.jar + / + + + ${project.basedir}/README.txt + / + + + ${project.basedir}/pom-dependencies.xml + / + pom.xml + + + ${project.basedir}/src/main/resources/log4j2.xml + / + + + + + ${project.basedir}/src/test/java/samples + /samples + + + ${project.build.directory}/doc + /doc + + + + + + false + false + runtime + third_party + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle deleted file mode 100644 index 5e5c303..0000000 --- a/app/build.gradle +++ /dev/null @@ -1,72 +0,0 @@ -apply plugin: 'com.android.library' - -android.packageBuildConfig = false - -android { - - compileOptions { - encoding "UTF-8" - } - - lintOptions { - abortOnError false - } - - packagingOptions { - exclude 'META-INF/DEPENDENCIES' - exclude 'META-INF/NOTICE' - exclude 'META-INF/LICENSE' - exclude 'META-INF/LICENSE.txt' - exclude 'META-INF/NOTICE.txt' - exclude '**/publicsuffixes.gz' - } - - sourceSets { - main { - manifest.srcFile 'src/main/AndroidManifest.xml' - java.srcDirs = ['src/main/java'] - //resources.srcDirs = ['src/main/resource'] - aidl.srcDirs = ['src/main/java'] - renderscript.srcDirs = ['src/main/java'] - res.srcDirs = ['src/main/res'] - assets.srcDirs = ['src/main/assets'] - } - - test{ - java.srcDirs = ['src/test/java'] - } - } - - compileSdkVersion 24 - buildToolsVersion "24.0.3" - defaultConfig { - minSdkVersion 9 - targetSdkVersion 24 - versionCode 1 - versionName "2.1.10" - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } - - task cleanLibs(type: Delete) { - delete fileTree('libs').include('**/*') - } - - task cleanLogs(type: Delete) { - delete fileTree('../logs').include('**/*') - delete fileTree('../log').include('**/*') - } - - clean.dependsOn cleanLibs - clean.dependsOn cleanLogs -} - -dependencies { - compile 'com.jamesmurty.utils:java-xmlbuilder:1.1' - compile 'com.squareup.okhttp3:okhttp:3.10.0' - compile 'com.squareup.okio:okio:1.14.0' -} diff --git a/app/pom.xml b/app/pom.xml new file mode 100644 index 0000000..04b3ffe --- /dev/null +++ b/app/pom.xml @@ -0,0 +1,171 @@ + + 4.0.0 + + com.huaweicloud + esdk-obs-java-android + 3.21.12 + + OBS SDK for Java/Android + The OBS SDK for Android used for accessing Object Storage Service + + + UTF-8 + ${project.basedir}/../../CI/checkstyle_ruleset_minimal_IT_product_line.xml + + + + + + com.jamesmurty.utils + java-xmlbuilder + 1.3 + + + net.iharder + base64 + + + + + + com.squareup.okhttp3 + okhttp + 4.9.1 + + + com.squareup.okio + okio + 2.7.0 + + + + com.fasterxml.jackson.core + jackson-core + 2.12.5 + + + com.fasterxml.jackson.core + jackson-databind + 2.12.5 + + + com.fasterxml.jackson.core + jackson-annotations + 2.12.5 + + + + org.apache.logging.log4j + log4j-core + 2.17.0 + + + org.apache.logging.log4j + log4j-api + 2.17.0 + + + + + src/main/java + src/test/java + + + org.codehaus.mojo + exec-maven-plugin + 1.2 + + + package + + exec + + + ${basedir}/gradlew.bat + + build + + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + ${checkStyleFile} + + 2.9.1 + + + + org.apache.maven.plugins + maven-clean-plugin + + + + logs + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + UTF-8 + + + + compile + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.0.0-M1 + + 1.8 + UTF-8 + UTF-8 + *.internal.*:*.log:*.proxy:okio + ${project.build.directory}/doc + + + + attach-javadocs + install + + jar + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + install + + single + + + + + false + + assembly-android.xml + + + + + + + + diff --git a/app/src/main/java/com/obs/log/AbstractLog4jLogger.java b/app/src/main/java/com/obs/log/AbstractLog4jLogger.java new file mode 100644 index 0000000..4ff4a21 --- /dev/null +++ b/app/src/main/java/com/obs/log/AbstractLog4jLogger.java @@ -0,0 +1,205 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.log; + +import java.util.logging.Logger; + +import com.obs.services.internal.utils.AccessLoggerUtils; + +public abstract class AbstractLog4jLogger { + private static final Logger ILOG = Logger.getLogger(AbstractLog4jLogger.class.getName()); + + protected final Object logger; + + AbstractLog4jLogger(Object logger) { + this.logger = logger; + } + + public void info(CharSequence msg) { + if (this.logger != null && LoggerMethodHolder.info != null) { + try { + LoggerMethodHolder.info.invoke(this.logger, msg, null); + AccessLoggerUtils.appendLog(msg, "info"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void info(Object obj) { + if (this.logger != null && LoggerMethodHolder.info != null) { + try { + LoggerMethodHolder.info.invoke(this.logger, obj, null); + AccessLoggerUtils.appendLog(obj, "info"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void info(Object obj, Throwable e) { + if (this.logger != null && LoggerMethodHolder.info != null) { + try { + LoggerMethodHolder.info.invoke(this.logger, obj, e); + AccessLoggerUtils.appendLog(obj, "info"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void warn(CharSequence msg) { + if (this.logger != null && LoggerMethodHolder.warn != null) { + try { + LoggerMethodHolder.warn.invoke(this.logger, msg, null); + AccessLoggerUtils.appendLog(msg, "warn"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void warn(Object obj) { + if (this.logger != null && LoggerMethodHolder.warn != null) { + try { + LoggerMethodHolder.warn.invoke(this.logger, obj, null); + AccessLoggerUtils.appendLog(obj, "warn"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void warn(Object obj, Throwable e) { + if (this.logger != null && LoggerMethodHolder.warn != null) { + try { + LoggerMethodHolder.warn.invoke(this.logger, obj, e); + AccessLoggerUtils.appendLog(obj, "warn"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void error(CharSequence msg) { + if (this.logger != null && LoggerMethodHolder.error != null) { + try { + LoggerMethodHolder.error.invoke(this.logger, msg, null); + AccessLoggerUtils.appendLog(msg, "error"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void error(Object obj) { + if (this.logger != null && LoggerMethodHolder.error != null) { + try { + LoggerMethodHolder.error.invoke(this.logger, obj, null); + AccessLoggerUtils.appendLog(obj, "error"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void error(Object obj, Throwable e) { + if (this.logger != null && LoggerMethodHolder.error != null) { + try { + LoggerMethodHolder.error.invoke(this.logger, obj, e); + AccessLoggerUtils.appendLog(obj, "error"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void debug(CharSequence msg) { + if (this.logger != null && LoggerMethodHolder.debug != null) { + try { + LoggerMethodHolder.debug.invoke(this.logger, msg, null); + AccessLoggerUtils.appendLog(msg, "debug"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void debug(Object obj) { + if (this.logger != null && LoggerMethodHolder.debug != null) { + try { + LoggerMethodHolder.debug.invoke(this.logger, obj, null); + AccessLoggerUtils.appendLog(obj, "debug"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void debug(Object obj, Throwable e) { + if (this.logger != null && LoggerMethodHolder.debug != null) { + try { + LoggerMethodHolder.debug.invoke(this.logger, obj, e); + AccessLoggerUtils.appendLog(obj, "debug"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void trace(CharSequence msg) { + if (this.logger != null && LoggerMethodHolder.trace != null) { + try { + LoggerMethodHolder.trace.invoke(this.logger, msg, null); + AccessLoggerUtils.appendLog(msg, "trace"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void trace(Object obj) { + if (this.logger != null && LoggerMethodHolder.trace != null) { + try { + LoggerMethodHolder.trace.invoke(this.logger, obj, null); + AccessLoggerUtils.appendLog(obj, "trace"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void trace(Object obj, Throwable e) { + if (this.logger != null && LoggerMethodHolder.trace != null) { + try { + LoggerMethodHolder.trace.invoke(this.logger, obj, e); + AccessLoggerUtils.appendLog(obj, "trace"); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } + + public void accessRecord(Object obj) { + if (this.logger != null && LoggerMethodHolder.info != null) { + try { + LoggerMethodHolder.info.invoke(this.logger, obj, null); + } catch (Exception ex) { + ILOG.warning(ex.getMessage()); + } + } + } +} diff --git a/app/src/main/java/com/obs/log/BasicLogger.java b/app/src/main/java/com/obs/log/BasicLogger.java index 61d3d0b..952ad67 100644 --- a/app/src/main/java/com/obs/log/BasicLogger.java +++ b/app/src/main/java/com/obs/log/BasicLogger.java @@ -11,155 +11,146 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.log; +package com.obs.log; import com.obs.services.LogConfigurator; import com.obs.services.internal.utils.AccessLoggerUtils; -public class BasicLogger implements ILogger -{ +public class BasicLogger implements ILogger { private final java.util.logging.Logger logger; - - BasicLogger(java.util.logging.Logger logger){ + + BasicLogger(java.util.logging.Logger logger) { this.logger = logger; } - - public boolean isInfoEnabled(){ + + public boolean isInfoEnabled() { return this.logger.isLoggable(LogConfigurator.INFO); } - - public void info(CharSequence msg){ - if(msg != null){ + + public void info(CharSequence msg) { + if (msg != null) { this.logger.info(msg.toString()); AccessLoggerUtils.appendLog(msg, "info"); } } - - public void info(Object obj){ - if(obj != null){ + + public void info(Object obj) { + if (obj != null) { this.logger.info(obj.toString()); AccessLoggerUtils.appendLog(obj, "info"); } } - - public void info(Object obj, Throwable e){ - if(obj != null){ + + public void info(Object obj, Throwable e) { + if (obj != null) { this.logger.log(LogConfigurator.INFO, obj.toString(), e); AccessLoggerUtils.appendLog(obj, "info"); } } - public boolean isWarnEnabled() - { + public boolean isWarnEnabled() { return this.logger.isLoggable(LogConfigurator.WARN); } - public void warn(CharSequence msg) - { - if(msg != null){ + public void warn(CharSequence msg) { + if (msg != null) { this.logger.warning(msg.toString()); AccessLoggerUtils.appendLog(msg, "warn"); } } - - public void warn(Object obj){ - if(obj != null){ + + public void warn(Object obj) { + if (obj != null) { this.logger.warning(obj.toString()); AccessLoggerUtils.appendLog(obj, "warn"); } } - - public void warn(Object obj, Throwable e){ - if(obj != null){ + + public void warn(Object obj, Throwable e) { + if (obj != null) { this.logger.log(LogConfigurator.WARN, obj.toString(), e); AccessLoggerUtils.appendLog(obj, "warn"); } } - public boolean isErrorEnabled() - { + public boolean isErrorEnabled() { return this.logger.isLoggable(LogConfigurator.ERROR); } - public void error(CharSequence msg) - { - if(msg != null){ - this.logger.severe(msg.toString()); - AccessLoggerUtils.appendLog(msg, "error"); - } + public void error(CharSequence msg) { + if (msg != null) { + this.logger.severe(msg.toString()); + AccessLoggerUtils.appendLog(msg, "error"); + } } - - public void error(Object obj){ - if(obj != null){ + + public void error(Object obj) { + if (obj != null) { this.logger.severe(obj.toString()); AccessLoggerUtils.appendLog(obj, "error"); } } - - public void error(Object obj, Throwable e){ - if(obj != null){ + + public void error(Object obj, Throwable e) { + if (obj != null) { this.logger.log(LogConfigurator.ERROR, obj.toString(), e); AccessLoggerUtils.appendLog(obj, "error"); } } - public boolean isDebugEnabled() - { + public boolean isDebugEnabled() { return this.logger.isLoggable(LogConfigurator.DEBUG); } - public void debug(CharSequence msg) - { - if(msg != null){ + public void debug(CharSequence msg) { + if (msg != null) { this.logger.log(LogConfigurator.DEBUG, msg.toString()); AccessLoggerUtils.appendLog(msg, "debug"); } } - - public void debug(Object obj){ - if(obj != null){ + + public void debug(Object obj) { + if (obj != null) { this.logger.log(LogConfigurator.DEBUG, obj.toString()); AccessLoggerUtils.appendLog(obj, "debug"); } } - - public void debug(Object obj, Throwable e){ - if(obj != null){ - this.logger.log(LogConfigurator.DEBUG, obj.toString(),e); + + public void debug(Object obj, Throwable e) { + if (obj != null) { + this.logger.log(LogConfigurator.DEBUG, obj.toString(), e); AccessLoggerUtils.appendLog(obj, "debug"); } } - public boolean isTraceEnabled() - { + public boolean isTraceEnabled() { return this.logger.isLoggable(LogConfigurator.TRACE); } - public void trace(CharSequence msg) - { - if(msg != null){ + public void trace(CharSequence msg) { + if (msg != null) { this.logger.log(LogConfigurator.TRACE, msg.toString()); AccessLoggerUtils.appendLog(msg, "trace"); } } - - public void trace(Object obj){ - if(obj != null){ + + public void trace(Object obj) { + if (obj != null) { this.logger.log(LogConfigurator.TRACE, obj.toString()); AccessLoggerUtils.appendLog(obj, "trace"); } } - - public void trace(Object obj, Throwable e){ - if(obj != null){ - this.logger.log(LogConfigurator.TRACE, obj.toString(),e); + + public void trace(Object obj, Throwable e) { + if (obj != null) { + this.logger.log(LogConfigurator.TRACE, obj.toString(), e); AccessLoggerUtils.appendLog(obj, "trace"); } } public void accessRecord(Object obj) { - if(obj != null){ + if (obj != null) { this.logger.log(LogConfigurator.INFO, obj.toString()); } } diff --git a/app/src/main/java/com/obs/log/ILogger.java b/app/src/main/java/com/obs/log/ILogger.java index 5ae3e26..7ea606b 100644 --- a/app/src/main/java/com/obs/log/ILogger.java +++ b/app/src/main/java/com/obs/log/ILogger.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.log; diff --git a/app/src/main/java/com/obs/log/InterfaceLogBean.java b/app/src/main/java/com/obs/log/InterfaceLogBean.java index b974b6a..05682ff 100644 --- a/app/src/main/java/com/obs/log/InterfaceLogBean.java +++ b/app/src/main/java/com/obs/log/InterfaceLogBean.java @@ -11,106 +11,112 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.log; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; + /** - * 接口日志类,主要提供日志的格式化 + * Interface log class, which is used to format logs. * */ -public class InterfaceLogBean -{ +public class InterfaceLogBean { private static final String DATE_FMT_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss"; - + /** - * 唯一标识接口消息所属事务,不存在时为空 + * Unique ID of the transaction to which an interface message belongs. If no + * transaction ID exists, this parameter is left empty. */ private String transactionId; - + /** - * 填写接口所属的产品,如UC的接口填写UC。包括UC、IVS、TP、FusionSphere、Storage等 + * Enter the interface product. For example, enter UC for the UC interface. + * The products include UC, IVS, TP, FusionSphere, and Storage. */ private String product; - + /** - * 接口类型,值为1和2:其中1标识为北向接口;2标识为南向接口 + * Interface type. The value can be 1 or 2. 1: northbound interface. 2: + * southbound interface */ private String interfaceType; - + /** - * 协议类型,值为SOAP(细分ParlayX)、Rest、COM、Native、HTTP+XML,SMPP + * Protocol type. The options are SOAP (ParlayX), Rest, COM, Native, + * HTTP+XML, and SMPP. */ private String protocolType; - + /** - * 接口名称 + * Interface name */ private String name; - + /** - * 源端设备,客户端API类为空,参数不对外体现 + * Source device. The client API class is empty. The parameter is not + * displayed externally. */ private String sourceAddr; - + /** - * 宿端设备,客户端API类为空,参数不对外体现 + * Sink device. The API class of the client is empty. The parameter is not + * displayed externally. */ private String targetAddr; - + /** - * 北向接口收到请求的时间,南向接口发起请求的时间 + * Time when the northbound interface receives a request and time when the + * southbound interface sends a request. */ private Date reqTime; - + /** - * 格式为yyyy-MM-dd HH:mm:ss + * Time format: yyyy-MM-dd HH:mm:ss */ private String reqTimeAsString; - + /** - * 北向接口应答的时间,南向接口收到应答的时间 + * Response time of the northbound interface, and time when the southbound + * interface receives the response. */ private Date respTime; - + /** - * 格式为yyyy-MM-dd HH:mm:ss + * Time format: yyyy-MM-dd HH:mm:ss */ private String respTimeAsString; - + /** - * 请求参数,关键字需要用*替换 + * Request parameter. The keyword needs to be replaced with *. */ private String reqParams; - + /** - * 接口返回结果码 + * Result code returned by the interface */ private String resultCode; - + /** - * 应答参数,关键字需要用*替换 + * Response parameter. The keyword needs to be replaced with *. */ private String respParams; - public InterfaceLogBean(){} - /** - * 默认 - * InterfaceType 1, - * Product Storage, - * ProtocolType HTTP+XML, - * ReqTime 构造时间, - * sourceAddr 本地ip, - * transactionId 请求编号,可以是当前生成的UUID - * 响应信息需要在响应的时候设置 - * @param name 接口名称 - * @param targetAddr 目标主机IP - * @param reqParams 请求参数 + * Default InterfaceType 1, Product Storage, ProtocolType HTTP+XML, ReqTime + * Construction time, sourceAddr Local IP address, transactionId Request ID, + * which can be the currently generated UUID. Response information needs to + * be set when the response is returned. + * + * @param name + * Interface name + * @param targetAddr + * IP address of the target host + * @param requestParams + * Request parameters */ - public InterfaceLogBean(String name,String targetAddr, String reqParams) - { + public InterfaceLogBean(String name, String targetAddr, String reqParams) { this.transactionId = ""; this.interfaceType = "1"; this.product = "Storage"; @@ -121,187 +127,157 @@ public InterfaceLogBean(String name,String targetAddr, String reqParams) this.targetAddr = ""; this.reqParams = reqParams; } - + /** - * 设置响应信息
- * 响应时间默认设置为使用该方法的时间,如需另外设置,可以使用setRespTime方法设置 - * @param respParams 响应参数 - * @param resultCode 结果码 + * Configure the response information.
+ * The default response time is the time when the method is called. You can + * also call the setRespTime method to specify the response time. + * + * @param respParams + * Response parameters + * @param resultCode + * Result code * @return */ - public void setResponseInfo(String respParams,String resultCode) - { + public void setResponseInfo(String respParams, String resultCode) { this.respParams = respParams; this.resultCode = resultCode; } - - public String getTransactionId() - { + + public String getTransactionId() { return transactionId; } - public void setTransactionId(String transactionId) - { + public void setTransactionId(String transactionId) { this.transactionId = transactionId; } - public String getProduct() - { + public String getProduct() { return product; } - public void setProduct(String product) - { + public void setProduct(String product) { this.product = product; } - public String getInterfaceType() - { + public String getInterfaceType() { return interfaceType; } - public void setInterfaceType(String interfaceType) - { + public void setInterfaceType(String interfaceType) { this.interfaceType = interfaceType; } - public String getProtocolType() - { + public String getProtocolType() { return protocolType; } - public void setProtocolType(String protocolType) - { + public void setProtocolType(String protocolType) { this.protocolType = protocolType; } - public String getName() - { + public String getName() { return name; } - public void setName(String name) - { + public void setName(String name) { this.name = name; } - public String getSourceAddr() - { + public String getSourceAddr() { return sourceAddr; } - public void setSourceAddr(String sourceAddr) - { + public void setSourceAddr(String sourceAddr) { this.sourceAddr = sourceAddr; } - public String getTargetAddr() - { + public String getTargetAddr() { return targetAddr; } - public void setTargetAddr(String targetAddr) - { + public void setTargetAddr(String targetAddr) { this.targetAddr = targetAddr; } - public Date getReqTime() - { - if(null == this.reqTime) { - return null; - } else - { - return (Date) this.reqTime.clone(); - } + public Date getReqTime() { + if (null == this.reqTime) { + return null; + } else { + return (Date) this.reqTime.clone(); + } } - public void setReqTime(Date reqTime) - { - if (null != reqTime) { - this.reqTime = (Date) reqTime.clone(); - } + public void setReqTime(Date reqTime) { + if (null != reqTime) { + this.reqTime = (Date) reqTime.clone(); + } } - public String getReqTimeAsString() - { - //DATE_FMT_YYYYMMDDHHMMSS - if (null == reqTimeAsString && null != reqTime) - { + public String getReqTimeAsString() { + // DATE_FMT_YYYYMMDDHHMMSS + if (null == reqTimeAsString && null != reqTime) { DateFormat df = new SimpleDateFormat(DATE_FMT_YYYYMMDDHHMMSS); return df.format(reqTime); } return reqTimeAsString; } - public void setReqTimeAsString(String reqTimeAsString) - { + public void setReqTimeAsString(String reqTimeAsString) { this.reqTimeAsString = reqTimeAsString; } - public Date getRespTime() - { - if(null == this.respTime) { - return null; - } else - { - return (Date) this.respTime.clone(); - } + public Date getRespTime() { + if (null == this.respTime) { + return null; + } else { + return (Date) this.respTime.clone(); + } } - public void setRespTime(Date respTime) - { + public void setRespTime(Date respTime) { if (null != respTime) { - this.respTime = (Date) respTime.clone(); - } + this.respTime = (Date) respTime.clone(); + } } - public String getRespTimeAsString() - { - if (null == respTimeAsString && null != respTime) - { + public String getRespTimeAsString() { + if (null == respTimeAsString && null != respTime) { DateFormat df = new SimpleDateFormat(DATE_FMT_YYYYMMDDHHMMSS); return df.format(respTime); } return respTimeAsString; } - public void setRespTimeAsString(String respTimeAsString) - { + public void setRespTimeAsString(String respTimeAsString) { this.respTimeAsString = respTimeAsString; } - public String getReqParams() - { + public String getReqParams() { return reqParams; } - public void setReqParams(String reqParams) - { + public void setReqParams(String reqParams) { this.reqParams = reqParams; } - public String getResultCode() - { + public String getResultCode() { return resultCode; } - public void setResultCode(String resultCode) - { + public void setResultCode(String resultCode) { this.resultCode = resultCode; } - public String getRespParams() - { + public String getRespParams() { return respParams; } - public void setRespParams(String respParams) - { + public void setRespParams(String respParams) { this.respParams = respParams; } - public String toString() - { + public String toString() { StringBuilder sb = new StringBuilder(); sb.append(this.getProduct()).append("|"); sb.append(this.getInterfaceType()).append("|"); diff --git a/app/src/main/java/com/obs/log/Log4j2Logger.java b/app/src/main/java/com/obs/log/Log4j2Logger.java index e130ed0..55b8762 100644 --- a/app/src/main/java/com/obs/log/Log4j2Logger.java +++ b/app/src/main/java/com/obs/log/Log4j2Logger.java @@ -11,350 +11,105 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.log; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.logging.Logger; -import com.obs.services.internal.utils.AccessLoggerUtils; - -public class Log4j2Logger implements ILogger -{ - private final Object logger; +public class Log4j2Logger extends AbstractLog4jLogger implements ILogger { + private static final Logger ILOG = Logger.getLogger(Log4j2Logger.class.getName()); - private static class LoggerMethodHolder{ - private static Method info; - - private static Method warn; - - private static Method debug; - - private static Method trace; - - private static Method error; - + private static class Log4j2LoggerMethodHolder extends LoggerMethodHolder { private static Method isInfo; private static Method isDebug; private static Method isError; private static Method isWarn; private static Method isTrace; - static{ - try{ - if(LoggerBuilder.GetLoggerHolder.loggerClass != null){ - info = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("info", Object.class, Throwable.class); - warn = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("warn", Object.class, Throwable.class); - error = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("error", Object.class, Throwable.class); - debug = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("debug", Object.class, Throwable.class); - trace = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("trace", Object.class, Throwable.class); + + static { + try { + if (LoggerBuilder.GetLoggerHolder.loggerClass != null) { isInfo = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("isInfoEnabled"); isDebug = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("isDebugEnabled"); isError = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("isErrorEnabled"); isWarn = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("isWarnEnabled"); isTrace = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("isTraceEnabled"); } - } - catch (NoSuchMethodException | SecurityException e) - { - e.printStackTrace(); + } catch (NoSuchMethodException | SecurityException e) { + ILOG.warning(e.getMessage()); } } } - + private volatile int isInfoE = -1; private volatile int isDebugE = -1; private volatile int isErrorE = -1; private volatile int isWarnE = -1; private volatile int isTraceE = -1; - - Log4j2Logger(Object logger){ - this.logger = logger; - } - - public boolean isInfoEnabled(){ - if(isInfoE == -1) { - try - { - isInfoE = (this.logger != null && LoggerMethodHolder.isInfo != null && (Boolean)(LoggerMethodHolder.isInfo.invoke(this.logger))) ? 1 : 0; - } - catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) - { - isInfoE = 0; - } - } - return isInfoE == 1; - } - - public void info(CharSequence msg){ - if(this.logger != null && LoggerMethodHolder.info != null){ - try - { - LoggerMethodHolder.info.invoke(this.logger, msg, null); - AccessLoggerUtils.appendLog(msg, "info"); - } - catch (Exception ex) - { - } - } - } - - public void info(Object obj){ - if(this.logger != null && LoggerMethodHolder.info != null){ - try - { - LoggerMethodHolder.info.invoke(this.logger, obj, null); - AccessLoggerUtils.appendLog(obj, "info"); - } - catch (Exception ex) - { - } - } - } - - public void info(Object obj, Throwable e){ - if(this.logger != null && LoggerMethodHolder.info != null){ - try - { - LoggerMethodHolder.info.invoke(this.logger, obj, e); - AccessLoggerUtils.appendLog(obj, "info"); - } - catch (Exception ex) - { - } - } - } - - public boolean isWarnEnabled() - { - if(isWarnE == -1) { - try - { - isWarnE = (this.logger != null && LoggerMethodHolder.isWarn != null && (Boolean)(LoggerMethodHolder.isWarn.invoke(this.logger))) ? 1: 0; - } - catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) - { - isWarnE = 0; - } - } - return isWarnE == 1; + Log4j2Logger(Object logger) { + super(logger); } - public void warn(CharSequence msg) - { - if(this.logger != null && LoggerMethodHolder.warn != null){ - try - { - LoggerMethodHolder.warn.invoke(this.logger, msg, null); - AccessLoggerUtils.appendLog(msg, "warn"); - } - catch (Exception ex) - { - } - } - } - - public void warn(Object obj){ - if(this.logger != null && LoggerMethodHolder.warn != null){ - try - { - LoggerMethodHolder.warn.invoke(this.logger, obj, null); - AccessLoggerUtils.appendLog(obj, "warn"); - } - catch (Exception ex) - { - } - } - } - - public void warn(Object obj, Throwable e){ - if(this.logger != null && LoggerMethodHolder.warn != null){ - try - { - LoggerMethodHolder.warn.invoke(this.logger, obj, e); - AccessLoggerUtils.appendLog(obj, "warn"); - } - catch (Exception ex) - { + public boolean isInfoEnabled() { + if (isInfoE == -1) { + try { + isInfoE = (this.logger != null && Log4j2LoggerMethodHolder.isInfo != null + && (Boolean) (Log4j2LoggerMethodHolder.isInfo.invoke(this.logger))) ? 1 : 0; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + isInfoE = 0; } } + return isInfoE == 1; } - public boolean isErrorEnabled() - { - if(isErrorE == -1) { - try - { - isErrorE = (this.logger != null && LoggerMethodHolder.isError != null && (Boolean)(LoggerMethodHolder.isError.invoke(this.logger))) ? 1: 0; - } - catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) - { - isErrorE = 0; - } - } - return isErrorE == 1; - } - - public void error(CharSequence msg) - { - if(this.logger != null && LoggerMethodHolder.error != null){ - try - { - LoggerMethodHolder.error.invoke(this.logger, msg, null); - AccessLoggerUtils.appendLog(msg, "error"); - } - catch (Exception ex) - { - } - } - } - - public void error(Object obj){ - if(this.logger != null && LoggerMethodHolder.error != null){ - try - { - LoggerMethodHolder.error.invoke(this.logger, obj, null); - AccessLoggerUtils.appendLog(obj, "error"); - } - catch (Exception ex) - { - } - } - } - - public void error(Object obj, Throwable e){ - if(this.logger != null && LoggerMethodHolder.error != null){ - try - { - LoggerMethodHolder.error.invoke(this.logger, obj, e); - AccessLoggerUtils.appendLog(obj, "error"); - } - catch (Exception ex) - { + public boolean isWarnEnabled() { + if (isWarnE == -1) { + try { + isWarnE = (this.logger != null && Log4j2LoggerMethodHolder.isWarn != null + && (Boolean) (Log4j2LoggerMethodHolder.isWarn.invoke(this.logger))) ? 1 : 0; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + isWarnE = 0; } } + return isWarnE == 1; } - public boolean isDebugEnabled() - { - if(isDebugE == -1) { - try - { - isDebugE = (this.logger != null && LoggerMethodHolder.isDebug != null && (Boolean)(LoggerMethodHolder.isDebug.invoke(this.logger))) ? 1 : 0; - } - catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) - { - isDebugE = 0; - } - } - return isDebugE == 1; - } - - public void debug(CharSequence msg) - { - if(this.logger != null && LoggerMethodHolder.debug != null){ - try - { - LoggerMethodHolder.debug.invoke(this.logger, msg, null); - AccessLoggerUtils.appendLog(msg, "debug"); - } - catch (Exception ex) - { - } - } - } - - public void debug(Object obj){ - if(this.logger != null && LoggerMethodHolder.debug != null){ - try - { - LoggerMethodHolder.debug.invoke(this.logger, obj, null); - AccessLoggerUtils.appendLog(obj, "debug"); - } - catch (Exception ex) - { - } - } - } - - public void debug(Object obj, Throwable e){ - if(this.logger != null && LoggerMethodHolder.debug != null){ - try - { - LoggerMethodHolder.debug.invoke(this.logger, obj, e); - AccessLoggerUtils.appendLog(obj, "debug"); - } - catch (Exception ex) - { + public boolean isErrorEnabled() { + if (isErrorE == -1) { + try { + isErrorE = (this.logger != null && Log4j2LoggerMethodHolder.isError != null + && (Boolean) (Log4j2LoggerMethodHolder.isError.invoke(this.logger))) ? 1 : 0; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + isErrorE = 0; } } + return isErrorE == 1; } - public boolean isTraceEnabled() - { - if(isTraceE == -1) { - try - { - isTraceE = (this.logger != null && LoggerMethodHolder.isTrace != null && (Boolean)(LoggerMethodHolder.isTrace.invoke(this.logger))) ? 1: 0; - } - catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) - { - isTraceE = 0; - } - } - return isTraceE == 1; - } - - public void trace(CharSequence msg) - { - if(this.logger != null && LoggerMethodHolder.trace != null){ - try - { - LoggerMethodHolder.trace.invoke(this.logger, msg, null); - AccessLoggerUtils.appendLog(msg, "trace"); - } - catch (Exception ex) - { - } - } - } - - public void trace(Object obj){ - if(this.logger != null && LoggerMethodHolder.trace != null){ - try - { - LoggerMethodHolder.trace.invoke(this.logger, obj, null); - AccessLoggerUtils.appendLog(obj, "trace"); - } - catch (Exception ex) - { - } - } - } - - public void trace(Object obj, Throwable e){ - if(this.logger != null && LoggerMethodHolder.trace != null){ - try - { - LoggerMethodHolder.trace.invoke(this.logger, obj, e); - AccessLoggerUtils.appendLog(obj, "trace"); - } - catch (Exception ex) - { + public boolean isDebugEnabled() { + if (isDebugE == -1) { + try { + isDebugE = (this.logger != null && Log4j2LoggerMethodHolder.isDebug != null + && (Boolean) (Log4j2LoggerMethodHolder.isDebug.invoke(this.logger))) ? 1 : 0; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + isDebugE = 0; } } + return isDebugE == 1; } - public void accessRecord(Object obj) - { - if(this.logger != null && LoggerMethodHolder.info != null){ - try - { - LoggerMethodHolder.info.invoke(this.logger, obj, null); - } - catch (Exception ex) - { + public boolean isTraceEnabled() { + if (isTraceE == -1) { + try { + isTraceE = (this.logger != null && Log4j2LoggerMethodHolder.isTrace != null + && (Boolean) (Log4j2LoggerMethodHolder.isTrace.invoke(this.logger))) ? 1 : 0; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + isTraceE = 0; } } + return isTraceE == 1; } - } diff --git a/app/src/main/java/com/obs/log/Log4jLogger.java b/app/src/main/java/com/obs/log/Log4jLogger.java index 5c84409..e788449 100644 --- a/app/src/main/java/com/obs/log/Log4jLogger.java +++ b/app/src/main/java/com/obs/log/Log4jLogger.java @@ -11,27 +11,17 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.log; -import com.obs.services.internal.utils.AccessLoggerUtils; +package com.obs.log; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.logging.Logger; -public class Log4jLogger implements ILogger { - private final Object logger; - - private static class LoggerMethodHolder { - private static Method info; - - private static Method warn; - - private static Method debug; - - private static Method trace; - - private static Method error; - +public class Log4jLogger extends AbstractLog4jLogger implements ILogger { + private static final Logger ILOG = Logger.getLogger(Log4jLogger.class.getName()); + + private static class Log4jLoggerMethodHolder extends LoggerMethodHolder { private static Method isEnabledFor; private static Class priority; @@ -42,15 +32,10 @@ private static class LoggerMethodHolder { private static Object errorLevel; private static Object warnLevel; private static Object traceLevel; + static { try { if (LoggerBuilder.GetLoggerHolder.loggerClass != null) { - info = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("info", Object.class, Throwable.class); - warn = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("warn", Object.class, Throwable.class); - error = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("error", Object.class, Throwable.class); - debug = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("debug", Object.class, Throwable.class); - trace = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("trace", Object.class, Throwable.class); - priority = Class.forName("org.apache.log4j.Priority"); isEnabledFor = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("isEnabledFor", priority); @@ -61,245 +46,64 @@ private static class LoggerMethodHolder { warnLevel = level.getField("WARN").get(level); traceLevel = level.getField("TRACE").get(level); } - } catch (ClassNotFoundException | NoSuchMethodException | SecurityException - | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) { - e.printStackTrace(); - } + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalArgumentException + | IllegalAccessException | NoSuchFieldException e) { + ILOG.warning(e.getMessage()); + } } } - private volatile int isInfoE = -1; - private volatile int isDebugE = -1; - private volatile int isErrorE = -1; - private volatile int isWarnE = -1; - private volatile int isTraceE = -1; - Log4jLogger(Object logger) { - this.logger = logger; + super(logger); } public boolean isInfoEnabled() { - if (isInfoE == -1) { - try { - isInfoE = (this.logger != null && LoggerMethodHolder.infoLevel != null - && (Boolean) (LoggerMethodHolder.isEnabledFor.invoke(this.logger, - LoggerMethodHolder.infoLevel))) ? 1 : 0; - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - isInfoE = 0; - } - } - return isInfoE == 1; - } - - public void info(CharSequence msg) { - if (this.logger != null && LoggerMethodHolder.info != null) { - try { - LoggerMethodHolder.info.invoke(this.logger, msg, null); - AccessLoggerUtils.appendLog(msg, "info"); - } catch (Exception ex) { - } - } - } - - public void info(Object obj) { - if (this.logger != null && LoggerMethodHolder.info != null) { - try { - LoggerMethodHolder.info.invoke(this.logger, obj, null); - AccessLoggerUtils.appendLog(obj, "info"); - } catch (Exception ex) { - } - } - } - - public void info(Object obj, Throwable e) { - if (this.logger != null && LoggerMethodHolder.info != null) { - try { - LoggerMethodHolder.info.invoke(this.logger, obj, e); - AccessLoggerUtils.appendLog(obj, "info"); - } catch (Exception ex) { - } + try { + return this.logger != null && Log4jLoggerMethodHolder.infoLevel != null + && (Boolean) (Log4jLoggerMethodHolder.isEnabledFor.invoke(this.logger, + Log4jLoggerMethodHolder.infoLevel)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + return false; } } public boolean isWarnEnabled() { - if (isWarnE == -1) { - try { - isWarnE = (this.logger != null && LoggerMethodHolder.warnLevel != null - && (Boolean) (LoggerMethodHolder.isEnabledFor.invoke(this.logger, - LoggerMethodHolder.warnLevel))) ? 1 : 0; - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - isWarnE = 0; - } - } - return isWarnE == 1; - } - - public void warn(CharSequence msg) { - if (this.logger != null && LoggerMethodHolder.warn != null) { - try { - LoggerMethodHolder.warn.invoke(this.logger, msg, null); - AccessLoggerUtils.appendLog(msg, "warn"); - } catch (Exception ex) { - } - } - } - - public void warn(Object obj) { - if (this.logger != null && LoggerMethodHolder.warn != null) { - try { - LoggerMethodHolder.warn.invoke(this.logger, obj, null); - AccessLoggerUtils.appendLog(obj, "warn"); - } catch (Exception ex) { - } - } - } - - public void warn(Object obj, Throwable e) { - if (this.logger != null && LoggerMethodHolder.warn != null) { - try { - LoggerMethodHolder.warn.invoke(this.logger, obj, e); - AccessLoggerUtils.appendLog(obj, "warn"); - } catch (Exception ex) { - } + try { + return this.logger != null && Log4jLoggerMethodHolder.warnLevel != null + && (Boolean) (Log4jLoggerMethodHolder.isEnabledFor.invoke(this.logger, + Log4jLoggerMethodHolder.warnLevel)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + return false; } } public boolean isErrorEnabled() { - if (isErrorE == -1) { - try { - isErrorE = (this.logger != null && LoggerMethodHolder.errorLevel != null - && (Boolean) (LoggerMethodHolder.isEnabledFor.invoke(this.logger, - LoggerMethodHolder.errorLevel))) ? 1 : 0; - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - isErrorE = 0; - } - } - return isErrorE == 1; - } - - public void error(CharSequence msg) { - if (this.logger != null && LoggerMethodHolder.error != null) { - try { - LoggerMethodHolder.error.invoke(this.logger, msg, null); - AccessLoggerUtils.appendLog(msg, "error"); - } catch (Exception ex) { - } - } - } - - public void error(Object obj) { - if (this.logger != null && LoggerMethodHolder.error != null) { - try { - LoggerMethodHolder.error.invoke(this.logger, obj, null); - AccessLoggerUtils.appendLog(obj, "error"); - } catch (Exception ex) { - } - } - } - - public void error(Object obj, Throwable e) { - if (this.logger != null && LoggerMethodHolder.error != null) { - try { - LoggerMethodHolder.error.invoke(this.logger, obj, e); - AccessLoggerUtils.appendLog(obj, "error"); - } catch (Exception ex) { - } + try { + return this.logger != null && Log4jLoggerMethodHolder.errorLevel != null + && (Boolean) (Log4jLoggerMethodHolder.isEnabledFor.invoke(this.logger, + Log4jLoggerMethodHolder.errorLevel)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + return false; } } public boolean isDebugEnabled() { - if (isDebugE == -1) { - try { - isDebugE = (this.logger != null && LoggerMethodHolder.debugLevel != null - && (Boolean) (LoggerMethodHolder.isEnabledFor.invoke(this.logger, - LoggerMethodHolder.debugLevel))) ? 1 : 0; - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - isDebugE = 0; - } - } - return isDebugE == 1; - } - - public void debug(CharSequence msg) { - if (this.logger != null && LoggerMethodHolder.debug != null) { - try { - LoggerMethodHolder.debug.invoke(this.logger, msg, null); - AccessLoggerUtils.appendLog(msg, "debug"); - } catch (Exception ex) { - } - } - } - - public void debug(Object obj) { - if (this.logger != null && LoggerMethodHolder.debug != null) { - try { - LoggerMethodHolder.debug.invoke(this.logger, obj, null); - AccessLoggerUtils.appendLog(obj, "debug"); - } catch (Exception ex) { - } - } - } - - public void debug(Object obj, Throwable e) { - if (this.logger != null && LoggerMethodHolder.debug != null) { - try { - LoggerMethodHolder.debug.invoke(this.logger, obj, e); - AccessLoggerUtils.appendLog(obj, "debug"); - } catch (Exception ex) { - } + try { + return this.logger != null && Log4jLoggerMethodHolder.debugLevel != null + && (Boolean) (Log4jLoggerMethodHolder.isEnabledFor.invoke(this.logger, + Log4jLoggerMethodHolder.debugLevel)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + return false; } } public boolean isTraceEnabled() { - if (isTraceE == -1) { - try { - isTraceE = (this.logger != null && LoggerMethodHolder.traceLevel != null - && (Boolean) (LoggerMethodHolder.isEnabledFor.invoke(this.logger, - LoggerMethodHolder.traceLevel))) ? 1 : 0; - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - isTraceE = 0; - } + try { + return this.logger != null && Log4jLoggerMethodHolder.traceLevel != null + && (Boolean) (Log4jLoggerMethodHolder.isEnabledFor.invoke(this.logger, + Log4jLoggerMethodHolder.traceLevel)); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + return false; } - return isTraceE == 1; } - - public void trace(CharSequence msg) { - if (this.logger != null && LoggerMethodHolder.trace != null) { - try { - LoggerMethodHolder.trace.invoke(this.logger, msg, null); - AccessLoggerUtils.appendLog(msg, "trace"); - } catch (Exception ex) { - } - } - } - - public void trace(Object obj) { - if (this.logger != null && LoggerMethodHolder.trace != null) { - try { - LoggerMethodHolder.trace.invoke(this.logger, obj, null); - AccessLoggerUtils.appendLog(obj, "trace"); - } catch (Exception ex) { - } - } - } - - public void trace(Object obj, Throwable e) { - if (this.logger != null && LoggerMethodHolder.trace != null) { - try { - LoggerMethodHolder.trace.invoke(this.logger, obj, e); - AccessLoggerUtils.appendLog(obj, "trace"); - } catch (Exception ex) { - } - } - } - - public void accessRecord(Object obj) { - if (this.logger != null && LoggerMethodHolder.info != null) { - try { - LoggerMethodHolder.info.invoke(this.logger, obj, null); - } catch (Exception ex) { - } - } - } - } diff --git a/app/src/main/java/com/obs/log/Logger.java b/app/src/main/java/com/obs/log/Logger.java index 8b0d4dd..3302907 100644 --- a/app/src/main/java/com/obs/log/Logger.java +++ b/app/src/main/java/com/obs/log/Logger.java @@ -11,146 +11,126 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.log; -public class Logger implements ILogger -{ +public class Logger implements ILogger { private final ILogger delegate; - - Logger(Object logger){ + + Logger(Object logger) { Class loggerCls = LoggerBuilder.GetLoggerHolder.loggerClass; - if(logger instanceof java.util.logging.Logger) { - this.delegate = new BasicLogger((java.util.logging.Logger)logger); - } else if (loggerCls != null && loggerCls.getName().equals("org.apache.log4j.Logger")){ - this.delegate = new Log4jLogger(logger); - } else { - this.delegate = new Log4j2Logger(logger); - } + if (logger instanceof java.util.logging.Logger) { + this.delegate = new BasicLogger((java.util.logging.Logger) logger); + } else if (loggerCls != null && loggerCls.getName().equals("org.apache.log4j.Logger")) { + this.delegate = new Log4jLogger(logger); + } else { + this.delegate = new Log4j2Logger(logger); + } } - @Override - public boolean isInfoEnabled() { - return this.delegate.isInfoEnabled(); - } - - - @Override - public void info(CharSequence msg) { - this.delegate.info(msg); - } - - - @Override - public void info(Object obj) { - this.delegate.info(obj); - } - - - @Override - public void info(Object obj, Throwable e) { - this.delegate.info(obj, e); - } - - - @Override - public boolean isWarnEnabled() { - return this.delegate.isWarnEnabled(); - } - - - @Override - public void warn(CharSequence msg) { - this.delegate.warn(msg); - } - - - @Override - public void warn(Object obj) { - this.delegate.warn(obj); - } - - - @Override - public void warn(Object obj, Throwable e) { - this.delegate.warn(obj, e); - } - - - @Override - public boolean isErrorEnabled() { - return this.delegate.isErrorEnabled(); - } - - - @Override - public void error(CharSequence msg) { - this.delegate.error(msg); - } - - - @Override - public void error(Object obj) { - this.delegate.error(obj); - } + @Override + public boolean isInfoEnabled() { + return this.delegate.isInfoEnabled(); + } + @Override + public void info(CharSequence msg) { + this.delegate.info(msg); + } - @Override - public void error(Object obj, Throwable e) { - this.delegate.error(obj, e); - } + @Override + public void info(Object obj) { + this.delegate.info(obj); + } + @Override + public void info(Object obj, Throwable e) { + this.delegate.info(obj, e); + } - @Override - public boolean isDebugEnabled() { - return this.delegate.isDebugEnabled(); - } + @Override + public boolean isWarnEnabled() { + return this.delegate.isWarnEnabled(); + } + @Override + public void warn(CharSequence msg) { + this.delegate.warn(msg); + } - @Override - public void debug(CharSequence msg) { - this.delegate.debug(msg); - } + @Override + public void warn(Object obj) { + this.delegate.warn(obj); + } + @Override + public void warn(Object obj, Throwable e) { + this.delegate.warn(obj, e); + } - @Override - public void debug(Object obj) { - this.delegate.debug(obj); - } + @Override + public boolean isErrorEnabled() { + return this.delegate.isErrorEnabled(); + } + @Override + public void error(CharSequence msg) { + this.delegate.error(msg); + } - @Override - public void debug(Object obj, Throwable e) { - this.delegate.debug(obj, e); - } + @Override + public void error(Object obj) { + this.delegate.error(obj); + } + @Override + public void error(Object obj, Throwable e) { + this.delegate.error(obj, e); + } - @Override - public boolean isTraceEnabled() { - return this.delegate.isTraceEnabled(); - } + @Override + public boolean isDebugEnabled() { + return this.delegate.isDebugEnabled(); + } + @Override + public void debug(CharSequence msg) { + this.delegate.debug(msg); + } - @Override - public void trace(CharSequence msg) { - this.delegate.trace(msg); - } + @Override + public void debug(Object obj) { + this.delegate.debug(obj); + } + @Override + public void debug(Object obj, Throwable e) { + this.delegate.debug(obj, e); + } - @Override - public void trace(Object obj) { - this.delegate.trace(obj); - } + @Override + public boolean isTraceEnabled() { + return this.delegate.isTraceEnabled(); + } + @Override + public void trace(CharSequence msg) { + this.delegate.trace(msg); + } - @Override - public void trace(Object obj, Throwable e) { - this.delegate.trace(obj, e); - } + @Override + public void trace(Object obj) { + this.delegate.trace(obj); + } - @Override - public void accessRecord(Object obj) { - this.delegate.accessRecord(obj); - } + @Override + public void trace(Object obj, Throwable e) { + this.delegate.trace(obj, e); + } + @Override + public void accessRecord(Object obj) { + this.delegate.accessRecord(obj); + } } diff --git a/app/src/main/java/com/obs/log/LoggerBuilder.java b/app/src/main/java/com/obs/log/LoggerBuilder.java index 9e88fab..3a363d3 100644 --- a/app/src/main/java/com/obs/log/LoggerBuilder.java +++ b/app/src/main/java/com/obs/log/LoggerBuilder.java @@ -11,49 +11,54 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.log; import java.lang.reflect.Method; -public class LoggerBuilder -{ +public class LoggerBuilder { + + private static final java.util.logging.Logger ILOG = + java.util.logging.Logger.getLogger(LoggerBuilder.class.getName()); - static class GetLoggerHolder{ + static class GetLoggerHolder { static Class logManagerClass; static Class loggerClass; static Method getLoggerClass; - static{ - try{ + + static { + try { logManagerClass = Class.forName("org.apache.logging.log4j.LogManager"); loggerClass = Class.forName("org.apache.logging.log4j.Logger"); getLoggerClass = GetLoggerHolder.logManagerClass.getMethod("getLogger", String.class); - }catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) { - try { - loggerClass = Class.forName("org.apache.log4j.Logger"); - getLoggerClass = GetLoggerHolder.loggerClass.getMethod("getLogger", String.class); - } catch (NoSuchMethodException | SecurityException | ClassNotFoundException ex) { - try { - loggerClass = Class.forName("java.util.logging.Logger"); - getLoggerClass = GetLoggerHolder.loggerClass.getMethod("getLogger", String.class); - } catch (NoSuchMethodException | SecurityException | ClassNotFoundException exx) { - - } - } + } catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) { + try { + loggerClass = Class.forName("org.apache.log4j.Logger"); + getLoggerClass = GetLoggerHolder.loggerClass.getMethod("getLogger", String.class); + } catch (NoSuchMethodException | SecurityException | ClassNotFoundException ex) { + try { + loggerClass = Class.forName("java.util.logging.Logger"); + getLoggerClass = GetLoggerHolder.loggerClass.getMethod("getLogger", String.class); + } catch (NoSuchMethodException | SecurityException | ClassNotFoundException exx) { + ILOG.warning(exx.getMessage()); + } + } } } } - - public static ILogger getLogger(String name){ - if(GetLoggerHolder.getLoggerClass != null){ - try{ - return new Logger(((Method)GetLoggerHolder.getLoggerClass).invoke(null, name)); - }catch (Exception e) { + + public static ILogger getLogger(String name) { + if (GetLoggerHolder.getLoggerClass != null) { + try { + return new Logger(((Method) GetLoggerHolder.getLoggerClass).invoke(null, name)); + } catch (Exception e) { + ILOG.warning(e.getMessage()); } } return new Logger(null); } - - public static ILogger getLogger(Class c){ + + public static ILogger getLogger(Class c) { return getLogger(c.getName()); } } diff --git a/app/src/main/java/com/obs/log/LoggerMethodHolder.java b/app/src/main/java/com/obs/log/LoggerMethodHolder.java new file mode 100644 index 0000000..3e32ca4 --- /dev/null +++ b/app/src/main/java/com/obs/log/LoggerMethodHolder.java @@ -0,0 +1,46 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.log; + +import java.lang.reflect.Method; +import java.util.logging.Logger; + +public class LoggerMethodHolder { + private static final Logger ILOG = Logger.getLogger(LoggerMethodHolder.class.getName()); + + static Method info; + + static Method warn; + + static Method debug; + + static Method trace; + + static Method error; + + static { + try { + if (LoggerBuilder.GetLoggerHolder.loggerClass != null) { + info = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("info", Object.class, Throwable.class); + warn = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("warn", Object.class, Throwable.class); + error = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("error", Object.class, Throwable.class); + debug = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("debug", Object.class, Throwable.class); + trace = LoggerBuilder.GetLoggerHolder.loggerClass.getMethod("trace", Object.class, Throwable.class); + } + } catch (NoSuchMethodException | SecurityException e) { + ILOG.warning(e.getMessage()); + } + } +} diff --git a/app/src/main/java/com/obs/services/AbstractBatchClient.java b/app/src/main/java/com/obs/services/AbstractBatchClient.java new file mode 100644 index 0000000..a7f6e52 --- /dev/null +++ b/app/src/main/java/com/obs/services/AbstractBatchClient.java @@ -0,0 +1,407 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services; + +import java.io.File; +import java.io.IOException; +import java.util.Date; +import java.util.LinkedList; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.task.DefaultTaskProgressStatus; +import com.obs.services.internal.task.LazyTaksCallback; +import com.obs.services.internal.task.PutObjectTask; +import com.obs.services.internal.task.RestoreObjectTask; +import com.obs.services.internal.task.ResumableUploadTask; +import com.obs.services.internal.task.UploadTaskProgressStatus; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.ExtensionObjectPermissionEnum; +import com.obs.services.model.KeyAndVersion; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ListVersionsRequest; +import com.obs.services.model.ListVersionsResult; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObsObject; +import com.obs.services.model.ProgressListener; +import com.obs.services.model.ProgressStatus; +import com.obs.services.model.PutObjectBasicRequest; +import com.obs.services.model.PutObjectRequest; +import com.obs.services.model.PutObjectResult; +import com.obs.services.model.PutObjectsRequest; +import com.obs.services.model.RestoreObjectRequest; +import com.obs.services.model.RestoreObjectResult; +import com.obs.services.model.RestoreObjectsRequest; +import com.obs.services.model.SseCHeader; +import com.obs.services.model.SseKmsHeader; +import com.obs.services.model.StorageClassEnum; +import com.obs.services.model.TaskCallback; +import com.obs.services.model.TaskProgressListener; +import com.obs.services.model.TaskProgressStatus; +import com.obs.services.model.UploadFileRequest; +import com.obs.services.model.UploadObjectsProgressListener; +import com.obs.services.model.UploadProgressStatus; +import com.obs.services.model.VersionOrDeleteMarker; + +public abstract class AbstractBatchClient extends AbstractFileClient { + private static final ILogger ILOG = LoggerBuilder.getLogger(AbstractBatchClient.class); + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#restoreObjects(com.obs.services.model. + * RestoreObjectsRequest) + */ + @Override + public TaskProgressStatus restoreObjects(RestoreObjectsRequest request) throws ObsException { + checkRestoreObjectsRequest(request); + DefaultTaskProgressStatus progreStatus = new DefaultTaskProgressStatus(); + ThreadPoolExecutor executor = this.initThreadPool(request); + + try { + TaskCallback callback; + TaskProgressListener listener; + callback = (request.getCallback() == null) + ? new LazyTaksCallback() : request.getCallback(); + listener = request.getProgressListener(); + int progressInterval = request.getProgressInterval(); + int totalTasks = 0; + if (request.getKeyAndVersions() != null) { + totalTasks = request.getKeyAndVersions().size(); + for (KeyAndVersion kv : request.getKeyAndVersions()) { + RestoreObjectRequest taskRequest = new RestoreObjectRequest(request.getBucketName(), kv.getKey(), + kv.getVersion(), request.getDays(), request.getRestoreTier()); + taskRequest.setRequesterPays(request.isRequesterPays()); + RestoreObjectTask task = new RestoreObjectTask(this, request.getBucketName(), taskRequest, + callback, listener, + progreStatus, progressInterval); + executor.execute(task); + } + } else { + if (request.isVersionRestored()) { + totalTasks = restoreVersions(request, progreStatus, executor, callback, listener, progressInterval); + } else { + totalTasks = restoreCurrentObject(request, progreStatus, executor, callback, listener, + progressInterval); + } + } + + progreStatus.setTotalTaskNum(totalTasks); + executor.shutdown(); + executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); + } catch (ObsException e) { + throw e; + } catch (Exception e) { + throw new ObsException(e.getMessage(), e); + } + return progreStatus; + } + + private int restoreCurrentObject(RestoreObjectsRequest request, DefaultTaskProgressStatus progreStatus, + ThreadPoolExecutor executor, TaskCallback callback, + TaskProgressListener listener, int progressInterval) { + int totalTasks = 0; + ObjectListing objectsResult; + ListObjectsRequest listObjectsRequest = new ListObjectsRequest(request.getBucketName()); + listObjectsRequest.setRequesterPays(request.isRequesterPays()); + listObjectsRequest.setPrefix(request.getPrefix()); + listObjectsRequest.setEncodingType(request.getEncodingType()); + do { + objectsResult = this.listObjects(listObjectsRequest); + for (ObsObject o : objectsResult.getObjects()) { + if (o.getMetadata().getObjectStorageClass() == StorageClassEnum.COLD) { + totalTasks++; + RestoreObjectRequest taskRequest = new RestoreObjectRequest(request.getBucketName(), + o.getObjectKey(), null, request.getDays(), request.getRestoreTier()); + submitRestoreTask(request, progreStatus, executor, callback, listener, progressInterval, totalTasks, + taskRequest); + } + } + listObjectsRequest.setMarker(objectsResult.getNextMarker()); + } while (objectsResult.isTruncated()); + return totalTasks; + } + + private void submitRestoreTask(RestoreObjectsRequest request, DefaultTaskProgressStatus progreStatus, + ThreadPoolExecutor executor, TaskCallback callback, + TaskProgressListener listener, int progressInterval, int totalTasks, RestoreObjectRequest taskRequest) { + taskRequest.setRequesterPays(request.isRequesterPays()); + + RestoreObjectTask task = new RestoreObjectTask(this, request.getBucketName(), taskRequest, callback, + listener, progreStatus, progressInterval); + executor.execute(task); + if (ILOG.isInfoEnabled()) { + if (totalTasks % 1000 == 0) { + ILOG.info("RestoreObjects: " + totalTasks + + " tasks have submitted to restore objects"); + } + } + } + + private int restoreVersions(RestoreObjectsRequest request, DefaultTaskProgressStatus progreStatus, + ThreadPoolExecutor executor, TaskCallback callback, + TaskProgressListener listener, int progressInterval) { + int totalTasks = 0; + ListVersionsResult versionResult; + ListVersionsRequest listRequest = new ListVersionsRequest(request.getBucketName()); + listRequest.setRequesterPays(request.isRequesterPays()); + listRequest.setEncodingType(request.getEncodingType()); + + listRequest.setPrefix(request.getPrefix()); + do { + versionResult = this.listVersions(listRequest); + for (VersionOrDeleteMarker v : versionResult.getVersions()) { + if (v.getObjectStorageClass() == StorageClassEnum.COLD) { + totalTasks++; + RestoreObjectRequest taskRequest = new RestoreObjectRequest(request.getBucketName(), v.getKey(), + v.getVersionId(), request.getDays(), request.getRestoreTier()); + submitRestoreTask(request, progreStatus, executor, callback, listener, progressInterval, totalTasks, + taskRequest); + } + } + listRequest.setKeyMarker(versionResult.getNextKeyMarker()); + listRequest.setVersionIdMarker(versionResult.getNextVersionIdMarker()); + } while (versionResult.isTruncated()); + return totalTasks; + } + + private void checkRestoreObjectsRequest(RestoreObjectsRequest request) { + ServiceUtils.assertParameterNotNull(request, "RestoreObjectsRequest is null"); + if (!this.isCname()) { + ServiceUtils.assertParameterNotNull(request.getBucketName(), "bucketName is null"); + } + + if (request.getKeyAndVersions() != null && request.getPrefix() != null) { + throw new IllegalArgumentException("Prefix and keyandVersions cannot coexist in the same request"); + } + + int days = request.getDays(); + if (!(days >= 1 && days <= 30)) { + throw new IllegalArgumentException("Restoration days should be at least 1 and at most 30"); + } + } + + @Override + public UploadProgressStatus putObjects(final PutObjectsRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "PutObjectsRequest is null"); + + ThreadPoolExecutor executor = this.initThreadPool(request); + Date now = new Date(); + UploadTaskProgressStatus progressStatus = new UploadTaskProgressStatus(request.getTaskProgressInterval(), now); + + try { + UploadObjectsProgressListener listener = request.getUploadObjectsProgressListener(); + TaskCallback callback = (request.getCallback() == null) + ? new LazyTaksCallback() : request.getCallback(); + String prefix = request.getPrefix() == null ? "" : request.getPrefix(); + + int totalTasks = 0; + if (request.getFolderPath() != null) { + totalTasks = uploadFolder(request, executor, progressStatus, listener, + callback, prefix); + } else if (request.getFilePaths() != null) { + totalTasks = uploadFileLists(request, executor, progressStatus, listener, + callback, prefix); + } + + progressStatus.setTotalTaskNum(totalTasks); + executor.shutdown(); + executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); + } catch (ObsException e) { + throw e; + } catch (Exception e) { + throw new ObsException(e.getMessage(), e); + } + + return progressStatus; + } + + /** + * upload folder + * @param request + * @param executor + * @param progressStatus + * @param listener + * @param callback + * @param prefix + * @return + * @throws IOException + */ + private int uploadFolder(final PutObjectsRequest request, ThreadPoolExecutor executor, + UploadTaskProgressStatus progressStatus, UploadObjectsProgressListener listener, + TaskCallback callback, String prefix) + throws IOException { + int totalTasks = 0; + String folderPath = request.getFolderPath(); + File fileRoot = new File(folderPath); + + if (!fileRoot.exists()) { + String erroInfo = "putObjects: the folder \"" + folderPath + "\" dose not exist"; + ILOG.warn(erroInfo); + throw new ObsException(erroInfo); + } + + if (!fileRoot.isDirectory()) { + String erroInfo = "putObjects: the folder \"" + folderPath + "\" dose not a folder"; + ILOG.warn(erroInfo); + throw new ObsException(erroInfo); + } + + String folderRoot = fileRoot.getName(); + LinkedList list = new LinkedList(); + list.add(fileRoot); + File[] files = fileRoot.listFiles(); + File tempFile; + while (!list.isEmpty()) { + tempFile = list.removeFirst(); + if (null == tempFile) { + continue; + } + files = tempFile.listFiles(); + if (null == files) { + continue; + } + for (File file : files) { + if (file.isDirectory()) { + if (!file.exists()) { + String filePath = file.getCanonicalPath(); + String erroInfo = "putObjects: the folder \"" + filePath + + "\" dose not a folder"; + ILOG.warn(erroInfo); + } else { + list.add(file); + } + } else { + // 文件上传 + String filePath = file.getCanonicalPath(); + if (!file.exists()) { + ILOG.warn("putObjects: the file \"" + filePath + "\" dose not exist"); + continue; + } + totalTasks++; + String objectKey = prefix + folderRoot + filePath + .substring(folderPath.length(), filePath.length()).replace("\\", "/"); + uploadObjectTask(request, filePath, objectKey, executor, progressStatus, callback, + listener); + } + } + } + + return totalTasks; + } + + /** + * upload by file lists + * @param request + * @param executor + * @param progressStatus + * @param listener + * @param callback + * @param prefix + * @return + */ + private int uploadFileLists(final PutObjectsRequest request, ThreadPoolExecutor executor, + UploadTaskProgressStatus progressStatus, UploadObjectsProgressListener listener, + TaskCallback callback, String prefix) { + int totalTasks = 0; + for (String filePath : request.getFilePaths()) { + File file = new File(filePath); + if (file.exists()) { + totalTasks++; + String objectKey = prefix + file.getName(); + uploadObjectTask(request, filePath, objectKey, executor, progressStatus, callback, listener); + } else { + ILOG.warn("putObjects: the file \"" + filePath + "\" is not exist"); + } + } + return totalTasks; + } + + private void uploadObjectTask(final PutObjectsRequest request, final String filePath, final String objectKey, + final ThreadPoolExecutor executor, final UploadTaskProgressStatus progressStatus, + final TaskCallback callback, + final UploadObjectsProgressListener listener) { + File fileObject = new File(filePath); + String bucketName = request.getBucketName(); + int progressInterval = request.getProgressInterval(); + int taskNum = request.getTaskNum(); + long detailProgressInterval = request.getDetailProgressInterval(); + long bigfileThreshold = request.getBigfileThreshold(); + long partSize = request.getPartSize(); + AccessControlList acl = request.getAcl(); + Map> extensionPermissionMap = request.getExtensionPermissionMap(); + SseCHeader sseCHeader = request.getSseCHeader(); + SseKmsHeader sseKmsHeader = request.getSseKmsHeader(); + String successRedirectLocation = request.getSuccessRedirectLocation(); + + if (fileObject.length() > bigfileThreshold) { + UploadFileRequest taskRequest = + new UploadFileRequest(bucketName, objectKey, filePath, partSize, taskNum, true); + + taskRequest.setExtensionPermissionMap(extensionPermissionMap); + taskRequest.setAcl(acl); + taskRequest.setSuccessRedirectLocation(successRedirectLocation); + taskRequest.setSseCHeader(sseCHeader); + taskRequest.setSseKmsHeader(sseKmsHeader); + progressStatus.addTotalSize(fileObject.length()); + taskRequest.setRequesterPays(request.isRequesterPays()); + + taskRequest.setProgressListener(createNewProgressListener(objectKey, progressStatus, listener)); + taskRequest.setProgressInterval(detailProgressInterval); + + ResumableUploadTask task = new ResumableUploadTask(this, bucketName, taskRequest, callback, listener, + progressStatus, progressInterval); + executor.execute(task); + } else { + PutObjectRequest taskRequest = new PutObjectRequest(bucketName, objectKey, fileObject); + taskRequest.setExtensionPermissionMap(extensionPermissionMap); + taskRequest.setAcl(acl); + taskRequest.setSuccessRedirectLocation(successRedirectLocation); + taskRequest.setSseCHeader(sseCHeader); + taskRequest.setSseKmsHeader(sseKmsHeader); + progressStatus.addTotalSize(fileObject.length()); + taskRequest.setRequesterPays(request.isRequesterPays()); + + taskRequest.setProgressListener(createNewProgressListener(objectKey, progressStatus, listener)); + taskRequest.setProgressInterval(detailProgressInterval); + PutObjectTask task = new PutObjectTask(this, bucketName, taskRequest, callback, listener, progressStatus, + progressInterval); + executor.execute(task); + } + } + + private ProgressListener createNewProgressListener(final String objectKey, + final UploadTaskProgressStatus progressStatus, final UploadObjectsProgressListener listener) { + return new ProgressListener() { + @Override + public void progressChanged(ProgressStatus status) { + progressStatus.putTaskTable(objectKey, status); + if (progressStatus.isRefreshprogress()) { + Date dateNow = new Date(); + long totalMilliseconds = dateNow.getTime() - progressStatus.getStartDate().getTime(); + progressStatus.setTotalMilliseconds(totalMilliseconds); + listener.progressChanged(progressStatus); + } + } + }; + } +} diff --git a/app/src/main/java/com/obs/services/AbstractBucketAdvanceClient.java b/app/src/main/java/com/obs/services/AbstractBucketAdvanceClient.java new file mode 100644 index 0000000..5c5ccc6 --- /dev/null +++ b/app/src/main/java/com/obs/services/AbstractBucketAdvanceClient.java @@ -0,0 +1,785 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services; + +import com.obs.services.exception.ObsException; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.BaseBucketRequest; +import com.obs.services.model.BucketDirectColdAccess; +import com.obs.services.model.BucketEncryption; +import com.obs.services.model.BucketNotificationConfiguration; +import com.obs.services.model.BucketPolicyResponse; +import com.obs.services.model.BucketTagInfo; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.LifecycleConfiguration; +import com.obs.services.model.ReplicationConfiguration; +import com.obs.services.model.SetBucketDirectColdAccessRequest; +import com.obs.services.model.SetBucketEncryptionRequest; +import com.obs.services.model.SetBucketLifecycleRequest; +import com.obs.services.model.SetBucketNotificationRequest; +import com.obs.services.model.SetBucketPolicyRequest; +import com.obs.services.model.SetBucketReplicationRequest; +import com.obs.services.model.SetBucketTaggingRequest; +import com.obs.services.model.SetBucketWebsiteRequest; +import com.obs.services.model.WebsiteConfiguration; +import com.obs.services.model.fs.GetBucketFSStatusRequest; +import com.obs.services.model.fs.GetBucketFSStatusResult; +import com.obs.services.model.fs.SetBucketFSStatusRequest; + +public abstract class AbstractBucketAdvanceClient extends AbstractBucketClient { + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketLifecycle(java.lang.String) + */ + @Override + public LifecycleConfiguration getBucketLifecycle(final String bucketName) throws ObsException { + return this.getBucketLifecycle(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketLifecycle(java.lang.String) + */ + @Override + public LifecycleConfiguration getBucketLifecycle(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("getBucketLifecycleConfiguration", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public LifecycleConfiguration action() throws ServiceException { + return AbstractBucketAdvanceClient.this.getBucketLifecycleConfigurationImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketLifecycle(java.lang.String, + * com.obs.services.model.LifecycleConfiguration) + */ + @Override + public HeaderResponse setBucketLifecycle(final String bucketName, final LifecycleConfiguration lifecycleConfig) + throws ObsException { + return this.setBucketLifecycle(new SetBucketLifecycleRequest(bucketName, lifecycleConfig)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketLifecycle(java.lang.String, + * com.obs.services.model.LifecycleConfiguration) + */ + @Override + public HeaderResponse setBucketLifecycle(final SetBucketLifecycleRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketLifecycleRequest is null"); + ServiceUtils.assertParameterNotNull(request.getLifecycleConfig(), "LifecycleConfiguration is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("setBucketLifecycleConfiguration", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.setBucketLifecycleConfigurationImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketLifecycle(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketLifecycle(final String bucketName) throws ObsException { + return this.deleteBucketLifecycle(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketLifecycle(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketLifecycle(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("deleteBucketLifecycleConfiguration", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.deleteBucketLifecycleConfigurationImpl(request); + } + }); + + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketPolicy(java.lang.String) + */ + @Override + public String getBucketPolicy(final String bucketName) throws ObsException { + return this.getBucketPolicyV2(new BaseBucketRequest(bucketName)).getPolicy(); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketPolicy(java.lang.String) + */ + @Override + public String getBucketPolicy(final BaseBucketRequest request) throws ObsException { + return this.getBucketPolicyV2(request).getPolicy(); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketPolicyV2(java.lang.String) + */ + @Override + public BucketPolicyResponse getBucketPolicyV2(final String bucketName) throws ObsException { + return getBucketPolicyV2(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketPolicyV2(java.lang.String) + */ + @Override + public BucketPolicyResponse getBucketPolicyV2(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("getBucketPolicy", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public BucketPolicyResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.getBucketPolicyImpl(request); + } + + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketPolicy(java.lang.String, + * java.lang.String) + */ + @Override + public HeaderResponse setBucketPolicy(final String bucketName, final String policy) throws ObsException { + return setBucketPolicy(new SetBucketPolicyRequest(bucketName, policy)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketPolicy(java.lang.String, + * java.lang.String) + */ + @Override + public HeaderResponse setBucketPolicy(final SetBucketPolicyRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketPolicyRequest is null"); + ServiceUtils.assertParameterNotNull(request.getPolicy(), "policy is null"); + return this.doActionWithResult("setBucketPolicy", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.setBucketPolicyImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketPolicy(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketPolicy(final String bucketName) throws ObsException { + return deleteBucketPolicy(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketPolicy(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketPolicy(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("deleteBucketPolicy", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.deleteBucketPolicyImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketWebsite(java.lang.String) + */ + @Override + public WebsiteConfiguration getBucketWebsite(final String bucketName) throws ObsException { + return getBucketWebsite(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketWebsite(java.lang.String) + */ + @Override + public WebsiteConfiguration getBucketWebsite(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("getBucketWebsiteConfiguration", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public WebsiteConfiguration action() throws ServiceException { + return AbstractBucketAdvanceClient.this.getBucketWebsiteConfigurationImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketWebsite(java.lang.String, + * com.obs.services.model.WebsiteConfiguration) + */ + @Override + public HeaderResponse setBucketWebsite(final String bucketName, final WebsiteConfiguration websiteConfig) + throws ObsException { + return setBucketWebsite(new SetBucketWebsiteRequest(bucketName, websiteConfig)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketWebsite(java.lang.String, + * com.obs.services.model.WebsiteConfiguration) + */ + @Override + public HeaderResponse setBucketWebsite(final SetBucketWebsiteRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketWebsiteRequest is null"); + ServiceUtils.assertParameterNotNull(request.getWebsiteConfig(), "WebsiteConfiguration is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + + return this.doActionWithResult("setBucketWebsiteConfiguration", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + + return AbstractBucketAdvanceClient.this.setBucketWebsiteConfigurationImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketWebsite(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketWebsite(final String bucketName) throws ObsException { + return deleteBucketWebsite(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketWebsite(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketWebsite(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("deleteBucketWebsiteConfiguration", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.deleteBucketWebsiteConfigurationImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketTagging(java.lang.String) + */ + @Override + public BucketTagInfo getBucketTagging(final String bucketName) throws ObsException { + return getBucketTagging(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketTagging(java.lang.String) + */ + @Override + public BucketTagInfo getBucketTagging(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("getBucketTagging", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public BucketTagInfo action() throws ServiceException { + return AbstractBucketAdvanceClient.this.getBucketTaggingImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketTagging(java.lang.String, + * com.obs.services.model.BucketTagInfo) + */ + @Override + public HeaderResponse setBucketTagging(final String bucketName, final BucketTagInfo bucketTagInfo) + throws ObsException { + return setBucketTagging(new SetBucketTaggingRequest(bucketName, bucketTagInfo)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketTagging(java.lang.String, + * com.obs.services.model.BucketTagInfo) + */ + @Override + public HeaderResponse setBucketTagging(final SetBucketTaggingRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketTaggingRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("setBucketTagging", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + + return AbstractBucketAdvanceClient.this.setBucketTaggingImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketTagging(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketTagging(final String bucketName) throws ObsException { + return deleteBucketTagging(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketTagging(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketTagging(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + + return this.doActionWithResult("deleteBucketTagging", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.deleteBucketTaggingImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketEncryption(java.lang.String) + */ + @Override + public BucketEncryption getBucketEncryption(final String bucketName) throws ObsException { + return this.getBucketEncryption(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketEncryption(java.lang.String) + */ + @Override + public BucketEncryption getBucketEncryption(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + return this.doActionWithResult("getBucketEncryption", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public BucketEncryption action() throws ServiceException { + return AbstractBucketAdvanceClient.this.getBucketEncryptionImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketEncryption(java.lang.String, + * com.obs.services.model.BucketEncryption) + */ + @Override + public HeaderResponse setBucketEncryption(final String bucketName, final BucketEncryption bucketEncryption) + throws ObsException { + return this.setBucketEncryption(new SetBucketEncryptionRequest(bucketName, bucketEncryption)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketEncryption(java.lang.String, + * com.obs.services.model.BucketEncryption) + */ + @Override + public HeaderResponse setBucketEncryption(final SetBucketEncryptionRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketEncryptionRequest is null"); + ServiceUtils.assertParameterNotNull(request.getBucketEncryption(), + "SetBucketEncryptionRequest.bucketEncryption is null"); + return this.doActionWithResult("setBucketEncryption", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.setBucketEncryptionImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketEncryption(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketEncryption(final String bucketName) throws ObsException { + return this.deleteBucketEncryption(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketEncryption(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketEncryption(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + return this.doActionWithResult("deleteBucketEncryption", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.deleteBucketEncryptionImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketReplication(java.lang.String, + * com.obs.services.model.ReplicationConfiguration) + */ + @Override + public HeaderResponse setBucketReplication(final String bucketName, + final ReplicationConfiguration replicationConfiguration) throws ObsException { + return setBucketReplication(new SetBucketReplicationRequest(bucketName, replicationConfiguration)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketReplication(java.lang.String, + * com.obs.services.model.ReplicationConfiguration) + */ + @Override + public HeaderResponse setBucketReplication(final SetBucketReplicationRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketReplicationRequest is null"); + ServiceUtils.assertParameterNotNull(request.getReplicationConfiguration(), "ReplicationConfiguration is null"); + ServiceUtils.assertParameterNotNull(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("setBucketReplication", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.setBucketReplicationConfigurationImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketReplication(java.lang.String) + */ + @Override + public ReplicationConfiguration getBucketReplication(final String bucketName) throws ObsException { + return getBucketReplication(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketReplication(java.lang.String) + */ + @Override + public ReplicationConfiguration getBucketReplication(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("getBucketReplicationConfiguration", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public ReplicationConfiguration action() throws ServiceException { + return AbstractBucketAdvanceClient.this.getBucketReplicationConfigurationImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#deleteBucketReplication(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketReplication(final String bucketName) throws ObsException { + return deleteBucketReplication(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#deleteBucketReplication(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketReplication(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + + return this.doActionWithResult("deleteBucketReplicationConfiguration", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.deleteBucketReplicationConfigurationImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketNotification(java.lang.String) + */ + @Override + public BucketNotificationConfiguration getBucketNotification(final String bucketName) throws ObsException { + return this.getBucketNotification(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketNotification(java.lang.String) + */ + @Override + public BucketNotificationConfiguration getBucketNotification(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + return this.doActionWithResult("getBucketNotification", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public BucketNotificationConfiguration action() throws ServiceException { + return AbstractBucketAdvanceClient.this.getBucketNotificationConfigurationImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketNotification(java.lang.String, + * com.obs.services.model.BucketNotificationConfiguration) + */ + @Override + public HeaderResponse setBucketNotification(final String bucketName, + final BucketNotificationConfiguration bucketNotificationConfiguration) throws ObsException { + return this + .setBucketNotification(new SetBucketNotificationRequest(bucketName, bucketNotificationConfiguration)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketNotification(java.lang.String, + * com.obs.services.model.BucketNotificationConfiguration) + */ + @Override + public HeaderResponse setBucketNotification(final SetBucketNotificationRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketNotificationRequest is null"); + return this.doActionWithResult("setBucketNotification", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + if (null == request.getBucketNotificationConfiguration()) { + request.setBucketNotificationConfiguration(new BucketNotificationConfiguration()); + } + return AbstractBucketAdvanceClient.this.setBucketNotificationImpl(request); + } + }); + } + + @Override + public HeaderResponse setBucketFSStatus(final SetBucketFSStatusRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketFileInterfaceRequest is null"); + return this.doActionWithResult("setBucketFSStatus", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.setBucketFSStatusImpl(request); + } + }); + } + + @Override + public GetBucketFSStatusResult getBucketFSStatus(final GetBucketFSStatusRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "GetBucketFSStatusRequest is null"); + return this.doActionWithResult("getBucketFSStatus", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public GetBucketFSStatusResult action() throws ServiceException { + return AbstractBucketAdvanceClient.this.getBucketMetadataImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketDirectColdAccess() + */ + @Override + public HeaderResponse setBucketDirectColdAccess(final String bucketName, final BucketDirectColdAccess access) + throws ObsException { + return this.setBucketDirectColdAccess(new SetBucketDirectColdAccessRequest(bucketName, access)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketDirectColdAccess() + */ + @Override + public HeaderResponse setBucketDirectColdAccess(final SetBucketDirectColdAccessRequest request) + throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketDirectColdAccessRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + ServiceUtils.assertParameterNotNull(request.getAccess(), "bucketDirectColdAccess is null"); + + return this.doActionWithResult("setBucketDirectColdAccess", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.setBucketDirectColdAccessImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketDirectColdAccess() + */ + @Override + public BucketDirectColdAccess getBucketDirectColdAccess(final String bucketName) throws ObsException { + return this.getBucketDirectColdAccess(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketDirectColdAccess() + */ + @Override + public BucketDirectColdAccess getBucketDirectColdAccess(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("getBucketDirectColdAccess", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public BucketDirectColdAccess action() throws ServiceException { + return AbstractBucketAdvanceClient.this.getBucketDirectColdAccessImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketDirectColdAccess() + */ + @Override + public HeaderResponse deleteBucketDirectColdAccess(final String bucketName) throws ObsException { + return this.deleteBucketDirectColdAccess(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketDirectColdAccess() + */ + @Override + public HeaderResponse deleteBucketDirectColdAccess(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("deleteBucketDirectColdAccess", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketAdvanceClient.this.deleteBucketDirectColdAccessImpl(request); + } + }); + } +} diff --git a/app/src/main/java/com/obs/services/AbstractBucketClient.java b/app/src/main/java/com/obs/services/AbstractBucketClient.java new file mode 100644 index 0000000..b2b73c9 --- /dev/null +++ b/app/src/main/java/com/obs/services/AbstractBucketClient.java @@ -0,0 +1,868 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services; + +import java.util.List; + +import com.obs.services.exception.ObsException; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.BaseBucketRequest; +import com.obs.services.model.BucketCors; +import com.obs.services.model.BucketCustomDomainInfo; +import com.obs.services.model.BucketLocationResponse; +import com.obs.services.model.BucketLoggingConfiguration; +import com.obs.services.model.BucketMetadataInfoRequest; +import com.obs.services.model.BucketMetadataInfoResult; +import com.obs.services.model.BucketQuota; +import com.obs.services.model.BucketStorageInfo; +import com.obs.services.model.BucketStoragePolicyConfiguration; +import com.obs.services.model.BucketVersioningConfiguration; +import com.obs.services.model.CreateBucketRequest; +import com.obs.services.model.DeleteBucketCustomDomainRequest; +import com.obs.services.model.GetBucketCustomDomainRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.ListBucketsRequest; +import com.obs.services.model.ListBucketsResult; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.RequestPaymentConfiguration; +import com.obs.services.model.RequestPaymentEnum; +import com.obs.services.model.SetBucketAclRequest; +import com.obs.services.model.SetBucketCorsRequest; +import com.obs.services.model.SetBucketCustomDomainRequest; +import com.obs.services.model.SetBucketLoggingRequest; +import com.obs.services.model.SetBucketQuotaRequest; +import com.obs.services.model.SetBucketRequestPaymentRequest; +import com.obs.services.model.SetBucketStoragePolicyRequest; +import com.obs.services.model.SetBucketVersioningRequest; + +public abstract class AbstractBucketClient extends AbstractDeprecatedBucketClient { + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#createBucket(java.lang.String) + */ + @Override + public ObsBucket createBucket(String bucketName) throws ObsException { + ObsBucket obsBucket = new ObsBucket(); + obsBucket.setBucketName(bucketName); + return this.createBucket(obsBucket); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#createBucket(java.lang.String, + * java.lang.String) + */ + @Override + public ObsBucket createBucket(String bucketName, String location) throws ObsException { + ObsBucket obsBucket = new ObsBucket(); + obsBucket.setBucketName(bucketName); + obsBucket.setLocation(location); + return this.createBucket(obsBucket); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#createBucket(com.obs.services.model. + * ObsBucket) + */ + @Override + public ObsBucket createBucket(final ObsBucket bucket) throws ObsException { + CreateBucketRequest request = new CreateBucketRequest(); + request.setBucketName(bucket.getBucketName()); + request.setAcl(bucket.getAcl()); + request.setBucketStorageClass(bucket.getBucketStorageClass()); + request.setLocation(bucket.getLocation()); + return this.createBucket(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#createBucket(com.obs.services.model. + * CreateBucketRequest) + */ + @Override + public ObsBucket createBucket(final CreateBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "CreateBucketRequest is null"); + return this.doActionWithResult("createBucket", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public ObsBucket action() throws ServiceException { + if (isCname()) { + throw new ServiceException("createBucket is not allowed in customdomain mode"); + } + try { + return AbstractBucketClient.this.createBucketImpl(request); + } catch (ServiceException e) { + if (AbstractBucketClient.this.isAuthTypeNegotiation() && e.getResponseCode() == 400 + && "Unsupported Authorization Type".equals(e.getErrorMessage()) + && AbstractBucketClient.this.getProviderCredentials() + .getLocalAuthType(request.getBucketName()) == AuthTypeEnum.OBS) { + AbstractBucketClient.this.getProviderCredentials() + .setLocalAuthType(request.getBucketName(), AuthTypeEnum.V2); + return AbstractBucketClient.this.createBucketImpl(request); + } else { + throw e; + } + } + } + + @Override + void authTypeNegotiate(String bucketName) throws ServiceException { + AuthTypeEnum authTypeEnum = AbstractBucketClient.this.getProviderCredentials() + .getLocalAuthType().get(bucketName); + if (authTypeEnum == null) { + authTypeEnum = AbstractBucketClient.this.getApiVersion(""); + AbstractBucketClient.this.getProviderCredentials() + .setLocalAuthType(bucketName, authTypeEnum); + } + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#listBuckets(com.obs.services.model. + * ListBucketsRequest) + */ + @Override + public List listBuckets(final ListBucketsRequest request) throws ObsException { + return this.listBucketsV2(request).getBuckets(); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#listBucketsV2(com.obs.services.model. + * ListBucketsRequest) + */ + @Override + public ListBucketsResult listBucketsV2(final ListBucketsRequest request) throws ObsException { + return this.doActionWithResult("listBuckets", "All Buckets", new ActionCallbackWithResult() { + @Override + public ListBucketsResult action() throws ServiceException { + if (isCname()) { + throw new ServiceException("listBuckets is not allowed in customdomain mode"); + } + return AbstractBucketClient.this.listAllBucketsImpl(request); + } + + @Override + void authTypeNegotiate(String bucketName) throws ServiceException { + AuthTypeEnum authTypeEnum = AbstractBucketClient.this.getProviderCredentials() + .getLocalAuthType().get(bucketName); + if (authTypeEnum == null) { + authTypeEnum = AbstractBucketClient.this.getApiVersion(""); + AbstractBucketClient.this.getProviderCredentials().setLocalAuthType(bucketName, authTypeEnum); + } + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#headBucket(java.lang.String) + */ + @Override + public boolean headBucket(final String bucketName) throws ObsException { + return headBucket(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#headBucket(java.lang.String) + */ + @Override + public boolean headBucket(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("headBucket", request.getBucketName(), new ActionCallbackWithResult() { + + @Override + public Boolean action() throws ServiceException { + return AbstractBucketClient.this.headBucketImpl(request); + } + + @Override + void authTypeNegotiate(String bucketName) throws ServiceException { + try { + AuthTypeEnum authTypeEnum = AbstractBucketClient.this.getProviderCredentials() + .getLocalAuthType().get(bucketName); + if (authTypeEnum == null) { + authTypeEnum = AbstractBucketClient.this.getApiVersion(bucketName); + AbstractBucketClient.this.getProviderCredentials().setLocalAuthType(bucketName, authTypeEnum); + } + } catch (ServiceException e) { + if (e.getResponseCode() != 404) { + throw e; + } + } + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucket(java.lang.String) + */ + @Override + public HeaderResponse deleteBucket(final String bucketName) throws ObsException { + return deleteBucket(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucket(java.lang.String) + */ + @Override + public HeaderResponse deleteBucket(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("deleteBucket", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketClient.this.deleteBucketImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#getBucketMetadata(com.obs.services.model. + * BucketMetadataInfoRequest) + */ + @Override + public BucketMetadataInfoResult getBucketMetadata(final BucketMetadataInfoRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BucketMetadataInfoRequest is null"); + return this.doActionWithResult("getBucketMetadata", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public BucketMetadataInfoResult action() throws ServiceException { + return AbstractBucketClient.this.getBucketMetadataImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketAcl(java.lang.String) + */ + @Override + public AccessControlList getBucketAcl(final String bucketName) throws ObsException { + return getBucketAcl(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketAcl(java.lang.String) + */ + @Override + public AccessControlList getBucketAcl(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("getBucketAcl", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public AccessControlList action() throws ServiceException { + return AbstractBucketClient.this.getBucketAclImpl(request); + } + + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketAcl(java.lang.String, + * com.obs.services.model.AccessControlList) + */ + @Override + public HeaderResponse setBucketAcl(final String bucketName, final AccessControlList acl) throws ObsException { + return setBucketAcl(new SetBucketAclRequest(bucketName, acl)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketAcl(java.lang.String, + * com.obs.services.model.AccessControlList) + */ + @Override + public HeaderResponse setBucketAcl(final SetBucketAclRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketAclRequest is null"); + return this.doActionWithResult("setBucketAcl", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + if (request.getAcl() == null && null == request.getCannedACL()) { + throw new IllegalArgumentException("Both CannedACL and AccessControlList is null"); + } + return AbstractBucketClient.this.setBucketAclImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketLocation(java.lang.String) + */ + @Override + public String getBucketLocation(final String bucketName) throws ObsException { + return this.getBucketLocation(new BaseBucketRequest(bucketName)).getLocation(); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketLocationV2(java.lang.String) + */ + @Override + public BucketLocationResponse getBucketLocation(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + return this.doActionWithResult("getBucketLocation", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public BucketLocationResponse action() throws ServiceException { + return AbstractBucketClient.this.getBucketLocationImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketLocationV2(java.lang.String) + */ + @Override + public BucketLocationResponse getBucketLocationV2(final String bucketName) throws ObsException { + return this.getBucketLocation(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketStorageInfo(java.lang.String) + */ + @Override + public BucketStorageInfo getBucketStorageInfo(final String bucketName) throws ObsException { + return this.getBucketStorageInfo(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketStorageInfo(java.lang.String) + */ + @Override + public BucketStorageInfo getBucketStorageInfo(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("getBucketStorageInfo", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public BucketStorageInfo action() throws ServiceException { + return AbstractBucketClient.this.getBucketStorageInfoImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketQuota(java.lang.String) + */ + @Override + public BucketQuota getBucketQuota(final String bucketName) throws ObsException { + return this.getBucketQuota(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketQuota(java.lang.String) + */ + @Override + public BucketQuota getBucketQuota(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + return this.doActionWithResult("getBucketQuota", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public BucketQuota action() throws ServiceException { + return AbstractBucketClient.this.getBucketQuotaImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketQuota(java.lang.String, + * com.obs.services.model.BucketQuota) + */ + @Override + public HeaderResponse setBucketQuota(final String bucketName, final BucketQuota bucketQuota) throws ObsException { + return this.setBucketQuota(new SetBucketQuotaRequest(bucketName, bucketQuota)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketQuota(java.lang.String, + * com.obs.services.model.BucketQuota) + */ + @Override + public HeaderResponse setBucketQuota(final SetBucketQuotaRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketQuotaRequest is null"); + ServiceUtils.assertParameterNotNull(request.getBucketQuota(), + "The bucket '" + request.getBucketName() + "' does not include Quota information"); + return this.doActionWithResult("setBucketQuota", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketClient.this.setBucketQuotaImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketStoragePolicy(java.lang.String) + */ + @Override + public BucketStoragePolicyConfiguration getBucketStoragePolicy(final String bucketName) throws ObsException { + return this.getBucketStoragePolicy(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketStoragePolicy(java.lang.String) + */ + @Override + public BucketStoragePolicyConfiguration getBucketStoragePolicy(final BaseBucketRequest request) + throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + return this.doActionWithResult("getBucketStoragePolicy", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public BucketStoragePolicyConfiguration action() throws ServiceException { + return AbstractBucketClient.this.getBucketStoragePolicyImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketStoragePolicy(java.lang.String, + * com.obs.services.model.BucketStoragePolicyConfiguration) + */ + @Override + public HeaderResponse setBucketStoragePolicy(final String bucketName, + final BucketStoragePolicyConfiguration bucketStorage) throws ObsException { + return this.setBucketStoragePolicy(new SetBucketStoragePolicyRequest(bucketName, bucketStorage)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketStoragePolicy(java.lang.String, + * com.obs.services.model.BucketStoragePolicyConfiguration) + */ + @Override + public HeaderResponse setBucketStoragePolicy(final SetBucketStoragePolicyRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketStoragePolicyRequest is null"); + ServiceUtils.assertParameterNotNull(request.getBucketStorage(), + "The bucket '" + request.getBucketName() + "' does not include storagePolicy information"); + + return this.doActionWithResult("setBucketStoragePolicy", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketClient.this.setBucketStorageImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketCors(java.lang.String, + * com.obs.services.model.BucketCors) + */ + @Override + public HeaderResponse setBucketCors(final String bucketName, final BucketCors bucketCors) throws ObsException { + return this.setBucketCors(new SetBucketCorsRequest(bucketName, bucketCors)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketCors(java.lang.String, + * com.obs.services.model.BucketCors) + */ + @Override + public HeaderResponse setBucketCors(final SetBucketCorsRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketCorsRequest is null"); + return this.doActionWithResult("setBucketCors", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketClient.this.setBucketCorsImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketCors(java.lang.String) + */ + @Override + public BucketCors getBucketCors(final String bucketName) throws ObsException { + return this.getBucketCors(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketCors(java.lang.String) + */ + @Override + public BucketCors getBucketCors(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + return this.doActionWithResult("getBucketCors", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public BucketCors action() throws ServiceException { + return AbstractBucketClient.this.getBucketCorsImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketCors(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketCors(final String bucketName) throws ObsException { + return this.deleteBucketCors(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteBucketCors(java.lang.String) + */ + @Override + public HeaderResponse deleteBucketCors(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + return this.doActionWithResult("deleteBucketCors", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketClient.this.deleteBucketCorsImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketLogging(java.lang.String) + */ + @Override + public BucketLoggingConfiguration getBucketLogging(final String bucketName) throws ObsException { + return getBucketLogging(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketLogging(java.lang.String) + */ + @Override + public BucketLoggingConfiguration getBucketLogging(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + return this.doActionWithResult("getBucketLoggingConfiguration", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public BucketLoggingConfiguration action() throws ServiceException { + return AbstractBucketClient.this.getBucketLoggingConfigurationImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketLoggingConfiguration(java.lang. + * String, com.obs.services.model.BucketLoggingConfiguration, boolean) + */ + @Override + public HeaderResponse setBucketLoggingConfiguration(final String bucketName, + final BucketLoggingConfiguration loggingConfiguration, final boolean updateTargetACLifRequired) + throws ObsException { + return this.setBucketLogging( + new SetBucketLoggingRequest(bucketName, loggingConfiguration, updateTargetACLifRequired)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketLogging(java.lang.String, + * com.obs.services.model.BucketLoggingConfiguration) + */ + @Override + public HeaderResponse setBucketLogging(final String bucketName, + final BucketLoggingConfiguration loggingConfiguration) throws ObsException { + return this.setBucketLogging(new SetBucketLoggingRequest(bucketName, loggingConfiguration)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketLogging(java.lang.String, + * com.obs.services.model.BucketLoggingConfiguration) + */ + @Override + public HeaderResponse setBucketLogging(final SetBucketLoggingRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketLoggingRequest is null"); + return this.doActionWithResult("setBucketLoggingConfiguration", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketClient.this.setBucketLoggingConfigurationImpl(request); + } + }); + } + + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketVersioning(java.lang.String, + * com.obs.services.model.BucketVersioningConfiguration) + */ + @Override + public HeaderResponse setBucketVersioning(final String bucketName, + final BucketVersioningConfiguration versioningConfiguration) throws ObsException { + return setBucketVersioning( + new SetBucketVersioningRequest(bucketName, versioningConfiguration.getVersioningStatus())); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketVersioning(java.lang.String, + * com.obs.services.model.BucketVersioningConfiguration) + */ + @Override + public HeaderResponse setBucketVersioning(final SetBucketVersioningRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketVersioningRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + + return this.doActionWithResult("setBucketVersioning", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketClient.this.setBucketVersioningImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketVersioning(java.lang.String) + */ + @Override + public BucketVersioningConfiguration getBucketVersioning(final String bucketName) throws ObsException { + return getBucketVersioning(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getBucketVersioning(java.lang.String) + */ + @Override + public BucketVersioningConfiguration getBucketVersioning(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("getBucketVersioning", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public BucketVersioningConfiguration action() throws ServiceException { + return AbstractBucketClient.this.getBucketVersioningImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setBucketRequestPayment() + */ + @Override + public HeaderResponse setBucketRequestPayment(String bucketName, RequestPaymentEnum payer) throws ObsException { + return setBucketRequestPayment(new SetBucketRequestPaymentRequest(bucketName, payer)); + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#setBucketRequestPayment(java.lang.String, + * com.obs.services.model.BucketVersioningConfiguration) + */ + @Override + public HeaderResponse setBucketRequestPayment(final SetBucketRequestPaymentRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetBucketRequestPaymentRequest is null"); + return this.doActionWithResult("setBucketRequestPayment", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + ServiceUtils.assertParameterNotNull(request.getPayer(), "payer is null"); + return AbstractBucketClient.this.setBucketRequestPaymentImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#getBucketRequestPayment(java.lang.String) + */ + public RequestPaymentConfiguration getBucketRequestPayment(String bucketName) throws ObsException { + return getBucketRequestPayment(new BaseBucketRequest(bucketName)); + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#getBucketRequestPayment(java.lang.String) + */ + @Override + public RequestPaymentConfiguration getBucketRequestPayment(final BaseBucketRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("getBucketRequestPayment", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public RequestPaymentConfiguration action() throws ServiceException { + return AbstractBucketClient.this.getBucketRequestPaymentImpl(request); + } + }); + } + + @Override + public HeaderResponse deleteBucketCustomDomain(String bucketName, String domainName) throws ObsException { + return deleteBucketCustomDomain(new DeleteBucketCustomDomainRequest(bucketName, domainName)); + } + + @Override + public HeaderResponse deleteBucketCustomDomain(DeleteBucketCustomDomainRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "request is null"); + ServiceUtils.assertParameterNotNull(request.getBucketName(), "bucketName is null"); + ServiceUtils.assertParameterNotNull2(request.getDomainName(), "domainName is null"); + + + return this.doActionWithResult("setBucketCustomDomain", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketClient.this.deleteBucketCustomDomainImpl(request); + } + }); + } + + @Override + public BucketCustomDomainInfo getBucketCustomDomain(String bucketName) throws ObsException { + return getBucketCustomDomain(new GetBucketCustomDomainRequest(bucketName)); + } + + @Override + public BucketCustomDomainInfo getBucketCustomDomain(final GetBucketCustomDomainRequest request) + throws ObsException { + ServiceUtils.assertParameterNotNull(request, "BaseBucketRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getBucketName(), "bucketName is null"); + return this.doActionWithResult("getBucketCustomDomain", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public BucketCustomDomainInfo action() throws ServiceException { + return AbstractBucketClient.this.getBucketCustomDomainImpl(request); + } + }); + } + + @Override + public HeaderResponse setBucketCustomDomain(String bucketName, String domainName) throws ObsException { + return setBucketCustomDomain(new SetBucketCustomDomainRequest(bucketName, domainName)); + } + + @Override + public HeaderResponse setBucketCustomDomain(SetBucketCustomDomainRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "request is null"); + ServiceUtils.assertParameterNotNull(request.getBucketName(), "bucketName is null"); + ServiceUtils.assertParameterNotNull2(request.getDomainName(), "domainName is null"); + + return this.doActionWithResult("setBucketCustomDomain", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + return AbstractBucketClient.this.setBucketCustomDomainImpl(request); + } + }); + } +} diff --git a/app/src/main/java/com/obs/services/AbstractClient.java b/app/src/main/java/com/obs/services/AbstractClient.java new file mode 100644 index 0000000..9d6fc5a --- /dev/null +++ b/app/src/main/java/com/obs/services/AbstractClient.java @@ -0,0 +1,492 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services; + +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; + +import com.obs.log.ILogger; +import com.obs.log.InterfaceLogBean; +import com.obs.log.LoggerBuilder; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.Constants; +import com.obs.services.internal.ObsConstraint; +import com.obs.services.internal.ObsProperties; +import com.obs.services.internal.ObsService; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.security.ProviderCredentials; +import com.obs.services.internal.utils.AccessLoggerUtils; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.internal.xml.OBSXMLBuilder; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.HttpMethodEnum; +import com.obs.services.model.PolicyConditionItem; +import com.obs.services.model.PolicyTempSignatureRequest; +import com.obs.services.model.PostSignatureRequest; +import com.obs.services.model.PostSignatureResponse; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.TemporarySignatureRequest; +import com.obs.services.model.TemporarySignatureResponse; +import com.obs.services.model.V4PostSignatureRequest; +import com.obs.services.model.V4PostSignatureResponse; +import com.obs.services.model.V4TemporarySignatureRequest; +import com.obs.services.model.V4TemporarySignatureResponse; +import static com.jamesmurty.utils.BaseXMLBuilder.failIfExternalEntityParsingCannotBeConfigured; + +public abstract class AbstractClient extends ObsService implements Closeable, IObsClient, IFSClient { + private static final ILogger ILOG = LoggerBuilder.getLogger(AbstractClient.class); + + static { + failIfExternalEntityParsingCannotBeConfigured = false; + } + + protected void init(String accessKey, String secretKey, String securityToken, ObsConfiguration config) { + InterfaceLogBean reqBean = new InterfaceLogBean("ObsClient", config.getEndPoint(), ""); + ProviderCredentials credentials = new ProviderCredentials(accessKey, secretKey, securityToken); + ObsProperties obsProperties = ServiceUtils.changeFromObsConfiguration(config); + credentials.setAuthType(config.getAuthType()); + credentials.setLocalAuthTypeCacheCapacity(config.getLocalAuthTypeCacheCapacity()); + this.obsProperties = obsProperties; + this.credentials = credentials; + this.keyManagerFactory = config.getKeyManagerFactory(); + this.trustManagerFactory = config.getTrustManagerFactory(); + if (this.isAuthTypeNegotiation()) { + this.getProviderCredentials().setIsAuthTypeNegotiation(true); + } + this.initHttpClient(config.getHttpDispatcher()); + OBSXMLBuilder.setXmlDocumentBuilderFactoryClass(config.getXmlDocumentBuilderFactoryClass()); + reqBean.setRespTime(new Date()); + reqBean.setResultCode(Constants.RESULTCODE_SUCCESS); + if (ILOG.isInfoEnabled()) { + ILOG.info(reqBean); + } + + if (ILOG.isWarnEnabled()) { + StringBuilder sb = new StringBuilder("[OBS SDK Version="); + sb.append(Constants.OBS_SDK_VERSION); + sb.append("];"); + sb.append("[Endpoint="); + String ep; + if (this.getHttpsOnly()) { + ep = "https://" + this.getEndpoint() + ":" + this.getHttpsPort() + "/"; + } else { + ep = "http://" + this.getEndpoint() + ":" + this.getHttpPort() + "/"; + } + sb.append(ep); + sb.append("];"); + sb.append("[Access Mode="); + sb.append(this.isPathStyle() ? "Path" : "Virtul Hosting"); + sb.append("]"); + ILOG.warn(sb); + } + } + + + /** + * Create a temporarily authorized URL. + * + * @param method + * HTTP request method + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param specialParam + * Special operator + * @param expiryTime + * Time when the temporary authentication expires + * @param headers + * Header information + * @param queryParams + * Query parameter information + * @return Temporarily authorized URL + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + @Deprecated + public String createSignedUrl(HttpMethodEnum method, String bucketName, String objectKey, + SpecialParamEnum specialParam, Date expiryTime, Map headers, + Map queryParams) throws ObsException { + return this.createSignedUrl(method, bucketName, objectKey, specialParam, expiryTime == null + ? ObsConstraint.DEFAULT_EXPIRE_SECONEDS : (expiryTime.getTime() - System.currentTimeMillis()) / 1000, + headers, queryParams); + } + + /** + * Create a temporarily authorized URL. + * + * @param method + * HTTP request method + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param specialParam + * Special operator + * @param expires + * Time when the temporary authentication expires. The unit is + * second and the default value is 300. + * @param headers + * Header information + * @param queryParams + * Query parameter information + * @return Temporarily authorized URL + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + @Deprecated + public String createSignedUrl(HttpMethodEnum method, String bucketName, String objectKey, + SpecialParamEnum specialParam, long expires, Map headers, Map queryParams) { + TemporarySignatureRequest request = new TemporarySignatureRequest(); + request.setMethod(method); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setSpecialParam(specialParam); + request.setHeaders(headers); + request.setQueryParams(queryParams); + if (expires > 0) { + request.setExpires(expires); + } + return createTemporarySignature(request).getSignedUrl(); + } + + @Deprecated + public V4TemporarySignatureResponse createV4TemporarySignature(V4TemporarySignatureRequest request) { + ServiceUtils.assertParameterNotNull(request, "V4TemporarySignatureRequest is null"); + InterfaceLogBean reqBean = new InterfaceLogBean("createV4TemporarySignature", this.getEndpoint(), ""); + try { + TemporarySignatureResponse response = this.createV4TemporarySignature((TemporarySignatureRequest) request); + V4TemporarySignatureResponse res = new V4TemporarySignatureResponse(response.getSignedUrl()); + res.getActualSignedRequestHeaders().putAll(response.getActualSignedRequestHeaders()); + return res; + } catch (Exception e) { + reqBean.setRespTime(new Date()); + if (ILOG.isErrorEnabled()) { + ILOG.error(reqBean); + } + throw new ObsException(e.getMessage(), e); + } + } + + @Deprecated + public V4PostSignatureResponse createV4PostSignature(String acl, String contentType, long expires, + String bucketName, String objectKey) throws ObsException { + V4PostSignatureRequest request = new V4PostSignatureRequest(expires, new Date(), bucketName, objectKey); + request.getFormParams().put("acl", acl); + request.getFormParams().put("content-type", contentType); + return this.createV4PostSignature(request); + } + + @Deprecated + public V4PostSignatureResponse createV4PostSignature(long expires, String bucketName, String objectKey) + throws ObsException { + V4PostSignatureRequest request = new V4PostSignatureRequest(expires, new Date(), bucketName, objectKey); + return this.createV4PostSignature(request); + } + + @Deprecated + public V4PostSignatureResponse createV4PostSignature(V4PostSignatureRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "V4PostSignatureRequest is null"); + InterfaceLogBean reqBean = new InterfaceLogBean("createV4PostSignature", this.getEndpoint(), ""); + return (V4PostSignatureResponse)createPostSignature(request, reqBean, true); + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#createTemporarySignature(com.obs.services. + * model.TemporarySignatureRequest) + */ + @Override + public TemporarySignatureResponse createTemporarySignature(TemporarySignatureRequest request) { + ServiceUtils.assertParameterNotNull(request, "TemporarySignatureRequest is null"); + InterfaceLogBean reqBean = new InterfaceLogBean("createTemporarySignature", this.getEndpoint(), ""); + try { + return this.getProviderCredentials().getLocalAuthType(request.getBucketName()) == AuthTypeEnum.V4 + ? this.createV4TemporarySignature(request) : this.createTemporarySignatureResponse(request); + } catch (Exception e) { + reqBean.setRespTime(new Date()); + if (ILOG.isErrorEnabled()) { + ILOG.error(reqBean); + } + throw new ObsException(e.getMessage(), e); + } + } + + /** + * Generate temporary authorization parameters for GET requests based on the + * object name prefix and validity period. + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param prefix + * Object name prefix + * @param expiryDate + * Expiration date (ISO 8601 UTC) + * @param headers + * Header information + * @param queryParams + * Query parameter information + * @return Response to the request for temporary access authorization + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public TemporarySignatureResponse createGetTemporarySignature(String bucketName, String objectKey, String prefix, + Date expiryDate, Map headers, Map queryParams) { + try { + PolicyTempSignatureRequest request = createPolicyGetRequest(bucketName, objectKey, prefix, headers, + queryParams); + request.setExpiryDate(expiryDate); + return this.createTemporarySignatureResponse(request); + } catch (Exception e) { + throw new ObsException(e.getMessage(), e); + } + } + + /** + * Generate temporary authorization parameters for GET requests based on the + * object name prefix and validity period. + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param prefix + * Object name prefix + * @param expires + * Validity period (seconds) + * @param headers + * Header information + * @param queryParams + * Query parameter information + * @return Response to the request for temporary access authorization + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public TemporarySignatureResponse createGetTemporarySignature(String bucketName, String objectKey, String prefix, + long expires, Map headers, Map queryParams) { + try { + PolicyTempSignatureRequest request = createPolicyGetRequest(bucketName, objectKey, prefix, headers, + queryParams); + request.setExpires(expires); + return this.createTemporarySignatureResponse(request); + } catch (Exception e) { + throw new ObsException(e.getMessage(), e); + } + } + + /** + * Generate parameters for browser-based authorized access. + * + * @param acl + * Object ACL + * @param contentType + * MIME type of the object + * @param expires + * Validity period (in seconds) + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @return Response to the V4 browser-based authorized access + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public PostSignatureResponse createPostSignature(String acl, String contentType, long expires, String bucketName, + String objectKey) throws ObsException { + PostSignatureRequest request = new PostSignatureRequest(expires, new Date(), bucketName, objectKey); + request.getFormParams().put( + this.getProviderCredentials().getLocalAuthType(bucketName) + == AuthTypeEnum.V4 ? "acl" : this.getIHeaders(bucketName).aclHeader(), acl); + request.getFormParams().put(com.obs.services.internal.Constants.CommonHeaders.CONTENT_TYPE, contentType); + return this.createPostSignature(request); + } + + /** + * Generate parameters for browser-based authorized access. + * + * @param expires + * Validity period (in seconds) + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @return Response to the V4 browser-based authorized access + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public PostSignatureResponse createPostSignature(long expires, String bucketName, String objectKey) + throws ObsException { + PostSignatureRequest request = new PostSignatureRequest(expires, new Date(), bucketName, objectKey); + return this.createPostSignature(request); + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#createPostSignature(com.obs.services.model. + * PostSignatureRequest) + */ + @Override + public PostSignatureResponse createPostSignature(PostSignatureRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "PostSignatureRequest is null"); + InterfaceLogBean reqBean = new InterfaceLogBean("createPostSignature", this.getEndpoint(), ""); + return createPostSignature(request, reqBean, this.getProviderCredentials() + .getLocalAuthType(request.getBucketName()) == AuthTypeEnum.V4); + } + + protected abstract class ActionCallbackWithResult { + + public abstract T action() throws ServiceException; + + void authTypeNegotiate(String bucketName) throws ServiceException { + AuthTypeEnum authTypeEnum = AbstractClient.this.getProviderCredentials() + .getLocalAuthType().get(bucketName); + if (authTypeEnum == null) { + authTypeEnum = AbstractClient.this.getApiVersion(bucketName); + AbstractClient.this.getProviderCredentials().setLocalAuthType(bucketName, authTypeEnum); + } + } + } + + protected T doActionWithResult(String action, String bucketName, ActionCallbackWithResult callback) + throws ObsException { + if (!this.isCname()) { + ServiceUtils.assertParameterNotNull(bucketName, "bucketName is null"); + } + InterfaceLogBean reqBean = new InterfaceLogBean(action, this.getEndpoint(), ""); + try { + long start = System.currentTimeMillis(); + if (this.isAuthTypeNegotiation()) { + callback.authTypeNegotiate(bucketName); + } + T ret = callback.action(); + reqBean.setRespTime(new Date()); + reqBean.setResultCode(Constants.RESULTCODE_SUCCESS); + if (ILOG.isInfoEnabled()) { + ILOG.info(reqBean); + ILOG.info("ObsClient [" + action + "] cost " + (System.currentTimeMillis() - start) + " ms"); + } + return ret; + } catch (ServiceException e) { + + ObsException ex = ServiceUtils.changeFromServiceException(e); + if (ex.getResponseCode() >= 400 && ex.getResponseCode() < 500) { + if (ILOG.isWarnEnabled()) { + reqBean.setRespTime(new Date()); + reqBean.setResultCode(String.valueOf(e.getResponseCode())); + ILOG.warn(reqBean); + } + } else if (ILOG.isErrorEnabled()) { + reqBean.setRespTime(new Date()); + reqBean.setResultCode(String.valueOf(ex.getResponseCode())); + ILOG.error(reqBean); + } + throw ex; + } finally { + AccessLoggerUtils.printLog(); + } + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#refresh(java.lang.String, + * java.lang.String, java.lang.String) + */ + @Override + public void refresh(String accessKey, String secretKey, String securityToken) { + ProviderCredentials credentials = new ProviderCredentials(accessKey, secretKey, securityToken); + credentials.setIsAuthTypeNegotiation(this.credentials.getIsAuthTypeNegotiation()); + credentials.setAuthType(this.credentials.getAuthType()); + credentials.setLocalAuthType(this.credentials.getLocalAuthType()); + this.setProviderCredentials(credentials); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#close() + */ + @Override + public void close() throws IOException { + this.shutdown(); + } + + public String base64Md5(InputStream is, long length, long offset) throws NoSuchAlgorithmException, IOException { + return ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(is, length, offset)); + } + + public String base64Md5(InputStream is) throws NoSuchAlgorithmException, IOException { + return ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(is)); + } + + private PolicyTempSignatureRequest createPolicyGetRequest(String bucketName, String objectKey, String prefix, + Map headers, Map queryParams) { + PolicyTempSignatureRequest request = new PolicyTempSignatureRequest(HttpMethodEnum.GET, bucketName, objectKey); + List conditions = new ArrayList(); + PolicyConditionItem keyCondition = + new PolicyConditionItem(com.obs.services.model.PolicyConditionItem.ConditionOperator.STARTS_WITH, + "key", prefix); + String bucket = this.isCname() ? this.getEndpoint() : bucketName; + PolicyConditionItem bucketCondition = + new PolicyConditionItem(com.obs.services.model.PolicyConditionItem.ConditionOperator.EQUAL, + "bucket", bucket); + conditions.add(keyCondition); + conditions.add(bucketCondition); + request.setConditions(conditions); + request.setHeaders(headers); + request.setQueryParams(queryParams); + return request; + } + + private PostSignatureResponse createPostSignature(PostSignatureRequest request, + InterfaceLogBean reqBean, + boolean isV4) { + try { + PostSignatureResponse response = this.createPostSignatureResponse(request, + isV4); + reqBean.setRespTime(new Date()); + reqBean.setResultCode(Constants.RESULTCODE_SUCCESS); + if (ILOG.isInfoEnabled()) { + ILOG.info(reqBean); + } + return response; + } catch (Exception e) { + reqBean.setRespTime(new Date()); + if (ILOG.isErrorEnabled()) { + ILOG.error(reqBean); + } + throw new ObsException(e.getMessage(), e); + } + } +} diff --git a/app/src/main/java/com/obs/services/AbstractDeprecatedBucketClient.java b/app/src/main/java/com/obs/services/AbstractDeprecatedBucketClient.java new file mode 100644 index 0000000..309f168 --- /dev/null +++ b/app/src/main/java/com/obs/services/AbstractDeprecatedBucketClient.java @@ -0,0 +1,197 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services; + +import java.util.ArrayList; +import java.util.List; + +import com.obs.services.exception.ObsException; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.BaseBucketRequest; +import com.obs.services.model.BucketCors; +import com.obs.services.model.BucketLoggingConfiguration; +import com.obs.services.model.BucketVersioningConfiguration; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.LifecycleConfiguration; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.OptionsInfoRequest; +import com.obs.services.model.OptionsInfoResult; +import com.obs.services.model.ReplicationConfiguration; +import com.obs.services.model.S3Bucket; +import com.obs.services.model.S3BucketCors; +import com.obs.services.model.SetBucketAclRequest; +import com.obs.services.model.SetBucketCorsRequest; +import com.obs.services.model.SetBucketLifecycleRequest; +import com.obs.services.model.SetBucketLoggingRequest; +import com.obs.services.model.SetBucketReplicationRequest; +import com.obs.services.model.SetBucketWebsiteRequest; +import com.obs.services.model.WebsiteConfiguration; + +public abstract class AbstractDeprecatedBucketClient extends AbstractClient { + + @Deprecated + public ObsBucket createBucket(final S3Bucket bucket) throws ObsException { + ServiceUtils.assertParameterNotNull(bucket, "bucket is null"); + ObsBucket obsBucket = new ObsBucket(); + obsBucket.setBucketName(bucket.getBucketName()); + obsBucket.setLocation(bucket.getLocation()); + obsBucket.setAcl(bucket.getAcl()); + obsBucket.setMetadata(bucket.getMetadata()); + obsBucket.setBucketStorageClass(bucket.getBucketStorageClass()); + return this.createBucket(obsBucket); + } + + @Deprecated + public List listBuckets() throws ObsException { + List ret = this.listBuckets(null); + List buckets = new ArrayList(ret.size()); + buckets.addAll(ret); + return buckets; + } + + /** + * Set a bucket ACL.
+ * + * @param bucketName + * Bucket name + * @param cannedACL + * Pre-defined access control policy + * @param acl + * ACL ("acl" and "cannedACL" cannot be used together.) + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + @Deprecated + public HeaderResponse setBucketAcl(final String bucketName, final String cannedACL, final AccessControlList acl) + throws ObsException { + SetBucketAclRequest request = new SetBucketAclRequest(bucketName, acl); + request.setCannedACL(cannedACL); + + return setBucketAcl(request); + } + + @Deprecated + public HeaderResponse setBucketCors(final String bucketName, final S3BucketCors s3BucketCors) throws ObsException { + ServiceUtils.assertParameterNotNull(s3BucketCors, + "The bucket '" + bucketName + "' does not include Cors information"); + BucketCors bucketCors = new BucketCors(); + bucketCors.setRules(s3BucketCors.getRules()); + return this.setBucketCors(new SetBucketCorsRequest(bucketName, bucketCors)); + } + + /** + * Pre-request a bucket. + * + * @param bucketName + * Bucket name + * @param optionInfo + * Parameters in a bucket preflight request + * @return Response to the bucket preflight request + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + @Deprecated + public OptionsInfoResult optionsBucket(final String bucketName, final OptionsInfoRequest optionInfo) + throws ObsException { + return this.doActionWithResult("optionsBucket", bucketName, new ActionCallbackWithResult() { + + @Override + public OptionsInfoResult action() throws ServiceException { + ServiceUtils.assertParameterNotNull(optionInfo, "OptionsInfoRequest is null"); + return AbstractDeprecatedBucketClient.this.optionsImpl(bucketName, null, optionInfo); + } + }); + } + + /** + * Obtain the logging settings of a bucket. + * + * @param bucketName + * Bucket name + * @return Logging settings of the bucket + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + @Deprecated + public BucketLoggingConfiguration getBucketLoggingConfiguration(final String bucketName) throws ObsException { + return getBucketLogging(new BaseBucketRequest(bucketName)); + } + + @Deprecated + public HeaderResponse setBucketLoggingConfiguration(final String bucketName, + final BucketLoggingConfiguration loggingConfiguration) throws ObsException { + return this.setBucketLogging(new SetBucketLoggingRequest(bucketName, loggingConfiguration)); + } + + @Deprecated + public HeaderResponse setBucketVersioning(String bucketName, String status) throws ObsException { + return this.setBucketVersioning(bucketName, new BucketVersioningConfiguration(status)); + } + + @Deprecated + public LifecycleConfiguration getBucketLifecycleConfiguration(final String bucketName) throws ObsException { + return this.getBucketLifecycle(new BaseBucketRequest(bucketName)); + } + + @Deprecated + public HeaderResponse setBucketLifecycleConfiguration(final String bucketName, + final LifecycleConfiguration lifecycleConfig) throws ObsException { + return this.setBucketLifecycle(new SetBucketLifecycleRequest(bucketName, lifecycleConfig)); + } + + @Deprecated + public HeaderResponse deleteBucketLifecycleConfiguration(final String bucketName) throws ObsException { + return this.deleteBucketLifecycle(new BaseBucketRequest(bucketName)); + } + + @Deprecated + public WebsiteConfiguration getBucketWebsiteConfiguration(final String bucketName) throws ObsException { + return this.getBucketWebsite(new BaseBucketRequest(bucketName)); + } + + @Deprecated + public HeaderResponse setBucketWebsiteConfiguration(final String bucketName, + final WebsiteConfiguration websiteConfig) throws ObsException { + return setBucketWebsite(new SetBucketWebsiteRequest(bucketName, websiteConfig)); + } + + @Deprecated + public HeaderResponse deleteBucketWebsiteConfiguration(final String bucketName) throws ObsException { + return this.deleteBucketWebsite(new BaseBucketRequest(bucketName)); + } + + @Deprecated + public HeaderResponse setBucketReplicationConfiguration(final String bucketName, + final ReplicationConfiguration replicationConfiguration) throws ObsException { + return setBucketReplication(new SetBucketReplicationRequest(bucketName, replicationConfiguration)); + } + + @Deprecated + public ReplicationConfiguration getBucketReplicationConfiguration(final String bucketName) throws ObsException { + return getBucketReplication(new BaseBucketRequest(bucketName)); + } + + @Deprecated + public HeaderResponse deleteBucketReplicationConfiguration(final String bucketName) throws ObsException { + return deleteBucketReplication(new BaseBucketRequest(bucketName)); + } +} diff --git a/app/src/main/java/com/obs/services/AbstractFileClient.java b/app/src/main/java/com/obs/services/AbstractFileClient.java new file mode 100644 index 0000000..216368a --- /dev/null +++ b/app/src/main/java/com/obs/services/AbstractFileClient.java @@ -0,0 +1,230 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.DownloadResumableClient; +import com.obs.services.internal.UploadResumableClient; +import com.obs.services.internal.task.DefaultTaskProgressStatus; +import com.obs.services.internal.task.DropFolderTask; +import com.obs.services.internal.task.LazyTaksCallback; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.CompleteMultipartUploadResult; +import com.obs.services.model.DeleteObjectResult; +import com.obs.services.model.DownloadFileRequest; +import com.obs.services.model.DownloadFileResult; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.MonitorableProgressListener; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObsObject; +import com.obs.services.model.TaskCallback; +import com.obs.services.model.TaskProgressListener; +import com.obs.services.model.TaskProgressStatus; +import com.obs.services.model.UploadFileRequest; +import com.obs.services.model.fs.DropFolderRequest; + +public abstract class AbstractFileClient extends AbstractPFSClient { + private static final ILogger ILOG = LoggerBuilder.getLogger(AbstractFileClient.class); + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#uploadFile(com.obs.services.model. + * UploadFileRequest) + */ + @Override + public CompleteMultipartUploadResult uploadFile(UploadFileRequest uploadFileRequest) throws ObsException { + return new UploadResumableClient(this).uploadFileResume(uploadFileRequest); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#downloadFile(com.obs.services.model. + * DownloadFileRequest) + */ + @Override + public DownloadFileResult downloadFile(DownloadFileRequest downloadFileRequest) throws ObsException { + try { + return new DownloadResumableClient(this).downloadFileResume(downloadFileRequest); + } finally { + if (null != downloadFileRequest.getProgressListener() + && downloadFileRequest.getProgressListener() instanceof MonitorableProgressListener) { + ((MonitorableProgressListener)downloadFileRequest.getProgressListener()).finishOneTask(); + } + } + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IFSClient#deleteFolder(com.obs.services.model.fs. + * DeleteFSFolderRequest) + */ + @Override + public TaskProgressStatus dropFolder(DropFolderRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "DropFolderRequest is null"); + if (!this.isCname()) { + ServiceUtils.assertParameterNotNull(request.getBucketName(), "bucketName is null"); + } + ThreadPoolExecutor executor = this.initThreadPool(request); + DefaultTaskProgressStatus progressStatus = new DefaultTaskProgressStatus(); + try { + String folderName = request.getFolderName(); + String delimiter = this.getFileSystemDelimiter(); + if (!folderName.endsWith(delimiter)) { + folderName = folderName + delimiter; + } + TaskCallback callback = (request.getCallback() == null) + ? new LazyTaksCallback() + : request.getCallback(); + TaskProgressListener listener = request.getProgressListener(); + int interval = request.getProgressInterval(); + int[] totalTasks = {0}; + boolean isSubDeleted = recurseFolders(request, folderName, callback, progressStatus, executor, totalTasks); + Map> futures = new HashMap>(); + totalTasks[0]++; + progressStatus.setTotalTaskNum(totalTasks[0]); + + if (isSubDeleted) { + submitDropTask(request, folderName, callback, progressStatus, executor, futures); + checkDropFutures(futures, progressStatus, callback, listener, interval); + } else { + progressStatus.failTaskIncrement(); + callback.onException(new ObsException("Failed to delete due to child file deletion failed"), + folderName); + recordBulkTaskStatus(progressStatus, callback, listener, interval); + } + + executor.shutdown(); + executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); + } catch (ObsException e) { + throw e; + } catch (Exception e) { + throw new ObsException(e.getMessage(), e); + } + return progressStatus; + } + + + private boolean recurseFolders(DropFolderRequest dropRequest, String folders, + TaskCallback callback, + DefaultTaskProgressStatus progressStatus, ThreadPoolExecutor executor, int[] count) { + ListObjectsRequest request = new ListObjectsRequest(dropRequest.getBucketName()); + request.setDelimiter("/"); + request.setPrefix(folders); + request.setRequesterPays(dropRequest.isRequesterPays()); + request.setEncodingType(dropRequest.getEncodingType()); + ObjectListing result; + boolean isDeleted = true; + do { + result = this.listObjects(request); + Map> futures = new HashMap>(); + + for (ObsObject o : result.getObjects()) { + if (!o.getObjectKey().endsWith("/")) { + count[0]++; + isDeleted = submitDropTask(dropRequest, o.getObjectKey(), callback, + progressStatus, executor, futures) && isDeleted; + if (ILOG.isInfoEnabled()) { + if (count[0] % 1000 == 0) { + ILOG.info("DropFolder: " + Arrays.toString(count) + + " tasks have submitted to delete objects"); + } + } + } + } + + for (String prefix : result.getCommonPrefixes()) { + boolean isSubDeleted = recurseFolders(dropRequest, prefix, callback, progressStatus, executor, count); + count[0]++; + if (isSubDeleted) { + isDeleted = submitDropTask(dropRequest, prefix, callback, + progressStatus, executor, futures) && isDeleted; + } else { + progressStatus.failTaskIncrement(); + callback.onException(new ObsException("Failed to delete due to child file deletion failed"), + prefix); + recordBulkTaskStatus(progressStatus, callback, dropRequest.getProgressListener(), + dropRequest.getProgressInterval()); + } + if (ILOG.isInfoEnabled()) { + if (count[0] % 1000 == 0) { + ILOG.info("DropFolder: " + Arrays.toString(count) + " tasks have submitted to delete objects"); + } + } + } + + request.setMarker(result.getNextMarker()); + isDeleted = checkDropFutures(futures, progressStatus, callback, dropRequest.getProgressListener(), + dropRequest.getProgressInterval()) && isDeleted; + } while (result.isTruncated()); + return isDeleted; + } + + private boolean submitDropTask(DropFolderRequest request, String key, + TaskCallback callback, + DefaultTaskProgressStatus progreStatus, + ThreadPoolExecutor executor, Map> futures) { + DropFolderTask task = new DropFolderTask(this, request.getBucketName(), key, progreStatus, + request.getProgressListener(), request.getProgressInterval(), callback, + request.isRequesterPays()); + try { + futures.put(key, executor.submit(task)); + } catch (RejectedExecutionException e) { + progreStatus.failTaskIncrement(); + callback.onException(new ObsException(e.getMessage(), e), key); + return false; + } + return true; + } + + private boolean checkDropFutures(Map> futures, DefaultTaskProgressStatus progressStatus, + TaskCallback callback, TaskProgressListener listener, int interval) { + boolean isDeleted = true; + for (Entry> entry : futures.entrySet()) { + try { + entry.getValue().get(); + } catch (ExecutionException e) { + progressStatus.failTaskIncrement(); + if (e.getCause() instanceof ObsException) { + callback.onException((ObsException) e.getCause(), entry.getKey()); + } else { + callback.onException(new ObsException(e.getMessage(), e), entry.getKey()); + } + isDeleted = false; + } catch (InterruptedException e) { + progressStatus.failTaskIncrement(); + callback.onException(new ObsException(e.getMessage(), e), entry.getKey()); + isDeleted = false; + } + recordBulkTaskStatus(progressStatus, callback, listener, interval); + } + return isDeleted; + } +} diff --git a/app/src/main/java/com/obs/services/AbstractMultipartObjectClient.java b/app/src/main/java/com/obs/services/AbstractMultipartObjectClient.java new file mode 100644 index 0000000..b50ea60 --- /dev/null +++ b/app/src/main/java/com/obs/services/AbstractMultipartObjectClient.java @@ -0,0 +1,233 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services; + +import java.io.File; +import java.io.InputStream; + +import com.obs.services.exception.ObsException; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AbortMultipartUploadRequest; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.CompleteMultipartUploadResult; +import com.obs.services.model.CopyPartRequest; +import com.obs.services.model.CopyPartResult; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ListMultipartUploadsRequest; +import com.obs.services.model.ListPartsRequest; +import com.obs.services.model.ListPartsResult; +import com.obs.services.model.MultipartUploadListing; +import com.obs.services.model.UploadPartRequest; +import com.obs.services.model.UploadPartResult; + +public abstract class AbstractMultipartObjectClient extends AbstractObjectClient { + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#initiateMultipartUpload(com.obs.services. + * model.InitiateMultipartUploadRequest) + */ + @Override + public InitiateMultipartUploadResult initiateMultipartUpload(final InitiateMultipartUploadRequest request) + throws ObsException { + ServiceUtils.assertParameterNotNull(request, "InitiateMultipartUploadRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + return this.doActionWithResult("initiateMultipartUpload", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public InitiateMultipartUploadResult action() throws ServiceException { + return AbstractMultipartObjectClient.this.initiateMultipartUploadImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#abortMultipartUpload(com.obs.services.model. + * AbortMultipartUploadRequest) + */ + @Override + public HeaderResponse abortMultipartUpload(final AbortMultipartUploadRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "AbortMultipartUploadRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + ServiceUtils.assertParameterNotNull(request.getUploadId(), "uploadId is null"); + return this.doActionWithResult("abortMultipartUpload", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + return AbstractMultipartObjectClient.this.abortMultipartUploadImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#uploadPart(java.lang.String, + * java.lang.String, java.lang.String, int, java.io.InputStream) + */ + @Override + public UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId, int partNumber, + InputStream input) throws ObsException { + UploadPartRequest request = new UploadPartRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setUploadId(uploadId); + request.setPartNumber(partNumber); + request.setInput(input); + return this.uploadPart(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#uploadPart(java.lang.String, + * java.lang.String, java.lang.String, int, java.io.File) + */ + @Override + public UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId, int partNumber, File file) + throws ObsException { + UploadPartRequest request = new UploadPartRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setUploadId(uploadId); + request.setPartNumber(partNumber); + request.setFile(file); + return this.uploadPart(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#uploadPart(com.obs.services.model. + * UploadPartRequest) + */ + @Override + public UploadPartResult uploadPart(final UploadPartRequest request) throws ObsException { + + ServiceUtils.assertParameterNotNull(request, "UploadPartRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + ServiceUtils.assertParameterNotNull(request.getUploadId(), "uploadId is null"); + return this.doActionWithResult("uploadPart", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public UploadPartResult action() throws ServiceException { + if (null != request.getInput() && null != request.getFile()) { + throw new ServiceException("Both input and file are set, only one is allowed"); + } + return AbstractMultipartObjectClient.this.uploadPartImpl(request); + } + }); + + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#copyPart(com.obs.services.model. + * CopyPartRequest) + */ + @Override + public CopyPartResult copyPart(final CopyPartRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "CopyPartRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getSourceObjectKey(), "sourceObjectKey is null"); + ServiceUtils.assertParameterNotNull(request.getDestinationBucketName(), "destinationBucketName is null"); + ServiceUtils.assertParameterNotNull2(request.getDestinationObjectKey(), "destinationObjectKey is null"); + ServiceUtils.assertParameterNotNull(request.getUploadId(), "uploadId is null"); + return this.doActionWithResult("copyPart", request.getSourceBucketName(), + new ActionCallbackWithResult() { + + @Override + public CopyPartResult action() throws ServiceException { + return AbstractMultipartObjectClient.this.copyPartImpl(request); + } + }); + + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#completeMultipartUpload(com.obs.services. + * model.CompleteMultipartUploadRequest) + */ + @Override + public CompleteMultipartUploadResult completeMultipartUpload(final CompleteMultipartUploadRequest request) + throws ObsException { + ServiceUtils.assertParameterNotNull(request, "CompleteMultipartUploadRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + ServiceUtils.assertParameterNotNull(request.getUploadId(), "uploadId is null"); + return this.doActionWithResult("completeMultipartUpload", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public CompleteMultipartUploadResult action() throws ServiceException { + return AbstractMultipartObjectClient.this.completeMultipartUploadImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#listParts(com.obs.services.model. + * ListPartsRequest) + */ + @Override + public ListPartsResult listParts(final ListPartsRequest request) throws ObsException { + + ServiceUtils.assertParameterNotNull(request, "ListPartsRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + ServiceUtils.assertParameterNotNull(request.getUploadId(), "uploadId is null"); + return this.doActionWithResult("listParts", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public ListPartsResult action() throws ServiceException { + return AbstractMultipartObjectClient.this.listPartsImpl(request); + } + }); + + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#listMultipartUploads(com.obs.services.model. + * ListMultipartUploadsRequest) + */ + @Override + public MultipartUploadListing listMultipartUploads(final ListMultipartUploadsRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "ListMultipartUploadsRequest is null"); + return this.doActionWithResult("listMultipartUploads", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public MultipartUploadListing action() throws ServiceException { + return AbstractMultipartObjectClient.this.listMultipartUploadsImpl(request); + } + }); + + } +} diff --git a/app/src/main/java/com/obs/services/AbstractObjectClient.java b/app/src/main/java/com/obs/services/AbstractObjectClient.java new file mode 100644 index 0000000..30b59c9 --- /dev/null +++ b/app/src/main/java/com/obs/services/AbstractObjectClient.java @@ -0,0 +1,703 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services; + +import java.io.File; +import java.io.InputStream; + +import com.obs.services.exception.ObsException; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.AppendObjectRequest; +import com.obs.services.model.AppendObjectResult; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.CopyObjectRequest; +import com.obs.services.model.CopyObjectResult; +import com.obs.services.model.DeleteObjectRequest; +import com.obs.services.model.DeleteObjectResult; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.DeleteObjectsResult; +import com.obs.services.model.GetObjectAclRequest; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.GetObjectRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ListVersionsRequest; +import com.obs.services.model.ListVersionsResult; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.ObsObject; +import com.obs.services.model.OptionsInfoRequest; +import com.obs.services.model.OptionsInfoResult; +import com.obs.services.model.PutObjectRequest; +import com.obs.services.model.PutObjectResult; +import com.obs.services.model.RestoreObjectRequest; +import com.obs.services.model.RestoreObjectResult; +import com.obs.services.model.SetObjectAclRequest; +import com.obs.services.model.SetObjectMetadataRequest; + +public abstract class AbstractObjectClient extends AbstractBucketAdvanceClient { + @Override + public boolean doesObjectExist(final String bucketName, final String objectKey) throws ObsException { + GetObjectMetadataRequest request = new GetObjectMetadataRequest(bucketName, objectKey); + return this.doesObjectExist(request); + } + + @Override + public boolean doesObjectExist(final GetObjectMetadataRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request.getBucketName(), "bucket is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + return doActionWithResult("doesObjectExist", request.getBucketName(), new ActionCallbackWithResult() { + @Override + public Boolean action() throws ServiceException { + try { + return AbstractObjectClient.this.doesObjectExistImpl(request); + } catch (ServiceException e) { + if (AbstractObjectClient.this.isAuthTypeNegotiation() && e.getResponseCode() == 400 + && "Unsupported Authorization Type".equals(e.getErrorMessage()) + && AbstractObjectClient.this.getProviderCredentials() + .getLocalAuthType(request.getBucketName()) == AuthTypeEnum.OBS) { + AbstractObjectClient.this.getProviderCredentials() + .setLocalAuthType(request.getBucketName(), AuthTypeEnum.V2); + return AbstractObjectClient.this.doesObjectExistImpl(request); + } else { + throw e; + } + } + } + }); + } + + /** + * Perform a preflight on a bucket. + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param optionInfo + * Parameters in an object preflight request + * @return Response to the object preflight request + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public OptionsInfoResult optionsObject(final String bucketName, final String objectKey, + final OptionsInfoRequest optionInfo) throws ObsException { + return this.doActionWithResult("optionsObject", bucketName, new ActionCallbackWithResult() { + + @Override + public OptionsInfoResult action() throws ServiceException { + ServiceUtils.assertParameterNotNull(optionInfo, "OptionsInfoRequest is null"); + return AbstractObjectClient.this.optionsImpl(bucketName, objectKey, optionInfo); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#listObjects(com.obs.services.model. + * ListObjectsRequest) + */ + @Override + public ObjectListing listObjects(final ListObjectsRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "ListObjectsRequest is null"); + return this.doActionWithResult("listObjects", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public ObjectListing action() throws ServiceException { + return AbstractObjectClient.this.listObjectsImpl(request); + } + + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#listObjects(java.lang.String) + */ + @Override + public ObjectListing listObjects(String bucketName) throws ObsException { + ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName); + return this.listObjects(listObjectsRequest); + } + + /** + * List versioning objects in a bucket. + * + * @param bucketName + * Bucket name + * @param prefix + * Object name prefix used for listing versioning objects + * @param delimiter + * Character for grouping object names + * @param keyMarker + * Start position for listing versioning objects (sorted by + * object name) + * @param versionIdMarker + * Start position for listing versioning objects (sorted by + * version ID) + * @param maxKeys + * Maximum number of versioning objects to be listed + * @param nextVersionIdMarker + * Deprecated field + * @return Response to the request for listing versioning objects in the + * bucket + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + @Deprecated + public ListVersionsResult listVersions(final String bucketName, final String prefix, final String delimiter, + final String keyMarker, final String versionIdMarker, final long maxKeys, final String nextVersionIdMarker) + throws ObsException { + ListVersionsRequest request = new ListVersionsRequest(); + request.setBucketName(bucketName); + request.setPrefix(prefix); + request.setKeyMarker(keyMarker); + request.setMaxKeys((int) maxKeys); + request.setVersionIdMarker(versionIdMarker); + request.setDelimiter(delimiter); + return this.listVersions(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#listVersions(com.obs.services.model. + * ListVersionsRequest) + */ + @Override + public ListVersionsResult listVersions(final ListVersionsRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "ListVersionsRequest is null"); + return this.doActionWithResult("listVersions", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public ListVersionsResult action() throws ServiceException { + return AbstractObjectClient.this.listVersionsImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#listVersions(java.lang.String) + */ + @Override + public ListVersionsResult listVersions(final String bucketName) throws ObsException { + ListVersionsRequest request = new ListVersionsRequest(); + request.setBucketName(bucketName); + return this.listVersions(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#listVersions(java.lang.String, long) + */ + @Override + public ListVersionsResult listVersions(final String bucketName, final long maxKeys) throws ObsException { + ListVersionsRequest request = new ListVersionsRequest(); + request.setBucketName(bucketName); + request.setMaxKeys((int) maxKeys); + return this.listVersions(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#listVersions(java.lang.String, + * java.lang.String, java.lang.String, java.lang.String, java.lang.String, + * long) + */ + @Override + public ListVersionsResult listVersions(final String bucketName, final String prefix, final String delimiter, + final String keyMarker, final String versionIdMarker, final long maxKeys) throws ObsException { + return this.listVersions(bucketName, prefix, delimiter, keyMarker, versionIdMarker, maxKeys, null); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#putObject(java.lang.String, + * java.lang.String, java.io.InputStream, + * com.obs.services.model.ObjectMetadata) + */ + @Override + public PutObjectResult putObject(String bucketName, String objectKey, InputStream input, ObjectMetadata metadata) + throws ObsException { + PutObjectRequest request = new PutObjectRequest(); + request.setBucketName(bucketName); + request.setInput(input); + request.setMetadata(metadata); + request.setObjectKey(objectKey); + return this.putObject(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#putObject(java.lang.String, + * java.lang.String, java.io.InputStream) + */ + @Override + public PutObjectResult putObject(String bucketName, String objectKey, InputStream input) throws ObsException { + return this.putObject(bucketName, objectKey, input, null); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#putObject(com.obs.services.model. + * PutObjectRequest) + */ + @Override + public PutObjectResult putObject(final PutObjectRequest request) throws ObsException { + + ServiceUtils.assertParameterNotNull(request, "PutObjectRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + + return this.doActionWithResult("putObject", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public PutObjectResult action() throws ServiceException { + if (null != request.getInput() && null != request.getFile()) { + throw new ServiceException("Both input and file are set, only one is allowed"); + } + return AbstractObjectClient.this.putObjectImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#putObject(java.lang.String, + * java.lang.String, java.io.File) + */ + @Override + public PutObjectResult putObject(String bucketName, String objectKey, File file) throws ObsException { + return this.putObject(bucketName, objectKey, file, null); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#putObject(java.lang.String, + * java.lang.String, java.io.File, com.obs.services.model.ObjectMetadata) + */ + @Override + public PutObjectResult putObject(String bucketName, String objectKey, File file, ObjectMetadata metadata) + throws ObsException { + PutObjectRequest request = new PutObjectRequest(); + request.setBucketName(bucketName); + request.setFile(file); + request.setObjectKey(objectKey); + request.setMetadata(metadata); + return this.putObject(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getObject(com.obs.services.model. + * GetObjectRequest) + */ + @Override + public ObsObject getObject(final GetObjectRequest request) throws ObsException { + + ServiceUtils.assertParameterNotNull(request, "GetObjectRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + return this.doActionWithResult("getObject", request.getBucketName(), new ActionCallbackWithResult() { + + @Override + public ObsObject action() throws ServiceException { + return AbstractObjectClient.this.getObjectImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getObject(java.lang.String, + * java.lang.String, java.lang.String) + */ + @Override + public ObsObject getObject(final String bucketName, final String objectKey, final String versionId) + throws ObsException { + return this.getObject(new GetObjectRequest(bucketName, objectKey, versionId)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getObject(java.lang.String, + * java.lang.String) + */ + @Override + public ObsObject getObject(final String bucketName, final String objectKey) throws ObsException { + return this.getObject(bucketName, objectKey, null); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteObject(java.lang.String, + * java.lang.String, java.lang.String) + */ + @Override + public DeleteObjectResult deleteObject(final String bucketName, final String objectKey, final String versionId) + throws ObsException { + DeleteObjectRequest request = new DeleteObjectRequest(bucketName, objectKey, versionId); + return this.deleteObject(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteObject(java.lang.String, + * java.lang.String) + */ + @Override + public DeleteObjectResult deleteObject(final String bucketName, final String objectKey) throws ObsException { + DeleteObjectRequest request = new DeleteObjectRequest(bucketName, objectKey); + return this.deleteObject(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteObject(java.lang.String, + * java.lang.String) + */ + @Override + public DeleteObjectResult deleteObject(final DeleteObjectRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "DeleteObjectRequest is null"); + return this.doActionWithResult("deleteObject", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public DeleteObjectResult action() throws ServiceException { + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + return AbstractObjectClient.this.deleteObjectImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#deleteObjects(com.obs.services.model. + * DeleteObjectsRequest) + */ + @Override + public DeleteObjectsResult deleteObjects(final DeleteObjectsRequest deleteObjectsRequest) throws ObsException { + ServiceUtils.assertParameterNotNull(deleteObjectsRequest, "DeleteObjectsRequest is null"); + return this.doActionWithResult("deleteObjects", deleteObjectsRequest.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public DeleteObjectsResult action() throws ServiceException { + return AbstractObjectClient.this.deleteObjectsImpl(deleteObjectsRequest); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#copyObject(com.obs.services.model. + * CopyObjectRequest) + */ + @Override + public CopyObjectResult copyObject(final CopyObjectRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "CopyObjectRequest is null"); + ServiceUtils.assertParameterNotNull(request.getDestinationBucketName(), "destinationBucketName is null"); + ServiceUtils.assertParameterNotNull2(request.getSourceObjectKey(), "sourceObjectKey is null"); + ServiceUtils.assertParameterNotNull2(request.getDestinationObjectKey(), "destinationObjectKey is null"); + return this.doActionWithResult("copyObject", request.getSourceBucketName(), + new ActionCallbackWithResult() { + @Override + public CopyObjectResult action() throws ServiceException { + return AbstractObjectClient.this.copyObjectImpl(request); + } + }); + + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#copyObject(java.lang.String, + * java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public CopyObjectResult copyObject(String sourceBucketName, String sourceObjectKey, String destBucketName, + String destObjectKey) throws ObsException { + return this.copyObject(new CopyObjectRequest(sourceBucketName, sourceObjectKey, destBucketName, destObjectKey)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getObjectAcl(java.lang.String, + * java.lang.String, java.lang.String) + */ + @Override + public AccessControlList getObjectAcl(final String bucketName, final String objectKey, final String versionId) + throws ObsException { + return getObjectAcl(new GetObjectAclRequest(bucketName, objectKey, versionId)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getObjectAcl(java.lang.String, + * java.lang.String) + */ + @Override + public AccessControlList getObjectAcl(final String bucketName, final String objectKey) throws ObsException { + return getObjectAcl(new GetObjectAclRequest(bucketName, objectKey, null)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getObjectAcl(java.lang.String, + * java.lang.String) + */ + @Override + public AccessControlList getObjectAcl(final GetObjectAclRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "GetObjectAclRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + return this.doActionWithResult("getObjectAcl", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public AccessControlList action() throws ServiceException { + return AbstractObjectClient.this.getObjectAclImpl(request); + } + + }); + } + + /** + * Set an object ACL. + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param cannedACL + * Pre-defined access control policy + * @param acl + * ACL ("acl" and "cannedACL" cannot be used together.) + * @param versionId + * Object version ID + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + @Deprecated + public HeaderResponse setObjectAcl(final String bucketName, final String objectKey, final String cannedACL, + final AccessControlList acl, final String versionId) throws ObsException { + SetObjectAclRequest request = new SetObjectAclRequest(bucketName, objectKey, acl, versionId); + request.setCannedACL(cannedACL); + return setObjectAcl(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setObjectAcl(java.lang.String, + * java.lang.String, com.obs.services.model.AccessControlList, + * java.lang.String) + */ + @Override + public HeaderResponse setObjectAcl(final SetObjectAclRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetObjectAclRequest is null"); + return this.doActionWithResult("setObjectAcl", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + if (request.getAcl() == null && null == request.getCannedACL()) { + throw new IllegalArgumentException("Both cannedACL and AccessControlList is null"); + } + return AbstractObjectClient.this.setObjectAclImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setObjectAcl(java.lang.String, + * java.lang.String, com.obs.services.model.AccessControlList, + * java.lang.String) + */ + @Override + public HeaderResponse setObjectAcl(final String bucketName, final String objectKey, final AccessControlList acl, + final String versionId) throws ObsException { + return this.setObjectAcl(new SetObjectAclRequest(bucketName, objectKey, acl, versionId)); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#setObjectAcl(java.lang.String, + * java.lang.String, com.obs.services.model.AccessControlList) + */ + @Override + public HeaderResponse setObjectAcl(final String bucketName, final String objectKey, final AccessControlList acl) + throws ObsException { + return this.setObjectAcl(new SetObjectAclRequest(bucketName, objectKey, acl)); + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#getObjectMetadata(com.obs.services.model. + * GetObjectMetadataRequest) + */ + @Override + public ObjectMetadata getObjectMetadata(final GetObjectMetadataRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "GetObjectMetadataRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + return this.doActionWithResult("getObjectMetadata", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public ObjectMetadata action() throws ServiceException { + return AbstractObjectClient.this.getObjectMetadataImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see + * com.obs.services.IObsClient#setObjectMetadata(com.obs.services.model. + * SetObjectMetadataRequest) + */ + @Override + public ObjectMetadata setObjectMetadata(final SetObjectMetadataRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "SetObjectMetadataRequest is null"); + return this.doActionWithResult("setObjectMetadata", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public ObjectMetadata action() throws ServiceException { + return AbstractObjectClient.this.setObjectMetadataImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getObjectMetadata(java.lang.String, + * java.lang.String, java.lang.String) + */ + @Override + public ObjectMetadata getObjectMetadata(String bucketName, String objectKey, String versionId) throws ObsException { + GetObjectMetadataRequest request = new GetObjectMetadataRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setVersionId(versionId); + return this.getObjectMetadata(request); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#getObjectMetadata(java.lang.String, + * java.lang.String) + */ + @Override + public ObjectMetadata getObjectMetadata(String bucketName, String objectKey) throws ObsException { + return this.getObjectMetadata(bucketName, objectKey, null); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#appendObject(com.obs.services.model. + * AppendObjectRequest) + */ + @Override + public AppendObjectResult appendObject(final AppendObjectRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "AppendObjectRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + + return this.doActionWithResult("appendObject", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public AppendObjectResult action() throws ServiceException { + if (null != request.getInput() && null != request.getFile()) { + throw new ServiceException("Both input and file are set, only one is allowed"); + } + return AbstractObjectClient.this.appendObjectImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#restoreObject(com.obs.services.model. + * RestoreObjectRequest) + */ + public RestoreObjectRequest.RestoreObjectStatus restoreObject( + final RestoreObjectRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "RestoreObjectRequest is null"); + return this.doActionWithResult("restoreObject", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public RestoreObjectRequest.RestoreObjectStatus action() + throws ServiceException { + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + return AbstractObjectClient.this.restoreObjectImpl(request); + } + }); + } + + /* + * (non-Javadoc) + * + * @see com.obs.services.IObsClient#restoreObjectV2(com.obs.services.model. + * RestoreObjectRequest) + */ + @Override + public RestoreObjectResult restoreObjectV2(final RestoreObjectRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "RestoreObjectRequest is null"); + return this.doActionWithResult("restoreObjectV2", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public RestoreObjectResult action() throws ServiceException { + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + return AbstractObjectClient.this.restoreObjectV2Impl(request); + } + }); + } +} diff --git a/app/src/main/java/com/obs/services/AbstractPFSClient.java b/app/src/main/java/com/obs/services/AbstractPFSClient.java new file mode 100644 index 0000000..d4a54f1 --- /dev/null +++ b/app/src/main/java/com/obs/services/AbstractPFSClient.java @@ -0,0 +1,264 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services; + +import java.io.File; +import java.io.InputStream; + +import com.obs.services.exception.ObsException; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.DeleteObjectRequest; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.ModifyObjectRequest; +import com.obs.services.model.ModifyObjectResult; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.RenameObjectRequest; +import com.obs.services.model.RenameObjectResult; +import com.obs.services.model.TruncateObjectRequest; +import com.obs.services.model.TruncateObjectResult; +import com.obs.services.model.fs.DropFileRequest; +import com.obs.services.model.fs.DropFileResult; +import com.obs.services.model.fs.GetAttributeRequest; +import com.obs.services.model.fs.ListContentSummaryRequest; +import com.obs.services.model.fs.ListContentSummaryResult; +import com.obs.services.model.fs.ObsFSAttribute; +import com.obs.services.model.fs.ObsFSFile; +import com.obs.services.model.fs.ReadFileRequest; +import com.obs.services.model.fs.ReadFileResult; +import com.obs.services.model.fs.RenameRequest; +import com.obs.services.model.fs.RenameResult; +import com.obs.services.model.fs.TruncateFileRequest; +import com.obs.services.model.fs.TruncateFileResult; +import com.obs.services.model.fs.WriteFileRequest; +import com.obs.services.model.fs.ListContentSummaryFsResult; +import com.obs.services.model.fs.ListContentSummaryFsRequest; +import com.obs.services.model.fs.ContentSummaryFsRequest; +import com.obs.services.model.fs.ContentSummaryFsResult; + +public abstract class AbstractPFSClient extends AbstractMultipartObjectClient { + + /* (non-Javadoc) + * @see com.obs.services.IFSClient#listContentSummary(com.obs.services.model.fs.listContentSummaryRequest) + */ + @Override + public ListContentSummaryResult listContentSummary(final ListContentSummaryRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "ListContentSummaryRequest is null"); + return this.doActionWithResult("listcontentsummary", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public ListContentSummaryResult action() throws ServiceException { + return AbstractPFSClient.this.listContentSummaryImpl(request); + } + }); + } + + @Override + public RenameObjectResult renameObject(final String bucketName, final String objectKey, final String newObjectKey) + throws ObsException { + RenameObjectRequest request = new RenameObjectRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setNewObjectKey(newObjectKey); + return this.renameObject(request); + } + + @Override + public RenameObjectResult renameObject(final RenameObjectRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "RenameObjectRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "ObjectKey is null"); + ServiceUtils.assertParameterNotNull2(request.getNewObjectKey(), "NewObjectKey is null"); + + return this.doActionWithResult("renameObject", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public RenameObjectResult action() throws ServiceException { + return AbstractPFSClient.this.renameObjectImpl(request); + } + }); + } + + @Override + public TruncateObjectResult truncateObject(final String bucketName, final String objectKey, final long newLength) + throws ObsException { + TruncateObjectRequest request = new TruncateObjectRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setNewLength(newLength); + return this.truncateObject(request); + } + + @Override + public TruncateObjectResult truncateObject(final TruncateObjectRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "TruncateObjectRequest is null"); + ServiceUtils.assertParameterNotNull(request.getNewLength(), "NewLength is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "ObjectKey is null"); + return this.doActionWithResult("truncateObject", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public TruncateObjectResult action() throws ServiceException { + return AbstractPFSClient.this.truncateObjectImpl(request); + } + }); + } + + @Override + public ModifyObjectResult modifyObject(final String bucketName, final String objectKey, final long position, + File file) throws ObsException { + ModifyObjectRequest request = new ModifyObjectRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setPosition(position); + request.setFile(file); + return this.modifyObject(request); + } + + @Override + public ModifyObjectResult modifyObject(final String bucketName, final String objectKey, final long position, + InputStream input) throws ObsException { + ModifyObjectRequest request = new ModifyObjectRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setPosition(position); + request.setInput(input); + return this.modifyObject(request); + } + + @Override + public ModifyObjectResult modifyObject(final ModifyObjectRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "ModifyObjectRequest is null"); + ServiceUtils.assertParameterNotNull(request.getPosition(), "position is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + return this.doActionWithResult("modifyObject", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public ModifyObjectResult action() throws ServiceException { + return AbstractPFSClient.this.modifyObjectImpl(request); + } + }); + } + + @Override + public ObsFSAttribute getAttribute(GetAttributeRequest request) throws ObsException { + return (ObsFSAttribute) this.getObjectMetadata(request); + } + + @Override + public ReadFileResult readFile(ReadFileRequest request) throws ObsException { + return (ReadFileResult) this.getObject(request); + } + + @Override + public ObsFSFile appendFile(WriteFileRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "WriteFileRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + ObjectMetadata metadata = this + .getObjectMetadata(new GetObjectMetadataRequest(request.getBucketName(), request.getObjectKey())); + if (request.getPosition() >= 0L && request.getPosition() != metadata.getNextPosition()) { + throw new IllegalArgumentException("Where you proposed append to is not equal to length"); + } + request.setPosition(metadata.getNextPosition()); + return this.writeFile(request); + } + + @Override + public RenameResult renameFile(final RenameRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "RenameRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "ObjectKey is null"); + ServiceUtils.assertParameterNotNull2(request.getNewObjectKey(), "NewObjectKey is null"); + + return this.doActionWithResult("rename", request.getBucketName(), new ActionCallbackWithResult() { + @Override + public RenameResult action() throws ServiceException { + return AbstractPFSClient.this.renameFileImpl(request); + } + }); + } + + @Override + public RenameResult renameFolder(RenameRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "RenameRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "ObjectKey is null"); + ServiceUtils.assertParameterNotNull2(request.getNewObjectKey(), "NewObjectKey is null"); + + String delimiter = this.getFileSystemDelimiter(); + if (!request.getObjectKey().endsWith(delimiter)) { + request.setObjectKey(request.getObjectKey() + delimiter); + } + + if (!request.getNewObjectKey().endsWith(delimiter)) { + request.setNewObjectKey(request.getNewObjectKey() + delimiter); + } + + return this.renameFile(request); + } + + @Override + public TruncateFileResult truncateFile(final TruncateFileRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "TruncateFileRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "ObjectKey is null"); + return this.doActionWithResult("truncateFile", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public TruncateFileResult action() throws ServiceException { + return AbstractPFSClient.this.truncateFileImpl(request); + } + }); + } + + @Override + public DropFileResult dropFile(final DropFileRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "DropFileRequest is null"); + return this.doActionWithResult("dropFile", request.getBucketName(), + new ActionCallbackWithResult() { + + @Override + public DropFileResult action() throws ServiceException { + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + return (DropFileResult) AbstractPFSClient.this.deleteObjectImpl(new DeleteObjectRequest( + request.getBucketName(), request.getObjectKey(), request.getVersionId())); + } + }); + } + + @Override + public ListContentSummaryFsResult listContentSummaryFs(final ListContentSummaryFsRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "ListContentSummaryFsRequest is null"); + return this.doActionWithResult("listcontentsummaryfs", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public ListContentSummaryFsResult action() throws ServiceException { + return AbstractPFSClient.this.listContentSummaryFsImpl(request); + } + }); + } + + @Override + public ContentSummaryFsResult getContentSummaryFs(final ContentSummaryFsRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "ContentSummaryFsRequest is null"); + ServiceUtils.assertParameterNotNull(request.getDirName(), "DirName is null"); + return this.doActionWithResult("contentsummaryfs", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public ContentSummaryFsResult action() throws ServiceException { + return AbstractPFSClient.this.getContentSummaryFsImpl(request); + } + }); + } +} diff --git a/app/src/main/java/com/obs/services/BasicObsCredentialsProvider.java b/app/src/main/java/com/obs/services/BasicObsCredentialsProvider.java index 1b6dfc2..6b321b6 100644 --- a/app/src/main/java/com/obs/services/BasicObsCredentialsProvider.java +++ b/app/src/main/java/com/obs/services/BasicObsCredentialsProvider.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; import com.obs.services.internal.security.BasicSecurityKey; diff --git a/app/src/main/java/com/obs/services/DefaultCredentialsProviderChain.java b/app/src/main/java/com/obs/services/DefaultCredentialsProviderChain.java index e4d7dd8..ac9fe3b 100644 --- a/app/src/main/java/com/obs/services/DefaultCredentialsProviderChain.java +++ b/app/src/main/java/com/obs/services/DefaultCredentialsProviderChain.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; import java.util.ArrayList; @@ -23,66 +24,66 @@ public class DefaultCredentialsProviderChain implements IObsCredentialsProvider { - private static final ILogger ILOG = LoggerBuilder.getLogger(DefaultCredentialsProviderChain.class); + private static final ILogger ILOG = LoggerBuilder.getLogger(DefaultCredentialsProviderChain.class); + + private IObsCredentialsProvider lastProvider = null; + + private boolean reused = true; - private IObsCredentialsProvider lastProvider = null; + private final List credentialsProviders = new ArrayList<>(2); - private boolean reused = true; - - private final List credentialsProviders = new ArrayList<>(2); - - public DefaultCredentialsProviderChain(IObsCredentialsProvider... credentialsProviders) { - if (credentialsProviders == null || credentialsProviders.length == 0) { - throw new IllegalArgumentException("No credential providers specified"); - } - - for (IObsCredentialsProvider provider : credentialsProviders) { + public DefaultCredentialsProviderChain(IObsCredentialsProvider... credentialsProviders) { + if (credentialsProviders == null || credentialsProviders.length == 0) { + throw new IllegalArgumentException("No credential providers specified"); + } + + for (IObsCredentialsProvider provider : credentialsProviders) { this.credentialsProviders.add(provider); } - } - - public DefaultCredentialsProviderChain(boolean reused, IObsCredentialsProvider... credentialsProviders) { - this(credentialsProviders); - this.reused = reused; - } - - @Override - public void setSecurityKey(ISecurityKey securityKey) { - throw new UnsupportedOperationException("OBSCredentialsProviderChain class does not support this method"); - } - - @Override - public ISecurityKey getSecurityKey() { - if(reused && lastProvider != null) { - return this.lastProvider.getSecurityKey(); - } - - for (IObsCredentialsProvider provider : credentialsProviders) { - try { - ISecurityKey credentials = provider.getSecurityKey(); - - if (credentials.getAccessKey() != null && credentials.getSecretKey() != null) { - ILOG.debug("Loading credentials from " + provider.toString()); - - this.lastProvider = provider; - return lastProvider.getSecurityKey(); - } - } catch (Exception e) { - if (ILOG.isWarnEnabled()) { - ILOG.warn("Loading credentials from " + provider.toString(), e); - } - } - } - - if (ILOG.isErrorEnabled()) { - ILOG.error("No credential providers specified"); - } - - this.lastProvider = null; - throw new ObsException("No credential providers specified"); - } - - public void refresh() { + } + + public DefaultCredentialsProviderChain(boolean reused, IObsCredentialsProvider... credentialsProviders) { + this(credentialsProviders); + this.reused = reused; + } + + @Override + public void setSecurityKey(ISecurityKey securityKey) { + throw new UnsupportedOperationException("OBSCredentialsProviderChain class does not support this method"); + } + + @Override + public ISecurityKey getSecurityKey() { + if (reused && lastProvider != null) { + return this.lastProvider.getSecurityKey(); + } + + for (IObsCredentialsProvider provider : credentialsProviders) { + try { + ISecurityKey credentials = provider.getSecurityKey(); + + if (credentials.getAccessKey() != null && credentials.getSecretKey() != null) { + ILOG.debug("Loading credentials from " + provider.toString()); + + this.lastProvider = provider; + return lastProvider.getSecurityKey(); + } + } catch (Exception e) { + if (ILOG.isWarnEnabled()) { + ILOG.warn("Loading credentials from " + provider.toString(), e); + } + } + } + + if (ILOG.isErrorEnabled()) { + ILOG.error("No credential providers specified"); + } + + this.lastProvider = null; + throw new ObsException("No credential providers specified"); + } + + public void refresh() { this.lastProvider = null; } } diff --git a/app/src/main/java/com/obs/services/EcsObsCredentialsProvider.java b/app/src/main/java/com/obs/services/EcsObsCredentialsProvider.java index e5f73d5..e4a2e07 100644 --- a/app/src/main/java/com/obs/services/EcsObsCredentialsProvider.java +++ b/app/src/main/java/com/obs/services/EcsObsCredentialsProvider.java @@ -11,30 +11,40 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.concurrent.atomic.AtomicBoolean; + import com.obs.log.ILogger; import com.obs.log.LoggerBuilder; -import com.obs.services.internal.ServiceException; import com.obs.services.internal.security.EcsSecurityUtils; +import com.obs.services.internal.security.LimitedTimeSecurityKey; import com.obs.services.internal.security.SecurityKey; import com.obs.services.internal.security.SecurityKeyBean; import com.obs.services.internal.utils.JSONChange; import com.obs.services.model.ISecurityKey; -import com.obs.services.internal.security.LimitedTimeSecurityKey; - -import java.io.IOException; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; public class EcsObsCredentialsProvider implements IObsCredentialsProvider { private volatile LimitedTimeSecurityKey securityKey; private AtomicBoolean getNewKeyFlag = new AtomicBoolean(false); - private static final ILogger ILOG = LoggerBuilder.getLogger(ObsClient.class); + private static final ILogger ILOG = LoggerBuilder.getLogger(EcsObsCredentialsProvider.class); + + // default is -1, not retry + private int maxRetryTimes = -1; + + public EcsObsCredentialsProvider() { + this.maxRetryTimes = 3; + } + + public EcsObsCredentialsProvider(int maxRetryTimes) { + this.maxRetryTimes = maxRetryTimes; + } @Override public void setSecurityKey(ISecurityKey securityKey) { @@ -43,51 +53,57 @@ public void setSecurityKey(ISecurityKey securityKey) { @Override public ISecurityKey getSecurityKey() { - if (securityKey == null || securityKey.willSoonExpire()) { - synchronized (this) { + if (getNewKeyFlag.compareAndSet(false, true)) { + try { if (securityKey == null || securityKey.willSoonExpire()) { - securityKey = getNewSecurityKey(); + refresh(false); + } else if (securityKey.aboutToExpire()) { + refresh(true); } + } finally { + getNewKeyFlag.set(false); + } + } else { + if (ILOG.isDebugEnabled()) { + ILOG.debug("some other thread is refreshing."); } - } else if (securityKey.aboutToExpire()) { - refresh(); } return securityKey; } - private void refresh() { - if (getNewKeyFlag.compareAndSet(false, true)) { + /** + * fefresh + * + * @param ignoreException + * ignore exception + */ + private void refresh(boolean ignoreException) { + int times = 0; + do { try { securityKey = getNewSecurityKey(); - } finally { - getNewKeyFlag.set(false); + } catch (IOException | RuntimeException e) { + ILOG.warn("refresh new security key failed. times : " + times + "; maxRetryTimes is : " + maxRetryTimes + + "; ignoreException : " + ignoreException, e); + + if (times >= this.maxRetryTimes) { + ILOG.error("refresh new security key failed.", e); + if (!ignoreException) { + throw new IllegalArgumentException(e); + } + } } - } + } while (times++ < maxRetryTimes && maxRetryTimes > 0); } - private LimitedTimeSecurityKey getNewSecurityKey() { - SecurityKey securityInfo = null; - String detail = null; - try { - List list = EcsSecurityUtils.getSecurityKeyInfoWithDetail(); - if (list != null && list.size() == 2) { - String securityKeyInfo = list.get(0); - detail = list.get(1); - securityInfo = (SecurityKey) JSONChange.jsonToObj(new SecurityKey(), securityKeyInfo); - } - } catch (ServiceException se) { - String errorMessage = "Get securityKey form ECS failed :" + se.getMessage() + " \n the detail : " + detail; - ILOG.warn(errorMessage); - throw new IllegalArgumentException(errorMessage, se); - } catch (IOException e) { - String errorMessage = "Get securityKey form ECS failed :" + e.getMessage() + " \n the detail : " + detail; - ILOG.warn(errorMessage); - throw new IllegalArgumentException(errorMessage, e); - } + private LimitedTimeSecurityKey getNewSecurityKey() throws IOException, IllegalArgumentException { + + String content = EcsSecurityUtils.getSecurityKeyInfoWithDetail(); + SecurityKey securityInfo = (SecurityKey) JSONChange.jsonToObj(new SecurityKey(), content); if (securityInfo == null) { - throw new IllegalArgumentException("Invalid securityKey"); + throw new IllegalArgumentException("Invalid securityKey : " + content); } Date expiryDate = null; @@ -108,6 +124,7 @@ private LimitedTimeSecurityKey getNewSecurityKey() { strAccess.append(accessKey.substring(2 * length / 3, length - 1)); ILOG.warn("the AccessKey : " + strAccess.toString() + "will expiry at UTC time : " + expiryDate); - return new LimitedTimeSecurityKey(bean.getAccessKey(), bean.getSecretKey(), bean.getSecurityToken(), expiryDate); + return new LimitedTimeSecurityKey(bean.getAccessKey(), bean.getSecretKey(), bean.getSecurityToken(), + expiryDate); } } diff --git a/app/src/main/java/com/obs/services/EnvironmentVariableObsCredentialsProvider.java b/app/src/main/java/com/obs/services/EnvironmentVariableObsCredentialsProvider.java index b8151b2..e727e7e 100644 --- a/app/src/main/java/com/obs/services/EnvironmentVariableObsCredentialsProvider.java +++ b/app/src/main/java/com/obs/services/EnvironmentVariableObsCredentialsProvider.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; import com.obs.services.internal.ObsConstraint; @@ -19,38 +20,38 @@ import com.obs.services.model.ISecurityKey; public class EnvironmentVariableObsCredentialsProvider implements IObsCredentialsProvider { - volatile private BasicSecurityKey securityKey; - - @Override - public void setSecurityKey(ISecurityKey securityKey) { - throw new UnsupportedOperationException( - "EnvironmentVariableObsCredentialsProvider class does not support this method"); - } - - @Override - public ISecurityKey getSecurityKey() { - if (securityKey == null) { - synchronized (this) { - if (securityKey == null) { - String accessKey = stringTrim(System.getenv(ObsConstraint.ACCESS_KEY_ENV_VAR)); - String secretKey = stringTrim(System.getenv(ObsConstraint.SECRET_KEY_ENV_VAR)); - String securityToken = stringTrim(System.getenv(ObsConstraint.SECURITY_TOKEN_ENV_VAR)); - - ServiceUtils.asserParameterNotNull(accessKey, "access key should not be null or empty."); - ServiceUtils.asserParameterNotNull(secretKey, "secret key should not be null or empty."); - - securityKey = new BasicSecurityKey(accessKey, secretKey, securityToken); - } - } - } - - return securityKey; - } - - private static String stringTrim(String value) { - if (value == null) { - return null; - } - return value.trim(); - } + volatile private BasicSecurityKey securityKey; + + @Override + public void setSecurityKey(ISecurityKey securityKey) { + throw new UnsupportedOperationException( + "EnvironmentVariableObsCredentialsProvider class does not support this method"); + } + + @Override + public ISecurityKey getSecurityKey() { + if (securityKey == null) { + synchronized (this) { + if (securityKey == null) { + String accessKey = stringTrim(System.getenv(ObsConstraint.ACCESS_KEY_ENV_VAR)); + String secretKey = stringTrim(System.getenv(ObsConstraint.SECRET_KEY_ENV_VAR)); + String securityToken = stringTrim(System.getenv(ObsConstraint.SECURITY_TOKEN_ENV_VAR)); + + ServiceUtils.assertParameterNotNull(accessKey, "access key should not be null or empty."); + ServiceUtils.assertParameterNotNull(secretKey, "secret key should not be null or empty."); + + securityKey = new BasicSecurityKey(accessKey, secretKey, securityToken); + } + } + } + + return securityKey; + } + + private static String stringTrim(String value) { + if (value == null) { + return null; + } + return value.trim(); + } } diff --git a/app/src/main/java/com/obs/services/HttpProxyConfiguration.java b/app/src/main/java/com/obs/services/HttpProxyConfiguration.java index 8a01b07..bde22a5 100644 --- a/app/src/main/java/com/obs/services/HttpProxyConfiguration.java +++ b/app/src/main/java/com/obs/services/HttpProxyConfiguration.java @@ -11,178 +11,196 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; /** * HTTP proxy configuration */ -public class HttpProxyConfiguration -{ - +public class HttpProxyConfiguration { + private String proxyAddr; - + private int proxyPort; - + private String proxyUname; - - private String userPaaswd; - + + private String userPasswd; + private String domain; - + private String workstation; - - public HttpProxyConfiguration() - { + + public HttpProxyConfiguration() { } - + /** * - * @param proxyAddr Proxy address - * @param proxyPort Proxy port - * @param proxyUname Proxy username - * @param userPaaswd Proxy password - * @param domain Proxy domain + * @param proxyAddr + * Proxy address + * @param proxyPort + * Proxy port + * @param proxyUname + * Proxy username + * @param userPasswd + * Proxy password + * @param domain + * Proxy domain */ - public HttpProxyConfiguration(String proxyAddr, int proxyPort, String proxyUname, String userPaaswd, String domain) - { + public HttpProxyConfiguration(String proxyAddr, int proxyPort, String proxyUname, String userPasswd, + String domain) { this.proxyAddr = proxyAddr; this.proxyPort = proxyPort; this.proxyUname = proxyUname; - this.userPaaswd = userPaaswd; + this.userPasswd = userPasswd; this.domain = domain; this.workstation = this.proxyAddr; } - + /** * Parameterized constructor * - * @param proxyAddr Proxy address - * @param proxyPort Proxy port - * @param proxyUname Proxy username - * @param userPaaswd Proxy password - * @param domain Proxy domain - * @param workstation Workstation where the proxy is resides + * @param proxyAddr + * Proxy address + * @param proxyPort + * Proxy port + * @param proxyUname + * Proxy username + * @param userPaaswd + * Proxy password + * @param domain + * Proxy domain + * @param workstation + * Workstation where the proxy is resides */ - public HttpProxyConfiguration(String proxyAddr, int proxyPort, String proxyUname, String userPaaswd, String domain, String workstation) - { - this(proxyAddr, proxyPort, proxyUname, userPaaswd, domain); + public HttpProxyConfiguration(String proxyAddr, int proxyPort, String proxyUname, String userPasswd, String domain, + String workstation) { + this(proxyAddr, proxyPort, proxyUname, userPasswd, domain); this.workstation = this.proxyAddr; } - + /** * Obtain the proxy address. * * @return Proxy address */ - public String getProxyAddr() - { + public String getProxyAddr() { return proxyAddr; } - + /** * Set the proxy address. * - * @param proxyAddr Proxy address + * @param proxyAddr + * Proxy address */ - public void setProxyAddr(String proxyAddr) - { + public void setProxyAddr(String proxyAddr) { this.proxyAddr = proxyAddr; } - + /** * Obtain the proxy port. * * @return Proxy port */ - public int getProxyPort() - { + public int getProxyPort() { return proxyPort; } - + /** * Set the proxy port. * - * @param proxyPort Proxy port + * @param proxyPort + * Proxy port */ - public void setProxyPort(int proxyPort) - { + public void setProxyPort(int proxyPort) { this.proxyPort = proxyPort; } - + /** * Obtain the proxy username. * * @return Proxy username */ - public String getProxyUName() - { + public String getProxyUName() { return proxyUname; } - + /** * Set the username. * - * @param proxyUName Proxy username + * @param proxyUName + * Proxy username */ - public void setProxyUName(String proxyUName) - { + public void setProxyUName(String proxyUName) { this.proxyUname = proxyUName; } - + /** * Obtain the proxy password. + * * @return Proxy password */ - public String getUserPaaswd() - { - return userPaaswd; + public String getUserPasswd() { + return userPasswd; } - + + @Deprecated + public String getUserPaaswd() { + return getUserPasswd(); + } + /** * Set the proxy password. - * @param userPaaswd Proxy password + * + * @param userPasswd + * Proxy password */ - public void setUserPaaswd(String userPaaswd) - { - this.userPaaswd = userPaaswd; + public void setUserPasswd(String userPasswd) { + this.userPasswd = userPasswd; } - + + @Deprecated + public void setUserPaaswd(String userPasswd) { + setUserPasswd(userPasswd); + } + /** * Obtain the proxy domain. * * @return Proxy domain */ - public String getDomain() - { + public String getDomain() { return domain; } - + /** * Set the proxy domain. * - * @param domain Proxy domain + * @param domain + * Proxy domain */ - public void setDomain(String domain) - { + public void setDomain(String domain) { this.domain = domain; } /** * Obtain the proxy workstation. + * * @return Proxy workstation */ - public String getWorkstation() - { + public String getWorkstation() { return workstation; } /** * Set the proxy workstation. - * @param workstation Workstation where the proxy is resides + * + * @param workstation + * Workstation where the proxy is resides */ - public void setWorkstation(String workstation) - { + public void setWorkstation(String workstation) { this.workstation = workstation; } - + } diff --git a/app/src/main/java/com/obs/services/IFSClient.java b/app/src/main/java/com/obs/services/IFSClient.java index 05e2c64..764287b 100644 --- a/app/src/main/java/com/obs/services/IFSClient.java +++ b/app/src/main/java/com/obs/services/IFSClient.java @@ -3,14 +3,15 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; import java.io.IOException; @@ -18,6 +19,8 @@ import com.obs.services.exception.ObsException; import com.obs.services.model.HeaderResponse; import com.obs.services.model.TaskProgressStatus; +import com.obs.services.model.fs.ListContentSummaryRequest; +import com.obs.services.model.fs.ListContentSummaryResult; import com.obs.services.model.fs.ObsFSAttribute; import com.obs.services.model.fs.DropFileRequest; import com.obs.services.model.fs.DropFileResult; @@ -39,128 +42,174 @@ import com.obs.services.model.fs.TruncateFileRequest; import com.obs.services.model.fs.TruncateFileResult; import com.obs.services.model.fs.WriteFileRequest; +import com.obs.services.model.fs.ContentSummaryFsResult; +import com.obs.services.model.fs.ContentSummaryFsRequest; +import com.obs.services.model.fs.ListContentSummaryFsResult; +import com.obs.services.model.fs.ListContentSummaryFsRequest; /** * Gateway interface for OBS files */ public interface IFSClient { - /** - * Disable ObsClient and release connection resources. - * @throws IOException - */ - void close() throws IOException; - - /** - * Create a bucket. - * @param request Request parameters for creating a bucket - * @return Bucket supporting the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - ObsFSBucket newBucket(NewBucketRequest request) throws ObsException; - - /** - * Specify whether to enable the file gateway feature for the bucket. - * @param request Request parameters for specifying whether to enable the file gateway feature for the bucket - * @return Common response headers - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - HeaderResponse setBucketFSStatus(SetBucketFSStatusRequest request) throws ObsException; - - /** - * Check whether the file gateway feature is enabled for the bucket. - * @param request Request parameters for checking whether the file gateway feature is enabled for the bucket - * @return Response to the check of whether the file gateway feature is enabled for the bucket - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - GetBucketFSStatusResult getBucketFSStatus(GetBucketFSStatusRequest request) throws ObsException; - - /** - * Create a file. - * @param request Request parameters for creating a file - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - ObsFSFile newFile(NewFileRequest request) throws ObsException; - - /** - * Create a folder. - * @param request Request parameters for creating a folder - * @return Folders in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - ObsFSFolder newFolder(NewFolderRequest request) throws ObsException; - - /** - * Obtain file or folder properties. - * @param request Request parameters for obtaining filer or folder properties - * @return File or folder properties - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - ObsFSAttribute getAttribute(GetAttributeRequest request) throws ObsException; - - - /** - * Obtain the file content. - * @param request Request parameters for obtaining the file content - * @return Response to the request for obtaining file content - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - ReadFileResult readFile(ReadFileRequest request) throws ObsException; - - /** - * Write data to a file. - * @param request Request parameters for writing data to a file - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - ObsFSFile writeFile(WriteFileRequest request) throws ObsException; - - /** - * Append data to a file. - * @param request Request parameters for writing data to a file - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - ObsFSFile appendFile(WriteFileRequest request) throws ObsException; - - /** - * Rename a file. - * @param request Request parameters for renaming a file - * @return Response to the request for renaming a file - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - RenameResult renameFile(RenameRequest request) throws ObsException; - - /** - * Rename a folder. - * @param request Request parameters for renaming a folder - * @return Response to the request for renaming a folder - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - RenameResult renameFolder(RenameRequest request) throws ObsException; - - /** - * Truncate a file. - * @param request Request parameters for truncating a file - * @return Response to the request for truncating a file - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - TruncateFileResult truncateFile(TruncateFileRequest request) throws ObsException; - - /** - * Delete a file. - * @param request Request parameters for deleting a file - * @return Response to the request for deleting a file - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - DropFileResult dropFile(DropFileRequest request) throws ObsException; - - /** - * Delete a folder. - * @param request Request parameters for deleting a folder - * @return Batch task execution status - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - TaskProgressStatus dropFolder(DropFolderRequest request) throws ObsException; - + /** + * Disable ObsClient and release connection resources. + * @throws IOException + */ + void close() throws IOException; + + /** + * Create a bucket. + * @param request Request parameters for creating a bucket + * @return Bucket supporting the file interface + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + ObsFSBucket newBucket(NewBucketRequest request) throws ObsException; + + /** + * Specify whether to enable the file gateway feature for the bucket. + * @param request Request parameters for specifying whether to enable the file gateway feature for the bucket + * @return Common response headers + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + HeaderResponse setBucketFSStatus(SetBucketFSStatusRequest request) throws ObsException; + + /** + * Check whether the file gateway feature is enabled for the bucket. + * @param request Request parameters for checking whether the file gateway feature is enabled for the bucket + * @return Response to the check of whether the file gateway feature is enabled for the bucket + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + GetBucketFSStatusResult getBucketFSStatus(GetBucketFSStatusRequest request) throws ObsException; + + /** + * Create a file. + * @param request Request parameters for creating a file + * @return Files in the bucket that supports the file interface + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + ObsFSFile newFile(NewFileRequest request) throws ObsException; + + /** + * Create a folder. + * @param request Request parameters for creating a folder + * @return Folders in the bucket that supports the file interface + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + ObsFSFolder newFolder(NewFolderRequest request) throws ObsException; + + /** + * Obtain file or folder properties. + * @param request Request parameters for obtaining filer or folder properties + * @return File or folder properties + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + ObsFSAttribute getAttribute(GetAttributeRequest request) throws ObsException; + + /** + * Obtain the file content. + * @param request Request parameters for obtaining the file content + * @return Response to the request for obtaining file content + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + ReadFileResult readFile(ReadFileRequest request) throws ObsException; + + /** + * Write data to a file. + * @param request Request parameters for writing data to a file + * @return Files in the bucket that supports the file interface + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + ObsFSFile writeFile(WriteFileRequest request) throws ObsException; + + /** + * Append data to a file. + * @param request Request parameters for writing data to a file + * @return Files in the bucket that supports the file interface + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + ObsFSFile appendFile(WriteFileRequest request) throws ObsException; + + /** + * Rename a file. + * @param request Request parameters for renaming a file + * @return Response to the request for renaming a file + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + RenameResult renameFile(RenameRequest request) throws ObsException; + + /** + * Rename a folder. + * @param request Request parameters for renaming a folder + * @return Response to the request for renaming a folder + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + RenameResult renameFolder(RenameRequest request) throws ObsException; + + /** + * Truncate a file. + * @param request Request parameters for truncating a file + * @return Response to the request for truncating a file + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + TruncateFileResult truncateFile(TruncateFileRequest request) throws ObsException; + + /** + * Delete a file. + * @param request Request parameters for deleting a file + * @return Response to the request for deleting a file + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + DropFileResult dropFile(DropFileRequest request) throws ObsException; + + /** + * Delete a folder. + * @param request Request parameters for deleting a folder + * @return Batch task execution status + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + TaskProgressStatus dropFolder(DropFolderRequest request) throws ObsException; + + /** + * obtain folder contentSummary + * + * @param request Request parameters for obtain folder contentSummary + * @return Response to the request for obtain folder contentSummary + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + * @since 3.20.5 + */ + ListContentSummaryResult listContentSummary(ListContentSummaryRequest request) throws ObsException; + + /** + * obtain folder contentSummary + * + * @param request Request parameters for obtain folder contentSummary + * @return Response to the request for obtain folder contentSummary + * @throws ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + ListContentSummaryFsResult listContentSummaryFs(ListContentSummaryFsRequest request) throws ObsException; + + /** + * obtain current folder contentSummary + * @param request Request parameters for obtain current folder contentSummary + * @return Response to the request for obtain current folder contentSummary + * @throws ObsException ObsException OBS SDK self-defined exception, + * thrown when the interface fails to be called or access to OBS fails + */ + ContentSummaryFsResult getContentSummaryFs(ContentSummaryFsRequest request) throws ObsException; } diff --git a/app/src/main/java/com/obs/services/IObsBucketExtendClient.java b/app/src/main/java/com/obs/services/IObsBucketExtendClient.java new file mode 100644 index 0000000..1e3a4dc --- /dev/null +++ b/app/src/main/java/com/obs/services/IObsBucketExtendClient.java @@ -0,0 +1,86 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services; + +import com.obs.services.exception.ObsException; +import com.obs.services.model.BucketCustomDomainInfo; +import com.obs.services.model.DeleteBucketCustomDomainRequest; +import com.obs.services.model.GetBucketCustomDomainRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.SetBucketCustomDomainRequest; + +/** + * + * + * @since 3.21.8 + */ +public interface IObsBucketExtendClient { + /** + * Deleting the Custom Domain Name of a Bucket + * @param bucketName Bucket name + * @param domainName Custom Domain Name + * @return + * @throws ObsException + * @since 3.21.8 + */ + HeaderResponse deleteBucketCustomDomain(String bucketName, String domainName) throws ObsException; + + /** + * Deleting the Custom Domain Name of a Bucket + * @param request + * @return + * @throws ObsException + * @since 3.21.8 + */ + HeaderResponse deleteBucketCustomDomain(DeleteBucketCustomDomainRequest request) throws ObsException; + + /** + * Obtaining the Custom Domain Name of a Bucket + * @param bucketName Bucket name + * @return + * @throws ObsException + * @since 3.21.8 + */ + BucketCustomDomainInfo getBucketCustomDomain(String bucketName) throws ObsException; + + /** + * Obtaining the Custom Domain Name of a Bucket + * @param request + * @return + * @throws ObsException + * @since 3.21.8 + */ + BucketCustomDomainInfo getBucketCustomDomain(GetBucketCustomDomainRequest request) throws ObsException; + + /** + * Configuring a Custom Domain Name for a Bucket + * @param bucketName Bucket name + * @param domainName Custom Domain Name + * @return + * @throws ObsException + * @since 3.21.8 + */ + HeaderResponse setBucketCustomDomain(String bucketName, String domainName) throws ObsException; + + /** + * Configuring a Custom Domain Name for a Bucket + * @param request + * @return + * @throws ObsException + * @since 3.21.8 + */ + HeaderResponse setBucketCustomDomain(SetBucketCustomDomainRequest request) throws ObsException; +} diff --git a/app/src/main/java/com/obs/services/IObsClient.java b/app/src/main/java/com/obs/services/IObsClient.java index c39f37c..0863666 100644 --- a/app/src/main/java/com/obs/services/IObsClient.java +++ b/app/src/main/java/com/obs/services/IObsClient.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; import java.io.File; @@ -23,6 +24,7 @@ import com.obs.services.model.AccessControlList; import com.obs.services.model.AppendObjectRequest; import com.obs.services.model.AppendObjectResult; +import com.obs.services.model.BaseBucketRequest; import com.obs.services.model.BucketCors; import com.obs.services.model.BucketDirectColdAccess; import com.obs.services.model.BucketEncryption; @@ -44,11 +46,13 @@ import com.obs.services.model.CopyPartRequest; import com.obs.services.model.CopyPartResult; import com.obs.services.model.CreateBucketRequest; +import com.obs.services.model.DeleteObjectRequest; import com.obs.services.model.DeleteObjectResult; import com.obs.services.model.DeleteObjectsRequest; import com.obs.services.model.DeleteObjectsResult; import com.obs.services.model.DownloadFileRequest; import com.obs.services.model.DownloadFileResult; +import com.obs.services.model.GetObjectAclRequest; import com.obs.services.model.GetObjectMetadataRequest; import com.obs.services.model.GetObjectRequest; import com.obs.services.model.HeaderResponse; @@ -81,10 +85,28 @@ import com.obs.services.model.RenameObjectRequest; import com.obs.services.model.RenameObjectResult; import com.obs.services.model.ReplicationConfiguration; +import com.obs.services.model.RequestPaymentConfiguration; +import com.obs.services.model.RequestPaymentEnum; import com.obs.services.model.RestoreObjectRequest; import com.obs.services.model.RestoreObjectRequest.RestoreObjectStatus; import com.obs.services.model.RestoreObjectResult; import com.obs.services.model.RestoreObjectsRequest; +import com.obs.services.model.SetBucketAclRequest; +import com.obs.services.model.SetBucketCorsRequest; +import com.obs.services.model.SetBucketDirectColdAccessRequest; +import com.obs.services.model.SetBucketEncryptionRequest; +import com.obs.services.model.SetBucketLifecycleRequest; +import com.obs.services.model.SetBucketLoggingRequest; +import com.obs.services.model.SetBucketNotificationRequest; +import com.obs.services.model.SetBucketPolicyRequest; +import com.obs.services.model.SetBucketQuotaRequest; +import com.obs.services.model.SetBucketReplicationRequest; +import com.obs.services.model.SetBucketRequestPaymentRequest; +import com.obs.services.model.SetBucketStoragePolicyRequest; +import com.obs.services.model.SetBucketTaggingRequest; +import com.obs.services.model.SetBucketVersioningRequest; +import com.obs.services.model.SetBucketWebsiteRequest; +import com.obs.services.model.SetObjectAclRequest; import com.obs.services.model.SetObjectMetadataRequest; import com.obs.services.model.TaskProgressStatus; import com.obs.services.model.TemporarySignatureRequest; @@ -100,8 +122,9 @@ /** * Basic OBS interface */ -public interface IObsClient { - +//CHECKSTYLE:OFF +public interface IObsClient extends IObsBucketExtendClient { + /** * * Refresh the temporary access key. @@ -123,7 +146,8 @@ public interface IObsClient { * Parameters in a request for temporarily authorized access * @return Response to the request for temporarily authorized access * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ TemporarySignatureResponse createTemporarySignature(TemporarySignatureRequest request); @@ -134,7 +158,8 @@ public interface IObsClient { * Request parameters for V4 browser-based authorized access * @return Response to the V4 browser-based authorized access * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ PostSignatureResponse createPostSignature(PostSignatureRequest request) throws ObsException; @@ -150,14 +175,16 @@ public interface IObsClient { *

  • Cannot be an IP address. *
  • Cannot end with a hyphen (-). *
  • Cannot contain two consecutive periods (..). - *
  • Cannot contain periods (.) and hyphens (-) adjacent to each other, for example, "my-.bucket" and "my.-bucket". + *
  • Cannot contain periods (.) and hyphens (-) adjacent to each other, + * for example, "my-.bucket" and "my.-bucket". * * * @param bucketName * Bucket name * @return Bucket information * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObsBucket createBucket(String bucketName) throws ObsException; @@ -174,17 +201,20 @@ public interface IObsClient { *
  • Cannot be an IP address.s *
  • Cannot end with a hyphen (-). *
  • Cannot contain two consecutive periods (..). - *
  • Cannot contain periods (.) and hyphens (-) adjacent to each other, for example, "my-.bucket" and "my.-bucket". + *
  • Cannot contain periods (.) and hyphens (-) adjacent to each other, + * for example, "my-.bucket" and "my.-bucket". * * * * @param bucketName * Bucket name * @param location - * Bucket location. This parameter is mandatory unless the endpoint belongs to the default region. + * Bucket location. This parameter is mandatory unless the + * endpoint belongs to the default region. * @return Bucket information * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObsBucket createBucket(String bucketName, String location) throws ObsException; @@ -201,14 +231,16 @@ public interface IObsClient { *
  • Cannot be an IP address. *
  • Cannot end with a hyphen (-). *
  • Cannot contain two consecutive periods (..). - *
  • Cannot contain periods (.) and hyphens (-) adjacent to each other, for example, "my-.bucket" and "my.-bucket". + *
  • Cannot contain periods (.) and hyphens (-) adjacent to each other, + * for example, "my-.bucket" and "my.-bucket". * * * @param bucket * Bucket information, including the request parameters * @return Bucket information * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObsBucket createBucket(ObsBucket bucket) throws ObsException; @@ -224,41 +256,130 @@ public interface IObsClient { *
  • Cannot be an IP address. *
  • Cannot end with a hyphen (-). *
  • Cannot contain two consecutive periods (..). - *
  • Cannot contain periods (.) and hyphens (-) adjacent to each other, for example, "my-.bucket" and "my.-bucket". + *
  • Cannot contain periods (.) and hyphens (-) adjacent to each other, + * for example, "my-.bucket" and "my.-bucket". * * * @param request * Request parameters for creating a bucket * @return Bucket information * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails * */ ObsBucket createBucket(CreateBucketRequest request) throws ObsException; + /** + * Rename a file or directory. Only the parallel file system supports this interface. + * + * @param bucketName + * Bucket name + * @param objectKey + * File name or directory name + * @param newObjectKey + * Name of the renamed file or directory + * @return Response to the request for renaming a file + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + */ + RenameObjectResult renameObject(String bucketName, String objectKey, String newObjectKey) throws ObsException; - RenameObjectResult renameObject(String bucketName, String objectKey, String newObjectKey) throws ObsException; - - - RenameObjectResult renameObject(final RenameObjectRequest request) throws ObsException; - - - TruncateObjectResult truncateObject(String bucketName, String objectKey, long newLength) throws ObsException; - - - TruncateObjectResult truncateObject(final TruncateObjectRequest request) throws ObsException; - - - ModifyObjectResult modifyObject(String bucketName, String objectKey, long position, File file) throws ObsException; + /** + * Rename a file or directory. Only the parallel file system supports this interface. + * + * @param request + * Parameters of a request for renaming a file + * @return Response to the request for renaming a file + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + RenameObjectResult renameObject(final RenameObjectRequest request) throws ObsException; + /** + * Truncate a file. Only the parallel file system supports this interface. + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param newLength + * Size of the truncated file + * @return Response to the request for truncating a file + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + */ + TruncateObjectResult truncateObject(String bucketName, String objectKey, long newLength) throws ObsException; - ModifyObjectResult modifyObject(String bucketName, String objectKey, long position, InputStream input) throws ObsException; + /** + * Truncate a file. Only the parallel file system supports this interface. + * + * @param request + * Parameters of a request for truncating a file + * @return Response to the request for truncating a file + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + TruncateObjectResult truncateObject(final TruncateObjectRequest request) throws ObsException; + /** + * Write a file. Only the parallel file system supports this interface. + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param position + * Start position for writing data to a file + * @param file + * Local file path + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + */ + ModifyObjectResult modifyObject(String bucketName, String objectKey, long position, File file) throws ObsException; - ModifyObjectResult modifyObject(ModifyObjectRequest request) throws ObsException; + /** + * Write a file. Only the parallel file system supports this interface. + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param position + * Start position for writing data to a file + * @param input + * Data stream to be uploaded + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + */ + ModifyObjectResult modifyObject(String bucketName, String objectKey, long position, InputStream input) + throws ObsException; + /** + * Write a file. Only the parallel file system supports this interface. + * + * @param request + * Request parameters for writing data to a file + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + */ + ModifyObjectResult modifyObject(ModifyObjectRequest request) throws ObsException; - List listBuckets(ListBucketsRequest request) throws ObsException; + /** + * Obtain the bucket list. + * + * @param request + * Obtain the request parameters for obtaining the bucket list. + * @return Bucket list + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + */ + List listBuckets(ListBucketsRequest request) throws ObsException; /** * Obtain the bucket list. @@ -267,7 +388,8 @@ public interface IObsClient { * Obtain the request parameters for obtaining the bucket list. * @return Response to the request for obtaining the bucket list * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ListBucketsResult listBucketsV2(ListBucketsRequest request) throws ObsException; @@ -278,10 +400,23 @@ public interface IObsClient { * Bucket name * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse deleteBucket(String bucketName) throws ObsException; + /** + * Delete a bucket. + * + * @param request + * Parameters of a request for deleting a bucket + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse deleteBucket(BaseBucketRequest request) throws ObsException; + /** * List objects in the bucket. * @@ -289,7 +424,8 @@ public interface IObsClient { * Request parameters for listing objects in a bucket * @return Response to the request for listing objects in the bucket * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObjectListing listObjects(ListObjectsRequest request) throws ObsException; @@ -300,7 +436,8 @@ public interface IObsClient { * Bucket name * @return Response to the request for listing objects in the bucket * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObjectListing listObjects(String bucketName) throws ObsException; @@ -311,18 +448,34 @@ public interface IObsClient { * Bucket name * @return Identifier indicating whether the bucket exists * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ boolean headBucket(String bucketName) throws ObsException; + /** + * Identify whether a bucket exists. + * + * @param request + * Request parameters + * @return Identifier indicating whether the bucket exists + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + boolean headBucket(BaseBucketRequest request) throws ObsException; + /** * List versioning objects in a bucket. * * @param request - * Request parameters for listing versioning objects in the bucket - * @return Response to the request for listing versioning objects in the bucket + * Request parameters for listing versioning objects in the + * bucket + * @return Response to the request for listing versioning objects in the + * bucket * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ListVersionsResult listVersions(ListVersionsRequest request) throws ObsException; @@ -331,9 +484,11 @@ public interface IObsClient { * * @param bucketName * Bucket name - * @return Response to the request for listing versioning objects in the bucket + * @return Response to the request for listing versioning objects in the + * bucket * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ListVersionsResult listVersions(String bucketName) throws ObsException; @@ -344,9 +499,11 @@ public interface IObsClient { * Bucket name * @param maxKeys * Maximum number of versioning objects to be listed - * @return Response to the request for listing versioning objects in the bucket + * @return Response to the request for listing versioning objects in the + * bucket * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ListVersionsResult listVersions(String bucketName, long maxKeys) throws ObsException; @@ -360,17 +517,21 @@ public interface IObsClient { * @param delimiter * Character for grouping object names * @param keyMarker - * Start position for listing versioning objects (sorted by object name) + * Start position for listing versioning objects (sorted by + * object name) * @param versionIdMarker - * Start position for listing versioning objects (sorted by version ID) + * Start position for listing versioning objects (sorted by + * version ID) * @param maxKeys * Maximum number of versioning objects to be listed - * @return Response to the request for listing versioning objects in the bucket + * @return Response to the request for listing versioning objects in the + * bucket * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ListVersionsResult listVersions(String bucketName, String prefix, String delimiter, String keyMarker, - String versionIdMarker, long maxKeys) throws ObsException; + String versionIdMarker, long maxKeys) throws ObsException; /** * Obtain bucket metadata. @@ -379,7 +540,8 @@ ListVersionsResult listVersions(String bucketName, String prefix, String delimit * Request parameters for obtaining bucket metadata * @return Response to the request for obtaining bucket metadata * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ BucketMetadataInfoResult getBucketMetadata(BucketMetadataInfoRequest request) throws ObsException; @@ -390,10 +552,23 @@ ListVersionsResult listVersions(String bucketName, String prefix, String delimit * Bucket name * @return Bucket ACL * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ AccessControlList getBucketAcl(String bucketName) throws ObsException; + /** + * Obtain a bucket ACL. + * + * @param request + * Request parameters for obtaining the bucket ACL + * @return Response to a request for obtaining the bucket ACL + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + AccessControlList getBucketAcl(BaseBucketRequest request) throws ObsException; + /** * Set a bucket ACL.
    * @@ -403,10 +578,23 @@ ListVersionsResult listVersions(String bucketName, String prefix, String delimit * ACL * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse setBucketAcl(String bucketName, AccessControlList acl) throws ObsException; + /** + * Set a bucket ACL. + * + * @param request + * Request parameters for setting a bucket ACL + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketAcl(SetBucketAclRequest request) throws ObsException; + /** * Obtain the bucket location. * @@ -414,10 +602,23 @@ ListVersionsResult listVersions(String bucketName, String prefix, String delimit * Bucket name * @return Bucket location * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ String getBucketLocation(String bucketName) throws ObsException; + /** + * Obtain the bucket location. + * + * @param request + * Request parameters + * @return Response to the request for obtaining the bucket location + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + BucketLocationResponse getBucketLocation(BaseBucketRequest request) throws ObsException; + /** * Obtain the bucket location. * @@ -425,7 +626,8 @@ ListVersionsResult listVersions(String bucketName, String prefix, String delimit * Bucket name * @return Response to the request for obtaining the bucket location * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ BucketLocationResponse getBucketLocationV2(String bucketName) throws ObsException; @@ -436,10 +638,23 @@ ListVersionsResult listVersions(String bucketName, String prefix, String delimit * Bucket name * @return Bcket storage information * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ BucketStorageInfo getBucketStorageInfo(String bucketName) throws ObsException; + /** + * Obtain bucket storage information. + * + * @param request + * Bucket name + * @return Bucket storage information + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + BucketStorageInfo getBucketStorageInfo(BaseBucketRequest request) throws ObsException; + /** * Obtain the bucket quota. * @@ -447,10 +662,23 @@ ListVersionsResult listVersions(String bucketName, String prefix, String delimit * Bucket name * @return Bucket quota * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ BucketQuota getBucketQuota(String bucketName) throws ObsException; + /** + * Obtain the bucket quota. + * + * @param request + * Request parameters + * @return Bucket quota + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + BucketQuota getBucketQuota(BaseBucketRequest request) throws ObsException; + /** * Set the bucket quota. * @@ -459,11 +687,24 @@ ListVersionsResult listVersions(String bucketName, String prefix, String delimit * @param bucketQuota * Bucket quota * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails * @return Common response headers */ HeaderResponse setBucketQuota(String bucketName, BucketQuota bucketQuota) throws ObsException; + /** + * Set the bucket quota. + * + * @param request + * Request parameters + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @return Common response headers + * @since 3.20.3 + */ + HeaderResponse setBucketQuota(SetBucketQuotaRequest request) throws ObsException; + /** * Obtain the bucket storage class. * @@ -471,10 +712,23 @@ ListVersionsResult listVersions(String bucketName, String prefix, String delimit * Bucket name * @return Bucket storage policy * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ BucketStoragePolicyConfiguration getBucketStoragePolicy(String bucketName) throws ObsException; + /** + * Obtain the bucket storage class. + * + * @param request + * Request parameters + * @return Bucket storage policy + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + BucketStoragePolicyConfiguration getBucketStoragePolicy(BaseBucketRequest request) throws ObsException; + /** * Set the bucket storage class. * @@ -484,11 +738,24 @@ ListVersionsResult listVersions(String bucketName, String prefix, String delimit * Bucket storage policy * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse setBucketStoragePolicy(String bucketName, BucketStoragePolicyConfiguration bucketStorage) throws ObsException; + /** + * Set the bucket storage class. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketStoragePolicy(SetBucketStoragePolicyRequest request) throws ObsException; + /** * Configure the bucket CORS. * @@ -498,10 +765,23 @@ HeaderResponse setBucketStoragePolicy(String bucketName, BucketStoragePolicyConf * CORS rules * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse setBucketCors(String bucketName, BucketCors bucketCors) throws ObsException; + /** + * Configure the bucket CORS. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketCors(SetBucketCorsRequest request) throws ObsException; + /** * Obtain the bucket CORS rules. * @@ -509,10 +789,23 @@ HeaderResponse setBucketStoragePolicy(String bucketName, BucketStoragePolicyConf * Bucket name * @return Bucket CORS rules * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ BucketCors getBucketCors(String bucketName) throws ObsException; + /** + * Obtain the bucket CORS rules. + * + * @param request + * Request parameters + * @return Bucket CORS configuration + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + BucketCors getBucketCors(BaseBucketRequest request) throws ObsException; + /** * Delete the bucket CORS rules. * @@ -520,10 +813,23 @@ HeaderResponse setBucketStoragePolicy(String bucketName, BucketStoragePolicyConf * Bucket name * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse deleteBucketCors(String bucketName) throws ObsException; + /** + * Delete the bucket CORS rules. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse deleteBucketCors(BaseBucketRequest request) throws ObsException; + /** * Obtain the logging settings of a bucket. * @@ -531,12 +837,25 @@ HeaderResponse setBucketStoragePolicy(String bucketName, BucketStoragePolicyConf * Bucket name * @return Logging settings of the bucket * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ BucketLoggingConfiguration getBucketLogging(String bucketName) throws ObsException; + /** + * Obtain the logging settings of a bucket. + * + * @param request + * Request parameters + * @return Logging settings of the bucket + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + BucketLoggingConfiguration getBucketLogging(BaseBucketRequest request) throws ObsException; + HeaderResponse setBucketLoggingConfiguration(String bucketName, BucketLoggingConfiguration loggingConfiguration, - boolean updateTargetACLifRequired) throws ObsException; + boolean updateTargetACLifRequired) throws ObsException; /** * Configure logging for a bucket.
    @@ -547,11 +866,24 @@ HeaderResponse setBucketLoggingConfiguration(String bucketName, BucketLoggingCon * Logging settings * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse setBucketLogging(String bucketName, BucketLoggingConfiguration loggingConfiguration) throws ObsException; + /** + * Configure logging for a bucket. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketLogging(SetBucketLoggingRequest request) throws ObsException; + /** * Set the versioning status for a bucket. * @@ -561,11 +893,24 @@ HeaderResponse setBucketLogging(String bucketName, BucketLoggingConfiguration lo * Versioning status of the bucket * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfiguration versioningConfiguration) throws ObsException; + /** + * Set the versioning status for a bucket. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketVersioning(SetBucketVersioningRequest request) throws ObsException; + /** * Obtain the versioning status for a bucket. * @@ -573,10 +918,72 @@ HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfigurat * Bucket name * @return Versioning status of the bucket * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ BucketVersioningConfiguration getBucketVersioning(String bucketName) throws ObsException; + /** + * Obtain the versioning status for a bucket. + * + * @param request + * Request parameters + * @return Versioning status of the bucket + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + BucketVersioningConfiguration getBucketVersioning(BaseBucketRequest request) throws ObsException; + + /** + * Configure the requester-pays function for a bucket. + * + * @param bucketName + * Bucket name + * @param payer + * The status of the requester-pays function + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + */ + HeaderResponse setBucketRequestPayment(String bucketName, RequestPaymentEnum payer) throws ObsException; + + /** + * Configure the requester-pays function for a bucket. + * + * @param request + * Configuration of the requester-pays function of a bucket + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketRequestPayment(SetBucketRequestPaymentRequest request) throws ObsException; + + /** + * Obtain the status of the requester-pays function of a bucket. + * + * @param bucketName + * Bucket name + * @return Configuration of the requester-pays function of the bucket + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + RequestPaymentConfiguration getBucketRequestPayment(String bucketName) throws ObsException; + + /** + * Obtain the requester-pays status of a bucket. + * + * @param request + * Basic bucket information + * @return Configuration of the requester-pays function of the bucket + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + RequestPaymentConfiguration getBucketRequestPayment(BaseBucketRequest request) throws ObsException; + /** * Obtain the bucket lifecycle rules. * @@ -584,10 +991,23 @@ HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfigurat * Bucket name * @return Bucket lifecycle rules * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ LifecycleConfiguration getBucketLifecycle(String bucketName) throws ObsException; + /** + * Obtain the bucket lifecycle rules. + * + * @param request + * Request parameters + * @return Bucket lifecycle rules + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + LifecycleConfiguration getBucketLifecycle(BaseBucketRequest request) throws ObsException; + /** * Set the bucket lifecycle rules. * @@ -597,10 +1017,23 @@ HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfigurat * Bucket lifecycle rules * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse setBucketLifecycle(String bucketName, LifecycleConfiguration lifecycleConfig) throws ObsException; + /** + * Configure lifecycle rules for a bucket. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketLifecycle(SetBucketLifecycleRequest request) throws ObsException; + /** * Delete the bucket lifecycle rules from a bucket. * @@ -608,10 +1041,23 @@ HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfigurat * Bucket name * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse deleteBucketLifecycle(String bucketName) throws ObsException; + /** + * Delete the bucket lifecycle rules from a bucket. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse deleteBucketLifecycle(BaseBucketRequest request) throws ObsException; + /** * Obtain bucket policies. * @@ -619,10 +1065,23 @@ HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfigurat * Bucket name * @return Bucket policy, in the JSON format * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ String getBucketPolicy(String bucketName) throws ObsException; + /** + * Obtain a bucket policy. + * + * @param request + * Request parameters + * @return Bucket policy, in the JSON format + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + String getBucketPolicy(BaseBucketRequest request) throws ObsException; + /** * Obtain bucket policies.
    * @@ -630,10 +1089,23 @@ HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfigurat * Bucket name * @return Response to a request for obtaining bucket policies * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ BucketPolicyResponse getBucketPolicyV2(String bucketName) throws ObsException; + /** + * Obtain bucket policies. + * + * @param request + * Request parameters + * @return Response to a request for obtaining bucket policies + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + BucketPolicyResponse getBucketPolicyV2(BaseBucketRequest request) throws ObsException; + /** * Set bucket policies. * @@ -643,10 +1115,23 @@ HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfigurat * Bucket policy, in the JSON format * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse setBucketPolicy(String bucketName, String policy) throws ObsException; + /** + * Set bucket policies. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketPolicy(SetBucketPolicyRequest request) throws ObsException; + /** * Delete bucket policies. * @@ -654,10 +1139,23 @@ HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfigurat * Bucket name * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse deleteBucketPolicy(String bucketName) throws ObsException; + /** + * Delete bucket policies. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse deleteBucketPolicy(BaseBucketRequest request) throws ObsException; + /** * Obtain the website hosting configuration of a Bucket * @@ -665,101 +1163,221 @@ HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfigurat * Bucket name * @return Website hosting configuration of a bucket * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + WebsiteConfiguration getBucketWebsite(String bucketName) throws ObsException; + + /** + * Obtain the website hosting settings of a Bucket + * + * @param request + * Request parameters + * @return Website hosting configuration of a bucket + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + WebsiteConfiguration getBucketWebsite(BaseBucketRequest request) throws ObsException; + + /** + * Configure website hosting for a bucket. + * + * @param bucketName + * Bucket name + * @param websiteConfig + * Website hosting configuration of a bucket + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + HeaderResponse setBucketWebsite(String bucketName, WebsiteConfiguration websiteConfig) throws ObsException; + + /** + * Configure website hosting for a bucket. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketWebsite(SetBucketWebsiteRequest request) throws ObsException; + + /** + * Delete the website hosting configuration of a bucket. + * + * @param bucketName + * Bucket name + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + HeaderResponse deleteBucketWebsite(String bucketName) throws ObsException; + + /** + * Delete the website hosting configuration of a bucket. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse deleteBucketWebsite(BaseBucketRequest request) throws ObsException; + + /** + * Obtain bucket tags. + * + * @param bucketName + * Bucket name + * @return Bucket tag + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + BucketTagInfo getBucketTagging(String bucketName) throws ObsException; + + /** + * Obtain bucket tags. + * + * @param request + * Request parameters + * @return Bucket tag + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + BucketTagInfo getBucketTagging(BaseBucketRequest request) throws ObsException; + + /** + * Set bucket tags. + * + * @param bucketName + * Bucket name + * @param bucketTagInfo + * Bucket tags + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + HeaderResponse setBucketTagging(String bucketName, BucketTagInfo bucketTagInfo) throws ObsException; + + /** + * Set bucket tags. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketTagging(SetBucketTaggingRequest request) throws ObsException; + + /** + * Delete bucket tags. + * + * @param bucketName + * Bucket name + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + HeaderResponse deleteBucketTagging(String bucketName) throws ObsException; + + /** + * Delete bucket tags. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 */ - WebsiteConfiguration getBucketWebsite(String bucketName) throws ObsException; - + HeaderResponse deleteBucketTagging(BaseBucketRequest request) throws ObsException; + + /** - * Configure website hosting for a bucket. - * + * Obtain bucket encryption configuration. + * * @param bucketName * Bucket name - * @param websiteConfig - * Website hosting configuration of a bucket - * @return Common response headers + * @return Bucket encryption configuration + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + BucketEncryption getBucketEncryption(String bucketName) throws ObsException; + + /** + * Obtain bucket encryption configuration. + * + * @param request + * Request parameters + * @return Bucket encryption configuration * @throws ObsException * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 */ - HeaderResponse setBucketWebsite(String bucketName, WebsiteConfiguration websiteConfig) throws ObsException; + BucketEncryption getBucketEncryption(BaseBucketRequest request) throws ObsException; /** - * Delete the website hosting configuration of a bucket. - * + * Set bucket encryption. + * * @param bucketName * Bucket name + * @param bucketEncryption + * Bucket encryption configuration * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ - HeaderResponse deleteBucketWebsite(String bucketName) throws ObsException; + HeaderResponse setBucketEncryption(String bucketName, BucketEncryption bucketEncryption) throws ObsException; /** - * Obtain bucket tags. - * - * @param bucketName - * Bucket name - * @return Bucket tag + * Configure bucket encryption. + * + * @param request + * Request parameters + * @return Common response headers * @throws ObsException * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 */ - BucketTagInfo getBucketTagging(String bucketName) throws ObsException; + HeaderResponse setBucketEncryption(SetBucketEncryptionRequest request) throws ObsException; /** - * Set bucket tags. - * + * Delete bucket encryption configuration. + * * @param bucketName * Bucket name - * @param bucketTagInfo - * Bucket tags * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ - HeaderResponse setBucketTagging(String bucketName, BucketTagInfo bucketTagInfo) throws ObsException; + HeaderResponse deleteBucketEncryption(String bucketName) throws ObsException; /** - * Delete bucket tags. - * - * @param bucketName - * Bucket name + * Delete bucket encryption configuration. + * + * @param request + * Request parameters * @return Common response headers * @throws ObsException * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 */ - HeaderResponse deleteBucketTagging(String bucketName) throws ObsException; - - - /** - * Obtain bucket encryption configuration. - * @param bucketName - * Bucket name - * @return Bucket encryption configuration - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - BucketEncryption getBucketEncryption(String bucketName) throws ObsException; - - /** - * Set bucket encryption. - * @param bucketName - * Bucket name - * @param bucketEncryption - * Bucket encryption configuration - * @return Common response headers - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - HeaderResponse setBucketEncryption(String bucketName, BucketEncryption bucketEncryption) throws ObsException; - - /** - * Delete bucket encryption configuration. - * @param bucketName - * Bucket name - * @return Common response headers - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - HeaderResponse deleteBucketEncryption(String bucketName) throws ObsException; + HeaderResponse deleteBucketEncryption(BaseBucketRequest request) throws ObsException; /** * Configure cross-region replication for a bucket. @@ -770,12 +1388,25 @@ HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfigurat * Cross-region replication configuration * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails * */ HeaderResponse setBucketReplication(String bucketName, ReplicationConfiguration replicationConfiguration) throws ObsException; + /** + * Configure cross-region replication for a bucket. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketReplication(SetBucketReplicationRequest request) throws ObsException; + /** * Obtain the cross-region replication configuration of a bucket. * @@ -783,10 +1414,23 @@ HeaderResponse setBucketReplication(String bucketName, ReplicationConfiguration * Bucket name * @return Cross-region replication configuration * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ReplicationConfiguration getBucketReplication(String bucketName) throws ObsException; + /** + * Obtain the cross-region replication configuration of a bucket. + * + * @param request + * Request parameters + * @return Cross-region replication configuration + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + ReplicationConfiguration getBucketReplication(BaseBucketRequest request) throws ObsException; + /** * Delete the bucket cross-region replication configuration. * @@ -794,10 +1438,23 @@ HeaderResponse setBucketReplication(String bucketName, ReplicationConfiguration * Bucket name * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse deleteBucketReplication(String bucketName) throws ObsException; + /** + * Delete the bucket cross-region replication configuration. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse deleteBucketReplication(BaseBucketRequest request) throws ObsException; + /** * Obtain the notification configuration of a bucket. * @@ -805,10 +1462,23 @@ HeaderResponse setBucketReplication(String bucketName, ReplicationConfiguration * Bucket name * @return Bucket notification configuration * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ BucketNotificationConfiguration getBucketNotification(String bucketName) throws ObsException; + /** + * Obtain the notification configuration of a bucket. + * + * @param request + * Request parameters + * @return Bucket notification configuration + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + BucketNotificationConfiguration getBucketNotification(BaseBucketRequest request) throws ObsException; + /** * Configure bucket notification. * @@ -818,10 +1488,23 @@ HeaderResponse setBucketReplication(String bucketName, ReplicationConfiguration * Bucket notification configuration * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse setBucketNotification(String bucketName, - BucketNotificationConfiguration bucketNotificationConfiguration) throws ObsException; + BucketNotificationConfiguration bucketNotificationConfiguration) throws ObsException; + + /** + * Set event notification for a bucket. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketNotification(SetBucketNotificationRequest request) throws ObsException; /** * Upload an object. @@ -836,7 +1519,8 @@ HeaderResponse setBucketNotification(String bucketName, * Object properties * @return Response to an object upload request * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ PutObjectResult putObject(String bucketName, String objectKey, InputStream input, ObjectMetadata metadata) throws ObsException; @@ -852,7 +1536,8 @@ PutObjectResult putObject(String bucketName, String objectKey, InputStream input * Data stream to be uploaded * @return Response to an object upload request * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ PutObjectResult putObject(String bucketName, String objectKey, InputStream input) throws ObsException; @@ -863,7 +1548,8 @@ PutObjectResult putObject(String bucketName, String objectKey, InputStream input * Parameters in an object upload request * @return Response to an object upload request * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ PutObjectResult putObject(PutObjectRequest request) throws ObsException; @@ -878,7 +1564,8 @@ PutObjectResult putObject(String bucketName, String objectKey, InputStream input * File to be uploaded * @return Response to an object upload request * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ PutObjectResult putObject(String bucketName, String objectKey, File file) throws ObsException; @@ -895,16 +1582,21 @@ PutObjectResult putObject(String bucketName, String objectKey, InputStream input * Object properties * @return Response to an object upload request * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ PutObjectResult putObject(String bucketName, String objectKey, File file, ObjectMetadata metadata) throws ObsException; /** * Perform an appendable upload. - * @param request Parameters in an appendable upload request + * + * @param request + * Parameters in an appendable upload request * @return Response to the appendable upload request - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ AppendObjectResult appendObject(AppendObjectRequest request) throws ObsException; @@ -915,22 +1607,55 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Parameters in a file upload request * @return Result of part combination * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ CompleteMultipartUploadResult uploadFile(UploadFileRequest uploadFileRequest) throws ObsException; + /** + * Upload files in a batch. + * + * @param request + * Request parameters for uploading files in a batch + * @return Batch task execution status + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + */ UploadProgressStatus putObjects(PutObjectsRequest request) throws ObsException; + /** + * Check whether an object exists. + * + * @param buckeName + * Bucket name + * @param objectKey + * Object name + * @return Whether an object exists + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + */ boolean doesObjectExist(String buckeName, String objectKey) throws ObsException; + /** + * Check whether an object exists. + * + * @param request + * Request parameters for obtaining the properties of an object + * @return Whether an object exists + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + */ boolean doesObjectExist(GetObjectMetadataRequest request) throws ObsException; + /** * Download a file. The resumable download mode is supported. * * @param downloadFileRequest * Parameters in a request for downloading a file * @return File download result - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ DownloadFileResult downloadFile(DownloadFileRequest downloadFileRequest) throws ObsException; @@ -941,7 +1666,8 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Parameters in an object download request * @return Object information, including the object data stream * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObsObject getObject(GetObjectRequest request) throws ObsException; @@ -956,7 +1682,8 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Object version ID * @return Object information, including the object data stream * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObsObject getObject(String bucketName, String objectKey, String versionId) throws ObsException; @@ -969,7 +1696,8 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Object name * @return Object information, including the object data stream * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObsObject getObject(String bucketName, String objectKey) throws ObsException; @@ -977,10 +1705,12 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Obtain object properties. * * @param request - * Parameters in a request for obtaining the properties of an object + * Parameters in a request for obtaining the properties of an + * object * @return Object properties * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObjectMetadata getObjectMetadata(GetObjectMetadataRequest request) throws ObsException; @@ -995,7 +1725,8 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Object version ID * @return Object properties * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObjectMetadata getObjectMetadata(String bucketName, String objectKey, String versionId) throws ObsException; @@ -1008,15 +1739,20 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Object name * @return Object properties * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObjectMetadata getObjectMetadata(String bucketName, String objectKey) throws ObsException; /** * Set object properties. - * @param request Parameters in the request for obtaining object properties + * + * @param request + * Parameters in the request for obtaining object properties * @return Object properties - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ObjectMetadata setObjectMetadata(SetObjectMetadataRequest request) throws ObsException; @@ -1027,33 +1763,35 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Parameters in a request for restoring an Archive object * @return Status of the to-be-restored Archive object * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails * */ - @Deprecated RestoreObjectStatus restoreObject(RestoreObjectRequest request) throws ObsException; - /** + /** * Restore an Archive object. * * @param request * Request parameters for restoring an Archive object * @return Result of restoring the Archive object * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails * */ RestoreObjectResult restoreObjectV2(RestoreObjectRequest request) throws ObsException; - /** + /** * Restore Archive objects in a batch. * * @param request * Request parameters for restoring Archive objects in a batch - * @return Batch task execution status + * @return Batch task execution status * * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails * */ @@ -1070,7 +1808,8 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Object version ID * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ DeleteObjectResult deleteObject(String bucketName, String objectKey, String versionId) throws ObsException; @@ -1084,10 +1823,23 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Object name * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ DeleteObjectResult deleteObject(String bucketName, String objectKey) throws ObsException; + /** + * Delete an object. + * + * @param request + * Request parameters for deleting an object + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + DeleteObjectResult deleteObject(DeleteObjectRequest request) throws ObsException; + /** * Delete objects in a batch. * @@ -1095,7 +1847,8 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Parameters in an object batch deletion request * @return Result of the object batch deletion request * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws ObsException; @@ -1110,7 +1863,8 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Object version ID * @return Object ACL * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ AccessControlList getObjectAcl(String bucketName, String objectKey, String versionId) throws ObsException; @@ -1123,10 +1877,23 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Object name * @return Object ACL * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ AccessControlList getObjectAcl(String bucketName, String objectKey) throws ObsException; + /** + * Obtain an object ACL. + * + * @param request + * Request parameters for obtaining an object ACL + * @return Object ACL + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + AccessControlList getObjectAcl(GetObjectAclRequest request) throws ObsException; + /** * Set an object ACL. * @@ -1140,14 +1907,15 @@ PutObjectResult putObject(String bucketName, String objectKey, File file, Object * Object version ID * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse setObjectAcl(String bucketName, String objectKey, AccessControlList acl, String versionId) throws ObsException; /** * Set an object ACL. - * + * * @param bucketName * Bucket name * @param objectKey @@ -1160,6 +1928,18 @@ HeaderResponse setObjectAcl(String bucketName, String objectKey, AccessControlLi */ HeaderResponse setObjectAcl(String bucketName, String objectKey, AccessControlList acl) throws ObsException; + /** + * Set an object ACL. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setObjectAcl(SetObjectAclRequest request) throws ObsException; + /** * Copy an object. * @@ -1167,7 +1947,8 @@ HeaderResponse setObjectAcl(String bucketName, String objectKey, AccessControlLi * Parameters in a request for copying an object * @return Result of the object copy * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ CopyObjectResult copyObject(CopyObjectRequest request) throws ObsException; @@ -1184,10 +1965,11 @@ HeaderResponse setObjectAcl(String bucketName, String objectKey, AccessControlLi * Destination object name * @return Result of the object copy * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ CopyObjectResult copyObject(String sourceBucketName, String sourceObjectKey, String destBucketName, - String destObjectKey) throws ObsException; + String destObjectKey) throws ObsException; /** * Initialize a multipart upload. @@ -1196,7 +1978,8 @@ CopyObjectResult copyObject(String sourceBucketName, String sourceObjectKey, Str * Parameters in a request for initializing a multipart upload * @return Result of the multipart upload * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ InitiateMultipartUploadResult initiateMultipartUpload(InitiateMultipartUploadRequest request) throws ObsException; @@ -1207,7 +1990,8 @@ CopyObjectResult copyObject(String sourceBucketName, String sourceObjectKey, Str * Parameters in a request for aborting a multipart upload * @return Common response headers * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ HeaderResponse abortMultipartUpload(AbortMultipartUploadRequest request) throws ObsException; @@ -1226,7 +2010,8 @@ CopyObjectResult copyObject(String sourceBucketName, String sourceObjectKey, Str * Data stream to be uploaded * @return Response to a part upload request * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId, int partNumber, InputStream input) throws ObsException; @@ -1246,7 +2031,8 @@ UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId * File to be uploaded * @return Response to a part upload request * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId, int partNumber, File file) throws ObsException; @@ -1258,7 +2044,8 @@ UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId * Parameters in a part upload request * @return Response to a part upload request * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ UploadPartResult uploadPart(UploadPartRequest request) throws ObsException; @@ -1269,7 +2056,8 @@ UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId * Parameters in the request for copying a part * @return Response to a part copy request * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ CopyPartResult copyPart(CopyPartRequest request) throws ObsException; @@ -1280,7 +2068,8 @@ UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId * Parameters in a request for combining parts * @return Result of part combination * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ CompleteMultipartUploadResult completeMultipartUpload(CompleteMultipartUploadRequest request) throws ObsException; @@ -1291,7 +2080,8 @@ UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId * Parameters in a request for listing uploaded parts * @return Response to a request for listing uploaded parts * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ ListPartsResult listParts(ListPartsRequest request) throws ObsException; @@ -1302,66 +2092,132 @@ UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId * Parameters in a request for listing multipart uploads * @return List of multipart uploads * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ MultipartUploadListing listMultipartUploads(ListMultipartUploadsRequest request) throws ObsException; - /** - * Read ahead objects. - * @param request Request parameters for reading ahead objects - * @return Response to the request for reading ahead objects - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ + /** + * Read ahead objects. + * + * @param request + * Request parameters for reading ahead objects + * @return Response to the request for reading ahead objects + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ ReadAheadResult readAheadObjects(ReadAheadRequest request) throws ObsException; - /** - * Delete the read-ahead cache. - * @param bucketName Bucket name - * @param prefix Name prefix of objects to be read ahead - * @return Response to the request for reading ahead objects - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ + /** + * Delete the read-ahead cache. + * + * @param bucketName + * Bucket name + * @param prefix + * Name prefix of objects to be read ahead + * @return Response to the request for reading ahead objects + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ ReadAheadResult deleteReadAheadObjects(String bucketName, String prefix) throws ObsException; - /** - * Query the progress of a read-ahead task. - * @param bucketName Bucket name - * @param taskId ID of the read-ahead task - * @return Response to the request for querying the progress of the read-ahead task - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ + /** + * Query the progress of a read-ahead task. + * + * @param bucketName + * Bucket name + * @param taskId + * ID of the read-ahead task + * @return Response to the request for querying the progress of the + * read-ahead task + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ ReadAheadQueryResult queryReadAheadObjectsTask(String bucketName, String taskId) throws ObsException; - /** - * Set the direct reading policy for Archive objects in a bucket. - * @param bucketName Bucket name - * @param access Direct reading policy - * @return - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ + /** + * Set the direct reading policy for Archive objects in a bucket. + * + * @param bucketName + * Bucket name + * @param access + * Direct reading policy + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ HeaderResponse setBucketDirectColdAccess(String bucketName, BucketDirectColdAccess access) throws ObsException; - /** - * Obtain the direct reading policy for Archive objects in a bucket. - * @param bucketName Bucket name - * @return - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ + /** + * Configure the direct reading policy for Archive objects in a bucket. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse setBucketDirectColdAccess(SetBucketDirectColdAccessRequest request) throws ObsException; + + /** + * Obtain the direct reading policy for Archive objects in a bucket. + * + * @param bucketName + * Bucket name + * @return Direct reading policy for Archive objects of a bucket + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ BucketDirectColdAccess getBucketDirectColdAccess(String bucketName) throws ObsException; - /** - * Delete the direct reading policy for Archive objects in a bucket. - * @param bucketName Bucket name - * @return - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ + /** + * Obtain the direct reading policy for Archive objects in a bucket. + * + * @param request + * Request parameters + * @return Direct reading policy for Archive objects in a bucket + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + BucketDirectColdAccess getBucketDirectColdAccess(BaseBucketRequest request) throws ObsException; + + /** + * Delete the direct reading policy for Archive objects in a bucket. + * + * @param bucketName + * Bucket name + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ HeaderResponse deleteBucketDirectColdAccess(String bucketName) throws ObsException; + /** + * Delete the direct reading policy for Archive objects in a bucket. + * + * @param request + * Request parameters + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails + * @since 3.20.3 + */ + HeaderResponse deleteBucketDirectColdAccess(BaseBucketRequest request) throws ObsException; + /** * Close ObsClient and release connection resources. - * @throws IOException ObsClient close exception + * + * @throws IOException + * ObsClient close exception */ void close() throws IOException; } - diff --git a/app/src/main/java/com/obs/services/IObsCredentialsProvider.java b/app/src/main/java/com/obs/services/IObsCredentialsProvider.java index d159bd3..90c2f1f 100644 --- a/app/src/main/java/com/obs/services/IObsCredentialsProvider.java +++ b/app/src/main/java/com/obs/services/IObsCredentialsProvider.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; import com.obs.services.model.ISecurityKey; diff --git a/app/src/main/java/com/obs/services/Log4j2Configurator.java b/app/src/main/java/com/obs/services/Log4j2Configurator.java index c9c9388..0f9fbc1 100644 --- a/app/src/main/java/com/obs/services/Log4j2Configurator.java +++ b/app/src/main/java/com/obs/services/Log4j2Configurator.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; import java.io.FileInputStream; @@ -20,113 +21,133 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.logging.Logger; /** - * Log configuration class integrated with Log4j2 + * Log configuration class integrated with Log4j2 * */ public class Log4j2Configurator { + + private static volatile boolean isWatchStart = false; + + private static volatile boolean log4j2Enabled = false; + + private static final Logger logger = Logger.getLogger(Log4j2Configurator.class.getName()); - private static volatile boolean isWatchStart = false; - - private static volatile boolean log4j2Enabled = false; - - /** - * Configure logs. - * @param configPath Path to the log configuration file - */ - public static synchronized void setLogConfig(String configPath) { - setLogConfig(configPath, false); - } - - /** - * Configure logs. - * @param configPath Path to the log configuration file - * @param isWatchConfig Whether to monitor changes of the log configuration file - */ - public static synchronized void setLogConfig(String configPath, boolean isWatchConfig) { - setLogConfig(configPath, isWatchConfig, 60000); - } - - private static Object getLogContext(String configPath) { - Object ctx = null; - FileInputStream configInputStream = null; - try { - Class configurationSource = Class.forName("org.apache.logging.log4j.core.config.ConfigurationSource"); - Class configurator = Class.forName("org.apache.logging.log4j.core.config.Configurator"); - Constructor con = configurationSource.getConstructor(InputStream.class); - Method m = configurator.getMethod("initialize", ClassLoader.class, configurationSource); - - configInputStream = new FileInputStream(configPath); - ctx = m.invoke(null, null, con.newInstance(configInputStream)); - }catch (ClassNotFoundException | NoSuchMethodException | SecurityException - | InvocationTargetException | IllegalAccessException | InstantiationException | FileNotFoundException e) { - }finally { - if(null != configInputStream) { - try { - configInputStream.close(); - } catch (IOException e) { - } - } - } - return ctx; - } - - private static class LogWatcher extends Thread{ - private Object ctx; - private long watchInterval; - private String configPath; - LogWatcher(String configPath, Object ctx, long watchInterval){ - this.configPath = configPath; - this.ctx = ctx; - this.watchInterval = watchInterval; - - } - public void run() { - try { - Class configuration = Class.forName("org.apache.logging.log4j.core.config.Configuration"); - final Method stop = ctx.getClass().getMethod("stop"); - final Method start = ctx.getClass().getMethod("start", configuration); - Class xmlConfiguration = Class.forName("org.apache.logging.log4j.core.config.xml.XmlConfiguration"); - Class configurationSource = Class.forName("org.apache.logging.log4j.core.config.ConfigurationSource"); - Constructor configurationSourceConstructor = configurationSource.getConstructor(InputStream.class); - Constructor xmlConfigurationConstructor = xmlConfiguration.getConstructor(ctx.getClass(), configurationSource); - while(true) { - Thread.sleep(this.watchInterval); - stop.invoke(ctx); - start.invoke(ctx, xmlConfigurationConstructor.newInstance(ctx, configurationSourceConstructor.newInstance(new FileInputStream(this.configPath)))); - } - }catch (ClassNotFoundException | FileNotFoundException | NoSuchMethodException | SecurityException - | InterruptedException | InvocationTargetException | IllegalAccessException | InstantiationException e) { - e.printStackTrace(); - } - } - } - - /** - * Configure logs. - * @param configPath Path to the log configuration file - * @param isWatchConfig Whether to monitor changes of the log configuration file - * @param watchInterval Interval for monitoring changes of the log configuration file, in units of ms - */ - public static synchronized void setLogConfig(String configPath, boolean isWatchConfig, long watchInterval) { - if(log4j2Enabled) { - return; - } - Object ctx = getLogContext(configPath); - if(isWatchConfig && ctx != null && !isWatchStart) { - try { - isWatchStart = true; - long _watchInterval = watchInterval > 0 ? watchInterval : 60000; - LogWatcher wather = new LogWatcher(configPath, ctx, _watchInterval); - wather.setDaemon(true); - wather.start(); - } catch (Exception e) { - } - } - log4j2Enabled = true; - } + /** + * Configure logs. + * + * @param configPath + * Path to the log configuration file + */ + public static synchronized void setLogConfig(String configPath) { + setLogConfig(configPath, false); + } -} + /** + * Configure logs. + * + * @param configPath + * Path to the log configuration file + * @param isWatchConfig + * Whether to monitor changes of the log configuration file + */ + public static synchronized void setLogConfig(String configPath, boolean isWatchConfig) { + setLogConfig(configPath, isWatchConfig, 60000); + } + + private static Object getLogContext(String configPath) { + Object ctx = null; + FileInputStream configInputStream = null; + try { + Class configurationSource = Class.forName("org.apache.logging.log4j.core.config.ConfigurationSource"); + Class configurator = Class.forName("org.apache.logging.log4j.core.config.Configurator"); + Constructor con = configurationSource.getConstructor(InputStream.class); + Method m = configurator.getMethod("initialize", ClassLoader.class, configurationSource); + configInputStream = new FileInputStream(configPath); + ctx = m.invoke(null, null, con.newInstance(configInputStream)); + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InvocationTargetException + | IllegalAccessException | InstantiationException | FileNotFoundException e) { + logger.warning(e.getMessage()); + } finally { + if (null != configInputStream) { + try { + configInputStream.close(); + } catch (IOException e) { + logger.warning(e.getMessage()); + } + } + } + return ctx; + } + private static class LogWatcher extends Thread { + private Object ctx; + private long watchInterval; + private String configPath; + + LogWatcher(String configPath, Object ctx, long watchInterval) { + this.configPath = configPath; + this.ctx = ctx; + this.watchInterval = watchInterval; + + } + + public void run() { + try { + Class configuration = Class.forName("org.apache.logging.log4j.core.config.Configuration"); + final Method stop = ctx.getClass().getMethod("stop"); + final Method start = ctx.getClass().getMethod("start", configuration); + Class xmlConfiguration = Class.forName("org.apache.logging.log4j.core.config.xml.XmlConfiguration"); + Class configurationSource = Class + .forName("org.apache.logging.log4j.core.config.ConfigurationSource"); + Constructor configurationSourceConstructor = configurationSource.getConstructor(InputStream.class); + Constructor xmlConfigurationConstructor = xmlConfiguration.getConstructor(ctx.getClass(), + configurationSource); + while (true) { + Thread.sleep(this.watchInterval); + stop.invoke(ctx); + start.invoke(ctx, xmlConfigurationConstructor.newInstance(ctx, + configurationSourceConstructor.newInstance(new FileInputStream(this.configPath)))); + } + } catch (ClassNotFoundException | FileNotFoundException | NoSuchMethodException | SecurityException + | InterruptedException | InvocationTargetException | IllegalAccessException + | InstantiationException e) { + logger.warning(e.getMessage()); + } + } + } + + /** + * Configure logs. + * + * @param configPath + * Path to the log configuration file + * @param isWatchConfig + * Whether to monitor changes of the log configuration file + * @param watchInterval + * Interval for monitoring changes of the log configuration file, + * in units of ms + */ + public static synchronized void setLogConfig(String configPath, boolean isWatchConfig, long watchInterval) { + if (log4j2Enabled) { + return; + } + Object ctx = getLogContext(configPath); + if (isWatchConfig && ctx != null && !isWatchStart) { + try { + isWatchStart = true; + long interval = watchInterval > 0 ? watchInterval : 60000; + LogWatcher wather = new LogWatcher(configPath, ctx, interval); + wather.setDaemon(true); + wather.start(); + } catch (Exception e) { + logger.warning(e.getMessage()); + } + } + log4j2Enabled = true; + } + +} diff --git a/app/src/main/java/com/obs/services/LogConfigurator.java b/app/src/main/java/com/obs/services/LogConfigurator.java index 28c8b2d..2b3eaa4 100644 --- a/app/src/main/java/com/obs/services/LogConfigurator.java +++ b/app/src/main/java/com/obs/services/LogConfigurator.java @@ -11,11 +11,12 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services; +package com.obs.services; import java.io.File; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; @@ -31,31 +32,28 @@ import com.obs.services.internal.utils.ServiceUtils; /** - * Log configuration class that uses the standard JDK log library + * Log configuration class that uses the standard JDK log library * */ public class LogConfigurator { - + public static final Level OFF = Level.parse("OFF"); - + public static final Level TRACE = Level.parse("FINEST"); - + public static final Level DEBUG = Level.parse("FINE"); - + public static final Level INFO = Level.parse("INFO"); - + public static final Level WARN = Level.parse("WARNING"); - - public static final Level ERROR = Level.parse("SEVERE"); - - private static final Logger logger = Logger.getLogger("com.obs"); - private static final Logger accessLogger = Logger.getLogger("com.obs.log.AccessLogger"); + public static final Level ERROR = Level.parse("SEVERE"); + private static final Logger ILOG = Logger.getLogger(LogConfigurator.class.getName()); - static{ - LogConfigurator.disableLog(); - LogConfigurator.disableAccessLog(); + static { + LogConfigurator.disableLog(); + LogConfigurator.disableAccessLog(); } private static Level logLevel; @@ -65,115 +63,113 @@ public class LogConfigurator { private static int logFileSize = 30 * 1024 * 1024; // 30MB private static int logFileRolloverCount = 50; - + private static volatile boolean logEnabled = false; private static volatile boolean accessLogEnabled = false; - private static String getDefaultLogFileDir(){ - try{ + private static String getDefaultLogFileDir() { + try { Class c = Class.forName("android.os.Environment"); Method m = c.getMethod("getExternalStorageDirectory"); - if(m != null){ + if (m != null) { return m.invoke(c).toString() + "/logs"; } - }catch (ClassNotFoundException | NoSuchMethodException - | InvocationTargetException | IllegalAccessException e){ - + } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException + | IllegalAccessException e) { + ILOG.warning(e.getMessage()); } return System.getProperty("user.dir") + "/logs"; } - - private static void logOn(final Logger pLogger, String logName) - { - pLogger.setUseParentHandlers(false); - pLogger.setLevel(logLevel == null ? LogConfigurator.WARN : logLevel); - if(logFileDir == null){ + private synchronized static void logOn(final Logger currentLogger, String logName) { + currentLogger.setUseParentHandlers(false); + currentLogger.setLevel(logLevel == null ? LogConfigurator.WARN : logLevel); + if (logFileDir == null) { logFileDir = getDefaultLogFileDir(); } - try - { - File dir = new File(logFileDir); - if(!dir.exists()){ - if(!dir.mkdirs()) { - if(!dir.exists()) { - logFileDir = System.getProperty("user.dir") + "/"; - } - } - } - FileHandler fh = new FileHandler(logFileDir + logName, logFileSize, logFileRolloverCount,true); - fh.setEncoding("UTF-8"); - fh.setFormatter(new Formatter() - { - - @Override - public String format(LogRecord record) - { - String levelName = record.getLevel().getName(); - if("SEVERE".equals(levelName)){ - levelName = "ERROR"; - }else if("FINE".equals(levelName)){ - levelName = "DEBUG"; - }else if("FINEST".equals(levelName)){ - levelName = "TRACE"; - } - if(pLogger == accessLogger) - { - return Thread.currentThread().getName() + "\n" + record.getMessage() + (record.getThrown() == null ? "" : record.getThrown()) + System.getProperty("line.separator"); - } - Date d = new Date(record.getMillis()); - - SimpleDateFormat format = AccessLoggerUtils.getFormat(); - - return format.format(d) + "|" + Thread.currentThread().getName() + "|" + levelName + " |" + record.getMessage() + (record.getThrown() == null ? "" : record.getThrown()) + System.getProperty("line.separator"); - } - }); - pLogger.addHandler(fh); - if(pLogger == accessLogger) - { + try { + FileHandler fh = createFileHandler(currentLogger, logName); + currentLogger.addHandler(fh); + if (currentLogger == AccessLog.getLogger()) { accessLogEnabled = true; - } - else if(pLogger == logger) - { + } else if (currentLogger == BaseLog.getLogger()) { logEnabled = true; } + } catch (IOException e) { + onException(currentLogger, e); } - catch (IOException e) - { - try - { - Class c = Class.forName("android.util.Log"); - try{ - Method m = c.getMethod("i", String.class, String.class, Throwable.class); - m.invoke(null, "OBS Android SDK", "Enable SDK log failed", e); - }catch (NoSuchMethodException | SecurityException ex) { - Method m = c.getMethod("i", String.class, String.class); - m.invoke(null, "OBS Android SDK", "Enable SDK log failed" + e.getMessage()); - } + } + + private static void onException(final Logger currentLogger, IOException e) { + try { + Class c = Class.forName("android.util.Log"); + try { + Method m = c.getMethod("i", String.class, String.class, Throwable.class); + m.invoke(null, "OBS Android SDK", "Enable SDK log failed", e); + } catch (NoSuchMethodException | SecurityException ex) { + Method m = c.getMethod("i", String.class, String.class); + m.invoke(null, "OBS Android SDK", "Enable SDK log failed" + e.getMessage()); } - catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) - { + } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException + | IllegalAccessException ex) { + ILOG.warning(e.getMessage()); + } + logOff(currentLogger); + } + + private static FileHandler createFileHandler(final Logger currentLogger, String logName) + throws IOException, UnsupportedEncodingException { + File dir = new File(logFileDir); + if (!dir.exists()) { + if (!dir.mkdirs()) { + if (!dir.exists()) { + logFileDir = System.getProperty("user.dir") + "/"; + } } - logOff(pLogger); } + FileHandler fh = new FileHandler(logFileDir + logName, logFileSize, logFileRolloverCount, true); + fh.setEncoding("UTF-8"); + fh.setFormatter(new Formatter() { + + @Override + public String format(LogRecord record) { + String levelName = record.getLevel().getName(); + if ("SEVERE".equals(levelName)) { + levelName = "ERROR"; + } else if ("FINE".equals(levelName)) { + levelName = "DEBUG"; + } else if ("FINEST".equals(levelName)) { + levelName = "TRACE"; + } + if (currentLogger == AccessLog.getLogger()) { + return Thread.currentThread().getName() + "\n" + record.getMessage() + + (record.getThrown() == null ? "" : record.getThrown()) + + System.getProperty("line.separator"); + } + Date d = new Date(record.getMillis()); + + SimpleDateFormat format = AccessLoggerUtils.getFormat(); + + return format.format(d) + "|" + Thread.currentThread().getName() + "|" + levelName + " |" + + record.getMessage() + (record.getThrown() == null ? "" : record.getThrown()) + + System.getProperty("line.separator"); + } + }); + return fh; } - private static void logOff(Logger pLogger) - { - pLogger.setLevel(LogConfigurator.OFF); - Handler[] handlers = pLogger.getHandlers(); - if(handlers != null){ - for(Handler handler : handlers){ - pLogger.removeHandler(handler); + private static void logOff(Logger currentLogger) { + currentLogger.setLevel(LogConfigurator.OFF); + Handler[] handlers = currentLogger.getHandlers(); + if (handlers != null) { + for (Handler handler : handlers) { + currentLogger.removeHandler(handler); } } - if(pLogger == accessLogger) - { + if (currentLogger == AccessLog.getLogger()) { accessLogEnabled = false; - } - else if(pLogger == logger) - { + } else if (currentLogger == BaseLog.getLogger()) { logEnabled = false; } } @@ -181,73 +177,92 @@ else if(pLogger == logger) /** * Enable SDK logging. */ - public synchronized static void enableLog(){ - if(logEnabled) { - logOff(logger); - } - logOn(logger, "/OBS-SDK.log"); + public synchronized static void enableLog() { + if (logEnabled) { + logOff(BaseLog.getLogger()); + } + logOn(BaseLog.getLogger(), "/OBS-SDK.log"); } /** * Disable SDK logging. */ - protected synchronized static void disableLog(){ - logOff(logger); + protected synchronized static void disableLog() { + logOff(BaseLog.getLogger()); } - - public synchronized static void enableAccessLog() - { - if(accessLogEnabled) - { - logOff(accessLogger); + public synchronized static void enableAccessLog() { + if (accessLogEnabled) { + logOff(AccessLog.getLogger()); } - logOn(accessLogger, "/OBS-SDK-access.log"); + logOn(AccessLog.getLogger(), "/OBS-SDK-access.log"); } - protected synchronized static void disableAccessLog() - { - logOff(accessLogger); + protected synchronized static void disableAccessLog() { + logOff(AccessLog.getLogger()); } /** * Set the log level. - * @param level Log level + * + * @param level + * Log level */ - public synchronized static void setLogLevel(Level level){ - if(level != null){ + public synchronized static void setLogLevel(Level level) { + if (level != null) { logLevel = level; } } /** * Set the number of retained log files. - * @param count Number of retained log files + * + * @param count + * Number of retained log files */ - public synchronized static void setLogFileRolloverCount(int count){ - if(count > 0){ + public synchronized static void setLogFileRolloverCount(int count) { + if (count > 0) { logFileRolloverCount = count; } } /** - * Set the log file size (in bytes). - * @param fileSize Log file size + * Set the log file size (in bytes). + * + * @param fileSize + * Log file size */ - public synchronized static void setLogFileSize(int fileSize){ - if(fileSize >= 0){ + public synchronized static void setLogFileSize(int fileSize) { + if (fileSize >= 0) { logFileSize = fileSize; } } /** * Set a directory for saving log files. - * @param dir Directory for saving log files + * + * @param dir + * Directory for saving log files */ - public synchronized static void setLogFileDir(String dir){ - if(ServiceUtils.isValid(dir)){ + public synchronized static void setLogFileDir(String dir) { + if (ServiceUtils.isValid(dir)) { logFileDir = dir; } } + private static class BaseLog { + private static final Logger logger = Logger.getLogger("com.obs"); + + public static Logger getLogger() { + return logger; + } + } + + private static class AccessLog { + private static final Logger logger = Logger.getLogger("com.obs.log.AccessLogger"); + + public static Logger getLogger() { + return logger; + } + } } diff --git a/app/src/main/java/com/obs/services/OBSCredentialsProviderChain.java b/app/src/main/java/com/obs/services/OBSCredentialsProviderChain.java index 2d52f32..891f5e0 100644 --- a/app/src/main/java/com/obs/services/OBSCredentialsProviderChain.java +++ b/app/src/main/java/com/obs/services/OBSCredentialsProviderChain.java @@ -11,15 +11,16 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; public class OBSCredentialsProviderChain extends DefaultCredentialsProviderChain { - public OBSCredentialsProviderChain() { - this(true); - } + public OBSCredentialsProviderChain() { + this(true); + } - public OBSCredentialsProviderChain(boolean reused) { - super(reused, new EnvironmentVariableObsCredentialsProvider(), new EcsObsCredentialsProvider()); - } + public OBSCredentialsProviderChain(boolean reused) { + super(reused, new EnvironmentVariableObsCredentialsProvider(), new EcsObsCredentialsProvider()); + } } diff --git a/app/src/main/java/com/obs/services/ObsClient.java b/app/src/main/java/com/obs/services/ObsClient.java index 7c3f4b6..3a43484 100644 --- a/app/src/main/java/com/obs/services/ObsClient.java +++ b/app/src/main/java/com/obs/services/ObsClient.java @@ -11,126 +11,38 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; -import java.io.Closeable; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; +import java.lang.reflect.Field; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import java.util.concurrent.RejectedExecutionException; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.ConcurrentHashMap; -import com.obs.log.ILogger; -import com.obs.log.InterfaceLogBean; -import com.obs.log.LoggerBuilder; import com.obs.services.exception.ObsException; -import com.obs.services.internal.Constants; -import com.obs.services.internal.Constants.CommonHeaders; -import com.obs.services.internal.ObsConstraint; -import com.obs.services.internal.ObsProperties; -import com.obs.services.internal.ObsService; -import com.obs.services.internal.ResumableClient; import com.obs.services.internal.ServiceException; -import com.obs.services.internal.consensus.CacheManager; -import com.obs.services.internal.consensus.SegmentLock; -import com.obs.services.internal.security.ProviderCredentials; -import com.obs.services.internal.task.DefaultTaskProgressStatus; -import com.obs.services.internal.task.DropFolderTask; -import com.obs.services.internal.task.LazyTaksCallback; -import com.obs.services.internal.task.PutObjectTask; -import com.obs.services.internal.task.RestoreObjectTask; -import com.obs.services.internal.task.ResumableUploadTask; -import com.obs.services.internal.task.UploadTaskProgressStatus; -import com.obs.services.internal.utils.AccessLoggerUtils; -import com.obs.services.internal.utils.ReflectUtils; import com.obs.services.internal.utils.ServiceUtils; -import com.obs.services.model.*; -import com.obs.services.model.PolicyConditionItem.ConditionOperator; -import com.obs.services.model.RestoreObjectRequest.RestoreObjectStatus; -import com.obs.services.model.fs.DropFileRequest; -import com.obs.services.model.fs.DropFileResult; -import com.obs.services.model.fs.DropFolderRequest; -import com.obs.services.model.fs.GetAttributeRequest; -import com.obs.services.model.fs.GetBucketFSStatusRequest; -import com.obs.services.model.fs.GetBucketFSStatusResult; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.PutObjectRequest; +import com.obs.services.model.ReadAheadQueryResult; +import com.obs.services.model.ReadAheadRequest; +import com.obs.services.model.ReadAheadResult; import com.obs.services.model.fs.NewBucketRequest; import com.obs.services.model.fs.NewFileRequest; import com.obs.services.model.fs.NewFolderRequest; -import com.obs.services.model.fs.ObsFSAttribute; import com.obs.services.model.fs.ObsFSBucket; import com.obs.services.model.fs.ObsFSFile; import com.obs.services.model.fs.ObsFSFolder; -import com.obs.services.model.fs.ReadFileRequest; -import com.obs.services.model.fs.ReadFileResult; -import com.obs.services.model.fs.RenameRequest; -import com.obs.services.model.fs.RenameResult; -import com.obs.services.model.fs.SetBucketFSStatusRequest; -import com.obs.services.model.fs.TruncateFileRequest; -import com.obs.services.model.fs.TruncateFileResult; import com.obs.services.model.fs.WriteFileRequest; /** * ObsClient */ -public class ObsClient extends ObsService implements Closeable, IObsClient, IFSClient { - - private static final ILogger ILOG = LoggerBuilder.getLogger(ObsClient.class); - - private void init(String accessKey, String secretKey, String securityToken, ObsConfiguration config) { - InterfaceLogBean reqBean = new InterfaceLogBean("ObsClient", config.getEndPoint(), ""); - ProviderCredentials credentials = new ProviderCredentials(accessKey, secretKey, securityToken); - ObsProperties obsProperties = ServiceUtils.changeFromObsConfiguration(config); - credentials.setAuthType(config.getAuthType()); - this.obsProperties = obsProperties; - this.credentials = credentials; - this.obsProperties = obsProperties; - this.keyManagerFactory = config.getKeyManagerFactory(); - this.trustManagerFactory = config.getTrustManagerFactory(); - if (this.isAuthTypeNegotiation()) { - this.apiVersionCache = new CacheManager(); - this.getProviderCredentials().initThreadLocalAuthType(); - this.segmentLock = new SegmentLock(); - } - this.initHttpClient(config.getHttpDispatcher()); - reqBean.setRespTime(new Date()); - reqBean.setResultCode(Constants.RESULTCODE_SUCCESS); - if (ILOG.isInfoEnabled()) { - ILOG.info(reqBean); - } - - if (ILOG.isWarnEnabled()) { - StringBuilder sb = new StringBuilder("[OBS SDK Version="); - sb.append(Constants.OBS_SDK_VERSION); - sb.append("];"); - sb.append("[Endpoint="); - String ep = ""; - if (this.getHttpsOnly()) { - ep = "https://" + this.getEndpoint() + ":" + this.getHttpsPort() + "/"; - } else { - ep = "http://" + this.getEndpoint() + ":" + this.getHttpPort() + "/"; - } - sb.append(ep); - sb.append("];"); - sb.append("[Access Mode="); - sb.append(this.isPathStyle() ? "Path" : "Virtul Hosting"); - sb.append("]"); - ILOG.warn(sb); - } - } +public class ObsClient extends AbstractBatchClient { + private static Map fields = new ConcurrentHashMap(); + /** * Constructor * @@ -138,11 +50,11 @@ private void init(String accessKey, String secretKey, String securityToken, ObsC * OBS endpoint * */ - public ObsClient(String endPoint) { - ObsConfiguration config = new ObsConfiguration(); - config.setEndPoint(endPoint); - this.init("", "", null, config); - } + public ObsClient(String endPoint) { + ObsConfiguration config = new ObsConfiguration(); + config.setEndPoint(endPoint); + this.init("", "", null, config); + } /** * Constructor @@ -151,12 +63,12 @@ public ObsClient(String endPoint) { * Configuration parameters of ObsClient * */ - public ObsClient(ObsConfiguration config) { - if (config == null) { - config = new ObsConfiguration(); - } - this.init("", "", null, config); - } + public ObsClient(ObsConfiguration config) { + if (config == null) { + config = new ObsConfiguration(); + } + this.init("", "", null, config); + } /** * Constructor @@ -169,11 +81,11 @@ public ObsClient(ObsConfiguration config) { * OBS endpoint * */ - public ObsClient(String accessKey, String secretKey, String endPoint) { - ObsConfiguration config = new ObsConfiguration(); - config.setEndPoint(endPoint); - this.init(accessKey, secretKey, null, config); - } + public ObsClient(String accessKey, String secretKey, String endPoint) { + ObsConfiguration config = new ObsConfiguration(); + config.setEndPoint(endPoint); + this.init(accessKey, secretKey, null, config); + } /** * Constructor @@ -186,12 +98,12 @@ public ObsClient(String accessKey, String secretKey, String endPoint) { * Configuration parameters of ObsClient * */ - public ObsClient(String accessKey, String secretKey, ObsConfiguration config) { - if (config == null) { - config = new ObsConfiguration(); - } - this.init(accessKey, secretKey, null, config); - } + public ObsClient(String accessKey, String secretKey, ObsConfiguration config) { + if (config == null) { + config = new ObsConfiguration(); + } + this.init(accessKey, secretKey, null, config); + } /** * Constructor @@ -206,11 +118,11 @@ public ObsClient(String accessKey, String secretKey, ObsConfiguration config) { * OBS endpoint * */ - public ObsClient(String accessKey, String secretKey, String securityToken, String endPoint) { - ObsConfiguration config = new ObsConfiguration(); - config.setEndPoint(endPoint); - this.init(accessKey, secretKey, securityToken, config); - } + public ObsClient(String accessKey, String secretKey, String securityToken, String endPoint) { + ObsConfiguration config = new ObsConfiguration(); + config.setEndPoint(endPoint); + this.init(accessKey, secretKey, securityToken, config); + } /** * Constructor @@ -225,2652 +137,157 @@ public ObsClient(String accessKey, String secretKey, String securityToken, Strin * Configuration parameters of ObsClient * */ - public ObsClient(String accessKey, String secretKey, String securityToken, ObsConfiguration config) { - if (config == null) { - config = new ObsConfiguration(); - } - this.init(accessKey, secretKey, securityToken, config); - } - - public ObsClient(IObsCredentialsProvider provider, String endPoint){ - ServiceUtils.asserParameterNotNull(provider, "ObsCredentialsProvider is null"); - ObsConfiguration config = new ObsConfiguration(); - config.setEndPoint(endPoint); - this.init(provider.getSecurityKey().getAccessKey(), provider.getSecurityKey().getSecretKey(), provider.getSecurityKey().getSecurityToken(), config); - this.credentials.setObsCredentialsProvider(provider); - } - - public ObsClient(IObsCredentialsProvider provider, ObsConfiguration config){ - ServiceUtils.asserParameterNotNull(provider, "ObsCredentialsProvider is null"); - if (config == null) { - config = new ObsConfiguration(); - } - this.init(provider.getSecurityKey().getAccessKey(), provider.getSecurityKey().getSecretKey(), provider.getSecurityKey().getSecurityToken(), config); - this.credentials.setObsCredentialsProvider(provider); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#refresh(java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public void refresh(String accessKey, String secretKey, String securityToken) { - ProviderCredentials credentials = new ProviderCredentials(accessKey, secretKey, securityToken); - credentials.setAuthType(this.credentials.getAuthType()); - this.setProviderCredentials(credentials); - } - - /** - * Create a temporarily authorized URL. - * - * @param method - * HTTP request method - * @param bucketName - * Bucket name - * @param objectKey - * Object name - * @param specialParam - * Special operator - * @param expiryTime - * Time when the temporary authentication expires - * @param headers - * Header information - * @param queryParams - * Query parameter information - * @return Temporarily authorized URL - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - @Deprecated - public String createSignedUrl(HttpMethodEnum method, String bucketName, String objectKey, - SpecialParamEnum specialParam, Date expiryTime, Map headers, - Map queryParams) throws ObsException { - return this.createSignedUrl(method, bucketName, objectKey, specialParam, - expiryTime == null ? ObsConstraint.DEFAULT_EXPIRE_SECONEDS - : (expiryTime.getTime() - System.currentTimeMillis()) / 1000, - headers, queryParams); - } - - /** - * Create a temporarily authorized URL. - * - * @param method - * HTTP request method - * @param bucketName - * Bucket name - * @param objectKey - * Object name - * @param specialParam - * Special operator - * @param expires - * Time when the temporary authentication expires. The unit is second and the default value is 300. - * @param headers - * Header information - * @param queryParams - * Query parameter information - * @return Temporarily authorized URL - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - @Deprecated - public String createSignedUrl(HttpMethodEnum method, String bucketName, String objectKey, - SpecialParamEnum specialParam, long expires, Map headers, Map queryParams) { - TemporarySignatureRequest request = new TemporarySignatureRequest(); - request.setMethod(method); - request.setBucketName(bucketName); - request.setObjectKey(objectKey); - request.setSpecialParam(specialParam); - request.setHeaders(headers); - request.setQueryParams(queryParams); - if(expires > 0) { - request.setExpires(expires); - } - return createTemporarySignature(request).getSignedUrl(); - } - - @Deprecated - public V4TemporarySignatureResponse createV4TemporarySignature(V4TemporarySignatureRequest request) { - ServiceUtils.asserParameterNotNull(request, "V4TemporarySignatureRequest is null"); - InterfaceLogBean reqBean = new InterfaceLogBean("createV4TemporarySignature", this.getEndpoint(), ""); - try { - TemporarySignatureResponse response = this.createV4TemporarySignature((TemporarySignatureRequest)request); - V4TemporarySignatureResponse res = new V4TemporarySignatureResponse(response.getSignedUrl()); - res.getActualSignedRequestHeaders().putAll(response.getActualSignedRequestHeaders()); - return res; - } catch (Exception e) { - reqBean.setRespTime(new Date()); - if (ILOG.isErrorEnabled()) { - ILOG.error(reqBean); - } - throw new ObsException(e.getMessage(), e); - } - } - - @Deprecated - public V4PostSignatureResponse createV4PostSignature(String acl, String contentType, long expires, - String bucketName, String objectKey) throws ObsException { - V4PostSignatureRequest request = new V4PostSignatureRequest(expires, new Date(), bucketName, objectKey); - request.getFormParams().put("acl", acl); - request.getFormParams().put("content-type", contentType); - return this.createV4PostSignature(request); - } - - @Deprecated - public V4PostSignatureResponse createV4PostSignature(long expires, String bucketName, String objectKey) - throws ObsException { - V4PostSignatureRequest request = new V4PostSignatureRequest(expires, new Date(), bucketName, objectKey); - return this.createV4PostSignature(request); - } - - @Deprecated - public V4PostSignatureResponse createV4PostSignature(V4PostSignatureRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "V4PostSignatureRequest is null"); - InterfaceLogBean reqBean = new InterfaceLogBean("createV4PostSignature", this.getEndpoint(), ""); - try { - V4PostSignatureResponse response = (V4PostSignatureResponse)this._createPostSignature(request, true); - reqBean.setRespTime(new Date()); - reqBean.setResultCode(Constants.RESULTCODE_SUCCESS); - if (ILOG.isInfoEnabled()) { - ILOG.info(reqBean); - } - return response; - } catch (Exception e) { - reqBean.setRespTime(new Date()); - if (ILOG.isErrorEnabled()) { - ILOG.error(reqBean); - } - throw new ObsException(e.getMessage(), e); - } - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#createTemporarySignature(com.obs.services.model.TemporarySignatureRequest) - */ - @Override - public TemporarySignatureResponse createTemporarySignature(TemporarySignatureRequest request) { - ServiceUtils.asserParameterNotNull(request, "TemporarySignatureRequest is null"); - InterfaceLogBean reqBean = new InterfaceLogBean("createTemporarySignature", this.getEndpoint(), ""); - try { - TemporarySignatureResponse response = this.getProviderCredentials().getAuthType() == AuthTypeEnum.V4 - ? this.createV4TemporarySignature(request) - : this._createTemporarySignature(request); - return response; - } catch (Exception e) { - reqBean.setRespTime(new Date()); - if (ILOG.isErrorEnabled()) { - ILOG.error(reqBean); - } - throw new ObsException(e.getMessage(), e); - } - } - - /** - * Generate temporary authorization parameters for GET requests based on the object name prefix and validity period. - * @param bucketName Bucket name - * @param objectKey Object name - * @param prefix Object name prefix - * @param expiryDate Expiration date (ISO 8601 UTC) - * @param headers Header information - * @param queryParams Query parameter information - * @return Response to the request for temporary access authorization - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public TemporarySignatureResponse createGetTemporarySignature(String bucketName, String objectKey, String prefix, - Date expiryDate, Map headers, Map queryParams) { - try { - PolicyTempSignatureRequest request = createPolicyGetRequest(bucketName, objectKey, prefix, headers, queryParams); - request.setExpiryDate(expiryDate); - TemporarySignatureResponse response = this._createTemporarySignature(request); - return response; - } catch (Exception e) { - throw new ObsException(e.getMessage(), e); - } - } - - /** - * Generate temporary authorization parameters for GET requests based on the object name prefix and validity period. - * @param bucketName Bucket name - * @param objectKey Object name - * @param prefix Object name prefix - * @param expires Validity period (seconds) - * @param headers Header information - * @param queryParams Query parameter information - * @return Response to the request for temporary access authorization - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public TemporarySignatureResponse createGetTemporarySignature(String bucketName, String objectKey, String prefix, - long expires, Map headers, Map queryParams) { - try { - PolicyTempSignatureRequest request = createPolicyGetRequest(bucketName, objectKey, prefix, headers, queryParams); - request.setExpires(expires); - TemporarySignatureResponse response = this._createTemporarySignature(request); - return response; - } catch (Exception e) { - throw new ObsException(e.getMessage(), e); - } - } - - private PolicyTempSignatureRequest createPolicyGetRequest(String bucketName, String objectKey, String prefix, - Map headers, Map queryParams) { - PolicyTempSignatureRequest request = new PolicyTempSignatureRequest(HttpMethodEnum.GET, bucketName, objectKey); - List conditions = new ArrayList(); - PolicyConditionItem keyCondition = new PolicyConditionItem(ConditionOperator.STARTS_WITH, "key", prefix); - String _bucket = this.isCname() ? this.getEndpoint() : bucketName; - PolicyConditionItem bucketCondition = new PolicyConditionItem(ConditionOperator.EQUAL, "bucket", _bucket); - conditions.add(keyCondition); - conditions.add(bucketCondition); - request.setConditions(conditions); - request.setHeaders(headers); - request.setQueryParams(queryParams); - return request; + public ObsClient(String accessKey, String secretKey, String securityToken, ObsConfiguration config) { + if (config == null) { + config = new ObsConfiguration(); + } + this.init(accessKey, secretKey, securityToken, config); } - /** - * Generate parameters for browser-based authorized access. - * - * @param acl - * Object ACL - * @param contentType - * MIME type of the object - * @param expires - * Validity period (in seconds) - * @param bucketName - * Bucket name - * @param objectKey - * Object name - * @return Response to the V4 browser-based authorized access - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public PostSignatureResponse createPostSignature(String acl, String contentType, long expires, String bucketName, - String objectKey) throws ObsException { - PostSignatureRequest request = new PostSignatureRequest(expires, new Date(), bucketName, objectKey); - request.getFormParams().put( - this.getProviderCredentials().getAuthType() == AuthTypeEnum.V4 ? "acl" : this.getIHeaders().aclHeader(), - acl); - request.getFormParams().put(CommonHeaders.CONTENT_TYPE, contentType); - return this.createPostSignature(request); - } - - /** - * Generate parameters for browser-based authorized access. - * - * @param expires - * Validity period (in seconds) - * @param bucketName - * Bucket name - * @param objectKey - * Object name - * @return Response to the V4 browser-based authorized access - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public PostSignatureResponse createPostSignature(long expires, String bucketName, String objectKey) - throws ObsException { - PostSignatureRequest request = new PostSignatureRequest(expires, new Date(), bucketName, objectKey); - return this.createPostSignature(request); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#createPostSignature(com.obs.services.model.PostSignatureRequest) - */ - @Override - public PostSignatureResponse createPostSignature(PostSignatureRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "PostSignatureRequest is null"); - InterfaceLogBean reqBean = new InterfaceLogBean("createPostSignature", this.getEndpoint(), ""); - try { - PostSignatureResponse response = this._createPostSignature(request, this.getProviderCredentials().getAuthType() == AuthTypeEnum.V4); - reqBean.setRespTime(new Date()); - reqBean.setResultCode(Constants.RESULTCODE_SUCCESS); - if (ILOG.isInfoEnabled()) { - ILOG.info(reqBean); - } - return response; - } catch (Exception e) { - reqBean.setRespTime(new Date()); - if (ILOG.isErrorEnabled()) { - ILOG.error(reqBean); - } - throw new ObsException(e.getMessage(), e); - } - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#createBucket(java.lang.String) - */ - @Override - public ObsBucket createBucket(String bucketName) throws ObsException { - ObsBucket obsBucket = new ObsBucket(); - obsBucket.setBucketName(bucketName); - return this.createBucket(obsBucket); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#createBucket(java.lang.String, java.lang.String) - */ - @Override - public ObsBucket createBucket(String bucketName, String location) throws ObsException { - ObsBucket obsBucket = new ObsBucket(); - obsBucket.setBucketName(bucketName); - obsBucket.setLocation(location); - return this.createBucket(obsBucket); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#createBucket(com.obs.services.model.ObsBucket) - */ - @Override - public ObsBucket createBucket(final ObsBucket bucket) throws ObsException { - CreateBucketRequest request = new CreateBucketRequest(); - request.setBucketName(bucket.getBucketName()); - request.setAcl(bucket.getAcl()); - request.setBucketStorageClass(bucket.getBucketStorageClass()); - request.setLocation(bucket.getLocation()); - return this.createBucket(request); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#createBucket(com.obs.services.model.CreateBucketRequest) - */ - @Override - public ObsBucket createBucket(final CreateBucketRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "CreateBucketRequest is null"); - return this.doActionWithResult("createBucket", request.getBucketName(), - new ActionCallbackWithResult() { - @Override - public ObsBucket action() throws ServiceException { - if (isCname()) { - throw new ServiceException("createBucket is not allowed in customdomain mode"); - } - try { - return ObsClient.this.createBucketImpl(request); - } catch (ServiceException e) { - if(ObsClient.this.isAuthTypeNegotiation() && e.getResponseCode() == 400 && - "Unsupported Authorization Type".equals(e.getErrorMessage()) && - ObsClient.this.getProviderCredentials().getAuthType() == AuthTypeEnum.OBS) { - ObsClient.this.getProviderCredentials().setThreadLocalAuthType(AuthTypeEnum.V2); - return ObsClient.this.createBucketImpl(request); - } else { - throw e; - } - } - } - - @Override - void authTypeNegotiate(String bucketName) throws ServiceException { - AuthTypeEnum authTypeEnum = ObsClient.this.getApiVersionCache().getApiVersionInCache(bucketName); - if (authTypeEnum == null) { - authTypeEnum = ObsClient.this.getApiVersion(""); - } - ObsClient.this.getProviderCredentials().setThreadLocalAuthType(authTypeEnum); - } - }); - } - - @Deprecated - public ObsBucket createBucket(final S3Bucket bucket) throws ObsException { - ServiceUtils.asserParameterNotNull(bucket, "bucket is null"); - ObsBucket obsBucket = new ObsBucket(); - obsBucket.setBucketName(bucket.getBucketName()); - obsBucket.setLocation(bucket.getLocation()); - obsBucket.setAcl(bucket.getAcl()); - obsBucket.setMetadata(bucket.getMetadata()); - obsBucket.setBucketStorageClass(bucket.getBucketStorageClass()); - return this.createBucket(obsBucket); - } - - @Deprecated - public List listBuckets() throws ObsException { - List ret = this.listBuckets(null); - List buckets = new ArrayList(ret.size()); - buckets.addAll(ret); - return buckets; - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#listBuckets(com.obs.services.model.ListBucketsRequest) - */ - @Override - public List listBuckets(final ListBucketsRequest request) throws ObsException { - return this.listBucketsV2(request).getBuckets(); - } - - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#listBucketsV2(com.obs.services.model.ListBucketsRequest) - */ - @Override - public ListBucketsResult listBucketsV2(final ListBucketsRequest request) throws ObsException { - return this.doActionWithResult("listBuckets", "All Buckets", new ActionCallbackWithResult() { - @Override - public ListBucketsResult action() throws ServiceException { - if (isCname()) { - throw new ServiceException("listBuckets is not allowed in customdomain mode"); - } - return ObsClient.this.listAllBucketsImpl(request); - } - - @Override - void authTypeNegotiate(String bucketName) throws ServiceException { - AuthTypeEnum authTypeEnum = ObsClient.this.getApiVersion(""); - ObsClient.this.getProviderCredentials().setThreadLocalAuthType(authTypeEnum); - } - }); - } - - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#deleteBucket(java.lang.String) - */ - @Override - public HeaderResponse deleteBucket(final String bucketName) throws ObsException { - return this.doActionWithResult("deleteBucket", bucketName, new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.deleteBucketImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#listObjects(com.obs.services.model.ListObjectsRequest) - */ - @Override - public ObjectListing listObjects(final ListObjectsRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "ListObjectsRequest is null"); - return this.doActionWithResult("listObjects", request.getBucketName(), - new ActionCallbackWithResult() { - @Override - public ObjectListing action() throws ServiceException { - return ObsClient.this.listObjectsImpl(request); - } - - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#listObjects(java.lang.String) - */ - @Override - public ObjectListing listObjects(String bucketName) throws ObsException { - ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName); - return this.listObjects(listObjectsRequest); - } + public ObsClient(IObsCredentialsProvider provider, String endPoint) { + ServiceUtils.assertParameterNotNull(provider, "ObsCredentialsProvider is null"); + ObsConfiguration config = new ObsConfiguration(); + config.setEndPoint(endPoint); + this.init(provider.getSecurityKey().getAccessKey(), provider.getSecurityKey().getSecretKey(), + provider.getSecurityKey().getSecurityToken(), config); + this.credentials.setObsCredentialsProvider(provider); + } - /* (non-Javadoc) - * @see com.obs.services.IObsClient#headBucket(java.lang.String) - */ - @Override - public boolean headBucket(final String bucketName) throws ObsException { - return this.doActionWithResult("headBucket", bucketName, new ActionCallbackWithResult() { + public ObsClient(IObsCredentialsProvider provider, ObsConfiguration config) { + ServiceUtils.assertParameterNotNull(provider, "ObsCredentialsProvider is null"); + if (config == null) { + config = new ObsConfiguration(); + } + this.init(provider.getSecurityKey().getAccessKey(), provider.getSecurityKey().getSecretKey(), + provider.getSecurityKey().getSecurityToken(), config); + this.credentials.setObsCredentialsProvider(provider); + } - @Override - public Boolean action() throws ServiceException { - return ObsClient.this.headBucketImpl(bucketName); - } + @Override + public ReadAheadResult readAheadObjects(final ReadAheadRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "request is null"); + return this.doActionWithResult("readAheadObjects", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public ReadAheadResult action() throws ServiceException { + return ObsClient.this.readAheadObjectsImpl(request); - @Override - void authTypeNegotiate(String bucketName) throws ServiceException { - try { - AuthTypeEnum authTypeEnum = ObsClient.this.getApiVersion(bucketName); - ObsClient.this.getProviderCredentials().setThreadLocalAuthType(authTypeEnum); - } catch (ServiceException e) { - if (e.getResponseCode() != 404) { - throw e; } - } - } - }); - } - - /** - * List versioning objects in a bucket. - * - * @param bucketName - * Bucket name - * @param prefix - * Object name prefix used for listing versioning objects - * @param delimiter - * Character for grouping object names - * @param keyMarker - * Start position for listing versioning objects (sorted by object name) - * @param versionIdMarker - * Start position for listing versioning objects (sorted by version ID) - * @param maxKeys - * Maximum number of versioning objects to be listed - * @param nextVersionIdMarker - * Deprecated field - * @return Response to the request for listing versioning objects in the bucket - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - @Deprecated - public ListVersionsResult listVersions(final String bucketName, final String prefix, final String delimiter, - final String keyMarker, final String versionIdMarker, final long maxKeys, final String nextVersionIdMarker) - throws ObsException { - ListVersionsRequest request = new ListVersionsRequest(); - request.setBucketName(bucketName); - request.setPrefix(prefix); - request.setKeyMarker(keyMarker); - request.setMaxKeys((int) maxKeys); - request.setVersionIdMarker(versionIdMarker); - request.setDelimiter(delimiter); - return this.listVersions(request); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#listVersions(com.obs.services.model.ListVersionsRequest) - */ - @Override - public ListVersionsResult listVersions(final ListVersionsRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "ListVersionsRequest is null"); - return this.doActionWithResult("listVersions", request.getBucketName(), - new ActionCallbackWithResult() { - @Override - public ListVersionsResult action() throws ServiceException { - return ObsClient.this.listVersionsImpl(request); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#listVersions(java.lang.String) - */ - @Override - public ListVersionsResult listVersions(final String bucketName) throws ObsException { - ListVersionsRequest request = new ListVersionsRequest(); - request.setBucketName(bucketName); - return this.listVersions(request); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#listVersions(java.lang.String, long) - */ - @Override - public ListVersionsResult listVersions(final String bucketName, final long maxKeys) throws ObsException { - ListVersionsRequest request = new ListVersionsRequest(); - request.setBucketName(bucketName); - request.setMaxKeys((int) maxKeys); - return this.listVersions(request); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#listVersions(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, long) - */ - @Override - public ListVersionsResult listVersions(final String bucketName, final String prefix, final String delimiter, - final String keyMarker, final String versionIdMarker, final long maxKeys) throws ObsException { - return this.listVersions(bucketName, prefix, delimiter, keyMarker, versionIdMarker, maxKeys, null); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketMetadata(com.obs.services.model.BucketMetadataInfoRequest) - */ - @Override - public BucketMetadataInfoResult getBucketMetadata(final BucketMetadataInfoRequest request) - throws ObsException { - ServiceUtils.asserParameterNotNull(request, "BucketMetadataInfoRequest is null"); - return this.doActionWithResult("getBucketMetadata", request.getBucketName(), - new ActionCallbackWithResult() { - @Override - public BucketMetadataInfoResult action() throws ServiceException { - return ObsClient.this.getBucketMetadataImpl(request); - } - }); - } - - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketAcl(java.lang.String) - */ - @Override - public AccessControlList getBucketAcl(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketAcl", bucketName, new ActionCallbackWithResult() { - - @Override - public AccessControlList action() throws ServiceException { - return ObsClient.this.getBucketAclImpl(bucketName); - } - - }); - } - - /** - * Set a bucket ACL.
    - * - * @param bucketName - * Bucket name - * @param cannedACL - * Pre-defined access control policy - * @param acl - * ACL ("acl" and "cannedACL" cannot be used together.) - * @return Common response headers - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - @Deprecated - public HeaderResponse setBucketAcl(final String bucketName, final String cannedACL, final AccessControlList acl) - throws ObsException { - return this.doActionWithResult("setBucketAcl", bucketName, new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - if (acl == null && null == cannedACL) { - throw new IllegalArgumentException("Both CannedACL and AccessControlList is null"); - } - return ObsClient.this.setBucketAclImpl(bucketName, cannedACL, acl); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketAcl(java.lang.String, com.obs.services.model.AccessControlList) - */ - @Override - public HeaderResponse setBucketAcl(final String bucketName, final AccessControlList acl) throws ObsException { - return this.setBucketAcl(bucketName, null, acl); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketLocation(java.lang.String) - */ - @Override - public String getBucketLocation(final String bucketName) throws ObsException { - return this.getBucketLocationV2(bucketName).getLocation(); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketLocationV2(java.lang.String) - */ - @Override - public BucketLocationResponse getBucketLocationV2(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketLocation", bucketName, - new ActionCallbackWithResult() { - @Override - public BucketLocationResponse action() throws ServiceException { - return ObsClient.this.getBucketLocationImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketStorageInfo(java.lang.String) - */ - @Override - public BucketStorageInfo getBucketStorageInfo(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketStorageInfo", bucketName, - new ActionCallbackWithResult() { - - @Override - public BucketStorageInfo action() throws ServiceException { - return ObsClient.this.getBucketStorageInfoImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketQuota(java.lang.String) - */ - @Override - public BucketQuota getBucketQuota(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketQuota", bucketName, new ActionCallbackWithResult() { - - @Override - public BucketQuota action() throws ServiceException { - return ObsClient.this.getBucketQuotaImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketQuota(java.lang.String, com.obs.services.model.BucketQuota) - */ - @Override - public HeaderResponse setBucketQuota(final String bucketName, final BucketQuota bucketQuota) throws ObsException { - return this.doActionWithResult("setBucketQuota", bucketName, new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - ServiceUtils.asserParameterNotNull(bucketQuota, - "The bucket '" + bucketName + "' does not include Quota information"); - return ObsClient.this.setBucketQuotaImpl(bucketName, bucketQuota); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketStoragePolicy(java.lang.String) - */ - @Override - public BucketStoragePolicyConfiguration getBucketStoragePolicy(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketStoragePolicy", bucketName, - new ActionCallbackWithResult() { - - @Override - public BucketStoragePolicyConfiguration action() throws ServiceException { - return ObsClient.this.getBucketStoragePolicyImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketStoragePolicy(java.lang.String, com.obs.services.model.BucketStoragePolicyConfiguration) - */ - @Override - public HeaderResponse setBucketStoragePolicy(final String bucketName, - final BucketStoragePolicyConfiguration bucketStorage) throws ObsException { - return this.doActionWithResult("setBucketStoragePolicy", bucketName, - new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - ServiceUtils.asserParameterNotNull(bucketStorage, - "The bucket '" + bucketName + "' does not include storagePolicy information"); - return ObsClient.this.setBucketStorageImpl(bucketName, bucketStorage); - } - }); - } - - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketCors(java.lang.String, com.obs.services.model.BucketCors) - */ - @Override - public HeaderResponse setBucketCors(final String bucketName, final BucketCors bucketCors) throws ObsException { - return this.doActionWithResult("setBucketCors", bucketName, new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - ServiceUtils.asserParameterNotNull(bucketCors, "BucketCors is null"); - return ObsClient.this.setBucketCorsImpl(bucketName, bucketCors); - } - }); - } - - @Deprecated - public HeaderResponse setBucketCors(final String bucketName, final S3BucketCors s3BucketCors) throws ObsException { - ServiceUtils.asserParameterNotNull(s3BucketCors, - "The bucket '" + bucketName + "' does not include Cors information"); - BucketCors bucketCors = new BucketCors(); - bucketCors.setRules(s3BucketCors.getRules()); - return this.setBucketCors(bucketName, bucketCors); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketCors(java.lang.String) - */ - @Override - public BucketCors getBucketCors(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketCors", bucketName, new ActionCallbackWithResult() { - - @Override - public BucketCors action() throws ServiceException { - return ObsClient.this.getBucketCorsImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#deleteBucketCors(java.lang.String) - */ - @Override - public HeaderResponse deleteBucketCors(final String bucketName) throws ObsException { - return this.doActionWithResult("deleteBucketCors", bucketName, new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.deleteBucketCorsImpl(bucketName); - } - }); - } - - /** - * Pre-request a bucket. - * - * @param bucketName - * Bucket name - * @param optionInfo - * Parameters in a bucket preflight request - * @return Response to the bucket preflight request - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - @Deprecated - public OptionsInfoResult optionsBucket(final String bucketName, final OptionsInfoRequest optionInfo) - throws ObsException { - return this.doActionWithResult("optionsBucket", bucketName, new ActionCallbackWithResult() { - - @Override - public OptionsInfoResult action() throws ServiceException { - ServiceUtils.asserParameterNotNull(optionInfo, "OptionsInfoRequest is null"); - return ObsClient.this.optionsImpl(bucketName, null, optionInfo); - } - }); - } - - /** - * Perform a preflight on a bucket. - * - * @param bucketName - * Bucket name - * @param objectKey - * Object name - * @param optionInfo - * Parameters in an object preflight request - * @return Response to the object preflight request - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - @Deprecated - public OptionsInfoResult optionsObject(final String bucketName, final String objectKey, - final OptionsInfoRequest optionInfo) throws ObsException { - return this.doActionWithResult("optionsObject", bucketName, new ActionCallbackWithResult() { - - @Override - public OptionsInfoResult action() throws ServiceException { - ServiceUtils.asserParameterNotNull(optionInfo, "OptionsInfoRequest is null"); - return ObsClient.this.optionsImpl(bucketName, objectKey, optionInfo); - } - }); - } - - /** - * Obtain the logging settings of a bucket. - * - * @param bucketName - * Bucket name - * @return Logging settings of the bucket - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - @Deprecated - public BucketLoggingConfiguration getBucketLoggingConfiguration(final String bucketName) throws ObsException { - return this.getBucketLogging(bucketName); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketLogging(java.lang.String) - */ - @Override - public BucketLoggingConfiguration getBucketLogging(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketLoggingConfiguration", bucketName, - new ActionCallbackWithResult() { - @Override - public BucketLoggingConfiguration action() throws ServiceException { - return ObsClient.this.getBucketLoggingConfigurationImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketLoggingConfiguration(java.lang.String, com.obs.services.model.BucketLoggingConfiguration, boolean) - */ - @Override - public HeaderResponse setBucketLoggingConfiguration(final String bucketName, - final BucketLoggingConfiguration loggingConfiguration, final boolean updateTargetACLifRequired) - throws ObsException { - return this.doActionWithResult("setBucketLoggingConfiguration", bucketName, - new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.setBucketLoggingConfigurationImpl(bucketName, - loggingConfiguration == null ? new BucketLoggingConfiguration() : loggingConfiguration, - updateTargetACLifRequired); - } - }); - } - - @Deprecated - public HeaderResponse setBucketLoggingConfiguration(final String bucketName, - final BucketLoggingConfiguration loggingConfiguration) throws ObsException { - return this.setBucketLogging(bucketName, loggingConfiguration); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketLogging(java.lang.String, com.obs.services.model.BucketLoggingConfiguration) - */ - @Override - public HeaderResponse setBucketLogging(final String bucketName, - final BucketLoggingConfiguration loggingConfiguration) throws ObsException { - return this.setBucketLoggingConfiguration(bucketName, loggingConfiguration, false); - } - - @Deprecated - public HeaderResponse setBucketVersioning(String bucketName, String status) throws ObsException { - return this.setBucketVersioning(bucketName, new BucketVersioningConfiguration(status)); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketVersioning(java.lang.String, com.obs.services.model.BucketVersioningConfiguration) - */ - @Override - public HeaderResponse setBucketVersioning(final String bucketName, - final BucketVersioningConfiguration versioningConfiguration) throws ObsException { - return this.doActionWithResult("setBucketVersioning", bucketName, - new ActionCallbackWithResult() { - @Override - public HeaderResponse action() throws ServiceException { - ServiceUtils.asserParameterNotNull(versioningConfiguration, "BucketVersioningConfiguration is null"); - return ObsClient.this.setBucketVersioningImpl(bucketName, - versioningConfiguration.getVersioningStatus()); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketVersioning(java.lang.String) - */ - @Override - public BucketVersioningConfiguration getBucketVersioning(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketVersioning", bucketName, - new ActionCallbackWithResult() { - - @Override - public BucketVersioningConfiguration action() throws ServiceException { - return ObsClient.this.getBucketVersioningImpl(bucketName); - } - }); - } - - @Deprecated - public LifecycleConfiguration getBucketLifecycleConfiguration(final String bucketName) throws ObsException { - return this.getBucketLifecycle(bucketName); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketLifecycle(java.lang.String) - */ - @Override - public LifecycleConfiguration getBucketLifecycle(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketLifecycleConfiguration", bucketName, - new ActionCallbackWithResult() { - - @Override - public LifecycleConfiguration action() throws ServiceException { - return ObsClient.this.getBucketLifecycleConfigurationImpl(bucketName); - } - }); - } - - @Deprecated - public HeaderResponse setBucketLifecycleConfiguration(final String bucketName, - final LifecycleConfiguration lifecycleConfig) throws ObsException { - return this.setBucketLifecycle(bucketName, lifecycleConfig); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketLifecycle(java.lang.String, com.obs.services.model.LifecycleConfiguration) - */ - @Override - public HeaderResponse setBucketLifecycle(final String bucketName, - final LifecycleConfiguration lifecycleConfig) throws ObsException { - return this.doActionWithResult("setBucketLifecycleConfiguration", bucketName, - new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - ServiceUtils.asserParameterNotNull(lifecycleConfig, "LifecycleConfiguration is null"); - return ObsClient.this.setBucketLifecycleConfigurationImpl(bucketName, lifecycleConfig); - } - }); - } - - @Deprecated - public HeaderResponse deleteBucketLifecycleConfiguration(final String bucketName) throws ObsException { - return this.deleteBucketLifecycle(bucketName); - - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#deleteBucketLifecycle(java.lang.String) - */ - @Override - public HeaderResponse deleteBucketLifecycle(final String bucketName) throws ObsException { - return this.doActionWithResult("deleteBucketLifecycleConfiguration", bucketName, - new ActionCallbackWithResult() { - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.deleteBucketLifecycleConfigurationImpl(bucketName); - } - }); - - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketPolicy(java.lang.String) - */ - @Override - public String getBucketPolicy(final String bucketName) throws ObsException { - return this.getBucketPolicyV2(bucketName).getPolicy(); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketPolicyV2(java.lang.String) - */ - @Override - public BucketPolicyResponse getBucketPolicyV2(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketPolicy", bucketName, - new ActionCallbackWithResult() { - @Override - public BucketPolicyResponse action() throws ServiceException { - return ObsClient.this.getBucketPolicyImpl(bucketName); - } - - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketPolicy(java.lang.String, java.lang.String) - */ - @Override - public HeaderResponse setBucketPolicy(final String bucketName, final String policy) throws ObsException { - return this.doActionWithResult("setBucketPolicy", bucketName, new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - ServiceUtils.asserParameterNotNull(policy, "policy is null"); - return ObsClient.this.setBucketPolicyImpl(bucketName, policy); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#deleteBucketPolicy(java.lang.String) - */ - @Override - public HeaderResponse deleteBucketPolicy(final String bucketName) throws ObsException { - return this.doActionWithResult("deleteBucketPolicy", bucketName, - new ActionCallbackWithResult() { - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.deleteBucketPolicyImpl(bucketName); - } - }); - } - - @Deprecated - public WebsiteConfiguration getBucketWebsiteConfiguration(final String bucketName) throws ObsException { - return this.getBucketWebsite(bucketName); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketWebsite(java.lang.String) - */ - @Override - public WebsiteConfiguration getBucketWebsite(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketWebsiteConfiguration", bucketName, - new ActionCallbackWithResult() { - - @Override - public WebsiteConfiguration action() throws ServiceException { - return ObsClient.this.getBucketWebsiteConfigurationImpl(bucketName); - } - }); - } - - @Deprecated - public HeaderResponse setBucketWebsiteConfiguration(final String bucketName, - final WebsiteConfiguration websiteConfig) throws ObsException { - return this.setBucketWebsite(bucketName, websiteConfig); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketWebsite(java.lang.String, com.obs.services.model.WebsiteConfiguration) - */ - @Override - public HeaderResponse setBucketWebsite(final String bucketName, - final WebsiteConfiguration websiteConfig) throws ObsException { - return this.doActionWithResult("setBucketWebsiteConfiguration", bucketName, - new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - ServiceUtils.asserParameterNotNull(websiteConfig, "WebsiteConfiguration is null"); - return ObsClient.this.setBucketWebsiteConfigurationImpl(bucketName, websiteConfig); - } - }); - } - - @Deprecated - public HeaderResponse deleteBucketWebsiteConfiguration(final String bucketName) throws ObsException { - return this.deleteBucketWebsite(bucketName); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#deleteBucketWebsite(java.lang.String) - */ - @Override - public HeaderResponse deleteBucketWebsite(final String bucketName) throws ObsException { - return this.doActionWithResult("deleteBucketWebsiteConfiguration", bucketName, - new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.deleteBucketWebsiteConfigurationImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketTagging(java.lang.String) - */ - @Override - public BucketTagInfo getBucketTagging(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketTagging", bucketName, new ActionCallbackWithResult() { - - @Override - public BucketTagInfo action() throws ServiceException { - return ObsClient.this.getBucketTaggingImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketTagging(java.lang.String, com.obs.services.model.BucketTagInfo) - */ - @Override - public HeaderResponse setBucketTagging(final String bucketName, final BucketTagInfo bucketTagInfo) - throws ObsException { - return this.doActionWithResult("setBucketTagging", bucketName, new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - ServiceUtils.asserParameterNotNull(bucketTagInfo, "BucketTagInfo is null"); - return ObsClient.this.setBucketTaggingImpl(bucketName, bucketTagInfo); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#deleteBucketTagging(java.lang.String) - */ - @Override - public HeaderResponse deleteBucketTagging(final String bucketName) throws ObsException { - return this.doActionWithResult("deleteBucketTagging", bucketName, - new ActionCallbackWithResult() { + }); + } - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.deleteBucketTaggingImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketEncryption(java.lang.String) - */ @Override - public BucketEncryption getBucketEncryption(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketEncryption", bucketName, new ActionCallbackWithResult() { + public ReadAheadResult deleteReadAheadObjects(final String bucketName, final String prefix) throws ObsException { + ServiceUtils.assertParameterNotNull(bucketName, "bucketName is null"); + ServiceUtils.assertParameterNotNull(prefix, "prefix is null"); + return this.doActionWithResult("deleteReadAheadObjects", bucketName, + new ActionCallbackWithResult() { + @Override + public ReadAheadResult action() throws ServiceException { + return ObsClient.this.deleteReadAheadObjectsImpl(bucketName, prefix); - @Override - BucketEncryption action() throws ServiceException { - return ObsClient.this.getBucketEncryptionImpl(bucketName); - } - }); + } + }); } - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketEncryption(java.lang.String, com.obs.services.model.BucketEncryption) - */ @Override - public HeaderResponse setBucketEncryption(final String bucketName, final BucketEncryption bucketEncryption) + public ReadAheadQueryResult queryReadAheadObjectsTask(final String bucketName, final String taskId) throws ObsException { - return this.doActionWithResult("setBucketEncryption", bucketName, new ActionCallbackWithResult() { + ServiceUtils.assertParameterNotNull(bucketName, "bucketName is null"); + ServiceUtils.assertParameterNotNull(taskId, "taskId is null"); + return this.doActionWithResult("queryReadAheadObjectsTask", bucketName, + new ActionCallbackWithResult() { + @Override + public ReadAheadQueryResult action() throws ServiceException { + return ObsClient.this.queryReadAheadObjectsTaskImpl(bucketName, taskId); - @Override - HeaderResponse action() throws ServiceException { - ServiceUtils.asserParameterNotNull(bucketEncryption, "BucketEncryption is null"); - return ObsClient.this.setBucketEncryptionImpl(bucketName, bucketEncryption); - } - }); + } + }); } - /* (non-Javadoc) - * @see com.obs.services.IObsClient#deleteBucketEncryption(java.lang.String) - */ @Override - public HeaderResponse deleteBucketEncryption(final String bucketName) throws ObsException { - return this.doActionWithResult("deleteBucketEncryption", bucketName, new ActionCallbackWithResult() { - @Override - HeaderResponse action() throws ServiceException { - return ObsClient.this.deleteBucketEncryptionImpl(bucketName); - } - }); + public ObsFSBucket newBucket(NewBucketRequest request) throws ObsException { + ObsBucket bucket = this.createBucket(request); + ObsFSBucket fsBucket = new ObsFSBucket(bucket.getBucketName(), bucket.getLocation()); + setInnerClient(fsBucket, this); + return fsBucket; } - @Deprecated - public HeaderResponse setBucketReplicationConfiguration(final String bucketName, - final ReplicationConfiguration replicationConfiguration) throws ObsException { - return this.setBucketReplication(bucketName, replicationConfiguration); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketReplication(java.lang.String, com.obs.services.model.ReplicationConfiguration) - */ - @Override - public HeaderResponse setBucketReplication(final String bucketName, - final ReplicationConfiguration replicationConfiguration) throws ObsException { - return this.doActionWithResult("setBucketReplication", bucketName, - new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - ServiceUtils.asserParameterNotNull(replicationConfiguration, "ReplicationConfiguration is null"); - return ObsClient.this.setBucketReplicationConfigurationImpl(bucketName, - replicationConfiguration); - } - }); - } - - @Deprecated - public ReplicationConfiguration getBucketReplicationConfiguration(final String bucketName) throws ObsException { - return this.getBucketReplication(bucketName); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketReplication(java.lang.String) - */ - @Override - public ReplicationConfiguration getBucketReplication(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketReplicationConfiguration", bucketName, - new ActionCallbackWithResult() { - - @Override - public ReplicationConfiguration action() throws ServiceException { - return ObsClient.this.getBucketReplicationConfigurationImpl(bucketName); - } - }); - } - - @Deprecated - public HeaderResponse deleteBucketReplicationConfiguration(final String bucketName) throws ObsException { - return this.deleteBucketReplication(bucketName); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#deleteBucketReplication(java.lang.String) - */ - @Override - public HeaderResponse deleteBucketReplication(final String bucketName) throws ObsException { - return this.doActionWithResult("deleteBucketReplicationConfiguration", bucketName, - new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.deleteBucketReplicationConfigurationImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getBucketNotification(java.lang.String) - */ - @Override - public BucketNotificationConfiguration getBucketNotification(final String bucketName) throws ObsException { - return this.doActionWithResult("getBucketNotification", bucketName, - new ActionCallbackWithResult() { - - @Override - public BucketNotificationConfiguration action() throws ServiceException { - return ObsClient.this.getBucketNotificationConfigurationImpl(bucketName); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setBucketNotification(java.lang.String, com.obs.services.model.BucketNotificationConfiguration) - */ - @Override - public HeaderResponse setBucketNotification(final String bucketName, - final BucketNotificationConfiguration bucketNotificationConfiguration) throws ObsException { - return this.doActionWithResult("setBucketNotification", bucketName, - new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.setBucketNotificationImpl(bucketName, - bucketNotificationConfiguration == null ? new BucketNotificationConfiguration() - : bucketNotificationConfiguration); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#putObject(java.lang.String, java.lang.String, java.io.InputStream, com.obs.services.model.ObjectMetadata) - */ - @Override - public PutObjectResult putObject(String bucketName, String objectKey, InputStream input, ObjectMetadata metadata) - throws ObsException { - PutObjectRequest request = new PutObjectRequest(); - request.setBucketName(bucketName); - request.setInput(input); - request.setMetadata(metadata); - request.setObjectKey(objectKey); - return this.putObject(request); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#putObject(java.lang.String, java.lang.String, java.io.InputStream) - */ - @Override - public PutObjectResult putObject(String bucketName, String objectKey, InputStream input) throws ObsException { - return this.putObject(bucketName, objectKey, input, null); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#putObject(com.obs.services.model.PutObjectRequest) - */ - @Override - public PutObjectResult putObject(final PutObjectRequest request) throws ObsException { - - ServiceUtils.asserParameterNotNull(request, "PutObjectRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - - return this.doActionWithResult("putObject", request.getBucketName(), - new ActionCallbackWithResult() { - @Override - public PutObjectResult action() throws ServiceException { - if (null != request.getInput() && null != request.getFile()) { - throw new ServiceException("Both input and file are set, only one is allowed"); - } - return ObsClient.this.putObjectImpl(request); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#putObject(java.lang.String, java.lang.String, java.io.File) - */ - @Override - public PutObjectResult putObject(String bucketName, String objectKey, File file) throws ObsException { - return this.putObject(bucketName, objectKey, file, null); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#putObject(java.lang.String, java.lang.String, java.io.File, com.obs.services.model.ObjectMetadata) - */ - @Override - public PutObjectResult putObject(String bucketName, String objectKey, File file, ObjectMetadata metadata) - throws ObsException { - PutObjectRequest request = new PutObjectRequest(); - request.setBucketName(bucketName); - request.setFile(file); - request.setObjectKey(objectKey); - request.setMetadata(metadata); - return this.putObject(request); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#appendObject(com.obs.services.model.AppendObjectRequest) - */ - @Override - public AppendObjectResult appendObject(final AppendObjectRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "AppendObjectRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - - return this.doActionWithResult("appendObject", request.getBucketName(), - new ActionCallbackWithResult() { - @Override - public AppendObjectResult action() throws ServiceException { - if (null != request.getInput() && null != request.getFile()) { - throw new ServiceException("Both input and file are set, only one is allowed"); - } - return ObsClient.this.appendObjectImpl(request); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#uploadFile(com.obs.services.model.UploadFileRequest) - */ - @Override - public CompleteMultipartUploadResult uploadFile(UploadFileRequest uploadFileRequest) throws ObsException { - return new ResumableClient(this).uploadFileResume(uploadFileRequest); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#downloadFile(com.obs.services.model.DownloadFileRequest) - */ - @Override - public DownloadFileResult downloadFile(DownloadFileRequest downloadFileRequest) throws ObsException { - return new ResumableClient(this).downloadFileResume(downloadFileRequest); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getObject(com.obs.services.model.GetObjectRequest) - */ - @Override - public ObsObject getObject(final GetObjectRequest request) throws ObsException { - - ServiceUtils.asserParameterNotNull(request, "GetObjectRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - return this.doActionWithResult("getObject", request.getBucketName(), new ActionCallbackWithResult() { - - @Override - public ObsObject action() throws ServiceException { - return ObsClient.this.getObjectImpl(request); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getObject(java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public ObsObject getObject(final String bucketName, final String objectKey, final String versionId) - throws ObsException { - return this.getObject(new GetObjectRequest(bucketName, objectKey, versionId)); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getObject(java.lang.String, java.lang.String) - */ - @Override - public ObsObject getObject(final String bucketName, final String objectKey) throws ObsException { - return this.getObject(bucketName, objectKey, null); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getObjectMetadata(com.obs.services.model.GetObjectMetadataRequest) - */ - @Override - public ObjectMetadata getObjectMetadata(final GetObjectMetadataRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "GetObjectMetadataRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - return this.doActionWithResult("getObjectMetadata", request.getBucketName(), - new ActionCallbackWithResult() { - - @Override - public ObjectMetadata action() throws ServiceException { - return ObsClient.this.getObjectMetadataImpl(request); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setObjectMetadata(com.obs.services.model.SetObjectMetadataRequest) - */ - @Override - public ObjectMetadata setObjectMetadata(final SetObjectMetadataRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "SetObjectMetadataRequest is null"); - return this.doActionWithResult("setObjectMetadata", request.getBucketName(), - new ActionCallbackWithResult() { - @Override - public ObjectMetadata action() throws ServiceException { - return ObsClient.this.setObjectMetadataImpl(request); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getObjectMetadata(java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public ObjectMetadata getObjectMetadata(String bucketName, String objectKey, String versionId) throws ObsException { - GetObjectMetadataRequest request = new GetObjectMetadataRequest(); - request.setBucketName(bucketName); - request.setObjectKey(objectKey); - request.setVersionId(versionId); - return this.getObjectMetadata(request); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getObjectMetadata(java.lang.String, java.lang.String) - */ - @Override - public ObjectMetadata getObjectMetadata(String bucketName, String objectKey) throws ObsException { - return this.getObjectMetadata(bucketName, objectKey, null); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#restoreObject(com.obs.services.model.RestoreObjectRequest) - */ - @Deprecated - public RestoreObjectStatus restoreObject(final RestoreObjectRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "RestoreObjectRequest is null"); - return this.doActionWithResult("restoreObject", request.getBucketName(), - new ActionCallbackWithResult() { - - @Override - public RestoreObjectStatus action() throws ServiceException { - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - return ObsClient.this.restoreObjectImpl(request); - } - }); - - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#restoreObjectV2(com.obs.services.model.RestoreObjectRequest) - */ - @Override - public RestoreObjectResult restoreObjectV2(final RestoreObjectRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "RestoreObjectRequest is null"); - return this.doActionWithResult("restoreObjectV2", request.getBucketName(), - new ActionCallbackWithResult() { - - @Override - public RestoreObjectResult action() throws ServiceException { - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - return ObsClient.this.restoreObjectV2Impl(request); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#restoreObjects(com.obs.services.model.RestoreObjectsRequest) - */ - @Override - public TaskProgressStatus restoreObjects(RestoreObjectsRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "RestoreObjectsRequest is null"); - if (! this.isCname()) { - ServiceUtils.asserParameterNotNull(request.getBucketName(), "bucketName is null"); - } - - if (request.getKeyAndVersions() != null && request.getPrefix() != null) { - throw new IllegalArgumentException("Prefix and keyandVersions cannot coexist in the same request"); - } - - int days = request.getDays(); - if (! (days >= 1 && days <= 30)) { - throw new IllegalArgumentException("Restoration days should be at least 1 and at most 30"); - } - DefaultTaskProgressStatus progreStatus = new DefaultTaskProgressStatus(); - ThreadPoolExecutor executor = this.initThreadPool(request); - - try { - String bucketName = request.getBucketName(); - String prefix = request.getPrefix(); - RestoreTierEnum tier = request.getRestoreTier(); - boolean versionRestored = request.isVersionRestored(); - TaskCallback callback; - TaskProgressListener listener; - callback = (request.getCallback() == null) ? new LazyTaksCallback() : request.getCallback(); - listener = request.getProgressListener() ; - int progressInterval = request.getProgressInterval(); - int totalTasks = 0; - if (request.getKeyAndVersions() != null) { - totalTasks = request.getKeyAndVersions().size(); - for (KeyAndVersion kv : request.getKeyAndVersions()) { - RestoreObjectRequest taskRequest = new RestoreObjectRequest(bucketName, kv.getKey(), kv.getVersion(), days, tier); - RestoreObjectTask task = new RestoreObjectTask(this, bucketName, taskRequest, callback, listener, progreStatus, progressInterval); - executor.execute(task); - } - } else { - if (versionRestored) { - ListVersionsResult versionResult; - ListVersionsRequest listRequest = new ListVersionsRequest(bucketName); - listRequest.setPrefix(prefix); - do{ - versionResult = this.listVersions(listRequest); - for(VersionOrDeleteMarker v : versionResult.getVersions()) { - if (v.getObjectStorageClass() == StorageClassEnum.COLD) { - totalTasks ++; - RestoreObjectRequest taskRequest = new RestoreObjectRequest(bucketName, v.getKey(), v.getVersionId(), days, tier); - RestoreObjectTask task = new RestoreObjectTask(this, bucketName, taskRequest, callback, listener, progreStatus, progressInterval); - executor.execute(task); - if (ILOG.isInfoEnabled()) { - if (totalTasks % 1000 == 0) { - ILOG.info("RestoreObjects: " + totalTasks + " tasks have submitted to restore objects"); - } - } - } - } - listRequest.setKeyMarker(versionResult.getNextKeyMarker()); - listRequest.setVersionIdMarker(versionResult.getNextVersionIdMarker()); - } while(versionResult.isTruncated()); - } else { - ObjectListing objectsResult; - ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName); - listObjectsRequest.setPrefix(prefix); - do{ - objectsResult = this.listObjects(listObjectsRequest); - for(ObsObject o : objectsResult.getObjects()) { - if (o.getMetadata().getObjectStorageClass() == StorageClassEnum.COLD) { - totalTasks ++; - RestoreObjectRequest taskRequest = new RestoreObjectRequest(bucketName, o.getObjectKey(), null, days, tier); - RestoreObjectTask task = new RestoreObjectTask(this, bucketName, taskRequest, callback, listener, progreStatus, progressInterval); - executor.execute(task); - if (ILOG.isInfoEnabled()) { - if (totalTasks % 1000 == 0) { - ILOG.info("RestoreObjects: " + totalTasks + " tasks have submitted to restore objects"); - } - } - } - } - listObjectsRequest.setMarker(objectsResult.getNextMarker()); - } while(objectsResult.isTruncated()); - } - } - - progreStatus.setTotalTaskNum(totalTasks); - executor.shutdown(); - executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); - } catch (ObsException e) { - throw e; - } catch (Exception e) { - throw new ObsException(e.getMessage(), e); - } - return progreStatus; + @Override + public ObsFSFile newFile(NewFileRequest request) throws ObsException { + ObsFSFile obsFile = (ObsFSFile) this.putObject(request); + setInnerClient(obsFile, this); + return obsFile; } - - @Override - public UploadProgressStatus putObjects(final PutObjectsRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "PutObjectsRequest is null"); - - ThreadPoolExecutor executor = this.initThreadPool(request); - Date now = new Date(); - UploadTaskProgressStatus progressStatus = new UploadTaskProgressStatus(request.getTaskProgressInterval(), now); - - try { - UploadObjectsProgressListener listener = request.getUploadObjectsProgressListener(); - TaskCallback callback = (request.getCallback() == null) ? - new LazyTaksCallback() : request.getCallback(); - String prefix = request.getPrefix() == null ? "" : request.getPrefix(); - int totalTasks = 0; - if(request.getFolderPath() != null) { - String folderPath = request.getFolderPath(); - File fileRoot = new File(folderPath); - - if(fileRoot.exists()) { - if(fileRoot.isDirectory()) { - String folderRoot = fileRoot.getName(); - LinkedList list = new LinkedList(); - list.add(fileRoot); - File[] files = fileRoot.listFiles(); - File temp_file; - while (!list.isEmpty()) { - temp_file = list.removeFirst(); - if(null == temp_file) { - continue; - } - files = temp_file.listFiles(); - for (File file : files) { - if (file.isDirectory()) { - if(!file.exists()) { - String filePath = file.getCanonicalPath(); - String erroInfo = "putObjects: the folder \"" + filePath + "\" dose not a folder"; - ILOG.warn(erroInfo); - }else { - list.add(file); - } - } else { - // File upload - String filePath = file.getCanonicalPath(); - if(!file.exists()) { - ILOG.warn("putObjects: the file \"" + filePath + "\" dose not exist"); - continue; - } - totalTasks ++; - String objectKey = prefix + folderRoot - + filePath.substring(folderPath.length(), filePath.length()).replace("\\", "/"); - uploadObjectTask(request, filePath, objectKey, executor, progressStatus, callback, listener); - } - } - } - }else { - String erroInfo = "putObjects: the folder \"" + folderPath + "\" dose not a folder"; - ILOG.warn(erroInfo); - throw new ObsException(erroInfo); - } - - }else { - String erroInfo = "putObjects: the folder \"" + folderPath + "\" dose not exist"; - ILOG.warn(erroInfo); - throw new ObsException(erroInfo); - } - - }else if (request.getFilePaths() != null) { - for(String filePath: request.getFilePaths()) { - File file = new File(filePath); - if(file.exists()) { - totalTasks ++; - String objectKey = prefix + file.getName(); - // File upload - uploadObjectTask(request, filePath, objectKey, executor, progressStatus, callback, listener); - }else { - ILOG.warn("putObjects: the file \"" + filePath + "\" is not exist"); - } - } - } - - progressStatus.setTotalTaskNum(totalTasks); - executor.shutdown(); - executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); - }catch (ObsException e) { - throw e; - }catch (Exception e) { - throw new ObsException(e.getMessage(), e); - } - - return progressStatus; - } - - private void uploadObjectTask(final PutObjectsRequest request, final String filePath, final String objectKey - , final ThreadPoolExecutor executor, final UploadTaskProgressStatus progressStatus - , final TaskCallback callback, final UploadObjectsProgressListener listener) { - File fileObject = new File(filePath); - String bucketName = request.getBucketName(); - int progressInterval = request.getProgressInterval(); - int taskNum = request.getTaskNum(); - long detailProgressInterval = request.getDetailProgressInterval(); - long bigfileThreshold = request.getBigfileThreshold(); - long partSize = request.getPartSize(); - AccessControlList acl = request.getAcl(); - Map> extensionPermissionMap = request.getExtensionPermissionMap(); - SseCHeader sseCHeader = request.getSseCHeader(); - SseKmsHeader sseKmsHeader = request.getSseKmsHeader(); - String successRedirectLocation = request.getSuccessRedirectLocation(); - - if(fileObject.length() > bigfileThreshold) { - UploadFileRequest taskRequest = new UploadFileRequest(bucketName, objectKey); - taskRequest.setUploadFile(filePath); - taskRequest.setPartSize(partSize); - taskRequest.setTaskNum(taskNum); - taskRequest.setExtensionPermissionMap(extensionPermissionMap); - taskRequest.setAcl(acl); - taskRequest.setSuccessRedirectLocation(successRedirectLocation); - taskRequest.setSseCHeader(sseCHeader); - taskRequest.setSseKmsHeader(sseKmsHeader); - taskRequest.setEnableCheckpoint(true); - progressStatus.addTotalSize(fileObject.length()); - taskRequest.setProgressListener(new ProgressListener() { - - @Override - public void progressChanged(ProgressStatus status) { - progressStatus.putTaskTable(objectKey, status); - if(progressStatus.isRefreshprogress()) { - Date dateNow = new Date(); - long totalMilliseconds = dateNow.getTime() - progressStatus.getStartDate().getTime(); - progressStatus.setTotalMilliseconds(totalMilliseconds); - listener.progressChanged(progressStatus); - } - } - }); - taskRequest.setProgressInterval(detailProgressInterval); - - ResumableUploadTask task = new ResumableUploadTask(this, bucketName, taskRequest - , callback, listener, progressStatus, progressInterval); - executor.execute(task); - }else { - PutObjectRequest taskRequest = new PutObjectRequest(bucketName, objectKey, fileObject); - taskRequest.setExtensionPermissionMap(extensionPermissionMap); - taskRequest.setAcl(acl); - taskRequest.setSuccessRedirectLocation(successRedirectLocation); - taskRequest.setSseCHeader(sseCHeader); - taskRequest.setSseKmsHeader(sseKmsHeader); - progressStatus.addTotalSize(fileObject.length()); - taskRequest.setProgressListener(new ProgressListener() { - - @Override - public void progressChanged(ProgressStatus status) { - progressStatus.putTaskTable(objectKey, status); - if(progressStatus.isRefreshprogress()) { - Date dateNow = new Date(); - long totalMilliseconds = dateNow.getTime() - progressStatus.getStartDate().getTime(); - progressStatus.setTotalMilliseconds(totalMilliseconds); - listener.progressChanged(progressStatus); - } - } - }); - taskRequest.setProgressInterval(detailProgressInterval); - PutObjectTask task = new PutObjectTask(this, bucketName, taskRequest, callback, listener - , progressStatus, progressInterval); - executor.execute(task); - } - } - - - /* (non-Javadoc) - * @see com.obs.services.IFSClient#deleteFolder(com.obs.services.model.fs.DeleteFSFolderRequest) - */ - @Override - public TaskProgressStatus dropFolder(DropFolderRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "DropFolderRequest is null"); - if (! this.isCname()) { - ServiceUtils.asserParameterNotNull(request.getBucketName(), "bucketName is null"); - } - ThreadPoolExecutor executor = this.initThreadPool(request); - DefaultTaskProgressStatus progressStatus = new DefaultTaskProgressStatus(); - try { - String bucketName = request.getBucketName(); - String folderName = request.getFolderName(); - String delimiter = this.getFileSystemDelimiter(); - if (! folderName.endsWith(delimiter)) { - folderName = folderName + delimiter; - } - TaskCallback callback; - TaskProgressListener listener; - callback = (request.getCallback() == null) ? new LazyTaksCallback() : request.getCallback(); - listener = request.getProgressListener(); - int interval = request.getProgressInterval(); - int[] totalTasks = {0}; - boolean isSubDeleted = recurseFolders(folderName, bucketName, callback, interval, progressStatus, listener, executor, totalTasks); - Map> futures = new HashMap>(); - totalTasks[0] ++; - progressStatus.setTotalTaskNum(totalTasks[0]); - - if (isSubDeleted) { - submitDropTask(folderName, bucketName, callback, interval, progressStatus, listener, executor, futures); - checkDropFutures(futures, progressStatus, callback, listener, interval); - } else { - progressStatus.failTaskIncrement(); - callback.onException(new ObsException("Failed to delete due to child file deletion failed"), folderName); - recordBulkTaskStatus(progressStatus, callback, listener, interval); + @Override + public ObsFSFolder newFolder(NewFolderRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "CreateFolderRequest is null"); + if (request.getObjectKey() != null) { + String delimiter = this.getFileSystemDelimiter(); + if (!request.getObjectKey().endsWith(delimiter)) { + request.setObjectKey(request.getObjectKey() + delimiter); } - - executor.shutdown(); - executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); - } catch (ObsException e) { - throw e; - } catch (Exception e) { - throw new ObsException(e.getMessage(), e); } - return progressStatus; + + ObsFSFolder obsFolder = (ObsFSFolder) this.putObject(new PutObjectRequest(request)); + setInnerClient(obsFolder, this); + return obsFolder; } - - private boolean recurseFolders(String folders, String bucketName, TaskCallback callback, int interval, - DefaultTaskProgressStatus progressStatus, TaskProgressListener listener, ThreadPoolExecutor executor, int[] count) { - ListObjectsRequest request = new ListObjectsRequest(bucketName); - request.setDelimiter("/"); - request.setPrefix(folders); - ObjectListing result; - boolean isDeleted = true; - do { - result = this.listObjects(request); - Map> futures = new HashMap>(); - - for (ObsObject o : result.getObjects()) { - if (!o.getObjectKey().endsWith("/")) { - count[0] ++; - isDeleted = submitDropTask(o.getObjectKey(),bucketName,callback,interval,progressStatus,listener,executor, futures) && isDeleted; - if (ILOG.isInfoEnabled()) { - if (count[0] % 1000 == 0) { - ILOG.info("DropFolder: " + Arrays.toString(count) + " tasks have submitted to delete objects"); + + @Override + public ObsFSFile writeFile(final WriteFileRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request, "WriteFileRequest is null"); + ServiceUtils.assertParameterNotNull2(request.getObjectKey(), "objectKey is null"); + ObsFSFile obsFile = this.doActionWithResult("writeFile", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public ObsFSFile action() throws ServiceException { + if (null != request.getInput() && null != request.getFile()) { + throw new ServiceException("Both input and file are set, only one is allowed"); } + return ObsClient.this.writeFileImpl(request); } - } - } - - for (String prefix : result.getCommonPrefixes()) { - boolean isSubDeleted = recurseFolders(prefix, bucketName, callback, interval, progressStatus, listener, executor, count); - count[0] ++; - if (isSubDeleted) { - isDeleted = submitDropTask(prefix, bucketName, callback, interval, progressStatus, listener, executor, futures) && isDeleted; - } else { - progressStatus.failTaskIncrement(); - callback.onException(new ObsException("Failed to delete due to child file deletion failed"), prefix); - recordBulkTaskStatus(progressStatus, callback, listener, interval); - } - if (ILOG.isInfoEnabled()) { - if (count[0] % 1000 == 0) { - ILOG.info("DropFolder: " + count + " tasks have submitted to delete objects"); - } - } - } - - request.setMarker(result.getNextMarker()); - isDeleted = checkDropFutures(futures, progressStatus, callback, listener, interval) && isDeleted; - } while (result.isTruncated()); - return isDeleted; - } - - private boolean submitDropTask(String key, String bucketName, TaskCallback callback, int interval, - DefaultTaskProgressStatus progreStatus, TaskProgressListener listener, ThreadPoolExecutor executor, Map> futures) { - DropFolderTask task = new DropFolderTask(this, bucketName, key, progreStatus, listener, interval, callback); - try { - futures.put(key, executor.submit(task)); - } catch (RejectedExecutionException e) { - progreStatus.failTaskIncrement(); - callback.onException(new ObsException(e.getMessage(), e), key); - return false; - } - return true; + }); + setInnerClient(obsFile, this); + return obsFile; } - private boolean checkDropFutures(Map> futures, DefaultTaskProgressStatus progressStatus, - TaskCallback callback, TaskProgressListener listener, int interval) { - boolean isDeleted = true; - for (Entry> entry :futures.entrySet()) { - try { - entry.getValue().get(); - } catch(ExecutionException e) { - progressStatus.failTaskIncrement(); - if (e.getCause() instanceof ObsException) { - callback.onException((ObsException)e.getCause(), entry.getKey()); - } else { - callback.onException(new ObsException(e.getMessage(), e), entry.getKey()); + private static void setInnerClient(final Object obj, final ObsClient obsClient) { + if (obj != null && obsClient != null) { + final Class clazz = obj.getClass(); + final String name = clazz.getName(); + + // fix findbugs: DP_DO_INSIDE_DO_PRIVILEGED + AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Object run() { + Field f = fields.get(name); + try { + if (f == null) { + f = getFieldFromClass(clazz, "innerClient"); + f.setAccessible(true); + fields.put(name, f); + } + f.set(obj, obsClient); + } catch (Exception e) { + throw new ObsException(e.getMessage(), e); + } + return null; } - isDeleted = false; - } catch(InterruptedException e) { - progressStatus.failTaskIncrement(); - callback.onException(new ObsException(e.getMessage(), e), entry.getKey()); - isDeleted = false; - } - recordBulkTaskStatus(progressStatus, callback, listener, interval); + + }); } - return isDeleted; } - - - - - - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#deleteObject(java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public DeleteObjectResult deleteObject(final String bucketName, final String objectKey, final String versionId) - throws ObsException { - return this.doActionWithResult("deleteObject", bucketName, new ActionCallbackWithResult() { - - @Override - public DeleteObjectResult action() throws ServiceException { - ServiceUtils.asserParameterNotNull2(objectKey, "objectKey is null"); - return ObsClient.this.deleteObjectImpl(bucketName, objectKey, versionId); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#deleteObject(java.lang.String, java.lang.String) - */ - @Override - public DeleteObjectResult deleteObject(final String bucketName, final String objectKey) throws ObsException { - return this.deleteObject(bucketName, objectKey, null); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#deleteObjects(com.obs.services.model.DeleteObjectsRequest) - */ - @Override - public DeleteObjectsResult deleteObjects(final DeleteObjectsRequest deleteObjectsRequest) throws ObsException { - ServiceUtils.asserParameterNotNull(deleteObjectsRequest, "DeleteObjectsRequest is null"); - return this.doActionWithResult("deleteObjects", deleteObjectsRequest.getBucketName(), - new ActionCallbackWithResult() { - - @Override - public DeleteObjectsResult action() throws ServiceException { - return ObsClient.this.deleteObjectsImpl(deleteObjectsRequest); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getObjectAcl(java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public AccessControlList getObjectAcl(final String bucketName, final String objectKey, final String versionId) - throws ObsException { - ServiceUtils.asserParameterNotNull2(objectKey, "objectKey is null"); - return this.doActionWithResult("getObjectAcl", bucketName, new ActionCallbackWithResult() { - @Override - public AccessControlList action() throws ServiceException { - return ObsClient.this.getObjectAclImpl(bucketName, objectKey, versionId); - } - - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#getObjectAcl(java.lang.String, java.lang.String) - */ - @Override - public AccessControlList getObjectAcl(final String bucketName, final String objectKey) throws ObsException { - return this.getObjectAcl(bucketName, objectKey, null); - } - - /** - * Set an object ACL. - * - * @param bucketName - * Bucket name - * @param objectKey - * Object name - * @param cannedACL - * Pre-defined access control policy - * @param acl - * ACL ("acl" and "cannedACL" cannot be used together.) - * @param versionId - * Object version ID - * @return Common response headers - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - @Deprecated - public HeaderResponse setObjectAcl(final String bucketName, final String objectKey, final String cannedACL, - final AccessControlList acl, final String versionId) throws ObsException { - - return this.doActionWithResult("setObjectAcl", bucketName, new ActionCallbackWithResult() { - @Override - public HeaderResponse action() throws ServiceException { - if (acl == null && null == cannedACL) { - throw new IllegalArgumentException("Both cannedACL and AccessControlList is null"); - } - return ObsClient.this.setObjectAclImpl(bucketName, objectKey, cannedACL, acl, versionId); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setObjectAcl(java.lang.String, java.lang.String, com.obs.services.model.AccessControlList, java.lang.String) - */ - @Override - public HeaderResponse setObjectAcl(final String bucketName, final String objectKey, final AccessControlList acl, - final String versionId) throws ObsException { - return this.setObjectAcl(bucketName, objectKey, null, acl, versionId); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#setObjectAcl(java.lang.String, java.lang.String, com.obs.services.model.AccessControlList) - */ - @Override - public HeaderResponse setObjectAcl(final String bucketName, final String objectKey, final AccessControlList acl) - throws ObsException { - return this.setObjectAcl(bucketName, objectKey, null, acl, null); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#copyObject(com.obs.services.model.CopyObjectRequest) - */ - @Override - public CopyObjectResult copyObject(final CopyObjectRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "CopyObjectRequest is null"); - ServiceUtils.asserParameterNotNull(request.getDestinationBucketName(), "destinationBucketName is null"); - ServiceUtils.asserParameterNotNull2(request.getSourceObjectKey(), "sourceObjectKey is null"); - ServiceUtils.asserParameterNotNull2(request.getDestinationObjectKey(), "destinationObjectKey is null"); - return this.doActionWithResult("copyObject", request.getSourceBucketName(), - new ActionCallbackWithResult() { - @Override - public CopyObjectResult action() throws ServiceException { - return ObsClient.this.copyObjectImpl(request); - } - }); - - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#copyObject(java.lang.String, java.lang.String, java.lang.String, java.lang.String) - */ - @Override - public CopyObjectResult copyObject(String sourceBucketName, String sourceObjectKey, String destBucketName, - String destObjectKey) throws ObsException { - return this.copyObject(new CopyObjectRequest(sourceBucketName, sourceObjectKey, destBucketName, destObjectKey)); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#initiateMultipartUpload(com.obs.services.model.InitiateMultipartUploadRequest) - */ - @Override - public InitiateMultipartUploadResult initiateMultipartUpload(final InitiateMultipartUploadRequest request) - throws ObsException { - ServiceUtils.asserParameterNotNull(request, "InitiateMultipartUploadRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - return this.doActionWithResult("initiateMultipartUpload", request.getBucketName(), - new ActionCallbackWithResult() { - @Override - public InitiateMultipartUploadResult action() throws ServiceException { - return ObsClient.this.initiateMultipartUploadImpl(request); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#abortMultipartUpload(com.obs.services.model.AbortMultipartUploadRequest) - */ - @Override - public HeaderResponse abortMultipartUpload(final AbortMultipartUploadRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "AbortMultipartUploadRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - ServiceUtils.asserParameterNotNull(request.getUploadId(), "uploadId is null"); - return this.doActionWithResult("abortMultipartUpload", request.getBucketName(), - new ActionCallbackWithResult() { - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.abortMultipartUploadImpl(request.getUploadId(), request.getBucketName(), - request.getObjectKey()); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#uploadPart(java.lang.String, java.lang.String, java.lang.String, int, java.io.InputStream) - */ - @Override - public UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId, int partNumber, - InputStream input) throws ObsException { - UploadPartRequest request = new UploadPartRequest(); - request.setBucketName(bucketName); - request.setObjectKey(objectKey); - request.setUploadId(uploadId); - request.setPartNumber(partNumber); - request.setInput(input); - return this.uploadPart(request); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#uploadPart(java.lang.String, java.lang.String, java.lang.String, int, java.io.File) - */ - @Override - public UploadPartResult uploadPart(String bucketName, String objectKey, String uploadId, int partNumber, File file) - throws ObsException { - UploadPartRequest request = new UploadPartRequest(); - request.setBucketName(bucketName); - request.setObjectKey(objectKey); - request.setUploadId(uploadId); - request.setPartNumber(partNumber); - request.setFile(file); - return this.uploadPart(request); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#uploadPart(com.obs.services.model.UploadPartRequest) - */ - @Override - public UploadPartResult uploadPart(final UploadPartRequest request) throws ObsException { - - ServiceUtils.asserParameterNotNull(request, "UploadPartRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - ServiceUtils.asserParameterNotNull(request.getUploadId(), "uploadId is null"); - return this.doActionWithResult("uploadPart", request.getBucketName(), - new ActionCallbackWithResult() { - - @Override - public UploadPartResult action() throws ServiceException { - if (null != request.getInput() && null != request.getFile()) { - throw new ServiceException("Both input and file are set, only one is allowed"); - } - return ObsClient.this.uploadPartImpl(request); - } - }); - - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#copyPart(com.obs.services.model.CopyPartRequest) - */ - @Override - public CopyPartResult copyPart(final CopyPartRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "CopyPartRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getSourceObjectKey(), "sourceObjectKey is null"); - ServiceUtils.asserParameterNotNull(request.getDestinationBucketName(), "destinationBucketName is null"); - ServiceUtils.asserParameterNotNull2(request.getDestinationObjectKey(), "destinationObjectKey is null"); - ServiceUtils.asserParameterNotNull(request.getUploadId(), "uploadId is null"); - return this.doActionWithResult("copyPart", request.getSourceBucketName(), - new ActionCallbackWithResult() { - - @Override - public CopyPartResult action() throws ServiceException { - return ObsClient.this.copyPartImpl(request); - } - }); - - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#completeMultipartUpload(com.obs.services.model.CompleteMultipartUploadRequest) - */ - @Override - public CompleteMultipartUploadResult completeMultipartUpload(final CompleteMultipartUploadRequest request) - throws ObsException { - ServiceUtils.asserParameterNotNull(request, "CompleteMultipartUploadRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - ServiceUtils.asserParameterNotNull(request.getUploadId(), "uploadId is null"); - return this.doActionWithResult("completeMultipartUpload", request.getBucketName(), - new ActionCallbackWithResult() { - @Override - public CompleteMultipartUploadResult action() throws ServiceException { - return ObsClient.this.completeMultipartUploadImpl(request); - } - }); - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#listParts(com.obs.services.model.ListPartsRequest) - */ - @Override - public ListPartsResult listParts(final ListPartsRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "ListPartsRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getKey(), "objectKey is null"); - ServiceUtils.asserParameterNotNull(request.getUploadId(), "uploadId is null"); - return this.doActionWithResult("listParts", request.getBucketName(), - new ActionCallbackWithResult() { - - @Override - public ListPartsResult action() throws ServiceException { - return ObsClient.this.listPartsImpl(request); - } - }); - - } - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#listMultipartUploads(com.obs.services.model.ListMultipartUploadsRequest) - */ - @Override - public MultipartUploadListing listMultipartUploads(final ListMultipartUploadsRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "ListMultipartUploadsRequest is null"); - return this.doActionWithResult("listMultipartUploads", request.getBucketName(), - new ActionCallbackWithResult() { - - @Override - public MultipartUploadListing action() throws ServiceException { - return ObsClient.this.listMultipartUploadsImpl(request); - } - }); - - } - - @Override - public RenameObjectResult renameObject(final String bucketName, final String objectKey, final String newObjectKey) throws ObsException { - RenameObjectRequest request = new RenameObjectRequest(); - request.setBucketName(bucketName); - request.setObjectKey(objectKey); - request.setNewObjectKey(newObjectKey); - return this.renameObject(request); - } - - @Override - public RenameObjectResult renameObject(final RenameObjectRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "RenameObjectRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "ObjectKey is null"); - ServiceUtils.asserParameterNotNull2(request.getNewObjectKey(), "NewObjectKey is null"); - - return this.doActionWithResult("renameObject", request.getBucketName(), new ActionCallbackWithResult() { - @Override - public RenameObjectResult action() throws ServiceException { - return ObsClient.this.renameObjectImpl(request); - } - }); - } - - @Override - public TruncateObjectResult truncateObject(final String bucketName, final String objectKey, final long newLength) throws ObsException { - TruncateObjectRequest request = new TruncateObjectRequest(); - request.setBucketName(bucketName); - request.setObjectKey(objectKey); - request.setNewLength(newLength); - return this.truncateObject(request); - } - - @Override - public TruncateObjectResult truncateObject(final TruncateObjectRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "TruncateObjectRequest is null"); - ServiceUtils.asserParameterNotNull(request.getNewLength(), "NewLength is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "ObjectKey is null"); - return this.doActionWithResult("truncateObject", request.getBucketName(), new ActionCallbackWithResult() { - - @Override - public TruncateObjectResult action() throws ServiceException { - return ObsClient.this.truncateObjectImpl(request); - } - }); - } - - @Override - public ModifyObjectResult modifyObject(final String bucketName, final String objectKey, final long position, File file) throws ObsException { - ModifyObjectRequest request = new ModifyObjectRequest(); - request.setBucketName(bucketName); - request.setObjectKey(objectKey); - request.setPosition(position); - request.setFile(file); - return this.modifyObject(request); - } - - @Override - public ModifyObjectResult modifyObject(final String bucketName, final String objectKey, final long position, InputStream input) throws ObsException{ - ModifyObjectRequest request = new ModifyObjectRequest(); - request.setBucketName(bucketName); - request.setObjectKey(objectKey); - request.setPosition(position); - request.setInput(input); - return this.modifyObject(request); - } - - @Override - public ModifyObjectResult modifyObject(final ModifyObjectRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "ModifyObjectRequest is null"); - ServiceUtils.asserParameterNotNull(request.getPosition(), "position is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - return this.doActionWithResult("modifyObject", request.getBucketName(), new ActionCallbackWithResult() { - - @Override - public ModifyObjectResult action() throws ServiceException { - return ObsClient.this.modifyObjectImpl(request); - } - }); - } - - @Override - public ObsFSBucket newBucket(NewBucketRequest request) throws ObsException { - ObsBucket bucket = this.createBucket(request); - ObsFSBucket fsBucket = new ObsFSBucket(bucket.getBucketName(), bucket.getLocation()); - ReflectUtils.setInnerClient(fsBucket, this); - return fsBucket; - } - - @Override - public ObsFSFile newFile(NewFileRequest request) throws ObsException { - ObsFSFile obsFile = (ObsFSFile)this.putObject(request); - ReflectUtils.setInnerClient(obsFile, this); - return obsFile; - } - - @Override - public ObsFSFolder newFolder(NewFolderRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "CreateFolderRequest is null"); - if(request.getObjectKey() != null) { - String delimiter = this.getFileSystemDelimiter(); - if(!request.getObjectKey().endsWith(delimiter)) { - request.setObjectKey(request.getObjectKey() + delimiter); - } - } - ObsFSFolder obsFolder = (ObsFSFolder)this.putObject(new PutObjectRequest(request)); - ReflectUtils.setInnerClient(obsFolder, this); - return obsFolder; - } - - @Override - public ObsFSAttribute getAttribute(GetAttributeRequest request) throws ObsException { - return (ObsFSAttribute)this.getObjectMetadata(request); - } - - @Override - public ReadFileResult readFile(ReadFileRequest request) throws ObsException { - return (ReadFileResult)this.getObject(request); - } - - @Override - public ObsFSFile writeFile(final WriteFileRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "WriteFileRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - ObsFSFile obsFile = this.doActionWithResult("writeFile", request.getBucketName(), - new ActionCallbackWithResult() { - @Override - public ObsFSFile action() throws ServiceException { - if (null != request.getInput() && null != request.getFile()) { - throw new ServiceException("Both input and file are set, only one is allowed"); - } - return ObsClient.this.writeFileImpl(request); - } - }); - ReflectUtils.setInnerClient(obsFile, this); - return obsFile; - } - - @Override - public ObsFSFile appendFile(WriteFileRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "WriteFileRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - ObjectMetadata metadata = this.getObjectMetadata(new GetObjectMetadataRequest(request.getBucketName(), request.getObjectKey())); - if(request.getPosition() >= 0L && request.getPosition() != metadata.getNextPosition()) { - throw new IllegalArgumentException("Where you proposed append to is not equal to length"); - } - request.setPosition(metadata.getNextPosition()); - return this.writeFile(request); - } - - @Override - public RenameResult renameFile(final RenameRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "RenameRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "ObjectKey is null"); - ServiceUtils.asserParameterNotNull2(request.getNewObjectKey(), "NewObjectKey is null"); - - return this.doActionWithResult("rename", request.getBucketName(), new ActionCallbackWithResult() { - @Override - public RenameResult action() throws ServiceException { - return ObsClient.this.renameFileImpl(request); - } - }); - } - - @Override - public RenameResult renameFolder(RenameRequest request) throws ObsException { - if(request != null && request.getObjectKey() != null && request.getNewObjectKey()!=null) { - String delimiter = this.getFileSystemDelimiter(); - if(!request.getObjectKey().endsWith(delimiter)) { - request.setObjectKey(request.getObjectKey() + delimiter); - } - - if(!request.getNewObjectKey().endsWith(delimiter)) { - request.setNewObjectKey(request.getNewObjectKey() + delimiter); - } - } - return this.renameFile(request); - } - - @Override - public TruncateFileResult truncateFile(final TruncateFileRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "TruncateFileRequest is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "ObjectKey is null"); - return this.doActionWithResult("truncateFile", request.getBucketName(), new ActionCallbackWithResult() { - - @Override - public TruncateFileResult action() throws ServiceException { - return ObsClient.this.truncateFileImpl(request); - } - }); - } - - @Override - public HeaderResponse setBucketFSStatus(final SetBucketFSStatusRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "SetBucketFileInterfaceRequest is null"); - return this.doActionWithResult("setBucketFSStatus", request.getBucketName(), new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.setBucketFSStatusImpl(request); - } - }); - } - - - - @Override - public GetBucketFSStatusResult getBucketFSStatus(final GetBucketFSStatusRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "GetBucketFSStatusRequest is null"); - return this.doActionWithResult("getBucketFSStatus", request.getBucketName(), new ActionCallbackWithResult() { - - @Override - public GetBucketFSStatusResult action() throws ServiceException { - return ObsClient.this.getBucketMetadataImpl(request); - } - }); - } - - @Override - public DropFileResult dropFile(final DropFileRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "DropFileRequest is null"); - return this.doActionWithResult("dropFile", request.getBucketName(), new ActionCallbackWithResult() { - - @Override - public DropFileResult action() throws ServiceException { - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - return (DropFileResult)ObsClient.this.deleteObjectImpl(request.getBucketName(), request.getObjectKey(), request.getVersionId()); + private static Field getFieldFromClass(Class clazz, String key) { + do { + try { + return clazz.getDeclaredField(key); + } catch (NoSuchFieldException e) { + clazz = clazz.getSuperclass(); } - }); + } while (clazz != null); + return null; } - - @Override - public ReadAheadResult readAheadObjects(final ReadAheadRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request, "request is null"); - return this.doActionWithResult("readAheadObjects", request.getBucketName(), new ActionCallbackWithResult() { - @Override - public ReadAheadResult action() throws ServiceException { - return ObsClient.this.readAheadObjectsImpl(request); - - } - }); - } - - @Override - public ReadAheadResult deleteReadAheadObjects(final String bucketName, final String prefix) throws ObsException { - ServiceUtils.asserParameterNotNull(bucketName, "bucketName is null"); - ServiceUtils.asserParameterNotNull(prefix, "prefix is null"); - return this.doActionWithResult("deleteReadAheadObjects", bucketName, new ActionCallbackWithResult() { - @Override - public ReadAheadResult action() throws ServiceException { - return ObsClient.this.deleteReadAheadObjectsImpl(bucketName, prefix); - - } - }); - } - - @Override - public ReadAheadQueryResult queryReadAheadObjectsTask(final String bucketName, final String taskId) throws ObsException { - ServiceUtils.asserParameterNotNull(bucketName, "bucketName is null"); - ServiceUtils.asserParameterNotNull(taskId, "taskId is null"); - return this.doActionWithResult("queryReadAheadObjectsTask", bucketName, new ActionCallbackWithResult() { - @Override - public ReadAheadQueryResult action() throws ServiceException { - return ObsClient.this.queryReadAheadObjectsTaskImpl(bucketName, taskId); - - } - }); - } - - @Override - public HeaderResponse setBucketDirectColdAccess(final String bucketName, final BucketDirectColdAccess access) - throws ObsException { - ServiceUtils.asserParameterNotNull(bucketName, "bucketName is null"); - ServiceUtils.asserParameterNotNull(access, "bucketDirectColdAccess is null"); - return this.doActionWithResult("setBucketDirectColdAccess", bucketName, new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.setBucketDirectColdAccessImpl(bucketName, access); - } - }); - } - - @Override - public BucketDirectColdAccess getBucketDirectColdAccess(final String bucketName) throws ObsException { - ServiceUtils.asserParameterNotNull(bucketName, "bucketName is null"); - return this.doActionWithResult("getBucketDirectColdAccess", bucketName, new ActionCallbackWithResult() { - - @Override - public BucketDirectColdAccess action() throws ServiceException { - return ObsClient.this.getBucketDirectColdAccessImpl(bucketName); - } - }); - } - - @Override - public HeaderResponse deleteBucketDirectColdAccess(final String bucketName) throws ObsException { - ServiceUtils.asserParameterNotNull(bucketName, "bucketName is null"); - return this.doActionWithResult("deleteBucketDirectColdAccess", bucketName, new ActionCallbackWithResult() { - - @Override - public HeaderResponse action() throws ServiceException { - return ObsClient.this.deleteBucketDirectColdAccessImpl(bucketName); - } - }); - } - - @Override - public boolean doesObjectExist(final String buckeName, final String objectKey) throws ObsException { - GetObjectMetadataRequest request = new GetObjectMetadataRequest(buckeName, objectKey); - return this.doesObjectExist(request); - } - - @Override - public boolean doesObjectExist(final GetObjectMetadataRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request.getBucketName(), "bucke is null"); - ServiceUtils.asserParameterNotNull2(request.getObjectKey(), "objectKey is null"); - try { - return ObsClient.this.doesObjectExistImpl(request); - } catch (ServiceException e) { - if(ObsClient.this.isAuthTypeNegotiation() && e.getResponseCode() == 400 && - "Unsupported Authorization Type".equals(e.getErrorMessage()) && - ObsClient.this.getProviderCredentials().getAuthType() == AuthTypeEnum.OBS) { - ObsClient.this.getProviderCredentials().setThreadLocalAuthType(AuthTypeEnum.V2); - return ObsClient.this.doesObjectExistImpl(request); - } else { - throw e; - } - } - } - - private abstract class ActionCallbackWithResult { - - abstract T action() throws ServiceException; - - void authTypeNegotiate(String bucketName) throws ServiceException { - AuthTypeEnum authTypeEnum = ObsClient.this.getApiVersion(bucketName); - ObsClient.this.getProviderCredentials().setThreadLocalAuthType(authTypeEnum); - } - } - - private T doActionWithResult(String action, String bucketName, ActionCallbackWithResult callback) - throws ObsException { - if (! this.isCname()) { - ServiceUtils.asserParameterNotNull(bucketName, "bucketName is null"); - } - InterfaceLogBean reqBean = new InterfaceLogBean(action, this.getEndpoint(), ""); - try { - long start = System.currentTimeMillis(); - if (this.isAuthTypeNegotiation()) { - callback.authTypeNegotiate(bucketName); - } - T ret = callback.action(); - reqBean.setRespTime(new Date()); - reqBean.setResultCode(Constants.RESULTCODE_SUCCESS); - if (ILOG.isInfoEnabled()) { - ILOG.info(reqBean); - } - if (ILOG.isInfoEnabled()) { - ILOG.info("ObsClient [" + action + "] cost " + (System.currentTimeMillis() - start) + " ms"); - } - return ret; - } catch (ServiceException e) { - - ObsException ex = ServiceUtils.changeFromServiceException(e); - if(ex.getResponseCode() >= 400 && ex.getResponseCode() < 500) { - if(ILOG.isWarnEnabled()) { - reqBean.setRespTime(new Date()); - reqBean.setResultCode(String.valueOf(e.getResponseCode())); - ILOG.warn(reqBean); - } - }else if (ILOG.isErrorEnabled()) { - reqBean.setRespTime(new Date()); - reqBean.setResultCode(String.valueOf(ex.getResponseCode())); - ILOG.error(reqBean); - } - throw ex; - } finally { - if(this.isAuthTypeNegotiation()) { - this.getProviderCredentials().removeThreadLocalAuthType(); - } - AccessLoggerUtils.printLog(); - } - } - - - /* (non-Javadoc) - * @see com.obs.services.IObsClient#close() - */ - @Override - public void close() throws IOException { - this.shutdown(); - } - - public String base64Md5(InputStream is, long length, long offset) throws NoSuchAlgorithmException, IOException { - return ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(is, length, offset)); - } - - public String base64Md5(InputStream is) throws NoSuchAlgorithmException, IOException { - return ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(is)); - } - - @Override - protected void finalize() throws Throwable { - super.finalize(); - this.close(); - } - } diff --git a/app/src/main/java/com/obs/services/ObsConfiguration.java b/app/src/main/java/com/obs/services/ObsConfiguration.java index fba9f8c..3ac5b7a 100644 --- a/app/src/main/java/com/obs/services/ObsConfiguration.java +++ b/app/src/main/java/com/obs/services/ObsConfiguration.java @@ -3,14 +3,15 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services; import javax.net.ssl.KeyManagerFactory; @@ -25,51 +26,52 @@ /** * Configuration parameters of ObsClient */ -public class ObsConfiguration implements Cloneable -{ - +public class ObsConfiguration implements Cloneable { + private int connectionTimeout; - + private int idleConnectionTime; - + private int maxIdleConnections; - - private int maxConnections; - + + private int maxConnections; + private int maxErrorRetry; - + private int socketTimeout; - + private String endPoint; - + private int endpointHttpPort; - + private int endpointHttpsPort; - + private boolean httpsOnly; - + private boolean pathStyle; - + private HttpProxyConfiguration httpProxy; - + private int uploadStreamRetryBufferSize; - + private boolean validateCertificate; - + private boolean verifyResponseContentType; - + private int readBufferSize; - + private int writeBufferSize; - + private KeyManagerFactory keyManagerFactory; private TrustManagerFactory trustManagerFactory; private boolean isStrictHostnameVerification; - - private AuthTypeEnum authType; - + + private AuthTypeEnum authType; + + private int localAuthTypeCacheCapacity; + private String signatString; private String defaultBucketLocation; private int bufferSize; @@ -80,22 +82,23 @@ public class ObsConfiguration implements Cloneable private boolean keepAlive; private int connectionRequestTimeout; private boolean authTypeNegotiation; - + private boolean cname; - + private String delimiter; - + private String sslProvider; - + private HttpProtocolTypeEnum httpProtocolType; - + private Dispatcher httpDispatcher; + + private String xmlDocumentBuilderFactoryClass; /** * Constructor */ - public ObsConfiguration() - { + public ObsConfiguration() { this.connectionTimeout = ObsConstraint.HTTP_CONNECT_TIMEOUT_VALUE; this.maxConnections = ObsConstraint.HTTP_MAX_CONNECT_VALUE; this.maxErrorRetry = ObsConstraint.HTTP_RETRY_MAX_VALUE; @@ -113,8 +116,8 @@ public ObsConfiguration() this.socketReadBufferSize = -1; this.readBufferSize = -1; this.writeBufferSize = -1; - this.idleConnectionTime = ObsConstraint.DEFAULT_IDLE_CONNECTION_TIME; - this.maxIdleConnections = ObsConstraint.DEFAULT_MAX_IDLE_CONNECTIONS; + this.idleConnectionTime = ObsConstraint.DEFAULT_IDLE_CONNECTION_TIME; + this.maxIdleConnections = ObsConstraint.DEFAULT_MAX_IDLE_CONNECTIONS; this.authType = AuthTypeEnum.OBS; this.keepAlive = true; this.signatString = ""; @@ -123,617 +126,661 @@ public ObsConfiguration() this.cname = false; this.delimiter = "/"; this.httpProtocolType = HttpProtocolTypeEnum.HTTP1_1; + this.xmlDocumentBuilderFactoryClass = ObsConstraint.OBS_XML_DOC_BUILDER_FACTORY_CLASS; + this.localAuthTypeCacheCapacity = ObsConstraint.DEFAULT_LOCAL_AUTH_TYPE_CACHE_CAPACITY; } - - public String getDelimiter() { - return delimiter; - } - - /** - * Set folder isolators to slashes. - * @param delimiter Folder isolator - */ - public void setDelimiter(String delimiter) { - this.delimiter = delimiter; - } - + + public String getDelimiter() { + return delimiter; + } + + /** + * Set folder isolators to slashes. + * + * @param delimiter + * Folder isolator + */ + public void setDelimiter(String delimiter) { + this.delimiter = delimiter; + } + @Deprecated - public String getSignatString() - { + public String getSignatString() { return signatString; } - + @Deprecated - public void setSignatString(String signatString) - { + public void setSignatString(String signatString) { this.signatString = signatString; } - + @Deprecated - public String getDefaultBucketLocation() - { + public String getDefaultBucketLocation() { return defaultBucketLocation; } - + @Deprecated - public void setDefaultBucketLocation(String defaultBucketLocation) - { + public void setDefaultBucketLocation(String defaultBucketLocation) { this.defaultBucketLocation = defaultBucketLocation; } - + @Deprecated - public int getBufferSize() - { + public int getBufferSize() { return bufferSize; } - + @Deprecated - public void setBufferSize(int bufferSize) - { + public void setBufferSize(int bufferSize) { this.bufferSize = bufferSize; } - + /** - * Check whether path-style access to OBS is enabled. "true" indicates that path-style access is enabled while "false" (default) indicates virtual hosted-style access is enabled. - * Note: If the path-style access is enabled, new bucket features of OBS 3.0 are not supported. + * Check whether path-style access to OBS is enabled. "true" indicates that + * path-style access is enabled while "false" (default) indicates virtual + * hosted-style access is enabled. Note: If the path-style access is + * enabled, new bucket features of OBS 3.0 are not supported. * * @return Whether to enable path-style access to OBS. */ - @Deprecated - public boolean isDisableDnsBucket() - { + public boolean isDisableDnsBucket() { return this.isPathStyle(); } - - /** - * Specify whether to enable path-style access to OBS. "true" indicates that path-style access is enabled while "false" (default) indicates that virtual hosted-style access is enabled. - * Note: If the path-style access is enabled, new bucket features of OBS 3.0 are not supported. + + /** + * Specify whether to enable path-style access to OBS. "true" indicates that + * path-style access is enabled while "false" (default) indicates that + * virtual hosted-style access is enabled. Note: If the path-style access is + * enabled, new bucket features of OBS 3.0 are not supported. * - * @param disableDns Whether to enable path-style access to OBS. + * @param disableDns + * Whether to enable path-style access to OBS. */ - @Deprecated - public void setDisableDnsBucket(boolean disableDns) - { + public void setDisableDnsBucket(boolean disableDns) { this.setPathStyle(disableDns); } - + /** - * Obtain the size (bytes) of the socket receive buffer, corresponding to the "java.net.SocketOptions.SO_RVCBUF" parameter. The default value is "-1", indicating that the size is not set. + * Obtain the size (bytes) of the socket receive buffer, corresponding to + * the "java.net.SocketOptions.SO_RVCBUF" parameter. The default value is + * "-1", indicating that the size is not set. + * * @return Socket receive buffer size */ - public int getSocketReadBufferSize() - { + public int getSocketReadBufferSize() { return socketReadBufferSize; } - + /** - * Set the size (bytes) of the socket receive buffer, corresponding to the "java.net.SocketOptions.SO_RVCBUF" parameter. The default value is "-1", indicating that the size is not set. - * @param socketReadBufferSize Socket receive buffer size + * Set the size (bytes) of the socket receive buffer, corresponding to the + * "java.net.SocketOptions.SO_RVCBUF" parameter. The default value is "-1", + * indicating that the size is not set. + * + * @param socketReadBufferSize + * Socket receive buffer size */ - public void setSocketReadBufferSize(int socketReadBufferSize) - { + public void setSocketReadBufferSize(int socketReadBufferSize) { this.socketReadBufferSize = socketReadBufferSize; } - + /** - * Obtain the size (bytes) of the socket send buffer, corresponding to the "java.net.SocketOptions.SO_SNDBUF" parameter. The default value is "-1", indicating that the size is not set. - * @return socketSocket send buffer size + * Obtain the size (bytes) of the socket send buffer, corresponding to the + * "java.net.SocketOptions.SO_SNDBUF" parameter. The default value is "-1", + * indicating that the size is not set. + * + * @return socketSocket send buffer size */ - public int getSocketWriteBufferSize() - { + public int getSocketWriteBufferSize() { return socketWriteBufferSize; } - + /** - * Set the size (bytes) of the socket send buffer (in bytes), corresponding to the "java.net.SocketOptions.SO_SNDBUF" parameter. The default value is "-1", indicating that the size is not set. - * @param socketWriteBufferSize socket Socket send buffer size + * Set the size (bytes) of the socket send buffer (in bytes), corresponding + * to the "java.net.SocketOptions.SO_SNDBUF" parameter. The default value is + * "-1", indicating that the size is not set. + * + * @param socketWriteBufferSize + * socket Socket send buffer size */ - public void setSocketWriteBufferSize(int socketWriteBufferSize) - { + public void setSocketWriteBufferSize(int socketWriteBufferSize) { this.socketWriteBufferSize = socketWriteBufferSize; } - + @Deprecated - public int getConnectionRequestTimeout() - { + public int getConnectionRequestTimeout() { return connectionRequestTimeout; } @Deprecated - public void setConnectionRequestTimeout(int connectionRequestTimeout) - { + public void setConnectionRequestTimeout(int connectionRequestTimeout) { this.connectionRequestTimeout = connectionRequestTimeout; } - + @Deprecated - public void disableNio() - { + public void disableNio() { this.isNio = false; } - + @Deprecated - public void enableNio() - { + public void enableNio() { this.isNio = true; } - + @Deprecated - public boolean isNio() - { + public boolean isNio() { return this.isNio; } - + @Deprecated public boolean isUseReaper() { return useReaper; } - + @Deprecated public void setUseReaper(boolean useReaper) { this.useReaper = useReaper; } - + /** * Obtain the factory for generating the "KeyManager" array. + * * @return Factory for generating the "KeyManager" array */ - public KeyManagerFactory getKeyManagerFactory() - { + public KeyManagerFactory getKeyManagerFactory() { return keyManagerFactory; } /** * Set the factory for generating the "KeyManager" array. - * @param keyManagerFactory Factory for generating the "KeyManager" array + * + * @param keyManagerFactory + * Factory for generating the "KeyManager" array */ - public void setKeyManagerFactory(KeyManagerFactory keyManagerFactory) - { + public void setKeyManagerFactory(KeyManagerFactory keyManagerFactory) { this.keyManagerFactory = keyManagerFactory; } - + /** * Obtain the factory for generating the "TrustManager" array. + * * @return Factory for generating the "TrustManager" array */ - public TrustManagerFactory getTrustManagerFactory() - { + public TrustManagerFactory getTrustManagerFactory() { return trustManagerFactory; } /** * Set the factory for generating the "TrustManager" array. - * @param trustManagerFactory Factory for generating the "TrustManager" array + * + * @param trustManagerFactory + * Factory for generating the "TrustManager" array */ - public void setTrustManagerFactory(TrustManagerFactory trustManagerFactory) - { + public void setTrustManagerFactory(TrustManagerFactory trustManagerFactory) { this.trustManagerFactory = trustManagerFactory; } - + /** - * Obtain the identifier specifying whether to verify the domain name ("false" by default). + * Obtain the identifier specifying whether to verify the domain name + * ("false" by default). + * * @return Identifier specifying whether to verify the domain name */ - public boolean isStrictHostnameVerification() - { + public boolean isStrictHostnameVerification() { return isStrictHostnameVerification; } /** * Set the identifier specifying whether to verify the domain name. - * @param isStrictHostnameVerification Identifier specifying whether to verify the domain name + * + * @param isStrictHostnameVerification + * Identifier specifying whether to verify the domain name */ - public void setIsStrictHostnameVerification(boolean isStrictHostnameVerification) - { + public void setIsStrictHostnameVerification(boolean isStrictHostnameVerification) { this.isStrictHostnameVerification = isStrictHostnameVerification; } - - + /** - * Check whether path-style access to OBS is enabled. "true" indicates that path-style access is enabled while "false" (default) indicates virtual hosted-style access is enabled. - * Note: If the path-style access is enabled, new bucket features of OBS 3.0 are not supported. + * Check whether path-style access to OBS is enabled. "true" indicates that + * path-style access is enabled while "false" (default) indicates virtual + * hosted-style access is enabled. Note: If the path-style access is + * enabled, new bucket features of OBS 3.0 are not supported. * * @return Whether to enable path-style access to OBS. */ - @Deprecated - public boolean isPathStyle() { - return pathStyle; - } - - /** - * Specify whether to enable path-style access to OBS. "true" indicates that path-style access is enabled while "false" (default) indicates that virtual hosted-style access is enabled. - * Note: If the path-style access is enabled, new bucket features of OBS 3.0 are not supported. + public boolean isPathStyle() { + return pathStyle; + } + + /** + * Specify whether to enable path-style access to OBS. "true" indicates that + * path-style access is enabled while "false" (default) indicates that + * virtual hosted-style access is enabled. Note: If the path-style access is + * enabled, new bucket features of OBS 3.0 are not supported. * - * @param pathStyle Whether to enable path-style access to OBS. + * @param pathStyle + * Whether to enable path-style access to OBS. */ - @Deprecated - public void setPathStyle(boolean pathStyle) { - this.pathStyle = pathStyle; - } - + public void setPathStyle(boolean pathStyle) { + this.pathStyle = pathStyle; + } + /** - * Obtain the timeout interval for establishing HTTP/HTTPS connections (in milliseconds, 60,000 milliseconds by default). + * Obtain the timeout interval for establishing HTTP/HTTPS connections (in + * milliseconds, 60,000 milliseconds by default). * * @return Timeout interval for establishing HTTP/HTTPS connections */ - public int getConnectionTimeout() - { + public int getConnectionTimeout() { return connectionTimeout; } - + /** - * Set the timeout interval for establishing HTTP/HTTPS connections (in milliseconds, 60,000 milliseconds by default). + * Set the timeout interval for establishing HTTP/HTTPS connections (in + * milliseconds, 60,000 milliseconds by default). * - * @param connectionTimeout Timeout interval for establishing HTTP/HTTPS connections + * @param connectionTimeout + * Timeout interval for establishing HTTP/HTTPS connections */ - public void setConnectionTimeout(int connectionTimeout) - { + public void setConnectionTimeout(int connectionTimeout) { this.connectionTimeout = connectionTimeout; } - + /** - * Obtain the maximum number of HTTP connections (1000 by default) that can be opened. + * Obtain the maximum number of HTTP connections (1000 by default) that can + * be opened. * * @return Maximum number of concurrently opened HTTP connections */ - public int getMaxConnections() - { + public int getMaxConnections() { return maxConnections; } - + /** - * Set the maximum number of HTTP connections (1000 by default) that can be opened. + * Set the maximum number of HTTP connections (1000 by default) that can be + * opened. * - * @param maxConnections Maximum number of concurrently opened HTTP connections + * @param maxConnections + * Maximum number of concurrently opened HTTP connections */ - public void setMaxConnections(int maxConnections) - { + public void setMaxConnections(int maxConnections) { this.maxConnections = maxConnections; } - + /** - * Obtain the maximum number of retry attempts (5 by default) upon a request failure (request exception, or error 500 or 503 on the server). + * Obtain the maximum number of retry attempts (5 by default) upon a request + * failure (request exception, or error 500 or 503 on the server). * * @return Maximum number of retry attempts upon a request failure */ - public int getMaxErrorRetry() - { + public int getMaxErrorRetry() { return maxErrorRetry; } - + /** - * Set the maximum number of retry attempts (5 by default) upon a request failure (request exception, or error 500 or 503 on the server). + * Set the maximum number of retry attempts (5 by default) upon a request + * failure (request exception, or error 500 or 503 on the server). * - * @param maxErrorRetry Maximum number of retry attempts upon a request failure + * @param maxErrorRetry + * Maximum number of retry attempts upon a request failure */ - public void setMaxErrorRetry(int maxErrorRetry) - { + public void setMaxErrorRetry(int maxErrorRetry) { this.maxErrorRetry = maxErrorRetry; } - + /** - * Obtain the timeout interval for data transmission at the socket layer (in milliseconds, 60,000 milliseconds by default). + * Obtain the timeout interval for data transmission at the socket layer (in + * milliseconds, 60,000 milliseconds by default). * * @return Timeout interval for data transmission at the socket layer */ - public int getSocketTimeout() - { + public int getSocketTimeout() { return socketTimeout; } - + /** - * Set the timeout interval for data transmission at the socket layer (in milliseconds, 60,000 milliseconds by default). + * Set the timeout interval for data transmission at the socket layer (in + * milliseconds, 60,000 milliseconds by default). * - * @param socketTimeout Timeout interval for data transmission at the socket layer + * @param socketTimeout + * Timeout interval for data transmission at the socket layer */ - public void setSocketTimeout(int socketTimeout) - { + public void setSocketTimeout(int socketTimeout) { this.socketTimeout = socketTimeout; } - + /** * Obtain the endpoint for accessing OBS. * * @return OBS endpoint */ - public String getEndPoint() - { - if(endPoint == null || endPoint.trim().equals("")){ + public String getEndPoint() { + if (endPoint == null || endPoint.trim().equals("")) { throw new IllegalArgumentException("EndPoint is not set"); } return endPoint.trim(); } - + /** * Set the endpoint for accessing OBS. * - * @param endPoint OBS endpoint + * @param endPoint + * OBS endpoint */ - public void setEndPoint(String endPoint) - { + public void setEndPoint(String endPoint) { this.endPoint = endPoint; } - + /** * Obtain the port number of the HTTP request (80 by default). * * @return Port number of the HTTP request */ - @Deprecated - public int getEndpointHttpPort() - { + public int getEndpointHttpPort() { return endpointHttpPort; } - + /** * Set the port number of the HTTP request (80 by default). * - * @param endpointHttpPort Port number of the HTTP request + * @param endpointHttpPort + * Port number of the HTTP request */ - @Deprecated - public void setEndpointHttpPort(int endpointHttpPort) - { + public void setEndpointHttpPort(int endpointHttpPort) { this.endpointHttpPort = endpointHttpPort; } - + /** * Obtain the port number of the HTTPS request (443 by default). * * @return Port number of the HTTPS request */ - @Deprecated - public int getEndpointHttpsPort() - { + public int getEndpointHttpsPort() { return endpointHttpsPort; } - + /** * Set the port number of the HTTPS request (443 by default). * - * @param endpointHttpsPort Port number of the HTTPS request + * @param endpointHttpsPort + * Port number of the HTTPS request */ - @Deprecated - public void setEndpointHttpsPort(int endpointHttpsPort) - { + public void setEndpointHttpsPort(int endpointHttpsPort) { this.endpointHttpsPort = endpointHttpsPort; } - + /** * Specify whether to use HTTPS to connect OBS ("true" by default). * - * @param httpsOnly Identifier specifying whether to use HTTPS to connect OBS + * @param httpsOnly + * Identifier specifying whether to use HTTPS to connect OBS */ - @Deprecated - public void setHttpsOnly(boolean httpsOnly) - { + public void setHttpsOnly(boolean httpsOnly) { this.httpsOnly = httpsOnly; } - + /** * Check whether HTTPS is used to connect OBS ("true" by default). * * @return Identifier specifying whether to use HTTPS to connect OBS */ - @Deprecated - public boolean isHttpsOnly() - { + public boolean isHttpsOnly() { return httpsOnly; } - + @Override - protected Object clone() - throws CloneNotSupportedException - { + public Object clone() throws CloneNotSupportedException { return super.clone(); } - + /** * Obtain the proxy configuration. * * @return Proxy configuration */ - public HttpProxyConfiguration getHttpProxy() - { + public HttpProxyConfiguration getHttpProxy() { return httpProxy; } - + /** * Configure the proxy. * - * @param httpProxy HTTP proxy configuration + * @param httpProxy + * HTTP proxy configuration */ - public void setHttpProxy(HttpProxyConfiguration httpProxy) - { + public void setHttpProxy(HttpProxyConfiguration httpProxy) { this.httpProxy = httpProxy; } - + /** * Configure the proxy server. * - * @param proxyAddr Proxy server address - * @param proxyPort Proxy server port - * @param userName Proxy username - * @param password Proxy password - * @param domain Proxy domain + * @param proxyAddr + * Proxy server address + * @param proxyPort + * Proxy server port + * @param userName + * Proxy username + * @param password + * Proxy password + * @param domain + * Proxy domain */ @Deprecated - public void setHttpProxy(String proxyAddr, int proxyPort, String userName, String password, String domain) - { + public void setHttpProxy(String proxyAddr, int proxyPort, String userName, String password, String domain) { this.httpProxy = new HttpProxyConfiguration(proxyAddr, proxyPort, userName, password, domain); } - + /** * Configure the proxy server. * - * @param proxyAddr Proxy server address - * @param proxyPort Proxy server port - * @param userName Proxy username - * @param password Proxy password - */ - public void setHttpProxy(String proxyAddr, int proxyPort, String userName, String password) - { + * @param proxyAddr + * Proxy server address + * @param proxyPort + * Proxy server port + * @param userName + * Proxy username + * @param password + * Proxy password + */ + public void setHttpProxy(String proxyAddr, int proxyPort, String userName, String password) { this.httpProxy = new HttpProxyConfiguration(proxyAddr, proxyPort, userName, password, null); } - + /** - * Set the buffer size used for uploading stream objects (in bytes). The default value is 512 KB. - * @param uploadStreamRetryBufferSize Buffer size used for uploading stream objects + * Set the buffer size used for uploading stream objects (in bytes). The + * default value is 512 KB. + * + * @param uploadStreamRetryBufferSize + * Buffer size used for uploading stream objects */ @Deprecated - public void setUploadStreamRetryBufferSize(int uploadStreamRetryBufferSize) - { + public void setUploadStreamRetryBufferSize(int uploadStreamRetryBufferSize) { this.uploadStreamRetryBufferSize = uploadStreamRetryBufferSize; } - + /** - * Obtain the buffer size used for uploading stream objects (in bytes). The default value is 512 KB. + * Obtain the buffer size used for uploading stream objects (in bytes). The + * default value is 512 KB. + * * @return Buffer size used for uploading stream objects */ @Deprecated - public int getUploadStreamRetryBufferSize() - { + public int getUploadStreamRetryBufferSize() { return this.uploadStreamRetryBufferSize; } - + /** - * Check whether server-side verification is enabled. The default value is "false". + * Check whether server-side verification is enabled. The default value is + * "false". + * * @return Identifier specifying whether to enable server-side verification */ - public boolean isValidateCertificate() - { + public boolean isValidateCertificate() { return validateCertificate; } - + /** - * Specify whether to enable server-side certificate verification. The default value is "false". - ** @param validateCertificate Identifier specifying whether to enable server-side verification + * Specify whether to enable server-side certificate verification. The + * default value is "false". + ** + * @param validateCertificate + * Identifier specifying whether to enable server-side + * verification */ - public void setValidateCertificate(boolean validateCertificate) - { + public void setValidateCertificate(boolean validateCertificate) { this.validateCertificate = validateCertificate; } - + /** - * Check whether "ContentType" in the response is verified. The default value is "true". - * @return Identifier specifying whether to verify "ContentType" in the response + * Check whether "ContentType" in the response is verified. The default + * value is "true". + * + * @return Identifier specifying whether to verify "ContentType" in the + * response */ - public boolean isVerifyResponseContentType() - { + public boolean isVerifyResponseContentType() { return verifyResponseContentType; } - + /** - * Specify whether to verify "ContentType" in the response. The default value is "true". - * @param verifyResponseContentType Identifier specifying whether to verify "ContentType" in the response + * Specify whether to verify "ContentType" in the response. The default + * value is "true". + * + * @param verifyResponseContentType + * Identifier specifying whether to verify "ContentType" in the + * response */ - public void setVerifyResponseContentType(boolean verifyResponseContentType) - { + public void setVerifyResponseContentType(boolean verifyResponseContentType) { this.verifyResponseContentType = verifyResponseContentType; } - + /** - * Obtain the read cache size used for uploading objects to socket streams (in bytes). The default value is 8192. + * Obtain the read cache size used for uploading objects to socket streams + * (in bytes). The default value is 8192. + * * @return Read cache size used for uploading objects to socket streams */ - public int getReadBufferSize() - { + public int getReadBufferSize() { return readBufferSize; } - + /** - * Set the read cache size used for uploading objects to socket streams (in bytes). The default value is 8192. - * @param readBufferSize Read cache size used for uploading objects to socket streams + * Set the read cache size used for uploading objects to socket streams (in + * bytes). The default value is 8192. + * + * @param readBufferSize + * Read cache size used for uploading objects to socket streams */ - public void setReadBufferSize(int readBufferSize) - { + public void setReadBufferSize(int readBufferSize) { this.readBufferSize = readBufferSize; } - + /** - * Obtain the write cache size used for uploading objects to socket streams (in bytes). The default value is 8192. + * Obtain the write cache size used for uploading objects to socket streams + * (in bytes). The default value is 8192. + * * @return Write cache size used for uploading objects to socket streams */ - public int getWriteBufferSize() - { + public int getWriteBufferSize() { return writeBufferSize; } - + /** - * Set the write cache size used for uploading objects to socket streams (in bytes). The default value is 8192. - * @param writeBufferSize Write cache size used for uploading objects to socket streams + * Set the write cache size used for uploading objects to socket streams (in + * bytes). The default value is 8192. + * + * @param writeBufferSize + * Write cache size used for uploading objects to socket streams */ - public void setWriteBufferSize(int writeBufferSize) - { + public void setWriteBufferSize(int writeBufferSize) { this.writeBufferSize = writeBufferSize; } - + /** - * Obtain the idle time for obtaining connections from the connection pool. The unit is millisecond and the default value is 30000. - * @return Maximum idle time for obtaining connections from the connection pool + * Obtain the idle time for obtaining connections from the connection pool. + * The unit is millisecond and the default value is 30000. + * + * @return Maximum idle time for obtaining connections from the connection + * pool */ - public int getIdleConnectionTime() - { + public int getIdleConnectionTime() { return idleConnectionTime; } /** - * Set the idle time for obtaining connections from the connection pool. The unit is millisecond and the default value is 30000. - * @param idleConnectionTime Maximum idle time for obtaining connections from the connection pool + * Set the idle time for obtaining connections from the connection pool. The + * unit is millisecond and the default value is 30000. + * + * @param idleConnectionTime + * Maximum idle time for obtaining connections from the + * connection pool */ - public void setIdleConnectionTime(int idleConnectionTime) - { + public void setIdleConnectionTime(int idleConnectionTime) { this.idleConnectionTime = idleConnectionTime; } - + /** - * Obtain the maximum number of idle connections in the connection pool. The default value is 1000. + * Obtain the maximum number of idle connections in the connection pool. The + * default value is 1000. + * * @return Maximum number of idle connections in the connection pool */ - public int getMaxIdleConnections() { - return maxIdleConnections; - } - - /** - * Set the maximum number of idle connections in the connection pool. The default value is 1000. - * @param maxIdleConnections Maximum number of idle connections in the connection pool - */ - public void setMaxIdleConnections(int maxIdleConnections) { - this.maxIdleConnections = maxIdleConnections; - } - + public int getMaxIdleConnections() { + return maxIdleConnections; + } + + /** + * Set the maximum number of idle connections in the connection pool. The + * default value is 1000. + * + * @param maxIdleConnections + * Maximum number of idle connections in the connection pool + */ + public void setMaxIdleConnections(int maxIdleConnections) { + this.maxIdleConnections = maxIdleConnections; + } + /** * Obtain the authentication type. + * * @return Authentication type */ - public AuthTypeEnum getAuthType() { - return authType; - } + public AuthTypeEnum getAuthType() { + return authType; + } - /** - * Set the authentication type. - * @param authType Authentication type - */ - public void setAuthType(AuthTypeEnum authType) { - this.authType = authType; - } + /** + * Set the authentication type. + * + * @param authType + * Authentication type + */ + public void setAuthType(AuthTypeEnum authType) { + this.authType = authType; + } /** * Specify whether to use persistent connections. + * * @return Identifier specifying whether to use the persistent connections */ - public boolean isKeepAlive() { - return keepAlive; - } - - /** - * Check whether persistent connections are used. - * @param keepAlive Identifier specifying whether to use the persistent connections - */ - public void setKeepAlive(boolean keepAlive) { - this.keepAlive = keepAlive; - } + public boolean isKeepAlive() { + return keepAlive; + } + + /** + * Check whether persistent connections are used. + * + * @param keepAlive + * Identifier specifying whether to use the persistent + * connections + */ + public void setKeepAlive(boolean keepAlive) { + this.keepAlive = keepAlive; + } /** * Check whether protocol negotiation is used. + * * @return Identifier specifying whether to use protocol negotiation */ public boolean isAuthTypeNegotiation() { @@ -741,8 +788,10 @@ public boolean isAuthTypeNegotiation() { } /** - * Specify whether to use protocol negotiation. - * @param authTypeNegotiation Identifier specifying whether to use protocol negotiation + * Specify whether to use protocol negotiation. + * + * @param authTypeNegotiation + * Identifier specifying whether to use protocol negotiation */ public void setAuthTypeNegotiation(boolean authTypeNegotiation) { this.authTypeNegotiation = authTypeNegotiation; @@ -750,66 +799,94 @@ public void setAuthTypeNegotiation(boolean authTypeNegotiation) { /** * Check whether user-defined domain name is used. + * * @return Identifier specifying whether to use user-defined domain name */ - public boolean isCname() { - return cname; - } - - /** - * Specify whether to use user-defined domain name. - * @param cname Identifier specifying whether to use user-defined domain name - */ - public void setCname(boolean cname) { - this.cname = cname; - } - - /** - * Set the provider of SSLContext. - * @return SSLContext provider - */ - public String getSslProvider() { - return sslProvider; - } - - /** - * Obtain the provider of SSLContext. - * @param sslProvider SSLContext provider - */ - public void setSslProvider(String sslProvider) { - this.sslProvider = sslProvider; - } - - /** - * Set the HTTP type used for accessing OBS servers. - * @return HTTP type - */ - public HttpProtocolTypeEnum getHttpProtocolType() { - return httpProtocolType; - } - - /** - * Obtain the HTTP type used for accessing OBS servers. - * @param httpProtocolType HTTP type - */ - public void setHttpProtocolType(HttpProtocolTypeEnum httpProtocolType) { - this.httpProtocolType = httpProtocolType; - } - - /** - * Set the customized dispatcher. - * @return Customized dispatcher - */ - public Dispatcher getHttpDispatcher() { - return httpDispatcher; - } - - /** - * Obtain the customized dispatcher. - * @param httpDispatcher Customized dispatcher - */ - public void setHttpDispatcher(Dispatcher httpDispatcher) { - this.httpDispatcher = httpDispatcher; - } - + public boolean isCname() { + return cname; + } + + /** + * Specify whether to use user-defined domain name. + * + * @param cname + * Identifier specifying whether to use user-defined domain name + */ + public void setCname(boolean cname) { + this.cname = cname; + } + + /** + * Set the provider of SSLContext. + * + * @return SSLContext provider + */ + public String getSslProvider() { + return sslProvider; + } + + /** + * Obtain the provider of SSLContext. + * + * @param sslProvider + * SSLContext provider + */ + public void setSslProvider(String sslProvider) { + this.sslProvider = sslProvider; + } + + /** + * Set the HTTP type used for accessing OBS servers. + * + * @return HTTP type + */ + public HttpProtocolTypeEnum getHttpProtocolType() { + return httpProtocolType; + } + + /** + * Obtain the HTTP type used for accessing OBS servers. + * + * @param httpProtocolType + * HTTP type + */ + public void setHttpProtocolType(HttpProtocolTypeEnum httpProtocolType) { + this.httpProtocolType = httpProtocolType; + } + + /** + * Set the customized dispatcher. + * + * @return Customized dispatcher + */ + public Dispatcher getHttpDispatcher() { + return httpDispatcher; + } + + /** + * Obtain the customized dispatcher. + * + * @param httpDispatcher + * Customized dispatcher + */ + public void setHttpDispatcher(Dispatcher httpDispatcher) { + this.httpDispatcher = httpDispatcher; + } + + public String getXmlDocumentBuilderFactoryClass() { + return xmlDocumentBuilderFactoryClass; + } + + public void setXmlDocumentBuilderFactoryClass(String xmlDocumentBuilderFactoryClass) { + this.xmlDocumentBuilderFactoryClass = xmlDocumentBuilderFactoryClass; + } + + + public int getLocalAuthTypeCacheCapacity() { + return localAuthTypeCacheCapacity; + } + + public void setLocalAuthTypeCacheCapacity(int localAuthTypeCacheCapacity) { + this.localAuthTypeCacheCapacity = localAuthTypeCacheCapacity; + } } diff --git a/app/src/main/java/com/obs/services/SecretFlexibleBucketObsClient.java b/app/src/main/java/com/obs/services/SecretFlexibleBucketObsClient.java new file mode 100644 index 0000000..9a47579 --- /dev/null +++ b/app/src/main/java/com/obs/services/SecretFlexibleBucketObsClient.java @@ -0,0 +1,1209 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services; + +import java.util.List; + +import com.obs.services.exception.ObsException; +import com.obs.services.internal.security.ProviderCredentialThreadContext; +import com.obs.services.internal.security.ProviderCredentials; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.BucketCors; +import com.obs.services.model.BucketLocationResponse; +import com.obs.services.model.BucketLoggingConfiguration; +import com.obs.services.model.BucketMetadataInfoRequest; +import com.obs.services.model.BucketMetadataInfoResult; +import com.obs.services.model.BucketNotificationConfiguration; +import com.obs.services.model.BucketPolicyResponse; +import com.obs.services.model.BucketQuota; +import com.obs.services.model.BucketStorageInfo; +import com.obs.services.model.BucketStoragePolicyConfiguration; +import com.obs.services.model.BucketTagInfo; +import com.obs.services.model.BucketVersioningConfiguration; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.LifecycleConfiguration; +import com.obs.services.model.ListBucketsRequest; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.OptionsInfoRequest; +import com.obs.services.model.OptionsInfoResult; +import com.obs.services.model.ReplicationConfiguration; +import com.obs.services.model.WebsiteConfiguration; + +public abstract class SecretFlexibleBucketObsClient extends ObsClient { + /** + * Constructor + * + * @param config + * Configuration parameters of ObsClient + */ + public SecretFlexibleBucketObsClient(ObsConfiguration config) { + this("", "", config); + } + + /** + * Constructor + * + * @param endPoint + * OBS endpoint + */ + public SecretFlexibleBucketObsClient(String endPoint) { + this("", "", endPoint); + } + + /** + * Constructor + * + * @param accessKey + * AK in the access key + * @param secretKey + * SK in the access key + * @param config + * Configuration parameters of ObsClient + */ + public SecretFlexibleBucketObsClient(String accessKey, String secretKey, ObsConfiguration config) { + super(accessKey, secretKey, config); + } + + /** + * Constructor + * + * @param accessKey + * AK in the access key + * @param secretKey + * SK in the access key + * @param endPoint + * OBS endpoint + */ + public SecretFlexibleBucketObsClient(String accessKey, String secretKey, String endPoint) { + super(accessKey, secretKey, endPoint); + } + + /** + * Constructor + * + * @param accessKey + * AK in the temporary access key + * @param secretKey + * SK in the temporary access key + * @param securityToken + * Security token + * @param config + * Configuration parameters of ObsClient + */ + public SecretFlexibleBucketObsClient(String accessKey, String secretKey, + String securityToken, ObsConfiguration config) { + super(accessKey, secretKey, securityToken, config); + } + + /** + * Constructor + * + * @param accessKey + * AK in the temporary access key + * @param secretKey + * SK in the temporary access key + * @param securityToken + * Security token + * @param endPoint + * OBS endpoint + */ + public SecretFlexibleBucketObsClient(String accessKey, String secretKey, String securityToken, String endPoint) { + super(accessKey, secretKey, securityToken, endPoint); + } + + public ObsBucket createBucket(ObsBucket bucket, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucket.getBucketName(), accessKey, secretKey); + try { + return super.createBucket(bucket); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ObsBucket createBucket(ObsBucket bucket, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(bucket.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.createBucket(bucket); + } finally { + this.clearContextProviderCredentials(); + } + } + + public List listBuckets(String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials("", accessKey, secretKey); + try { + return super.listBuckets(null); + } finally { + this.clearContextProviderCredentials(); + } + } + + public List listBuckets(String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials("", accessKey, secretKey, securityToken); + try { + return super.listBuckets(null); + } finally { + this.clearContextProviderCredentials(); + } + } + + public List listBuckets(ListBucketsRequest request, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials("", accessKey, secretKey); + try { + return super.listBuckets(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public List listBuckets(ListBucketsRequest request, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials("", accessKey, secretKey, securityToken); + try { + return super.listBuckets(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucket(String bucketName, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteBucket(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucket(String bucketName, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.deleteBucket(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public boolean headBucket(String bucketName, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.headBucket(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public boolean headBucket(String bucketName, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.headBucket(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketMetadataInfoResult getBucketMetadata(BucketMetadataInfoRequest bucketMetadataInfoRequest, + String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketMetadataInfoRequest.getBucketName(), accessKey, secretKey); + try { + return super.getBucketMetadata(bucketMetadataInfoRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketMetadataInfoResult getBucketMetadata(BucketMetadataInfoRequest bucketMetadataInfoRequest, + String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketMetadataInfoRequest.getBucketName(), + accessKey, secretKey, securityToken); + try { + return super.getBucketMetadata(bucketMetadataInfoRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + public AccessControlList getBucketAcl(String bucketName, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketAcl(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public AccessControlList getBucketAcl(String bucketName, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketAcl(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketAcl(String bucketName, String cannedACL, AccessControlList acl, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketAcl(bucketName, cannedACL, acl); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketAcl(String bucketName, String cannedACL, AccessControlList acl, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketAcl(bucketName, cannedACL, acl); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketAcl(String bucketName, AccessControlList acl, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketAcl(bucketName, acl); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketAcl(String bucketName, AccessControlList acl, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketAcl(bucketName, acl); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketLocationResponse getBucketLocation(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketLocationV2(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketLocationResponse getBucketLocation(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketLocationV2(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketStorageInfo getBucketStorageInfo(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketStorageInfo(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketStorageInfo getBucketStorageInfo(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketStorageInfo(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketQuota getBucketQuota(String bucketName, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketQuota(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketQuota getBucketQuota(String bucketName, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketQuota(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketQuota(String bucketName, BucketQuota bucketQuota, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketQuota(bucketName, bucketQuota); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketQuota(String bucketName, BucketQuota bucketQuota, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketQuota(bucketName, bucketQuota); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketCors(String bucketName, BucketCors bucketCors, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketCors(bucketName, bucketCors); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketCors(String bucketName, BucketCors bucketCors, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketCors(bucketName, bucketCors); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketCors getBucketCors(String bucketName, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketCors(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketCors getBucketCors(String bucketName, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketCors(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketCors(String bucketName, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteBucketCors(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketCors(String bucketName, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.deleteBucketCors(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public OptionsInfoResult optionsBucket(String bucketName, OptionsInfoRequest optionInfo, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.optionsBucket(bucketName, optionInfo); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public OptionsInfoResult optionsBucket(String bucketName, OptionsInfoRequest optionInfo, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.optionsBucket(bucketName, optionInfo); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public BucketLoggingConfiguration getBucketLoggingConfiguration(String bucketName, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketLoggingConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketLoggingConfiguration getBucketLogging(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketLogging(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public BucketLoggingConfiguration getBucketLoggingConfiguration(String bucketName, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketLoggingConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketLoggingConfiguration getBucketLogging(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketLogging(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketLoggingConfiguration(String bucketName, + BucketLoggingConfiguration loggingConfiguration, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketLoggingConfiguration(bucketName, loggingConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketLogging(String bucketName, BucketLoggingConfiguration loggingConfiguration, + String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketLogging(bucketName, loggingConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketLoggingConfiguration(String bucketName, + BucketLoggingConfiguration loggingConfiguration, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketLoggingConfiguration(bucketName, loggingConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketLogging(String bucketName, BucketLoggingConfiguration loggingConfiguration, + String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketLogging(bucketName, loggingConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketLoggingConfiguration(String bucketName, + BucketLoggingConfiguration loggingConfiguration, boolean updateTargetACLifRequired, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketLoggingConfiguration(bucketName, loggingConfiguration, updateTargetACLifRequired); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketLoggingConfiguration(String bucketName, + BucketLoggingConfiguration loggingConfiguration, boolean updateTargetACLifRequired, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketLoggingConfiguration(bucketName, loggingConfiguration, updateTargetACLifRequired); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfiguration versioningConfiguration, + String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketVersioning(bucketName, versioningConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfiguration versioningConfiguration, + String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketVersioning(bucketName, versioningConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketVersioningConfiguration getBucketVersioning(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketVersioning(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketVersioningConfiguration getBucketVersioning(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketVersioning(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public LifecycleConfiguration getBucketLifecycleConfiguration(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketLifecycleConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public LifecycleConfiguration getBucketLifecycle(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketLifecycle(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public LifecycleConfiguration getBucketLifecycleConfiguration(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketLifecycleConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public LifecycleConfiguration getBucketLifecycle(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketLifecycle(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketLifecycleConfiguration(String bucketName, LifecycleConfiguration lifecycleConfig, + String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketLifecycleConfiguration(bucketName, lifecycleConfig); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketLifecycle(String bucketName, LifecycleConfiguration lifecycleConfig, + String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketLifecycle(bucketName, lifecycleConfig); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketLifecycleConfiguration(String bucketName, LifecycleConfiguration lifecycleConfig, + String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketLifecycleConfiguration(bucketName, lifecycleConfig); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketLifecycle(String bucketName, LifecycleConfiguration lifecycleConfig, + String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketLifecycle(bucketName, lifecycleConfig); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse deleteBucketLifecycleConfiguration(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteBucketLifecycleConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketLifecycle(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteBucketLifecycle(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse deleteBucketLifecycleConfiguration(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.deleteBucketLifecycleConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketLifecycle(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.deleteBucketLifecycle(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketPolicyResponse getBucketPolicy(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketPolicyV2(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketPolicyResponse getBucketPolicy(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketPolicyV2(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketPolicy(String bucketName, String policy, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketPolicy(bucketName, policy); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketPolicy(String bucketName, String policy, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketPolicy(bucketName, policy); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketPolicy(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteBucketPolicy(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketPolicy(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.deleteBucketPolicy(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public WebsiteConfiguration getBucketWebsiteConfiguration(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketWebsiteConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public WebsiteConfiguration getBucketWebsite(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketWebsite(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public WebsiteConfiguration getBucketWebsiteConfiguration(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketWebsiteConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public WebsiteConfiguration getBucketWebsite(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketWebsite(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketWebsiteConfiguration(String bucketName, WebsiteConfiguration websiteConfig, + String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketWebsiteConfiguration(bucketName, websiteConfig); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketWebsite(String bucketName, WebsiteConfiguration websiteConfig, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketWebsite(bucketName, websiteConfig); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketWebsiteConfiguration(String bucketName, WebsiteConfiguration websiteConfig, + String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketWebsiteConfiguration(bucketName, websiteConfig); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketWebsite(String bucketName, WebsiteConfiguration websiteConfig, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketWebsite(bucketName, websiteConfig); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse deleteBucketWebsiteConfiguration(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteBucketWebsiteConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketWebsite(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteBucketWebsite(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse deleteBucketWebsiteConfiguration(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.deleteBucketWebsiteConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketWebsite(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.deleteBucketWebsite(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketTagging(String bucketName, BucketTagInfo bucketTagInfo, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketTagging(bucketName, bucketTagInfo); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketTagging(String bucketName, BucketTagInfo bucketTagInfo, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketTagging(bucketName, bucketTagInfo); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketTagInfo getBucketTagging(String bucketName, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketTagging(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketTagInfo getBucketTagging(String bucketName, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketTagging(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketTagging(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteBucketTagging(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketTagging(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.deleteBucketTagging(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketReplicationConfiguration(String bucketName, + ReplicationConfiguration replicationConfiguration, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketReplicationConfiguration(bucketName, replicationConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketReplication(String bucketName, ReplicationConfiguration replicationConfiguration, + String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketReplication(bucketName, replicationConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setBucketReplicationConfiguration(String bucketName, + ReplicationConfiguration replicationConfiguration, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketReplicationConfiguration(bucketName, replicationConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketReplication(String bucketName, ReplicationConfiguration replicationConfiguration, + String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketReplication(bucketName, replicationConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public ReplicationConfiguration getBucketReplicationConfiguration(String bucketName, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketReplicationConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ReplicationConfiguration getBucketReplication(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketReplication(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public ReplicationConfiguration getBucketReplicationConfiguration(String bucketName, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketReplicationConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ReplicationConfiguration getBucketReplication(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketReplication(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse deleteBucketReplicationConfiguration(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteBucketReplicationConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketReplication(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteBucketReplication(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse deleteBucketReplicationConfiguration(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.deleteBucketReplicationConfiguration(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteBucketReplication(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.deleteBucketReplication(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketNotificationConfiguration getBucketNotification(String bucketName, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketNotification(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketNotificationConfiguration getBucketNotification(String bucketName, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketNotification(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketNotification(String bucketName, + BucketNotificationConfiguration bucketNotificationConfiguration, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketNotification(bucketName, bucketNotificationConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketNotification(String bucketName, + BucketNotificationConfiguration bucketNotificationConfiguration, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketNotification(bucketName, bucketNotificationConfiguration); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketStoragePolicy(final String bucketName, + final BucketStoragePolicyConfiguration bucketStorage, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setBucketStoragePolicy(bucketName, bucketStorage); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setBucketStoragePolicy(final String bucketName, + final BucketStoragePolicyConfiguration bucketStorage, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setBucketStoragePolicy(bucketName, bucketStorage); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketStoragePolicyConfiguration getBucketStoragePolicy(final String bucketName, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getBucketStoragePolicy(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public BucketStoragePolicyConfiguration getBucketStoragePolicy(final String bucketName, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getBucketStoragePolicy(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + protected void setContextProviderCredentials(String bucketName, + String accessKey, String secretKey, String securityToken) { + ProviderCredentials providerCredentials = new ProviderCredentials(accessKey, secretKey, securityToken); + providerCredentials.setIsAuthTypeNegotiation(this.getProviderCredentials().getIsAuthTypeNegotiation()); + providerCredentials.setAuthType(this.getProviderCredentials().getLocalAuthType(bucketName)); + ProviderCredentialThreadContext.getInstance().setProviderCredentials(providerCredentials); + } + + protected void setContextProviderCredentials(String bucketName, String accessKey, String secretKey) { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, null); + } + + protected void clearContextProviderCredentials() { + ProviderCredentialThreadContext.getInstance().clearProviderCredentials(); + } +} diff --git a/app/src/main/java/com/obs/services/SecretFlexibleObsClient.java b/app/src/main/java/com/obs/services/SecretFlexibleObsClient.java index 12df105..1b6c647 100644 --- a/app/src/main/java/com/obs/services/SecretFlexibleObsClient.java +++ b/app/src/main/java/com/obs/services/SecretFlexibleObsClient.java @@ -11,27 +11,12 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services; -import java.util.List; +package com.obs.services; import com.obs.services.exception.ObsException; -import com.obs.services.internal.security.ProviderCredentialThreadContext; -import com.obs.services.internal.security.ProviderCredentials; import com.obs.services.model.AbortMultipartUploadRequest; import com.obs.services.model.AccessControlList; -import com.obs.services.model.BucketCors; -import com.obs.services.model.BucketLocationResponse; -import com.obs.services.model.BucketLoggingConfiguration; -import com.obs.services.model.BucketMetadataInfoRequest; -import com.obs.services.model.BucketMetadataInfoResult; -import com.obs.services.model.BucketNotificationConfiguration; -import com.obs.services.model.BucketPolicyResponse; -import com.obs.services.model.BucketQuota; -import com.obs.services.model.BucketStorageInfo; -import com.obs.services.model.BucketStoragePolicyConfiguration; -import com.obs.services.model.BucketTagInfo; -import com.obs.services.model.BucketVersioningConfiguration; import com.obs.services.model.CompleteMultipartUploadRequest; import com.obs.services.model.CompleteMultipartUploadResult; import com.obs.services.model.CopyObjectRequest; @@ -45,8 +30,6 @@ import com.obs.services.model.HeaderResponse; import com.obs.services.model.InitiateMultipartUploadRequest; import com.obs.services.model.InitiateMultipartUploadResult; -import com.obs.services.model.LifecycleConfiguration; -import com.obs.services.model.ListBucketsRequest; import com.obs.services.model.ListMultipartUploadsRequest; import com.obs.services.model.ListObjectsRequest; import com.obs.services.model.ListPartsRequest; @@ -56,1682 +39,597 @@ import com.obs.services.model.MultipartUploadListing; import com.obs.services.model.ObjectListing; import com.obs.services.model.ObjectMetadata; -import com.obs.services.model.ObsBucket; import com.obs.services.model.ObsObject; import com.obs.services.model.OptionsInfoRequest; import com.obs.services.model.OptionsInfoResult; import com.obs.services.model.PutObjectRequest; import com.obs.services.model.PutObjectResult; -import com.obs.services.model.ReplicationConfiguration; import com.obs.services.model.RestoreObjectRequest; import com.obs.services.model.RestoreObjectRequest.RestoreObjectStatus; import com.obs.services.model.UploadPartRequest; import com.obs.services.model.UploadPartResult; -import com.obs.services.model.WebsiteConfiguration; /** - * ObsClient that supports transparent transfer of AK/SK, inherited from {@link com.obs.services.ObsClient} + * ObsClient that supports transparent transfer of AK/SK, inherited from + * {@link com.obs.services.ObsClient} * */ -public class SecretFlexibleObsClient extends ObsClient { - - /** - * Constructor - * - * @param config - * Configuration parameters of ObsClient - */ - public SecretFlexibleObsClient(ObsConfiguration config) { - this(null, null, config); - } - - /** - * Constructor - * - * @param endPoint - * OBS endpoint - */ - public SecretFlexibleObsClient(String endPoint) { - this(null, null, endPoint); - } - - /** - * Constructor - * - * @param accessKey - * AK in the access key - * @param secretKey - * SK in the access key - * @param config - * Configuration parameters of ObsClient - */ - public SecretFlexibleObsClient(String accessKey, String secretKey, ObsConfiguration config) { - super(accessKey, secretKey, config); - } - - /** - * Constructor - * - * @param accessKey - * AK in the access key - * @param secretKey - * SK in the access key - * @param endPoint - * OBS endpoint - */ - public SecretFlexibleObsClient(String accessKey, String secretKey, String endPoint) { - super(accessKey, secretKey, endPoint); - } - - /** - * Constructor - * - * @param accessKey - * AK in the temporary access key - * @param secretKey - * SK in the temporary access key - * @param securityToken - * Security token - * @param config - * Configuration parameters of ObsClient - */ - public SecretFlexibleObsClient(String accessKey, String secretKey, String securityToken, ObsConfiguration config) { - super(accessKey, secretKey, securityToken, config); - } - - /** - * Constructor - * - * @param accessKey - * AK in the temporary access key - * @param secretKey - * SK in the temporary access key - * @param securityToken - * Security token - * @param endPoint - * OBS endpoint - */ - public SecretFlexibleObsClient(String accessKey, String secretKey, String securityToken, String endPoint) { - super(accessKey, secretKey, securityToken, endPoint); - } - - private void setContextProviderCredentials(String accessKey, String secretKey, String securityToken) { - ProviderCredentials providerCredentials = new ProviderCredentials(accessKey, secretKey, securityToken); - providerCredentials.setAuthType(this.getProviderCredentials().getAuthType()); - ProviderCredentialThreadContext.getInstance().setProviderCredentials(providerCredentials); - } - - private void setContextProviderCredentials(String accessKey, String secretKey) { - this.setContextProviderCredentials(accessKey, secretKey, null); - } - - private void clearContextProviderCredentials() { - ProviderCredentialThreadContext.getInstance().clearProviderCredentials(); - } - - - public ObsBucket createBucket(ObsBucket bucket, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.createBucket(bucket); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ObsBucket createBucket(ObsBucket bucket, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.createBucket(bucket); - } finally { - this.clearContextProviderCredentials(); - } - } - - public List listBuckets(String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.listBuckets(null); - } finally { - this.clearContextProviderCredentials(); - } - } - - public List listBuckets(String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.listBuckets(null); - } finally { - this.clearContextProviderCredentials(); - } - } - - public List listBuckets(ListBucketsRequest request, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.listBuckets(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public List listBuckets(ListBucketsRequest request, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.listBuckets(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucket(String bucketName, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteBucket(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucket(String bucketName, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteBucket(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ObjectListing listObjects(ListObjectsRequest listObjectsRequest, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.listObjects(listObjectsRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ObjectListing listObjects(ListObjectsRequest listObjectsRequest, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.listObjects(listObjectsRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public boolean headBucket(String bucketName, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.headBucket(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public boolean headBucket(String bucketName, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.headBucket(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public ListVersionsResult listVersions(String bucketName, String prefix, String delimiter, String keyMarker, - String versionIdMarker, long maxKeys, String nextVersionIdMarker, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.listVersions(bucketName, prefix, delimiter, keyMarker, versionIdMarker, maxKeys, - nextVersionIdMarker); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public ListVersionsResult listVersions(String bucketName, String prefix, String delimiter, String keyMarker, - String versionIdMarker, long maxKeys, String nextVersionIdMarker, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.listVersions(bucketName, prefix, delimiter, keyMarker, versionIdMarker, maxKeys, - nextVersionIdMarker); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ListVersionsResult listVersions(ListVersionsRequest request, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.listVersions(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ListVersionsResult listVersions(ListVersionsRequest request, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.listVersions(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ListVersionsResult listVersions(String bucketName, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.listVersions(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ListVersionsResult listVersions(String bucketName, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.listVersions(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ListVersionsResult listVersions(String bucketName, long maxKeys, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.listVersions(bucketName, maxKeys); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ListVersionsResult listVersions(String bucketName, long maxKeys, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.listVersions(bucketName, maxKeys); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketMetadataInfoResult getBucketMetadata(BucketMetadataInfoRequest bucketMetadataInfoRequest, - String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketMetadata(bucketMetadataInfoRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketMetadataInfoResult getBucketMetadata(BucketMetadataInfoRequest bucketMetadataInfoRequest, - String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketMetadata(bucketMetadataInfoRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public AccessControlList getBucketAcl(String bucketName, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketAcl(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public AccessControlList getBucketAcl(String bucketName, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketAcl(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketAcl(String bucketName, String cannedACL, AccessControlList acl, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketAcl(bucketName, cannedACL, acl); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketAcl(String bucketName, String cannedACL, AccessControlList acl, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketAcl(bucketName, cannedACL, acl); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketAcl(String bucketName, AccessControlList acl, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketAcl(bucketName, acl); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketAcl(String bucketName, AccessControlList acl, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketAcl(bucketName, acl); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketLocationResponse getBucketLocation(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketLocationV2(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketLocationResponse getBucketLocation(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketLocationV2(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketStorageInfo getBucketStorageInfo(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketStorageInfo(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketStorageInfo getBucketStorageInfo(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketStorageInfo(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketQuota getBucketQuota(String bucketName, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketQuota(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketQuota getBucketQuota(String bucketName, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketQuota(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketQuota(String bucketName, BucketQuota bucketQuota, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketQuota(bucketName, bucketQuota); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketQuota(String bucketName, BucketQuota bucketQuota, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketQuota(bucketName, bucketQuota); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketCors(String bucketName, BucketCors bucketCors, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketCors(bucketName, bucketCors); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketCors(String bucketName, BucketCors bucketCors, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketCors(bucketName, bucketCors); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketCors getBucketCors(String bucketName, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketCors(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketCors getBucketCors(String bucketName, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketCors(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketCors(String bucketName, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteBucketCors(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketCors(String bucketName, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteBucketCors(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public OptionsInfoResult optionsBucket(String bucketName, OptionsInfoRequest optionInfo, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.optionsBucket(bucketName, optionInfo); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public OptionsInfoResult optionsBucket(String bucketName, OptionsInfoRequest optionInfo, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.optionsBucket(bucketName, optionInfo); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public OptionsInfoResult optionsObject(String bucketName, String objectKey, OptionsInfoRequest optionInfo, - String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.optionsObject(bucketName, objectKey, optionInfo); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public OptionsInfoResult optionsObject(String bucketName, String objectKey, OptionsInfoRequest optionInfo, - String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.optionsObject(bucketName, objectKey, optionInfo); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public BucketLoggingConfiguration getBucketLoggingConfiguration(String bucketName, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketLoggingConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketLoggingConfiguration getBucketLogging(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketLogging(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public BucketLoggingConfiguration getBucketLoggingConfiguration(String bucketName, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketLoggingConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketLoggingConfiguration getBucketLogging(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketLogging(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketLoggingConfiguration(String bucketName, - BucketLoggingConfiguration loggingConfiguration, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketLoggingConfiguration(bucketName, loggingConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketLogging(String bucketName, BucketLoggingConfiguration loggingConfiguration, - String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketLogging(bucketName, loggingConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketLoggingConfiguration(String bucketName, - BucketLoggingConfiguration loggingConfiguration, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketLoggingConfiguration(bucketName, loggingConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketLogging(String bucketName, BucketLoggingConfiguration loggingConfiguration, - String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketLogging(bucketName, loggingConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketLoggingConfiguration(String bucketName, - BucketLoggingConfiguration loggingConfiguration, boolean updateTargetACLifRequired, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketLoggingConfiguration(bucketName, loggingConfiguration, updateTargetACLifRequired); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketLoggingConfiguration(String bucketName, - BucketLoggingConfiguration loggingConfiguration, boolean updateTargetACLifRequired, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketLoggingConfiguration(bucketName, loggingConfiguration, updateTargetACLifRequired); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfiguration versioningConfiguration, - String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketVersioning(bucketName, versioningConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketVersioning(String bucketName, BucketVersioningConfiguration versioningConfiguration, - String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketVersioning(bucketName, versioningConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketVersioningConfiguration getBucketVersioning(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketVersioning(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketVersioningConfiguration getBucketVersioning(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketVersioning(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public LifecycleConfiguration getBucketLifecycleConfiguration(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketLifecycleConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public LifecycleConfiguration getBucketLifecycle(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketLifecycle(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public LifecycleConfiguration getBucketLifecycleConfiguration(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketLifecycleConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public LifecycleConfiguration getBucketLifecycle(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketLifecycle(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketLifecycleConfiguration(String bucketName, LifecycleConfiguration lifecycleConfig, - String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketLifecycleConfiguration(bucketName, lifecycleConfig); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketLifecycle(String bucketName, LifecycleConfiguration lifecycleConfig, - String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketLifecycle(bucketName, lifecycleConfig); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketLifecycleConfiguration(String bucketName, LifecycleConfiguration lifecycleConfig, - String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketLifecycleConfiguration(bucketName, lifecycleConfig); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketLifecycle(String bucketName, LifecycleConfiguration lifecycleConfig, - String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketLifecycle(bucketName, lifecycleConfig); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse deleteBucketLifecycleConfiguration(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteBucketLifecycleConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketLifecycle(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteBucketLifecycle(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse deleteBucketLifecycleConfiguration(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteBucketLifecycleConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketLifecycle(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteBucketLifecycle(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketPolicyResponse getBucketPolicy(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketPolicyV2(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketPolicyResponse getBucketPolicy(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketPolicyV2(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketPolicy(String bucketName, String policy, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketPolicy(bucketName, policy); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketPolicy(String bucketName, String policy, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketPolicy(bucketName, policy); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketPolicy(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteBucketPolicy(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketPolicy(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteBucketPolicy(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public WebsiteConfiguration getBucketWebsiteConfiguration(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketWebsiteConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public WebsiteConfiguration getBucketWebsite(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketWebsite(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public WebsiteConfiguration getBucketWebsiteConfiguration(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketWebsiteConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public WebsiteConfiguration getBucketWebsite(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketWebsite(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketWebsiteConfiguration(String bucketName, WebsiteConfiguration websiteConfig, - String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketWebsiteConfiguration(bucketName, websiteConfig); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketWebsite(String bucketName, WebsiteConfiguration websiteConfig, - String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketWebsite(bucketName, websiteConfig); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketWebsiteConfiguration(String bucketName, WebsiteConfiguration websiteConfig, - String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketWebsiteConfiguration(bucketName, websiteConfig); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketWebsite(String bucketName, WebsiteConfiguration websiteConfig, - String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketWebsite(bucketName, websiteConfig); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse deleteBucketWebsiteConfiguration(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteBucketWebsiteConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketWebsite(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteBucketWebsite(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse deleteBucketWebsiteConfiguration(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteBucketWebsiteConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketWebsite(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteBucketWebsite(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketTagging(String bucketName, BucketTagInfo bucketTagInfo, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketTagging(bucketName, bucketTagInfo); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketTagging(String bucketName, BucketTagInfo bucketTagInfo, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketTagging(bucketName, bucketTagInfo); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketTagInfo getBucketTagging(String bucketName, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketTagging(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketTagInfo getBucketTagging(String bucketName, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketTagging(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketTagging(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteBucketTagging(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketTagging(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteBucketTagging(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketReplicationConfiguration(String bucketName, - ReplicationConfiguration replicationConfiguration, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketReplicationConfiguration(bucketName, replicationConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketReplication(String bucketName, - ReplicationConfiguration replicationConfiguration, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketReplication(bucketName, replicationConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setBucketReplicationConfiguration(String bucketName, - ReplicationConfiguration replicationConfiguration, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketReplicationConfiguration(bucketName, replicationConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketReplication(String bucketName, - ReplicationConfiguration replicationConfiguration, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketReplication(bucketName, replicationConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public ReplicationConfiguration getBucketReplicationConfiguration(String bucketName, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketReplicationConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ReplicationConfiguration getBucketReplication(String bucketName, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketReplication(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public ReplicationConfiguration getBucketReplicationConfiguration(String bucketName, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketReplicationConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ReplicationConfiguration getBucketReplication(String bucketName, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketReplication(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse deleteBucketReplicationConfiguration(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteBucketReplicationConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketReplication(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteBucketReplication(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse deleteBucketReplicationConfiguration(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteBucketReplicationConfiguration(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteBucketReplication(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteBucketReplication(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketNotificationConfiguration getBucketNotification(String bucketName, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketNotification(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketNotificationConfiguration getBucketNotification(String bucketName, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketNotification(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketNotification(String bucketName, - BucketNotificationConfiguration bucketNotificationConfiguration, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketNotification(bucketName, bucketNotificationConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketNotification(String bucketName, - BucketNotificationConfiguration bucketNotificationConfiguration, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketNotification(bucketName, bucketNotificationConfiguration); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketStoragePolicy(final String bucketName, - final BucketStoragePolicyConfiguration bucketStorage, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setBucketStoragePolicy(bucketName, bucketStorage); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setBucketStoragePolicy(final String bucketName, - final BucketStoragePolicyConfiguration bucketStorage, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setBucketStoragePolicy(bucketName, bucketStorage); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketStoragePolicyConfiguration getBucketStoragePolicy(final String bucketName, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getBucketStoragePolicy(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public BucketStoragePolicyConfiguration getBucketStoragePolicy(final String bucketName, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getBucketStoragePolicy(bucketName); - } finally { - this.clearContextProviderCredentials(); - } - } - - public PutObjectResult putObject(PutObjectRequest request, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.putObject(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public PutObjectResult putObject(PutObjectRequest request, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.putObject(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ObsObject getObject(GetObjectRequest getObjectRequest, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getObject(getObjectRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ObsObject getObject(GetObjectRequest getObjectRequest, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getObject(getObjectRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ObjectMetadata getObjectMetadata(GetObjectMetadataRequest request, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getObjectMetadata(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ObjectMetadata getObjectMetadata(GetObjectMetadataRequest request, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getObjectMetadata(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public RestoreObjectStatus restoreObject(RestoreObjectRequest restoreObjectRequest, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.restoreObject(restoreObjectRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public RestoreObjectStatus restoreObject(RestoreObjectRequest restoreObjectRequest, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.restoreObject(restoreObjectRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteObject(String bucketName, String objectKey, String versionId, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteObject(bucketName, objectKey, versionId); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteObject(String bucketName, String objectKey, String versionId, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteObject(bucketName, objectKey, versionId); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse deleteObject(String bucketName, String objectKey, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteObject(bucketName, objectKey); - } finally { - this.clearContextProviderCredentials(); - } - } - - public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.deleteObjects(deleteObjectsRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.deleteObjects(deleteObjectsRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public AccessControlList getObjectAcl(String bucketName, String objectKey, String versionId, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getObjectAcl(bucketName, objectKey, versionId); - } finally { - this.clearContextProviderCredentials(); - } - } - - public AccessControlList getObjectAcl(String bucketName, String objectKey, String versionId, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.getObjectAcl(bucketName, objectKey, versionId); - } finally { - this.clearContextProviderCredentials(); - } - } - - public AccessControlList getObjectAcl(String bucketName, String objectKey, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.getObjectAcl(bucketName, objectKey); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setObjectAcl(String bucketName, String objectKey, String cannedACL, AccessControlList acl, - String versionId, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setObjectAcl(bucketName, objectKey, cannedACL, acl, versionId); - } finally { - this.clearContextProviderCredentials(); - } - } - - @Deprecated - public HeaderResponse setObjectAcl(String bucketName, String objectKey, String cannedACL, AccessControlList acl, - String versionId, String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setObjectAcl(bucketName, objectKey, cannedACL, acl, versionId); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setObjectAcl(String bucketName, String objectKey, AccessControlList acl, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setObjectAcl(bucketName, objectKey, acl); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setObjectAcl(String bucketName, String objectKey, AccessControlList acl, String versionId, - String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.setObjectAcl(bucketName, objectKey, acl, versionId); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse setObjectAcl(String bucketName, String objectKey, AccessControlList acl, String versionId, - String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.setObjectAcl(bucketName, objectKey, acl, versionId); - } finally { - this.clearContextProviderCredentials(); - } - } - - public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.copyObject(copyObjectRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.copyObject(copyObjectRequest); - } finally { - this.clearContextProviderCredentials(); - } - } - - public InitiateMultipartUploadResult initiateMultipartUpload(InitiateMultipartUploadRequest request, - String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.initiateMultipartUpload(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public InitiateMultipartUploadResult initiateMultipartUpload(InitiateMultipartUploadRequest request, - String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.initiateMultipartUpload(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse abortMultipartUpload(AbortMultipartUploadRequest request, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.abortMultipartUpload(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public HeaderResponse abortMultipartUpload(AbortMultipartUploadRequest request, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.abortMultipartUpload(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public UploadPartResult uploadPart(UploadPartRequest request, String accessKey, String secretKey) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.uploadPart(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public UploadPartResult uploadPart(UploadPartRequest request, String accessKey, String secretKey, - String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.uploadPart(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public CopyPartResult copyPart(CopyPartRequest request, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.copyPart(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public CopyPartResult copyPart(CopyPartRequest request, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.copyPart(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public CompleteMultipartUploadResult completeMultipartUpload(CompleteMultipartUploadRequest request, - String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.completeMultipartUpload(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public CompleteMultipartUploadResult completeMultipartUpload(CompleteMultipartUploadRequest request, - String accessKey, String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.completeMultipartUpload(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ListPartsResult listParts(ListPartsRequest request, String accessKey, String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.listParts(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public ListPartsResult listParts(ListPartsRequest request, String accessKey, String secretKey, String securityToken) - throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.listParts(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public MultipartUploadListing listMultipartUploads(ListMultipartUploadsRequest request, String accessKey, - String secretKey) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey); - try { - return super.listMultipartUploads(request); - } finally { - this.clearContextProviderCredentials(); - } - } - - public MultipartUploadListing listMultipartUploads(ListMultipartUploadsRequest request, String accessKey, - String secretKey, String securityToken) throws ObsException { - this.setContextProviderCredentials(accessKey, secretKey, securityToken); - try { - return super.listMultipartUploads(request); - } finally { - this.clearContextProviderCredentials(); - } - } +public class SecretFlexibleObsClient extends SecretFlexibleBucketObsClient { + /** + * Constructor + * + * @param config + * Configuration parameters of ObsClient + */ + public SecretFlexibleObsClient(ObsConfiguration config) { + this("", "", config); + } + + /** + * Constructor + * + * @param endPoint + * OBS endpoint + */ + public SecretFlexibleObsClient(String endPoint) { + this("", "", endPoint); + } + + /** + * Constructor + * + * @param accessKey + * AK in the access key + * @param secretKey + * SK in the access key + * @param config + * Configuration parameters of ObsClient + */ + public SecretFlexibleObsClient(String accessKey, String secretKey, ObsConfiguration config) { + super(accessKey, secretKey, config); + } + + /** + * Constructor + * + * @param accessKey + * AK in the access key + * @param secretKey + * SK in the access key + * @param endPoint + * OBS endpoint + */ + public SecretFlexibleObsClient(String accessKey, String secretKey, String endPoint) { + super(accessKey, secretKey, endPoint); + } + + /** + * Constructor + * + * @param accessKey + * AK in the temporary access key + * @param secretKey + * SK in the temporary access key + * @param securityToken + * Security token + * @param config + * Configuration parameters of ObsClient + */ + public SecretFlexibleObsClient(String accessKey, String secretKey, String securityToken, ObsConfiguration config) { + super(accessKey, secretKey, securityToken, config); + } + + /** + * Constructor + * + * @param accessKey + * AK in the temporary access key + * @param secretKey + * SK in the temporary access key + * @param securityToken + * Security token + * @param endPoint + * OBS endpoint + */ + public SecretFlexibleObsClient(String accessKey, String secretKey, String securityToken, String endPoint) { + super(accessKey, secretKey, securityToken, endPoint); + } + + public ObjectListing listObjects(ListObjectsRequest listObjectsRequest, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(listObjectsRequest.getBucketName(), accessKey, secretKey); + try { + return super.listObjects(listObjectsRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ObjectListing listObjects(ListObjectsRequest listObjectsRequest, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(listObjectsRequest.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.listObjects(listObjectsRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + //CHECKSTYLE:OFF + public ListVersionsResult listVersions(String bucketName, String prefix, String delimiter, String keyMarker, + String versionIdMarker, long maxKeys, String nextVersionIdMarker, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.listVersions(bucketName, prefix, delimiter, keyMarker, versionIdMarker, maxKeys, + nextVersionIdMarker); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + //CHECKSTYLE:OFF + public ListVersionsResult listVersions(String bucketName, String prefix, String delimiter, String keyMarker, + String versionIdMarker, long maxKeys, String nextVersionIdMarker, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.listVersions(bucketName, prefix, delimiter, keyMarker, versionIdMarker, maxKeys, + nextVersionIdMarker); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ListVersionsResult listVersions(ListVersionsRequest request, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey); + try { + return super.listVersions(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ListVersionsResult listVersions(ListVersionsRequest request, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.listVersions(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ListVersionsResult listVersions(String bucketName, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.listVersions(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ListVersionsResult listVersions(String bucketName, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.listVersions(bucketName); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ListVersionsResult listVersions(String bucketName, long maxKeys, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.listVersions(bucketName, maxKeys); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ListVersionsResult listVersions(String bucketName, long maxKeys, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.listVersions(bucketName, maxKeys); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public OptionsInfoResult optionsObject(String bucketName, String objectKey, OptionsInfoRequest optionInfo, + String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.optionsObject(bucketName, objectKey, optionInfo); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public OptionsInfoResult optionsObject(String bucketName, String objectKey, OptionsInfoRequest optionInfo, + String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.optionsObject(bucketName, objectKey, optionInfo); + } finally { + this.clearContextProviderCredentials(); + } + } + + public PutObjectResult putObject(PutObjectRequest request, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey); + try { + return super.putObject(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public PutObjectResult putObject(PutObjectRequest request, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.putObject(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ObsObject getObject(GetObjectRequest getObjectRequest, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(getObjectRequest.getBucketName(), accessKey, secretKey); + try { + return super.getObject(getObjectRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ObsObject getObject(GetObjectRequest getObjectRequest, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(getObjectRequest.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.getObject(getObjectRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ObjectMetadata getObjectMetadata(GetObjectMetadataRequest request, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey); + try { + return super.getObjectMetadata(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ObjectMetadata getObjectMetadata(GetObjectMetadataRequest request, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.getObjectMetadata(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public RestoreObjectStatus restoreObject(RestoreObjectRequest restoreObjectRequest, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(restoreObjectRequest.getBucketName(), accessKey, secretKey); + try { + return super.restoreObject(restoreObjectRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + public RestoreObjectStatus restoreObject(RestoreObjectRequest restoreObjectRequest, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(restoreObjectRequest.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.restoreObject(restoreObjectRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteObject(String bucketName, String objectKey, String versionId, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteObject(bucketName, objectKey, versionId); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteObject(String bucketName, String objectKey, String versionId, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.deleteObject(bucketName, objectKey, versionId); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse deleteObject(String bucketName, String objectKey, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.deleteObject(bucketName, objectKey); + } finally { + this.clearContextProviderCredentials(); + } + } + + public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(deleteObjectsRequest.getBucketName(), accessKey, secretKey); + try { + return super.deleteObjects(deleteObjectsRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(deleteObjectsRequest.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.deleteObjects(deleteObjectsRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + public AccessControlList getObjectAcl(String bucketName, String objectKey, String versionId, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getObjectAcl(bucketName, objectKey, versionId); + } finally { + this.clearContextProviderCredentials(); + } + } + + public AccessControlList getObjectAcl(String bucketName, String objectKey, String versionId, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.getObjectAcl(bucketName, objectKey, versionId); + } finally { + this.clearContextProviderCredentials(); + } + } + + public AccessControlList getObjectAcl(String bucketName, String objectKey, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.getObjectAcl(bucketName, objectKey); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setObjectAcl(String bucketName, String objectKey, String cannedACL, AccessControlList acl, + String versionId, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setObjectAcl(bucketName, objectKey, cannedACL, acl, versionId); + } finally { + this.clearContextProviderCredentials(); + } + } + + @Deprecated + public HeaderResponse setObjectAcl(String bucketName, String objectKey, String cannedACL, AccessControlList acl, + String versionId, String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setObjectAcl(bucketName, objectKey, cannedACL, acl, versionId); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setObjectAcl(String bucketName, String objectKey, AccessControlList acl, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setObjectAcl(bucketName, objectKey, acl); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setObjectAcl(String bucketName, String objectKey, AccessControlList acl, String versionId, + String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey); + try { + return super.setObjectAcl(bucketName, objectKey, acl, versionId); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse setObjectAcl(String bucketName, String objectKey, AccessControlList acl, String versionId, + String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(bucketName, accessKey, secretKey, securityToken); + try { + return super.setObjectAcl(bucketName, objectKey, acl, versionId); + } finally { + this.clearContextProviderCredentials(); + } + } + + public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(copyObjectRequest.getBucketName(), accessKey, secretKey); + try { + return super.copyObject(copyObjectRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(copyObjectRequest.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.copyObject(copyObjectRequest); + } finally { + this.clearContextProviderCredentials(); + } + } + + public InitiateMultipartUploadResult initiateMultipartUpload(InitiateMultipartUploadRequest request, + String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey); + try { + return super.initiateMultipartUpload(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public InitiateMultipartUploadResult initiateMultipartUpload(InitiateMultipartUploadRequest request, + String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.initiateMultipartUpload(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse abortMultipartUpload(AbortMultipartUploadRequest request, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey); + try { + return super.abortMultipartUpload(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public HeaderResponse abortMultipartUpload(AbortMultipartUploadRequest request, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.abortMultipartUpload(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public UploadPartResult uploadPart(UploadPartRequest request, String accessKey, String secretKey) + throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey); + try { + return super.uploadPart(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public UploadPartResult uploadPart(UploadPartRequest request, String accessKey, String secretKey, + String securityToken) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.uploadPart(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public CopyPartResult copyPart(CopyPartRequest request, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(request.getSourceBucketName(), accessKey, secretKey); + try { + return super.copyPart(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public CopyPartResult copyPart(CopyPartRequest request, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(request.getSourceBucketName(), accessKey, secretKey, securityToken); + try { + return super.copyPart(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public CompleteMultipartUploadResult completeMultipartUpload(CompleteMultipartUploadRequest request, + String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey); + try { + return super.completeMultipartUpload(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public CompleteMultipartUploadResult completeMultipartUpload(CompleteMultipartUploadRequest request, + String accessKey, String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.completeMultipartUpload(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ListPartsResult listParts(ListPartsRequest request, String accessKey, String secretKey) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey); + try { + return super.listParts(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public ListPartsResult listParts(ListPartsRequest request, String accessKey, String secretKey, String securityToken) + throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.listParts(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public MultipartUploadListing listMultipartUploads(ListMultipartUploadsRequest request, String accessKey, + String secretKey) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey); + try { + return super.listMultipartUploads(request); + } finally { + this.clearContextProviderCredentials(); + } + } + + public MultipartUploadListing listMultipartUploads(ListMultipartUploadsRequest request, String accessKey, + String secretKey, String securityToken) throws ObsException { + this.setContextProviderCredentials(request.getBucketName(), accessKey, secretKey, securityToken); + try { + return super.listMultipartUploads(request); + } finally { + this.clearContextProviderCredentials(); + } + } } - - diff --git a/app/src/main/java/com/obs/services/exception/ObsException.java b/app/src/main/java/com/obs/services/exception/ObsException.java index 12e485f..a450d3e 100644 --- a/app/src/main/java/com/obs/services/exception/ObsException.java +++ b/app/src/main/java/com/obs/services/exception/ObsException.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.exception; import java.util.Map; @@ -18,229 +19,199 @@ import java.util.regex.Pattern; /** - * This exception is thrown when the interface fails to be called or access to OBS fails. + * This exception is thrown when the interface fails to be called or access to + * OBS fails. */ -public class ObsException extends RuntimeException -{ +public class ObsException extends RuntimeException { private static final long serialVersionUID = 1L; - + private String xmlMessage = null; - + private String errorCode = null; - + private String errorMessage = null; - + private String errorRequestId = null; - + private String errorHostId = null; - + private Map responseHeaders = null; - + private int responseCode = -1; - + private String responseStatus = null; - + private String errorIndicator = null; - - public ObsException(String message) - { + + public ObsException(String message) { this(message, null, null); } - - public ObsException(String message, Throwable e) - { + + public ObsException(String message, Throwable e) { this(message, null, e); } - - public ObsException(String message, String xmlMessage) - { + + public ObsException(String message, String xmlMessage) { this(message, xmlMessage, null); } - - public ObsException(String message, String xmlMessage, Throwable cause) - { + + public ObsException(String message, String xmlMessage, Throwable cause) { super(message, cause); - if (xmlMessage != null) - { + if (xmlMessage != null) { parseXmlMessage(xmlMessage); } } - + @Override - public String toString() - { + public String toString() { String myString = super.toString(); - - if (responseCode != -1) - { + + if (responseCode != -1) { myString += " -- ResponseCode: " + responseCode + ", ResponseStatus: " + responseStatus; } - if (isParsedFromXmlMessage()) - { + if (isParsedFromXmlMessage()) { myString += ", XML Error Message: " + xmlMessage; - } - else if (errorRequestId != null) - { + } else if (errorRequestId != null) { myString += ", RequestId: " + errorRequestId + ", HostId: " + errorHostId; } return myString; } - - private boolean isParsedFromXmlMessage() - { + + private boolean isParsedFromXmlMessage() { return xmlMessage != null; } - - private String findXmlElementText(String xmlMsg, String elementName) - { + + private String findXmlElementText(String xmlMsg, String elementName) { Pattern pattern = Pattern.compile(".*<" + elementName + ">(.*).*"); Matcher matcher = pattern.matcher(xmlMsg); - if (matcher.matches() && matcher.groupCount() == 1) - { + if (matcher.matches() && matcher.groupCount() == 1) { return matcher.group(1); - } - else - { + } else { return null; } } - - private void parseXmlMessage(String xmlMsg) - { + + private void parseXmlMessage(String xmlMsg) { xmlMsg = xmlMsg.replaceAll("\n", ""); this.xmlMessage = xmlMsg; - + this.errorCode = findXmlElementText(xmlMsg, "Code"); this.errorMessage = findXmlElementText(xmlMsg, "Message"); this.errorRequestId = findXmlElementText(xmlMsg, "RequestId"); this.errorHostId = findXmlElementText(xmlMsg, "HostId"); } - - public String getXmlMessage() - { + + public String getXmlMessage() { return xmlMessage; } - - public void setXmlMessage(String xmlMessage) - { + + public void setXmlMessage(String xmlMessage) { this.xmlMessage = xmlMessage; } - + /** - * Obtain the error code returned by the OBS server. + * Obtain the error code returned by the OBS server. * * @return Error code */ - public String getErrorCode() - { + public String getErrorCode() { return errorCode; } - public void setErrorCode(String errorCode) - { + public void setErrorCode(String errorCode) { this.errorCode = errorCode; } - + /** - * Obtain the error details returned by the OBS server. + * Obtain the error details returned by the OBS server. * * @return Error details */ - public String getErrorMessage() - { + public String getErrorMessage() { return errorMessage; } - - public void setErrorMessage(String errorMessage) - { + + public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } - + /** * Obtain the request ID returned by the OBS server. * * @return Request ID */ - public String getErrorRequestId() - { + public String getErrorRequestId() { return errorRequestId; } - - public void setErrorRequestId(String errorRequestId) - { + + public void setErrorRequestId(String errorRequestId) { this.errorRequestId = errorRequestId; } - + /** * Obtain the server ID. * * @return Server ID */ - public String getErrorHostId() - { + public String getErrorHostId() { return errorHostId; } - - public void setErrorHostId(String errorHostId) - { + + public void setErrorHostId(String errorHostId) { this.errorHostId = errorHostId; } - + /** - * Obtain the response headers of the HTTP request returned by the OBS server. + * Obtain the response headers of the HTTP request returned by the OBS + * server. * * @return HTTP response headers */ - public Map getResponseHeaders() - { + public Map getResponseHeaders() { return responseHeaders; } - - public void setResponseHeaders(Map responseHeaders) - { + + public void setResponseHeaders(Map responseHeaders) { this.responseHeaders = responseHeaders; } - + /** * Obtain the HTTP status code returned by the OBS server. * * @return HTTP status code */ - public int getResponseCode() - { + public int getResponseCode() { return responseCode; } - - public void setResponseCode(int responseCode) - { + + public void setResponseCode(int responseCode) { this.responseCode = responseCode; } - + /** * Obtain the HTTP response description returned by the OBS server. * * @return HTTP response description */ - public String getResponseStatus() - { + public String getResponseStatus() { return responseStatus; } - - public void setResponseStatus(String responseStatus) - { + + public void setResponseStatus(String responseStatus) { this.responseStatus = responseStatus; } - /** - * Obtain the detailed error indicators returned by the OBS server. - * @return OBS Detailed error indicators returned by the server - */ - public String getErrorIndicator() { - return errorIndicator; - } + /** + * Obtain the detailed error indicators returned by the OBS server. + * + * @return OBS Detailed error indicators returned by the server + */ + public String getErrorIndicator() { + return errorIndicator; + } + + public void setErrorIndicator(String errorIndicator) { + this.errorIndicator = errorIndicator; + } - public void setErrorIndicator(String errorIndicator) { - this.errorIndicator = errorIndicator; - } - } diff --git a/app/src/main/java/com/obs/services/internal/ConcurrentProgressManager.java b/app/src/main/java/com/obs/services/internal/ConcurrentProgressManager.java index 41be4bc..eafd883 100644 --- a/app/src/main/java/com/obs/services/internal/ConcurrentProgressManager.java +++ b/app/src/main/java/com/obs/services/internal/ConcurrentProgressManager.java @@ -3,71 +3,87 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services.internal; -import java.util.Date; -import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicLong; +package com.obs.services.internal; import com.obs.services.model.ProgressListener; import com.obs.services.model.ProgressStatus; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; + public class ConcurrentProgressManager extends ProgressManager { - private AtomicBoolean startFlag = new AtomicBoolean(false); - protected AtomicLong transferredBytes; - protected AtomicLong newlyTransferredBytes; + private AtomicBoolean startFlag = new AtomicBoolean(false); + protected AtomicLong transferredBytes; + protected AtomicLong newlyTransferredBytes; + protected AtomicLong lastSecondBytes; + protected AtomicLong currentSecondBytes; + protected AtomicLong lastSwapTimeStamp; - public ConcurrentProgressManager(long totalBytes, long transferredBytes, ProgressListener progressListener, - long intervalBytes) { - super(totalBytes, progressListener, intervalBytes); - this.transferredBytes = transferredBytes < 0 ? new AtomicLong(0) : new AtomicLong(transferredBytes); - this.newlyTransferredBytes = new AtomicLong(0); - } + public ConcurrentProgressManager(long totalBytes, long transferredBytes, ProgressListener progressListener, + long intervalBytes) { + super(totalBytes, progressListener, intervalBytes); + this.transferredBytes = transferredBytes < 0 ? new AtomicLong(0) : new AtomicLong(transferredBytes); + this.newlyTransferredBytes = new AtomicLong(0); + this.lastSecondBytes = new AtomicLong(-1); + this.currentSecondBytes = new AtomicLong(0); + this.lastSwapTimeStamp = new AtomicLong(System.currentTimeMillis()); + } - public void progressStart() { - if(startFlag.compareAndSet(false, true)) { - super.progressStart(); - } - } + public void progressStart() { + if (startFlag.compareAndSet(false, true)) { + super.progressStart(); + } + } - public void progressEnd() { - if(this.progressListener == null) { - return; - } - synchronized (this) { - Date now = new Date(); - ProgressStatus status = new DefaultProgressStatus(this.newlyTransferredBytes.get(), this.transferredBytes.get(), - this.totalBytes, now.getTime() - this.lastCheckpoint.getTime(), now.getTime() - this.startCheckpoint.getTime()); - this.progressListener.progressChanged(status); - } - } + public void progressEnd() { + if (this.progressListener == null) { + return; + } + synchronized (this) { + long now = System.currentTimeMillis(); + ProgressStatus status = new DefaultProgressStatus(this.newlyTransferredBytes.get(), + this.transferredBytes.get(), this.totalBytes, now - this.lastCheckpoint, + now - this.startCheckpoint); + this.progressListener.progressChanged(status); + } + } - @Override - protected void doProgressChanged(int bytes) { - long _transferredBytes = this.transferredBytes.addAndGet(bytes); - long _newlyTransferredBytes = this.newlyTransferredBytes.addAndGet(bytes); - Date now = new Date(); - List currentInstantaneousBytes = this.createCurrentInstantaneousBytes(bytes, now); - this.lastInstantaneousBytes = currentInstantaneousBytes; - if(_newlyTransferredBytes >= this.intervalBytes && (_transferredBytes < this.totalBytes || this.totalBytes == -1)) { - if(this.newlyTransferredBytes.compareAndSet(_newlyTransferredBytes, -_newlyTransferredBytes)) { - DefaultProgressStatus status = new DefaultProgressStatus(_newlyTransferredBytes, _transferredBytes, - this.totalBytes, now.getTime() - this.lastCheckpoint.getTime(), now.getTime() - this.startCheckpoint.getTime()); - status.setInstantaneousBytes(currentInstantaneousBytes); - this.progressListener.progressChanged(status); - this.lastCheckpoint = now; - } - } - } + @Override + protected void doProgressChanged(int bytes) { + long transferred = this.transferredBytes.addAndGet(bytes); + long newlyTransferred = this.newlyTransferredBytes.addAndGet(bytes); + // 获取当前时间戳,减去上次更新速度的时间,如若时间差大于 1000ms,则对上一秒传输字节数进行更新 + long now = System.currentTimeMillis(); + // 采用局部变量保证线程安全 + long swapIntervalTime = now - lastSwapTimeStamp.get(); + currentSecondBytes.addAndGet(bytes); + // 上一秒传输字节设置为 当前传输字节 / 耗时,更新当前传输字节为 0,重设更新时间 + if (swapIntervalTime > 1000) { + lastSecondBytes.set((long) (currentSecondBytes.get() / (swapIntervalTime / 1000.0))); + currentSecondBytes.set(0); + lastSwapTimeStamp.set(now); + } + // 当新传输字节数大于用户设置阈值时更新回调函数,其中瞬时速度使用上一秒传输字节数作为近似值 + if (newlyTransferred >= this.intervalBytes + && (transferred < this.totalBytes || this.totalBytes == -1)) { + if (this.newlyTransferredBytes.compareAndSet(newlyTransferred, -newlyTransferred)) { + DefaultProgressStatus status = new DefaultProgressStatus(newlyTransferred, transferred, + this.totalBytes, now - this.lastCheckpoint, now - this.startCheckpoint); + status.setInstantaneousSpeed(lastSecondBytes.get()); + this.progressListener.progressChanged(status); + this.lastCheckpoint = now; + } + } + } } diff --git a/app/src/main/java/com/obs/services/internal/Constants.java b/app/src/main/java/com/obs/services/internal/Constants.java index adaf349..e8ae4dc 100644 --- a/app/src/main/java/com/obs/services/internal/Constants.java +++ b/app/src/main/java/com/obs/services/internal/Constants.java @@ -11,11 +11,12 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; +import java.util.EnumMap; import java.util.List; import java.util.Map; import java.util.TimeZone; @@ -25,119 +26,129 @@ import com.obs.services.internal.utils.V2Authentication; import com.obs.services.model.AuthTypeEnum; -public class Constants -{ - public static class CommonHeaders { - - public static final String CONTENT_LENGTH = "Content-Length"; - - public static final String CONTENT_TYPE = "Content-Type"; - - public static final String HOST = "Host"; - - public static final String ETAG = "ETag"; - - public static final String CONTENT_MD5 = "Content-MD5"; - - public static final String ORIGIN = "Origin"; - - public static final String USER_AGENT = "User-Agent"; - - public static final String PROXY_AUTHORIZATION = "Proxy-Authorization"; - - public static final String LOCATION = "Location"; - - public static final String ACCESS_CONTROL_REQUEST_HEADERS = "Access-Control-Request-Headers"; - public static final String ACCESS_CONTROL_REQUEST_METHOD = "Access-Control-Request-Method"; - - public static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers"; - public static final String ACCESS_CONTROL_MAX_AGE = "Access-Control-Max-Age"; - public static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin"; - public static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods"; - public static final String ACCESS_CONTROL_EXPOSE_HEADERS = "Access-Control-Expose-Headers"; - - - public static final String CACHE_CONTROL = "Cache-Control"; - - public static final String CONTENT_DISPOSITION = "Content-Disposition"; - - public static final String CONTENT_ENCODING = "Content-Encoding"; - - public static final String CONTENT_LANGUAGE = "Content-Language"; - - public static final String EXPIRES = "Expires"; - - public static final String DATE = "Date"; - - public static final String LAST_MODIFIED = "Last-Modified"; - - public static final String CONNECTION = "Connection"; - - public static final String AUTHORIZATION = "Authorization"; - - public static final String RANGE = "Range"; - - public static final String IF_MODIFIED_SINCE = "If-Modified-Since"; - - public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since"; - - public static final String IF_MATCH = "If-Match"; - - public static final String IF_NONE_MATCH = "If-None-Match"; - - public static final String X_RESERVED_INDICATOR = "x-reserved-indicator"; - } - - public static class ObsRequestParams{ - public static final String UPLOAD_ID = "uploadId"; - public static final String VERSION_ID = "versionId"; - public static final String PREFIX = "prefix"; - public static final String MARKER = "marker"; - public static final String MAX_KEYS = "max-keys"; - public static final String MAX_UPLOADS = "max-uploads"; - public static final String DELIMITER = "delimiter"; - public static final String KEY_MARKER = "key-marker"; - public static final String UPLOAD_ID_MARKER = "upload-id-marker"; - public static final String VERSION_ID_MARKER = "version-id-marker"; - public static final String RESPONSE_CONTENT_TYPE = "response-content-type"; - public static final String RESPONSE_CONTENT_LANGUAGE = "response-content-language"; - public static final String RESPONSE_EXPIRES = "response-expires"; - public static final String RESPONSE_CACHE_CONTROL = "response-cache-control"; - public static final String RESPONSE_CONTENT_DISPOSITION = "response-content-disposition"; - public static final String RESPONSE_CONTENT_ENCODING = "response-content-encoding"; - public static final String X_IMAGE_PROCESS = "x-image-process"; - public static final String POSITION = "position"; - - public static final String MAX_PARTS = "max-parts"; - public static final String PART_NUMBER_MARKER = "part-number-marker"; - public static final String PART_NUMBER = "partNumber"; - - public static final String NAME = "name"; - public static final String LENGTH = "length"; - - public static final String READAHEAD = "readAhead"; - public static final String X_CACHE_CONTROL = "x-cache-control"; - public static final String TASKID = "taskID"; - } - - - public static final Map HEADERS_MAP = new HashMap(); - public static final Map CONVERTOR_MAP = new HashMap(); - public static final Map AUTHTICATION_MAP = new HashMap(); - - static { - HEADERS_MAP.put(AuthTypeEnum.V2, V2Headers.getInstance()); - HEADERS_MAP.put(AuthTypeEnum.V4, V2Headers.getInstance()); - HEADERS_MAP.put(AuthTypeEnum.OBS, ObsHeaders.getInstance()); - - CONVERTOR_MAP.put(AuthTypeEnum.V2, V2Convertor.getInstance()); - CONVERTOR_MAP.put(AuthTypeEnum.V4, V2Convertor.getInstance()); - CONVERTOR_MAP.put(AuthTypeEnum.OBS, ObsConvertor.getInstance()); - - AUTHTICATION_MAP.put(AuthTypeEnum.V2, V2Authentication.getInstance()); - AUTHTICATION_MAP.put(AuthTypeEnum.OBS, ObsAuthentication.getInstance()); - } - +public class Constants { + public static class CommonHeaders { + + public static final String CONTENT_LENGTH = "Content-Length"; + + public static final String CONTENT_TYPE = "Content-Type"; + + public static final String HOST = "Host"; + + public static final String ETAG = "ETag"; + + public static final String CONTENT_MD5 = "Content-MD5"; + + public static final String ORIGIN = "Origin"; + + public static final String USER_AGENT = "User-Agent"; + + public static final String PROXY_AUTHORIZATION = "Proxy-Authorization"; + + public static final String LOCATION = "Location"; + + public static final String ACCESS_CONTROL_REQUEST_HEADERS = "Access-Control-Request-Headers"; + public static final String ACCESS_CONTROL_REQUEST_METHOD = "Access-Control-Request-Method"; + + public static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers"; + public static final String ACCESS_CONTROL_MAX_AGE = "Access-Control-Max-Age"; + public static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin"; + public static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods"; + public static final String ACCESS_CONTROL_EXPOSE_HEADERS = "Access-Control-Expose-Headers"; + + public static final String CACHE_CONTROL = "Cache-Control"; + + public static final String CONTENT_DISPOSITION = "Content-Disposition"; + + public static final String HASH_CRC64ECMA = "hash-crc64ecma"; + + public static final String CONTENT_ENCODING = "Content-Encoding"; + + public static final String CONTENT_LANGUAGE = "Content-Language"; + + public static final String EXPIRES = "Expires"; + + public static final String DATE = "Date"; + + public static final String LAST_MODIFIED = "Last-Modified"; + + public static final String CONNECTION = "Connection"; + + public static final String AUTHORIZATION = "Authorization"; + + public static final String RANGE = "Range"; + + public static final String IF_MODIFIED_SINCE = "If-Modified-Since"; + + public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since"; + + public static final String IF_MATCH = "If-Match"; + + public static final String IF_NONE_MATCH = "If-None-Match"; + + public static final String X_RESERVED_INDICATOR = "x-reserved-indicator"; + + public static final String ACCETP_ENCODING = "Accept-Encoding"; + + public static final String CALLBACK = "callback"; + } + + public static class ObsRequestParams { + public static final String UPLOAD_ID = "uploadId"; + public static final String VERSION_ID = "versionId"; + public static final String PREFIX = "prefix"; + public static final String MARKER = "marker"; + public static final String MAX_KEYS = "max-keys"; + public static final String MAX_UPLOADS = "max-uploads"; + public static final String DELIMITER = "delimiter"; + public static final String KEY_MARKER = "key-marker"; + public static final String UPLOAD_ID_MARKER = "upload-id-marker"; + public static final String VERSION_ID_MARKER = "version-id-marker"; + public static final String RESPONSE_CONTENT_TYPE = "response-content-type"; + public static final String RESPONSE_CONTENT_LANGUAGE = "response-content-language"; + public static final String RESPONSE_EXPIRES = "response-expires"; + public static final String RESPONSE_CACHE_CONTROL = "response-cache-control"; + public static final String RESPONSE_CONTENT_DISPOSITION = "response-content-disposition"; + public static final String RESPONSE_CONTENT_ENCODING = "response-content-encoding"; + public static final String X_IMAGE_PROCESS = "x-image-process"; + public static final String POSITION = "position"; + public static final String ENCODING_TYPE = "encoding-type"; + + public static final String MAX_PARTS = "max-parts"; + public static final String PART_NUMBER_MARKER = "part-number-marker"; + public static final String PART_NUMBER = "partNumber"; + + public static final String NAME = "name"; + public static final String LENGTH = "length"; + + public static final String READAHEAD = "readAhead"; + public static final String X_CACHE_CONTROL = "x-cache-control"; + public static final String TASKID = "taskID"; + } + + public static final Map HEADERS_MAP; + public static final Map CONVERTOR_MAP; + public static final Map AUTHTICATION_MAP; + + static { + Map headersMap = new EnumMap<>(AuthTypeEnum.class); + headersMap.put(AuthTypeEnum.V2, V2Headers.getInstance()); + headersMap.put(AuthTypeEnum.V4, V2Headers.getInstance()); + headersMap.put(AuthTypeEnum.OBS, ObsHeaders.getInstance()); + HEADERS_MAP = Collections.unmodifiableMap(headersMap); + + Map convertorMap = new EnumMap<>(AuthTypeEnum.class); + convertorMap.put(AuthTypeEnum.V2, V2Convertor.getInstance()); + convertorMap.put(AuthTypeEnum.V4, V2Convertor.getInstance()); + convertorMap.put(AuthTypeEnum.OBS, ObsConvertor.getInstance()); + CONVERTOR_MAP = Collections.unmodifiableMap(convertorMap); + + Map authticationMap = new EnumMap<>(AuthTypeEnum.class); + authticationMap.put(AuthTypeEnum.V2, V2Authentication.getInstance()); + authticationMap.put(AuthTypeEnum.OBS, ObsAuthentication.getInstance()); + AUTHTICATION_MAP = Collections.unmodifiableMap(authticationMap); + } + public static final String ACL_PRIVATE = "private"; public static final String ACL_PUBLIC_READ = "public-read"; public static final String ACL_PUBLIC_READ_WRITE = "public-read-write"; @@ -147,85 +158,90 @@ public static class ObsRequestParams{ public static final String ACL_BUCKET_OWNER_READ = "bucket-owner-read"; public static final String ACL_BUCKET_OWNER_FULL_CONTROL = "bucket-owner-full-control"; public static final String ACL_LOG_DELIVERY_WRITE = "log-delivery-write"; - + public static final String ALL_USERS_URI = "http://acs.amazonaws.com/groups/global/AllUsers"; public static final String AUTHENTICATED_USERS_URI = "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"; public static final String LOG_DELIVERY_URI = "http://acs.amazonaws.com/groups/s3/LogDelivery"; - + public static final String PERMISSION_FULL_CONTROL = "FULL_CONTROL"; - + public static final String PERMISSION_READ = "READ"; - + public static final String PERMISSION_WRITE = "WRITE"; - + public static final String PERMISSION_READ_ACP = "READ_ACP"; public static final String PERMISSION_WRITE_ACP = "WRITE_ACP"; - + public static final String PERMISSION_READ_OBJECT = "READ_OBJECT"; - + public static final String PERMISSION_FULL_CONTROL_OBJECT = "FULL_CONTROL_OBJECT"; public static final String DERECTIVE_COPY = "COPY"; - + public static final String DERECTIVE_REPLACE = "REPLACE"; - + public static final String DERECTIVE_REPLACE_NEW = "REPLACE_NEW"; - - public static final String RESULTCODE_SUCCESS = "0"; - + + public static final String RESULTCODE_SUCCESS = "0"; + public static final String SERVICE = "s3"; - + public static final String REQUEST_TAG = "aws4_request"; - + public static final String V4_ALGORITHM = "AWS4-HMAC-SHA256"; - + public static final String SHORT_DATE_FORMATTER = "yyyyMMdd"; - + public static final String LONG_DATE_FORMATTER = "yyyyMMdd'T'HHmmss'Z'"; - + public static final String HEADER_DATE_FORMATTER = "EEE, dd MMM yyyy HH:mm:ss z"; - + public static final String EXPIRATION_DATE_FORMATTER = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; - + public static final TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT"); - - public static final String OBS_SDK_VERSION = "3.19.12"; - + + public static final String OBS_SDK_VERSION = "3.22.3"; + public static final String USER_AGENT_VALUE = "obs-sdk-java/" + Constants.OBS_SDK_VERSION; - + public static final String DEFAULT_ENCODING = "UTF-8"; - + public static final String ISO_8859_1_ENCOING = "ISO-8859-1"; - + public static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; - + public static final String HMAC_SHA256_ALGORITHM = "HmacSHA256"; - + public static final String OBS_HEADER_PREFIX = "x-obs-"; - + public static final String OBS_HEADER_META_PREFIX = "x-obs-meta-"; - + public static final String V2_HEADER_PREFIX = "x-amz-"; - + public static final String V2_HEADER_META_PREFIX = "x-amz-meta-"; - + public static final String V2_HEADER_PREFIX_CAMEL = "X-Amz-"; - + + public static final List NOT_NEED_HEADER_PREFIXES = Arrays.asList(OBS_HEADER_META_PREFIX, + OBS_HEADER_PREFIX, V2_HEADER_META_PREFIX, V2_HEADER_PREFIX_CAMEL, V2_HEADER_PREFIX); + public static final String REQUEST_ID_HEADER = "request-id"; - + public static final String OEF_MARKER = "oef-marker"; - + + public static final String FS_SUMMARY_DIR_LIST = "fs-summary-dir-list"; + public static final String TRUE = "true"; - + public static final String FALSE = "false"; - + public static final String ENABLED = "Enabled"; - + public static final String DISABLED = "Disabled"; - + public static final String YES = "yes"; public static final String OBJECT = "OBJECT"; @@ -233,119 +249,41 @@ public static class ObsRequestParams{ public static final String PFS = "PFS"; public static final String POSIX = "POSIX"; - - - public static final List ALLOWED_RESPONSE_HTTP_HEADER_METADATA_NAMES = Collections.unmodifiableList(Arrays.asList( - "content-type", - "content-md5", - "content-length", - "content-language", - "expires", - "origin", - "cache-control", - "content-disposition", - "content-encoding", - "x-default-storage-class", - "location", - "date", - "etag", - "host", - "last-modified", - "content-range", - "x-reserved", - "x-reserved-indicator", - "access-control-allow-origin", - "access-control-allow-headers", - "access-control-max-age", - "access-control-allow-methods", - "access-control-expose-headers", - "connection", - "pragma" - )); - - public static final List ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES = Collections.unmodifiableList(Arrays.asList( - "content-type", - "content-md5", - "content-length", - "content-language", - "expires", - "origin", - "cache-control", - "content-disposition", - "content-encoding", - "access-control-request-method", - "access-control-request-headers", - "success-action-redirect", - "x-default-storage-class", - "location", - "date", - "etag", - "range", - "host", - "if-modified-since", - "if-unmodified-since", - "if-match", - "if-none-match", - "last-modified", - "content-range", - "x-cache-control", - "x-obs-bucket-type")); - - - public static final List ALLOWED_RESOURCE_PARAMTER_NAMES = Collections.unmodifiableList(Arrays.asList( - "acl", - "backtosource", - "policy", - "torrent", - "logging", - "location", - "storageinfo", - "quota", - "storagepolicy", - "storageclass", - "requestpayment", - "versions", - "versioning", - "versionid", - "uploads", - "uploadid", - "partnumber", - "website", - "notification", - "lifecycle", - "deletebucket", - "delete", - "cors", - "restore", - "tagging", - "replication", - "metadata", - "encryption", - "directcoldaccess", - /** - * File System API - */ - "append", - "position", - "truncate", - "modify", - "rename", - "length", - "name", - "fileinterface", - - "readahead", - - "response-content-type", - "response-content-language", - "response-expires", - "response-cache-control", - "response-content-disposition", - "response-content-encoding", - "x-image-save-bucket", - "x-image-save-object", - "x-image-process", - "x-obs-sse-kms-key-project-id", - "x-oss-process")); - + + public static final long MAX_PART_SIZE = 5 * 1024 * 1024 * 1024L; + + public static final long MIN_PART_SIZE = 100 * 1024L; + + public static final List ALLOWED_RESPONSE_HTTP_HEADER_METADATA_NAMES = Collections.unmodifiableList( + Arrays.asList("content-type", "content-md5", "content-length", "content-language", "expires", "origin", + "cache-control", "content-disposition", "content-encoding", "x-default-storage-class", "location", + "date", "etag", "host", "last-modified", "content-range", "x-reserved", "x-reserved-indicator", + "access-control-allow-origin", "access-control-allow-headers", "access-control-max-age", + "access-control-allow-methods", "access-control-expose-headers", "connection", "pragma")); + + public static final List ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES = Collections.unmodifiableList( + Arrays.asList("content-type", "content-md5", "content-length", "content-language", "expires", "origin", + "cache-control", "content-disposition", "content-encoding", "access-control-request-method", + "access-control-request-headers", "success-action-redirect", "x-default-storage-class", "location", + "date", "etag", "range", "host", "if-modified-since", "if-unmodified-since", "if-match", + "if-none-match", "last-modified", "content-range", "x-cache-control", "x-obs-bucket-type", + "accept-encoding", "x-amz-fs-summary-dir-list", "x-obs-fs-summary-dir-list", "x-obs-callback", + "x-amz-callback", "x-obs-hash-crc64ecma", "x-amz-hash-crc64ecma")); + + public static final List ALLOWED_RESOURCE_PARAMTER_NAMES = Collections.unmodifiableList( + Arrays.asList("acl", "backtosource", "policy", "torrent", "logging", "location", "storageinfo", "quota", + "storagepolicy", "storageclass", "requestpayment", "versions", "versioning", "versionid", "uploads", + "uploadid", "partnumber", "website", "notification", "lifecycle", "deletebucket", "delete", "cors", + "restore", "tagging", "replication", "metadata", "encryption", "directcoldaccess", "mirrorrefresh", + "mirrorbacktosource", + /** + * File System API + */ + "append", "position", "truncate", "modify", "rename", "length", "name", "fileinterface", + "readahead", "response-content-type", "response-content-language", "response-expires", + "response-cache-control", "response-content-disposition", "response-content-encoding", + "x-image-save-bucket", "x-image-save-object", "x-image-process", "x-obs-sse-kms-key-project-id", + "x-oss-process", "ignore-sign-in-query", "listcontentsummary", "multilistcontentsummary", + "getcontentsummary")); + } diff --git a/app/src/main/java/com/obs/services/internal/DefaultProgressStatus.java b/app/src/main/java/com/obs/services/internal/DefaultProgressStatus.java index 81b2a8f..6c01149 100644 --- a/app/src/main/java/com/obs/services/internal/DefaultProgressStatus.java +++ b/app/src/main/java/com/obs/services/internal/DefaultProgressStatus.java @@ -3,14 +3,15 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; import java.util.List; @@ -18,76 +19,89 @@ import com.obs.services.internal.ProgressManager.BytesUnit; import com.obs.services.model.ProgressStatus; -public class DefaultProgressStatus implements ProgressStatus{ +public class DefaultProgressStatus implements ProgressStatus { private final long newlyTransferredBytes; private final long transferredBytes; private final long totalBytes; private final long intervalMilliseconds; private final long totalMilliseconds; + private long instantaneousSpeed; private List instantaneousBytes; - + public DefaultProgressStatus(long newlyTransferredBytes, long transferredBytes, long totalBytes, - long intervalMilliseconds, long totalMilliseconds) { - this.newlyTransferredBytes = newlyTransferredBytes; - this.transferredBytes = transferredBytes; - this.totalBytes = totalBytes; - this.intervalMilliseconds = intervalMilliseconds; - this.totalMilliseconds = totalMilliseconds; + long intervalMilliseconds, long totalMilliseconds) { + this.newlyTransferredBytes = newlyTransferredBytes; + this.transferredBytes = transferredBytes; + this.totalBytes = totalBytes; + this.intervalMilliseconds = intervalMilliseconds; + this.totalMilliseconds = totalMilliseconds; + } + + @Override + public double getInstantaneousSpeed() { + if (this.intervalMilliseconds <= 0) { + return -1d; + } + return this.instantaneousSpeed; } - - @Override - public double getInstantaneousSpeed() { - if(this.instantaneousBytes != null) { - long instantaneousSpeed = 0; - for (BytesUnit item : this.instantaneousBytes) - { - instantaneousSpeed += item.bytes; + + @Deprecated + public double getOldInstantaneousSpeed() { + if (this.instantaneousBytes != null) { + long oldInstantaneousSpeed = 0; + for (BytesUnit item : this.instantaneousBytes) { + oldInstantaneousSpeed += item.bytes; } - return instantaneousSpeed; - } - - if(this.intervalMilliseconds <= 0) { - return -1d; - } - return this.newlyTransferredBytes * 1000.0d / this.intervalMilliseconds; - } - - @Override - public double getAverageSpeed() { - if(this.totalMilliseconds <= 0) { - return -1d; - } - return this.transferredBytes * 1000.0d / this.totalMilliseconds; - } - - @Override - public int getTransferPercentage() { - if(this.totalBytes < 0) { - return -1; - }else if(this.totalBytes == 0) { - return 100; - } - return (int)(this.transferredBytes * 100 / this.totalBytes); - } - - @Override - public long getNewlyTransferredBytes() { - return this.newlyTransferredBytes; - } - - @Override - public long getTransferredBytes() { - return this.transferredBytes; - } - - @Override - public long getTotalBytes() { - return this.totalBytes; - } - - public void setInstantaneousBytes(List instantaneousBytes) { - this.instantaneousBytes = instantaneousBytes; - } + return oldInstantaneousSpeed; + } + + if (this.intervalMilliseconds <= 0) { + return -1d; + } + return this.newlyTransferredBytes * 1000.0d / this.intervalMilliseconds; + } + + @Override + public double getAverageSpeed() { + if (this.totalMilliseconds <= 0) { + return -1d; + } + return this.transferredBytes * 1000.0d / this.totalMilliseconds; + } + + @Override + public int getTransferPercentage() { + if (this.totalBytes < 0) { + return -1; + } else if (this.totalBytes == 0) { + return 100; + } + return (int) (this.transferredBytes * 100 / this.totalBytes); + } + + @Override + public long getNewlyTransferredBytes() { + return this.newlyTransferredBytes; + } + + @Override + public long getTransferredBytes() { + return this.transferredBytes; + } + + @Override + public long getTotalBytes() { + return this.totalBytes; + } + + @Deprecated + public void setInstantaneousBytes(List instantaneousBytes) { + this.instantaneousBytes = instantaneousBytes; + } + + public void setInstantaneousSpeed(long instantaneousSpeed) { + this.instantaneousSpeed = instantaneousSpeed; + } } diff --git a/app/src/main/java/com/obs/services/internal/DownloadResumableClient.java b/app/src/main/java/com/obs/services/internal/DownloadResumableClient.java new file mode 100644 index 0000000..22cc030 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/DownloadResumableClient.java @@ -0,0 +1,948 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.internal; + +import java.io.Closeable; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.io.RandomAccessFile; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.AbstractClient; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.io.ProgressInputStream; +import com.obs.services.internal.utils.SecureObjectInputStream; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.DownloadFileRequest; +import com.obs.services.model.DownloadFileResult; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.GetObjectRequest; +import com.obs.services.model.MonitorableProgressListener; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.ObsObject; + +public class DownloadResumableClient { + + private static final ILogger log = LoggerBuilder.getLogger("com.obs.services.ObsClient"); + private AbstractClient obsClient; + + public DownloadResumableClient(AbstractClient obsClient) { + this.obsClient = obsClient; + } + + public DownloadFileResult downloadFileResume(DownloadFileRequest downloadFileRequest) { + ServiceUtils.assertParameterNotNull(downloadFileRequest, "DownloadFileRequest is null"); + ServiceUtils.assertParameterNotNull(downloadFileRequest.getBucketName(), "the bucketName is null"); + String key = downloadFileRequest.getObjectKey(); + ServiceUtils.assertParameterNotNull2(key, "the objectKey is null"); + + if (downloadFileRequest.getDownloadFile() == null) { + downloadFileRequest.setDownloadFile(key); + } + if (downloadFileRequest.isEnableCheckpoint()) { + if (downloadFileRequest.getCheckpointFile() == null || downloadFileRequest.getCheckpointFile().isEmpty()) { + downloadFileRequest.setCheckpointFile(downloadFileRequest.getDownloadFile() + ".downloadFile_record"); + } + } + try { + return downloadCheckPoint(downloadFileRequest); + } catch (ObsException e) { + throw e; + } catch (ServiceException e) { + throw ServiceUtils.changeFromServiceException(e); + } catch (Exception e) { + throw new ObsException(e.getMessage(), e); + } + } + + private DownloadFileResult downloadCheckPoint(DownloadFileRequest downloadFileRequest) throws Exception { + + ObjectMetadata objectMetadata = getObjectMetadata(downloadFileRequest); + + DownloadFileResult downloadFileResult = new DownloadFileResult(); + downloadFileResult.setObjectMetadata(objectMetadata); + + if (objectMetadata.getContentLength() == 0) { + ServiceUtils.deleteFileIgnoreException(downloadFileRequest.getTempDownloadFile()); + ServiceUtils.deleteFileIgnoreException(downloadFileRequest.getCheckpointFile()); + + File dfile = new File(downloadFileRequest.getDownloadFile()); + if (!dfile.getParentFile().mkdirs()) { + if (log.isWarnEnabled()) { + log.warn("create parent directory failed."); + } + } + new RandomAccessFile(dfile, "rw").close(); + if (downloadFileRequest.getProgressListener() != null) { + downloadFileRequest.getProgressListener().progressChanged(new DefaultProgressStatus(0, 0, 0, 0, 0)); + } + return downloadFileResult; + } + + DownloadCheckPoint downloadCheckPoint = new DownloadCheckPoint(); + if (downloadFileRequest.isEnableCheckpoint()) { + boolean needRecreate = false; + try { + downloadCheckPoint.load(downloadFileRequest.getCheckpointFile()); + } catch (Exception e) { + needRecreate = true; + } + if (!needRecreate) { + if (!(downloadFileRequest.getBucketName().equals(downloadCheckPoint.bucketName) + && downloadFileRequest.getObjectKey().equals(downloadCheckPoint.objectKey) + && downloadFileRequest.getDownloadFile().equals(downloadCheckPoint.downloadFile))) { + needRecreate = true; + } else if (!downloadCheckPoint.isValid(downloadFileRequest.getTempDownloadFile(), objectMetadata)) { + needRecreate = true; + } else if (downloadFileRequest.getVersionId() == null) { + if (downloadCheckPoint.versionId != null) { + needRecreate = true; + } + } else if (!downloadFileRequest.getVersionId().equals(downloadCheckPoint.versionId)) { + needRecreate = true; + } + } + if (needRecreate) { + if (downloadCheckPoint.tmpFileStatus != null) { + ServiceUtils.deleteFileIgnoreException(downloadCheckPoint.tmpFileStatus.tmpFilePath); + } + + ServiceUtils.deleteFileIgnoreException(downloadFileRequest.getCheckpointFile()); + + prepare(downloadFileRequest, downloadCheckPoint, objectMetadata); + } + } else { + prepare(downloadFileRequest, downloadCheckPoint, objectMetadata); + } + + // 并发下载分片 + DownloadResult downloadResult = this.download(downloadCheckPoint, downloadFileRequest); + checkDownloadResult(downloadFileRequest, downloadCheckPoint, downloadResult); + + // 重命名临时文件 + renameTo(downloadFileRequest.getTempDownloadFile(), downloadFileRequest.getDownloadFile()); + + // 开启了断点下载,成功上传后删除checkpoint文件 + if (downloadFileRequest.isEnableCheckpoint()) { + ServiceUtils.deleteFileIgnoreException(downloadFileRequest.getCheckpointFile()); + } + + return downloadFileResult; + } + + private void checkDownloadResult(DownloadFileRequest downloadFileRequest, DownloadCheckPoint downloadCheckPoint, + DownloadResult downloadResult) throws Exception { + for (PartResultDown partResult : downloadResult.getPartResults()) { + if (partResult.isFailed() && partResult.getException() != null) { + if (!downloadFileRequest.isEnableCheckpoint()) { + ServiceUtils.deleteFileIgnoreException(downloadCheckPoint.tmpFileStatus.tmpFilePath); + } else if (downloadCheckPoint.isAbort) { + ServiceUtils.deleteFileIgnoreException(downloadCheckPoint.tmpFileStatus.tmpFilePath); + ServiceUtils.deleteFileIgnoreException(downloadFileRequest.getCheckpointFile()); + } + throw partResult.getException(); + } + } + } + + private ObjectMetadata getObjectMetadata(DownloadFileRequest downloadFileRequest) { + ObjectMetadata objectMetadata; + try { + GetObjectMetadataRequest request = new GetObjectMetadataRequest(downloadFileRequest.getBucketName(), + downloadFileRequest.getObjectKey(), downloadFileRequest.getVersionId()); + request.setRequesterPays(downloadFileRequest.isRequesterPays()); + request.setIsEncodeHeaders(downloadFileRequest.isEncodeHeaders()); + objectMetadata = this.obsClient.getObjectMetadata(request); + } catch (ObsException e) { + if (e.getResponseCode() >= 300 && e.getResponseCode() < 500 && e.getResponseCode() != 408) { + ServiceUtils.deleteFileIgnoreException(downloadFileRequest.getTempDownloadFile()); + ServiceUtils.deleteFileIgnoreException(downloadFileRequest.getCheckpointFile()); + } + throw e; + } + return objectMetadata; + } + + private void renameTo(String tempDownloadFilePath, String downloadFilePath) throws IOException { + File tmpfile = new File(tempDownloadFilePath); + File downloadFile = new File(downloadFilePath); + if (!tmpfile.exists()) { + throw new FileNotFoundException("tmpFile '" + tmpfile + "' does not exist"); + } + if (downloadFile.exists()) { + if (!downloadFile.delete()) { + throw new IOException("downloadFile '" + downloadFile + "' is exist"); + } + } + if (tmpfile.isDirectory() || downloadFile.isDirectory()) { + throw new IOException("downloadPath is a directory"); + } + final boolean renameFlag = tmpfile.renameTo(downloadFile); + if (!renameFlag) { + InputStream input = null; + OutputStream output = null; + try { + input = new FileInputStream(tmpfile); + output = new FileOutputStream(downloadFile); + byte[] buffer = new byte[1024 * 8]; + int length; + while ((length = input.read(buffer)) > 0) { + output.write(buffer, 0, length); + } + } finally { + if (null != input) { + try { + input.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + + if (null != output) { + try { + output.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + } + if (!tmpfile.delete()) { + if (log.isErrorEnabled()) { + log.error("the tmpfile '" + tmpfile + + "' can not delete, please delete it to ensure the download finish."); + } + throw new IOException("the tmpfile '" + tmpfile + + "' can not delete, please delete it to ensure the download finish."); + } + } + } + + private DownloadResult download(final DownloadCheckPoint downloadCheckPoint, + final DownloadFileRequest downloadFileRequest) throws Exception { + ArrayList taskResults = new ArrayList(); + DownloadResult downloadResult = new DownloadResult(); + ArrayList> futures = new ArrayList>(); + + List unfinishedTasks = new LinkedList(); + long transferredBytes = 0L; + for (int i = 0; i < downloadCheckPoint.downloadParts.size(); i++) { + DownloadPart downloadPart = downloadCheckPoint.downloadParts.get(i); + if (!downloadPart.isCompleted) { + Task task = new Task(i, "download-" + i, downloadCheckPoint, i, downloadFileRequest, this.obsClient); + unfinishedTasks.add(task); + } else { + transferredBytes += downloadPart.end - downloadPart.offset + 1; + taskResults.add(new PartResultDown(i + 1, downloadPart.offset, downloadPart.end)); + } + } + + ProgressManager progressManager = null; + if (null != downloadFileRequest.getProgressListener()) { + progressManager = new ConcurrentProgressManager(downloadCheckPoint.objectStatus.size, transferredBytes, + downloadFileRequest.getProgressListener(), downloadFileRequest.getProgressInterval() > 0 + ? downloadFileRequest.getProgressInterval() : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); + } + + ExecutorService service = Executors.newFixedThreadPool(downloadFileRequest.getTaskNum()); + for (Task task : unfinishedTasks) { + task.setProgressManager(progressManager); + futures.add(service.submit(task)); + } + + service.shutdown(); + List notStartTasks = null; + try { + service.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); + } catch (InterruptedException ie) { + notStartTasks = service.shutdownNow(); + Thread.currentThread().interrupt(); + throw ie; + } finally { + if (null != notStartTasks) { + if (log.isWarnEnabled()) { + log.warn("there are still " + notStartTasks.size() + " tasks not started for request : " + + downloadFileRequest); + } + } + } + + for (Future future : futures) { + try { + PartResultDown tr = future.get(); + taskResults.add(tr); + } catch (ExecutionException e) { + throw e; + } + } + + downloadResult.setPartResults(taskResults); + if (progressManager != null) { + progressManager.progressEnd(); + } + + return downloadResult; + } + + static class Task implements Callable { + + private int id; + private String name; + private DownloadCheckPoint downloadCheckPoint; + private int partIndex; + private final DownloadFileRequest downloadFileRequest; + private AbstractClient obsClient; + private ProgressManager progressManager; + + public Task(int id, String name, DownloadCheckPoint downloadCheckPoint, int partIndex, + DownloadFileRequest downloadFileRequest, AbstractClient obsClient) { + if (null == downloadCheckPoint + || null == downloadFileRequest + || null == obsClient) { + log.warn("some parameters is null. { " + downloadCheckPoint + + ", " + downloadFileRequest + ", " + obsClient + " }"); + throw new IllegalArgumentException("some parameters is null."); + } + this.id = id; + this.name = name; + this.downloadCheckPoint = downloadCheckPoint; + this.partIndex = partIndex; + this.downloadFileRequest = downloadFileRequest; + this.obsClient = obsClient; + } + + @Override + public PartResultDown call() throws Exception { + DownloadPart downloadPart = downloadCheckPoint.downloadParts.get(partIndex); + PartResultDown tr = new PartResultDown(partIndex + 1, downloadPart.offset, downloadPart.end); + + if (downloadCheckPoint.isAbort) { + tr.setFailed(true); + return tr; + } + + RandomAccessFile output = null; + InputStream content = null; + + try { + if (log.isDebugEnabled()) { + log.debug("start task : " + downloadPart.toString()); + } + + // 标记启动一个子任务 + startOneTask(downloadFileRequest); + + output = new RandomAccessFile(downloadFileRequest.getTempDownloadFile(), "rw"); + output.seek(downloadPart.offset); + + GetObjectRequest getObjectRequest = createNewGetObjectRequest(downloadFileRequest, downloadPart); + getObjectRequest.setIsEncodeHeaders(downloadFileRequest.isEncodeHeaders()); + + ObsObject object = obsClient.getObject(getObjectRequest); + content = object.getObjectContent(); + if (this.progressManager != null) { + content = new ProgressInputStream(content, this.progressManager, false); + } + + byte[] buffer = new byte[ObsConstraint.DEFAULT_CHUNK_SIZE]; + int bytesOffset; + while ((bytesOffset = content.read(buffer)) != -1) { + output.write(buffer, 0, bytesOffset); + } + downloadCheckPoint.update(partIndex, true, downloadFileRequest.getTempDownloadFile()); + } catch (ObsException e) { + if (e.getResponseCode() >= 300 && e.getResponseCode() < 500 && e.getResponseCode() != 408) { + downloadCheckPoint.isAbort = true; + } + signPartResultFailed(tr, e); + if (log.isErrorEnabled()) { + log.error(String.format("Task %d:%s download part %d failed: ", id, name, partIndex), e); + } + } catch (Exception e) { + signPartResultFailed(tr, e); + if (log.isErrorEnabled()) { + log.error(String.format("Task %d:%s download part %d failed: ", id, name, partIndex), e); + } + } finally { + // 结束一个子任务 + finishOneTask(downloadFileRequest); + + closeResource(output); + + closeResource(content); + + if (log.isDebugEnabled()) { + log.debug("end task : " + downloadPart.toString()); + } + + if (downloadFileRequest.isEnableCheckpoint()) { + downloadCheckPoint.updateTmpFile(downloadFileRequest.getTempDownloadFile()); + downloadCheckPoint.record(downloadFileRequest.getCheckpointFile()); + } + } + return tr; + } + + private void closeResource(Closeable resource) { + if (null != resource) { + try { + resource.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + } + + private void finishOneTask(DownloadFileRequest downloadFileRequest) { + if (null != downloadFileRequest.getProgressListener() + && downloadFileRequest.getProgressListener() instanceof MonitorableProgressListener) { + ((MonitorableProgressListener) downloadFileRequest.getProgressListener()).finishOneTask(); + } + } + + private void startOneTask(DownloadFileRequest downloadFileRequest) { + if (null != downloadFileRequest.getProgressListener() + && downloadFileRequest.getProgressListener() instanceof MonitorableProgressListener) { + ((MonitorableProgressListener) downloadFileRequest.getProgressListener()).startOneTask(); + } + } + + private void signPartResultFailed(PartResultDown tr, Exception e) { + tr.setFailed(true); + tr.setException(e); + } + + private GetObjectRequest createNewGetObjectRequest(DownloadFileRequest downloadFileRequest, + DownloadPart downloadPart) { + GetObjectRequest getObjectRequest = new GetObjectRequest(downloadFileRequest.getBucketName(), + downloadFileRequest.getObjectKey(), downloadFileRequest.getVersionId()); + + getObjectRequest.setRequesterPays(downloadFileRequest.isRequesterPays()); + getObjectRequest.setIfMatchTag(downloadFileRequest.getIfMatchTag()); + getObjectRequest.setIfNoneMatchTag(downloadFileRequest.getIfNoneMatchTag()); + getObjectRequest.setIfModifiedSince(downloadFileRequest.getIfModifiedSince()); + getObjectRequest.setIfUnmodifiedSince(downloadFileRequest.getIfUnmodifiedSince()); + getObjectRequest.setRangeStart(downloadPart.offset); + getObjectRequest.setRangeEnd(downloadPart.end); + getObjectRequest.setCacheOption(downloadFileRequest.getCacheOption()); + getObjectRequest.setTtl(downloadFileRequest.getTtl()); + return getObjectRequest; + } + + public void setProgressManager(ProgressManager progressManager) { + this.progressManager = progressManager; + } + } + + private void prepare(DownloadFileRequest downloadFileRequest, DownloadCheckPoint downloadCheckPoint, + ObjectMetadata objectMetadata) throws Exception { + downloadCheckPoint.bucketName = downloadFileRequest.getBucketName(); + downloadCheckPoint.objectKey = downloadFileRequest.getObjectKey(); + downloadCheckPoint.versionId = downloadFileRequest.getVersionId(); + downloadCheckPoint.downloadFile = downloadFileRequest.getDownloadFile(); + ObjectStatus objStatus = new ObjectStatus(); + objStatus.size = objectMetadata.getContentLength(); + objStatus.lastModified = objectMetadata.getLastModified(); + objStatus.etag = objectMetadata.getEtag(); + downloadCheckPoint.objectStatus = objStatus; + downloadCheckPoint.downloadParts = splitObject(downloadCheckPoint.objectStatus.size, + downloadFileRequest.getPartSize()); + File tmpfile = new File(downloadFileRequest.getTempDownloadFile()); + if (null != tmpfile.getParentFile()) { + if (!tmpfile.getParentFile().mkdirs()) { + if (log.isWarnEnabled()) { + log.warn("create parent directory for tempfile failed."); + } + } + } + RandomAccessFile randomAccessFile = null; + try { + randomAccessFile = new RandomAccessFile(tmpfile, "rw"); + randomAccessFile.setLength(downloadCheckPoint.objectStatus.size); + } finally { + if (null != randomAccessFile) { + try { + randomAccessFile.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + } + downloadCheckPoint.tmpFileStatus = new TmpFileStatus(downloadCheckPoint.objectStatus.size, + new Date(tmpfile.lastModified()), downloadFileRequest.getTempDownloadFile()); + + if (downloadFileRequest.isEnableCheckpoint()) { + try { + downloadCheckPoint.record(downloadFileRequest.getCheckpointFile()); + } catch (Exception e) { + ServiceUtils.deleteFileIgnoreException(tmpfile); + throw e; + } + } + } + + private ArrayList splitObject(long size, long partSize) { + ArrayList parts = new ArrayList(); + + long piece = size / partSize; + if (piece >= 10000) { + partSize = size % 10000 == 0 ? size / 10000 : size / 10000 + 1; + } + + long offset = 0L; + for (int i = 0; offset < size; offset += partSize, i++) { + DownloadPart downloadPart = new DownloadPart(); + downloadPart.partNumber = i; + downloadPart.offset = offset; + if (offset + partSize > size) { + downloadPart.end = size - 1; + } else { + downloadPart.end = offset + partSize - 1; + } + parts.add(downloadPart); + } + return parts; + } + + /** + * 断点续传的下载所需类 + */ + static class DownloadCheckPoint implements Serializable { + private static final long serialVersionUID = 2282950186694419179L; + + public int md5; + public String bucketName; + public String objectKey; + public String versionId; + public String downloadFile; + public ObjectStatus objectStatus; + public TmpFileStatus tmpFileStatus; + ArrayList downloadParts; + public transient volatile boolean isAbort = false; + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((bucketName == null) ? 0 : bucketName.hashCode()); + result = prime * result + ((downloadFile == null) ? 0 : downloadFile.hashCode()); + result = prime * result + ((versionId == null) ? 0 : versionId.hashCode()); + result = prime * result + ((objectKey == null) ? 0 : objectKey.hashCode()); + result = prime * result + ((objectStatus == null) ? 0 : objectStatus.hashCode()); + result = prime * result + ((tmpFileStatus == null) ? 0 : tmpFileStatus.hashCode()); + result = prime * result + ((downloadParts == null) ? 0 : downloadParts.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } else { + if (obj instanceof DownloadCheckPoint) { + DownloadCheckPoint downloadCheckPoint = (DownloadCheckPoint) obj; + if (downloadCheckPoint.hashCode() == this.hashCode()) { + return true; + } + } + } + return false; + } + + /** + * 从checkpoint文件中加载checkpoint数据 + * + * @param checkPointFile + * @throws Exception + */ + public void load(String checkPointFile) throws Exception { + FileInputStream fileIn = null; + SecureObjectInputStream in = null; + try { + fileIn = new FileInputStream(checkPointFile); + in = new SecureObjectInputStream(fileIn); + DownloadCheckPoint info = (DownloadCheckPoint) in.readObject(); + assign(info); + } finally { + if (null != in) { + try { + in.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + + if (null != fileIn) { + try { + fileIn.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + } + } + + private void assign(DownloadCheckPoint info) { + this.md5 = info.md5; + this.downloadFile = info.downloadFile; + this.bucketName = info.bucketName; + this.objectKey = info.objectKey; + this.versionId = info.versionId; + this.objectStatus = info.objectStatus; + this.tmpFileStatus = info.tmpFileStatus; + this.downloadParts = info.downloadParts; + } + + /** + * 判断序列化文件、临时文件和实际信息是否一致 + * + * @param tmpFilePath + * 临时文件路径 + * @param objectMetadata + * 对象元数据 + */ + public boolean isValid(String tmpFilePath, ObjectMetadata objectMetadata) { + if (this.md5 != hashCode()) { + return false; + } + if (objectMetadata.getContentLength() != this.objectStatus.size + || !objectMetadata.getLastModified().equals(this.objectStatus.lastModified) + || !objectMetadata.getEtag().equals(this.objectStatus.etag)) { + return false; + } + + File tmpfile = new File(tmpFilePath); + return this.tmpFileStatus.size == tmpfile.length(); + } + + /** + * 分片下载成功后,更新分片和临时文件信息 + * + * @param index + * @param completed + * @param tmpFilePath + * @throws IOException + */ + public synchronized void update(int index, boolean completed, String tmpFilePath) throws IOException { + downloadParts.get(index).isCompleted = completed; + File tmpfile = new File(tmpFilePath); + this.tmpFileStatus.lastModified = new Date(tmpfile.lastModified()); + } + + /** + * 出现网络异常时,更新临时文件的修改时间 + * + * @param tmpFilePath + * @throws IOException + */ + public synchronized void updateTmpFile(String tmpFilePath) throws IOException { + File tmpfile = new File(tmpFilePath); + this.tmpFileStatus.lastModified = new Date(tmpfile.lastModified()); + } + + /** + * 把DownloadCheckPoint数据写到序列化文件 + * + * @throws IOException + */ + public synchronized void record(String checkPointFilePath) throws IOException { + FileOutputStream fileOutStream = null; + ObjectOutputStream objOutStream = null; + this.md5 = hashCode(); + try { + fileOutStream = new FileOutputStream(checkPointFilePath); + objOutStream = new ObjectOutputStream(fileOutStream); + objOutStream.writeObject(this); + } finally { + if (objOutStream != null) { + try { + objOutStream.close(); + } catch (Exception e) { + log.warn("close outputstrem failed.", e); + } + } + if (fileOutStream != null) { + try { + fileOutStream.close(); + } catch (Exception e) { + log.warn("close outputstrem failed.", e); + } + } + } + } + } + + static class ObjectStatus implements Serializable { + + private static final long serialVersionUID = -6267040832855296342L; + + public long size; // 桶中对象大小 + public Date lastModified; // 对象的最后修改时间 + public String etag; // 对象的Etag + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((etag == null) ? 0 : etag.hashCode()); + result = prime * result + ((lastModified == null) ? 0 : lastModified.hashCode()); + result = prime * result + (int) (size ^ (size >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } else { + if (obj instanceof ObjectStatus) { + ObjectStatus objectStatus = (ObjectStatus) obj; + return objectStatus.hashCode() == this.hashCode(); + } + } + return false; + } + } + + static class TmpFileStatus implements Serializable { + private static final long serialVersionUID = 4478330948103112660L; + + public long size; // 对象大小 + public Date lastModified; // 对象的最后修改时间 + public String tmpFilePath; + + public TmpFileStatus(long size, Date lastMoidified, String tmpFilePath) { + this.size = size; + this.lastModified = lastMoidified; + this.tmpFilePath = tmpFilePath; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((lastModified == null) ? 0 : lastModified.hashCode()); + result = prime * result + ((tmpFilePath == null) ? 0 : tmpFilePath.hashCode()); + result = prime * result + (int) (size ^ (size >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } else { + if (obj instanceof TmpFileStatus) { + TmpFileStatus tmpFileStatus = (TmpFileStatus) obj; + return tmpFileStatus.hashCode() == this.hashCode(); + } + } + return false; + } + } + + static class DownloadPart implements Serializable { + + private static final long serialVersionUID = 961987949814206093L; + + public int partNumber; // 分片序号,从0开始编号 + public long offset; // 分片起始位置 + public long end; // 分片片结束位置 + public boolean isCompleted; // 该分片下载是否完成 + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + partNumber; + result = prime * result + (isCompleted ? 0 : 8); + result = prime * result + (int) (end ^ (end >>> 32)); + result = prime * result + (int) (offset ^ (offset >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } else { + if (obj instanceof DownloadPart) { + DownloadPart downloadPart = (DownloadPart) obj; + return downloadPart.hashCode() == this.hashCode(); + } + } + return false; + } + + @Override + public String toString() { + return "DownloadPart [partNumber=" + partNumber + ", offset=" + offset + ", end=" + end + ", isCompleted=" + + isCompleted + "]"; + } + } + + static class PartResultDown { + private int partNumber; // 分片序号,从1开始编号 + private long start; // 分片开始位置 + private long end; // 分片结束位置 + private boolean isFailed; // 分片上传是否失败 + private Exception exception; // 分片上传异常 + + public PartResultDown(int partNumber, long start, long end) { + this.partNumber = partNumber; + this.start = start; + this.end = end; + } + + /** + * 获取分片的起始位置 + * + * @return 分片的起始位置 + */ + public long getStart() { + return start; + } + + /** + * 设置分片的起始位置 + * + * @param start + * 分片起始位置 + */ + public void setStart(long start) { + this.start = start; + } + + /** + * 获取分片的结束位置 + * + * @return 分片的结束位置 + */ + public long getEnd() { + return end; + } + + /** + * 设置分片的结束位置 + * + * @param end + * 分片结束位置 + */ + public void setEnd(long end) { + this.end = end; + } + + /** + * 获取分片的编号 + * + * @return 分片的编号 + */ + public int getpartNumber() { + return partNumber; + } + + /** + * 获取分片的下载状态 + * + * @return 分片的下载状态 + */ + public boolean isFailed() { + return isFailed; + } + + /** + * 设置分片的下载状态 + * + * @param failed + * 分片的下载状态 + */ + public void setFailed(boolean failed) { + this.isFailed = failed; + } + + /** + * 获取分片的下载异常 + * + * @return 分片的下载异常 + */ + public Exception getException() { + return exception; + } + + /** + * 设置分片的下载异常 + * + * @param exception + * 分片的下载异常 + */ + public void setException(Exception exception) { + this.exception = exception; + } + } + + static class DownloadResult { + + private List partResults; + + /** + * 获取分片的上传最终结果 + * + * @return 分片的上传汇总结果 + */ + public List getPartResults() { + return partResults; + } + + /** + * 设置分片的上传最终结果 + * + * @param partResults + * 分片的上传汇总结果 + */ + public void setPartResults(List partResults) { + this.partResults = partResults; + } + + } +} diff --git a/app/src/main/java/com/obs/services/internal/IConvertor.java b/app/src/main/java/com/obs/services/internal/IConvertor.java index 72c442d..1ffcfb5 100644 --- a/app/src/main/java/com/obs/services/internal/IConvertor.java +++ b/app/src/main/java/com/obs/services/internal/IConvertor.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; import java.util.List; @@ -37,49 +38,52 @@ public interface IConvertor { - String transCompleteMultipartUpload(List parts) throws ServiceException; + String transCompleteMultipartUpload(List parts) throws ServiceException; + + String transBucketLoction(String location) throws ServiceException; + + String transVersioningConfiguration(String bucketName, String status) throws ServiceException; + + String transRequestPaymentConfiguration(String bucketName, String payer) throws ServiceException; + + String transLifecycleConfiguration(LifecycleConfiguration config) throws ServiceException; + + String transWebsiteConfiguration(WebsiteConfiguration config) throws ServiceException; + + String transRestoreObjectRequest(RestoreObjectRequest req) throws ServiceException; + + String transBucketQuota(BucketQuota quota) throws ServiceException; + + String transBucketEcryption(BucketEncryption encryption) throws ServiceException; + + String transStoragePolicy(BucketStoragePolicyConfiguration status) throws ServiceException; + + String transBucketLoggingConfiguration(BucketLoggingConfiguration c) throws ServiceException; - String transBucketLoction(String location) throws ServiceException; + String transBucketCors(BucketCors cors) throws ServiceException; - String transVersioningConfiguration(String bucketName, String status) throws ServiceException; + String transAccessControlList(AccessControlList acl, boolean isBucket) throws ServiceException; - String transLifecycleConfiguration(LifecycleConfiguration config) throws ServiceException; + String transKeyAndVersion(KeyAndVersion[] objectNameAndVersions, boolean isQuiet, String encodingType) + throws ServiceException; - String transWebsiteConfiguration(WebsiteConfiguration config) throws ServiceException; + String transBucketTagInfo(BucketTagInfo bucketTagInfo) throws ServiceException; - String transRestoreObjectRequest(RestoreObjectRequest req) throws ServiceException; + String transBucketNotificationConfiguration(BucketNotificationConfiguration bucketNotificationConfiguration) + throws ServiceException; - String transBucketQuota(BucketQuota quota) throws ServiceException; - - String transBucketEcryption(BucketEncryption encryption) throws ServiceException; + String transReplicationConfiguration(ReplicationConfiguration replicationConfiguration) throws ServiceException; - String transStoragePolicy(BucketStoragePolicyConfiguration status) throws ServiceException; + String transBucketFileInterface(FSStatusEnum status) throws ServiceException; - String transBucketLoggingConfiguration(BucketLoggingConfiguration c) throws ServiceException; + String transStorageClass(StorageClassEnum storageClass); - String transBucketCors(BucketCors cors) throws ServiceException; + String transEventType(EventTypeEnum eventType); - String transAccessControlList(AccessControlList acl, boolean isBucket) throws ServiceException; - - String transKeyAndVersion(KeyAndVersion[] objectNameAndVersions, boolean isQuiet) throws ServiceException; + String transGroupGrantee(GroupGranteeEnum groupGrantee); - String transBucketTagInfo(BucketTagInfo bucketTagInfo) throws ServiceException; + AccessControlList transCannedAcl(String cannedAcl); - String transBucketNotificationConfiguration(BucketNotificationConfiguration bucketNotificationConfiguration) - throws ServiceException; + String transBucketDirectColdAccess(BucketDirectColdAccess access) throws ServiceException; - String transReplicationConfiguration(ReplicationConfiguration replicationConfiguration) throws ServiceException; - - String transBucketFileInterface(FSStatusEnum status) throws ServiceException; - - String transStorageClass(StorageClassEnum storageClass); - - String transEventType(EventTypeEnum eventType); - - String transGroupGrantee(GroupGranteeEnum groupGrantee); - - AccessControlList transCannedAcl(String cannedAcl); - - String transBucketDirectColdAccess(BucketDirectColdAccess access) throws ServiceException; - } \ No newline at end of file diff --git a/app/src/main/java/com/obs/services/internal/IHeaders.java b/app/src/main/java/com/obs/services/internal/IHeaders.java index 1e7b683..32083d7 100644 --- a/app/src/main/java/com/obs/services/internal/IHeaders.java +++ b/app/src/main/java/com/obs/services/internal/IHeaders.java @@ -11,67 +11,113 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; -public interface IHeaders{ - String defaultStorageClassHeader(); - String epidHeader(); - String aclHeader(); - String requestIdHeader(); - String requestId2Header(); - String bucketRegionHeader(); - String locationHeader(); - String storageClassHeader(); - String websiteRedirectLocationHeader(); - String successRedirectLocationHeader(); - String sseKmsHeader(); - String sseKmsKeyHeader(); - String sseKmsProjectIdHeader(); - String sseCHeader(); - String sseCKeyHeader(); - String sseCKeyMd5Header(); - String expiresHeader(); - String versionIdHeader(); - String copySourceHeader(); - String copySourceRangeHeader(); - String copySourceVersionIdHeader(); - String copySourceSseCHeader(); - String copySourceSseCKeyHeader(); - String copySourceSseCKeyMd5Header(); - String metadataDirectiveHeader(); - String dateHeader(); - String deleteMarkerHeader(); - String headerPrefix(); - String headerMetaPrefix(); - String securityTokenHeader(); - String contentSha256Header(); - - String listTimeoutHeader(); - - String objectTypeHeader(); - String nextPositionHeader(); - - String expirationHeader(); - String restoreHeader(); - - String serverVersionHeader(); - - String grantReadHeader(); - String grantWriteHeader(); - String grantReadAcpHeader(); - String grantWriteAcpHeader(); - String grantFullControlHeader(); - String grantReadDeliveredHeader(); - String grantFullControlDeliveredHeader(); - - String copySourceIfModifiedSinceHeader(); - String copySourceIfUnmodifiedSinceHeader(); - String copySourceIfNoneMatchHeader(); - String copySourceIfMatchHeader(); - - String fsFileInterfaceHeader(); - String fsModeHeader(); - - String azRedundancyHeader(); - String bucketTypeHeader(); +public interface IHeaders { + String defaultStorageClassHeader(); + + String epidHeader(); + + String aclHeader(); + + String requestIdHeader(); + + String requestId2Header(); + + String bucketRegionHeader(); + + String locationHeader(); + + String storageClassHeader(); + + String websiteRedirectLocationHeader(); + + String successRedirectLocationHeader(); + + String sseKmsHeader(); + + String sseKmsKeyHeader(); + + String sseKmsProjectIdHeader(); + + String sseCHeader(); + + String sseCKeyHeader(); + + String sseCKeyMd5Header(); + + String expiresHeader(); + + String versionIdHeader(); + + String copySourceHeader(); + + String copySourceRangeHeader(); + + String copySourceVersionIdHeader(); + + String copySourceSseCHeader(); + + String copySourceSseCKeyHeader(); + + String copySourceSseCKeyMd5Header(); + + String metadataDirectiveHeader(); + + String dateHeader(); + + String deleteMarkerHeader(); + + String headerPrefix(); + + String headerMetaPrefix(); + + String securityTokenHeader(); + + String contentSha256Header(); + + String listTimeoutHeader(); + + String objectTypeHeader(); + + String nextPositionHeader(); + + String expirationHeader(); + + String restoreHeader(); + + String serverVersionHeader(); + + String grantReadHeader(); + + String grantWriteHeader(); + + String grantReadAcpHeader(); + + String grantWriteAcpHeader(); + + String grantFullControlHeader(); + + String grantReadDeliveredHeader(); + + String grantFullControlDeliveredHeader(); + + String copySourceIfModifiedSinceHeader(); + + String copySourceIfUnmodifiedSinceHeader(); + + String copySourceIfNoneMatchHeader(); + + String copySourceIfMatchHeader(); + + String fsFileInterfaceHeader(); + + String fsModeHeader(); + + String azRedundancyHeader(); + + String bucketTypeHeader(); + + String requestPaymentHeader(); } diff --git a/app/src/main/java/com/obs/services/internal/ObsCallback.java b/app/src/main/java/com/obs/services/internal/ObsCallback.java index d0a420e..fff2d26 100644 --- a/app/src/main/java/com/obs/services/internal/ObsCallback.java +++ b/app/src/main/java/com/obs/services/internal/ObsCallback.java @@ -11,11 +11,12 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; interface ObsCallback { - - public void onSuccess(T result); - - public void onFailure(K e); + + public void onSuccess(T result); + + public void onFailure(K e); } diff --git a/app/src/main/java/com/obs/services/internal/ObsConstraint.java b/app/src/main/java/com/obs/services/internal/ObsConstraint.java index f4ef5b5..3fa5ef8 100644 --- a/app/src/main/java/com/obs/services/internal/ObsConstraint.java +++ b/app/src/main/java/com/obs/services/internal/ObsConstraint.java @@ -11,141 +11,140 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; /** * OBS系统中的用到的常量。通过常量名称获取配置信息 */ -public class ObsConstraint -{ +public class ObsConstraint { /** * OBS域名的名称 */ public static final String END_POINT = "obs-endpoint"; - + /** * 是否使用OPEN SSL(HTTPS) */ public static final String HTTPS_ONLY = "obs.https-only"; - + /** * 是否不把请求中的桶名加入域名 */ public static final String DISABLE_DNS_BUCKET = "obs.disable-dns-buckets"; - + /** * 若使用HTTP,使用的端口号 */ public static final String HTTP_PORT = "obs-endpoint-http-port"; - + /** * 若使用HTTPS,使用的端口号 */ public static final String HTTPS_PORT = "obs-endpoint-https-port"; - + /** * 与OBS服务端建立的最大连接数 */ public static final String HTTP_MAX_CONNECT = "httpclient.max-connections"; - + /** * 发送OBS业务请求最大重复次数 */ public static final String HTTP_RETRY_MAX = "httpclient.retry-max"; - + /** * 发送OBS业务请求连接超时时间 */ public static final String HTTP_CONNECT_TIMEOUT = "httpclient.connection-timeout-ms"; - + /** * SOCKET的超时时间 */ public static final String HTTP_SOCKET_TIMEOUT = "httpclient.socket-timeout-ms"; - + public static final String HTTP_IDLE_CONNECTION_TIME = "httpclient.idle-connection-time"; - + public static final String HTTP_MAX_IDLE_CONNECTIONS = "httpclient.max-idle-connections"; - - + public static final String HTTP_STRICT_HOSTNAME_VERIFICATION = "httpclient.strict-hostname-verification"; - + public static final int HTTP_CONNECT_TIMEOUT_VALUE = 60000; - + public static final int HTTP_MAX_CONNECT_VALUE = 1000; - + public static final int HTTP_RETRY_MAX_VALUE = 3; - + public static final int HTTP_SOCKET_TIMEOUT_VALUE = 60000; - + public static final int HTTP_PORT_VALUE = 80; - + public static final int HTTPS_PORT_VALUE = 443; - + public static final long DEFAULT_PROGRESS_INTERVAL = 100 * 1024L; - + public static final int DEFAULT_CHUNK_SIZE = 4096; - + public static final String DEFAULT_BUCKET_LOCATION_VALUE = "region"; - - public static final int DEFAULT_BUFFER_STREAM = 512* 1024;// 512KB - + +// public static final int DEFAULT_BUFFER_STREAM = 512 * 1024;// 512KB + public static final int DEFAULT_READ_BUFFER_STREAM = 8192;// 8KB - + public static final int DEFAULT_WRITE_BUFFER_STREAM = 8192;// 8KB - + public static final long DEFAULT_EXPIRE_SECONEDS = 300; - + public static final int DEFAULT_IDLE_CONNECTION_TIME = 30000; - - public static final int DEFAULT_MAX_IDLE_CONNECTIONS = HTTP_MAX_CONNECT_VALUE; - + + public static final int DEFAULT_MAX_IDLE_CONNECTIONS = HTTP_MAX_CONNECT_VALUE; + public static final int DEFAULT_TASK_THREAD_NUM = 10; - + public static final int DEFAULT_WORK_QUEUE_NUM = 20000; - + public static final int DEFAULT_TASK_PROGRESS_INTERVAL = 50; - - + + public static final int DEFAULT_LOCAL_AUTH_TYPE_CACHE_CAPACITY = 50; + // HTTP代理配置 public static final String PROXY_ISABLE = "httpclient.proxy-enable"; - + public static final String PROXY_HOST = "httpclient.proxy-host"; - + public static final String PROXY_PORT = "httpclient.proxy-port"; - + public static final String PROXY_UNAME = "httpclient.proxy-user"; - + public static final String PROXY_PAWD = "httpclient.proxy-password"; - + public static final String PROXY_DOMAIN = "httpclient.proxy-domain"; - + public static final String PROXY_WORKSTATION = "httpclient.proxy-workstation"; - - public static final String BUFFER_STREAM = "uploads.stream-retry-buffer-size"; - + +// public static final String BUFFER_STREAM = "uploads.stream-retry-buffer-size"; + public static final String VALIDATE_CERTIFICATE = "httpclient.validate-certificate"; public static final String VERIFY_RESPONSE_CONTENT_TYPE = "obs.verify-content-type"; - + public static final String WRITE_BUFFER_SIZE = "httpclient.write-buffer-size"; public static final String READ_BUFFER_SIZE = "httpclient.read-buffer-size"; - - + public static final String SOCKET_WRITE_BUFFER_SIZE = "socket.write-buffer-size"; public static final String SOCKET_READ_BUFFER_SIZE = "socket.read-buffer-size"; - public static final String KEEP_ALIVE = "httpclient.keep-alive"; - - public static final String HTTP_PROTOCOL = "httpclient.protocol"; - - public static final String FS_DELIMITER = "filesystem.delimiter"; - - public static final String AUTH_TYPE_NEGOTIATION = "httpclient.auth-type-negotiation"; - - public static final String IS_CNAME = "httpclient.is-cname"; - - public static final String SSL_PROVIDER = "httpclient.ssl-provider"; + public static final String KEEP_ALIVE = "httpclient.keep-alive"; + + public static final String HTTP_PROTOCOL = "httpclient.protocol"; + + public static final String FS_DELIMITER = "filesystem.delimiter"; + + public static final String AUTH_TYPE_NEGOTIATION = "httpclient.auth-type-negotiation"; + + public static final String IS_CNAME = "httpclient.is-cname"; + + public static final String SSL_PROVIDER = "httpclient.ssl-provider"; /** * Environment variable name for the obs accesskey @@ -162,5 +161,8 @@ public class ObsConstraint */ public static final String SECURITY_TOKEN_ENV_VAR = "OBS_SECURITY_TOKEN"; + public static final String OBS_XML_DOC_BUILDER_FACTORY = "obs.xml.document.builder.factory"; + public static final String OBS_XML_DOC_BUILDER_FACTORY_CLASS = + "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"; } diff --git a/app/src/main/java/com/obs/services/internal/ObsConvertor.java b/app/src/main/java/com/obs/services/internal/ObsConvertor.java index 9486f23..355a12b 100644 --- a/app/src/main/java/com/obs/services/internal/ObsConvertor.java +++ b/app/src/main/java/com/obs/services/internal/ObsConvertor.java @@ -11,14 +11,15 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; -import com.jamesmurty.utils.XMLBuilder; import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.internal.xml.OBSXMLBuilder; import com.obs.services.model.AccessControlList; import com.obs.services.model.BucketEncryption; import com.obs.services.model.BucketLoggingConfiguration; @@ -41,312 +42,315 @@ import com.obs.services.model.TopicConfiguration; public class ObsConvertor extends V2Convertor { - - private static ObsConvertor instance = new ObsConvertor(); - - ObsConvertor() { - - } - - public static IConvertor getInstance() { - return instance; - } - - @Override - public String transBucketLoction(String location) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("CreateBucketConfiguration") - .elem("Location").text(ServiceUtils.toValid(location)); - return builder.asString(); - } catch (Exception e) { - throw new ServiceException(e); - } - } - - @Override - public String transRestoreObjectRequest(RestoreObjectRequest req) throws ServiceException { - - try { - XMLBuilder builder = XMLBuilder.create("RestoreRequest").elem("Days").t(String.valueOf(req.getDays())).up(); - if (req.getRestoreTier() != null && req.getRestoreTier() != RestoreTierEnum.BULK) { - builder.e("RestoreJob").e("Tier").t(req.getRestoreTier().getCode()); - } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for restoreobject", e); - } - - } - - @Override + + private static ObsConvertor instance = new ObsConvertor(); + + public ObsConvertor() { + + } + + public static IConvertor getInstance() { + return instance; + } + + @Override + public String transBucketLoction(String location) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("CreateBucketConfiguration").elem("Location") + .text(ServiceUtils.toValid(location)); + return builder.asString(); + } catch (Exception e) { + throw new ServiceException(e); + } + } + + @Override + public String transRestoreObjectRequest(RestoreObjectRequest req) throws ServiceException { + + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("RestoreRequest") + .elem("Days").t(String.valueOf(req.getDays())).up(); + if (req.getRestoreTier() != null && req.getRestoreTier() != RestoreTierEnum.BULK) { + builder.e("RestoreJob").e("Tier").t(req.getRestoreTier().getCode()); + } + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for restoreobject", e); + } + + } + + @Override public String transBucketEcryption(BucketEncryption encryption) throws ServiceException { String algorithm = encryption.getSseAlgorithm().getCode(); String kmsKeyId = ""; if (algorithm.equals(SSEAlgorithmEnum.KMS.getCode())) { kmsKeyId = encryption.getKmsKeyId(); } + return transBucketEcryptionXML(algorithm, kmsKeyId); + } + + @Override + public String transStoragePolicy(BucketStoragePolicyConfiguration status) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("StorageClass") + .text(this.transStorageClass(status.getBucketStorageClass())); + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for StorageClass", e); + } + } + + @Override + public String transBucketLoggingConfiguration(BucketLoggingConfiguration c) throws ServiceException { try { - XMLBuilder builder = XMLBuilder.create("ServerSideEncryptionConfiguration").e("Rule").e("ApplyServerSideEncryptionByDefault"); - builder.e("SSEAlgorithm").t(algorithm); - if (ServiceUtils.isValid(kmsKeyId)) { - builder.e("KMSMasterKeyID").t(kmsKeyId); + OBSXMLBuilder builder = OBSXMLBuilder.create("BucketLoggingStatus"); + if (c.getAgency() != null) { + builder.e("Agency").t(ServiceUtils.toValid(c.getAgency())); + } + if (c.isLoggingEnabled()) { + OBSXMLBuilder enabledBuilder = builder.elem("LoggingEnabled"); + if (c.getTargetBucketName() != null) { + enabledBuilder.elem("TargetBucket").text(ServiceUtils.toValid(c.getTargetBucketName())); + } + if (c.getLogfilePrefix() != null) { + enabledBuilder.elem("TargetPrefix").text(ServiceUtils.toValid(c.getLogfilePrefix())); + } + GrantAndPermission[] grants = c.getTargetGrants(); + if (grants.length > 0) { + OBSXMLBuilder grantsBuilder = enabledBuilder.elem("TargetGrants"); + transGrantsBuilder(grants, grantsBuilder); + } } return builder.asString(); } catch (Exception e) { - throw new ServiceException("Failed to build XML document for bucketEncryption", e); + throw new ServiceException("Failed to build XML document for BucketLoggingConfiguration", e); + } + } + + private void transGrantsBuilder(GrantAndPermission[] grants, OBSXMLBuilder grantsBuilder) + throws ParserConfigurationException, FactoryConfigurationError { + for (GrantAndPermission gap : grants) { + GranteeInterface grantee = gap.getGrantee(); + Permission permission = gap.getPermission(); + if (permission != null) { + OBSXMLBuilder subBuilder = null; + if (grantee instanceof CanonicalGrantee) { + subBuilder = OBSXMLBuilder.create("Grantee").element("ID") + .text(ServiceUtils.toValid(grantee.getIdentifier())); + } else if (grantee instanceof GroupGrantee) { + subBuilder = OBSXMLBuilder.create("Grantee").element("Canned") + .text(this.transGroupGrantee(((GroupGrantee) grantee).getGroupGranteeType())); + } + + if (subBuilder != null) { + grantsBuilder.elem("Grant").importXMLBuilder(subBuilder).elem("Permission") + .text(ServiceUtils.toValid(permission.getPermissionString())); + } + } } } + @Override + public String transAccessControlList(AccessControlList acl, boolean isBucket) throws ServiceException { + Owner owner = acl.getOwner(); + GrantAndPermission[] grants = acl.getGrantAndPermissions(); + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("AccessControlPolicy"); + if (owner != null) { + builder.elem("Owner").elem("ID").text(ServiceUtils.toValid(owner.getId())); + } + if (!isBucket) { + builder.elem("Delivered").text(String.valueOf(acl.isDelivered())); + } + + if (grants.length > 0) { + OBSXMLBuilder accessControlList = builder.elem("AccessControlList"); + for (GrantAndPermission gap : grants) { + GranteeInterface grantee = gap.getGrantee(); + Permission permission = gap.getPermission(); - @Override - public String transStoragePolicy(BucketStoragePolicyConfiguration status) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("StorageClass") - .text(this.transStorageClass(status.getBucketStorageClass())); - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for StorageClass", e); - } - } - - @Override - public String transBucketLoggingConfiguration(BucketLoggingConfiguration c) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("BucketLoggingStatus"); - if(c.getAgency() != null) { - builder.e("Agency").t(ServiceUtils.toValid(c.getAgency())); - } - if (c.isLoggingEnabled()) { - XMLBuilder enabledBuilder = builder.elem("LoggingEnabled"); - if(c.getTargetBucketName() != null) { - enabledBuilder.elem("TargetBucket") - .text(ServiceUtils.toValid(c.getTargetBucketName())); - } - if(c.getLogfilePrefix() != null) { - enabledBuilder.elem("TargetPrefix").text(ServiceUtils.toValid(c.getLogfilePrefix())); - } - GrantAndPermission[] grants = c.getTargetGrants(); - if (grants.length > 0) { - XMLBuilder grantsBuilder = enabledBuilder.elem("TargetGrants"); - for (GrantAndPermission gap : grants) { - GranteeInterface grantee = gap.getGrantee(); - Permission permission = gap.getPermission(); - if(permission != null) { - XMLBuilder subBuilder = null; - if (grantee instanceof CanonicalGrantee) { - subBuilder = XMLBuilder.create("Grantee").element("ID").text(ServiceUtils.toValid(grantee.getIdentifier())); - } else if (grantee instanceof GroupGrantee) { - subBuilder = XMLBuilder.create("Grantee").element("Canned").text(this.transGroupGrantee(((GroupGrantee)grantee).getGroupGranteeType())); - } - - if(subBuilder != null) { - grantsBuilder.elem("Grant").importXMLBuilder(subBuilder).elem("Permission") - .text(ServiceUtils.toValid(permission.getPermissionString())).up(); - } - } - } - } - } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for BucketLoggingConfiguration", e); - } - } - - @Override - public String transAccessControlList(AccessControlList acl, boolean isBucket) throws ServiceException { - Owner owner = acl.getOwner(); - GrantAndPermission[] grants = acl.getGrantAndPermissions(); - try { - XMLBuilder builder = XMLBuilder.create("AccessControlPolicy"); - if (owner != null) { - builder.elem("Owner").elem("ID").text(ServiceUtils.toValid(owner.getId())); - } - if(!isBucket) { - builder.elem("Delivered").text(String.valueOf(acl.isDelivered())); - } - - if(grants.length > 0) { - XMLBuilder accessControlList = builder.elem("AccessControlList"); - for (GrantAndPermission gap : grants) { - GranteeInterface grantee = gap.getGrantee(); - Permission permission = gap.getPermission(); - - XMLBuilder subBuilder = null; - if (grantee instanceof CanonicalGrantee) { - subBuilder = XMLBuilder.create("Grantee").element("ID").text(ServiceUtils.toValid(grantee.getIdentifier())); - } else if (grantee instanceof GroupGrantee) { - if(((GroupGrantee)grantee).getGroupGranteeType() != GroupGranteeEnum.ALL_USERS) { - continue; - } - subBuilder = XMLBuilder.create("Grantee").element("Canned").text(this.transGroupGrantee(((GroupGrantee)grantee).getGroupGranteeType())); - }else if(grantee != null) { - subBuilder = XMLBuilder.create("Grantee").element("ID").text(ServiceUtils.toValid(grantee.getIdentifier())); - } - if(subBuilder != null) { - XMLBuilder grantBuilder = accessControlList.elem("Grant").importXMLBuilder(subBuilder); - if(permission != null) { - grantBuilder.elem("Permission").text(ServiceUtils.toValid(permission.getPermissionString())); - } - if(isBucket) { - grantBuilder.e("Delivered").t(String.valueOf(gap.isDelivered())); - } - } - } - } - - return builder.asString(); - } catch (ParserConfigurationException | FactoryConfigurationError | TransformerException e) { - throw new ServiceException("Failed to build XML document for ACL", e); - } - } - - - @Override - public String transBucketNotificationConfiguration(BucketNotificationConfiguration bucketNotificationConfiguration) - throws ServiceException { - - try { - XMLBuilder builder = XMLBuilder.create("NotificationConfiguration"); - if (bucketNotificationConfiguration == null) { - return builder.asString(); - } - - for (TopicConfiguration config : bucketNotificationConfiguration.getTopicConfigurations()) { + OBSXMLBuilder subBuilder = null; + if (grantee instanceof CanonicalGrantee) { + subBuilder = OBSXMLBuilder.create("Grantee").element("ID") + .text(ServiceUtils.toValid(grantee.getIdentifier())); + } else if (grantee instanceof GroupGrantee) { + if (((GroupGrantee) grantee).getGroupGranteeType() != GroupGranteeEnum.ALL_USERS) { + continue; + } + subBuilder = OBSXMLBuilder.create("Grantee").element("Canned") + .text(this.transGroupGrantee(((GroupGrantee) grantee).getGroupGranteeType())); + } else if (grantee != null) { + subBuilder = OBSXMLBuilder.create("Grantee").element("ID") + .text(ServiceUtils.toValid(grantee.getIdentifier())); + } + if (subBuilder != null) { + OBSXMLBuilder grantBuilder = accessControlList.elem("Grant").importXMLBuilder(subBuilder); + if (permission != null) { + grantBuilder.elem("Permission") + .text(ServiceUtils.toValid(permission.getPermissionString())); + } + if (isBucket) { + grantBuilder.e("Delivered").t(String.valueOf(gap.isDelivered())); + } + } + } + } + + return builder.asString(); + } catch (ParserConfigurationException | FactoryConfigurationError | TransformerException e) { + throw new ServiceException("Failed to build XML document for ACL", e); + } + } + + @Override + public String transBucketNotificationConfiguration(BucketNotificationConfiguration bucketNotificationConfiguration) + throws ServiceException { + + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("NotificationConfiguration"); + if (bucketNotificationConfiguration == null) { + return builder.asString(); + } + + for (TopicConfiguration config : bucketNotificationConfiguration.getTopicConfigurations()) { packNotificationConfig(builder, config, "TopicConfiguration", "Topic", "Object"); } - + for (FunctionGraphConfiguration config : bucketNotificationConfiguration.getFunctionGraphConfigurations()) { packNotificationConfig(builder, config, "FunctionGraphConfiguration", "FunctionGraph", "Object"); } - - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for Notification", e); - } - } - - - @Override - public String transReplicationConfiguration(ReplicationConfiguration replicationConfiguration) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("ReplicationConfiguration").e("Agency").t(ServiceUtils.toValid(replicationConfiguration.getAgency())) - .up(); - for (ReplicationConfiguration.Rule rule : replicationConfiguration.getRules()) { - builder = builder.e("Rule"); - if (rule.getId() != null) { - builder.e("ID").t(rule.getId()); - } - builder.e("Prefix").t(ServiceUtils.toValid(rule.getPrefix())); - if (rule.getStatus() != null) { - builder.e("Status").t(rule.getStatus().getCode()); - } - if (rule.getDestination() != null) { - String bucketName = ServiceUtils.toValid(rule.getDestination().getBucket()); - builder = builder.e("Destination").e("Bucket").t(bucketName).up(); - if (rule.getDestination().getObjectStorageClass() != null) { - builder.e("StorageClass").t(this.transStorageClass(rule.getDestination().getObjectStorageClass())); - } - builder = builder.up(); - } - builder = builder.up(); - } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for Replication", e); - } - } - - @Override - public String transEventType(EventTypeEnum eventType) { - return transEventTypeStatic(eventType); - } - - public static String transEventTypeStatic(EventTypeEnum eventType) { - String eventTypeStr = ""; - if(eventType != null) { - switch (eventType) { - case OBJECT_CREATED_ALL: - eventTypeStr = "ObjectCreated:*"; - break; - case OBJECT_CREATED_PUT: - eventTypeStr = "ObjectCreated:Put"; - break; - case OBJECT_CREATED_POST: - eventTypeStr = "ObjectCreated:Post"; - break; - case OBJECT_CREATED_COPY: - eventTypeStr = "ObjectCreated:Copy"; - break; - case OBJECT_CREATED_COMPLETE_MULTIPART_UPLOAD: - eventTypeStr = "ObjectCreated:CompleteMultipartUpload"; - break; - case OBJECT_REMOVED_ALL: - eventTypeStr = "ObjectRemoved:*"; - break; - case OBJECT_REMOVED_DELETE: - eventTypeStr = "ObjectRemoved:Delete"; - break; - case OBJECT_REMOVED_DELETE_MARKER_CREATED: - eventTypeStr = "ObjectRemoved:DeleteMarkerCreated"; - break; - default: - break; - } - } - return eventTypeStr; - } - - - @Override - public String transStorageClass(StorageClassEnum storageClass) { - String storageClassStr = ""; - if(storageClass != null) { - switch (storageClass) { - case STANDARD: - storageClassStr = "STANDARD"; - break; - case WARM: - storageClassStr = "WARM"; - break; - case COLD: - storageClassStr = "COLD"; - break; - default: - break; - } - } - return storageClassStr; - } - - @Override - public AccessControlList transCannedAcl(String cannedAcl) { - if(Constants.ACL_PRIVATE.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_PRIVATE; - }else if(Constants.ACL_PUBLIC_READ.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_PUBLIC_READ; - }else if(Constants.ACL_PUBLIC_READ_WRITE.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_PUBLIC_READ_WRITE; - }else if(Constants.ACL_PUBLIC_READ_DELIVERED.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_PUBLIC_READ_DELIVERED; - }else if(Constants.ACL_PUBLIC_READ_WRITE_DELIVERED.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_PUBLIC_READ_WRITE_DELIVERED; - } - return null; - } - - @Override - public String transGroupGrantee(GroupGranteeEnum groupGrantee) { - String groupGranteeStr = ""; - if(groupGrantee != null) { - switch (groupGrantee) { - case ALL_USERS: - groupGranteeStr = "Everyone"; - break; - default: - break; - } - } - return groupGranteeStr; - } + + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for Notification", e); + } + } + + @Override + public String transReplicationConfiguration(ReplicationConfiguration replicationConfiguration) + throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("ReplicationConfiguration").e("Agency") + .t(ServiceUtils.toValid(replicationConfiguration.getAgency())).up(); + for (ReplicationConfiguration.Rule rule : replicationConfiguration.getRules()) { + builder = builder.e("Rule"); + if (rule.getId() != null) { + builder.e("ID").t(rule.getId()); + } + builder.e("Prefix").t(ServiceUtils.toValid(rule.getPrefix())); + if (rule.getStatus() != null) { + builder.e("Status").t(rule.getStatus().getCode()); + } + if (rule.getHistoricalObjectReplication() != null) { + builder.e("HistoricalObjectReplication").t(rule.getHistoricalObjectReplication().getCode()); + } + if (rule.getDestination() != null) { + String bucketName = ServiceUtils.toValid(rule.getDestination().getBucket()); + builder = builder.e("Destination").e("Bucket").t(bucketName).up(); + if (rule.getDestination().getObjectStorageClass() != null) { + builder.e("StorageClass") + .t(this.transStorageClass(rule.getDestination().getObjectStorageClass())); + } + builder = builder.up(); + } + builder = builder.up(); + } + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for Replication", e); + } + } + + @Override + public String transEventType(EventTypeEnum eventType) { + return transEventTypeStatic(eventType); + } + + public static String transEventTypeStatic(EventTypeEnum eventType) { + String eventTypeStr = ""; + if (eventType != null) { + switch (eventType) { + case OBJECT_CREATED_ALL: + eventTypeStr = "ObjectCreated:*"; + break; + case OBJECT_CREATED_PUT: + eventTypeStr = "ObjectCreated:Put"; + break; + case OBJECT_CREATED_POST: + eventTypeStr = "ObjectCreated:Post"; + break; + case OBJECT_CREATED_COPY: + eventTypeStr = "ObjectCreated:Copy"; + break; + case OBJECT_CREATED_COMPLETE_MULTIPART_UPLOAD: + eventTypeStr = "ObjectCreated:CompleteMultipartUpload"; + break; + case OBJECT_REMOVED_ALL: + eventTypeStr = "ObjectRemoved:*"; + break; + case OBJECT_REMOVED_DELETE: + eventTypeStr = "ObjectRemoved:Delete"; + break; + case OBJECT_REMOVED_DELETE_MARKER_CREATED: + eventTypeStr = "ObjectRemoved:DeleteMarkerCreated"; + break; + default: + break; + } + } + return eventTypeStr; + } + + @Override + public String transStorageClass(StorageClassEnum storageClass) { + String storageClassStr = ""; + if (storageClass != null) { + switch (storageClass) { + case STANDARD: + storageClassStr = "STANDARD"; + break; + case WARM: + storageClassStr = "WARM"; + break; + case COLD: + storageClassStr = "COLD"; + break; + default: + break; + } + } + return storageClassStr; + } + + @Override + public AccessControlList transCannedAcl(String cannedAcl) { + if (Constants.ACL_PRIVATE.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_PRIVATE; + } else if (Constants.ACL_PUBLIC_READ.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_PUBLIC_READ; + } else if (Constants.ACL_PUBLIC_READ_WRITE.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_PUBLIC_READ_WRITE; + } else if (Constants.ACL_PUBLIC_READ_DELIVERED.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_PUBLIC_READ_DELIVERED; + } else if (Constants.ACL_PUBLIC_READ_WRITE_DELIVERED.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_PUBLIC_READ_WRITE_DELIVERED; + } + return null; + } + + @Override + public String transGroupGrantee(GroupGranteeEnum groupGrantee) { + String groupGranteeStr = ""; + if (groupGrantee != null) { + switch (groupGrantee) { + case ALL_USERS: + groupGranteeStr = "Everyone"; + break; + default: + break; + } + } + return groupGranteeStr; + } } diff --git a/app/src/main/java/com/obs/services/internal/ObsHeaders.java b/app/src/main/java/com/obs/services/internal/ObsHeaders.java index ccefb1d..b07df0a 100644 --- a/app/src/main/java/com/obs/services/internal/ObsHeaders.java +++ b/app/src/main/java/com/obs/services/internal/ObsHeaders.java @@ -11,278 +11,103 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services.internal; +package com.obs.services.internal; -public class ObsHeaders implements IHeaders{ - - private ObsHeaders() { - - } - - private static ObsHeaders instance = new ObsHeaders(); - - public static IHeaders getInstance() { - return instance; - } - - @Override - public String defaultStorageClassHeader() { - return this.headerPrefix() + "storage-class"; - } +public class ObsHeaders extends V2Headers { - @Override - public String epidHeader() { - return this.headerPrefix() + "epid"; + public ObsHeaders() { + super(); } - @Override - public String aclHeader() { - return this.headerPrefix() + "acl"; - } - - @Override - public String requestIdHeader() { - return this.headerPrefix() + "request-id"; - } - - @Override - public String requestId2Header() { - return this.headerPrefix() + "id-2"; - } - - @Override - public String storageClassHeader() { - return this.headerPrefix() + "storage-class"; - } - - @Override - public String websiteRedirectLocationHeader() { - return this.headerPrefix() + "website-redirect-location"; - } - - @Override - public String sseKmsHeader() { - return this.headerPrefix() + "server-side-encryption"; - } - - @Override - public String sseKmsKeyHeader() { - return this.headerPrefix() + "server-side-encryption-kms-key-id"; - } - - @Override - public String sseKmsProjectIdHeader() { - return this.headerPrefix() + "sse-kms-key-project-id"; - } - - @Override - public String sseCHeader() { - return this.headerPrefix() + "server-side-encryption-customer-algorithm"; - } - - @Override - public String sseCKeyHeader() { - return this.headerPrefix() + "server-side-encryption-customer-key"; - } - - @Override - public String sseCKeyMd5Header() { - return this.headerPrefix() + "server-side-encryption-customer-key-MD5"; - } - - @Override - public String expiresHeader() { - return this.headerPrefix() + "expires"; - } - - @Override - public String versionIdHeader() { - return this.headerPrefix() + "version-id"; - } - - @Override - public String copySourceSseCHeader() { - return this.headerPrefix() + "copy-source-server-side-encryption-customer-algorithm"; - } - - @Override - public String metadataDirectiveHeader() { - return this.headerPrefix() + "metadata-directive"; - } - - @Override - public String headerPrefix() { - return Constants.OBS_HEADER_PREFIX; - } - - @Override - public String headerMetaPrefix() { - return Constants.OBS_HEADER_META_PREFIX; - } + private static ObsHeaders instance = new ObsHeaders(); - @Override - public String dateHeader() { - return this.headerPrefix() + "date"; - } - - @Override - public String grantReadHeader() { - return this.headerPrefix() + "grant-read"; - } - - @Override - public String grantWriteHeader() { - return this.headerPrefix() + "grant-write"; - } - - @Override - public String grantReadAcpHeader() { - return this.headerPrefix() + "grant-read-acp"; - } - - @Override - public String grantWriteAcpHeader() { - return this.headerPrefix() + "grant-write-acp"; - } - - @Override - public String grantFullControlHeader() { - return this.headerPrefix() + "grant-full-control"; - } - - @Override - public String grantReadDeliveredHeader() { - return this.headerPrefix() + "grant-read-delivered"; - } - - @Override - public String grantFullControlDeliveredHeader() { - return this.headerPrefix() + "grant-full-control-delivered"; - } - - @Override - public String serverVersionHeader() { - return this.headerPrefix() + "version"; - } - - @Override - public String bucketRegionHeader() { - return this.headerPrefix() + "bucket-location"; - } - - @Override - public String locationHeader() { - return null; - } - - @Override - public String successRedirectLocationHeader() { - return "success-action-redirect"; - } - - @Override - public String deleteMarkerHeader() { - return this.headerPrefix() + "delete-marker"; - } - - @Override - public String copySourceSseCKeyHeader() { - return this.headerPrefix() + "copy-source-server-side-encryption-customer-key"; - } - - @Override - public String copySourceSseCKeyMd5Header() { - return this.headerPrefix() + "copy-source-server-side-encryption-customer-key-MD5"; - } - - @Override - public String copySourceIfModifiedSinceHeader() { - return this.headerPrefix() + "copy-source-if-modified-since"; - } + public static IHeaders getInstance() { + return instance; + } - @Override - public String copySourceIfUnmodifiedSinceHeader() { - return this.headerPrefix() + "copy-source-if-unmodified-since"; - } + @Override + public String defaultStorageClassHeader() { + return this.headerPrefix() + "storage-class"; + } - @Override - public String copySourceIfNoneMatchHeader() { - return this.headerPrefix() + "copy-source-if-none-match"; - } + @Override + public String sseKmsKeyHeader() { + return this.headerPrefix() + "server-side-encryption-kms-key-id"; + } - @Override - public String copySourceIfMatchHeader() { - return this.headerPrefix() + "copy-source-if-match"; - } + @Override + public String expiresHeader() { + return this.headerPrefix() + "expires"; + } - @Override - public String copySourceHeader() { - return this.headerPrefix() + "copy-source"; - } + @Override + public String headerPrefix() { + return Constants.OBS_HEADER_PREFIX; + } - @Override - public String copySourceVersionIdHeader() { - return this.headerPrefix() + "copy-source-version-id"; - } + @Override + public String headerMetaPrefix() { + return Constants.OBS_HEADER_META_PREFIX; + } - @Override - public String expirationHeader() { - return this.headerPrefix() + "expiration"; - } + @Override + public String grantReadDeliveredHeader() { + return this.headerPrefix() + "grant-read-delivered"; + } - @Override - public String restoreHeader() { - return this.headerPrefix() + "restore"; - } + @Override + public String grantFullControlDeliveredHeader() { + return this.headerPrefix() + "grant-full-control-delivered"; + } - @Override - public String copySourceRangeHeader() { - return this.headerPrefix() + "copy-source-range"; - } + @Override + public String serverVersionHeader() { + return this.headerPrefix() + "version"; + } - @Override - public String securityTokenHeader() { - return this.headerPrefix() + "security-token"; - } + @Override + public String bucketRegionHeader() { + return this.headerPrefix() + "bucket-location"; + } - @Override - public String contentSha256Header() { - return null; - } + @Override + public String locationHeader() { + return null; + } - @Override - public String objectTypeHeader() { - return this.headerPrefix() + "object-type"; - } + @Override + public String successRedirectLocationHeader() { + return "success-action-redirect"; + } - @Override - public String nextPositionHeader() { - return this.headerPrefix() + "next-append-position"; - } + @Override + public String contentSha256Header() { + return null; + } - @Override - public String listTimeoutHeader() { - return this.headerPrefix() + "list-timeout"; - } + @Override + public String objectTypeHeader() { + return this.headerPrefix() + "object-type"; + } - @Override - public String fsFileInterfaceHeader() { - return this.headerPrefix() + "fs-file-interface"; - } + @Override + public String nextPositionHeader() { + return this.headerPrefix() + "next-append-position"; + } - @Override - public String fsModeHeader() { - return this.headerMetaPrefix() + "mode"; - } + @Override + public String fsFileInterfaceHeader() { + return this.headerPrefix() + "fs-file-interface"; + } - @Override - public String azRedundancyHeader() { - return this.headerPrefix() + "az-redundancy"; - } + @Override + public String azRedundancyHeader() { + return this.headerPrefix() + "az-redundancy"; + } - @Override - public String bucketTypeHeader() { - return this.headerPrefix() + "bucket-type"; - } + @Override + public String bucketTypeHeader() { + return this.headerPrefix() + "bucket-type"; + } } diff --git a/app/src/main/java/com/obs/services/internal/ObsProperties.java b/app/src/main/java/com/obs/services/internal/ObsProperties.java index 628479f..c05d2cf 100644 --- a/app/src/main/java/com/obs/services/internal/ObsProperties.java +++ b/app/src/main/java/com/obs/services/internal/ObsProperties.java @@ -1,9 +1,4 @@ /** - * - * JetS3t : Java S3 Toolkit - * Project hosted at http://bitbucket.org/jmurty/jets3t/ - * - * Copyright 2006-2010 James Murty * Copyright 2019 Huawei Technologies Co.,Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the @@ -16,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; import java.io.Serializable; @@ -28,16 +24,15 @@ public class ObsProperties implements Serializable { private static final long serialVersionUID = -822234326095333142L; - private static final ILogger log = LoggerBuilder.getLogger(ObsProperties.class); + private static final ILogger LOG = LoggerBuilder.getLogger(ObsProperties.class); private final Properties properties = new Properties(); - public void setProperty(String propertyName, String propertyValue) { if (propertyValue == null) { this.clearProperty(propertyName); } else { - this.properties.put(propertyName, trim(propertyValue)); + this.properties.setProperty(propertyName, trim(propertyValue)); } } @@ -51,47 +46,33 @@ public void clearAllProperties() { public String getStringProperty(String propertyName, String defaultValue) { String stringValue = trim(properties.getProperty(propertyName, defaultValue)); - if (log.isDebugEnabled()) { - log.debug(propertyName + "=" + stringValue); + if (LOG.isDebugEnabled() && !"httpclient.proxy-user".equals(propertyName) + && !"httpclient.proxy-password".equals(propertyName)) { + LOG.debug(propertyName + "=" + stringValue); } return stringValue; } - public long getLongProperty(String propertyName, long defaultValue) - throws NumberFormatException - { - String longValue = trim(properties.getProperty(propertyName, String.valueOf(defaultValue))); - if (log.isDebugEnabled()) { - log.debug(propertyName + "=" + longValue); - } - return Long.parseLong(longValue); - } - - public int getIntProperty(String propertyName, int defaultValue) - throws NumberFormatException - { - String intValue = trim(properties.getProperty(propertyName, String.valueOf(defaultValue))); - if (log.isDebugEnabled()) { - log.debug(propertyName + "=" + intValue); + public int getIntProperty(String propertyName, int defaultValue) throws NumberFormatException { + String value = trim(properties.getProperty(propertyName, String.valueOf(defaultValue))); + if (LOG.isDebugEnabled()) { + LOG.debug(propertyName + "=" + value); } - return Integer.parseInt(intValue); + return Integer.parseInt(value); } - public boolean getBoolProperty(String propertyName, boolean defaultValue) - throws IllegalArgumentException - { + public boolean getBoolProperty(String propertyName, boolean defaultValue) throws IllegalArgumentException { String boolValue = trim(properties.getProperty(propertyName, String.valueOf(defaultValue))); - if (log.isDebugEnabled()) { - log.debug(propertyName + "=" + boolValue); + if (LOG.isDebugEnabled()) { + LOG.debug(propertyName + "=" + boolValue); } - if ("true".equalsIgnoreCase(boolValue)) { - return true; - } else if ("false".equalsIgnoreCase(boolValue)) { - return false; - } else { - throw new IllegalArgumentException("Boolean value '" + boolValue + "' for obs property '" - + propertyName + "' must be 'true' or 'false' (case-insensitive)"); + + if (!"true".equalsIgnoreCase(boolValue) && !"false".equalsIgnoreCase(boolValue)) { + throw new IllegalArgumentException("Boolean value '" + boolValue + "' for obs property '" + propertyName + + "' must be 'true' or 'false' (case-insensitive)"); } + + return Boolean.parseBoolean(boolValue); } public boolean containsKey(String propertyName) { @@ -99,11 +80,10 @@ public boolean containsKey(String propertyName) { } private static String trim(String str) { - if (str != null) { - return str.trim(); - } else { + if (null == str) { return null; } + return str.trim(); } } diff --git a/app/src/main/java/com/obs/services/internal/ObsService.java b/app/src/main/java/com/obs/services/internal/ObsService.java index 4f47cf8..2adab3f 100644 --- a/app/src/main/java/com/obs/services/internal/ObsService.java +++ b/app/src/main/java/com/obs/services/internal/ObsService.java @@ -3,897 +3,219 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services.internal; -import com.obs.log.ILogger; -import com.obs.log.LoggerBuilder; -import com.obs.services.internal.Constants.CommonHeaders; -import com.obs.services.internal.Constants.ObsRequestParams; -import com.obs.services.internal.handler.XmlResponsesSaxParser.*; -import com.obs.services.internal.io.HttpMethodReleaseInputStream; -import com.obs.services.internal.io.ProgressInputStream; -import com.obs.services.internal.security.BasicSecurityKey; -import com.obs.services.internal.task.BlockRejectedExecutionHandler; -import com.obs.services.internal.task.DefaultTaskProgressStatus; -import com.obs.services.internal.utils.*; -import com.obs.services.model.*; -import com.obs.services.model.RestoreObjectRequest.RestoreObjectStatus; -import com.obs.services.model.fs.*; -import com.oef.services.model.*; -import okhttp3.Headers; -import okhttp3.MediaType; -import okhttp3.RequestBody; -import okhttp3.Response; +package com.obs.services.internal; -import java.io.*; -import java.lang.reflect.Method; -import java.security.NoSuchAlgorithmException; -import java.text.ParseException; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; -import java.util.*; -import java.util.Map.Entry; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -public class ObsService extends RestStorageService { - - private static final ILogger log = LoggerBuilder.getLogger("com.obs.services.ObsClient"); - - protected ObsService() { - - } - - private static class TransResult { - private Map headers; - - private Map params; - - private RequestBody body; - - TransResult(Map headers) { - this(headers, null, null); - } - - TransResult(Map headers, RequestBody body) { - this(headers, null, body); - } - - TransResult(Map headers, Map params, RequestBody body) { - this.headers = headers; - this.params = params; - this.body = body; - } - - Map getHeaders() { - if (this.headers == null) { - headers = new HashMap(); - } - return this.headers; - } - - Map getParams() { - if (this.params == null) { - params = new HashMap(); - } - return this.params; - } - } - - protected HeaderResponse setBucketVersioningImpl(String bucketName, VersioningStatusEnum status) - throws ServiceException { - Map requestParams = new HashMap(); - requestParams.put(SpecialParamEnum.VERSIONING.getOriginalStringCode(), ""); - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - - String xml = this.getIConvertor().transVersioningConfiguration(bucketName, - status != null ? status.getCode() : null); - - Response response = performRestPut(bucketName, null, metadata, requestParams, - createRequestBody(Mimetypes.MIMETYPE_XML, xml), true); - return this.build(response); - } - - protected RequestBody createRequestBody(String mimeType, String content) throws ServiceException { - try { - if (log.isTraceEnabled()) { - try { - log.trace("Entity Content:" + content); - } catch (Exception e) { - } - } - return RequestBody.create(MediaType.parse(mimeType), content.getBytes("UTF-8")); - } catch (UnsupportedEncodingException e) { - throw new ServiceException(e); - } - } - - protected void verifyResponseContentType(Response response) throws ServiceException { - if (this.obsProperties.getBoolProperty(ObsConstraint.VERIFY_RESPONSE_CONTENT_TYPE, true)) { - String contentType = response.header(Constants.CommonHeaders.CONTENT_TYPE); - if (!Mimetypes.MIMETYPE_XML.equalsIgnoreCase(contentType) - && !Mimetypes.MIMETYPE_TEXT_XML.equalsIgnoreCase(contentType)) { - throw new ServiceException( - "Expected XML document response from OBS but received content type " + contentType); - } - } - } - - protected BucketVersioningConfiguration getBucketVersioningImpl(String bucketName) throws ServiceException { - Map requestParams = new HashMap(); - requestParams.put(SpecialParamEnum.VERSIONING.getOriginalStringCode(), ""); - Response response = performRestGet(bucketName, null, requestParams, null); - - this.verifyResponseContentType(response); - - BucketVersioningConfiguration ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(response), BucketVersioningHandler.class, false) - .getVersioningStatus(); - setResponseHeaders(ret, this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - TransResult transListVersionsRequest(ListVersionsRequest request) { - Map params = new HashMap(); - params.put(SpecialParamEnum.VERSIONS.getOriginalStringCode(), ""); - if (request.getPrefix() != null) { - params.put(ObsRequestParams.PREFIX, request.getPrefix()); - } - if (request.getDelimiter() != null) { - params.put(ObsRequestParams.DELIMITER, request.getDelimiter()); - } - if (request.getMaxKeys() > 0) { - params.put(ObsRequestParams.MAX_KEYS, String.valueOf(request.getMaxKeys())); - } - if (request.getKeyMarker() != null) { - params.put(ObsRequestParams.KEY_MARKER, request.getKeyMarker()); - } - if (request.getVersionIdMarker() != null) { - params.put(ObsRequestParams.VERSION_ID_MARKER, request.getVersionIdMarker()); - } - Map headers = new HashMap(); - if (request.getListTimeout() > 0) { - putHeader(headers, this.getIHeaders().listTimeoutHeader(), String.valueOf(request.getListTimeout())); - } - return new TransResult(headers, params, null); - } - - protected ListVersionsResult listVersionsImpl(ListVersionsRequest request) throws ServiceException { - - TransResult result = this.transListVersionsRequest(request); - - Response response = performRestGet(request.getBucketName(), null, result.getParams(), null); - - this.verifyResponseContentType(response); - - ListVersionsHandler handler = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(response), - ListVersionsHandler.class, true); - List partialItems = handler.getItems(); - - ListVersionsResult listVersionsResult = new ListVersionsResult(handler.getBucketName() == null ? request.getBucketName() : handler.getBucketName(), - handler.getRequestPrefix() == null ? request.getPrefix() : handler.getRequestPrefix(), handler.getKeyMarker() == null ? request.getKeyMarker() : handler.getKeyMarker(), handler.getNextKeyMarker(), - handler.getVersionIdMarker() == null ? request.getVersionIdMarker() : handler.getVersionIdMarker(), handler.getNextVersionIdMarker(), String.valueOf(handler.getRequestMaxKeys()), handler.isListingTruncated(), - partialItems.toArray(new VersionOrDeleteMarker[partialItems.size()]), handler.getCommonPrefixes(), response.header(this.getIHeaders().bucketRegionHeader()), handler.getDelimiter() == null ? request.getDelimiter() : handler.getDelimiter()); - setResponseHeaders(listVersionsResult, this.cleanResponseHeaders(response)); - setStatusCode(listVersionsResult, response.code()); - return listVersionsResult; - - } - - protected BucketPolicyResponse getBucketPolicyImpl(String bucketName) throws ServiceException { - try { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.POLICY.getOriginalStringCode(), ""); - - Response response = performRestGet(bucketName, null, requestParameters, null); - BucketPolicyResponse ret = new BucketPolicyResponse(response.body().string()); - setResponseHeaders(ret, this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } catch (IOException e) { - throw new ServiceException(e); - } - } - - protected BucketNotificationConfiguration getBucketNotificationConfigurationImpl(String bucketName) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.NOTIFICATION.getOriginalStringCode(), ""); - Response httpResponse = performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - BucketNotificationConfiguration result = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketNotificationConfigurationHandler.class, - false) - .getBucketNotificationConfiguration(); - setResponseHeaders(result, this.cleanResponseHeaders(httpResponse)); - setStatusCode(result, httpResponse.code()); - return result; - } - - protected HeaderResponse setBucketNotificationImpl(String bucketName, - BucketNotificationConfiguration bucketNotificationConfiguration) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.NOTIFICATION.getOriginalStringCode(), ""); - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - String xml = this.getIConvertor().transBucketNotificationConfiguration(bucketNotificationConfiguration); - - Response response = performRestPut(bucketName, null, metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, xml), true); - - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse setBucketPolicyImpl(String bucketName, String policyDocument) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.POLICY.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_TEXT_PLAIN); - Response response = performRestPut(bucketName, null, metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_TEXT_PLAIN, policyDocument), true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse deleteBucketPolicyImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.POLICY.getOriginalStringCode(), ""); - Response response = performRestDelete(bucketName, null, requestParameters); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - TransResult transInitiateMultipartUploadRequest(InitiateMultipartUploadRequest request) throws ServiceException { - Map headers = new HashMap(); - IHeaders iheaders = this.getIHeaders(); - IConvertor iconvertor = this.getIConvertor(); - - ObjectMetadata objectMetadata = request.getMetadata() == null ? new ObjectMetadata() : request.getMetadata(); - - for (Map.Entry entry : objectMetadata.getMetadata().entrySet()) { - String key = entry.getKey(); - if (!ServiceUtils.isValid(key)) { - continue; - } - key = key.trim(); - if ((this.canUseStandardHTTPHeaders.get() == null || (this.canUseStandardHTTPHeaders.get() != null - && !this.canUseStandardHTTPHeaders.get().booleanValue())) - && Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(key.toLowerCase())) - { - continue; - } - headers.put(key, entry.getValue() == null ? "" : entry.getValue().toString()); - } - - if (objectMetadata.getObjectStorageClass() != null) { - putHeader(headers, iheaders.storageClassHeader(), - iconvertor.transStorageClass(objectMetadata.getObjectStorageClass())); - } - - if (request.getExpires() > 0) { - putHeader(headers, iheaders.expiresHeader(), String.valueOf(request.getExpires())); - } - - if (ServiceUtils.isValid(objectMetadata.getWebSiteRedirectLocation())) { - putHeader(headers, iheaders.websiteRedirectLocationHeader(), objectMetadata.getWebSiteRedirectLocation()); - } - - if (ServiceUtils.isValid(request.getSuccessRedirectLocation())) { - putHeader(headers, iheaders.successRedirectLocationHeader(), request.getSuccessRedirectLocation()); - } - - if (ServiceUtils.isValid(objectMetadata.getContentEncoding())) { - headers.put(CommonHeaders.CONTENT_ENCODING, objectMetadata.getContentEncoding().trim()); - } - - transExtensionPermissions(request, headers); - - transSseHeaders(request, headers, iheaders); - - Object contentType = objectMetadata.getContentType() == null - ? objectMetadata.getValue(CommonHeaders.CONTENT_TYPE) - : objectMetadata.getContentType(); - if (contentType == null) { - contentType = Mimetypes.getInstance().getMimetype(request.getObjectKey()); - } - - String _contentType = contentType.toString().trim(); - headers.put(CommonHeaders.CONTENT_TYPE, _contentType); - - Map params = new HashMap(); - params.put(SpecialParamEnum.UPLOADS.getOriginalStringCode(), ""); - - return new TransResult(headers, params, null); - } - - protected InitiateMultipartUploadResult initiateMultipartUploadImpl(InitiateMultipartUploadRequest request) - throws ServiceException { - - TransResult result = this.transInitiateMultipartUploadRequest(request); - - this.prepareRESTHeaderAcl(result.getHeaders(), request.getAcl()); - - Response httpResponse = performRestPost(request.getBucketName(), request.getObjectKey(), result.getHeaders(), - result.getParams(), null, false); - - this.verifyResponseContentType(httpResponse); - - InitiateMultipartUploadResult multipartUpload = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), InitiateMultipartUploadHandler.class, true) - .getInitiateMultipartUploadResult(); - setResponseHeaders(multipartUpload, this.cleanResponseHeaders(httpResponse)); - setStatusCode(multipartUpload, httpResponse.code()); - return multipartUpload; - } - - protected HeaderResponse abortMultipartUploadImpl(String uploadId, String bucketName, String objectKey) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(ObsRequestParams.UPLOAD_ID, uploadId); - Response response = performRestDelete(bucketName, objectKey, requestParameters); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - - protected CompleteMultipartUploadResult completeMultipartUploadImpl(CompleteMultipartUploadRequest request) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(ObsRequestParams.UPLOAD_ID, request.getUploadId()); - - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - - Response response = performRestPost(request.getBucketName(), request.getObjectKey(), metadata, - requestParameters, createRequestBody(Mimetypes.MIMETYPE_XML, - this.getIConvertor().transCompleteMultipartUpload(request.getPartEtag())), - false); - - this.verifyResponseContentType(response); - - CompleteMultipartUploadHandler handler = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(response), CompleteMultipartUploadHandler.class, true); - - String versionId = response.header(this.getIHeaders().versionIdHeader()); - - CompleteMultipartUploadResult ret = new CompleteMultipartUploadResult(handler.getBucketName(), - handler.getObjectKey(), handler.getEtag(), handler.getLocation(), versionId, this.getObjectUrl(handler.getBucketName(), handler.getObjectKey())); - setResponseHeaders(ret, this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected MultipartUploadListing listMultipartUploadsImpl(ListMultipartUploadsRequest request) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.UPLOADS.getOriginalStringCode(), ""); - if (request.getPrefix() != null) { - requestParameters.put(ObsRequestParams.PREFIX, request.getPrefix()); - } - if (request.getDelimiter() != null) { - requestParameters.put(ObsRequestParams.DELIMITER, request.getDelimiter()); - } - if (request.getMaxUploads() != null) { - requestParameters.put(ObsRequestParams.MAX_UPLOADS, request.getMaxUploads().toString()); - } - if (request.getKeyMarker() != null) { - requestParameters.put(ObsRequestParams.KEY_MARKER, request.getKeyMarker()); - } - if (request.getUploadIdMarker() != null) { - requestParameters.put(ObsRequestParams.UPLOAD_ID_MARKER, request.getUploadIdMarker()); - } - - Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - ListMultipartUploadsHandler handler = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), ListMultipartUploadsHandler.class, true); - - MultipartUploadListing listResult = new MultipartUploadListing(handler.getBucketName() == null ? request.getBucketName() : handler.getBucketName(), - handler.getKeyMarker() == null ? request.getKeyMarker() : handler.getKeyMarker(), handler.getUploadIdMarker() == null ? request.getUploadIdMarker() : handler.getUploadIdMarker(), - handler.getNextKeyMarker(), handler.getNextUploadIdMarker(), handler.getPrefix() == null ? request.getPrefix() : handler.getPrefix(), handler.getMaxUploads(), handler.isTruncated(), handler.getMultipartUploadList(), - handler.getDelimiter() == null ? request.getDelimiter() : handler.getDelimiter(), handler.getCommonPrefixes().toArray(new String[handler.getCommonPrefixes().size()])); - setResponseHeaders(listResult, this.cleanResponseHeaders(httpResponse)); - setStatusCode(listResult, httpResponse.code()); - return listResult; - } - - protected ListPartsResult listPartsImpl(ListPartsRequest request) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(ObsRequestParams.UPLOAD_ID, request.getUploadId()); - if (null != request.getMaxParts()) { - requestParameters.put(ObsRequestParams.MAX_PARTS, request.getMaxParts().toString()); - } - if (null != request.getPartNumberMarker()) { - requestParameters.put(ObsRequestParams.PART_NUMBER_MARKER, request.getPartNumberMarker().toString()); - } - Response httpResponse = performRestGet(request.getBucketName(), request.getKey(), requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - ListPartsHandler handler = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), - ListPartsHandler.class, true); - - ListPartsResult result = new ListPartsResult(handler.getBucketName() == null ? request.getBucketName() : handler.getBucketName(), handler.getObjectKey() == null ? request.getKey() : handler.getObjectKey(), - handler.getUploadId() == null ? request.getUploadId() : handler.getUploadId(), handler.getInitiator(), handler.getOwner(), - StorageClassEnum.getValueFromCode(handler.getStorageClass()), handler.getMultiPartList(), handler.getMaxParts(), handler.isTruncated(), handler.getPartNumberMarker() == null - ? (request.getPartNumberMarker() == null ? null : request.getPartNumberMarker().toString()) - : handler.getPartNumberMarker(), handler.getNextPartNumberMarker()); - setResponseHeaders(result, this.cleanResponseHeaders(httpResponse)); - setStatusCode(result, httpResponse.code()); - return result; - } - - protected WebsiteConfiguration getBucketWebsiteConfigurationImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.WEBSITE.getOriginalStringCode(), ""); - - Response httpResponse = performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - WebsiteConfiguration ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketWebsiteConfigurationHandler.class, false) - .getWebsiteConfig(); - setResponseHeaders(ret, this.cleanResponseHeaders(httpResponse)); - setStatusCode(ret, httpResponse.code()); - return ret; - } - - protected HeaderResponse setBucketWebsiteConfigurationImpl(String bucketName, WebsiteConfiguration config) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.WEBSITE.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - - String xml = this.getIConvertor().transWebsiteConfiguration(config); - - Response response = performRestPut(bucketName, null, metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, xml), true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse deleteBucketWebsiteConfigurationImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.WEBSITE.getOriginalStringCode(), ""); - Response response = performRestDelete(bucketName, null, requestParameters); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected LifecycleConfiguration getBucketLifecycleConfigurationImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.LIFECYCLE.getOriginalStringCode(), ""); - - Response response = performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(response); - - LifecycleConfiguration ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(response), BucketLifecycleConfigurationHandler.class, false) - .getLifecycleConfig(); - setResponseHeaders(ret, this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse setBucketLifecycleConfigurationImpl(String bucketName, LifecycleConfiguration config) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.LIFECYCLE.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - String xml = this.getIConvertor().transLifecycleConfiguration(config); - metadata.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - - Response response = performRestPut(bucketName, null, metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, xml), true); - - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse deleteBucketLifecycleConfigurationImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.LIFECYCLE.getOriginalStringCode(), ""); - Response response = performRestDelete(bucketName, null, requestParameters); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected DeleteObjectsResult deleteObjectsImpl(DeleteObjectsRequest deleteObjectsRequest) throws ServiceException { - String xml = this.getIConvertor().transKeyAndVersion(deleteObjectsRequest.getKeyAndVersions(), - deleteObjectsRequest.isQuiet()); - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.DELETE.getOriginalStringCode(), ""); - - Response httpResponse = performRestPost(deleteObjectsRequest.getBucketName(), null, metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, xml), false); - this.verifyResponseContentType(httpResponse); +import com.obs.services.internal.Constants.CommonHeaders; +import com.obs.services.internal.security.BasicSecurityKey; +import com.obs.services.internal.service.ObsExtensionService; +import com.obs.services.internal.task.BlockRejectedExecutionHandler; +import com.obs.services.internal.task.DefaultTaskProgressStatus; +import com.obs.services.internal.utils.AbstractAuthentication; +import com.obs.services.internal.utils.RestUtils; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.internal.utils.V2Authentication; +import com.obs.services.internal.utils.V4Authentication; +import com.obs.services.model.AbstractBulkRequest; +import com.obs.services.model.AbstractTemporarySignatureRequest; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.DeleteObjectResult; +import com.obs.services.model.PolicyTempSignatureRequest; +import com.obs.services.model.PostSignatureRequest; +import com.obs.services.model.PostSignatureResponse; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.TaskCallback; +import com.obs.services.model.TaskProgressListener; +import com.obs.services.model.TemporarySignatureRequest; +import com.obs.services.model.TemporarySignatureResponse; +import com.obs.services.model.V4PostSignatureResponse; + +public class ObsService extends ObsExtensionService { + protected ObsService() { - DeleteObjectsResult ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), DeleteObjectsHandler.class, true) - .getMultipleDeleteResult(); - setResponseHeaders(ret, this.cleanResponseHeaders(httpResponse)); - setStatusCode(ret, httpResponse.code()); - return ret; - } + } - protected boolean headBucketImpl(String bucketName) throws ServiceException { - try { - performRestHead(bucketName, null, null, null); - return true; - } catch (ServiceException e) { - if (e.getResponseCode() == 404) { - return false; - } - throw e; - } - } - - protected HeaderResponse setBucketFSStatusImpl(SetBucketFSStatusRequest request) throws ServiceException{ - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.FILEINTERFACE.getOriginalStringCode(), ""); - String xml = this.getIConvertor().transBucketFileInterface(request.getStatus()); - Response response = performRestPut(request.getBucketName(), null, null, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, xml), true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - - protected TruncateFileResult truncateFileImpl(TruncateFileRequest request) throws ServiceException{ - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.TRUNCATE.getOriginalStringCode(), ""); - requestParameters.put(Constants.ObsRequestParams.LENGTH, String.valueOf(request.getNewLength())); - - Response response = performRestPut(request.getBucketName(), request.getObjectKey(), null, requestParameters, null, true); - TruncateFileResult result = new TruncateFileResult(); - setResponseHeaders(result, this.cleanResponseHeaders(response)); - setStatusCode(result, response.code()); - return result; - } + protected TemporarySignatureResponse createTemporarySignatureResponse(AbstractTemporarySignatureRequest request) + throws Exception { + String hostname = ServiceUtils.generateHostnameForBucket(request.getBucketName(), + this.isPathStyle(), this.getEndpoint()); + String virtualBucketPath = ""; + String uriPath = ""; + String objectKeyPath = (request.getObjectKey() != null) + ? RestUtils.encodeUrlPath(request.getObjectKey(), "/") : ""; + if (!this.getEndpoint().equals(hostname)) { + int subdomainOffset = hostname.lastIndexOf("." + this.getEndpoint()); + if (subdomainOffset > 0) { + virtualBucketPath = hostname.substring(0, subdomainOffset) + "/"; + } + uriPath = objectKeyPath; + } else { + uriPath = (((!ServiceUtils.isValid(request.getBucketName())) + ? "" : request.getBucketName().trim()) + "/" + objectKeyPath); + } - protected TruncateObjectResult truncateObjectImpl(TruncateObjectRequest request) throws ServiceException{ - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.TRUNCATE.getOriginalStringCode(), ""); - requestParameters.put(Constants.ObsRequestParams.LENGTH, String.valueOf(request.getNewLength())); + if (this.isCname()) { + hostname = this.getEndpoint(); + uriPath = objectKeyPath; + virtualBucketPath = this.getEndpoint() + "/"; + } - Response response = performRestPut(request.getBucketName(), request.getObjectKey(), null, requestParameters, null, true); - TruncateObjectResult result = new TruncateObjectResult(); - setResponseHeaders(result, this.cleanResponseHeaders(response)); - setStatusCode(result, response.code()); - return result; - } + uriPath += "?"; + if (request.getSpecialParam() != null) { + if (request.getSpecialParam() == SpecialParamEnum.STORAGECLASS + || request.getSpecialParam() == SpecialParamEnum.STORAGEPOLICY) { + request.setSpecialParam(this.getSpecialParamForStorageClass(request.getBucketName())); + } + uriPath += request.getSpecialParam().getOriginalStringCode() + "&"; + } - protected RenameResult renameFileImpl(RenameRequest request) throws ServiceException{ - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.RENAME.getOriginalStringCode(), ""); - requestParameters.put(Constants.ObsRequestParams.NAME, request.getNewObjectKey()); + BasicSecurityKey securityKey = this.getProviderCredentials().getSecurityKey(); + + uriPath = appendAccessKey(request.getBucketName(), uriPath, securityKey); - Response response = performRestPost(request.getBucketName(), request.getObjectKey(), null, requestParameters, null, true); - RenameResult result = new RenameResult(); - setResponseHeaders(result, this.cleanResponseHeaders(response)); - setStatusCode(result, response.code()); - return result; - } + String expiresOrPolicy = getExpiresParams(request); + uriPath = appendExpiresParams(request, uriPath, expiresOrPolicy); - protected RenameObjectResult renameObjectImpl(RenameObjectRequest request) throws ServiceException{ - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.RENAME.getOriginalStringCode(), ""); - requestParameters.put(Constants.ObsRequestParams.NAME, request.getNewObjectKey()); + uriPath = appendQueryParams(request, uriPath, securityKey); - Response response = performRestPost(request.getBucketName(), request.getObjectKey(), null, requestParameters, null, true); - RenameObjectResult result = new RenameObjectResult(); - setResponseHeaders(result, this.cleanResponseHeaders(response)); - setStatusCode(result, response.code()); - return result; - } - protected GetBucketFSStatusResult getBucketMetadataImpl(BucketMetadataInfoRequest bucketMetadataInfoRequest) - throws ServiceException { - GetBucketFSStatusResult output = null; - String origin = bucketMetadataInfoRequest.getOrigin(); - List requestHeaders = bucketMetadataInfoRequest.getRequestHeaders(); - if (origin != null && requestHeaders != null && requestHeaders.size() > 0) { - for (int i = 0; i < requestHeaders.size(); i++) { - String value = requestHeaders.get(i); - Map headers = new HashMap(); - headers.put(Constants.CommonHeaders.ORIGIN, origin); - headers.put(Constants.CommonHeaders.ACCESS_CONTROL_REQUEST_HEADERS, value); - Response response = performRestHead(bucketMetadataInfoRequest.getBucketName(), null, null, headers); + String resource = ""; + if (request instanceof TemporarySignatureRequest) { + resource = "/" + virtualBucketPath + uriPath; + } - if (output == null) { - output = this.getOptionInfoResult(response); - } else { - String header = response.header(Constants.CommonHeaders.ACCESS_CONTROL_ALLOW_HEADERS); - if (header != null) { - if (!output.getAllowHeaders().contains(header)) { - output.getAllowHeaders().add(header); - } - } - } - response.close(); - } - } else { - Map headers = new HashMap(); - if (origin != null) { - headers.put(Constants.CommonHeaders.ORIGIN, origin); - } - Response response = performRestHead(bucketMetadataInfoRequest.getBucketName(), null, null, headers); - output = this.getOptionInfoResult(response); - response.close(); - } + AbstractAuthentication authentication = + Constants.AUTHTICATION_MAP.get(this.getProviderCredentials().getLocalAuthType(request.getBucketName())); + if (authentication == null) { + authentication = V2Authentication.getInstance(); + } + + Map headers = new HashMap(); + headers.putAll(request.getHeaders()); + headers.put(CommonHeaders.HOST, + hostname + ":" + (this.getHttpsOnly() ? this.getHttpsPort() : this.getHttpPort())); + String requestMethod = request.getMethod() != null ? request.getMethod().getOperationType() : "GET"; + + Map actualSignedRequestHeaders = convertHeader(request.getBucketName(), headers, requestMethod); + String canonicalString = authentication.makeServiceCanonicalString(requestMethod, resource, + actualSignedRequestHeaders, expiresOrPolicy, Constants.ALLOWED_RESOURCE_PARAMTER_NAMES); - return output; - } + uriPath = appendSignature(uriPath, securityKey, canonicalString); - protected RestoreObjectStatus restoreObjectImpl(RestoreObjectRequest restoreObjectRequest) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.RESTORE.getOriginalStringCode(), ""); - if (restoreObjectRequest.getVersionId() != null) { - requestParameters.put(ObsRequestParams.VERSION_ID, restoreObjectRequest.getVersionId()); - } - Map metadata = new HashMap(); - String requestXmlElement = this.getIConvertor().transRestoreObjectRequest(restoreObjectRequest); - metadata.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(requestXmlElement)); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - Response response = this.performRestPost(restoreObjectRequest.getBucketName(), - restoreObjectRequest.getObjectKey(), metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, requestXmlElement), true); - RestoreObjectStatus ret = RestoreObjectStatus.valueOf(response.code()); - setResponseHeaders(ret, this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected RestoreObjectResult restoreObjectV2Impl(RestoreObjectRequest restoreObjectRequest) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.RESTORE.getOriginalStringCode(), ""); - if (restoreObjectRequest.getVersionId() != null) { - requestParameters.put(ObsRequestParams.VERSION_ID, restoreObjectRequest.getVersionId()); + String signedUrl; + if (this.getHttpsOnly()) { + signedUrl = "https://"; + } else { + signedUrl = "http://"; } - Map metadata = new HashMap(); - String requestXmlElement = this.getIConvertor().transRestoreObjectRequest(restoreObjectRequest); - metadata.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(requestXmlElement)); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - Response response = this.performRestPost(restoreObjectRequest.getBucketName(), - restoreObjectRequest.getObjectKey(), metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, requestXmlElement), true); - RestoreObjectResult ret = new RestoreObjectResult(restoreObjectRequest.getBucketName(), - restoreObjectRequest.getObjectKey(), restoreObjectRequest.getVersionId()); - setResponseHeaders(ret, this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; + signedUrl += headers.get(CommonHeaders.HOST) + "/" + uriPath; + TemporarySignatureResponse response = new TemporarySignatureResponse(signedUrl); + response.getActualSignedRequestHeaders().putAll(actualSignedRequestHeaders); + return response; } - protected BucketTagInfo getBucketTaggingImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.TAGGING.getOriginalStringCode(), ""); - Response httpResponse = this.performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - BucketTagInfo result = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketTagInfoHandler.class, false) - .getBucketTagInfo(); - setResponseHeaders(result, this.cleanResponseHeaders(httpResponse)); - setStatusCode(result, httpResponse.code()); - return result; - } - - protected HeaderResponse setBucketTaggingImpl(String bucketName, BucketTagInfo bucketTagInfo) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.TAGGING.getOriginalStringCode(), ""); - Map headers = new HashMap(); - - String requestXmlElement = this.getIConvertor().transBucketTagInfo(bucketTagInfo); - - headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(requestXmlElement)); - headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + private String appendExpiresParams(AbstractTemporarySignatureRequest request, String uriPath, + String expiresOrPolicy) { + if (request instanceof TemporarySignatureRequest) { + uriPath += "&Expires=" + expiresOrPolicy; + } else if (request instanceof PolicyTempSignatureRequest) { + uriPath += "&Policy=" + expiresOrPolicy; + } + return uriPath; + } - Response response = this.performRestPut(bucketName, null, headers, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, requestXmlElement), true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } + private String appendSignature(String uriPath, BasicSecurityKey securityKey, String canonicalString) { + String signedCanonical = ServiceUtils.signWithHmacSha1(securityKey.getSecretKey(), canonicalString); + String encodedCanonical = RestUtils.encodeUrlString(signedCanonical); + uriPath += "&Signature=" + encodedCanonical; + return uriPath; + } - protected HeaderResponse deleteBucketTaggingImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.TAGGING.getOriginalStringCode(), ""); - Response response = performRestDelete(bucketName, null, requestParameters); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected BucketEncryption getBucketEncryptionImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.ENCRYPTION.getOriginalStringCode(), ""); - - Response httpResponse = performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - BucketEncryption ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketEncryptionHandler.class, false).getEncryption(); - - setResponseHeaders(ret, this.cleanResponseHeaders(httpResponse)); - setStatusCode(ret, httpResponse.code()); - return ret; + private String appendAccessKey(String bucketName, String uriPath, BasicSecurityKey securityKey) { + String accessKeyIdPrefix = this.getProviderCredentials().getLocalAuthType(bucketName) + == AuthTypeEnum.OBS ? "AccessKeyId=" : "AWSAccessKeyId="; + uriPath += accessKeyIdPrefix + securityKey.getAccessKey(); + return uriPath; } - protected HeaderResponse setBucketEncryptionImpl(String bucketName, BucketEncryption encryption) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.ENCRYPTION.getOriginalStringCode(), ""); - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + private String getExpiresParams(AbstractTemporarySignatureRequest request) { + String expiresOrPolicy = ""; + if (request instanceof TemporarySignatureRequest) { + TemporarySignatureRequest tempRequest = (TemporarySignatureRequest) request; + long secondsSinceEpoch = tempRequest.getExpires() <= 0 ? ObsConstraint.DEFAULT_EXPIRE_SECONEDS + : tempRequest.getExpires(); + secondsSinceEpoch += System.currentTimeMillis() / 1000; + expiresOrPolicy = String.valueOf(secondsSinceEpoch); + } else if (request instanceof PolicyTempSignatureRequest) { + PolicyTempSignatureRequest policyRequest = (PolicyTempSignatureRequest) request; + String policy = policyRequest.generatePolicy(); + expiresOrPolicy = ServiceUtils.toBase64(policy.getBytes(StandardCharsets.UTF_8)); + } - String encryptAsXml = encryption == null ? "" : this.getIConvertor().transBucketEcryption(encryption); - metadata.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(encryptAsXml.length())); - Response response = performRestPut(bucketName, null, metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, encryptAsXml), true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; + return expiresOrPolicy; } - - protected HeaderResponse deleteBucketEncryptionImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.ENCRYPTION.getOriginalStringCode(), ""); - Response response = performRestDelete(bucketName, null, requestParameters); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; + + private Map convertHeader(String bucketName, Map headers, String requestMethod) { + Map actualSignedRequestHeaders = new TreeMap(); + + for (Map.Entry entry : headers.entrySet()) { + if (ServiceUtils.isValid(entry.getKey())) { + String key = entry.getKey().toLowerCase().trim(); + key = formatHeaderKey(bucketName, requestMethod, key); + + if (null != key) { + String value = entry.getValue() == null ? "" : entry.getValue().trim(); + if (key.startsWith(this.getRestMetadataPrefix(bucketName))) { + value = RestUtils.uriEncode(value, true); + } + actualSignedRequestHeaders.put(entry.getKey().trim(), value); + } + } + } + + return actualSignedRequestHeaders; } - protected ReplicationConfiguration getBucketReplicationConfigurationImpl(String bucketName) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.REPLICATION.getOriginalStringCode(), ""); - Response httpResponse = this.performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - ReplicationConfiguration result = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketReplicationConfigurationHandler.class, - false) - .getReplicationConfiguration(); - setResponseHeaders(result, this.cleanResponseHeaders(httpResponse)); - setStatusCode(result, httpResponse.code()); - return result; - } - - protected HeaderResponse setBucketReplicationConfigurationImpl(String bucketName, - ReplicationConfiguration replicationConfiguration) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.REPLICATION.getOriginalStringCode(), ""); - Map headers = new HashMap(); - - String requestXmlElement = this.getIConvertor().transReplicationConfiguration(replicationConfiguration); - - headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(requestXmlElement)); - headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - - Response response = this.performRestPut(bucketName, null, headers, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, requestXmlElement), true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse deleteBucketReplicationConfigurationImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.REPLICATION.getOriginalStringCode(), ""); - Response response = performRestDelete(bucketName, null, requestParameters); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected TemporarySignatureResponse _createTemporarySignature(AbstractTemporarySignatureRequest request) throws Exception { - String requestMethod = request.getMethod() != null ? request.getMethod().getOperationType() : "GET"; - - Map queryParams = new TreeMap(); - queryParams.putAll(request.getQueryParams()); - BasicSecurityKey securityKey = this.getProviderCredentials().getSecurityKey(); - String accessKey = securityKey.getAccessKey(); - String secretKey = securityKey.getSecretKey(); - String securityToken = securityKey.getSecurityToken(); - if (!queryParams.containsKey(this.getIHeaders().securityTokenHeader())) { - if (ServiceUtils.isValid(securityToken)) { - queryParams.put(this.getIHeaders().securityTokenHeader(), securityToken); - } - } - - String endpoint = this.getEndpoint(); - String bucketName = request.getBucketName(); - String objectKey = request.getObjectKey(); - String hostname = ServiceUtils.generateHostnameForBucket(bucketName, this.isPathStyle(), endpoint); - String virtualBucketPath = ""; - String uriPath = ""; - String objectKeyPath = (objectKey != null) ? RestUtils.encodeUrlPath(objectKey, "/") : ""; - if (!endpoint.equals(hostname)) { - int subdomainOffset = hostname.lastIndexOf("." + endpoint); - if (subdomainOffset > 0) { - virtualBucketPath = hostname.substring(0, subdomainOffset) + "/"; + /** + * 拼接query参数 + * @param request + * @param uriPath + * @param securityKey + * @return + */ + private String appendQueryParams(AbstractTemporarySignatureRequest request, String uriPath, + BasicSecurityKey securityKey) { + StringBuilder temp = new StringBuilder(uriPath); + Map queryParams = new TreeMap(); + queryParams.putAll(request.getQueryParams()); + String securityToken = securityKey.getSecurityToken(); + if (!queryParams.containsKey(this.getIHeaders(request.getBucketName()).securityTokenHeader())) { + if (ServiceUtils.isValid(securityToken)) { + queryParams.put(this.getIHeaders(request.getBucketName()).securityTokenHeader(), securityToken); } - uriPath = objectKeyPath; - } else { - uriPath = (((!ServiceUtils.isValid(bucketName)) ? "" : bucketName.trim()) + "/" + objectKeyPath); } - if(this.isCname()) { - hostname = endpoint; - uriPath = objectKeyPath; - virtualBucketPath = endpoint + "/"; - } - - uriPath += "?"; - if (request.getSpecialParam() != null) { - if(request.getSpecialParam() == SpecialParamEnum.STORAGECLASS || request.getSpecialParam() == SpecialParamEnum.STORAGEPOLICY) { - request.setSpecialParam(this.getSpecialParamForStorageClass()); - } - uriPath += request.getSpecialParam().getOriginalStringCode() + "&"; - } - - String accessKeyIdPrefix = this.getProviderCredentials().getAuthType() == AuthTypeEnum.OBS ? "AccessKeyId=" : "AWSAccessKeyId="; - uriPath += accessKeyIdPrefix + accessKey; - - String expiresOrPolicy = ""; - String uriExpiresOrPolicy = ""; - if (request instanceof TemporarySignatureRequest) { - TemporarySignatureRequest tempRequest = (TemporarySignatureRequest)request; - long secondsSinceEpoch = tempRequest.getExpires() <= 0 ? ObsConstraint.DEFAULT_EXPIRE_SECONEDS : tempRequest.getExpires(); - secondsSinceEpoch += System.currentTimeMillis() / 1000; - expiresOrPolicy = String.valueOf(secondsSinceEpoch); - uriExpiresOrPolicy = "&Expires=" + expiresOrPolicy; - } else if(request instanceof PolicyTempSignatureRequest) { - PolicyTempSignatureRequest policyRequest = (PolicyTempSignatureRequest)request; - String policy = policyRequest.generatePolicy(); - expiresOrPolicy = ServiceUtils.toBase64(policy.getBytes(Constants.DEFAULT_ENCODING)); - uriExpiresOrPolicy = "&Policy=" + expiresOrPolicy; - } - uriPath += uriExpiresOrPolicy; - - StringBuilder temp = new StringBuilder(uriPath); - for (Map.Entry entry : queryParams.entrySet()) { + for (Map.Entry entry : queryParams.entrySet()) { if (entry.getKey() != null && entry.getValue() != null) { String key = RestUtils.uriEncode(entry.getKey(), false); - + temp.append("&"); temp.append(key); temp.append("="); @@ -901,2166 +223,322 @@ protected TemporarySignatureResponse _createTemporarySignature(AbstractTemporary temp.append(value); } } - uriPath = temp.toString(); - - Map headers = new HashMap(); - headers.putAll(request.getHeaders()); - headers.put(CommonHeaders.HOST, hostname + ":" + (this.getHttpsOnly() ? this.getHttpsPort() : this.getHttpPort())); - Map actualSignedRequestHeaders = new TreeMap(); - for (Map.Entry entry : headers.entrySet()) { - if (ServiceUtils.isValid(entry.getKey())) { - String key = entry.getKey().toLowerCase().trim(); - boolean validKey = false; - if (Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(key) - || key.startsWith(this.getRestHeaderPrefix()) || key.startsWith(Constants.OBS_HEADER_PREFIX)) { - validKey = true; - } else if (requestMethod.equals("PUT") || requestMethod.equals("POST")) { - key = this.getRestMetadataPrefix() + key; - validKey = true; - } - if (validKey) { - String value = entry.getValue() == null ? "" : entry.getValue().trim(); - if (key.startsWith(this.getRestMetadataPrefix())) { - value = RestUtils.uriEncode(value, true); - } - actualSignedRequestHeaders.put(entry.getKey().trim(), value); - } - } - } - - String resource = ""; - if (request instanceof TemporarySignatureRequest) { - resource = "/" + virtualBucketPath + uriPath; - } - - AbstractAuthentication authentication = this.getAuthentication(); - if (authentication == null) { - authentication = V2Authentication.getInstance(); - } - String canonicalString = authentication.makeServiceCanonicalString(requestMethod, resource, - actualSignedRequestHeaders, expiresOrPolicy, Constants.ALLOWED_RESOURCE_PARAMTER_NAMES); - if (log.isDebugEnabled()) { - log.debug("CanonicalString is :" + canonicalString); - } - - String signedCanonical = ServiceUtils.signWithHmacSha1(secretKey, canonicalString); - String encodedCanonical = RestUtils.encodeUrlString(signedCanonical); - uriPath += "&Signature=" + encodedCanonical; - - String signedUrl; - if (this.getHttpsOnly()) { - signedUrl = "https://"; - } else { - signedUrl = "http://"; - } - signedUrl += headers.get(CommonHeaders.HOST) + "/" + uriPath; - TemporarySignatureResponse response = new TemporarySignatureResponse(signedUrl); - response.getActualSignedRequestHeaders().putAll(actualSignedRequestHeaders); - return response; - } - - protected PostSignatureResponse _createPostSignature(PostSignatureRequest request, boolean isV4) throws Exception { - BasicSecurityKey securityKey = this.getProviderCredentials().getSecurityKey(); - String accessKey = securityKey.getAccessKey(); - String secretKey = securityKey.getSecretKey(); - String securityToken = securityKey.getSecurityToken(); - Date requestDate = request.getRequestDate() != null ? request.getRequestDate() : new Date(); - SimpleDateFormat expirationDateFormat = ServiceUtils.getExpirationDateFormat(); - Date expiryDate = request.getExpiryDate() == null - ? new Date(requestDate.getTime() + (request.getExpires() <=0 ? ObsConstraint.DEFAULT_EXPIRE_SECONEDS : request.getExpires()) * 1000) - : request.getExpiryDate(); - - String expiration = expirationDateFormat.format(expiryDate); - - StringBuilder originPolicy = new StringBuilder(); - originPolicy.append("{\"expiration\":").append("\"").append(expiration).append("\",") - .append("\"conditions\":["); - - String shortDate = ServiceUtils.getShortDateFormat().format(requestDate); - String longDate = ServiceUtils.getLongDateFormat().format(requestDate); - String credential = this.getCredential(shortDate, accessKey); - if (request.getConditions() != null && !request.getConditions().isEmpty()) { - originPolicy.append(ServiceUtils.join(request.getConditions(), ",")).append(","); - } else { - Map params = new TreeMap(); - if(isV4) { - params.put(Constants.V2_HEADER_PREFIX_CAMEL + "Algorithm", Constants.V4_ALGORITHM); - params.put(Constants.V2_HEADER_PREFIX_CAMEL + "Date", longDate); - params.put(Constants.V2_HEADER_PREFIX_CAMEL + "Credential", credential); - } - - params.putAll(request.getFormParams()); - - if (!params.containsKey(this.getIHeaders().securityTokenHeader())) { - if (ServiceUtils.isValid(securityToken)) { - params.put(this.getIHeaders().securityTokenHeader(), securityToken); - } - } - - if (ServiceUtils.isValid(request.getBucketName())) { - params.put("bucket", request.getBucketName()); - } - - if (ServiceUtils.isValid(request.getObjectKey())) { - params.put("key", request.getObjectKey()); - } - - boolean matchAnyBucket = true; - boolean matchAnyKey = true; - - for (Map.Entry entry : params.entrySet()) { - if (ServiceUtils.isValid(entry.getKey())) { - String key = entry.getKey().toLowerCase().trim(); - - if (key.equals("bucket")) { - matchAnyBucket = false; - } else if (key.equals("key")) { - matchAnyKey = false; - } - - if (!Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(key) - && !key.startsWith(this.getRestHeaderPrefix()) && !key.startsWith(Constants.OBS_HEADER_PREFIX) && !key.equals("acl") - && !key.equals("bucket") && !key.equals("key") && !key.equals("success_action_redirect") - && !key.equals("redirect") && !key.equals("success_action_status")) { - continue; - } - String value = entry.getValue() == null ? "" : entry.getValue().toString(); - originPolicy.append("{\"").append(key).append("\":").append("\"").append(value).append("\"},"); - } - } + return temp.toString(); + } - if (matchAnyBucket) { - originPolicy.append("[\"starts-with\", \"$bucket\", \"\"],"); - } + protected PostSignatureResponse createPostSignatureResponse(PostSignatureRequest request, boolean isV4) + throws Exception { + BasicSecurityKey securityKey = this.getProviderCredentials().getSecurityKey(); + String expiration = getFormatExpiration(request); + + StringBuilder originPolicy = new StringBuilder(); + originPolicy.append("{\"expiration\":").append("\"").append(expiration).append("\",") + .append("\"conditions\":["); + + Date requestDate = request.getRequestDate() != null ? request.getRequestDate() : new Date(); + String shortDate = ServiceUtils.getShortDateFormat().format(requestDate); + String longDate = ServiceUtils.getLongDateFormat().format(requestDate); + String credential = this.getCredential(shortDate, securityKey.getAccessKey()); + if (request.getConditions() != null && !request.getConditions().isEmpty()) { + originPolicy.append(ServiceUtils.join(request.getConditions(), ",")).append(","); + } else { + appendOriginPolicy(request, isV4, securityKey, originPolicy, longDate, credential); + } - if (matchAnyKey) { - originPolicy.append("[\"starts-with\", \"$key\", \"\"],"); - } + if (originPolicy.length() > 0 && originPolicy.charAt(originPolicy.length() - 1) == ',') { + originPolicy.deleteCharAt(originPolicy.length() - 1); + } - } + originPolicy.append("]}"); + String policy = ServiceUtils.toBase64(originPolicy.toString().getBytes(StandardCharsets.UTF_8)); - originPolicy.append("]}"); - String policy = ServiceUtils.toBase64(originPolicy.toString().getBytes(Constants.DEFAULT_ENCODING)); - - if(isV4) { - String signature = V4Authentication.caculateSignature(policy, shortDate, secretKey); - return new V4PostSignatureResponse(policy, originPolicy.toString(), - Constants.V4_ALGORITHM, credential, longDate, signature, expiration); - }else { - String signature = AbstractAuthentication.caculateSignature(policy, secretKey); - return new PostSignatureResponse(policy, originPolicy.toString(), signature, expiration, accessKey); - } - - } + if (isV4) { + String signature = V4Authentication.caculateSignature(policy, shortDate, securityKey.getSecretKey()); + return new V4PostSignatureResponse(policy, originPolicy.toString(), Constants.V4_ALGORITHM, credential, + longDate, signature, expiration); + } else { + String signature = AbstractAuthentication.calculateSignature(policy, securityKey.getSecretKey()); + return new PostSignatureResponse(policy, originPolicy.toString(), + signature, expiration, securityKey.getAccessKey()); + } - protected TemporarySignatureResponse createV4TemporarySignature(TemporarySignatureRequest request) throws Exception { - StringBuilder canonicalUri = new StringBuilder(); - String bucketName = request.getBucketName(); - String endpoint = this.getEndpoint(); - String objectKey = request.getObjectKey(); + } - if (!this.isCname()) { - if (ServiceUtils.isValid(bucketName)) { - if (this.isPathStyle() || !ServiceUtils.isBucketNameValidDNSName(bucketName)) { - canonicalUri.append("/").append(bucketName.trim()); - } else { - endpoint = bucketName.trim() + "." + endpoint; - } - if (ServiceUtils.isValid(objectKey)) { - canonicalUri.append("/").append(RestUtils.uriEncode(objectKey, false)); - } - } - } else { - if (ServiceUtils.isValid(objectKey)) { - canonicalUri.append("/").append(RestUtils.uriEncode(objectKey, false)); + private void appendOriginPolicy(PostSignatureRequest request, boolean isV4, BasicSecurityKey securityKey, + StringBuilder originPolicy, String longDate, String credential) { + Map params = prepareSignatureParameters(request, isV4, + securityKey.getSecurityToken(), longDate, credential); + + boolean matchAnyBucket = true; + boolean matchAnyKey = true; + + for (Map.Entry entry : params.entrySet()) { + if (ServiceUtils.isValid(entry.getKey())) { + String key = entry.getKey().toLowerCase().trim(); + + if (key.equals("bucket")) { + matchAnyBucket = false; + } else if (key.equals("key")) { + matchAnyKey = false; + } + + if (!Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(key) + && !key.startsWith(this.getRestHeaderPrefix(request.getBucketName())) + && !key.startsWith(Constants.OBS_HEADER_PREFIX) && !key.equals("acl") + && !key.equals("bucket") && !key.equals("key") && !key.equals("success_action_redirect") + && !key.equals("redirect") && !key.equals("success_action_status")) { + continue; + } + String value = entry.getValue() == null ? "" : entry.getValue().toString(); + originPolicy.append("{\"").append(key).append("\":").append("\"").append(value).append("\"},"); } - } - - - if(this.isCname()) { - endpoint = this.getEndpoint(); - } - - Map headers = new TreeMap(); - headers.putAll(request.getHeaders()); - Map queryParams = new TreeMap(); - queryParams.putAll(request.getQueryParams()); - - Date requestDate = request.getRequestDate(); - if (requestDate == null) { - requestDate = new Date(); - } - if((this.getHttpsOnly() && this.getHttpsPort() == 443) || (!this.getHttpsOnly() && this.getHttpPort() == 80)) { - headers.put(CommonHeaders.HOST, endpoint); - }else { - headers.put(CommonHeaders.HOST, endpoint + ":" + (this.getHttpsOnly() ? this.getHttpsPort() : this.getHttpPort())); - } - - BasicSecurityKey securityKey = this.getProviderCredentials().getSecurityKey(); - String accessKey = securityKey.getAccessKey(); - String secretKey = securityKey.getSecretKey(); - String securityToken = securityKey.getSecurityToken(); - if (!queryParams.containsKey(this.getIHeaders().securityTokenHeader())) { - if (ServiceUtils.isValid(securityToken)) { - queryParams.put(this.getIHeaders().securityTokenHeader(), securityToken); - } - } - - String requestMethod = request.getMethod() != null ? request.getMethod().getOperationType() : "GET"; - - StringBuilder signedHeaders = new StringBuilder(); - StringBuilder canonicalHeaders = new StringBuilder(); - int index = 0; - Map actualSignedRequestHeaders = new TreeMap(); - for (Map.Entry entry : headers.entrySet()) { - if (ServiceUtils.isValid(entry.getKey())) { - String key = entry.getKey().toLowerCase().trim(); - boolean validKey = false; - if (Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(key) - || key.startsWith(this.getRestHeaderPrefix()) || key.startsWith(Constants.OBS_HEADER_PREFIX)) { - validKey = true; - } else if (requestMethod.equals("PUT") || requestMethod.equals("POST")) { - key = this.getRestMetadataPrefix() + key; - validKey = true; - } - if (validKey) { - String value = entry.getValue() == null ? "" : entry.getValue().trim(); - if (key.startsWith(this.getRestMetadataPrefix())) { - value = RestUtils.uriEncode(value, true); - } - signedHeaders.append(key); - canonicalHeaders.append(key).append(":").append(value).append("\n"); - if (index++ != headers.size() - 1) { - signedHeaders.append(";"); - } - actualSignedRequestHeaders.put(entry.getKey().trim(), value); - } - } - } - - String shortDate = ServiceUtils.getShortDateFormat().format(requestDate); - String longDate = ServiceUtils.getLongDateFormat().format(requestDate); - - queryParams.put(Constants.V2_HEADER_PREFIX_CAMEL + "Algorithm", Constants.V4_ALGORITHM); - queryParams.put(Constants.V2_HEADER_PREFIX_CAMEL + "Credential", this.getCredential(shortDate, accessKey)); - queryParams.put(Constants.V2_HEADER_PREFIX_CAMEL + "Date", longDate); - queryParams.put(Constants.V2_HEADER_PREFIX_CAMEL + "Expires", request.getExpires() <= 0 ? ObsConstraint.DEFAULT_EXPIRE_SECONEDS : request.getExpires()); - queryParams.put(Constants.V2_HEADER_PREFIX_CAMEL + "SignedHeaders", signedHeaders.toString()); - - StringBuilder canonicalQueryString = new StringBuilder(); - - StringBuilder signedUrl = new StringBuilder(); - if (this.getHttpsOnly()) { - String securePortStr = this.getHttpsPort() == 443 ? "" : ":" + this.getHttpsPort(); - signedUrl.append("https://").append(endpoint).append(securePortStr); - } else { - String insecurePortStr = this.getHttpPort() == 80 ? "" : ":" + this.getHttpPort(); - signedUrl.append("http://").append(endpoint).append(insecurePortStr); - } - signedUrl.append(canonicalUri).append("?"); - - if (request.getSpecialParam() != null) { - if(request.getSpecialParam() == SpecialParamEnum.STORAGECLASS || request.getSpecialParam() == SpecialParamEnum.STORAGEPOLICY) { - request.setSpecialParam(this.getSpecialParamForStorageClass()); - } - queryParams.put(request.getSpecialParam().getOriginalStringCode(), null); - } - - index = 0; - for (Map.Entry entry : queryParams.entrySet()) { - if (ServiceUtils.isValid(entry.getKey())) { - String key = RestUtils.uriEncode(entry.getKey(), false); - - canonicalQueryString.append(key).append("="); - signedUrl.append(key); - if (entry.getValue() != null) { - String value = RestUtils.uriEncode(entry.getValue().toString(), false); - canonicalQueryString.append(value); - signedUrl.append("=").append(value); - } else { - canonicalQueryString.append(""); - } - if (index++ != queryParams.size() - 1) { - canonicalQueryString.append("&"); - signedUrl.append("&"); - } - } - } - - StringBuilder canonicalRequest = new StringBuilder(requestMethod).append("\n") - .append(canonicalUri.length() == 0 ? "/" : canonicalUri).append("\n").append(canonicalQueryString) - .append("\n").append(canonicalHeaders).append("\n").append(signedHeaders).append("\n") - .append("UNSIGNED-PAYLOAD"); - - StringBuilder stringToSign = new StringBuilder(Constants.V4_ALGORITHM).append("\n").append(longDate) - .append("\n").append(shortDate).append("/").append(ObsConstraint.DEFAULT_BUCKET_LOCATION_VALUE) - .append("/").append(Constants.SERVICE).append("/").append(Constants.REQUEST_TAG).append("\n") - .append(V4Authentication.byteToHex((V4Authentication.sha256encode(canonicalRequest.toString())))); - signedUrl.append("&").append(Constants.V2_HEADER_PREFIX_CAMEL).append("Signature=") - .append(V4Authentication.caculateSignature(stringToSign.toString(), shortDate, secretKey)); - TemporarySignatureResponse response = new TemporarySignatureResponse(signedUrl.toString()); - response.getActualSignedRequestHeaders().putAll(actualSignedRequestHeaders); - return response; - } - - protected String getCredential(String shortDate, String accessKey) { - return new StringBuilder(accessKey).append("/").append(shortDate).append("/") - .append(ObsConstraint.DEFAULT_BUCKET_LOCATION_VALUE).append("/").append(Constants.SERVICE).append("/") - .append(Constants.REQUEST_TAG).toString(); - } - - GetBucketFSStatusResult getOptionInfoResult(Response response) { - - Headers headers = response.headers(); - - Map> map = headers.toMultimap(); - String maxAge = headers.get(Constants.CommonHeaders.ACCESS_CONTROL_MAX_AGE); - - IHeaders iheaders = this.getIHeaders(); - FSStatusEnum status = FSStatusEnum.getValueFromCode(headers.get(iheaders.fsFileInterfaceHeader())); - - BucketTypeEnum bucketType = BucketTypeEnum.OBJECT; - if (FSStatusEnum.ENABLED == status) - { - bucketType = BucketTypeEnum.PFS; - } - - GetBucketFSStatusResult output = new GetBucketFSStatusResult(headers.get(Constants.CommonHeaders.ACCESS_CONTROL_ALLOW_ORIGIN), map.get(Constants.CommonHeaders.ACCESS_CONTROL_ALLOW_HEADERS), maxAge == null ? 0 :Integer.parseInt(maxAge) , map.get(Constants.CommonHeaders.ACCESS_CONTROL_ALLOW_METHODS), map.get(Constants.CommonHeaders.ACCESS_CONTROL_EXPOSE_HEADERS), - StorageClassEnum.getValueFromCode(headers.get(iheaders.defaultStorageClassHeader())), headers.get(iheaders.bucketRegionHeader()), headers.get(iheaders.serverVersionHeader()), status, - AvailableZoneEnum.getValueFromCode(headers.get(iheaders.azRedundancyHeader())), headers.get(iheaders.epidHeader()), bucketType); - - setResponseHeaders(output, this.cleanResponseHeaders(response)); - setStatusCode(output, response.code()); - return output; - } - - protected BucketMetadataInfoResult optionsImpl(String bucketName, String ObjectName, OptionsInfoRequest option) - throws ServiceException { - Map metadata = new IdentityHashMap(); - - if (ServiceUtils.isValid(option.getOrigin())) { - metadata.put(CommonHeaders.ORIGIN, option.getOrigin().trim()); - } - - for (int i = 0; option.getRequestMethod() != null && i < option.getRequestMethod().size(); i++) { - metadata.put(new String(CommonHeaders.ACCESS_CONTROL_REQUEST_METHOD), option.getRequestMethod().get(i)); - } - for (int i = 0; option.getRequestHeaders() != null && i < option.getRequestHeaders().size(); i++) { - metadata.put(new String(CommonHeaders.ACCESS_CONTROL_REQUEST_HEADERS), option.getRequestHeaders().get(i)); - } - - Response rsult = performRestOptions(bucketName, ObjectName, metadata, null, true); - return getOptionInfoResult(rsult); - - } - - protected HeaderResponse deleteBucketCorsImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.CORS.getOriginalStringCode(), ""); - - Response response = performRestDelete(bucketName, null, requestParameters); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected BucketCors getBucketCorsImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.CORS.getOriginalStringCode(), ""); - Response httpResponse = performRestGet(bucketName, null, requestParameters, null); - this.verifyResponseContentType(httpResponse); - BucketCors ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketCorsHandler.class, false) - .getConfiguration(); - setResponseHeaders(ret, this.cleanResponseHeaders(httpResponse)); - setStatusCode(ret, httpResponse.code()); - return ret; - - } - - protected HeaderResponse setBucketCorsImpl(String bucketName, BucketCors bucketCors) throws ServiceException { - String corsXML = bucketCors == null ? "" : this.getIConvertor().transBucketCors(bucketCors); - - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.CORS.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - metadata.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(corsXML)); - - metadata.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(corsXML.length())); - Response response = performRestPut(bucketName, null, metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, corsXML), true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse setBucketQuotaImpl(String bucketName, BucketQuota quota) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.QUOTA.getOriginalStringCode(), ""); - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - - String quotaAsXml = quota == null ? "" : this.getIConvertor().transBucketQuota(quota); - metadata.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(quotaAsXml.length())); - Response response = performRestPut(bucketName, null, metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, quotaAsXml), true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse setBucketStorageImpl(String bucketName, - BucketStoragePolicyConfiguration storagePolicy) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(this.getSpecialParamForStorageClass().getOriginalStringCode(), ""); - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - String xml = storagePolicy == null ? "" : this.getIConvertor().transStoragePolicy(storagePolicy); - metadata.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(xml.length())); - Response response = performRestPut(bucketName, null, metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, xml), true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - void putAclImpl(String bucketName, String objectKey, AccessControlList acl, String versionId) - throws ServiceException { - if (acl != null) { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.ACL.getOriginalStringCode(), ""); - if (versionId != null) { - requestParameters.put(Constants.ObsRequestParams.VERSION_ID, versionId); - } - - Map metadata = new HashMap(); - metadata.put(Constants.CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - String aclAsXml = this.getIConvertor().transAccessControlList(acl, !ServiceUtils.isValid(objectKey)); - metadata.put(Constants.CommonHeaders.CONTENT_LENGTH, String.valueOf(aclAsXml.length())); - performRestPut(bucketName, objectKey, metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, aclAsXml), true); - } - } - - protected HeaderResponse setObjectAclImpl(String bucketName, String objectKey, String cannedACL, - AccessControlList acl, String versionId) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.ACL.getOriginalStringCode(), ""); - if (versionId != null) { - requestParameters.put(ObsRequestParams.VERSION_ID, versionId); - } - RequestBody entity = null; - if (ServiceUtils.isValid(cannedACL)) { - acl = this.getIConvertor().transCannedAcl(cannedACL.trim()); - } - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - boolean isExtraAclPutRequired = !prepareRESTHeaderAclObject(metadata, acl); - if (isExtraAclPutRequired) { - String aclAsXml = acl == null ? "" : this.getIConvertor().transAccessControlList(acl, false); - metadata.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(aclAsXml.length())); - entity = createRequestBody(Mimetypes.MIMETYPE_XML, aclAsXml); - } - Response response = performRestPut(bucketName, objectKey, metadata, requestParameters, entity, true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse setBucketAclImpl(String bucketName, String cannedACL, AccessControlList acl) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.ACL.getOriginalStringCode(), ""); - RequestBody entity = null; - if (ServiceUtils.isValid(cannedACL)) { - acl = this.getIConvertor().transCannedAcl(cannedACL.trim()); - } - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - boolean isExtraAclPutRequired = !prepareRESTHeaderAcl(metadata, acl); - if (isExtraAclPutRequired) { - String aclAsXml = acl == null ? "" : this.getIConvertor().transAccessControlList(acl, true); - metadata.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(aclAsXml.length())); - entity = createRequestBody(Mimetypes.MIMETYPE_XML, aclAsXml); - } - Response response = performRestPut(bucketName, null, metadata, requestParameters, entity, true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - AbstractAuthentication getAuthentication() { - return Constants.AUTHTICATION_MAP.get(this.getProviderCredentials().getAuthType()); - } - - SpecialParamEnum getSpecialParamForStorageClass() { - return this.getProviderCredentials().getAuthType() == AuthTypeEnum.OBS ? SpecialParamEnum.STORAGECLASS - : SpecialParamEnum.STORAGEPOLICY; - } - - void putHeader(Map headers, String key, String value) { - if (ServiceUtils.isValid(key)) { - headers.put(key, value); - } - } - - TransResult transCreateBucketRequest(CreateBucketRequest request) throws ServiceException { - Map headers = new HashMap(); - IConvertor convertor = this.getIConvertor(); - - if (request.getBucketStorageClass() != null) { - putHeader(headers, getIHeaders().defaultStorageClassHeader(), - convertor.transStorageClass(request.getBucketStorageClass())); - } - - if (request.getEpid() != null) { - putHeader(headers, getIHeaders().epidHeader(), request.getEpid()); } - - if (request instanceof NewBucketRequest) { - putHeader(headers, getIHeaders().fsFileInterfaceHeader(), Constants.ENABLED); - } - - if (null != request.getBucketType() && BucketTypeEnum.PFS == request.getBucketType()) - { - putHeader(headers, getIHeaders().fsFileInterfaceHeader(), Constants.ENABLED); - } - - if(request.getAvailableZone() != null) { - putHeader(headers, getIHeaders().azRedundancyHeader(), request.getAvailableZone().getCode()); - } - - Set extensionPermissionEnums = request.getAllGrantPermissions(); - if (!extensionPermissionEnums.isEmpty()) { - for (ExtensionBucketPermissionEnum extensionPermissionEnum : extensionPermissionEnums) { - Set domainIds = request.getDomainIdsByGrantPermission(extensionPermissionEnum); - List domainIdList = new ArrayList(domainIds.size()); - for (String domainId : domainIds) { - domainIdList.add("id=" + domainId); - } - putHeader(headers, getHeaderByMethodName(extensionPermissionEnum.getCode()), - ServiceUtils.join(domainIdList, ",")); - } - } - - if (request.getExtensionHeaderMap() != null) { - for (Entry kv : request.getExtensionHeaderMap().entrySet()) { - putHeader(headers, kv.getKey(), kv.getValue()); - } - } - - String contentType = Mimetypes.MIMETYPE_XML; - headers.put(Constants.CommonHeaders.CONTENT_TYPE, contentType); - TransResult result = new TransResult(headers); - if (ServiceUtils.isValid(request.getLocation())) { - String configXml = convertor.transBucketLoction(request.getLocation()); - headers.put(Constants.CommonHeaders.CONTENT_LENGTH, String.valueOf(configXml.length())); - RequestBody requestEntity = createRequestBody(contentType, configXml); - result.body = requestEntity; - } - return result; - } - - String getHeaderByMethodName(String code) { - try { - IHeaders iheaders = this.getIHeaders(); - Method m = iheaders.getClass().getMethod(code); - Object result = m.invoke(iheaders); - return result == null ? "" : result.toString(); - } catch (Exception e) { - if (log.isWarnEnabled()) { - log.warn("Invoke getHeaderByMethodName error", e); - } - } - return null; - } - - protected ObsBucket createBucketImpl(CreateBucketRequest request) throws ServiceException { - TransResult result = this.transCreateBucketRequest(request); - String bucketName = request.getBucketName(); - AccessControlList acl = request.getAcl(); - - boolean isExtraAclPutRequired = !prepareRESTHeaderAcl(result.getHeaders(), acl); - - Response response = performRestPut(bucketName, null, result.getHeaders(), null, result.body, true); - - if (isExtraAclPutRequired && acl != null) { - if (log.isDebugEnabled()) { - log.debug("Creating bucket with a non-canned ACL using REST, so an extra ACL Put is required"); - } - try { - putAclImpl(bucketName, null, acl, null); - } catch (Exception e) { - if (log.isWarnEnabled()) { - log.warn("Try to set bucket acl error", e); - } - } - } - - Map map = this.cleanResponseHeaders(response); - ObsBucket bucket = new ObsBucket(); - bucket.setBucketName(bucketName); - bucket.setLocation(request.getLocation()); - bucket.setAcl(acl); - bucket.setBucketStorageClass(request.getBucketStorageClass()); - setResponseHeaders(bucket, map); - setStatusCode(bucket, response.code()); - return bucket; - } - - boolean prepareRESTHeaderAclForV2(Map metadata, AccessControlList acl) { - String restHeaderAclValue = null; - if (acl == AccessControlList.REST_CANNED_PRIVATE) { - restHeaderAclValue = Constants.ACL_PRIVATE; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_DELIVERED) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE_DELIVERED) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE; - } else if (acl == AccessControlList.REST_CANNED_AUTHENTICATED_READ) { - restHeaderAclValue = Constants.ACL_AUTHENTICATED_READ; - } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_READ) { - restHeaderAclValue = Constants.ACL_BUCKET_OWNER_READ; - } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_FULL_CONTROL) { - restHeaderAclValue = Constants.ACL_BUCKET_OWNER_FULL_CONTROL; - } else if (acl == AccessControlList.REST_CANNED_LOG_DELIVERY_WRITE) { - restHeaderAclValue = Constants.ACL_LOG_DELIVERY_WRITE; - } - String aclHeader = this.getIHeaders().aclHeader(); - if (restHeaderAclValue != null) { - metadata.put(aclHeader, restHeaderAclValue); - } - return metadata.containsKey(aclHeader); - } - - boolean prepareRESTHeaderAclForOBS(Map metadata, AccessControlList acl) throws ServiceException { - String restHeaderAclValue = null; - boolean invalid = false; - if (acl == AccessControlList.REST_CANNED_PRIVATE) { - restHeaderAclValue = Constants.ACL_PRIVATE; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_DELIVERED) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ_DELIVERED; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE_DELIVERED) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE_DELIVERED; - } else if (acl == AccessControlList.REST_CANNED_AUTHENTICATED_READ) { - restHeaderAclValue = Constants.ACL_AUTHENTICATED_READ; - invalid = true; - } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_READ) { - restHeaderAclValue = Constants.ACL_BUCKET_OWNER_READ; - invalid = true; - } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_FULL_CONTROL) { - restHeaderAclValue = Constants.ACL_BUCKET_OWNER_FULL_CONTROL; - invalid = true; - } else if (acl == AccessControlList.REST_CANNED_LOG_DELIVERY_WRITE) { - restHeaderAclValue = Constants.ACL_LOG_DELIVERY_WRITE; - invalid = true; - } - if (invalid) { - log.info("Invalid Canned ACL:" + restHeaderAclValue); - } - - String aclHeader = this.getIHeaders().aclHeader(); - if (restHeaderAclValue != null) { - metadata.put(aclHeader, restHeaderAclValue); - } - return metadata.containsKey(aclHeader); - } - - boolean prepareRESTHeaderAclObject(Map metadata, AccessControlList acl) throws ServiceException { - return this.getProviderCredentials().getAuthType() == AuthTypeEnum.OBS - ? this.prepareRESTHeaderAclForOBSObject(metadata, acl) - : this.prepareRESTHeaderAclForV2(metadata, acl); - } - - boolean prepareRESTHeaderAclForOBSObject(Map metadata, AccessControlList acl) - throws ServiceException { - String restHeaderAclValue = null; - boolean invalid = false; - if (acl == AccessControlList.REST_CANNED_PRIVATE) { - restHeaderAclValue = Constants.ACL_PRIVATE; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_DELIVERED) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ; - } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE_DELIVERED) { - restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE; - } else if (acl == AccessControlList.REST_CANNED_AUTHENTICATED_READ) { - restHeaderAclValue = Constants.ACL_AUTHENTICATED_READ; - invalid = true; - } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_READ) { - restHeaderAclValue = Constants.ACL_BUCKET_OWNER_READ; - invalid = true; - } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_FULL_CONTROL) { - restHeaderAclValue = Constants.ACL_BUCKET_OWNER_FULL_CONTROL; - invalid = true; - } else if (acl == AccessControlList.REST_CANNED_LOG_DELIVERY_WRITE) { - restHeaderAclValue = Constants.ACL_LOG_DELIVERY_WRITE; - invalid = true; - } - if (invalid) { - log.info("Invalid Canned ACL:" + restHeaderAclValue); - } - - String aclHeader = this.getIHeaders().aclHeader(); - if (restHeaderAclValue != null) { - metadata.put(aclHeader, restHeaderAclValue); - } - return metadata.containsKey(aclHeader); - } - - boolean prepareRESTHeaderAcl(Map metadata, AccessControlList acl) throws ServiceException { - return this.getProviderCredentials().getAuthType() == AuthTypeEnum.OBS - ? this.prepareRESTHeaderAclForOBS(metadata, acl) - : this.prepareRESTHeaderAclForV2(metadata, acl); - } - - protected BucketLocationResponse getBucketLocationImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.LOCATION.getOriginalStringCode(), ""); - - Response httpResponse = performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - BucketLocationResponse ret = new BucketLocationResponse(getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketLocationHandler.class, false) - .getLocation()); - setResponseHeaders(ret, this.cleanResponseHeaders(httpResponse)); - setStatusCode(ret, httpResponse.code()); - return ret; - } - - protected BucketLoggingConfiguration getBucketLoggingConfigurationImpl(String bucketName) throws ServiceException { - - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.LOGGING.getOriginalStringCode(), ""); - - Response httpResponse = performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - BucketLoggingConfiguration ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketLoggingHandler.class, false) - .getBucketLoggingStatus(); - - setResponseHeaders(ret, this.cleanResponseHeaders(httpResponse)); - setStatusCode(ret, httpResponse.code()); - return ret; - } - - HeaderResponse setBucketLoggingConfigurationImpl(String bucketName, BucketLoggingConfiguration status) - throws ServiceException { - - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.LOGGING.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - - String statusAsXml = status == null ? "" : this.getIConvertor().transBucketLoggingConfiguration(status); - - Response response = performRestPut(bucketName, null, metadata, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, statusAsXml), true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - TransResult transPutObjectRequest(PutObjectRequest request) throws ServiceException { - Map headers = new HashMap(); - IConvertor iconvertor = this.getIConvertor(); - IHeaders iheaders = this.getIHeaders(); - - ObjectMetadata objectMetadata = request.getMetadata() == null ? new ObjectMetadata() : request.getMetadata(); - - for (Map.Entry entry : objectMetadata.getMetadata().entrySet()) { - String key = entry.getKey(); - if (!ServiceUtils.isValid(key)) { - continue; - } - key = key.trim(); - if ((this.canUseStandardHTTPHeaders.get() == null || (this.canUseStandardHTTPHeaders.get() != null - && !this.canUseStandardHTTPHeaders.get().booleanValue())) - && Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(key.toLowerCase())) - { - continue; - } - headers.put(key, entry.getValue() == null ? "" : entry.getValue().toString()); - } - - if (ServiceUtils.isValid(objectMetadata.getContentMd5())) { - headers.put(CommonHeaders.CONTENT_MD5, objectMetadata.getContentMd5().trim()); - } - - if (ServiceUtils.isValid(objectMetadata.getContentEncoding())) { - headers.put(CommonHeaders.CONTENT_ENCODING, objectMetadata.getContentEncoding().trim()); - } - - - if (objectMetadata.getObjectStorageClass() != null) { - putHeader(headers, iheaders.storageClassHeader(), - iconvertor.transStorageClass(objectMetadata.getObjectStorageClass())); - } - - if (request.getExpires() >= 0) { - putHeader(headers, iheaders.expiresHeader(), String.valueOf(request.getExpires())); - } - - if (objectMetadata.getWebSiteRedirectLocation() != null) { - putHeader(headers, iheaders.websiteRedirectLocationHeader(), objectMetadata.getWebSiteRedirectLocation()); - } - - if (request.getSuccessRedirectLocation() != null) { - putHeader(headers, iheaders.successRedirectLocationHeader(), request.getSuccessRedirectLocation()); - } - - transExtensionPermissions(request, headers); - - transSseHeaders(request, headers, iheaders); - - Object contentType = objectMetadata.getContentType() == null - ? objectMetadata.getValue(CommonHeaders.CONTENT_TYPE) - : objectMetadata.getContentType(); - if (contentType == null) { - contentType = Mimetypes.getInstance().getMimetype(request.getObjectKey()); - } - Object contentLength = objectMetadata.getContentLength(); - - if (contentLength == null) { - contentLength = objectMetadata.getValue(CommonHeaders.CONTENT_LENGTH); - } - - long _contentLength = contentLength == null ? -1l : Long.parseLong(contentLength.toString()); - - InputStream input = null; - if (request.getFile() != null) { - - if (Mimetypes.MIMETYPE_OCTET_STREAM.equals(contentType)) { - contentType = Mimetypes.getInstance().getMimetype(request.getFile()); - } - - try { - input = new FileInputStream(request.getFile()); - } catch (FileNotFoundException e) { - throw new IllegalArgumentException("File doesnot exist"); - } - - long fileSize = request.getFile().length(); - if(request.getOffset() > 0 && request.getOffset() < fileSize) { - _contentLength = (_contentLength > 0 && _contentLength <= fileSize - request.getOffset()) ? _contentLength : fileSize - request.getOffset(); - try { - input.skip(request.getOffset()); - } catch (IOException e) { - ServiceUtils.closeStream(input); - throw new ServiceException(e); - } - }else if(_contentLength < 0 || _contentLength > fileSize) { - _contentLength = fileSize; - } - - if(request.getProgressListener() != null) { - ProgressManager progressManager = new SimpleProgressManager(_contentLength, 0, request.getProgressListener(), - request.getProgressInterval() > 0 ? request.getProgressInterval() : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); - input = new ProgressInputStream(input, progressManager); - } - - } else { - input = request.getInput(); - if(input != null && request.getProgressListener() != null) { - ProgressManager progressManager = new SimpleProgressManager(_contentLength, 0, request.getProgressListener(), - request.getProgressInterval() > 0 ? request.getProgressInterval() : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); - input = new ProgressInputStream(input, progressManager); - } - } - - String _contentType = contentType.toString().trim(); - headers.put(CommonHeaders.CONTENT_TYPE, _contentType); - - if(_contentLength > -1) { - this.putHeader(headers, CommonHeaders.CONTENT_LENGTH, String.valueOf(_contentLength)); - } - - RequestBody body = input == null ? null - : new RepeatableRequestEntity(input, _contentType, _contentLength, this.obsProperties); - - return new TransResult(headers, body); - } - - TransResult transWriteFileRequest(WriteFileRequest request) throws ServiceException { - TransResult result = this.transPutObjectRequest(request); - if(request.getPosition() > 0) { - Map params = new HashMap(); - params.put(SpecialParamEnum.MODIFY.getOriginalStringCode(), ""); - params.put(ObsRequestParams.POSITION, String.valueOf(request.getPosition())); - result.params = params; - } - return result; - } - - TransResult transModifyObjectRequest(ModifyObjectRequest request) throws ServiceException { - TransResult result = this.transPutObjectRequest(request); - if(request.getPosition() > 0) { - Map params = new HashMap(); - params.put(SpecialParamEnum.MODIFY.getOriginalStringCode(), ""); - params.put(ObsRequestParams.POSITION, String.valueOf(request.getPosition())); - result.params = params; - } - return result; - } - - TransResult transAppendObjectRequest(AppendObjectRequest request) throws ServiceException { - TransResult result = this.transPutObjectRequest(request); - Map params = new HashMap(); - params.put(SpecialParamEnum.APPEND.getOriginalStringCode(), ""); - params.put(ObsRequestParams.POSITION, String.valueOf(request.getPosition())); - result.params = params; - return result; - } - - void transSseKmsHeaders(SseKmsHeader kmsHeader, Map headers, IHeaders iheaders) { - if (kmsHeader == null) { - return; - } - - String sseKmsEncryption = this.getProviderCredentials().getAuthType() != AuthTypeEnum.OBS ? "aws:" + kmsHeader.getSSEAlgorithm().getCode() : kmsHeader.getSSEAlgorithm().getCode(); - putHeader(headers, iheaders.sseKmsHeader(), ServiceUtils.toValid(sseKmsEncryption)); - if (ServiceUtils.isValid(kmsHeader.getKmsKeyId())) { - putHeader(headers, iheaders.sseKmsKeyHeader(), kmsHeader.getKmsKeyId()); - } - - if(ServiceUtils.isValid(kmsHeader.getProjectId())) { - putHeader(headers, iheaders.sseKmsProjectIdHeader(), kmsHeader.getProjectId()); - } - } - - void transSseCHeaders(SseCHeader cHeader, Map headers, IHeaders iheaders) throws ServiceException { - if (cHeader == null) { - return; - } - - String sseCAlgorithm = cHeader.getSSEAlgorithm().getCode(); - - putHeader(headers, iheaders.sseCHeader(), ServiceUtils.toValid(sseCAlgorithm)); - if(cHeader.getSseCKeyBase64() != null) { - try { - putHeader(headers, iheaders.sseCKeyHeader(), cHeader.getSseCKeyBase64()); - putHeader(headers, iheaders.sseCKeyMd5Header(), ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(ServiceUtils.fromBase64(cHeader.getSseCKeyBase64())))); - } catch (IOException e) { - throw new IllegalStateException("fail to read sseCkey", e); - } catch (NoSuchAlgorithmException e) { - throw new IllegalStateException("fail to read sseCkey", e); - } - }else if (null != cHeader.getSseCKey()) { - try { - byte[] data = cHeader.getSseCKey(); - putHeader(headers, iheaders.sseCKeyHeader(), ServiceUtils.toBase64(data)); - putHeader(headers, iheaders.sseCKeyMd5Header(), ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(data))); - } catch (IOException e) { - throw new IllegalStateException("fail to read sseCkey", e); - } catch (NoSuchAlgorithmException e) { - throw new IllegalStateException("fail to read sseCkey", e); - } - } - } - - void transSseHeaders(PutObjectBasicRequest request, Map headers, IHeaders iheaders) - throws ServiceException { - if (null != request.getSseCHeader()) { - this.transSseCHeaders(request.getSseCHeader(), headers, iheaders); - } else if (null != request.getSseKmsHeader()) { - this.transSseKmsHeaders(request.getSseKmsHeader(), headers, iheaders); - } - } - - void transExtensionPermissions(PutObjectBasicRequest request, Map headers) { - Set extensionPermissionEnums = request.getAllGrantPermissions(); - if (!extensionPermissionEnums.isEmpty()) { - for (ExtensionObjectPermissionEnum extensionPermissionEnum : extensionPermissionEnums) { - Set domainIds = request.getDomainIdsByGrantPermission(extensionPermissionEnum); - List domainIdList = new ArrayList(domainIds.size()); - for (String domainId : domainIds) { - domainIdList.add("id=" + domainId); - } - putHeader(headers, getHeaderByMethodName(extensionPermissionEnum.getCode()), - ServiceUtils.join(domainIdList, ",")); - } - } - } - - protected AppendObjectResult appendObjectImpl(AppendObjectRequest request) throws ServiceException { - TransResult result = null; - Response response; - boolean isExtraAclPutRequired; - AccessControlList acl = request.getAcl(); - try { - result = this.transAppendObjectRequest(request); - - isExtraAclPutRequired = !prepareRESTHeaderAcl(result.getHeaders(), acl); - - response = performRestPost(request.getBucketName(), request.getObjectKey(), result.getHeaders(), result.getParams(), - result.body, true); - } finally { - if (result != null && result.body != null && request.isAutoClose()) { - RepeatableRequestEntity entity = (RepeatableRequestEntity) result.body; - ServiceUtils.closeStream(entity); - } - } - String nextPosition = response.header(this.getIHeaders().nextPositionHeader()); - AppendObjectResult ret = new AppendObjectResult(request.getBucketName(), request.getObjectKey(), - response.header(CommonHeaders.ETAG), nextPosition != null ? Long.parseLong(nextPosition) : -1, - StorageClassEnum.getValueFromCode(response.header(this.getIHeaders().storageClassHeader())), - this.getObjectUrl(request.getBucketName(), request.getObjectKey())); - - Map map = this.cleanResponseHeaders(response); - setResponseHeaders(ret, map); - setStatusCode(ret, response.code()); - if (isExtraAclPutRequired && acl != null) { - try { - putAclImpl(request.getBucketName(), request.getObjectKey(), acl, null); - } catch (Exception e) { - if (log.isWarnEnabled()) { - log.warn("Try to set object acl error", e); - } - } - } - return ret; - } - - protected ObsFSFile writeFileImpl(WriteFileRequest request) throws ServiceException { - - TransResult result = null; - Response response; - boolean isExtraAclPutRequired; - AccessControlList acl = request.getAcl(); - try { - result = this.transWriteFileRequest(request); - - isExtraAclPutRequired = !prepareRESTHeaderAcl(result.getHeaders(), acl); - - response = performRestPut(request.getBucketName(), request.getObjectKey(), result.getHeaders(), result.getParams(), - result.body, true); - } finally { - if (result != null && result.body != null && request.isAutoClose()) { - if(result.body instanceof Closeable) { - ServiceUtils.closeStream((Closeable)result.body); - } - } - } - - ObsFSFile ret = new ObsFSFile(request.getBucketName(), request.getObjectKey(), - response.header(CommonHeaders.ETAG), response.header(this.getIHeaders().versionIdHeader()), - StorageClassEnum.getValueFromCode(response.header(this.getIHeaders().storageClassHeader())), this.getObjectUrl(request.getBucketName(), request.getObjectKey())); - Map map = this.cleanResponseHeaders(response); - setResponseHeaders(ret, map); - setStatusCode(ret, response.code()); - if (isExtraAclPutRequired && acl != null) { - try { - putAclImpl(request.getBucketName(), request.getObjectKey(), acl, null); - } catch (Exception e) { - if (log.isWarnEnabled()) { - log.warn("Try to set object acl error", e); - } - } - } - return ret; - } - - protected ModifyObjectResult modifyObjectImpl(ModifyObjectRequest request) throws ServiceException { - - TransResult result = null; - Response response; - boolean isExtraAclPutRequired; - AccessControlList acl = request.getAcl(); - try { - result = this.transModifyObjectRequest(request); - - isExtraAclPutRequired = !prepareRESTHeaderAcl(result.getHeaders(), acl); - - response = performRestPut(request.getBucketName(), request.getObjectKey(), result.getHeaders(), result.getParams(), - result.body, true); - } finally { - if (result != null && result.body != null && request.isAutoClose()) { - if(result.body instanceof Closeable) { - ServiceUtils.closeStream((Closeable)result.body); - } - } - } - ModifyObjectResult ret = new ModifyObjectResult(); - Map map = this.cleanResponseHeaders(response); - setResponseHeaders(ret, map); - setStatusCode(ret, response.code()); - if (isExtraAclPutRequired && acl != null) { - try { - putAclImpl(request.getBucketName(), request.getObjectKey(), acl, null); - } catch (Exception e) { - if (log.isWarnEnabled()) { - log.warn("Try to set object acl error", e); - } - } - } - return ret; - } - - protected ObsFSFile putObjectImpl(PutObjectRequest request) throws ServiceException { - - TransResult result = null; - Response response; - boolean isExtraAclPutRequired; - AccessControlList acl = request.getAcl(); - try { - result = this.transPutObjectRequest(request); - - isExtraAclPutRequired = !prepareRESTHeaderAcl(result.getHeaders(), acl); - - response = performRestPut(request.getBucketName(), request.getObjectKey(), result.getHeaders(), null, - result.body, true); - } finally { - if (result != null && result.body != null && request.isAutoClose()) { - if(result.body instanceof Closeable) { - ServiceUtils.closeStream((Closeable)result.body); - } - } - } - - ObsFSFile ret = new ObsFSFile(request.getBucketName(), request.getObjectKey(), - response.header(CommonHeaders.ETAG), response.header(this.getIHeaders().versionIdHeader()), - StorageClassEnum.getValueFromCode(response.header(this.getIHeaders().storageClassHeader())), this.getObjectUrl(request.getBucketName(), request.getObjectKey())); - Map map = this.cleanResponseHeaders(response); - setResponseHeaders(ret, map); - setStatusCode(ret, response.code()); - if (isExtraAclPutRequired && acl != null) { - try { - putAclImpl(request.getBucketName(), request.getObjectKey(), acl, null); - } catch (Exception e) { - if (log.isWarnEnabled()) { - log.warn("Try to set object acl error", e); - } - } - } - return ret; - } - - TransResult transCopyObjectRequest(CopyObjectRequest request) throws ServiceException { - Map headers = new HashMap(); - IConvertor iconvertor = this.getIConvertor(); - IHeaders iheaders = this.getIHeaders(); - - ObjectMetadata objectMetadata = request.getNewObjectMetadata() == null ? new ObjectMetadata() - : request.getNewObjectMetadata(); - - putHeader(headers, iheaders.metadataDirectiveHeader(), - request.isReplaceMetadata() ? Constants.DERECTIVE_REPLACE : Constants.DERECTIVE_COPY); - if (request.isReplaceMetadata()) { - objectMetadata.getMetadata().remove(iheaders.requestIdHeader()); - objectMetadata.getMetadata().remove(iheaders.requestId2Header()); - for (Map.Entry entry : objectMetadata.getMetadata().entrySet()) { - String key = entry.getKey(); - if (!ServiceUtils.isValid(key)) { - continue; - } - key = key.trim(); - if (Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(key.toLowerCase())) { - continue; - } - headers.put(key, entry.getValue() == null ? "" : entry.getValue().toString()); - } - } - - if (objectMetadata.getContentType() != null) { - headers.put(CommonHeaders.CONTENT_TYPE, objectMetadata.getContentType().trim()); - } - - if (objectMetadata.getContentEncoding() != null) { - headers.put(CommonHeaders.CONTENT_ENCODING, objectMetadata.getContentEncoding().trim()); - } - - if (objectMetadata.getObjectStorageClass() != null) { - putHeader(headers, iheaders.storageClassHeader(), - iconvertor.transStorageClass(objectMetadata.getObjectStorageClass())); - } - - if (objectMetadata.getWebSiteRedirectLocation() != null) { - putHeader(headers, iheaders.websiteRedirectLocationHeader(), objectMetadata.getWebSiteRedirectLocation()); - } - - if (request.getSuccessRedirectLocation() != null) { - putHeader(headers, iheaders.successRedirectLocationHeader(), request.getSuccessRedirectLocation()); - } - - this.transExtensionPermissions(request, headers); - this.transSseHeaders(request, headers, iheaders); - - transSseCSourceHeaders(request.getSseCHeaderSource(), headers, iheaders); - - transConditionCopyHeaders(request, headers, iheaders); - - String sourceKey = RestUtils - .encodeUrlString(request.getSourceBucketName()) + "/" + RestUtils - .encodeUrlString(request.getSourceObjectKey()); - if (ServiceUtils.isValid(request.getVersionId())) { - sourceKey += "?versionId=" + request.getVersionId().trim(); - } - putHeader(headers, iheaders.copySourceHeader(), sourceKey); - - return new TransResult(headers); - } - - void transSseCSourceHeaders(SseCHeader sseCHeader, Map headers, IHeaders iheaders) - throws ServiceException { - if (sseCHeader != null) { - String algorithm = sseCHeader.getSSEAlgorithm().getCode(); - putHeader(headers, iheaders.copySourceSseCHeader(), ServiceUtils.toValid(algorithm)); - if(sseCHeader.getSseCKeyBase64() != null) { - try { - putHeader(headers, iheaders.copySourceSseCKeyHeader(), sseCHeader.getSseCKeyBase64()); - putHeader(headers, iheaders.copySourceSseCKeyMd5Header(), ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(ServiceUtils.fromBase64(sseCHeader.getSseCKeyBase64())))); - } catch (IOException e) { - throw new IllegalStateException("fail to read sseCkey", e); - } catch (NoSuchAlgorithmException e) { - throw new IllegalStateException("fail to read sseCkey", e); - } - }else if (null != sseCHeader.getSseCKey()) { - try { - byte[] data = sseCHeader.getSseCKey(); - putHeader(headers, iheaders.copySourceSseCKeyHeader(), ServiceUtils.toBase64(data)); - putHeader(headers, iheaders.copySourceSseCKeyMd5Header(), ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(data))); - } catch (IOException e) { - throw new IllegalStateException("fail to read sseCkey", e); - } catch (NoSuchAlgorithmException e) { - throw new IllegalStateException("fail to read sseCkey", e); - } - } - } - } - - void transConditionCopyHeaders(CopyObjectRequest request, Map headers, IHeaders iheaders) { - if (request.getIfModifiedSince() != null) { - putHeader(headers, iheaders.copySourceIfModifiedSinceHeader(), - ServiceUtils.formatRfc822Date(request.getIfModifiedSince())); - } - if (request.getIfUnmodifiedSince() != null) { - putHeader(headers, iheaders.copySourceIfUnmodifiedSinceHeader(), - ServiceUtils.formatRfc822Date(request.getIfUnmodifiedSince())); - } - if (ServiceUtils.isValid(request.getIfMatchTag())) { - putHeader(headers, iheaders.copySourceIfMatchHeader(), request.getIfMatchTag().trim()); - } - if (ServiceUtils.isValid(request.getIfNoneMatchTag())) { - putHeader(headers, iheaders.copySourceIfNoneMatchHeader(), request.getIfNoneMatchTag().trim()); - } - } - - protected CopyObjectResult copyObjectImpl(CopyObjectRequest request) throws ServiceException { - - TransResult result = this.transCopyObjectRequest(request); - - AccessControlList acl = request.getAcl(); - boolean isExtraAclPutRequired = !prepareRESTHeaderAcl(result.getHeaders(), acl); - - Response response = performRestPut(request.getDestinationBucketName(), request.getDestinationObjectKey(), - result.getHeaders(), null, null, false); - - this.verifyResponseContentType(response); - - CopyObjectResultHandler handler = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(response), - CopyObjectResultHandler.class, false); - CopyObjectResult copyRet = new CopyObjectResult(handler.getETag(), handler.getLastModified(), response.header(this.getIHeaders().versionIdHeader()), response.header(this.getIHeaders().copySourceVersionIdHeader()), StorageClassEnum.getValueFromCode(response.header(this.getIHeaders().storageClassHeader()))); - Map map = this.cleanResponseHeaders(response); - setResponseHeaders(copyRet, map); - setStatusCode(copyRet, response.code()); - if (isExtraAclPutRequired && acl != null) { - if (log.isDebugEnabled()) { - log.debug("Creating object with a non-canned ACL using REST, so an extra ACL Put is required"); - } - try { - putAclImpl(request.getDestinationBucketName(), request.getDestinationObjectKey(), acl, null); - } catch (Exception e) { - if (log.isWarnEnabled()) { - log.warn("Try to set object acl error", e); - } - } - } - - return copyRet; - } - - protected ObsFSAttribute getObjectMetadataImpl(GetObjectMetadataRequest request) throws ServiceException { - Map headers = new HashMap(); - this.transSseCHeaders(request.getSseCHeader(), headers, this.getIHeaders()); - Map params = new HashMap(); - if (request.getVersionId() != null) { - params.put(ObsRequestParams.VERSION_ID, request.getVersionId()); - } - return (ObsFSAttribute) this.getObjectImpl(true, request.getBucketName(), request.getObjectKey(), headers, - params, null, -1); - } - - protected boolean doesObjectExistImpl(GetObjectMetadataRequest request) throws ServiceException { - Map headers = new HashMap(); - this.transSseCHeaders(request.getSseCHeader(), headers, this.getIHeaders()); - Map params = new HashMap(); - if (request.getVersionId() != null) { - params.put(ObsRequestParams.VERSION_ID, request.getVersionId()); - } - boolean doesObjectExist = false; - try{ - Response response = performRestHead(request.getBucketName(), request.getObjectKey(), params, headers); - if(200 == response.code()) { - doesObjectExist = true; - } - }catch (ServiceException ex){ - if(404 == ex.getResponseCode()){ - doesObjectExist = false; - }else { - throw ex; - } - } - return doesObjectExist; - } - - TransResult transSetObjectMetadataRequest(SetObjectMetadataRequest request) throws ServiceException { - Map headers = new HashMap(); - IHeaders iheaders = this.getIHeaders(); - IConvertor iconvertor = this.getIConvertor(); - - for (Map.Entry entry : request.getMetadata().entrySet()) { - String key = entry.getKey(); - if (!ServiceUtils.isValid(key)) { - continue; - } - key = key.trim(); - headers.put(key, entry.getValue() == null ? "" : entry.getValue().toString()); - } - - if (request.getObjectStorageClass() != null) { - putHeader(headers, iheaders.storageClassHeader(), - iconvertor.transStorageClass(request.getObjectStorageClass())); - } - - if (request.getWebSiteRedirectLocation() != null) { - putHeader(headers, iheaders.websiteRedirectLocationHeader(), request.getWebSiteRedirectLocation()); - } - - if(request.getContentDisposition() != null) { - putHeader(headers, Constants.CommonHeaders.CONTENT_DISPOSITION, request.getContentDisposition()); - } - - if(request.getContentEncoding() != null) { - putHeader(headers, Constants.CommonHeaders.CONTENT_ENCODING, request.getContentEncoding()); - } - - if(request.getContentLanguage() != null) { - putHeader(headers, Constants.CommonHeaders.CONTENT_LANGUAGE, request.getContentLanguage()); - } - - if(request.getContentType() != null) { - putHeader(headers, Constants.CommonHeaders.CONTENT_TYPE, request.getContentType()); - } - - if(request.getCacheControl() != null) { - putHeader(headers, Constants.CommonHeaders.CACHE_CONTROL, request.getCacheControl()); - } - - if(request.getExpires() != null) { - putHeader(headers, Constants.CommonHeaders.EXPIRES, request.getExpires()); - } - - putHeader(headers, iheaders.metadataDirectiveHeader(), request.isRemoveUnset() ? Constants.DERECTIVE_REPLACE : Constants.DERECTIVE_REPLACE_NEW); - - Map params = new HashMap(); - params.put(SpecialParamEnum.METADATA.getOriginalStringCode(), ""); - if (request.getVersionId() != null) { - params.put(ObsRequestParams.VERSION_ID, request.getVersionId()); - } - - return new TransResult(headers, params, null); - } - - protected ObjectMetadata setObjectMetadataImpl(SetObjectMetadataRequest request) { - TransResult result = this.transSetObjectMetadataRequest(request); - Response response = performRestPut(request.getBucketName(), request.getObjectKey(), result.headers, result.params, - result.body, true); - return this.getObsFSAttributeFromResponse(response); - } - - - TransResult transGetObjectRequest(GetObjectRequest request) throws ServiceException { - Map headers = new HashMap(); - this.transSseCHeaders(request.getSseCHeader(), headers, this.getIHeaders()); - this.transConditionGetObjectHeaders(request, headers); - - transRangeHeader(request, headers); - - Map params = new HashMap(); - this.transGetObjectParams(request, params); - return new TransResult(headers, params, null); - } - - /** - * - * @param request - * @param headers - */ - private void transRangeHeader(GetObjectRequest request, Map headers) { - String start = ""; - String end = ""; - - if(null != request.getRangeStart()) { - ServiceUtils.assertParameterNotNegative(request.getRangeStart().longValue(), "start range should not be negative."); - start = String.valueOf(request.getRangeStart()); - } - - if(null != request.getRangeEnd()) { - ServiceUtils.assertParameterNotNegative(request.getRangeEnd().longValue(), "end range should not be negative."); - end = String.valueOf(request.getRangeEnd()); - } - - if(null != request.getRangeStart() - && null != request.getRangeEnd()) { - if(request.getRangeStart().longValue() > request.getRangeEnd().longValue()) { - throw new IllegalArgumentException("start must be less than end."); - } - } - - if(!"".equals(start) || !"".equals(end)) { - String range = String.format("bytes=%s-%s", start, end); - headers.put(CommonHeaders.RANGE, range); - } - } - - void transGetObjectParams(GetObjectRequest request, Map params) { - if (null != request.getReplaceMetadata()) { - if (ServiceUtils.isValid(request.getReplaceMetadata().getCacheControl())) { - params.put(ObsRequestParams.RESPONSE_CACHE_CONTROL, request.getReplaceMetadata().getCacheControl()); - } - if (ServiceUtils.isValid(request.getReplaceMetadata().getContentDisposition())) { - params.put(ObsRequestParams.RESPONSE_CONTENT_DISPOSITION, - request.getReplaceMetadata().getContentDisposition()); - } - if (ServiceUtils.isValid(request.getReplaceMetadata().getContentEncoding())) { - params.put(ObsRequestParams.RESPONSE_CONTENT_ENCODING, - request.getReplaceMetadata().getContentEncoding()); - } - if (ServiceUtils.isValid(request.getReplaceMetadata().getContentLanguage())) { - params.put(ObsRequestParams.RESPONSE_CONTENT_LANGUAGE, - request.getReplaceMetadata().getContentLanguage()); - } - if (ServiceUtils.isValid(request.getReplaceMetadata().getContentType())) { - params.put(ObsRequestParams.RESPONSE_CONTENT_TYPE, request.getReplaceMetadata().getContentType()); - } - if (ServiceUtils.isValid(request.getReplaceMetadata().getExpires())) { - params.put(ObsRequestParams.RESPONSE_EXPIRES, request.getReplaceMetadata().getExpires()); - } - } - if (ServiceUtils.isValid(request.getImageProcess())) { - params.put(ObsRequestParams.X_IMAGE_PROCESS, request.getImageProcess()); - } - if (request.getVersionId() != null) { - params.put(ObsRequestParams.VERSION_ID, request.getVersionId()); - } - if(request.getCacheOption() != null) { - String cacheControl = request.getCacheOption().getCode() + ", ttl=" + request.getTtl(); - params.put(ObsRequestParams.X_CACHE_CONTROL, cacheControl); - } - } - - void transConditionGetObjectHeaders(GetObjectRequest request, Map headers) { - if (request.getIfModifiedSince() != null) { - headers.put(CommonHeaders.IF_MODIFIED_SINCE, ServiceUtils.formatRfc822Date(request.getIfModifiedSince())); - } - if (request.getIfUnmodifiedSince() != null) { - headers.put(CommonHeaders.IF_UNMODIFIED_SINCE, - ServiceUtils.formatRfc822Date(request.getIfUnmodifiedSince())); - } - if (ServiceUtils.isValid(request.getIfMatchTag())) { - headers.put(CommonHeaders.IF_MATCH, request.getIfMatchTag().trim()); - } - if (ServiceUtils.isValid(request.getIfNoneMatchTag())) { - headers.put(CommonHeaders.IF_NONE_MATCH, request.getIfNoneMatchTag().trim()); - } - } - - protected ObsObject getObjectImpl(GetObjectRequest request) throws ServiceException { - TransResult result = this.transGetObjectRequest(request); - if (request.getRequestParameters() != null) { - result.getParams().putAll(request.getRequestParameters()); + if (matchAnyBucket) { + originPolicy.append("[\"starts-with\", \"$bucket\", \"\"],"); } - return (ObsObject) this.getObjectImpl(false, request.getBucketName(), request.getObjectKey(), - result.getHeaders(), result.getParams(), request.getProgressListener(), request.getProgressInterval()); - } - - private ObsFSAttribute getObsFSAttributeFromResponse(Response response) { - Date _lastModified = null; - String lastModified = response.header(CommonHeaders.LAST_MODIFIED); - if (lastModified != null) { - try { - _lastModified = ServiceUtils.parseRfc822Date(lastModified); - } catch (ParseException e) { - if (log.isWarnEnabled()) { - log.warn("Response last-modified is not well-format", e); - } - } - } - ObsFSAttribute objMetadata = new ObsFSAttribute(); - objMetadata.setLastModified(_lastModified); - objMetadata.setContentEncoding(response.header(CommonHeaders.CONTENT_ENCODING)); - objMetadata.setContentType(response.header(CommonHeaders.CONTENT_TYPE)); - String contentLength = response.header(CommonHeaders.CONTENT_LENGTH); - if (contentLength != null) { - try { - objMetadata.setContentLength(Long.parseLong(contentLength)); - } catch (NumberFormatException e) { - if (log.isWarnEnabled()) { - log.warn("Response content-length is not well-format", e); - } - } - } - String fsMode; - if((fsMode=response.header(this.getIHeaders().fsModeHeader())) != null) { - objMetadata.setMode(Integer.parseInt(fsMode)); - } - objMetadata.setWebSiteRedirectLocation(response.header(this.getIHeaders().websiteRedirectLocationHeader())); - objMetadata.setObjectStorageClass( - StorageClassEnum.getValueFromCode(response.header(this.getIHeaders().storageClassHeader()))); - - String etag = response.header(CommonHeaders.ETAG); - objMetadata.setEtag(etag); - if (etag != null && !etag.contains("-")) { - String md5 = etag; - if (md5.startsWith("\"")) { - md5 = md5.substring(1); - } - if (md5.endsWith("\"")) { - md5 = md5.substring(0, md5.length() - 1); - } - try { - objMetadata.setContentMd5(ServiceUtils.toBase64(ServiceUtils.fromHex(md5))); - } catch (Exception e) { - if (log.isDebugEnabled()) { - log.debug(e.getMessage(), e); - } - } - } - - objMetadata.setAppendable("Appendable".equals(response.header(this.getIHeaders().objectTypeHeader()))); - String nextPosition = response.header(this.getIHeaders().nextPositionHeader(), "-1"); - objMetadata.setNextPosition(Long.parseLong(nextPosition)); - if(objMetadata.getNextPosition() == -1L) { - objMetadata.setNextPosition(Long.parseLong(response.header(Constants.CommonHeaders.CONTENT_LENGTH, "-1"))); - } - - objMetadata.getMetadata().putAll(this.cleanResponseHeaders(response)); - setStatusCode(objMetadata, response.code()); - return objMetadata; - } - - protected Object getObjectImpl(boolean headOnly, String bucketName, String objectKey, Map headers, - Map params, ProgressListener progressListener, long progressInterval) throws ServiceException { - Response response; - if (headOnly) { - response = performRestHead(bucketName, objectKey, params, headers); - } else { - response = performRestGet(bucketName, objectKey, params, headers); - } - - ObsFSAttribute objMetadata = this.getObsFSAttributeFromResponse(response); - - if (headOnly) { - response.close(); - return objMetadata; - } - ReadFileResult obsObject = new ReadFileResult(); - obsObject.setObjectKey(objectKey); - obsObject.setBucketName(bucketName); - obsObject.setMetadata(objMetadata); - InputStream input = response.body().byteStream(); - if(progressListener != null) { - ProgressManager progressManager = new SimpleProgressManager(objMetadata.getContentLength(), 0, - progressListener, progressInterval > 0 ? progressInterval : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); - input = new ProgressInputStream(input, progressManager); - } - - int readBufferSize = obsProperties.getIntProperty(ObsConstraint.READ_BUFFER_SIZE, - ObsConstraint.DEFAULT_READ_BUFFER_STREAM); - if(readBufferSize > 0) { - input = new BufferedInputStream(input, readBufferSize); - } - - obsObject.setObjectContent(input); - return obsObject; - } - - protected BucketQuota getBucketQuotaImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.QUOTA.getOriginalStringCode(), ""); - - Response httpResponse = performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - BucketQuota ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketQuotaHandler.class, false).getQuota(); - setResponseHeaders(ret, this.cleanResponseHeaders(httpResponse)); - setStatusCode(ret, httpResponse.code()); - return ret; - } - - protected BucketStoragePolicyConfiguration getBucketStoragePolicyImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(this.getSpecialParamForStorageClass().getOriginalStringCode(), ""); - - Response httpResponse = performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - BucketStoragePolicyConfiguration ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketStoragePolicyHandler.class, false) - .getStoragePolicy(); - setResponseHeaders(ret, this.cleanResponseHeaders(httpResponse)); - setStatusCode(ret, httpResponse.code()); - return ret; - } - - protected BucketStorageInfo getBucketStorageInfoImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.STORAGEINFO.getOriginalStringCode(), ""); - - Response httpResponse = performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - BucketStorageInfo ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketStorageInfoHandler.class, false) - .getStorageInfo(); - setResponseHeaders(ret, this.cleanResponseHeaders(httpResponse)); - setStatusCode(ret, httpResponse.code()); - return ret; - } - - protected AccessControlList getBucketAclImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.ACL.getOriginalStringCode(), ""); - - Response httpResponse = performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - AccessControlList ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), AccessControlListHandler.class, false) - .getAccessControlList(); - setResponseHeaders(ret, this.cleanResponseHeaders(httpResponse)); - setStatusCode(ret, httpResponse.code()); - return ret; - } - - protected AccessControlList getObjectAclImpl(String bucketName, String objectKey, String versionId) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.ACL.getOriginalStringCode(), ""); - if (ServiceUtils.isValid(versionId)) { - requestParameters.put(ObsRequestParams.VERSION_ID, versionId.trim()); - } - Response httpResponse = performRestGet(bucketName, objectKey, requestParameters, null); - - this.verifyResponseContentType(httpResponse); - - AccessControlList ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), AccessControlListHandler.class, false) - .getAccessControlList(); - setResponseHeaders(ret, this.cleanResponseHeaders(httpResponse)); - setStatusCode(ret, httpResponse.code()); - return ret; - } - - protected DeleteObjectResult deleteObjectImpl(String bucketName, String objectKey, String versionId) - throws ServiceException { - Map requestParameters = new HashMap(); - if (versionId != null) { - requestParameters.put(ObsRequestParams.VERSION_ID, versionId); - } - - Response response = performRestDelete(bucketName, objectKey, requestParameters); - - DropFileResult result = new DropFileResult(Boolean.valueOf(response.header(this.getIHeaders().deleteMarkerHeader())), - objectKey, response.header(this.getIHeaders().versionIdHeader())); - Map map = this.cleanResponseHeaders(response); - setResponseHeaders(result, map); - setStatusCode(result, response.code()); - return result; - } - - protected ReadAheadResult readAheadObjectsImpl(ReadAheadRequest request) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(Constants.ObsRequestParams.READAHEAD, ""); - requestParameters.put(Constants.ObsRequestParams.PREFIX, request.getPrefix()); - - Map metadata = new HashMap(); - String cacheControl = request.getCacheOption().getCode() + ", ttl=" + request.getTtl(); - metadata.put(ObsRequestParams.X_CACHE_CONTROL, cacheControl); - - Response response = performRestPost(request.getBucketName(), null, metadata, requestParameters, null, false); - - this.verifyResponseContentTypeForJson(response); - - String body; - try { - body = response.body().string(); - } catch (IOException e) { - throw new ServiceException(e); - } - - ReadAheadResult result = (ReadAheadResult) JSONChange.jsonToObj(new ReadAheadResult(), body); - result.setResponseHeaders(this.cleanResponseHeaders(response)); - setStatusCode(result, response.code()); - - return result; - } - - protected ReadAheadResult deleteReadAheadObjectsImpl(String bucketName, String prefix) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(Constants.ObsRequestParams.READAHEAD, ""); - requestParameters.put(Constants.ObsRequestParams.PREFIX, prefix); - - Response response = performRestDelete(bucketName, null, requestParameters, false); - - this.verifyResponseContentTypeForJson(response); - - String body; - try { - body = response.body().string(); - } catch (IOException e) { - throw new ServiceException(e); - } - - ReadAheadResult result = (ReadAheadResult) JSONChange.jsonToObj(new ReadAheadResult(), body); - result.setResponseHeaders(this.cleanResponseHeaders(response)); - setStatusCode(result, response.code()); - - return result; - } - - protected ReadAheadQueryResult queryReadAheadObjectsTaskImpl(String bucketName, String taskId) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(Constants.ObsRequestParams.READAHEAD, ""); - requestParameters.put(Constants.ObsRequestParams.TASKID, taskId); - - Response response = performRestGet(bucketName, null, requestParameters, null); - - this.verifyResponseContentTypeForJson(response); - - String body; - try { - body = response.body().string(); - } catch (IOException e) { - throw new ServiceException(e); - } - - ReadAheadQueryResult result = (ReadAheadQueryResult) JSONChange.jsonToObj(new ReadAheadQueryResult(), body); - result.setResponseHeaders(this.cleanResponseHeaders(response)); - setStatusCode(result, response.code()); - - return result; - } - - protected BucketDirectColdAccess getBucketDirectColdAccessImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.DIRECTCOLDACCESS.getOriginalStringCode(), ""); - Response httpResponse = this.performRestGet(bucketName, null, requestParameters, null); - this.verifyResponseContentType(httpResponse); - - BucketDirectColdAccess result = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), BucketDirectColdAccessHandler.class, false) - .getBucketDirectColdAccess(); - setResponseHeaders(result, this.cleanResponseHeaders(httpResponse)); - setStatusCode(result, httpResponse.code()); - return result; - } - - protected HeaderResponse setBucketDirectColdAccessImpl(String bucketName, BucketDirectColdAccess access) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.DIRECTCOLDACCESS.getOriginalStringCode(), ""); - Map headers = new HashMap(); - - String requestXmlElement = this.getIConvertor().transBucketDirectColdAccess(access); - - headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(requestXmlElement)); - headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); - - Response response = this.performRestPut(bucketName, null, headers, requestParameters, - createRequestBody(Mimetypes.MIMETYPE_XML, requestXmlElement), true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse deleteBucketDirectColdAccessImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(SpecialParamEnum.DIRECTCOLDACCESS.getOriginalStringCode(), ""); - Response response = performRestDelete(bucketName, null, requestParameters); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - TransResult transListObjectsRequest(ListObjectsRequest listObjectsRequest) { - Map params = new HashMap(); - if (listObjectsRequest.getPrefix() != null) { - params.put(ObsRequestParams.PREFIX, listObjectsRequest.getPrefix()); - } - if (listObjectsRequest.getDelimiter() != null) { - params.put(ObsRequestParams.DELIMITER, listObjectsRequest.getDelimiter()); - } - if (listObjectsRequest.getMaxKeys() > 0) { - params.put(ObsRequestParams.MAX_KEYS, String.valueOf(listObjectsRequest.getMaxKeys())); - } - - if (listObjectsRequest.getMarker() != null) { - params.put(ObsRequestParams.MARKER, listObjectsRequest.getMarker()); - } - - Map headers = new HashMap(); - - if (listObjectsRequest.getListTimeout() > 0) { - putHeader(headers, this.getIHeaders().listTimeoutHeader(), String.valueOf(listObjectsRequest.getListTimeout())); - } - - return new TransResult(headers, params, null); - } - - protected ObjectListing listObjectsImpl(ListObjectsRequest listObjectsRequest) throws ServiceException { - - TransResult result = this.transListObjectsRequest(listObjectsRequest); - - Response httpResponse = performRestGet(listObjectsRequest.getBucketName(), null, result.getParams(), null); - - this.verifyResponseContentType(httpResponse); - - ListObjectsHandler listObjectsHandler = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), ListObjectsHandler.class, true); - - ObjectListing objList = new ObjectListing(listObjectsHandler.getObjects(), listObjectsHandler.getCommonPrefixes(), listObjectsHandler.getBucketName() == null ? listObjectsRequest.getBucketName() - : listObjectsHandler.getBucketName(), listObjectsHandler.isListingTruncated(), listObjectsHandler.getRequestPrefix() == null ? listObjectsRequest.getPrefix() - : listObjectsHandler.getRequestPrefix(), listObjectsHandler.getRequestMarker() == null ? listObjectsRequest.getMarker() - : listObjectsHandler.getRequestMarker(), listObjectsHandler.getRequestMaxKeys(), listObjectsHandler.getRequestDelimiter() == null ? listObjectsRequest.getDelimiter() - : listObjectsHandler.getRequestDelimiter(), listObjectsHandler.getMarkerForNextListing(), httpResponse.header(this.getIHeaders().bucketRegionHeader())); - setResponseHeaders(objList, this.cleanResponseHeaders(httpResponse)); - setStatusCode(objList, httpResponse.code()); - return objList; - } - - protected ListBucketsResult listAllBucketsImpl(ListBucketsRequest request) throws ServiceException { - Map headers = new HashMap(); - if (request != null && request.isQueryLocation()) { - this.putHeader(headers, this.getIHeaders().locationHeader(), Constants.TRUE); - } - if (request != null && request.getBucketType() != null) { - this.putHeader(headers, this.getIHeaders().bucketTypeHeader(), request.getBucketType().getCode()); - } - Response httpResponse = performRestGetForListBuckets("", null, null, headers); - - this.verifyResponseContentType(httpResponse); - - ListBucketsHandler handler = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(httpResponse), ListBucketsHandler.class, true); - - Map responseHeaders = this.cleanResponseHeaders(httpResponse); - - ListBucketsResult result = new ListBucketsResult(handler.getBuckets(), handler.getOwner()); - setResponseHeaders(result, responseHeaders); - setStatusCode(result, httpResponse.code()); - - return result; - } - - protected HeaderResponse setBucketLoggingConfigurationImpl(String bucketName, - BucketLoggingConfiguration status, boolean updateTargetACLifRequired) throws ServiceException { - if (status.isLoggingEnabled() && updateTargetACLifRequired - && this.getProviderCredentials().getAuthType() != AuthTypeEnum.OBS) { - boolean isSetLoggingGroupWrite = false; - boolean isSetLoggingGroupReadACP = false; - String groupIdentifier = Constants.LOG_DELIVERY_URI; - - AccessControlList logBucketACL = getBucketAclImpl(status.getTargetBucketName()); - for (GrantAndPermission gap : logBucketACL.getGrantAndPermissions()) { - if (gap.getGrantee() instanceof GroupGrantee) { - GroupGrantee grantee = (GroupGrantee) gap.getGrantee(); - if (groupIdentifier.equals(this.getIConvertor().transGroupGrantee(grantee.getGroupGranteeType()))) { - if (Permission.PERMISSION_WRITE.equals(gap.getPermission())) { - isSetLoggingGroupWrite = true; - } else if (Permission.PERMISSION_READ_ACP.equals(gap.getPermission())) { - isSetLoggingGroupReadACP = true; - } - } - } - } - - if (!isSetLoggingGroupWrite || !isSetLoggingGroupReadACP) { - if (log.isWarnEnabled()) { - log.warn("Target logging bucket '" + status.getTargetBucketName() - + "' does not have the necessary ACL settings, updating ACL now"); - } - if (logBucketACL.getOwner() != null) { - logBucketACL.getOwner().setDisplayName(null); - } - logBucketACL.grantPermission(GroupGrantee.LOG_DELIVERY, Permission.PERMISSION_WRITE); - logBucketACL.grantPermission(GroupGrantee.LOG_DELIVERY, Permission.PERMISSION_READ_ACP); - setBucketAclImpl(status.getTargetBucketName(), null, logBucketACL); - } else { - if (log.isDebugEnabled()) { - log.debug("Target logging bucket '" + status.getTargetBucketName() - + "' has the necessary ACL settings"); - } - } - } - - return setBucketLoggingConfigurationImpl(bucketName, status); - } - - protected HeaderResponse deleteBucketImpl(String bucketName) throws ServiceException { - Response response = performRestDelete(bucketName, null, null); - return this.build(response); - } - - Map cleanResponseHeaders(Response response) { - Map> map = response.headers().toMultimap(); - return ServiceUtils.cleanRestMetadataMap(map, this.getIHeaders().headerPrefix(), - this.getIHeaders().headerMetaPrefix()); - } - - TransResult transUploadPartRequest(UploadPartRequest request) throws ServiceException { - Map params = new HashMap(); - params.put(ObsRequestParams.PART_NUMBER, String.valueOf(request.getPartNumber())); - params.put(ObsRequestParams.UPLOAD_ID, request.getUploadId()); + if (matchAnyKey) { + originPolicy.append("[\"starts-with\", \"$key\", \"\"],"); + } + } - Map headers = new HashMap(); - IHeaders iheaders = this.getIHeaders(); + private String getFormatExpiration(PostSignatureRequest request) { + Date requestDate = request.getRequestDate() != null ? request.getRequestDate() : new Date(); + SimpleDateFormat expirationDateFormat = ServiceUtils.getExpirationDateFormat(); + Date expiryDate = request.getExpiryDate() == null ? new Date(requestDate.getTime() + + (request.getExpires() <= 0 ? ObsConstraint.DEFAULT_EXPIRE_SECONEDS : request.getExpires()) * 1000) + : request.getExpiryDate(); - if (ServiceUtils.isValid(request.getContentMd5())) { - headers.put(CommonHeaders.CONTENT_MD5, request.getContentMd5().trim()); - } - this.transSseCHeaders(request.getSseCHeader(), headers, iheaders); + String expiration = expirationDateFormat.format(expiryDate); + return expiration; + } - InputStream input = null; - long contentLength = -1L; - if (null != request.getFile()) { - long fileSize = request.getFile().length(); - long offset = (request.getOffset() >= 0 && request.getOffset() < fileSize) ? request.getOffset() : 0; - long partSize = (request.getPartSize() != null && request.getPartSize() > 0 - && request.getPartSize() <= (fileSize - offset)) ? request.getPartSize() : fileSize - offset; - contentLength = partSize; - - try { - if (request.isAttachMd5() && !ServiceUtils.isValid(request.getContentMd5())) { - headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.toBase64( - ServiceUtils.computeMD5Hash(new FileInputStream(request.getFile()), partSize, offset))); - } - input = new FileInputStream(request.getFile()); - input.skip(offset); - } catch (Exception e) { - ServiceUtils.closeStream(input); - throw new ServiceException(e); - } - - if(request.getProgressListener() != null) { - ProgressManager progressManager = new SimpleProgressManager(contentLength, 0, request.getProgressListener(), - request.getProgressInterval() > 0 ? request.getProgressInterval() : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); - input = new ProgressInputStream(input, progressManager); - } - - } else if (null != request.getInput()) { - if(request.getPartSize() != null && request.getPartSize() > 0){ - contentLength = request.getPartSize(); - } - input = request.getInput(); - - if(input != null && request.getProgressListener() != null) { - ProgressManager progressManager = new SimpleProgressManager(contentLength, 0, request.getProgressListener(), - request.getProgressInterval() > 0 ? request.getProgressInterval() : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); - input = new ProgressInputStream(input, progressManager); - } - - } - String contentType = Mimetypes.getInstance().getMimetype(request.getObjectKey()); - headers.put(CommonHeaders.CONTENT_TYPE, contentType); - - if(contentLength > -1) { - this.putHeader(headers, CommonHeaders.CONTENT_LENGTH, String.valueOf(contentLength)); - } - RequestBody body = input == null ? null - : new RepeatableRequestEntity(input, contentType, contentLength, this.obsProperties); - return new TransResult(headers, params, body); - } + private Map prepareSignatureParameters(PostSignatureRequest request, boolean isV4, + String securityToken, String longDate, String credential) { + Map params = new TreeMap(); + if (isV4) { + params.put(Constants.V2_HEADER_PREFIX_CAMEL + "Algorithm", Constants.V4_ALGORITHM); + params.put(Constants.V2_HEADER_PREFIX_CAMEL + "Date", longDate); + params.put(Constants.V2_HEADER_PREFIX_CAMEL + "Credential", credential); + } - protected UploadPartResult uploadPartImpl(UploadPartRequest request) throws ServiceException { - TransResult result = null; - Response response; - try { - result = this.transUploadPartRequest(request); - response = performRestPut(request.getBucketName(), request.getObjectKey(), result.getHeaders(), - result.getParams(), result.body, true); - } finally { - if (result != null && result.body != null && request.isAutoClose()) { - RepeatableRequestEntity entity = (RepeatableRequestEntity) result.body; - ServiceUtils.closeStream(entity); - } - } - UploadPartResult ret = new UploadPartResult(); - ret.setEtag(response.header(CommonHeaders.ETAG)); - ret.setPartNumber(request.getPartNumber()); - setResponseHeaders(ret, this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } + params.putAll(request.getFormParams()); - TransResult transCopyPartRequest(CopyPartRequest request) throws ServiceException { - Map params = new HashMap(); - params.put(ObsRequestParams.PART_NUMBER, String.valueOf(request.getPartNumber())); - params.put(ObsRequestParams.UPLOAD_ID, request.getUploadId()); + if (!params.containsKey(this.getIHeaders(request.getBucketName()).securityTokenHeader())) { + if (ServiceUtils.isValid(securityToken)) { + params.put(this.getIHeaders(request.getBucketName()).securityTokenHeader(), securityToken); + } + } - Map headers = new HashMap(); - IHeaders iheaders = this.getIHeaders(); + if (ServiceUtils.isValid(request.getBucketName())) { + params.put("bucket", request.getBucketName()); + } - String sourceKey = RestUtils - .encodeUrlString(request.getSourceBucketName()) + "/" + RestUtils - .encodeUrlString(request.getSourceObjectKey()); - if (ServiceUtils.isValid(request.getVersionId())) { - sourceKey += "?versionId=" + request.getVersionId().trim(); - } - putHeader(headers, iheaders.copySourceHeader(), sourceKey); + if (ServiceUtils.isValid(request.getObjectKey())) { + params.put("key", request.getObjectKey()); + } + return params; + } - if (request.getByteRangeStart() != null) { - String rangeEnd = request.getByteRangeEnd() != null ? String.valueOf(request.getByteRangeEnd()) : ""; - String range = String.format("bytes=%s-%s", request.getByteRangeStart(), rangeEnd); - putHeader(headers, iheaders.copySourceRangeHeader(), range); - } + protected TemporarySignatureResponse createV4TemporarySignature(TemporarySignatureRequest request) + throws Exception { + StringBuilder canonicalUri = new StringBuilder(); + String bucketName = request.getBucketName(); + String endpoint = this.getEndpoint(); + String objectKey = request.getObjectKey(); + + if (!this.isCname()) { + if (ServiceUtils.isValid(bucketName)) { + if (this.isPathStyle() || !ServiceUtils.isBucketNameValidDNSName(bucketName)) { + canonicalUri.append("/").append(bucketName.trim()); + } else { + endpoint = bucketName.trim() + "." + endpoint; + } + if (ServiceUtils.isValid(objectKey)) { + canonicalUri.append("/").append(RestUtils.uriEncode(objectKey, false)); + } + } + } else { + if (ServiceUtils.isValid(objectKey)) { + canonicalUri.append("/").append(RestUtils.uriEncode(objectKey, false)); + } + } - this.transSseCHeaders(request.getSseCHeaderDestination(), headers, iheaders); - this.transSseCSourceHeaders(request.getSseCHeaderSource(), headers, iheaders); + if (this.isCname()) { + endpoint = this.getEndpoint(); + } - return new TransResult(headers, params, null); - } + String requestMethod = request.getMethod() != null ? request.getMethod().getOperationType() : "GET"; + Map actualSignedRequestHeaders = new TreeMap(); + + Map headers = createBaseRequestHeaders(request, endpoint); + + StringBuilder signedHeaders = new StringBuilder(); + StringBuilder canonicalHeaders = new StringBuilder(); + concatHeaderString(bucketName, requestMethod, actualSignedRequestHeaders, + headers, signedHeaders, canonicalHeaders); + + Date requestDate = request.getRequestDate(); + if (requestDate == null) { + requestDate = new Date(); + } + String shortDate = ServiceUtils.getShortDateFormat().format(requestDate); + String longDate = ServiceUtils.getLongDateFormat().format(requestDate); + + BasicSecurityKey securityKey = this.getProviderCredentials().getSecurityKey(); + Map queryParams = createBaseQueryParams(request, signedHeaders.toString(), shortDate, + longDate, securityKey.getAccessKey(), securityKey.getSecurityToken()); + + StringBuilder signedUrl = createBaseSignedUrl(canonicalUri, endpoint); + StringBuilder canonicalQueryString = new StringBuilder(); + concatQueryString(queryParams, signedUrl, canonicalQueryString); + + StringBuilder canonicalRequest = new StringBuilder(requestMethod).append("\n") + .append(canonicalUri.length() == 0 ? "/" : canonicalUri).append("\n").append(canonicalQueryString) + .append("\n").append(canonicalHeaders).append("\n").append(signedHeaders).append("\n") + .append("UNSIGNED-PAYLOAD"); + + StringBuilder stringToSign = new StringBuilder(Constants.V4_ALGORITHM).append("\n").append(longDate) + .append("\n").append(shortDate).append("/").append(ObsConstraint.DEFAULT_BUCKET_LOCATION_VALUE) + .append("/").append(Constants.SERVICE).append("/").append(Constants.REQUEST_TAG).append("\n") + .append(V4Authentication.byteToHex((V4Authentication.sha256encode(canonicalRequest.toString())))); + signedUrl.append("&").append(Constants.V2_HEADER_PREFIX_CAMEL).append("Signature=") + .append(V4Authentication.caculateSignature(stringToSign.toString(), shortDate, + securityKey.getSecretKey())); + TemporarySignatureResponse response = new TemporarySignatureResponse(signedUrl.toString()); + response.getActualSignedRequestHeaders().putAll(actualSignedRequestHeaders); + return response; + } - protected CopyPartResult copyPartImpl(CopyPartRequest request) throws ServiceException { + private StringBuilder createBaseSignedUrl(StringBuilder canonicalUri, String endpoint) { + StringBuilder signedUrl = new StringBuilder(); + if (this.getHttpsOnly()) { + String securePortStr = this.getHttpsPort() == 443 ? "" : ":" + this.getHttpsPort(); + signedUrl.append("https://").append(endpoint).append(securePortStr); + } else { + String insecurePortStr = this.getHttpPort() == 80 ? "" : ":" + this.getHttpPort(); + signedUrl.append("http://").append(endpoint).append(insecurePortStr); + } + signedUrl.append(canonicalUri).append("?"); + return signedUrl; + } - TransResult result = this.transCopyPartRequest(request); - Response response = this.performRestPut(request.getDestinationBucketName(), request.getDestinationObjectKey(), - result.getHeaders(), result.getParams(), null, false); - this.verifyResponseContentType(response); + private void concatQueryString(Map queryParams, StringBuilder signedUrl, + StringBuilder canonicalQueryString) { + int index = 0; + for (Map.Entry entry : queryParams.entrySet()) { + if (ServiceUtils.isValid(entry.getKey())) { + String key = RestUtils.uriEncode(entry.getKey(), false); - CopyPartResult ret = getXmlResponseSaxParser() - .parse(new HttpMethodReleaseInputStream(response), CopyPartResultHandler.class, true) - .getCopyPartResult(request.getPartNumber()); - setResponseHeaders(ret, this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - HeaderResponse build(Response res){ - HeaderResponse response = new HeaderResponse(); - setResponseHeaders(response, this.cleanResponseHeaders(res)); - setStatusCode(response, res.code()); - return response; - } - - - static HeaderResponse build(Map responseHeaders){ - HeaderResponse response = new HeaderResponse(); - setResponseHeaders(response, responseHeaders); - return response; + canonicalQueryString.append(key).append("="); + signedUrl.append(key); + if (entry.getValue() != null) { + String value = RestUtils.uriEncode(entry.getValue().toString(), false); + canonicalQueryString.append(value); + signedUrl.append("=").append(value); + } else { + canonicalQueryString.append(""); + } + if (index++ != queryParams.size() - 1) { + canonicalQueryString.append("&"); + signedUrl.append("&"); + } + } + } } - - static void setStatusCode(HeaderResponse response, int statusCode) - { - response.setStatusCode(statusCode); + + private String formatHeaderKey(String bucketName, String requestMethod, String originalKey) { + if (Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(originalKey) + || originalKey.startsWith(this.getRestHeaderPrefix(bucketName)) + || originalKey.startsWith(Constants.OBS_HEADER_PREFIX)) { + return originalKey; + } else if (requestMethod.equals("PUT") || requestMethod.equals("POST")) { + return this.getRestMetadataPrefix(bucketName) + originalKey; + } + + return null; } - static void setResponseHeaders(HeaderResponse response, Map responseHeaders) - { - response.setResponseHeaders(responseHeaders); + private void concatHeaderString(String bucketName, String requestMethod, + Map actualSignedRequestHeaders, Map headers, + StringBuilder signedHeaders, StringBuilder canonicalHeaders) { + int index = 0; + for (Map.Entry entry : headers.entrySet()) { + if (ServiceUtils.isValid(entry.getKey())) { + String key = entry.getKey().toLowerCase().trim(); + key = formatHeaderKey(bucketName, requestMethod, key); + + if (null != key) { + String value = entry.getValue() == null ? "" : entry.getValue().trim(); + if (key.startsWith(this.getRestMetadataPrefix(bucketName))) { + value = RestUtils.uriEncode(value, true); + } + signedHeaders.append(key); + canonicalHeaders.append(key).append(":").append(value).append("\n"); + if (index++ != headers.size() - 1) { + signedHeaders.append(";"); + } + actualSignedRequestHeaders.put(entry.getKey().trim(), value); + } + } + } } - - private String getObjectUrl(String bucketName, String objectKey) { - boolean pathStyle = this.isPathStyle(); - boolean https = this.getHttpsOnly(); - boolean isCname = this.isCname(); - return new StringBuilder().append(https? "https://" : "http://") - .append(pathStyle || isCname? "" : bucketName + ".") - .append(this.getEndpoint()) - .append(":").append(https? this.getHttpsPort(): this.getHttpPort()) - .append("/") - .append(pathStyle? bucketName + "/" : "") - .append(RestUtils.uriEncode(objectKey, false)).toString(); - } - protected AuthTypeEnum getApiVersion(String bucketName) throws ServiceException { - if (!ServiceUtils.isValid(bucketName)) { - return parseAuthTypeInResponse(""); - } - AuthTypeEnum apiVersion = apiVersionCache.getApiVersionInCache(bucketName); - if (apiVersion == null) { - try { - segmentLock.lock(bucketName); - apiVersion = apiVersionCache.getApiVersionInCache(bucketName); - if (apiVersion == null) { - apiVersion = parseAuthTypeInResponse(bucketName); - apiVersionCache.addApiVersion(bucketName, apiVersion); - } - } finally { - segmentLock.unlock(bucketName); - } - } - return apiVersion; - } + private Map createBaseRequestHeaders(TemporarySignatureRequest request, String endpoint) { + Map headers = new TreeMap(); + headers.putAll(request.getHeaders()); + if ((this.getHttpsOnly() && this.getHttpsPort() == 443) || (!this.getHttpsOnly() && this.getHttpPort() == 80)) { + headers.put(CommonHeaders.HOST, endpoint); + } else { + headers.put(CommonHeaders.HOST, + endpoint + ":" + (this.getHttpsOnly() ? this.getHttpsPort() : this.getHttpPort())); + } + return headers; + } - private AuthTypeEnum parseAuthTypeInResponse(String bucketName) throws ServiceException { - Response response; - try { - response = getAuthTypeNegotiationResponseImpl(bucketName); - } catch (ServiceException e) { - if (e.getResponseCode() == 404 || e.getResponseCode() <= 0 || e.getResponseCode() == 408 || e.getResponseCode() >= 500) { - throw e; - } else { - return AuthTypeEnum.V2; - } - } - String apiVersion; - return (response.code() == 200 && (apiVersion=response.headers().get("x-obs-api")) != null && apiVersion.compareTo("3.0") >= 0) ? - AuthTypeEnum.OBS : AuthTypeEnum.V2; - } + private Map createBaseQueryParams(TemporarySignatureRequest request, String signedHeaders, + String shortDate, String longDate, String accessKey, String securityToken) { + Map queryParams = new TreeMap(); + queryParams.putAll(request.getQueryParams()); + if (!queryParams.containsKey(this.getIHeaders(request.getBucketName()).securityTokenHeader())) { + if (ServiceUtils.isValid(securityToken)) { + queryParams.put(this.getIHeaders(request.getBucketName()).securityTokenHeader(), securityToken); + } + } + + queryParams.put(Constants.V2_HEADER_PREFIX_CAMEL + "Algorithm", Constants.V4_ALGORITHM); + queryParams.put(Constants.V2_HEADER_PREFIX_CAMEL + "Credential", + this.getCredential(shortDate, accessKey)); + queryParams.put(Constants.V2_HEADER_PREFIX_CAMEL + "Date", longDate); + queryParams.put(Constants.V2_HEADER_PREFIX_CAMEL + "Expires", + request.getExpires() <= 0 ? ObsConstraint.DEFAULT_EXPIRE_SECONEDS : request.getExpires()); + queryParams.put(Constants.V2_HEADER_PREFIX_CAMEL + "SignedHeaders", signedHeaders); + + if (request.getSpecialParam() != null) { + if (request.getSpecialParam() == SpecialParamEnum.STORAGECLASS + || request.getSpecialParam() == SpecialParamEnum.STORAGEPOLICY) { + request.setSpecialParam(this.getSpecialParamForStorageClass(request.getBucketName())); + } + queryParams.put(request.getSpecialParam().getOriginalStringCode(), null); + } + + return queryParams; + } - private Response getAuthTypeNegotiationResponseImpl(String bucketName) throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put("apiversion", ""); - return performRestForApiVersion(bucketName, null, requestParameters, null); - } - - protected ThreadPoolExecutor initThreadPool(AbstractBulkRequest request) { + protected ThreadPoolExecutor initThreadPool(AbstractBulkRequest request) { int taskThreadNum = request.getTaskThreadNum(); int workQueenLength = request.getTaskQueueNum(); - ThreadPoolExecutor executor = new ThreadPoolExecutor(taskThreadNum, taskThreadNum, 0, TimeUnit.SECONDS, + ThreadPoolExecutor executor = new ThreadPoolExecutor(taskThreadNum, taskThreadNum, 0, TimeUnit.SECONDS, new LinkedBlockingQueue(workQueenLength)); executor.setRejectedExecutionHandler(new BlockRejectedExecutionHandler()); return executor; } - - protected void recordBulkTaskStatus(DefaultTaskProgressStatus progressStatus, TaskCallback callback, - TaskProgressListener listener, int interval) { - - progressStatus.execTaskIncrement(); + + protected void recordBulkTaskStatus(DefaultTaskProgressStatus progressStatus, + TaskCallback callback, TaskProgressListener listener, int interval) { + + progressStatus.execTaskIncrement(); if (listener != null) { if (progressStatus.getExecTaskNum() % interval == 0) { listener.progressChanged(progressStatus); @@ -3069,182 +547,5 @@ protected void recordBulkTaskStatus(DefaultTaskProgressStatus progressStatus, Ta listener.progressChanged(progressStatus); } } - } - - protected HeaderResponse setExtensionPolicyImpl(String bucketName, String policyDocument) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(RequestParamEnum.EXTENSION_POLICY.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_JSON); - metadata.put((this.getProviderCredentials().getAuthType() != AuthTypeEnum.OBS ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) - + Constants.OEF_MARKER, Constants.YES); - - Response response = performRestPut(bucketName, null, metadata, requestParameters, - this.createRequestBody(Mimetypes.MIMETYPE_JSON, policyDocument), false, true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected QueryExtensionPolicyResult queryExtensionPolicyImpl(String bucketName) throws ServiceException{ - Map requestParams = new HashMap(); - requestParams.put(RequestParamEnum.EXTENSION_POLICY.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - metadata.put((this.getProviderCredentials().getAuthType() != AuthTypeEnum.OBS ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) - + Constants.OEF_MARKER, Constants.YES); - - Response response = performRestGet(bucketName, null, requestParams, metadata, true); - - this.verifyResponseContentTypeForJson(response); - - String body; - try { - body = response.body().string(); - } catch (IOException e) { - throw new ServiceException(e); - } - - QueryExtensionPolicyResult ret = (QueryExtensionPolicyResult) JSONChange.jsonToObj(new QueryExtensionPolicyResult(), body); - ret.setResponseHeaders(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse deleteExtensionPolicyImpl(String bucketName) throws ServiceException { - Map requestParams = new HashMap(); - requestParams.put(RequestParamEnum.EXTENSION_POLICY.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - metadata.put((this.getProviderCredentials().getAuthType() != AuthTypeEnum.OBS ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) - + Constants.OEF_MARKER, Constants.YES); - - Response response = performRestDelete(bucketName, null, requestParams, metadata, true); - return this.build(response); - } - - protected CreateAsynchFetchJobsResult createFetchJobImpl(String bucketName, String policyDocument) throws ServiceException{ - Map requestParameters = new HashMap(); - requestParameters.put(RequestParamEnum.ASYNC_FETCH_JOBS.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_JSON); - metadata.put((this.getProviderCredentials().getAuthType() != AuthTypeEnum.OBS ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) - + Constants.OEF_MARKER, Constants.YES); - - Response response = performRestPost(bucketName, null, metadata, requestParameters, - this.createRequestBody(Mimetypes.MIMETYPE_JSON, policyDocument), false, true); - - this.verifyResponseContentTypeForJson(response); - - String body; - try { - body = response.body().string(); - } catch (IOException e) { - throw new ServiceException(e); - } - - CreateAsynchFetchJobsResult ret = (CreateAsynchFetchJobsResult) JSONChange.jsonToObj(new CreateAsynchFetchJobsResult(), body); - ret.setResponseHeaders(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected QueryAsynchFetchJobsResult queryFetchJobImpl(String bucketName, String jobId) throws ServiceException{ - Map requestParams = new HashMap(); - requestParams.put(RequestParamEnum.ASYNC_FETCH_JOBS.getOriginalStringCode() + "/" +jobId, ""); - - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_JSON); - metadata.put((this.getProviderCredentials().getAuthType() != AuthTypeEnum.OBS ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) - + Constants.OEF_MARKER, Constants.YES); - - Response response = performRestGet(bucketName, null, requestParams, metadata, true); - - this.verifyResponseContentTypeForJson(response); - - String body; - try { - body = response.body().string(); - } catch (IOException e) { - throw new ServiceException(e); - } - - QueryAsynchFetchJobsResult ret = (QueryAsynchFetchJobsResult) JSONChange.jsonToObj(new QueryAsynchFetchJobsResult(), body); - ret.setResponseHeaders(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - - return ret; - } - - protected HeaderResponse putDisPolicyImpl(String bucketName, String policyDocument) - throws ServiceException { - Map requestParameters = new HashMap(); - requestParameters.put(RequestParamEnum.DIS_POLICIES.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_JSON); - metadata.put((this.getProviderCredentials().getAuthType() != AuthTypeEnum.OBS ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) - + Constants.OEF_MARKER, Constants.YES); - - Response response = performRestPut(bucketName, null, metadata, requestParameters, - this.createRequestBody(Mimetypes.MIMETYPE_JSON, policyDocument), false, true); - HeaderResponse ret = build(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected GetDisPolicyResult getDisPolicyImpl(String bucketName) throws ServiceException{ - Map requestParams = new HashMap(); - requestParams.put(RequestParamEnum.DIS_POLICIES.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - metadata.put((this.getProviderCredentials().getAuthType() != AuthTypeEnum.OBS ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) - + Constants.OEF_MARKER, Constants.YES); - - Response response = performRestGet(bucketName, null, requestParams, metadata, true); - - this.verifyResponseContentTypeForJson(response); - - String body; - try { - body = response.body().string(); - } catch (IOException e) { - throw new ServiceException(e); - } - - DisPolicy policy = (DisPolicy) JSONChange.jsonToObj(new DisPolicy(), body); - GetDisPolicyResult ret = new GetDisPolicyResult(policy); - ret.setResponseHeaders(this.cleanResponseHeaders(response)); - setStatusCode(ret, response.code()); - return ret; - } - - protected HeaderResponse deleteDisPolicyImpl(String bucketName) throws ServiceException { - Map requestParams = new HashMap(); - requestParams.put(RequestParamEnum.DIS_POLICIES.getOriginalStringCode(), ""); - - Map metadata = new HashMap(); - metadata.put((this.getProviderCredentials().getAuthType() != AuthTypeEnum.OBS ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) - + Constants.OEF_MARKER, Constants.YES); - - Response response = performRestDelete(bucketName, null, requestParams, metadata, true); - return this.build(response); - } - - protected void verifyResponseContentTypeForJson(Response response) throws ServiceException { - if (this.obsProperties.getBoolProperty(ObsConstraint.VERIFY_RESPONSE_CONTENT_TYPE, true)) { - String contentType = response.header(Constants.CommonHeaders.CONTENT_TYPE); - if(null == contentType) { - throw new ServiceException( - "Expected JSON document response but received content type is null"); - } else if(-1 == contentType.toString().indexOf(Mimetypes.MIMETYPE_JSON)) { - throw new ServiceException( - "Expected JSON document response but received content type is " + contentType); - } - } - } - + } } diff --git a/app/src/main/java/com/obs/services/internal/OefExceptionMessage.java b/app/src/main/java/com/obs/services/internal/OefExceptionMessage.java index 1962f5e..4acf399 100644 --- a/app/src/main/java/com/obs/services/internal/OefExceptionMessage.java +++ b/app/src/main/java/com/obs/services/internal/OefExceptionMessage.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; import com.fasterxml.jackson.annotation.JsonProperty; @@ -20,58 +21,61 @@ * */ public class OefExceptionMessage { - @JsonProperty(value = "message") - private String message; - - @JsonProperty(value = "code") - private String code; - - @JsonProperty(value = "request_id") - private String request_id; - - public OefExceptionMessage() { - - } - - /** - * 构造函数 - * @param message 错误信息 - * @param code 错误码 - * @param request_id 请求ID - */ - public OefExceptionMessage(String message, String code, String request_id) { - this.message = message; - this.code = code; - this.request_id = request_id; - } + @JsonProperty(value = "message") + private String message; + + @JsonProperty(value = "code") + private String code; + + @JsonProperty(value = "request_id") + private String requestId; + + public OefExceptionMessage() { - public String getMessage() { - return message; - } + } - public void setMessage(String message) { - this.message = message; - } + /** + * 构造函数 + * + * @param message + * 错误信息 + * @param code + * 错误码 + * @param requestId + * 请求ID + */ + public OefExceptionMessage(String message, String code, String requestId) { + this.message = message; + this.code = code; + this.requestId = requestId; + } - public String getCode() { - return code; - } + public String getMessage() { + return message; + } - public void setCode(String code) { - this.code = code; - } + public void setMessage(String message) { + this.message = message; + } - public String getRequest_id() { - return request_id; - } + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getRequestId() { + return requestId; + } + + public void setRequestId(String requestId) { + this.requestId = requestId; + } - public void setRequest_id(String request_id) { - this.request_id = request_id; - } - - @Override - public String toString() - { - return "OefExceptionMessage [message=" + message + ", code=" + code + ", request_id" + request_id + "]"; + @Override + public String toString() { + return "OefExceptionMessage [message=" + message + ", code=" + code + ", request_id" + requestId + "]"; } } diff --git a/app/src/main/java/com/obs/services/internal/ProgressManager.java b/app/src/main/java/com/obs/services/internal/ProgressManager.java index e752c3a..6c88f10 100644 --- a/app/src/main/java/com/obs/services/internal/ProgressManager.java +++ b/app/src/main/java/com/obs/services/internal/ProgressManager.java @@ -11,66 +11,62 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; +import com.obs.services.model.ProgressListener; + import java.util.ArrayList; -import java.util.Date; import java.util.List; -import com.obs.services.model.ProgressListener; - public abstract class ProgressManager { - - static class BytesUnit{ - Date dateTime; - long bytes; - BytesUnit(Date dateTime, long bytes){ - this.dateTime = dateTime; - this.bytes = bytes; - } - } - - protected final long totalBytes; - protected Date startCheckpoint; - protected Date lastCheckpoint; - protected final long intervalBytes; - protected final ProgressListener progressListener; - protected volatile List lastInstantaneousBytes; - - public ProgressManager(long totalBytes, ProgressListener progressListener, - long intervalBytes) { - this.totalBytes = totalBytes; - this.progressListener = progressListener; - Date now = new Date(); - this.startCheckpoint = now; - this.lastCheckpoint = now; - this.intervalBytes = intervalBytes; - } - + + static class BytesUnit { + long dateTime; + long bytes; + + BytesUnit(long dateTime, long bytes) { + this.dateTime = dateTime; + this.bytes = bytes; + } + } + + protected final long totalBytes; + protected long startCheckpoint; + protected long lastCheckpoint; + protected final long intervalBytes; + protected final ProgressListener progressListener; + protected volatile List lastInstantaneousBytes = new ArrayList<>(); + + public ProgressManager(long totalBytes, ProgressListener progressListener, long intervalBytes) { + this.totalBytes = totalBytes; + this.progressListener = progressListener; + long now = System.currentTimeMillis(); + this.startCheckpoint = now; + this.lastCheckpoint = now; + this.intervalBytes = intervalBytes; + } + public void progressStart() { - Date now = new Date(); - this.startCheckpoint = now; - this.lastCheckpoint = now; + long now = System.currentTimeMillis(); + this.startCheckpoint = now; + this.lastCheckpoint = now; } - - + public final void progressChanged(int bytes) { - if(this.progressListener == null || bytes <= 0) { - return; - } - this.doProgressChanged(bytes); + if (this.progressListener == null || bytes <= 0) { + return; + } + this.doProgressChanged(bytes); } - - protected List createCurrentInstantaneousBytes(long bytes, Date now) - { + + @Deprecated + protected List createCurrentInstantaneousBytes(long bytes, long now) { List currentInstantaneousBytes = new ArrayList(); - List _lastInstantaneousBytes = this.lastInstantaneousBytes; - if (_lastInstantaneousBytes != null) - { - for(BytesUnit item : _lastInstantaneousBytes) - { - if ((now.getTime() - item.dateTime.getTime()) < 1000) - { + List temp = this.lastInstantaneousBytes; + if (temp != null) { + for (BytesUnit item : temp) { + if ((now - item.dateTime) < 1000) { currentInstantaneousBytes.add(item); } } @@ -78,8 +74,8 @@ protected List createCurrentInstantaneousBytes(long bytes, Date now) currentInstantaneousBytes.add(new BytesUnit(now, bytes)); return currentInstantaneousBytes; } - + public abstract void progressEnd(); - + protected abstract void doProgressChanged(int bytes); } diff --git a/app/src/main/java/com/obs/services/internal/RepeatableRequestEntity.java b/app/src/main/java/com/obs/services/internal/RepeatableRequestEntity.java index 3bc04aa..2bfe4e6 100644 --- a/app/src/main/java/com/obs/services/internal/RepeatableRequestEntity.java +++ b/app/src/main/java/com/obs/services/internal/RepeatableRequestEntity.java @@ -11,15 +11,12 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; -import java.nio.ByteBuffer; -import java.nio.channels.Channels; -import java.nio.channels.ReadableByteChannel; -import java.nio.channels.WritableByteChannel; import com.obs.log.ILogger; import com.obs.log.LoggerBuilder; @@ -31,127 +28,92 @@ import okio.BufferedSink; public class RepeatableRequestEntity extends RequestBody implements Closeable { - private static final ILogger interfaceLog = LoggerBuilder.getLogger("com.obs.services.internal.RestStorageService"); - - private String contentType; - private long contentLength = -1; - - private volatile long bytesWritten = 0; - private InputStream inputStream; - - private static final int writeBufferSize = ObsConstraint.DEFAULT_CHUNK_SIZE; - - public RepeatableRequestEntity(InputStream is, String contentType, long contentLength, - ObsProperties obsProperties) { - if (is == null) { - throw new IllegalArgumentException("InputStream cannot be null"); - } - this.inputStream = is; - this.contentLength = contentLength; - this.contentType = contentType; - - if (!(this.inputStream instanceof MayRepeatableInputStream)) { - this.inputStream = new MayRepeatableInputStream(is, obsProperties.getIntProperty(ObsConstraint.WRITE_BUFFER_SIZE, - ObsConstraint.DEFAULT_WRITE_BUFFER_STREAM)); - } - this.inputStream.mark(0); - } - - @Override - public long contentLength() throws IOException { - return this.contentLength; - } - - public boolean isRepeatable() { - return this.inputStream == null || this.inputStream.markSupported(); - } - - protected void writeToNIO(BufferedSink out) throws IOException { - ReadableByteChannel in = Channels.newChannel(this.inputStream); - WritableByteChannel ou = Channels.newChannel(out.outputStream()); - ByteBuffer buffer = ByteBuffer.allocate(this.writeBufferSize); - - int count = 0; - if (this.contentLength < 0) { - count = in.read(buffer); - while (count > 0) { - buffer.flip(); - while (buffer.hasRemaining()) { - ou.write(buffer); - } - buffer.clear(); - this.bytesWritten += count; - count = in.read(buffer); - } - } else { - long remaining = this.contentLength; - while (remaining > 0) { - count = in.read(buffer); - if (count <= 0) { - break; - } - buffer.position((int) Math.min(this.writeBufferSize, remaining)); - buffer.flip(); - while (buffer.hasRemaining()) { - ou.write(buffer); - } - buffer.clear(); - this.bytesWritten += count; - remaining -= count; - } - } - } - - protected void writeToBIO(BufferedSink out) throws IOException { - byte[] tmp = new byte[this.writeBufferSize]; - int count = 0; - if (this.contentLength < 0) { - count = this.inputStream.read(tmp); - while (count != -1) { - bytesWritten += count; - out.write(tmp, 0, count); - count = this.inputStream.read(tmp); - } - } else { - // consume no more than length - long remaining = this.contentLength; - while (remaining > 0) { - count = inputStream.read(tmp, 0, (int) Math.min(this.writeBufferSize, remaining)); - if (count == -1) { - break; - } - out.write(tmp, 0, count); - bytesWritten += count; - remaining -= count; - } - } - } - - @Override - public MediaType contentType() { - return MediaType.parse(this.contentType == null ? Mimetypes.MIMETYPE_OCTET_STREAM : this.contentType); - } - - @Override - public void writeTo(BufferedSink sink) throws IOException { - - long start = System.currentTimeMillis(); - if (bytesWritten > 0) { - inputStream.reset(); - bytesWritten = 0; - } -// this.writeToNIO(sink); - this.writeToBIO(sink); - if (interfaceLog.isInfoEnabled()) { - interfaceLog.info("write data end, cost " + (System.currentTimeMillis() - start) + " ms"); - } - } - - @Override - public void close() throws IOException { - if (this.inputStream != null) { - this.inputStream.close(); - } - } + private static final ILogger INTERFACE_LOG = + LoggerBuilder.getLogger("com.obs.services.internal.RestStorageService"); + + private String contentType; + private long contentLength = -1; + + private volatile long bytesWritten = 0; + private InputStream inputStream; + + private static final int WRITE_BUFFER_SIZE = ObsConstraint.DEFAULT_CHUNK_SIZE; + + public RepeatableRequestEntity(InputStream is, String contentType, long contentLength, + ObsProperties obsProperties) { + if (is == null) { + throw new IllegalArgumentException("InputStream cannot be null"); + } + this.inputStream = is; + this.contentLength = contentLength; + this.contentType = contentType; + + if (!(this.inputStream instanceof MayRepeatableInputStream)) { + this.inputStream = new MayRepeatableInputStream(is, obsProperties + .getIntProperty(ObsConstraint.WRITE_BUFFER_SIZE, ObsConstraint.DEFAULT_WRITE_BUFFER_STREAM)); + } + this.inputStream.mark(0); + } + + @Override + public long contentLength() throws IOException { + return this.contentLength; + } + + public boolean isRepeatable() { + return this.inputStream == null || this.inputStream.markSupported(); + } + + protected void writeToBIO(BufferedSink out) throws IOException { + byte[] tmp = new byte[WRITE_BUFFER_SIZE]; + int count = 0; + if (this.contentLength < 0) { + count = this.inputStream.read(tmp); + while (count != -1) { + bytesWritten += count; + out.write(tmp, 0, count); + count = this.inputStream.read(tmp); + } + } else { + // consume no more than length + long remaining = this.contentLength; + while (remaining > 0) { + count = inputStream.read(tmp, 0, (int) Math.min(WRITE_BUFFER_SIZE, remaining)); + if (count == -1) { + break; + } + out.write(tmp, 0, count); + bytesWritten += count; + remaining -= count; + } + } + } + + @Override + public MediaType contentType() { + return MediaType.parse(this.contentType == null ? Mimetypes.MIMETYPE_OCTET_STREAM : this.contentType); + } + + @Override + public void writeTo(BufferedSink sink) throws IOException { + + long start = System.currentTimeMillis(); + if (bytesWritten > 0) { + inputStream.reset(); + bytesWritten = 0; + } + // this.writeToNIO(sink); + this.writeToBIO(sink); + if (INTERFACE_LOG.isInfoEnabled()) { + INTERFACE_LOG.info("write data end, cost " + (System.currentTimeMillis() - start) + " ms"); + } + } + + @Override + public void close() throws IOException { + if (this.inputStream != null) { + this.inputStream.close(); + } + } } diff --git a/app/src/main/java/com/obs/services/internal/RestConnectionService.java b/app/src/main/java/com/obs/services/internal/RestConnectionService.java new file mode 100644 index 0000000..0623efc --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/RestConnectionService.java @@ -0,0 +1,264 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal; + +import java.lang.reflect.Method; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicBoolean; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.TrustManagerFactory; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.internal.security.ProviderCredentials; +import com.obs.services.internal.utils.RestUtils; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.HttpMethodEnum; + +import okhttp3.Dispatcher; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; + +public class RestConnectionService { + private static final ILogger log = LoggerBuilder.getLogger(RestConnectionService.class); + + protected OkHttpClient httpClient; + + protected ObsProperties obsProperties; + + protected KeyManagerFactory keyManagerFactory; + + protected TrustManagerFactory trustManagerFactory; + + protected Semaphore semaphore; + + protected AtomicBoolean shuttingDown = new AtomicBoolean(false); + + protected volatile ProviderCredentials credentials; + + protected void initHttpClient(Dispatcher httpDispatcher) { + + OkHttpClient.Builder builder = RestUtils.initHttpClientBuilder(obsProperties, keyManagerFactory, + trustManagerFactory, httpDispatcher); + + if (this.obsProperties.getBoolProperty(ObsConstraint.PROXY_ISABLE, true)) { + String proxyHostAddress = this.obsProperties.getStringProperty(ObsConstraint.PROXY_HOST, null); + int proxyPort = this.obsProperties.getIntProperty(ObsConstraint.PROXY_PORT, -1); + String proxyUser = this.obsProperties.getStringProperty(ObsConstraint.PROXY_UNAME, null); + String proxyPassword = this.obsProperties.getStringProperty(ObsConstraint.PROXY_PAWD, null); + RestUtils.initHttpProxy(builder, proxyHostAddress, proxyPort, proxyUser, proxyPassword); + } + + this.httpClient = builder.build(); + // Fix okhttp bug + int maxConnections = this.obsProperties.getIntProperty(ObsConstraint.HTTP_MAX_CONNECT, + ObsConstraint.HTTP_MAX_CONNECT_VALUE); + this.semaphore = new Semaphore(maxConnections); + } + + protected void shutdown() { + this.shutdownImpl(); + } + + protected void shutdownImpl() { + if (shuttingDown.compareAndSet(false, true)) { + this.credentials = null; + this.obsProperties = null; + if (this.httpClient != null) { + invokeShutdown(); + if (httpClient.connectionPool() != null) { + httpClient.connectionPool().evictAll(); + } + httpClient = null; + } + } + } + + private void invokeShutdown() { + try { + Method dispatcherMethod = httpClient.getClass().getMethod("dispatcher"); + if (dispatcherMethod != null) { + Method m = dispatcherMethod.invoke(httpClient).getClass().getDeclaredMethod("executorService"); + // fix findbugs: DP_DO_INSIDE_DO_PRIVILEGED + // m.setAccessible(true); + Object exeService = m.invoke(httpClient.dispatcher()); + if (exeService instanceof ExecutorService) { + ExecutorService executorService = (ExecutorService) exeService; + executorService.shutdown(); + } + } + } catch (Exception e) { + // ignore + if (log.isWarnEnabled()) { + log.warn("shows some exceptions at the time of shutdown httpClient. ", e); + } + } + } + + protected Request.Builder setupConnection(HttpMethodEnum method, String bucketName, String objectKey, + Map requestParameters, RequestBody body) + throws ServiceException { + return this.setupConnection(method, bucketName, objectKey, requestParameters, body, false); + } + + protected Request.Builder setupConnection(HttpMethodEnum method, String bucketName, String objectKey, + Map requestParameters, RequestBody body, boolean isOEF) + throws ServiceException { + return this.setupConnection(method, bucketName, objectKey, requestParameters, body, isOEF, false); + } + + protected Request.Builder setupConnection(HttpMethodEnum method, String bucketName, String objectKey, + Map requestParameters, RequestBody body, boolean isOEF, + boolean isListBuckets) + throws ServiceException { + + boolean pathStyle = this.isPathStyle(); + String endPoint = this.getEndpoint(); + boolean isCname = this.isCname(); + String hostname = (isCname || isListBuckets) ? endPoint + : ServiceUtils.generateHostnameForBucket(RestUtils.encodeUrlString(bucketName), pathStyle, endPoint); + String resourceString = "/"; + if (hostname.equals(endPoint) && !isCname && bucketName.length() > 0) { + resourceString += RestUtils.encodeUrlString(bucketName); + } + if (objectKey != null) { + resourceString += ((pathStyle && !isCname) ? "/" : "") + RestUtils.encodeUrlString(objectKey); + } + + String url = addProtocol(hostname, resourceString); + url = addRequestParametersToUrlPath(url, requestParameters, isOEF); + + Request.Builder builder = createRequestBuilder(method, body, url); + + return builder; + } + + private Request.Builder createRequestBuilder(HttpMethodEnum method, RequestBody body, String url) { + Request.Builder builder = new Request.Builder(); + builder.url(url); + if (body == null) { + body = RequestBody.create("", null); + } + switch (method) { + case PUT: + builder.put(body); + break; + case POST: + builder.post(body); + break; + case HEAD: + builder.head(); + break; + case GET: + builder.get(); + break; + case DELETE: + builder.delete(body); + break; + case OPTIONS: + builder.method("OPTIONS", null); + break; + default: + throw new IllegalArgumentException("Unrecognised HTTP method name: " + method); + } + + if (!this.isKeepAlive()) { + builder.addHeader("Connection", "Close"); + } + return builder; + } + + private String addProtocol(String hostname, String resourceString) { + String url = null; + if (getHttpsOnly()) { + int securePort = this.getHttpsPort(); + String securePortStr = securePort == 443 ? "" : ":" + securePort; + url = "https://" + hostname + securePortStr + resourceString; + } else { + int insecurePort = this.getHttpPort(); + String insecurePortStr = insecurePort == 80 ? "" : ":" + insecurePort; + url = "http://" + hostname + insecurePortStr + resourceString; + } + if (log.isDebugEnabled()) { + log.debug("OBS URL: " + url); + } + return url; + } + + protected String addRequestParametersToUrlPath(String urlPath, Map requestParameters, boolean isOEF) + throws ServiceException { + StringBuilder urlPathBuilder = new StringBuilder(urlPath); + if (requestParameters != null) { + for (Map.Entry entry : requestParameters.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if (isOEF) { + if (isPathStyle()) { + urlPathBuilder.append("/").append(key); + } else { + urlPathBuilder.append(key); + } + } else { + urlPathBuilder.append((urlPathBuilder.indexOf("?") < 0 ? "?" : "&")) + .append(RestUtils.encodeUrlString(key)); + } + if (ServiceUtils.isValid(value)) { + urlPathBuilder.append("=").append(RestUtils.encodeUrlString(value)); + if (log.isDebugEnabled()) { + log.debug("Added request parameter: " + key + "=" + value); + } + } else { + if (log.isDebugEnabled()) { + log.debug("Added request parameter without value: " + key); + } + } + } + } + return urlPathBuilder.toString(); + } + + protected String getEndpoint() { + return this.obsProperties.getStringProperty(ObsConstraint.END_POINT, ""); + } + + protected int getHttpPort() { + return this.obsProperties.getIntProperty(ObsConstraint.HTTP_PORT, ObsConstraint.HTTP_PORT_VALUE); + } + + protected int getHttpsPort() { + return this.obsProperties.getIntProperty(ObsConstraint.HTTPS_PORT, ObsConstraint.HTTPS_PORT_VALUE); + } + + protected boolean isKeepAlive() { + return this.obsProperties.getBoolProperty(ObsConstraint.KEEP_ALIVE, true); + } + + protected boolean isPathStyle() { + return this.obsProperties.getBoolProperty(ObsConstraint.DISABLE_DNS_BUCKET, false); + } + + protected boolean isCname() { + return this.obsProperties.getBoolProperty(ObsConstraint.IS_CNAME, false); + } + + protected boolean getHttpsOnly() { + return this.obsProperties.getBoolProperty(ObsConstraint.HTTPS_ONLY, true); + } +} diff --git a/app/src/main/java/com/obs/services/internal/RestStorageService.java b/app/src/main/java/com/obs/services/internal/RestStorageService.java index fbfb94d..a9054e1 100644 --- a/app/src/main/java/com/obs/services/internal/RestStorageService.java +++ b/app/src/main/java/com/obs/services/internal/RestStorageService.java @@ -1,1274 +1,1066 @@ /** - * - * JetS3t : Java S3 Toolkit - * Project hosted at http://bitbucket.org/jmurty/jets3t/ - * - * Copyright 2006-2010 James Murty - * * Copyright 2019 Huawei Technologies Co.,Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; import com.obs.log.ILogger; import com.obs.log.InterfaceLogBean; import com.obs.log.LoggerBuilder; import com.obs.services.internal.Constants.CommonHeaders; -import com.obs.services.internal.consensus.CacheManager; -import com.obs.services.internal.consensus.SegmentLock; +import com.obs.services.internal.ext.ExtObsConstraint; import com.obs.services.internal.handler.XmlResponsesSaxParser; import com.obs.services.internal.io.UnrecoverableIOException; import com.obs.services.internal.security.BasicSecurityKey; import com.obs.services.internal.security.ProviderCredentialThreadContext; import com.obs.services.internal.security.ProviderCredentials; -import com.obs.services.internal.utils.*; +import com.obs.services.internal.trans.NewTransResult; +import com.obs.services.internal.utils.IAuthentication; +import com.obs.services.internal.utils.JSONChange; +import com.obs.services.internal.utils.Mimetypes; +import com.obs.services.internal.utils.RestUtils; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.internal.utils.V4Authentication; import com.obs.services.model.AuthTypeEnum; import com.obs.services.model.HttpMethodEnum; -import okhttp3.*; +import okhttp3.Call; +import okhttp3.Headers; +import okhttp3.Request; +import okhttp3.Response; -import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLException; -import javax.net.ssl.TrustManagerFactory; import java.io.IOException; import java.io.InterruptedIOException; -import java.lang.reflect.Method; import java.net.ConnectException; import java.net.URI; import java.net.UnknownHostException; import java.text.ParseException; -import java.util.*; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Semaphore; -import java.util.concurrent.atomic.AtomicBoolean; - -public abstract class RestStorageService { - private static final ILogger log = LoggerBuilder.getLogger(RestStorageService.class); - - private static final Set> nonRetriableClasses = new HashSet>(); - - private static final String REQUEST_TIMEOUT_CODE = "RequestTimeout"; - - static { - nonRetriableClasses.add(UnknownHostException.class); - nonRetriableClasses.add(SSLException.class); - nonRetriableClasses.add(ConnectException.class); - } - - protected OkHttpClient httpClient; - - protected AtomicBoolean shuttingDown = new AtomicBoolean(false); - - protected ObsProperties obsProperties; - - protected volatile ProviderCredentials credentials; - - protected KeyManagerFactory keyManagerFactory; - - protected TrustManagerFactory trustManagerFactory; - - protected CacheManager apiVersionCache; - - protected SegmentLock segmentLock; - - protected Semaphore semaphore; - - protected ThreadLocal> userHeaders = new ThreadLocal>(); - - // switch of using standard http headers - protected ThreadLocal canUseStandardHTTPHeaders = new ThreadLocal(); - - protected RestStorageService() { - - } - - /** - * set switch of using standard http headers - * @param canUseStandardHTTPHeaders A Boolean variable to control switch of using http standard headers - */ - public void setCanUseStandardHTTPHeaders(Boolean canUseStandardHTTPHeaders) - { - this.canUseStandardHTTPHeaders.set(canUseStandardHTTPHeaders); - } - - /** - * set user headers - * @param userHeaders A HashMap - */ - public void setUserHeaders(HashMap userHeaders){ - this.userHeaders.set(userHeaders); - } - - /** - * add user headers to Request - * @param builder Request in OKHttp3.0 - */ - private void addUserHeaderToRequest(Request.Builder builder){ - HashMap userHeaderMap = this.userHeaders.get(); - //check headers - for (Map.Entry entry : userHeaderMap.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - if (!ServiceUtils.isValid(key)) { - continue; - } - key = key.trim(); - if (!key.startsWith(this.getRestHeaderPrefix()) && !key.startsWith(Constants.OBS_HEADER_PREFIX) - && !Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES - .contains(key.toLowerCase(Locale.getDefault()))) { - key = this.getRestMetadataPrefix() + key; - } - try { - if(key.startsWith(this.getRestMetadataPrefix())) { - key = RestUtils.uriEncode(key, true); - } - builder.addHeader(key, RestUtils.uriEncode(value == null ? "" : value, true)); - } catch (ServiceException e) { - if (log.isDebugEnabled()) { - log.debug("Ignore metadata key:" + key); - } - } - } - } - - protected void initHttpClient(Dispatcher httpDispatcher) { - - OkHttpClient.Builder builder = RestUtils.initHttpClientBuilder(this, obsProperties, keyManagerFactory, - trustManagerFactory, httpDispatcher); - - if (this.obsProperties.getBoolProperty(ObsConstraint.PROXY_ISABLE, true)) { - String proxyHostAddress = this.obsProperties.getStringProperty(ObsConstraint.PROXY_HOST, null); - int proxyPort = this.obsProperties.getIntProperty(ObsConstraint.PROXY_PORT, -1); - String proxyUser = this.obsProperties.getStringProperty(ObsConstraint.PROXY_UNAME, null); - String proxyPassword = this.obsProperties.getStringProperty(ObsConstraint.PROXY_PAWD, null); - String proxyDomain = this.obsProperties.getStringProperty(ObsConstraint.PROXY_DOMAIN, null); - String proxyWorkstation = this.obsProperties.getStringProperty(ObsConstraint.PROXY_WORKSTATION, null); - RestUtils.initHttpProxy(builder, proxyHostAddress, proxyPort, proxyUser, proxyPassword, proxyDomain, - proxyWorkstation); - } - - this.httpClient = builder.build(); - //Fix okhttp bug - int maxConnections = this.obsProperties.getIntProperty(ObsConstraint.HTTP_MAX_CONNECT, ObsConstraint.HTTP_MAX_CONNECT_VALUE); - this.semaphore = new Semaphore(maxConnections); - } - - protected void shutdownImpl() { - if(shuttingDown.compareAndSet(false, true)) { - this.credentials = null; - this.obsProperties = null; - if (this.httpClient != null) { - try { - Method dispatcherMethod = httpClient.getClass().getMethod("dispatcher"); - if (dispatcherMethod != null) { - Method m = dispatcherMethod.invoke(httpClient).getClass().getDeclaredMethod("executorService"); - m.setAccessible(true); - Object exeService = m.invoke(httpClient.dispatcher()); - if (exeService instanceof ExecutorService) { - ExecutorService executorService = (ExecutorService)exeService; - executorService.shutdown(); - } - } - } catch (Exception e) { - //ignore - } - if (httpClient.connectionPool() != null) { - httpClient.connectionPool().evictAll(); - } - httpClient = null; - } - } - if (apiVersionCache != null) { - apiVersionCache.clear(); - apiVersionCache = null; - } - if (segmentLock != null) { - segmentLock.clear(); - segmentLock = null; - } - } - - protected boolean retryRequest(IOException exception, int executionCount, int retryMaxCount, - Request request, Call call) { - if (executionCount > retryMaxCount) { - return false; - } - if (nonRetriableClasses.contains(exception.getClass())) { - return false; - } else { - for (final Class rejectException : nonRetriableClasses) { - if (rejectException.isInstance(exception)) { - return false; - } - } - } - - if(call.isCanceled()) { - return false; - } - - return true; - } - - private ServiceException handleThrowable(Request request, Response response, InterfaceLogBean reqBean, Call call, Throwable t) { - - ServiceException serviceException = (t instanceof ServiceException) ? (ServiceException) t : new ServiceException("Request Error: " + t, t); - serviceException.setRequestHost(request.header(CommonHeaders.HOST)); - serviceException.setRequestVerb(request.method()); - serviceException.setRequestPath(request.url().toString()); - - if(response != null) { - ServiceUtils.closeStream(response); - serviceException.setResponseCode(response.code()); - serviceException.setResponseStatus(response.message()); - serviceException.setResponseDate(response.header(CommonHeaders.DATE)); - serviceException.setErrorIndicator(response.header(CommonHeaders.X_RESERVED_INDICATOR)); - serviceException - .setResponseHeaders(ServiceUtils.cleanRestMetadataMapV2(convertHeadersToMap(response.headers()), - getRestHeaderPrefix(), getRestMetadataPrefix())); - if(!ServiceUtils.isValid(serviceException.getErrorRequestId())) { - serviceException.setRequestAndHostIds(response.header(getIHeaders().requestIdHeader()), - response.header(getIHeaders().requestId2Header())); - } - } - - if (log.isWarnEnabled()) { - log.warn(serviceException); - } - - if (call != null) { - call.cancel(); - } - return serviceException; - } - - private void performRequestAsync(final Request request, final RequestContext context, final ObsCallback callback) throws InterruptedException{ - this.performRequestAsync(request, context, callback, false); - } - - private boolean isLocationHostOnly(String location) { - boolean isOnlyHost = false; - - URI uri; - uri = URI.create(location); - String path = uri.getPath(); - - if (location.indexOf("?") < 0) { - if(path == null || path.isEmpty() || path.equals("/")) { - isOnlyHost = true; - } - } - - return isOnlyHost; - } - - private void performRequestAsync(final Request request, final RequestContext context, final ObsCallback callback, final boolean isOEF) throws InterruptedException { - - Call call = httpClient.newCall(request); - final long start = System.currentTimeMillis(); - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) throws IOException { - try { - int responseCode = response.code(); - context.reqBean.setRespParams("[responseCode: " + responseCode + "][request-id: " - + response.header(getIHeaders().requestIdHeader(), "") + "]"); - - String contentType = response.header(CommonHeaders.CONTENT_TYPE); - if(log.isDebugEnabled()) { - log.debug("Response for '" + context.method + "'. Content-Type: " + contentType + ", ResponseCode:" + responseCode + ", Headers: " - + response.headers()); - } - if (log.isTraceEnabled()) { - if (response.body() != null) { - log.trace("Entity length: " + response.body().contentLength()); - } - } - - if (responseCode >= 300 && responseCode < 400 && responseCode != 304) { - String location = response.header(CommonHeaders.LOCATION); - if (!ServiceUtils.isValid(location)) { - ServiceException exception = new ServiceException("Try to redirect, but location is null!"); - context.reqBean.setResponseInfo("Request Error:" + exception.getMessage(), - "|" + responseCode + "|" + response.message() + "|"); - throw exception; - } - - if (location.indexOf("?") < 0) { - location = addRequestParametersToUrlPath(location, context.requestParameters, isOEF); - } - - context.internalErrorCount++; - - if (context.internalErrorCount > context.retryMaxCount) { - String xmlMessage = null; - try { - if( response.body() != null) { - xmlMessage = response.body().string(); - } - }catch (IOException e) { - } - throw new ServiceException("Exceeded 3xx redirect limit (" + context.retryMaxCount + ").", xmlMessage); - }else { - ServiceUtils.closeStream(response); - } - - if(context.doSignature && isLocationHostOnly(location)) { - performRequestAsync(authorizeHttpRequest(request, context.bucketName, location), context, callback); - }else { - Request.Builder builder = request.newBuilder(); - RestStorageService.this.setHost(builder, request, location); - performRequestAsync(builder.build(), context, callback); - } - return; - }else if((responseCode >= 400 && responseCode < 500) || responseCode == 304) { - String xmlMessage = null; - try { - if( response.body() != null) { - xmlMessage = response.body().string(); - } - }catch (IOException e) { - } - ServiceException exception = new ServiceException("Request Error.", xmlMessage); - if (REQUEST_TIMEOUT_CODE.equals(exception.getErrorCode())) { - context.internalErrorCount++; - if (context.internalErrorCount < context.retryMaxCount) { - if (log.isWarnEnabled()) { - log.warn("Retrying connection that failed with RequestTimeout error" - + ", attempt number " + context.internalErrorCount + " of " + context.retryMaxCount); - } - performRequestAsync(authorizeHttpRequest(request, context.bucketName, null), context, callback); - return; - } - if (log.isErrorEnabled()) { - log.error("Exceeded maximum number of retries for RequestTimeout errors: " - + context.retryMaxCount); - } - } - throw exception; - }else if (responseCode >= 500) { - context.reqBean.setResponseInfo("Internal Server error(s).", String.valueOf(responseCode)); - if (log.isErrorEnabled()) { - log.error(context.reqBean); - } - context.internalErrorCount++; - sleepOnInternalError(context.internalErrorCount, context.retryMaxCount, response, context.reqBean); - performRequestAsync(authorizeHttpRequest(request, context.bucketName, null), context, callback); - return; - } - if (log.isInfoEnabled()) { - context.reqBean.setRespTime(new Date()); - context.reqBean.setResultCode(Constants.RESULTCODE_SUCCESS); - log.info(context.reqBean); - } - callback.onSuccess(response); - } catch (Throwable t) { - ServiceException s = handleThrowable(request, response, context.reqBean, call, t); - callback.onFailure(s); - }finally { - if (log.isInfoEnabled()) { - log.info("OkHttp cost " + (System.currentTimeMillis() - start) + " ms to apply http request"); - } - } - } - - @Override - public void onFailure(Call call, IOException e) { - try { - if (e instanceof UnrecoverableIOException) { - if (context.lastException != null) { - throw context.lastException; - } else { - throw e; - } - } - context.lastException = e; - context.internalErrorCount++; - if (retryRequest(e, context.internalErrorCount, context.retryMaxCount, request, call)) { - long delayMs = 50L * (int) Math.pow(2, context.internalErrorCount); - Thread.sleep(delayMs); - performRequestAsync(authorizeHttpRequest(request, context.bucketName, null), context, callback); - return; - } - - if ((e instanceof ConnectException) || (e instanceof InterruptedIOException)) { - ServiceException se = new ServiceException("Request error. ", e); - se.setResponseCode(408); - se.setErrorCode("RequestTimeOut"); - se.setErrorMessage(e.getMessage()); - se.setResponseStatus("Request error. "); - throw se; - } - throw e; - }catch (Throwable t) { - ServiceException s = handleThrowable(request, null, context.reqBean, call, t); - callback.onFailure(s); - }finally { - if (log.isInfoEnabled()) { - log.info("OkHttp cost " + (System.currentTimeMillis() - start) + " ms to apply http request"); - } - } - } - }); - } - - private static class RequestContext{ - InterfaceLogBean reqBean; - String method; - int retryMaxCount; - int internalErrorCount = 0; - String bucketName; - Exception lastException; - boolean doSignature; - Map requestParameters; - } - - private static class ResponseContext{ - Response response; - ServiceException ex; - } - - protected void performRequestAsync(Request request, Map requestParameters, String bucketName, boolean doSignature, ObsCallback callback) throws ServiceException, InterruptedException { - InterfaceLogBean reqBean = new InterfaceLogBean("performRequest", "", ""); - - if (log.isDebugEnabled()) { - log.debug("Performing " + request.method() + " request for '" + request.url()); - log.debug("Headers: " + request.headers()); - } - RequestContext context = new RequestContext(); - context.reqBean = reqBean; - context.method = request.method(); - context.retryMaxCount = obsProperties.getIntProperty(ObsConstraint.HTTP_RETRY_MAX, - ObsConstraint.HTTP_RETRY_MAX_VALUE); - context.bucketName = bucketName; - context.requestParameters = requestParameters; - context.doSignature = doSignature; - - if(doSignature) { - request = authorizeHttpRequest(request, bucketName, null); - }else { - Request.Builder builder = request.newBuilder(); - builder.headers(request.headers().newBuilder().removeAll(CommonHeaders.AUTHORIZATION).build()); - this.setHost(builder, request, null); - request = builder.build(); - } - this.performRequestAsync(request, context, callback); - } - - protected Response performRequestAsync(Request request, Map requestParameters, String bucketName, boolean doSignature) throws ServiceException { - final CountDownLatch latch = new CountDownLatch(1); - final ResponseContext context = new ResponseContext(); - try { - this.performRequestAsync(request, requestParameters, bucketName, doSignature, new ObsCallback() { - - @Override - public void onSuccess(Response result) { - context.response = result; - latch.countDown(); - } - - @Override - public void onFailure(ServiceException e) { - context.ex = e; - latch.countDown(); - } - }); - latch.await(); - }catch (InterruptedException e) { - throw new ServiceException(e); - } - - if(context.ex != null) { - throw context.ex; - } - - return context.response; - } - - protected Response performRequestAsync(Request request, Map requestParameters, String bucketName) throws ServiceException { - return this.performRequestAsync(request, requestParameters, bucketName, true); - } - - protected Response performRequesttWithoutSignatureAsync(Request request, Map requestParameters, String bucketName) throws ServiceException { - return this.performRequestAsync(request, requestParameters, bucketName, false); - } - - - protected Response performRequest(Request request, Map requestParameters, String bucketName) throws ServiceException { - return performRequest(request, requestParameters, bucketName, true); - } - - protected Response performRequestWithoutSignature(Request request, Map requestParameters, String bucketName) throws ServiceException { - return performRequest(request, requestParameters, bucketName, false); - } - - protected Response performRequest(Request request, Map requestParameters, String bucketName, boolean doSignature) throws ServiceException{ - return this.performRequest(request, requestParameters, bucketName, doSignature, false); - } - - protected Response performRequest(Request request, Map requestParameters, String bucketName, boolean doSignature, boolean isOEF) throws ServiceException { - Response response = null; - - if(this.userHeaders.get() != null && this.userHeaders.get().size() > 0){ - // create a new Builder from current Request - Request.Builder builderTmp = request.newBuilder(); - // add user header - addUserHeaderToRequest(builderTmp); - // build new request - request = builderTmp.build(); - } - - InterfaceLogBean reqBean = new InterfaceLogBean("performRequest", "", ""); - Call call = null; - try { - if (log.isDebugEnabled()) { - log.debug("Performing " + request.method() + " request for '" + request.url()); - log.debug("Headers: " + request.headers()); - } - - boolean completedWithoutRecoverableError = false; - int internalErrorCount = 0; - boolean wasRecentlyRedirected = false; - Exception lastException = null; - int responseCode = -1; - int retryMaxCount = obsProperties.getIntProperty(ObsConstraint.HTTP_RETRY_MAX, - ObsConstraint.HTTP_RETRY_MAX_VALUE); - do { - if (!wasRecentlyRedirected) { - if(doSignature) { - request = authorizeHttpRequest(request, bucketName, null); - }else { - Request.Builder builder = request.newBuilder(); - builder.headers(request.headers().newBuilder().removeAll(CommonHeaders.AUTHORIZATION).build()); - this.setHost(builder, request, null); - builder.header(CommonHeaders.USER_AGENT, Constants.USER_AGENT_VALUE); - request = builder.build(); - } - } else { - wasRecentlyRedirected = false; - } - long start = System.currentTimeMillis(); - - call = httpClient.newCall(request); - try { - semaphore.acquire(); - response = call.execute(); - } catch (IOException e) { - if (e instanceof UnrecoverableIOException) { - if (lastException != null) { - throw lastException; - } else { - throw e; - } - } - lastException = e; - if (retryRequest(e, ++internalErrorCount, retryMaxCount, request, call)) { - long delayMs = 50L * (int) Math.pow(2, internalErrorCount); - Thread.sleep(delayMs); - continue; - } - - if ((e instanceof ConnectException) || (e instanceof InterruptedIOException)) { - ServiceException se = new ServiceException("Request error. ", e); - se.setResponseCode(408); - se.setErrorCode("RequestTimeOut"); - se.setErrorMessage(e.getMessage()); - se.setResponseStatus("Request error. "); - throw se; - } - throw e; - }finally { - semaphore.release(); - if (log.isInfoEnabled()) { - log.info("OkHttp cost " + (System.currentTimeMillis() - start) + " ms to apply http request"); - } - } - - responseCode = response.code(); - reqBean.setRespParams("[responseCode: " + responseCode + "][request-id: " - + response.header(this.getIHeaders().requestIdHeader(), "") + "]"); - - String contentType = response.header(CommonHeaders.CONTENT_TYPE); - if(log.isDebugEnabled()) { - log.debug("Response for '" + request.method() + "'. Content-Type: " + contentType + ", ResponseCode:" + responseCode + ", Headers: " - + response.headers()); - } - if (log.isTraceEnabled()) { - if (response.body() != null) { - log.trace("Entity length: " + response.body().contentLength()); - } - } - - if(isOEF && Mimetypes.MIMETYPE_JSON.equalsIgnoreCase(contentType)) { - if((responseCode >= 400 && responseCode < 500)) { - String xmlMessage = null; - try { - if( response.body() != null) { - xmlMessage = response.body().string(); - } - }catch (IOException e) { - } - - OefExceptionMessage oefException = (OefExceptionMessage) JSONChange.jsonToObj(new OefExceptionMessage(), ServiceUtils.toValid(xmlMessage)); - ServiceException exception = new ServiceException("Request Error." + ServiceUtils.toValid(xmlMessage)); - exception.setErrorMessage(oefException.getMessage()); - exception.setErrorCode(oefException.getCode()); - exception.setErrorRequestId(oefException.getRequest_id()); - - throw exception; - }else if (responseCode >= 500) { - reqBean.setResponseInfo("Internal Server error(s).", String.valueOf(responseCode)); - if (log.isErrorEnabled()) { - log.error(reqBean); - } - String xmlMessage = null; - try { - if( response.body() != null) { - xmlMessage = response.body().string(); - } - }catch (IOException e) { - } - ServiceException exception = new ServiceException( - "Encountered too many 5xx errors (" + internalErrorCount + "), aborting request.", xmlMessage); - throw exception; - } - else { - completedWithoutRecoverableError = true; - } - }else { - if (responseCode >= 300 && responseCode < 400 && responseCode != 304) { - String location = response.header(CommonHeaders.LOCATION); - if (!ServiceUtils.isValid(location)) { - ServiceException exception = new ServiceException("Try to redirect, but location is null!"); - reqBean.setResponseInfo("Request Error:" + exception.getMessage(), - "|" + responseCode + "|" + response.message() + "|"); - throw exception; - } - - if (location.indexOf("?") < 0) { - location = addRequestParametersToUrlPath(location, requestParameters, isOEF); - } - - if (doSignature && isLocationHostOnly(location)) { - request = authorizeHttpRequest(request, bucketName, location); - }else { - Request.Builder builder = request.newBuilder(); - - if(responseCode == 302 && HttpMethodEnum.GET.getOperationType().equalsIgnoreCase(request.method())) { - Headers headers = request.headers().newBuilder().removeAll(CommonHeaders.AUTHORIZATION).build(); - builder.headers(headers); - } - - this.setHost(builder, request, location); - - request = builder.build(); - } - - internalErrorCount++; - wasRecentlyRedirected = true; - if (internalErrorCount > retryMaxCount) { - String xmlMessage = null; - try { - if(response.body() != null) { - xmlMessage = response.body().string(); - } - }catch (IOException e) { - } - throw new ServiceException("Exceeded 3xx redirect limit (" + retryMaxCount + ").", xmlMessage); - } - ServiceUtils.closeStream(response); - }else if((responseCode >= 400 && responseCode < 500) || responseCode == 304) { - String xmlMessage = null; - try { - if( response.body() != null) { - xmlMessage = response.body().string(); - } - }catch (IOException e) { - } - ServiceException exception = new ServiceException("Request Error.", xmlMessage); - if (REQUEST_TIMEOUT_CODE.equals(exception.getErrorCode())) { - internalErrorCount++; - if (internalErrorCount < retryMaxCount) { - if (log.isWarnEnabled()) { - log.warn("Retrying connection that failed with RequestTimeout error" - + ", attempt number " + internalErrorCount + " of " + retryMaxCount); - } - continue; - } - if (log.isErrorEnabled()) { - log.error("Exceeded maximum number of retries for RequestTimeout errors: " - + retryMaxCount); - } - } - throw exception; - }else if (responseCode >= 500) { - reqBean.setResponseInfo("Internal Server error(s).", String.valueOf(responseCode)); - if (log.isErrorEnabled()) { - log.error(reqBean); - } - internalErrorCount++; - sleepOnInternalError(internalErrorCount, retryMaxCount, response, reqBean); - } - else { - completedWithoutRecoverableError = true; - } - } - - } while (!completedWithoutRecoverableError); - } catch (Throwable t) { - ServiceException serviceException = this.handleThrowable(request, response, reqBean, call, t); - throw serviceException; - } - - if (log.isInfoEnabled()) { - reqBean.setRespTime(new Date()); - reqBean.setResultCode(Constants.RESULTCODE_SUCCESS); - log.info(reqBean); - } - return response; - } - - protected String getRestMetadataPrefix() { - return this.getIHeaders().headerMetaPrefix(); - } - - protected String getRestHeaderPrefix() { - return this.getIHeaders().headerPrefix(); - } - - private boolean isProviderCredentialsInValid(ProviderCredentials providerCredentials) { - return providerCredentials == null || providerCredentials.getObsCredentialsProvider().getSecurityKey() == null || - !ServiceUtils.isValid(providerCredentials.getSecurityKey().getAccessKey()) || !ServiceUtils.isValid(providerCredentials.getSecurityKey().getSecretKey()) ; - } - - private URI setHost(Request.Builder builder, Request request, String url) { - URI uri; - if (url == null) { - uri = request.url().uri(); - } else { - uri = URI.create(url); - builder.url(url); - } - - String portStr; - if (getHttpsOnly()) { - int securePort = this.getHttpsPort(); - portStr = securePort == 443 ? "" : ":" + securePort; - } else { - int insecurePort = this.getHttpPort(); - portStr = insecurePort == 80 ? "" : ":" + insecurePort; - } - - builder.header(CommonHeaders.HOST, uri.getHost() + portStr); - return uri; - } - - protected Request authorizeHttpRequest(Request request, String bucketName, String url) - throws ServiceException { - - Headers headers = request.headers().newBuilder().removeAll(CommonHeaders.AUTHORIZATION).build(); - Request.Builder builder = request.newBuilder(); - builder.headers(headers); - - URI uri = this.setHost(builder, request, url); - String hostname = uri.getHost(); - - ProviderCredentials providerCredentials = ProviderCredentialThreadContext.getInstance() - .getProviderCredentials(); - if (isProviderCredentialsInValid(providerCredentials)) { - providerCredentials = this.getProviderCredentials(); - }else { - providerCredentials.setAuthType(this.getProviderCredentials().getAuthType()); - } - if (isProviderCredentialsInValid(providerCredentials)) { - if (log.isInfoEnabled()) { - log.info("Service has no Credential and is un-authenticated, skipping authorization"); - } - return request; - } - - String dateHeader = this.getIHeaders().dateHeader(); - String date = request.header(dateHeader); - Date now = null; - boolean isV4 = providerCredentials.getAuthType() == AuthTypeEnum.V4; - if (date != null) { - try { - now = isV4 ? ServiceUtils.getLongDateFormat().parse(date) : ServiceUtils.parseRfc822Date(date); - } catch (ParseException e) { - throw new ServiceException(dateHeader + " is not well-format", e); - } - }else { - now = new Date(); - } - builder.header(CommonHeaders.DATE, ServiceUtils.formatRfc822Date(now)); - - BasicSecurityKey securityKey = providerCredentials.getSecurityKey(); - String securityToken = securityKey.getSecurityToken(); - if (ServiceUtils.isValid(securityToken)) { - builder.header(this.getIHeaders().securityTokenHeader(), securityToken); - } - - String fullUrl = uri.getRawPath(); - String endpoint = this.getEndpoint(); - - if ((!this.isPathStyle() || isCname()) && hostname != null && !isV4) { - if (isCname()) { - fullUrl = "/" + hostname + fullUrl; - } else if (ServiceUtils.isValid(bucketName) && !endpoint.equals(hostname) && hostname.indexOf(bucketName) >=0) { +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +public abstract class RestStorageService extends RestConnectionService { + private static final ILogger log = LoggerBuilder.getLogger(RestStorageService.class); + + private static final Set> NON_RETRIABLE_CLASSES = + new HashSet<>(); + + private static final String REQUEST_TIMEOUT_CODE = "RequestTimeout"; + + // for example:Caused by: java.io.IOException: unexpected end of stream on + // Connection{...} + private static final String UNEXPECTED_END_OF_STREAM_EXCEPTION = "unexpected end of stream"; + + static { + NON_RETRIABLE_CLASSES.add(UnknownHostException.class); + NON_RETRIABLE_CLASSES.add(SSLException.class); + NON_RETRIABLE_CLASSES.add(ConnectException.class); + } + + private static ThreadLocal> userHeaders = new ThreadLocal<>(); + + // switch of using standard http headers + protected static final ThreadLocal CAN_USE_STANDARD_HTTP_HEADERS = new ThreadLocal<>(); + + protected RestStorageService() { + + } + + /** + * set switch of using standard http headers + * + * @param canUseStandardHTTPHeadersMap A Boolean variable to control switch of using http standard + * headers + */ + public void setCanUseStandardHTTPHeaders(Boolean canUseStandardHTTPHeadersMap) { + CAN_USE_STANDARD_HTTP_HEADERS.set(canUseStandardHTTPHeadersMap); + } + + /** + * set user headers + * + * @param userHeadersMap A HashMap + */ + public void setUserHeaders(HashMap userHeadersMap) { + userHeaders.set(userHeadersMap); + } + + /** + * add user headers to Request + * + * @param builder Request in OKHttp3.0 + */ + private void addUserHeaderToRequest(String bucketName, Request.Builder builder, boolean needEncode) { + Map userHeaderMap = formatMetadataAndHeader(bucketName, userHeaders.get(), needEncode); + for (Map.Entry entry : userHeaderMap.entrySet()) { + builder.addHeader(entry.getKey(), entry.getValue()); + if (log.isDebugEnabled()) { + log.debug("Added request header to connection: " + entry.getKey() + "=" + entry.getValue()); + } + } + } + + private Map formatMetadataAndHeader(String bucketName, Map metadataAndHeader, + boolean needEncode) { + Map format = new HashMap<>(); + if (metadataAndHeader != null) { + for (Map.Entry entry : metadataAndHeader.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if (!ServiceUtils.isValid(key)) { + continue; + } + key = key.trim(); + if (!key.startsWith(this.getRestHeaderPrefix(bucketName)) + && !key.startsWith(Constants.OBS_HEADER_PREFIX) + && !Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES + .contains(key.toLowerCase(Locale.getDefault()))) { + key = this.getRestMetadataPrefix(bucketName) + key; + } + try { + if (key.startsWith(this.getRestMetadataPrefix(bucketName))) { + if (needEncode) { + key = RestUtils.uriEncode(key, true); + } + } + if (needEncode) { + format.put(key, RestUtils.uriEncode(value == null ? "" : value, true)); + } else { + format.put(key, value); + } + } catch (ServiceException e) { + if (log.isDebugEnabled()) { + log.debug("Ignore key:" + key); + } + } + } + } + + return format; + } + + + protected boolean retryRequest(IOException exception, RetryCounter retryCounter, Request request, + Call call) { + retryCounter.addErrorCount(); + + if (retryCounter.getErrorCount() > retryCounter.getRetryMaxCount()) { + return false; + } + if (NON_RETRIABLE_CLASSES.contains(exception.getClass())) { + return false; + } else { + for (final Class rejectException : NON_RETRIABLE_CLASSES) { + if (rejectException.isInstance(exception)) { + return false; + } + } + } + + return !call.isCanceled(); + } + + private boolean retryRequestForUnexpectedException(IOException exception, RetryCounter retryCounter, + Call call) { + retryCounter.addErrorCount(); + + if (null == exception || retryCounter.getErrorCount() > retryCounter.getRetryMaxCount()) { + return false; + } + + if (!exception.getMessage().contains(UNEXPECTED_END_OF_STREAM_EXCEPTION)) { + return false; + } + + return !call.isCanceled(); + } + + private ServiceException handleThrowable(String bucketName, Request request, Response response, Call call, + Throwable t, boolean needEncode) { + ServiceException serviceException = (t instanceof ServiceException) ? (ServiceException) t + : new ServiceException("Request Error: " + t, t); + serviceException.setRequestHost(request.header(CommonHeaders.HOST)); + serviceException.setRequestVerb(request.method()); + serviceException.setRequestPath(request.url().toString()); + + if (response != null) { + ServiceUtils.closeStream(response); + serviceException.setResponseCode(response.code()); + serviceException.setResponseStatus(response.message()); + serviceException.setResponseDate(response.header(CommonHeaders.DATE)); + serviceException.setErrorIndicator(response.header(CommonHeaders.X_RESERVED_INDICATOR)); + serviceException.setResponseHeaders(ServiceUtils.cleanRestMetadataMapV2( + convertHeadersToMap(response.headers()), getRestHeaderPrefix(bucketName), + getRestMetadataPrefix(bucketName), needEncode)); + if (!ServiceUtils.isValid(serviceException.getErrorRequestId())) { + serviceException.setRequestAndHostIds(response.header(getIHeaders(bucketName).requestIdHeader()), + response.header(getIHeaders(bucketName).requestId2Header())); + } + } + + if (log.isWarnEnabled()) { + log.warn("Request failed, Response code: " + serviceException.getResponseCode() + + "; Request ID: " + serviceException.getErrorRequestId() + + "; Request path: " + serviceException.getRequestPath()); + } + if (log.isDebugEnabled()) { + log.debug("Exception detail.", serviceException); + } + + if (call != null) { + call.cancel(); + } + return serviceException; + } + + private boolean isLocationHostOnly(String location) { + boolean isOnlyHost = false; + + URI uri; + uri = URI.create(location); + String path = uri.getPath(); + + if (!location.contains("?")) { + if (path == null || path.isEmpty() || path.equals("/")) { + isOnlyHost = true; + } + } + + return isOnlyHost; + } + + protected Response performRequest(Request request, Map requestParameters, String bucketName) + throws ServiceException { + return performRequest(request, requestParameters, bucketName, true); + } + + protected Response performRequestWithoutSignature(Request request, Map requestParameters, + String bucketName) throws ServiceException { + return performRequest(request, requestParameters, bucketName, false); + } + + protected Response performRequest(Request request, Map requestParameters, String bucketName, + boolean doSignature) throws ServiceException { + return this.performRequest(request, requestParameters, bucketName, doSignature, false); + } + + private static final class RetryCounter { + private int errorCount = 0; + private int retryMaxCount; + + public RetryCounter(int retryMaxCount) { + super(); + this.retryMaxCount = retryMaxCount; + } + + public int getErrorCount() { + return errorCount; + } + + public void addErrorCount() { + this.errorCount++; + } + + public int getRetryMaxCount() { + return retryMaxCount; + } + } + + private static final class RetryController { + private RetryCounter errorRetryCounter; + private RetryCounter unexpectedErrorRetryCounter; + private Exception lastException = null; + private boolean wasRecentlyRedirected; + + public RetryController(RetryCounter errorRetryCounter, RetryCounter unexpectedErrorRetryCounter, + boolean wasRecentlyRedirected) { + super(); + this.errorRetryCounter = errorRetryCounter; + this.unexpectedErrorRetryCounter = unexpectedErrorRetryCounter; + this.wasRecentlyRedirected = wasRecentlyRedirected; + } + + public Exception getLastException() { + return lastException; + } + + public void setLastException(Exception lastException) { + this.lastException = lastException; + } + + public RetryCounter getErrorRetryCounter() { + return errorRetryCounter; + } + + public RetryCounter getUnexpectedErrorRetryCounter() { + return unexpectedErrorRetryCounter; + } + + public boolean isWasRecentlyRedirected() { + return wasRecentlyRedirected; + } + + public void setWasRecentlyRedirected(boolean wasRecentlyRedirected) { + this.wasRecentlyRedirected = wasRecentlyRedirected; + } + } + + private static final class RequestInfo { + private Request request; + private Response response = null; + private Call call = null; + private InterfaceLogBean reqBean; + + public RequestInfo(Request request, InterfaceLogBean reqBean) { + super(); + this.request = request; + this.reqBean = reqBean; + } + + public Request getRequest() { + return request; + } + + public void setRequest(Request request) { + this.request = request; + } + + public Response getResponse() { + return response; + } + + public void setResponse(Response response) { + this.response = response; + } + + public Call getCall() { + return call; + } + + public void setCall(Call call) { + this.call = call; + } + + public InterfaceLogBean getReqBean() { + return reqBean; + } + } + + protected Response performRequest(Request request, Map requestParameters, String bucketName, + boolean doSignature, boolean isOEF) throws ServiceException { + return performRequest(request, requestParameters, bucketName, doSignature, isOEF, true); + } + + protected Response performRequest(Request request, Map requestParameters, String bucketName, + boolean doSignature, boolean isOEF, boolean needEncode) throws ServiceException { + RequestInfo requestInfo = new RequestInfo(request, new InterfaceLogBean("performRequest", "", "")); + try { + tryRequest(requestParameters, bucketName, doSignature, isOEF, requestInfo, needEncode); + } catch (Throwable t) { + throw this.handleThrowable(bucketName, requestInfo.getRequest(), + requestInfo.getResponse(), requestInfo.getCall(), t, needEncode); + } + + if (log.isInfoEnabled()) { + requestInfo.getReqBean().setRespTime(new Date()); + requestInfo.getReqBean().setResultCode(Constants.RESULTCODE_SUCCESS); + log.info(requestInfo.getReqBean()); + } + return requestInfo.getResponse(); + } + + protected Response performRequest(NewTransResult result) { + return performRequest(result, true, true, false); + } + + // todo 重构后仅保留这一个 performRequest,将其他的均合并至这里 + // todo 重构时需要评估下 needSignature, autoRelease, isOEF 这三个参数是否要合入到 transResult 里 + protected Response performRequest(NewTransResult result, boolean needSignature, + boolean autoRelease, boolean isOEF) { + Request.Builder builder = setupConnection(result.getHttpMethod(), result.getBucketName(), + result.getObjectKey(), result.getParams(), result.getBody(), isOEF); + renameMetadataKeys(result.getBucketName(), builder, result.getHeaders(), result.isEncodeHeaders()); + if (result.getUserHeaders() != null) { + result.getUserHeaders().forEach(builder::addHeader); + } + Request request = builder.build(); + RequestInfo requestInfo = new RequestInfo(request, new InterfaceLogBean("performRequest", "", "")); + try { + tryRequest(result.getParams(), result.getBucketName(), needSignature, + isOEF, requestInfo, result.isEncodeHeaders()); + } catch (Throwable t) { + throw this.handleThrowable(result.getBucketName(), requestInfo.getRequest(), + requestInfo.getResponse(), requestInfo.getCall(), t, result.isEncodeHeaders()); + } + + if (log.isInfoEnabled()) { + requestInfo.getReqBean().setRespTime(new Date()); + requestInfo.getReqBean().setResultCode(Constants.RESULTCODE_SUCCESS); + log.info(requestInfo.getReqBean()); + } + Response response = requestInfo.getResponse(); + if (autoRelease) { + response.close(); + } + return response; + } + + private void tryRequest(Map requestParameters, String bucketName, boolean doSignature, + boolean isOEF, RequestInfo requestInfo, boolean needEncode) throws Exception { + requestInfo.setRequest(initRequest(bucketName, requestInfo.getRequest(), needEncode)); + + log.debug("Performing " + requestInfo.getRequest().method() + + " request for '" + requestInfo.getRequest().url()); + log.debug("Headers: " + requestInfo.getRequest().headers()); + + RetryController retryController = new RetryController(new RetryCounter( + obsProperties.getIntProperty(ObsConstraint.HTTP_RETRY_MAX, + ObsConstraint.HTTP_RETRY_MAX_VALUE)), + new RetryCounter(obsProperties.getIntProperty( + ExtObsConstraint.HTTP_MAX_RETRY_ON_UNEXPECTED_END_EXCEPTION, + ExtObsConstraint.DEFAULT_MAX_RETRY_ON_UNEXPECTED_END_EXCEPTION)), + false); + + do { + if (!retryController.isWasRecentlyRedirected()) { + requestInfo.setRequest(addBaseHeaders(requestInfo.getRequest(), bucketName, doSignature)); + } else { + retryController.setWasRecentlyRedirected(false); + } + + requestInfo.setCall(httpClient.newCall(requestInfo.getRequest())); + requestInfo.setResponse(executeRequest(requestInfo.getCall(), + requestInfo.getRequest(), retryController)); + if (null == requestInfo.getResponse()) { + continue; + } + + int responseCode = requestInfo.getResponse().code(); + requestInfo.getReqBean().setRespParams("[responseCode: " + responseCode + "][request-id: " + + requestInfo.getResponse().header(this.getIHeaders(bucketName).requestIdHeader(), "") + "]"); + + String contentType = requestInfo.getResponse().header(CommonHeaders.CONTENT_TYPE); + if (log.isDebugEnabled()) { + log.debug("Response for '" + requestInfo.getRequest().method() + "'. Content-Type: " + contentType + + ", ResponseCode:" + responseCode + + ", Headers: " + requestInfo.getResponse().headers()); + } + if (log.isTraceEnabled()) { + if (requestInfo.getResponse().body() != null) { + log.trace("Entity length: " + requestInfo.getResponse().body().contentLength()); + } + } + + if (isOEF && Mimetypes.MIMETYPE_JSON.equalsIgnoreCase(contentType)) { + transOEFResponse(requestInfo.getResponse(), requestInfo.getReqBean(), + retryController.getErrorRetryCounter().getErrorCount(), responseCode); + break; + } else { + if (responseCode >= 300 && responseCode < 400 && responseCode != 304) { + requestInfo.setRequest(handleRedirectResponse(requestInfo.getRequest(), + requestParameters, bucketName, doSignature, isOEF, + requestInfo.getReqBean(), requestInfo.getResponse(), retryController)); + } else if ((responseCode >= 400 && responseCode < 500) || responseCode == 304) { + handleRequestErrorResponse(requestInfo.getResponse(), retryController); + } else if (responseCode >= 500) { + handleServerErrorResponse(requestInfo.getReqBean(), + requestInfo.getResponse(), retryController, responseCode); + } else { + break; + } + } + + } while (true); + } + + private void handleRequestErrorResponse(Response response, RetryController retryController) { + ServiceException exception = createServiceException("Request Error.", response); + + if (!REQUEST_TIMEOUT_CODE.equals(exception.getErrorCode())) { + throw exception; + } + + retryController.getErrorRetryCounter().addErrorCount(); + if (retryController.getErrorRetryCounter().getErrorCount() + < retryController.getErrorRetryCounter().getRetryMaxCount()) { + if (log.isWarnEnabled()) { + log.warn("Retrying connection that failed with RequestTimeout error" + + ", attempt number " + retryController.getErrorRetryCounter().getErrorCount() + + " of " + retryController.getErrorRetryCounter().getRetryMaxCount()); + } + } else { + if (log.isErrorEnabled()) { + log.error("Exceeded maximum number of retries for RequestTimeout errors: " + + retryController.getErrorRetryCounter().getRetryMaxCount()); + } + + throw exception; + } + } + + private void handleServerErrorResponse(InterfaceLogBean reqBean, Response response, RetryController retryController, + int responseCode) { + reqBean.setResponseInfo("Internal Server error(s).", String.valueOf(responseCode)); + if (log.isErrorEnabled()) { + log.error(reqBean); + } + + doRetry(response, + "Encountered too many 5xx errors (" + + retryController.getErrorRetryCounter().getErrorCount() + + "), aborting request.", + retryController.getErrorRetryCounter()); + + sleepBeforeRetry(retryController.getErrorRetryCounter().getErrorCount()); + } + + private Request handleRedirectResponse(Request request, Map requestParameters, String bucketName, + boolean doSignature, boolean isOEF, InterfaceLogBean reqBean, + Response response, RetryController retryController) { + int responseCode = response.code(); + String location = response.header(CommonHeaders.LOCATION); + if (!ServiceUtils.isValid(location)) { + ServiceException exception = new ServiceException("Try to redirect, but location is null!"); + reqBean.setResponseInfo("Request Error:" + exception.getMessage(), + "|" + responseCode + "|" + response.message() + "|"); + throw exception; + } + + request = createRedirectRequest(request, requestParameters, bucketName, doSignature, isOEF, + responseCode, location); + + retryController.setWasRecentlyRedirected(true); + + doRetry(response, + "Exceeded 3xx redirect limit (" + + retryController.getErrorRetryCounter().getRetryMaxCount() + + ").", + retryController.getErrorRetryCounter()); + return request; + } + + private Response executeRequest(Call call, + Request request, + RetryController retryController) throws Exception { + long start = System.currentTimeMillis(); + + try { + semaphore.acquire(); + return call.execute(); + } catch (IOException e) { + if (e instanceof UnrecoverableIOException) { + if (retryController.getLastException() != null) { + throw retryController.getLastException(); + } else { + throw e; + } + } + retryController.setLastException(e); + + retryOnIOException(e, + request, + retryController, + call); + + if (log.isWarnEnabled()) { + log.warn("Retrying connection that failed with error" + + ", attempt number " + retryController.getErrorRetryCounter().getErrorCount() + + " of " + retryController.getErrorRetryCounter().getRetryMaxCount()); + } + return null; + } finally { + semaphore.release(); + if (log.isInfoEnabled()) { + log.info("OkHttp cost " + (System.currentTimeMillis() - start) + " ms to apply http request"); + } + } + } + + private void retryOnIOException(IOException e, + Request request, + RetryController retryController, + Call call) throws Exception { + + // for example:Caused by: java.io.IOException: unexpected + // end of stream on Connection{...} + if (retryRequestForUnexpectedException(e, retryController.getUnexpectedErrorRetryCounter(), call)) { + if (log.isErrorEnabled()) { + log.error("unexpected end of stream excepiton."); + } + return; + } + + if (retryRequest(e, retryController.getErrorRetryCounter(), request, call)) { + sleepBeforeRetry(retryController.getErrorRetryCounter().getErrorCount()); + return; + } + + if ((e instanceof ConnectException) || (e instanceof InterruptedIOException)) { + ServiceException se = new ServiceException("Request error. ", e); + se.setResponseCode(408); + se.setErrorCode("RequestTimeOut"); + se.setErrorMessage(e.getMessage()); + se.setResponseStatus("Request error. "); + throw se; + } + throw e; + } + + private void doRetry(Response response, String message, RetryCounter retryCounter) { + retryCounter.addErrorCount(); + if (retryCounter.getErrorCount() > retryCounter.getRetryMaxCount()) { + throw createServiceException(message, response); + } + ServiceUtils.closeStream(response); + } + + private ServiceException createServiceException(String message, Response response) { + String xmlMessage = readResponseMessage(response); + return new ServiceException(message, xmlMessage); + } + + private String readResponseMessage(Response response) { + String xmlMessage = null; + try { + if (response.body() != null) { + xmlMessage = response.body().string(); + } + } catch (IOException e) { + log.warn("read response body failed.", e); + } + return xmlMessage; + } + + private Request createRedirectRequest(Request request, Map requestParameters, String bucketName, + boolean doSignature, boolean isOEF, int responseCode, String location) { + if (!location.contains("?")) { + location = addRequestParametersToUrlPath(location, requestParameters, isOEF); + } + + if (doSignature && isLocationHostOnly(location)) { + request = authorizeHttpRequest(request, bucketName, location); + } else { + Request.Builder builder = request.newBuilder(); + + if (responseCode == 302 + && HttpMethodEnum.GET.getOperationType().equalsIgnoreCase(request.method())) { + Headers headers = request.headers().newBuilder().removeAll(CommonHeaders.AUTHORIZATION) + .build(); + builder.headers(headers); + } + + this.setHost(builder, request, location); + + request = builder.build(); + } + return request; + } + + private void transOEFResponse(Response response, InterfaceLogBean reqBean, + int internalErrorCount, int responseCode) { + if ((responseCode >= 400 && responseCode < 500)) { + String xmlMessage = readResponseMessage(response); + + OefExceptionMessage oefException = (OefExceptionMessage) JSONChange + .jsonToObj(new OefExceptionMessage(), ServiceUtils.toValid(xmlMessage)); + ServiceException exception = new ServiceException( + "Request Error." + ServiceUtils.toValid(xmlMessage)); + exception.setErrorMessage(oefException.getMessage()); + exception.setErrorCode(oefException.getCode()); + exception.setErrorRequestId(oefException.getRequestId()); + + throw exception; + } else if (responseCode >= 500) { + reqBean.setResponseInfo("Internal Server error(s).", String.valueOf(responseCode)); + if (log.isErrorEnabled()) { + log.error(reqBean); + } + throw createServiceException("Encountered too many 5xx errors (" + internalErrorCount + + "), aborting request.", + response); + } + } + + private Request addBaseHeaders(Request request, String bucketName, boolean doSignature) { + if (doSignature) { + request = authorizeHttpRequest(request, bucketName, null); + } else { + Request.Builder builder = request.newBuilder(); + builder.headers(request.headers().newBuilder().removeAll(CommonHeaders.AUTHORIZATION).build()); + this.setHost(builder, request, null); + builder.header(CommonHeaders.USER_AGENT, Constants.USER_AGENT_VALUE); + request = builder.build(); + } + log.debug("Request Headers: " + request.headers().toString().replace("\n", "|")); + return request; + } + + private Request initRequest(String bucketName, Request request, boolean needEncode) { + if (userHeaders.get() != null && userHeaders.get().size() > 0) { + // create a new Builder from current Request + Request.Builder builderTmp = request.newBuilder(); + // add user header + addUserHeaderToRequest(bucketName, builderTmp, needEncode); + // build new request + request = builderTmp.build(); + } + return request; + } + + protected String getRestMetadataPrefix(String bucketName) { + return this.getIHeaders(bucketName).headerMetaPrefix(); + } + + protected String getRestHeaderPrefix(String bucketName) { + return this.getIHeaders(bucketName).headerPrefix(); + } + + private boolean isProviderCredentialsInValid(ProviderCredentials providerCredentials) { + return providerCredentials == null || providerCredentials.getObsCredentialsProvider().getSecurityKey() == null + || !ServiceUtils.isValid(providerCredentials.getSecurityKey().getAccessKey()) + || !ServiceUtils.isValid(providerCredentials.getSecurityKey().getSecretKey()); + } + + private URI setHost(Request.Builder builder, Request request, String url) { + URI uri; + if (url == null) { + uri = request.url().uri(); + } else { + uri = URI.create(url); + builder.url(url); + } + + String portStr; + if (getHttpsOnly()) { + int securePort = this.getHttpsPort(); + portStr = securePort == 443 ? "" : ":" + securePort; + } else { + int insecurePort = this.getHttpPort(); + portStr = insecurePort == 80 ? "" : ":" + insecurePort; + } + + builder.header(CommonHeaders.HOST, uri.getHost() + portStr); + return uri; + } + + private Date parseDate(String bucketName, Request request, boolean isV4) { + String dateHeader = this.getIHeaders(bucketName).dateHeader(); + String date = request.header(dateHeader); + + if (date != null) { + try { + return isV4 ? ServiceUtils.getLongDateFormat().parse(date) : ServiceUtils.parseRfc822Date(date); + } catch (ParseException e) { + throw new ServiceException(dateHeader + " is not well-format", e); + } + } else { + return new Date(); + } + } + + protected Request authorizeHttpRequest(Request request, String bucketName, String url) throws ServiceException { + + Headers headers = request.headers().newBuilder().removeAll(CommonHeaders.AUTHORIZATION).build(); + Request.Builder builder = request.newBuilder(); + builder.headers(headers); + + URI uri = this.setHost(builder, request, url); + String hostname = uri.getHost(); + + ProviderCredentials providerCredentials = ProviderCredentialThreadContext.getInstance() + .getProviderCredentials(); + if (isProviderCredentialsInValid(providerCredentials)) { + providerCredentials = this.getProviderCredentials(); + } else { + providerCredentials.setAuthType(this.getProviderCredentials().getLocalAuthType(bucketName)); + } + if (isProviderCredentialsInValid(providerCredentials)) { + if (log.isInfoEnabled()) { + log.info("Service has no Credential and is un-authenticated, skipping authorization"); + } + return request; + } + + boolean isV4 = providerCredentials.getLocalAuthType(bucketName) == AuthTypeEnum.V4; + + Date now = parseDate(bucketName, request, isV4); + + builder.header(CommonHeaders.DATE, ServiceUtils.formatRfc822Date(now)); + + BasicSecurityKey securityKey = providerCredentials.getSecurityKey(); + String securityToken = securityKey.getSecurityToken(); + if (ServiceUtils.isValid(securityToken)) { + builder.header(this.getIHeaders(bucketName).securityTokenHeader(), securityToken); + } + + String fullUrl = uri.getRawPath(); + String endpoint = this.getEndpoint(); + + if ((!this.isPathStyle() || isCname()) && hostname != null && !isV4) { + if (isCname()) { + fullUrl = "/" + hostname + fullUrl; + } else if (ServiceUtils.isValid(bucketName) && !endpoint.equals(hostname) + && hostname.contains(bucketName)) { fullUrl = "/" + bucketName + fullUrl; - } - } - - String queryString = uri.getRawQuery(); - if (queryString != null && queryString.length() > 0) { - fullUrl += "?" + queryString; - } - - if (log.isDebugEnabled()) { - log.debug("For creating canonical string, using uri: " + fullUrl); - } - - IAuthentication iauthentication; - if (isV4) { - builder.header(this.getIHeaders().contentSha256Header(), V4Authentication.content_sha256); - iauthentication = V4Authentication.makeServiceCanonicalString(request.method(), - convertHeadersToMap(builder.build().headers()), fullUrl, providerCredentials, now, securityKey); - if (log.isDebugEnabled()) { - log.debug("CanonicalRequest:" + iauthentication.getCanonicalRequest()); - } - } else { - iauthentication = Constants.AUTHTICATION_MAP.get(providerCredentials.getAuthType()).makeAuthorizationString( - request.method(), convertHeadersToMap(builder.build().headers()), fullUrl, - Constants.ALLOWED_RESOURCE_PARAMTER_NAMES, securityKey); - } - if (log.isDebugEnabled()) { - log.debug("StringToSign ('|' is a newline): " + iauthentication.getStringToSign().replace('\n', '|')); - } - - String authorizationString = iauthentication.getAuthorization(); - builder.header(CommonHeaders.AUTHORIZATION, authorizationString); - builder.header(CommonHeaders.USER_AGENT, Constants.USER_AGENT_VALUE); - return builder.build(); - } - - protected Response performRestHead(String bucketName, String objectKey, Map requestParameters, - Map requestHeaders) throws ServiceException { - - Request.Builder builder = setupConnection(HttpMethodEnum.HEAD, bucketName, objectKey, requestParameters, null); - - addRequestHeadersToConnection(builder, requestHeaders); - - return performRequest(builder.build(), requestParameters, bucketName); - } - - protected Response performRestGet(String bucketName, String objectKey, Map requestParameters, - Map requestHeaders) throws ServiceException { - return this.performRestGet(bucketName, objectKey, requestParameters, requestHeaders, false); - } - - protected Response performRestGetForListBuckets(String bucketName, String objectKey, Map requestParameters, - Map requestHeaders) throws ServiceException { - - //no bucket name required for listBuckets - Request.Builder builder = setupConnection(HttpMethodEnum.GET, bucketName, objectKey, requestParameters, null, false, true); - - addRequestHeadersToConnection(builder, requestHeaders); - return performRequest(builder.build(), requestParameters, bucketName, true, false); - } - - protected Response performRestGet(String bucketName, String objectKey, Map requestParameters, - Map requestHeaders, boolean isOEF) throws ServiceException { - - Request.Builder builder = setupConnection(HttpMethodEnum.GET, bucketName, objectKey, requestParameters, null, isOEF); - - addRequestHeadersToConnection(builder, requestHeaders); - return performRequest(builder.build(), requestParameters, bucketName, true, isOEF); - } - - protected Response performRestPut(String bucketName, String objectKey, Map metadata, - Map requestParameters, RequestBody body, boolean autoRelease) throws ServiceException { - return this.performRestPut(bucketName, objectKey, metadata, requestParameters, body, autoRelease, false); - } - - protected Response performRestPut(String bucketName, String objectKey, Map metadata, - Map requestParameters, RequestBody body, boolean autoRelease, boolean isOEF) throws ServiceException { - Request.Builder builder = setupConnection(HttpMethodEnum.PUT, bucketName, objectKey, requestParameters, body, isOEF); - - renameMetadataKeys(builder, metadata); - - Response result = performRequest(builder.build(), requestParameters, bucketName, true, isOEF); - - if (autoRelease) { - result.close(); - } - - return result; - } - - protected Response performRestPost(String bucketName, String objectKey, Map metadata, - Map requestParameters, RequestBody body, boolean autoRelease) throws ServiceException { - return this.performRestPost(bucketName, objectKey, metadata, requestParameters, body, autoRelease, false); - } - - protected Response performRestPost(String bucketName, String objectKey, Map metadata, - Map requestParameters, RequestBody body, boolean autoRelease, boolean isOEF) throws ServiceException { - Request.Builder builder = setupConnection(HttpMethodEnum.POST, bucketName, objectKey, requestParameters, body, isOEF); - - renameMetadataKeys(builder, metadata); - - Response result = performRequest(builder.build(), requestParameters, bucketName, true, isOEF); - - if (autoRelease) { - result.close(); - } - - return result; - } - - protected Response performRestDelete(String bucketName, String objectKey, Map requestParameters, Map metadata) - throws ServiceException { - return this.performRestDelete(bucketName, objectKey, requestParameters, metadata, false); - } - - protected Response performRestDelete(String bucketName, String objectKey, Map requestParameters, - Map metadata, boolean isOEF) - throws ServiceException { - - Request.Builder builder = setupConnection(HttpMethodEnum.DELETE, bucketName, objectKey, requestParameters, - null, isOEF); - - renameMetadataKeys(builder, metadata); - - Response result = performRequest(builder.build(), requestParameters, bucketName, true, isOEF); - - result.close(); - - return result; - } - - protected Response performRestDelete(String bucketName, String objectKey, Map requestParameters) throws ServiceException { - return this.performRestDelete(bucketName, objectKey, requestParameters, true); - } - - protected Response performRestDelete(String bucketName, String objectKey, Map requestParameters, boolean autoRelease) - throws ServiceException { - - Request.Builder builder = setupConnection(HttpMethodEnum.DELETE, bucketName, objectKey, requestParameters, - null); - - Response result = performRequest(builder.build(), requestParameters, bucketName); - - if(autoRelease) { - result.close(); - } - - return result; - } - - protected Response performRestOptions(String bucketName, String objectKey, Map metadata, - Map requestParameters, boolean autoRelease) throws ServiceException { - - Request.Builder builder = setupConnection(HttpMethodEnum.OPTIONS, bucketName, objectKey, requestParameters, - null); - - addRequestHeadersToConnection(builder, metadata); - - Response result = performRequest(builder.build(), requestParameters, bucketName); - - if (autoRelease) { - result.close(); - } - return result; - } - - protected Response performRestForApiVersion(String bucketName, String objectKey, Map requestParameters, - Map requestHeaders) throws ServiceException { - - Request.Builder builder = setupConnection(HttpMethodEnum.HEAD, bucketName, objectKey, requestParameters, null, false, true); - - addRequestHeadersToConnection(builder, requestHeaders); - - return performRequestWithoutSignature(builder.build(), requestParameters, bucketName); - } - - protected void shutdown() { - this.shutdownImpl(); - } - - protected void sleepOnInternalError(int internalErrorCount, int retryMaxCount, Response response, - InterfaceLogBean reqBean) throws ServiceException { - String xmlMessage = null; - if (internalErrorCount <= retryMaxCount) { - ServiceUtils.closeStream(response); - long delayMs = 50L * (int) Math.pow(2, internalErrorCount); - if (log.isWarnEnabled()) { - log.warn("Encountered " + internalErrorCount + " Internal Server error(s), will retry in " + delayMs - + "ms"); - } - try { - Thread.sleep(delayMs); - } catch (InterruptedException e) { - } - } else { - try { - xmlMessage = response.body().string(); - } catch (IOException e) { - } - ServiceException exception = new ServiceException( - "Encountered too many 5xx errors (" + internalErrorCount + "), aborting request.", xmlMessage); - throw exception; - } - } - - protected Map convertHeadersToMap(Headers headers) { - Map map = new IdentityHashMap(); - for (Map.Entry> entry : headers.toMultimap().entrySet()) { - List values = entry.getValue(); - for (String value : values) { - map.put(new String(entry.getKey()), value); - } - } - return map; - } - - protected ProviderCredentials getProviderCredentials() { - return credentials; - } - - protected void setProviderCredentials(ProviderCredentials credentials) { - this.credentials = credentials; - } - - protected void renameMetadataKeys(Request.Builder builder, Map metadata) { - Map convertedMetadata = new HashMap(); - if (metadata != null) { - for (Map.Entry entry : metadata.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - if (!ServiceUtils.isValid(key)) { - continue; - } - key = key.trim(); - if (!key.startsWith(this.getRestHeaderPrefix()) && !key.startsWith(Constants.OBS_HEADER_PREFIX) - && !Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES - .contains(key.toLowerCase(Locale.getDefault()))) { - key = this.getRestMetadataPrefix() + key; - } - try { - if(key.startsWith(this.getRestMetadataPrefix())) { - key = RestUtils.uriEncode(key, true); - } - convertedMetadata.put(key, RestUtils.uriEncode(value == null ? "" : value, true)); - } catch (ServiceException e) { - if (log.isDebugEnabled()) { - log.debug("Ignore metadata key:" + key); - } - } - } - } - for (Map.Entry entry : convertedMetadata.entrySet()) { - builder.addHeader(entry.getKey(), entry.getValue()); - if (log.isDebugEnabled()) { - log.debug("Added request header to connection: " + entry.getKey() + "=" + entry.getValue()); - } - } - } - - protected Request.Builder setupConnection(HttpMethodEnum method, String bucketName, String objectKey, - Map requestParameters, RequestBody body) throws ServiceException{ - return this.setupConnection(method, bucketName, objectKey, requestParameters, body, false); - } - - protected Request.Builder setupConnection(HttpMethodEnum method, String bucketName, String objectKey, - Map requestParameters, RequestBody body, boolean isOEF) throws ServiceException { - return this.setupConnection(method, bucketName, objectKey, requestParameters, body, isOEF, false); - } - - protected Request.Builder setupConnection(HttpMethodEnum method, String bucketName, String objectKey, - Map requestParameters, RequestBody body, boolean isOEF, boolean isListBuckets) throws ServiceException { - - boolean pathStyle = this.isPathStyle(); - String endPoint = this.getEndpoint(); - boolean isCname = this.isCname(); - String hostname = (isCname || isListBuckets) ? endPoint : ServiceUtils.generateHostnameForBucket(RestUtils.encodeUrlString(bucketName), - pathStyle, endPoint); - String resourceString = "/"; - if (hostname.equals(endPoint) && !isCname && bucketName.length() > 0 ) - { - resourceString += RestUtils.encodeUrlString(bucketName); - } - if (objectKey != null) { - resourceString += ((pathStyle && !isCname) ? "/" : "") + RestUtils.encodeUrlString(objectKey); - } - - String url = null; - if (getHttpsOnly()) { - int securePort = this.getHttpsPort(); - String securePortStr = securePort == 443 ? "" : ":" + securePort; - url = "https://" + hostname + securePortStr + resourceString; - } else { - int insecurePort = this.getHttpPort(); - String insecurePortStr = insecurePort == 80 ? "" : ":" + insecurePort; - url = "http://" + hostname + insecurePortStr + resourceString; - } - if (log.isDebugEnabled()) { - log.debug("OBS URL: " + url); - } - url = addRequestParametersToUrlPath(url, requestParameters, isOEF); - - Request.Builder builder = new Request.Builder(); - builder.url(url); - if (body == null) { - body = RequestBody.create(null, ""); - } - switch (method) { - case PUT: - builder.put(body); - break; - case POST: - builder.post(body); - break; - case HEAD: - builder.head(); - break; - case GET: - builder.get(); - break; - case DELETE: - builder.delete(body); - break; - case OPTIONS: - builder.method("OPTIONS", null); - break; - default: - throw new IllegalArgumentException("Unrecognised HTTP method name: " + method); - } - - if(!this.isKeepAlive()) { - builder.addHeader("Connection", "Close"); - } - - return builder; - } - - protected String addRequestParametersToUrlPath(String urlPath, Map requestParameters) - { - return this.addRequestParametersToUrlPath(urlPath, requestParameters, false); - } - - protected String addRequestParametersToUrlPath(String urlPath, Map requestParameters, boolean isOEF) - throws ServiceException { - if (requestParameters != null) { - for (Map.Entry entry : requestParameters.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - if(isOEF) { - if(isPathStyle()) { - urlPath += "/" + key; - }else { - urlPath += key; - } - }else { - urlPath += (urlPath.indexOf("?") < 0 ? "?" : "&") + RestUtils.encodeUrlString(key); - } - if (ServiceUtils.isValid(value)) { - urlPath += "=" + RestUtils.encodeUrlString(value); - if (log.isDebugEnabled()) { - log.debug("Added request parameter: " + key + "=" + value); - } - } else { - if (log.isDebugEnabled()) { - log.debug("Added request parameter without value: " + key); - } - } - } - } - return urlPath; - } - - protected XmlResponsesSaxParser getXmlResponseSaxParser() throws ServiceException { - return new XmlResponsesSaxParser(); - } - - protected void addRequestHeadersToConnection(Request.Builder builder, Map requestHeaders) { - if (requestHeaders != null) { - for (Map.Entry entry : requestHeaders.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - if (!ServiceUtils.isValid(key) || value == null) { - continue; - } - key = key.trim(); - - if (!Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(key.toLowerCase(Locale.getDefault())) - && !key.startsWith(this.getRestHeaderPrefix())) { - continue; - } - builder.addHeader(key, value); - if (log.isDebugEnabled()) { - log.debug("Added request header to connection: " + key + "=" + value); - } - } - } - } - - protected IHeaders getIHeaders() { - return Constants.HEADERS_MAP.get(this.getProviderCredentials().getAuthType()); - } - - protected IConvertor getIConvertor() { - return Constants.CONVERTOR_MAP.get(this.getProviderCredentials().getAuthType()); - } - - - protected boolean isKeepAlive() - { - return this.obsProperties.getBoolProperty(ObsConstraint.KEEP_ALIVE, true); + } + } + + String queryString = uri.getRawQuery(); + if (queryString != null && queryString.length() > 0) { + fullUrl += "?" + queryString; + } + + if (log.isDebugEnabled()) { + log.debug("For creating canonical string, using uri: " + fullUrl); + } + + IAuthentication iauthentication; + if (isV4) { + builder.header(this.getIHeaders(bucketName).contentSha256Header(), V4Authentication.CONTENT_SHA256); + iauthentication = V4Authentication.makeServiceCanonicalString(request.method(), + convertHeadersToMap(builder.build().headers()), fullUrl, providerCredentials, now, securityKey); + if (log.isDebugEnabled()) { + log.debug("CanonicalRequest:" + iauthentication.getCanonicalRequest()); + } + } else { + iauthentication = Constants.AUTHTICATION_MAP.get(providerCredentials.getLocalAuthType(bucketName)) + .makeAuthorizationString(request.method(), convertHeadersToMap(builder.build().headers()), fullUrl, + Constants.ALLOWED_RESOURCE_PARAMTER_NAMES, securityKey); + } + log.debug("StringToSign ('|' is a newline): " + + iauthentication.getStringToSign().replace('\n', '|')); + + String authorizationString = iauthentication.getAuthorization(); + builder.header(CommonHeaders.AUTHORIZATION, authorizationString); + builder.header(CommonHeaders.USER_AGENT, Constants.USER_AGENT_VALUE); + return builder.build(); + } + + protected Response performRestHead(String bucketName, String objectKey, Map requestParameters, + Map requestHeaders, Map userHeaders, + boolean needEncode) throws ServiceException { + Request.Builder builder = setupConnection(HttpMethodEnum.HEAD, bucketName, objectKey, requestParameters, null); + + addRequestHeadersToConnection(bucketName, builder, requestHeaders); + if (userHeaders != null) { + userHeaders.forEach(builder::addHeader); + } + return performRequest(builder.build(), requestParameters, bucketName, true, false, needEncode); + } + + protected Response performRestGet(String bucketName, String objectKey, Map requestParameters, + Map requestHeaders, Map userHeaders) + throws ServiceException { + return this.performRestGet(bucketName, objectKey, requestParameters, requestHeaders, userHeaders, false); + } + + protected Response performRestGetForListBuckets(String bucketName, String objectKey, + Map requestParameters, + Map requestHeaders) throws ServiceException { + + // no bucket name required for listBuckets + Request.Builder builder = setupConnection(HttpMethodEnum.GET, bucketName, objectKey, requestParameters, null, + false, true); + + addRequestHeadersToConnection(bucketName, builder, requestHeaders); + return performRequest(builder.build(), requestParameters, bucketName, true, false); + } + + protected Response performRestGet(String bucketName, String objectKey, Map requestParameters, + Map requestHeaders, Map userHeaders, + boolean isOEF) throws ServiceException { + + return performRestGet(bucketName, objectKey, requestParameters, requestHeaders, userHeaders, isOEF, true); + } + + protected Response performRestGet(String bucketName, String objectKey, Map requestParameters, + Map requestHeaders, Map userHeaders, + boolean isOEF, boolean needEncode) throws ServiceException { + + Request.Builder builder = setupConnection(HttpMethodEnum.GET, bucketName, objectKey, requestParameters, null, + isOEF); + + addRequestHeadersToConnection(bucketName, builder, requestHeaders); + if (userHeaders != null) { + userHeaders.forEach(builder::addHeader); + } + return performRequest(builder.build(), requestParameters, bucketName, true, isOEF, needEncode); + } + + protected Response performRestDelete(String bucketName, String objectKey, Map requestParameters, + Map metadata, Map userHeaders) + throws ServiceException { + return this.performRestDelete(bucketName, objectKey, requestParameters, metadata, userHeaders, true, false); + } + + protected Response performRestDelete(String bucketName, String objectKey, Map requestParameters, + Map metadata, Map userHeaders, + boolean autoRelease, boolean isOEF) + throws ServiceException { + + Request.Builder builder = setupConnection(HttpMethodEnum.DELETE, bucketName, objectKey, requestParameters, null, + isOEF); + + renameMetadataKeys(bucketName, builder, metadata); + if (userHeaders != null) { + userHeaders.forEach(builder::addHeader); + } + Response result = performRequest(builder.build(), requestParameters, bucketName, true, isOEF); + + if (autoRelease) { + result.close(); + } + + return result; + } + + protected Response performRestDelete(String bucketName, String objectKey, Map requestParameters, + Map userHeaders, boolean autoRelease) + throws ServiceException { + return performRestDelete(bucketName, objectKey, requestParameters, new HashMap<>(), + userHeaders, autoRelease, false); + } + + protected Response performRestOptions(String bucketName, String objectKey, Map metadata, + Map requestParameters, boolean autoRelease) + throws ServiceException { + + Request.Builder builder = setupConnection(HttpMethodEnum.OPTIONS, bucketName, objectKey, requestParameters, + null); + + addRequestHeadersToConnection(bucketName, builder, metadata); + + Response result = performRequest(builder.build(), requestParameters, bucketName); + + if (autoRelease) { + result.close(); + } + return result; + } + + protected Response performRestForApiVersion(String bucketName, String objectKey, + Map requestParameters, + Map requestHeaders) throws ServiceException { + + Request.Builder builder; + + if (null != bucketName && !"".equals(bucketName.trim())) { + builder = setupConnection(HttpMethodEnum.HEAD, bucketName, objectKey, requestParameters, null, false, + false); + } else { + builder = setupConnection(HttpMethodEnum.HEAD, bucketName, objectKey, requestParameters, null, false, true); + } + + addRequestHeadersToConnection(bucketName, builder, requestHeaders); + + return performRequestWithoutSignature(builder.build(), requestParameters, bucketName); + } + + private void sleepBeforeRetry(int internalErrorCount) { + long delayMs = 50L * (int) Math.pow(2, internalErrorCount); + if (log.isWarnEnabled()) { + log.warn("Encountered " + internalErrorCount + " Internal Server error(s), will retry in " + delayMs + + "ms"); + } + try { + Thread.sleep(delayMs); + } catch (InterruptedException e) { + log.warn("thread sleep failed.", e); + } + } + + protected Map convertHeadersToMap(Headers headers) { + Map map = new IdentityHashMap<>(); + for (Map.Entry> entry : headers.toMultimap().entrySet()) { + List values = entry.getValue(); + for (String value : values) { + // map.put(new String(entry.getKey()), value); + // fix FindBug + map.put(new StringBuilder(entry.getKey()).toString(), value); + } + } + return map; + } + + protected ProviderCredentials getProviderCredentials() { + return credentials; + } + + protected void setProviderCredentials(ProviderCredentials credentials) { + this.credentials = credentials; + } + + protected void renameMetadataKeys(String bucketName, Request.Builder builder, Map metadata, + boolean needEncode) { + Map convertedMetadata = formatMetadataAndHeader(bucketName, metadata, needEncode); + for (Map.Entry entry : convertedMetadata.entrySet()) { + builder.addHeader(entry.getKey(), entry.getValue()); + if (log.isDebugEnabled()) { + log.debug("Added metadata to connection: " + entry.getKey() + "=" + entry.getValue()); + } + } + } + + protected void renameMetadataKeys(String bucketName, Request.Builder builder, Map metadata) { + renameMetadataKeys(bucketName, builder, metadata, true); + } + + protected XmlResponsesSaxParser getXmlResponseSaxParser() throws ServiceException { + return new XmlResponsesSaxParser(); + } + + protected void addRequestHeadersToConnection(String bucketName, Request.Builder builder, + Map requestHeaders) { + if (requestHeaders != null) { + for (Map.Entry entry : requestHeaders.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if (!ServiceUtils.isValid(key) || value == null) { + continue; + } + key = key.trim(); + + if (!Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(key.toLowerCase(Locale.getDefault())) + && !key.startsWith(this.getRestHeaderPrefix(bucketName))) { + continue; + } + builder.addHeader(key, value); + if (log.isDebugEnabled()) { + log.debug("Added request header to connection: " + key + "=" + value); + } + } + } + } + + protected IHeaders getIHeaders(String bucketName) { + return Constants.HEADERS_MAP.get(this.getProviderCredentials().getLocalAuthType(bucketName)); + } + + protected IConvertor getIConvertor(String bucketName) { + return Constants.CONVERTOR_MAP.get(this.getProviderCredentials().getLocalAuthType(bucketName)); + } + + protected boolean isAuthTypeNegotiation() { + return this.obsProperties.getBoolProperty(ObsConstraint.AUTH_TYPE_NEGOTIATION, true); + } + + protected String getFileSystemDelimiter() { + return this.obsProperties.getStringProperty(ObsConstraint.FS_DELIMITER, "/"); } - - protected String getEndpoint() { - return this.obsProperties.getStringProperty(ObsConstraint.END_POINT, ""); - } - - protected boolean isPathStyle() { - return this.obsProperties.getBoolProperty(ObsConstraint.DISABLE_DNS_BUCKET, false); - } - - protected int getHttpPort() { - return this.obsProperties.getIntProperty(ObsConstraint.HTTP_PORT, ObsConstraint.HTTP_PORT_VALUE); - } - - protected int getHttpsPort() { - return this.obsProperties.getIntProperty(ObsConstraint.HTTPS_PORT, ObsConstraint.HTTPS_PORT_VALUE); - } - - protected boolean getHttpsOnly() { - return this.obsProperties.getBoolProperty(ObsConstraint.HTTPS_ONLY, true); - } - - protected boolean isAuthTypeNegotiation() { - return this.obsProperties.getBoolProperty(ObsConstraint.AUTH_TYPE_NEGOTIATION, true); - } - - protected CacheManager getApiVersionCache() { - return apiVersionCache; - } - - protected boolean isCname() { - return this.obsProperties.getBoolProperty(ObsConstraint.IS_CNAME, false); - } - - protected String getFileSystemDelimiter() { - return this.obsProperties.getStringProperty(ObsConstraint.FS_DELIMITER, "/"); - } } diff --git a/app/src/main/java/com/obs/services/internal/ResumableClient.java b/app/src/main/java/com/obs/services/internal/ResumableClient.java deleted file mode 100644 index c8adcab..0000000 --- a/app/src/main/java/com/obs/services/internal/ResumableClient.java +++ /dev/null @@ -1,1475 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co.,Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use - * this file except in compliance with the License. You may obtain a copy of the - * License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package com.obs.services.internal; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectOutputStream; -import java.io.OutputStream; -import java.io.RandomAccessFile; -import java.io.Serializable; -import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; - -import com.obs.log.ILogger; -import com.obs.log.LoggerBuilder; -import com.obs.services.ObsClient; -import com.obs.services.exception.ObsException; -import com.obs.services.internal.io.ProgressInputStream; -import com.obs.services.internal.utils.SecureObjectInputStream; -import com.obs.services.internal.utils.ServiceUtils; -import com.obs.services.model.AbortMultipartUploadRequest; -import com.obs.services.model.CompleteMultipartUploadRequest; -import com.obs.services.model.CompleteMultipartUploadResult; -import com.obs.services.model.DownloadFileRequest; -import com.obs.services.model.DownloadFileResult; -import com.obs.services.model.GetObjectMetadataRequest; -import com.obs.services.model.GetObjectRequest; -import com.obs.services.model.HeaderResponse; -import com.obs.services.model.InitiateMultipartUploadRequest; -import com.obs.services.model.InitiateMultipartUploadResult; -import com.obs.services.model.ObjectMetadata; -import com.obs.services.model.ObsObject; -import com.obs.services.model.PartEtag; -import com.obs.services.model.UploadFileRequest; -import com.obs.services.model.UploadPartRequest; -import com.obs.services.model.UploadPartResult; - -public class ResumableClient { - - private static final ILogger log = LoggerBuilder.getLogger("com.obs.services.ObsClient"); - private ObsClient obsClient; - - public ResumableClient(ObsClient obsClient) { - this.obsClient = obsClient; - } - - public CompleteMultipartUploadResult uploadFileResume(UploadFileRequest uploadFileRequest) { - ServiceUtils.asserParameterNotNull(uploadFileRequest, "UploadFileRequest is null"); - ServiceUtils.asserParameterNotNull(uploadFileRequest.getBucketName(), "bucketName is null"); - ServiceUtils.asserParameterNotNull2(uploadFileRequest.getObjectKey(), "objectKey is null"); - ServiceUtils.asserParameterNotNull(uploadFileRequest.getUploadFile(), "uploadfile is null"); - if (uploadFileRequest.isEnableCheckpoint()) { - if (!ServiceUtils.isValid(uploadFileRequest.getCheckpointFile())) { - uploadFileRequest.setCheckpointFile(uploadFileRequest.getUploadFile() + ".uploadFile_record"); - } - } - try { - return uploadFileCheckPoint(uploadFileRequest); - } catch (ObsException e) { - throw e; - }catch (ServiceException e) { - throw ServiceUtils.changeFromServiceException(e); - } - catch (Exception e) { - throw new ObsException(e.getMessage(), e); - } - } - - protected void abortMultipartUploadSilent(String uploadId, String bucketName, String objectKey) { - try { - AbortMultipartUploadRequest request = new AbortMultipartUploadRequest(bucketName, objectKey, uploadId); - this.obsClient.abortMultipartUpload(request); - }catch (Exception e) { - if(log.isWarnEnabled()) { - log.warn("Abort multipart upload failed", e); - } - } - } - - protected HeaderResponse abortMultipartUpload(String uploadId, String bucketName, String objectKey) { - AbortMultipartUploadRequest request = new AbortMultipartUploadRequest(bucketName, objectKey, uploadId); - return this.obsClient.abortMultipartUpload(request); - } - - - private CompleteMultipartUploadResult uploadFileCheckPoint(UploadFileRequest uploadFileRequest) throws Exception { - UploadCheckPoint uploadCheckPoint = new UploadCheckPoint(); - if (uploadFileRequest.isEnableCheckpoint()) { - boolean needRecreate = false; - try { - uploadCheckPoint.load(uploadFileRequest.getCheckpointFile()); - } catch (Exception e) { - needRecreate = true; - } - - if (!needRecreate) { - if (!(uploadFileRequest.getBucketName().equals(uploadCheckPoint.bucketName) - && uploadFileRequest.getObjectKey().equals(uploadCheckPoint.objectKey) - && uploadFileRequest.getUploadFile().equals(uploadCheckPoint.uploadFile))) { - needRecreate = true; - } else if (!uploadCheckPoint.isValid(uploadFileRequest.getUploadFile())) { - needRecreate = true; - } - } - - if (needRecreate) { - if (uploadCheckPoint.bucketName != null && uploadCheckPoint.objectKey != null - && uploadCheckPoint.uploadID != null) { - this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadCheckPoint.bucketName, uploadCheckPoint.objectKey); - } - File uploadCheckFile = new File(uploadFileRequest.getCheckpointFile()); - if (uploadCheckFile.exists() && uploadCheckFile.isFile()) { - uploadCheckFile.delete(); - } - prepare(uploadFileRequest, uploadCheckPoint); - } - } else { - prepare(uploadFileRequest, uploadCheckPoint); - } - - // 开始上传 - List partResults = uploadfile(uploadFileRequest, uploadCheckPoint); - - // 有错误抛出异常,无异常则合并多段 - for (PartResult partResult : partResults) { - if (partResult.isFailed() && partResult.getException() != null) { - // 未开启,取消多段 - if (!uploadFileRequest.isEnableCheckpoint()) { - this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadFileRequest.getBucketName(), uploadFileRequest.getObjectKey()); - } else if (uploadCheckPoint.isAbort) { - this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadFileRequest.getBucketName(), uploadFileRequest.getObjectKey()); - File uploadCheckFile = new File(uploadFileRequest.getCheckpointFile()); - if (uploadCheckFile.exists() && uploadCheckFile.isFile()) { - uploadCheckFile.delete(); - } - } - throw partResult.getException(); - } - } - - // 合并多段 - CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest( - uploadFileRequest.getBucketName(), uploadFileRequest.getObjectKey(), uploadCheckPoint.uploadID, - uploadCheckPoint.partEtags); - - try { - CompleteMultipartUploadResult result = this.obsClient.completeMultipartUpload(completeMultipartUploadRequest); - if (uploadFileRequest.isEnableCheckpoint()) { - File uploadFileTmp = new File(uploadFileRequest.getCheckpointFile()); - if (uploadFileTmp.isFile() && uploadFileTmp.exists()) { - uploadFileTmp.delete(); - } - } - return result; - } catch (ObsException e) { - if (!uploadFileRequest.isEnableCheckpoint()) { - this.abortMultipartUpload(uploadCheckPoint.uploadID, uploadFileRequest.getBucketName(), uploadFileRequest.getObjectKey()); - } else { - if (e.getResponseCode() >= 300 && e.getResponseCode() < 500 && e.getResponseCode() != 408) { - this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadFileRequest.getBucketName(), uploadFileRequest.getObjectKey()); - File uploadCheckFile = new File(uploadFileRequest.getCheckpointFile()); - if (uploadCheckFile.exists() && uploadCheckFile.isFile()) { - uploadCheckFile.delete(); - } - } - } - throw e; - } - } - - private List uploadfile(UploadFileRequest uploadFileRequest, UploadCheckPoint uploadCheckPoint) - throws Exception { - ArrayList pieceResults = new ArrayList(); - ExecutorService executorService = Executors.newFixedThreadPool(uploadFileRequest.getTaskNum()); - ArrayList> futures = new ArrayList>(); - - ProgressManager progressManager = null; - if(uploadFileRequest.getProgressListener() == null) { - for (int i = 0; i < uploadCheckPoint.uploadParts.size(); i++) { - UploadPart uploadPart = uploadCheckPoint.uploadParts.get(i); - if (uploadPart.isCompleted) { - PartResult pr = new PartResult(uploadPart.partNumber, uploadPart.offset, uploadPart.size); - pr.setFailed(false); - pieceResults.add(pr); - } else { - futures.add(executorService.submit(new Mission(i, uploadCheckPoint, i, uploadFileRequest, this.obsClient))); - } - } - }else { - long transferredBytes = 0L; - List unfinishedUploadMissions = new LinkedList(); - for (int i = 0; i < uploadCheckPoint.uploadParts.size(); i++) { - UploadPart uploadPart = uploadCheckPoint.uploadParts.get(i); - if (uploadPart.isCompleted) { - PartResult pr = new PartResult(uploadPart.partNumber, uploadPart.offset, uploadPart.size); - pr.setFailed(false); - pieceResults.add(pr); - transferredBytes += uploadPart.size; - } else { - unfinishedUploadMissions.add(new Mission(i, uploadCheckPoint, i, uploadFileRequest, this.obsClient)); - } - } - progressManager = new ConcurrentProgressManager(uploadCheckPoint.uploadFileStatus.size, transferredBytes, uploadFileRequest.getProgressListener(), - uploadFileRequest.getProgressInterval() > 0 ? uploadFileRequest.getProgressInterval() : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); - for(Mission mission : unfinishedUploadMissions) { - mission.setProgressManager(progressManager); - futures.add(executorService.submit(mission)); - } - } - - executorService.shutdown(); - executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); - - for (Future future : futures) { - try { - PartResult tr = future.get(); - pieceResults.add(tr); - } catch (ExecutionException e) { - if (!uploadFileRequest.isEnableCheckpoint()) { - this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadFileRequest.getBucketName(), uploadFileRequest.getObjectKey()); - } - throw e; - } - } - - if(progressManager != null) { - progressManager.progressEnd(); - } - - return pieceResults; - } - - static class Mission implements Callable { - - private int id; - private UploadCheckPoint uploadCheckPoint; - private int partIndex; - private UploadFileRequest uploadFileRequest; - private ObsClient obsClient; - private ProgressManager progressManager; - - public Mission(int id, UploadCheckPoint uploadCheckPoint, int partIndex, UploadFileRequest uploadFileRequest, - ObsClient obsClient) { - this.id = id; - this.uploadCheckPoint = uploadCheckPoint; - this.partIndex = partIndex; - this.uploadFileRequest = uploadFileRequest; - this.obsClient = obsClient; - } - - @Override - public PartResult call() throws Exception { - PartResult tr = null; - UploadPart uploadPart = uploadCheckPoint.uploadParts.get(partIndex); - tr = new PartResult(partIndex + 1, uploadPart.offset, uploadPart.size); - if (!uploadCheckPoint.isAbort) { - try { - UploadPartRequest uploadPartRequest = new UploadPartRequest(); - uploadPartRequest.setBucketName(uploadFileRequest.getBucketName()); - uploadPartRequest.setObjectKey(uploadFileRequest.getObjectKey()); - uploadPartRequest.setUploadId(uploadCheckPoint.uploadID); - uploadPartRequest.setPartSize(uploadPart.size); - uploadPartRequest.setPartNumber(uploadPart.partNumber); - - if(this.progressManager == null) { - uploadPartRequest.setFile(new File(uploadFileRequest.getUploadFile())); - uploadPartRequest.setOffset(uploadPart.offset); - }else { - InputStream input = new FileInputStream(uploadFileRequest.getUploadFile()); - long offset = uploadPart.offset; - long skipByte = input.skip(offset); - if(offset < skipByte) { - log.error(String.format("The actual number of skipped bytes (%d) is less than expected (%d): ", skipByte, offset)); - } - uploadPartRequest.setInput(new ProgressInputStream(input, this.progressManager, false)); - } - - UploadPartResult result = obsClient.uploadPart(uploadPartRequest); - - PartEtag partEtag = new PartEtag(result.getEtag(), result.getPartNumber()); - uploadCheckPoint.update(partIndex, partEtag, true); - tr.setFailed(false); - - if (uploadFileRequest.isEnableCheckpoint()) { - uploadCheckPoint.record(uploadFileRequest.getCheckpointFile()); - } - - } catch (ObsException e) { - if (e.getResponseCode() >= 300 && e.getResponseCode() < 500 && e.getResponseCode() != 408) { - uploadCheckPoint.isAbort = true; - } - // 有异常打印到日志文件 - tr.setFailed(true); - tr.setException(e); - if(log.isErrorEnabled()) { - log.error(String.format("Task %d:%s upload part %d failed: ", id, "upload" + id, partIndex + 1), - e); - } - }catch(Exception e) { - tr.setFailed(true); - tr.setException(e); - if(log.isErrorEnabled()) { - log.error(String.format("Task %d:%s upload part %d failed: ", id, "upload" + id, partIndex + 1), - e); - } - } - } else { - tr.setFailed(true); - } - return tr; - } - - public void setProgressManager(ProgressManager progressManager) { - this.progressManager = progressManager; - } - - } - - private void prepare(UploadFileRequest uploadFileRequest, UploadCheckPoint uploadCheckPoint) throws Exception { - uploadCheckPoint.uploadFile = uploadFileRequest.getUploadFile(); - uploadCheckPoint.bucketName = uploadFileRequest.getBucketName(); - uploadCheckPoint.objectKey = uploadFileRequest.getObjectKey(); - uploadCheckPoint.uploadFileStatus = FileStatus.getFileStatus(uploadCheckPoint.uploadFile, - uploadFileRequest.isEnableCheckSum()); - uploadCheckPoint.uploadParts = splitUploadFile(uploadCheckPoint.uploadFileStatus.size, - uploadFileRequest.getPartSize()); - uploadCheckPoint.partEtags = new ArrayList(); - - InitiateMultipartUploadRequest initiateUploadRequest = new InitiateMultipartUploadRequest( - uploadFileRequest.getBucketName(), uploadFileRequest.getObjectKey()); - - initiateUploadRequest.setExtensionPermissionMap(uploadFileRequest.getExtensionPermissionMap()); - initiateUploadRequest.setAcl(uploadFileRequest.getAcl()); - initiateUploadRequest.setSuccessRedirectLocation(uploadFileRequest.getSuccessRedirectLocation()); - initiateUploadRequest.setSseCHeader(uploadFileRequest.getSseCHeader()); - initiateUploadRequest.setSseKmsHeader(uploadFileRequest.getSseKmsHeader()); - initiateUploadRequest.setMetadata(uploadFileRequest.getObjectMetadata()); - - InitiateMultipartUploadResult initiateUploadResult = this.obsClient.initiateMultipartUpload(initiateUploadRequest); - uploadCheckPoint.uploadID = initiateUploadResult.getUploadId(); - if (uploadFileRequest.isEnableCheckpoint()) { - try { - uploadCheckPoint.record(uploadFileRequest.getCheckpointFile()); - } catch (Exception e) { - this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadCheckPoint.bucketName, uploadCheckPoint.objectKey); - throw e; - } - } - } - - private ArrayList splitUploadFile(long size, long partSize) { - ArrayList parts = new ArrayList(); - - long partNum = size / partSize; - if (partNum >= 10000) { - partSize = size % 10000 == 0? size / 10000 : size / 10000 + 1; - partNum = size / partSize; - } - if (size % partSize > 0) { - partNum++; - } - if(partNum == 0) { - UploadPart part = new UploadPart(); - part.partNumber = 1; - part.offset = 0; - part.size = 0; - part.isCompleted = false; - parts.add(part); - }else { - for (long i = 0; i < partNum; i++) { - UploadPart part = new UploadPart(); - part.partNumber = (int) (i + 1); - part.offset = i * partSize; - part.size = partSize; - part.isCompleted = false; - parts.add(part); - } - if (size % partSize > 0) { - parts.get(parts.size() - 1).size = size % partSize; - } - } - - - - return parts; - } - - /** - * 断点续传上传所需类 - */ - static class UploadCheckPoint implements Serializable { - - private static final long serialVersionUID = 5564757792864743464L; - - /** - * 从checkpoint文件中加载checkpoint数据 - * - * @param checkPointFile - * @throws Exception - */ - public void load(String checkPointFile) throws Exception { - FileInputStream fileInput = null; - SecureObjectInputStream in = null; - try { - fileInput = new FileInputStream(checkPointFile); - in = new SecureObjectInputStream(fileInput); - UploadCheckPoint tmp = (UploadCheckPoint) in.readObject(); - assign(tmp); - } finally { - ServiceUtils.closeStream(in); - ServiceUtils.closeStream(fileInput); - } - } - - /** - * 把checkpoint数据写到checkpoint文件 - * - * @param checkPointFile - * @throws IOException - */ - public synchronized void record(String checkPointFile) throws IOException { - this.md5 = hashCode(); - FileOutputStream fileOutput = null; - ObjectOutputStream outStream = null; - try { - fileOutput = new FileOutputStream(checkPointFile); - outStream = new ObjectOutputStream(fileOutput); - outStream.writeObject(this); - } finally { - ServiceUtils.closeStream(outStream); - ServiceUtils.closeStream(fileOutput); - } - } - - /** - * 分块上传完成,更新状态 - * - * @param partIndex - * @param partETag - * @param completed - */ - public synchronized void update(int partIndex, PartEtag partETag, boolean completed) { - partEtags.add(partETag); - uploadParts.get(partIndex).isCompleted = completed; - } - - /** - * 判读本地文件与checkpoint中记录的信息是否相符合,校验一致性 - * - * @param uploadFile - * @return boolean - * @throws IOException - */ - public boolean isValid(String uploadFile) throws IOException { - if (this.md5 != hashCode()) { - return false; - } - - File upload = new File(uploadFile); - if (!this.uploadFile.equals(uploadFile) || this.uploadFileStatus.size != upload.length() - || this.uploadFileStatus.lastModified != upload.lastModified()) { - return false; - } - - if (this.uploadFileStatus.checkSum != null) { - try { - return this.uploadFileStatus.checkSum - .equals(ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(new FileInputStream(upload)))); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException(e.getMessage(), e); - } - } - - return true; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((objectKey == null) ? 0 : objectKey.hashCode()); - result = prime * result + ((bucketName == null) ? 0 : bucketName.hashCode()); - result = prime * result + ((partEtags == null) ? 0 : partEtags.hashCode()); - result = prime * result + ((uploadFile == null) ? 0 : uploadFile.hashCode()); - result = prime * result + ((uploadFileStatus == null) ? 0 : uploadFileStatus.hashCode()); - result = prime * result + ((uploadID == null) ? 0 : uploadID.hashCode()); - result = prime * result + ((uploadParts == null) ? 0 : uploadParts.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } else { - if (obj instanceof UploadCheckPoint) { - UploadCheckPoint uploadCheckPoint = (UploadCheckPoint) obj; - if (uploadCheckPoint.hashCode() == obj.hashCode()) { - return true; - } - } - } - return false; - } - - private void assign(UploadCheckPoint tmp) { - this.md5 = tmp.md5; - this.bucketName = tmp.bucketName; - this.uploadFile = tmp.uploadFile; - this.uploadFileStatus = tmp.uploadFileStatus; - this.objectKey = tmp.objectKey; - this.uploadID = tmp.uploadID; - this.uploadParts = tmp.uploadParts; - this.partEtags = tmp.partEtags; - } - - public int md5; - public String uploadFile; - public FileStatus uploadFileStatus; - public String bucketName; - public String objectKey; - public String uploadID; - public ArrayList uploadParts; - public ArrayList partEtags; - public transient volatile boolean isAbort = false; - - } - - static class FileStatus implements Serializable { - private static final long serialVersionUID = -3135754191745936521L; - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((checkSum == null) ? 0 : checkSum.hashCode()); - result = prime * result + (int) (lastModified ^ (lastModified >>> 32)); - result = prime * result + (int) (size ^ (size >>> 32)); - return result; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } else { - if (obj instanceof FileStatus) { - FileStatus fileStatus = (FileStatus) obj; - if (fileStatus.hashCode() == obj.hashCode()) { - return true; - } - } - } - return false; - } - - public static FileStatus getFileStatus(String uploadFile, boolean checkSum) throws IOException { - FileStatus fileStatus = new FileStatus(); - File file = new File(uploadFile); - fileStatus.size = file.length(); - fileStatus.lastModified = file.lastModified(); - if (checkSum) { - try { - fileStatus.checkSum = ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(new FileInputStream(file))); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException(e.getMessage(), e); - } - } - return fileStatus; - } - - public long size; // 文件大小 - public long lastModified; // 文件最后修改时间 - public String checkSum; // 文件checkSum - } - - static class UploadPart implements Serializable { - - private static final long serialVersionUID = 751520598820222785L; - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (isCompleted ? 1 : 0); - result = prime * result + partNumber; - result = prime * result + (int) (offset ^ (offset >>> 32)); - result = prime * result + (int) (size ^ (size >>> 32)); - return result; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } else { - if (obj instanceof UploadPart) { - UploadPart uploadPart = (UploadPart) obj; - if (uploadPart.hashCode() == obj.hashCode()) { - return true; - } - } - } - return false; - } - - public int partNumber; // 分片序号 - public long offset; // 分片在文件中的偏移量 - public long size; // 分片大小 - public boolean isCompleted; // 该分片上传是否完成 - } - - static class PartResult { - - public PartResult(int partNumber, long offset, long length) { - this.partNumber = partNumber; - this.offset = offset; - this.length = length; - } - - public int getpartNumber() { - return partNumber; - } - - public void setpartNumber(int partNumber) { - this.partNumber = partNumber; - } - - public long getOffset() { - return offset; - } - - public void setOffset(long offset) { - this.offset = offset; - } - - public long getLength() { - return length; - } - - public void setLength(long length) { - this.length = length; - } - - public boolean isFailed() { - return isfailed; - } - - public void setFailed(boolean isfailed) { - this.isfailed = isfailed; - } - - public Exception getException() { - return exception; - } - - public void setException(Exception exception) { - this.exception = exception; - } - - private int partNumber; // 分片序号 - private long offset; // 分片在文件中的偏移 - private long length; // 分片长度 - private boolean isfailed; // 分片上传是否失败 - private Exception exception; // 分片上传异常 - } - - public DownloadFileResult downloadFileResume(DownloadFileRequest downloadFileRequest) { - ServiceUtils.asserParameterNotNull(downloadFileRequest, "DownloadFileRequest is null"); - ServiceUtils.asserParameterNotNull(downloadFileRequest.getBucketName(), "the bucketName is null"); - String key = downloadFileRequest.getObjectKey(); - ServiceUtils.asserParameterNotNull2(key, "the objectKey is null"); - - if (downloadFileRequest.getDownloadFile() == null) { - downloadFileRequest.setDownloadFile(key); - } - if (downloadFileRequest.isEnableCheckpoint()) { - if (downloadFileRequest.getCheckpointFile() == null || downloadFileRequest.getCheckpointFile().isEmpty()) { - downloadFileRequest.setCheckpointFile(downloadFileRequest.getDownloadFile() + ".downloadFile_record"); - } - } - try { - return downloadCheckPoint(downloadFileRequest); - } catch (ObsException e) { - throw e; - }catch (ServiceException e) { - throw ServiceUtils.changeFromServiceException(e); - } - catch (Exception e) { - throw new ObsException(e.getMessage(), e); - } - } - - private DownloadFileResult downloadCheckPoint(DownloadFileRequest downloadFileRequest) throws Exception { - - ObjectMetadata objectMetadata; - DownloadFileResult downloadFileResult = new DownloadFileResult(); - try { - GetObjectMetadataRequest request = new GetObjectMetadataRequest(downloadFileRequest.getBucketName(), downloadFileRequest.getObjectKey(), downloadFileRequest.getVersionId()); - objectMetadata = this.obsClient.getObjectMetadata(request); - } catch (ObsException e) { - if (e.getResponseCode() >= 300 && e.getResponseCode() < 500 && e.getResponseCode() != 408) { - File tmpfile = new File(downloadFileRequest.getTempDownloadFile()); - if (tmpfile.exists() && tmpfile.isFile()) { - tmpfile.delete(); - } - File file = new File(downloadFileRequest.getCheckpointFile()); - if (file.isFile() && file.exists()) { - file.delete(); - } - } - throw e; - } - - downloadFileResult.setObjectMetadata(objectMetadata); - - if(objectMetadata.getContentLength() == 0) { - File tmpfile = new File(downloadFileRequest.getTempDownloadFile()); - if (tmpfile.exists() && tmpfile.isFile()) { - tmpfile.delete(); - } - File file = new File(downloadFileRequest.getCheckpointFile()); - if (file.isFile() && file.exists()) { - file.delete(); - } - File dfile = new File(downloadFileRequest.getDownloadFile()); - dfile.getParentFile().mkdirs(); - new RandomAccessFile(dfile, "rw").close(); - if(downloadFileRequest.getProgressListener() != null) { - downloadFileRequest.getProgressListener(). - progressChanged(new DefaultProgressStatus(0, 0, 0, 0, 0)); - } - return downloadFileResult; - } - - DownloadCheckPoint downloadCheckPoint = new DownloadCheckPoint(); - if (downloadFileRequest.isEnableCheckpoint()) { - boolean needRecreate = false; - try { - downloadCheckPoint.load(downloadFileRequest.getCheckpointFile()); - } catch (Exception e) { - needRecreate = true; - } - if (!needRecreate) { - if (!(downloadFileRequest.getBucketName().equals(downloadCheckPoint.bucketName) - && downloadFileRequest.getObjectKey().equals(downloadCheckPoint.objectKey) - && downloadFileRequest.getDownloadFile().equals(downloadCheckPoint.downloadFile))) { - needRecreate = true; - }else if (!downloadCheckPoint.isValid(downloadFileRequest.getTempDownloadFile(), objectMetadata)) { - needRecreate = true; - }else if(downloadFileRequest.getVersionId() == null) { - if(downloadCheckPoint.versionId != null) { - needRecreate = true; - } - }else if(!downloadFileRequest.getVersionId().equals(downloadCheckPoint.versionId)) { - needRecreate = true; - } - } - if (needRecreate) { - if (downloadCheckPoint.tmpFileStatus != null) { - File tmpfile = new File(downloadCheckPoint.tmpFileStatus.tmpFilePath); - if (tmpfile.exists() && tmpfile.isFile()) { - tmpfile.delete(); - } - } - File file = new File(downloadFileRequest.getCheckpointFile()); - if (file.isFile() && file.exists()) { - file.delete(); - } - prepare(downloadFileRequest, downloadCheckPoint, objectMetadata); - } - } else { - prepare(downloadFileRequest, downloadCheckPoint, objectMetadata); - } - - // 并发下载分片 - DownloadResult downloadResult = download(downloadCheckPoint, downloadFileRequest); - for (PartResultDown partResult : downloadResult.getPartResults()) { - if (partResult.isFailed() && partResult.getException() != null) { - if (!downloadFileRequest.isEnableCheckpoint()) { - File tmpfile = new File(downloadCheckPoint.tmpFileStatus.tmpFilePath); - if (tmpfile.exists() && tmpfile.isFile()) { - tmpfile.delete(); - } - } else if (downloadCheckPoint.isAbort) { - File tmpfile = new File(downloadCheckPoint.tmpFileStatus.tmpFilePath); - if (tmpfile.exists() && tmpfile.isFile()) { - tmpfile.delete(); - } - File file = new File(downloadFileRequest.getCheckpointFile()); - if (file.isFile() && file.exists()) { - file.delete(); - } - } - throw partResult.getException(); - } - } - - // 重命名临时文件 - renameTo(downloadFileRequest.getTempDownloadFile(), downloadFileRequest.getDownloadFile()); - - // 开启了断点下载,成功上传后删除checkpoint文件 - if (downloadFileRequest.isEnableCheckpoint()) { - File file = new File(downloadFileRequest.getCheckpointFile()); - if (file.isFile() && file.exists()) { - file.delete(); - } - } - - return downloadFileResult; - } - - private void renameTo(String tempDownloadFilePath, String downloadFilePath) throws IOException { - File tmpfile = new File(tempDownloadFilePath); - File downloadFile = new File(downloadFilePath); - if (!tmpfile.exists()) { - throw new FileNotFoundException("tmpFile '" + tmpfile + "' does not exist"); - } - if (downloadFile.exists()) { - if (!downloadFile.delete()) { - throw new IOException("downloadFile '" + downloadFile + "' is exist"); - } - } - if (tmpfile.isDirectory() || downloadFile.isDirectory()) { - throw new IOException("downloadPath is a directory"); - } - final boolean renameFlag = tmpfile.renameTo(downloadFile); - if (!renameFlag) { - InputStream input = null; - OutputStream output = null; - try { - input = new FileInputStream(tmpfile); - output = new FileOutputStream(downloadFile); - byte[] buffer = new byte[1024 * 8]; - int length; - while ((length = input.read(buffer)) > 0) { - output.write(buffer, 0, length); - } - } finally { - ServiceUtils.closeStream(input); - ServiceUtils.closeStream(output); - } - if (!tmpfile.delete()) { - throw new IOException("the tmpfile '" + tmpfile - + "' can not delete, please delete it to ensure the download finish."); - } - } - } - - private DownloadResult download(DownloadCheckPoint downloadCheckPoint, DownloadFileRequest downloadFileRequest) - throws Exception { - ArrayList taskResults = new ArrayList(); - DownloadResult downloadResult = new DownloadResult(); - ExecutorService service = Executors.newFixedThreadPool(downloadFileRequest.getTaskNum()); - ArrayList> futures = new ArrayList>(); - - ProgressManager progressManager = null; - if(downloadFileRequest.getProgressListener() == null) { - for (int i = 0; i < downloadCheckPoint.downloadParts.size(); i++) { - DownloadPart downloadPart = downloadCheckPoint.downloadParts.get(i); - if (!downloadPart.isCompleted) { - Task task = new Task(i, "download-" + i, downloadCheckPoint, i, downloadFileRequest, this.obsClient); - futures.add(service.submit(task)); - } else { - taskResults.add(new PartResultDown(i + 1, downloadPart.offset, downloadPart.end)); - } - } - }else { - List unfinishedTasks = new LinkedList(); - long transferredBytes = 0L; - for (int i = 0; i < downloadCheckPoint.downloadParts.size(); i++) { - DownloadPart downloadPart = downloadCheckPoint.downloadParts.get(i); - if (!downloadPart.isCompleted) { - Task task = new Task(i, "download-" + i, downloadCheckPoint, i, downloadFileRequest, this.obsClient); - unfinishedTasks.add(task); - } else { - transferredBytes += downloadPart.end - downloadPart.offset + 1; - taskResults.add(new PartResultDown(i + 1, downloadPart.offset, downloadPart.end)); - } - } - - progressManager = new ConcurrentProgressManager(downloadCheckPoint.objectStatus.size, transferredBytes, downloadFileRequest.getProgressListener(), - downloadFileRequest.getProgressInterval() > 0 ? downloadFileRequest.getProgressInterval() : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); - for(Task task : unfinishedTasks) { - task.setProgressManager(progressManager); - futures.add(service.submit(task)); - } - } - - service.shutdown(); - service.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); - - - for (Future future : futures) { - try { - PartResultDown tr = future.get(); - taskResults.add(tr); - } catch (ExecutionException e) { - throw e; - } - } - - downloadResult.setPartResults(taskResults); - if(progressManager != null) { - progressManager.progressEnd(); - } - - return downloadResult; - } - - static class Task implements Callable { - - private int id; - private String name; - private DownloadCheckPoint downloadCheckPoint; - private int partIndex; - private DownloadFileRequest downloadFileRequest; - private ObsClient obsClient; - private ProgressManager progressManager; - - public Task(int id, String name, DownloadCheckPoint downloadCheckPoint, int partIndex, - DownloadFileRequest downloadFileRequest, ObsClient obsClient) { - this.id = id; - this.name = name; - this.downloadCheckPoint = downloadCheckPoint; - this.partIndex = partIndex; - this.downloadFileRequest = downloadFileRequest; - this.obsClient = obsClient; - } - - @Override - public PartResultDown call() throws Exception { - RandomAccessFile output = null; - InputStream content = null; - DownloadPart downloadPart = downloadCheckPoint.downloadParts.get(partIndex); - PartResultDown tr = new PartResultDown(partIndex + 1, downloadPart.offset, downloadPart.end); - if (!downloadCheckPoint.isAbort) { - try { - output = new RandomAccessFile(downloadFileRequest.getTempDownloadFile(), "rw"); - output.seek(downloadPart.offset); - - GetObjectRequest getObjectRequest = new GetObjectRequest(downloadFileRequest.getBucketName(), - downloadFileRequest.getObjectKey()); - - getObjectRequest.setIfMatchTag(downloadFileRequest.getIfMatchTag()); - getObjectRequest.setIfNoneMatchTag(downloadFileRequest.getIfNoneMatchTag()); - getObjectRequest.setIfModifiedSince(downloadFileRequest.getIfModifiedSince()); - getObjectRequest.setIfUnmodifiedSince(downloadFileRequest.getIfUnmodifiedSince()); - getObjectRequest.setRangeStart(downloadPart.offset); - getObjectRequest.setRangeEnd(downloadPart.end); - getObjectRequest.setVersionId(downloadFileRequest.getVersionId()); - getObjectRequest.setCacheOption(downloadFileRequest.getCacheOption()); - getObjectRequest.setTtl(downloadFileRequest.getTtl()); - - - ObsObject object = obsClient.getObject(getObjectRequest); - content = object.getObjectContent(); - if(this.progressManager != null) { - content = new ProgressInputStream(content, this.progressManager, false); - } - - byte[] buffer = new byte[ObsConstraint.DEFAULT_CHUNK_SIZE]; - int bytesOffset; - while ((bytesOffset = content.read(buffer)) != -1) { - output.write(buffer, 0, bytesOffset); - } - downloadCheckPoint.update(partIndex, true, downloadFileRequest.getTempDownloadFile()); - } catch (ObsException e) { - if (e.getResponseCode() >= 300 && e.getResponseCode() < 500 && e.getResponseCode() != 408) { - downloadCheckPoint.isAbort = true; - } - tr.setFailed(true); - tr.setException(e); - if(log.isErrorEnabled()) { - log.error(String.format("Task %d:%s download part %d failed: ", id, name, partIndex), e); - } - } catch(Exception e) { - tr.setFailed(true); - tr.setException(e); - if(log.isErrorEnabled()) { - log.error(String.format("Task %d:%s download part %d failed: ", id, name, partIndex), e); - } - }finally { - ServiceUtils.closeStream(output); - ServiceUtils.closeStream(content); - if (downloadFileRequest.isEnableCheckpoint()) { - downloadCheckPoint.updateTmpFile(downloadFileRequest.getTempDownloadFile()); - downloadCheckPoint.record(downloadFileRequest.getCheckpointFile()); - } - } - } else { - tr.setFailed(true); - } - return tr; - } - - public void setProgressManager(ProgressManager progressManager) { - this.progressManager = progressManager; - } - } - - private void prepare(DownloadFileRequest downloadFileRequest, DownloadCheckPoint downloadCheckPoint, ObjectMetadata objectMetadata) - throws Exception { - downloadCheckPoint.bucketName = downloadFileRequest.getBucketName(); - downloadCheckPoint.objectKey = downloadFileRequest.getObjectKey(); - downloadCheckPoint.versionId = downloadFileRequest.getVersionId(); - downloadCheckPoint.downloadFile = downloadFileRequest.getDownloadFile(); - ObjectStatus objStatus = new ObjectStatus(); - objStatus.size = objectMetadata.getContentLength(); - objStatus.lastModified = objectMetadata.getLastModified(); - objStatus.Etag = objectMetadata.getEtag(); - downloadCheckPoint.objectStatus = objStatus; - downloadCheckPoint.downloadParts = splitObject(downloadCheckPoint.objectStatus.size, - downloadFileRequest.getPartSize()); - File tmpfile = new File(downloadFileRequest.getTempDownloadFile()); - tmpfile.getParentFile().mkdirs(); - RandomAccessFile randomAccessFile = null; - try { - randomAccessFile = new RandomAccessFile(tmpfile, "rw"); - randomAccessFile.setLength(downloadCheckPoint.objectStatus.size); - } finally { - ServiceUtils.closeStream(randomAccessFile); - } - downloadCheckPoint.tmpFileStatus = new TmpFileStatus(downloadCheckPoint.objectStatus.size, - new Date(tmpfile.lastModified()), downloadFileRequest.getTempDownloadFile()); - - if (downloadFileRequest.isEnableCheckpoint()) { - try { - downloadCheckPoint.record(downloadFileRequest.getCheckpointFile()); - } catch (Exception e) { - if (tmpfile.exists() && tmpfile.isFile()) { - tmpfile.delete(); - } - throw e; - } - } - } - - private ArrayList splitObject(long size, long partSize) { - ArrayList parts = new ArrayList(); - - long piece = size / partSize; - if (piece >= 10000) { - partSize = size % 10000 == 0? size/10000 : size /10000 + 1; - } - - long offset = 0l; - for (int i = 0; offset < size; offset += partSize, i++) { - DownloadPart downloadPart = new DownloadPart(); - downloadPart.partNumber = i; - downloadPart.offset = offset; - if (offset + partSize > size) { - downloadPart.end = size - 1; - } else { - downloadPart.end = offset + partSize - 1; - } - parts.add(downloadPart); - } - return parts; - } - - /** - * 断点续传的下载所需类 - */ - static class DownloadCheckPoint implements Serializable { - private static final long serialVersionUID = 2282950186694419179L; - - public int md5; - public String bucketName; - public String objectKey; - public String versionId; - public String downloadFile; - public ObjectStatus objectStatus; - public TmpFileStatus tmpFileStatus; - ArrayList downloadParts; - public transient volatile boolean isAbort = false; - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((bucketName == null) ? 0 : bucketName.hashCode()); - result = prime * result + ((downloadFile == null) ? 0 : downloadFile.hashCode()); - result = prime * result + ((versionId == null) ? 0 : versionId.hashCode()); - result = prime * result + ((objectKey == null) ? 0 : objectKey.hashCode()); - result = prime * result + ((objectStatus == null) ? 0 : objectStatus.hashCode()); - result = prime * result + ((tmpFileStatus == null) ? 0 : tmpFileStatus.hashCode()); - result = prime * result + ((downloadParts == null) ? 0 : downloadParts.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } else { - if (obj instanceof DownloadCheckPoint) { - DownloadCheckPoint downloadCheckPoint = (DownloadCheckPoint) obj; - if (downloadCheckPoint.hashCode() == this.hashCode()) { - return true; - } - } - } - return false; - } - - /** - * 从checkpoint文件中加载checkpoint数据 - * - * @param checkPointFile - * @throws Exception - */ - public void load(String checkPointFile) throws Exception { - FileInputStream fileIn = null; - SecureObjectInputStream in = null; - try { - fileIn = new FileInputStream(checkPointFile); - in = new SecureObjectInputStream(fileIn); - DownloadCheckPoint info = (DownloadCheckPoint) in.readObject(); - assign(info); - } finally { - ServiceUtils.closeStream(in); - ServiceUtils.closeStream(fileIn); - } - } - - private void assign(DownloadCheckPoint info) { - this.md5 = info.md5; - this.downloadFile = info.downloadFile; - this.bucketName = info.bucketName; - this.objectKey = info.objectKey; - this.versionId = info.versionId; - this.objectStatus = info.objectStatus; - this.tmpFileStatus = info.tmpFileStatus; - this.downloadParts = info.downloadParts; - } - - /** - * 判断序列化文件、临时文件和实际信息是否一致 - * - * @param tmpFilePath - * @param obsClient - * @return - */ - public boolean isValid(String tmpFilePath, ObjectMetadata objectMetadata) { - if (this.md5 != hashCode()) { - return false; - } - if (objectMetadata.getContentLength() != this.objectStatus.size - || !objectMetadata.getLastModified().equals(this.objectStatus.lastModified) - || !objectMetadata.getEtag().equals(this.objectStatus.Etag)) { - return false; - } - - File tmpfile = new File(tmpFilePath); - if (this.tmpFileStatus.size != tmpfile.length()) { - return false; - } - return true; - } - - /** - * 分片下载成功后,更新分片和临时文件信息 - * - * @param index - * @param completed - * @param tmpFilePath - * @throws IOException - */ - public synchronized void update(int index, boolean completed, String tmpFilePath) throws IOException { - downloadParts.get(index).isCompleted = completed; - File tmpfile = new File(tmpFilePath); - this.tmpFileStatus.lastModified = new Date(tmpfile.lastModified()); - } - - /** - * 出现网络异常时,更新临时文件的修改时间 - * - * @param tmpFilePath - * @throws IOException - */ - public synchronized void updateTmpFile(String tmpFilePath) throws IOException { - File tmpfile = new File(tmpFilePath); - this.tmpFileStatus.lastModified = new Date(tmpfile.lastModified()); - } - - /** - * 把DownloadCheckPoint数据写到序列化文件 - * - * @throws IOException - */ - public synchronized void record(String checkPointFilePath) throws IOException { - FileOutputStream fileOutStream = null; - ObjectOutputStream objOutStream = null; - this.md5 = hashCode(); - try { - fileOutStream = new FileOutputStream(checkPointFilePath); - objOutStream = new ObjectOutputStream(fileOutStream); - objOutStream.writeObject(this); - } finally { - if (objOutStream != null) { - try { - objOutStream.close(); - } catch (Exception e) { - } - } - if (fileOutStream != null) { - try { - fileOutStream.close(); - } catch (Exception e) { - } - } - } - } - } - - static class ObjectStatus implements Serializable { - - private static final long serialVersionUID = -6267040832855296342L; - - public long size; // 桶中对象大小 - public Date lastModified; // 对象的最后修改时间 - public String Etag; // 对象的Etag - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((Etag == null) ? 0 : Etag.hashCode()); - result = prime * result + ((lastModified == null) ? 0 : lastModified.hashCode()); - result = prime * result + (int) (size ^ (size >>> 32)); - return result; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } else { - if (obj instanceof ObjectStatus) { - ObjectStatus objectStatus = (ObjectStatus) obj; - if (objectStatus.hashCode() == this.hashCode()) { - return true; - } - } - } - return false; - } - } - - static class TmpFileStatus implements Serializable { - private static final long serialVersionUID = 4478330948103112660L; - - public long size; // 对象大小 - public Date lastModified; // 对象的最后修改时间 - public String tmpFilePath; - - public TmpFileStatus(long size, Date lastMoidified, String tmpFilePath) { - this.size = size; - this.lastModified = lastMoidified; - this.tmpFilePath = tmpFilePath; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((lastModified == null) ? 0 : lastModified.hashCode()); - result = prime * result + ((tmpFilePath == null) ? 0 : tmpFilePath.hashCode()); - result = prime * result + (int) (size ^ (size >>> 32)); - return result; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } else { - if (obj instanceof TmpFileStatus) { - TmpFileStatus tmpFileStatus = (TmpFileStatus) obj; - if (tmpFileStatus.hashCode() == this.hashCode()) { - return true; - } - } - } - return false; - } - } - - static class DownloadPart implements Serializable { - - private static final long serialVersionUID = 961987949814206093L; - - public int partNumber; // 分片序号,从0开始编号 - public long offset; // 分片起始位置 - public long end; // 分片片结束位置 - public boolean isCompleted; // 该分片下载是否完成 - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + partNumber; - result = prime * result + (isCompleted ? 0 : 8); - result = prime * result + (int) (end ^ (end >>> 32)); - result = prime * result + (int) (offset ^ (offset >>> 32)); - return result; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } else { - if (obj instanceof DownloadPart) { - DownloadPart downloadPart = (DownloadPart) obj; - if (downloadPart.hashCode() == this.hashCode()) { - return true; - } - } - } - return false; - } - } - - static class PartResultDown { - private int partNumber; // 分片序号,从1开始编号 - private long start; // 分片开始位置 - private long end; // 分片结束位置 - private boolean isFailed; // 分片上传是否失败 - private Exception exception; // 分片上传异常 - - public PartResultDown(int partNumber, long start, long end) { - this.partNumber = partNumber; - this.start = start; - this.end = end; - } - - /** - * 获取分片的起始位置 - * - * @return 分片的起始位置 - */ - public long getStart() { - return start; - } - - /** - * 设置分片的起始位置 - * - * @param start - * 分片起始位置 - */ - public void setStart(long start) { - this.start = start; - } - - /** - * 获取分片的结束位置 - * - * @return 分片的结束位置 - */ - public long getEnd() { - return end; - } - - /** - * 设置分片的结束位置 - * - * @param end - * 分片结束位置 - */ - public void setEnd(long end) { - this.end = end; - } - - /** - * 获取分片的编号 - * - * @return 分片的编号 - */ - public int getpartNumber() { - return partNumber; - } - - /** - * 获取分片的下载状态 - * - * @return 分片的下载状态 - */ - public boolean isFailed() { - return isFailed; - } - - /** - * 设置分片的下载状态 - * - * @param failed - * 分片的下载状态 - */ - public void setFailed(boolean failed) { - this.isFailed = failed; - } - - /** - * 获取分片的下载异常 - * - * @return 分片的下载异常 - */ - public Exception getException() { - return exception; - } - - /** - * 设置分片的下载异常 - * - * @param exception - * 分片的下载异常 - */ - public void setException(Exception exception) { - this.exception = exception; - } - - } - - static class DownloadResult { - - private List partResults; - - /** - * 获取分片的上传最终结果 - * - * @return 分片的上传汇总结果 - */ - public List getPartResults() { - return partResults; - } - - /** - * 设置分片的上传最终结果 - * - * @param partResults - * 分片的上传汇总结果 - */ - public void setPartResults(List partResults) { - this.partResults = partResults; - } - - - } -} diff --git a/app/src/main/java/com/obs/services/internal/ServiceException.java b/app/src/main/java/com/obs/services/internal/ServiceException.java index 324412d..08865dd 100644 --- a/app/src/main/java/com/obs/services/internal/ServiceException.java +++ b/app/src/main/java/com/obs/services/internal/ServiceException.java @@ -1,9 +1,4 @@ /** - * JetS3t : Java S3 Toolkit - * Project hosted at http://bitbucket.org/jmurty/jets3t/ - * - * Copyright 2006-2010 James Murty - * * Copyright 2019 Huawei Technologies Co.,Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the @@ -16,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; import java.io.IOException; @@ -29,14 +25,14 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import com.jamesmurty.utils.XMLBuilder; import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.internal.xml.OBSXMLBuilder; public class ServiceException extends RuntimeException { private static final long serialVersionUID = -757626557833455141L; - private String xmlMessage ; + private String xmlMessage; private String errorCode; private String errorMessage; @@ -82,29 +78,28 @@ public ServiceException(Throwable cause) { @Override public String toString() { - String myString = super.toString(); - - if (requestVerb != null) { - myString += - " " + requestVerb - + " '" + requestPath + "'" - + (requestHost != null ? " on Host '" + requestHost + "'" : "") - + (responseDate != null ? " @ '" + responseDate + "'" : ""); + StringBuilder myString = new StringBuilder(super.toString()); + + if (this.requestVerb != null) { + myString.append(" ").append(this.requestVerb) + .append(" '").append(this.requestPath).append("'") + .append(this.requestHost != null ? " on Host '" + this.requestHost + "'" : "") + .append(this.responseDate != null ? " @ '" + this.responseDate + "'" : ""); } - if (responseCode != -1) { - myString += - " -- ResponseCode: " + responseCode - + ", ResponseStatus: " + responseStatus; + + if (this.responseCode != -1) { + myString.append(" -- ResponseCode: ").append(this.responseCode) + .append(", ResponseStatus: ").append(this.responseStatus); } if (isParsedFromXmlMessage()) { - myString += ", XML Error Message: " + xmlMessage; + myString.append(", XML Error Message: ").append(this.xmlMessage); } else { - if (errorRequestId != null) { - myString += ", RequestId: " + errorRequestId - + ", HostId: " + errorHostId; + if (this.errorRequestId != null) { + myString.append(", RequestId: ").append(this.errorRequestId) + .append(", HostId: ").append(this.errorHostId); } } - return myString; + return myString.toString(); } private String findXmlElementText(String xmlMessage, String elementName) { @@ -146,7 +141,7 @@ public String getErrorMessage() { } public void setErrorMessage(String message) { - this.errorMessage= message; + this.errorMessage = message; } public String getErrorHostId() { @@ -169,13 +164,12 @@ public String getXmlMessage() { return xmlMessage; } - public XMLBuilder getXmlMessageAsBuilder() - throws IOException, ParserConfigurationException, SAXException - { + public OBSXMLBuilder getXmlMessageAsBuilder() + throws IOException, ParserConfigurationException, SAXException { if (this.xmlMessage == null) { return null; } - XMLBuilder builder = XMLBuilder.parse( + OBSXMLBuilder builder = OBSXMLBuilder.parse( new InputSource(new StringReader(this.xmlMessage))); return builder; } @@ -245,12 +239,12 @@ public void setResponseHeaders(Map responseHeaders) { this.responseHeaders = responseHeaders; } - public String getErrorIndicator() { - return errorIndicator; - } + public String getErrorIndicator() { + return errorIndicator; + } - public void setErrorIndicator(String errorIndicator) { - this.errorIndicator = errorIndicator; - } + public void setErrorIndicator(String errorIndicator) { + this.errorIndicator = errorIndicator; + } } diff --git a/app/src/main/java/com/obs/services/internal/SimpleProgressManager.java b/app/src/main/java/com/obs/services/internal/SimpleProgressManager.java index 53f3920..bce28e3 100644 --- a/app/src/main/java/com/obs/services/internal/SimpleProgressManager.java +++ b/app/src/main/java/com/obs/services/internal/SimpleProgressManager.java @@ -3,59 +3,66 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services.internal; -import java.util.Date; -import java.util.List; +package com.obs.services.internal; import com.obs.services.model.ProgressListener; import com.obs.services.model.ProgressStatus; -public class SimpleProgressManager extends ProgressManager{ - - protected long transferredBytes; - protected long newlyTransferredBytes; - - public SimpleProgressManager(long totalBytes, long transferredBytes, - ProgressListener progressListener, long intervalBytes) { - super(totalBytes, progressListener, intervalBytes); - this.transferredBytes = transferredBytes < 0 ? 0 : transferredBytes; - } - - @Override - protected void doProgressChanged(int bytes) { - this.transferredBytes += bytes; - this.newlyTransferredBytes += bytes; - Date now = new Date(); - List currentInstantaneousBytes = this.createCurrentInstantaneousBytes(bytes, now); - this.lastInstantaneousBytes = currentInstantaneousBytes; - if(this.newlyTransferredBytes >= this.intervalBytes && (this.transferredBytes < this.totalBytes || this.totalBytes == -1)) { - DefaultProgressStatus status = new DefaultProgressStatus(this.newlyTransferredBytes, this.transferredBytes, - this.totalBytes, now.getTime() - this.lastCheckpoint.getTime(), now.getTime() - this.startCheckpoint.getTime()); - status.setInstantaneousBytes(currentInstantaneousBytes); - this.progressListener.progressChanged(status); - this.newlyTransferredBytes = 0; - this.lastCheckpoint = now; - } - } - - @Override - public void progressEnd() { - if(this.progressListener == null) { - return; - } - Date now = new Date(); - ProgressStatus status = new DefaultProgressStatus(this.newlyTransferredBytes, this.transferredBytes, - this.totalBytes, now.getTime() - this.lastCheckpoint.getTime(), now.getTime() - this.startCheckpoint.getTime()); +public class SimpleProgressManager extends ProgressManager { + + protected long transferredBytes; + protected long newlyTransferredBytes; + protected long lastSecondBytes = -1; + protected long currentSecondBytes; + protected long lastSwapTimeStamp = System.currentTimeMillis(); + + public SimpleProgressManager(long totalBytes, long transferredBytes, ProgressListener progressListener, + long intervalBytes) { + super(totalBytes, progressListener, intervalBytes); + this.transferredBytes = transferredBytes < 0 ? 0 : transferredBytes; + } + + @Override + protected void doProgressChanged(int bytes) { + this.transferredBytes += bytes; + this.newlyTransferredBytes += bytes; + long now = System.currentTimeMillis(); + long swapIntervalTime = now - lastSwapTimeStamp; + currentSecondBytes += bytes; + if (swapIntervalTime > 1000) { + lastSecondBytes = (long) (currentSecondBytes / (swapIntervalTime / 1000.0)); + currentSecondBytes = 0; + lastSwapTimeStamp = now; + } + if (this.newlyTransferredBytes >= this.intervalBytes + && (this.transferredBytes < this.totalBytes || this.totalBytes == -1)) { + DefaultProgressStatus status = new DefaultProgressStatus(this.newlyTransferredBytes, this.transferredBytes, + this.totalBytes, now - this.lastCheckpoint, now - this.startCheckpoint); + status.setInstantaneousSpeed(lastSecondBytes); + this.progressListener.progressChanged(status); + this.newlyTransferredBytes = 0; + this.lastCheckpoint = now; + } + } + + @Override + public void progressEnd() { + if (this.progressListener == null) { + return; + } + long now = System.currentTimeMillis(); + ProgressStatus status = new DefaultProgressStatus(this.newlyTransferredBytes, this.transferredBytes, + this.totalBytes, now - this.lastCheckpoint, now - this.startCheckpoint); this.progressListener.progressChanged(status); - } + } } diff --git a/app/src/main/java/com/obs/services/internal/UploadResumableClient.java b/app/src/main/java/com/obs/services/internal/UploadResumableClient.java new file mode 100644 index 0000000..f9d89aa --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/UploadResumableClient.java @@ -0,0 +1,735 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.internal; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.AbstractClient; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.io.ProgressInputStream; +import com.obs.services.internal.utils.SecureObjectInputStream; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AbortMultipartUploadRequest; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.CompleteMultipartUploadResult; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.PartEtag; +import com.obs.services.model.UploadFileRequest; +import com.obs.services.model.UploadPartRequest; +import com.obs.services.model.UploadPartResult; + +public class UploadResumableClient { + + private static final ILogger log = LoggerBuilder.getLogger("com.obs.services.ObsClient"); + private AbstractClient obsClient; + + public UploadResumableClient(AbstractClient obsClient) { + this.obsClient = obsClient; + } + + public CompleteMultipartUploadResult uploadFileResume(UploadFileRequest uploadFileRequest) { + ServiceUtils.assertParameterNotNull(uploadFileRequest, "UploadFileRequest is null"); + ServiceUtils.assertParameterNotNull(uploadFileRequest.getBucketName(), "bucketName is null"); + ServiceUtils.assertParameterNotNull2(uploadFileRequest.getObjectKey(), "objectKey is null"); + ServiceUtils.assertParameterNotNull(uploadFileRequest.getUploadFile(), "uploadfile is null"); + if (uploadFileRequest.isEnableCheckpoint()) { + if (!ServiceUtils.isValid(uploadFileRequest.getCheckpointFile())) { + uploadFileRequest.setCheckpointFile(uploadFileRequest.getUploadFile() + ".uploadFile_record"); + } + } + if (uploadFileRequest.getCallback() != null) { + ServiceUtils.assertParameterNotNull(uploadFileRequest.getCallback().getCallbackUrl(), + "callbackUrl is null"); + ServiceUtils.assertParameterNotNull(uploadFileRequest.getCallback().getCallbackBody(), + "callbackBody is null"); + } + try { + return uploadFileCheckPoint(uploadFileRequest); + } catch (ObsException e) { + throw e; + } catch (ServiceException e) { + throw ServiceUtils.changeFromServiceException(e); + } catch (Exception e) { + throw new ObsException(e.getMessage(), e); + } + } + + protected void abortMultipartUploadSilent(String uploadId, String bucketName, String objectKey, + boolean isRequesterPays) { + try { + abortMultipartUpload(uploadId, bucketName, objectKey, isRequesterPays); + } catch (Exception e) { + if (log.isWarnEnabled()) { + log.warn("Abort multipart upload failed", e); + } + } + } + + protected HeaderResponse abortMultipartUpload(String uploadId, String bucketName, String objectKey, + boolean isRequesterPays) { + AbortMultipartUploadRequest request = new AbortMultipartUploadRequest(bucketName, objectKey, uploadId); + request.setRequesterPays(isRequesterPays); + return this.obsClient.abortMultipartUpload(request); + } + + private CompleteMultipartUploadResult uploadFileCheckPoint(UploadFileRequest uploadFileRequest) throws Exception { + UploadCheckPoint uploadCheckPoint = new UploadCheckPoint(); + if (uploadFileRequest.isEnableCheckpoint()) { + prepareWithCheckpoint(uploadFileRequest, uploadCheckPoint); + } else { + prepare(uploadFileRequest, uploadCheckPoint); + } + + // 开始上传 + List partResults = uploadfile(uploadFileRequest, uploadCheckPoint); + + // 有错误抛出异常,无异常则合并多段 + for (PartResult partResult : partResults) { + if (partResult.isFailed() && partResult.getException() != null) { + // 未开启,取消多段 + if (!uploadFileRequest.isEnableCheckpoint()) { + this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadFileRequest.getBucketName(), + uploadFileRequest.getObjectKey(), uploadFileRequest.isRequesterPays()); + } else if (uploadCheckPoint.isAbort) { + this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadFileRequest.getBucketName(), + uploadFileRequest.getObjectKey(), uploadFileRequest.isRequesterPays()); + ServiceUtils.deleteFileIgnoreException(uploadFileRequest.getCheckpointFile()); + } + throw partResult.getException(); + } + } + + // 合并多段 + CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest( + uploadFileRequest.getBucketName(), uploadFileRequest.getObjectKey(), uploadCheckPoint.uploadID, + uploadCheckPoint.partEtags); + completeMultipartUploadRequest.setRequesterPays(uploadFileRequest.isRequesterPays()); + completeMultipartUploadRequest.setEncodingType(uploadFileRequest.getEncodingType()); + completeMultipartUploadRequest.setIsIgnorePort(uploadFileRequest.getIsIgnorePort()); + completeMultipartUploadRequest.setCallback(uploadFileRequest.getCallback()); + + try { + CompleteMultipartUploadResult result = this.obsClient + .completeMultipartUpload(completeMultipartUploadRequest); + if (uploadFileRequest.isEnableCheckpoint()) { + ServiceUtils.deleteFileIgnoreException(uploadFileRequest.getCheckpointFile()); + } + return result; + } catch (ObsException e) { + if (!uploadFileRequest.isEnableCheckpoint()) { + this.abortMultipartUpload(uploadCheckPoint.uploadID, uploadFileRequest.getBucketName(), + uploadFileRequest.getObjectKey(), uploadFileRequest.isRequesterPays()); + } else { + if (e.getResponseCode() >= 300 && e.getResponseCode() < 500 && e.getResponseCode() != 408) { + this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadFileRequest.getBucketName(), + uploadFileRequest.getObjectKey(), uploadFileRequest.isRequesterPays()); + ServiceUtils.deleteFileIgnoreException(uploadFileRequest.getCheckpointFile()); + } + } + throw e; + } + } + + private void prepareWithCheckpoint(UploadFileRequest uploadFileRequest, UploadCheckPoint uploadCheckPoint) + throws IOException, Exception { + boolean needRecreate = false; + try { + uploadCheckPoint.load(uploadFileRequest.getCheckpointFile()); + } catch (Exception e) { + needRecreate = true; + } + + if (!needRecreate) { + if (!(uploadFileRequest.getBucketName().equals(uploadCheckPoint.bucketName) + && uploadFileRequest.getObjectKey().equals(uploadCheckPoint.objectKey) + && uploadFileRequest.getUploadFile().equals(uploadCheckPoint.uploadFile))) { + needRecreate = true; + } else if (!uploadCheckPoint.isValid(uploadFileRequest.getUploadFile())) { + needRecreate = true; + } + } + + if (needRecreate) { + if (uploadCheckPoint.bucketName != null && uploadCheckPoint.objectKey != null + && uploadCheckPoint.uploadID != null) { + this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadCheckPoint.bucketName, + uploadCheckPoint.objectKey, uploadFileRequest.isRequesterPays()); + } + ServiceUtils.deleteFileIgnoreException(uploadFileRequest.getCheckpointFile()); + prepare(uploadFileRequest, uploadCheckPoint); + } + } + + private List uploadfile(UploadFileRequest uploadFileRequest, UploadCheckPoint uploadCheckPoint) + throws Exception { + ArrayList pieceResults = new ArrayList(); + ExecutorService executorService = Executors.newFixedThreadPool(uploadFileRequest.getTaskNum()); + ArrayList> futures = new ArrayList>(); + + ProgressManager progressManager = null; + if (uploadFileRequest.getProgressListener() == null) { + for (int i = 0; i < uploadCheckPoint.uploadParts.size(); i++) { + UploadPart uploadPart = uploadCheckPoint.uploadParts.get(i); + if (uploadPart.isCompleted) { + PartResult pr = new PartResult(uploadPart.partNumber, uploadPart.offset, uploadPart.size); + pr.setFailed(false); + pieceResults.add(pr); + } else { + futures.add(executorService + .submit(new Mission(i, uploadCheckPoint, i, uploadFileRequest, this.obsClient))); + } + } + } else { + long transferredBytes = 0L; + List unfinishedUploadMissions = new LinkedList(); + for (int i = 0; i < uploadCheckPoint.uploadParts.size(); i++) { + UploadPart uploadPart = uploadCheckPoint.uploadParts.get(i); + if (uploadPart.isCompleted) { + PartResult pr = new PartResult(uploadPart.partNumber, uploadPart.offset, uploadPart.size); + pr.setFailed(false); + pieceResults.add(pr); + transferredBytes += uploadPart.size; + } else { + unfinishedUploadMissions + .add(new Mission(i, uploadCheckPoint, i, uploadFileRequest, this.obsClient)); + } + } + progressManager = new ConcurrentProgressManager(uploadCheckPoint.uploadFileStatus.size, transferredBytes, + uploadFileRequest.getProgressListener(), uploadFileRequest.getProgressInterval() > 0 + ? uploadFileRequest.getProgressInterval() : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); + for (Mission mission : unfinishedUploadMissions) { + mission.setProgressManager(progressManager); + futures.add(executorService.submit(mission)); + } + } + + executorService.shutdown(); + executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); + + for (Future future : futures) { + try { + PartResult tr = future.get(); + pieceResults.add(tr); + } catch (ExecutionException e) { + if (!uploadFileRequest.isEnableCheckpoint()) { + this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadFileRequest.getBucketName(), + uploadFileRequest.getObjectKey(), uploadFileRequest.isRequesterPays()); + } + throw e; + } + } + + if (progressManager != null) { + progressManager.progressEnd(); + } + + return pieceResults; + } + + static class Mission implements Callable { + + private int id; + private UploadCheckPoint uploadCheckPoint; + private int partIndex; + private UploadFileRequest uploadFileRequest; + private AbstractClient obsClient; + private ProgressManager progressManager; + + public Mission(int id, UploadCheckPoint uploadCheckPoint, int partIndex, UploadFileRequest uploadFileRequest, + AbstractClient obsClient) { + this.id = id; + this.uploadCheckPoint = uploadCheckPoint; + this.partIndex = partIndex; + this.uploadFileRequest = uploadFileRequest; + this.obsClient = obsClient; + } + + @Override + public PartResult call() throws Exception { + PartResult tr = null; + UploadPart uploadPart = uploadCheckPoint.uploadParts.get(partIndex); + tr = new PartResult(partIndex + 1, uploadPart.offset, uploadPart.size); + if (!uploadCheckPoint.isAbort) { + InputStream input = null; + try { + UploadPartRequest uploadPartRequest = new UploadPartRequest(); + uploadPartRequest.setBucketName(uploadFileRequest.getBucketName()); + uploadPartRequest.setObjectKey(uploadFileRequest.getObjectKey()); + uploadPartRequest.setUploadId(uploadCheckPoint.uploadID); + uploadPartRequest.setPartSize(uploadPart.size); + uploadPartRequest.setPartNumber(uploadPart.partNumber); + uploadPartRequest.setRequesterPays(uploadFileRequest.isRequesterPays()); + + if (this.progressManager == null) { + uploadPartRequest.setFile(new File(uploadFileRequest.getUploadFile())); + uploadPartRequest.setOffset(uploadPart.offset); + } else { + input = new FileInputStream(uploadFileRequest.getUploadFile()); + long offset = uploadPart.offset; + long skipByte = input.skip(offset); + if (offset < skipByte) { + log.error(String.format( + "The actual number of skipped bytes (%d) is less than expected (%d): ", skipByte, + offset)); + } + // TODO no md5 + uploadPartRequest.setInput(new ProgressInputStream(input, this.progressManager, false)); + } + + UploadPartResult result = obsClient.uploadPart(uploadPartRequest); + + PartEtag partEtag = new PartEtag(result.getEtag(), result.getPartNumber()); + uploadCheckPoint.update(partIndex, partEtag, true); + tr.setFailed(false); + + if (uploadFileRequest.isEnableCheckpoint()) { + uploadCheckPoint.record(uploadFileRequest.getCheckpointFile()); + } + + } catch (ObsException e) { + if (e.getResponseCode() >= 300 && e.getResponseCode() < 500 && e.getResponseCode() != 408) { + uploadCheckPoint.isAbort = true; + } + // 有异常打印到日志文件 + tr.setFailed(true); + tr.setException(e); + if (log.isErrorEnabled()) { + log.error(String.format("Task %d:%s upload part %d failed: ", id, "upload" + id, partIndex + 1), + e); + } + } catch (Exception e) { + tr.setFailed(true); + tr.setException(e); + if (log.isErrorEnabled()) { + log.error(String.format("Task %d:%s upload part %d failed: ", id, "upload" + id, partIndex + 1), + e); + } + } finally { + if (null != input) { + input.close(); + } + } + } else { + tr.setFailed(true); + } + return tr; + } + + public void setProgressManager(ProgressManager progressManager) { + this.progressManager = progressManager; + } + + } + + private void prepare(UploadFileRequest uploadFileRequest, UploadCheckPoint uploadCheckPoint) throws Exception { + uploadCheckPoint.uploadFile = uploadFileRequest.getUploadFile(); + uploadCheckPoint.bucketName = uploadFileRequest.getBucketName(); + uploadCheckPoint.objectKey = uploadFileRequest.getObjectKey(); + uploadCheckPoint.uploadFileStatus = FileStatus.getFileStatus(uploadCheckPoint.uploadFile, + uploadFileRequest.isEnableCheckSum()); + uploadCheckPoint.uploadParts = splitUploadFile(uploadCheckPoint.uploadFileStatus.size, + uploadFileRequest.getPartSize()); + uploadCheckPoint.partEtags = new ArrayList(); + + InitiateMultipartUploadRequest initiateUploadRequest = new InitiateMultipartUploadRequest( + uploadFileRequest.getBucketName(), uploadFileRequest.getObjectKey()); + + initiateUploadRequest.setExtensionPermissionMap(uploadFileRequest.getExtensionPermissionMap()); + initiateUploadRequest.setAcl(uploadFileRequest.getAcl()); + initiateUploadRequest.setSuccessRedirectLocation(uploadFileRequest.getSuccessRedirectLocation()); + initiateUploadRequest.setSseCHeader(uploadFileRequest.getSseCHeader()); + initiateUploadRequest.setSseKmsHeader(uploadFileRequest.getSseKmsHeader()); + initiateUploadRequest.setMetadata(uploadFileRequest.getObjectMetadata()); + initiateUploadRequest.setRequesterPays(uploadFileRequest.isRequesterPays()); + initiateUploadRequest.setEncodingType(uploadFileRequest.getEncodingType()); + initiateUploadRequest.setIsEncodeHeaders(uploadFileRequest.isEncodeHeaders()); + + InitiateMultipartUploadResult initiateUploadResult = this.obsClient + .initiateMultipartUpload(initiateUploadRequest); + uploadCheckPoint.uploadID = initiateUploadResult.getUploadId(); + if (uploadFileRequest.isEnableCheckpoint()) { + try { + uploadCheckPoint.record(uploadFileRequest.getCheckpointFile()); + } catch (Exception e) { + this.abortMultipartUploadSilent(uploadCheckPoint.uploadID, uploadCheckPoint.bucketName, + uploadCheckPoint.objectKey, uploadFileRequest.isRequesterPays()); + throw e; + } + } + } + + private ArrayList splitUploadFile(long size, long partSize) { + ArrayList parts = new ArrayList(); + + long partNum = size / partSize; + if (partNum >= 10000) { + partSize = size % 10000 == 0 ? size / 10000 : size / 10000 + 1; + partNum = size / partSize; + } + if (size % partSize > 0) { + partNum++; + } + if (partNum == 0) { + UploadPart part = new UploadPart(); + part.partNumber = 1; + part.offset = 0; + part.size = 0; + part.isCompleted = false; + parts.add(part); + } else { + for (long i = 0; i < partNum; i++) { + UploadPart part = new UploadPart(); + part.partNumber = (int) (i + 1); + part.offset = i * partSize; + part.size = partSize; + part.isCompleted = false; + parts.add(part); + } + if (size % partSize > 0) { + parts.get(parts.size() - 1).size = size % partSize; + } + } + + return parts; + } + + /** + * 断点续传上传所需类 + */ + static class UploadCheckPoint implements Serializable { + + private static final long serialVersionUID = 5564757792864743464L; + + /** + * 从checkpoint文件中加载checkpoint数据 + * + * @param checkPointFile + * @throws Exception + */ + public void load(String checkPointFile) throws Exception { + FileInputStream fileInput = null; + SecureObjectInputStream in = null; + try { + fileInput = new FileInputStream(checkPointFile); + in = new SecureObjectInputStream(fileInput); + UploadCheckPoint tmp = (UploadCheckPoint) in.readObject(); + assign(tmp); + } finally { + if (null != in) { + try { + in.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + + if (null != fileInput) { + try { + fileInput.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + } + } + + /** + * 把checkpoint数据写到checkpoint文件 + * + * @param checkPointFile + * @throws IOException + */ + public synchronized void record(String checkPointFile) throws IOException { + this.md5 = hashCode(); + FileOutputStream fileOutput = null; + ObjectOutputStream outStream = null; + try { + fileOutput = new FileOutputStream(checkPointFile); + outStream = new ObjectOutputStream(fileOutput); + outStream.writeObject(this); + } finally { + if (null != outStream) { + try { + outStream.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + + if (null != fileOutput) { + try { + fileOutput.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + } + } + + /** + * 分块上传完成,更新状态 + * + * @param partIndex + * @param partETag + * @param completed + */ + public synchronized void update(int partIndex, PartEtag partETag, boolean completed) { + partEtags.add(partETag); + uploadParts.get(partIndex).isCompleted = completed; + } + + /** + * 判读本地文件与checkpoint中记录的信息是否相符合,校验一致性 + * + * @param uploadFile + * @return boolean + * @throws IOException + */ + public boolean isValid(String uploadFile) throws IOException { + if (this.md5 != hashCode()) { + return false; + } + + File upload = new File(uploadFile); + if (!this.uploadFile.equals(uploadFile) || this.uploadFileStatus.size != upload.length() + || this.uploadFileStatus.lastModified != upload.lastModified()) { + return false; + } + + if (this.uploadFileStatus.checkSum != null) { + try { + return this.uploadFileStatus.checkSum + .equals(ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(new FileInputStream(upload)))); + } catch (NoSuchAlgorithmException e) { + throw new ObsException("computeMD5Hash failed.", e); + } + } + + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((objectKey == null) ? 0 : objectKey.hashCode()); + result = prime * result + ((bucketName == null) ? 0 : bucketName.hashCode()); + result = prime * result + ((partEtags == null) ? 0 : partEtags.hashCode()); + result = prime * result + ((uploadFile == null) ? 0 : uploadFile.hashCode()); + result = prime * result + ((uploadFileStatus == null) ? 0 : uploadFileStatus.hashCode()); + result = prime * result + ((uploadID == null) ? 0 : uploadID.hashCode()); + result = prime * result + ((uploadParts == null) ? 0 : uploadParts.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } else { + if (obj instanceof UploadCheckPoint) { + UploadCheckPoint uploadCheckPoint = (UploadCheckPoint) obj; + return uploadCheckPoint.hashCode() == obj.hashCode(); + } + } + return false; + } + + private void assign(UploadCheckPoint tmp) { + this.md5 = tmp.md5; + this.bucketName = tmp.bucketName; + this.uploadFile = tmp.uploadFile; + this.uploadFileStatus = tmp.uploadFileStatus; + this.objectKey = tmp.objectKey; + this.uploadID = tmp.uploadID; + this.uploadParts = tmp.uploadParts; + this.partEtags = tmp.partEtags; + } + + public int md5; + public String uploadFile; + public FileStatus uploadFileStatus; + public String bucketName; + public String objectKey; + public String uploadID; + public ArrayList uploadParts; + public ArrayList partEtags; + public transient volatile boolean isAbort = false; + + } + + static class FileStatus implements Serializable { + private static final long serialVersionUID = -3135754191745936521L; + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((checkSum == null) ? 0 : checkSum.hashCode()); + result = prime * result + (int) (lastModified ^ (lastModified >>> 32)); + result = prime * result + (int) (size ^ (size >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } else { + if (obj instanceof FileStatus) { + FileStatus fileStatus = (FileStatus) obj; + return fileStatus.hashCode() == obj.hashCode(); + } + } + return false; + } + + public static FileStatus getFileStatus(String uploadFile, boolean checkSum) throws IOException { + FileStatus fileStatus = new FileStatus(); + File file = new File(uploadFile); + fileStatus.size = file.length(); + fileStatus.lastModified = file.lastModified(); + if (checkSum) { + try { + fileStatus.checkSum = ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(new FileInputStream(file))); + } catch (NoSuchAlgorithmException e) { + throw new ObsException("computeMD5Hash failed.", e); + } + } + return fileStatus; + } + + public long size; // 文件大小 + public long lastModified; // 文件最后修改时间 + public String checkSum; // 文件checkSum + } + + static class UploadPart implements Serializable { + + private static final long serialVersionUID = 751520598820222785L; + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (isCompleted ? 1 : 0); + result = prime * result + partNumber; + result = prime * result + (int) (offset ^ (offset >>> 32)); + result = prime * result + (int) (size ^ (size >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } else { + if (obj instanceof UploadPart) { + UploadPart uploadPart = (UploadPart) obj; + return uploadPart.hashCode() == obj.hashCode(); + } + } + return false; + } + + public int partNumber; // 分片序号 + public long offset; // 分片在文件中的偏移量 + public long size; // 分片大小 + public boolean isCompleted; // 该分片上传是否完成 + } + + static class PartResult { + + public PartResult(int partNumber, long offset, long length) { + this.partNumber = partNumber; + this.offset = offset; + this.length = length; + } + + public int getpartNumber() { + return partNumber; + } + + public void setpartNumber(int partNumber) { + this.partNumber = partNumber; + } + + public long getOffset() { + return offset; + } + + public void setOffset(long offset) { + this.offset = offset; + } + + public long getLength() { + return length; + } + + public void setLength(long length) { + this.length = length; + } + + public boolean isFailed() { + return isFailed; + } + + public void setFailed(boolean isFailed) { + this.isFailed = isFailed; + } + + public Exception getException() { + return exception; + } + + public void setException(Exception exception) { + this.exception = exception; + } + + private int partNumber; // 分片序号 + private long offset; // 分片在文件中的偏移 + private long length; // 分片长度 + private boolean isFailed; // 分片上传是否失败 + private Exception exception; // 分片上传异常 + } +} diff --git a/app/src/main/java/com/obs/services/internal/V2BucketConvertor.java b/app/src/main/java/com/obs/services/internal/V2BucketConvertor.java new file mode 100644 index 0000000..137c082 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/V2BucketConvertor.java @@ -0,0 +1,308 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal; + +import javax.xml.parsers.FactoryConfigurationError; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.internal.xml.OBSXMLBuilder; +import com.obs.services.model.AbstractNotification; +import com.obs.services.model.BucketCors; +import com.obs.services.model.BucketCorsRule; +import com.obs.services.model.BucketDirectColdAccess; +import com.obs.services.model.BucketEncryption; +import com.obs.services.model.BucketLoggingConfiguration; +import com.obs.services.model.BucketNotificationConfiguration; +import com.obs.services.model.BucketQuota; +import com.obs.services.model.BucketTagInfo; +import com.obs.services.model.CanonicalGrantee; +import com.obs.services.model.EventTypeEnum; +import com.obs.services.model.FunctionGraphConfiguration; +import com.obs.services.model.GrantAndPermission; +import com.obs.services.model.GranteeInterface; +import com.obs.services.model.GroupGrantee; +import com.obs.services.model.Permission; +import com.obs.services.model.SSEAlgorithmEnum; +import com.obs.services.model.TopicConfiguration; +import com.obs.services.model.fs.FSStatusEnum; + +public abstract class V2BucketConvertor implements IConvertor { + + @Override + public String transBucketQuota(BucketQuota quota) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("Quota").elem("StorageQuota") + .text(String.valueOf(quota.getBucketQuota())).up(); + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for storageQuota", e); + } + } + + @Override + public String transBucketLoction(String location) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("CreateBucketConfiguration").elem("LocationConstraint") + .text(ServiceUtils.toValid(location)); + return builder.asString(); + } catch (Exception e) { + throw new ServiceException(e); + } + } + + @Override + public String transBucketEcryption(BucketEncryption encryption) throws ServiceException { + String algorithm = encryption.getSseAlgorithm().getCode(); + String kmsKeyId = ""; + if (algorithm.equals(SSEAlgorithmEnum.KMS.getCode())) { + algorithm = "aws:" + algorithm; + kmsKeyId = encryption.getKmsKeyId(); + } + return transBucketEcryptionXML(algorithm, kmsKeyId); + } + + protected String transBucketEcryptionXML(String algorithm, String kmsKeyId) throws FactoryConfigurationError { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("ServerSideEncryptionConfiguration").e("Rule") + .e("ApplyServerSideEncryptionByDefault"); + builder.e("SSEAlgorithm").t(algorithm); + if (ServiceUtils.isValid(kmsKeyId)) { + builder.e("KMSMasterKeyID").t(kmsKeyId); + } + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for bucketEncryption", e); + } + } + + @Override + public String transBucketLoggingConfiguration(BucketLoggingConfiguration c) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("BucketLoggingStatus"); + if (c.isLoggingEnabled()) { + OBSXMLBuilder enabledBuilder = builder.elem("LoggingEnabled"); + if (c.getTargetBucketName() != null) { + enabledBuilder.elem("TargetBucket").text(ServiceUtils.toValid(c.getTargetBucketName())); + } + + if (c.getLogfilePrefix() != null) { + enabledBuilder.elem("TargetPrefix").text(ServiceUtils.toValid(c.getLogfilePrefix())); + } + GrantAndPermission[] grants = c.getTargetGrants(); + if (grants.length > 0) { + OBSXMLBuilder grantsBuilder = enabledBuilder.elem("TargetGrants"); + transGrantsBuilder(grants, grantsBuilder); + } + } + return builder.asString(); + } catch (ParserConfigurationException e) { + throw new ServiceException("Failed to build XML document for BucketLoggingConfiguration", e); + } catch (TransformerException e) { + throw new ServiceException("Failed to build XML document for BucketLoggingConfiguration", e); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for BucketLoggingConfiguration", e); + } + } + + private void transGrantsBuilder(GrantAndPermission[] grants, OBSXMLBuilder grantsBuilder) + throws ParserConfigurationException, FactoryConfigurationError { + for (GrantAndPermission gap : grants) { + GranteeInterface grantee = gap.getGrantee(); + Permission permission = gap.getPermission(); + if (permission != null) { + OBSXMLBuilder subBuilder = null; + if (grantee instanceof CanonicalGrantee) { + subBuilder = buildCanonicalGrantee(grantee); + } else if (grantee instanceof GroupGrantee) { + subBuilder = buildGroupGrantee(grantee); + } + + if (subBuilder != null) { + grantsBuilder.elem("Grant").importXMLBuilder(subBuilder).elem("Permission") + .text(ServiceUtils.toValid(permission.getPermissionString())); + } + } + } + } + + protected OBSXMLBuilder buildGroupGrantee(GranteeInterface grantee) + throws ParserConfigurationException, FactoryConfigurationError { + OBSXMLBuilder subBuilder; + subBuilder = OBSXMLBuilder.create("Grantee") + .attr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") + .attr("xsi:type", "Group").element("URI") + .text(this.transGroupGrantee(((GroupGrantee) grantee).getGroupGranteeType())); + return subBuilder; + } + + protected OBSXMLBuilder buildCanonicalGrantee(GranteeInterface grantee) + throws ParserConfigurationException, FactoryConfigurationError { + OBSXMLBuilder subBuilder = null; + subBuilder = OBSXMLBuilder.create("Grantee") + .attr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") + .attr("xsi:type", "CanonicalUser").element("ID") + .text(ServiceUtils.toValid(grantee.getIdentifier())); + String displayName = ((CanonicalGrantee) grantee).getDisplayName(); + if (ServiceUtils.isValid2(displayName)) { + subBuilder.up().element("DisplayName").text(displayName); + } + return subBuilder; + } + + @Override + public String transBucketCors(BucketCors cors) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("CORSConfiguration"); + for (BucketCorsRule rule : cors.getRules()) { + builder = builder.e("CORSRule"); + if (rule.getId() != null) { + builder.e("ID").t(rule.getId()); + } + if (rule.getAllowedMethod() != null) { + for (String method : rule.getAllowedMethod()) { + builder.e("AllowedMethod").t(ServiceUtils.toValid(method)); + } + } + + if (rule.getAllowedOrigin() != null) { + for (String origin : rule.getAllowedOrigin()) { + builder.e("AllowedOrigin").t(ServiceUtils.toValid(origin)); + } + } + + if (rule.getAllowedHeader() != null) { + for (String header : rule.getAllowedHeader()) { + builder.e("AllowedHeader").t(ServiceUtils.toValid(header)); + } + } + builder.e("MaxAgeSeconds").t(String.valueOf(rule.getMaxAgeSecond())); + if (rule.getExposeHeader() != null) { + for (String exposeHeader : rule.getExposeHeader()) { + builder.e("ExposeHeader").t(ServiceUtils.toValid(exposeHeader)); + } + } + builder = builder.up(); + } + return builder.asString(); + } catch (ParserConfigurationException e) { + throw new ServiceException("Failed to build XML document for cors", e); + } catch (TransformerException e) { + throw new ServiceException("Failed to build XML document for cors", e); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for cors", e); + } + } + + @Override + public String transBucketTagInfo(BucketTagInfo bucketTagInfo) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("Tagging").e("TagSet"); + for (BucketTagInfo.TagSet.Tag tag : bucketTagInfo.getTagSet().getTags()) { + if (tag != null) { + builder.e("Tag").e("Key").t(ServiceUtils.toValid(tag.getKey())).up().e("Value") + .t(ServiceUtils.toValid(tag.getValue())); + } + } + return builder.up().asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for Tagging", e); + } + } + + @Override + public String transBucketNotificationConfiguration(BucketNotificationConfiguration bucketNotificationConfiguration) + throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("NotificationConfiguration"); + if (bucketNotificationConfiguration == null) { + return builder.asString(); + } + + for (TopicConfiguration config : bucketNotificationConfiguration.getTopicConfigurations()) { + packNotificationConfig(builder, config, "TopicConfiguration", "Topic", "S3Key"); + } + + for (FunctionGraphConfiguration config : bucketNotificationConfiguration.getFunctionGraphConfigurations()) { + packNotificationConfig(builder, config, "FunctionGraphConfiguration", "FunctionGraph", "S3Key"); + } + + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for Notification", e); + } + } + + protected void packNotificationConfig(OBSXMLBuilder builder, AbstractNotification config, String configType, + String urnType, String adapter) { + builder = builder.e(configType); + if (config.getId() != null) { + builder.e("Id").t(config.getId()); + } + if (config.getFilter() != null && !config.getFilter().getFilterRules().isEmpty()) { + builder = builder.e("Filter").e(adapter); + for (AbstractNotification.Filter.FilterRule rule : config.getFilter().getFilterRules()) { + if (rule != null) { + builder.e("FilterRule").e("Name").t(ServiceUtils.toValid(rule.getName())).up().e("Value") + .t(ServiceUtils.toValid(rule.getValue())); + } + } + builder = builder.up().up(); + } + String urn = null; + if (config instanceof TopicConfiguration) { + urn = ((TopicConfiguration) config).getTopic(); + } + if (config instanceof FunctionGraphConfiguration) { + urn = ((FunctionGraphConfiguration) config).getFunctionGraph(); + } + if (urn != null) { + builder.e(urnType).t(urn); + } + + if (config.getEventTypes() != null) { + for (EventTypeEnum event : config.getEventTypes()) { + if (event != null) { + builder.e("Event").t(this.transEventType(event)); + } + } + } + } + + @Override + public String transBucketFileInterface(FSStatusEnum status) throws ServiceException { + try { + return OBSXMLBuilder.create("FileInterfaceConfiguration").e("Status").t(status.getCode()).up().asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for FileInterface", e); + } + } + + @Override + public String transBucketDirectColdAccess(BucketDirectColdAccess access) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("DirectColdAccessConfiguration"); + + builder = builder.e("Status").t(access.getStatus().getCode()); + builder = builder.up(); + + return builder.up().asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for Tagging", e); + } + } +} diff --git a/app/src/main/java/com/obs/services/internal/V2Convertor.java b/app/src/main/java/com/obs/services/internal/V2Convertor.java index 47c346d..b1e765c 100644 --- a/app/src/main/java/com/obs/services/internal/V2Convertor.java +++ b/app/src/main/java/com/obs/services/internal/V2Convertor.java @@ -11,28 +11,24 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.util.Collections; import java.util.Comparator; import java.util.List; -import com.jamesmurty.utils.XMLBuilder; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + import com.obs.services.internal.utils.ServiceUtils; -import com.obs.services.model.AbstractNotification; +import com.obs.services.internal.xml.OBSXMLBuilder; import com.obs.services.model.AccessControlList; -import com.obs.services.model.BucketCors; -import com.obs.services.model.BucketCorsRule; -import com.obs.services.model.BucketDirectColdAccess; -import com.obs.services.model.BucketEncryption; -import com.obs.services.model.BucketLoggingConfiguration; -import com.obs.services.model.BucketNotificationConfiguration; -import com.obs.services.model.BucketQuota; import com.obs.services.model.BucketStoragePolicyConfiguration; -import com.obs.services.model.BucketTagInfo; import com.obs.services.model.CanonicalGrantee; import com.obs.services.model.EventTypeEnum; -import com.obs.services.model.FunctionGraphConfiguration; import com.obs.services.model.GrantAndPermission; import com.obs.services.model.GranteeInterface; import com.obs.services.model.GroupGrantee; @@ -50,671 +46,483 @@ import com.obs.services.model.RestoreObjectRequest; import com.obs.services.model.RouteRule; import com.obs.services.model.RouteRuleCondition; -import com.obs.services.model.SSEAlgorithmEnum; import com.obs.services.model.StorageClassEnum; -import com.obs.services.model.TopicConfiguration; import com.obs.services.model.WebsiteConfiguration; -import com.obs.services.model.fs.FSStatusEnum; - -@SuppressWarnings("deprecation") -public class V2Convertor implements IConvertor { - - private static IConvertor instance = new V2Convertor(); - - protected V2Convertor() { - - } - - public static IConvertor getInstance() { - return instance; - } - - @Override - public String transCompleteMultipartUpload(List parts) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("CompleteMultipartUpload"); - Collections.sort(parts, new Comparator() { - @Override - public int compare(PartEtag o1, PartEtag o2) { - if (o1 == o2) { - return 0; - } - if (o1 == null) { - return -1; - } - if (o2 == null) { - return 1; - } - return o1.getPartNumber().compareTo(o2.getPartNumber()); - } - - }); - for (PartEtag part : parts) { - builder.e("Part").e("PartNumber").t(part.getPartNumber() == null ? "" : part.getPartNumber().toString()).up() - .e("ETag").t(ServiceUtils.toValid(part.geteTag())); - } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException(e); - } - } - - @Override - public String transBucketLoction(String location) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("CreateBucketConfiguration") - .elem("LocationConstraint").text(ServiceUtils.toValid(location)); - return builder.asString(); - } catch (Exception e) { - throw new ServiceException(e); - } - } - - @Override - public String transVersioningConfiguration(String bucketName, String status) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("VersioningConfiguration") - .elem("Status").text(ServiceUtils.toValid(status)); - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for versioning", e); - } - } - - @Override - public String transLifecycleConfiguration(LifecycleConfiguration config) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("LifecycleConfiguration"); - for (Rule rule : config.getRules()) { - XMLBuilder b = builder.elem("Rule"); - if (ServiceUtils.isValid2(rule.getId())) { - b.elem("ID").t(rule.getId()); - } - if(rule.getPrefix() != null) { - b.elem("Prefix").t(ServiceUtils.toValid(rule.getPrefix())); - } - b.elem("Status").t(rule.getEnabled() ? "Enabled" : "Disabled"); - - if (rule.getTransitions() != null) { - for (Transition transition : rule.getTransitions()) { - if (transition.getObjectStorageClass() != null) { - XMLBuilder tBuilder = b.elem("Transition"); - if (transition.getDate() != null) { - tBuilder.elem("Date").t(ServiceUtils.formatIso8601MidnightDate(transition.getDate())); - } else if (transition.getDays() != null) { - tBuilder.elem("Days").t(transition.getDays().toString()); - } - tBuilder.elem("StorageClass").t(this.transStorageClass(transition.getObjectStorageClass())); - } - } - } - - if (rule.getExpiration() != null) { - XMLBuilder eBuilder = b.elem("Expiration"); - if (rule.getExpiration().getDate() != null) { - eBuilder.elem("Date").t(ServiceUtils.formatIso8601MidnightDate(rule.getExpiration().getDate())); - } else if (rule.getExpiration().getDays() != null) { - eBuilder.elem("Days").t(rule.getExpiration().getDays().toString()); - } - } - - if (rule.getNoncurrentVersionTransitions() != null) { - for (NoncurrentVersionTransition noncurrentVersionTransition : rule - .getNoncurrentVersionTransitions()) { - if (noncurrentVersionTransition.getObjectStorageClass() != null - && noncurrentVersionTransition.getDays() != null) { - XMLBuilder eBuilder = b.elem("NoncurrentVersionTransition"); - eBuilder.elem("NoncurrentDays").t(noncurrentVersionTransition.getDays().toString()); - eBuilder.elem("StorageClass") - .t(this.transStorageClass(noncurrentVersionTransition.getObjectStorageClass())); - } - } - } - - if (rule.getNoncurrentVersionExpiration() != null - && rule.getNoncurrentVersionExpiration().getDays() != null) { - XMLBuilder eBuilder = b.elem("NoncurrentVersionExpiration"); - eBuilder.elem("NoncurrentDays") - .t(rule.getNoncurrentVersionExpiration().getDays().toString()); - } - } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for lifecycle", e); - } - } - - @Override - public String transWebsiteConfiguration(WebsiteConfiguration config) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("WebsiteConfiguration"); - if (config.getRedirectAllRequestsTo() != null) { - if (null != config.getRedirectAllRequestsTo().getHostName()) { - builder = builder.elem("RedirectAllRequestsTo").elem("HostName") - .text(ServiceUtils.toValid(config.getRedirectAllRequestsTo().getHostName())); - } - if (null != config.getRedirectAllRequestsTo().getRedirectProtocol()) { - builder = builder.up().elem("Protocol") - .text(config.getRedirectAllRequestsTo().getRedirectProtocol().getCode()); - } - builder.up().up(); - return builder.asString(); - } - if (ServiceUtils.isValid2(config.getSuffix())) { - builder.elem("IndexDocument").elem("Suffix").text(config.getSuffix()).up().up(); - } - if (ServiceUtils.isValid2(config.getKey())) { - builder.elem("ErrorDocument").elem("Key").text(config.getKey()).up().up(); - } - if (null != config.getRouteRules() && config.getRouteRules().size() > 0) { - builder = builder.elem("RoutingRules"); - for (RouteRule routingRule : config.getRouteRules()) { - builder = builder.elem("RoutingRule"); - RouteRuleCondition condition = routingRule.getCondition(); - Redirect redirect = routingRule.getRedirect(); - if (null != condition) { - builder = builder.elem("Condition"); - String keyPrefixEquals = condition.getKeyPrefixEquals(); - String hecre = condition.getHttpErrorCodeReturnedEquals(); - if (ServiceUtils.isValid2(keyPrefixEquals)) { - builder = builder.elem("KeyPrefixEquals").text(keyPrefixEquals); - builder = builder.up(); - } - if (ServiceUtils.isValid2(hecre)) { - builder = builder.elem("HttpErrorCodeReturnedEquals").text(hecre); - builder = builder.up(); - } - builder = builder.up(); - } - if (null != redirect) { - builder = builder.elem("Redirect"); - String hostName = redirect.getHostName(); - String repalceKeyWith = redirect.getReplaceKeyWith(); - String replaceKeyPrefixWith = redirect.getReplaceKeyPrefixWith(); - String redirectCode = redirect.getHttpRedirectCode(); - if (ServiceUtils.isValid2(hostName)) { - builder = builder.elem("HostName").text(hostName); - builder = builder.up(); - } - if (ServiceUtils.isValid2(redirectCode)) { - builder = builder.elem("HttpRedirectCode").text(redirectCode); - builder = builder.up(); - } - if (ServiceUtils.isValid2(repalceKeyWith)) { - builder = builder.elem("ReplaceKeyWith").text(repalceKeyWith); - builder = builder.up(); - } - if (ServiceUtils.isValid2(replaceKeyPrefixWith)) { - builder = builder.elem("ReplaceKeyPrefixWith").text(replaceKeyPrefixWith); - builder = builder.up(); - } - if (redirect.getRedirectProtocol() != null) { - builder = builder.elem("Protocol").text(redirect.getRedirectProtocol().getCode()); - builder = builder.up(); - } - builder = builder.up(); - } - builder = builder.up(); - } - builder = builder.up(); - } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for website", e); - } - } - - @Override - public String transRestoreObjectRequest(RestoreObjectRequest req) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("RestoreRequest").elem("Days").t(String.valueOf(req.getDays())).up(); - if (req.getRestoreTier() != null) { - builder.e("GlacierJobParameters").e("Tier").t(req.getRestoreTier().getCode()); - } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for restoreobject", e); - } - } - - @Override - public String transBucketQuota(BucketQuota quota) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("Quota").elem("StorageQuota") - .text(String.valueOf(quota.getBucketQuota())).up(); - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for storageQuota", e); - } - } - - @Override - public String transBucketEcryption(BucketEncryption encryption) throws ServiceException { - String algorithm = encryption.getSseAlgorithm().getCode(); - String kmsKeyId = ""; - if (algorithm.equals(SSEAlgorithmEnum.KMS.getCode())) { - algorithm = "aws:" + algorithm; - kmsKeyId = encryption.getKmsKeyId(); - } - try { - XMLBuilder builder = XMLBuilder.create("ServerSideEncryptionConfiguration").e("Rule").e("ApplyServerSideEncryptionByDefault"); - builder.e("SSEAlgorithm").t(algorithm); - if (ServiceUtils.isValid(kmsKeyId)) { - builder.e("KMSMasterKeyID").t(kmsKeyId); + +public class V2Convertor extends V2BucketConvertor { + + private static IConvertor instance = new V2Convertor(); + + public V2Convertor() { + + } + + public static IConvertor getInstance() { + return instance; + } + + public static String getEncodedString(String value, String encodingType) { + if (encodingType != null && encodingType.toLowerCase().equals("url")) { + try { + return URLEncoder.encode(value, "UTF-8"); + } catch (UnsupportedEncodingException exception) { + throw new ServiceException(exception); } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for bucketEncryption", e); } - - } - - @Override - public String transStoragePolicy(BucketStoragePolicyConfiguration status) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("StoragePolicy") - .elem("DefaultStorageClass") - .text(this.transStorageClass(status.getBucketStorageClass())); - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for StoragePolicy", e); - } - } - - @Override - public String transBucketLoggingConfiguration(BucketLoggingConfiguration c) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("BucketLoggingStatus"); - if (c.isLoggingEnabled()) { - XMLBuilder enabledBuilder = builder.elem("LoggingEnabled"); - if(c.getTargetBucketName() != null) { - enabledBuilder.elem("TargetBucket") - .text(ServiceUtils.toValid(c.getTargetBucketName())); - } - - if(c.getLogfilePrefix() != null) { - enabledBuilder.elem("TargetPrefix").text(ServiceUtils.toValid(c.getLogfilePrefix())); - } - GrantAndPermission[] grants = c.getTargetGrants(); - if (grants.length > 0) { - XMLBuilder grantsBuilder = enabledBuilder.elem("TargetGrants"); - for (GrantAndPermission gap : grants) { - GranteeInterface grantee = gap.getGrantee(); - Permission permission = gap.getPermission(); - if(permission != null) { - XMLBuilder subBuilder = null; - if (grantee instanceof CanonicalGrantee) { - subBuilder = XMLBuilder.create("Grantee") - .attr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") - .attr("xsi:type", "CanonicalUser").element("ID").text(ServiceUtils.toValid(grantee.getIdentifier())); - String displayName = ((CanonicalGrantee) grantee).getDisplayName(); - if (ServiceUtils.isValid2(displayName)) { - subBuilder.up().element("DisplayName") - .text(displayName); - } - } else if (grantee instanceof GroupGrantee) { - subBuilder = XMLBuilder.create("Grantee") - .attr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") - .attr("xsi:type", "Group").element("URI").text(this.transGroupGrantee(((GroupGrantee)grantee).getGroupGranteeType())); - } - - if(subBuilder != null) { - grantsBuilder.elem("Grant").importXMLBuilder(subBuilder).elem("Permission") - .text(ServiceUtils.toValid(permission.getPermissionString())); - } - } - } - } - } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for BucketLoggingConfiguration", e); - } - } - - @Override - public String transBucketCors(BucketCors cors) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("CORSConfiguration"); - for (BucketCorsRule rule : cors.getRules()) { - builder = builder.e("CORSRule"); - if (rule.getId() != null) { - builder.e("ID").t(rule.getId()); - } - if(rule.getAllowedMethod() != null) { - for(String method : rule.getAllowedMethod()) { - builder.e("AllowedMethod").t(ServiceUtils.toValid(method)); - } - } - - if(rule.getAllowedOrigin() != null) { - for(String origin : rule.getAllowedOrigin()) { - builder.e("AllowedOrigin").t(ServiceUtils.toValid(origin)); - } - } - - if(rule.getAllowedHeader() != null) { - for(String header : rule.getAllowedHeader()) { - builder.e("AllowedHeader").t(ServiceUtils.toValid(header)); - } - } - builder.e("MaxAgeSeconds").t(String.valueOf(rule.getMaxAgeSecond())); - if(rule.getExposeHeader() != null) { - for(String exposeHeader : rule.getExposeHeader()) { - builder.e("ExposeHeader").t(ServiceUtils.toValid(exposeHeader)); - } - } - builder = builder.up(); - } - return builder.asString(); - }catch (Exception e) { - throw new ServiceException("Failed to build XML document for cors", e); - } - } - - - @Override - public String transAccessControlList(AccessControlList acl, boolean isBucket) throws ServiceException { - Owner owner = acl.getOwner(); - GrantAndPermission[] grants = acl.getGrantAndPermissions(); - try { - XMLBuilder builder = XMLBuilder.create("AccessControlPolicy"); - if (owner != null) { - builder = builder.elem("Owner").elem("ID").text(ServiceUtils.toValid(owner.getId())); - if (null != owner.getDisplayName()) { - builder.up().elem("DisplayName").text(owner.getDisplayName()); - } - builder = builder.up().up(); - } - if(grants.length > 0) { - XMLBuilder accessControlList = builder.elem("AccessControlList"); - for (GrantAndPermission gap : grants) { - GranteeInterface grantee = gap.getGrantee(); - Permission permission = gap.getPermission(); - XMLBuilder subBuilder = null; - if (grantee instanceof CanonicalGrantee) { - subBuilder = XMLBuilder.create("Grantee") - .attr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") - .attr("xsi:type", "CanonicalUser").element("ID").text(ServiceUtils.toValid(grantee.getIdentifier())); - String displayName = ((CanonicalGrantee) grantee).getDisplayName(); - if (ServiceUtils.isValid2(displayName)) { - subBuilder.up().element("DisplayName").text(displayName); - } - } else if (grantee instanceof GroupGrantee) { - subBuilder = XMLBuilder.create("Grantee") - .attr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").attr("xsi:type", "Group") - .element("URI").text(this.transGroupGrantee(((GroupGrantee)grantee).getGroupGranteeType())); - }else if(grantee != null) { - subBuilder = XMLBuilder.create("Grantee") - .attr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") - .attr("xsi:type", "CanonicalUser").element("ID").text(ServiceUtils.toValid(grantee.getIdentifier())); - } - - if(subBuilder != null) { - XMLBuilder grantBuilder = accessControlList.elem("Grant").importXMLBuilder(subBuilder); - if(permission != null) { - grantBuilder.elem("Permission") - .text(ServiceUtils.toValid(permission.getPermissionString())); - } - } - } - } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for ACL", e); - } - } - - public String transKeyAndVersion(KeyAndVersion[] objectNameAndVersions, boolean isQuiet) - throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("Delete").elem("Quiet") - .text(String.valueOf(isQuiet)).up(); - for (KeyAndVersion nav : objectNameAndVersions) { - XMLBuilder objectBuilder = builder.elem("Object").elem("Key").text(ServiceUtils.toValid(nav.getKey())).up(); - if (ServiceUtils.isValid(nav.getVersion())) { - objectBuilder.elem("VersionId").text(nav.getVersion()); - } - } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document", e); - } - } - - @Override - public String transBucketTagInfo(BucketTagInfo bucketTagInfo) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("Tagging").e("TagSet"); - for (BucketTagInfo.TagSet.Tag tag : bucketTagInfo.getTagSet().getTags()) { - if (tag != null) { - builder.e("Tag") - .e("Key").t(ServiceUtils.toValid(tag.getKey())) - .up() - .e("Value") - .t(ServiceUtils.toValid(tag.getValue())); - } - } - return builder.up().asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for Tagging", e); - } - } - - @Override - public String transBucketNotificationConfiguration( - BucketNotificationConfiguration bucketNotificationConfiguration) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("NotificationConfiguration"); - if (bucketNotificationConfiguration == null) { - return builder.asString(); - } - - for (TopicConfiguration config : bucketNotificationConfiguration.getTopicConfigurations()) { - packNotificationConfig(builder, config, "TopicConfiguration", "Topic", "S3Key"); - } - - for (FunctionGraphConfiguration config : bucketNotificationConfiguration.getFunctionGraphConfigurations()) { - packNotificationConfig(builder, config, "FunctionGraphConfiguration", "FunctionGraph", "S3Key"); + return value; + } + + @Override + public String transCompleteMultipartUpload(List parts) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("CompleteMultipartUpload"); + Collections.sort(parts, new Comparator() { + @Override + public int compare(PartEtag o1, PartEtag o2) { + if (o1 == o2) { + return 0; + } + if (o1 == null) { + return -1; + } + if (o2 == null) { + return 1; + } + return o1.getPartNumber().compareTo(o2.getPartNumber()); + } + + }); + for (PartEtag part : parts) { + builder.e("Part").e("PartNumber").t(part.getPartNumber() == null ? "" : part.getPartNumber().toString()) + .up().e("ETag").t(ServiceUtils.toValid(part.getEtag())); } - - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for Notification", e); - } - } - - protected void packNotificationConfig(XMLBuilder builder, AbstractNotification config, String configType, String urnType, String adapter) { - builder = builder.e(configType); - if (config.getId() != null) { - builder.e("Id").t(config.getId()); + return builder.asString(); + } catch (Exception e) { + throw new ServiceException(e); } - if (config.getFilter() != null && !config.getFilter().getFilterRules().isEmpty()) { - builder = builder.e("Filter").e(adapter); - for (AbstractNotification.Filter.FilterRule rule : config.getFilter().getFilterRules()) { - if (rule != null) { - builder.e("FilterRule").e("Name").t(ServiceUtils.toValid(rule.getName())).up() - .e("Value").t(ServiceUtils.toValid(rule.getValue())); + } + + @Override + public String transVersioningConfiguration(String bucketName, String status) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("VersioningConfiguration").elem("Status") + .text(ServiceUtils.toValid(status)); + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for versioning", e); + } + } + + @Override + public String transRequestPaymentConfiguration(String bucketName, String payer) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("RequestPaymentConfiguration").elem("Payer") + .text(ServiceUtils.toValid(payer)); + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for requestPayment", e); + } + } + + @Override + public String transLifecycleConfiguration(LifecycleConfiguration config) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("LifecycleConfiguration"); + for (Rule rule : config.getRules()) { + OBSXMLBuilder b = builder.elem("Rule"); + if (ServiceUtils.isValid2(rule.getId())) { + b.elem("ID").t(rule.getId()); + } + if (rule.getPrefix() != null) { + b.elem("Prefix").t(ServiceUtils.toValid(rule.getPrefix())); + } + b.elem("Status").t(rule.getEnabled() ? "Enabled" : "Disabled"); + + if (rule.getTransitions() != null) { + transTransitions(rule, b); + } + + if (rule.getExpiration() != null) { + transExpiration(rule, b); + } + + if (rule.getNoncurrentVersionTransitions() != null) { + transNoncurrentVersionTransition(rule, b); + } + + if (rule.getNoncurrentVersionExpiration() != null + && rule.getNoncurrentVersionExpiration().getDays() != null) { + OBSXMLBuilder noncurrentVersionBuilder = b.elem("NoncurrentVersionExpiration"); + noncurrentVersionBuilder.elem("NoncurrentDays").t( + rule.getNoncurrentVersionExpiration().getDays().toString()); } } - builder = builder.up().up(); + return builder.asString(); + } catch (ParserConfigurationException e) { + throw new ServiceException("Failed to build XML document for lifecycle", e); + } catch (TransformerException e) { + throw new ServiceException("Failed to build XML document for lifecycle", e); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for lifecycle", e); } - String urn = null; - if (config instanceof TopicConfiguration) { - urn = ((TopicConfiguration)config).getTopic(); - } - if (config instanceof FunctionGraphConfiguration) { - urn = ((FunctionGraphConfiguration)config).getFunctionGraph(); + } + + private void transNoncurrentVersionTransition(Rule rule, OBSXMLBuilder b) { + for (NoncurrentVersionTransition noncurrentVersionTransition : rule + .getNoncurrentVersionTransitions()) { + if (noncurrentVersionTransition.getObjectStorageClass() != null + && noncurrentVersionTransition.getDays() != null) { + OBSXMLBuilder noncurrentVersionBuilder = b.elem("NoncurrentVersionTransition"); + noncurrentVersionBuilder.elem("NoncurrentDays").t( + noncurrentVersionTransition.getDays().toString()); + noncurrentVersionBuilder.elem("StorageClass") + .t(this.transStorageClass(noncurrentVersionTransition.getObjectStorageClass())); + } } - if (urn != null) { - builder.e(urnType).t(urn); + } + + private void transExpiration(Rule rule, OBSXMLBuilder b) { + OBSXMLBuilder expirationBuilder = b.elem("Expiration"); + if (rule.getExpiration().getDate() != null) { + expirationBuilder.elem("Date").t( + ServiceUtils.formatIso8601MidnightDate(rule.getExpiration().getDate())); + } else if (rule.getExpiration().getDays() != null) { + expirationBuilder.elem("Days").t(rule.getExpiration().getDays().toString()); + } + } + + private void transTransitions(Rule rule, OBSXMLBuilder b) { + for (Transition transition : rule.getTransitions()) { + if (transition.getObjectStorageClass() != null) { + OBSXMLBuilder transitionBuilder = b.elem("Transition"); + if (transition.getDate() != null) { + transitionBuilder.elem("Date").t( + ServiceUtils.formatIso8601MidnightDate(transition.getDate())); + } else if (transition.getDays() != null) { + transitionBuilder.elem("Days").t(transition.getDays().toString()); + } + transitionBuilder.elem("StorageClass").t( + this.transStorageClass(transition.getObjectStorageClass())); + } } + } - if (config.getEventTypes() != null) { - for (EventTypeEnum event : config.getEventTypes()) { - if(event != null) { - builder.e("Event").t(this.transEventType(event)); + @Override + public String transWebsiteConfiguration(WebsiteConfiguration config) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("WebsiteConfiguration"); + if (config.getRedirectAllRequestsTo() != null) { + if (null != config.getRedirectAllRequestsTo().getHostName()) { + builder = builder.elem("RedirectAllRequestsTo").elem("HostName") + .text(ServiceUtils.toValid(config.getRedirectAllRequestsTo().getHostName())); } + if (null != config.getRedirectAllRequestsTo().getRedirectProtocol()) { + builder = builder.up().elem("Protocol") + .text(config.getRedirectAllRequestsTo().getRedirectProtocol().getCode()); + } + return builder.asString(); + } + if (ServiceUtils.isValid2(config.getSuffix())) { + builder.elem("IndexDocument").elem("Suffix").text(config.getSuffix()); + } + if (ServiceUtils.isValid2(config.getKey())) { + builder.elem("ErrorDocument").elem("Key").text(config.getKey()); + } + if (null != config.getRouteRules() && config.getRouteRules().size() > 0) { + builder = builder.elem("RoutingRules"); + for (RouteRule routingRule : config.getRouteRules()) { + builder = transWebsiteRoutingRule(builder, routingRule); + } + builder = builder.up(); + } + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for website", e); + } + } + + private OBSXMLBuilder transWebsiteRoutingRule(OBSXMLBuilder builder, RouteRule routingRule) { + builder = builder.elem("RoutingRule"); + RouteRuleCondition condition = routingRule.getCondition(); + if (null != condition) { + builder = builder.elem("Condition"); + String keyPrefixEquals = condition.getKeyPrefixEquals(); + String hecre = condition.getHttpErrorCodeReturnedEquals(); + if (ServiceUtils.isValid2(keyPrefixEquals)) { + builder = builder.elem("KeyPrefixEquals").text(keyPrefixEquals); + builder = builder.up(); + } + if (ServiceUtils.isValid2(hecre)) { + builder = builder.elem("HttpErrorCodeReturnedEquals").text(hecre); + builder = builder.up(); + } + builder = builder.up(); + } + + Redirect redirect = routingRule.getRedirect(); + if (null != redirect) { + builder = builder.elem("Redirect"); + String hostName = redirect.getHostName(); + String repalceKeyWith = redirect.getReplaceKeyWith(); + String replaceKeyPrefixWith = redirect.getReplaceKeyPrefixWith(); + String redirectCode = redirect.getHttpRedirectCode(); + if (ServiceUtils.isValid2(hostName)) { + builder = builder.elem("HostName").text(hostName); + builder = builder.up(); + } + if (ServiceUtils.isValid2(redirectCode)) { + builder = builder.elem("HttpRedirectCode").text(redirectCode); + builder = builder.up(); + } + if (ServiceUtils.isValid2(repalceKeyWith)) { + builder = builder.elem("ReplaceKeyWith").text(repalceKeyWith); + builder = builder.up(); } + if (ServiceUtils.isValid2(replaceKeyPrefixWith)) { + builder = builder.elem("ReplaceKeyPrefixWith").text(replaceKeyPrefixWith); + builder = builder.up(); + } + if (redirect.getRedirectProtocol() != null) { + builder = builder.elem("Protocol").text(redirect.getRedirectProtocol().getCode()); + builder = builder.up(); + } + builder = builder.up(); } builder = builder.up(); - } - - @Override - public String transReplicationConfiguration(ReplicationConfiguration replicationConfiguration) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("ReplicationConfiguration").e("Agency").t(ServiceUtils.toValid(replicationConfiguration.getAgency())) - .up(); - for (ReplicationConfiguration.Rule rule : replicationConfiguration.getRules()) { - builder = builder.e("Rule"); - if (rule.getId() != null) { - builder.e("ID").t(rule.getId()); - } - builder.e("Prefix").t(ServiceUtils.toValid(rule.getPrefix())); - if (rule.getStatus() != null) { - builder.e("Status").t(rule.getStatus().getCode()); - } - if (rule.getDestination() != null) { - String bucketName = ServiceUtils.toValid(rule.getDestination().getBucket()); - builder = builder.e("Destination").e("Bucket").t(bucketName.startsWith("arn:aws:s3:::") ? bucketName : "arn:aws:s3:::" + bucketName).up(); - if (rule.getDestination().getObjectStorageClass() != null) { - builder.e("StorageClass").t(this.transStorageClass(rule.getDestination().getObjectStorageClass())); - } - builder = builder.up(); - } - builder = builder.up(); - } - return builder.asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for Replication", e); - } - } - - @Override - public String transBucketFileInterface(FSStatusEnum status) throws ServiceException { - try { - return XMLBuilder.create("FileInterfaceConfiguration").e("Status").t(status.getCode()) - .up().asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for FileInterface", e); - } - } - - @Override - public String transEventType(EventTypeEnum eventType) { - String eventTypeStr = ""; - if(eventType != null) { - switch (eventType) { - case OBJECT_CREATED_ALL: - eventTypeStr = "s3:ObjectCreated:*"; - break; - case OBJECT_CREATED_PUT: - eventTypeStr = "s3:ObjectCreated:Put"; - break; - case OBJECT_CREATED_POST: - eventTypeStr = "s3:ObjectCreated:Post"; - break; - case OBJECT_CREATED_COPY: - eventTypeStr = "s3:ObjectCreated:Copy"; - break; - case OBJECT_CREATED_COMPLETE_MULTIPART_UPLOAD: - eventTypeStr = "s3:ObjectCreated:CompleteMultipartUpload"; - break; - case OBJECT_REMOVED_ALL: - eventTypeStr = "s3:ObjectRemoved:*"; - break; - case OBJECT_REMOVED_DELETE: - eventTypeStr = "s3:ObjectRemoved:Delete"; - break; - case OBJECT_REMOVED_DELETE_MARKER_CREATED: - eventTypeStr = "s3:ObjectRemoved:DeleteMarkerCreated"; - break; - default: - break; - } - } - return eventTypeStr; - } - - @Override - public String transStorageClass(StorageClassEnum storageClass) { - String storageClassStr = ""; - if(storageClass != null) { - switch (storageClass) { - case STANDARD: - storageClassStr = "STANDARD"; - break; - case WARM: - storageClassStr = "STANDARD_IA"; - break; - case COLD: - storageClassStr = "GLACIER"; - break; - default: - break; - } - } - return storageClassStr; - } - - @Override - public String transBucketDirectColdAccess(BucketDirectColdAccess access) throws ServiceException { - try { - XMLBuilder builder = XMLBuilder.create("DirectColdAccessConfiguration"); - - builder = builder.e("Status").t(access.getStatus().getCode()); - builder = builder.up(); - - return builder.up().asString(); - } catch (Exception e) { - throw new ServiceException("Failed to build XML document for Tagging", e); - } - } - - @Override - public AccessControlList transCannedAcl(String cannedAcl) { - if(Constants.ACL_PRIVATE.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_PRIVATE; - }else if(Constants.ACL_PUBLIC_READ.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_PUBLIC_READ; - }else if(Constants.ACL_PUBLIC_READ_WRITE.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_PUBLIC_READ_WRITE; - }else if(Constants.ACL_PUBLIC_READ_DELIVERED.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_PUBLIC_READ; - }else if(Constants.ACL_PUBLIC_READ_WRITE_DELIVERED.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_PUBLIC_READ_WRITE; - }else if(Constants.ACL_AUTHENTICATED_READ.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_AUTHENTICATED_READ; - }else if(Constants.ACL_BUCKET_OWNER_READ.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_BUCKET_OWNER_READ; - }else if(Constants.ACL_BUCKET_OWNER_FULL_CONTROL.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_BUCKET_OWNER_FULL_CONTROL; - }else if(Constants.ACL_LOG_DELIVERY_WRITE.equals(cannedAcl)) { - return AccessControlList.REST_CANNED_LOG_DELIVERY_WRITE; - } - return null; - } - - @Override - public String transGroupGrantee(GroupGranteeEnum groupGrantee) { - String groupGranteeStr = ""; - if(groupGrantee != null) { - switch (groupGrantee) { - case ALL_USERS: - groupGranteeStr = Constants.ALL_USERS_URI; - break; - case AUTHENTICATED_USERS: - groupGranteeStr = Constants.AUTHENTICATED_USERS_URI; - break; - case LOG_DELIVERY: - groupGranteeStr = Constants.LOG_DELIVERY_URI; - break; - default: - break; - } - } - return groupGranteeStr; - } - - + return builder; + } + + @Override + public String transRestoreObjectRequest(RestoreObjectRequest req) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("RestoreRequest") + .elem("Days").t(String.valueOf(req.getDays())).up(); + if (req.getRestoreTier() != null) { + builder.e("GlacierJobParameters").e("Tier").t(req.getRestoreTier().getCode()); + } + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for restoreobject", e); + } + } + + @Override + public String transStoragePolicy(BucketStoragePolicyConfiguration status) throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("StoragePolicy").elem("DefaultStorageClass") + .text(this.transStorageClass(status.getBucketStorageClass())); + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for StoragePolicy", e); + } + } + + @Override + public String transAccessControlList(AccessControlList acl, boolean isBucket) throws ServiceException { + Owner owner = acl.getOwner(); + GrantAndPermission[] grants = acl.getGrantAndPermissions(); + + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("AccessControlPolicy"); + if (owner != null) { + builder = builder.elem("Owner").elem("ID").text(ServiceUtils.toValid(owner.getId())); + if (null != owner.getDisplayName()) { + builder.up().elem("DisplayName").text(owner.getDisplayName()); + } + builder = builder.up().up(); + } + if (grants.length > 0) { + OBSXMLBuilder accessControlList = builder.elem("AccessControlList"); + for (GrantAndPermission gap : grants) { + GranteeInterface grantee = gap.getGrantee(); + Permission permission = gap.getPermission(); + OBSXMLBuilder subBuilder = null; + if (grantee instanceof CanonicalGrantee) { + subBuilder = buildCanonicalGrantee(grantee); + } else if (grantee instanceof GroupGrantee) { + subBuilder = buildGroupGrantee(grantee); + } else if (grantee != null) { + subBuilder = OBSXMLBuilder.create("Grantee") + .attr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") + .attr("xsi:type", "CanonicalUser").element("ID") + .text(ServiceUtils.toValid(grantee.getIdentifier())); + } + + if (subBuilder != null) { + OBSXMLBuilder grantBuilder = accessControlList.elem("Grant").importXMLBuilder(subBuilder); + if (permission != null) { + grantBuilder.elem("Permission") + .text(ServiceUtils.toValid(permission.getPermissionString())); + } + } + } + } + return builder.asString(); + } catch (ParserConfigurationException e) { + throw new ServiceException("Failed to build XML document for ACL", e); + } catch (TransformerException e) { + throw new ServiceException("Failed to build XML document for ACL", e); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for ACL", e); + } + } + + public String transKeyAndVersion(KeyAndVersion[] objectNameAndVersions, boolean isQuiet, String encodingType) + throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("Delete").elem("Quiet").text(String.valueOf(isQuiet)).up(); + if (encodingType != null) { + builder.elem("EncodingType").text(encodingType); + } + for (KeyAndVersion nav : objectNameAndVersions) { + String encodedString = getEncodedString(ServiceUtils.toValid(nav.getKey()), encodingType); + OBSXMLBuilder objectBuilder = builder.elem("Object").elem("Key").text(encodedString).up(); + if (ServiceUtils.isValid(nav.getVersion())) { + objectBuilder.elem("VersionId").text(nav.getVersion()); + } + } + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document", e); + } + } + + @Override + public String transReplicationConfiguration(ReplicationConfiguration replicationConfiguration) + throws ServiceException { + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("ReplicationConfiguration").e("Agency") + .t(ServiceUtils.toValid(replicationConfiguration.getAgency())).up(); + for (ReplicationConfiguration.Rule rule : replicationConfiguration.getRules()) { + builder = builder.e("Rule"); + if (rule.getId() != null) { + builder.e("ID").t(rule.getId()); + } + builder.e("Prefix").t(ServiceUtils.toValid(rule.getPrefix())); + if (rule.getStatus() != null) { + builder.e("Status").t(rule.getStatus().getCode()); + } + if (rule.getHistoricalObjectReplication() != null) { + builder.e("HistoricalObjectReplication").t(rule.getHistoricalObjectReplication().getCode()); + } + if (rule.getDestination() != null) { + String bucketName = ServiceUtils.toValid(rule.getDestination().getBucket()); + builder = builder.e("Destination").e("Bucket") + .t(bucketName.startsWith("arn:aws:s3:::") ? bucketName : "arn:aws:s3:::" + bucketName).up(); + if (rule.getDestination().getObjectStorageClass() != null) { + builder.e("StorageClass") + .t(this.transStorageClass(rule.getDestination().getObjectStorageClass())); + } + builder = builder.up(); + } + builder = builder.up(); + } + return builder.asString(); + } catch (Exception e) { + throw new ServiceException("Failed to build XML document for Replication", e); + } + } + + @Override + public String transEventType(EventTypeEnum eventType) { + String eventTypeStr = ""; + if (eventType != null) { + switch (eventType) { + case OBJECT_CREATED_ALL: + eventTypeStr = "s3:ObjectCreated:*"; + break; + case OBJECT_CREATED_PUT: + eventTypeStr = "s3:ObjectCreated:Put"; + break; + case OBJECT_CREATED_POST: + eventTypeStr = "s3:ObjectCreated:Post"; + break; + case OBJECT_CREATED_COPY: + eventTypeStr = "s3:ObjectCreated:Copy"; + break; + case OBJECT_CREATED_COMPLETE_MULTIPART_UPLOAD: + eventTypeStr = "s3:ObjectCreated:CompleteMultipartUpload"; + break; + case OBJECT_REMOVED_ALL: + eventTypeStr = "s3:ObjectRemoved:*"; + break; + case OBJECT_REMOVED_DELETE: + eventTypeStr = "s3:ObjectRemoved:Delete"; + break; + case OBJECT_REMOVED_DELETE_MARKER_CREATED: + eventTypeStr = "s3:ObjectRemoved:DeleteMarkerCreated"; + break; + default: + break; + } + } + return eventTypeStr; + } + + @Override + public String transStorageClass(StorageClassEnum storageClass) { + String storageClassStr = ""; + if (storageClass != null) { + switch (storageClass) { + case STANDARD: + storageClassStr = "STANDARD"; + break; + case WARM: + storageClassStr = "STANDARD_IA"; + break; + case COLD: + storageClassStr = "GLACIER"; + break; + default: + break; + } + } + return storageClassStr; + } + + @Override + public AccessControlList transCannedAcl(String cannedAcl) { + if (Constants.ACL_PRIVATE.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_PRIVATE; + } else if (Constants.ACL_PUBLIC_READ.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_PUBLIC_READ; + } else if (Constants.ACL_PUBLIC_READ_WRITE.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_PUBLIC_READ_WRITE; + } else if (Constants.ACL_PUBLIC_READ_DELIVERED.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_PUBLIC_READ; + } else if (Constants.ACL_PUBLIC_READ_WRITE_DELIVERED.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_PUBLIC_READ_WRITE; + } else if (Constants.ACL_AUTHENTICATED_READ.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_AUTHENTICATED_READ; + } else if (Constants.ACL_BUCKET_OWNER_READ.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_BUCKET_OWNER_READ; + } else if (Constants.ACL_BUCKET_OWNER_FULL_CONTROL.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_BUCKET_OWNER_FULL_CONTROL; + } else if (Constants.ACL_LOG_DELIVERY_WRITE.equals(cannedAcl)) { + return AccessControlList.REST_CANNED_LOG_DELIVERY_WRITE; + } + return null; + } + + @Override + public String transGroupGrantee(GroupGranteeEnum groupGrantee) { + String groupGranteeStr = ""; + if (groupGrantee != null) { + switch (groupGrantee) { + case ALL_USERS: + groupGranteeStr = Constants.ALL_USERS_URI; + break; + case AUTHENTICATED_USERS: + groupGranteeStr = Constants.AUTHENTICATED_USERS_URI; + break; + case LOG_DELIVERY: + groupGranteeStr = Constants.LOG_DELIVERY_URI; + break; + default: + break; + } + } + return groupGranteeStr; + } + } diff --git a/app/src/main/java/com/obs/services/internal/V2Headers.java b/app/src/main/java/com/obs/services/internal/V2Headers.java index 3f456ef..efc4429 100644 --- a/app/src/main/java/com/obs/services/internal/V2Headers.java +++ b/app/src/main/java/com/obs/services/internal/V2Headers.java @@ -11,280 +11,283 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal; +public class V2Headers implements IHeaders { + + public V2Headers() { + + } + + private static V2Headers instance = new V2Headers(); -public class V2Headers implements IHeaders{ - - private V2Headers() { - - } - - private static V2Headers instance = new V2Headers(); - - public static IHeaders getInstance() { - return instance; - } - - @Override - public String defaultStorageClassHeader() { - return "x-default-storage-class"; - } - - @Override + public static IHeaders getInstance() { + return instance; + } + + @Override + public String defaultStorageClassHeader() { + return "x-default-storage-class"; + } + + @Override public String epidHeader() { return this.headerPrefix() + "epid"; } @Override - public String aclHeader() { - return this.headerPrefix() + "acl"; - } - - @Override - public String requestIdHeader() { - return this.headerPrefix() + "request-id"; - } - - @Override - public String requestId2Header() { - return this.headerPrefix() + "id-2"; - } - - @Override - public String storageClassHeader() { - return this.headerPrefix() + "storage-class"; - } - - @Override - public String websiteRedirectLocationHeader() { - return this.headerPrefix() + "website-redirect-location"; - } - - @Override - public String sseKmsHeader() { - return this.headerPrefix() + "server-side-encryption"; - } - - @Override - public String sseKmsKeyHeader() { - return this.headerPrefix() + "server-side-encryption-aws-kms-key-id"; - } - - @Override - public String sseKmsProjectIdHeader() { - return this.headerPrefix() + "sse-kms-key-project-id"; - } - - @Override - public String sseCHeader() { - return this.headerPrefix() + "server-side-encryption-customer-algorithm"; - } - - @Override - public String sseCKeyHeader() { - return this.headerPrefix() + "server-side-encryption-customer-key"; - } - - @Override - public String sseCKeyMd5Header() { - return this.headerPrefix() + "server-side-encryption-customer-key-MD5"; - } - - @Override - public String expiresHeader() { - return "x-obs-expires"; - } - - @Override - public String versionIdHeader() { - return this.headerPrefix() + "version-id"; - } - - @Override - public String copySourceSseCHeader() { - return this.headerPrefix() + "copy-source-server-side-encryption-customer-algorithm"; - } - - @Override - public String metadataDirectiveHeader() { - return this.headerPrefix() + "metadata-directive"; - } - - @Override - public String headerPrefix() { - return Constants.V2_HEADER_PREFIX; - } - - @Override - public String headerMetaPrefix() { - return Constants.V2_HEADER_META_PREFIX; - } - - @Override - public String dateHeader() { - return this.headerPrefix() + "date"; - } - - @Override - public String grantReadHeader() { - return this.headerPrefix() + "grant-read"; - } - - @Override - public String grantWriteHeader() { - return this.headerPrefix() + "grant-write"; - } - - @Override - public String grantReadAcpHeader() { - return this.headerPrefix() + "grant-read-acp"; - } - - @Override - public String grantWriteAcpHeader() { - return this.headerPrefix() + "grant-write-acp"; - } - - @Override - public String grantFullControlHeader() { - return this.headerPrefix() + "grant-full-control"; - } - - @Override - public String grantReadDeliveredHeader() { - return null; - } - - @Override - public String grantFullControlDeliveredHeader() { - return null; - } - - @Override - public String serverVersionHeader() { - return "x-obs-version"; - } - - @Override - public String bucketRegionHeader() { - return this.headerPrefix() + "bucket-region"; - } - - @Override - public String locationHeader() { - return this.headerPrefix() + "location"; - } - - @Override - public String successRedirectLocationHeader() { - return null; - } - - @Override - public String deleteMarkerHeader() { - return this.headerPrefix() + "delete-marker"; - } - - @Override - public String copySourceSseCKeyHeader() { - return this.headerPrefix() + "copy-source-server-side-encryption-customer-key"; - } - - @Override - public String copySourceSseCKeyMd5Header() { - return this.headerPrefix() + "copy-source-server-side-encryption-customer-key-MD5"; - } - - @Override - public String copySourceIfModifiedSinceHeader() { - return this.headerPrefix() + "copy-source-if-modified-since"; - } - - @Override - public String copySourceIfUnmodifiedSinceHeader() { - return this.headerPrefix() + "copy-source-if-unmodified-since"; - } - - @Override - public String copySourceIfNoneMatchHeader() { - return this.headerPrefix() + "copy-source-if-none-match"; - } - - @Override - public String copySourceIfMatchHeader() { - return this.headerPrefix() + "copy-source-if-match"; - } - - @Override - public String copySourceHeader() { - return this.headerPrefix() + "copy-source"; - } - - @Override - public String copySourceVersionIdHeader() { - return this.headerPrefix() + "copy-source-version-id"; - } - - @Override - public String expirationHeader() { - return this.headerPrefix() + "expiration"; - } - - @Override - public String restoreHeader() { - return this.headerPrefix() + "restore"; - } - - @Override - public String copySourceRangeHeader() { - return this.headerPrefix() + "copy-source-range"; - } - - @Override - public String securityTokenHeader() { - return this.headerPrefix() + "security-token"; - } - - @Override - public String contentSha256Header() { - return this.headerPrefix() + "content-sha256"; - } - - @Override - public String objectTypeHeader() { - return "x-obs-object-type"; - } - - @Override - public String nextPositionHeader() { - return "x-obs-next-append-position"; - } - - @Override - public String listTimeoutHeader() { - return this.headerPrefix() + "list-timeout"; - } - - @Override - public String fsFileInterfaceHeader() { - return "x-obs-fs-file-interface"; - } - - @Override - public String fsModeHeader() { - return this.headerMetaPrefix() + "mode"; - } - - @Override - public String azRedundancyHeader() { - return "x-obs-az-redundancy"; - } - - @Override - public String bucketTypeHeader() { - return "x-obs-bucket-type"; - } - -} + public String aclHeader() { + return this.headerPrefix() + "acl"; + } + + @Override + public String requestIdHeader() { + return this.headerPrefix() + "request-id"; + } + + @Override + public String requestId2Header() { + return this.headerPrefix() + "id-2"; + } + + @Override + public String storageClassHeader() { + return this.headerPrefix() + "storage-class"; + } + + @Override + public String websiteRedirectLocationHeader() { + return this.headerPrefix() + "website-redirect-location"; + } + + @Override + public String sseKmsHeader() { + return this.headerPrefix() + "server-side-encryption"; + } + + @Override + public String sseKmsKeyHeader() { + return this.headerPrefix() + "server-side-encryption-aws-kms-key-id"; + } + + @Override + public String sseKmsProjectIdHeader() { + return this.headerPrefix() + "sse-kms-key-project-id"; + } + + @Override + public String sseCHeader() { + return this.headerPrefix() + "server-side-encryption-customer-algorithm"; + } + + @Override + public String sseCKeyHeader() { + return this.headerPrefix() + "server-side-encryption-customer-key"; + } + + @Override + public String sseCKeyMd5Header() { + return this.headerPrefix() + "server-side-encryption-customer-key-MD5"; + } + + @Override + public String expiresHeader() { + return "x-obs-expires"; + } + + @Override + public String versionIdHeader() { + return this.headerPrefix() + "version-id"; + } + + @Override + public String copySourceSseCHeader() { + return this.headerPrefix() + "copy-source-server-side-encryption-customer-algorithm"; + } + + @Override + public String metadataDirectiveHeader() { + return this.headerPrefix() + "metadata-directive"; + } + + @Override + public String headerPrefix() { + return Constants.V2_HEADER_PREFIX; + } + + @Override + public String headerMetaPrefix() { + return Constants.V2_HEADER_META_PREFIX; + } + + @Override + public String dateHeader() { + return this.headerPrefix() + "date"; + } + + @Override + public String grantReadHeader() { + return this.headerPrefix() + "grant-read"; + } + + @Override + public String grantWriteHeader() { + return this.headerPrefix() + "grant-write"; + } + + @Override + public String grantReadAcpHeader() { + return this.headerPrefix() + "grant-read-acp"; + } + + @Override + public String grantWriteAcpHeader() { + return this.headerPrefix() + "grant-write-acp"; + } + + @Override + public String grantFullControlHeader() { + return this.headerPrefix() + "grant-full-control"; + } + + @Override + public String grantReadDeliveredHeader() { + return null; + } + @Override + public String grantFullControlDeliveredHeader() { + return null; + } + + @Override + public String serverVersionHeader() { + return "x-obs-version"; + } + + @Override + public String bucketRegionHeader() { + return this.headerPrefix() + "bucket-region"; + } + + @Override + public String locationHeader() { + return this.headerPrefix() + "location"; + } + + @Override + public String successRedirectLocationHeader() { + return null; + } + + @Override + public String deleteMarkerHeader() { + return this.headerPrefix() + "delete-marker"; + } + + @Override + public String copySourceSseCKeyHeader() { + return this.headerPrefix() + "copy-source-server-side-encryption-customer-key"; + } + + @Override + public String copySourceSseCKeyMd5Header() { + return this.headerPrefix() + "copy-source-server-side-encryption-customer-key-MD5"; + } + + @Override + public String copySourceIfModifiedSinceHeader() { + return this.headerPrefix() + "copy-source-if-modified-since"; + } + + @Override + public String copySourceIfUnmodifiedSinceHeader() { + return this.headerPrefix() + "copy-source-if-unmodified-since"; + } + + @Override + public String copySourceIfNoneMatchHeader() { + return this.headerPrefix() + "copy-source-if-none-match"; + } + + @Override + public String copySourceIfMatchHeader() { + return this.headerPrefix() + "copy-source-if-match"; + } + + @Override + public String copySourceHeader() { + return this.headerPrefix() + "copy-source"; + } + + @Override + public String copySourceVersionIdHeader() { + return this.headerPrefix() + "copy-source-version-id"; + } + + @Override + public String expirationHeader() { + return this.headerPrefix() + "expiration"; + } + + @Override + public String restoreHeader() { + return this.headerPrefix() + "restore"; + } + + @Override + public String copySourceRangeHeader() { + return this.headerPrefix() + "copy-source-range"; + } + + @Override + public String securityTokenHeader() { + return this.headerPrefix() + "security-token"; + } + + @Override + public String contentSha256Header() { + return this.headerPrefix() + "content-sha256"; + } + + @Override + public String objectTypeHeader() { + return "x-obs-object-type"; + } + + @Override + public String nextPositionHeader() { + return "x-obs-next-append-position"; + } + + @Override + public String listTimeoutHeader() { + return this.headerPrefix() + "list-timeout"; + } + + @Override + public String fsFileInterfaceHeader() { + return "x-obs-fs-file-interface"; + } + + @Override + public String fsModeHeader() { + return this.headerMetaPrefix() + "mode"; + } + + @Override + public String azRedundancyHeader() { + return "x-obs-az-redundancy"; + } + + @Override + public String bucketTypeHeader() { + return "x-obs-bucket-type"; + } + + @Override + public String requestPaymentHeader() { + return this.headerPrefix() + "request-payer"; + } +} diff --git a/app/src/main/java/com/obs/services/internal/consensus/CacheManager.java b/app/src/main/java/com/obs/services/internal/consensus/CacheManager.java deleted file mode 100644 index 3cb6ef9..0000000 --- a/app/src/main/java/com/obs/services/internal/consensus/CacheManager.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co.,Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use - * this file except in compliance with the License. You may obtain a copy of the - * License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package com.obs.services.internal.consensus; - -import com.obs.services.model.AuthTypeEnum; - -import java.util.Date; -import java.util.concurrent.ConcurrentHashMap; - -/** - * 缓存管理器 - */ -public class CacheManager { - private ConcurrentHashMap apiVersionCache = new ConcurrentHashMap(); - - public CacheManager() {} - - private static class SingletonHolder { - private static final CacheManager INSTANCE = new CacheManager(); - } - - public static CacheManager getInstance() { - return SingletonHolder.INSTANCE; - } - - public void addApiVersion(String bucketName, AuthTypeEnum apiVersion) { - apiVersionCache.put(bucketName, new CacheData(apiVersion)); - } - - public AuthTypeEnum getApiVersionInCache(String bucketName) { - CacheData authTypeCache = apiVersionCache.get(bucketName); - return isValid(authTypeCache) ? authTypeCache.getApiVersion() : null; - } - - public void clear() { - if (apiVersionCache != null) { - apiVersionCache.clear(); - } - } - - public boolean isValid(CacheData authTypeCache) { - return authTypeCache != null && authTypeCache.getExpirationTime() >= new Date().getTime(); - } -} diff --git a/app/src/main/java/com/obs/services/internal/consensus/SegmentLock.java b/app/src/main/java/com/obs/services/internal/consensus/SegmentLock.java deleted file mode 100644 index 6c475f2..0000000 --- a/app/src/main/java/com/obs/services/internal/consensus/SegmentLock.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co.,Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use - * this file except in compliance with the License. You may obtain a copy of the - * License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package com.obs.services.internal.consensus; - -import java.util.concurrent.locks.ReentrantLock; - -public class SegmentLock { - - private static final int SEGMENT_NUM = 16; - - private ReentrantLock[] locks; - - public SegmentLock() { - locks = new ReentrantLock[SEGMENT_NUM]; - for (int i = 0; i < SEGMENT_NUM; i++) { - locks[i] = new ReentrantLock(); - } - } - - public static SegmentLock getInstance() { - return SegmentLockHolder.instance; - } - - private static class SegmentLockHolder{ - private static SegmentLock instance = new SegmentLock(); - } - - public void lock(final String key) { - locks[Math.abs(key.hashCode()) % SEGMENT_NUM].lock(); - } - - public void unlock(final String key) { - locks[Math.abs(key.hashCode()) % SEGMENT_NUM].unlock(); - } - - public void clear() { - locks = null; - } -} diff --git a/app/src/main/java/com/obs/services/internal/ext/ExtObsConfiguration.java b/app/src/main/java/com/obs/services/internal/ext/ExtObsConfiguration.java new file mode 100644 index 0000000..e82dfb7 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/ext/ExtObsConfiguration.java @@ -0,0 +1,33 @@ +package com.obs.services.internal.ext; + +import com.obs.services.ObsConfiguration; + +public class ExtObsConfiguration extends ObsConfiguration { + + private boolean retryOnConnectionFailureInOkhttp; + + // times for retryOnRetryOnUnexpectedEndException; + private int maxRetryOnUnexpectedEndException; + + public ExtObsConfiguration() { + super(); + this.retryOnConnectionFailureInOkhttp = ExtObsConstraint.DEFAULT_RETRY_ON_CONNECTION_FAILURE_IN_OKHTTP; + this.maxRetryOnUnexpectedEndException = ExtObsConstraint.DEFAULT_MAX_RETRY_ON_UNEXPECTED_END_EXCEPTION; + } + + public boolean isRetryOnConnectionFailureInOkhttp() { + return retryOnConnectionFailureInOkhttp; + } + + public void retryOnConnectionFailureInOkhttp(boolean retryOnConnectionFailureInOkhttp) { + this.retryOnConnectionFailureInOkhttp = retryOnConnectionFailureInOkhttp; + } + + public int getMaxRetryOnUnexpectedEndException() { + return maxRetryOnUnexpectedEndException; + } + + public void setMaxRetryOnUnexpectedEndException(int maxRetryOnUnexpectedEndException) { + this.maxRetryOnUnexpectedEndException = maxRetryOnUnexpectedEndException; + } +} diff --git a/app/src/main/java/com/obs/services/internal/ext/ExtObsConstraint.java b/app/src/main/java/com/obs/services/internal/ext/ExtObsConstraint.java new file mode 100644 index 0000000..b164e26 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/ext/ExtObsConstraint.java @@ -0,0 +1,14 @@ +package com.obs.services.internal.ext; + +public class ExtObsConstraint { + + public static final boolean DEFAULT_RETRY_ON_CONNECTION_FAILURE_IN_OKHTTP = false; + + public static final int DEFAULT_MAX_RETRY_ON_UNEXPECTED_END_EXCEPTION = -1; + + public static final String IS_RETRY_ON_CONNECTION_FAILURE_IN_OKHTTP = + "httpclient.is-retry-on-connection-failure-in-okhttp"; + + public static final String HTTP_MAX_RETRY_ON_UNEXPECTED_END_EXCEPTION = + "httpclient.max-retry-on-unexpected-end-exception"; +} diff --git a/app/src/main/java/com/obs/services/internal/handler/DefaultXmlHandler.java b/app/src/main/java/com/obs/services/internal/handler/DefaultXmlHandler.java index 009147f..416595a 100644 --- a/app/src/main/java/com/obs/services/internal/handler/DefaultXmlHandler.java +++ b/app/src/main/java/com/obs/services/internal/handler/DefaultXmlHandler.java @@ -1,65 +1,59 @@ /** - * - * JetS3t : Java S3 Toolkit - * Project hosted at http://bitbucket.org/jmurty/jets3t/ - * - * Copyright 2006-2010 James Murty - * * Copyright 2019 Huawei Technologies Co.,Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services.internal.handler; -import java.util.LinkedList; +package com.obs.services.internal.handler; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; -public abstract class DefaultXmlHandler extends DefaultHandler{ +public abstract class DefaultXmlHandler extends DefaultHandler { - private StringBuilder currText = null; - private final LinkedList context = new LinkedList(); + private StringBuilder currText = null; - public void startDocument() { - return; - } + @Override + public void startDocument() { + return; + } - public void endDocument() { - return; - } + @Override + public void endDocument() { + return; + } - public void startElement(String uri, String name, String qName, Attributes attrs) { - this.currText = new StringBuilder(); - this.startElement(name, attrs); - } + @Override + public void startElement(String uri, String name, String qualifiedName, Attributes attrs) { + this.currText = new StringBuilder(); + this.startElement(name, attrs); + } - public void startElement(String name, Attributes attrs) { - this.startElement(name); - } + public void startElement(String name, Attributes attrs) { + this.startElement(name); + } - public void startElement(String name) { - context.add(name); - } + public void startElement(String name) { + return; + } - public void endElement(String uri, String name, String qName) { - String elementText = this.currText.toString(); - this.endElement(name, elementText); - } + @Override + public void endElement(String uri, String name, String qualifiedName) { + String elementText = this.currText.toString(); + this.endElement(name, elementText); + } - public void endElement(String name, String content) { - context.removeLast(); - } + public abstract void endElement(String name, String content); - public void characters(char ch[], int start, int length) { - this.currText.append(ch, start, length); - } + public void characters(char[] ch, int start, int length) { + this.currText.append(ch, start, length); + } } diff --git a/app/src/main/java/com/obs/services/internal/handler/SimpleHandler.java b/app/src/main/java/com/obs/services/internal/handler/SimpleHandler.java index 5ef1f3e..25f0fae 100644 --- a/app/src/main/java/com/obs/services/internal/handler/SimpleHandler.java +++ b/app/src/main/java/com/obs/services/internal/handler/SimpleHandler.java @@ -1,23 +1,22 @@ /** - * JetS3t : Java S3 Toolkit - * Project hosted at http://bitbucket.org/jmurty/jets3t/ - * - * Copyright 2006-2010 James Murty * Copyright 2019 Huawei Technologies Co.,Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.handler; import java.lang.reflect.Method; +import java.util.Deque; +import java.util.concurrent.LinkedBlockingDeque; import org.xml.sax.Attributes; import org.xml.sax.XMLReader; @@ -26,91 +25,107 @@ import com.obs.log.ILogger; import com.obs.log.LoggerBuilder; -public abstract class SimpleHandler extends DefaultHandler{ - private static final ILogger log = LoggerBuilder.getLogger(SimpleHandler.class); - - protected XMLReader xr = null; - private StringBuffer textContent = null; - protected SimpleHandler currentHandler = null; - protected SimpleHandler parentHandler = null; - - public SimpleHandler(XMLReader xr) { - this.xr = xr; - this.textContent = new StringBuffer(); - currentHandler = this; - } - - public void transferControlToHandler(SimpleHandler toHandler) { - currentHandler = toHandler; - toHandler.parentHandler = this; - xr.setContentHandler(currentHandler); - xr.setErrorHandler(currentHandler); - } - - public void returnControlToParentHandler() { - if (isChildHandler()) { - parentHandler.currentHandler = parentHandler; - parentHandler.controlReturned(this); - currentHandler = parentHandler; - xr.setContentHandler(currentHandler); - xr.setErrorHandler(currentHandler); - } else { - if(log.isDebugEnabled()) { - log.debug("Ignoring call to return control to parent handler, as this class has no parent: " - + this.getClass().getName()); - } - } - } - - public boolean isChildHandler() { - return parentHandler != null; - } - - public void controlReturned(SimpleHandler childHandler) { - return; - } - - @Override - public void startElement(String uri, String name, String qName, Attributes attrs) { - try { - Method method = currentHandler.getClass().getMethod("start" + name, new Class[] {}); - method.invoke(currentHandler, new Object[] {}); - } catch (NoSuchMethodException e) { - if(log.isDebugEnabled()) { - log.debug("Skipped non-existent SimpleHandler subclass's startElement method for '" + name + "' in " - + this.getClass().getName()); - } - } catch (Throwable t) { - if(log.isErrorEnabled()) { - log.error("Unable to invoke SimpleHandler subclass's startElement method for '" + name + "' in " - + this.getClass().getName(), t); - } - } - } - - @Override - public void endElement(String uri, String name, String qName) { - String elementText = this.textContent.toString().trim(); - try { - Method method = currentHandler.getClass().getMethod("end" + name, new Class[] { String.class }); - method.invoke(currentHandler, new Object[] { elementText }); - } catch (NoSuchMethodException e) { - if(log.isDebugEnabled()) { - log.debug("Skipped non-existent SimpleHandler subclass's endElement method for '" + name + "' in " - + this.getClass().getName()); - } - } catch (Throwable t) { - if(log.isErrorEnabled()) { - log.error("Unable to invoke SimpleHandler subclass's endElement method for '" + name + "' in " - + this.getClass().getName(), t); - } - } - this.textContent = new StringBuffer(); - } - - @Override - public void characters(char ch[], int start, int length) { - this.textContent.append(ch, start, length); - } +public abstract class SimpleHandler extends DefaultHandler { + private static final ILogger LOG = LoggerBuilder.getLogger(SimpleHandler.class); + + protected XMLReader xr; + private StringBuilder textBuffer; + + private Deque handlerStack = new LinkedBlockingDeque<>(); + + public SimpleHandler(XMLReader xr) { + this.xr = xr; + this.textBuffer = new StringBuilder(); + this.handlerStack.push(this); + } + + public void transferControl(SimpleHandler toHandler) { + toHandler.setHandlerStack(this.handlerStack); + this.handlerStack.push(toHandler); + setReaderHandler(handlerStack.peek()); + } + + public void returnControlToParentHandler() { + if (hasParentHandler()) { + this.handlerStack.removeFirst(); + this.handlerStack.peek().controlReturned(this); + setReaderHandler(handlerStack.peek()); + } else { + if (LOG.isDebugEnabled()) { + LOG.debug("this class has no parent: " + this.getClass().getName()); + } + } + } + + public boolean hasParentHandler() { + return handlerStack.size() >= 2; + } + + public void controlReturned(SimpleHandler childHandler) { + return; + } + + public void setHandlerStack(Deque handlerStack) { + this.handlerStack = handlerStack; + } + + @Override + public void startElement(String uri, String name, String qualifiedName, Attributes attrs) { + invokeMethodWithoutException("start" + name, null); + } + + @Override + public void endElement(String uri, String name, String qualifiedName) { + String elementContent = this.textBuffer.toString(); + + invokeMethodWithoutException("end" + name, elementContent); + + this.textBuffer = new StringBuilder(); + } + + private void setReaderHandler(SimpleHandler handler) { + xr.setContentHandler(handler); + xr.setErrorHandler(handler); + } + + @SuppressWarnings("rawtypes") + private void invokeMethodWithoutException(String methodName, String parameter) { + SimpleHandler handler = this.handlerStack.peek(); + + if (null == handler) { + if (LOG.isInfoEnabled()) { + LOG.info("non-existent SimpleHandler in " + this.getClass().getName()); + } + return; + } + + Class[] clazz = new Class[] {}; + Object[] parameters = new Object[] {}; + + if (null != parameter) { + clazz = new Class[] {String.class}; + parameters = new Object[] {parameter}; + } + + try { + Method method = handler.getClass().getMethod(methodName, clazz); + method.invoke(handler, parameters); + } catch (NoSuchMethodException e) { + if (LOG.isDebugEnabled()) { + LOG.debug("non-existent SimpleHandler subclass's method for '" + methodName + "' in " + + this.getClass().getName()); + } + } catch (Throwable t) { + if (LOG.isErrorEnabled()) { + LOG.error("Unable to invoke SimpleHandler subclass's method for '" + methodName + "' in " + + this.getClass().getName(), t); + } + } + } + + @Override + public void characters(char[] ch, int start, int length) { + this.textBuffer.append(ch, start, length); + } } diff --git a/app/src/main/java/com/obs/services/internal/handler/XmlResponsesSaxParser.java b/app/src/main/java/com/obs/services/internal/handler/XmlResponsesSaxParser.java index 0b0dc89..6c90897 100644 --- a/app/src/main/java/com/obs/services/internal/handler/XmlResponsesSaxParser.java +++ b/app/src/main/java/com/obs/services/internal/handler/XmlResponsesSaxParser.java @@ -1,38 +1,18 @@ /** - * JetS3t : Java S3 Toolkit - * Project hosted at http://bitbucket.org/jmurty/jets3t/ - * - * Copyright 2006-2010 James Murty - * * Copyright 2019 Huawei Technologies Co.,Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services.internal.handler; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.lang.reflect.Constructor; -import java.text.ParseException; -import java.util.ArrayList; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; -import com.obs.services.model.BucketTypeEnum; -import org.xml.sax.InputSource; -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.DefaultHandler; +package com.obs.services.internal.handler; import com.obs.log.ILogger; import com.obs.log.LoggerBuilder; @@ -43,6 +23,7 @@ import com.obs.services.model.AccessControlList; import com.obs.services.model.BucketCors; import com.obs.services.model.BucketCorsRule; +import com.obs.services.model.BucketCustomDomainInfo; import com.obs.services.model.BucketDirectColdAccess; import com.obs.services.model.BucketEncryption; import com.obs.services.model.BucketLoggingConfiguration; @@ -51,6 +32,7 @@ import com.obs.services.model.BucketStorageInfo; import com.obs.services.model.BucketStoragePolicyConfiguration; import com.obs.services.model.BucketTagInfo; +import com.obs.services.model.BucketTypeEnum; import com.obs.services.model.BucketVersioningConfiguration; import com.obs.services.model.CanonicalGrantee; import com.obs.services.model.CopyPartResult; @@ -60,6 +42,7 @@ import com.obs.services.model.GrantAndPermission; import com.obs.services.model.GranteeInterface; import com.obs.services.model.GroupGrantee; +import com.obs.services.model.HistoricalObjectReplicationEnum; import com.obs.services.model.InitiateMultipartUploadResult; import com.obs.services.model.LifecycleConfiguration; import com.obs.services.model.Multipart; @@ -72,6 +55,8 @@ import com.obs.services.model.Redirect; import com.obs.services.model.RedirectAllRequest; import com.obs.services.model.ReplicationConfiguration; +import com.obs.services.model.RequestPaymentConfiguration; +import com.obs.services.model.RequestPaymentEnum; import com.obs.services.model.RouteRule; import com.obs.services.model.RouteRuleCondition; import com.obs.services.model.RuleStatusEnum; @@ -81,1712 +66,2350 @@ import com.obs.services.model.VersionOrDeleteMarker; import com.obs.services.model.VersioningStatusEnum; import com.obs.services.model.WebsiteConfiguration; +import com.obs.services.model.fs.DirContentSummary; +import com.obs.services.model.fs.DirSummary; +import com.obs.services.model.fs.FolderContentSummary; +import com.obs.services.model.fs.ListContentSummaryFsResult; +import org.xml.sax.InputSource; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.DefaultHandler; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Constructor; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; public class XmlResponsesSaxParser { - - private static final ILogger log = LoggerBuilder.getLogger("com.obs.services.internal.RestStorageService"); - - private XMLReader xmlReader; - - public XmlResponsesSaxParser() throws ServiceException { - this.xmlReader = ServiceUtils.loadXMLReader(); - } - - protected void parseXmlInputStream(DefaultHandler handler, InputStream inputStream) throws ServiceException { - if (inputStream == null) { - return; - } - try { - xmlReader.setErrorHandler(handler); - xmlReader.setContentHandler(handler); - xmlReader.parse(new InputSource(inputStream)); - } catch (Exception t) { - throw new ServiceException("Failed to parse XML document with handler " + handler.getClass(), t); - } finally { - ServiceUtils.closeStream(inputStream); - } - } - - protected InputStream sanitizeXmlDocument(InputStream inputStream) throws ServiceException { - if (inputStream == null) { - return null; - } - BufferedReader br = null; - try { - StringBuilder listingDocBuffer = new StringBuilder(); - br = new BufferedReader(new InputStreamReader(inputStream, Constants.DEFAULT_ENCODING)); - - char[] buf = new char[8192]; - int read = -1; - while ((read = br.read(buf)) != -1) { - listingDocBuffer.append(buf, 0, read); - } - - String listingDoc = listingDocBuffer.toString().replaceAll("\r", " "); - if(log.isTraceEnabled()) { - log.trace("Response entity: " + listingDoc); - } - return new ByteArrayInputStream(listingDoc.getBytes(Constants.DEFAULT_ENCODING)); - } catch (Throwable t) { - throw new ServiceException("Failed to sanitize XML document destined", t); - } finally { - ServiceUtils.closeStream(br); - ServiceUtils.closeStream(inputStream); - } - } - - public T parse(InputStream inputStream, Class handlerClass, boolean sanitize) throws ServiceException{ - try { - T handler = null; - if(SimpleHandler.class.isAssignableFrom(handlerClass)) { - Constructor c = handlerClass.getConstructor(XMLReader.class); - handler = c.newInstance(this.xmlReader); - }else { - handler = handlerClass.getConstructor().newInstance(); - } - if(handler instanceof DefaultHandler) { - if(sanitize) { - inputStream = sanitizeXmlDocument(inputStream); - } - parseXmlInputStream((DefaultHandler)handler, inputStream); - } - return handler; - }catch (ServiceException e) { - throw e; - }catch(Exception e) { - throw new ServiceException(e); - } - } - - - public static class ListObjectsHandler extends DefaultXmlHandler { - private ObsObject currentObject; - - private Owner currentOwner; - - private boolean insideCommonPrefixes = false; - - private final List objects = new ArrayList(); - - private final List commonPrefixes = new ArrayList(); - - private String bucketName; - - private String requestPrefix; - - private String requestMarker; - - private String requestDelimiter; - - private int requestMaxKeys = 0; - - private boolean listingTruncated = false; - - private String lastKey; - - private String nextMarker; - - public String getMarkerForNextListing() { - return listingTruncated ? nextMarker == null ? lastKey : nextMarker : null; - } - - public String getBucketName() { - return bucketName; - } - - public boolean isListingTruncated() { - return listingTruncated; - } - - public List getObjects() { - return this.objects; - } - - public List getCommonPrefixes() { - return commonPrefixes; - } - - public String getRequestPrefix() { - return requestPrefix; - } - - public String getRequestMarker() { - return requestMarker; - } - - public String getNextMarker() { - return nextMarker; - } - - public int getRequestMaxKeys() { - return requestMaxKeys; - } - - public String getRequestDelimiter() { - return requestDelimiter; - } - - @Override - public void startElement(String name) { - if (name.equals("Contents")) { - currentObject = new ObsObject(); - currentObject.setBucketName(bucketName); - } else if (name.equals("Owner")) { - currentOwner = new Owner(); - } else if (name.equals("CommonPrefixes")) { - insideCommonPrefixes = true; - } - } - - @Override - public void endElement(String name, String elementText) { - try { - if (name.equals("Name")) { - bucketName = elementText; - } else if (!insideCommonPrefixes && name.equals("Prefix")) { - requestPrefix = elementText; - } else if (name.equals("Marker")) { - requestMarker = elementText; - } else if (name.equals("NextMarker")) { - nextMarker = elementText; - } else if (name.equals("MaxKeys")) { - requestMaxKeys = Integer.parseInt(elementText); - } else if (name.equals("Delimiter")) { - requestDelimiter = elementText; - } else if (name.equals("IsTruncated")) { - listingTruncated = Boolean.valueOf(elementText); - }else if (name.equals("Contents")) { - objects.add(currentObject); - } else if (name.equals("Key")) { - currentObject.setObjectKey(elementText); - lastKey = elementText; - } else if (name.equals("LastModified")) { - try { - currentObject.getMetadata().setLastModified(ServiceUtils.parseIso8601Date(elementText)); - } catch (ParseException e) { - if(log.isErrorEnabled()) { - log.error("Non-ISO8601 date for LastModified in bucket's object listing output: " + elementText, e); - } - } - } else if (name.equals("ETag")) { - currentObject.getMetadata().setEtag(elementText); - } else if (name.equals("Size")) { - currentObject.getMetadata().setContentLength(Long.parseLong(elementText)); - } else if (name.equals("StorageClass")) { - currentObject.getMetadata().setObjectStorageClass(StorageClassEnum.getValueFromCode(elementText)); - }else if (name.equals("ID")) { - if (currentOwner == null) { - currentOwner = new Owner(); - } - currentObject.setOwner(currentOwner); - currentOwner.setId(elementText); - }else if(name.equals("Type")) { - currentObject.getMetadata().setAppendable("Appendable".equals(elementText)); - } else if (name.equals("DisplayName")) { - if(currentOwner != null) { - currentOwner.setDisplayName(elementText); - } - }else if (insideCommonPrefixes && name.equals("Prefix")) { - commonPrefixes.add(elementText); - } else if (name.equals("CommonPrefixes")) { - insideCommonPrefixes = false; - } - }catch (NullPointerException e) { - if(log.isErrorEnabled()) { - log.error("Response xml is not well-formt", e); - } - } - } - } - - public static class ListBucketsHandler extends DefaultXmlHandler { - private Owner bucketsOwner; - - private ObsBucket currentBucket; - - private final List buckets = new ArrayList(); - - public List getBuckets() { - return this.buckets; - } - - public Owner getOwner() { - return bucketsOwner; - } - - @Override - public void startElement(String name) { - if (name.equals("Bucket")) { - currentBucket = new ObsBucket(); - } else if (name.equals("Owner")) { - bucketsOwner = new Owner(); - } - } - - @Override - public void endElement(String name, String elementText) { - try { - if (name.equals("ID")) { - bucketsOwner.setId(elementText); - } else if (name.equals("DisplayName")) { - bucketsOwner.setDisplayName(elementText); - } else if (name.equals("Bucket")) { - currentBucket.setOwner(bucketsOwner); - buckets.add(currentBucket); - } else if (name.equals("Name")) { - currentBucket.setBucketName(elementText); - } else if (name.equals("Location")) { - currentBucket.setLocation(elementText); - } else if (name.equals("CreationDate")) { - elementText += ".000Z"; - try { - currentBucket.setCreationDate(ServiceUtils.parseIso8601Date(elementText)); - } catch (ParseException e) { - if(log.isWarnEnabled()) { - log.warn("Non-ISO8601 date for CreationDate in list buckets output: " + elementText, e); - } - } - } else if (name.equals("BucketType")) { - if (Constants.POSIX.equals(elementText)) { - currentBucket.setBucketType(BucketTypeEnum.PFS); - } else { - currentBucket.setBucketType(BucketTypeEnum.OBJECT); - } - } - }catch (NullPointerException e) { - if(log.isErrorEnabled()) { - log.error("Response xml is not well-formt", e); - } - } - } - } - - public static class BucketLoggingHandler extends DefaultXmlHandler { - - private BucketLoggingConfiguration bucketLoggingStatus = new BucketLoggingConfiguration(); - - private String targetBucket; - - private String targetPrefix; - - private GranteeInterface currentGrantee; - - private Permission currentPermission; - - private boolean currentDelivered; - - public BucketLoggingConfiguration getBucketLoggingStatus() { - return bucketLoggingStatus; - } - - @Override - public void endElement(String name, String elementText) { - if (name.equals("TargetBucket")) { - targetBucket = elementText; - } else if (name.equals("TargetPrefix")) { - targetPrefix = elementText; - } else if (name.equals("LoggingEnabled")) { - bucketLoggingStatus.setTargetBucketName(targetBucket); - bucketLoggingStatus.setLogfilePrefix(targetPrefix); - } else if (name.equals("Agency")) { - bucketLoggingStatus.setAgency(elementText); - } else if (name.equals("ID")) { - currentGrantee = new CanonicalGrantee(); - currentGrantee.setIdentifier(elementText); - } else if (name.equals("URI") || name.equals("Canned")) { - currentGrantee = new GroupGrantee(); - currentGrantee.setIdentifier(elementText); - } else if (name.equals("DisplayName")) { - if(currentGrantee instanceof CanonicalGrantee) { - ((CanonicalGrantee) currentGrantee).setDisplayName(elementText); - } - } else if(name.equals("Delivered")) { - currentDelivered = Boolean.parseBoolean(elementText); - } else if (name.equals("Permission")) { - currentPermission = Permission.parsePermission(elementText); - } else if (name.equals("Grant")) { - GrantAndPermission gap = new GrantAndPermission(currentGrantee, currentPermission); - gap.setDelivered(currentDelivered); - bucketLoggingStatus.addTargetGrant(gap); - } - } - } - - public static class BucketLocationHandler extends DefaultXmlHandler { - private String location; - - public String getLocation() { - return location; - } - - @Override - public void endElement(String name, String elementText) { - if (name.equals("LocationConstraint") || name.equals("Location")) { - location = elementText; - } - } - } - - public static class BucketCorsHandler extends DefaultXmlHandler { - - private final BucketCors configuration = new BucketCors(); - - private BucketCorsRule currentRule; - - private List allowedMethods = null; - - private List allowedOrigins = null; - - private List exposedHeaders = null; - - private List allowedHeaders = null; - - public BucketCors getConfiguration() { - return configuration; - } - - @Override - public void startElement(String name) { - - if ("CORSRule".equals(name)) { - currentRule = new BucketCorsRule(); - } - if ("AllowedOrigin".equals(name)) { - if (allowedOrigins == null) { - allowedOrigins = new ArrayList(); - } - } else if ("AllowedMethod".equals(name)) { - if (allowedMethods == null) { - allowedMethods = new ArrayList(); - } - } else if ("ExposeHeader".equals(name)) { - if (exposedHeaders == null) { - exposedHeaders = new ArrayList(); - } - } else if ("AllowedHeader".equals(name)) { - if (allowedHeaders == null) { - allowedHeaders = new LinkedList(); - } - } - } - - @Override - public void endElement(String name, String elementText) { - if (name.equals("CORSRule")) { - currentRule.setAllowedHeader(allowedHeaders); - currentRule.setAllowedMethod(allowedMethods); - currentRule.setAllowedOrigin(allowedOrigins); - currentRule.setExposeHeader(exposedHeaders); - configuration.getRules().add(currentRule); - allowedHeaders = null; - allowedMethods = null; - allowedOrigins = null; - exposedHeaders = null; - currentRule = null; - } - if (name.equals("ID") && (null != currentRule)) { - currentRule.setId(elementText); - - } else if (name.equals("AllowedOrigin") && (null != allowedOrigins)) { - allowedOrigins.add(elementText); - - } else if (name.equals("AllowedMethod") && (null != allowedMethods)) { - allowedMethods.add(elementText); - - } else if (name.equals("MaxAgeSeconds") && (null != currentRule)) { - currentRule.setMaxAgeSecond(Integer.parseInt(elementText)); - - } else if (name.equals("ExposeHeader") && (null != exposedHeaders)) { - exposedHeaders.add(elementText); - - } else if (name.equals("AllowedHeader") && (null != allowedHeaders)) { - allowedHeaders.add(elementText); - } - } - } - - public static class CopyObjectResultHandler extends DefaultXmlHandler { - private String etag; - - private Date lastModified; - - public Date getLastModified() { - return ServiceUtils.cloneDateIgnoreNull(lastModified); - } - - public String getETag() { - return etag; - } - - @Override - public void endElement(String name, String elementText) { - if (name.equals("LastModified")) { - try { - lastModified = ServiceUtils.parseIso8601Date(elementText); - } catch (ParseException e) { - if(log.isErrorEnabled()) { - log.error("Non-ISO8601 date for LastModified in copy object output: " + elementText, e); - } - } - } else if (name.equals("ETag")) { - etag = elementText; - } - } - } - - public static class RequestPaymentConfigurationHandler extends DefaultXmlHandler { - private String payer = null; - - public boolean isRequesterPays() { - return "Requester".equals(payer); - } - - @Override - public void endElement(String name, String elementText) { - if (name.equals("Payer")) { - payer = elementText; - } - } - } - - public static class BucketVersioningHandler extends DefaultXmlHandler { - - private BucketVersioningConfiguration versioningStatus; - - private String status; - - public BucketVersioningConfiguration getVersioningStatus() { - return this.versioningStatus; - } - - @Override - public void endElement(String name, String elementText) { - if (name.equals("Status")) { - this.status = elementText; - } else if (name.equals("VersioningConfiguration")) { - this.versioningStatus = new BucketVersioningConfiguration( - VersioningStatusEnum.getValueFromCode(this.status)); - } - } - } - - public static class ListVersionsHandler extends DefaultXmlHandler { - - private final List items = new ArrayList(); - - private final List commonPrefixes = new ArrayList(); - - private String key; - - private String versionId; - - private boolean isLatest = false; - - private Date lastModified; - - private Owner owner; - - private String etag; - - private long size = 0; - - private String storageClass; - - private boolean isAppendable; - - private boolean insideCommonPrefixes = false; - - // Listing properties. - private String bucketName; - - private String requestPrefix; - - private String keyMarker; - - private String versionIdMarker; - - private long requestMaxKeys = 0; - - private boolean listingTruncated = false; - - private String nextMarker; - - private String nextVersionIdMarker; - - private String delimiter; - - public String getDelimiter() { - return this.delimiter; - } - - public String getBucketName() { - return this.bucketName; - } - - public boolean isListingTruncated() { - return listingTruncated; - } - - public List getItems() { - return this.items; - } - - public List getCommonPrefixes() { - return commonPrefixes; - } - - public String getRequestPrefix() { - return requestPrefix; - } - - public String getKeyMarker() { - return keyMarker; - } - - public String getVersionIdMarker() { - return versionIdMarker; - } - - public String getNextKeyMarker() { - return nextMarker; - } - - public String getNextVersionIdMarker() { - return nextVersionIdMarker; - } - - public long getRequestMaxKeys() { - return requestMaxKeys; - } - - private void reset() { - this.key = null; - this.versionId = null; - this.isLatest = false; - this.lastModified = null; - this.etag = null; - this.isAppendable = false; - this.size = 0; - this.storageClass = null; - this.owner = null; - } - - @Override - public void startElement(String name) { - if (name.equals("Owner")) { - owner = new Owner(); - } else if (name.equals("CommonPrefixes")) { - insideCommonPrefixes = true; - } - } - - @Override - public void endElement(String name, String elementText) { - try { - if (name.equals("Name")) { - bucketName = elementText; - } else if (!insideCommonPrefixes && name.equals("Prefix")) { - requestPrefix = elementText; - } else if (name.equals("KeyMarker")) { - keyMarker = elementText; - } else if (name.equals("NextKeyMarker")) { - nextMarker = elementText; - } else if (name.equals("VersionIdMarker")) { - versionIdMarker = elementText; - } else if (name.equals("NextVersionIdMarker")) { - nextVersionIdMarker = elementText; - } else if (name.equals("MaxKeys")) { - requestMaxKeys = Long.parseLong(elementText); - } else if (name.equals("IsTruncated")) { - listingTruncated = Boolean.valueOf(elementText); - } else if (name.equals("Delimiter")) { - delimiter = elementText; - } else if (name.equals("Version")) { - VersionOrDeleteMarker item = new VersionOrDeleteMarker(bucketName, key, versionId, isLatest, lastModified, owner, etag, size, StorageClassEnum.getValueFromCode(storageClass), false, isAppendable); - items.add(item); - this.reset(); - } else if (name.equals("DeleteMarker")) { - VersionOrDeleteMarker item = new VersionOrDeleteMarker(bucketName, key, versionId, isLatest, lastModified, owner, null, 0, null, true, false); - items.add(item); - this.reset(); - } else if (name.equals("Key")) { - key = elementText; - } else if (name.equals("VersionId")) { - versionId = elementText; - } else if (name.equals("IsLatest")) { - isLatest = Boolean.valueOf(elementText); - } else if (name.equals("LastModified")) { - try { - lastModified = ServiceUtils.parseIso8601Date(elementText); - } catch (ParseException e) { - if(log.isWarnEnabled()) { - log.warn("Non-ISO8601 date for LastModified in bucket's versions listing output: " + elementText, e); - } - } - } else if (name.equals("ETag")) { - etag = elementText; - } else if (name.equals("Size")) { - size = Long.parseLong(elementText); - } else if (name.equals("StorageClass")) { - storageClass = elementText; - } else if (name.equals("Type")) { - isAppendable = "Appendable".equals(elementText); - } else if (name.equals("ID")) { - if(owner == null) { - owner = new Owner(); - } - owner.setId(elementText); - } else if (name.equals("DisplayName")) { - if(owner != null) { - owner.setDisplayName(elementText); - } - }else if (insideCommonPrefixes && name.equals("Prefix")) { - commonPrefixes.add(elementText); - } else if (name.equals("CommonPrefixes")) { - insideCommonPrefixes = false; - } - }catch (NullPointerException e) { - if(log.isErrorEnabled()) { - log.error("Response xml is not well-formt", e); - } - } - } - } - - public static class OwnerHandler extends SimpleHandler { - private String id; - - private String displayName; - - public OwnerHandler(XMLReader xr) { - super(xr); - } - - public Owner getOwner() { - Owner owner = new Owner(); - owner.setId(id); - owner.setDisplayName(displayName); - return owner; - } - - public void endID(String text) { - this.id = text; - } - - public void endDisplayName(String text) { - this.displayName = text; - } - - public void endOwner(String text) { - returnControlToParentHandler(); - } - - public void endInitiator(String text) { - returnControlToParentHandler(); - } - } - - public static class InitiateMultipartUploadHandler extends SimpleHandler { - private String uploadId; - - private String bucketName; - - private String objectKey; - - public InitiateMultipartUploadHandler(XMLReader xr) { - super(xr); - } - - public InitiateMultipartUploadResult getInitiateMultipartUploadResult() { - InitiateMultipartUploadResult result = new InitiateMultipartUploadResult(bucketName, objectKey, uploadId); - return result; - } - - public void endUploadId(String text) { - this.uploadId = text; - } - - public void endBucket(String text) { - this.bucketName = text; - } - - public void endKey(String text) { - this.objectKey = text; - } - } - - public static class MultipartUploadHandler extends SimpleHandler { - private String uploadId; - - private String objectKey; - - private String storageClass; - - private Owner owner; - - private Owner initiator; - - private Date initiatedDate; - - private boolean inInitiator = false; - - public MultipartUploadHandler(XMLReader xr) { - super(xr); - } - - public MultipartUpload getMultipartUpload() { - MultipartUpload multipartUpload = new MultipartUpload(uploadId, objectKey, initiatedDate, StorageClassEnum.getValueFromCode(storageClass), owner, initiator); - return multipartUpload; - } - public void endUploadId(String text) { - this.uploadId = text; - } + private static final ILogger log = LoggerBuilder.getLogger("com.obs.services.internal.RestStorageService"); - public void endKey(String text) { - this.objectKey = text; - } + private XMLReader xmlReader; - public void endStorageClass(String text) { - this.storageClass = text; - } + public XmlResponsesSaxParser() throws ServiceException { + this.xmlReader = ServiceUtils.loadXMLReader(); + } - public void endInitiated(String text) { - try { - this.initiatedDate = ServiceUtils.parseIso8601Date(text); - } catch (ParseException e) { - } - } + protected void parseXmlInputStream(DefaultHandler handler, InputStream inputStream) throws ServiceException { + if (inputStream == null) { + return; + } + try { + xmlReader.setErrorHandler(handler); + xmlReader.setContentHandler(handler); + xmlReader.parse(new InputSource(inputStream)); + } catch (Exception t) { + throw new ServiceException("Failed to parse XML document with handler " + handler.getClass(), t); + } finally { + ServiceUtils.closeStream(inputStream); + } + } + + protected InputStream sanitizeXmlDocument(InputStream inputStream) throws ServiceException { + if (inputStream == null) { + return null; + } + BufferedReader br = null; + try { + StringBuilder listingDocBuffer = new StringBuilder(); + br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); + + char[] buf = new char[8192]; + int read = -1; + while ((read = br.read(buf)) != -1) { + listingDocBuffer.append(buf, 0, read); + } - public void startOwner() { - inInitiator = false; - transferControlToHandler(new OwnerHandler(xr)); - } + String listingDoc = listingDocBuffer.toString().replaceAll("\r", " "); + if (log.isTraceEnabled()) { + log.trace("Response entity: " + listingDoc); + } + return new ByteArrayInputStream(listingDoc.getBytes(StandardCharsets.UTF_8)); + } catch (Throwable t) { + throw new ServiceException("Failed to sanitize XML document destined", t); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + ServiceUtils.closeStream(inputStream); + } + } + + public T parse(InputStream inputStream, Class handlerClass, boolean sanitize) throws ServiceException { + try { + T handler = null; + if (SimpleHandler.class.isAssignableFrom(handlerClass)) { + Constructor c = handlerClass.getConstructor(XMLReader.class); + handler = c.newInstance(this.xmlReader); + } else { + handler = handlerClass.getConstructor().newInstance(); + } + if (handler instanceof DefaultHandler) { + if (sanitize) { + inputStream = sanitizeXmlDocument(inputStream); + } + parseXmlInputStream((DefaultHandler) handler, inputStream); + } + return handler; + } catch (Exception e) { + throw new ServiceException(e); + } + } - public void startInitiator() { - inInitiator = true; - transferControlToHandler(new OwnerHandler(xr)); - } + public static String getDecodedString(String value, boolean needDecode) { + if (needDecode && value != null) { + try { + return URLDecoder.decode(value, "UTF-8"); + } catch (UnsupportedEncodingException exception) { + throw new ServiceException(exception); + } + } + return value; + } - @Override - public void controlReturned(SimpleHandler childHandler) { - if (inInitiator) { - this.owner = ((OwnerHandler) childHandler).getOwner(); - } else { - this.initiator = ((OwnerHandler) childHandler).getOwner(); - } - } - public void endUpload(String text) { - returnControlToParentHandler(); - } - } + public static class ListObjectsHandler extends DefaultXmlHandler { + private ObsObject currentObject; - public static class ListMultipartUploadsHandler extends SimpleHandler { - - private final List uploads = new ArrayList(); + private Owner currentOwner; - private final List commonPrefixes = new ArrayList(); + private boolean insideCommonPrefixes = false; - private boolean insideCommonPrefixes; + private final List objects = new ArrayList(); - private String bucketName; + private final List commonPrefixes = new ArrayList(); - private String keyMarker; + private final List finalCommonPrefixes = new ArrayList<>(); - private String uploadIdMarker; + private ObsObject currentExtendCommonPrefix; - private String nextKeyMarker; + private final List extendCommonPrefixes = new ArrayList(); - private String nextUploadIdMarker; + private String bucketName; - private String delimiter; + private String requestPrefix; - private int maxUploads; - - private String prefix; + private String requestMarker; - private boolean isTruncated = false; - - public ListMultipartUploadsHandler(XMLReader xr) { - super(xr); - } - - public List getMultipartUploadList() { - for (MultipartUpload upload : uploads) { - upload.setBucketName(bucketName); - } - return uploads; - } + private String requestDelimiter; - public String getBucketName() { - return this.bucketName; - } - - public boolean isTruncated() { - return isTruncated; - } - - public String getKeyMarker() { - return keyMarker; - } - - public String getUploadIdMarker() { - return uploadIdMarker; - } - - public String getNextKeyMarker() { - return nextKeyMarker; - } - - public String getNextUploadIdMarker() { - return nextUploadIdMarker; - } - - public int getMaxUploads() { - return maxUploads; - } - - public List getCommonPrefixes() { - return commonPrefixes; - } - - public String getDelimiter() { - return this.delimiter; - } - - public String getPrefix() { - return this.prefix; - } + private int requestMaxKeys = 0; - public void startUpload() { - transferControlToHandler(new MultipartUploadHandler(xr)); - } + private boolean listingTruncated = false; - public void startCommonPrefixes() { - insideCommonPrefixes = true; - } + private String lastKey; - @Override - public void controlReturned(SimpleHandler childHandler) { - uploads.add(((MultipartUploadHandler) childHandler).getMultipartUpload()); - } + private String nextMarker; - public void endDelimiter(String text) { - this.delimiter = text; - } + private String encodingType; - public void endBucket(String text) { - this.bucketName = text; - } + private boolean needDecode; - public void endKeyMarker(String text) { - this.keyMarker = text; - } + public String getMarkerForNextListing() { + return getDecodedString(listingTruncated ? nextMarker == null ? lastKey : nextMarker : null, needDecode); + } - public void endUploadIdMarker(String text) { - this.uploadIdMarker = text; - } + public String getBucketName() { + return bucketName; + } - public void endNextKeyMarker(String text) { - this.nextKeyMarker = text; - } + public boolean isListingTruncated() { + return listingTruncated; + } - public void endNextUploadIdMarker(String text) { - this.nextUploadIdMarker = text; - } + public List getObjects() { + for (ObsObject object : this.objects) { + object.setObjectKey(getDecodedString(object.getObjectKey(), needDecode)); + } + return this.objects; + } - public void endMaxUploads(String text) { - try { - this.maxUploads = Integer.parseInt(text); - }catch (Exception e) { - if(log.isErrorEnabled()) { - log.error("Response xml is not well-format", e); - } - } - } + public List getCommonPrefixes() { + for (String commonPrefix : commonPrefixes) { + finalCommonPrefixes.add(getDecodedString(commonPrefix, needDecode)); + } + return finalCommonPrefixes; + } - public void endIsTruncated(String text) { - this.isTruncated = Boolean.parseBoolean(text); - } + @Deprecated + public List getExtenedCommonPrefixes() { + return getExtendCommonPrefixes(); + } - public void endPrefix(String text) { - if (insideCommonPrefixes) { - commonPrefixes.add(text); - }else { - this.prefix = text; - } - } + public List getExtendCommonPrefixes() { + for (ObsObject object : extendCommonPrefixes) { + object.setObjectKey(getDecodedString(object.getObjectKey(), needDecode)); + } + return extendCommonPrefixes; + } - public void endCommonPrefixes() { - insideCommonPrefixes = false; - } + public String getRequestPrefix() { + return getDecodedString(requestPrefix, needDecode); + } - } + public String getRequestMarker() { + return getDecodedString(requestMarker, needDecode); + } - public static class CopyPartResultHandler extends SimpleHandler { - private Date lastModified; + public String getNextMarker() { + return getDecodedString(nextMarker, needDecode); + } - private String etag; + public int getRequestMaxKeys() { + return requestMaxKeys; + } + public String getRequestDelimiter() { + return getDecodedString(requestDelimiter, needDecode); + } - public CopyPartResultHandler(XMLReader xr) { - super(xr); - } + public String getEncodingType() { + return encodingType; + } - public CopyPartResult getCopyPartResult(int partNumber) { - CopyPartResult result = new CopyPartResult(partNumber, etag, lastModified); - return result; - } + @Override + public void startElement(String name) { + switch (name) { + case "Contents": + currentObject = new ObsObject(); + currentObject.setBucketName(bucketName); + break; + case "Owner": + currentOwner = new Owner(); + break; + case "CommonPrefixes": + insideCommonPrefixes = true; + currentExtendCommonPrefix = new ObsObject(); + break; + default: + break; + } + } - public void endLastModified(String text) { - try { - this.lastModified = ServiceUtils.parseIso8601Date(text); - } catch (ParseException e) { - } - } + @Override + public void endElement(String name, String elementText) { + if (name.equals("Name")) { + bucketName = elementText; + } else if (!insideCommonPrefixes && name.equals("Prefix")) { + requestPrefix = elementText; + } else if (name.equals("Marker")) { + requestMarker = elementText; + } else if (name.equals("NextMarker")) { + nextMarker = elementText; + } else if (name.equals("MaxKeys")) { + requestMaxKeys = Integer.parseInt(elementText); + } else if (name.equals("Delimiter")) { + requestDelimiter = elementText; + } else if (name.equals("IsTruncated")) { + listingTruncated = Boolean.parseBoolean(elementText); + } else if (name.equals("Contents")) { + objects.add(currentObject); + } else if (name.equals("DisplayName")) { + if (currentOwner != null) { + currentOwner.setDisplayName(elementText); + } + } else if (name.equals("EncodingType")) { + encodingType = elementText; + if (encodingType.equals("url")) { + needDecode = true; + } + } - public void endETag(String text) { - this.etag = text; - } + setCurrentObjectProperties(name, elementText); - } - - public static class PartResultHandler extends SimpleHandler { - private int partNumber; + if (null != currentExtendCommonPrefix) { + if (insideCommonPrefixes && name.equals("Prefix")) { + commonPrefixes.add(elementText); + currentExtendCommonPrefix.setObjectKey(elementText); + } else if (insideCommonPrefixes && name.equals("MTime")) { + currentExtendCommonPrefix.getMetadata() + .setLastModified(new Date(Long.parseLong(elementText) * 1000)); + } + } - private Date lastModified; + if (name.equals("CommonPrefixes")) { + extendCommonPrefixes.add(currentExtendCommonPrefix); + insideCommonPrefixes = false; + } + } - private String etag; + private void setCurrentObjectProperties(String name, String elementText) { + if (name.equals("Key")) { + currentObject.setObjectKey(elementText); + lastKey = elementText; + } else if (name.equals("LastModified")) { + if (!insideCommonPrefixes) { + try { + currentObject.getMetadata().setLastModified(ServiceUtils.parseIso8601Date(elementText)); + } catch (ParseException e) { + if (log.isErrorEnabled()) { + log.error("Non-ISO8601 date for LastModified in bucket's object listing output: " + + elementText, e); + } + } + } + } else if (name.equals("ETag")) { + currentObject.getMetadata().setEtag(elementText); + } else if (name.equals("Size")) { + currentObject.getMetadata().setContentLength(Long.parseLong(elementText)); + } else if (name.equals("StorageClass")) { + currentObject.getMetadata().setObjectStorageClass(StorageClassEnum.getValueFromCode(elementText)); + } else if (name.equals("ID")) { + if (currentOwner == null) { + currentOwner = new Owner(); + } + currentObject.setOwner(currentOwner); + currentOwner.setId(elementText); + } else if (name.equals("Type")) { + currentObject.getMetadata().setAppendable("Appendable".equals(elementText)); + } + } + } - private long size; + public static class ListContentSummaryHandler extends DefaultXmlHandler { + private FolderContentSummary currentFolderContentSummary; - public PartResultHandler(XMLReader xr) { - super(xr); - } + private FolderContentSummary.LayerSummary currentLayerSummary; - public Multipart getMultipartPart() { - return new Multipart(partNumber, lastModified, etag, size); - } + private final List folderContentSummaries = new ArrayList<>(); - public void endPartNumber(String text) { - this.partNumber = Integer.parseInt(text); - } + private String bucketName; - public void endLastModified(String text) { - try { - this.lastModified = ServiceUtils.parseIso8601Date(text); - } catch (ParseException e) { - } - } + private String requestPrefix; - public void endETag(String text) { - this.etag = text; - } + private String requestMarker; - public void endSize(String text) { - this.size = Long.parseLong(text); - } + private String requestDelimiter; - public void endPart(String text) { - returnControlToParentHandler(); - } - } + private int requestMaxKeys = 0; - public static class ListPartsHandler extends SimpleHandler { - private final List parts = new ArrayList(); + private boolean listingTruncated = false; - private String bucketName; + private String nextMarker; - private String objectKey; + private String lastFolder; - private String uploadId; - private Owner initiator; + public String getBucketName() { + return bucketName; + } - private Owner owner; + public boolean isListingTruncated() { + return listingTruncated; + } - private String storageClass; + public List getFolderContentSummaries() { + return folderContentSummaries; + } - private String partNumberMarker; + public String getRequestPrefix() { + return requestPrefix; + } - private String nextPartNumberMarker; + public String getRequestMarker() { + return requestMarker; + } - private int maxParts; + public String getNextMarker() { + return nextMarker; + } - private boolean isTruncated = false; + public int getRequestMaxKeys() { + return requestMaxKeys; + } - private boolean inInitiator = false; + public String getRequestDelimiter() { + return requestDelimiter; + } - public ListPartsHandler(XMLReader xr) { - super(xr); - } + public String getMarkerForNextListing() { + return listingTruncated ? nextMarker == null ? lastFolder : nextMarker : null; + } - public List getMultiPartList() { - return parts; - } + @Override + public void startElement(String name) { + if (name.equals("Contents")) { + currentFolderContentSummary = new FolderContentSummary(); + } else if (name.equals("LayerSummary")) { + currentLayerSummary = new FolderContentSummary.LayerSummary(); + } + } - public boolean isTruncated() { - return isTruncated; - } + @Override + public void endElement(String name, String elementText) { + if (name.equals("BucketName")) { + bucketName = elementText; + } else if (name.equals("Prefix")) { + requestPrefix = elementText; + } else if (name.equals("Marker")) { + requestMarker = elementText; + } else if (name.equals("NextMarker")) { + nextMarker = elementText; + } else if (name.equals("MaxKeys")) { + requestMaxKeys = Integer.parseInt(elementText); + } else if (name.equals("Delimiter")) { + requestDelimiter = elementText; + } else if (name.equals("IsTruncated")) { + listingTruncated = Boolean.parseBoolean(elementText); + } else if (name.equals("Directory")) { + currentFolderContentSummary.setDir(elementText); + lastFolder = elementText; + } else if (name.equals("DirHeight")) { + currentFolderContentSummary.setDirHeight(Long.parseLong(elementText)); + } else if (name.equals("SummaryHeight")) { + currentLayerSummary.setSummaryHeight(Long.parseLong(elementText)); + } else if (name.equals("DirCount")) { + currentLayerSummary.setDirCount(Long.parseLong(elementText)); + } else if (name.equals("FileCount")) { + currentLayerSummary.setFileCount(Long.parseLong(elementText)); + } else if (name.equals("FileSize")) { + currentLayerSummary.setFileSize(Long.parseLong(elementText)); + } else if (name.equals("LayerSummary")) { + currentFolderContentSummary.getLayerSummaries().add(currentLayerSummary); + } else if (name.equals("Contents")) { + folderContentSummaries.add(currentFolderContentSummary); + } + } + } - public String getBucketName() { - return bucketName; - } + public static class ListContentSummaryFsHandler extends DefaultXmlHandler { - public String getObjectKey() { - return objectKey; - } + private String bucketName; - public String getUploadId() { - return uploadId; - } + private DirSummary dirSummary; - public Owner getInitiator() { - return initiator; - } + private DirContentSummary dirContentSummary; - public Owner getOwner() { - return owner; - } + private ListContentSummaryFsResult.ErrorResult errorResult; - public String getStorageClass() { - return storageClass; - } + private List dirContentSummaries = new ArrayList<>(); - public String getPartNumberMarker() { - return partNumberMarker; - } + private List errorResults = new ArrayList<>(); - public String getNextPartNumberMarker() { - return nextPartNumberMarker; - } + private List subDirs; - public int getMaxParts() { - return maxParts; - } + public DirContentSummary getDirContentSummary() { + return dirContentSummary; + } - public void startPart() { - transferControlToHandler(new PartResultHandler(xr)); - } + public void setDirContentSummary(DirContentSummary dirContentSummary) { + this.dirContentSummary = dirContentSummary; + } - @Override - public void controlReturned(SimpleHandler childHandler) { - if (childHandler instanceof PartResultHandler) { - parts.add(((PartResultHandler) childHandler).getMultipartPart()); - } else { - if (inInitiator) { - initiator = ((OwnerHandler) childHandler).getOwner(); - } else { - owner = ((OwnerHandler) childHandler).getOwner(); - } - } - } + public ListContentSummaryFsResult.ErrorResult getErrorResult() { + return errorResult; + } - public void startInitiator() { - inInitiator = true; - transferControlToHandler(new OwnerHandler(xr)); - } + public void setErrorResult(ListContentSummaryFsResult.ErrorResult errorResult) { + this.errorResult = errorResult; + } - public void startOwner() { - inInitiator = false; - transferControlToHandler(new OwnerHandler(xr)); - } + public List getDirContentSummaries() { + return dirContentSummaries; + } - public void endBucket(String text) { - this.bucketName = text; - } + public void setDirContentSummaries(List dirContentSummaries) { + this.dirContentSummaries = dirContentSummaries; + } - public void endKey(String text) { - this.objectKey = text; - } + public List getErrorResults() { + return errorResults; + } - public void endStorageClass(String text) { - this.storageClass = text; - } + public void setErrorResults(List errorResults) { + this.errorResults = errorResults; + } - public void endUploadId(String text) { - this.uploadId = text; - } + public String getBucketName() { + return bucketName; + } - public void endPartNumberMarker(String text) { - this.partNumberMarker = text; - } - - public void endNextPartNumberMarker(String text) { - this.nextPartNumberMarker = text; - } - - public void endMaxParts(String text) { - this.maxParts = Integer.parseInt(text); - } - - public void endIsTruncated(String text) { - this.isTruncated = Boolean.parseBoolean(text); - } - } - - public static class CompleteMultipartUploadHandler extends SimpleHandler { - - private String location; - - private String bucketName; - - private String objectKey; - - private String etag; - - - public CompleteMultipartUploadHandler(XMLReader xr) { - super(xr); - } - - public void endLocation(String text) { - this.location = text; - } - - public void endBucket(String text) { - this.bucketName = text; - } - - public void endKey(String text) { - this.objectKey = text; - } - - public void endETag(String text) { - this.etag = text; - } - - public String getLocation() { - return location; - } - - public String getBucketName() { - return bucketName; - } - - public String getObjectKey() { - return objectKey; - } - - public String getEtag() { - return etag; - } - - - } - - public static class BucketWebsiteConfigurationHandler extends DefaultXmlHandler { - private WebsiteConfiguration config = new WebsiteConfiguration(); - - private Redirect currentRedirectRule; - - private RedirectAllRequest currentRedirectAllRule; - - private RouteRule currentRoutingRule; - - private RouteRuleCondition currentCondition; - - public WebsiteConfiguration getWebsiteConfig() { - return config; - } - - @Override - public void startElement(String name) { - if (name.equals("RedirectAllRequestsTo")) { - currentRedirectAllRule = new RedirectAllRequest(); - this.config.setRedirectAllRequestsTo(currentRedirectAllRule); - }else if (name.equals("RoutingRule")) { - currentRoutingRule = new RouteRule(); - this.config.getRouteRules().add(currentRoutingRule); - }else if (name.equals("Condition")) { - currentCondition = new RouteRuleCondition(); - currentRoutingRule.setCondition(currentCondition); - }else if (name.equals("Redirect")) { - currentRedirectRule = new Redirect(); - currentRoutingRule.setRedirect(currentRedirectRule); - } - } - - @Override - public void endElement(String name, String elementText) { - try { - if (name.equals("Suffix")) { - config.setSuffix(elementText); - }else if (name.equals("Key")) { - config.setKey(elementText); - }else if (name.equals("KeyPrefixEquals")) { - currentCondition.setKeyPrefixEquals(elementText); - } else if (name.equals("HttpErrorCodeReturnedEquals")) { - currentCondition.setHttpErrorCodeReturnedEquals(elementText); - }else if (name.equals("Protocol")) { - if (currentRedirectAllRule != null) { - currentRedirectAllRule.setRedirectProtocol(ProtocolEnum.getValueFromCode(elementText)); - } else if (currentRedirectRule != null) { - currentRedirectRule.setRedirectProtocol(ProtocolEnum.getValueFromCode(elementText)); - } - } else if (name.equals("HostName")) { - if (currentRedirectAllRule != null) { - currentRedirectAllRule.setHostName(elementText); - } else if (currentRedirectRule != null) { - currentRedirectRule.setHostName(elementText); - } - } else if (name.equals("ReplaceKeyPrefixWith")) { - currentRedirectRule.setReplaceKeyPrefixWith(elementText); - } else if (name.equals("ReplaceKeyWith")) { - currentRedirectRule.setReplaceKeyWith(elementText); - } else if (name.equals("HttpRedirectCode")) { - currentRedirectRule.setHttpRedirectCode(elementText); - } - }catch (NullPointerException e) { - if(log.isErrorEnabled()) { - log.error("Response xml is not well-formt", e); - } - } - } - } - - public static class DeleteObjectsHandler extends DefaultXmlHandler { - - private DeleteObjectsResult result; - - private List deletedObjectResults = new ArrayList(); - - private List errorResults = new ArrayList(); - - private String key; - - private String version; - - private String deleteMarkerVersion; - - private String errorCode; - - private String message; - - private boolean withDeleteMarker; - - public DeleteObjectsResult getMultipleDeleteResult() { - return result; - } - - @Override - public void startElement(String name) { - if (name.equals("DeleteResult")) { - result = new DeleteObjectsResult(); - } - } - - @Override - public void endElement(String name, String elementText) { - if ("Key".equals(name)) { - key = elementText; - } else if ("VersionId".equals(name)) { - version = elementText; - } else if ("DeleteMarker".equals(name)) { - withDeleteMarker = Boolean.parseBoolean(elementText); - } else if ("DeleteMarkerVersionId".equals(name)) { - deleteMarkerVersion = elementText; - } else if ("Code".equals(name)) { - errorCode = elementText; - } else if ("Message".equals(name)) { - message = elementText; - }else if ("Deleted".equals(name)) { - DeleteObjectsResult.DeleteObjectResult r = result.new DeleteObjectResult(key, version, withDeleteMarker, deleteMarkerVersion); - deletedObjectResults.add(r); - key = version = deleteMarkerVersion = null; - withDeleteMarker = false; - } else if ("Error".equals(name)) { - errorResults.add(result.new ErrorResult(key, version, errorCode, message)); - key = version = errorCode = message = null; - }else if (name.equals("DeleteResult")) { - result.getDeletedObjectResults().addAll(deletedObjectResults); - result.getErrorResults().addAll(errorResults); - } - } - } - - public static class BucketTagInfoHandler extends DefaultXmlHandler { - private BucketTagInfo tagInfo = new BucketTagInfo(); - - private String currentKey; - - private String currentValue; - - public BucketTagInfo getBucketTagInfo() { - return tagInfo; - } - - @Override - public void endElement(String name, String elementText) { - if ("Key".equals(name)) { - currentKey = elementText; - } else if ("Value".equals(name)) { - currentValue = elementText; - } else if ("Tag".equals(name)) { - tagInfo.getTagSet().addTag(currentKey, currentValue); - } - } - - } - - public static class BucketNotificationConfigurationHandler extends DefaultXmlHandler { - - private BucketNotificationConfiguration bucketNotificationConfiguration = new BucketNotificationConfiguration(); - - private String id; - - private String urn; - - private AbstractNotification.Filter filter; - - private List events = new ArrayList(); - - private String ruleName; - - private String ruleValue; - - public BucketNotificationConfiguration getBucketNotificationConfiguration() { - return bucketNotificationConfiguration; - } - - @Override - public void startElement(String name) { - if ("Filter".equals(name)) { - filter = new AbstractNotification.Filter(); - } - } - - @Override - public void endElement(String name, String elementText) { - try { - if ("Id".equals(name)) { - id = elementText; - } else if ("Topic".equals(name) || "FunctionGraph".equals(name)) { - urn = elementText; - } else if ("Event".equals(name)) { - events.add(EventTypeEnum.getValueFromCode(elementText)); - } else if ("Name".equals(name)) { - ruleName = elementText; - } else if ("Value".equals(name)) { - ruleValue = elementText; - } else if ("FilterRule".equals(name)) { - filter.addFilterRule(ruleName, ruleValue); - } else if ("TopicConfiguration".equals(name)) { - bucketNotificationConfiguration.addTopicConfiguration(new TopicConfiguration(id, filter, urn, events)); - events = new ArrayList(); - } else if ("FunctionGraphConfiguration".equals(name)) { - bucketNotificationConfiguration.addFunctionGraphConfiguration(new FunctionGraphConfiguration(id, filter, urn, events)); - events = new ArrayList(); - } - } catch (NullPointerException e) { - if(log.isErrorEnabled()) { - log.error("Response xml is not well-formt", e); - } - } - } - - } - - public static class BucketLifecycleConfigurationHandler extends SimpleHandler { - private LifecycleConfiguration config = new LifecycleConfiguration(); - - private LifecycleConfiguration.Rule latestRule; - - private LifecycleConfiguration.TimeEvent latestTimeEvent; - - public BucketLifecycleConfigurationHandler(XMLReader xr) { - super(xr); - } - - public LifecycleConfiguration getLifecycleConfig() { - return config; - } - - public void startExpiration() { - latestTimeEvent = config.new Expiration(); - latestRule.setExpiration(((LifecycleConfiguration.Expiration) latestTimeEvent)); - } - - public void startNoncurrentVersionExpiration() { - latestTimeEvent = config.new NoncurrentVersionExpiration(); - latestRule.setNoncurrentVersionExpiration( - ((LifecycleConfiguration.NoncurrentVersionExpiration) latestTimeEvent)); - } - - public void startTransition() { - latestTimeEvent = config.new Transition(); - latestRule.getTransitions().add(((LifecycleConfiguration.Transition) latestTimeEvent)); - } - - public void startNoncurrentVersionTransition() { - latestTimeEvent = config.new NoncurrentVersionTransition(); - latestRule.getNoncurrentVersionTransitions() - .add(((LifecycleConfiguration.NoncurrentVersionTransition) latestTimeEvent)); - } - - public void endStorageClass(String text) { - LifecycleConfiguration.setStorageClass(latestTimeEvent, StorageClassEnum.getValueFromCode(text)); - } - - public void endDate(String text) throws ParseException { - LifecycleConfiguration.setDate(latestTimeEvent, ServiceUtils.parseIso8601Date(text)); - } - - public void endNoncurrentDays(String text) { - LifecycleConfiguration.setDays(latestTimeEvent, Integer.parseInt(text)); - } - - public void endDays(String text) { - LifecycleConfiguration.setDays(latestTimeEvent, Integer.parseInt(text)); - } - - public void startRule() { - latestRule = config.new Rule(); - } - - public void endID(String text) { - latestRule.setId(text); - } - - public void endPrefix(String text) { - latestRule.setPrefix(text); - } - - public void endStatus(String text) { - latestRule.setEnabled("Enabled".equals(text)); - } - - public void endRule(String text) { - config.addRule(latestRule); - } - } - - public static class AccessControlListHandler extends DefaultXmlHandler { - protected AccessControlList accessControlList; - - protected Owner owner; - protected GranteeInterface currentGrantee; - protected Permission currentPermission; - protected boolean currentDelivered; - - protected boolean insideACL = false; - - public AccessControlList getAccessControlList() { - return accessControlList; - } - - @Override - public void startElement(String name) { - if(name.equals("AccessControlPolicy")) { - accessControlList = new AccessControlList(); - }else if (name.equals("Owner")) { - owner = new Owner(); - accessControlList.setOwner(owner); - } else if (name.equals("AccessControlList")) { - insideACL = true; - } - } - @Override - public void endElement(String name, String elementText) { - if (name.equals("ID") && !insideACL) { - owner.setId(elementText); - } else if (name.equals("DisplayName") && !insideACL) { - owner.setDisplayName(elementText); - }else if (name.equals("ID")) { - currentGrantee = new CanonicalGrantee(); - currentGrantee.setIdentifier(elementText); - } else if (name.equals("URI") || name.equals("Canned")) { - currentGrantee = new GroupGrantee(); - currentGrantee.setIdentifier(elementText); - }else if (name.equals("DisplayName")) { - if (currentGrantee instanceof CanonicalGrantee) { - ((CanonicalGrantee) currentGrantee).setDisplayName(elementText); - } - } else if (name.equals("Permission")) { - currentPermission = Permission.parsePermission(elementText); - } else if(name.equals("Delivered")) { - if(insideACL) { - currentDelivered = Boolean.parseBoolean(elementText); - }else { - accessControlList.setDelivered(Boolean.parseBoolean(elementText)); - } - } else if (name.equals("Grant")) { - GrantAndPermission obj = accessControlList.grantPermission(currentGrantee, currentPermission); - obj.setDelivered(currentDelivered); - } else if (name.equals("AccessControlList")) { - insideACL = false; - } - } - - } - - public static class BucketQuotaHandler extends DefaultXmlHandler { - protected BucketQuota quota; - - public BucketQuota getQuota() { - return quota; - } - - @Override - public void startElement(String name) { - if (name.equals("Quota")) { - quota = new BucketQuota(); - } - } - - @Override - public void endElement(String name, String elementText) { - if (name.equals("StorageQuota")) { - if(quota != null) { - quota.setBucketQuota(Long.parseLong(elementText)); - } - } - } - } - - public static class BucketEncryptionHandler extends DefaultXmlHandler { - protected BucketEncryption encryption; - - public BucketEncryption getEncryption() { - return encryption; - } + public void setBucketName(String bucketName) { + this.bucketName = bucketName; + } @Override public void startElement(String name) { - if(name.equals("ApplyServerSideEncryptionByDefault")) { - encryption = new BucketEncryption(); + if (name.equals("Contents")) { + dirContentSummary = new DirContentSummary(); + subDirs = new ArrayList<>(); + errorResult = null; + } else if (name.equals("ErrContents")) { + errorResult = new ListContentSummaryFsResult.ErrorResult(); + dirContentSummary = null; + } else if (name.equals("SubDir")) { + dirSummary = new DirSummary(); } } @Override public void endElement(String name, String elementText) { - try { - if (name.equals("SSEAlgorithm")) { - encryption.setSseAlgorithm(SSEAlgorithmEnum.getValueFromCode(elementText.replace("aws:", ""))); - } else if(name.equals("KMSMasterKeyID")) { - encryption.setKmsKeyId(elementText); + if (name.equals("BucketName")) { + bucketName = elementText; + } else if (name.equals("Key")) { + fnSetKey(elementText); + } else if (name.equals("Inode")) { + fnSetInode(elementText); + } else if (name.equals("IsTruncated")) { + dirContentSummary.setTruncated(Boolean.parseBoolean(elementText)); + } else if (name.equals("NextMarker")) { + dirContentSummary.setNextMarker(elementText); + } else if (name.equals("SubDir")) { + subDirs.add(dirSummary); + dirSummary = null; + } else if (name.equals("Name")) { + dirSummary.setName(elementText); + } else if (name.equals("DirCount")) { + dirSummary.setDirCount(Long.parseLong(elementText)); + } else if (name.equals("FileCount")) { + dirSummary.setFileCount(Long.parseLong(elementText)); + } else if (name.equals("FileSize")) { + dirSummary.setFileSize(Long.parseLong(elementText)); + } else if (name.equals("ErrContents")) { + errorResults.add(errorResult); + } else if (name.equals("ErrorCode")) { + errorResult.setErrorCode(elementText); + } else if (name.equals("Message")) { + errorResult.setMessage(elementText); + } else if (name.equals("StatusCode")) { + errorResult.setStatusCode(elementText); + } else if (name.equals("Contents")) { + dirContentSummary.setSubDir(subDirs); + dirContentSummaries.add(dirContentSummary); + } + } + + private void fnSetInode(String elementText) { + if (dirSummary != null) { + dirSummary.setInode(Long.parseLong(elementText)); + } else if (errorResult != null) { + errorResult.setInode(Long.parseLong(elementText)); + } else if (dirContentSummary != null) { + dirContentSummary.setInode(Long.parseLong(elementText)); + } + } + + private void fnSetKey(String elementText) { + if ((dirContentSummary != null)) { + dirContentSummary.setKey(elementText); + } else { + errorResult.setKey(elementText); + } + } + } + + public static class ContentSummaryFsHandler extends DefaultXmlHandler { + + private String bucketName; + + private DirSummary contentSummary; + + public String getBucketName() { + return bucketName; + } + + public void setBucketName(String bucketName) { + this.bucketName = bucketName; + } + + public DirSummary getContentSummary() { + return contentSummary; + } + + public void setContentSummary(DirSummary contentSummary) { + this.contentSummary = contentSummary; + } + + @Override + public void startElement(String name) { + if (name.equals("GetContentSummaryResult")) { + contentSummary = new DirSummary(); + } + } + + @Override + public void endElement(String name, String elementText) { + if (name.equals("BucketName")) { + bucketName = elementText; + } else if (name.equals("Name")) { + contentSummary.setName(elementText); + } else if (name.equals("DirCount")) { + contentSummary.setDirCount(Long.parseLong(elementText)); + } else if (name.equals("FileCount")) { + contentSummary.setFileCount(Long.parseLong(elementText)); + } else if (name.equals("FileSize")) { + contentSummary.setFileSize(Long.parseLong(elementText)); + } else if (name.equals("Inode")) { + contentSummary.setInode(Long.parseLong(elementText)); + } + } + } + + public static class ListBucketsHandler extends DefaultXmlHandler { + private Owner bucketsOwner; + + private ObsBucket currentBucket; + + private final List buckets = new ArrayList<>(); + + public List getBuckets() { + return this.buckets; + } + + public Owner getOwner() { + return bucketsOwner; + } + + @Override + public void startElement(String name) { + if (name.equals("Bucket")) { + currentBucket = new ObsBucket(); + } else if (name.equals("Owner")) { + bucketsOwner = new Owner(); + } + } + + @Override + public void endElement(String name, String elementText) { + if (null != bucketsOwner) { + if (name.equals("ID")) { + bucketsOwner.setId(elementText); + } else if (name.equals("DisplayName")) { + bucketsOwner.setDisplayName(elementText); + } + } + + if (null != currentBucket) { + switch (name) { + case "Bucket": + currentBucket.setOwner(bucketsOwner); + buckets.add(currentBucket); + break; + case "Name": + currentBucket.setBucketName(elementText); + break; + case "Location": + currentBucket.setLocation(elementText); + break; + case "CreationDate": + elementText += ".000Z"; + try { + currentBucket.setCreationDate(ServiceUtils.parseIso8601Date(elementText)); + } catch (ParseException e) { + if (log.isWarnEnabled()) { + log.warn("Non-ISO8601 date for CreationDate in list buckets output: " + elementText, e); + } + } + break; + case "BucketType": + if (Constants.POSIX.equals(elementText)) { + currentBucket.setBucketType(BucketTypeEnum.PFS); + } else { + currentBucket.setBucketType(BucketTypeEnum.OBJECT); + } + break; + default: + break; + } + } + } + } + + public static class BucketLoggingHandler extends DefaultXmlHandler { + + private BucketLoggingConfiguration bucketLoggingStatus = new BucketLoggingConfiguration(); + + private String targetBucket; + + private String targetPrefix; + + private GranteeInterface currentGrantee; + + private Permission currentPermission; + + private boolean currentDelivered; + + public BucketLoggingConfiguration getBucketLoggingStatus() { + return bucketLoggingStatus; + } + + @Override + public void endElement(String name, String elementText) { + switch (name) { + case "TargetBucket": + targetBucket = elementText; + break; + case "TargetPrefix": + targetPrefix = elementText; + break; + case "LoggingEnabled": + bucketLoggingStatus.setTargetBucketName(targetBucket); + bucketLoggingStatus.setLogfilePrefix(targetPrefix); + break; + case "Agency": + bucketLoggingStatus.setAgency(elementText); + break; + case "ID": + currentGrantee = new CanonicalGrantee(); + currentGrantee.setIdentifier(elementText); + break; + case "URI": + case "Canned": + currentGrantee = new GroupGrantee(); + currentGrantee.setIdentifier(elementText); + break; + case "DisplayName": + if (currentGrantee instanceof CanonicalGrantee) { + ((CanonicalGrantee) currentGrantee).setDisplayName(elementText); + } + break; + case "Delivered": + currentDelivered = Boolean.parseBoolean(elementText); + break; + case "Permission": + currentPermission = Permission.parsePermission(elementText); + break; + case "Grant": + GrantAndPermission gap = new GrantAndPermission(currentGrantee, currentPermission); + gap.setDelivered(currentDelivered); + bucketLoggingStatus.addTargetGrant(gap); + break; + default: + break; + } + } + } + + public static class BucketLocationHandler extends DefaultXmlHandler { + private String location; + + public String getLocation() { + return location; + } + + @Override + public void endElement(String name, String elementText) { + if (name.equals("LocationConstraint") || name.equals("Location")) { + location = elementText; + } + } + } + + public static class BucketCorsHandler extends DefaultXmlHandler { + + private final BucketCors configuration = new BucketCors(); + + private BucketCorsRule currentRule; + + private List allowedMethods = null; + + private List allowedOrigins = null; + + private List exposedHeaders = null; + + private List allowedHeaders = null; + + public BucketCors getConfiguration() { + return configuration; + } + + @Override + public void startElement(String name) { + + if ("CORSRule".equals(name)) { + currentRule = new BucketCorsRule(); + } + if ("AllowedOrigin".equals(name)) { + if (allowedOrigins == null) { + allowedOrigins = new ArrayList<>(); + } + } else if ("AllowedMethod".equals(name)) { + if (allowedMethods == null) { + allowedMethods = new ArrayList<>(); + } + } else if ("ExposeHeader".equals(name)) { + if (exposedHeaders == null) { + exposedHeaders = new ArrayList<>(); + } + } else if ("AllowedHeader".equals(name)) { + if (allowedHeaders == null) { + allowedHeaders = new LinkedList<>(); + } + } + } + + @Override + public void endElement(String name, String elementText) { + if (name.equals("CORSRule")) { + currentRule.setAllowedHeader(allowedHeaders); + currentRule.setAllowedMethod(allowedMethods); + currentRule.setAllowedOrigin(allowedOrigins); + currentRule.setExposeHeader(exposedHeaders); + configuration.getRules().add(currentRule); + allowedHeaders = null; + allowedMethods = null; + allowedOrigins = null; + exposedHeaders = null; + currentRule = null; + } + if (name.equals("ID") && (null != currentRule)) { + currentRule.setId(elementText); + + } else if (name.equals("AllowedOrigin") && (null != allowedOrigins)) { + allowedOrigins.add(elementText); + + } else if (name.equals("AllowedMethod") && (null != allowedMethods)) { + allowedMethods.add(elementText); + + } else if (name.equals("MaxAgeSeconds") && (null != currentRule)) { + currentRule.setMaxAgeSecond(Integer.parseInt(elementText)); + + } else if (name.equals("ExposeHeader") && (null != exposedHeaders)) { + exposedHeaders.add(elementText); + + } else if (name.equals("AllowedHeader") && (null != allowedHeaders)) { + allowedHeaders.add(elementText); + } + } + } + + public static class CopyObjectResultHandler extends DefaultXmlHandler { + private String etag; + + private Date lastModified; + + public Date getLastModified() { + return ServiceUtils.cloneDateIgnoreNull(lastModified); + } + + public String getETag() { + return etag; + } + + @Override + public void endElement(String name, String elementText) { + if (name.equals("LastModified")) { + try { + lastModified = ServiceUtils.parseIso8601Date(elementText); + } catch (ParseException e) { + if (log.isErrorEnabled()) { + log.error("Non-ISO8601 date for LastModified in copy object output: " + elementText, e); + } } - } catch (NullPointerException e) { - if(log.isWarnEnabled()) { - log.warn("Response xml is not well-formt", e); + } else if (name.equals("ETag")) { + etag = elementText; + } + } + } + + public static class RequestPaymentConfigurationHandler extends DefaultXmlHandler { + private String payer = null; + + public boolean isRequesterPays() { + return "Requester".equals(payer); + } + + @Override + public void endElement(String name, String elementText) { + if (name.equals("Payer")) { + payer = elementText; + } + } + } + + public static class BucketVersioningHandler extends DefaultXmlHandler { + + private BucketVersioningConfiguration versioningStatus; + + private String status; + + public BucketVersioningConfiguration getVersioningStatus() { + return this.versioningStatus; + } + + @Override + public void endElement(String name, String elementText) { + if (name.equals("Status")) { + this.status = elementText; + } else if (name.equals("VersioningConfiguration")) { + this.versioningStatus = new BucketVersioningConfiguration( + VersioningStatusEnum.getValueFromCode(this.status)); + } + } + } + + public static class BucketCustomDomainHandler extends DefaultXmlHandler { + private BucketCustomDomainInfo bucketCustomDomainInfo = new BucketCustomDomainInfo(); + + private String domainName; + + private Date createTime; + + public BucketCustomDomainInfo getBucketTagInfo() { + return bucketCustomDomainInfo; + } + + @Override + public void endElement(String name, String content) { + if ("DomainName".equals(name)) { + domainName = content; + } else if ("CreateTime".equals(name)) { + try { + createTime = ServiceUtils.parseIso8601Date(content); + } catch (ParseException e) { + if (log.isErrorEnabled()) { + log.error("Non-ISO8601 date for CreateTime in domain listing output: " + + content, e); + } } + } else if ("Domains".equals(name)) { + bucketCustomDomainInfo.addDomain(domainName, createTime); + } + } + + } + + public static class RequestPaymentHandler extends DefaultXmlHandler { + + private RequestPaymentConfiguration requestPaymentConfiguration; + + private String payer; + + public RequestPaymentConfiguration getRequestPaymentConfiguration() { + return this.requestPaymentConfiguration; + } + + @Override + public void endElement(String name, String elementText) { + if (name.equals("Payer")) { + this.payer = elementText; + } else if (name.equals("RequestPaymentConfiguration")) { + this.requestPaymentConfiguration = new RequestPaymentConfiguration( + RequestPaymentEnum.getValueFromCode(this.payer)); + } + } + } + + public static class ListVersionsHandler extends DefaultXmlHandler { + + private final List items = new ArrayList<>(); + + private final List commonPrefixes = new ArrayList<>(); + + private final List finalCommonPrefixes = new ArrayList<>(); + + private String key; + + private String versionId; + + private boolean isLatest = false; + + private Date lastModified; + + private Owner owner; + + private String etag; + + private long size = 0; + + private String storageClass; + + private boolean isAppendable; + + private boolean insideCommonPrefixes = false; + + // Listing properties. + private String bucketName; + + private String requestPrefix; + + private String keyMarker; + + private String versionIdMarker; + + private long requestMaxKeys = 0; + + private boolean listingTruncated = false; + + private String nextMarker; + + private String nextVersionIdMarker; + + private String delimiter; + + private String encodingType; + + private boolean needDecode; + + public String getDelimiter() { + return getDecodedString(this.delimiter, needDecode); + } + + public String getBucketName() { + return this.bucketName; + } + + public boolean isListingTruncated() { + return listingTruncated; + } + + public List getItems() { + for (VersionOrDeleteMarker marker : this.items) { + marker.setKey(getDecodedString(marker.getKey(), needDecode)); + } + return this.items; + } + + public List getCommonPrefixes() { + for (String commonPrefix : commonPrefixes) { + finalCommonPrefixes.add(getDecodedString(commonPrefix, needDecode)); + } + return finalCommonPrefixes; + } + + public String getRequestPrefix() { + return getDecodedString(requestPrefix, needDecode); + } + + public String getKeyMarker() { + return getDecodedString(keyMarker, needDecode); + } + + public String getVersionIdMarker() { + return versionIdMarker; + } + + public String getNextKeyMarker() { + return getDecodedString(nextMarker, needDecode); + } + + public String getNextVersionIdMarker() { + return nextVersionIdMarker; + } + + public String getEncodingType() { + return encodingType; + } + + public long getRequestMaxKeys() { + return requestMaxKeys; + } + + private void reset() { + this.key = null; + this.versionId = null; + this.isLatest = false; + this.lastModified = null; + this.etag = null; + this.isAppendable = false; + this.size = 0; + this.storageClass = null; + this.owner = null; + } + + @Override + public void startElement(String name) { + if (name.equals("Owner")) { + owner = new Owner(); + } else if (name.equals("CommonPrefixes")) { + insideCommonPrefixes = true; + } + } + + @Override + public void endElement(String name, String content) { + setBaseInfo(name, content); + + if (name.equals("Key")) { + key = content; + } else if (name.equals("VersionId")) { + versionId = content; + } else if (name.equals("IsLatest")) { + isLatest = Boolean.parseBoolean(content); + } else if (name.equals("LastModified")) { + try { + lastModified = ServiceUtils.parseIso8601Date(content); + } catch (ParseException e) { + if (log.isWarnEnabled()) { + log.warn( + "Non-ISO8601 date for LastModified in bucket's versions listing output: " + content, + e); + } + } + } else if (name.equals("ETag")) { + etag = content; + } else if (name.equals("Size")) { + size = Long.parseLong(content); + } else if (name.equals("StorageClass")) { + storageClass = content; + } else if (name.equals("Type")) { + isAppendable = "Appendable".equals(content); + } else if (name.equals("ID")) { + if (owner == null) { + owner = new Owner(); + } + owner.setId(content); + } else if (name.equals("DisplayName")) { + if (owner != null) { + owner.setDisplayName(content); + } + } else if (insideCommonPrefixes && name.equals("Prefix")) { + commonPrefixes.add(content); + } else if (name.equals("CommonPrefixes")) { + insideCommonPrefixes = false; + } else if (name.equals("EncodingType")) { + encodingType = content; + if (encodingType.equals("url")) { + needDecode = true; + } + } + + addVersionOrDeleteMarker(name); + } + + private void setBaseInfo(String name, String content) { + if (name.equals("Name")) { + bucketName = content; + } else if (!insideCommonPrefixes && name.equals("Prefix")) { + requestPrefix = content; + } else if (name.equals("KeyMarker")) { + keyMarker = content; + } else if (name.equals("NextKeyMarker")) { + nextMarker = content; + } else if (name.equals("VersionIdMarker")) { + versionIdMarker = content; + } else if (name.equals("NextVersionIdMarker")) { + nextVersionIdMarker = content; + } else if (name.equals("MaxKeys")) { + requestMaxKeys = Long.parseLong(content); + } else if (name.equals("IsTruncated")) { + listingTruncated = Boolean.parseBoolean(content); + } else if (name.equals("Delimiter")) { + delimiter = content; + } + } + + private void addVersionOrDeleteMarker(String name) { + VersionOrDeleteMarker.Builder builder = new VersionOrDeleteMarker.Builder() + .bucketName(bucketName) + .key(key) + .versionId(versionId) + .isLatest(isLatest) + .lastModified(lastModified) + .owner(owner); + + if (name.equals("Version")) { + VersionOrDeleteMarker item = builder.etag(etag) + .size(size) + .storageClass(StorageClassEnum.getValueFromCode(storageClass)) + .isDeleteMarker(false) + .appendable(isAppendable) + .builder(); + + items.add(item); + this.reset(); + } else if (name.equals("DeleteMarker")) { + VersionOrDeleteMarker item = builder.etag(null) + .size(0) + .storageClass(null) + .isDeleteMarker(true) + .appendable(false) + .builder(); + + items.add(item); + this.reset(); + } + } + + } + + public static class OwnerHandler extends SimpleHandler { + private String id; + + private String displayName; + + public OwnerHandler(XMLReader xr) { + super(xr); + } + + public Owner getOwner() { + Owner owner = new Owner(); + owner.setId(id); + owner.setDisplayName(displayName); + return owner; + } + + public void endID(String content) { + this.id = content; + } + + public void endDisplayName(String content) { + this.displayName = content; + } + + public void endOwner(String content) { + returnControlToParentHandler(); + } + + public void endInitiator(String content) { + returnControlToParentHandler(); + } + } + + public static class InitiateMultipartUploadHandler extends SimpleHandler { + private String uploadId; + + private String bucketName; + + private String objectKey; + + private String encodingType; + + private boolean needDecode; + + public InitiateMultipartUploadHandler(XMLReader xr) { + super(xr); + } + + public InitiateMultipartUploadResult getInitiateMultipartUploadResult() { + objectKey = getDecodedString(objectKey, needDecode); + return new InitiateMultipartUploadResult(bucketName, objectKey, uploadId); + } + + public void endUploadId(String content) { + this.uploadId = content; + } + + public void endBucket(String content) { + this.bucketName = content; + } + + public void endKey(String content) { + this.objectKey = content; + } + + public void endEncodingType(String content) { + this.encodingType = content; + if (encodingType.equals("url")) { + needDecode = true; + } + } + + public String getEncodingType() { + return encodingType; + } + + } + + public static class MultipartUploadHandler extends SimpleHandler { + private String uploadId; + + private String objectKey; + + private String storageClass; + + private Owner owner; + + private Owner initiator; + + private Date initiatedDate; + + private boolean isInInitiator = false; + + public MultipartUploadHandler(XMLReader xr) { + super(xr); + } + + public MultipartUpload getMultipartUpload() { + return new MultipartUpload(uploadId, objectKey, initiatedDate, + StorageClassEnum.getValueFromCode(storageClass), owner, initiator); + } + + public void endUploadId(String content) { + this.uploadId = content; + } + + public void endKey(String content) { + this.objectKey = content; + } + + public void endStorageClass(String content) { + this.storageClass = content; + } + + public void endInitiated(String content) { + try { + this.initiatedDate = ServiceUtils.parseIso8601Date(content); + } catch (ParseException e) { + log.warn("date parse failed.", e); + } + } + + public void startOwner() { + isInInitiator = false; + transferControl(new OwnerHandler(xr)); + } + + public void startInitiator() { + isInInitiator = true; + transferControl(new OwnerHandler(xr)); + } + + @Override + public void controlReturned(SimpleHandler childHandler) { + if (childHandler instanceof OwnerHandler) { + if (isInInitiator) { + this.initiator = ((OwnerHandler) childHandler).getOwner(); + } else { + this.owner = ((OwnerHandler) childHandler).getOwner(); + } + } + } + + public void endUpload(String content) { + returnControlToParentHandler(); + } + } + + public static class ListMultipartUploadsHandler extends SimpleHandler { + + private final List uploads = new ArrayList<>(); + + private final List commonPrefixes = new ArrayList<>(); + + private final List finalCommonPrefixes = new ArrayList<>(); + + private boolean insideCommonPrefixes; + + private String bucketName; + + private String keyMarker; + + private String uploadIdMarker; + + private String nextKeyMarker; + + private String nextUploadIdMarker; + + private String delimiter; + + private int maxUploads; + + private String prefix; + + private boolean isTruncated = false; + + private String encodingType; + + private boolean needDecode; + + public ListMultipartUploadsHandler(XMLReader xr) { + super(xr); + } + + public List getMultipartUploadList() { + for (MultipartUpload upload : uploads) { + upload.setBucketName(bucketName); + upload.setObjectKey(getDecodedString(upload.getObjectKey(), needDecode)); + } + return uploads; + } + + public String getBucketName() { + return this.bucketName; + } + + public boolean isTruncated() { + return isTruncated; + } + + public String getKeyMarker() { + return getDecodedString(keyMarker, needDecode); + } + + public String getUploadIdMarker() { + return uploadIdMarker; + } + + public String getNextKeyMarker() { + return getDecodedString(nextKeyMarker, needDecode); + } + + public String getNextUploadIdMarker() { + return nextUploadIdMarker; + } + + public int getMaxUploads() { + return maxUploads; + } + + public List getCommonPrefixes() { + for (String commonPrefix : commonPrefixes) { + finalCommonPrefixes.add(getDecodedString(commonPrefix, needDecode)); + } + return finalCommonPrefixes; + } + + public String getDelimiter() { + return getDecodedString(this.delimiter, needDecode); + } + + public String getPrefix() { + return getDecodedString(this.prefix, needDecode); + } + + public String getEncodingType() { + return encodingType; + } + + public void startUpload() { + transferControl(new MultipartUploadHandler(xr)); + } + + public void startCommonPrefixes() { + insideCommonPrefixes = true; + } + + @Override + public void controlReturned(SimpleHandler childHandler) { + if (childHandler instanceof MultipartUploadHandler) { + uploads.add(((MultipartUploadHandler) childHandler).getMultipartUpload()); + } + } + + public void endDelimiter(String content) { + this.delimiter = content; + } + + public void endBucket(String content) { + this.bucketName = content; + } + + public void endKeyMarker(String content) { + this.keyMarker = content; + } + + public void endUploadIdMarker(String content) { + this.uploadIdMarker = content; + } + + public void endNextKeyMarker(String content) { + this.nextKeyMarker = content; + } + + public void endNextUploadIdMarker(String content) { + this.nextUploadIdMarker = content; + } + + public void endMaxUploads(String content) { + try { + this.maxUploads = Integer.parseInt(content); + } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error("Response xml is not well-format", e); + } + } + } + + public void endIsTruncated(String content) { + this.isTruncated = Boolean.parseBoolean(content); + } + + public void endPrefix(String content) { + if (insideCommonPrefixes) { + commonPrefixes.add(content); + } else { + this.prefix = content; + } + } + + public void endCommonPrefixes() { + insideCommonPrefixes = false; + } + + public void endEncodingType(String content) { + encodingType = content; + if (encodingType.equals("url")) { + needDecode = true; + } + } + } + + public static class CopyPartResultHandler extends SimpleHandler { + private Date lastModified; + + private String etag; + + public CopyPartResultHandler(XMLReader xr) { + super(xr); + } + + public CopyPartResult getCopyPartResult(int partNumber) { + CopyPartResult result = new CopyPartResult(partNumber, etag, lastModified); + return result; + } + + public void endLastModified(String content) { + try { + this.lastModified = ServiceUtils.parseIso8601Date(content); + } catch (ParseException e) { + log.warn("date parse failed.", e); + } + } + + public void endETag(String content) { + this.etag = content; + } + + } + + public static class PartResultHandler extends SimpleHandler { + private int partNumber; + + private Date lastModified; + + private String etag; + + private long size; + + public PartResultHandler(XMLReader xr) { + super(xr); + } + + public Multipart getMultipartPart() { + return new Multipart(partNumber, lastModified, etag, size); + } + + public void endPartNumber(String content) { + this.partNumber = Integer.parseInt(content); + } + + public void endLastModified(String content) { + try { + this.lastModified = ServiceUtils.parseIso8601Date(content); + } catch (ParseException e) { + log.warn("date parse failed.", e); + } + } + + public void endETag(String content) { + this.etag = content; + } + + public void endSize(String content) { + this.size = Long.parseLong(content); + } + + public void endPart(String content) { + returnControlToParentHandler(); + } + } + + public static class ListPartsHandler extends SimpleHandler { + private final List parts = new ArrayList<>(); + + private String bucketName; + + private String objectKey; + + private String uploadId; + + private Owner initiator; + + private Owner owner; + + private String storageClass; + + private String partNumberMarker; + + private String nextPartNumberMarker; + + private String encodingType; + + private boolean needDecode; + + private int maxParts; + + private boolean isTruncated = false; + + private boolean isInInitiator = false; + + public ListPartsHandler(XMLReader xr) { + super(xr); + } + + public List getMultiPartList() { + return parts; + } + + public boolean isTruncated() { + return isTruncated; + } + + public String getBucketName() { + return bucketName; + } + + public String getObjectKey() { + return getDecodedString(objectKey, needDecode); + } + + public String getUploadId() { + return uploadId; + } + + public Owner getInitiator() { + return initiator; + } + + public Owner getOwner() { + return owner; + } + + public String getStorageClass() { + return storageClass; + } + + public String getPartNumberMarker() { + return partNumberMarker; + } + + public String getNextPartNumberMarker() { + return nextPartNumberMarker; + } + + public String getEncodingType() { + return encodingType; + } + + public int getMaxParts() { + return maxParts; + } + + public void startPart() { + transferControl(new PartResultHandler(xr)); + } + + @Override + public void controlReturned(SimpleHandler childHandler) { + if (childHandler instanceof PartResultHandler) { + parts.add(((PartResultHandler) childHandler).getMultipartPart()); + } else if (childHandler instanceof OwnerHandler) { + if (isInInitiator) { + initiator = ((OwnerHandler) childHandler).getOwner(); + } else { + owner = ((OwnerHandler) childHandler).getOwner(); + } + } + } + + public void startInitiator() { + isInInitiator = true; + transferControl(new OwnerHandler(xr)); + } + + public void startOwner() { + isInInitiator = false; + transferControl(new OwnerHandler(xr)); + } + + public void endBucket(String content) { + this.bucketName = content; + } + + public void endKey(String content) { + this.objectKey = content; + } + + public void endStorageClass(String content) { + this.storageClass = content; + } + + public void endUploadId(String content) { + this.uploadId = content; + } + + public void endPartNumberMarker(String content) { + this.partNumberMarker = content; + } + + public void endNextPartNumberMarker(String content) { + this.nextPartNumberMarker = content; + } + + public void endMaxParts(String content) { + this.maxParts = Integer.parseInt(content); + } + + public void endIsTruncated(String content) { + this.isTruncated = Boolean.parseBoolean(content); + } + + public void endEncodingType(String content) { + encodingType = content; + if (encodingType.equals("url")) { + needDecode = true; + } + } + } + + public static class CompleteMultipartUploadHandler extends SimpleHandler { + + private String location; + + private String bucketName; + + private String objectKey; + + private String etag; + + private String encodingType; + + private boolean needDecode; + + public CompleteMultipartUploadHandler(XMLReader xr) { + super(xr); + } + + public void endLocation(String content) { + this.location = content; + } + + public void endBucket(String content) { + this.bucketName = content; + } + + public void endKey(String content) { + this.objectKey = content; + } + + public void endETag(String content) { + this.etag = content; + } + + public String getLocation() { + return location; + } + + public String getBucketName() { + return bucketName; + } + + public String getObjectKey() { + return getDecodedString(objectKey, needDecode); + } + + public String getEtag() { + return etag; + } + + public void endEncodingType(String content) { + this.encodingType = content; + if (encodingType.equals("url")) { + needDecode = true; + } + } + + public String getEncodingType() { + return encodingType; + } + + } + + public static class BucketWebsiteConfigurationHandler extends DefaultXmlHandler { + private WebsiteConfiguration config = new WebsiteConfiguration(); + + private Redirect currentRedirectRule; + + private RedirectAllRequest currentRedirectAllRule; + + private RouteRule currentRoutingRule; + + private RouteRuleCondition currentCondition; + + public WebsiteConfiguration getWebsiteConfig() { + return config; + } + + @Override + public void startElement(String name) { + switch (name) { + case "RedirectAllRequestsTo": + currentRedirectAllRule = new RedirectAllRequest(); + this.config.setRedirectAllRequestsTo(currentRedirectAllRule); + break; + case "RoutingRule": + currentRoutingRule = new RouteRule(); + this.config.getRouteRules().add(currentRoutingRule); + break; + case "Condition": + currentCondition = new RouteRuleCondition(); + currentRoutingRule.setCondition(currentCondition); + break; + case "Redirect": + currentRedirectRule = new Redirect(); + currentRoutingRule.setRedirect(currentRedirectRule); + break; + default: + break; + } + } + + @Override + public void endElement(String name, String content) { + if (null != config) { + if (name.equals("Suffix")) { + config.setSuffix(content); + } else if (name.equals("Key")) { + config.setKey(content); + } + } + + if (null != currentCondition) { + if (name.equals("KeyPrefixEquals")) { + currentCondition.setKeyPrefixEquals(content); + } else if (name.equals("HttpErrorCodeReturnedEquals")) { + currentCondition.setHttpErrorCodeReturnedEquals(content); + } + } + + if (name.equals("Protocol")) { + if (currentRedirectAllRule != null) { + currentRedirectAllRule.setRedirectProtocol(ProtocolEnum.getValueFromCode(content)); + } else if (currentRedirectRule != null) { + currentRedirectRule.setRedirectProtocol(ProtocolEnum.getValueFromCode(content)); + } + } else if (name.equals("HostName")) { + if (currentRedirectAllRule != null) { + currentRedirectAllRule.setHostName(content); + } else if (currentRedirectRule != null) { + currentRedirectRule.setHostName(content); + } + } + + if (null != currentRedirectRule) { + switch (name) { + case "ReplaceKeyPrefixWith": + currentRedirectRule.setReplaceKeyPrefixWith(content); + break; + case "ReplaceKeyWith": + currentRedirectRule.setReplaceKeyWith(content); + break; + case "HttpRedirectCode": + currentRedirectRule.setHttpRedirectCode(content); + break; + default: + break; + } + } + } + } + + public static class DeleteObjectsHandler extends DefaultXmlHandler { + + private DeleteObjectsResult result; + + private final List deletedObjectResults = + new ArrayList<>(); + + private final List errorResults = new ArrayList<>(); + + private String key; + + private String version; + + private String deleteMarkerVersion; + + private String errorCode; + + private String message; + + private boolean withDeleteMarker; + + private String encodingType; + + private boolean needDecode; + + public DeleteObjectsResult getMultipleDeleteResult() { + for (DeleteObjectsResult.DeleteObjectResult deleteObjectResult : result.getDeletedObjectResults()) { + deleteObjectResult.setObjectKey(getDecodedString(deleteObjectResult.getObjectKey(), needDecode)); + } + for (DeleteObjectsResult.ErrorResult errorResult : result.getErrorResults()) { + errorResult.setObjectKey(getDecodedString(errorResult.getObjectKey(), needDecode)); + } + return result; + } + + @Override + public void startElement(String name) { + if (name.equals("DeleteResult")) { + result = new DeleteObjectsResult(); + } + } + + @Override + public void endElement(String name, String content) { + if ("Key".equals(name)) { + key = content; + } else if ("VersionId".equals(name)) { + version = content; + } else if ("DeleteMarker".equals(name)) { + withDeleteMarker = Boolean.parseBoolean(content); + } else if ("DeleteMarkerVersionId".equals(name)) { + deleteMarkerVersion = content; + } else if ("Code".equals(name)) { + errorCode = content; + } else if ("Message".equals(name)) { + message = content; + } else if ("Deleted".equals(name)) { + DeleteObjectsResult.DeleteObjectResult r = + new DeleteObjectsResult.DeleteObjectResult(key, version, withDeleteMarker, + deleteMarkerVersion); + deletedObjectResults.add(r); + key = version = deleteMarkerVersion = null; + withDeleteMarker = false; + } else if ("Error".equals(name)) { + errorResults.add(new DeleteObjectsResult.ErrorResult(key, version, errorCode, message)); + key = version = errorCode = message = null; + } else if (name.equals("DeleteResult")) { + result.getDeletedObjectResults().addAll(deletedObjectResults); + result.getErrorResults().addAll(errorResults); + } else if (name.equals("EncodingType")) { + encodingType = content; + if (encodingType.equals("url")) { + needDecode = true; + } + } + } + + public String getEncodingType() { + return encodingType; + } + } + + public static class BucketTagInfoHandler extends DefaultXmlHandler { + private BucketTagInfo tagInfo = new BucketTagInfo(); + + private String currentKey; + + private String currentValue; + + public BucketTagInfo getBucketTagInfo() { + return tagInfo; + } + + @Override + public void endElement(String name, String content) { + if ("Key".equals(name)) { + currentKey = content; + } else if ("Value".equals(name)) { + currentValue = content; + } else if ("Tag".equals(name)) { + tagInfo.getTagSet().addTag(currentKey, currentValue); + } + } + + } + + public static class BucketNotificationConfigurationHandler extends DefaultXmlHandler { + + private BucketNotificationConfiguration bucketNotificationConfiguration = new BucketNotificationConfiguration(); + + private String id; + + private String urn; + + private AbstractNotification.Filter filter; + + private List events = new ArrayList<>(); + + private String ruleName; + + private String ruleValue; + + public BucketNotificationConfiguration getBucketNotificationConfiguration() { + return bucketNotificationConfiguration; + } + + @Override + public void startElement(String name) { + if ("Filter".equals(name)) { + filter = new AbstractNotification.Filter(); + } + } + + @Override + public void endElement(String name, String content) { + if ("Id".equals(name)) { + id = content; + } else if ("Topic".equals(name) || "FunctionGraph".equals(name)) { + urn = content; + } else if ("Event".equals(name)) { + events.add(EventTypeEnum.getValueFromCode(content)); + } else if ("Name".equals(name)) { + ruleName = content; + } else if ("Value".equals(name)) { + ruleValue = content; + } else if ("FilterRule".equals(name)) { + if (null == filter) { + if (log.isErrorEnabled()) { + log.error("Response xml is not well-formt"); + } + return; + } + filter.addFilterRule(ruleName, ruleValue); + } else if ("TopicConfiguration".equals(name)) { + if (null == bucketNotificationConfiguration) { + if (log.isErrorEnabled()) { + log.error("Response xml is not well-formt"); + } + return; + } + bucketNotificationConfiguration.addTopicConfiguration(new TopicConfiguration(id, filter, urn, events)); + events = new ArrayList<>(); + } else if ("FunctionGraphConfiguration".equals(name)) { + if (null == bucketNotificationConfiguration) { + if (log.isErrorEnabled()) { + log.error("Response xml is not well-formt"); + } + return; + } + bucketNotificationConfiguration + .addFunctionGraphConfiguration(new FunctionGraphConfiguration(id, filter, urn, events)); + events = new ArrayList<>(); + } + } + + } + + public static class BucketLifecycleConfigurationHandler extends SimpleHandler { + private LifecycleConfiguration config = new LifecycleConfiguration(); + + private LifecycleConfiguration.Rule latestRule; + + private LifecycleConfiguration.TimeEvent latestTimeEvent; + + public BucketLifecycleConfigurationHandler(XMLReader xr) { + super(xr); + } + + public LifecycleConfiguration getLifecycleConfig() { + return config; + } + + public void startExpiration() { + latestTimeEvent = config.new Expiration(); + latestRule.setExpiration(((LifecycleConfiguration.Expiration) latestTimeEvent)); + } + + public void startNoncurrentVersionExpiration() { + latestTimeEvent = config.new NoncurrentVersionExpiration(); + latestRule.setNoncurrentVersionExpiration( + ((LifecycleConfiguration.NoncurrentVersionExpiration) latestTimeEvent)); + } + + public void startTransition() { + latestTimeEvent = config.new Transition(); + latestRule.getTransitions().add(((LifecycleConfiguration.Transition) latestTimeEvent)); + } + + public void startNoncurrentVersionTransition() { + latestTimeEvent = config.new NoncurrentVersionTransition(); + latestRule.getNoncurrentVersionTransitions() + .add(((LifecycleConfiguration.NoncurrentVersionTransition) latestTimeEvent)); + } + + public void endStorageClass(String content) { + LifecycleConfiguration.setStorageClass(latestTimeEvent, StorageClassEnum.getValueFromCode(content)); + } + + public void endDate(String content) throws ParseException { + LifecycleConfiguration.setDate(latestTimeEvent, ServiceUtils.parseIso8601Date(content)); + } + + public void endNoncurrentDays(String content) { + LifecycleConfiguration.setDays(latestTimeEvent, Integer.parseInt(content)); + } + + public void endDays(String content) { + LifecycleConfiguration.setDays(latestTimeEvent, Integer.parseInt(content)); + } + + public void startRule() { + latestRule = config.new Rule(); + } + + public void endID(String content) { + latestRule.setId(content); + } + + public void endPrefix(String content) { + latestRule.setPrefix(content); + } + + public void endStatus(String content) { + latestRule.setEnabled("Enabled".equals(content)); + } + + public void endRule(String content) { + config.addRule(latestRule); + } + } + + public static class AccessControlListHandler extends DefaultXmlHandler { + protected AccessControlList accessControlList; + + protected Owner owner; + protected GranteeInterface currentGrantee; + protected Permission currentPermission; + protected boolean currentDelivered; + + protected boolean insideACL = false; + + public AccessControlList getAccessControlList() { + return accessControlList; + } + + @Override + public void startElement(String name) { + switch (name) { + case "AccessControlPolicy": + accessControlList = new AccessControlList(); + break; + case "Owner": + owner = new Owner(); + accessControlList.setOwner(owner); + break; + case "AccessControlList": + insideACL = true; + break; + default: + break; + } + } + + @Override + public void endElement(String name, String content) { + if (name.equals("ID") && !insideACL) { + owner.setId(content); + } else if (name.equals("DisplayName") && !insideACL) { + owner.setDisplayName(content); + } else if (name.equals("ID")) { + currentGrantee = new CanonicalGrantee(); + currentGrantee.setIdentifier(content); + } else if (name.equals("URI") || name.equals("Canned")) { + currentGrantee = new GroupGrantee(); + currentGrantee.setIdentifier(content); + } else if (name.equals("DisplayName")) { + if (currentGrantee instanceof CanonicalGrantee) { + ((CanonicalGrantee) currentGrantee).setDisplayName(content); + } + } else if (name.equals("Permission")) { + currentPermission = Permission.parsePermission(content); + } else if (name.equals("Delivered")) { + if (insideACL) { + currentDelivered = Boolean.parseBoolean(content); + } else { + accessControlList.setDelivered(Boolean.parseBoolean(content)); + } + } else if (name.equals("Grant")) { + GrantAndPermission obj = accessControlList.grantPermission(currentGrantee, currentPermission); + obj.setDelivered(currentDelivered); + } else if (name.equals("AccessControlList")) { + insideACL = false; + } + } + + } + + public static class BucketQuotaHandler extends DefaultXmlHandler { + protected BucketQuota quota; + + public BucketQuota getQuota() { + return quota; + } + + @Override + public void startElement(String name) { + if (name.equals("Quota")) { + quota = new BucketQuota(); + } + } + + @Override + public void endElement(String name, String content) { + if (name.equals("StorageQuota")) { + if (quota != null) { + quota.setBucketQuota(Long.parseLong(content)); + } + } + } + } + + public static class BucketEncryptionHandler extends DefaultXmlHandler { + protected BucketEncryption encryption; + + public BucketEncryption getEncryption() { + return encryption; + } + + @Override + public void startElement(String name) { + if (name.equals("ApplyServerSideEncryptionByDefault")) { + encryption = new BucketEncryption(); + } + } + + @Override + public void endElement(String name, String content) { + if (null == encryption) { + if (log.isWarnEnabled()) { + log.warn("Response xml is not well-formt"); + } + return; + } + if (name.equals("SSEAlgorithm")) { + encryption.setSseAlgorithm(SSEAlgorithmEnum.getValueFromCode(content.replace("aws:", ""))); + } else if (name.equals("KMSMasterKeyID")) { + encryption.setKmsKeyId(content); + } + } + } + + public static class BucketStoragePolicyHandler extends DefaultXmlHandler { + protected BucketStoragePolicyConfiguration storagePolicyConfiguration; + + public BucketStoragePolicyConfiguration getStoragePolicy() { + return storagePolicyConfiguration; + } + + @Override + public void startElement(String name) { + if (name.equals("StoragePolicy") || name.equals("StorageClass")) { + storagePolicyConfiguration = new BucketStoragePolicyConfiguration(); + } + } + + @Override + public void endElement(String name, String content) { + if (name.equals("DefaultStorageClass") || name.equals("StorageClass")) { + if (storagePolicyConfiguration != null) { + storagePolicyConfiguration.setBucketStorageClass(StorageClassEnum.getValueFromCode(content)); + } + } + } + } + + public static class BucketStorageInfoHandler extends DefaultXmlHandler { + private BucketStorageInfo storageInfo; + + public BucketStorageInfo getStorageInfo() { + return storageInfo; + } + + @Override + public void startElement(String name) { + if (name.equals("GetBucketStorageInfoResult")) { + storageInfo = new BucketStorageInfo(); + } + } + + @Override + public void endElement(String name, String content) { + if (null == storageInfo) { + if (log.isWarnEnabled()) { + log.warn("Response xml is not well-formt"); + } + return; + } + + if (name.equals("Size")) { + storageInfo.setSize(Long.parseLong(content)); + } else if (name.equals("ObjectNumber")) { + storageInfo.setObjectNumber(Long.parseLong(content)); + } + } + } + + public static class BucketReplicationConfigurationHandler extends DefaultXmlHandler { + private ReplicationConfiguration replicationConfiguration = new ReplicationConfiguration(); + + private ReplicationConfiguration.Rule currentRule; + + public ReplicationConfiguration getReplicationConfiguration() { + return replicationConfiguration; + } + + @Override + public void startElement(String name) { + if ("Rule".equals(name)) { + currentRule = new ReplicationConfiguration.Rule(); + } else if ("Destination".equals(name)) { + currentRule.setDestination(new ReplicationConfiguration.Destination()); + } + } + + @Override + public void endElement(String name, String content) { + if (null != replicationConfiguration) { + if ("Agency".equals(name)) { + replicationConfiguration.setAgency(content); + } else if ("Rule".equals(name)) { + replicationConfiguration.getRules().add(currentRule); + } + } + + if (null == currentRule) { + if (log.isErrorEnabled()) { + log.error("Response xml is not well-formt"); + } + return; + } + + if ("ID".equals(name)) { + currentRule.setId(content); + } else if ("Status".equals(name)) { + currentRule.setStatus(RuleStatusEnum.getValueFromCode(content)); + } else if ("Prefix".equals(name)) { + currentRule.setPrefix(content); + } else if ("Bucket".equals(name)) { + currentRule.getDestination().setBucket(content); + } else if ("StorageClass".equals(name)) { + currentRule.getDestination().setObjectStorageClass(StorageClassEnum.getValueFromCode(content)); + } else if ("HistoricalObjectReplication".equals(name)) { + currentRule + .setHistoricalObjectReplication(HistoricalObjectReplicationEnum.getValueFromCode(content)); + } + } + } + + public static class BucketDirectColdAccessHandler extends DefaultXmlHandler { + private BucketDirectColdAccess access = new BucketDirectColdAccess(); + + public BucketDirectColdAccess getBucketDirectColdAccess() { + return access; + } + + @Override + public void endElement(String name, String elementText) { + if ("Status".equals(name)) { + access.setStatus(RuleStatusEnum.getValueFromCode(elementText)); } } - - } - - public static class BucketStoragePolicyHandler extends DefaultXmlHandler{ - protected BucketStoragePolicyConfiguration storagePolicyConfiguration; - - public BucketStoragePolicyConfiguration getStoragePolicy() { - return storagePolicyConfiguration; - } - - @Override - public void startElement(String name) { - if (name.equals("StoragePolicy") || name.equals("StorageClass")) { - storagePolicyConfiguration = new BucketStoragePolicyConfiguration(); - } - } - - @Override - public void endElement(String name, String elementText) { - if (name.equals("DefaultStorageClass") || name.equals("StorageClass")) { - if(storagePolicyConfiguration != null) { - storagePolicyConfiguration.setBucketStorageClass(StorageClassEnum.getValueFromCode(elementText)); - } - } - } - } - - public static class BucketStorageInfoHandler extends DefaultXmlHandler { - private BucketStorageInfo storageInfo; - - public BucketStorageInfo getStorageInfo() { - return storageInfo; - } - - @Override - public void startElement(String name) { - if (name.equals("GetBucketStorageInfoResult")) { - storageInfo = new BucketStorageInfo(); - } - } - - @Override - public void endElement(String name, String elementText) { - try { - if (name.equals("Size")) { - storageInfo.setSize(Long.parseLong(elementText)); - } else if (name.equals("ObjectNumber")) { - storageInfo.setObjectNumber(Long.parseLong(elementText)); - } - }catch (NullPointerException e) { - if(log.isWarnEnabled()) { - log.warn("Response xml is not well-formt", e); - } - } - } - } - - public static class BucketReplicationConfigurationHandler extends DefaultXmlHandler { - private ReplicationConfiguration replicationConfiguration = new ReplicationConfiguration(); - - private ReplicationConfiguration.Rule currentRule; - - public ReplicationConfiguration getReplicationConfiguration() { - return replicationConfiguration; - } - - @Override - public void startElement(String name) { - if ("Rule".equals(name)) { - currentRule = new ReplicationConfiguration.Rule(); - } else if ("Destination".equals(name)) { - currentRule.setDestination(new ReplicationConfiguration.Destination()); - } - } - - @Override - public void endElement(String name, String elementText) { - try { - if ("Agency".equals(name)) { - replicationConfiguration.setAgency(elementText); - } else if ("Rule".equals(name)) { - replicationConfiguration.getRules().add(currentRule); - } else if ("ID".equals(name)) { - currentRule.setId(elementText); - } else if ("Status".equals(name)) { - currentRule.setStatus(RuleStatusEnum.getValueFromCode(elementText)); - } else if ("Prefix".equals(name)) { - currentRule.setPrefix(elementText); - } else if ("Bucket".equals(name)) { - currentRule.getDestination().setBucket(elementText); - } else if ("StorageClass".equals(name)) { - currentRule.getDestination().setObjectStorageClass(StorageClassEnum.getValueFromCode(elementText)); - } - }catch (NullPointerException e) { - if(log.isErrorEnabled()) { - log.error("Response xml is not well-formt", e); - } - } - } - } - - public static class BucketDirectColdAccessHandler extends DefaultXmlHandler { - private BucketDirectColdAccess access = new BucketDirectColdAccess(); - - public BucketDirectColdAccess getBucketDirectColdAccess() { - return access; - } - - @Override - public void endElement(String name, String elementText) { - if ("Status".equals(name)) { - access.setStatus(RuleStatusEnum.getValueFromCode(elementText)); - } - } - - } + } } diff --git a/app/src/main/java/com/obs/services/internal/io/HttpMethodReleaseInputStream.java b/app/src/main/java/com/obs/services/internal/io/HttpMethodReleaseInputStream.java index d77e8e5..1791d4d 100644 --- a/app/src/main/java/com/obs/services/internal/io/HttpMethodReleaseInputStream.java +++ b/app/src/main/java/com/obs/services/internal/io/HttpMethodReleaseInputStream.java @@ -1,10 +1,4 @@ /** - * - * JetS3t : Java S3 Toolkit - * Project hosted at http://bitbucket.org/jmurty/jets3t/ - * - * Copyright 2006-2010 James Murty - * * Copyright 2019 Huawei Technologies Co.,Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the @@ -17,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.io; import java.io.ByteArrayInputStream; @@ -29,8 +24,7 @@ import okhttp3.Response; public class HttpMethodReleaseInputStream extends InputStream implements InputStreamWrapper { - private static final ILogger interfaceLog = LoggerBuilder.getLogger("com.obs.services.internal.RestStorageService"); - + private static final ILogger log = LoggerBuilder.getLogger(HttpMethodReleaseInputStream.class); private InputStream inputStream = null; private Response httpResponse = null; private boolean flag = false; @@ -43,8 +37,8 @@ public HttpMethodReleaseInputStream(Response response) { } catch (Exception e) { try { response.close(); - } catch (Exception ee){ - // ignore + } catch (Exception ee) { + log.warn("io close failed.", ee); } this.inputStream = new ByteArrayInputStream(new byte[] {}); } @@ -54,7 +48,7 @@ public Response getHttpResponse() { return httpResponse; } - protected void releaseConnection() throws IOException { + protected void closeConnection() throws IOException { if (!flag) { if (!comsumed && httpResponse != null) { httpResponse.close(); @@ -68,16 +62,17 @@ public int read() throws IOException { try { int read = inputStream.read(); if (read == -1) { - comsumed = true; + comsumed = true; if (!flag) { - releaseConnection(); + closeConnection(); } } return read; } catch (IOException e) { try { - releaseConnection(); - } catch(IOException ignored) { + closeConnection(); + } catch (IOException ignored) { + log.warn("io close failed.", ignored); } throw e; } @@ -88,17 +83,17 @@ public int read(byte[] b, int off, int len) throws IOException { try { int read = inputStream.read(b, off, len); if (read == -1) { - comsumed = true; + comsumed = true; if (!flag) { - releaseConnection(); + closeConnection(); } } return read; } catch (IOException e) { try { - releaseConnection(); - } catch(IOException ignored) { - // + closeConnection(); + } catch (IOException ignored) { + log.warn("io close failed.", ignored); } throw e; } @@ -110,9 +105,9 @@ public int available() throws IOException { return inputStream.available(); } catch (IOException e) { try { - releaseConnection(); - } catch(IOException ignored) { - // + closeConnection(); + } catch (IOException ignored) { + log.warn("io close failed.", ignored); } throw e; } @@ -121,22 +116,11 @@ public int available() throws IOException { @Override public void close() throws IOException { if (!flag) { - releaseConnection(); + closeConnection(); } inputStream.close(); } - @Override - protected void finalize() throws Throwable { - if (!flag) { - releaseConnection(); - if (interfaceLog.isWarnEnabled()) { - interfaceLog.warn("Successfully released HttpMethod in finalize()."); - } - } - super.finalize(); - } - public InputStream getWrappedInputStream() { return inputStream; } diff --git a/app/src/main/java/com/obs/services/internal/io/InputStreamWrapper.java b/app/src/main/java/com/obs/services/internal/io/InputStreamWrapper.java index 9b74749..52e9a4c 100644 --- a/app/src/main/java/com/obs/services/internal/io/InputStreamWrapper.java +++ b/app/src/main/java/com/obs/services/internal/io/InputStreamWrapper.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.io; import java.io.InputStream; diff --git a/app/src/main/java/com/obs/services/internal/io/InterruptableInputStream.java b/app/src/main/java/com/obs/services/internal/io/InterruptableInputStream.java index fca97fc..9f1e617 100644 --- a/app/src/main/java/com/obs/services/internal/io/InterruptableInputStream.java +++ b/app/src/main/java/com/obs/services/internal/io/InterruptableInputStream.java @@ -1,9 +1,4 @@ /** - * JetS3t : Java S3 Toolkit - * Project hosted at http://bitbucket.org/jmurty/jets3t/ - * - * Copyright 2006-2010 James Murty - * * Copyright 2019 Huawei Technologies Co.,Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the @@ -16,12 +11,18 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.io; import java.io.IOException; import java.io.InputStream; +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; + public class InterruptableInputStream extends InputStream implements InputStreamWrapper { + private static final ILogger log = LoggerBuilder.getLogger(InterruptableInputStream.class); + private InputStream inputStream = null; private boolean interrupted = false; @@ -30,11 +31,12 @@ public InterruptableInputStream(InputStream inputStream) { this.inputStream = inputStream; } - private void maybeInterruptInputStream() throws IOException { + private void doInterrupted() throws IOException { if (interrupted) { try { close(); } catch (IOException ioe) { + log.warn("io close failed.", ioe); } throw new UnrecoverableIOException("Reading from input stream deliberately interrupted"); } @@ -42,19 +44,19 @@ private void maybeInterruptInputStream() throws IOException { @Override public int read() throws IOException { - maybeInterruptInputStream(); + doInterrupted(); return inputStream.read(); } @Override public int read(byte[] b, int off, int len) throws IOException { - maybeInterruptInputStream(); + doInterrupted(); return inputStream.read(b, off, len); } @Override public int available() throws IOException { - maybeInterruptInputStream(); + doInterrupted(); return inputStream.available(); } @@ -63,6 +65,7 @@ public void close() throws IOException { inputStream.close(); } + @Override public InputStream getWrappedInputStream() { return inputStream; } diff --git a/app/src/main/java/com/obs/services/internal/io/MayRepeatableInputStream.java b/app/src/main/java/com/obs/services/internal/io/MayRepeatableInputStream.java index ef9877a..328c392 100644 --- a/app/src/main/java/com/obs/services/internal/io/MayRepeatableInputStream.java +++ b/app/src/main/java/com/obs/services/internal/io/MayRepeatableInputStream.java @@ -14,6 +14,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.io; import java.io.BufferedInputStream; @@ -24,118 +25,119 @@ import java.io.InputStream; import java.nio.channels.FileChannel; +import com.obs.services.exception.ObsException; import com.obs.services.internal.ServiceException; public class MayRepeatableInputStream extends FilterInputStream { - - private static class SdkBufferedInputStream extends BufferedInputStream{ - - public SdkBufferedInputStream(InputStream in, int size) { - super(in, size); - } - - public void tearDown() { - this.count = 0; - this.markpos = -1; - this.marklimit = 0; - this.pos = 0; - } - } - - private FileChannel fileChannel; - private long markPos; - private InputStream originInputStream; - - public MayRepeatableInputStream(InputStream in, int bufferSize) { - super(in); - this.init(bufferSize); - this.originInputStream = in; - } - - private void init(int bufferSize) { - if (this.in instanceof FileInputStream) { - fileChannel = ((FileInputStream) in).getChannel(); - try { - markPos = fileChannel.position(); - } catch (IOException e) { - throw new IllegalArgumentException("Invalid FileInputStream", e); - } - } - if(bufferSize > 0) { - this.in = new SdkBufferedInputStream(in, bufferSize); - } - } - - @Override - public final boolean markSupported() { - return fileChannel != null || (originInputStream instanceof ByteArrayInputStream); - } - - protected final void abortIfNeeded() { - if (Thread.interrupted()) { - throw new RuntimeException("Abort io due to thread interrupted"); - } - } - - @Override - public void mark(int a) { - abortIfNeeded(); - try { - if (fileChannel != null) { - markPos = fileChannel.position(); - }else if(originInputStream instanceof ByteArrayInputStream) { - ((ByteArrayInputStream)originInputStream).mark(a); - } - } catch (IOException e) { - throw new ServiceException("Failed to mark the file position", e); - } - } - - @Override - public void reset() throws IOException { - if (fileChannel != null) { - fileChannel.position(markPos); - if(in instanceof SdkBufferedInputStream) { - ((SdkBufferedInputStream)in).tearDown(); - } - } else if(originInputStream instanceof ByteArrayInputStream) { - if(in instanceof SdkBufferedInputStream) { - ((SdkBufferedInputStream)in).tearDown(); - } - ((ByteArrayInputStream)originInputStream).reset(); - } else { - throw new UnrecoverableIOException("UnRepeatable"); - } - } - - @Override - public long skip(long n) throws IOException { - abortIfNeeded(); - return super.skip(n); - } - - @Override - public int available() throws IOException { - abortIfNeeded(); - return super.available(); - } - - @Override - public void close() throws IOException { - super.close(); - abortIfNeeded(); - } - - @Override - public int read() throws IOException { - abortIfNeeded(); - return super.read(); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - abortIfNeeded(); - return super.read(b, off, len); - } + + private static class SdkBufferedInputStream extends BufferedInputStream { + + public SdkBufferedInputStream(InputStream in, int size) { + super(in, size); + } + + public void tearDown() { + this.count = 0; + this.markpos = -1; + this.marklimit = 0; + this.pos = 0; + } + } + + private FileChannel fileChannel; + private long markPos; + private InputStream originInputStream; + + public MayRepeatableInputStream(InputStream in, int bufferSize) { + super(in); + this.init(bufferSize); + this.originInputStream = in; + } + + private void init(int bufferSize) { + if (this.in instanceof FileInputStream) { + fileChannel = ((FileInputStream) in).getChannel(); + try { + markPos = fileChannel.position(); + } catch (IOException e) { + throw new IllegalArgumentException("Invalid FileInputStream", e); + } + } + if (bufferSize > 0) { + this.in = new SdkBufferedInputStream(in, bufferSize); + } + } + + @Override + public final boolean markSupported() { + return fileChannel != null || (originInputStream instanceof ByteArrayInputStream); + } + + protected final void throwExceptionWhileInterrupted() { + if (Thread.interrupted()) { + throw new ObsException("Abort io due to thread interrupted"); + } + } + + @Override + public void mark(int a) { + throwExceptionWhileInterrupted(); + try { + if (fileChannel != null) { + markPos = fileChannel.position(); + } else if (originInputStream instanceof ByteArrayInputStream) { + ((ByteArrayInputStream) originInputStream).mark(a); + } + } catch (IOException e) { + throw new ServiceException("Failed to mark the file position", e); + } + } + + @Override + public void reset() throws IOException { + if (fileChannel != null) { + fileChannel.position(markPos); + if (in instanceof SdkBufferedInputStream) { + ((SdkBufferedInputStream) in).tearDown(); + } + } else if (originInputStream instanceof ByteArrayInputStream) { + if (in instanceof SdkBufferedInputStream) { + ((SdkBufferedInputStream) in).tearDown(); + } + ((ByteArrayInputStream) originInputStream).reset(); + } else { + throw new UnrecoverableIOException("UnRepeatable"); + } + } + + @Override + public long skip(long n) throws IOException { + throwExceptionWhileInterrupted(); + return super.skip(n); + } + + @Override + public int available() throws IOException { + throwExceptionWhileInterrupted(); + return super.available(); + } + + @Override + public void close() throws IOException { + super.close(); + throwExceptionWhileInterrupted(); + } + + @Override + public int read() throws IOException { + throwExceptionWhileInterrupted(); + return super.read(); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + throwExceptionWhileInterrupted(); + return super.read(b, off, len); + } } diff --git a/app/src/main/java/com/obs/services/internal/io/ProgressInputStream.java b/app/src/main/java/com/obs/services/internal/io/ProgressInputStream.java index c6ca292..0232e80 100644 --- a/app/src/main/java/com/obs/services/internal/io/ProgressInputStream.java +++ b/app/src/main/java/com/obs/services/internal/io/ProgressInputStream.java @@ -14,92 +14,93 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.io; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; +import com.obs.services.exception.ObsException; import com.obs.services.internal.ProgressManager; public class ProgressInputStream extends FilterInputStream { - - private boolean readFlag; - private ProgressManager progressManager; - private boolean endFlag; - - public ProgressInputStream(InputStream in, ProgressManager progressManager) { - this(in, progressManager, true); - } - - public ProgressInputStream(InputStream in, ProgressManager progressManager, boolean endFlag) { - super(in); - this.progressManager = progressManager; - this.endFlag = endFlag; - } - - @Override - public final boolean markSupported() { - return false; - } - - protected final void abortIfNeeded() { - if (Thread.interrupted()) { - throw new RuntimeException("Abort io due to thread interrupted"); - } - } - - @Override - public void mark(int a) { - abortIfNeeded(); - } - - @Override - public void reset() throws IOException { - throw new UnrecoverableIOException("UnRepeatable"); - } - - @Override - public long skip(long n) throws IOException { - abortIfNeeded(); - return super.skip(n); - } - - @Override - public int available() throws IOException { - abortIfNeeded(); - return super.available(); - } - - @Override - public void close() throws IOException { - try { - super.close(); - abortIfNeeded(); - }finally { - if(endFlag) { - this.progressManager.progressEnd(); - } - } - } - - @Override - public int read() throws IOException { - abortIfNeeded(); - return super.read(); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - abortIfNeeded(); - if (!this.readFlag) - { + + private boolean readFlag; + private ProgressManager progressManager; + private boolean endFlag; + + public ProgressInputStream(InputStream in, ProgressManager progressManager) { + this(in, progressManager, true); + } + + public ProgressInputStream(InputStream in, ProgressManager progressManager, boolean endFlag) { + super(in); + this.progressManager = progressManager; + this.endFlag = endFlag; + } + + @Override + public final boolean markSupported() { + return false; + } + + protected final void abortWhileThreadIsInterrupted() { + if (Thread.interrupted()) { + throw new ObsException("Abort io due to thread interrupted"); + } + } + + @Override + public void mark(int a) { + abortWhileThreadIsInterrupted(); + } + + @Override + public void reset() throws IOException { + throw new UnrecoverableIOException("UnRepeatable"); + } + + @Override + public long skip(long n) throws IOException { + abortWhileThreadIsInterrupted(); + return super.skip(n); + } + + @Override + public int available() throws IOException { + abortWhileThreadIsInterrupted(); + return super.available(); + } + + @Override + public void close() throws IOException { + try { + super.close(); + abortWhileThreadIsInterrupted(); + } finally { + if (endFlag) { + this.progressManager.progressEnd(); + } + } + } + + @Override + public int read() throws IOException { + abortWhileThreadIsInterrupted(); + return super.read(); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + abortWhileThreadIsInterrupted(); + if (!this.readFlag) { this.readFlag = true; this.progressManager.progressStart(); } - int bytes = super.read(b, off, len); - this.progressManager.progressChanged(bytes); - return bytes; - } + int bytes = super.read(b, off, len); + this.progressManager.progressChanged(bytes); + return bytes; + } } diff --git a/app/src/main/java/com/obs/services/internal/io/UnrecoverableIOException.java b/app/src/main/java/com/obs/services/internal/io/UnrecoverableIOException.java index d63258b..ceeaea2 100644 --- a/app/src/main/java/com/obs/services/internal/io/UnrecoverableIOException.java +++ b/app/src/main/java/com/obs/services/internal/io/UnrecoverableIOException.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.io; import java.io.IOException; diff --git a/app/src/main/java/com/obs/services/internal/security/BasicSecurityKey.java b/app/src/main/java/com/obs/services/internal/security/BasicSecurityKey.java index 5b0b722..09e4a19 100644 --- a/app/src/main/java/com/obs/services/internal/security/BasicSecurityKey.java +++ b/app/src/main/java/com/obs/services/internal/security/BasicSecurityKey.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.security; import com.obs.services.model.ISecurityKey; @@ -20,12 +21,12 @@ public class BasicSecurityKey implements ISecurityKey { protected String secretKey; protected String securityToken; - public BasicSecurityKey(String accessKey, String secretKey){ + public BasicSecurityKey(String accessKey, String secretKey) { this.accessKey = accessKey; this.secretKey = secretKey; } - public BasicSecurityKey(String accessKey, String secretKey, String securityToken){ + public BasicSecurityKey(String accessKey, String secretKey, String securityToken) { this(accessKey, secretKey); this.securityToken = securityToken; } diff --git a/app/src/main/java/com/obs/services/internal/security/EcsSecurityUtils.java b/app/src/main/java/com/obs/services/internal/security/EcsSecurityUtils.java index 8bd89f5..104faf8 100644 --- a/app/src/main/java/com/obs/services/internal/security/EcsSecurityUtils.java +++ b/app/src/main/java/com/obs/services/internal/security/EcsSecurityUtils.java @@ -11,18 +11,17 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.security; +import java.io.IOException; +import java.util.concurrent.TimeUnit; + import okhttp3.Call; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - public class EcsSecurityUtils { /** * Default root url for the openstack metadata apis. @@ -36,19 +35,21 @@ public class EcsSecurityUtils { private static final String EC2_METADATA_SERVICE_OVERRIDE_URL = "ecsMetadataServiceOverrideEndpoint"; - private static final long HTTP_CONNECT_TIMEOUT_VALUE = 50 * 1000; + private static final long HTTP_CONNECT_TIMEOUT_VALUE = 30 * 1000; private static OkHttpClient httpClient = new OkHttpClient.Builder().followRedirects(false) - .retryOnConnectionFailure(true).cache(null).connectTimeout(HTTP_CONNECT_TIMEOUT_VALUE, TimeUnit.MILLISECONDS) - .writeTimeout(HTTP_CONNECT_TIMEOUT_VALUE, TimeUnit.MILLISECONDS).readTimeout(HTTP_CONNECT_TIMEOUT_VALUE, TimeUnit.MILLISECONDS).build(); + .retryOnConnectionFailure(true).cache(null) + .connectTimeout(HTTP_CONNECT_TIMEOUT_VALUE, TimeUnit.MILLISECONDS) + .writeTimeout(HTTP_CONNECT_TIMEOUT_VALUE, TimeUnit.MILLISECONDS) + .readTimeout(HTTP_CONNECT_TIMEOUT_VALUE, TimeUnit.MILLISECONDS).build(); /** - * Returns the temporary security credentials (access, secret, securitytoken, - * and expires_at) associated with the IAM roles on the instance. + * Returns the temporary security credentials (access, secret, + * securitytoken, and expires_at) associated with the IAM roles on the + * instance. */ - public static List getSecurityKeyInfoWithDetail() throws IOException { - List list = getResourceWithDetail(getEndpointForECSMetadataService() + OPENSTACK_METADATA_ROOT + "/securitykey"); - return list; + public static String getSecurityKeyInfoWithDetail() throws IOException { + return getResourceWithDetail(getEndpointForECSMetadataService() + OPENSTACK_METADATA_ROOT + "/securitykey"); } /** @@ -60,17 +61,16 @@ public static String getEndpointForECSMetadataService() { } /** - * Get resource and return contents from metadata service - * with the specify path. + * Get resource and return contents from metadata service with the specify + * path. */ - private static List getResourceWithDetail(String endpoint) throws IOException { + private static String getResourceWithDetail(String endpoint) throws IOException { Request.Builder builder = new Request.Builder(); builder.header("Accept", "*/*"); Request request = builder.url(endpoint).get().build(); Call c = httpClient.newCall(request); Response res = null; String content = ""; - List list = new ArrayList(); try { res = c.execute(); String header = ""; @@ -80,20 +80,18 @@ private static List getResourceWithDetail(String endpoint) throws IOExce if (res.body() != null) { content = res.body().string(); } - String detail = "Get securityKey form ECS failed code:" + res.code() + " the headers : " + header + "the content : " + content; - if(!(res.code() >= 200 && res.code() < 300)){ - String errorMessage = "Get securityKey form ECS failed the detail : " +detail; + if (!(res.code() >= 200 && res.code() < 300)) { + String errorMessage = "Get securityKey form ECS failed, Code : " + res.code() + "; Headers : " + header + + "; Content : " + content; throw new IllegalArgumentException(errorMessage); } - list.add(content); - list.add(detail); + return content; } finally { if (res != null) { res.close(); } } - return list; } } diff --git a/app/src/main/java/com/obs/services/internal/security/LimitedTimeSecurityKey.java b/app/src/main/java/com/obs/services/internal/security/LimitedTimeSecurityKey.java index d8fa7f0..979160b 100644 --- a/app/src/main/java/com/obs/services/internal/security/LimitedTimeSecurityKey.java +++ b/app/src/main/java/com/obs/services/internal/security/LimitedTimeSecurityKey.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.security; import java.util.Calendar; @@ -19,12 +20,9 @@ import com.obs.services.internal.utils.ServiceUtils; public class LimitedTimeSecurityKey extends BasicSecurityKey { - protected String accessKey; - protected String secretKey; - protected String securityToken; protected Date expiryDate; - private static final long expirySeconds = 5 * 60; - private static final long willSoonExpireSeconds = 60; + private static final long EXPIRY_SECONDS = 5 * 60L; + private static final long WILL_SOON_EXPIRE_SECONDS = 2 * 60L; public LimitedTimeSecurityKey(String accessKey, String secretKey, String securityToken) { super(accessKey, secretKey, securityToken); @@ -42,36 +40,33 @@ public LimitedTimeSecurityKey(String accessKey, String secretKey, String securit this.expiryDate = ServiceUtils.cloneDateIgnoreNull(expiryDate); } + /** + * about 2~5 minutes + * + * @return + */ public boolean aboutToExpire() { - return (expiryDate.getTime() - getUtcTime().getTime()) >= willSoonExpireSeconds * 1000 && (expiryDate.getTime() - getUtcTime().getTime()) < expirySeconds * 1000; + return (expiryDate.getTime() - getUtcTime().getTime()) >= WILL_SOON_EXPIRE_SECONDS * 1000 + && (expiryDate.getTime() - getUtcTime().getTime()) < EXPIRY_SECONDS * 1000; } + /** + * less than 2 minutes + * + * @return + */ public boolean willSoonExpire() { - return expiryDate.before(getUtcTime()) || (expiryDate.getTime() - getUtcTime().getTime()) < willSoonExpireSeconds * 1000; + return expiryDate.before(getUtcTime()) + || (expiryDate.getTime() - getUtcTime().getTime()) < WILL_SOON_EXPIRE_SECONDS * 1000; } - private static Date getUtcTime() { + public static Date getUtcTime() { Calendar calendar = Calendar.getInstance(); int offset = calendar.get(Calendar.ZONE_OFFSET); calendar.add(Calendar.MILLISECOND, -offset); return calendar.getTime(); } - @Override - public String getAccessKey() { - return accessKey; - } - - @Override - public String getSecretKey() { - return secretKey; - } - - @Override - public String getSecurityToken() { - return securityToken; - } - public Date getExpiryDate() { return ServiceUtils.cloneDateIgnoreNull(this.expiryDate); } diff --git a/app/src/main/java/com/obs/services/internal/security/ProviderCredentialThreadContext.java b/app/src/main/java/com/obs/services/internal/security/ProviderCredentialThreadContext.java index 101e574..3b3da2b 100644 --- a/app/src/main/java/com/obs/services/internal/security/ProviderCredentialThreadContext.java +++ b/app/src/main/java/com/obs/services/internal/security/ProviderCredentialThreadContext.java @@ -11,35 +11,35 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.security; -public class ProviderCredentialThreadContext -{ - - private ThreadLocal context = new ThreadLocal(); - - private ProviderCredentialThreadContext(){ - +public class ProviderCredentialThreadContext { + + private static ThreadLocal context = new ThreadLocal(); + + private ProviderCredentialThreadContext() { + } - - private static class ProviderCredentialThreadContextHolder{ + + private static class ProviderCredentialThreadContextHolder { private static ProviderCredentialThreadContext instance = new ProviderCredentialThreadContext(); } - - public static ProviderCredentialThreadContext getInstance(){ + + public static ProviderCredentialThreadContext getInstance() { return ProviderCredentialThreadContextHolder.instance; } - - public void setProviderCredentials(ProviderCredentials providerCredentials){ + + public void setProviderCredentials(ProviderCredentials providerCredentials) { context.set(providerCredentials); } - - public ProviderCredentials getProviderCredentials(){ + + public ProviderCredentials getProviderCredentials() { return context.get(); } - - public void clearProviderCredentials(){ + + public void clearProviderCredentials() { context.remove(); } - + } diff --git a/app/src/main/java/com/obs/services/internal/security/ProviderCredentials.java b/app/src/main/java/com/obs/services/internal/security/ProviderCredentials.java index 48a939e..3c8d207 100644 --- a/app/src/main/java/com/obs/services/internal/security/ProviderCredentials.java +++ b/app/src/main/java/com/obs/services/internal/security/ProviderCredentials.java @@ -11,77 +11,96 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.security; import com.obs.log.ILogger; import com.obs.log.LoggerBuilder; import com.obs.services.BasicObsCredentialsProvider; -import com.obs.services.EcsObsCredentialsProvider; import com.obs.services.IObsCredentialsProvider; import com.obs.services.internal.ObsConstraint; import com.obs.services.model.AuthTypeEnum; +import java.util.LinkedHashMap; +import java.util.Map; + public class ProviderCredentials { protected static final ILogger log = LoggerBuilder.getLogger(ProviderCredentials.class); protected AuthTypeEnum authType; - private ThreadLocal threadLocalAuthType; + + private LinkedHashMap localAuthType; + private IObsCredentialsProvider obsCredentialsProvider; + private boolean isAuthTypeNegotiation; public String getRegion() { return ObsConstraint.DEFAULT_BUCKET_LOCATION_VALUE; } - public ProviderCredentials(String accessKey, String secretKey) { - this.setObsCredentialsProvider(new BasicObsCredentialsProvider(accessKey, secretKey)); - } - public ProviderCredentials(String accessKey, String secretKey, String securityToken) { this.setObsCredentialsProvider(new BasicObsCredentialsProvider(accessKey, secretKey, securityToken)); } + public void setLocalAuthTypeCacheCapacity(int localAuthTypeCacheCapacity) { + localAuthType = new LinkedHashMap(localAuthTypeCacheCapacity, 0.7F, true) { + @Override + protected boolean removeEldestEntry(Map.Entry entry) { + return this.size() > localAuthTypeCacheCapacity; + } + }; + } + public AuthTypeEnum getAuthType() { - return (threadLocalAuthType == null) ? authType : threadLocalAuthType.get(); + return authType; } public void setAuthType(AuthTypeEnum authType) { this.authType = authType; } - public void setObsCredentialsProvider(IObsCredentialsProvider ObsCredentialsProvider) { - this.obsCredentialsProvider = ObsCredentialsProvider; + public void setIsAuthTypeNegotiation(boolean isAuthTypeNegotiation) { + this.isAuthTypeNegotiation = isAuthTypeNegotiation; } - public IObsCredentialsProvider getObsCredentialsProvider(){ - return this.obsCredentialsProvider; + public boolean getIsAuthTypeNegotiation() { + return isAuthTypeNegotiation; } - public BasicSecurityKey getSecurityKey(){ - return (BasicSecurityKey)this.obsCredentialsProvider.getSecurityKey(); + public void setObsCredentialsProvider(IObsCredentialsProvider obsCredentialsProvider) { + this.obsCredentialsProvider = obsCredentialsProvider; } + public IObsCredentialsProvider getObsCredentialsProvider() { + return this.obsCredentialsProvider; + } - public void setThreadLocalAuthType(AuthTypeEnum authType) { - if (threadLocalAuthType != null) { - threadLocalAuthType.set(authType); - } + public BasicSecurityKey getSecurityKey() { + return (BasicSecurityKey) this.obsCredentialsProvider.getSecurityKey(); } - public void removeThreadLocalAuthType() { - if (threadLocalAuthType != null) { - threadLocalAuthType.remove(); + public AuthTypeEnum getLocalAuthType(String bucketName) { + if (!isAuthTypeNegotiation) { + return authType; } + AuthTypeEnum authTypeEnum = localAuthType.get(bucketName); + return authTypeEnum == null ? authType : authTypeEnum; } - public void initThreadLocalAuthType() { - if (threadLocalAuthType == null) { - threadLocalAuthType = new ThreadLocal() { - @Override - protected AuthTypeEnum initialValue() { - return ProviderCredentials.this.authType; - } - }; + public void setLocalAuthType(String bucketName, AuthTypeEnum authType) { + if (localAuthType == null || bucketName.isEmpty()) { + return; } + + localAuthType.put(bucketName, authType); + } + + public LinkedHashMap getLocalAuthType() { + return localAuthType; + } + + public void setLocalAuthType(LinkedHashMap localAuthType) { + this.localAuthType = localAuthType; } } diff --git a/app/src/main/java/com/obs/services/internal/security/SecurityKey.java b/app/src/main/java/com/obs/services/internal/security/SecurityKey.java index 39887d8..b563f4c 100644 --- a/app/src/main/java/com/obs/services/internal/security/SecurityKey.java +++ b/app/src/main/java/com/obs/services/internal/security/SecurityKey.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.security; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/app/src/main/java/com/obs/services/internal/security/SecurityKeyBean.java b/app/src/main/java/com/obs/services/internal/security/SecurityKeyBean.java index 3beae43..c40ea8e 100644 --- a/app/src/main/java/com/obs/services/internal/security/SecurityKeyBean.java +++ b/app/src/main/java/com/obs/services/internal/security/SecurityKeyBean.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.security; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/app/src/main/java/com/obs/services/internal/service/AbstractRequestConvertor.java b/app/src/main/java/com/obs/services/internal/service/AbstractRequestConvertor.java new file mode 100644 index 0000000..7c5199c --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/service/AbstractRequestConvertor.java @@ -0,0 +1,390 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal.service; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.internal.Constants; +import com.obs.services.internal.IHeaders; +import com.obs.services.internal.ObsConstraint; +import com.obs.services.internal.RestStorageService; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.trans.NewTransResult; +import com.obs.services.internal.utils.Mimetypes; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.AvailableZoneEnum; +import com.obs.services.model.BaseObjectRequest; +import com.obs.services.model.BucketTypeEnum; +import com.obs.services.model.GenericRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.StorageClassEnum; +import com.obs.services.model.fs.FSStatusEnum; +import com.obs.services.model.fs.GetBucketFSStatusResult; +import okhttp3.Headers; +import okhttp3.MediaType; +import okhttp3.RequestBody; +import okhttp3.Response; + +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Method; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +public abstract class AbstractRequestConvertor extends RestStorageService { + private static final ILogger log = LoggerBuilder.getLogger("com.obs.services.ObsClient"); + + protected static class TransResult { + private Map headers; + + private Map params; + + // todo 临时手段,重构时要改掉 + private Map userHeaders = new HashMap<>(); + + private RequestBody body; + + TransResult(Map headers) { + this(headers, null, null); + } + + TransResult(Map headers, RequestBody body) { + this(headers, null, body); + } + + TransResult(Map headers, Map params, RequestBody body) { + this.headers = headers; + this.params = params; + this.body = body; + } + + public Map getHeaders() { + if (this.headers == null) { + headers = new HashMap<>(); + } + return this.headers; + } + + public Map getUserHeaders() { + return this.userHeaders; + } + + public void addUserHeaders(String key, String value) { + userHeaders.put(key, value); + } + + public Map getParams() { + if (this.params == null) { + params = new HashMap<>(); + } + return this.params; + } + + public void setParams(Map params) { + this.params = params; + } + + public void setBody(RequestBody body) { + this.body = body; + } + + public RequestBody getBody() { + return body; + } + } + + /** + * set requestHeader for requestPayment + * + * @param isRequesterPays + * @param headers + * @param iheaders + * @throws ServiceException + */ + protected Map transRequestPaymentHeaders(boolean isRequesterPays, Map headers, + IHeaders iheaders) throws ServiceException { + if (isRequesterPays) { + if (null == headers) { + headers = new HashMap<>(); + } + putHeader(headers, iheaders.requestPaymentHeader(), "requester"); + } + + return headers; + } + + /** + * set requestHeader for requestPayment + * + * @param request + * @param headers + * @param iheaders + * @throws ServiceException + */ + protected Map transRequestPaymentHeaders(GenericRequest request, Map headers, + IHeaders iheaders) throws ServiceException { + if (null != request) { + return transRequestPaymentHeaders(request.isRequesterPays(), headers, iheaders); + } + + return null; + } + + protected String getHeaderByMethodName(String bucketName, String code) { + try { + IHeaders iheaders = this.getIHeaders(bucketName); + Method m = iheaders.getClass().getMethod(code); + Object result = m.invoke(iheaders); + return result == null ? "" : result.toString(); + } catch (Exception e) { + if (log.isWarnEnabled()) { + log.warn("Invoke getHeaderByMethodName error", e); + } + } + return null; + } + + protected void putHeader(Map headers, String key, String value) { + if (ServiceUtils.isValid(key)) { + headers.put(key, value); + } + } + + protected HeaderResponse build(Response res) { + HeaderResponse response = new HeaderResponse(); + setHeadersAndStatus(response, res); + return response; + } + + protected void setHeadersAndStatus(HeaderResponse response, Response res) { + setHeadersAndStatus(response, res, true); + } + + protected void setHeadersAndStatus(HeaderResponse response, Response res, boolean needDecode) { + response.setStatusCode(res.code()); + Map> headerMap = res.headers().toMultimap(); + Map originalHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + Map responseHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + for (Map.Entry> entry : headerMap.entrySet()) { + String key = entry.getKey(); + List values = entry.getValue(); + if (!(key == null || values == null)) { + Object originalValue = values.size() == 1 ? values.get(0) : values; + originalHeaders.put(key, originalValue); + putCleanedKeyAndValues(responseHeaders, key, values, needDecode); + } + } + + response.setOriginalHeaders(originalHeaders); + response.setResponseHeaders(responseHeaders); + } + + protected void putCleanedKeyAndValues(Map responseHeaders, + String key, List values, boolean needDecode) { + String cleanedKey = key.toLowerCase(); + List cleanedValues = new ArrayList<>(values.size()); + Object finalValues; + if ((Constants.CommonHeaders.DATE.equalsIgnoreCase(key) + || Constants.CommonHeaders.LAST_MODIFIED.equalsIgnoreCase(key))) { + finalValues = values.get(0); + if (log.isDebugEnabled()) { + log.debug("Parsing date string '" + finalValues + "' into Date object for key: " + key); + } + try { + finalValues = ServiceUtils.parseRfc822Date(finalValues.toString()); + } catch (ParseException pe) { + // Try ISO-8601 + try { + finalValues = ServiceUtils.parseIso8601Date(finalValues.toString()); + } catch (ParseException pe2) { + if (log.isWarnEnabled()) { + log.warn("Date string is not RFC 822 or ISO-8601 compliant for metadata field " + key, pe); + } + } + } + } else { + for (String prefix : Constants.NOT_NEED_HEADER_PREFIXES) { + if (key.toLowerCase().startsWith(prefix)) { + cleanedKey = cleanedKey.replace(prefix, ""); + break; + } + } + for (String value : values) { + if (needDecode) { + try { + cleanedValues.add(URLDecoder.decode(value, Constants.DEFAULT_ENCODING)); + } catch (UnsupportedEncodingException | IllegalArgumentException e) { + if (log.isDebugEnabled()) { + log.debug("Error to decode value of key:" + key); + } + } + } else { + cleanedValues.add(value); + } + } + if (needDecode) { + try { + cleanedKey = URLDecoder.decode(cleanedKey, Constants.DEFAULT_ENCODING); + } catch (UnsupportedEncodingException | IllegalArgumentException e) { + if (log.isWarnEnabled()) { + log.debug("Error to decode key:" + key); + } + } + } + finalValues = cleanedValues.size() == 1 ? cleanedValues.get(0) : cleanedValues; + } + responseHeaders.put(cleanedKey, finalValues); + } + + protected SpecialParamEnum getSpecialParamForStorageClass(String bucketName) { + return this.getProviderCredentials().getLocalAuthType(bucketName) == AuthTypeEnum.OBS + ? SpecialParamEnum.STORAGECLASS : SpecialParamEnum.STORAGEPOLICY; + } + + protected RequestBody createRequestBody(String mimeType, String content) throws ServiceException { + if (log.isTraceEnabled()) { + log.trace("Entity Content:" + content); + } + return RequestBody.create(content.getBytes(StandardCharsets.UTF_8), MediaType.parse(mimeType)); + } + + protected GetBucketFSStatusResult getOptionInfoResult(String bucketName, Response response) { + + Headers headers = response.headers(); + + Map> map = headers.toMultimap(); + String maxAge = headers.get(Constants.CommonHeaders.ACCESS_CONTROL_MAX_AGE); + + IHeaders iheaders = this.getIHeaders(bucketName); + FSStatusEnum status = FSStatusEnum.getValueFromCode(headers.get(iheaders.fsFileInterfaceHeader())); + + BucketTypeEnum bucketType = BucketTypeEnum.OBJECT; + if (FSStatusEnum.ENABLED == status) { + bucketType = BucketTypeEnum.PFS; + } + + GetBucketFSStatusResult output = new GetBucketFSStatusResult.Builder() + .allowOrigin(headers.get(Constants.CommonHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)) + .allowHeaders(map.get(Constants.CommonHeaders.ACCESS_CONTROL_ALLOW_HEADERS)) + .maxAge(maxAge == null ? 0 : Integer.parseInt(maxAge)) + .allowMethods(map.get(Constants.CommonHeaders.ACCESS_CONTROL_ALLOW_METHODS)) + .exposeHeaders(map.get(Constants.CommonHeaders.ACCESS_CONTROL_EXPOSE_HEADERS)) + .storageClass(StorageClassEnum.getValueFromCode(headers.get(iheaders.defaultStorageClassHeader()))) + .location(headers.get(iheaders.bucketRegionHeader())) + .obsVersion(headers.get(iheaders.serverVersionHeader())) + .status(status) + .availableZone(AvailableZoneEnum.getValueFromCode(headers.get(iheaders.azRedundancyHeader()))) + .epid(headers.get(iheaders.epidHeader())) + .bucketType(bucketType).build(); + + setHeadersAndStatus(output, response); + return output; + } + + protected AuthTypeEnum getApiVersion(String bucketName) throws ServiceException { + if (!ServiceUtils.isValid(bucketName)) { + return parseAuthTypeInResponse(""); + } + + return parseAuthTypeInResponse(bucketName); + } + + protected void verifyResponseContentType(Response response) throws ServiceException { + if (this.obsProperties.getBoolProperty(ObsConstraint.VERIFY_RESPONSE_CONTENT_TYPE, true)) { + String contentType = response.header(Constants.CommonHeaders.CONTENT_TYPE); + if (!Mimetypes.MIMETYPE_XML.equalsIgnoreCase(contentType) + && !Mimetypes.MIMETYPE_TEXT_XML.equalsIgnoreCase(contentType)) { + throw new ServiceException( + "Expected XML document response from OBS but received content type " + contentType); + } + } + } + + protected void verifyResponseContentTypeForJson(Response response) throws ServiceException { + if (this.obsProperties.getBoolProperty(ObsConstraint.VERIFY_RESPONSE_CONTENT_TYPE, true)) { + String contentType = response.header(Constants.CommonHeaders.CONTENT_TYPE); + if (null == contentType) { + throw new ServiceException("Expected JSON document response but received content type is null"); + } else if (!contentType.contains(Mimetypes.MIMETYPE_JSON)) { + throw new ServiceException( + "Expected JSON document response but received content type is " + contentType); + } + } + } + + private AuthTypeEnum parseAuthTypeInResponse(String bucketName) throws ServiceException { + Response response; + try { + response = getAuthTypeNegotiationResponseImpl(bucketName); + } catch (ServiceException e) { + if (e.getResponseCode() == 404 || e.getResponseCode() <= 0 || e.getResponseCode() == 408 + || e.getResponseCode() >= 500) { + throw e; + } else { + return AuthTypeEnum.V2; + } + } + String apiVersion; + return (response.code() == 200 && (apiVersion = response.headers().get("x-obs-api")) != null + && apiVersion.compareTo("3.0") >= 0) ? AuthTypeEnum.OBS : AuthTypeEnum.V2; + } + + private Response getAuthTypeNegotiationResponseImpl(String bucketName) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put("apiversion", ""); + return performRestForApiVersion(bucketName, null, requestParameters, null); + } + + protected NewTransResult transRequestWithResult(TransResult result, GenericRequest request) { + NewTransResult newResult = new NewTransResult(); + newResult.setHttpMethod(request.getHttpMethod()); + newResult.setBucketName(request.getBucketName()); + newResult.setHeaders(result.getHeaders()); + newResult.setUserHeaders(request.getUserHeaders()); + newResult.setBody(result.getBody()); + newResult.setParams(result.getParams()); + return newResult; + } + + protected NewTransResult transObjectRequestWithResult(TransResult result, BaseObjectRequest request) { + NewTransResult newTransResult = transRequestWithResult(result, request); + newTransResult.setObjectKey(request.getObjectKey()); + newTransResult.setIsEncodeHeaders(request.isEncodeHeaders()); + return newTransResult; + } + + protected NewTransResult transRequest(GenericRequest request) { + NewTransResult newResult = new NewTransResult(); + newResult.setHttpMethod(request.getHttpMethod()); + newResult.setBucketName(request.getBucketName()); + newResult.setUserHeaders(request.getUserHeaders()); + return newResult; + } + + protected NewTransResult transObjectRequest(BaseObjectRequest request) { + NewTransResult newTransResult = transRequest(request); + newTransResult.setObjectKey(request.getObjectKey()); + newTransResult.setIsEncodeHeaders(request.isEncodeHeaders()); + return newTransResult; + } +} diff --git a/app/src/main/java/com/obs/services/internal/service/AclHeaderConvertor.java b/app/src/main/java/com/obs/services/internal/service/AclHeaderConvertor.java new file mode 100644 index 0000000..031942d --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/service/AclHeaderConvertor.java @@ -0,0 +1,194 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal.service; + +import java.util.HashMap; +import java.util.Map; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.internal.Constants; +import com.obs.services.internal.ObsConstraint; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.trans.NewTransResult; +import com.obs.services.internal.utils.Mimetypes; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.HttpMethodEnum; +import com.obs.services.model.SpecialParamEnum; + +public abstract class AclHeaderConvertor extends AbstractRequestConvertor { + private static final ILogger log = LoggerBuilder.getLogger("com.obs.services.ObsClient"); + + boolean prepareRESTHeaderAclForV2(String bucketName, Map metadata, AccessControlList acl) { + String restHeaderAclValue = null; + if (acl == AccessControlList.REST_CANNED_PRIVATE) { + restHeaderAclValue = Constants.ACL_PRIVATE; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_DELIVERED) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE_DELIVERED) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE; + } else if (acl == AccessControlList.REST_CANNED_AUTHENTICATED_READ) { + restHeaderAclValue = Constants.ACL_AUTHENTICATED_READ; + } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_READ) { + restHeaderAclValue = Constants.ACL_BUCKET_OWNER_READ; + } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_FULL_CONTROL) { + restHeaderAclValue = Constants.ACL_BUCKET_OWNER_FULL_CONTROL; + } else if (acl == AccessControlList.REST_CANNED_LOG_DELIVERY_WRITE) { + restHeaderAclValue = Constants.ACL_LOG_DELIVERY_WRITE; + } + String aclHeader = this.getIHeaders(bucketName).aclHeader(); + if (restHeaderAclValue != null) { + metadata.put(aclHeader, restHeaderAclValue); + } + return metadata.containsKey(aclHeader); + } + + boolean prepareRESTHeaderAclForOBS(String bucketName, Map metadata, + AccessControlList acl) throws ServiceException { + String restHeaderAclValue = null; + boolean invalid = false; + if (acl == AccessControlList.REST_CANNED_PRIVATE) { + restHeaderAclValue = Constants.ACL_PRIVATE; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_DELIVERED) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ_DELIVERED; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE_DELIVERED) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE_DELIVERED; + } else if (acl == AccessControlList.REST_CANNED_AUTHENTICATED_READ) { + restHeaderAclValue = Constants.ACL_AUTHENTICATED_READ; + invalid = true; + } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_READ) { + restHeaderAclValue = Constants.ACL_BUCKET_OWNER_READ; + invalid = true; + } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_FULL_CONTROL) { + restHeaderAclValue = Constants.ACL_BUCKET_OWNER_FULL_CONTROL; + invalid = true; + } else if (acl == AccessControlList.REST_CANNED_LOG_DELIVERY_WRITE) { + restHeaderAclValue = Constants.ACL_LOG_DELIVERY_WRITE; + invalid = true; + } + if (invalid) { + log.info("Invalid Canned ACL:" + restHeaderAclValue); + } + + String aclHeader = this.getIHeaders(bucketName).aclHeader(); + if (restHeaderAclValue != null) { + metadata.put(aclHeader, restHeaderAclValue); + } + return metadata.containsKey(aclHeader); + } + + protected boolean prepareRESTHeaderAclObject(String bucketName, Map metadata, AccessControlList acl) + throws ServiceException { + return this.getProviderCredentials().getLocalAuthType(bucketName) == AuthTypeEnum.OBS + ? this.prepareRESTHeaderAclForOBSObject(bucketName, metadata, acl) + : this.prepareRESTHeaderAclForV2(bucketName, metadata, acl); + } + + boolean prepareRESTHeaderAclForOBSObject(String bucketName, Map metadata, AccessControlList acl) + throws ServiceException { + String restHeaderAclValue = null; + boolean invalid = false; + if (acl == AccessControlList.REST_CANNED_PRIVATE) { + restHeaderAclValue = Constants.ACL_PRIVATE; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_DELIVERED) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ; + } else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE_DELIVERED) { + restHeaderAclValue = Constants.ACL_PUBLIC_READ_WRITE; + } else if (acl == AccessControlList.REST_CANNED_AUTHENTICATED_READ) { + restHeaderAclValue = Constants.ACL_AUTHENTICATED_READ; + invalid = true; + } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_READ) { + restHeaderAclValue = Constants.ACL_BUCKET_OWNER_READ; + invalid = true; + } else if (acl == AccessControlList.REST_CANNED_BUCKET_OWNER_FULL_CONTROL) { + restHeaderAclValue = Constants.ACL_BUCKET_OWNER_FULL_CONTROL; + invalid = true; + } else if (acl == AccessControlList.REST_CANNED_LOG_DELIVERY_WRITE) { + restHeaderAclValue = Constants.ACL_LOG_DELIVERY_WRITE; + invalid = true; + } + if (invalid) { + log.info("Invalid Canned ACL:" + restHeaderAclValue); + } + + String aclHeader = this.getIHeaders(bucketName).aclHeader(); + if (restHeaderAclValue != null) { + metadata.put(aclHeader, restHeaderAclValue); + } + return metadata.containsKey(aclHeader); + } + + protected boolean prepareRESTHeaderAcl(String bucketName, Map metadata, AccessControlList acl) + throws ServiceException { + return this.getProviderCredentials().getLocalAuthType(bucketName) == AuthTypeEnum.OBS + ? this.prepareRESTHeaderAclForOBS(bucketName, metadata, acl) + : this.prepareRESTHeaderAclForV2(bucketName, metadata, acl); + } + + protected String getCredential(String shortDate, String accessKey) { + return new StringBuilder(accessKey).append("/").append(shortDate).append("/") + .append(ObsConstraint.DEFAULT_BUCKET_LOCATION_VALUE).append("/").append(Constants.SERVICE).append("/") + .append(Constants.REQUEST_TAG).toString(); + } + + /** + * @param bucketName + * @param objectKey + * @param acl + * @param versionId + * @param isRequesterPays + * @throws ServiceException + */ + protected void putAclImpl(String bucketName, String objectKey, AccessControlList acl, String versionId, + boolean isRequesterPays) throws ServiceException { + if (acl != null) { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.ACL.getOriginalStringCode(), ""); + if (versionId != null) { + requestParams.put(Constants.ObsRequestParams.VERSION_ID, versionId); + } + + Map headers = new HashMap<>(); + headers.put(Constants.CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + String xml = this.getIConvertor(bucketName).transAccessControlList(acl, !ServiceUtils.isValid(objectKey)); + headers.put(Constants.CommonHeaders.CONTENT_LENGTH, String.valueOf(xml.length())); + headers.put(Constants.CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + transRequestPaymentHeaders(isRequesterPays, headers, this.getIHeaders(bucketName)); + NewTransResult result = new NewTransResult(); + result.setHttpMethod(HttpMethodEnum.PUT); + result.setBucketName(bucketName); + result.setObjectKey(objectKey); + result.setHeaders(headers); + result.setParams(requestParams); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + performRequest(result); + } + } +} diff --git a/app/src/main/java/com/obs/services/internal/InternalHeaderResponse.java b/app/src/main/java/com/obs/services/internal/service/InternalHeaderResponse.java similarity index 62% rename from app/src/main/java/com/obs/services/internal/InternalHeaderResponse.java rename to app/src/main/java/com/obs/services/internal/service/InternalHeaderResponse.java index 3f8dc0d..5569e27 100644 --- a/app/src/main/java/com/obs/services/internal/InternalHeaderResponse.java +++ b/app/src/main/java/com/obs/services/internal/service/InternalHeaderResponse.java @@ -11,25 +11,29 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services.internal; + +package com.obs.services.internal.service; import java.util.Map; public class InternalHeaderResponse { - protected Map responseHeaders; - - protected int statusCode; + protected Map responseHeaders; + protected Map originalHeaders; - protected void setResponseHeaders(Map responseHeaders) { - this.responseHeaders = responseHeaders; - } + protected int statusCode; + public void setResponseHeaders(Map responseHeaders) { + this.responseHeaders = responseHeaders; + } + + public void setOriginalHeaders(Map originalHeaders) { + this.originalHeaders = originalHeaders; + } protected void setStatusCode(int statusCode) { - this.statusCode = statusCode; - } - - + this.statusCode = statusCode; + } + } diff --git a/app/src/main/java/com/obs/services/internal/service/ObsBucketAdvanceService.java b/app/src/main/java/com/obs/services/internal/service/ObsBucketAdvanceService.java new file mode 100644 index 0000000..2930831 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/service/ObsBucketAdvanceService.java @@ -0,0 +1,702 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal.service; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.internal.Constants; +import com.obs.services.internal.Constants.CommonHeaders; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.handler.XmlResponsesSaxParser; +import com.obs.services.internal.io.HttpMethodReleaseInputStream; +import com.obs.services.internal.trans.NewTransResult; +import com.obs.services.internal.utils.Mimetypes; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.BaseBucketRequest; +import com.obs.services.model.BucketCors; +import com.obs.services.model.BucketCustomDomainInfo; +import com.obs.services.model.BucketDirectColdAccess; +import com.obs.services.model.BucketEncryption; +import com.obs.services.model.BucketLoggingConfiguration; +import com.obs.services.model.BucketNotificationConfiguration; +import com.obs.services.model.BucketQuota; +import com.obs.services.model.BucketTagInfo; +import com.obs.services.model.DeleteBucketCustomDomainRequest; +import com.obs.services.model.GetBucketCustomDomainRequest; +import com.obs.services.model.GrantAndPermission; +import com.obs.services.model.GroupGrantee; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.LifecycleConfiguration; +import com.obs.services.model.Permission; +import com.obs.services.model.ReplicationConfiguration; +import com.obs.services.model.RequestPaymentConfiguration; +import com.obs.services.model.SetBucketAclRequest; +import com.obs.services.model.SetBucketCorsRequest; +import com.obs.services.model.SetBucketCustomDomainRequest; +import com.obs.services.model.SetBucketDirectColdAccessRequest; +import com.obs.services.model.SetBucketEncryptionRequest; +import com.obs.services.model.SetBucketLifecycleRequest; +import com.obs.services.model.SetBucketLoggingRequest; +import com.obs.services.model.SetBucketNotificationRequest; +import com.obs.services.model.SetBucketQuotaRequest; +import com.obs.services.model.SetBucketReplicationRequest; +import com.obs.services.model.SetBucketRequestPaymentRequest; +import com.obs.services.model.SetBucketTaggingRequest; +import com.obs.services.model.SetBucketVersioningRequest; +import com.obs.services.model.SetBucketWebsiteRequest; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.WebsiteConfiguration; +import okhttp3.RequestBody; +import okhttp3.Response; + +import java.util.HashMap; +import java.util.Map; + +public abstract class ObsBucketAdvanceService extends ObsBucketBaseService { + private static final ILogger log = LoggerBuilder.getLogger(ObsBucketAdvanceService.class); + + protected HeaderResponse setBucketVersioningImpl(SetBucketVersioningRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.VERSIONING.getOriginalStringCode(), ""); + Map headers = new HashMap<>(); + + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + String xml = this.getIConvertor(request.getBucketName()).transVersioningConfiguration(request.getBucketName(), + request.getStatus() != null ? request.getStatus().getCode() : null); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return this.build(response); + } + + protected HeaderResponse setBucketRequestPaymentImpl(SetBucketRequestPaymentRequest request) + throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.REQUEST_PAYMENT.getOriginalStringCode(), ""); + Map headers = new HashMap<>(); + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + String xml = this.getIConvertor(request.getBucketName()) + .transRequestPaymentConfiguration(request.getBucketName(), + request.getPayer() != null ? request.getPayer().getCode() : null); + + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return this.build(response); + } + + protected RequestPaymentConfiguration getBucketRequestPaymentImpl(BaseBucketRequest request) + throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.REQUEST_PAYMENT.getOriginalStringCode(), ""); + + Response response = performRestGet(request.getBucketName(), null, requestParams, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(response); + + RequestPaymentConfiguration ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(response), + XmlResponsesSaxParser.RequestPaymentHandler.class, false).getRequestPaymentConfiguration(); + setHeadersAndStatus(ret, response); + return ret; + } + + protected HeaderResponse setBucketNotificationImpl(SetBucketNotificationRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.NOTIFICATION.getOriginalStringCode(), ""); + Map headers = new HashMap<>(); + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + String xml = this.getIConvertor(request.getBucketName()) + .transBucketNotificationConfiguration(request.getBucketNotificationConfiguration()); + + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + + return build(response); + } + + protected BucketNotificationConfiguration getBucketNotificationConfigurationImpl(BaseBucketRequest request) + throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.NOTIFICATION.getOriginalStringCode(), ""); + Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + BucketNotificationConfiguration result = getXmlResponseSaxParser() + .parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketNotificationConfigurationHandler.class, false) + .getBucketNotificationConfiguration(); + setHeadersAndStatus(result, httpResponse); + return result; + } + + protected HeaderResponse setBucketWebsiteConfigurationImpl(SetBucketWebsiteRequest request) + throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.WEBSITE.getOriginalStringCode(), ""); + + Map headers = new HashMap<>(); + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + String xml = this.getIConvertor(request.getBucketName()).transWebsiteConfiguration(request.getWebsiteConfig()); + + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return build(response); + } + + protected WebsiteConfiguration getBucketWebsiteConfigurationImpl(BaseBucketRequest request) + throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.WEBSITE.getOriginalStringCode(), ""); + + Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + WebsiteConfiguration ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketWebsiteConfigurationHandler.class, false).getWebsiteConfig(); + + setHeadersAndStatus(ret, httpResponse); + return ret; + } + + protected HeaderResponse deleteBucketWebsiteConfigurationImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.WEBSITE.getOriginalStringCode(), ""); + Response response = performRestDelete(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + return build(response); + } + + protected HeaderResponse setBucketLifecycleConfigurationImpl(SetBucketLifecycleRequest request) + throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.LIFECYCLE.getOriginalStringCode(), ""); + + Map headers = new HashMap<>(); + String xml = this.getIConvertor(request.getBucketName()) + .transLifecycleConfiguration(request.getLifecycleConfig()); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return build(response); + } + + protected LifecycleConfiguration getBucketLifecycleConfigurationImpl(BaseBucketRequest request) + throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.LIFECYCLE.getOriginalStringCode(), ""); + + Response response = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(response); + + LifecycleConfiguration ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(response), + XmlResponsesSaxParser.BucketLifecycleConfigurationHandler.class, false).getLifecycleConfig(); + setHeadersAndStatus(ret, response); + return ret; + } + + protected HeaderResponse deleteBucketLifecycleConfigurationImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.LIFECYCLE.getOriginalStringCode(), ""); + Response response = performRestDelete(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + return build(response); + } + + protected HeaderResponse setBucketTaggingImpl(SetBucketTaggingRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.TAGGING.getOriginalStringCode(), ""); + Map headers = new HashMap<>(); + + String xml = this.getIConvertor(request.getBucketName()).transBucketTagInfo(request.getBucketTagInfo()); + + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return build(response); + } + + protected BucketTagInfo getBucketTaggingImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.TAGGING.getOriginalStringCode(), ""); + Response httpResponse = this.performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + BucketTagInfo result = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketTagInfoHandler.class, false).getBucketTagInfo(); + setHeadersAndStatus(result, httpResponse); + return result; + } + + protected HeaderResponse deleteBucketTaggingImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.TAGGING.getOriginalStringCode(), ""); + Response response = performRestDelete(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + return build(response); + } + + protected HeaderResponse setBucketEncryptionImpl(SetBucketEncryptionRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.ENCRYPTION.getOriginalStringCode(), ""); + Map headers = new HashMap<>(); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + + String xml = request.getBucketEncryption() == null ? "" + : this.getIConvertor(request.getBucketName()).transBucketEcryption(request.getBucketEncryption()); + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return build(response); + } + + protected BucketEncryption getBucketEncryptionImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.ENCRYPTION.getOriginalStringCode(), ""); + + Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + BucketEncryption ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketEncryptionHandler.class, false).getEncryption(); + + setHeadersAndStatus(ret, httpResponse); + return ret; + } + + protected HeaderResponse deleteBucketEncryptionImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.ENCRYPTION.getOriginalStringCode(), ""); + Response response = performRestDelete(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + return build(response); + } + + protected HeaderResponse setBucketReplicationConfigurationImpl(SetBucketReplicationRequest request) + throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.REPLICATION.getOriginalStringCode(), ""); + Map headers = new HashMap<>(); + + String xml = this.getIConvertor(request.getBucketName()) + .transReplicationConfiguration(request.getReplicationConfiguration()); + + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return build(response); + } + + protected ReplicationConfiguration getBucketReplicationConfigurationImpl(BaseBucketRequest request) + throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.REPLICATION.getOriginalStringCode(), ""); + Response httpResponse = this.performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + ReplicationConfiguration result = getXmlResponseSaxParser() + .parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketReplicationConfigurationHandler.class, false) + .getReplicationConfiguration(); + + setHeadersAndStatus(result, httpResponse); + return result; + } + + protected HeaderResponse deleteBucketReplicationConfigurationImpl(BaseBucketRequest request) + throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.REPLICATION.getOriginalStringCode(), ""); + Response response = performRestDelete(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + return build(response); + } + + protected HeaderResponse setBucketCorsImpl(SetBucketCorsRequest request) throws ServiceException { + String xml = request.getBucketCors() == null ? "" + : this.getIConvertor(request.getBucketName()).transBucketCors(request.getBucketCors()); + + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.CORS.getOriginalStringCode(), ""); + + Map headers = new HashMap<>(); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + + headers.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(xml.length())); + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return build(response); + } + + protected BucketCors getBucketCorsImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.CORS.getOriginalStringCode(), ""); + Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + BucketCors ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketCorsHandler.class, false).getConfiguration(); + setHeadersAndStatus(ret, httpResponse); + return ret; + + } + + protected HeaderResponse deleteBucketCorsImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.CORS.getOriginalStringCode(), ""); + + Response response = performRestDelete(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + return build(response); + } + + protected HeaderResponse setBucketQuotaImpl(SetBucketQuotaRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.QUOTA.getOriginalStringCode(), ""); + Map headers = new HashMap<>(); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + + String xml = request.getBucketQuota() == null ? "" + : this.getIConvertor(request.getBucketName()).transBucketQuota(request.getBucketQuota()); + headers.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(xml.length())); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return build(response); + } + + protected BucketQuota getBucketQuotaImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.QUOTA.getOriginalStringCode(), ""); + + Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + BucketQuota ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketQuotaHandler.class, false).getQuota(); + setHeadersAndStatus(ret, httpResponse); + return ret; + } + + protected HeaderResponse setBucketAclImpl(SetBucketAclRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.ACL.getOriginalStringCode(), ""); + RequestBody entity = null; + if (ServiceUtils.isValid(request.getCannedACL())) { + request.setAcl(this.getIConvertor(request.getBucketName()).transCannedAcl(request.getCannedACL().trim())); + } + Map headers = new HashMap<>(); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + boolean isExtraAclPutRequired = !prepareRESTHeaderAcl(request.getBucketName(), headers, request.getAcl()); + if (isExtraAclPutRequired) { + String xml = request.getAcl() == null ? "" + : this.getIConvertor(request.getBucketName()).transAccessControlList(request.getAcl(), true); + headers.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(xml.length())); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + entity = createRequestBody(Mimetypes.MIMETYPE_XML, xml); + } + + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(entity); + Response response = performRequest(result); + return build(response); + } + + protected AccessControlList getBucketAclImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.ACL.getOriginalStringCode(), ""); + + Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + AccessControlList ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.AccessControlListHandler.class, false).getAccessControlList(); + setHeadersAndStatus(ret, httpResponse); + return ret; + } + + protected HeaderResponse setBucketLoggingConfigurationImpl(SetBucketLoggingRequest request) + throws ServiceException { + if (request.getLoggingConfiguration().isLoggingEnabled() && request.isUpdateTargetACLifRequired() + && this.getProviderCredentials().getLocalAuthType(request.getBucketName()) != AuthTypeEnum.OBS) { + boolean isSetLoggingGroupWrite = false; + boolean isSetLoggingGroupReadACP = false; + String groupIdentifier = Constants.LOG_DELIVERY_URI; + + BaseBucketRequest getBucketAclRequest = new BaseBucketRequest( + request.getLoggingConfiguration().getTargetBucketName()); + getBucketAclRequest.setRequesterPays(request.isRequesterPays()); + AccessControlList logBucketACL = getBucketAclImpl(getBucketAclRequest); + + for (GrantAndPermission gap : logBucketACL.getGrantAndPermissions()) { + if (gap.getGrantee() instanceof GroupGrantee) { + GroupGrantee grantee = (GroupGrantee) gap.getGrantee(); + if (groupIdentifier.equals(this.getIConvertor(request.getBucketName()) + .transGroupGrantee(grantee.getGroupGranteeType()))) { + if (Permission.PERMISSION_WRITE.equals(gap.getPermission())) { + isSetLoggingGroupWrite = true; + } else if (Permission.PERMISSION_READ_ACP.equals(gap.getPermission())) { + isSetLoggingGroupReadACP = true; + } + } + } + } + + if (!isSetLoggingGroupWrite || !isSetLoggingGroupReadACP) { + if (log.isWarnEnabled()) { + log.warn("Target logging bucket '" + request.getLoggingConfiguration().getTargetBucketName() + + "' does not have the necessary ACL settings, updating ACL now"); + } + if (logBucketACL.getOwner() != null) { + logBucketACL.getOwner().setDisplayName(null); + } + logBucketACL.grantPermission(GroupGrantee.LOG_DELIVERY, Permission.PERMISSION_WRITE); + logBucketACL.grantPermission(GroupGrantee.LOG_DELIVERY, Permission.PERMISSION_READ_ACP); + + SetBucketAclRequest aclReqeust = new SetBucketAclRequest(request.getBucketName(), logBucketACL); + aclReqeust.setRequesterPays(request.isRequesterPays()); + setBucketAclImpl(aclReqeust); + } else { + if (log.isDebugEnabled()) { + log.debug("Target logging bucket '" + request.getLoggingConfiguration().getTargetBucketName() + + "' has the necessary ACL settings"); + } + } + } + + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.LOGGING.getOriginalStringCode(), ""); + + Map headers = new HashMap<>(); + + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + String xml = request.getLoggingConfiguration() == null ? "" + : this.getIConvertor(request.getBucketName()) + .transBucketLoggingConfiguration(request.getLoggingConfiguration()); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return build(response); + } + + protected BucketLoggingConfiguration getBucketLoggingConfigurationImpl(BaseBucketRequest request) + throws ServiceException { + + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.LOGGING.getOriginalStringCode(), ""); + + Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + BucketLoggingConfiguration ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketLoggingHandler.class, false).getBucketLoggingStatus(); + + setHeadersAndStatus(ret, httpResponse); + return ret; + } + + protected HeaderResponse setBucketDirectColdAccessImpl(SetBucketDirectColdAccessRequest request) + throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.DIRECTCOLDACCESS.getOriginalStringCode(), ""); + Map headers = new HashMap<>(); + + String xml = this.getIConvertor(request.getBucketName()).transBucketDirectColdAccess(request.getAccess()); + + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return build(response); + } + + protected BucketDirectColdAccess getBucketDirectColdAccessImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.DIRECTCOLDACCESS.getOriginalStringCode(), ""); + Response httpResponse = this.performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + BucketDirectColdAccess result = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketDirectColdAccessHandler.class, false).getBucketDirectColdAccess(); + setHeadersAndStatus(result, httpResponse); + return result; + } + + protected HeaderResponse deleteBucketDirectColdAccessImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.DIRECTCOLDACCESS.getOriginalStringCode(), ""); + Response response = performRestDelete(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + return build(response); + } + + protected BucketCustomDomainInfo getBucketCustomDomainImpl(GetBucketCustomDomainRequest request) + throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.CUSTOMDOMAIN.getOriginalStringCode(), ""); + + Response response = performRestGet(request.getBucketName(), null, requestParams, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(response); + + BucketCustomDomainInfo ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(response), + XmlResponsesSaxParser.BucketCustomDomainHandler.class, true).getBucketTagInfo(); + setHeadersAndStatus(ret, response); + return ret; + } + + protected HeaderResponse setBucketCustomDomainImpl(SetBucketCustomDomainRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.CUSTOMDOMAIN.getOriginalStringCode(), request.getDomainName()); + Map headers = transRequestPaymentHeaders(request, null, + this.getIHeaders(request.getBucketName())); + + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + Response response = performRequest(result); + return this.build(response); + } + + protected HeaderResponse deleteBucketCustomDomainImpl(DeleteBucketCustomDomainRequest request) + throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.CUSTOMDOMAIN.getOriginalStringCode(), request.getDomainName()); + + Response response = performRestDelete(request.getBucketName(), null, requestParams, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + return this.build(response); + } +} diff --git a/app/src/main/java/com/obs/services/internal/service/ObsBucketBaseService.java b/app/src/main/java/com/obs/services/internal/service/ObsBucketBaseService.java new file mode 100644 index 0000000..a8dfbaf --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/service/ObsBucketBaseService.java @@ -0,0 +1,424 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal.service; + +import java.io.IOException; +import java.util.HashMap; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.internal.Constants; +import com.obs.services.internal.Constants.CommonHeaders; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.handler.XmlResponsesSaxParser; +import com.obs.services.internal.io.HttpMethodReleaseInputStream; +import com.obs.services.internal.trans.NewTransResult; +import com.obs.services.internal.utils.Mimetypes; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.BaseBucketRequest; +import com.obs.services.model.BucketLocationResponse; +import com.obs.services.model.BucketMetadataInfoRequest; +import com.obs.services.model.BucketMetadataInfoResult; +import com.obs.services.model.BucketPolicyResponse; +import com.obs.services.model.BucketStorageInfo; +import com.obs.services.model.BucketStoragePolicyConfiguration; +import com.obs.services.model.BucketVersioningConfiguration; +import com.obs.services.model.CreateBucketRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.ListBucketsRequest; +import com.obs.services.model.ListBucketsResult; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ListVersionsRequest; +import com.obs.services.model.ListVersionsResult; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.OptionsInfoRequest; +import com.obs.services.model.SetBucketPolicyRequest; +import com.obs.services.model.SetBucketStoragePolicyRequest; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.VersionOrDeleteMarker; +import com.obs.services.model.fs.GetBucketFSStatusResult; +import com.obs.services.model.fs.SetBucketFSStatusRequest; + +import okhttp3.Response; + +public abstract class ObsBucketBaseService extends RequestConvertor { + + private static final ILogger log = LoggerBuilder.getLogger(ObsBucketBaseService.class); + + protected ObsBucket createBucketImpl(CreateBucketRequest request) throws ServiceException { + TransResult result = this.transCreateBucketRequest(request); + String bucketName = request.getBucketName(); + AccessControlList acl = request.getAcl(); + + boolean isExtraAclPutRequired = !prepareRESTHeaderAcl(request.getBucketName(), result.getHeaders(), acl); + NewTransResult newTransResult = transRequestWithResult(result, request); + Response response = performRequest(newTransResult); + + if (isExtraAclPutRequired && acl != null) { + if (log.isDebugEnabled()) { + log.debug("Creating bucket with a non-canned ACL using REST, so an extra ACL Put is required"); + } + try { + putAclImpl(bucketName, null, acl, null, false); + } catch (Exception e) { + if (log.isWarnEnabled()) { + log.warn("Try to set bucket acl error", e); + } + } + } + + ObsBucket bucket = new ObsBucket(); + bucket.setBucketName(bucketName); + bucket.setLocation(request.getLocation()); + bucket.setAcl(acl); + bucket.setBucketStorageClass(request.getBucketStorageClass()); + setHeadersAndStatus(bucket, response); + return bucket; + } + + protected HeaderResponse deleteBucketImpl(BaseBucketRequest request) throws ServiceException { + Response response = performRestDelete(request.getBucketName(), null, null, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + return this.build(response); + } + + protected ListBucketsResult listAllBucketsImpl(ListBucketsRequest request) throws ServiceException { + Map headers = new HashMap<>(); + if (request != null && request.isQueryLocation()) { + this.putHeader(headers, this.getIHeaders("").locationHeader(), Constants.TRUE); + } + if (request != null && request.getBucketType() != null) { + this.putHeader(headers, this.getIHeaders("").bucketTypeHeader(), request.getBucketType().getCode()); + } + Response httpResponse = performRestGetForListBuckets("", null, null, headers); + + this.verifyResponseContentType(httpResponse); + + XmlResponsesSaxParser.ListBucketsHandler handler = getXmlResponseSaxParser().parse( + new HttpMethodReleaseInputStream(httpResponse), XmlResponsesSaxParser.ListBucketsHandler.class, true); + + ListBucketsResult result = new ListBucketsResult(handler.getBuckets(), handler.getOwner()); + setHeadersAndStatus(result, httpResponse); + + return result; + } + + protected boolean headBucketImpl(BaseBucketRequest request) throws ServiceException { + try { + performRestHead(request.getBucketName(), null, null, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), null, true); + return true; + } catch (ServiceException e) { + if (e.getResponseCode() == 404) { + return false; + } + throw e; + } + } + + protected GetBucketFSStatusResult getBucketMetadataImpl(BucketMetadataInfoRequest bucketMetadataInfoRequest) + throws ServiceException { + GetBucketFSStatusResult output = null; + String origin = bucketMetadataInfoRequest.getOrigin(); + List requestHeaders = bucketMetadataInfoRequest.getRequestHeaders(); + if (origin != null && requestHeaders != null && requestHeaders.size() > 0) { + for (String value : requestHeaders) { + Map headers = new HashMap<>(); + headers.put(CommonHeaders.ORIGIN, origin); + headers.put(CommonHeaders.ACCESS_CONTROL_REQUEST_HEADERS, value); + transRequestPaymentHeaders(bucketMetadataInfoRequest, headers, + this.getIHeaders(bucketMetadataInfoRequest.getBucketName())); + + Response response = performRestHead(bucketMetadataInfoRequest.getBucketName(), null, + null, headers, bucketMetadataInfoRequest.getUserHeaders(), true); + + if (output == null) { + output = this.getOptionInfoResult(bucketMetadataInfoRequest.getBucketName(), response); + } else { + String header = response.header(CommonHeaders.ACCESS_CONTROL_ALLOW_HEADERS); + if (header != null) { + if (!output.getAllowHeaders().contains(header)) { + output.getAllowHeaders().add(header); + } + } + } + response.close(); + } + } else { + Map headers = new HashMap<>(); + if (origin != null) { + headers.put(Constants.CommonHeaders.ORIGIN, origin); + } + transRequestPaymentHeaders(bucketMetadataInfoRequest, headers, + this.getIHeaders(bucketMetadataInfoRequest.getBucketName())); + + Response response = performRestHead(bucketMetadataInfoRequest.getBucketName(), null, + null, headers, bucketMetadataInfoRequest.getUserHeaders(), true); + output = this.getOptionInfoResult(bucketMetadataInfoRequest.getBucketName(), response); + response.close(); + } + + return output; + } + + protected HeaderResponse setBucketFSStatusImpl(SetBucketFSStatusRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.FILEINTERFACE.getOriginalStringCode(), ""); + + Map headers = transRequestPaymentHeaders(request, null, + this.getIHeaders(request.getBucketName())); + + String xml = this.getIConvertor(request.getBucketName()).transBucketFileInterface(request.getStatus()); + headers.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(xml.length())); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + + return build(response); + } + + protected BucketStoragePolicyConfiguration getBucketStoragePolicyImpl(BaseBucketRequest request) + throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(this.getSpecialParamForStorageClass(request.getBucketName()).getOriginalStringCode(), ""); + + Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + BucketStoragePolicyConfiguration ret = getXmlResponseSaxParser() + .parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketStoragePolicyHandler.class, false) + .getStoragePolicy(); + setHeadersAndStatus(ret, httpResponse); + return ret; + } + + protected HeaderResponse setBucketStorageImpl(SetBucketStoragePolicyRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(this.getSpecialParamForStorageClass(request.getBucketName()).getOriginalStringCode(), ""); + Map headers = new HashMap<>(); + String xml = request.getBucketStorage() == null ? "" + : this.getIConvertor(request.getBucketName()).transStoragePolicy(request.getBucketStorage()); + + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + headers.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(xml.length())); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(result); + return build(response); + } + + protected BucketStorageInfo getBucketStorageInfoImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.STORAGEINFO.getOriginalStringCode(), ""); + + Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + BucketStorageInfo ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketStorageInfoHandler.class, false).getStorageInfo(); + setHeadersAndStatus(ret, httpResponse); + return ret; + } + + protected BucketLocationResponse getBucketLocationImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.LOCATION.getOriginalStringCode(), ""); + + Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + BucketLocationResponse ret = new BucketLocationResponse( + getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.BucketLocationHandler.class, false).getLocation()); + + setHeadersAndStatus(ret, httpResponse); + return ret; + } + + protected HeaderResponse setBucketPolicyImpl(SetBucketPolicyRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.POLICY.getOriginalStringCode(), ""); + + Map headers = new HashMap<>(); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_TEXT_PLAIN); + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + NewTransResult result = transRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_TEXT_PLAIN, request.getPolicy())); + Response response = performRequest(result); + return build(response); + } + + protected BucketPolicyResponse getBucketPolicyImpl(BaseBucketRequest request) throws ServiceException { + try { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.POLICY.getOriginalStringCode(), ""); + + Response response = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + BucketPolicyResponse ret = new BucketPolicyResponse(response.body().string()); + + setHeadersAndStatus(ret, response); + return ret; + } catch (IOException e) { + throw new ServiceException(e); + } + } + + protected HeaderResponse deleteBucketPolicyImpl(BaseBucketRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.POLICY.getOriginalStringCode(), ""); + Response response = performRestDelete(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + return build(response); + } + + protected ListVersionsResult listVersionsImpl(ListVersionsRequest request) throws ServiceException { + + TransResult result = this.transListVersionsRequest(request); + + Response response = performRestGet(request.getBucketName(), null, result.getParams(), + result.getHeaders(), request.getUserHeaders()); + + this.verifyResponseContentType(response); + + XmlResponsesSaxParser.ListVersionsHandler handler = getXmlResponseSaxParser().parse( + new HttpMethodReleaseInputStream(response), XmlResponsesSaxParser.ListVersionsHandler.class, true); + List partialItems = handler.getItems(); + + ListVersionsResult listVersionsResult = new ListVersionsResult.Builder() + .bucketName(handler.getBucketName() == null ? request.getBucketName() : handler.getBucketName()) + .prefix(handler.getRequestPrefix() == null ? request.getPrefix() : handler.getRequestPrefix()) + .keyMarker(handler.getKeyMarker() == null ? request.getKeyMarker() : handler.getKeyMarker()) + .nextKeyMarker(handler.getNextKeyMarker()) + .versionIdMarker(handler.getVersionIdMarker() == null + ? request.getVersionIdMarker() : handler.getVersionIdMarker()) + .nextVersionIdMarker(handler.getNextVersionIdMarker()) + .maxKeys(String.valueOf(handler.getRequestMaxKeys())) + .isTruncated(handler.isListingTruncated()) + .versions(partialItems.toArray(new VersionOrDeleteMarker[partialItems.size()])) + .commonPrefixes(handler.getCommonPrefixes()) + .location(response.header(this.getIHeaders(request.getBucketName()).bucketRegionHeader())) + .delimiter(handler.getDelimiter() == null ? request.getDelimiter() : handler.getDelimiter()) + .builder(); + + setHeadersAndStatus(listVersionsResult, response); + return listVersionsResult; + } + + protected ObjectListing listObjectsImpl(ListObjectsRequest listObjectsRequest) throws ServiceException { + + TransResult result = this.transListObjectsRequest(listObjectsRequest); + + Response httpResponse = performRestGet(listObjectsRequest.getBucketName(), null, result.getParams(), + result.getHeaders(), listObjectsRequest.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + XmlResponsesSaxParser.ListObjectsHandler listObjectsHandler = getXmlResponseSaxParser().parse( + new HttpMethodReleaseInputStream(httpResponse), XmlResponsesSaxParser.ListObjectsHandler.class, true); + + ObjectListing objList = new ObjectListing.Builder() + .objectSummaries(listObjectsHandler.getObjects()) + .commonPrefixes(listObjectsHandler.getCommonPrefixes()) + .bucketName(listObjectsHandler.getBucketName() == null + ? listObjectsRequest.getBucketName() : listObjectsHandler.getBucketName()) + .truncated(listObjectsHandler.isListingTruncated()) + .prefix(listObjectsHandler.getRequestPrefix() == null + ? listObjectsRequest.getPrefix() : listObjectsHandler.getRequestPrefix()) + .marker(listObjectsHandler.getRequestMarker() == null + ? listObjectsRequest.getMarker() : listObjectsHandler.getRequestMarker()) + .maxKeys(listObjectsHandler.getRequestMaxKeys()) + .delimiter(listObjectsHandler.getRequestDelimiter() == null + ? listObjectsRequest.getDelimiter() : listObjectsHandler.getRequestDelimiter()) + .nextMarker(listObjectsHandler.getMarkerForNextListing()) + .location(httpResponse.header(this.getIHeaders(listObjectsRequest.getBucketName()) + .bucketRegionHeader())) + .extendCommonPrefixes(listObjectsHandler.getExtendCommonPrefixes()) + .builder(); + + setHeadersAndStatus(objList, httpResponse); + return objList; + } + + protected BucketMetadataInfoResult optionsImpl(String bucketName, String objectName, OptionsInfoRequest option) + throws ServiceException { + Map metadata = new IdentityHashMap<>(); + + if (ServiceUtils.isValid(option.getOrigin())) { + metadata.put(CommonHeaders.ORIGIN, option.getOrigin().trim()); + } + + for (int i = 0; option.getRequestMethod() != null && i < option.getRequestMethod().size(); i++) { + metadata.put(new String(new StringBuilder(CommonHeaders.ACCESS_CONTROL_REQUEST_METHOD)), + option.getRequestMethod().get(i)); + } + for (int i = 0; option.getRequestHeaders() != null && i < option.getRequestHeaders().size(); i++) { + metadata.put(new String(new StringBuilder(CommonHeaders.ACCESS_CONTROL_REQUEST_HEADERS)), + option.getRequestHeaders().get(i)); + } + transRequestPaymentHeaders(option.isRequesterPays(), metadata, this.getIHeaders(bucketName)); + + Response result = performRestOptions(bucketName, objectName, metadata, null, true); + return getOptionInfoResult(bucketName, result); + + } + + protected BucketVersioningConfiguration getBucketVersioningImpl(BaseBucketRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.VERSIONING.getOriginalStringCode(), ""); + + Response response = performRestGet(request.getBucketName(), null, requestParams, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(response); + + BucketVersioningConfiguration ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(response), + XmlResponsesSaxParser.BucketVersioningHandler.class, false).getVersioningStatus(); + setHeadersAndStatus(ret, response); + return ret; + } +} diff --git a/app/src/main/java/com/obs/services/internal/service/ObsExtensionService.java b/app/src/main/java/com/obs/services/internal/service/ObsExtensionService.java new file mode 100644 index 0000000..4ca6ae8 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/service/ObsExtensionService.java @@ -0,0 +1,216 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal.service; + +import com.obs.services.internal.Constants; +import com.obs.services.internal.Constants.CommonHeaders; +import com.obs.services.internal.Constants.ObsRequestParams; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.trans.NewTransResult; +import com.obs.services.internal.utils.JSONChange; +import com.obs.services.internal.utils.Mimetypes; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.HttpMethodEnum; +import com.obs.services.model.ReadAheadQueryResult; +import com.obs.services.model.ReadAheadRequest; +import com.obs.services.model.ReadAheadResult; +import com.oef.services.model.CreateAsynchFetchJobsResult; +import com.oef.services.model.QueryAsynchFetchJobsResult; +import com.oef.services.model.QueryExtensionPolicyResult; +import com.oef.services.model.RequestParamEnum; +import okhttp3.Response; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public abstract class ObsExtensionService extends ObsFileService { + protected HeaderResponse setExtensionPolicyImpl(String bucketName, String policyDocument) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(RequestParamEnum.EXTENSION_POLICY.getOriginalStringCode(), ""); + + Map headers = new HashMap<>(); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_JSON); + headers.put((this.getProviderCredentials().getLocalAuthType(bucketName) != AuthTypeEnum.OBS + ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) + Constants.OEF_MARKER, Constants.YES); + NewTransResult result = new NewTransResult(); + result.setParams(requestParams); + result.setHttpMethod(HttpMethodEnum.PUT); + result.setBucketName(bucketName); + result.setHeaders(headers); + result.setBody(createRequestBody(Mimetypes.MIMETYPE_JSON, policyDocument)); + return build(performRequest(result, true, false, true)); + } + + protected QueryExtensionPolicyResult queryExtensionPolicyImpl(String bucketName) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(RequestParamEnum.EXTENSION_POLICY.getOriginalStringCode(), ""); + + Map metadata = new HashMap<>(); + Response response = performRestGet(bucketName, requestParams, metadata); + + String body = readBodyFromResponse(response); + + QueryExtensionPolicyResult ret = (QueryExtensionPolicyResult) JSONChange + .jsonToObj(new QueryExtensionPolicyResult(), body); + + setHeadersAndStatus(ret, response); + return ret; + } + + private String readBodyFromResponse(Response response) { + String body; + try { + body = response.body().string(); + } catch (IOException e) { + throw new ServiceException(e); + } + return body; + } + + private Response performRestGet(String bucketName, Map requestParams, + Map metadata) { + metadata.put((this.getProviderCredentials().getLocalAuthType(bucketName) != AuthTypeEnum.OBS + ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) + Constants.OEF_MARKER, Constants.YES); + + Response response = performRestGet(bucketName, null, requestParams, metadata, null, true); + + this.verifyResponseContentTypeForJson(response); + return response; + } + + protected HeaderResponse deleteExtensionPolicyImpl(String bucketName) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(RequestParamEnum.EXTENSION_POLICY.getOriginalStringCode(), ""); + + Map metadata = new HashMap<>(); + metadata.put((this.getProviderCredentials().getLocalAuthType(bucketName) != AuthTypeEnum.OBS + ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) + Constants.OEF_MARKER, Constants.YES); + + Response response = performRestDelete(bucketName, null, requestParams, metadata, null, true, true); + return this.build(response); + } + + protected CreateAsynchFetchJobsResult createFetchJobImpl(String bucketName, String policyDocument) + throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(RequestParamEnum.ASYNC_FETCH_JOBS.getOriginalStringCode(), ""); + + Map headers = new HashMap<>(); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_JSON); + headers.put((this.getProviderCredentials().getLocalAuthType(bucketName) != AuthTypeEnum.OBS + ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) + Constants.OEF_MARKER, Constants.YES); + + NewTransResult result = new NewTransResult(); + result.setBucketName(bucketName); + result.setHttpMethod(HttpMethodEnum.POST); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(this.createRequestBody(Mimetypes.MIMETYPE_JSON, policyDocument)); + Response response = performRequest(result, true, false, true); + + this.verifyResponseContentTypeForJson(response); + + String body = readBodyFromResponse(response); + + CreateAsynchFetchJobsResult ret = (CreateAsynchFetchJobsResult) JSONChange + .jsonToObj(new CreateAsynchFetchJobsResult(), body); + + setHeadersAndStatus(ret, response); + return ret; + } + + protected QueryAsynchFetchJobsResult queryFetchJobImpl(String bucketName, String jobId) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(RequestParamEnum.ASYNC_FETCH_JOBS.getOriginalStringCode() + "/" + jobId, ""); + + Map metadata = new HashMap<>(); + metadata.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_JSON); + Response response = performRestGet(bucketName, requestParams, metadata); + + String body = readBodyFromResponse(response); + + QueryAsynchFetchJobsResult ret = (QueryAsynchFetchJobsResult) JSONChange + .jsonToObj(new QueryAsynchFetchJobsResult(), body); + + setHeadersAndStatus(ret, response); + + return ret; + } + + protected ReadAheadResult readAheadObjectsImpl(ReadAheadRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(Constants.ObsRequestParams.READAHEAD, ""); + requestParams.put(Constants.ObsRequestParams.PREFIX, request.getPrefix()); + + Map headers = new HashMap<>(); + String cacheControl = request.getCacheOption().getCode() + ", ttl=" + request.getTtl(); + headers.put(ObsRequestParams.X_CACHE_CONTROL, cacheControl); + NewTransResult transResult = transRequest(request); + transResult.setHeaders(headers); + transResult.setParams(requestParams); + + Response response = performRequest(transResult); + + this.verifyResponseContentTypeForJson(response); + + String body = readBodyFromResponse(response); + + ReadAheadResult result = (ReadAheadResult) JSONChange.jsonToObj(new ReadAheadResult(), body); + + setHeadersAndStatus(result, response); + + return result; + } + + protected ReadAheadResult deleteReadAheadObjectsImpl(String bucketName, String prefix) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(Constants.ObsRequestParams.READAHEAD, ""); + requestParameters.put(Constants.ObsRequestParams.PREFIX, prefix); + + Response response = performRestDelete(bucketName, null, requestParameters, null, false); + + this.verifyResponseContentTypeForJson(response); + + String body = readBodyFromResponse(response); + + ReadAheadResult result = (ReadAheadResult) JSONChange.jsonToObj(new ReadAheadResult(), body); + + setHeadersAndStatus(result, response); + + return result; + } + + protected ReadAheadQueryResult queryReadAheadObjectsTaskImpl(String bucketName, String taskId) + throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(Constants.ObsRequestParams.READAHEAD, ""); + requestParameters.put(Constants.ObsRequestParams.TASKID, taskId); + + Response response = performRestGet(bucketName, null, requestParameters, null, null); + + this.verifyResponseContentTypeForJson(response); + + String body = readBodyFromResponse(response); + + ReadAheadQueryResult result = (ReadAheadQueryResult) JSONChange.jsonToObj(new ReadAheadQueryResult(), body); + + setHeadersAndStatus(result, response); + + return result; + } +} diff --git a/app/src/main/java/com/obs/services/internal/service/ObsFileService.java b/app/src/main/java/com/obs/services/internal/service/ObsFileService.java new file mode 100644 index 0000000..957eda5 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/service/ObsFileService.java @@ -0,0 +1,201 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal.service; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.internal.Constants; +import com.obs.services.internal.Constants.CommonHeaders; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.handler.XmlResponsesSaxParser; +import com.obs.services.internal.io.HttpMethodReleaseInputStream; +import com.obs.services.internal.trans.NewTransResult; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.StorageClassEnum; +import com.obs.services.model.fs.ListContentSummaryRequest; +import com.obs.services.model.fs.ListContentSummaryResult; +import com.obs.services.model.fs.ObsFSFile; +import com.obs.services.model.fs.RenameRequest; +import com.obs.services.model.fs.RenameResult; +import com.obs.services.model.fs.TruncateFileRequest; +import com.obs.services.model.fs.TruncateFileResult; +import com.obs.services.model.fs.WriteFileRequest; +import com.obs.services.model.fs.ContentSummaryFsRequest; +import com.obs.services.model.fs.ContentSummaryFsResult; +import com.obs.services.model.fs.ListContentSummaryFsRequest; +import com.obs.services.model.fs.ListContentSummaryFsResult; +import okhttp3.Response; + +import java.io.Closeable; +import java.util.HashMap; +import java.util.Map; + +public abstract class ObsFileService extends ObsObjectService { + private static final ILogger log = LoggerBuilder.getLogger(ObsFileService.class); + + protected TruncateFileResult truncateFileImpl(TruncateFileRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.TRUNCATE.getOriginalStringCode(), ""); + requestParams.put(Constants.ObsRequestParams.LENGTH, String.valueOf(request.getNewLength())); + Map headers = transRequestPaymentHeaders(request, null, + this.getIHeaders(request.getBucketName())); + NewTransResult transResult = transObjectRequest(request); + transResult.setParams(requestParams); + transResult.setHeaders(headers); + Response response = performRequest(transResult); + TruncateFileResult result = new TruncateFileResult(); + setHeadersAndStatus(result, response); + return result; + } + + protected RenameResult renameFileImpl(RenameRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.RENAME.getOriginalStringCode(), ""); + requestParams.put(Constants.ObsRequestParams.NAME, request.getNewObjectKey()); + + Map headers = transRequestPaymentHeaders(request, null, + this.getIHeaders(request.getBucketName())); + + NewTransResult transResult = transObjectRequest(request); + transResult.setParams(requestParams); + transResult.setHeaders(headers); + Response response = performRequest(transResult); + + RenameResult result = new RenameResult(); + setHeadersAndStatus(result, response); + return result; + } + + protected ObsFSFile writeFileImpl(WriteFileRequest request) throws ServiceException { + TransResult result = null; + Response response; + boolean isExtraAclPutRequired; + AccessControlList acl = request.getAcl(); + try { + result = this.transWriteFileRequest(request); + + isExtraAclPutRequired = !prepareRESTHeaderAcl(request.getBucketName(), result.getHeaders(), acl); + NewTransResult newTransResult = transObjectRequestWithResult(result, request); + response = performRequest(newTransResult); + } finally { + if (result != null && result.getBody() != null && request.isAutoClose()) { + if (result.getBody() instanceof Closeable) { + ServiceUtils.closeStream((Closeable) result.getBody()); + } + } + } + + ObsFSFile ret = new ObsFSFile(request.getBucketName(), request.getObjectKey(), + response.header(CommonHeaders.ETAG), + response.header(this.getIHeaders(request.getBucketName()).versionIdHeader()), + StorageClassEnum.getValueFromCode(response.header(this.getIHeaders(request.getBucketName()) + .storageClassHeader())), + this.getObjectUrl(request.getBucketName(), request.getObjectKey(), request.getIsIgnorePort())); + + setHeadersAndStatus(ret, response); + if (isExtraAclPutRequired && acl != null) { + try { + putAclImpl(request.getBucketName(), request.getObjectKey(), acl, null, request.isRequesterPays()); + } catch (Exception e) { + if (log.isWarnEnabled()) { + log.warn("Try to set object acl error", e); + } + } + } + return ret; + } + + protected ListContentSummaryResult listContentSummaryImpl(ListContentSummaryRequest listContentSummaryRequest) + throws ServiceException { + + TransResult result = this.transListContentSummaryRequest(listContentSummaryRequest); + + Response httpResponse = performRestGet(listContentSummaryRequest.getBucketName(), null, + result.getParams(), null, listContentSummaryRequest.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + XmlResponsesSaxParser.ListContentSummaryHandler listContentSummaryHandler = getXmlResponseSaxParser().parse( + new HttpMethodReleaseInputStream(httpResponse), XmlResponsesSaxParser.ListContentSummaryHandler.class, + true); + + ListContentSummaryResult contentSummaryResult = new ListContentSummaryResult.Builder() + .folderContentSummaries(listContentSummaryHandler.getFolderContentSummaries()) + .bucketName(listContentSummaryHandler.getBucketName() == null + ? listContentSummaryRequest.getBucketName() : listContentSummaryHandler.getBucketName()) + .truncated(listContentSummaryHandler.isListingTruncated()) + .prefix(listContentSummaryHandler.getRequestPrefix() == null ? listContentSummaryRequest.getPrefix() + : listContentSummaryHandler.getRequestPrefix()) + .marker(listContentSummaryHandler.getRequestMarker() == null ? listContentSummaryRequest.getMarker() + : listContentSummaryHandler.getRequestMarker()) + .maxKeys(listContentSummaryHandler.getRequestMaxKeys()) + .delimiter(listContentSummaryHandler.getRequestDelimiter() == null + ? listContentSummaryRequest.getDelimiter() : listContentSummaryHandler.getRequestDelimiter()) + .nextMarker(listContentSummaryHandler.getMarkerForNextListing()) + .location(httpResponse + .header(this.getIHeaders(listContentSummaryRequest.getBucketName()).bucketRegionHeader())) + .builder(); + + setHeadersAndStatus(contentSummaryResult, httpResponse); + return contentSummaryResult; + } + + protected ListContentSummaryFsResult listContentSummaryFsImpl( + ListContentSummaryFsRequest listContentSummaryFsRequest) throws ServiceException { + + TransResult result = this.transListContentSummaryFsRequest(listContentSummaryFsRequest); + + Response httpResponse = performRestGet(listContentSummaryFsRequest.getBucketName(), null, + result.getParams(), result.getHeaders(), listContentSummaryFsRequest.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + XmlResponsesSaxParser.ListContentSummaryFsHandler listContentSummaryFsHandler = getXmlResponseSaxParser().parse( + new HttpMethodReleaseInputStream(httpResponse), XmlResponsesSaxParser.ListContentSummaryFsHandler.class, + true); + + ListContentSummaryFsResult listContentSummaryFsResult = new ListContentSummaryFsResult(); + listContentSummaryFsResult.setDirContentSummaries(listContentSummaryFsHandler.getDirContentSummaries()); + listContentSummaryFsResult.setErrorResults(listContentSummaryFsHandler.getErrorResults()); + + setHeadersAndStatus(listContentSummaryFsResult, httpResponse); + return listContentSummaryFsResult; + } + + protected ContentSummaryFsResult getContentSummaryFsImpl(ContentSummaryFsRequest contentSummaryFsRequest) + throws ServiceException { + + TransResult result = this.transGetContentSummaryFs(contentSummaryFsRequest); + + Response httpResponse = performRestGet(contentSummaryFsRequest.getBucketName(), + contentSummaryFsRequest.getDirName().equals("/") ? null : contentSummaryFsRequest.getDirName(), + result.getParams(), null, contentSummaryFsRequest.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + XmlResponsesSaxParser.ContentSummaryFsHandler contentSummaryFsHandler = getXmlResponseSaxParser().parse( + new HttpMethodReleaseInputStream(httpResponse), XmlResponsesSaxParser.ContentSummaryFsHandler.class, + true); + + ContentSummaryFsResult contentSummaryFsResult = new ContentSummaryFsResult(); + contentSummaryFsResult.setContentSummary(contentSummaryFsHandler.getContentSummary()); + + setHeadersAndStatus(contentSummaryFsResult, httpResponse); + return contentSummaryFsResult; + } +} diff --git a/app/src/main/java/com/obs/services/internal/service/ObsMultipartObjectService.java b/app/src/main/java/com/obs/services/internal/service/ObsMultipartObjectService.java new file mode 100644 index 0000000..b22df5e --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/service/ObsMultipartObjectService.java @@ -0,0 +1,270 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal.service; + +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +import com.obs.services.internal.Constants; +import com.obs.services.internal.Constants.CommonHeaders; +import com.obs.services.internal.Constants.ObsRequestParams; +import com.obs.services.internal.RepeatableRequestEntity; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.handler.XmlResponsesSaxParser; +import com.obs.services.internal.io.HttpMethodReleaseInputStream; +import com.obs.services.internal.trans.NewTransResult; +import com.obs.services.internal.utils.JSONChange; +import com.obs.services.internal.utils.Mimetypes; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AbortMultipartUploadRequest; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.CompleteMultipartUploadResult; +import com.obs.services.model.CopyPartRequest; +import com.obs.services.model.CopyPartResult; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ListMultipartUploadsRequest; +import com.obs.services.model.ListPartsRequest; +import com.obs.services.model.ListPartsResult; +import com.obs.services.model.MultipartUploadListing; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.StorageClassEnum; +import com.obs.services.model.UploadPartRequest; +import com.obs.services.model.UploadPartResult; + +import okhttp3.Response; + +public abstract class ObsMultipartObjectService extends ObsObjectBaseService { + protected InitiateMultipartUploadResult initiateMultipartUploadImpl(InitiateMultipartUploadRequest request) + throws ServiceException { + + TransResult result = this.transInitiateMultipartUploadRequest(request); + + this.prepareRESTHeaderAcl(request.getBucketName(), result.getHeaders(), request.getAcl()); + + NewTransResult newTransResult = transObjectRequestWithResult(result, request); + Response response = performRequest(newTransResult, true, false, false); + + this.verifyResponseContentType(response); + + InitiateMultipartUploadResult multipartUpload = getXmlResponseSaxParser() + .parse(new HttpMethodReleaseInputStream(response), + XmlResponsesSaxParser.InitiateMultipartUploadHandler.class, true) + .getInitiateMultipartUploadResult(); + setHeadersAndStatus(multipartUpload, response); + return multipartUpload; + } + + protected HeaderResponse abortMultipartUploadImpl(AbortMultipartUploadRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(ObsRequestParams.UPLOAD_ID, request.getUploadId()); + + Response response = performRestDelete(request.getBucketName(), request.getObjectKey(), requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + return build(response); + } + + protected CompleteMultipartUploadResult completeMultipartUploadImpl(CompleteMultipartUploadRequest request) + throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(ObsRequestParams.UPLOAD_ID, request.getUploadId()); + if (request.getEncodingType() != null) { + requestParams.put(ObsRequestParams.ENCODING_TYPE, request.getEncodingType()); + } + + Map headers = new HashMap<>(); + + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + String xml = this.getIConvertor(request.getBucketName()).transCompleteMultipartUpload(request.getPartEtag()); + headers.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(xml.length())); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + if (request.getCallback() != null) { + headers.put((this.getProviderCredentials().getLocalAuthType(request.getBucketName()) != AuthTypeEnum.OBS + ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) + CommonHeaders.CALLBACK, + ServiceUtils.toBase64(JSONChange.objToJson(request.getCallback()).getBytes(StandardCharsets.UTF_8))); + } + + NewTransResult transResult = transObjectRequest(request); + transResult.setParams(requestParams); + transResult.setHeaders(headers); + transResult.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + + Response response = performRequest(transResult, true, false, false); + + CompleteMultipartUploadResult ret; + if (request.getCallback() == null) { + this.verifyResponseContentType(response); + XmlResponsesSaxParser.CompleteMultipartUploadHandler handler = getXmlResponseSaxParser().parse( + new HttpMethodReleaseInputStream(response), XmlResponsesSaxParser.CompleteMultipartUploadHandler.class, + true); + + String versionId = response.header(this.getIHeaders(request.getBucketName()).versionIdHeader()); + + ret = new CompleteMultipartUploadResult(handler.getBucketName(), + handler.getObjectKey(), handler.getEtag(), handler.getLocation(), versionId, + this.getObjectUrl(handler.getBucketName(), handler.getObjectKey(), request.getIsIgnorePort())); + } else { + ret = new CompleteMultipartUploadResult(request.getBucketName(), + request.getObjectKey(), null, null, null, this.getObjectUrl(request.getBucketName(), + request.getObjectKey(), request.getIsIgnorePort())); + try { + ret.setCallbackResponseBody(Objects.requireNonNull(response.body()).byteStream()); + } catch (Exception e) { + throw new ServiceException(e); + } + } + setHeadersAndStatus(ret, response); + + return ret; + } + + protected MultipartUploadListing listMultipartUploadsImpl(ListMultipartUploadsRequest request) + throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.UPLOADS.getOriginalStringCode(), ""); + if (request.getPrefix() != null) { + requestParameters.put(ObsRequestParams.PREFIX, request.getPrefix()); + } + if (request.getDelimiter() != null) { + requestParameters.put(ObsRequestParams.DELIMITER, request.getDelimiter()); + } + if (request.getMaxUploads() != null) { + requestParameters.put(ObsRequestParams.MAX_UPLOADS, request.getMaxUploads().toString()); + } + if (request.getKeyMarker() != null) { + requestParameters.put(ObsRequestParams.KEY_MARKER, request.getKeyMarker()); + } + if (request.getUploadIdMarker() != null) { + requestParameters.put(ObsRequestParams.UPLOAD_ID_MARKER, request.getUploadIdMarker()); + } + if (request.getEncodingType() != null) { + requestParameters.put(ObsRequestParams.ENCODING_TYPE, request.getEncodingType()); + } + + Response httpResponse = performRestGet(request.getBucketName(), null, requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + XmlResponsesSaxParser.ListMultipartUploadsHandler handler = getXmlResponseSaxParser().parse( + new HttpMethodReleaseInputStream(httpResponse), XmlResponsesSaxParser.ListMultipartUploadsHandler.class, + true); + + MultipartUploadListing listResult = new MultipartUploadListing.Builder() + .bucketName(handler.getBucketName() == null + ? request.getBucketName() : handler.getBucketName()) + .keyMarker(handler.getKeyMarker() == null + ? request.getKeyMarker() : handler.getKeyMarker()) + .uploadIdMarker(handler.getUploadIdMarker() == null + ? request.getUploadIdMarker() : handler.getUploadIdMarker()) + .nextKeyMarker(handler.getNextKeyMarker()) + .nextUploadIdMarker(handler.getNextUploadIdMarker()) + .prefix(handler.getPrefix() == null + ? request.getPrefix() : handler.getPrefix()) + .maxUploads(handler.getMaxUploads()) + .truncated(handler.isTruncated()) + .multipartTaskList(handler.getMultipartUploadList()) + .delimiter(handler.getDelimiter() == null + ? request.getDelimiter() : handler.getDelimiter()) + .commonPrefixes(handler.getCommonPrefixes().toArray( + new String[handler.getCommonPrefixes().size()])) + .builder(); + setHeadersAndStatus(listResult, httpResponse); + return listResult; + } + + protected ListPartsResult listPartsImpl(ListPartsRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(ObsRequestParams.UPLOAD_ID, request.getUploadId()); + if (null != request.getMaxParts()) { + requestParameters.put(ObsRequestParams.MAX_PARTS, request.getMaxParts().toString()); + } + if (null != request.getPartNumberMarker()) { + requestParameters.put(ObsRequestParams.PART_NUMBER_MARKER, request.getPartNumberMarker().toString()); + } + if (null != request.getEncodingType()) { + requestParameters.put(ObsRequestParams.ENCODING_TYPE, request.getEncodingType()); + } + + Response httpResponse = performRestGet(request.getBucketName(), request.getObjectKey(), requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + XmlResponsesSaxParser.ListPartsHandler handler = getXmlResponseSaxParser().parse( + new HttpMethodReleaseInputStream(httpResponse), XmlResponsesSaxParser.ListPartsHandler.class, true); + + ListPartsResult result = new ListPartsResult.Builder() + .bucket(handler.getBucketName() == null ? request.getBucketName() : handler.getBucketName()) + .key(handler.getObjectKey() == null ? request.getObjectKey() : handler.getObjectKey()) + .uploadId(handler.getUploadId() == null ? request.getUploadId() : handler.getUploadId()) + .initiator(handler.getInitiator()) + .owner(handler.getOwner()) + .storageClass(StorageClassEnum.getValueFromCode(handler.getStorageClass())) + .multipartList(handler.getMultiPartList()) + .maxParts(handler.getMaxParts()) + .isTruncated(handler.isTruncated()) + .partNumberMarker(handler.getPartNumberMarker() == null + ? (request.getPartNumberMarker() == null ? null : request.getPartNumberMarker().toString()) + : handler.getPartNumberMarker()) + .nextPartNumberMarker(handler.getNextPartNumberMarker()).builder(); + + setHeadersAndStatus(result, httpResponse); + return result; + } + + protected UploadPartResult uploadPartImpl(UploadPartRequest request) throws ServiceException { + TransResult result = null; + Response response; + try { + result = this.transUploadPartRequest(request); + NewTransResult newTransResult = transObjectRequestWithResult(result, request); + response = performRequest(newTransResult); + } finally { + if (result != null && result.getBody() != null && request.isAutoClose()) { + RepeatableRequestEntity entity = (RepeatableRequestEntity) result.getBody(); + ServiceUtils.closeStream(entity); + } + } + UploadPartResult ret = new UploadPartResult(); + ret.setEtag(response.header(CommonHeaders.ETAG)); + ret.setPartNumber(request.getPartNumber()); + setHeadersAndStatus(ret, response); + return ret; + } + + protected CopyPartResult copyPartImpl(CopyPartRequest request) throws ServiceException { + + TransResult result = this.transCopyPartRequest(request); + NewTransResult newTransResult = transObjectRequestWithResult(result, request); + Response response = this.performRequest(newTransResult, true, false, false); + this.verifyResponseContentType(response); + + CopyPartResult ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(response), + XmlResponsesSaxParser.CopyPartResultHandler.class, true).getCopyPartResult(request.getPartNumber()); + + setHeadersAndStatus(ret, response); + return ret; + } +} diff --git a/app/src/main/java/com/obs/services/internal/service/ObsObjectBaseService.java b/app/src/main/java/com/obs/services/internal/service/ObsObjectBaseService.java new file mode 100644 index 0000000..3523783 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/service/ObsObjectBaseService.java @@ -0,0 +1,424 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal.service; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.internal.Constants; +import com.obs.services.internal.Constants.CommonHeaders; +import com.obs.services.internal.Constants.ObsRequestParams; +import com.obs.services.internal.ObsConstraint; +import com.obs.services.internal.ProgressManager; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.SimpleProgressManager; +import com.obs.services.internal.handler.XmlResponsesSaxParser; +import com.obs.services.internal.io.HttpMethodReleaseInputStream; +import com.obs.services.internal.io.ProgressInputStream; +import com.obs.services.internal.trans.NewTransResult; +import com.obs.services.internal.utils.Mimetypes; +import com.obs.services.internal.utils.RestUtils; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.CopyObjectRequest; +import com.obs.services.model.CopyObjectResult; +import com.obs.services.model.DeleteObjectRequest; +import com.obs.services.model.DeleteObjectResult; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.DeleteObjectsResult; +import com.obs.services.model.GetObjectAclRequest; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.GetObjectRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.ObsObject; +import com.obs.services.model.PutObjectRequest; +import com.obs.services.model.SetObjectAclRequest; +import com.obs.services.model.SetObjectMetadataRequest; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.StorageClassEnum; +import com.obs.services.model.fs.DropFileResult; +import com.obs.services.model.fs.ObsFSAttribute; +import com.obs.services.model.fs.ObsFSFile; +import com.obs.services.model.fs.ReadFileResult; +import okhttp3.RequestBody; +import okhttp3.Response; + +import java.io.BufferedInputStream; +import java.io.Closeable; +import java.io.InputStream; +import java.text.ParseException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +public abstract class ObsObjectBaseService extends ObsBucketAdvanceService { + private static final ILogger log = LoggerBuilder.getLogger(ObsObjectBaseService.class); + + protected boolean doesObjectExistImpl(GetObjectMetadataRequest request) throws ServiceException { + Map headers = new HashMap<>(); + this.transSseCHeaders(request.getSseCHeader(), headers, this.getIHeaders(request.getBucketName())); + this.transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + Map params = new HashMap<>(); + if (request.getVersionId() != null) { + params.put(ObsRequestParams.VERSION_ID, request.getVersionId()); + } + boolean doesObjectExist = false; + try { + Response response = performRestHead(request.getBucketName(), request.getObjectKey(), params, headers, + new HashMap<>(), request.isEncodeHeaders()); + if (200 == response.code()) { + doesObjectExist = true; + } + } catch (ServiceException ex) { + if (!(404 == ex.getResponseCode())) { + throw ex; + } + } + return doesObjectExist; + } + + protected ObsFSFile putObjectImpl(PutObjectRequest request) throws ServiceException { + + TransResult result = null; + Response response; + boolean isExtraAclPutRequired; + AccessControlList acl = request.getAcl(); + NewTransResult newTransResult; + try { + result = this.transPutObjectRequest(request); + isExtraAclPutRequired = !prepareRESTHeaderAcl(request.getBucketName(), result.getHeaders(), acl); + // todo prepareRESTHeaderAcl 也会操作头域,下次重构可以将其合并 + newTransResult = transObjectRequestWithResult(result, request); + response = performRequest(newTransResult); + } finally { + if (result != null && result.getBody() != null && request.isAutoClose()) { + if (result.getBody() instanceof Closeable) { + ServiceUtils.closeStream((Closeable) result.getBody()); + } + } + } + + ObsFSFile ret = new ObsFSFile(request.getBucketName(), request.getObjectKey(), + response.header(CommonHeaders.ETAG), + response.header(this.getIHeaders(request.getBucketName()).versionIdHeader()), + StorageClassEnum.getValueFromCode(response.header(this.getIHeaders(request.getBucketName()) + .storageClassHeader())), this.getObjectUrl(request.getBucketName(), request.getObjectKey(), + request.getIsIgnorePort())); + + setHeadersAndStatus(ret, response); + if (isExtraAclPutRequired && acl != null) { + try { + putAclImpl(request.getBucketName(), request.getObjectKey(), acl, null, request.isRequesterPays()); + } catch (Exception e) { + if (log.isWarnEnabled()) { + log.warn("Try to set object acl error", e); + } + } + } + return ret; + } + + protected ObsObject getObjectImpl(GetObjectRequest request) throws ServiceException { + return (ObsObject) this.getObjectImpl((GetObjectMetadataRequest) request); + } + + protected Object getObjectImpl(GetObjectMetadataRequest request) throws ServiceException { + Response response; + GetObjectRequest getRequest = null; + if (!(request instanceof GetObjectRequest)) { + Map headers = new HashMap<>(); + this.transSseCHeaders(request.getSseCHeader(), headers, this.getIHeaders(request.getBucketName())); + this.transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + Map params = new HashMap<>(); + if (request.getVersionId() != null) { + params.put(ObsRequestParams.VERSION_ID, request.getVersionId()); + } + response = performRestHead(request.getBucketName(), request.getObjectKey(), + params, headers, request.getUserHeaders(), request.isEncodeHeaders()); + } else { + getRequest = (GetObjectRequest) request; + TransResult result = this.transGetObjectRequest(getRequest); + if (getRequest.getRequestParameters() != null) { + result.getParams().putAll(getRequest.getRequestParameters()); + } + response = performRestGet(request.getBucketName(), request.getObjectKey(), result.getParams(), + result.getHeaders(), request.getUserHeaders(), false, request.isEncodeHeaders()); + } + + ObsFSAttribute objMetadata = this.getObsFSAttributeFromResponse(request.getBucketName(), + response, request.isEncodeHeaders()); + + if (!(request instanceof GetObjectRequest)) { + response.close(); + return objMetadata; + } + ReadFileResult obsObject = new ReadFileResult(); + obsObject.setObjectKey(request.getObjectKey()); + obsObject.setBucketName(request.getBucketName()); + obsObject.setMetadata(objMetadata); + // pmd error message: CloseResource - Ensure that resources like this + // InputStream object are closed after use + // 该接口是下载对象,需要将流返回给客户(调用方),我们不能关闭这个流 + InputStream input = response.body().byteStream(); // NOPMD + if (getRequest.getProgressListener() != null) { + ProgressManager progressManager = new SimpleProgressManager(objMetadata.getContentLength(), 0, + getRequest.getProgressListener(), + getRequest.getProgressInterval() > 0 + ? getRequest.getProgressInterval() : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); + input = new ProgressInputStream(input, progressManager); + } + + int readBufferSize = obsProperties.getIntProperty(ObsConstraint.READ_BUFFER_SIZE, + ObsConstraint.DEFAULT_READ_BUFFER_STREAM); + if (readBufferSize > 0) { + input = new BufferedInputStream(input, readBufferSize); + } + + obsObject.setObjectContent(input); + return obsObject; + } + + protected DeleteObjectsResult deleteObjectsImpl(DeleteObjectsRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.DELETE.getOriginalStringCode(), ""); + + String xml = this.getIConvertor(request.getBucketName()).transKeyAndVersion(request.getKeyAndVersions(), + request.isQuiet(), request.getEncodingType()); + + Map headers = new HashMap<>(); + headers.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(xml.length())); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + NewTransResult transResult = transRequest(request); + transResult.setParams(requestParams); + transResult.setHeaders(headers); + transResult.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = performRequest(transResult, true, false, false); + this.verifyResponseContentType(response); + + DeleteObjectsResult ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(response), + XmlResponsesSaxParser.DeleteObjectsHandler.class, true).getMultipleDeleteResult(); + + setHeadersAndStatus(ret, response); + return ret; + } + + protected DeleteObjectResult deleteObjectImpl(DeleteObjectRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + if (request.getVersionId() != null) { + requestParameters.put(ObsRequestParams.VERSION_ID, request.getVersionId()); + } + + Response response = performRestDelete(request.getBucketName(), request.getObjectKey(), requestParameters, + transRequestPaymentHeaders(request, null, this.getIHeaders(request.getBucketName())), + request.getUserHeaders()); + + DropFileResult result = new DropFileResult(Boolean.valueOf(response.header(this.getIHeaders( + request.getBucketName()).deleteMarkerHeader())), request.getObjectKey(), + response.header(this.getIHeaders(request.getBucketName()).versionIdHeader())); + setHeadersAndStatus(result, response); + return result; + } + + protected CopyObjectResult copyObjectImpl(CopyObjectRequest request) throws ServiceException { + + TransResult result = this.transCopyObjectRequest(request); + + AccessControlList acl = request.getAcl(); + // todo prepareRESTHeaderAcl 也会操作头域,下次重构可以将其合并 + boolean isExtraAclPutRequired = !prepareRESTHeaderAcl(request.getBucketName(), result.getHeaders(), acl); + NewTransResult newTransResult = transObjectRequestWithResult(result, request); + + Response response = performRequest(newTransResult, true, false, false); + + this.verifyResponseContentType(response); + + XmlResponsesSaxParser.CopyObjectResultHandler handler = getXmlResponseSaxParser().parse( + new HttpMethodReleaseInputStream(response), XmlResponsesSaxParser.CopyObjectResultHandler.class, false); + CopyObjectResult copyRet = new CopyObjectResult(handler.getETag(), handler.getLastModified(), + response.header(this.getIHeaders(request.getBucketName()).versionIdHeader()), + response.header(this.getIHeaders(request.getBucketName()).copySourceVersionIdHeader()), + StorageClassEnum.getValueFromCode(response.header( + this.getIHeaders(request.getBucketName()).storageClassHeader()))); + + setHeadersAndStatus(copyRet, response); + if (isExtraAclPutRequired && acl != null) { + if (log.isDebugEnabled()) { + log.debug("Creating object with a non-canned ACL using REST, so an extra ACL Put is required"); + } + try { + putAclImpl(request.getDestinationBucketName(), request.getDestinationObjectKey(), acl, null, + request.isRequesterPays()); + } catch (Exception e) { + if (log.isWarnEnabled()) { + log.warn("Try to set object acl error", e); + } + } + } + + return copyRet; + } + + protected ObjectMetadata setObjectMetadataImpl(SetObjectMetadataRequest request) { + TransResult result = this.transSetObjectMetadataRequest(request); + NewTransResult newTransResult = transObjectRequestWithResult(result, request); + Response response = performRequest(newTransResult); + return this.getObsFSAttributeFromResponse(request.getBucketName(), response, request.isEncodeHeaders()); + } + + protected ObsFSAttribute getObjectMetadataImpl(GetObjectMetadataRequest request) throws ServiceException { + return (ObsFSAttribute) this.getObjectImpl(request); + } + + protected HeaderResponse setObjectAclImpl(SetObjectAclRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.ACL.getOriginalStringCode(), ""); + if (request.getVersionId() != null) { + requestParams.put(ObsRequestParams.VERSION_ID, request.getVersionId()); + } + RequestBody entity = null; + if (ServiceUtils.isValid(request.getCannedACL())) { + request.setAcl(this.getIConvertor(request.getBucketName()).transCannedAcl(request.getCannedACL().trim())); + } + Map headers = new HashMap<>(); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + boolean isExtraAclPutRequired = !prepareRESTHeaderAclObject(request.getBucketName(), headers, request.getAcl()); + if (isExtraAclPutRequired) { + String xml = request.getAcl() == null ? "" + : this.getIConvertor(request.getBucketName()).transAccessControlList(request.getAcl(), false); + headers.put(CommonHeaders.CONTENT_LENGTH, String.valueOf(xml.length())); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + entity = createRequestBody(Mimetypes.MIMETYPE_XML, xml); + } + + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + NewTransResult result = transObjectRequest(request); + result.setParams(requestParams); + result.setHeaders(headers); + result.setBody(entity); + Response response = performRequest(result); + return build(response); + } + + protected AccessControlList getObjectAclImpl(GetObjectAclRequest getObjectAclRequest) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.ACL.getOriginalStringCode(), ""); + if (ServiceUtils.isValid(getObjectAclRequest.getVersionId())) { + requestParameters.put(ObsRequestParams.VERSION_ID, getObjectAclRequest.getVersionId().trim()); + } + + Response httpResponse = performRestGet(getObjectAclRequest.getBucketName(), getObjectAclRequest.getObjectKey(), + requestParameters, transRequestPaymentHeaders(getObjectAclRequest, null, + this.getIHeaders(getObjectAclRequest.getBucketName())), getObjectAclRequest.getUserHeaders()); + + this.verifyResponseContentType(httpResponse); + + AccessControlList ret = getXmlResponseSaxParser().parse(new HttpMethodReleaseInputStream(httpResponse), + XmlResponsesSaxParser.AccessControlListHandler.class, false).getAccessControlList(); + setHeadersAndStatus(ret, httpResponse); + return ret; + } + + protected String getObjectUrl(String bucketName, String objectKey, boolean isIgnorePort) { + boolean pathStyle = this.isPathStyle(); + boolean https = this.getHttpsOnly(); + boolean isCname = this.isCname(); + String port = ""; + if (!isIgnorePort) { + port = ":" + (https ? this.getHttpsPort() : this.getHttpPort()); + } + return (https ? "https://" : "http://") + (pathStyle || isCname ? "" : bucketName + ".") + + this.getEndpoint() + port + "/" + (pathStyle ? bucketName + "/" : "") + + RestUtils.uriEncode(objectKey, false); + } + + private ObsFSAttribute getObsFSAttributeFromResponse(String bucketName, Response response, boolean needDecode) { + Date lastModifiedDate = null; + String lastModified = response.header(CommonHeaders.LAST_MODIFIED); + if (lastModified != null) { + try { + lastModifiedDate = ServiceUtils.parseRfc822Date(lastModified); + } catch (ParseException e) { + if (log.isWarnEnabled()) { + log.warn("Response last-modified is not well-format", e); + } + } + } + ObsFSAttribute objMetadata = new ObsFSAttribute(); + objMetadata.setLastModified(lastModifiedDate); + objMetadata.setContentEncoding(response.header(CommonHeaders.CONTENT_ENCODING)); + objMetadata.setContentType(response.header(CommonHeaders.CONTENT_TYPE)); + + objMetadata.setContentDisposition(response.header(CommonHeaders.CONTENT_DISPOSITION)); + objMetadata.setContentLanguage(response.header(CommonHeaders.CONTENT_LANGUAGE)); + objMetadata.setCacheControl(response.header(CommonHeaders.CACHE_CONTROL)); + objMetadata.setExpires(response.header(CommonHeaders.EXPIRES)); + objMetadata.setCrc64(response.header(CommonHeaders.HASH_CRC64ECMA)); + + String contentLength = response.header(CommonHeaders.CONTENT_LENGTH); + if (contentLength != null) { + try { + objMetadata.setContentLength(Long.parseLong(contentLength)); + } catch (NumberFormatException e) { + if (log.isWarnEnabled()) { + log.warn("Response content-length is not well-format", e); + } + } + } + String fsMode; + if ((fsMode = response.header(this.getIHeaders(bucketName).fsModeHeader())) != null) { + objMetadata.setMode(Integer.parseInt(fsMode)); + } + objMetadata.setWebSiteRedirectLocation(response.header(this.getIHeaders(bucketName) + .websiteRedirectLocationHeader())); + objMetadata.setObjectStorageClass( + StorageClassEnum.getValueFromCode(response.header(this.getIHeaders(bucketName).storageClassHeader()))); + + String etag = response.header(CommonHeaders.ETAG); + objMetadata.setEtag(etag); + if (etag != null && !etag.contains("-")) { + String md5 = etag; + if (md5.startsWith("\"")) { + md5 = md5.substring(1); + } + if (md5.endsWith("\"")) { + md5 = md5.substring(0, md5.length() - 1); + } + try { + objMetadata.setContentMd5(ServiceUtils.toBase64(ServiceUtils.fromHex(md5))); + } catch (Exception e) { + if (log.isDebugEnabled()) { + log.debug(e.getMessage(), e); + } + } + } + + objMetadata.setAppendable("Appendable".equals(response.header(this.getIHeaders(bucketName) + .objectTypeHeader()))); + String nextPosition = response.header(this.getIHeaders(bucketName).nextPositionHeader(), "-1"); + objMetadata.setNextPosition(Long.parseLong(nextPosition)); + if (objMetadata.getNextPosition() == -1L) { + objMetadata.setNextPosition(Long.parseLong(response.header(Constants.CommonHeaders.CONTENT_LENGTH, "-1"))); + } + setHeadersAndStatus(objMetadata, response, needDecode); + objMetadata.setUserMetadata(ServiceUtils.cleanUserMetadata(objMetadata.getOriginalHeaders(), needDecode)); + return objMetadata; + } +} diff --git a/app/src/main/java/com/obs/services/internal/service/ObsObjectService.java b/app/src/main/java/com/obs/services/internal/service/ObsObjectService.java new file mode 100644 index 0000000..f7201a6 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/service/ObsObjectService.java @@ -0,0 +1,188 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal.service; + +import java.io.Closeable; +import java.util.HashMap; +import java.util.Map; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.internal.Constants; +import com.obs.services.internal.Constants.CommonHeaders; +import com.obs.services.internal.Constants.ObsRequestParams; +import com.obs.services.internal.RepeatableRequestEntity; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.trans.NewTransResult; +import com.obs.services.internal.utils.Mimetypes; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.AppendObjectRequest; +import com.obs.services.model.AppendObjectResult; +import com.obs.services.model.ModifyObjectRequest; +import com.obs.services.model.ModifyObjectResult; +import com.obs.services.model.RenameObjectRequest; +import com.obs.services.model.RenameObjectResult; +import com.obs.services.model.RestoreObjectRequest; +import com.obs.services.model.RestoreObjectRequest.RestoreObjectStatus; +import com.obs.services.model.RestoreObjectResult; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.StorageClassEnum; +import com.obs.services.model.TruncateObjectRequest; +import com.obs.services.model.TruncateObjectResult; + +import okhttp3.Response; + +public abstract class ObsObjectService extends ObsMultipartObjectService { + private static final ILogger log = LoggerBuilder.getLogger(ObsObjectService.class); + + protected TruncateObjectResult truncateObjectImpl(TruncateObjectRequest request) throws ServiceException { + Map requestParameters = new HashMap<>(); + requestParameters.put(SpecialParamEnum.TRUNCATE.getOriginalStringCode(), ""); + requestParameters.put(Constants.ObsRequestParams.LENGTH, String.valueOf(request.getNewLength())); + Map headers = transRequestPaymentHeaders(request, null, + this.getIHeaders(request.getBucketName())); + NewTransResult transResult = transObjectRequest(request); + transResult.setHeaders(headers); + transResult.setParams(requestParameters); + Response response = performRequest(transResult); + TruncateObjectResult result = new TruncateObjectResult(); + setHeadersAndStatus(result, response); + return result; + } + + protected RenameObjectResult renameObjectImpl(RenameObjectRequest request) throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.RENAME.getOriginalStringCode(), ""); + requestParams.put(Constants.ObsRequestParams.NAME, request.getNewObjectKey()); + + Map headers = transRequestPaymentHeaders(request, null, + this.getIHeaders(request.getBucketName())); + + NewTransResult transResult = transObjectRequest(request); + transResult.setParams(requestParams); + transResult.setHeaders(headers); + Response response = performRequest(transResult); + RenameObjectResult result = new RenameObjectResult(); + setHeadersAndStatus(result, response); + return result; + } + + protected RestoreObjectStatus restoreObjectImpl(RestoreObjectRequest restoreObjectRequest) throws ServiceException { + RestoreObjectResult restoreObjectResult = restoreObjectV2Impl(restoreObjectRequest); + return transRestoreObjectResultToRestoreObjectStatus(restoreObjectResult); + } + + protected RestoreObjectResult restoreObjectV2Impl(RestoreObjectRequest request) + throws ServiceException { + Map requestParams = new HashMap<>(); + requestParams.put(SpecialParamEnum.RESTORE.getOriginalStringCode(), ""); + if (request.getVersionId() != null) { + requestParams.put(ObsRequestParams.VERSION_ID, request.getVersionId()); + } + + Map headers = new HashMap<>(); + String xml = this.getIConvertor(request.getBucketName()).transRestoreObjectRequest(request); + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.computeMD5(xml)); + headers.put(CommonHeaders.CONTENT_TYPE, Mimetypes.MIMETYPE_XML); + transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + NewTransResult transResult = transObjectRequest(request); + transResult.setParams(requestParams); + transResult.setHeaders(headers); + transResult.setBody(createRequestBody(Mimetypes.MIMETYPE_XML, xml)); + Response response = this.performRequest(transResult); + + RestoreObjectResult ret = new RestoreObjectResult(request.getBucketName(), + request.getObjectKey(), request.getVersionId()); + + setHeadersAndStatus(ret, response); + return ret; + } + + protected AppendObjectResult appendObjectImpl(AppendObjectRequest request) throws ServiceException { + TransResult result = null; + Response response; + boolean isExtraAclPutRequired; + AccessControlList acl = request.getAcl(); + try { + result = this.transAppendObjectRequest(request); + + isExtraAclPutRequired = !prepareRESTHeaderAcl(request.getBucketName(), result.getHeaders(), acl); + + NewTransResult newTransResult = transObjectRequestWithResult(result, request); + response = performRequest(newTransResult); + } finally { + if (result != null && result.getBody() != null && request.isAutoClose()) { + RepeatableRequestEntity entity = (RepeatableRequestEntity) result.getBody(); + ServiceUtils.closeStream(entity); + } + } + String nextPosition = response.header(this.getIHeaders(request.getBucketName()).nextPositionHeader()); + AppendObjectResult ret = new AppendObjectResult(request.getBucketName(), request.getObjectKey(), + response.header(CommonHeaders.ETAG), nextPosition != null ? Long.parseLong(nextPosition) : -1, + StorageClassEnum.getValueFromCode(response.header(this.getIHeaders(request.getBucketName()) + .storageClassHeader())), + this.getObjectUrl(request.getBucketName(), request.getObjectKey(), request.getIsIgnorePort())); + + setHeadersAndStatus(ret, response); + if (isExtraAclPutRequired && acl != null) { + try { + putAclImpl(request.getBucketName(), request.getObjectKey(), acl, null, request.isRequesterPays()); + } catch (Exception e) { + if (log.isWarnEnabled()) { + log.warn("Try to set object acl error", e); + } + } + } + return ret; + } + + protected ModifyObjectResult modifyObjectImpl(ModifyObjectRequest request) throws ServiceException { + + TransResult result = null; + Response response; + boolean isExtraAclPutRequired; + AccessControlList acl = request.getAcl(); + try { + result = this.transModifyObjectRequest(request); + + isExtraAclPutRequired = !prepareRESTHeaderAcl(request.getBucketName(), result.getHeaders(), acl); + // todo prepareRESTHeaderAcl 也会操作头域,下次重构可以将其合并 + NewTransResult newTransResult = transObjectRequestWithResult(result, request); + response = performRequest(newTransResult); + } finally { + if (result != null && result.getBody() != null && request.isAutoClose()) { + if (result.getBody() instanceof Closeable) { + ServiceUtils.closeStream((Closeable) result.getBody()); + } + } + } + ModifyObjectResult ret = new ModifyObjectResult(); + + setHeadersAndStatus(ret, response, request.isEncodeHeaders()); + if (isExtraAclPutRequired && acl != null) { + try { + putAclImpl(request.getBucketName(), request.getObjectKey(), acl, null, request.isRequesterPays()); + } catch (Exception e) { + if (log.isWarnEnabled()) { + log.warn("Try to set object acl error", e); + } + } + } + return ret; + } +} diff --git a/app/src/main/java/com/obs/services/internal/service/RequestConvertor.java b/app/src/main/java/com/obs/services/internal/service/RequestConvertor.java new file mode 100644 index 0000000..0d62086 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/service/RequestConvertor.java @@ -0,0 +1,952 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.internal.service; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.internal.Constants; +import com.obs.services.internal.Constants.CommonHeaders; +import com.obs.services.internal.Constants.ObsRequestParams; +import com.obs.services.internal.IConvertor; +import com.obs.services.internal.IHeaders; +import com.obs.services.internal.ObsConstraint; +import com.obs.services.internal.ProgressManager; +import com.obs.services.internal.RepeatableRequestEntity; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.SimpleProgressManager; +import com.obs.services.internal.io.ProgressInputStream; +import com.obs.services.internal.utils.Mimetypes; +import com.obs.services.internal.utils.RestUtils; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.internal.xml.OBSXMLBuilder; +import com.obs.services.model.AppendObjectRequest; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.BucketTypeEnum; +import com.obs.services.model.CopyObjectRequest; +import com.obs.services.model.CopyPartRequest; +import com.obs.services.model.CreateBucketRequest; +import com.obs.services.model.ExtensionBucketPermissionEnum; +import com.obs.services.model.ExtensionObjectPermissionEnum; +import com.obs.services.model.GetObjectRequest; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ListVersionsRequest; +import com.obs.services.model.ModifyObjectRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.PutObjectBasicRequest; +import com.obs.services.model.PutObjectRequest; +import com.obs.services.model.RestoreObjectRequest.RestoreObjectStatus; +import com.obs.services.model.RestoreObjectResult; +import com.obs.services.model.SetObjectMetadataRequest; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.SseCHeader; +import com.obs.services.model.SseKmsHeader; +import com.obs.services.model.UploadPartRequest; +import com.obs.services.model.fs.ContentSummaryFsRequest; +import com.obs.services.model.fs.ListContentSummaryFsRequest; +import com.obs.services.model.fs.ListContentSummaryRequest; +import com.obs.services.model.fs.NewBucketRequest; +import com.obs.services.model.fs.WriteFileRequest; +import okhttp3.RequestBody; + +import javax.xml.parsers.FactoryConfigurationError; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +public abstract class RequestConvertor extends AclHeaderConvertor { + private static final ILogger log = LoggerBuilder.getLogger("com.obs.services.ObsClient"); + + protected TransResult transListVersionsRequest(ListVersionsRequest request) { + Map params = new HashMap(); + params.put(SpecialParamEnum.VERSIONS.getOriginalStringCode(), ""); + if (request.getPrefix() != null) { + params.put(ObsRequestParams.PREFIX, request.getPrefix()); + } + if (request.getDelimiter() != null) { + params.put(ObsRequestParams.DELIMITER, request.getDelimiter()); + } + if (request.getMaxKeys() > 0) { + params.put(ObsRequestParams.MAX_KEYS, String.valueOf(request.getMaxKeys())); + } + if (request.getKeyMarker() != null) { + params.put(ObsRequestParams.KEY_MARKER, request.getKeyMarker()); + } + if (request.getVersionIdMarker() != null) { + params.put(ObsRequestParams.VERSION_ID_MARKER, request.getVersionIdMarker()); + } + if (request.getEncodingType() != null) { + params.put(ObsRequestParams.ENCODING_TYPE, request.getEncodingType()); + } + Map headers = new HashMap(); + if (request.getListTimeout() > 0) { + putHeader(headers, this.getIHeaders(request.getBucketName()).listTimeoutHeader(), + String.valueOf(request.getListTimeout())); + } + + this.transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + + return new TransResult(headers, params, null); + } + + protected TransResult transInitiateMultipartUploadRequest(InitiateMultipartUploadRequest request) + throws ServiceException { + Map headers = new HashMap(); + IHeaders iheaders = this.getIHeaders(request.getBucketName()); + IConvertor iconvertor = this.getIConvertor(request.getBucketName()); + + ObjectMetadata objectMetadata = request.getMetadata() == null ? new ObjectMetadata() : request.getMetadata(); + + selectAllowedHeader(headers, objectMetadata); + + if (objectMetadata.getObjectStorageClass() != null) { + putHeader(headers, iheaders.storageClassHeader(), + iconvertor.transStorageClass(objectMetadata.getObjectStorageClass())); + } + + if (ServiceUtils.isValid(objectMetadata.getWebSiteRedirectLocation())) { + putHeader(headers, iheaders.websiteRedirectLocationHeader(), objectMetadata.getWebSiteRedirectLocation()); + } + + if (ServiceUtils.isValid(request.getSuccessRedirectLocation())) { + putHeader(headers, iheaders.successRedirectLocationHeader(), request.getSuccessRedirectLocation()); + } + + if (request.getExpires() > 0) { + putHeader(headers, iheaders.expiresHeader(), String.valueOf(request.getExpires())); + } + + setBaseHeaderFromMetadata(request.getBucketName(), headers, objectMetadata); + + transRequestPaymentHeaders(request, headers, iheaders); + + transExtensionPermissions(request, headers); + + transSseHeaders(request, headers, iheaders); + + Object contentType = objectMetadata.getContentType() == null + ? objectMetadata.getValue(CommonHeaders.CONTENT_TYPE) : objectMetadata.getContentType(); + if (contentType == null) { + contentType = Mimetypes.getInstance().getMimetype(request.getObjectKey()); + } + + String contentTypeStr = contentType.toString().trim(); + headers.put(CommonHeaders.CONTENT_TYPE, contentTypeStr); + + Map params = new HashMap(); + params.put(SpecialParamEnum.UPLOADS.getOriginalStringCode(), ""); + + if (request.getEncodingType() != null) { + params.put(ObsRequestParams.ENCODING_TYPE, request.getEncodingType()); + } + + return new TransResult(headers, params, null); + } + + private void selectAllowedHeader(Map headers, ObjectMetadata objectMetadata) { + for (Map.Entry entry : objectMetadata.getAllMetadata().entrySet()) { + String key = entry.getKey(); + if (!ServiceUtils.isValid(key)) { + continue; + } + key = key.trim(); + if ((CAN_USE_STANDARD_HTTP_HEADERS.get() == null || (CAN_USE_STANDARD_HTTP_HEADERS.get() != null + && !CAN_USE_STANDARD_HTTP_HEADERS.get())) + && Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(key.toLowerCase())) { + continue; + } + headers.put(key, entry.getValue() == null ? "" : entry.getValue().toString()); + } + } + + protected void transExtensionPermissions(PutObjectBasicRequest request, Map headers) { + Set extensionPermissionEnums = request.getAllGrantPermissions(); + if (!extensionPermissionEnums.isEmpty()) { + for (ExtensionObjectPermissionEnum extensionPermissionEnum : extensionPermissionEnums) { + Set domainIds = request.getDomainIdsByGrantPermission(extensionPermissionEnum); + List domainIdList = new ArrayList(domainIds.size()); + for (String domainId : domainIds) { + domainIdList.add("id=" + domainId); + } + putHeader(headers, getHeaderByMethodName(request.getBucketName(), extensionPermissionEnum.getCode()), + ServiceUtils.join(domainIdList, ",")); + } + } + } + + protected void transSseHeaders(PutObjectBasicRequest request, Map headers, IHeaders iheaders) + throws ServiceException { + if (null != request.getSseCHeader()) { + this.transSseCHeaders(request.getSseCHeader(), headers, iheaders); + } else if (null != request.getSseKmsHeader()) { + this.transSseKmsHeaders(request.getSseKmsHeader(), headers, iheaders, request.getBucketName()); + } + } + + protected void transSseCHeaders(SseCHeader ssecHeader, Map headers, IHeaders iheaders) + throws ServiceException { + if (ssecHeader == null) { + return; + } + + String sseCAlgorithm = ssecHeader.getSSEAlgorithm().getCode(); + + putHeader(headers, iheaders.sseCHeader(), ServiceUtils.toValid(sseCAlgorithm)); + if (ssecHeader.getSseCKeyBase64() != null) { + try { + putHeader(headers, iheaders.sseCKeyHeader(), ssecHeader.getSseCKeyBase64()); + putHeader(headers, iheaders.sseCKeyMd5Header(), ServiceUtils + .toBase64(ServiceUtils.computeMD5Hash(ServiceUtils.fromBase64(ssecHeader.getSseCKeyBase64())))); + } catch (IOException e) { + throw new IllegalStateException("fail to read sseCkey", e); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("fail to read sseCkey", e); + } + } else if (null != ssecHeader.getSseCKey() + && ssecHeader.getSseCKey().length > 0) { + try { + byte[] data = ssecHeader.getSseCKey(); + putHeader(headers, iheaders.sseCKeyHeader(), ServiceUtils.toBase64(data)); + putHeader(headers, iheaders.sseCKeyMd5Header(), + ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(data))); + } catch (IOException e) { + throw new IllegalStateException("fail to read sseCkey", e); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("fail to read sseCkey", e); + } + } + } + + protected TransResult transCreateBucketRequest(CreateBucketRequest request) throws ServiceException { + Map headers = new HashMap(); + IConvertor convertor = this.getIConvertor(request.getBucketName()); + + if (request.getBucketStorageClass() != null) { + putHeader(headers, getIHeaders(request.getBucketName()).defaultStorageClassHeader(), + convertor.transStorageClass(request.getBucketStorageClass())); + } + + if (request.getEpid() != null) { + putHeader(headers, getIHeaders(request.getBucketName()).epidHeader(), request.getEpid()); + } + + if (request instanceof NewBucketRequest) { + putHeader(headers, getIHeaders(request.getBucketName()).fsFileInterfaceHeader(), Constants.ENABLED); + } + + if (null != request.getBucketType() && BucketTypeEnum.PFS == request.getBucketType()) { + putHeader(headers, getIHeaders(request.getBucketName()).fsFileInterfaceHeader(), Constants.ENABLED); + } + + if (request.getAvailableZone() != null) { + putHeader(headers, getIHeaders(request.getBucketName()).azRedundancyHeader(), + request.getAvailableZone().getCode()); + } + + Set extensionPermissionEnums = request.getAllGrantPermissions(); + if (!extensionPermissionEnums.isEmpty()) { + for (ExtensionBucketPermissionEnum extensionPermissionEnum : extensionPermissionEnums) { + Set domainIds = request.getDomainIdsByGrantPermission(extensionPermissionEnum); + List domainIdList = new ArrayList(domainIds.size()); + for (String domainId : domainIds) { + domainIdList.add("id=" + domainId); + } + putHeader(headers, getHeaderByMethodName(request.getBucketName(), extensionPermissionEnum.getCode()), + ServiceUtils.join(domainIdList, ",")); + } + } + + if (request.getExtensionHeaderMap() != null) { + for (Entry kv : request.getExtensionHeaderMap().entrySet()) { + putHeader(headers, kv.getKey(), kv.getValue()); + } + } + + String contentType = Mimetypes.MIMETYPE_XML; + headers.put(Constants.CommonHeaders.CONTENT_TYPE, contentType); + TransResult result = new TransResult(headers); + if (ServiceUtils.isValid(request.getLocation())) { + String configXml = convertor.transBucketLoction(request.getLocation()); + headers.put(Constants.CommonHeaders.CONTENT_LENGTH, String.valueOf(configXml.length())); + RequestBody requestEntity = createRequestBody(contentType, configXml); + result.setBody(requestEntity); + } + return result; + } + + protected RestoreObjectStatus transRestoreObjectResultToRestoreObjectStatus(RestoreObjectResult result) { + RestoreObjectStatus ret = RestoreObjectStatus.valueOf(result.getStatusCode()); + ret.setResponseHeaders(result.getResponseHeaders()); + ret.setStatusCode(result.getStatusCode()); + + return ret; + } + + protected TransResult transPutObjectRequest(PutObjectRequest request) throws ServiceException { + Map headers = new HashMap(); + IHeaders iheaders = this.getIHeaders(request.getBucketName()); + + ObjectMetadata objectMetadata = request.getMetadata() == null ? new ObjectMetadata() : request.getMetadata(); + + setBaseHeaderFromMetadata(request.getBucketName(), headers, objectMetadata); + + if (request.getExpires() >= 0) { + putHeader(headers, iheaders.expiresHeader(), String.valueOf(request.getExpires())); + } + + if (request.getSuccessRedirectLocation() != null) { + putHeader(headers, iheaders.successRedirectLocationHeader(), request.getSuccessRedirectLocation()); + } + + transRequestPaymentHeaders(request, headers, iheaders); + + transExtensionPermissions(request, headers); + + transSseHeaders(request, headers, iheaders); + + Object contentType = objectMetadata.getContentType() == null + ? objectMetadata.getValue(CommonHeaders.CONTENT_TYPE) : objectMetadata.getContentType(); + if (contentType == null) { + contentType = Mimetypes.getInstance().getMimetype(request.getObjectKey()); + } + Object contentLength = objectMetadata.getContentLength(); + + if (contentLength == null) { + contentLength = objectMetadata.getValue(CommonHeaders.CONTENT_LENGTH); + } + + long contentLengthValue = contentLength == null ? -1L : Long.parseLong(contentLength.toString()); + + if (request.getFile() != null) { + if (Mimetypes.MIMETYPE_OCTET_STREAM.equals(contentType)) { + contentType = Mimetypes.getInstance().getMimetype(request.getFile()); + } + + long fileSize = request.getFile().length(); + try { + request.setInput(new FileInputStream(request.getFile())); + } catch (FileNotFoundException e) { + throw new IllegalArgumentException("File doesnot exist"); + } + + contentLengthValue = getContentLengthFromFile(request, contentLengthValue, fileSize); + } + + String contentTypeStr = contentType.toString().trim(); + headers.put(CommonHeaders.CONTENT_TYPE, contentTypeStr); + + if (contentLengthValue > -1) { + this.putHeader(headers, CommonHeaders.CONTENT_LENGTH, String.valueOf(contentLengthValue)); + } + + if (request.getInput() != null && request.getProgressListener() != null) { + ProgressManager progressManager = new SimpleProgressManager(contentLengthValue, 0, + request.getProgressListener(), request.getProgressInterval() > 0 ? request.getProgressInterval() + : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); + request.setInput(new ProgressInputStream(request.getInput(), progressManager)); + } + + RequestBody body = request.getInput() == null ? null + : new RepeatableRequestEntity(request.getInput(), contentTypeStr, contentLengthValue, + this.obsProperties); + + return new TransResult(headers, body); + } + + private long getContentLengthFromFile(PutObjectRequest request, long contentLengthValue, long fileSize) { + if (request.getOffset() > 0 && request.getOffset() < fileSize) { + contentLengthValue = (contentLengthValue > 0 + && contentLengthValue <= fileSize - request.getOffset()) + ? contentLengthValue : fileSize - request.getOffset(); + try { + long skipByte = request.getInput().skip(request.getOffset()); + if (log.isDebugEnabled()) { + log.debug("Skip " + skipByte + " bytes; offset : " + request.getOffset()); + } + } catch (IOException e) { + ServiceUtils.closeStream(request.getInput()); + throw new ServiceException(e); + } + } else if (contentLengthValue < 0 || contentLengthValue > fileSize) { + contentLengthValue = fileSize; + } + return contentLengthValue; + } + + private void setBaseHeaderFromMetadata(String bucketName, Map headers, + ObjectMetadata objectMetadata) { + IConvertor iconvertor = this.getIConvertor(bucketName); + IHeaders iheaders = this.getIHeaders(bucketName); + + selectAllowedHeader(headers, objectMetadata); + + if (ServiceUtils.isValid(objectMetadata.getContentMd5())) { + headers.put(CommonHeaders.CONTENT_MD5, objectMetadata.getContentMd5().trim()); + } + + if (ServiceUtils.isValid(objectMetadata.getContentEncoding())) { + headers.put(CommonHeaders.CONTENT_ENCODING, objectMetadata.getContentEncoding().trim()); + } + + if (ServiceUtils.isValid(objectMetadata.getContentDisposition())) { + headers.put(CommonHeaders.CONTENT_DISPOSITION, objectMetadata.getContentDisposition().trim()); + } + + if (ServiceUtils.isValid(objectMetadata.getCacheControl())) { + headers.put(CommonHeaders.CACHE_CONTROL, objectMetadata.getCacheControl().trim()); + } + + if (ServiceUtils.isValid(objectMetadata.getContentLanguage())) { + headers.put(CommonHeaders.CONTENT_LANGUAGE, objectMetadata.getContentLanguage().trim()); + } + + if (ServiceUtils.isValid(objectMetadata.getExpires())) { + headers.put(CommonHeaders.EXPIRES, objectMetadata.getExpires().trim()); + } + + if (ServiceUtils.isValid(objectMetadata.getCrc64())) { + headers.put(this.getProviderCredentials().getLocalAuthType(bucketName) != AuthTypeEnum.OBS + ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX + CommonHeaders.HASH_CRC64ECMA, + objectMetadata.getCrc64().trim()); + } + + if (objectMetadata.getObjectStorageClass() != null) { + putHeader(headers, iheaders.storageClassHeader(), + iconvertor.transStorageClass(objectMetadata.getObjectStorageClass())); + } + + if (objectMetadata.getWebSiteRedirectLocation() != null) { + putHeader(headers, iheaders.websiteRedirectLocationHeader(), objectMetadata.getWebSiteRedirectLocation()); + } + } + + protected TransResult transWriteFileRequest(WriteFileRequest request) throws ServiceException { + TransResult result = this.transPutObjectRequest(request); + if (request.getPosition() > 0) { + Map params = new HashMap(); + params.put(SpecialParamEnum.MODIFY.getOriginalStringCode(), ""); + params.put(ObsRequestParams.POSITION, String.valueOf(request.getPosition())); + result.setParams(params); + } + return result; + } + + protected TransResult transModifyObjectRequest(ModifyObjectRequest request) throws ServiceException { + TransResult result = this.transPutObjectRequest(request); + if (request.getPosition() > 0) { + Map params = new HashMap(); + params.put(SpecialParamEnum.MODIFY.getOriginalStringCode(), ""); + params.put(ObsRequestParams.POSITION, String.valueOf(request.getPosition())); + result.setParams(params); + } + return result; + } + + protected TransResult transAppendObjectRequest(AppendObjectRequest request) throws ServiceException { + TransResult result = this.transPutObjectRequest(request); + Map params = new HashMap(); + params.put(SpecialParamEnum.APPEND.getOriginalStringCode(), ""); + params.put(ObsRequestParams.POSITION, String.valueOf(request.getPosition())); + result.setParams(params); + return result; + } + + protected TransResult transCopyObjectRequest(CopyObjectRequest request) throws ServiceException { + Map headers = new HashMap(); + IConvertor iconvertor = this.getIConvertor(request.getBucketName()); + IHeaders iheaders = this.getIHeaders(request.getBucketName()); + + ObjectMetadata objectMetadata = request.getNewObjectMetadata() == null ? new ObjectMetadata() + : request.getNewObjectMetadata(); + + putHeader(headers, iheaders.metadataDirectiveHeader(), + request.isReplaceMetadata() ? Constants.DERECTIVE_REPLACE : Constants.DERECTIVE_COPY); + if (request.isReplaceMetadata()) { + objectMetadata.getAllMetadata().remove(iheaders.requestIdHeader()); + objectMetadata.getAllMetadata().remove(iheaders.requestId2Header()); + for (Map.Entry entry : objectMetadata.getAllMetadata().entrySet()) { + String key = entry.getKey(); + if (!ServiceUtils.isValid(key)) { + continue; + } + key = key.trim(); + if (Constants.ALLOWED_REQUEST_HTTP_HEADER_METADATA_NAMES.contains(key.toLowerCase())) { + continue; + } + headers.put(key, entry.getValue() == null ? "" : entry.getValue().toString()); + } + } + + setBaseHeaderFromMetadata(request.getBucketName(), headers, objectMetadata); + + if (objectMetadata.getObjectStorageClass() != null) { + putHeader(headers, iheaders.storageClassHeader(), + iconvertor.transStorageClass(objectMetadata.getObjectStorageClass())); + } + + if (objectMetadata.getWebSiteRedirectLocation() != null) { + putHeader(headers, iheaders.websiteRedirectLocationHeader(), objectMetadata.getWebSiteRedirectLocation()); + } + + if (request.getSuccessRedirectLocation() != null) { + putHeader(headers, iheaders.successRedirectLocationHeader(), request.getSuccessRedirectLocation()); + } + + this.transRequestPaymentHeaders(request, headers, iheaders); + this.transExtensionPermissions(request, headers); + this.transSseHeaders(request, headers, iheaders); + + transSseCSourceHeaders(request.getSseCHeaderSource(), headers, iheaders); + + transConditionCopyHeaders(request, headers, iheaders); + + String sourceKey = RestUtils.encodeUrlString(request.getSourceBucketName()) + "/" + + RestUtils.encodeUrlString(request.getSourceObjectKey()); + if (ServiceUtils.isValid(request.getVersionId())) { + sourceKey += "?versionId=" + request.getVersionId().trim(); + } + putHeader(headers, iheaders.copySourceHeader(), sourceKey); + + return new TransResult(headers); + } + + protected void transSseCSourceHeaders(SseCHeader sseCHeader, Map headers, IHeaders iheaders) + throws ServiceException { + if (sseCHeader != null) { + String algorithm = sseCHeader.getSSEAlgorithm().getCode(); + putHeader(headers, iheaders.copySourceSseCHeader(), ServiceUtils.toValid(algorithm)); + if (sseCHeader.getSseCKeyBase64() != null) { + try { + putHeader(headers, iheaders.copySourceSseCKeyHeader(), sseCHeader.getSseCKeyBase64()); + putHeader(headers, iheaders.copySourceSseCKeyMd5Header(), ServiceUtils.toBase64( + ServiceUtils.computeMD5Hash(ServiceUtils.fromBase64(sseCHeader.getSseCKeyBase64())))); + } catch (IOException e) { + throw new IllegalStateException("fail to read sseCkey", e); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("fail to read sseCkey", e); + } + } else if (null != sseCHeader.getSseCKey() + && sseCHeader.getSseCKey().length > 0) { + try { + byte[] data = sseCHeader.getSseCKey(); + putHeader(headers, iheaders.copySourceSseCKeyHeader(), ServiceUtils.toBase64(data)); + putHeader(headers, iheaders.copySourceSseCKeyMd5Header(), + ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(data))); + } catch (IOException e) { + throw new IllegalStateException("fail to read sseCkey", e); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("fail to read sseCkey", e); + } + } + } + } + + protected void transConditionCopyHeaders(CopyObjectRequest request, Map headers, + IHeaders iheaders) { + if (request.getIfModifiedSince() != null) { + putHeader(headers, iheaders.copySourceIfModifiedSinceHeader(), + ServiceUtils.formatRfc822Date(request.getIfModifiedSince())); + } + if (request.getIfUnmodifiedSince() != null) { + putHeader(headers, iheaders.copySourceIfUnmodifiedSinceHeader(), + ServiceUtils.formatRfc822Date(request.getIfUnmodifiedSince())); + } + if (ServiceUtils.isValid(request.getIfMatchTag())) { + putHeader(headers, iheaders.copySourceIfMatchHeader(), request.getIfMatchTag().trim()); + } + if (ServiceUtils.isValid(request.getIfNoneMatchTag())) { + putHeader(headers, iheaders.copySourceIfNoneMatchHeader(), request.getIfNoneMatchTag().trim()); + } + } + + protected TransResult transGetObjectRequest(GetObjectRequest request) throws ServiceException { + Map headers = new HashMap(); + this.transSseCHeaders(request.getSseCHeader(), headers, this.getIHeaders(request.getBucketName())); + this.transConditionGetObjectHeaders(request, headers); + + this.transRequestPaymentHeaders(request, headers, this.getIHeaders(request.getBucketName())); + transRangeHeader(request, headers); + + Map params = new HashMap(); + this.transGetObjectParams(request, params); + + return new TransResult(headers, params, null); + } + + /** + * + * @param request + * @param headers + */ + protected void transRangeHeader(GetObjectRequest request, Map headers) { + String start = ""; + String end = ""; + + if (null != request.getRangeStart()) { + ServiceUtils.assertParameterNotNegative(request.getRangeStart().longValue(), + "start range should not be negative."); + start = String.valueOf(request.getRangeStart()); + } + + if (null != request.getRangeEnd()) { + ServiceUtils.assertParameterNotNegative(request.getRangeEnd().longValue(), + "end range should not be negative."); + end = String.valueOf(request.getRangeEnd()); + } + + if (null != request.getRangeStart() && null != request.getRangeEnd()) { + if (request.getRangeStart().longValue() > request.getRangeEnd().longValue()) { + throw new IllegalArgumentException("start must be less than end."); + } + } + + if (!"".equals(start) || !"".equals(end)) { + String range = String.format("bytes=%s-%s", start, end); + headers.put(CommonHeaders.RANGE, range); + } + } + + protected void transGetObjectParams(GetObjectRequest request, Map params) { + if (null != request.getReplaceMetadata()) { + if (ServiceUtils.isValid(request.getReplaceMetadata().getCacheControl())) { + params.put(ObsRequestParams.RESPONSE_CACHE_CONTROL, request.getReplaceMetadata().getCacheControl()); + } + if (ServiceUtils.isValid(request.getReplaceMetadata().getContentDisposition())) { + params.put(ObsRequestParams.RESPONSE_CONTENT_DISPOSITION, + request.getReplaceMetadata().getContentDisposition()); + } + if (ServiceUtils.isValid(request.getReplaceMetadata().getContentEncoding())) { + params.put(ObsRequestParams.RESPONSE_CONTENT_ENCODING, + request.getReplaceMetadata().getContentEncoding()); + } + if (ServiceUtils.isValid(request.getReplaceMetadata().getContentLanguage())) { + params.put(ObsRequestParams.RESPONSE_CONTENT_LANGUAGE, + request.getReplaceMetadata().getContentLanguage()); + } + if (ServiceUtils.isValid(request.getReplaceMetadata().getContentType())) { + params.put(ObsRequestParams.RESPONSE_CONTENT_TYPE, request.getReplaceMetadata().getContentType()); + } + if (ServiceUtils.isValid(request.getReplaceMetadata().getExpires())) { + params.put(ObsRequestParams.RESPONSE_EXPIRES, request.getReplaceMetadata().getExpires()); + } + } + if (ServiceUtils.isValid(request.getImageProcess())) { + params.put(ObsRequestParams.X_IMAGE_PROCESS, request.getImageProcess()); + } + if (request.getVersionId() != null) { + params.put(ObsRequestParams.VERSION_ID, request.getVersionId()); + } + if (request.getCacheOption() != null) { + String cacheControl = request.getCacheOption().getCode() + ", ttl=" + request.getTtl(); + params.put(ObsRequestParams.X_CACHE_CONTROL, cacheControl); + } + } + + protected void transConditionGetObjectHeaders(GetObjectRequest request, Map headers) { + if (request.getIfModifiedSince() != null) { + headers.put(CommonHeaders.IF_MODIFIED_SINCE, ServiceUtils.formatRfc822Date(request.getIfModifiedSince())); + } + if (request.getIfUnmodifiedSince() != null) { + headers.put(CommonHeaders.IF_UNMODIFIED_SINCE, + ServiceUtils.formatRfc822Date(request.getIfUnmodifiedSince())); + } + if (ServiceUtils.isValid(request.getIfMatchTag())) { + headers.put(CommonHeaders.IF_MATCH, request.getIfMatchTag().trim()); + } + if (ServiceUtils.isValid(request.getIfNoneMatchTag())) { + headers.put(CommonHeaders.IF_NONE_MATCH, request.getIfNoneMatchTag().trim()); + } + if (!request.isAutoUnzipResponse()) { + headers.put(CommonHeaders.ACCETP_ENCODING, "identity"); + } + } + + protected TransResult transSetObjectMetadataRequest(SetObjectMetadataRequest request) throws ServiceException { + Map headers = new HashMap(); + IHeaders iheaders = this.getIHeaders(request.getBucketName()); + IConvertor iconvertor = this.getIConvertor(request.getBucketName()); + + for (Map.Entry entry : request.getAllUserMetadata().entrySet()) { + String key = entry.getKey(); + if (!ServiceUtils.isValid(key)) { + continue; + } + key = key.trim(); + headers.put(key, entry.getValue() == null ? "" : entry.getValue()); + } + + if (request.getObjectStorageClass() != null) { + putHeader(headers, iheaders.storageClassHeader(), + iconvertor.transStorageClass(request.getObjectStorageClass())); + } + + if (request.getWebSiteRedirectLocation() != null) { + putHeader(headers, iheaders.websiteRedirectLocationHeader(), request.getWebSiteRedirectLocation()); + } + + if (request.getContentDisposition() != null) { + putHeader(headers, Constants.CommonHeaders.CONTENT_DISPOSITION, request.getContentDisposition()); + } + + if (request.getContentEncoding() != null) { + putHeader(headers, Constants.CommonHeaders.CONTENT_ENCODING, request.getContentEncoding()); + } + + if (request.getContentLanguage() != null) { + putHeader(headers, Constants.CommonHeaders.CONTENT_LANGUAGE, request.getContentLanguage()); + } + + if (request.getContentType() != null) { + putHeader(headers, Constants.CommonHeaders.CONTENT_TYPE, request.getContentType()); + } + + if (request.getCacheControl() != null) { + putHeader(headers, Constants.CommonHeaders.CACHE_CONTROL, request.getCacheControl()); + } + + if (request.getExpires() != null) { + putHeader(headers, Constants.CommonHeaders.EXPIRES, request.getExpires()); + } + + this.transRequestPaymentHeaders(request, headers, iheaders); + putHeader(headers, iheaders.metadataDirectiveHeader(), + request.isRemoveUnset() ? Constants.DERECTIVE_REPLACE : Constants.DERECTIVE_REPLACE_NEW); + + Map params = new HashMap(); + params.put(SpecialParamEnum.METADATA.getOriginalStringCode(), ""); + if (request.getVersionId() != null) { + params.put(ObsRequestParams.VERSION_ID, request.getVersionId()); + } + + return new TransResult(headers, params, null); + } + + protected TransResult transCopyPartRequest(CopyPartRequest request) throws ServiceException { + Map params = new HashMap(); + params.put(ObsRequestParams.PART_NUMBER, String.valueOf(request.getPartNumber())); + params.put(ObsRequestParams.UPLOAD_ID, request.getUploadId()); + + Map headers = new HashMap(); + IHeaders iheaders = this.getIHeaders(request.getBucketName()); + + String sourceKey = RestUtils.encodeUrlString(request.getSourceBucketName()) + "/" + + RestUtils.encodeUrlString(request.getSourceObjectKey()); + if (ServiceUtils.isValid(request.getVersionId())) { + sourceKey += "?versionId=" + request.getVersionId().trim(); + } + putHeader(headers, iheaders.copySourceHeader(), sourceKey); + + if (request.getByteRangeStart() != null) { + String rangeEnd = request.getByteRangeEnd() != null ? String.valueOf(request.getByteRangeEnd()) : ""; + String range = String.format("bytes=%s-%s", request.getByteRangeStart(), rangeEnd); + putHeader(headers, iheaders.copySourceRangeHeader(), range); + } + + this.transRequestPaymentHeaders(request, headers, iheaders); + this.transSseCHeaders(request.getSseCHeaderDestination(), headers, iheaders); + this.transSseCSourceHeaders(request.getSseCHeaderSource(), headers, iheaders); + + return new TransResult(headers, params, null); + } + + protected TransResult transListObjectsRequest(ListObjectsRequest listObjectsRequest) { + Map params = new HashMap(); + if (listObjectsRequest.getPrefix() != null) { + params.put(ObsRequestParams.PREFIX, listObjectsRequest.getPrefix()); + } + if (listObjectsRequest.getDelimiter() != null) { + params.put(ObsRequestParams.DELIMITER, listObjectsRequest.getDelimiter()); + } + if (listObjectsRequest.getMaxKeys() > 0) { + params.put(ObsRequestParams.MAX_KEYS, String.valueOf(listObjectsRequest.getMaxKeys())); + } + if (listObjectsRequest.getMarker() != null) { + params.put(ObsRequestParams.MARKER, listObjectsRequest.getMarker()); + } + if (listObjectsRequest.getEncodingType() != null) { + params.put(ObsRequestParams.ENCODING_TYPE, listObjectsRequest.getEncodingType()); + } + + Map headers = new HashMap(); + transRequestPaymentHeaders(listObjectsRequest, headers, this.getIHeaders(listObjectsRequest.getBucketName())); + if (listObjectsRequest.getListTimeout() > 0) { + putHeader(headers, this.getIHeaders(listObjectsRequest.getBucketName()).listTimeoutHeader(), + String.valueOf(listObjectsRequest.getListTimeout())); + } + + return new TransResult(headers, params, null); + } + + protected TransResult transListContentSummaryRequest(ListContentSummaryRequest listContentSummaryRequest) { + Map params = new HashMap(); + if (listContentSummaryRequest.getPrefix() != null) { + params.put(ObsRequestParams.PREFIX, listContentSummaryRequest.getPrefix()); + } + if (listContentSummaryRequest.getDelimiter() != null) { + params.put(ObsRequestParams.DELIMITER, listContentSummaryRequest.getDelimiter()); + } + if (listContentSummaryRequest.getMaxKeys() > 0) { + params.put(ObsRequestParams.MAX_KEYS, String.valueOf(listContentSummaryRequest.getMaxKeys())); + } + + if (listContentSummaryRequest.getMarker() != null) { + params.put(ObsRequestParams.MARKER, listContentSummaryRequest.getMarker()); + } + + params.put(SpecialParamEnum.LISTCONTENTSUMMARY.getOriginalStringCode(), ""); + + Map headers = new HashMap(); + transRequestPaymentHeaders(listContentSummaryRequest, headers, + this.getIHeaders(listContentSummaryRequest.getBucketName())); + if (listContentSummaryRequest.getListTimeout() > 0) { + putHeader(headers, this.getIHeaders(listContentSummaryRequest.getBucketName()).listTimeoutHeader(), + String.valueOf(listContentSummaryRequest.getListTimeout())); + } + return new TransResult(headers, params, null); + } + + protected TransResult transListContentSummaryFsRequest(ListContentSummaryFsRequest listContentSummaryFsRequest) { + Map params = new HashMap<>(); + params.put(SpecialParamEnum.LISTCONTENTSUMMARYFS.getOriginalStringCode(), ""); + Map headers = new HashMap(); + String sbXml = ""; + try { + OBSXMLBuilder builder = OBSXMLBuilder.create("MultiListContentSummary"); + builder.elem("MaxKeys").text(String.valueOf(listContentSummaryFsRequest.getMaxKeys())); + if (listContentSummaryFsRequest.getDirLayers().size() > 0) { + for (ListContentSummaryFsRequest.DirLayer dir : listContentSummaryFsRequest.getDirLayers()) { + OBSXMLBuilder builderDir = OBSXMLBuilder.create("Dir"); + builderDir.elem("Key").text(dir.getKey()); + if (ServiceUtils.isValid(dir.getMarker())) { + builderDir.elem("Marker").text(dir.getMarker()); + } + if (dir.getInode() != 0) { + builderDir.elem("Inode").text(String.valueOf(dir.getInode())); + } + builder.importXMLBuilder(builderDir); + } + } + sbXml = builder.asString(); + } catch (ParserConfigurationException | FactoryConfigurationError | TransformerException e) { + throw new ServiceException("Failed to build XML document for MultiListContentSummary", e); + } + + putHeader(headers, (this.getProviderCredentials().getLocalAuthType(listContentSummaryFsRequest.getBucketName()) + != AuthTypeEnum.OBS ? Constants.V2_HEADER_PREFIX : Constants.OBS_HEADER_PREFIX) + + Constants.FS_SUMMARY_DIR_LIST, + ServiceUtils.toBase64(sbXml.getBytes(StandardCharsets.UTF_8))); + transRequestPaymentHeaders(listContentSummaryFsRequest, headers, + this.getIHeaders(listContentSummaryFsRequest.getBucketName())); + return new TransResult(headers, params, null); + } + + protected TransResult transGetContentSummaryFs(ContentSummaryFsRequest contentSummaryFsRequest) { + Map params = new HashMap<>(); + params.put(SpecialParamEnum.GETCONTENTSUMMARY.getOriginalStringCode(), ""); + Map headers = new HashMap<>(); + transRequestPaymentHeaders(contentSummaryFsRequest, headers, + this.getIHeaders(contentSummaryFsRequest.getBucketName())); + return new TransResult(headers, params, null); + } + + protected TransResult transUploadPartRequest(UploadPartRequest request) throws ServiceException { + Map params = new HashMap(); + params.put(ObsRequestParams.PART_NUMBER, String.valueOf(request.getPartNumber())); + params.put(ObsRequestParams.UPLOAD_ID, request.getUploadId()); + + Map headers = new HashMap(); + IHeaders iheaders = this.getIHeaders(request.getBucketName()); + + if (ServiceUtils.isValid(request.getContentMd5())) { + headers.put(CommonHeaders.CONTENT_MD5, request.getContentMd5().trim()); + } + + this.transRequestPaymentHeaders(request, headers, iheaders); + this.transSseCHeaders(request.getSseCHeader(), headers, iheaders); + + long contentLength = -1L; + if (null != request.getFile()) { + long fileSize = request.getFile().length(); + long offset = (request.getOffset() >= 0 && request.getOffset() < fileSize) ? request.getOffset() : 0; + long partSize = (request.getPartSize() != null && request.getPartSize() > 0 + && request.getPartSize() <= (fileSize - offset)) ? request.getPartSize() : fileSize - offset; + contentLength = partSize; + + try { + if (request.isAttachMd5() && !ServiceUtils.isValid(request.getContentMd5())) { + headers.put(CommonHeaders.CONTENT_MD5, ServiceUtils.toBase64( + ServiceUtils.computeMD5Hash(new FileInputStream(request.getFile()), partSize, offset))); + } + request.setInput(new FileInputStream(request.getFile())); + long skipByte = request.getInput().skip(offset); + if (log.isDebugEnabled()) { + log.debug("Skip " + skipByte + " bytes; offset : " + offset); + } + } catch (Exception e) { + ServiceUtils.closeStream(request.getInput()); + throw new ServiceException(e); + } + } else if (null != request.getInput()) { + if (request.getPartSize() != null && request.getPartSize() > 0) { + contentLength = request.getPartSize(); + } + } + + if (request.getInput() != null && request.getProgressListener() != null) { + ProgressManager progressManager = new SimpleProgressManager(contentLength, 0, request.getProgressListener(), + request.getProgressInterval() > 0 ? request.getProgressInterval() + : ObsConstraint.DEFAULT_PROGRESS_INTERVAL); + request.setInput(new ProgressInputStream(request.getInput(), progressManager)); + } + + String contentType = Mimetypes.getInstance().getMimetype(request.getObjectKey()); + headers.put(CommonHeaders.CONTENT_TYPE, contentType); + + if (contentLength > -1) { + this.putHeader(headers, CommonHeaders.CONTENT_LENGTH, String.valueOf(contentLength)); + } + RequestBody body = request.getInput() == null ? null + : new RepeatableRequestEntity(request.getInput(), contentType, contentLength, this.obsProperties); + return new TransResult(headers, params, body); + } + + protected void transSseKmsHeaders(SseKmsHeader kmsHeader, Map headers, IHeaders iheaders, + String bucketName) { + if (kmsHeader == null) { + return; + } + + String sseKmsEncryption = this.getProviderCredentials().getLocalAuthType(bucketName) != AuthTypeEnum.OBS + ? "aws:" + kmsHeader.getSSEAlgorithm().getCode() : kmsHeader.getSSEAlgorithm().getCode(); + putHeader(headers, iheaders.sseKmsHeader(), ServiceUtils.toValid(sseKmsEncryption)); + if (ServiceUtils.isValid(kmsHeader.getKmsKeyId())) { + putHeader(headers, iheaders.sseKmsKeyHeader(), kmsHeader.getKmsKeyId()); + } + + if (ServiceUtils.isValid(kmsHeader.getProjectId())) { + putHeader(headers, iheaders.sseKmsProjectIdHeader(), kmsHeader.getProjectId()); + } + } +} diff --git a/app/src/main/java/com/obs/services/internal/task/AbstractObsTask.java b/app/src/main/java/com/obs/services/internal/task/AbstractObsTask.java index 8872017..f2794e3 100644 --- a/app/src/main/java/com/obs/services/internal/task/AbstractObsTask.java +++ b/app/src/main/java/com/obs/services/internal/task/AbstractObsTask.java @@ -11,49 +11,30 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.task; -import com.obs.services.ObsClient; +import com.obs.services.AbstractClient; import com.obs.services.model.TaskProgressListener; -public abstract class AbstractObsTask implements Runnable { +public abstract class AbstractObsTask extends AbstractTask { + + private DefaultTaskProgressStatus progressStatus; + private TaskProgressListener progressListener; + private int taskProgressInterval; - protected ObsClient obsClient; - protected String bucketName; - protected DefaultTaskProgressStatus progressStatus; - protected TaskProgressListener progressListener; - protected int taskProgressInterval; - - public AbstractObsTask(ObsClient obsClient, String bucketName) { - this.obsClient = obsClient; - this.bucketName = bucketName; + public AbstractObsTask(AbstractClient obsClient, String bucketName) { + super(obsClient, bucketName); } - public AbstractObsTask(ObsClient obsClient, String bucketName, DefaultTaskProgressStatus progressStatus, + public AbstractObsTask(AbstractClient obsClient, String bucketName, DefaultTaskProgressStatus progressStatus, TaskProgressListener progressListener, int taskProgressInterval) { - this.obsClient = obsClient; - this.bucketName = bucketName; + super(obsClient, bucketName); this.progressStatus = progressStatus; this.progressListener = progressListener; this.taskProgressInterval = taskProgressInterval; } - public ObsClient getObsClient() { - return obsClient; - } - - public void setObsClient(ObsClient obsClient) { - this.obsClient = obsClient; - } - - public String getBucketName() { - return bucketName; - } - - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } - public DefaultTaskProgressStatus getProgressStatus() { return progressStatus; } diff --git a/app/src/main/java/com/obs/services/internal/task/AbstractPutTask.java b/app/src/main/java/com/obs/services/internal/task/AbstractPutTask.java new file mode 100644 index 0000000..9fa2937 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/task/AbstractPutTask.java @@ -0,0 +1,75 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal.task; + +import com.obs.services.AbstractClient; +import com.obs.services.model.PutObjectBasicRequest; +import com.obs.services.model.PutObjectResult; +import com.obs.services.model.TaskCallback; +import com.obs.services.model.UploadObjectsProgressListener; + +public abstract class AbstractPutTask extends AbstractTask { + private UploadObjectsProgressListener progressListener; + + private int taskProgressInterval; + + private TaskCallback callback; + + private UploadTaskProgressStatus taskStatus; + + public AbstractPutTask(AbstractClient obsClient, String bucketName, + TaskCallback callback, + UploadObjectsProgressListener progressListener, UploadTaskProgressStatus progressStatus, + int taskProgressInterval) { + super(obsClient, bucketName); + this.callback = callback; + this.progressListener = progressListener; + this.taskStatus = progressStatus; + this.taskProgressInterval = taskProgressInterval; + } + + public UploadObjectsProgressListener getProgressListener() { + return progressListener; + } + + public void setProgressListener(UploadObjectsProgressListener progressListener) { + this.progressListener = progressListener; + } + + public int getTaskProgressInterval() { + return taskProgressInterval; + } + + public void setTaskProgressInterval(int taskProgressInterval) { + this.taskProgressInterval = taskProgressInterval; + } + + public TaskCallback getCallback() { + return callback; + } + + public void setCallback(TaskCallback callback) { + this.callback = callback; + } + + public UploadTaskProgressStatus getTaskStatus() { + return taskStatus; + } + + public void setTaskStatus(UploadTaskProgressStatus taskStatus) { + this.taskStatus = taskStatus; + } +} diff --git a/app/src/main/java/com/obs/services/internal/task/AbstractTask.java b/app/src/main/java/com/obs/services/internal/task/AbstractTask.java new file mode 100644 index 0000000..c177ea4 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/task/AbstractTask.java @@ -0,0 +1,44 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.internal.task; + +import com.obs.services.AbstractClient; + +public abstract class AbstractTask implements Runnable { + private AbstractClient obsClient; + private String bucketName; + + public AbstractTask(AbstractClient obsClient, String bucketName) { + this.obsClient = obsClient; + this.bucketName = bucketName; + } + + public AbstractClient getObsClient() { + return obsClient; + } + + public void setObsClient(AbstractClient obsClient) { + this.obsClient = obsClient; + } + + public String getBucketName() { + return bucketName; + } + + public void setBucketName(String bucketName) { + this.bucketName = bucketName; + } +} diff --git a/app/src/main/java/com/obs/services/internal/task/BlockRejectedExecutionHandler.java b/app/src/main/java/com/obs/services/internal/task/BlockRejectedExecutionHandler.java index f79543d..f178fda 100644 --- a/app/src/main/java/com/obs/services/internal/task/BlockRejectedExecutionHandler.java +++ b/app/src/main/java/com/obs/services/internal/task/BlockRejectedExecutionHandler.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.task; import java.util.concurrent.RejectedExecutionException; diff --git a/app/src/main/java/com/obs/services/internal/task/DefaultTaskProgressStatus.java b/app/src/main/java/com/obs/services/internal/task/DefaultTaskProgressStatus.java index 78e30c2..c8bbb14 100644 --- a/app/src/main/java/com/obs/services/internal/task/DefaultTaskProgressStatus.java +++ b/app/src/main/java/com/obs/services/internal/task/DefaultTaskProgressStatus.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.task; import java.util.concurrent.atomic.AtomicInteger; diff --git a/app/src/main/java/com/obs/services/internal/task/DropFolderTask.java b/app/src/main/java/com/obs/services/internal/task/DropFolderTask.java index 305833d..aee7481 100644 --- a/app/src/main/java/com/obs/services/internal/task/DropFolderTask.java +++ b/app/src/main/java/com/obs/services/internal/task/DropFolderTask.java @@ -11,9 +11,11 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.task; -import com.obs.services.ObsClient; +import com.obs.services.AbstractClient; +import com.obs.services.model.DeleteObjectRequest; import com.obs.services.model.DeleteObjectResult; import com.obs.services.model.TaskCallback; import com.obs.services.model.TaskProgressListener; @@ -23,17 +25,20 @@ public class DropFolderTask extends AbstractObsTask { private String objectKey; private TaskCallback callback; + + boolean isRequesterPays; - public DropFolderTask(ObsClient obsClient, String bucketName) { + public DropFolderTask(AbstractClient obsClient, String bucketName) { super(obsClient, bucketName); } - public DropFolderTask(ObsClient obsClient, String bucketName, String objectKey, + public DropFolderTask(AbstractClient obsClient, String bucketName, String objectKey, DefaultTaskProgressStatus progressStatus, TaskProgressListener progressListener, int taskProgressInterval, - TaskCallback callback) { + TaskCallback callback, boolean isRequesterPays) { super(obsClient, bucketName, progressStatus, progressListener, taskProgressInterval); this.objectKey = objectKey; this.callback = callback; + this.isRequesterPays = isRequesterPays; } public String getObjectKey() { @@ -53,8 +58,10 @@ public void setCallback(TaskCallback callback) { } private void dropFolder() { - DeleteObjectResult result = obsClient.deleteObject(bucketName, objectKey); - progressStatus.succeedTaskIncrement(); + DeleteObjectRequest request = new DeleteObjectRequest(this.getBucketName(), objectKey); + request.setRequesterPays(this.isRequesterPays); + DeleteObjectResult result = this.getObsClient().deleteObject(request); + this.getProgressStatus().succeedTaskIncrement(); callback.onSuccess(result); } diff --git a/app/src/main/java/com/obs/services/internal/task/LazyTaksCallback.java b/app/src/main/java/com/obs/services/internal/task/LazyTaksCallback.java index 266582f..0da4b7a 100644 --- a/app/src/main/java/com/obs/services/internal/task/LazyTaksCallback.java +++ b/app/src/main/java/com/obs/services/internal/task/LazyTaksCallback.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.task; import com.obs.services.exception.ObsException; diff --git a/app/src/main/java/com/obs/services/internal/task/LazyTaskProgressStatus.java b/app/src/main/java/com/obs/services/internal/task/LazyTaskProgressStatus.java index b28c2fb..f345946 100644 --- a/app/src/main/java/com/obs/services/internal/task/LazyTaskProgressStatus.java +++ b/app/src/main/java/com/obs/services/internal/task/LazyTaskProgressStatus.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.task; public class LazyTaskProgressStatus extends DefaultTaskProgressStatus { diff --git a/app/src/main/java/com/obs/services/internal/task/PutObjectTask.java b/app/src/main/java/com/obs/services/internal/task/PutObjectTask.java index 4349163..ad15575 100644 --- a/app/src/main/java/com/obs/services/internal/task/PutObjectTask.java +++ b/app/src/main/java/com/obs/services/internal/task/PutObjectTask.java @@ -11,9 +11,10 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.task; -import com.obs.services.ObsClient; +import com.obs.services.AbstractClient; import com.obs.services.exception.ObsException; import com.obs.services.model.ProgressStatus; import com.obs.services.model.PutObjectBasicRequest; @@ -22,121 +23,57 @@ import com.obs.services.model.TaskCallback; import com.obs.services.model.UploadObjectsProgressListener; -public class PutObjectTask implements Runnable { - - protected ObsClient obsClient; - - protected String bucketName; - - private UploadObjectsProgressListener progressListener; - - private int taskProgressInterval; - - private PutObjectRequest taskRequest; - - private TaskCallback callback; - - private UploadTaskProgressStatus taskStatus; - - public PutObjectTask(ObsClient obsClient, String bucketName, PutObjectRequest taskRequest, - TaskCallback callback, UploadObjectsProgressListener progressListener, - UploadTaskProgressStatus progressStatus, int taskProgressInterval) { - this.obsClient = obsClient; - this.bucketName = bucketName; - this.taskRequest = taskRequest; - this.callback = callback; - this.progressListener = progressListener; - this.taskStatus = progressStatus; - this.taskProgressInterval = taskProgressInterval; - } - - public ObsClient getObsClient() { - return obsClient; - } +public class PutObjectTask extends AbstractPutTask { - public void setObsClient(ObsClient obsClient) { - this.obsClient = obsClient; - } + private PutObjectRequest taskRequest; - public String getBucketName() { - return bucketName; + public PutObjectTask(AbstractClient obsClient, String bucketName, PutObjectRequest taskRequest, + TaskCallback callback, + UploadObjectsProgressListener progressListener, UploadTaskProgressStatus progressStatus, + int taskProgressInterval) { + super(obsClient, bucketName, callback, progressListener, progressStatus, taskProgressInterval); + this.taskRequest = taskRequest; } - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } - - public UploadObjectsProgressListener getUploadObjectsProgressListener() { - return progressListener; + public PutObjectRequest getTaskRequest() { + return taskRequest; } - public void setUploadObjectsProgressListener(UploadObjectsProgressListener progressListener) { - this.progressListener = progressListener; - } - - public int getTaskProgressInterval() { - return taskProgressInterval; - } - - public void setTaskProgressInterval(int taskProgressInterval) { - this.taskProgressInterval = taskProgressInterval; + public void setTaskRequest(PutObjectRequest taskRequest) { + this.taskRequest = taskRequest; } - - public PutObjectRequest getTaskRequest() { - return taskRequest; - } - - public void setTaskRequest(PutObjectRequest taskRequest) { - this.taskRequest = taskRequest; - } - - public TaskCallback getCallback() { - return callback; - } - - public void setCallback(TaskCallback callback) { - this.callback = callback; - } - public UploadTaskProgressStatus getTaskStatus() { - return taskStatus; - } - - public void setTaskStatus(UploadTaskProgressStatus taskStatus) { - this.taskStatus = taskStatus; - } - - private void putObjects() { - try { - PutObjectResult result = obsClient.putObject(taskRequest); - taskStatus.succeedTaskIncrement(); - PutObjectResult ret = new PutObjectResult(result.getBucketName(), result.getObjectKey() - , result.getEtag(), result.getVersionId(), result.getObjectUrl(), result.getResponseHeaders(), result.getStatusCode()); - callback.onSuccess(ret); + private void putObjects() { + try { + PutObjectResult result = this.getObsClient().putObject(taskRequest); + this.getTaskStatus().succeedTaskIncrement(); + PutObjectResult ret = new PutObjectResult(result.getBucketName(), result.getObjectKey(), result.getEtag(), + result.getVersionId(), result.getObjectUrl(), result.getResponseHeaders(), result.getStatusCode()); + this.getCallback().onSuccess(ret); } catch (ObsException e) { - taskStatus.failTaskIncrement(); - callback.onException(e, taskRequest); + this.getTaskStatus().failTaskIncrement(); + this.getCallback().onException(e, taskRequest); } finally { - taskStatus.execTaskIncrement(); - if (progressListener != null) { - if (taskStatus.getExecTaskNum() % this.taskProgressInterval == 0) { - progressListener.progressChanged(taskStatus); + this.getTaskStatus().execTaskIncrement(); + if (this.getProgressListener() != null) { + if (this.getTaskStatus().getExecTaskNum() % this.getTaskProgressInterval() == 0) { + this.getProgressListener().progressChanged(this.getTaskStatus()); } - if (taskStatus.getExecTaskNum() == taskStatus.getTotalTaskNum()) { - progressListener.progressChanged(taskStatus); + if (this.getTaskStatus().getExecTaskNum() == this.getTaskStatus().getTotalTaskNum()) { + this.getProgressListener().progressChanged(this.getTaskStatus()); } } - //更新已完成任务的大小信息,移除不在线程中的任务进度信息 - final String key = taskRequest.getObjectKey(); - ProgressStatus status = taskStatus.getTaskStatus(key); - if(status != null) { - taskStatus.addEndingTaskSize(status.getTransferredBytes()); - } - taskStatus.removeTaskTable(key); + // 更新已完成任务的大小信息,移除不在线程中的任务进度信息 + final String key = taskRequest.getObjectKey(); + ProgressStatus status = this.getTaskStatus().getTaskStatus(key); + if (status != null) { + this.getTaskStatus().addEndingTaskSize(status.getTransferredBytes()); + } + this.getTaskStatus().removeTaskTable(key); } - } + } - @Override + @Override public void run() { putObjects(); } diff --git a/app/src/main/java/com/obs/services/internal/task/RestoreObjectTask.java b/app/src/main/java/com/obs/services/internal/task/RestoreObjectTask.java index 2a0006e..cf5f2d2 100644 --- a/app/src/main/java/com/obs/services/internal/task/RestoreObjectTask.java +++ b/app/src/main/java/com/obs/services/internal/task/RestoreObjectTask.java @@ -11,8 +11,10 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.task; +import com.obs.services.AbstractClient; import com.obs.services.ObsClient; import com.obs.services.exception.ObsException; import com.obs.services.model.RestoreObjectRequest; @@ -30,14 +32,14 @@ public RestoreObjectTask(ObsClient obsClient, String bucketName) { super(obsClient, bucketName); } - public RestoreObjectTask(ObsClient obsClient, String bucketName, RestoreObjectRequest taskRequest, + public RestoreObjectTask(AbstractClient obsClient, String bucketName, RestoreObjectRequest taskRequest, TaskCallback callback) { super(obsClient, bucketName); this.taskRequest = taskRequest; this.callback = callback; } - public RestoreObjectTask(ObsClient obsClient, String bucketName, RestoreObjectRequest taskRequest, + public RestoreObjectTask(AbstractClient obsClient, String bucketName, RestoreObjectRequest taskRequest, TaskCallback callback, TaskProgressListener listener, DefaultTaskProgressStatus progressStatus, int taskProgressInterval) { super(obsClient, bucketName, progressStatus, listener, taskProgressInterval); @@ -63,20 +65,20 @@ public void setCallback(TaskCallback private void restoreObjects() { try { - RestoreObjectResult result = obsClient.restoreObjectV2(taskRequest); - progressStatus.succeedTaskIncrement(); + RestoreObjectResult result = this.getObsClient().restoreObjectV2(taskRequest); + this.getProgressStatus().succeedTaskIncrement(); callback.onSuccess(result); } catch (ObsException e) { - progressStatus.failTaskIncrement(); + this.getProgressStatus().failTaskIncrement(); callback.onException(e, taskRequest); } - progressStatus.execTaskIncrement(); - if (progressListener != null) { - if (progressStatus.getExecTaskNum() % this.taskProgressInterval == 0) { - progressListener.progressChanged(progressStatus); + this.getProgressStatus().execTaskIncrement(); + if (this.getProgressListener() != null) { + if (this.getProgressStatus().getExecTaskNum() % this.getTaskProgressInterval() == 0) { + this.getProgressListener().progressChanged(this.getProgressStatus()); } - if (progressStatus.getExecTaskNum() == progressStatus.getTotalTaskNum()) { - progressListener.progressChanged(progressStatus); + if (this.getProgressStatus().getExecTaskNum() == this.getProgressStatus().getTotalTaskNum()) { + this.getProgressListener().progressChanged(this.getProgressStatus()); } } } diff --git a/app/src/main/java/com/obs/services/internal/task/ResumableUploadTask.java b/app/src/main/java/com/obs/services/internal/task/ResumableUploadTask.java index 7762566..da19c6a 100644 --- a/app/src/main/java/com/obs/services/internal/task/ResumableUploadTask.java +++ b/app/src/main/java/com/obs/services/internal/task/ResumableUploadTask.java @@ -11,9 +11,10 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.task; -import com.obs.services.ObsClient; +import com.obs.services.AbstractClient; import com.obs.services.exception.ObsException; import com.obs.services.model.CompleteMultipartUploadResult; import com.obs.services.model.ProgressStatus; @@ -23,126 +24,59 @@ import com.obs.services.model.UploadFileRequest; import com.obs.services.model.UploadObjectsProgressListener; -public class ResumableUploadTask implements Runnable { - - protected ObsClient obsClient; - - protected String bucketName; - - private UploadObjectsProgressListener progressListener; - - private int taskProgressInterval; - - private UploadFileRequest taskRequest; - - private TaskCallback callback; +public class ResumableUploadTask extends AbstractPutTask { + private UploadFileRequest taskRequest; - private UploadTaskProgressStatus taskStatus; - - public ResumableUploadTask(ObsClient obsClient, String bucketName, UploadFileRequest taskRequest, - TaskCallback callback, UploadObjectsProgressListener progressListener, - UploadTaskProgressStatus progressStatus, int taskProgressInterval) { - this.obsClient = obsClient; - this.bucketName = bucketName; + public ResumableUploadTask(AbstractClient obsClient, String bucketName, UploadFileRequest taskRequest, + TaskCallback callback, + UploadObjectsProgressListener progressListener, UploadTaskProgressStatus progressStatus, + int taskProgressInterval) { + super(obsClient, bucketName, callback, progressListener, progressStatus, taskProgressInterval); + this.taskRequest = taskRequest; - this.callback = callback; - this.progressListener = progressListener; - this.taskStatus = progressStatus; - this.taskProgressInterval = taskProgressInterval; - } - - public ObsClient getObsClient() { - return obsClient; - } - - public void setObsClient(ObsClient obsClient) { - this.obsClient = obsClient; - } - - public String getBucketName() { - return bucketName; } - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } - - public UploadObjectsProgressListener getUploadObjectsProgressListener() { - return progressListener; - } - - public void setUploadObjectsProgressListener(UploadObjectsProgressListener progressListener) { - this.progressListener = progressListener; + public UploadFileRequest getTaskRequest() { + return taskRequest; } - public int getTaskProgressInterval() { - return taskProgressInterval; - } - - public void setTaskProgressInterval(int taskProgressInterval) { - this.taskProgressInterval = taskProgressInterval; + public void setTaskRequest(UploadFileRequest taskRequest) { + this.taskRequest = taskRequest; } - public UploadFileRequest getTaskRequest() { - return taskRequest; - } - - - public void setTaskRequest(UploadFileRequest taskRequest) { - this.taskRequest = taskRequest; - } - - - public TaskCallback getCallback() { - return callback; - } - - - public void setCallback(TaskCallback callback) { - this.callback = callback; - } - - public UploadTaskProgressStatus getTaskStatus() { - return taskStatus; - } - - public void setTaskStatus(UploadTaskProgressStatus taskStatus) { - this.taskStatus = taskStatus; - } - - private void ResumableUpload() { - try { - CompleteMultipartUploadResult result = obsClient.uploadFile(taskRequest); - taskStatus.succeedTaskIncrement(); - PutObjectResult ret = new PutObjectResult(result.getBucketName(), result.getObjectKey() - , result.getEtag(), result.getVersionId(), result.getObjectUrl(), result.getResponseHeaders(), result.getStatusCode()); - callback.onSuccess(ret); + private void resumableUpload() { + try { + CompleteMultipartUploadResult result = this.getObsClient().uploadFile(taskRequest); + this.getTaskStatus().succeedTaskIncrement(); + PutObjectResult ret = new PutObjectResult(result.getBucketName(), result.getObjectKey(), result.getEtag(), + result.getVersionId(), result.getObjectUrl(), result.getResponseHeaders(), result.getStatusCode()); + this.getCallback().onSuccess(ret); } catch (ObsException e) { - taskStatus.failTaskIncrement(); - callback.onException(e, taskRequest); + this.getTaskStatus().failTaskIncrement(); + this.getCallback().onException(e, taskRequest); } finally { - taskStatus.execTaskIncrement(); - if (progressListener != null) { - if (taskStatus.getExecTaskNum() % this.taskProgressInterval == 0) { - progressListener.progressChanged(taskStatus); + this.getTaskStatus().execTaskIncrement(); + if (this.getProgressListener() != null) { + if (this.getTaskStatus().getExecTaskNum() % this.getTaskProgressInterval() == 0) { + this.getProgressListener().progressChanged(this.getTaskStatus()); } - if (taskStatus.getExecTaskNum() == taskStatus.getTotalTaskNum()) { - progressListener.progressChanged(taskStatus); + if (this.getTaskStatus().getExecTaskNum() == this.getTaskStatus().getTotalTaskNum()) { + this.getProgressListener().progressChanged(this.getTaskStatus()); } } - - final String key = taskRequest.getObjectKey(); - ProgressStatus status = taskStatus.getTaskStatus(key); - if(status != null) { - taskStatus.addEndingTaskSize(status.getTransferredBytes()); - } - taskStatus.removeTaskTable(key); + + final String key = taskRequest.getObjectKey(); + ProgressStatus status = this.getTaskStatus().getTaskStatus(key); + if (status != null) { + this.getTaskStatus().addEndingTaskSize(status.getTransferredBytes()); + } + this.getTaskStatus().removeTaskTable(key); } - } + } - @Override - public void run() { - ResumableUpload(); - } + @Override + public void run() { + resumableUpload(); + } } diff --git a/app/src/main/java/com/obs/services/internal/task/UploadTaskProgressStatus.java b/app/src/main/java/com/obs/services/internal/task/UploadTaskProgressStatus.java index b09275e..54114d6 100644 --- a/app/src/main/java/com/obs/services/internal/task/UploadTaskProgressStatus.java +++ b/app/src/main/java/com/obs/services/internal/task/UploadTaskProgressStatus.java @@ -11,205 +11,155 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.task; import java.util.Date; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import com.obs.services.internal.utils.ServiceUtils; import com.obs.services.model.ProgressStatus; import com.obs.services.model.UploadProgressStatus; +public class UploadTaskProgressStatus extends DefaultTaskProgressStatus implements UploadProgressStatus { -public class UploadTaskProgressStatus implements UploadProgressStatus{ - - private final long progressInterval; - private final Date startDate; + private final long progressInterval; + private final Date startDate; private ConcurrentHashMap taskTable = new ConcurrentHashMap(); private AtomicLong totalSize = new AtomicLong(); private AtomicLong totalMilliseconds = new AtomicLong(); private AtomicLong endingTaskSize = new AtomicLong(); - private AtomicInteger execTaskNum = new AtomicInteger(); - private AtomicInteger succeedTaskNum = new AtomicInteger(); - private AtomicInteger failTaskNum = new AtomicInteger(); - private AtomicInteger totalTaskNum = new AtomicInteger(); private AtomicLong taskTagSize = new AtomicLong(); public UploadTaskProgressStatus(long progressInterval, Date startDate) { - this.progressInterval = progressInterval; - this.startDate = ServiceUtils.cloneDateIgnoreNull(startDate); - } - - public void execTaskIncrement() { - execTaskNum.incrementAndGet(); + this.progressInterval = progressInterval; + this.startDate = ServiceUtils.cloneDateIgnoreNull(startDate); } - public void succeedTaskIncrement() { - succeedTaskNum.incrementAndGet(); - } - - public void failTaskIncrement() { - failTaskNum.incrementAndGet(); - } - public void setTaskTagSize(long taskTagSize) { - this.taskTagSize = new AtomicLong(taskTagSize); + this.taskTagSize = new AtomicLong(taskTagSize); } - + public long getTaskTagSize() { - return taskTagSize.get(); + return taskTagSize.get(); } - public void setTotalTaskNum(int totalNum) { - this.totalTaskNum.set(totalNum); - } - public boolean isRefreshprogress() { - if(this.progressInterval <= 0) { - return false; - } - - long transferredSize = this.getTransferredSize(); - long taskTagSize = this.getTaskTagSize(); - System.out.println("[UploadTaskProgressStatus]transferredSize:" + transferredSize + ", taskTagSize:" +taskTagSize + ", progressInterval" + progressInterval); - if(transferredSize - taskTagSize >= progressInterval) { - this.setTaskTagSize(transferredSize); - return true; - }else { - return false; - } + if (this.progressInterval <= 0) { + return false; + } + + long transferredSize = this.getTransferredSize(); + long taskSize = this.getTaskTagSize(); + if (transferredSize - taskSize >= progressInterval) { + this.setTaskTagSize(transferredSize); + return true; + } else { + return false; + } } @Override - public int getExecPercentage() { - if (totalTaskNum.get() <= 0) { - return -1; + public long getTotalSize() { + // 任务未加载完毕时,返回-1 + if (getTotalTaskNum() <= 0) { + return -1L; } else { - return execTaskNum.get() * 100 / totalTaskNum.get(); + return totalSize.get(); } } @Override - public int getTotalTaskNum() { - return totalTaskNum.get(); + public long getTransferredSize() { + long transferredSize = this.endingTaskSize.get(); + ConcurrentHashMap taskStatusTable = new ConcurrentHashMap( + this.taskTable); + for (Entry entry : taskStatusTable.entrySet()) { + transferredSize += entry.getValue().getTransferredBytes(); + } + return transferredSize; } @Override - public int getExecTaskNum() { - return execTaskNum.get(); + public double getInstantaneousSpeed() { + if (this.taskTable != null) { + long instantaneousSpeed = 0; + for (Entry entry : this.taskTable.entrySet()) { + instantaneousSpeed += entry.getValue().getInstantaneousSpeed(); + } + return instantaneousSpeed; + } else { + return -1d; + } } @Override - public int getSucceedTaskNum() { - return succeedTaskNum.get(); + public double getAverageSpeed() { + if (this.totalMilliseconds.get() <= 0) { + return -1d; + } + return this.getTransferredSize() * 1000.0d / this.totalMilliseconds.get(); } @Override - public int getFailTaskNum() { - return failTaskNum.get(); - } - - @Override - public long getTotalSize() { - //任务未加载完毕时,返回-1 - if(getTotalTaskNum() <= 0) { - return -1L; - }else { - return totalSize.get(); - } - } - - @Override - public long getTransferredSize() { - long transferredSize = this.endingTaskSize.get(); - ConcurrentHashMap taskStatusTable = new ConcurrentHashMap(this.taskTable); - for(Entry entry: taskStatusTable.entrySet()){ - transferredSize += entry.getValue().getTransferredBytes(); - } - return transferredSize; - } - - @Override - public double getInstantaneousSpeed() { - if(this.taskTable != null) { - long instantaneousSpeed = 0; - for(Entry entry: this.taskTable.entrySet()){ - instantaneousSpeed += entry.getValue().getInstantaneousSpeed(); - } - return instantaneousSpeed; - }else { - return -1d; - } - } - - @Override - public double getAverageSpeed() { - if(this.totalMilliseconds.get() <= 0) { - return -1d; - } - return this.getTransferredSize() * 1000.0d / this.totalMilliseconds.get(); - } - - @Override - public ConcurrentHashMap getTaskTable() { - ConcurrentHashMap taskStatusTable = new ConcurrentHashMap(this.taskTable); - return taskStatusTable; - } - - @Override - public ProgressStatus getTaskStatus(String key) { - return this.taskTable.get(key); - } - - - public void setTotalSize(long totalSize) { - this.totalSize = new AtomicLong(totalSize); - } - - public void addTotalSize(long bytes) { - this.totalSize.addAndGet(bytes); - } - - public void setTaskTable(ConcurrentHashMap taskTable) { - this.taskTable = taskTable; - } - - public void putTaskTable(String key, ProgressStatus status) { - this.taskTable.put(key, status); - } - - public void removeTaskTable(String key) { - if(null == this.taskTable) { - return; - } - this.taskTable.remove(key); - } - - public long getTotalMilliseconds() { - return totalMilliseconds.get(); - } - - public void setTotalMilliseconds(long totalMilliseconds) { - this.totalMilliseconds = new AtomicLong(totalMilliseconds); - } - - public long getEndingTaskSize() { - return endingTaskSize.get(); - } - - public void setEndingTaskSize(long endingTaskSize) { - this.endingTaskSize = new AtomicLong(endingTaskSize); - } - - public void addEndingTaskSize(long bytes) { - this.endingTaskSize.addAndGet(bytes); - } - - public Date getStartDate() { - return ServiceUtils.cloneDateIgnoreNull(this.startDate); - } + public ConcurrentHashMap getTaskTable() { + ConcurrentHashMap taskStatusTable = new ConcurrentHashMap( + this.taskTable); + return taskStatusTable; + } + + @Override + public ProgressStatus getTaskStatus(String key) { + return this.taskTable.get(key); + } + + public void setTotalSize(long totalSize) { + this.totalSize = new AtomicLong(totalSize); + } + + public void addTotalSize(long bytes) { + this.totalSize.addAndGet(bytes); + } + + public void setTaskTable(ConcurrentHashMap taskTable) { + this.taskTable = taskTable; + } + + public void putTaskTable(String key, ProgressStatus status) { + this.taskTable.put(key, status); + } + + public void removeTaskTable(String key) { + if (null == this.taskTable) { + return; + } + this.taskTable.remove(key); + } + + public long getTotalMilliseconds() { + return totalMilliseconds.get(); + } + + public void setTotalMilliseconds(long totalMilliseconds) { + this.totalMilliseconds = new AtomicLong(totalMilliseconds); + } + + public long getEndingTaskSize() { + return endingTaskSize.get(); + } + + public void setEndingTaskSize(long endingTaskSize) { + this.endingTaskSize = new AtomicLong(endingTaskSize); + } + + public void addEndingTaskSize(long bytes) { + this.endingTaskSize.addAndGet(bytes); + } + + public Date getStartDate() { + return ServiceUtils.cloneDateIgnoreNull(this.startDate); + } } diff --git a/app/src/main/java/com/obs/services/internal/trans/NewTransResult.java b/app/src/main/java/com/obs/services/internal/trans/NewTransResult.java new file mode 100644 index 0000000..ed8478d --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/trans/NewTransResult.java @@ -0,0 +1,99 @@ +package com.obs.services.internal.trans; + +import com.obs.services.model.HttpMethodEnum; +import okhttp3.RequestBody; + +import java.util.HashMap; +import java.util.Map; + +public class NewTransResult { + private Map headers; + private Map userHeaders; + private Map params; + private RequestBody body; + private String bucketName; + private String objectKey; + private HttpMethodEnum httpMethod; + private boolean encodeHeaders = false; + + public NewTransResult() { + } + + public NewTransResult(Map headers) { + this.headers = headers; + this.params = new HashMap<>(); + } + + public Map getHeaders() { + if (this.headers == null) { + headers = new HashMap<>(); + } + return this.headers; + } + + public Map getParams() { + if (this.params == null) { + params = new HashMap<>(); + } + return this.params; + } + + public Map getUserHeaders() { + if (this.userHeaders == null) { + userHeaders = new HashMap<>(); + } + return this.userHeaders; + } + + public void setUserHeaders(Map userHeaders) { + this.userHeaders = userHeaders; + } + + public void setParams(Map params) { + this.params = params; + } + + public void setHeaders(Map headers) { + this.headers = headers; + } + + public void setBucketName(String bucketName) { + this.bucketName = bucketName; + } + + public void setObjectKey(String objectKey) { + this.objectKey = objectKey; + } + + public String getBucketName() { + return bucketName; + } + + public String getObjectKey() { + return objectKey; + } + + public void setBody(RequestBody body) { + this.body = body; + } + + public void setHttpMethod(HttpMethodEnum httpMethod) { + this.httpMethod = httpMethod; + } + + public HttpMethodEnum getHttpMethod() { + return httpMethod; + } + + public RequestBody getBody() { + return body; + } + + public void setIsEncodeHeaders(boolean encodeHeaders) { + this.encodeHeaders = encodeHeaders; + } + + public boolean isEncodeHeaders() { + return encodeHeaders; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/obs/services/internal/utils/AbstractAuthentication.java b/app/src/main/java/com/obs/services/internal/utils/AbstractAuthentication.java index 71a1cc9..f72fc4b 100644 --- a/app/src/main/java/com/obs/services/internal/utils/AbstractAuthentication.java +++ b/app/src/main/java/com/obs/services/internal/utils/AbstractAuthentication.java @@ -1,9 +1,4 @@ /** - * JetS3t : Java S3 Toolkit - * Project hosted at http://bitbucket.org/jmurty/jets3t/ - * - * Copyright 2006-2010 James Murty - * * Copyright 2019 Huawei Technologies Co.,Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the @@ -16,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; import java.io.UnsupportedEncodingException; @@ -32,171 +28,158 @@ import com.obs.services.internal.ServiceException; import com.obs.services.internal.security.BasicSecurityKey; -public abstract class AbstractAuthentication{ - - protected abstract IHeaders getIHeaders(); - - protected abstract String getAuthPrefix(); - - public static String caculateSignature(String stringToSign, String sk) throws ServiceException { - return ServiceUtils.signWithHmacSha1(sk, stringToSign); - } - - public final IAuthentication makeAuthorizationString(String method, Map headers, String fullUrl, List serviceResourceParameterNames,BasicSecurityKey securityKey) throws ServiceException{ +public abstract class AbstractAuthentication { + + protected abstract IHeaders getIHeaders(); + + protected abstract String getAuthPrefix(); + + public static String calculateSignature(String stringToSign, String sk) throws ServiceException { + return ServiceUtils.signWithHmacSha1(sk, stringToSign); + } + + public final IAuthentication makeAuthorizationString(String method, Map headers, String fullUrl, + List serviceResourceParameterNames, BasicSecurityKey securityKey) throws ServiceException { String canonicalString; - try - { - canonicalString = this.makeServiceCanonicalString(method, fullUrl, - headers, null, serviceResourceParameterNames); - } - catch (UnsupportedEncodingException e) - { + try { + canonicalString = this.makeServiceCanonicalString(method, fullUrl, headers, null, + serviceResourceParameterNames); + } catch (UnsupportedEncodingException e) { throw new ServiceException(e); } String accessKey = securityKey.getAccessKey(); String secretKey = securityKey.getSecretKey(); - String signedCanonical = AbstractAuthentication.caculateSignature(canonicalString, secretKey); - String auth = new StringBuilder(this.getAuthPrefix()).append(" ").append(accessKey).append(":").append(signedCanonical).toString(); + String signedCanonical = AbstractAuthentication.calculateSignature(canonicalString, secretKey); + String auth = this.getAuthPrefix() + " " + accessKey + ":" + signedCanonical; return new DefaultAuthentication(canonicalString, canonicalString, auth); } - + + public final String makeServiceCanonicalString(String method, String resource, Map headersMap, + String expires, List serviceResourceParameterNames) throws UnsupportedEncodingException { + String headerPrefix = this.getIHeaders().headerPrefix(); + SortedMap interestingHeaders = transHeaders(headersMap, headerPrefix, expires); + StringBuilder canonicalStringBuf = transCanonicalString(method, headerPrefix, interestingHeaders); + + int queryIndex = resource.indexOf('?'); + if (queryIndex < 0) { + canonicalStringBuf.append(resource); + } else { + canonicalStringBuf.append(resource, 0, queryIndex); + + SortedMap sortedResourceParams = new TreeMap(); + + String query = resource.substring(queryIndex + 1); + for (String paramPair : query.split("&")) { + String[] paramNameValue = paramPair.split("="); + String name = URLDecoder.decode(paramNameValue[0], Constants.DEFAULT_ENCODING); + String value = null; + if (paramNameValue.length > 1) { + value = URLDecoder.decode(paramNameValue[1], Constants.DEFAULT_ENCODING); + } + if (serviceResourceParameterNames.contains(name.toLowerCase()) + || name.toLowerCase().startsWith(headerPrefix)) { + sortedResourceParams.put(name, value); + } + } + + if (sortedResourceParams.size() > 0) { + canonicalStringBuf.append("?"); + } + boolean addedParam = false; + for (Map.Entry entry : sortedResourceParams.entrySet()) { + if (addedParam) { + canonicalStringBuf.append("&"); + } + canonicalStringBuf.append(entry.getKey()); + if (null != entry.getValue()) { + canonicalStringBuf.append("=").append(entry.getValue()); + } + addedParam = true; + } + } + + return canonicalStringBuf.toString(); + } + @SuppressWarnings("unchecked") - public final String makeServiceCanonicalString(String method, String resource, Map headersMap, String expires, List serviceResourceParameterNames) - throws UnsupportedEncodingException - { + private StringBuilder transCanonicalString(String method, String headerPrefix, + SortedMap interestingHeaders) { StringBuilder canonicalStringBuf = new StringBuilder(); canonicalStringBuf.append(method).append("\n"); + String headerMetaPrefix = this.getIHeaders().headerMetaPrefix(); + + for (Map.Entry entry : interestingHeaders.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + + if (value instanceof List) { + value = ServiceUtils.join((List) value, ",", true); + } else if (value == null) { + value = ""; + } + + if (key.startsWith(headerMetaPrefix)) { + canonicalStringBuf.append(key).append(':').append(value.toString().trim()); + } else if (key.startsWith(headerPrefix)) { + canonicalStringBuf.append(key).append(':').append(value); + } else { + canonicalStringBuf.append(value); + } + canonicalStringBuf.append("\n"); + } + return canonicalStringBuf; + } + + @SuppressWarnings("unchecked") + private SortedMap transHeaders(Map headersMap, + String headerPrefix, String expires) { String dateHeader = Constants.CommonHeaders.DATE.toLowerCase(); String contentTypeHeader = Constants.CommonHeaders.CONTENT_TYPE.toLowerCase(); String contentMd5Header = Constants.CommonHeaders.CONTENT_MD5.toLowerCase(); - String headerPrefix = this.getIHeaders().headerPrefix(); - String headerMetaPrefix = this.getIHeaders().headerMetaPrefix(); SortedMap interestingHeaders = new TreeMap(); - if (headersMap != null && headersMap.size() > 0) - { - for (Map.Entry entry : headersMap.entrySet()) - { + if (null != headersMap) { + for (Map.Entry entry : headersMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); - - if (key == null) - { + + if (null == key) { continue; } + String lk = key.toLowerCase(Locale.getDefault()); - - if (lk.equals(contentTypeHeader) || lk.equals(contentMd5Header) || lk.equals(dateHeader)) - { + + if (lk.equals(contentTypeHeader) || lk.equals(contentMd5Header) || lk.equals(dateHeader)) { interestingHeaders.put(lk, value); - }else if(lk.startsWith(headerPrefix)) { - List values; - if(interestingHeaders.containsKey(lk)) { - values = (List)interestingHeaders.get(lk); - }else { - values = new ArrayList(); - interestingHeaders.put(lk, values); - } - values.add(value); + } else if (lk.startsWith(headerPrefix)) { + List values; + if (interestingHeaders.containsKey(lk)) { + values = (List) interestingHeaders.get(lk); + } else { + values = new ArrayList<>(); + interestingHeaders.put(lk, values); + } + values.add(value); } } } - if (interestingHeaders.containsKey(this.getIHeaders().dateHeader())) - { + if (interestingHeaders.containsKey(this.getIHeaders().dateHeader())) { interestingHeaders.put(dateHeader, ""); } - - if (expires != null) - { + + if (expires != null) { interestingHeaders.put(dateHeader, expires); } - - if (!interestingHeaders.containsKey(contentTypeHeader)) - { + + if (!interestingHeaders.containsKey(contentTypeHeader)) { interestingHeaders.put(contentTypeHeader, ""); } - if (!interestingHeaders.containsKey(contentMd5Header)) - { + if (!interestingHeaders.containsKey(contentMd5Header)) { interestingHeaders.put(contentMd5Header, ""); } - for (Map.Entry entry : interestingHeaders.entrySet()) - { - String key = entry.getKey(); - Object value = entry.getValue(); - - if(value instanceof List) { - value = ServiceUtils.join((List)value, ",", true); - }else if(value == null) { - value = ""; - } - - if(key.startsWith(headerMetaPrefix)) { - canonicalStringBuf.append(key).append(':').append(value.toString().trim()); - }else if (key.startsWith(headerPrefix)) - { - canonicalStringBuf.append(key).append(':').append(value); - } - else - { - canonicalStringBuf.append(value); - } - canonicalStringBuf.append("\n"); - } - - int queryIndex = resource.indexOf('?'); - if (queryIndex == -1) - { - canonicalStringBuf.append(resource); - } - else - { - canonicalStringBuf.append(resource.substring(0, queryIndex)); - } - - if (queryIndex >= 0) - { - SortedMap sortedResourceParams = new TreeMap(); - - // Parse parameters from resource string - String query = resource.substring(queryIndex + 1); - for (String paramPair : query.split("&")) - { - String[] paramNameValue = paramPair.split("="); - String name = URLDecoder.decode(paramNameValue[0], Constants.DEFAULT_ENCODING); - String value = null; - if (paramNameValue.length > 1) - { - value = URLDecoder.decode(paramNameValue[1], Constants.DEFAULT_ENCODING); - } - if (serviceResourceParameterNames.contains(name.toLowerCase()) || name.toLowerCase().startsWith(headerPrefix)) - { - sortedResourceParams.put(name, value); - } - } - - if (sortedResourceParams.size() > 0) - { - canonicalStringBuf.append("?"); - } - boolean addedParam = false; - for (Map.Entry entry : sortedResourceParams.entrySet()) - { - if (addedParam) - { - canonicalStringBuf.append("&"); - } - canonicalStringBuf.append(entry.getKey()); - if (entry.getValue() != null) - { - canonicalStringBuf.append("=").append(entry.getValue()); - } - addedParam = true; - } - } - - return canonicalStringBuf.toString(); + return interestingHeaders; } } diff --git a/app/src/main/java/com/obs/services/internal/utils/AccessLoggerUtils.java b/app/src/main/java/com/obs/services/internal/utils/AccessLoggerUtils.java index 789e2bf..7b99e1c 100644 --- a/app/src/main/java/com/obs/services/internal/utils/AccessLoggerUtils.java +++ b/app/src/main/java/com/obs/services/internal/utils/AccessLoggerUtils.java @@ -11,88 +11,91 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; -import com.obs.log.*; +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; import java.lang.ref.SoftReference; import java.text.SimpleDateFormat; import java.util.Date; public class AccessLoggerUtils { - private static final ILogger accessLog = LoggerBuilder.getLogger("com.obs.log.AccessLogger"); - private static final ThreadLocal threadLocalLog = new ThreadLocal(); - private static final ThreadLocal> simpleDateFormateHolder = new ThreadLocal>(); - private static final int INDEX = 5; - public static volatile boolean ACCESSLOG_ENABLED = true; + private static final ILogger ACCESS_LOG = LoggerBuilder.getLogger("com.obs.log.AccessLogger"); + private static final ThreadLocal THREADLOCAL_LOG = new ThreadLocal(); + private static final ThreadLocal> DATE_FORMAT_HOLDER = + new ThreadLocal>(); + private static final int INDEX = 5; + public static volatile boolean accesslogEnabled = true; - private static String getLogPrefix() { - if (!ACCESSLOG_ENABLED) { - return ""; - } - StackTraceElement[] stacktraces = Thread.currentThread().getStackTrace(); - StackTraceElement stacktrace = null; - if (stacktraces.length > INDEX) { - stacktrace = stacktraces[INDEX]; - } else { - stacktrace = stacktraces[stacktraces.length - 1]; - } + private static String getLogPrefix() { + if (!accesslogEnabled) { + return ""; + } + StackTraceElement[] stacktraces = Thread.currentThread().getStackTrace(); + StackTraceElement stacktrace = null; + if (stacktraces.length > INDEX) { + stacktrace = stacktraces[INDEX]; + } else { + stacktrace = stacktraces[stacktraces.length - 1]; + } - return new StringBuilder().append(stacktrace.getClassName()).append("|").append(stacktrace.getMethodName()) - .append("|").append(stacktrace.getLineNumber()).append("|").toString(); - } + return new StringBuilder().append(stacktrace.getClassName()).append("|").append(stacktrace.getMethodName()) + .append("|").append(stacktrace.getLineNumber()).append("|").toString(); + } - private static StringBuilder getLog() { - StringBuilder logSb = threadLocalLog.get(); - if (logSb == null) { - logSb = new StringBuilder(); - threadLocalLog.set(logSb); - } - return logSb; - } + private static StringBuilder getLog() { + StringBuilder logSb = THREADLOCAL_LOG.get(); + if (logSb == null) { + logSb = new StringBuilder(); + THREADLOCAL_LOG.set(logSb); + } + return logSb; + } - public static void appendLog(Object log, String level) { - if (!ACCESSLOG_ENABLED) { - return; - } - Boolean isLog = false; - if ("info".equalsIgnoreCase(level)) { - isLog = accessLog.isInfoEnabled(); - } else if ("debug".equalsIgnoreCase(level)) { - isLog = accessLog.isDebugEnabled(); - } else if ("warn".equalsIgnoreCase(level)) { - isLog = accessLog.isWarnEnabled(); - } else if ("error".equalsIgnoreCase(level)) { - isLog = accessLog.isErrorEnabled(); - } else if ("trace".equalsIgnoreCase(level)) { - isLog = accessLog.isTraceEnabled(); - } - if (isLog) { - StringBuilder sb = new StringBuilder(getFormat().format(new Date())); - sb.append("|").append(AccessLoggerUtils.getLogPrefix()).append(log.toString()).append("\n"); - getLog().append(sb.toString()); - } - } + public static void appendLog(Object log, String level) { + if (!accesslogEnabled) { + return; + } + Boolean isLog = false; + if ("info".equalsIgnoreCase(level)) { + isLog = ACCESS_LOG.isInfoEnabled(); + } else if ("debug".equalsIgnoreCase(level)) { + isLog = ACCESS_LOG.isDebugEnabled(); + } else if ("warn".equalsIgnoreCase(level)) { + isLog = ACCESS_LOG.isWarnEnabled(); + } else if ("error".equalsIgnoreCase(level)) { + isLog = ACCESS_LOG.isErrorEnabled(); + } else if ("trace".equalsIgnoreCase(level)) { + isLog = ACCESS_LOG.isTraceEnabled(); + } + if (isLog) { + StringBuilder sb = new StringBuilder(getFormat().format(new Date())); + sb.append("|").append(AccessLoggerUtils.getLogPrefix()).append(log.toString()).append("\n"); + getLog().append(sb.toString()); + } + } - public static SimpleDateFormat getFormat() { - SoftReference holder = simpleDateFormateHolder.get(); - SimpleDateFormat format; - if (holder == null || ((format = holder.get()) == null)) { - format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); - holder = new SoftReference(format); - simpleDateFormateHolder.set(holder); - } - return format; - } + public static SimpleDateFormat getFormat() { + SoftReference holder = DATE_FORMAT_HOLDER.get(); + SimpleDateFormat format; + if (holder == null || ((format = holder.get()) == null)) { + format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); + holder = new SoftReference(format); + DATE_FORMAT_HOLDER.set(holder); + } + return format; + } - public static void printLog() { - if (!ACCESSLOG_ENABLED) { - return; - } - String message = getLog().toString(); - if (ServiceUtils.isValid(message)) { - accessLog.accessRecord(message); - } - threadLocalLog.remove(); - } + public static void printLog() { + if (!accesslogEnabled) { + return; + } + String message = getLog().toString(); + if (ServiceUtils.isValid(message)) { + ACCESS_LOG.accessRecord(message); + } + THREADLOCAL_LOG.remove(); + } } diff --git a/app/src/main/java/com/obs/services/internal/utils/DefaultAuthentication.java b/app/src/main/java/com/obs/services/internal/utils/DefaultAuthentication.java index d1ede7f..1f00a8d 100644 --- a/app/src/main/java/com/obs/services/internal/utils/DefaultAuthentication.java +++ b/app/src/main/java/com/obs/services/internal/utils/DefaultAuthentication.java @@ -11,35 +11,36 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; -public class DefaultAuthentication implements IAuthentication{ +public class DefaultAuthentication implements IAuthentication { private String stringToSign; - + private String authorization; - + private String canonicalRequest; - - public DefaultAuthentication( String canonicalRequest, String stringToSign, String authorization) { - this.canonicalRequest = canonicalRequest; - this.stringToSign = stringToSign; - this.authorization = authorization; - } - - @Override - public String getCanonicalRequest() { - return this.canonicalRequest; - } - - @Override - public String getAuthorization() { - return this.authorization; - } - - @Override - public String getStringToSign() { - return this.stringToSign; - } + + public DefaultAuthentication(String canonicalRequest, String stringToSign, String authorization) { + this.canonicalRequest = canonicalRequest; + this.stringToSign = stringToSign; + this.authorization = authorization; + } + + @Override + public String getCanonicalRequest() { + return this.canonicalRequest; + } + + @Override + public String getAuthorization() { + return this.authorization; + } + + @Override + public String getStringToSign() { + return this.stringToSign; + } } diff --git a/app/src/main/java/com/obs/services/internal/utils/IAuthentication.java b/app/src/main/java/com/obs/services/internal/utils/IAuthentication.java index eee16db..6190c7d 100644 --- a/app/src/main/java/com/obs/services/internal/utils/IAuthentication.java +++ b/app/src/main/java/com/obs/services/internal/utils/IAuthentication.java @@ -11,10 +11,13 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; public interface IAuthentication { - String getCanonicalRequest(); - String getAuthorization(); - String getStringToSign(); + String getCanonicalRequest(); + + String getAuthorization(); + + String getStringToSign(); } diff --git a/app/src/main/java/com/obs/services/internal/utils/JSONChange.java b/app/src/main/java/com/obs/services/internal/utils/JSONChange.java index 225424d..b1220bb 100644 --- a/app/src/main/java/com/obs/services/internal/utils/JSONChange.java +++ b/app/src/main/java/com/obs/services/internal/utils/JSONChange.java @@ -11,10 +11,14 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; import java.io.IOException; + +import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -25,88 +29,96 @@ import com.obs.services.internal.ServiceException; public class JSONChange { - /* - * json转换成对象 - * @param:传入对象,json字符串 - * @return:Object - */ - public static Object jsonToObj(Object obj,String jsonStr) throws ServiceException { - MyObjectMapper mapper = ObjectMapperUtil.getInstance(); - try { - obj = mapper.readValue(jsonStr, obj.getClass()); - return obj; - } catch (JsonParseException e) { - throw new ServiceException(" conversion JSON failed ", e); - } catch (JsonMappingException e) { - throw new ServiceException(" conversion JSON failed", e); - } catch (IOException e) { - throw new ServiceException(" conversion JSON failed", e); - } - } - /* - * 对象转换成json - * @param:传入对象 - * @return:json字符串 - */ - public static String objToJson(Object obj) throws ServiceException { - MyObjectMapper mapper = ObjectMapperUtil.getInstance(); - try { - return mapper.writeValueAsString(obj); - } catch (JsonProcessingException e) { - throw new ServiceException("conversion JSON failed", e); - } - } -} + /* + * json转换成对象 + * + * @param:传入对象,json字符串 + * + * @return:Object + */ + public static Object jsonToObj(Object obj, String jsonStr) throws ServiceException { + MyObjectMapper mapper = ObjectMapperUtil.getInstance(); + try { + obj = mapper.readValue(jsonStr, obj.getClass()); + return obj; + } catch (JsonParseException e) { + throw new ServiceException(" conversion JSON failed ", e); + } catch (JsonMappingException e) { + throw new ServiceException(" conversion JSON failed", e); + } catch (IOException e) { + throw new ServiceException(" conversion JSON failed", e); + } + } -class ObjectMapperUtil{ + /* + * 对象转换成json + * + * @param:传入对象 + * + * @return:json字符串 + */ + public static String objToJson(Object obj) throws ServiceException { + MyObjectMapper mapper = ObjectMapperUtil.getInstance(); + try { + return mapper.writeValueAsString(obj); + } catch (JsonProcessingException e) { + throw new ServiceException("conversion JSON failed", e); + } + } + + private static class ObjectMapperUtil { - private ObjectMapperUtil() {} + private ObjectMapperUtil() { + } - private static class ObjectMapperUtilInstance { - private static final MyObjectMapper mapper = new MyObjectMapper(); - } + private static class ObjectMapperUtilInstance { + private static final MyObjectMapper MAPPER = new MyObjectMapper(); + } - public static MyObjectMapper getInstance() { - return ObjectMapperUtilInstance.mapper; + public static MyObjectMapper getInstance() { + return ObjectMapperUtilInstance.MAPPER; + } } -} - -class MyObjectMapper extends ObjectMapper{ - private static final long serialVersionUID = 4563671462132723274L; - // 默认构造函数 - public MyObjectMapper() { - super(); - // 从JSON到java object - // 没有匹配的属性名称时不作失败处理 - this.configure(MapperFeature.AUTO_DETECT_FIELDS, true); - - // 反序列化 - // 禁止遇到空原始类型时抛出异常,用默认值代替。 - this.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false); - this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true); - // 禁止遇到未知(新)属性时报错,支持兼容扩展 - this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - this.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); - this.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); - // 按时间戳格式读取日期 - this.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true); - this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true); - this.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); - // 序列化 - // 禁止序列化空值 - this.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); - this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); - this.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); - this.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true); - // 按时间戳格式生成日期 - this.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, true); - this.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true); - // 不包含空值属性 - this.setSerializationInclusion(Include.NON_EMPTY); - this.setSerializationInclusion(Include.NON_NULL); - // this.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true); - // 是否缩放排列输出,默认false, - this.configure(SerializationFeature.INDENT_OUTPUT, false); - } + private static class MyObjectMapper extends ObjectMapper { + private static final long serialVersionUID = 4563671462132723274L; + + // 默认构造函数 + public MyObjectMapper() { + super(); + // 从JSON到java object + // 没有匹配的属性名称时不作失败处理 + this.configure(MapperFeature.AUTO_DETECT_FIELDS, true); + + // 反序列化 + // 禁止遇到空原始类型时抛出异常,用默认值代替。 + this.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false); + this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true); + // 禁止遇到未知(新)属性时报错,支持兼容扩展 + this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + this.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); + this.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); + // 按时间戳格式读取日期 + this.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true); + this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true); + this.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + + // 序列化 + // 禁止序列化空值 + this.setDefaultPropertyInclusion(JsonInclude.Value.construct(Include.ALWAYS, Include.NON_NULL)); + this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); + this.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); + this.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + // 按时间戳格式生成日期 + this.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, true); + this.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN); + // 不包含空值属性 + this.setSerializationInclusion(Include.NON_EMPTY); + this.setSerializationInclusion(Include.NON_NULL); + // this.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, + // true); + // 是否缩放排列输出,默认false, + this.configure(SerializationFeature.INDENT_OUTPUT, false); + } + } } diff --git a/app/src/main/java/com/obs/services/internal/utils/Mimetypes.java b/app/src/main/java/com/obs/services/internal/utils/Mimetypes.java index 57d685a..4eee4fe 100644 --- a/app/src/main/java/com/obs/services/internal/utils/Mimetypes.java +++ b/app/src/main/java/com/obs/services/internal/utils/Mimetypes.java @@ -1,10 +1,4 @@ /** - * - * JetS3t : Java S3 Toolkit - * Project hosted at http://bitbucket.org/jmurty/jets3t/ - * - * Copyright 2006-2010 James Murty - * * Copyright 2019 Huawei Technologies Co.,Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the @@ -17,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; import java.io.File; @@ -24,443 +19,491 @@ import java.util.Locale; import java.util.Map; -public class Mimetypes -{ +public class Mimetypes { public static final String MIMETYPE_XML = "application/xml"; - + public static final String MIMETYPE_TEXT_XML = "text/xml"; - + public static final String MIMETYPE_TEXT_PLAIN = "text/plain"; - + public static final String MIMETYPE_HTML = "text/html"; - + public static final String MIMETYPE_OCTET_STREAM = "application/octet-stream"; - + public static final String MIMETYPE_BINARY_OCTET_STREAM = "binary/octet-stream"; - + public static final String MIMETYPE_GZIP = "application/x-gzip"; - + public static final String MIMETYPE_JSON = "application/json"; - + private final Map extensionToMimetypeMap; - - private Mimetypes() - { + + private Mimetypes() { extensionToMimetypeMap = new HashMap(); + addApplicationMimetype(extensionToMimetypeMap); + addApplicationXMimetypeOne(extensionToMimetypeMap); + addApplicationXMimetypeTwo(extensionToMimetypeMap); + addApplicationXMimetypeThree(extensionToMimetypeMap); + addApplicationXMimetypeFour(extensionToMimetypeMap); + + addApplicationVNDMimetype(extensionToMimetypeMap); + addDrawingMimetype(extensionToMimetypeMap); + addXMLTextMimetype(extensionToMimetypeMap); + addOtherTextMimetype(extensionToMimetypeMap); + + addJavaMimetype(extensionToMimetypeMap); + addMessageMimetype(extensionToMimetypeMap); + addVideoMimetype(extensionToMimetypeMap); + addImageMimetype(extensionToMimetypeMap); + addAudioMimetype(extensionToMimetypeMap); + } + + private static void addApplicationMimetype(Map extensionToMimetypeMap) { + extensionToMimetypeMap.put("mdb", "application/msaccess"); + extensionToMimetypeMap.put("ogx", "application/ogg"); + extensionToMimetypeMap.put("p10", "application/pkcs10"); + extensionToMimetypeMap.put("p7c", "application/pkcs7-mime"); + extensionToMimetypeMap.put("p7m", "application/pkcs7-mime"); + extensionToMimetypeMap.put("p7s", "application/pkcs7-signature"); + extensionToMimetypeMap.put("cu", "application/cu-seeme"); + extensionToMimetypeMap.put("gz", "application/gzip"); + extensionToMimetypeMap.put("zip", "application/zip"); + extensionToMimetypeMap.put("hta", "application/hta"); + extensionToMimetypeMap.put("ai", "application/postscript"); + extensionToMimetypeMap.put("ps", "application/postscript"); + extensionToMimetypeMap.put("atom", "application/atom+xml"); + extensionToMimetypeMap.put("crl", "application/pkix-crl"); + extensionToMimetypeMap.put("doc", "application/msword"); + extensionToMimetypeMap.put("dot", "application/msword"); + extensionToMimetypeMap.put("wiz", "application/msword"); + extensionToMimetypeMap.put("eps", "application/postscript"); + extensionToMimetypeMap.put("epub", "application/epub+zip"); + extensionToMimetypeMap.put("fif", "application/fractals"); + extensionToMimetypeMap.put("hqx", "application/mac-binhex40"); + extensionToMimetypeMap.put("jar", "application/java-archive"); + extensionToMimetypeMap.put("json", "application/json"); + extensionToMimetypeMap.put("prf", "application/pics-rules"); + extensionToMimetypeMap.put("sdp", "application/sdp"); + extensionToMimetypeMap.put("smi", "application/smil"); + extensionToMimetypeMap.put("smil", "application/smil"); + extensionToMimetypeMap.put("spl", "application/futuresplash"); + extensionToMimetypeMap.put("ssm", "application/streamingmedia"); + extensionToMimetypeMap.put("pdf", "application/pdf"); + extensionToMimetypeMap.put("rat", "application/rat-file"); + } + + private static void addApplicationXMimetypeOne(Map extensionToMimetypeMap) { + extensionToMimetypeMap.put("cer", "application/x-x509-ca-cert"); + extensionToMimetypeMap.put("crt", "application/x-x509-ca-cert"); + extensionToMimetypeMap.put("der", "application/x-x509-ca-cert"); + extensionToMimetypeMap.put("7z", "application/x-7z-compressed"); + extensionToMimetypeMap.put("deb", "application/x-debian-package"); + extensionToMimetypeMap.put("wmz", "application/x-ms-wmz"); + extensionToMimetypeMap.put("woff", "application/x-font-woff"); + extensionToMimetypeMap.put("xap", "application/x-silverlight-app"); + extensionToMimetypeMap.put("man", "application/x-troff-man"); + extensionToMimetypeMap.put("mfp", "application/x-shockwave-flash"); + extensionToMimetypeMap.put("p7b", "application/x-pkcs7-certificates"); + extensionToMimetypeMap.put("spc", "application/x-pkcs7-certificates"); + extensionToMimetypeMap.put("p7r", "application/x-pkcs7-certreqresp"); + extensionToMimetypeMap.put("ins", "application/x-internet-signup"); + extensionToMimetypeMap.put("iso", "application/x-iso9660-image"); + extensionToMimetypeMap.put("isp", "application/x-internet-signup"); + extensionToMimetypeMap.put("lar", "application/x-laplayer-reg"); + extensionToMimetypeMap.put("rar", "application/x-rar-compressed"); + extensionToMimetypeMap.put("swf", "application/x-shockwave-flash"); + extensionToMimetypeMap.put("ttf", "application/x-font-ttf"); + extensionToMimetypeMap.put("wmd", "application/x-ms-wmd"); + extensionToMimetypeMap.put("exe", "application/x-msdownload"); + extensionToMimetypeMap.put("dll", "application/x-msdownload"); + extensionToMimetypeMap.put("js", "application/x-javascript"); + extensionToMimetypeMap.put("mocha", "application/x-javascript"); + extensionToMimetypeMap.put("ls", "application/x-javascript"); + extensionToMimetypeMap.put("latex", "application/x-latex"); + extensionToMimetypeMap.put("torrent", "application/x-bittorrent"); + extensionToMimetypeMap.put("vpg", "application/x-vpeg005"); extensionToMimetypeMap.put("001", "application/x-001"); extensionToMimetypeMap.put("301", "application/x-301"); - extensionToMimetypeMap.put("323", "text/h323"); - extensionToMimetypeMap.put("7z", "application/x-7z-compressed"); extensionToMimetypeMap.put("906", "application/x-906"); - extensionToMimetypeMap.put("907", "drawing/907"); - extensionToMimetypeMap.put("IVF", "video/x-ivf"); - extensionToMimetypeMap.put("a11", "application/x-a11"); - extensionToMimetypeMap.put("aac", "audio/x-aac"); - extensionToMimetypeMap.put("acp", "audio/x-mei-aac"); - extensionToMimetypeMap.put("ai", "application/postscript"); - extensionToMimetypeMap.put("aif", "audio/aiff"); - extensionToMimetypeMap.put("aifc", "audio/aiff"); - extensionToMimetypeMap.put("aiff", "audio/aiff"); extensionToMimetypeMap.put("anv", "application/x-anv"); - extensionToMimetypeMap.put("apk", "application/vnd.android.package-archive"); - extensionToMimetypeMap.put("asa", "text/asa"); - extensionToMimetypeMap.put("asf", "video/x-ms-asf"); - extensionToMimetypeMap.put("asp", "text/asp"); - extensionToMimetypeMap.put("asx", "video/x-ms-asf"); - extensionToMimetypeMap.put("atom", "application/atom+xml"); - extensionToMimetypeMap.put("au", "audio/basic"); - extensionToMimetypeMap.put("avi", "video/avi"); - extensionToMimetypeMap.put("awf", "application/vnd.adobe.workflow"); - extensionToMimetypeMap.put("biz", "text/xml"); + extensionToMimetypeMap.put("a11", "application/x-a11"); extensionToMimetypeMap.put("bmp", "application/x-bmp"); extensionToMimetypeMap.put("bot", "application/x-bot"); extensionToMimetypeMap.put("bz2", "application/x-bzip2"); extensionToMimetypeMap.put("c4t", "application/x-c4t"); extensionToMimetypeMap.put("c90", "application/x-c90"); extensionToMimetypeMap.put("cal", "application/x-cals"); - extensionToMimetypeMap.put("cat", "application/vnd.ms-pki.seccat"); extensionToMimetypeMap.put("cdf", "application/x-netcdf"); extensionToMimetypeMap.put("cdr", "application/x-cdr"); extensionToMimetypeMap.put("cel", "application/x-cel"); - extensionToMimetypeMap.put("cer", "application/x-x509-ca-cert"); + } + + private static void addApplicationXMimetypeTwo(Map extensionToMimetypeMap) { extensionToMimetypeMap.put("cg4", "application/x-g4"); extensionToMimetypeMap.put("cgm", "application/x-cgm"); extensionToMimetypeMap.put("cit", "application/x-cit"); - extensionToMimetypeMap.put("class", "java/*"); - extensionToMimetypeMap.put("cml", "text/xml"); extensionToMimetypeMap.put("cmp", "application/x-cmp"); extensionToMimetypeMap.put("cmx", "application/x-cmx"); extensionToMimetypeMap.put("cot", "application/x-cot"); - extensionToMimetypeMap.put("crl", "application/pkix-crl"); - extensionToMimetypeMap.put("crt", "application/x-x509-ca-cert"); extensionToMimetypeMap.put("csi", "application/x-csi"); - extensionToMimetypeMap.put("css", "text/css"); - extensionToMimetypeMap.put("csv", "text/csv"); - extensionToMimetypeMap.put("cu", "application/cu-seeme"); extensionToMimetypeMap.put("cut", "application/x-cut"); extensionToMimetypeMap.put("dbf", "application/x-dbf"); extensionToMimetypeMap.put("dbm", "application/x-dbm"); extensionToMimetypeMap.put("dbx", "application/x-dbx"); - extensionToMimetypeMap.put("dcd", "text/xml"); extensionToMimetypeMap.put("dcx", "application/x-dcx"); - extensionToMimetypeMap.put("deb", "application/x-debian-package"); - extensionToMimetypeMap.put("der", "application/x-x509-ca-cert"); extensionToMimetypeMap.put("dgn", "application/x-dgn"); extensionToMimetypeMap.put("dib", "application/x-dib"); - extensionToMimetypeMap.put("dll", "application/x-msdownload"); - extensionToMimetypeMap.put("doc", "application/msword"); - extensionToMimetypeMap.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); - extensionToMimetypeMap.put("dot", "application/msword"); extensionToMimetypeMap.put("drw", "application/x-drw"); - extensionToMimetypeMap.put("dtd", "text/xml"); extensionToMimetypeMap.put("dvi", "application/x-dvi"); extensionToMimetypeMap.put("dwf", "application/x-dwf"); extensionToMimetypeMap.put("dwg", "application/x-dwg"); extensionToMimetypeMap.put("dxb", "application/x-dxb"); extensionToMimetypeMap.put("dxf", "application/x-dxf"); - extensionToMimetypeMap.put("edn", "application/vnd.adobe.edn"); extensionToMimetypeMap.put("emf", "application/x-emf"); - extensionToMimetypeMap.put("eml", "message/rfc822"); - extensionToMimetypeMap.put("ent", "text/xml"); - extensionToMimetypeMap.put("eot", "application/vnd.ms-fontobject"); extensionToMimetypeMap.put("epi", "application/x-epi"); - extensionToMimetypeMap.put("eps", "application/postscript"); - extensionToMimetypeMap.put("epub", "application/epub+zip"); extensionToMimetypeMap.put("etd", "application/x-ebx"); - extensionToMimetypeMap.put("etx", "text/x-setext"); - extensionToMimetypeMap.put("exe", "application/x-msdownload"); - extensionToMimetypeMap.put("fax", "image/fax"); - extensionToMimetypeMap.put("fdf", "application/vnd.fdf"); - extensionToMimetypeMap.put("fif", "application/fractals"); - extensionToMimetypeMap.put("flac", "audio/flac"); - extensionToMimetypeMap.put("flv", "video/x-flv"); - extensionToMimetypeMap.put("fo", "text/xml"); extensionToMimetypeMap.put("frm", "application/x-frm"); extensionToMimetypeMap.put("g4", "application/x-g4"); extensionToMimetypeMap.put("gbr", "application/x-gbr"); - extensionToMimetypeMap.put("gif", "image/gif"); extensionToMimetypeMap.put("gl2", "application/x-gl2"); extensionToMimetypeMap.put("gp4", "application/x-gp4"); - extensionToMimetypeMap.put("gz", "application/gzip"); extensionToMimetypeMap.put("hgl", "application/x-hgl"); extensionToMimetypeMap.put("hmr", "application/x-hmr"); extensionToMimetypeMap.put("hpg", "application/x-hpgl"); extensionToMimetypeMap.put("hpl", "application/x-hpl"); - extensionToMimetypeMap.put("hqx", "application/mac-binhex40"); extensionToMimetypeMap.put("hrf", "application/x-hrf"); - extensionToMimetypeMap.put("hta", "application/hta"); - extensionToMimetypeMap.put("htc", "text/x-component"); - extensionToMimetypeMap.put("htm", "text/html"); - extensionToMimetypeMap.put("html", "text/html"); - extensionToMimetypeMap.put("htt", "text/webviewhtml"); - extensionToMimetypeMap.put("htx", "text/html"); extensionToMimetypeMap.put("icb", "application/x-icb"); extensionToMimetypeMap.put("ico", "application/x-ico"); - extensionToMimetypeMap.put("ics", "text/calendar"); extensionToMimetypeMap.put("iff", "application/x-iff"); extensionToMimetypeMap.put("ig4", "application/x-g4"); extensionToMimetypeMap.put("igs", "application/x-igs"); - extensionToMimetypeMap.put("iii", "application/x-iphone"); extensionToMimetypeMap.put("img", "application/x-img"); - extensionToMimetypeMap.put("ini", "text/plain"); - extensionToMimetypeMap.put("ins", "application/x-internet-signup"); - extensionToMimetypeMap.put("ipa", "application/vnd.iphone"); - extensionToMimetypeMap.put("iso", "application/x-iso9660-image"); - extensionToMimetypeMap.put("isp", "application/x-internet-signup"); - extensionToMimetypeMap.put("jar", "application/java-archive"); - extensionToMimetypeMap.put("java", "java/*"); - extensionToMimetypeMap.put("jfif", "image/jpeg"); - extensionToMimetypeMap.put("jpe", "image/jpeg"); - extensionToMimetypeMap.put("jpeg", "image/jpeg"); - extensionToMimetypeMap.put("jpg", "image/jpeg"); - extensionToMimetypeMap.put("js", "application/x-javascript"); - extensionToMimetypeMap.put("json", "application/json"); - extensionToMimetypeMap.put("jsp", "text/html"); - extensionToMimetypeMap.put("la1", "audio/x-liquid-file"); - extensionToMimetypeMap.put("lar", "application/x-laplayer-reg"); - extensionToMimetypeMap.put("latex", "application/x-latex"); - extensionToMimetypeMap.put("lavs", "audio/x-liquid-secure"); + extensionToMimetypeMap.put("iii", "application/x-iphone"); + } + + private static void addApplicationXMimetypeThree(Map extensionToMimetypeMap) { extensionToMimetypeMap.put("lbm", "application/x-lbm"); - extensionToMimetypeMap.put("lmsff", "audio/x-la-lms"); - extensionToMimetypeMap.put("log", "text/plain"); - extensionToMimetypeMap.put("ls", "application/x-javascript"); extensionToMimetypeMap.put("ltr", "application/x-ltr"); - extensionToMimetypeMap.put("m1v", "video/x-mpeg"); - extensionToMimetypeMap.put("m2v", "video/x-mpeg"); - extensionToMimetypeMap.put("m3u", "audio/mpegurl"); - extensionToMimetypeMap.put("m4a", "audio/mp4"); - extensionToMimetypeMap.put("m4e", "video/mpeg4"); - extensionToMimetypeMap.put("m4v", "video/mp4"); - extensionToMimetypeMap.put("mac", "application/x-mac"); - extensionToMimetypeMap.put("man", "application/x-troff-man"); - extensionToMimetypeMap.put("math", "text/xml"); - extensionToMimetypeMap.put("mdb", "application/msaccess"); - extensionToMimetypeMap.put("mfp", "application/x-shockwave-flash"); - extensionToMimetypeMap.put("mht", "message/rfc822"); - extensionToMimetypeMap.put("mhtml", "message/rfc822"); - extensionToMimetypeMap.put("mi", "application/x-mi"); - extensionToMimetypeMap.put("mid", "audio/mid"); - extensionToMimetypeMap.put("midi", "audio/mid"); - extensionToMimetypeMap.put("mil", "application/x-mil"); - extensionToMimetypeMap.put("mml", "text/xml"); - extensionToMimetypeMap.put("mnd", "audio/x-musicnet-download"); - extensionToMimetypeMap.put("mns", "audio/x-musicnet-stream"); - extensionToMimetypeMap.put("mocha", "application/x-javascript"); - extensionToMimetypeMap.put("mov", "video/quicktime"); - extensionToMimetypeMap.put("movie", "video/x-sgi-movie"); - extensionToMimetypeMap.put("mp1", "audio/mp1"); - extensionToMimetypeMap.put("mp2", "audio/mp2"); - extensionToMimetypeMap.put("mp2v", "video/mpeg"); - extensionToMimetypeMap.put("mp3", "audio/mp3"); - extensionToMimetypeMap.put("mp4", "video/mp4"); - extensionToMimetypeMap.put("mp4a", "audio/mp4"); - extensionToMimetypeMap.put("mp4v", "video/mp4"); - extensionToMimetypeMap.put("mpa", "video/x-mpg"); - extensionToMimetypeMap.put("mpd", "application/vnd.ms-project"); - extensionToMimetypeMap.put("mpe", "video/x-mpeg"); - extensionToMimetypeMap.put("mpeg", "video/mpg"); - extensionToMimetypeMap.put("mpg", "video/mpg"); - extensionToMimetypeMap.put("mpg4", "video/mp4"); - extensionToMimetypeMap.put("mpga", "audio/rn-mpeg"); - extensionToMimetypeMap.put("mpp", "application/vnd.ms-project"); - extensionToMimetypeMap.put("mps", "video/x-mpeg"); - extensionToMimetypeMap.put("mpt", "application/vnd.ms-project"); - extensionToMimetypeMap.put("mpv", "video/mpg"); - extensionToMimetypeMap.put("mpv2", "video/mpeg"); - extensionToMimetypeMap.put("mpw", "application/vnd.ms-project"); - extensionToMimetypeMap.put("mpx", "application/vnd.ms-project"); - extensionToMimetypeMap.put("mtx", "text/xml"); - extensionToMimetypeMap.put("mxp", "application/x-mmxp"); - extensionToMimetypeMap.put("net", "image/pnetvue"); - extensionToMimetypeMap.put("nrf", "application/x-nrf"); - extensionToMimetypeMap.put("nws", "message/rfc822"); - extensionToMimetypeMap.put("odc", "text/x-ms-odc"); - extensionToMimetypeMap.put("oga", "audio/ogg"); - extensionToMimetypeMap.put("ogg", "audio/ogg"); - extensionToMimetypeMap.put("ogv", "video/ogg"); - extensionToMimetypeMap.put("ogx", "application/ogg"); - extensionToMimetypeMap.put("out", "application/x-out"); - extensionToMimetypeMap.put("p10", "application/pkcs10"); - extensionToMimetypeMap.put("p12", "application/x-pkcs12"); - extensionToMimetypeMap.put("p7b", "application/x-pkcs7-certificates"); - extensionToMimetypeMap.put("p7c", "application/pkcs7-mime"); - extensionToMimetypeMap.put("p7m", "application/pkcs7-mime"); - extensionToMimetypeMap.put("p7r", "application/x-pkcs7-certreqresp"); - extensionToMimetypeMap.put("p7s", "application/pkcs7-signature"); - extensionToMimetypeMap.put("pbm", "image/x-portable-bitmap"); - extensionToMimetypeMap.put("pc5", "application/x-pc5"); - extensionToMimetypeMap.put("pci", "application/x-pci"); - extensionToMimetypeMap.put("pcl", "application/x-pcl"); - extensionToMimetypeMap.put("pcx", "application/x-pcx"); - extensionToMimetypeMap.put("pdf", "application/pdf"); - extensionToMimetypeMap.put("pdx", "application/vnd.adobe.pdx"); - extensionToMimetypeMap.put("pfx", "application/x-pkcs12"); - extensionToMimetypeMap.put("pgl", "application/x-pgl"); - extensionToMimetypeMap.put("pgm", "image/x-portable-graymap"); - extensionToMimetypeMap.put("pic", "application/x-pic"); - extensionToMimetypeMap.put("pko", "application/vnd.ms-pki.pko"); - extensionToMimetypeMap.put("pl", "application/x-perl"); - extensionToMimetypeMap.put("plg", "text/html"); - extensionToMimetypeMap.put("pls", "audio/scpls"); - extensionToMimetypeMap.put("plt", "application/x-plt"); - extensionToMimetypeMap.put("png", "image/png"); - extensionToMimetypeMap.put("pnm", "image/x-portable-anymap"); - extensionToMimetypeMap.put("pot", "application/vnd.ms-powerpoint"); - extensionToMimetypeMap.put("ppa", "application/vnd.ms-powerpoint"); extensionToMimetypeMap.put("ppm", "application/x-ppm"); - extensionToMimetypeMap.put("pps", "application/vnd.ms-powerpoint"); - extensionToMimetypeMap.put("ppt", "application/vnd.ms-powerpoint"); - extensionToMimetypeMap.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"); extensionToMimetypeMap.put("pr", "application/x-pr"); - extensionToMimetypeMap.put("prf", "application/pics-rules"); extensionToMimetypeMap.put("prn", "application/x-prn"); extensionToMimetypeMap.put("prt", "application/x-prt"); - extensionToMimetypeMap.put("ps", "application/postscript"); extensionToMimetypeMap.put("ptn", "application/x-ptn"); - extensionToMimetypeMap.put("pwz", "application/vnd.ms-powerpoint"); - extensionToMimetypeMap.put("qt", "video/quicktime"); - extensionToMimetypeMap.put("r3t", "text/vnd.rn-realtext3d"); - extensionToMimetypeMap.put("ra", "audio/vnd.rn-realaudio"); - extensionToMimetypeMap.put("ram", "audio/x-pn-realaudio"); - extensionToMimetypeMap.put("rar", "application/x-rar-compressed"); extensionToMimetypeMap.put("ras", "application/x-ras"); - extensionToMimetypeMap.put("rat", "application/rat-file"); - extensionToMimetypeMap.put("rdf", "text/xml"); - extensionToMimetypeMap.put("rec", "application/vnd.rn-recording"); + extensionToMimetypeMap.put("rtf", "application/x-rtf"); extensionToMimetypeMap.put("red", "application/x-red"); extensionToMimetypeMap.put("rgb", "application/x-rgb"); - extensionToMimetypeMap.put("rjs", "application/vnd.rn-realsystem-rjs"); - extensionToMimetypeMap.put("rjt", "application/vnd.rn-realsystem-rjt"); extensionToMimetypeMap.put("rlc", "application/x-rlc"); extensionToMimetypeMap.put("rle", "application/x-rle"); - extensionToMimetypeMap.put("rm", "application/vnd.rn-realmedia"); - extensionToMimetypeMap.put("rmf", "application/vnd.adobe.rmf"); - extensionToMimetypeMap.put("rmi", "audio/mid"); - extensionToMimetypeMap.put("rmj", "application/vnd.rn-realsystem-rmj"); - extensionToMimetypeMap.put("rmm", "audio/x-pn-realaudio"); - extensionToMimetypeMap.put("rmp", "application/vnd.rn-rn_music_package"); - extensionToMimetypeMap.put("rms", "application/vnd.rn-realmedia-secure"); - extensionToMimetypeMap.put("rmvb", "application/vnd.rn-realmedia-vbr"); - extensionToMimetypeMap.put("rmx", "application/vnd.rn-realsystem-rmx"); - extensionToMimetypeMap.put("rnx", "application/vnd.rn-realplayer"); - extensionToMimetypeMap.put("rp", "image/vnd.rn-realpix"); - extensionToMimetypeMap.put("rpm", "audio/x-pn-realaudio-plugin"); - extensionToMimetypeMap.put("rsml", "application/vnd.rn-rsml"); extensionToMimetypeMap.put("rss", "application/rss+xml"); - extensionToMimetypeMap.put("rt", "text/vnd.rn-realtext"); - extensionToMimetypeMap.put("rtf", "application/x-rtf"); - extensionToMimetypeMap.put("rv", "video/vnd.rn-realvideo"); extensionToMimetypeMap.put("sam", "application/x-sam"); extensionToMimetypeMap.put("sat", "application/x-sat"); - extensionToMimetypeMap.put("sdp", "application/sdp"); extensionToMimetypeMap.put("sdw", "application/x-sdw"); - extensionToMimetypeMap.put("sgm", "text/sgml"); - extensionToMimetypeMap.put("sgml", "text/sgml"); - extensionToMimetypeMap.put("sis", "application/vnd.symbian.install"); - extensionToMimetypeMap.put("sisx", "application/vnd.symbian.install"); extensionToMimetypeMap.put("sit", "application/x-stuffit"); extensionToMimetypeMap.put("slb", "application/x-slb"); extensionToMimetypeMap.put("sld", "application/x-sld"); - extensionToMimetypeMap.put("slk", "drawing/x-slk"); - extensionToMimetypeMap.put("smi", "application/smil"); - extensionToMimetypeMap.put("smil", "application/smil"); extensionToMimetypeMap.put("smk", "application/x-smk"); - extensionToMimetypeMap.put("snd", "audio/basic"); - extensionToMimetypeMap.put("sol", "text/plain"); - extensionToMimetypeMap.put("sor", "text/plain"); - extensionToMimetypeMap.put("spc", "application/x-pkcs7-certificates"); - extensionToMimetypeMap.put("spl", "application/futuresplash"); - extensionToMimetypeMap.put("spp", "text/xml"); - extensionToMimetypeMap.put("ssm", "application/streamingmedia"); - extensionToMimetypeMap.put("sst", "application/vnd.ms-pki.certstore"); - extensionToMimetypeMap.put("stl", "application/vnd.ms-pki.stl"); - extensionToMimetypeMap.put("stm", "text/html"); extensionToMimetypeMap.put("sty", "application/x-sty"); - extensionToMimetypeMap.put("svg", "image/svg+xml"); - extensionToMimetypeMap.put("swf", "application/x-shockwave-flash"); extensionToMimetypeMap.put("tar", "application/x-tar"); extensionToMimetypeMap.put("tdf", "application/x-tdf"); extensionToMimetypeMap.put("tg4", "application/x-tg4"); extensionToMimetypeMap.put("tga", "application/x-tga"); - extensionToMimetypeMap.put("tif", "image/tiff"); - extensionToMimetypeMap.put("tiff", "image/tiff"); - extensionToMimetypeMap.put("tld", "text/xml"); - extensionToMimetypeMap.put("top", "drawing/x-top"); - extensionToMimetypeMap.put("torrent", "application/x-bittorrent"); - extensionToMimetypeMap.put("tsd", "text/xml"); - extensionToMimetypeMap.put("ttf", "application/x-font-ttf"); - extensionToMimetypeMap.put("txt", "text/plain"); - extensionToMimetypeMap.put("uin", "application/x-icq"); - extensionToMimetypeMap.put("uls", "text/iuls"); - extensionToMimetypeMap.put("vcf", "text/x-vcard"); extensionToMimetypeMap.put("vda", "application/x-vda"); - extensionToMimetypeMap.put("vdx", "application/vnd.visio"); - extensionToMimetypeMap.put("vml", "text/xml"); - extensionToMimetypeMap.put("vpg", "application/x-vpeg005"); - extensionToMimetypeMap.put("vsd", "application/vnd.visio"); - extensionToMimetypeMap.put("vss", "application/vnd.visio"); extensionToMimetypeMap.put("vst", "application/x-vst"); - extensionToMimetypeMap.put("vsw", "application/vnd.visio"); - extensionToMimetypeMap.put("vsx", "application/vnd.visio"); - extensionToMimetypeMap.put("vtx", "application/vnd.visio"); - extensionToMimetypeMap.put("vxml", "text/xml"); - extensionToMimetypeMap.put("wav", "audio/wav"); - extensionToMimetypeMap.put("wax", "audio/x-ms-wax"); extensionToMimetypeMap.put("wb1", "application/x-wb1"); extensionToMimetypeMap.put("wb2", "application/x-wb2"); extensionToMimetypeMap.put("wb3", "application/x-wb3"); - extensionToMimetypeMap.put("wbmp", "image/vnd.wap.wbmp"); - extensionToMimetypeMap.put("webm", "video/webm"); - extensionToMimetypeMap.put("wiz", "application/msword"); extensionToMimetypeMap.put("wk3", "application/x-wk3"); extensionToMimetypeMap.put("wk4", "application/x-wk4"); extensionToMimetypeMap.put("wkq", "application/x-wkq"); extensionToMimetypeMap.put("wks", "application/x-wks"); - extensionToMimetypeMap.put("wm", "video/x-ms-wm"); - extensionToMimetypeMap.put("wma", "audio/x-ms-wma"); - extensionToMimetypeMap.put("wmd", "application/x-ms-wmd"); extensionToMimetypeMap.put("wmf", "application/x-wmf"); - extensionToMimetypeMap.put("wml", "text/vnd.wap.wml"); - extensionToMimetypeMap.put("wmv", "video/x-ms-wmv"); - extensionToMimetypeMap.put("wmx", "video/x-ms-wmx"); - extensionToMimetypeMap.put("wmz", "application/x-ms-wmz"); - extensionToMimetypeMap.put("woff", "application/x-font-woff"); extensionToMimetypeMap.put("wp6", "application/x-wp6"); extensionToMimetypeMap.put("wpd", "application/x-wpd"); extensionToMimetypeMap.put("wpg", "application/x-wpg"); - extensionToMimetypeMap.put("wpl", "application/vnd.ms-wpl"); extensionToMimetypeMap.put("wq1", "application/x-wq1"); + } + + private static void addApplicationXMimetypeFour(Map extensionToMimetypeMap) { extensionToMimetypeMap.put("wr1", "application/x-wr1"); extensionToMimetypeMap.put("wri", "application/x-wri"); extensionToMimetypeMap.put("wrk", "application/x-wrk"); extensionToMimetypeMap.put("ws", "application/x-ws"); extensionToMimetypeMap.put("ws2", "application/x-ws"); - extensionToMimetypeMap.put("wsc", "text/scriptlet"); - extensionToMimetypeMap.put("wsdl", "text/xml"); - extensionToMimetypeMap.put("wvx", "video/x-ms-wvx"); extensionToMimetypeMap.put("x_b", "application/x-x_b"); extensionToMimetypeMap.put("x_t", "application/x-x_t"); - extensionToMimetypeMap.put("xap", "application/x-silverlight-app"); - extensionToMimetypeMap.put("xbm", "image/x-xbitmap"); + extensionToMimetypeMap.put("xlw", "application/x-xlw"); + extensionToMimetypeMap.put("xwd", "application/x-xwd"); + extensionToMimetypeMap.put("uin", "application/x-icq"); + extensionToMimetypeMap.put("mac", "application/x-mac"); + extensionToMimetypeMap.put("mi", "application/x-mi"); + extensionToMimetypeMap.put("mil", "application/x-mil"); + extensionToMimetypeMap.put("mxp", "application/x-mmxp"); + extensionToMimetypeMap.put("nrf", "application/x-nrf"); + extensionToMimetypeMap.put("out", "application/x-out"); + extensionToMimetypeMap.put("p12", "application/x-pkcs12"); + extensionToMimetypeMap.put("pc5", "application/x-pc5"); + extensionToMimetypeMap.put("pci", "application/x-pci"); + extensionToMimetypeMap.put("pcl", "application/x-pcl"); + extensionToMimetypeMap.put("pcx", "application/x-pcx"); + extensionToMimetypeMap.put("pfx", "application/x-pkcs12"); + extensionToMimetypeMap.put("pgl", "application/x-pgl"); + extensionToMimetypeMap.put("pic", "application/x-pic"); + extensionToMimetypeMap.put("pl", "application/x-perl"); + extensionToMimetypeMap.put("plt", "application/x-plt"); + } + + private static void addApplicationVNDMimetype(Map extensionToMimetypeMap) { + extensionToMimetypeMap.put("pko", "application/vnd.ms-pki.pko"); + extensionToMimetypeMap.put("vdx", "application/vnd.visio"); + extensionToMimetypeMap.put("apk", "application/vnd.android.package-archive"); + extensionToMimetypeMap.put("awf", "application/vnd.adobe.workflow"); + extensionToMimetypeMap.put("cat", "application/vnd.ms-pki.seccat"); + extensionToMimetypeMap.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); + extensionToMimetypeMap.put("edn", "application/vnd.adobe.edn"); + extensionToMimetypeMap.put("eot", "application/vnd.ms-fontobject"); + extensionToMimetypeMap.put("fdf", "application/vnd.fdf"); + extensionToMimetypeMap.put("ipa", "application/vnd.iphone"); + extensionToMimetypeMap.put("pot", "application/vnd.ms-powerpoint"); + extensionToMimetypeMap.put("ppa", "application/vnd.ms-powerpoint"); + extensionToMimetypeMap.put("pps", "application/vnd.ms-powerpoint"); + extensionToMimetypeMap.put("ppt", "application/vnd.ms-powerpoint"); + extensionToMimetypeMap.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"); + extensionToMimetypeMap.put("pwz", "application/vnd.ms-powerpoint"); + extensionToMimetypeMap.put("rmj", "application/vnd.rn-realsystem-rmj"); + extensionToMimetypeMap.put("rec", "application/vnd.rn-recording"); + extensionToMimetypeMap.put("rjs", "application/vnd.rn-realsystem-rjs"); + extensionToMimetypeMap.put("rjt", "application/vnd.rn-realsystem-rjt"); + extensionToMimetypeMap.put("rm", "application/vnd.rn-realmedia"); + extensionToMimetypeMap.put("rmf", "application/vnd.adobe.rmf"); + extensionToMimetypeMap.put("rmp", "application/vnd.rn-rn_music_package"); + extensionToMimetypeMap.put("rms", "application/vnd.rn-realmedia-secure"); + extensionToMimetypeMap.put("rmvb", "application/vnd.rn-realmedia-vbr"); + extensionToMimetypeMap.put("rmx", "application/vnd.rn-realsystem-rmx"); + extensionToMimetypeMap.put("rnx", "application/vnd.rn-realplayer"); + extensionToMimetypeMap.put("rsml", "application/vnd.rn-rsml"); + extensionToMimetypeMap.put("sis", "application/vnd.symbian.install"); + extensionToMimetypeMap.put("sisx", "application/vnd.symbian.install"); + extensionToMimetypeMap.put("vsd", "application/vnd.visio"); + extensionToMimetypeMap.put("vss", "application/vnd.visio"); + extensionToMimetypeMap.put("vsw", "application/vnd.visio"); + extensionToMimetypeMap.put("vsx", "application/vnd.visio"); + extensionToMimetypeMap.put("vtx", "application/vnd.visio"); + extensionToMimetypeMap.put("wpl", "application/vnd.ms-wpl"); extensionToMimetypeMap.put("xdp", "application/vnd.adobe.xdp"); - extensionToMimetypeMap.put("xdr", "text/xml"); extensionToMimetypeMap.put("xfd", "application/vnd.adobe.xfd"); extensionToMimetypeMap.put("xfdf", "application/vnd.adobe.xfdf"); - extensionToMimetypeMap.put("xhtml", "text/html"); extensionToMimetypeMap.put("xls", "application/vnd.ms-excel"); extensionToMimetypeMap.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); - extensionToMimetypeMap.put("xlw", "application/x-xlw"); + extensionToMimetypeMap.put("mpd", "application/vnd.ms-project"); + extensionToMimetypeMap.put("mpp", "application/vnd.ms-project"); + extensionToMimetypeMap.put("mpt", "application/vnd.ms-project"); + extensionToMimetypeMap.put("mpw", "application/vnd.ms-project"); + extensionToMimetypeMap.put("mpx", "application/vnd.ms-project"); + extensionToMimetypeMap.put("pdx", "application/vnd.adobe.pdx"); + extensionToMimetypeMap.put("sst", "application/vnd.ms-pki.certstore"); + extensionToMimetypeMap.put("stl", "application/vnd.ms-pki.stl"); + } + + private static void addDrawingMimetype(Map extensionToMimetypeMap) { + extensionToMimetypeMap.put("907", "drawing/907"); + extensionToMimetypeMap.put("slk", "drawing/x-slk"); + extensionToMimetypeMap.put("top", "drawing/x-top"); + } + + private static void addXMLTextMimetype(Map extensionToMimetypeMap) { + extensionToMimetypeMap.put("dcd", "text/xml"); + extensionToMimetypeMap.put("dtd", "text/xml"); + extensionToMimetypeMap.put("ent", "text/xml"); + extensionToMimetypeMap.put("biz", "text/xml"); + extensionToMimetypeMap.put("cml", "text/xml"); + extensionToMimetypeMap.put("fo", "text/xml"); + extensionToMimetypeMap.put("tld", "text/xml"); + extensionToMimetypeMap.put("tsd", "text/xml"); + extensionToMimetypeMap.put("vml", "text/xml"); + extensionToMimetypeMap.put("vxml", "text/xml"); + extensionToMimetypeMap.put("wsdl", "text/xml"); + extensionToMimetypeMap.put("xdr", "text/xml"); extensionToMimetypeMap.put("xml", "text/xml"); - extensionToMimetypeMap.put("xpl", "audio/scpls"); - extensionToMimetypeMap.put("xpm", "image/x-xpixmap"); extensionToMimetypeMap.put("xq", "text/xml"); extensionToMimetypeMap.put("xql", "text/xml"); - extensionToMimetypeMap.put("xquery", "text/xml"); extensionToMimetypeMap.put("xsd", "text/xml"); extensionToMimetypeMap.put("xsl", "text/xml"); extensionToMimetypeMap.put("xslt", "text/xml"); - extensionToMimetypeMap.put("xwd", "application/x-xwd"); + extensionToMimetypeMap.put("math", "text/xml"); + extensionToMimetypeMap.put("mml", "text/xml"); + extensionToMimetypeMap.put("mtx", "text/xml"); + extensionToMimetypeMap.put("rdf", "text/xml"); + extensionToMimetypeMap.put("spp", "text/xml"); + extensionToMimetypeMap.put("xquery", "text/xml"); + } + + private static void addOtherTextMimetype(Map extensionToMimetypeMap) { + extensionToMimetypeMap.put("323", "text/h323"); + extensionToMimetypeMap.put("asa", "text/asa"); + extensionToMimetypeMap.put("asp", "text/asp"); + extensionToMimetypeMap.put("css", "text/css"); + extensionToMimetypeMap.put("csv", "text/csv"); + extensionToMimetypeMap.put("etx", "text/x-setext"); + extensionToMimetypeMap.put("htc", "text/x-component"); + extensionToMimetypeMap.put("htm", "text/html"); + extensionToMimetypeMap.put("html", "text/html"); + extensionToMimetypeMap.put("htt", "text/webviewhtml"); + extensionToMimetypeMap.put("htx", "text/html"); + extensionToMimetypeMap.put("ics", "text/calendar"); + extensionToMimetypeMap.put("ini", "text/plain"); + extensionToMimetypeMap.put("jsp", "text/html"); + extensionToMimetypeMap.put("log", "text/plain"); + extensionToMimetypeMap.put("odc", "text/x-ms-odc"); + extensionToMimetypeMap.put("plg", "text/html"); + extensionToMimetypeMap.put("r3t", "text/vnd.rn-realtext3d"); + extensionToMimetypeMap.put("rt", "text/vnd.rn-realtext"); + extensionToMimetypeMap.put("sgm", "text/sgml"); + extensionToMimetypeMap.put("sgml", "text/sgml"); + extensionToMimetypeMap.put("stm", "text/html"); + extensionToMimetypeMap.put("wml", "text/vnd.wap.wml"); + extensionToMimetypeMap.put("sol", "text/plain"); + extensionToMimetypeMap.put("sor", "text/plain"); + extensionToMimetypeMap.put("txt", "text/plain"); + extensionToMimetypeMap.put("uls", "text/iuls"); + extensionToMimetypeMap.put("vcf", "text/x-vcard"); + extensionToMimetypeMap.put("wsc", "text/scriptlet"); + extensionToMimetypeMap.put("xhtml", "text/html"); extensionToMimetypeMap.put("yaml", "text/yaml"); extensionToMimetypeMap.put("yml", "text/yaml"); - extensionToMimetypeMap.put("zip", "application/zip"); + } + + private static void addJavaMimetype(Map extensionToMimetypeMap) { + extensionToMimetypeMap.put("class", "java/*"); + extensionToMimetypeMap.put("java", "java/*"); + } + + private static void addMessageMimetype(Map extensionToMimetypeMap) { + extensionToMimetypeMap.put("eml", "message/rfc822"); + extensionToMimetypeMap.put("mht", "message/rfc822"); + extensionToMimetypeMap.put("mhtml", "message/rfc822"); + extensionToMimetypeMap.put("nws", "message/rfc822"); + } + + private static void addVideoMimetype(Map extensionToMimetypeMap) { + extensionToMimetypeMap.put("IVF", "video/x-ivf"); + extensionToMimetypeMap.put("asf", "video/x-ms-asf"); + extensionToMimetypeMap.put("asx", "video/x-ms-asf"); + extensionToMimetypeMap.put("avi", "video/avi"); + extensionToMimetypeMap.put("flv", "video/x-flv"); + extensionToMimetypeMap.put("m1v", "video/x-mpeg"); + extensionToMimetypeMap.put("m2v", "video/x-mpeg"); + extensionToMimetypeMap.put("m4e", "video/mpeg4"); + extensionToMimetypeMap.put("m4v", "video/mp4"); + extensionToMimetypeMap.put("mov", "video/quicktime"); + extensionToMimetypeMap.put("movie", "video/x-sgi-movie"); + extensionToMimetypeMap.put("mp2v", "video/mpeg"); + extensionToMimetypeMap.put("mp4", "video/mp4"); + extensionToMimetypeMap.put("mp4v", "video/mp4"); + extensionToMimetypeMap.put("mpa", "video/x-mpg"); + extensionToMimetypeMap.put("mpe", "video/x-mpeg"); + extensionToMimetypeMap.put("mpeg", "video/mpg"); + extensionToMimetypeMap.put("mpg", "video/mpg"); + extensionToMimetypeMap.put("mpg4", "video/mp4"); + extensionToMimetypeMap.put("mps", "video/x-mpeg"); + extensionToMimetypeMap.put("mpv", "video/mpg"); + extensionToMimetypeMap.put("mpv2", "video/mpeg"); + extensionToMimetypeMap.put("ogv", "video/ogg"); + extensionToMimetypeMap.put("qt", "video/quicktime"); + extensionToMimetypeMap.put("rv", "video/vnd.rn-realvideo"); + extensionToMimetypeMap.put("webm", "video/webm"); + extensionToMimetypeMap.put("wvx", "video/x-ms-wvx"); + extensionToMimetypeMap.put("wm", "video/x-ms-wm"); + extensionToMimetypeMap.put("wmv", "video/x-ms-wmv"); + extensionToMimetypeMap.put("wmx", "video/x-ms-wmx"); + } + + private static void addImageMimetype(Map extensionToMimetypeMap) { + extensionToMimetypeMap.put("fax", "image/fax"); + extensionToMimetypeMap.put("gif", "image/gif"); + extensionToMimetypeMap.put("jfif", "image/jpeg"); + extensionToMimetypeMap.put("jpe", "image/jpeg"); + extensionToMimetypeMap.put("jpeg", "image/jpeg"); + extensionToMimetypeMap.put("jpg", "image/jpeg"); + extensionToMimetypeMap.put("net", "image/pnetvue"); + extensionToMimetypeMap.put("pbm", "image/x-portable-bitmap"); + extensionToMimetypeMap.put("pgm", "image/x-portable-graymap"); + extensionToMimetypeMap.put("png", "image/png"); + extensionToMimetypeMap.put("pnm", "image/x-portable-anymap"); + extensionToMimetypeMap.put("rp", "image/vnd.rn-realpix"); + extensionToMimetypeMap.put("svg", "image/svg+xml"); + extensionToMimetypeMap.put("tif", "image/tiff"); + extensionToMimetypeMap.put("tiff", "image/tiff"); + extensionToMimetypeMap.put("wbmp", "image/vnd.wap.wbmp"); + extensionToMimetypeMap.put("xbm", "image/x-xbitmap"); + extensionToMimetypeMap.put("xpm", "image/x-xpixmap"); + } + private static void addAudioMimetype(Map extensionToMimetypeMap) { + extensionToMimetypeMap.put("aac", "audio/x-aac"); + extensionToMimetypeMap.put("acp", "audio/x-mei-aac"); + extensionToMimetypeMap.put("aif", "audio/aiff"); + extensionToMimetypeMap.put("aifc", "audio/aiff"); + extensionToMimetypeMap.put("aiff", "audio/aiff"); + extensionToMimetypeMap.put("au", "audio/basic"); + extensionToMimetypeMap.put("flac", "audio/flac"); + extensionToMimetypeMap.put("la1", "audio/x-liquid-file"); + extensionToMimetypeMap.put("lavs", "audio/x-liquid-secure"); + extensionToMimetypeMap.put("lmsff", "audio/x-la-lms"); + extensionToMimetypeMap.put("m3u", "audio/mpegurl"); + extensionToMimetypeMap.put("m4a", "audio/mp4"); + extensionToMimetypeMap.put("mid", "audio/mid"); + extensionToMimetypeMap.put("midi", "audio/mid"); + extensionToMimetypeMap.put("mnd", "audio/x-musicnet-download"); + extensionToMimetypeMap.put("mns", "audio/x-musicnet-stream"); + extensionToMimetypeMap.put("mp1", "audio/mp1"); + extensionToMimetypeMap.put("mp2", "audio/mp2"); + extensionToMimetypeMap.put("mp3", "audio/mp3"); + extensionToMimetypeMap.put("mp4a", "audio/mp4"); + extensionToMimetypeMap.put("mpga", "audio/rn-mpeg"); + extensionToMimetypeMap.put("oga", "audio/ogg"); + extensionToMimetypeMap.put("ogg", "audio/ogg"); + extensionToMimetypeMap.put("pls", "audio/scpls"); + extensionToMimetypeMap.put("ra", "audio/vnd.rn-realaudio"); + extensionToMimetypeMap.put("ram", "audio/x-pn-realaudio"); + extensionToMimetypeMap.put("rmi", "audio/mid"); + extensionToMimetypeMap.put("rmm", "audio/x-pn-realaudio"); + extensionToMimetypeMap.put("rpm", "audio/x-pn-realaudio-plugin"); + extensionToMimetypeMap.put("snd", "audio/basic"); + extensionToMimetypeMap.put("wav", "audio/wav"); + extensionToMimetypeMap.put("wax", "audio/x-ms-wax"); + extensionToMimetypeMap.put("wma", "audio/x-ms-wma"); + extensionToMimetypeMap.put("xpl", "audio/scpls"); } - - private static class MimetypesHolder - { + + private static class MimetypesHolder { private static Mimetypes mimetypes = new Mimetypes(); } - - public static Mimetypes getInstance() - { + + public static Mimetypes getInstance() { return MimetypesHolder.mimetypes; } - - - public String getMimetype(String fileName) - { + + public String getMimetype(String fileName) { String mimetype = extensionToMimetypeMap.get("*"); - if (mimetype == null) - { + if (mimetype == null) { mimetype = MIMETYPE_OCTET_STREAM; } - + int lastPeriodIndex = fileName.lastIndexOf("."); - if (lastPeriodIndex > 0 && lastPeriodIndex + 1 < fileName.length()) - { + if (lastPeriodIndex > 0 && lastPeriodIndex + 1 < fileName.length()) { String ext = fileName.substring(lastPeriodIndex + 1); ext = ext.toLowerCase(Locale.getDefault()); - if (extensionToMimetypeMap.keySet().contains(ext)) - { + + if (extensionToMimetypeMap.containsKey(ext)) { mimetype = extensionToMimetypeMap.get(ext); return mimetype; } } return mimetype; } - - public String getMimetype(File file) - { + + public String getMimetype(File file) { return getMimetype(file.getName()); } } diff --git a/app/src/main/java/com/obs/services/internal/utils/ObjectUtils.java b/app/src/main/java/com/obs/services/internal/utils/ObjectUtils.java new file mode 100644 index 0000000..5904445 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/utils/ObjectUtils.java @@ -0,0 +1,29 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.internal.utils; + +public class ObjectUtils { + public static boolean isEquals(Object obj1, Object obj2) { + if (obj1 == null) { + if (obj2 == null) { + return true; + } else { + return false; + } + } else { + return obj1.equals(obj2); + } + } +} diff --git a/app/src/main/java/com/obs/services/internal/utils/ObsAuthentication.java b/app/src/main/java/com/obs/services/internal/utils/ObsAuthentication.java index f9af114..d048013 100644 --- a/app/src/main/java/com/obs/services/internal/utils/ObsAuthentication.java +++ b/app/src/main/java/com/obs/services/internal/utils/ObsAuthentication.java @@ -11,31 +11,32 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; import com.obs.services.internal.IHeaders; import com.obs.services.internal.ObsHeaders; -public class ObsAuthentication extends AbstractAuthentication -{ - - private static ObsAuthentication instance = new ObsAuthentication(); - - private ObsAuthentication() { - - } - - public static AbstractAuthentication getInstance() { - return instance; - } - @Override - protected IHeaders getIHeaders() { - return ObsHeaders.getInstance(); - } - - @Override - protected String getAuthPrefix() { - return "OBS"; - } - +public class ObsAuthentication extends AbstractAuthentication { + + private static ObsAuthentication instance = new ObsAuthentication(); + + private ObsAuthentication() { + + } + + public static AbstractAuthentication getInstance() { + return instance; + } + + @Override + protected IHeaders getIHeaders() { + return ObsHeaders.getInstance(); + } + + @Override + protected String getAuthPrefix() { + return "OBS"; + } + } diff --git a/app/src/main/java/com/obs/services/internal/utils/ReflectUtils.java b/app/src/main/java/com/obs/services/internal/utils/ReflectUtils.java index 93e7942..44b3dcb 100644 --- a/app/src/main/java/com/obs/services/internal/utils/ReflectUtils.java +++ b/app/src/main/java/com/obs/services/internal/utils/ReflectUtils.java @@ -11,152 +11,137 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; import java.io.UnsupportedEncodingException; -import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.charset.Charset; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.nio.charset.StandardCharsets; -import com.obs.services.ObsClient; -import com.obs.services.exception.ObsException; +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; import com.obs.services.internal.ServiceException; public class ReflectUtils { + private static final ILogger ILOG = LoggerBuilder.getLogger(ReflectUtils.class); + + private static Class androidBase64Class; + + private static Class jdkBase64EncoderClass; + + private static Class jdkBase64DecoderClass; + + private static Object jdkNewEncoder; - private static Class androidBase64Class; - - private static Class jdkBase64EncoderClass; - - private static Class jdkBase64DecoderClass; - - private static Object jdkNewEncoder; - - private static Object jdkNewDecoder; - - private static Map fields = new ConcurrentHashMap(); - - static{ - try - { + private static Object jdkNewDecoder; + + static { + try { androidBase64Class = Class.forName("android.util.Base64"); + } catch (ClassNotFoundException e) { + if (ILOG.isTraceEnabled()) { + ILOG.trace("class not found.", e); + } } - catch (ClassNotFoundException e) - { + + try { + Class base64 = Class.forName("java.util.Base64"); + jdkNewEncoder = base64.getMethod("getEncoder").invoke(null); + jdkNewDecoder = base64.getMethod("getDecoder").invoke(null); + } catch (ClassNotFoundException e) { + if (ILOG.isTraceEnabled()) { + ILOG.trace("class not found exception.", e); + } + } catch (IllegalAccessException e) { + ILOG.warn("illegal access exception.", e); + } catch (IllegalArgumentException e) { + ILOG.warn("illegal argument exception.", e); + } catch (InvocationTargetException e) { + ILOG.warn("invocation target exception.", e); + } catch (NoSuchMethodException e) { + ILOG.warn("nosuch method exception.", e); + } catch (SecurityException e) { + ILOG.warn("security exception.", e); } - + try { - Class base64 = Class.forName("java.util.Base64"); - jdkNewEncoder = base64.getMethod("getEncoder").invoke(null); - jdkNewDecoder = base64.getMethod("getDecoder").invoke(null); - } catch (Exception e1) { - } - - try{ - jdkBase64EncoderClass = Class.forName("sun.misc.BASE64Encoder"); - }catch (ClassNotFoundException e) { + jdkBase64EncoderClass = Class.forName("sun.misc.BASE64Encoder"); + } catch (ClassNotFoundException e) { + if (ILOG.isTraceEnabled()) { + ILOG.trace("class not found exception.", e); + } } - - try{ - jdkBase64DecoderClass = Class.forName("sun.misc.BASE64Decoder"); - }catch (ClassNotFoundException e) { + + try { + jdkBase64DecoderClass = Class.forName("sun.misc.BASE64Decoder"); + } catch (ClassNotFoundException e) { + if (ILOG.isTraceEnabled()) { + ILOG.trace("class not found exception.", e); + } } - - } - - public static String toBase64(byte[] data) - { - if(androidBase64Class != null){ - try{ - Method m = androidBase64Class.getMethod("encode", byte[].class, int.class); - return new String((byte[])m.invoke(null, data, 2), Charset.defaultCharset()); - }catch (Exception e) { - throw new ServiceException(e); + } + + public static String toBase64(byte[] data) { + if (androidBase64Class != null) { + try { + Method m = androidBase64Class.getMethod("encode", byte[].class, int.class); + return new String((byte[]) m.invoke(null, data, 2), Charset.defaultCharset()); + } catch (Exception e) { + throw new ServiceException(e); } - } - - if(jdkNewEncoder != null) { - try{ - Method m = jdkNewEncoder.getClass().getMethod("encode", byte[].class); - return new String((byte[])m.invoke(jdkNewEncoder, data), "UTF-8").replaceAll("\\s", ""); - }catch (Exception e) { - throw new ServiceException(e); + } + + if (jdkNewEncoder != null) { + try { + Method m = jdkNewEncoder.getClass().getMethod("encode", byte[].class); + return new String((byte[]) m.invoke(jdkNewEncoder, data), StandardCharsets.UTF_8).replaceAll("\\s", ""); + } catch (Exception e) { + throw new ServiceException(e); } - } - - if(jdkBase64EncoderClass != null){ - try{ - Method m = jdkBase64EncoderClass.getMethod("encode", byte[].class); - return ((String)m.invoke(jdkBase64EncoderClass.getConstructor().newInstance(), data)).replaceAll("\\s", ""); - }catch (Exception e) { - throw new ServiceException(e); + } + + if (jdkBase64EncoderClass != null) { + try { + Method m = jdkBase64EncoderClass.getMethod("encode", byte[].class); + return ((String) m.invoke(jdkBase64EncoderClass.getConstructor().newInstance(), data)).replaceAll("\\s", + ""); + } catch (Exception e) { + throw new ServiceException(e); } - } - - throw new ServiceException("Failed to find a base64 encoder"); - } - - public static byte[] fromBase64(String b64Data) throws UnsupportedEncodingException - { - if(androidBase64Class != null){ - try{ - Method m = androidBase64Class.getMethod("decode", byte[].class, int.class); - return (byte[])m.invoke(null, b64Data.getBytes("UTF-8"), 2); - }catch (Exception e) { - throw new ServiceException(e); + } + + throw new ServiceException("Failed to find a base64 encoder"); + } + + public static byte[] fromBase64(String b64Data) throws UnsupportedEncodingException { + if (androidBase64Class != null) { + try { + Method m = androidBase64Class.getMethod("decode", byte[].class, int.class); + return (byte[]) m.invoke(null, b64Data.getBytes(StandardCharsets.UTF_8), 2); + } catch (Exception e) { + throw new ServiceException(e); } - } - - if(jdkNewDecoder != null) { - try{ - Method m = jdkNewDecoder.getClass().getMethod("decode", byte[].class); - return (byte[])m.invoke(jdkNewDecoder, b64Data.getBytes("UTF-8")); - }catch (Exception e) { - throw new ServiceException(e); + } + + if (jdkNewDecoder != null) { + try { + Method m = jdkNewDecoder.getClass().getMethod("decode", byte[].class); + return (byte[]) m.invoke(jdkNewDecoder, b64Data.getBytes(StandardCharsets.UTF_8)); + } catch (Exception e) { + throw new ServiceException(e); } - } - - if(jdkBase64DecoderClass != null){ - try{ + } + + if (jdkBase64DecoderClass != null) { + try { Method m = jdkBase64DecoderClass.getMethod("decodeBuffer", String.class); - return (byte[])m.invoke(jdkBase64DecoderClass.getConstructor().newInstance(), b64Data); - }catch (Exception e) { - throw new ServiceException(e); + return (byte[]) m.invoke(jdkBase64DecoderClass.getConstructor().newInstance(), b64Data); + } catch (Exception e) { + throw new ServiceException(e); } - } - throw new ServiceException("Failed to find a base64 decoder"); - } - - - public static void setInnerClient(Object obj, ObsClient obsClient) { - if(obj != null && obsClient != null) { - Class clazz = obj.getClass(); - String name = clazz.getName(); - Field f = fields.get(name); - try { - if(f == null) { - f = getFieldFromClass(clazz, "innerClient"); - f.setAccessible(true); - fields.put(name, f); - } - f.set(obj, obsClient); - } catch (Exception e) { - throw new ObsException(e.getMessage(), e); - } - } - } - - private static Field getFieldFromClass(Class clazz, String key) { - do { - try { - return clazz.getDeclaredField(key); - } catch (NoSuchFieldException e) { - clazz = clazz.getSuperclass(); - } - }while(clazz != null); - return null; - } - + } + throw new ServiceException("Failed to find a base64 decoder"); + } } diff --git a/app/src/main/java/com/obs/services/internal/utils/RestUtils.java b/app/src/main/java/com/obs/services/internal/utils/RestUtils.java index fda286c..42123ad 100644 --- a/app/src/main/java/com/obs/services/internal/utils/RestUtils.java +++ b/app/src/main/java/com/obs/services/internal/utils/RestUtils.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; import java.io.IOException; @@ -47,12 +48,13 @@ import com.obs.log.ILogger; import com.obs.log.LoggerBuilder; +import com.obs.services.exception.ObsException; import com.obs.services.internal.Constants; import com.obs.services.internal.Constants.CommonHeaders; import com.obs.services.internal.ObsConstraint; import com.obs.services.internal.ObsProperties; -import com.obs.services.internal.RestStorageService; import com.obs.services.internal.ServiceException; +import com.obs.services.internal.ext.ExtObsConstraint; import com.obs.services.model.HttpProtocolTypeEnum; import okhttp3.Authenticator; @@ -65,132 +67,120 @@ import okhttp3.Response; import okhttp3.Route; -public class RestUtils -{ - +public class RestUtils { + private static final ILogger log = LoggerBuilder.getLogger(RestUtils.class); - + + //CHECKSTYLE:OFF private static Pattern chinesePattern = Pattern.compile("[\u4e00-\u9fa5]"); - - private static final HostnameVerifier ALLOW_ALL_HOSTNAME = new HostnameVerifier() - { - + + private static final HostnameVerifier ALLOW_ALL_HOSTNAME = new HostnameVerifier() { @Override - public boolean verify(String arg0, SSLSession arg1) - { + public boolean verify(String arg0, SSLSession arg1) { return true; } - }; - - - private static final X509TrustManager TRUST_ALL_MANAGER = new X509TrustManager() - { - - @Override - public X509Certificate[] getAcceptedIssuers() - { - return new X509Certificate[]{}; - } - - @Override - public void checkServerTrusted(X509Certificate[] arg0, String arg1) - throws CertificateException - { - } - - @Override - public void checkClientTrusted(X509Certificate[] arg0, String arg1) - throws CertificateException - { - } }; - - public static String uriEncode(CharSequence input, boolean chineseOnly) throws ServiceException - { - + + private static final X509TrustManager TRUST_ALL_MANAGER = new X509TrustManager() { + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[] {}; + } + + @Override + public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + } + + @Override + public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + } + }; + + public static String uriEncode(CharSequence input, boolean chineseOnly) throws ServiceException { + StringBuilder result = new StringBuilder(); - try{ - - if(chineseOnly){ - for (int i = 0; i < input.length(); i++){ - char ch = input.charAt(i); - String s = Character.toString(ch); - Matcher m = chinesePattern.matcher(s); - if(m != null && m.find()){ - result.append(URLEncoder.encode(s, Constants.DEFAULT_ENCODING)); - }else{ - result.append(ch); - } - } - }else{ - for (int i = 0; i < input.length(); i++) - { - char ch = input.charAt(i); - if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-' || ch == '~' - || ch == '.') - { - result.append(ch); - } - else if (ch == '/') - { - result.append("%2F"); - } - else - { - result.append(URLEncoder.encode(Character.toString(ch), Constants.DEFAULT_ENCODING)); - } - } - } - }catch (UnsupportedEncodingException e) - { + try { + tryEncode(input, chineseOnly, result); + } catch (UnsupportedEncodingException e) { throw new ServiceException("Unable to encode input: " + input); } return result.toString(); } - - public static String encodeUrlString(String path) - throws ServiceException - { - try - { - return URLEncoder.encode(path, Constants.DEFAULT_ENCODING) - .replaceAll("\\+", "%20") //Web browsers do not always handle '+' characters well, use the well-supported '%20' instead. - .replaceAll("%7E", "~") - .replaceAll("\\*", "%2A"); + + private static void tryEncode(CharSequence input, boolean chineseOnly, StringBuilder result) + throws UnsupportedEncodingException { + if (chineseOnly) { + for (int i = 0; i < input.length(); i++) { + char ch = input.charAt(i); + String s = Character.toString(ch); + Matcher m = chinesePattern.matcher(s); + if (m != null && m.find()) { + result.append(URLEncoder.encode(s, Constants.DEFAULT_ENCODING)); + } else { + result.append(ch); + } + } + } else { + for (int i = 0; i < input.length(); i++) { + char ch = input.charAt(i); + if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' + || ch == '-' || ch == '~' || ch == '.') { + result.append(ch); + } else if (ch == '/') { + result.append("%2F"); + } else { + result.append(URLEncoder.encode(Character.toString(ch), Constants.DEFAULT_ENCODING)); + } + } } - catch (UnsupportedEncodingException uee) - { + } + + public static String encodeUrlString(String path) throws ServiceException { + try { + return URLEncoder.encode(path, Constants.DEFAULT_ENCODING).replaceAll("\\+", "%20") // Web + // browsers + // do + // not + // always + // handle + // '+' + // characters + // well, + // use + // the + // well-supported + // '%20' + // instead. + .replaceAll("%7E", "~").replaceAll("\\*", "%2A"); + } catch (UnsupportedEncodingException uee) { throw new ServiceException("Unable to encode path: " + path, uee); } } - - public static String encodeUrlPath(String path, String delimiter) - throws ServiceException - { + + public static String encodeUrlPath(String path, String delimiter) throws ServiceException { StringBuilder result = new StringBuilder(); - String tokens[] = path.split(delimiter); - for (int i = 0; i < tokens.length; i++) - { + String[] tokens = path.split(delimiter); + for (int i = 0; i < tokens.length; i++) { result.append(encodeUrlString(tokens[i])); - if (i < tokens.length - 1) - { + if (i < tokens.length - 1) { result.append(delimiter); } } return result.toString(); } - - private static SSLContext createSSLContext(KeyManager[] km, TrustManager[] tm, String provider) throws Exception{ + + private static SSLContext createSSLContext(KeyManager[] km, TrustManager[] tm, String provider) throws Exception { SSLContext sslContext; - try{ - sslContext =SSLContext.getInstance("TLSv1.2", provider); - }catch (Exception e) { - try{ - sslContext = SSLContext.getInstance("TLSv1.1", provider); - }catch (Exception ex) { - try{ - sslContext = SSLContext.getInstance("TLSv1.0", provider); - }catch (Exception exx) { + try { + sslContext = SSLContext.getInstance("TLSv1.2", provider); + } catch (Exception e) { + try { + sslContext = SSLContext.getInstance("TLSv1.1", provider); + } catch (Exception ex) { + try { + sslContext = SSLContext.getInstance("TLSv1.0", provider); + } catch (Exception exx) { sslContext = SSLContext.getInstance("TLS", provider); } } @@ -198,18 +188,18 @@ private static SSLContext createSSLContext(KeyManager[] km, TrustManager[] tm, S sslContext.init(km, tm, new SecureRandom()); return sslContext; } - - private static SSLContext createSSLContext(KeyManager[] km, TrustManager[] tm) throws Exception{ + + private static SSLContext createSSLContext(KeyManager[] km, TrustManager[] tm) throws Exception { SSLContext sslContext; - try{ - sslContext =SSLContext.getInstance("TLSv1.2"); - }catch (Exception e) { - try{ - sslContext = SSLContext.getInstance("TLSv1.1"); - }catch (Exception ex) { - try{ - sslContext = SSLContext.getInstance("TLSv1.0"); - }catch (Exception exx) { + try { + sslContext = SSLContext.getInstance("TLSv1.2"); + } catch (Exception e) { + try { + sslContext = SSLContext.getInstance("TLSv1.1"); + } catch (Exception ex) { + try { + sslContext = SSLContext.getInstance("TLSv1.0"); + } catch (Exception exx) { sslContext = SSLContext.getInstance("TLS"); } } @@ -217,233 +207,255 @@ private static SSLContext createSSLContext(KeyManager[] km, TrustManager[] tm) t sslContext.init(km, tm, new SecureRandom()); return sslContext; } - - private static class WrapperedSocketFactory extends SocketFactory{ - - private SocketFactory delegate; - private int socketReadWriteBufferSize; - - WrapperedSocketFactory(SocketFactory delegate, int socketReadWriteBufferSize) { - this.delegate = delegate; - this.socketReadWriteBufferSize = socketReadWriteBufferSize; - } - - private Socket doWrap(Socket s) throws SocketException { - if(s != null) { - if(socketReadWriteBufferSize > 0) { - s.setReceiveBufferSize(socketReadWriteBufferSize); - s.setReceiveBufferSize(socketReadWriteBufferSize); - } - s.setTcpNoDelay(true); - } - return s; - } - - @Override - public Socket createSocket() throws IOException, UnknownHostException { - return this.doWrap(this.delegate.createSocket()); - } - - @Override - public Socket createSocket(String host, int port) throws IOException, UnknownHostException { - return this.doWrap(this.delegate.createSocket(host, port)); - } - - @Override - public Socket createSocket(InetAddress host, int port) throws IOException { - return this.doWrap(this.delegate.createSocket(host, port)); - } - - @Override - public Socket createSocket(String host, int port, InetAddress localHost, int localPort) - throws IOException, UnknownHostException { - return this.doWrap(this.delegate.createSocket(host, port, localHost, localPort)); - } - - @Override - public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) - throws IOException { - return this.doWrap(this.delegate.createSocket(address, port, localAddress, localPort)); - } - + + private static class WrapperedSocketFactory extends SocketFactory { + + private SocketFactory delegate; + private int socketReadWriteBufferSize; + + WrapperedSocketFactory(SocketFactory delegate, int socketReadWriteBufferSize) { + this.delegate = delegate; + this.socketReadWriteBufferSize = socketReadWriteBufferSize; + } + + private Socket doWrap(Socket s) throws SocketException { + if (s != null) { + if (socketReadWriteBufferSize > 0) { + s.setReceiveBufferSize(socketReadWriteBufferSize); + s.setReceiveBufferSize(socketReadWriteBufferSize); + } + s.setTcpNoDelay(true); + } + return s; + } + + @Override + public Socket createSocket() throws IOException, UnknownHostException { + return this.doWrap(this.delegate.createSocket()); + } + + @Override + public Socket createSocket(String host, int port) throws IOException, UnknownHostException { + return this.doWrap(this.delegate.createSocket(host, port)); + } + + @Override + public Socket createSocket(InetAddress host, int port) throws IOException { + return this.doWrap(this.delegate.createSocket(host, port)); + } + + @Override + public Socket createSocket(String host, int port, InetAddress localHost, int localPort) + throws IOException, UnknownHostException { + return this.doWrap(this.delegate.createSocket(host, port, localHost, localPort)); + } + + @Override + public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) + throws IOException { + return this.doWrap(this.delegate.createSocket(address, port, localAddress, localPort)); + } + } - - private static class WrapperedSSLSocketFactory extends SSLSocketFactory{ - - private SSLSocketFactory delegate; - private int socketReadWriteBufferSize; - - WrapperedSSLSocketFactory(SSLSocketFactory delegate, int socketReadWriteBufferSize){ - this.delegate = delegate; - this.socketReadWriteBufferSize = socketReadWriteBufferSize; - } - - private Socket doWrap(Socket s) throws SocketException { - if(s != null) { - if(socketReadWriteBufferSize > 0) { - s.setReceiveBufferSize(socketReadWriteBufferSize); - s.setReceiveBufferSize(socketReadWriteBufferSize); - } - s.setTcpNoDelay(true); - } - return s; - } - - @Override - public Socket createSocket() throws IOException, UnknownHostException { - return this.doWrap(this.delegate.createSocket()); - } - - @Override - public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { - return doWrap(delegate.createSocket(s, host, port, autoClose)); - } - - @Override - public String[] getDefaultCipherSuites() { - return delegate.getDefaultCipherSuites(); - } - - @Override - public String[] getSupportedCipherSuites() { - return delegate.getSupportedCipherSuites(); - } - - @Override - public Socket createSocket(String host, int port) throws IOException, UnknownHostException { - return doWrap(this.delegate.createSocket(host, port)); - } - - @Override - public Socket createSocket(InetAddress host, int port) throws IOException { - return doWrap(delegate.createSocket(host, port)); - } - - @Override - public Socket createSocket(String host, int port, InetAddress localHost, int localPort) - throws IOException, UnknownHostException { - return doWrap(delegate.createSocket(host, port, localHost, localPort)); - } - - @Override - public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) - throws IOException { - return doWrap(delegate.createSocket(address, port, localAddress, localPort)); - } - + + private static class WrapperedSSLSocketFactory extends SSLSocketFactory { + + private SSLSocketFactory delegate; + private int socketReadWriteBufferSize; + + WrapperedSSLSocketFactory(SSLSocketFactory delegate, int socketReadWriteBufferSize) { + this.delegate = delegate; + this.socketReadWriteBufferSize = socketReadWriteBufferSize; + } + + private Socket doWrap(Socket s) throws SocketException { + if (s != null) { + if (socketReadWriteBufferSize > 0) { + s.setReceiveBufferSize(socketReadWriteBufferSize); + s.setReceiveBufferSize(socketReadWriteBufferSize); + } + s.setTcpNoDelay(true); + } + return s; + } + + @Override + public Socket createSocket() throws IOException, UnknownHostException { + return this.doWrap(this.delegate.createSocket()); + } + + @Override + public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { + return doWrap(delegate.createSocket(s, host, port, autoClose)); + } + + @Override + public String[] getDefaultCipherSuites() { + return delegate.getDefaultCipherSuites(); + } + + @Override + public String[] getSupportedCipherSuites() { + return delegate.getSupportedCipherSuites(); + } + + @Override + public Socket createSocket(String host, int port) throws IOException, UnknownHostException { + return doWrap(this.delegate.createSocket(host, port)); + } + + @Override + public Socket createSocket(InetAddress host, int port) throws IOException { + return doWrap(delegate.createSocket(host, port)); + } + + @Override + public Socket createSocket(String host, int port, InetAddress localHost, int localPort) + throws IOException, UnknownHostException { + return doWrap(delegate.createSocket(host, port, localHost, localPort)); + } + + @Override + public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) + throws IOException { + return doWrap(delegate.createSocket(address, port, localAddress, localPort)); + } + } - - public static OkHttpClient.Builder initHttpClientBuilder(final RestStorageService restStorageService, ObsProperties obsProperties, - KeyManagerFactory keyManagerFactory, TrustManagerFactory trustManagerFactory, Dispatcher httpDispatcher) - { - + + public static OkHttpClient.Builder initHttpClientBuilder(ObsProperties obsProperties, + KeyManagerFactory keyManagerFactory, TrustManagerFactory trustManagerFactory, + Dispatcher httpDispatcher) { + List protocols = new ArrayList(2); protocols.add(Protocol.HTTP_1_1); - - if(HttpProtocolTypeEnum.getValueFromCode(obsProperties.getStringProperty(ObsConstraint.HTTP_PROTOCOL, HttpProtocolTypeEnum.HTTP1_1.getCode())) == HttpProtocolTypeEnum.HTTP2_0) { - protocols.add(Protocol.HTTP_2); + + if (HttpProtocolTypeEnum.getValueFromCode(obsProperties.getStringProperty(ObsConstraint.HTTP_PROTOCOL, + HttpProtocolTypeEnum.HTTP1_1.getCode())) == HttpProtocolTypeEnum.HTTP2_0) { + protocols.add(Protocol.HTTP_2); } - + OkHttpClient.Builder builder = new OkHttpClient.Builder(); - - if(httpDispatcher == null) { - int maxConnections = obsProperties.getIntProperty(ObsConstraint.HTTP_MAX_CONNECT, ObsConstraint.HTTP_MAX_CONNECT_VALUE); - httpDispatcher = new Dispatcher(); - httpDispatcher.setMaxRequests(maxConnections); - httpDispatcher.setMaxRequestsPerHost(maxConnections); - builder.dispatcher(httpDispatcher); - }else { - try { - Method m = builder.getClass().getMethod("dispatcher", httpDispatcher.getClass()); - m.invoke(builder, httpDispatcher); - }catch (Exception e) { - try { - Class c = Class.forName("okhttp3.AbsDispatcher"); - Method m = builder.getClass().getMethod("dispatcher", c); - m.invoke(builder, httpDispatcher); - }catch (Exception ex) { - throw new RuntimeException(ex); - } - } - } - - ConnectionPool pool = new ConnectionPool(obsProperties.getIntProperty(ObsConstraint.HTTP_MAX_IDLE_CONNECTIONS, ObsConstraint.DEFAULT_MAX_IDLE_CONNECTIONS), - obsProperties.getIntProperty(ObsConstraint.HTTP_IDLE_CONNECTION_TIME, ObsConstraint.DEFAULT_IDLE_CONNECTION_TIME), TimeUnit.MILLISECONDS); - - builder.protocols(protocols).followRedirects(false).followSslRedirects(false).retryOnConnectionFailure(false).cache(null).connectTimeout(obsProperties.getIntProperty(ObsConstraint.HTTP_CONNECT_TIMEOUT, ObsConstraint.HTTP_CONNECT_TIMEOUT_VALUE), TimeUnit.MILLISECONDS) - .writeTimeout(obsProperties.getIntProperty(ObsConstraint.HTTP_SOCKET_TIMEOUT, ObsConstraint.HTTP_SOCKET_TIMEOUT_VALUE), TimeUnit.MILLISECONDS) - .readTimeout(obsProperties.getIntProperty(ObsConstraint.HTTP_SOCKET_TIMEOUT, ObsConstraint.HTTP_SOCKET_TIMEOUT_VALUE), TimeUnit.MILLISECONDS) - .connectionPool(pool).hostnameVerifier(obsProperties.getBoolProperty(ObsConstraint.HTTP_STRICT_HOSTNAME_VERIFICATION, false) ? HttpsURLConnection.getDefaultHostnameVerifier(): ALLOW_ALL_HOSTNAME); - + // OkHttpClient.Builder builder = new + // OkHttpClient.Builder().addNetworkInterceptor(new + // RemoveDirtyConnIntercepter()); + + initHttpDispatcher(obsProperties, httpDispatcher, builder); + + ConnectionPool pool = new ConnectionPool( + obsProperties.getIntProperty(ObsConstraint.HTTP_MAX_IDLE_CONNECTIONS, + ObsConstraint.DEFAULT_MAX_IDLE_CONNECTIONS), + obsProperties.getIntProperty(ObsConstraint.HTTP_IDLE_CONNECTION_TIME, + ObsConstraint.DEFAULT_IDLE_CONNECTION_TIME), + TimeUnit.MILLISECONDS); + + builder.protocols(protocols).followRedirects(false).followSslRedirects(false) + .retryOnConnectionFailure( + obsProperties.getBoolProperty(ExtObsConstraint.IS_RETRY_ON_CONNECTION_FAILURE_IN_OKHTTP, false)) + .cache(null) + .connectTimeout(obsProperties.getIntProperty(ObsConstraint.HTTP_CONNECT_TIMEOUT, + ObsConstraint.HTTP_CONNECT_TIMEOUT_VALUE), TimeUnit.MILLISECONDS) + .writeTimeout(obsProperties.getIntProperty(ObsConstraint.HTTP_SOCKET_TIMEOUT, + ObsConstraint.HTTP_SOCKET_TIMEOUT_VALUE), TimeUnit.MILLISECONDS) + .readTimeout(obsProperties.getIntProperty(ObsConstraint.HTTP_SOCKET_TIMEOUT, + ObsConstraint.HTTP_SOCKET_TIMEOUT_VALUE), TimeUnit.MILLISECONDS) + .connectionPool(pool) + .hostnameVerifier(obsProperties.getBoolProperty(ObsConstraint.HTTP_STRICT_HOSTNAME_VERIFICATION, false) + ? HttpsURLConnection.getDefaultHostnameVerifier() : ALLOW_ALL_HOSTNAME); + int socketReadBufferSize = obsProperties.getIntProperty(ObsConstraint.SOCKET_READ_BUFFER_SIZE, -1); int socketWriteBufferSize = obsProperties.getIntProperty(ObsConstraint.SOCKET_WRITE_BUFFER_SIZE, -1); - + int socketReadWriteBufferSize = Math.max(socketReadBufferSize, socketWriteBufferSize); - + builder.socketFactory(new WrapperedSocketFactory(SocketFactory.getDefault(), socketReadWriteBufferSize)); - - try{ + + try { KeyManager[] km = null; X509TrustManager trustManager; TrustManager[] tm; - - if(obsProperties.getBoolProperty(ObsConstraint.VALIDATE_CERTIFICATE, false)){ + + if (obsProperties.getBoolProperty(ObsConstraint.VALIDATE_CERTIFICATE, false)) { km = keyManagerFactory == null ? null : keyManagerFactory.getKeyManagers(); - if(trustManagerFactory == null || trustManagerFactory.getTrustManagers().length < 1){ + if (trustManagerFactory == null || trustManagerFactory.getTrustManagers().length < 1) { trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); - trustManagerFactory.init((KeyStore)null); + trustManagerFactory.init((KeyStore) null); } tm = trustManagerFactory.getTrustManagers(); - trustManager = (X509TrustManager)tm[0]; - }else{ + trustManager = (X509TrustManager) tm[0]; + } else { trustManager = TRUST_ALL_MANAGER; - tm = new TrustManager[]{trustManager}; + tm = new TrustManager[] {trustManager}; } String provider = obsProperties.getStringProperty(ObsConstraint.SSL_PROVIDER, ""); SSLContext sslContext = null; - if(ServiceUtils.isValid(provider)) { - try { - sslContext = createSSLContext(km, tm, provider); - }catch (Exception e) { - if(log.isErrorEnabled()) { - log.error("Exception happened in create ssl context with provider" + provider, e); - } + if (ServiceUtils.isValid(provider)) { + try { + sslContext = createSSLContext(km, tm, provider); + } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error("Exception happened in create ssl context with provider" + provider, e); + } } } - if(sslContext == null) { - sslContext = createSSLContext(km, tm); + if (sslContext == null) { + sslContext = createSSLContext(km, tm); } SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); - builder.sslSocketFactory(new WrapperedSSLSocketFactory(sslSocketFactory, socketReadWriteBufferSize), trustManager); - }catch (Exception e) { - if(log.isErrorEnabled()) { - log.error("Exception happened in HttpClient.configSSL,and e = " + e); - } + builder.sslSocketFactory(new WrapperedSSLSocketFactory(sslSocketFactory, socketReadWriteBufferSize), + trustManager); + } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error("Exception happened in HttpClient.configSSL,and e = " + e); + } } - + return builder; } - - - public static void initHttpProxy(OkHttpClient.Builder builder, - String proxyHostAddress, int proxyPort, final String proxyUser, final String proxyPassword, String proxyDomain, String proxyWorkstation) - { - if (proxyHostAddress != null && proxyPort != -1) - { - if (log.isInfoEnabled()) - { + + private static void initHttpDispatcher(ObsProperties obsProperties, Dispatcher httpDispatcher, + OkHttpClient.Builder builder) { + if (httpDispatcher == null) { + int maxConnections = obsProperties.getIntProperty(ObsConstraint.HTTP_MAX_CONNECT, + ObsConstraint.HTTP_MAX_CONNECT_VALUE); + httpDispatcher = new Dispatcher(); + httpDispatcher.setMaxRequests(maxConnections); + httpDispatcher.setMaxRequestsPerHost(maxConnections); + builder.dispatcher(httpDispatcher); + } else { + try { + Method m = builder.getClass().getMethod("dispatcher", httpDispatcher.getClass()); + m.invoke(builder, httpDispatcher); + } catch (Exception e) { + if (log.isWarnEnabled()) { + log.warn("invoke " + httpDispatcher.getClass() + ".dispatcher() failed.", e); + } + try { + Class c = Class.forName("okhttp3.AbsDispatcher"); + Method m = builder.getClass().getMethod("dispatcher", c); + m.invoke(builder, httpDispatcher); + } catch (Exception ex) { + throw new ObsException("invoke okhttp3.AbsDispatcher.dispatcher failed", ex); + } + } + } + } + + public static void initHttpProxy(OkHttpClient.Builder builder, String proxyHostAddress, int proxyPort, + final String proxyUser, final String proxyPassword) { + if (proxyHostAddress != null && proxyPort != -1) { + if (log.isInfoEnabled()) { log.info("Using Proxy: " + proxyHostAddress + ":" + proxyPort); } builder.proxy(new java.net.Proxy(Type.HTTP, new InetSocketAddress(proxyHostAddress, proxyPort))); - - if (proxyUser != null && !proxyUser.trim().equals("")) - { + + if (proxyUser != null && !proxyUser.trim().equals("")) { Authenticator proxyAuthenticator = new Authenticator() { - @Override public Request authenticate(Route route, Response response) throws IOException { - String credential = Credentials.basic(proxyUser, proxyPassword); - return response.request().newBuilder() - .header(CommonHeaders.PROXY_AUTHORIZATION, credential) - .build(); + @Override + public Request authenticate(Route route, Response response) throws IOException { + String credential = Credentials.basic(proxyUser, proxyPassword); + return response.request().newBuilder().header(CommonHeaders.PROXY_AUTHORIZATION, credential) + .build(); } }; builder.proxyAuthenticator(proxyAuthenticator); diff --git a/app/src/main/java/com/obs/services/internal/utils/SecureObjectInputStream.java b/app/src/main/java/com/obs/services/internal/utils/SecureObjectInputStream.java index c53c72e..ec164e4 100644 --- a/app/src/main/java/com/obs/services/internal/utils/SecureObjectInputStream.java +++ b/app/src/main/java/com/obs/services/internal/utils/SecureObjectInputStream.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; import java.io.IOException; @@ -22,40 +23,33 @@ import java.util.List; public final class SecureObjectInputStream extends ObjectInputStream { - - public static final List ALLOWED_ClASS_NAMES = Collections.unmodifiableList(Arrays.asList( - "java.util.ArrayList", - "com.obs.services.model.PartEtag", - "java.lang.Integer", - "java.lang.Number", - "java.util.Date", - "com.obs.services.internal.ResumableClient$TmpFileStatus", - "com.obs.services.internal.ResumableClient$UploadCheckPoint", - "com.obs.services.internal.ResumableClient$FileStatus", - "com.obs.services.internal.ResumableClient$UploadPart", - "com.obs.services.internal.ResumableClient$DownloadCheckPoint", - "com.obs.services.internal.ResumableClient$DownloadPart", - "com.obs.services.internal.ResumableClient$ObjectStatus" - )); - - public SecureObjectInputStream() throws IOException, SecurityException { - super(); - } - - public SecureObjectInputStream(InputStream in) throws IOException { - super(in); - } - - protected Class resolveClass(ObjectStreamClass desc) - throws IOException, ClassNotFoundException - { - String name = desc.getName(); - //白名单校验 - if(!ALLOWED_ClASS_NAMES.contains(name)) { - throw new ClassNotFoundException(name + "not find"); - } - return super.resolveClass(desc); - } + public static final List ALLOWED_CLASS_NAMES = Collections.unmodifiableList( + Arrays.asList("java.util.ArrayList", "com.obs.services.model.PartEtag", "java.lang.Integer", + "java.lang.Number", "java.util.Date", + "com.obs.services.internal.DownloadResumableClient$TmpFileStatus", + "com.obs.services.internal.UploadResumableClient$UploadCheckPoint", + "com.obs.services.internal.UploadResumableClient$FileStatus", + "com.obs.services.internal.UploadResumableClient$UploadPart", + "com.obs.services.internal.DownloadResumableClient$DownloadCheckPoint", + "com.obs.services.internal.DownloadResumableClient$DownloadPart", + "com.obs.services.internal.DownloadResumableClient$ObjectStatus")); + + public SecureObjectInputStream() throws IOException, SecurityException { + super(); + } + + public SecureObjectInputStream(InputStream in) throws IOException { + super(in); + } + + protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { + String name = desc.getName(); + // 白名单校验 + if (!ALLOWED_CLASS_NAMES.contains(name)) { + throw new ClassNotFoundException(name + "not find"); + } + return super.resolveClass(desc); + } } diff --git a/app/src/main/java/com/obs/services/internal/utils/ServiceUtils.java b/app/src/main/java/com/obs/services/internal/utils/ServiceUtils.java index f4366aa..de5a5c8 100644 --- a/app/src/main/java/com/obs/services/internal/utils/ServiceUtils.java +++ b/app/src/main/java/com/obs/services/internal/utils/ServiceUtils.java @@ -1,32 +1,47 @@ /** - * JetS3t : Java S3 Toolkit - * Project hosted at http://bitbucket.org/jmurty/jets3t/ - * - * Copyright 2006-2010 James Murty - * * Copyright 2019 Huawei Technologies Co.,Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.Constants; +import com.obs.services.internal.ObsConstraint; +import com.obs.services.internal.ObsProperties; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.ext.ExtObsConfiguration; +import com.obs.services.internal.ext.ExtObsConstraint; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.HttpProtocolTypeEnum; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.Closeable; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -43,719 +58,479 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.crypto.Mac; -import javax.crypto.spec.SecretKeySpec; +public class ServiceUtils { + private static final ILogger log = LoggerBuilder.getLogger(ServiceUtils.class); -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.XMLReaderFactory; + protected static final String ISO_8601_TIME_PARSER_STRING = Constants.EXPIRATION_DATE_FORMATTER; + protected static final String ISO_8601_TIME_MIDNING_PARSER_STRING = "yyyy-MM-dd'T'00:00:00'Z'"; + protected static final String ISO_8601_TIME_PARSER_WALRUS_STRING = "yyyy-MM-dd'T'HH:mm:ss"; + protected static final String RFC_822_TIME_PARSER_STRING = Constants.HEADER_DATE_FORMATTER; + protected static final String ISO_8601_DATE_PARSER_STRING = "yyyy-MM-dd"; -import com.obs.log.ILogger; -import com.obs.log.LoggerBuilder; -import com.obs.services.ObsConfiguration; -import com.obs.services.exception.ObsException; -import com.obs.services.internal.Constants; -import com.obs.services.internal.ObsConstraint; -import com.obs.services.internal.ObsProperties; -import com.obs.services.internal.ServiceException; -import com.obs.services.model.AuthTypeEnum; -import com.obs.services.model.HttpProtocolTypeEnum; + private static Pattern pattern = Pattern + .compile("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$"); -import okhttp3.Headers; - -public class ServiceUtils -{ - private static final ILogger log = LoggerBuilder.getLogger(ServiceUtils.class); - - protected static final String iso8601DateParserString = Constants.EXPIRATION_DATE_FORMATTER; - protected static final String iso8601DateMidnightParserString = "yyyy-MM-dd'T'00:00:00'Z'"; - protected static final String iso8601DateParser_WalrusString = "yyyy-MM-dd'T'HH:mm:ss"; - protected static final String rfc822DateParserString = Constants.HEADER_DATE_FORMATTER; - protected static final String _iso8601DateParserString = "yyyy-MM-dd"; - - private static Pattern pattern = Pattern.compile("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$"); - - - public static boolean isValid(String s) { - return s != null && !s.trim().equals(""); - } - - public static boolean isValid2(String s) { - return s != null && !s.equals(""); - } - - public static String toValid(String s) { - return s == null ? "" : s; - } - - public static void asserParameterNotNull(String value, String errorMessage) { - if (!isValid(value)) { - throw new IllegalArgumentException(errorMessage); - } - } - - public static void asserParameterNotNull2(String value, String errorMessage) { - if (value == null) { - throw new IllegalArgumentException(errorMessage); - } - } - - public static void asserParameterNotNull(Object value, String errorMessage) { - if (value == null) { - throw new IllegalArgumentException(errorMessage); - } - } - - public static void assertParameterNotNegative(long value, String errorMessage) { - if (value < 0) { - throw new IllegalArgumentException(errorMessage); - } - } - - public static Date parseIso8601Date(String dateString) throws ParseException - { - ParseException exception = null; - SimpleDateFormat iso8601DateParser = new SimpleDateFormat(iso8601DateParserString); - TimeZone gmt = Constants.GMT_TIMEZONE; - iso8601DateParser.setTimeZone(gmt); - try - { - return iso8601DateParser.parse(dateString); + public static boolean isValid(String s) { + return s != null && !s.equals(""); + } + + public static boolean isValid2(String s) { + return s != null && !s.equals(""); + } + + public static String toValid(String s) { + return s == null ? "" : s; + } + + public static void assertParameterNotNull(String value, String errorMessage) { + if (!isValid(value)) { + throw new IllegalArgumentException(errorMessage); } - catch (ParseException e) - { - exception = e; + } + + public static void assertParameterNotNull2(String value, String errorMessage) { + if (value == null) { + throw new IllegalArgumentException(errorMessage); } - SimpleDateFormat iso8601DateParser_Walrus = new SimpleDateFormat(iso8601DateParser_WalrusString); - iso8601DateParser_Walrus.setTimeZone(gmt); - try - { - return iso8601DateParser_Walrus.parse(dateString); + } + + public static void assertParameterNotNull(Object value, String errorMessage) { + if (value == null) { + throw new IllegalArgumentException(errorMessage); + } + } + + public static void assertParameterNotNegative(long value, String errorMessage) { + if (value < 0) { + throw new IllegalArgumentException(errorMessage); + } + } + + public static Date parseIso8601Date(String dateString) throws ParseException { + ParseException exception; + SimpleDateFormat iso8601TimeParser = new SimpleDateFormat(ISO_8601_TIME_PARSER_STRING); + TimeZone gmt = Constants.GMT_TIMEZONE; + iso8601TimeParser.setTimeZone(gmt); + try { + return iso8601TimeParser.parse(dateString); + } catch (ParseException e) { + exception = e; } - catch (ParseException e) - { - + SimpleDateFormat iso8601TimeParserWalrus = new SimpleDateFormat(ISO_8601_TIME_PARSER_WALRUS_STRING); + iso8601TimeParserWalrus.setTimeZone(gmt); + try { + return iso8601TimeParserWalrus.parse(dateString); + } catch (ParseException e) { + exception = e; } - // Throw original exception if the Walrus work-around doesn't save us. - - SimpleDateFormat _iso8601DateParser = new SimpleDateFormat(_iso8601DateParserString); - _iso8601DateParser.setTimeZone(gmt); - try{ - return _iso8601DateParser.parse(dateString); - }catch (Exception e) { + SimpleDateFormat iso8601DateParser = new SimpleDateFormat(ISO_8601_DATE_PARSER_STRING); + iso8601DateParser.setTimeZone(gmt); + try { + return iso8601DateParser.parse(dateString); + } catch (Exception e) { + log.warn("date parser failed.", e); } throw exception; - } - - public static String formatIso8601Date(Date date) - { - SimpleDateFormat iso8601DateParser = new SimpleDateFormat(iso8601DateParserString); - iso8601DateParser.setTimeZone(Constants.GMT_TIMEZONE); - return iso8601DateParser.format(date); - } - - public static String formatIso8601MidnightDate(Date date){ - SimpleDateFormat iso8601DateParser = new SimpleDateFormat(iso8601DateMidnightParserString); - iso8601DateParser.setTimeZone(Constants.GMT_TIMEZONE); - return iso8601DateParser.format(date); - } - - public static Date parseRfc822Date(String dateString) throws ParseException - { - SimpleDateFormat rfc822DateParser = new SimpleDateFormat(rfc822DateParserString, Locale.US); - rfc822DateParser.setTimeZone(Constants.GMT_TIMEZONE); - return rfc822DateParser.parse(dateString); - } - - public static String formatRfc822Date(Date date) - { - SimpleDateFormat rfc822DateParser = new SimpleDateFormat(rfc822DateParserString, Locale.US); - rfc822DateParser.setTimeZone(Constants.GMT_TIMEZONE); - return rfc822DateParser.format(date); - } - - public static String signWithHmacSha1(String sk, String canonicalString) throws ServiceException - { - SecretKeySpec signingKey = null; - try - { - signingKey = new SecretKeySpec(sk.getBytes(Constants.DEFAULT_ENCODING), Constants.HMAC_SHA1_ALGORITHM); - } - catch (UnsupportedEncodingException e) - { - throw new ServiceException("Unable to get bytes from secret string", e); - } - - // Acquire the MAC instance and initialize with the signing key. - Mac mac = null; - try - { - mac = Mac.getInstance(Constants.HMAC_SHA1_ALGORITHM); - } - catch (NoSuchAlgorithmException e) - { - // should not happen - throw new ServiceException("Could not find sha1 algorithm", e); - } - try - { - mac.init(signingKey); - } - catch (InvalidKeyException e) - { - // also should not happen - throw new RuntimeException("Could not initialize the MAC algorithm", e); - } - - // Compute the HMAC on the digest, and set it. - try - { - return ServiceUtils.toBase64(mac.doFinal(canonicalString.getBytes(Constants.DEFAULT_ENCODING))); - } - catch (UnsupportedEncodingException e) - { - throw new ServiceException("Unable to get bytes from canonical string", e); - } - } - - - public static Map cleanRestMetadataMap(Map> metadata, String headerPrefix, String metadataPrefix) - { - if (log.isDebugEnabled()) - { - log.debug("Cleaning up REST metadata items"); - } - Map cleanMap = new TreeMap(String.CASE_INSENSITIVE_ORDER); - if (metadata != null) - { - for (Map.Entry> entry : metadata.entrySet()) - { - String key = entry.getKey(); - List values = entry.getValue(); - - if(key == null || values == null) { - continue; - } - - Object value = values.size() == 1 ? values.get(0) : values; - if ((Constants.CommonHeaders.DATE.equalsIgnoreCase(key) || Constants.CommonHeaders.LAST_MODIFIED.equalsIgnoreCase(key))) - { - if (log.isDebugEnabled()) - { - log.debug("Parsing date string '" + value + "' into Date object for key: " + key); - } - try - { - value = ServiceUtils.parseRfc822Date(value.toString()); - } - catch (ParseException pe) - { - // Try ISO-8601 date format, just in case - try - { - value = ServiceUtils.parseIso8601Date(value.toString()); - } - catch (ParseException pe2) - { - // Log original exception if the work-around fails. - if (log.isWarnEnabled()) - { - log.warn("Date string is not RFC 822 compliant for metadata field " + key, pe); - } - } - } - } - else if (key.toLowerCase().startsWith(headerPrefix)) - { - try - { - if (key.toLowerCase().startsWith(metadataPrefix)){ - key = key.substring(metadataPrefix.length(), key.length()); - key = URLDecoder.decode(key, Constants.DEFAULT_ENCODING); - if (log.isDebugEnabled()) - { - log.debug("Removed meatadata header prefix " + metadataPrefix + " from key: " + key + "=>" + key); - } - }else { - key = key.substring(headerPrefix.length(), key.length()); - } - if(value instanceof List) { - List _values = new ArrayList(values.size()); - for(String _value : values) { - _values.add(_value != null ? URLDecoder.decode(_value, Constants.DEFAULT_ENCODING) : null); - } - value = _values; - }else { - value = URLDecoder.decode(value.toString(), Constants.DEFAULT_ENCODING); - } - } - catch (UnsupportedEncodingException e) - { - if (log.isDebugEnabled()) - { - log.debug("Error to decode value of key:" + key); - } - } - } - else if (key.toLowerCase().startsWith(Constants.OBS_HEADER_PREFIX)) - { - try - { - if (key.toLowerCase().startsWith(Constants.OBS_HEADER_META_PREFIX)){ - key = key.substring(Constants.OBS_HEADER_META_PREFIX.length(), key.length()); - key = URLDecoder.decode(key, Constants.DEFAULT_ENCODING); - if (log.isDebugEnabled()) - { - log.debug("Removed meatadata header prefix " + Constants.OBS_HEADER_META_PREFIX + " from key: " + key + "=>" + key); - } - }else { - key = key.substring(Constants.OBS_HEADER_PREFIX.length(), key.length()); - } - if(value instanceof List) { - List _values = new ArrayList(values.size()); - for(String _value : values) { - _values.add(_value != null ? URLDecoder.decode(_value, Constants.DEFAULT_ENCODING) : null); - } - value = _values; - }else { - value = URLDecoder.decode(value.toString(), Constants.DEFAULT_ENCODING); - } - } - catch (UnsupportedEncodingException e) - { - if (log.isDebugEnabled()) - { - log.debug("Error to decode value of key:" + key); - } - } - } - else if (Constants.ALLOWED_RESPONSE_HTTP_HEADER_METADATA_NAMES.contains(key.toLowerCase(Locale.getDefault()))) - { - if (log.isDebugEnabled()) - { - log.debug("Leaving HTTP header item unchanged: " + key + "=" + values); - } - } - else - { - if (log.isDebugEnabled()) - { - log.debug("Ignoring metadata item: " + key + "=" + values); - } - continue; - } - cleanMap.put(key, value); - } - } - return cleanMap; - } - - public static Map cleanRestMetadataMapV2(Map metadata, String headerPrefix, String metadataPrefix) - { - if (log.isDebugEnabled()) - { + } + + public static String formatIso8601Date(Date date) { + SimpleDateFormat iso8601TimeParser = new SimpleDateFormat(ISO_8601_TIME_PARSER_STRING); + iso8601TimeParser.setTimeZone(Constants.GMT_TIMEZONE); + return iso8601TimeParser.format(date); + } + + public static String formatIso8601MidnightDate(Date date) { + SimpleDateFormat iso8601TimeParser = new SimpleDateFormat(ISO_8601_TIME_MIDNING_PARSER_STRING); + iso8601TimeParser.setTimeZone(Constants.GMT_TIMEZONE); + return iso8601TimeParser.format(date); + } + + public static Date parseRfc822Date(String dateString) throws ParseException { + SimpleDateFormat rfc822TimeParser = new SimpleDateFormat(RFC_822_TIME_PARSER_STRING, Locale.US); + rfc822TimeParser.setTimeZone(Constants.GMT_TIMEZONE); + return rfc822TimeParser.parse(dateString); + } + + public static String formatRfc822Date(Date date) { + SimpleDateFormat rfc822TimeParser = new SimpleDateFormat(RFC_822_TIME_PARSER_STRING, Locale.US); + rfc822TimeParser.setTimeZone(Constants.GMT_TIMEZONE); + return rfc822TimeParser.format(date); + } + + public static String signWithHmacSha1(String sk, String canonicalString) throws ServiceException { + SecretKeySpec signingKey; + signingKey = new SecretKeySpec(sk.getBytes(StandardCharsets.UTF_8), Constants.HMAC_SHA1_ALGORITHM); + + Mac mac; + try { + mac = Mac.getInstance(Constants.HMAC_SHA1_ALGORITHM); + } catch (NoSuchAlgorithmException e) { + throw new ServiceException("Could not find sha1 algorithm", e); + } + try { + mac.init(signingKey); + } catch (InvalidKeyException e) { + throw new ObsException("Could not initialize the MAC algorithm", e); + } + + return ServiceUtils.toBase64(mac.doFinal(canonicalString.getBytes(StandardCharsets.UTF_8))); + } + + private static String transRealKey(String headerPrefix, String metadataPrefix, String key, boolean needDecode) + throws UnsupportedEncodingException { + if (key.toLowerCase().startsWith(metadataPrefix)) { + key = key.substring(metadataPrefix.length()); + if (needDecode) { + key = URLDecoder.decode(key, Constants.DEFAULT_ENCODING); + } + if (log.isDebugEnabled()) { + log.debug("Removed metadata header prefix " + metadataPrefix + " from key: " + key + + "=>" + key); + } + } else { + key = key.substring(headerPrefix.length()); + } + return key; + } + + public static Map cleanRestMetadataMapV2(Map metadata, String headerPrefix, + String metadataPrefix, boolean needDecode) { + if (log.isDebugEnabled()) { log.debug("Cleaning up REST metadata items"); } - Map cleanMap = new IdentityHashMap(); - if (metadata != null) - { - for (Map.Entry entry : metadata.entrySet()) - { + Map cleanMap = new IdentityHashMap<>(); + + if (metadata != null) { + for (Map.Entry entry : metadata.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); // Trim prefixes from keys. - key = key != null ? key.toString() : ""; - if (key.toLowerCase().startsWith(headerPrefix)) - { - try - { - if (key.toLowerCase().startsWith(metadataPrefix)){ - key = key.substring(metadataPrefix.length(), key.length()); - key = URLDecoder.decode(key, Constants.DEFAULT_ENCODING); - if (log.isDebugEnabled()) - { - log.debug("Removed meatadata header prefix " + metadataPrefix + " from key: " + key + "=>" + key); - } - }else { - key = key.substring(headerPrefix.length(), key.length()); - } - value = URLDecoder.decode(value.toString(), Constants.DEFAULT_ENCODING); - } - catch (UnsupportedEncodingException e) - { - if (log.isDebugEnabled()) - { + key = key != null ? key : ""; + if (key.toLowerCase().startsWith(headerPrefix)) { + try { + key = transRealKey(headerPrefix, metadataPrefix, key, needDecode); + if (needDecode) { + value = URLDecoder.decode(value, Constants.DEFAULT_ENCODING); + } + } catch (UnsupportedEncodingException | IllegalArgumentException e) { + if (log.isDebugEnabled()) { log.debug("Error to decode value of key:" + key); } } - - } - else if (key.toLowerCase().startsWith(Constants.OBS_HEADER_PREFIX)) - { - try - { - if (key.toLowerCase().startsWith(Constants.OBS_HEADER_META_PREFIX)){ - key = key.substring(Constants.OBS_HEADER_META_PREFIX.length(), key.length()); - key = URLDecoder.decode(key, Constants.DEFAULT_ENCODING); - if (log.isDebugEnabled()) - { - log.debug("Removed meatadata header prefix " + Constants.OBS_HEADER_META_PREFIX + " from key: " + key + "=>" + key); - } - }else { - key = key.substring(Constants.OBS_HEADER_PREFIX.length(), key.length()); - } - value = URLDecoder.decode(value.toString(), Constants.DEFAULT_ENCODING); - } - catch (UnsupportedEncodingException e) - { - if (log.isDebugEnabled()) - { + + } else if (key.toLowerCase().startsWith(Constants.OBS_HEADER_PREFIX)) { + try { + key = transRealKey(Constants.OBS_HEADER_PREFIX, + Constants.OBS_HEADER_META_PREFIX, key, needDecode); + if (needDecode) { + value = URLDecoder.decode(value, Constants.DEFAULT_ENCODING); + } + } catch (UnsupportedEncodingException | IllegalArgumentException e) { + if (log.isDebugEnabled()) { log.debug("Error to decode value of key:" + key); } } - } - else if (Constants.ALLOWED_RESPONSE_HTTP_HEADER_METADATA_NAMES.contains(key.toLowerCase(Locale.getDefault()))) - { - if (log.isDebugEnabled()) - { + } else if (Constants.ALLOWED_RESPONSE_HTTP_HEADER_METADATA_NAMES + .contains(key.toLowerCase(Locale.getDefault()))) { + if (log.isDebugEnabled()) { log.debug("Leaving HTTP header item unchanged: " + key + "=" + value); } - } - else - { - if (log.isDebugEnabled()) - { + } else { + if (log.isDebugEnabled()) { log.debug("Ignoring metadata item: " + key + "=" + value); } continue; } - cleanMap.put(new String(key), value); + // FIXME + cleanMap.put(key, value); } } return cleanMap; } - public static String toHex(byte[] data) - { - StringBuilder sb = new StringBuilder(data.length * 2); - for (int i = 0; i < data.length; i++) - { - String hex = Integer.toHexString(data[i]); - if (hex.length() == 1) - { - // Append leading zero. - sb.append("0"); - } - else if (hex.length() == 8) - { - // Remove ff prefix from negative numbers. - hex = hex.substring(6); - } - sb.append(hex); - } - return sb.toString().toLowerCase(Locale.getDefault()); - } - - public static byte[] fromHex(String hexData) - { - if ((hexData.length() & 1) != 0 || hexData.replaceAll("[a-fA-F0-9]", "").length() > 0) - { - throw new java.lang.IllegalArgumentException("'" + hexData + "' is not a hex string"); - } - - byte[] result = new byte[(hexData.length() + 1) / 2]; - String hexNumber = null; - int stringOffset = 0; - int byteOffset = 0; - while (stringOffset < hexData.length()) - { - hexNumber = hexData.substring(stringOffset, stringOffset + 2); - stringOffset += 2; - result[byteOffset++] = (byte) Integer.parseInt(hexNumber, 16); - } - return result; - } - - public static String toBase64(byte[] data) - { - return ReflectUtils.toBase64(data); - } - - public static String join(Object[] items, String delimiter) - { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < items.length; i++) - { - sb.append(items[i]); - if (i < items.length - 1) - { - sb.append(delimiter); - } - } - return sb.toString(); - } - - public static String join(List items, String delimiter, boolean needTrim) - { + public static Map cleanUserMetadata(Map originalHeaders, boolean decodeHeaders) { + Map userMetadata = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + for (Map.Entry entry : originalHeaders.entrySet()) { + String key = entry.getKey(); + if (key.toLowerCase().startsWith("x-obs-meta-") || key.toLowerCase().startsWith("x-amz-meta-")) { + Object originalValue = originalHeaders.get(key); + try { + if (originalValue instanceof ArrayList) { + cleanListMetadata(originalHeaders, decodeHeaders, userMetadata, key); + } else { + if (decodeHeaders) { + userMetadata.put(key.substring(11), URLDecoder.decode((String) originalValue, + Constants.DEFAULT_ENCODING)); + } else { + userMetadata.put(key.substring(11), originalHeaders.get(key)); + } + } + } catch (UnsupportedEncodingException | IllegalArgumentException e) { + if (log.isDebugEnabled()) { + log.debug("Error to decode value of key:" + key); + } + } + } + } + return userMetadata; + } + + public static void cleanListMetadata(Map originalHeaders, boolean decodeHeaders, + Map userMetadata, String key) + throws UnsupportedEncodingException { + List cleanedValue = new ArrayList<>(); + for (Object v : (List) originalHeaders.get(key)) { + if (decodeHeaders) { + cleanedValue.add(URLDecoder.decode((String) v, Constants.DEFAULT_ENCODING)); + } else { + cleanedValue.add((String) v); + } + } + userMetadata.put(key.substring(11), cleanedValue); + } + + public static String toHex(byte[] data) { + if (null == data) { + return null; + } + if (data.length <= 0) { + return ""; + } + StringBuilder sb = new StringBuilder(); + for (byte datum : data) { + String hv = Integer.toHexString(datum); + if (hv.length() < 2) { + sb.append("0"); + } else if (hv.length() == 8) { + hv = hv.substring(6); + } + sb.append(hv); + } + return sb.toString().toLowerCase(Locale.getDefault()); + } + + public static byte[] fromHex(String hexData) { + if (null == hexData) { + return new byte[0]; + } + + if ((hexData.length() & 1) != 0 || hexData.replaceAll("[a-fA-F0-9]", "").length() > 0) { + throw new java.lang.IllegalArgumentException("'" + hexData + "' is not a hex string"); + } + + byte[] result = new byte[(hexData.length() + 1) / 2]; + String hexNumber; + int offset = 0; + int byteIndex = 0; + while (offset < hexData.length()) { + hexNumber = hexData.substring(offset, offset + 2); + offset += 2; + result[byteIndex++] = (byte) Integer.parseInt(hexNumber, 16); + } + return result; + } + + public static String toBase64(byte[] data) { + return ReflectUtils.toBase64(data); + } + + public static String join(List items, String delimiter, boolean needTrim) { StringBuilder sb = new StringBuilder(); - for (int i = 0; i < items.size(); i++) - { - String item = items.get(i).toString(); + for (int i = 0; i < items.size(); i++) { + String item = items.get(i).toString(); sb.append(needTrim ? item.trim() : item); - if (i < items.size() - 1) - { + if (i < items.size() - 1) { sb.append(delimiter); } } return sb.toString(); } - - public static String join(List items, String delimiter) - { - return join(items, delimiter, false); - } - - public static String join(Headers headers, String delimiter, List excludes) - { - if(excludes == null){ - excludes = new ArrayList(); + + public static String join(List items, String delimiter) { + return join(items, delimiter, false); + } + + public static byte[] fromBase64(String b64Data) throws UnsupportedEncodingException { + return ReflectUtils.fromBase64(b64Data); + } + + public static byte[] computeMD5Hash(InputStream is) throws NoSuchAlgorithmException, IOException { + BufferedInputStream bis = null; + try { + bis = new BufferedInputStream(is); + MessageDigest messageDigest = MessageDigest.getInstance("MD5"); + byte[] buffer = new byte[16384]; + int bytesRead = -1; + while ((bytesRead = bis.read(buffer, 0, buffer.length)) != -1) { + messageDigest.update(buffer, 0, bytesRead); + } + return messageDigest.digest(); + } finally { + if (bis != null) { + try { + bis.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } } - StringBuilder sb = new StringBuilder(); - Map> map = headers.toMultimap(); - int i = 0; - for (Map.Entry> entry : map.entrySet()) - { - if(!excludes.contains(entry.getKey())){ - sb.append(entry.getValue()); + } + + public static byte[] computeMD5Hash(InputStream is, long length, long offset) + throws NoSuchAlgorithmException, IOException { + BufferedInputStream bis = null; + try { + bis = new BufferedInputStream(is); + if (offset > 0) { + long skipByte = bis.skip(offset); + if (log.isDebugEnabled()) { + log.debug("computeMD5Hash: Skip " + skipByte + " bytes"); + } } - if (i < map.size() - 1) - { - sb.append(delimiter); + MessageDigest messageDigest = MessageDigest.getInstance("MD5"); + byte[] buffer = new byte[16384]; + int bytesRead = -1; + long readLen = 0; + long bufLen = 16384 > length ? length : 16384; + while (readLen < length && (bytesRead = bis.read(buffer, 0, (int) bufLen)) != -1) { + messageDigest.update(buffer, 0, bytesRead); + readLen += bytesRead; + bufLen = (length - readLen) > 16384 ? 16384 : (length - readLen); + } + return messageDigest.digest(); + } finally { + if (bis != null) { + try { + bis.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } } } - return sb.toString(); } - - - public static String join(int[] ints, String delimiter) - { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < ints.length; i++) - { - sb.append(ints[i]); - if (i < ints.length - 1) - { - sb.append(delimiter); - } - } - return sb.toString(); - } - - public static byte[] fromBase64(String b64Data) throws UnsupportedEncodingException - { - return ReflectUtils.fromBase64(b64Data); - } - - public static byte[] computeMD5Hash(InputStream is) throws NoSuchAlgorithmException, IOException - { - BufferedInputStream bis = new BufferedInputStream(is); - try - { - MessageDigest messageDigest = MessageDigest.getInstance("MD5"); - byte[] buffer = new byte[16384]; - int bytesRead = -1; - while ((bytesRead = bis.read(buffer, 0, buffer.length)) != -1) - { - messageDigest.update(buffer, 0, bytesRead); - } - return messageDigest.digest(); - } - finally - { - closeStream(bis); - } - } - - public static byte[] computeMD5Hash(InputStream is, long length, long offset) throws NoSuchAlgorithmException, IOException - { - BufferedInputStream bis = null; - try - { - bis = new BufferedInputStream(is); - if(offset > 0){ - long skipByte = bis.skip(offset); - if(log.isDebugEnabled()){ - log.debug("computeMD5Hash: Skip " + skipByte + " bytes"); - } - } - MessageDigest messageDigest = MessageDigest.getInstance("MD5"); - byte[] buffer = new byte[16384]; - int bytesRead = -1; - long readLen = 0; - long bufLen = 16384 > length ? length : 16384; - while (readLen < length && (bytesRead = bis.read(buffer, 0, (int) bufLen)) != -1) - { - messageDigest.update(buffer, 0, bytesRead); - readLen += bytesRead; - bufLen = (length - readLen) > 16384 ? 16384 : (length - readLen); - } - return messageDigest.digest(); - } - finally - { - closeStream(bis); - } - } - - public static String computeMD5(String data) throws ServiceException - { - try - { - return ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(data.getBytes(Constants.DEFAULT_ENCODING))); - } - catch (NoSuchAlgorithmException e) - { + + public static String computeMD5(String data) throws ServiceException { + try { + return ServiceUtils.toBase64(ServiceUtils.computeMD5Hash(data.getBytes(StandardCharsets.UTF_8))); + } catch (NoSuchAlgorithmException | IOException e) { throw new ServiceException("Failed to get MD5 for requestXmlElement:" + data); } - catch (UnsupportedEncodingException e) - { - throw new ServiceException("Failed to get MD5 for requestXmlElement:" + data); + } + + public static byte[] computeMD5Hash(byte[] data) throws NoSuchAlgorithmException, IOException { + return computeMD5Hash(new ByteArrayInputStream(data)); + } + + public static boolean isBucketNameValidDNSName(String bucketName) { + if (bucketName == null || bucketName.length() > 63 || bucketName.length() < 3) { + return false; } - catch (IOException e) - { - throw new ServiceException("Failed to get MD5 for requestXmlElement:" + data); + + if (!Pattern.matches("^[a-z0-9][a-z0-9.-]+$", bucketName)) { + return false; + } + + if (Pattern.matches("(\\d{1,3}\\.){3}\\d{1,3}", bucketName)) { + return false; + } + + String[] fragments = bucketName.split("\\."); + for (String fragment : fragments) { + if (Pattern.matches("^-.*", fragment) || Pattern.matches(".*-$", fragment) + || Pattern.matches("^$", fragment)) { + return false; + } + } + + return true; + } + + public static String generateHostnameForBucket(String bucketName, boolean pathStyle, String endpoint) { + if (!isBucketNameValidDNSName(bucketName)) { + throw new IllegalArgumentException("the bucketName is illegal"); + } + + if (!pathStyle) { + return bucketName + "." + endpoint; + } else { + return endpoint; + } + } + + public static XMLReader loadXMLReader() throws ServiceException { + Exception ex; + try { + XMLReader reader = XMLReaderFactory.createXMLReader(); + try { + reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + reader.setFeature("http://xml.org/sax/features/external-general-entities", false); + reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + } catch (Exception e) { + log.warn("Enable protection for XML External Entity Injection Failed"); + } + return reader; + } catch (Exception e) { + // Ignore failure + ex = e; } + + String[] altXmlReaderClasspaths = new String[]{"org.apache.crimson." + + "parser.XMLReaderImpl", "org.xmlpull.v1.sax2.Driver"}; + for (String xmlReaderClasspath : altXmlReaderClasspaths) { + try { + XMLReader reader = XMLReaderFactory.createXMLReader(xmlReaderClasspath); + try { + reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + reader.setFeature("http://xml.org/sax/features/external-general-entities", false); + reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + } catch (Exception e) { + log.warn("Enable protection for XML External Entity Injection Failed"); + } + + return reader; + } catch (Exception e) { + // Ignore failure + } + } + // If we haven't found and returned an XMLReader yet, give up. + throw new ServiceException("Failed to initialize a SAX XMLReader", ex); } - public static byte[] computeMD5Hash(byte[] data) throws NoSuchAlgorithmException, IOException - { - return computeMD5Hash(new ByteArrayInputStream(data)); - } - - public static boolean isBucketNameValidDNSName(String bucketName) - { - if (bucketName == null || bucketName.length() > 63 || bucketName.length() < 3) - { - return false; - } - - // Only lower-case letters, numbers, '.' or '-' characters allowed - if (!Pattern.matches("^[a-z0-9][a-z0-9.-]+$", bucketName)) - { - return false; - } - - // Cannot be an IP address, i.e. must not contain four '.'-delimited - // sections with 1 to 3 digits each. - if (Pattern.matches("([0-9]{1,3}\\.){3}[0-9]{1,3}", bucketName)) - { - return false; - } - - // Components of name between '.' characters cannot start or end with '-', - // and cannot be empty - String[] fragments = bucketName.split("\\."); - for (int i = 0; i < fragments.length; i++) - { - if (Pattern.matches("^-.*", fragments[i]) || Pattern.matches(".*-$", fragments[i]) || Pattern.matches("^$", fragments[i])) - { - return false; - } - } - - return true; - } - - public static String generateHostnameForBucket(String bucketName, boolean pathStyle, String endpoint) - { - if(!isBucketNameValidDNSName(bucketName)) - { - throw new IllegalArgumentException("the bucketName is illegal"); - } - - if (!pathStyle) - { - return bucketName + "." + endpoint; - } - else - { - return endpoint; - } - } - - public static XMLReader loadXMLReader() throws ServiceException - { - Exception ex; - try - { - return XMLReaderFactory.createXMLReader(); - } - catch (Exception e) - { - // Ignore failure - ex = e; - } - - // No dice using the standard approach, try loading alternatives... - String[] altXmlReaderClasspaths = new String[] { "org.apache.crimson.parser.XMLReaderImpl", // JDK 1.4 - "org.xmlpull.v1.sax2.Driver", // Android - }; - for (int i = 0; i < altXmlReaderClasspaths.length; i++) - { - String xmlReaderClasspath = altXmlReaderClasspaths[i]; - try - { - return XMLReaderFactory.createXMLReader(xmlReaderClasspath); - } - catch (Exception e) - { - // Ignore failure - } - } - // If we haven't found and returned an XMLReader yet, give up. - throw new ServiceException("Failed to initialize a SAX XMLReader", ex); - } - - public static SimpleDateFormat getShortDateFormat() - { + public static SimpleDateFormat getShortDateFormat() { SimpleDateFormat format = new SimpleDateFormat(Constants.SHORT_DATE_FORMATTER); format.setTimeZone(Constants.GMT_TIMEZONE); return format; } - - public static SimpleDateFormat getLongDateFormat() - { + + public static SimpleDateFormat getLongDateFormat() { SimpleDateFormat format = new SimpleDateFormat(Constants.LONG_DATE_FORMATTER); format.setTimeZone(Constants.GMT_TIMEZONE); return format; } - - public static SimpleDateFormat getHeaderDateFormat() - { - SimpleDateFormat format = new SimpleDateFormat(Constants.HEADER_DATE_FORMATTER); - format.setTimeZone(Constants.GMT_TIMEZONE); - return format; - } - - public static SimpleDateFormat getExpirationDateFormat() - { + + public static SimpleDateFormat getExpirationDateFormat() { SimpleDateFormat format = new SimpleDateFormat(Constants.EXPIRATION_DATE_FORMATTER); format.setTimeZone(Constants.GMT_TIMEZONE); return format; } - - public static ObsException changeFromServiceException(ServiceException se) - { + + public static ObsException changeFromServiceException(ServiceException se) { ObsException exception; - if (se.getResponseCode() < 0) - { + if (se.getResponseCode() < 0) { exception = new ObsException("OBS servcie Error Message. " + se.getMessage(), se.getCause()); - } - else - { - exception = new ObsException((se.getMessage() != null ? "Error message:" + se.getMessage() : "") + "OBS servcie Error Message.", se.getXmlMessage(), se.getCause()); + } else { + exception = new ObsException( + (se.getMessage() != null ? "Error message:" + se.getMessage() : "") + "OBS servcie Error Message.", + se.getXmlMessage(), se.getCause()); exception.setErrorCode(se.getErrorCode()); exception.setErrorMessage(se.getErrorMessage() == null ? se.getMessage() : se.getErrorMessage()); exception.setErrorRequestId(se.getErrorRequestId()); @@ -767,81 +542,94 @@ public static ObsException changeFromServiceException(ServiceException se) } return exception; } - + public static void closeStream(Closeable closeable) { - if(closeable != null) { - try { - closeable.close(); - } catch (IOException e) { - if(log.isWarnEnabled()) { - log.warn(e); - } - } - } - } - - public static String toString(InputStream in) throws IOException{ - String ret = null; - if(in != null){ - StringBuilder sb = new StringBuilder(); - BufferedReader br = null; - try{ - br = new BufferedReader(new InputStreamReader(in, Constants.DEFAULT_ENCODING)); - String temp; - while((temp=br.readLine())!=null){ - sb.append(temp); - } - ret = sb.toString(); - }finally{ - closeStream(br); - closeStream(in); - } - } - return ret; - } - - public static ObsProperties changeFromObsConfiguration(ObsConfiguration config) - { + if (closeable != null) { + try { + closeable.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + } + + public static String toString(InputStream in) throws IOException { + String ret = null; + if (in != null) { + StringBuilder sb = new StringBuilder(); + BufferedReader br = null; + try { + br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); + String temp; + while ((temp = br.readLine()) != null) { + sb.append(temp); + } + ret = sb.toString(); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + if (log.isWarnEnabled()) { + log.warn("close failed.", e); + } + } + } + closeStream(in); + } + } + return ret; + } + + public static ObsProperties changeFromObsConfiguration(ObsConfiguration config) { ObsProperties obsProperties = new ObsProperties(); - + String endPoint = config.getEndPoint(); - + int index; - while((index = endPoint.lastIndexOf("/")) == endPoint.length()-1) { - endPoint = endPoint.substring(0, index); - } - - if(endPoint.startsWith("http://")) { - config.setHttpsOnly(false); - endPoint = endPoint.substring("http://".length()); - }else if(endPoint.startsWith("https://")) { - config.setHttpsOnly(true); - endPoint = endPoint.substring("https://".length()); - } - - if((index = endPoint.lastIndexOf(":")) > 0) { - int port = Integer.parseInt(endPoint.substring(index + 1)); - if(config.isHttpsOnly()) { - config.setEndpointHttpsPort(port); - }else { - config.setEndpointHttpPort(port); - } - endPoint = endPoint.substring(0, index); - } - + while ((index = endPoint.lastIndexOf("/")) == endPoint.length() - 1) { + endPoint = endPoint.substring(0, index); + } + + if (endPoint.startsWith("http://")) { + config.setHttpsOnly(false); + endPoint = endPoint.substring("http://".length()); + } else if (endPoint.startsWith("https://")) { + config.setHttpsOnly(true); + endPoint = endPoint.substring("https://".length()); + } + + if ((index = endPoint.lastIndexOf(":")) > 0) { + int port = Integer.parseInt(endPoint.substring(index + 1)); + if (config.isHttpsOnly()) { + config.setEndpointHttpsPort(port); + } else { + config.setEndpointHttpPort(port); + } + endPoint = endPoint.substring(0, index); + } + Matcher m = pattern.matcher(endPoint); - if(m.matches()) { - config.setPathStyle(true); + if (m.matches()) { + config.setPathStyle(true); } - - if(config.isPathStyle() || config.isCname()) { - config.setAuthTypeNegotiation(false); - if(config.getAuthType() == AuthTypeEnum.OBS) { - config.setAuthType(AuthTypeEnum.V2); - } + + if (config.isPathStyle() || config.isCname()) { + config.setAuthTypeNegotiation(false); + if (config.getAuthType() == AuthTypeEnum.OBS) { + config.setAuthType(AuthTypeEnum.V2); + } } - + config.setEndPoint(endPoint); + setObsProperties(config, obsProperties); + + return obsProperties; + } + + private static void setObsProperties(ObsConfiguration config, ObsProperties obsProperties) { obsProperties.setProperty(ObsConstraint.END_POINT, config.getEndPoint()); obsProperties.setProperty(ObsConstraint.HTTP_PORT, String.valueOf(config.getEndpointHttpPort())); obsProperties.setProperty(ObsConstraint.HTTPS_ONLY, String.valueOf(config.isHttpsOnly())); @@ -852,46 +640,87 @@ public static ObsProperties changeFromObsConfiguration(ObsConfiguration config) obsProperties.setProperty(ObsConstraint.HTTP_RETRY_MAX, String.valueOf(config.getMaxErrorRetry())); obsProperties.setProperty(ObsConstraint.HTTP_CONNECT_TIMEOUT, String.valueOf(config.getConnectionTimeout())); obsProperties.setProperty(ObsConstraint.PROXY_ISABLE, String.valueOf(Boolean.FALSE)); - obsProperties.setProperty(ObsConstraint.BUFFER_STREAM, String.valueOf(config.getUploadStreamRetryBufferSize() > 0 ? config.getUploadStreamRetryBufferSize(): ObsConstraint.DEFAULT_BUFFER_STREAM)); +// obsProperties.setProperty(ObsConstraint.BUFFER_STREAM, +// String.valueOf(config.getUploadStreamRetryBufferSize() > 0 ? config.getUploadStreamRetryBufferSize() +// : ObsConstraint.DEFAULT_BUFFER_STREAM)); obsProperties.setProperty(ObsConstraint.VALIDATE_CERTIFICATE, String.valueOf(config.isValidateCertificate())); - obsProperties.setProperty(ObsConstraint.VERIFY_RESPONSE_CONTENT_TYPE, String.valueOf(config.isVerifyResponseContentType())); + obsProperties.setProperty(ObsConstraint.VERIFY_RESPONSE_CONTENT_TYPE, + String.valueOf(config.isVerifyResponseContentType())); obsProperties.setProperty(ObsConstraint.WRITE_BUFFER_SIZE, String.valueOf(config.getWriteBufferSize())); obsProperties.setProperty(ObsConstraint.READ_BUFFER_SIZE, String.valueOf(config.getReadBufferSize())); - obsProperties.setProperty(ObsConstraint.SOCKET_WRITE_BUFFER_SIZE, - String.valueOf(config.getSocketWriteBufferSize())); - obsProperties.setProperty(ObsConstraint.SOCKET_READ_BUFFER_SIZE, - String.valueOf(config.getSocketReadBufferSize())); - obsProperties.setProperty(ObsConstraint.HTTP_STRICT_HOSTNAME_VERIFICATION, String.valueOf(config.isStrictHostnameVerification())); + obsProperties.setProperty(ObsConstraint.SOCKET_WRITE_BUFFER_SIZE, + String.valueOf(config.getSocketWriteBufferSize())); + obsProperties.setProperty(ObsConstraint.SOCKET_READ_BUFFER_SIZE, + String.valueOf(config.getSocketReadBufferSize())); + obsProperties.setProperty(ObsConstraint.HTTP_STRICT_HOSTNAME_VERIFICATION, + String.valueOf(config.isStrictHostnameVerification())); obsProperties.setProperty(ObsConstraint.HTTP_IDLE_CONNECTION_TIME, - String.valueOf(config.getIdleConnectionTime())); + String.valueOf(config.getIdleConnectionTime())); obsProperties.setProperty(ObsConstraint.HTTP_MAX_IDLE_CONNECTIONS, - String.valueOf(config.getMaxIdleConnections())); - obsProperties.setProperty(ObsConstraint.SSL_PROVIDER, config.getSslProvider() == null ? "": config.getSslProvider()); + String.valueOf(config.getMaxIdleConnections())); + obsProperties.setProperty(ObsConstraint.SSL_PROVIDER, + config.getSslProvider() == null ? "" : config.getSslProvider()); obsProperties.setProperty(ObsConstraint.KEEP_ALIVE, String.valueOf(config.isKeepAlive())); - obsProperties.setProperty(ObsConstraint.FS_DELIMITER, config.getDelimiter() == null ? "/" : config.getDelimiter()); - obsProperties.setProperty(ObsConstraint.HTTP_PROTOCOL, config.getHttpProtocolType() == null ? HttpProtocolTypeEnum.HTTP1_1.getCode() : config.getHttpProtocolType().getCode()); - + obsProperties.setProperty(ObsConstraint.FS_DELIMITER, + config.getDelimiter() == null ? "/" : config.getDelimiter()); + obsProperties.setProperty(ObsConstraint.HTTP_PROTOCOL, config.getHttpProtocolType() == null + ? HttpProtocolTypeEnum.HTTP1_1.getCode() : config.getHttpProtocolType().getCode()); + obsProperties.setProperty(ObsConstraint.IS_CNAME, String.valueOf(config.isCname())); obsProperties.setProperty(ObsConstraint.AUTH_TYPE_NEGOTIATION, String.valueOf(config.isAuthTypeNegotiation())); - if (null != config.getHttpProxy()) - { + if (null != config.getHttpProxy()) { obsProperties.setProperty(ObsConstraint.PROXY_ISABLE, String.valueOf(Boolean.TRUE)); obsProperties.setProperty(ObsConstraint.PROXY_HOST, config.getHttpProxy().getProxyAddr()); obsProperties.setProperty(ObsConstraint.PROXY_PORT, String.valueOf(config.getHttpProxy().getProxyPort())); obsProperties.setProperty(ObsConstraint.PROXY_UNAME, config.getHttpProxy().getProxyUName()); - obsProperties.setProperty(ObsConstraint.PROXY_PAWD, config.getHttpProxy().getUserPaaswd()); + obsProperties.setProperty(ObsConstraint.PROXY_PAWD, config.getHttpProxy().getUserPasswd()); obsProperties.setProperty(ObsConstraint.PROXY_DOMAIN, config.getHttpProxy().getDomain()); obsProperties.setProperty(ObsConstraint.PROXY_WORKSTATION, config.getHttpProxy().getWorkstation()); } - - return obsProperties; + + if (config instanceof ExtObsConfiguration) { + // retry in okhttp + obsProperties.setProperty(ExtObsConstraint.IS_RETRY_ON_CONNECTION_FAILURE_IN_OKHTTP, + String.valueOf(((ExtObsConfiguration) config).isRetryOnConnectionFailureInOkhttp())); + + // retry on unexpected end exception + obsProperties.setProperty(ExtObsConstraint.HTTP_MAX_RETRY_ON_UNEXPECTED_END_EXCEPTION, + String.valueOf(((ExtObsConfiguration) config).getMaxRetryOnUnexpectedEndException())); + } + if (null != config.getXmlDocumentBuilderFactoryClass() + && !config.getXmlDocumentBuilderFactoryClass().trim().equals("")) { + obsProperties.setProperty(ObsConstraint.OBS_XML_DOC_BUILDER_FACTORY, + config.getXmlDocumentBuilderFactoryClass()); + } } - + public static Date cloneDateIgnoreNull(Date date) { - if(null == date) { - return null; - } else { - return (Date) date.clone(); - } + if (null == date) { + return null; + } else { + return (Date) date.clone(); + } + } + + public static void deleteFileIgnoreException(String path) { + if (null == path) { + return; + } + + if (!deleteFileIgnoreException(new File(path))) { + log.warn("delete file '" + path + "' failed"); + } + } + + public static boolean deleteFileIgnoreException(File file) { + if (null == file) { + return true; + } + + if (file.exists() && file.isFile()) { + return file.delete(); + } + + return true; } } diff --git a/app/src/main/java/com/obs/services/internal/utils/V2Authentication.java b/app/src/main/java/com/obs/services/internal/utils/V2Authentication.java index 4a15088..7ca8d7e 100644 --- a/app/src/main/java/com/obs/services/internal/utils/V2Authentication.java +++ b/app/src/main/java/com/obs/services/internal/utils/V2Authentication.java @@ -11,32 +11,32 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; import com.obs.services.internal.IHeaders; import com.obs.services.internal.V2Headers; -public class V2Authentication extends AbstractAuthentication -{ - - private static V2Authentication instance = new V2Authentication(); - - private V2Authentication() { - - } - - public static AbstractAuthentication getInstance() { - return instance; - } - - @Override - protected IHeaders getIHeaders() { - return V2Headers.getInstance(); - } - - @Override - protected String getAuthPrefix() { - return "AWS"; - } - +public class V2Authentication extends AbstractAuthentication { + + private static V2Authentication instance = new V2Authentication(); + + private V2Authentication() { + + } + + public static AbstractAuthentication getInstance() { + return instance; + } + + @Override + protected IHeaders getIHeaders() { + return V2Headers.getInstance(); + } + + @Override + protected String getAuthPrefix() { + return "AWS"; + } + } diff --git a/app/src/main/java/com/obs/services/internal/utils/V4Authentication.java b/app/src/main/java/com/obs/services/internal/utils/V4Authentication.java index a7b215e..cd8d6d5 100644 --- a/app/src/main/java/com/obs/services/internal/utils/V4Authentication.java +++ b/app/src/main/java/com/obs/services/internal/utils/V4Authentication.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.internal.utils; import com.obs.services.internal.Constants; @@ -22,261 +23,231 @@ import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; -import java.util.*; +import java.util.Map; +import java.util.TreeMap; +import java.util.Date; +import java.util.List; +import java.util.ArrayList; +import java.util.Locale; + +public class V4Authentication { + public static final String CONTENT_SHA256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; -public class V4Authentication -{ - - public static final String content_sha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; - private String ak; - + private String sk; - + private String region; - + private String nowISOtime; - - protected V4Authentication() - { + + protected V4Authentication() { } - - public String getAk() - { + + public String getAk() { return ak; } - - public void setAk(String ak) - { + + public void setAk(String ak) { this.ak = ak; } - - public String getSk() - { + + public String getSk() { return sk; } - - public void setSk(String sk) - { + + public void setSk(String sk) { this.sk = sk; } - - public String getRegion() - { + + public String getRegion() { return region; } - - public void setRegion(String region) - { + + public void setRegion(String region) { this.region = region; } - - public static String caculateSignature(String stringToSign, String shortDate, String sk) throws Exception { - byte[] dateKey = V4Authentication.hmac_sha256Encode( - ("AWS4" + sk).getBytes(Constants.DEFAULT_ENCODING), - shortDate); - byte[] dataRegionKey = V4Authentication.hmac_sha256Encode(dateKey, ObsConstraint.DEFAULT_BUCKET_LOCATION_VALUE); - - byte[] dateRegionServiceKey = V4Authentication.hmac_sha256Encode(dataRegionKey, Constants.SERVICE); - - byte[] signingKey = V4Authentication.hmac_sha256Encode(dateRegionServiceKey, Constants.REQUEST_TAG); - - return V4Authentication.byteToHex(V4Authentication.hmac_sha256Encode(signingKey, stringToSign)); - } - - public static IAuthentication makeServiceCanonicalString(String method, Map headers, String strURIPath, - ProviderCredentials credent, Date date, BasicSecurityKey securityKey) - throws ServiceException - { + + public static String caculateSignature(String stringToSign, String shortDate, String sk) throws Exception { + byte[] dateKey = V4Authentication.hmacSha256Encode(("AWS4" + sk).getBytes(StandardCharsets.UTF_8), + shortDate); + byte[] dataRegionKey = V4Authentication.hmacSha256Encode(dateKey, ObsConstraint.DEFAULT_BUCKET_LOCATION_VALUE); + + byte[] dateRegionServiceKey = V4Authentication.hmacSha256Encode(dataRegionKey, Constants.SERVICE); + + byte[] signingKey = V4Authentication.hmacSha256Encode(dateRegionServiceKey, Constants.REQUEST_TAG); + + return V4Authentication.byteToHex(V4Authentication.hmacSha256Encode(signingKey, stringToSign)); + } + + public static IAuthentication makeServiceCanonicalString(String method, Map headers, + String strURIPath, ProviderCredentials credent, Date date, BasicSecurityKey securityKey) + throws ServiceException { V4Authentication v4 = new V4Authentication(); v4.setAk(securityKey.getAccessKey()); v4.setSk(securityKey.getSecretKey()); v4.setRegion(credent.getRegion()); v4.setNowISOTime(date); - + List signedAndCanonicalList = v4.getSignedAndCanonicalHeaders(headers); - + String scope = v4.getScope(); - try - { + try { String canonicalRequest = v4.getCanonicalRequest(method, strURIPath, signedAndCanonicalList); - String stringToSign = new StringBuilder(Constants.V4_ALGORITHM).append("\n").append(v4.nowISOtime).append("\n").append(scope).append("\n").append(V4Authentication.byteToHex(V4Authentication.sha256encode(canonicalRequest))).toString(); - String signature = V4Authentication.byteToHex(V4Authentication.hmac_sha256Encode(v4.getSigningKey(), stringToSign)); - String auth = new StringBuilder(Constants.V4_ALGORITHM).append(" Credential=").append(v4.ak).append("/").append(scope) - .append(",SignedHeaders=").append(signedAndCanonicalList.get(0)).append(",Signature=").append(signature).toString(); + String stringToSign = new StringBuilder(Constants.V4_ALGORITHM).append("\n").append(v4.nowISOtime) + .append("\n").append(scope).append("\n") + .append(V4Authentication.byteToHex(V4Authentication.sha256encode(canonicalRequest))).toString(); + String signature = V4Authentication + .byteToHex(V4Authentication.hmacSha256Encode(v4.getSigningKey(), stringToSign)); + String auth = new StringBuilder(Constants.V4_ALGORITHM).append(" Credential=").append(v4.ak).append("/") + .append(scope).append(",SignedHeaders=").append(signedAndCanonicalList.get(0)).append(",Signature=") + .append(signature).toString(); return new DefaultAuthentication(canonicalRequest, stringToSign, auth); - } - catch (Exception e) - { + } catch (Exception e) { throw new ServiceException("has an err when V4 aurhentication ", e); } } - - private void setNowISOTime(Date headerDate) - { + + private void setNowISOTime(Date headerDate) { SimpleDateFormat fmt1 = new SimpleDateFormat(Constants.LONG_DATE_FORMATTER); fmt1.setTimeZone(Constants.GMT_TIMEZONE); this.nowISOtime = fmt1.format(headerDate); } - - - private List getSignedAndCanonicalHeaders(Map headers) - { + + private List getSignedAndCanonicalHeaders(Map headers) { List list = new ArrayList(); - StringBuilder Signed = new StringBuilder(); - StringBuilder Canonical = new StringBuilder(); + StringBuilder signed = new StringBuilder(); + StringBuilder canonical = new StringBuilder(); Map> map = new TreeMap>(); - if (headers != null && headers.size() > 0) - { - for (Map.Entry entry : headers.entrySet()) - { + if (headers != null && headers.size() > 0) { + for (Map.Entry entry : headers.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); - - if (key == null || "".equals(key) || "connection".equalsIgnoreCase(key)) - { + + if (key == null || "".equals(key) || "connection".equalsIgnoreCase(key)) { continue; } - + String lk = key.toLowerCase(Locale.getDefault()); List values = map.get(lk); - if(values == null) { - values = new ArrayList(); - map.put(lk, values); + if (values == null) { + values = new ArrayList(); + map.put(lk, values); } values.add(value); } int i = 0; - for (Map.Entry> entry : map.entrySet()) - { + for (Map.Entry> entry : map.entrySet()) { String key = entry.getKey(); List values = entry.getValue(); - if (i != 0) - { - Signed.append(";"); + if (i != 0) { + signed.append(";"); } i = 1; - Signed.append(key); - for(String value : values) { - Canonical.append(key).append(":").append(value).append( "\n"); + signed.append(key); + for (String value : values) { + canonical.append(key).append(":").append(value).append("\n"); } } } - - list.add(Signed.toString()); - list.add(Canonical.toString()); + + list.add(signed.toString()); + list.add(canonical.toString()); return list; } - - private List getCanonicalURIAndQuery(String fulPath) - throws ServiceException - { - String URI = ""; + + private List getCanonicalURIAndQuery(String fulPath) throws ServiceException { + String url = ""; String query = ""; String[] pathStrings = fulPath.split("[?]"); - if (pathStrings.length > 0) - { - URI += pathStrings[0]; + if (pathStrings.length > 0) { + url += pathStrings[0]; } - if (pathStrings.length > 1) - { + if (pathStrings.length > 1) { String[] uri = pathStrings[1].split("[&]"); Map map = new TreeMap(); - for (int i = 0; i < uri.length; i++) - { + for (int i = 0; i < uri.length; i++) { String[] kvStrings = uri[i].split("[=]"); String key = kvStrings[0]; String val = ""; - if (kvStrings.length > 1) - { + if (kvStrings.length > 1) { val = kvStrings[1]; } map.put(key, val); } int j = 0; - - StringBuilder tempStr = new StringBuilder(query); - for (Map.Entry entry : map.entrySet()) - { + + StringBuilder tempStr = new StringBuilder(); + for (Map.Entry entry : map.entrySet()) { Object key = entry.getKey(); Object value = entry.getValue(); - if (j != 0) - { - tempStr.append("&"); + if (j != 0) { + tempStr.append("&"); } j = 1; tempStr.append(key.toString()).append("=").append(value.toString()); } - + query = tempStr.toString(); - + } List list = new ArrayList(); - list.add(URI); + list.add(url); list.add(query); return list; } - - - private String getScope() - { - return new StringBuilder().append(this.nowISOtime.split("T")[0]).append("/") - .append(this.region).append("/").append(Constants.SERVICE).append("/").append(Constants.REQUEST_TAG).toString(); + + private String getScope() { + return new StringBuilder().append(this.nowISOtime.split("T")[0]).append("/").append(this.region).append("/") + .append(Constants.SERVICE).append("/").append(Constants.REQUEST_TAG).toString(); } - - private String getCanonicalRequest(String method, String fulPath, List canonical) - throws ServiceException - { + + private String getCanonicalRequest(String method, String fulPath, List canonical) throws ServiceException { List list = this.getCanonicalURIAndQuery(fulPath); - StringBuilder outPut = new StringBuilder(method).append("\n").append(list.get(0)).append("\n").append(list.get(1)).append("\n") - .append(canonical.get(1)).append("\n").append(canonical.get(0)).append("\n").append(V4Authentication.content_sha256); + StringBuilder outPut = new StringBuilder(method).append("\n").append(list.get(0)).append("\n") + .append(list.get(1)).append("\n").append(canonical.get(1)).append("\n").append(canonical.get(0)) + .append("\n").append(V4Authentication.CONTENT_SHA256); return outPut.toString(); } - - private byte[] getSigningKey() - throws ServiceException - { + + private byte[] getSigningKey() throws ServiceException { String shortDate = this.nowISOtime.split("[T]")[0]; String keyString = "AWS4" + this.sk; - try - { - byte[] dateKey = V4Authentication.hmac_sha256Encode(keyString.getBytes(Constants.DEFAULT_ENCODING), shortDate); - byte[] dateRegionKey = V4Authentication.hmac_sha256Encode(dateKey, this.region); - byte[] dateRegionServiceKey = V4Authentication.hmac_sha256Encode(dateRegionKey, Constants.SERVICE); - return V4Authentication.hmac_sha256Encode(dateRegionServiceKey, Constants.REQUEST_TAG); - } - catch (Exception e) - { + try { + byte[] dateKey = V4Authentication.hmacSha256Encode(keyString.getBytes(StandardCharsets.UTF_8), + shortDate); + byte[] dateRegionKey = V4Authentication.hmacSha256Encode(dateKey, this.region); + byte[] dateRegionServiceKey = V4Authentication.hmacSha256Encode(dateRegionKey, Constants.SERVICE); + return V4Authentication.hmacSha256Encode(dateRegionServiceKey, Constants.REQUEST_TAG); + } catch (Exception e) { throw new ServiceException("Get sign string for v4 aurhentication error", e); } } - - public static byte[] hmac_sha256Encode(byte[] key, String data) throws InvalidKeyException, NoSuchAlgorithmException, IllegalStateException, UnsupportedEncodingException - { - Mac sha256_HMAC = Mac.getInstance(Constants.HMAC_SHA256_ALGORITHM); - SecretKeySpec secret_key = new SecretKeySpec(key, Constants.HMAC_SHA256_ALGORITHM); - sha256_HMAC.init(secret_key); - return sha256_HMAC.doFinal(data.getBytes(Constants.DEFAULT_ENCODING)); + + public static byte[] hmacSha256Encode(byte[] key, String data) + throws InvalidKeyException, NoSuchAlgorithmException, IllegalStateException, UnsupportedEncodingException { + Mac sha256HMAC = Mac.getInstance(Constants.HMAC_SHA256_ALGORITHM); + SecretKeySpec secretkey = new SecretKeySpec(key, Constants.HMAC_SHA256_ALGORITHM); + sha256HMAC.init(secretkey); + return sha256HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8)); } - - public static byte[] sha256encode(String str) - throws NoSuchAlgorithmException, UnsupportedEncodingException - { + + public static byte[] sha256encode(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest digest; byte[] hash = null; digest = MessageDigest.getInstance("SHA-256"); - hash = digest.digest(str.getBytes(Constants.DEFAULT_ENCODING)); - + hash = digest.digest(str.getBytes(StandardCharsets.UTF_8)); + return hash; } - - public static String byteToHex(byte[] hash) - { + + public static String byteToHex(byte[] hash) { return ServiceUtils.toHex(hash); } diff --git a/app/src/main/java/com/obs/services/internal/xml/OBSXMLBuilder.java b/app/src/main/java/com/obs/services/internal/xml/OBSXMLBuilder.java new file mode 100644 index 0000000..de5e8e8 --- /dev/null +++ b/app/src/main/java/com/obs/services/internal/xml/OBSXMLBuilder.java @@ -0,0 +1,398 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.internal.xml; + +import com.jamesmurty.utils.BaseXMLBuilder; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.FactoryConfigurationError; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; +import java.io.IOException; +import java.security.AccessController; +import java.security.PrivilegedAction; + +public class OBSXMLBuilder extends BaseXMLBuilder { + + private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal"; + + private static String xmlDocumentBuilderFactoryClass = + "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"; + + public static void setXmlDocumentBuilderFactoryClass(String className) { + if (null != className + && !className.trim().equals("")) { + xmlDocumentBuilderFactoryClass = className; + } + } + + protected OBSXMLBuilder(Document xmlDocument) { + super(xmlDocument); + } + + protected OBSXMLBuilder(Node myNode, Node parentNode) { + super(myNode, parentNode); + } + + private static DocumentBuilderFactory findDocumentBuilderFactory() { + if (xmlDocumentBuilderFactoryClass != null && xmlDocumentBuilderFactoryClass.startsWith(DEFAULT_PACKAGE)) { + return DocumentBuilderFactory.newInstance(); + } + + return newInstance(DocumentBuilderFactory.class, xmlDocumentBuilderFactoryClass, null, true, false); + } + + protected static Document createDocumentImpl(String name, String namespaceURI, boolean enableExternalEntities, + boolean isNamespaceAware) throws ParserConfigurationException, FactoryConfigurationError { + DocumentBuilderFactory factory = OBSXMLBuilder.findDocumentBuilderFactory(); + factory.setNamespaceAware(isNamespaceAware); + enableOrDisableExternalEntityParsing(factory, enableExternalEntities); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.newDocument(); + Element rootElement = null; + if (namespaceURI != null && namespaceURI.length() > 0) { + rootElement = document.createElementNS(namespaceURI, name); + } else { + rootElement = document.createElement(name); + } + document.appendChild(rootElement); + return document; + } + + protected static Document parseDocumentImpl(InputSource inputSource, boolean enableExternalEntities, + boolean isNamespaceAware) throws ParserConfigurationException, SAXException, IOException { + DocumentBuilderFactory factory = OBSXMLBuilder.findDocumentBuilderFactory(); + factory.setNamespaceAware(isNamespaceAware); + enableOrDisableExternalEntityParsing(factory, enableExternalEntities); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(inputSource); + return document; + } + + private static ClassLoader getContextClassLoader() throws SecurityException { + return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + ClassLoader cl = null; + cl = Thread.currentThread().getContextClassLoader(); + + if (cl == null) { + cl = ClassLoader.getSystemClassLoader(); + } + return cl; + } + }); + } + + private static Class getProviderClass(String className, ClassLoader cl, boolean doFallback, + boolean useBSClsLoader) throws ClassNotFoundException { + try { + if (cl == null) { + if (useBSClsLoader) { + cl = OBSXMLBuilder.class.getClassLoader(); + } else { + cl = getContextClassLoader(); + if (cl == null) { + throw new ClassNotFoundException(); + } + } + } + + return Class.forName(className, false, cl); + } catch (ClassNotFoundException e1) { + if (doFallback) { + return Class.forName(className, false, OBSXMLBuilder.class.getClassLoader()); + } else { + throw e1; + } + } + } + + private static T newInstance(Class type, String className, ClassLoader cl, boolean doFallback, + boolean useBSClsLoader) throws FactoryConfigurationError { + if (System.getSecurityManager() != null) { + if (className != null && className.startsWith(DEFAULT_PACKAGE)) { + cl = null; + useBSClsLoader = true; + } + } + + try { + Class providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader); + if (!type.isAssignableFrom(providerClass)) { + throw new ClassCastException(className + " cannot be cast to " + type.getName()); + } + Object instance = providerClass.newInstance(); + return type.cast(instance); + } catch (ClassNotFoundException x) { + throw new FactoryConfigurationError(x, "Provider " + className + " not found"); + } catch (Exception x) { + throw new FactoryConfigurationError(x, "Provider " + className + " could not be instantiated: " + x); + } + } + + public static OBSXMLBuilder create(String name) throws ParserConfigurationException, FactoryConfigurationError { + return new OBSXMLBuilder(createDocumentImpl(name, null, false, true)); + } + + public static OBSXMLBuilder parse(InputSource inputSource, boolean enableExternalEntities, boolean isNamespaceAware) + throws ParserConfigurationException, SAXException, IOException { + return new OBSXMLBuilder(parseDocumentImpl(inputSource, enableExternalEntities, isNamespaceAware)); + } + + public static OBSXMLBuilder parse(InputSource inputSource) + throws ParserConfigurationException, SAXException, IOException { + return OBSXMLBuilder.parse(inputSource, false, true); + } + + @Override + public OBSXMLBuilder stripWhitespaceOnlyTextNodes() throws XPathExpressionException { + super.stripWhitespaceOnlyTextNodesImpl(); + return this; + } + + @Override + public OBSXMLBuilder importXMLBuilder(BaseXMLBuilder builder) { + super.importXMLBuilderImpl(builder); + return this; + } + + @Override + public OBSXMLBuilder root() { + return new OBSXMLBuilder(getDocument()); + } + + @Override + public OBSXMLBuilder xpathFind(String xpath, NamespaceContext nsContext) throws XPathExpressionException { + Node foundNode = super.xpathFindImpl(xpath, nsContext); + return new OBSXMLBuilder(foundNode, null); + } + + @Override + public OBSXMLBuilder xpathFind(String xpath) throws XPathExpressionException { + return xpathFind(xpath, null); + } + + @Override + public OBSXMLBuilder element(String name) { + String namespaceURI = super.lookupNamespaceURIImpl(name); + return element(name, namespaceURI); + } + + @Override + public OBSXMLBuilder elem(String name) { + return element(name); + } + + @Override + public OBSXMLBuilder e(String name) { + return element(name); + } + + @Override + public OBSXMLBuilder element(String name, String namespaceURI) { + Element elem = super.elementImpl(name, namespaceURI); + return new OBSXMLBuilder(elem, this.getElement()); + } + + @Override + public OBSXMLBuilder elementBefore(String name) { + Element newElement = super.elementBeforeImpl(name); + return new OBSXMLBuilder(newElement, null); + } + + @Override + public OBSXMLBuilder elementBefore(String name, String namespaceURI) { + Element newElement = super.elementBeforeImpl(name, namespaceURI); + return new OBSXMLBuilder(newElement, null); + } + + @Override + public OBSXMLBuilder attribute(String name, String value) { + super.attributeImpl(name, value); + return this; + } + + @Override + public OBSXMLBuilder attr(String name, String value) { + return attribute(name, value); + } + + @Override + public OBSXMLBuilder a(String name, String value) { + return attribute(name, value); + } + + @Override + public OBSXMLBuilder text(String value, boolean replaceText) { + super.textImpl(value, replaceText); + return this; + } + + @Override + public OBSXMLBuilder text(String value) { + return this.text(value, false); + } + + @Override + public OBSXMLBuilder t(String value) { + return text(value); + } + + @Override + public OBSXMLBuilder cdata(String data) { + super.cdataImpl(data); + return this; + } + + @Override + public OBSXMLBuilder data(String data) { + return cdata(data); + } + + @Override + public OBSXMLBuilder d(String data) { + return cdata(data); + } + + @Override + public OBSXMLBuilder cdata(byte[] data) { + super.cdataImpl(data); + return this; + } + + @Override + public OBSXMLBuilder data(byte[] data) { + return cdata(data); + } + + @Override + public OBSXMLBuilder d(byte[] data) { + return cdata(data); + } + + @Override + public OBSXMLBuilder comment(String comment) { + super.commentImpl(comment); + return this; + } + + @Override + public OBSXMLBuilder cmnt(String comment) { + return comment(comment); + } + + @Override + public OBSXMLBuilder c(String comment) { + return comment(comment); + } + + @Override + public OBSXMLBuilder instruction(String target, String data) { + super.instructionImpl(target, data); + return this; + } + + @Override + public OBSXMLBuilder inst(String target, String data) { + return instruction(target, data); + } + + @Override + public OBSXMLBuilder i(String target, String data) { + return instruction(target, data); + } + + @Override + public OBSXMLBuilder insertInstruction(String target, String data) { + super.insertInstructionImpl(target, data); + return this; + } + + @Override + public OBSXMLBuilder reference(String name) { + super.referenceImpl(name); + return this; + } + + @Override + public OBSXMLBuilder ref(String name) { + return reference(name); + } + + @Override + public OBSXMLBuilder r(String name) { + return reference(name); + } + + @Override + public OBSXMLBuilder namespace(String prefix, String namespaceURI) { + super.namespaceImpl(prefix, namespaceURI); + return this; + } + + @Override + public OBSXMLBuilder ns(String prefix, String namespaceURI) { + return attribute(prefix, namespaceURI); + } + + @Override + public OBSXMLBuilder namespace(String namespaceURI) { + this.namespace(null, namespaceURI); + return this; + } + + @Override + public OBSXMLBuilder ns(String namespaceURI) { + return namespace(namespaceURI); + } + + @Override + public OBSXMLBuilder up(int steps) { + Node currNode = super.upImpl(steps); + if (currNode instanceof Document) { + return new OBSXMLBuilder((Document) currNode); + } else { + return new OBSXMLBuilder(currNode, null); + } + } + + @Override + public OBSXMLBuilder up() { + return up(1); + } + + @Override + public OBSXMLBuilder document() { + return new OBSXMLBuilder(getDocument(), null); + } + + @Override + public int hashCode() throws UnsupportedOperationException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean equals(Object obj) { + return obj == this; + } + +} diff --git a/app/src/main/java/com/obs/services/model/AbortMultipartUploadRequest.java b/app/src/main/java/com/obs/services/model/AbortMultipartUploadRequest.java index 29dcc4a..0eb9474 100644 --- a/app/src/main/java/com/obs/services/model/AbortMultipartUploadRequest.java +++ b/app/src/main/java/com/obs/services/model/AbortMultipartUploadRequest.java @@ -11,101 +11,39 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * Parameters in a request for aborting an multipart upload */ -public class AbortMultipartUploadRequest -{ - private String uploadId; - - private String bucketName; - - private String objectKey; - - - public AbortMultipartUploadRequest(){ - - } - - /** - * Constructor - * @param bucketName Bucket name - * @param objectKey Object name - * @param uploadId Multipart upload ID - */ - public AbortMultipartUploadRequest(String bucketName, String objectKey, String uploadId) - { - this.bucketName = bucketName; - this.objectKey = objectKey; - this.uploadId = uploadId; +public class AbortMultipartUploadRequest extends AbstractMultipartRequest { + public AbortMultipartUploadRequest() { + super(); } /** - * Obtain the multipart upload ID. - * - * @return Multipart upload ID - */ - public String getUploadId() - { - return uploadId; - } - - /** - * Set the multipart upload ID. - * - * @param uploadId Multipart upload ID - */ - public void setUploadId(String uploadId) - { - this.uploadId = uploadId; - } - - /** - * Obtain the name of the bucket to which the multipart upload belongs. - * - * @return Name of the bucket to which the multipart upload belongs - */ - public String getBucketName() - { - return bucketName; - } - - /** - * Name of the bucket to which the multipart upload belongs - * - * @param bucketName Name of the bucket to which the multipart upload belongs - */ - public void setBucketName(String bucketName) - { - this.bucketName = bucketName; - } - - /** - * Obtain the name of the object involved in the multipart upload. - * - * @return Name of the object involved in the multipart upload - */ - public String getObjectKey() - { - return objectKey; - } - - /** - * Set the name of the object involved in the multipart upload. + * Constructor * - @param objectKey Name of the object involved in the multipart upload + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param uploadId + * Multipart upload ID */ - public void setObjectKey(String objectKey) - { - this.objectKey = objectKey; + public AbortMultipartUploadRequest(String bucketName, String objectKey, String uploadId) { + super(); + this.setBucketName(bucketName); + this.setObjectKey(objectKey); + this.setUploadId(uploadId); } @Override - public String toString() - { - return "AbortMultipartUploadRequest [uploadId=" + uploadId + ", bucketName=" + bucketName + ", objectKey=" + objectKey + "]"; + public String toString() { + return "AbortMultipartUploadRequest [uploadId=" + this.getUploadId() + ", bucketName=" + + this.getBucketName() + ", objectKey=" + + this.getObjectKey() + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/AbstractBulkRequest.java b/app/src/main/java/com/obs/services/model/AbstractBulkRequest.java index 753269a..740e48f 100644 --- a/app/src/main/java/com/obs/services/model/AbstractBulkRequest.java +++ b/app/src/main/java/com/obs/services/model/AbstractBulkRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import com.obs.services.internal.ObsConstraint; @@ -19,9 +20,7 @@ * Abstract class for request parameters of bulk tasks * */ -public abstract class AbstractBulkRequest { - - protected String bucketName; +public abstract class AbstractBulkRequest extends GenericRequest { protected TaskProgressListener listener; @@ -37,25 +36,6 @@ public AbstractBulkRequest() { public AbstractBulkRequest(String bucketName) { this.bucketName = bucketName; } - - - /** - * Obtain the bucket name. - * - * @return Bucket name - */ - public String getBucketName() { - return bucketName; - } - - /** - * Set the bucket name. - * - * @param bucketName Bucket name - */ - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } /** * Obtain the progress listener of the bulk task. @@ -69,14 +49,16 @@ public TaskProgressListener getProgressListener() { /** * Set the progress listener of the bulk task. * - * @param listener Progress listener + * @param listener + * Progress listener */ public void setProgressListener(TaskProgressListener listener) { this.listener = listener; } /** - * Obtain the maximum number of concurrent bulk tasks. The default value is 10. + * Obtain the maximum number of concurrent bulk tasks. The default value is + * 10. * * @return Maximum number of threads */ @@ -87,7 +69,8 @@ public int getTaskThreadNum() { /** * Set the maximum number of concurrent bulk tasks. The default value is 10. * - * @param taskThreadNum Maximum number of threads + * @param taskThreadNum + * Maximum number of threads */ public void setTaskThreadNum(int taskThreadNum) { this.taskThreadNum = taskThreadNum; @@ -103,16 +86,19 @@ public int getTaskQueueNum() { } /** - * Set the task queue length of the thread pool in the bulk task. The default value is 20000. + * Set the task queue length of the thread pool in the bulk task. The + * default value is 20000. * - * @param taskQueueNum Length of the task queue + * @param taskQueueNum + * Length of the task queue */ public void setTaskQueueNum(int taskQueueNum) { this.taskQueueNum = taskQueueNum; } /** - * Obtain the callback threshold of the task progress listener. The default value is 50. + * Obtain the callback threshold of the task progress listener. The default + * value is 50. * * @return Callback threshold of the task progress listener */ @@ -121,9 +107,11 @@ public int getProgressInterval() { } /** - * Set the callback threshold of the task progress listener. The default value is 50. + * Set the callback threshold of the task progress listener. The default + * value is 50. * - * @param taskProgressInterval Callback threshold of the task progress listener + * @param taskProgressInterval + * Callback threshold of the task progress listener */ public void setProgressInterval(int taskProgressInterval) { if (taskProgressInterval <= 0) { diff --git a/app/src/main/java/com/obs/services/model/AbstractMultipartRequest.java b/app/src/main/java/com/obs/services/model/AbstractMultipartRequest.java new file mode 100644 index 0000000..22b3f7d --- /dev/null +++ b/app/src/main/java/com/obs/services/model/AbstractMultipartRequest.java @@ -0,0 +1,48 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.model; + +public class AbstractMultipartRequest extends BaseObjectRequest { + + protected String uploadId; + + public AbstractMultipartRequest() { + } + + public AbstractMultipartRequest(String bucketName) { + this.bucketName = bucketName; + } + + /** + * Obtain the multipart upload ID. + * + * @return Multipart upload ID + */ + public String getUploadId() { + return uploadId; + } + + /** + * Set the multipart upload ID. + * + * @param uploadId + * Multipart upload ID + */ + public void setUploadId(String uploadId) { + this.uploadId = uploadId; + } + +} diff --git a/app/src/main/java/com/obs/services/model/AbstractNotification.java b/app/src/main/java/com/obs/services/model/AbstractNotification.java index 0555b1c..7ae83b5 100644 --- a/app/src/main/java/com/obs/services/model/AbstractNotification.java +++ b/app/src/main/java/com/obs/services/model/AbstractNotification.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; import java.util.ArrayList; @@ -22,103 +23,113 @@ * Abstract class for event notification configuration * */ -public class AbstractNotification extends HeaderResponse -{ - - public AbstractNotification(){ - +public class AbstractNotification extends HeaderResponse { + + public AbstractNotification() { + } - + /** * Constructor - * @param id Event notification configuration ID - * @param filter Filtering rules - * @param events List of event types that need to be notified + * + * @param id + * Event notification configuration ID + * @param filter + * Filtering rules + * @param events + * List of event types that need to be notified */ - public AbstractNotification(String id, Filter filter, List events) - { + public AbstractNotification(String id, Filter filter, List events) { this.id = id; this.filter = filter; this.events = events; } protected String id; - + protected Filter filter; - + protected List events; - + /** * List of filtering rules configured for event notification * */ - public static class Filter{ - + public static class Filter { + private List filterRules; - + /** - * Filtering rules configured for event notification + * Filtering rules configured for event notification * */ - public static class FilterRule{ - + public static class FilterRule { + private String name; - + private String value; - public FilterRule(){ - + public FilterRule() { + } - + /** * Constructor - * @param name Prefix or suffix of object names for filtering - * @param value Object name keyword in the filtering rule + * + * @param name + * Prefix or suffix of object names for filtering + * @param value + * Object name keyword in the filtering rule */ - public FilterRule(String name, String value){ + public FilterRule(String name, String value) { this.name = name; this.value = value; } - + /** - * Obtain the identifier that specifies whether objects are filtered by object name prefix or suffix. - * @return Identifier specifying whether objects are filtered by object name prefix or suffix + * Obtain the identifier that specifies whether objects are filtered + * by object name prefix or suffix. + * + * @return Identifier specifying whether objects are filtered by + * object name prefix or suffix */ - public String getName() - { + public String getName() { return name; } /** - * Set the identifier that specifies whether objects are filtered by object name prefix or suffix. - * @param name Identifier specifying whether objects are filtered by object name prefix or suffix + * Set the identifier that specifies whether objects are filtered by + * object name prefix or suffix. + * + * @param name + * Identifier specifying whether objects are filtered by + * object name prefix or suffix */ - public void setName(String name) - { + public void setName(String name) { this.name = name; } /** - * Obtain keywords of the object name. + * Obtain keywords of the object name. + * * @return Keywords of the object name */ - public String getValue() - { + public String getValue() { return value; } /** * Set keywords for the object name. - * @param value Keywords of the object name + * + * @param value + * Keywords of the object name */ - public void setValue(String value) - { + public void setValue(String value) { this.value = value; } @Override - public int hashCode() - { + public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); @@ -127,47 +138,48 @@ public int hashCode() } @Override - public boolean equals(Object obj) - { - if (this == obj) + public boolean equals(Object obj) { + if (this == obj) { return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) + } + if (obj == null) { return false; - FilterRule other = (FilterRule)obj; - if (name == null) - { - if (other.name != null) - return false; } - else if (!name.equals(other.name)) + if (getClass() != obj.getClass()) { return false; - if (value == null) - { - if (other.value != null) + } + FilterRule other = (FilterRule) obj; + if (name == null) { + if (other.name != null) { return false; + } + } else if (!name.equals(other.name)) { + return false; } - else if (!value.equals(other.value)) + if (value == null) { + if (other.value != null) { + return false; + } + } else if (!value.equals(other.value)) { return false; + } return true; } @Override - public String toString() - { + public String toString() { return "FilterRule [name=" + name + ", value=" + value + "]"; } - + } - + /** * Obtain the list of filtering rules. + * * @return Filtering rule list */ - public List getFilterRules() - { - if(this.filterRules == null){ + public List getFilterRules() { + if (this.filterRules == null) { this.filterRules = new ArrayList(); } return filterRules; @@ -175,79 +187,80 @@ public List getFilterRules() /** * Set the list of filtering rules. - * @param filterRules Filtering rule list + * + * @param filterRules + * Filtering rule list */ - public void setFilterRules(List filterRules) - { + public void setFilterRules(List filterRules) { this.filterRules = filterRules; } - + /** * Add a filtering rule. - * @param name Prefix or suffix of object names for filtering - * @param value Object name keyword in the filtering rule + * + * @param name + * Prefix or suffix of object names for filtering + * @param value + * Object name keyword in the filtering rule */ - public void addFilterRule(String name, String value){ + public void addFilterRule(String name, String value) { this.getFilterRules().add(new FilterRule(name, value)); } @Override - public String toString() - { + public String toString() { return "Filter [fileterRules=" + filterRules + "]"; } - + } /** * Obtain the event notification configuration ID. + * * @return Event notification configuration ID */ - public String getId() - { + public String getId() { return id; } /** * Set the event notification configuration ID. - * @param id Event notification configuration ID + * + * @param id + * Event notification configuration ID */ - public void setId(String id) - { + public void setId(String id) { this.id = id; } - @Deprecated - public List getEvents() - { - List list = new ArrayList(); - for(EventTypeEnum e : this.getEventTypes()) { - list.add(ObsConvertor.transEventTypeStatic(e)); - } + public List getEvents() { + List list = new ArrayList(); + for (EventTypeEnum e : this.getEventTypes()) { + list.add(ObsConvertor.transEventTypeStatic(e)); + } return list; } @Deprecated - public void setEvents(List events) - { - if(events != null) { - for(String event : events) { - EventTypeEnum e = EventTypeEnum.getValueFromCode(event); - if(e != null) { - this.getEventTypes().add(e); - } - } - } + public void setEvents(List events) { + if (events != null) { + for (String event : events) { + EventTypeEnum e = EventTypeEnum.getValueFromCode(event); + if (e != null) { + this.getEventTypes().add(e); + } + } + } } /** * Obtain the list of event types that need to be notified. + * * @return List of event types */ - public List getEventTypes() - { - if(this.events == null){ + public List getEventTypes() { + if (this.events == null) { this.events = new ArrayList(); } return events; @@ -255,29 +268,31 @@ public List getEventTypes() /** * Set the list of event types that need to be notified. - * @param events List of event types + * + * @param events + * List of event types */ - public void setEventTypes(List events) - { - this.events = events; + public void setEventTypes(List events) { + this.events = events; } /** * Obtain the filtering rule group. + * * @return Filtering rules */ - public Filter getFilter() - { + public Filter getFilter() { return filter; } /** * Set the filtering rule group. - * @param filter Filtering rules + * + * @param filter + * Filtering rules */ - public void setFilter(Filter filter) - { + public void setFilter(Filter filter) { this.filter = filter; } - + } diff --git a/app/src/main/java/com/obs/services/model/AbstractTemporarySignatureRequest.java b/app/src/main/java/com/obs/services/model/AbstractTemporarySignatureRequest.java index 40b2fcf..fcfa25b 100644 --- a/app/src/main/java/com/obs/services/model/AbstractTemporarySignatureRequest.java +++ b/app/src/main/java/com/obs/services/model/AbstractTemporarySignatureRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; import java.util.Map; @@ -21,7 +22,7 @@ * */ public abstract class AbstractTemporarySignatureRequest { - + protected HttpMethodEnum method; protected String bucketName; @@ -33,15 +34,19 @@ public abstract class AbstractTemporarySignatureRequest { protected Map headers; protected Map queryParams; - + public AbstractTemporarySignatureRequest() { } /** * Constructor - * @param method HTTP/HTTPS request method - * @param bucketName Bucket name - * @param objectKey Object name + * + * @param method + * HTTP/HTTPS request method + * @param bucketName + * Bucket name + * @param objectKey + * Object name */ public AbstractTemporarySignatureRequest(HttpMethodEnum method, String bucketName, String objectKey) { this.method = method; @@ -51,117 +56,121 @@ public AbstractTemporarySignatureRequest(HttpMethodEnum method, String bucketNam /** * Obtain the HTTP/HTTPS request method. + * * @return HTTP/HTTPS request method */ - public HttpMethodEnum getMethod() - { + public HttpMethodEnum getMethod() { return method; } - + /** * Set the HTTP/HTTP request method. - * @param method HTTP/HTTPS request method + * + * @param method + * HTTP/HTTPS request method */ - public void setMethod(HttpMethodEnum method) - { + public void setMethod(HttpMethodEnum method) { this.method = method; } - + /** * Obtain the bucket name. + * * @return Bucket name */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } - + /** * Set the bucket name. - * @param bucketName Bucket name + * + * @param bucketName + * Bucket name */ - public void setBucketName(String bucketName) - { + public void setBucketName(String bucketName) { this.bucketName = bucketName; } - + /** * Obtain the object name. + * * @return Object name */ - public String getObjectKey() - { + public String getObjectKey() { return objectKey; } - + /** * Set the object name. - * @param objectKey Object name + * + * @param objectKey + * Object name */ - public void setObjectKey(String objectKey) - { + public void setObjectKey(String objectKey) { this.objectKey = objectKey; } - + /** * Obtain the request header information. + * * @return Request header information */ - public Map getHeaders() - { - if (headers == null) - { + public Map getHeaders() { + if (headers == null) { headers = new TreeMap(); } return headers; } - + /** * Obtain the query parameters of the request. + * * @return Query parameter information */ - public Map getQueryParams() - { - if (queryParams == null) - { + public Map getQueryParams() { + if (queryParams == null) { queryParams = new TreeMap(); } return queryParams; } - + /** * Obtain the special operator. + * * @return Special operator */ - public SpecialParamEnum getSpecialParam() - { + public SpecialParamEnum getSpecialParam() { return specialParam; } - + /** * Set the special operator. - * @param specialParam Special operator + * + * @param specialParam + * Special operator */ - public void setSpecialParam(SpecialParamEnum specialParam) - { + public void setSpecialParam(SpecialParamEnum specialParam) { this.specialParam = specialParam; } /** * Set request header information. - * @param headers Request header information + * + * @param headers + * Request header information */ - public void setHeaders(Map headers) - { + public void setHeaders(Map headers) { this.headers = headers; } /** * Set request query parameters. - * @param queryParams Request query parameter + * + * @param queryParams + * Request query parameter */ - public void setQueryParams(Map queryParams) - { + public void setQueryParams(Map queryParams) { this.queryParams = queryParams; } } diff --git a/app/src/main/java/com/obs/services/model/AccessControlList.java b/app/src/main/java/com/obs/services/model/AccessControlList.java index 63c7812..1f3a571 100644 --- a/app/src/main/java/com/obs/services/model/AccessControlList.java +++ b/app/src/main/java/com/obs/services/model/AccessControlList.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; import java.util.ArrayList; @@ -19,11 +20,11 @@ import java.util.Set; /** - * Bucket or object ACL - *Include a set of permissions ({@link com.obs.services.model.Permission}) authorized to specified grantee ({@link com.obs.services.model.GranteeInterface}). + * Bucket or object ACL Include a set of permissions ( + * {@link com.obs.services.model.Permission}) authorized to specified grantee ( + * {@link com.obs.services.model.GranteeInterface}). */ -public class AccessControlList extends HeaderResponse -{ +public class AccessControlList extends HeaderResponse { /** * Pre-defined access control policy: private */ @@ -38,7 +39,7 @@ public class AccessControlList extends HeaderResponse * Pre-defined access control policy: public-read-write */ public static final AccessControlList REST_CANNED_PUBLIC_READ_WRITE = new AccessControlList(); - + /** * Pre-defined access control policy: public-read-delivered */ @@ -48,160 +49,155 @@ public class AccessControlList extends HeaderResponse * Pre-defined access control policy: public-read-write-delivered */ public static final AccessControlList REST_CANNED_PUBLIC_READ_WRITE_DELIVERED = new AccessControlList(); - /** * Pre-defined access control policy: authenticated-read */ - @Deprecated public static final AccessControlList REST_CANNED_AUTHENTICATED_READ = new AccessControlList(); - + /** * Pre-defined access control policy: bucket-owner-read */ - @Deprecated public static final AccessControlList REST_CANNED_BUCKET_OWNER_READ = new AccessControlList(); /** * Pre-defined access control policy: bucket-owner-full-control */ - @Deprecated public static final AccessControlList REST_CANNED_BUCKET_OWNER_FULL_CONTROL = new AccessControlList(); /** * Pre-defined access control policy: log-delivery-write */ - @Deprecated public static final AccessControlList REST_CANNED_LOG_DELIVERY_WRITE = new AccessControlList(); private Set grants; - + private Owner owner; - - private boolean delivered; - /** - * Check whether the object ACL is delivered. + private boolean delivered; + + /** + * Check whether the object ACL is delivered. + * * @return Identifier specifying whether the ACL is delivered */ - public boolean isDelivered() { - return delivered; - } - - /** - * Specify whether to deliver the object ACL. (This is only applicable to object ACLs.) - * @param delivered Whether to deliver the object ACL - */ - public void setDelivered(boolean delivered) { - this.delivered = delivered; - } - - /** - * Obtain the owner. + public boolean isDelivered() { + return delivered; + } + + /** + * Specify whether to deliver the object ACL. (This is only applicable to + * object ACLs.) + * + * @param delivered + * Whether to deliver the object ACL + */ + public void setDelivered(boolean delivered) { + this.delivered = delivered; + } + + /** + * Obtain the owner. * * @return Owner */ - public Owner getOwner() - { + public Owner getOwner() { return owner; } - + /** - * Set the owner. + * Set the owner. * - * @param owner Owner + * @param owner + * Owner */ - public void setOwner(Owner owner) - { + public void setOwner(Owner owner) { this.owner = owner; } - + /** * Obtain all permissions in the ACL. * * @return All grantee groups */ - public Set getGrants() { - if(grants == null) { - grants = new HashSet(); - } - return grants; - } + public Set getGrants() { + if (grants == null) { + grants = new HashSet(); + } + return grants; + } /** - * Obtain the permission specified in the ACL {@link com.obs.services.model.GranteeInterface}. - * @param grantee Authorized user - * @return Permission list of {@link com.obs.services.model.GranteeInterface} + * Obtain the permission specified in the ACL + * {@link com.obs.services.model.GranteeInterface}. + * + * @param grantee + * Authorized user + * @return Permission list of + * {@link com.obs.services.model.GranteeInterface} */ - public List getPermissionsForGrantee(GranteeInterface grantee) - { + public List getPermissionsForGrantee(GranteeInterface grantee) { List permissions = new ArrayList(); - for (GrantAndPermission gap : getGrants()) - { - if (gap.getGrantee().equals(grantee)) - { + for (GrantAndPermission gap : getGrants()) { + if (gap.getGrantee().equals(grantee)) { permissions.add(gap.getPermission()); } } return permissions; } - + /** - * Specify permissions {@link com.obs.services.model.Permission} in the ACL {@link com.obs.services.model.GranteeInterface}. + * Specify permissions {@link com.obs.services.model.Permission} in the ACL + * {@link com.obs.services.model.GranteeInterface}. * - * @param grantee Authorized user - * @param permission Permissions defined in {@link com.obs.services.model.Permission} + * @param grantee + * Authorized user + * @param permission + * Permissions defined in + * {@link com.obs.services.model.Permission} * @return Permission information */ - public GrantAndPermission grantPermission(GranteeInterface grantee, Permission permission) - { + public GrantAndPermission grantPermission(GranteeInterface grantee, Permission permission) { return grantPermission(grantee, permission, false); } - - public GrantAndPermission grantPermission(GranteeInterface grantee, Permission permission, boolean delivered) - { + public GrantAndPermission grantPermission(GranteeInterface grantee, Permission permission, boolean delivered) { GrantAndPermission obj = new GrantAndPermission(grantee, permission); obj.setDelivered(delivered); getGrants().add(obj); return obj; } - + /** * Add grantee groups to the ACL. * - * @param grantAndPermissions Grantee group + * @param grantAndPermissions + * Grantee group */ - public void grantAllPermissions(GrantAndPermission[] grantAndPermissions) - { - for (int i = 0; i < grantAndPermissions.length; i++) - { + public void grantAllPermissions(GrantAndPermission[] grantAndPermissions) { + for (int i = 0; i < grantAndPermissions.length; i++) { GrantAndPermission gap = grantAndPermissions[i]; grantPermission(gap.getGrantee(), gap.getPermission(), gap.isDelivered()); } } - + /** * Obtain all permissions in the ACL. * * @return All grantee groups */ - public GrantAndPermission[] getGrantAndPermissions() - { + public GrantAndPermission[] getGrantAndPermissions() { return getGrants().toArray(new GrantAndPermission[getGrants().size()]); } - + @Override public String toString() { StringBuilder sb = new StringBuilder("["); - for(GrantAndPermission item : getGrantAndPermissions()){ + for (GrantAndPermission item : getGrantAndPermissions()) { sb.append(item.toString()).append(","); } sb.append("]"); return "AccessControlList [owner=" + owner + ", grants=" + sb.toString() + "]"; } - } - - diff --git a/app/src/main/java/com/obs/services/model/AppendObjectRequest.java b/app/src/main/java/com/obs/services/model/AppendObjectRequest.java index 0382e0a..45b1eb4 100644 --- a/app/src/main/java/com/obs/services/model/AppendObjectRequest.java +++ b/app/src/main/java/com/obs/services/model/AppendObjectRequest.java @@ -20,26 +20,38 @@ * * */ -public class AppendObjectRequest extends PutObjectRequest{ - - protected long position; - - /** - * Obtain the position where data is to be appended. - * @return Location where the data is to be appended - */ - public long getPosition() { - return position; - } - - /** - * Set the position where the data is to be appended. - * @param position Location where the data is to be appended - */ - public void setPosition(long position) { - this.position = position; - } - -} +public class AppendObjectRequest extends PutObjectRequest { + + protected long position; + + { + this.httpMethod = HttpMethodEnum.POST; + } + + public AppendObjectRequest() { + } + public AppendObjectRequest(String bucketName) { + this.bucketName = bucketName; + } + /** + * Obtain the position where data is to be appended. + * + * @return Location where the data is to be appended + */ + public long getPosition() { + return position; + } + + /** + * Set the position where the data is to be appended. + * + * @param position + * Location where the data is to be appended + */ + public void setPosition(long position) { + this.position = position; + } + +} diff --git a/app/src/main/java/com/obs/services/model/AppendObjectResult.java b/app/src/main/java/com/obs/services/model/AppendObjectResult.java index 6bbf53e..bfa12d9 100644 --- a/app/src/main/java/com/obs/services/model/AppendObjectResult.java +++ b/app/src/main/java/com/obs/services/model/AppendObjectResult.java @@ -18,88 +18,89 @@ * Response to an appendable upload request * */ -public class AppendObjectResult extends HeaderResponse{ - - +public class AppendObjectResult extends HeaderResponse { + private String bucketName; - + private String objectKey; - + private String etag; - + private long nextPosition = -1; - + private StorageClassEnum storageClass; - + private String objectUrl; - - - public AppendObjectResult(String bucketName, String objectKey, String etag, long nextPosition, StorageClassEnum storageClass, String objectUrl) { - this.bucketName = bucketName; - this.objectKey = objectKey; - this.nextPosition = nextPosition; - this.etag = etag; - this.storageClass = storageClass; - this.objectUrl = objectUrl; - } - - /** - * Obtain the position from which the next appending starts. - * @return Position from which the next appending starts - */ - public long getNextPosition() { - return nextPosition; - } - - /** + + public AppendObjectResult(String bucketName, String objectKey, String etag, long nextPosition, + StorageClassEnum storageClass, String objectUrl) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.nextPosition = nextPosition; + this.etag = etag; + this.storageClass = storageClass; + this.objectUrl = objectUrl; + } + + /** + * Obtain the position from which the next appending starts. + * + * @return Position from which the next appending starts + */ + public long getNextPosition() { + return nextPosition; + } + + /** * Obtain the ETag of the appended data. * * @return ETag of the appended data */ - public String getEtag() { - return etag; - } + public String getEtag() { + return etag; + } - /** - * Obtain the name of the bucket to which the object belongs. + /** + * Obtain the name of the bucket to which the object belongs. + * * @return Name of the bucket to which the object belongs */ - public String getBucketName() { - return bucketName; - } + public String getBucketName() { + return bucketName; + } - /** + /** * Obtain the object name. + * * @return Object name */ - public String getObjectKey() { - return objectKey; - } + public String getObjectKey() { + return objectKey; + } /** - * Obtain the object storage class. + * Obtain the object storage class. + * * @return Object storage class */ - public StorageClassEnum getObjectStorageClass() - { + public StorageClassEnum getObjectStorageClass() { return storageClass; } - + /** * Obtain the full path to the object. + * * @return Full path to the object */ - public String getObjectUrl() { - return objectUrl; - } - - @Override - public String toString() { - return "AppendObjectResult [bucketName=" + bucketName + ", objectKey=" + objectKey + ", etag=" + etag - + ", nextPosition=" + nextPosition + ", storageClass=" + storageClass + ", objectUrl=" + objectUrl - + "]"; - } - -} + public String getObjectUrl() { + return objectUrl; + } + @Override + public String toString() { + return "AppendObjectResult [bucketName=" + bucketName + ", objectKey=" + objectKey + ", etag=" + etag + + ", nextPosition=" + nextPosition + ", storageClass=" + storageClass + ", objectUrl=" + objectUrl + + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/AuthTypeEnum.java b/app/src/main/java/com/obs/services/model/AuthTypeEnum.java index aac0178..57ca831 100644 --- a/app/src/main/java/com/obs/services/model/AuthTypeEnum.java +++ b/app/src/main/java/com/obs/services/model/AuthTypeEnum.java @@ -15,23 +15,21 @@ package com.obs.services.model; /** - * Authentication type + * Authentication type * */ -public enum AuthTypeEnum{ - /** - * V2 protocol - */ - V2, - /** - * OBS protocol - */ - OBS, - - /** - * V4 protocol - */ - @Deprecated - V4 -} +public enum AuthTypeEnum { + /** + * V2 protocol + */ + V2, + /** + * OBS protocol + */ + OBS, + /** + * V4 protocol + */ + V4 +} diff --git a/app/src/main/java/com/obs/services/model/AvailableZoneEnum.java b/app/src/main/java/com/obs/services/model/AvailableZoneEnum.java index a7f39e3..e6771d4 100644 --- a/app/src/main/java/com/obs/services/model/AvailableZoneEnum.java +++ b/app/src/main/java/com/obs/services/model/AvailableZoneEnum.java @@ -11,32 +11,33 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; /** - * Bucket cluster type + * Bucket cluster type * */ public enum AvailableZoneEnum { - - MULTI_AZ("3az"); - - private String code; - private AvailableZoneEnum(String code) { - this.code = code; - } + MULTI_AZ("3az"); + + private String code; + + private AvailableZoneEnum(String code) { + this.code = code; + } - public String getCode() { - return code; - } + public String getCode() { + return code; + } - public static AvailableZoneEnum getValueFromCode(String code) { - for (AvailableZoneEnum val : AvailableZoneEnum.values()) { - if (val.code.equals(code)) { - return val; - } - } - return null; - } + public static AvailableZoneEnum getValueFromCode(String code) { + for (AvailableZoneEnum val : AvailableZoneEnum.values()) { + if (val.code.equals(code)) { + return val; + } + } + return null; + } } diff --git a/app/src/main/java/com/obs/services/internal/consensus/CacheData.java b/app/src/main/java/com/obs/services/model/BaseBucketRequest.java similarity index 50% rename from app/src/main/java/com/obs/services/internal/consensus/CacheData.java rename to app/src/main/java/com/obs/services/model/BaseBucketRequest.java index 58cebcd..2bceef3 100644 --- a/app/src/main/java/com/obs/services/internal/consensus/CacheData.java +++ b/app/src/main/java/com/obs/services/model/BaseBucketRequest.java @@ -11,32 +11,26 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services.internal.consensus; -import com.obs.services.model.AuthTypeEnum; - -import java.util.Date; +package com.obs.services.model; /** - * 协议协商的缓存数据 + * Basic class of bucket requests + * + * @since 3.20.3 + * */ -public class CacheData { - private static final long VALID_PERIOD = (15 + (int)(5 * Math.random())) * 60 * 1000; +public class BaseBucketRequest extends GenericRequest { - private AuthTypeEnum apiVersion; - private long expirationTime; - - public CacheData(AuthTypeEnum apiVersion) { - this.apiVersion = apiVersion; - this.expirationTime = new Date().getTime() + VALID_PERIOD; + public BaseBucketRequest() { } - public AuthTypeEnum getApiVersion() { - return apiVersion; + public BaseBucketRequest(String bucketName) { + this.bucketName = bucketName; } - public long getExpirationTime() { - return expirationTime; + @Override + public String toString() { + return "BaseBucketRequest [bucketName=" + bucketName + ", isRequesterPays()=" + isRequesterPays() + "]"; } - } diff --git a/app/src/main/java/com/obs/services/model/BaseObjectRequest.java b/app/src/main/java/com/obs/services/model/BaseObjectRequest.java new file mode 100644 index 0000000..cf714a2 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/BaseObjectRequest.java @@ -0,0 +1,90 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Basic class of object requests + * + * @since 3.20.3 + */ +public class BaseObjectRequest extends GenericRequest { + protected boolean isIgnorePort; + protected String objectKey; + protected boolean encodeHeaders = true; + + public BaseObjectRequest() { + } + + public BaseObjectRequest(String bucketName) { + this.bucketName = bucketName; + } + + public BaseObjectRequest(String bucketName, String objectKey) { + this.bucketName = bucketName; + this.objectKey = objectKey; + } + + /** + * Specifies whether to encode and decode the returned header fields. + * + * @param encodeHeaders + * Specifies whether to encode and decode header fields. + */ + public void setIsEncodeHeaders(boolean encodeHeaders) { + this.encodeHeaders = encodeHeaders; + } + + /** + * Specifies whether to encode and decode the returned header fields. + * + * @return Specifies whether to encode and decode header fields. + */ + public boolean isEncodeHeaders() { + return encodeHeaders; + } + + /** + * Obtain the object name. + * + * @return Object name + */ + public String getObjectKey() { + return objectKey; + } + + /** + * Set the object name. + * + * @param objectKey + * Object name + */ + public void setObjectKey(String objectKey) { + this.objectKey = objectKey; + } + + public boolean getIsIgnorePort() { + return isIgnorePort; + } + + public void setIsIgnorePort(boolean ignorePort) { + isIgnorePort = ignorePort; + } + + @Override + public String toString() { + return "BaseObjectRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/BucketCors.java b/app/src/main/java/com/obs/services/model/BucketCors.java index 4bb6c8e..c53db2f 100644 --- a/app/src/main/java/com/obs/services/model/BucketCors.java +++ b/app/src/main/java/com/obs/services/model/BucketCors.java @@ -14,57 +14,28 @@ package com.obs.services.model; -import java.util.ArrayList; import java.util.List; /** * Bucket CORS rules */ -public class BucketCors extends S3BucketCors -{ - private List rules; - - public BucketCors() - { +public class BucketCors extends S3BucketCors { + public BucketCors() { + super(); } - + /** * Constructor - * @param rules Bucket CORS rule list - */ - public BucketCors(List rules) - { - this.rules = rules; - } - - /** - * Obtain the bucket CORS rule list. - * @return Bucket CORS rule list - */ - public List getRules() - { - if (null == rules) - { - rules = new ArrayList(); - } - return rules; - } - - /** - * Configure the bucket CORS rule list. - * @param rules Bucket CORS rule list + * + * @param rules + * Bucket CORS rule list */ - public void setRules(List rules) - { - this.rules = rules; + public BucketCors(List rules) { + super(rules); } - - + @Override - public String toString() - { - return "ObsBucketCors [rules=" + rules + "]"; + public String toString() { + return "ObsBucketCors [rules=" + this.getRules() + "]"; } } - - diff --git a/app/src/main/java/com/obs/services/model/BucketCorsRule.java b/app/src/main/java/com/obs/services/model/BucketCorsRule.java index 49bdb6a..4ea1154 100644 --- a/app/src/main/java/com/obs/services/model/BucketCorsRule.java +++ b/app/src/main/java/com/obs/services/model/BucketCorsRule.java @@ -20,167 +20,158 @@ /** * Bucket CORS rules */ -public class BucketCorsRule -{ +public class BucketCorsRule { private String id; - + private int maxAgeSecond = Integer.MIN_VALUE; - + private List allowedMethod; - + private List allowedOrigin; - + private List allowedHeader; private List exposeHeader; - + /** * Obtain the CORS rule ID. * * @return CORS rule ID */ - public String getId() - { + public String getId() { return id; } - + /** * Set the CORS rule ID. * - * @param id CORS rule ID + * @param id + * CORS rule ID */ - public void setId(String id) - { + public void setId(String id) { this.id = id; } - + /** - * Obtain the cache duration (in seconds) of the request result in the instance of ObsClient. + * Obtain the cache duration (in seconds) of the request result in the + * instance of ObsClient. * * @return Cache duration */ - public int getMaxAgeSecond() - { - if(this.maxAgeSecond == Integer.MIN_VALUE) { - return 0; - } + public int getMaxAgeSecond() { + if (this.maxAgeSecond == Integer.MIN_VALUE) { + return 0; + } return maxAgeSecond; } - + /** - * Set the cache duration (in seconds) of the request result in the instance of ObsClient. + * Set the cache duration (in seconds) of the request result in the instance + * of ObsClient. * - * @param maxAgeSecond Cache duration + * @param maxAgeSecond + * Cache duration */ - public void setMaxAgeSecond(int maxAgeSecond) - { + public void setMaxAgeSecond(int maxAgeSecond) { this.maxAgeSecond = maxAgeSecond; } - + /** * Obtain the list of methods allowed by the CORS rules. * * @return Method list */ - public List getAllowedMethod() - { - if (null == allowedMethod) - { + public List getAllowedMethod() { + if (null == allowedMethod) { return allowedMethod = new ArrayList(); } return allowedMethod; } - + /** - * Set the methods allowed by the CORS rules. Possible values are GET, PUT, DELETE, POST, and HEAD. + * Set the methods allowed by the CORS rules. Possible values are GET, PUT, + * DELETE, POST, and HEAD. * - * @param allowedMethod Method list + * @param allowedMethod + * Method list */ - public void setAllowedMethod(List allowedMethod) - { + public void setAllowedMethod(List allowedMethod) { this.allowedMethod = allowedMethod; } - + /** - * Obtain the list of origins (character strings representing domain names) allowed by the CORS rules. + * Obtain the list of origins (character strings representing domain names) + * allowed by the CORS rules. * * @return List of request origins */ - public List getAllowedOrigin() - { - if (null == allowedOrigin) - { + public List getAllowedOrigin() { + if (null == allowedOrigin) { return allowedOrigin = new ArrayList(); } return allowedOrigin; } - + /** - * Set the list of origins (character strings representing domain names) allowed by the CORS rules. + * Set the list of origins (character strings representing domain names) + * allowed by the CORS rules. * - * @param allowedOrigin List of request origins + * @param allowedOrigin + * List of request origins */ - public void setAllowedOrigin(List allowedOrigin) - { + public void setAllowedOrigin(List allowedOrigin) { this.allowedOrigin = allowedOrigin; } - + /** * Obtain the list of request headers allowed by the CORS rules. * * @return List of request headers */ - public List getAllowedHeader() - { - if (null == allowedHeader) - { + public List getAllowedHeader() { + if (null == allowedHeader) { return allowedHeader = new ArrayList(); } return allowedHeader; } - + /** * Set the list of request headers allowed by the CORS rules. * - * @param allowedHeader List of request headers + * @param allowedHeader + * List of request headers */ - public void setAllowedHeader(List allowedHeader) - { + public void setAllowedHeader(List allowedHeader) { this.allowedHeader = allowedHeader; } - + /** * Obtain the additional response headers allowed by the CORS rules. * * @return List of additional headers */ - public List getExposeHeader() - { - if (null == exposeHeader) - { + public List getExposeHeader() { + if (null == exposeHeader) { return exposeHeader = new ArrayList(); } return exposeHeader; } - + /** * Specify the additional response headers allowed by the CORS rules. * - * @param exposeHeader List of additional headers + * @param exposeHeader + * List of additional headers */ - public void setExposeHeader(List exposeHeader) - { + public void setExposeHeader(List exposeHeader) { this.exposeHeader = exposeHeader; } - - + @Override - public String toString() - { - return "BucketCorsRule [id=" + id + ", maxAgeSecond=" + maxAgeSecond + ", allowedMethod=" + allowedMethod + ", allowedOrigin=" - + allowedOrigin + ", allowedHeader=" + allowedHeader + ", exposeHeader=" + exposeHeader + "]"; + public String toString() { + return "BucketCorsRule [id=" + id + ", maxAgeSecond=" + maxAgeSecond + ", allowedMethod=" + allowedMethod + + ", allowedOrigin=" + allowedOrigin + ", allowedHeader=" + allowedHeader + ", exposeHeader=" + + exposeHeader + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/BucketCustomDomainInfo.java b/app/src/main/java/com/obs/services/model/BucketCustomDomainInfo.java new file mode 100644 index 0000000..1d0c325 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/BucketCustomDomainInfo.java @@ -0,0 +1,80 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.model; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +/** + * the Custom Domain Name for Bucket + * + * @since + */ +public class BucketCustomDomainInfo extends HeaderResponse { + private List domains; + + public List getDomains() { + if (domains == null) { + domains = new ArrayList(); + } + return domains; + } + + public Domains addDomain(String domainName, Date createTime) { + Domains domain = new Domains(domainName, createTime); + this.getDomains().add(domain); + return domain; + } + + @Override + public String toString() { + return "BucketCustomDomainInfo [domains=" + Arrays.toString(this.getDomains().toArray()) + "]"; + } + + public static class Domains { + private String domainName; + private Date createTime; + + public Domains(String domainName, Date createTime) { + super(); + this.domainName = domainName; + this.createTime = createTime; + } + + public String getDomainName() { + return domainName; + } + + public void setDomainName(String domainName) { + this.domainName = domainName; + } + + public Date getCreateTime() { + return createTime; + } + + public void setCreateTime(Date createTime) { + this.createTime = createTime; + } + + @Override + public String toString() { + return "Domains [domainName=" + domainName + ", createTime=" + createTime + "]"; + } + } +} diff --git a/app/src/main/java/com/obs/services/model/BucketDirectColdAccess.java b/app/src/main/java/com/obs/services/model/BucketDirectColdAccess.java index 2fc1db5..6f27ee8 100644 --- a/app/src/main/java/com/obs/services/model/BucketDirectColdAccess.java +++ b/app/src/main/java/com/obs/services/model/BucketDirectColdAccess.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** @@ -18,42 +19,47 @@ * */ public class BucketDirectColdAccess extends HeaderResponse { - - private RuleStatusEnum status; - - /** - * Constructor - * @param status Direct reading status of Archive objects in a bucket - */ - public BucketDirectColdAccess(RuleStatusEnum status) { - this.status = status; - } - - /** - * Constructor - */ - public BucketDirectColdAccess() { - } - - /** - * Obtain the direct reading status of Archive objects in a bucket. - * @return Direct reading status of Archive objects in a bucket - */ - public RuleStatusEnum getStatus() { - return status; - } - - /** - * Set the direct reading status of Archive objects in a bucket. - * @param status Direct reading status of Archive objects in a bucket - */ - public void setStatus(RuleStatusEnum status) { - this.status = status; - } - - @Override + + private RuleStatusEnum status; + + /** + * Constructor + * + * @param status + * Direct reading status of Archive objects in a bucket + */ + public BucketDirectColdAccess(RuleStatusEnum status) { + this.status = status; + } + + /** + * Constructor + */ + public BucketDirectColdAccess() { + } + + /** + * Obtain the direct reading status of Archive objects in a bucket. + * + * @return Direct reading status of Archive objects in a bucket + */ + public RuleStatusEnum getStatus() { + return status; + } + + /** + * Set the direct reading status of Archive objects in a bucket. + * + * @param status + * Direct reading status of Archive objects in a bucket + */ + public void setStatus(RuleStatusEnum status) { + this.status = status; + } + + @Override public String toString() { return "BucketDirectColdAccess [Status=" + status.getCode() + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/BucketEncryption.java b/app/src/main/java/com/obs/services/model/BucketEncryption.java index 87324d3..2adb002 100644 --- a/app/src/main/java/com/obs/services/model/BucketEncryption.java +++ b/app/src/main/java/com/obs/services/model/BucketEncryption.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; /** @@ -18,23 +19,27 @@ * */ public class BucketEncryption extends HeaderResponse { - + private SSEAlgorithmEnum sseAlgorithm; - + private String kmsKeyId; - - public BucketEncryption() {} + + public BucketEncryption() { + } /** * Constructor - * @param sseAlgorithm Bucket encryption algorithm + * + * @param sseAlgorithm + * Bucket encryption algorithm */ public BucketEncryption(SSEAlgorithmEnum sseAlgorithm) { this.sseAlgorithm = sseAlgorithm; } - + /** * Obtain the bucket encryption algorithm. + * * @return Bucket encryption algorithm */ public SSEAlgorithmEnum getSseAlgorithm() { @@ -43,14 +48,17 @@ public SSEAlgorithmEnum getSseAlgorithm() { /** * Set the bucket encryption algorithm. - * @param sseAlgorithm Bucket encryption algorithm + * + * @param sseAlgorithm + * Bucket encryption algorithm */ public void setSseAlgorithm(SSEAlgorithmEnum sseAlgorithm) { this.sseAlgorithm = sseAlgorithm; } /** - * Obtain the master key used in the SSE-KMS encryption. If the value is blank, the default master key is used. + * Obtain the master key used in the SSE-KMS encryption. If the value is + * blank, the default master key is used. * * @return Master key used in the SSE-KMS encryption */ @@ -59,9 +67,11 @@ public String getKmsKeyId() { } /** - * Set the master key used in the SSE-KMS encryption. If the value is blank, the default master key will be used. + * Set the master key used in the SSE-KMS encryption. If the value is blank, + * the default master key will be used. * - * @param kmsKeyId Master key used in the SSE-KMS encryption + * @param kmsKeyId + * Master key used in the SSE-KMS encryption */ public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; @@ -71,5 +81,5 @@ public void setKmsKeyId(String kmsKeyId) { public String toString() { return "BucketEncryption [sseAlgorithm=" + sseAlgorithm + ", kmsKeyId=" + kmsKeyId + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/BucketLocationResponse.java b/app/src/main/java/com/obs/services/model/BucketLocationResponse.java index 1b65ec3..ca7145a 100644 --- a/app/src/main/java/com/obs/services/model/BucketLocationResponse.java +++ b/app/src/main/java/com/obs/services/model/BucketLocationResponse.java @@ -18,29 +18,25 @@ * Response to a request for obtaining the bucket location * */ -public class BucketLocationResponse extends HeaderResponse -{ +public class BucketLocationResponse extends HeaderResponse { private String location; - + public BucketLocationResponse(String location) { - this.location = location; + this.location = location; } - + /** * Obtain the bucket location. + * * @return Bucket location */ - public String getLocation() - { + public String getLocation() { return location; } @Override - public String toString() - { + public String toString() { return "BucketLocationResponse [location=" + location + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/BucketLoggingConfiguration.java b/app/src/main/java/com/obs/services/model/BucketLoggingConfiguration.java index bbc30ab..10e1299 100644 --- a/app/src/main/java/com/obs/services/model/BucketLoggingConfiguration.java +++ b/app/src/main/java/com/obs/services/model/BucketLoggingConfiguration.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.ArrayList; @@ -20,130 +21,131 @@ /** * Logging settings of a bucket */ -public class BucketLoggingConfiguration extends HeaderResponse -{ +public class BucketLoggingConfiguration extends HeaderResponse { private String targetBucketName; - + private String logfilePrefix; - + private String agency; - + private final List targetGrantsList = new ArrayList(); - - public BucketLoggingConfiguration() - { + + public BucketLoggingConfiguration() { } - - + /** * Constructor - * @param targetBucketName Name of the bucket in which logs are saved - * @param logfilePrefix Name prefix of the logged objects + * + * @param targetBucketName + * Name of the bucket in which logs are saved + * @param logfilePrefix + * Name prefix of the logged objects */ - public BucketLoggingConfiguration(String targetBucketName, String logfilePrefix) - { + public BucketLoggingConfiguration(String targetBucketName, String logfilePrefix) { this.targetBucketName = targetBucketName; this.logfilePrefix = logfilePrefix; } - /** * Obtain the bucket in which logs are saved. + * * @return Bucket in which logs are saved */ - public String getTargetBucketName() - { + public String getTargetBucketName() { return targetBucketName; } - + /** * Set the bucket to write logs. - * @param targetBucketName Name of the bucket in which logs are saved + * + * @param targetBucketName + * Name of the bucket in which logs are saved */ - public void setTargetBucketName(String targetBucketName) - { + public void setTargetBucketName(String targetBucketName) { this.targetBucketName = targetBucketName; } - + /** - * Obtain the name prefix of the logged objects. + * Obtain the name prefix of the logged objects. + * * @return Name prefix of the logged objects */ - public String getLogfilePrefix() - { + public String getLogfilePrefix() { return logfilePrefix; } - + /** * Set the name prefix of the logged objects. - * @param logfilePrefix Name prefix of the logged objects + * + * @param logfilePrefix + * Name prefix of the logged objects */ - public void setLogfilePrefix(String logfilePrefix) - { + public void setLogfilePrefix(String logfilePrefix) { this.logfilePrefix = logfilePrefix; } - + /** * Obtain the log delivery group. + * * @return Log delivery group {@link GrantAndPermission} */ - public GrantAndPermission[] getTargetGrants() - { + public GrantAndPermission[] getTargetGrants() { return targetGrantsList.toArray(new GrantAndPermission[targetGrantsList.size()]); } - + /** * Set the log delivery group. - * @param targetGrants Log delivery group {@link GrantAndPermission} + * + * @param targetGrants + * Log delivery group {@link GrantAndPermission} */ - public void setTargetGrants(GrantAndPermission[] targetGrants) - { + public void setTargetGrants(GrantAndPermission[] targetGrants) { targetGrantsList.clear(); targetGrantsList.addAll(Arrays.asList(targetGrants)); } - + /** * Add permissions for logged objects - * @param targetGrant Permissions of the logged object + * + * @param targetGrant + * Permissions of the logged object */ - public void addTargetGrant(GrantAndPermission targetGrant) - { + public void addTargetGrant(GrantAndPermission targetGrant) { targetGrantsList.add(targetGrant); } - + /** * Check whether bucket logging is enabled. + * * @return Identifier specifying whether bucket logging is enabled */ - public boolean isLoggingEnabled() - { + public boolean isLoggingEnabled() { return targetBucketName != null || logfilePrefix != null || this.targetGrantsList.size() > 0; } + /** + * Set the agent name. + * + * @return Agent name + */ + public String getAgency() { + return agency; + } - /** - * Set the agent name. - * @return Agent name - */ - public String getAgency() { - return agency; - } - - /** - * Obtain the agent name. - * @param agency Agent name - */ - public void setAgency(String agency) { - this.agency = agency; - } - - - @Override - public String toString() { - return "BucketLoggingConfiguration [targetBucketName=" + targetBucketName + ", logfilePrefix=" + logfilePrefix - + ", agency=" + agency + ", targetGrantsList=" + targetGrantsList + "]"; - } - -} + /** + * Obtain the agent name. + * + * @param agency + * Agent name + */ + public void setAgency(String agency) { + this.agency = agency; + } + @Override + public String toString() { + return "BucketLoggingConfiguration [targetBucketName=" + targetBucketName + ", logfilePrefix=" + logfilePrefix + + ", agency=" + agency + ", targetGrantsList=" + targetGrantsList + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/BucketMetadataInfoRequest.java b/app/src/main/java/com/obs/services/model/BucketMetadataInfoRequest.java index 1f1b5c6..693c4f5 100644 --- a/app/src/main/java/com/obs/services/model/BucketMetadataInfoRequest.java +++ b/app/src/main/java/com/obs/services/model/BucketMetadataInfoRequest.java @@ -20,63 +20,67 @@ * Parameters in a request for obtaining bucket metadata * */ -public class BucketMetadataInfoRequest extends OptionsInfoRequest -{ - protected String bucketName; - - private List requestHeaders; - - public BucketMetadataInfoRequest() +public class BucketMetadataInfoRequest extends OptionsInfoRequest { + { - + httpMethod = HttpMethodEnum.HEAD; + } + + // todo 这个 requestHeaders 具体作用,如何合并? + private List requestHeaders; + + public BucketMetadataInfoRequest() { + super(); } - + /** * Constructor - * @param bucketName Bucket name + * + * @param bucketName + * Bucket name */ - public BucketMetadataInfoRequest(String bucketName) - { - this.bucketName = bucketName; + public BucketMetadataInfoRequest(String bucketName) { + super(bucketName); } - + /** * Constructor - * @param bucketName Bucket name - * @param origin Origin allowed by the CORS rule - * @param requestHeaders Request headers allowed by the CORS rules + * + * @param bucketName + * Bucket name + * @param origin + * Origin allowed by the CORS rule + * @param requestHeaders + * Request headers allowed by the CORS rules */ - public BucketMetadataInfoRequest(String bucketName, String origin, List requestHeaders) - { + public BucketMetadataInfoRequest(String bucketName, String origin, List requestHeaders) { this.bucketName = bucketName; this.setOrigin(origin); this.requestHeaders = requestHeaders; } - + /** * Obtain the bucket name. + * * @return Bucket name */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } - + /** * Set the bucket name. - * @param bucketName Bucket name + * + * @param bucketName + * Bucket name */ - public void setBucketName(String bucketName) - { + public void setBucketName(String bucketName) { this.bucketName = bucketName; } @Override - public String toString() - { + public String toString() { return "BucketMetadataInfoRequest [bucketName=" + bucketName + ", requestHeaders=" + requestHeaders + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/BucketMetadataInfoResult.java b/app/src/main/java/com/obs/services/model/BucketMetadataInfoResult.java index e8c523d..10e1e2a 100644 --- a/app/src/main/java/com/obs/services/model/BucketMetadataInfoResult.java +++ b/app/src/main/java/com/obs/services/model/BucketMetadataInfoResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; import java.util.List; @@ -19,106 +20,205 @@ * Response to a request for obtaining bucket metadata * */ -public class BucketMetadataInfoResult extends OptionsInfoResult -{ - - private StorageClassEnum storageClass; - - private String location; +public class BucketMetadataInfoResult extends OptionsInfoResult { + + protected StorageClassEnum storageClass; + + protected String location; + + protected String obsVersion; + + protected AvailableZoneEnum availableZone; + + protected String epid; + + protected BucketTypeEnum bucketType = BucketTypeEnum.OBJECT; + + public BucketMetadataInfoResult(String allowOrigin, List allowHeaders, int maxAge, + List allowMethods, List exposeHeaders, StorageClassEnum storageClass, String location, + String obsVersion) { + super(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders); + this.storageClass = storageClass; + this.location = location; + this.obsVersion = obsVersion; + } + + @Deprecated + //CHECKSTYLE:OFF + public BucketMetadataInfoResult(String allowOrigin, List allowHeaders, int maxAge, + List allowMethods, List exposeHeaders, StorageClassEnum storageClass, String location, + String obsVersion, AvailableZoneEnum availableZone) { + this(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion); + this.availableZone = availableZone; + } + + @Deprecated + //CHECKSTYLE:OFF + public BucketMetadataInfoResult(String allowOrigin, List allowHeaders, int maxAge, + List allowMethods, List exposeHeaders, StorageClassEnum storageClass, String location, + String obsVersion, AvailableZoneEnum availableZone, String epid, BucketTypeEnum bucketType) { + this(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion); + this.availableZone = availableZone; + this.epid = epid; + this.bucketType = bucketType; + } + + protected BucketMetadataInfoResult() { + super(); + } - private String obsVersion; + private BucketMetadataInfoResult(Builder builder) { + super(builder.allowOrigin, builder.allowHeaders, builder.maxAge, builder.allowMethods, builder.exposeHeaders); + this.storageClass = builder.storageClass; + this.location = builder.location; + this.obsVersion = builder.obsVersion; + this.availableZone = builder.availableZone; + this.epid = builder.epid; + this.bucketType = builder.bucketType; + } - private AvailableZoneEnum availableZone; + public static final class Builder { + private String allowOrigin; + private List allowHeaders; + private int maxAge; + private List allowMethods; + private List exposeHeaders; + private StorageClassEnum storageClass; + private String location; + private String obsVersion; + private AvailableZoneEnum availableZone; + private String epid; + private BucketTypeEnum bucketType = BucketTypeEnum.OBJECT; + + public Builder allowOrigin(String allowOrigin) { + this.allowOrigin = allowOrigin; + return this; + } + + public Builder allowHeaders(List allowHeaders) { + this.allowHeaders = allowHeaders; + return this; + } + + public Builder maxAge(int maxAge) { + this.maxAge = maxAge; + return this; + } + + public Builder allowMethods(List allowMethods) { + this.allowMethods = allowMethods; + return this; + } + + public Builder exposeHeaders(List exposeHeaders) { + this.exposeHeaders = exposeHeaders; + return this; + } + + public Builder storageClass(StorageClassEnum storageClass) { + this.storageClass = storageClass; + return this; + } + + public Builder location(String location) { + this.location = location; + return this; + } + + public Builder obsVersion(String obsVersion) { + this.obsVersion = obsVersion; + return this; + } + + public Builder availableZone(AvailableZoneEnum availableZone) { + this.availableZone = availableZone; + return this; + } + + public Builder epid(String epid) { + this.epid = epid; + return this; + } + + public Builder bucketType(BucketTypeEnum bucketType) { + this.bucketType = bucketType; + return this; + } + + public BucketMetadataInfoResult build() { + return new BucketMetadataInfoResult(this); + } + } - private String epid; - - private BucketTypeEnum bucketType = BucketTypeEnum.OBJECT; - public BucketMetadataInfoResult(String allowOrigin, List allowHeaders, int maxAge, - List allowMethods, List exposeHeaders, StorageClassEnum storageClass, String location, - String obsVersion) { - super(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders); - this.storageClass = storageClass; - this.location = location; - this.obsVersion = obsVersion; - } - - public BucketMetadataInfoResult(String allowOrigin, List allowHeaders, int maxAge, - List allowMethods, List exposeHeaders, StorageClassEnum storageClass, String location, - String obsVersion, AvailableZoneEnum availableZone) { - this(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion); - this.availableZone = availableZone; - } - - public BucketMetadataInfoResult(String allowOrigin, List allowHeaders, int maxAge, - List allowMethods, List exposeHeaders, StorageClassEnum storageClass, String location, - String obsVersion, AvailableZoneEnum availableZone, String epid, BucketTypeEnum bucketType) { - this(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion); - this.availableZone = availableZone; - this.epid = epid; - this.bucketType = bucketType; - } - - - - /** - * Obtain the bucket storage class. + /** + * Obtain the bucket storage class. + * * @return Bucket storage class */ @Deprecated - public String getDefaultStorageClass() - { + public String getDefaultStorageClass() { return this.storageClass == null ? null : storageClass.getCode(); } - + /** - * Obtain the bucket storage class. + * Obtain the bucket storage class. + * * @return Bucket storage class */ public StorageClassEnum getBucketStorageClass() { - return this.storageClass; + return this.storageClass; } - + /** * Obtain the bucket location. + * * @return Bucket location */ - public String getLocation() { - return location; - } - - /** - * Obtain the OBS version. - * @return OBS version - */ - public String getObsVersion() { - return obsVersion; - } - - /** - * Obtain the enterprise ID of a bucket. - * @return Enterprise ID - */ - public String getEpid() { - return epid; - } - - /** - * Obtain the bucket cluster type. - * @return Bucket cluster type - */ - public AvailableZoneEnum getAvailableZone() { - return this.availableZone; - } - - @Override - public String toString() { - return "BucketMetadataInfoResult [storageClass=" + storageClass + ", location=" + location + ", obsVersion=" - + obsVersion + ", bucketType="+ bucketType.name() + "]"; - } - - - public BucketTypeEnum getBucketType() { - return bucketType; - } + public String getLocation() { + return location; + } + + /** + * Obtain the OBS version. + * + * @return OBS version + */ + public String getObsVersion() { + return obsVersion; + } + + /** + * Obtain the enterprise ID of a bucket. + * + * @return Enterprise ID + */ + public String getEpid() { + return epid; + } + + /** + * Obtain the bucket cluster type. + * + * @return Bucket cluster type + */ + public AvailableZoneEnum getAvailableZone() { + return this.availableZone; + } + + @Override + public String toString() { + return "BucketMetadataInfoResult [storageClass=" + storageClass + ", location=" + location + ", obsVersion=" + + obsVersion + ", bucketType=" + bucketType.name() + "]"; + } + + /** + * Get type of the bucket + * + * @return Type of bucket + */ + public BucketTypeEnum getBucketType() { + return bucketType; + } + } diff --git a/app/src/main/java/com/obs/services/model/BucketNotificationConfiguration.java b/app/src/main/java/com/obs/services/model/BucketNotificationConfiguration.java index 4e17f6b..9145a8c 100644 --- a/app/src/main/java/com/obs/services/model/BucketNotificationConfiguration.java +++ b/app/src/main/java/com/obs/services/model/BucketNotificationConfiguration.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; import java.util.ArrayList; @@ -20,54 +21,58 @@ * Bucket notification configuration * */ -public class BucketNotificationConfiguration extends HeaderResponse -{ +public class BucketNotificationConfiguration extends HeaderResponse { private List topicConfigurations; private List functionGraphConfigurations; - - public BucketNotificationConfiguration(){ - + + public BucketNotificationConfiguration() { + } - + /** * Add event notification configuration. - * @param topicConfiguration Event notification configuration + * + * @param topicConfiguration + * Event notification configuration * @return Bucket notification configuration */ - public BucketNotificationConfiguration addTopicConfiguration(TopicConfiguration topicConfiguration){ + public BucketNotificationConfiguration addTopicConfiguration(TopicConfiguration topicConfiguration) { this.getTopicConfigurations().add(topicConfiguration); return this; } - + /** * Add FunctionGraph notification configuration. - * @param functionGraphConfiguration FunctionGraph notification configuration + * + * @param functionGraphConfiguration + * FunctionGraph notification configuration * @return Event notification configuration of the bucket */ - public BucketNotificationConfiguration addFunctionGraphConfiguration(FunctionGraphConfiguration functionGraphConfiguration){ + public BucketNotificationConfiguration addFunctionGraphConfiguration( + FunctionGraphConfiguration functionGraphConfiguration) { this.getFunctionGraphConfigurations().add(functionGraphConfiguration); return this; } - + /** * Obtain the list of event notification configurations + * * @return List of event notification configurations */ - public List getTopicConfigurations() - { - if(this.topicConfigurations == null){ + public List getTopicConfigurations() { + if (this.topicConfigurations == null) { this.topicConfigurations = new ArrayList(); } return topicConfigurations; } - + /** * Obtain the list of FunctionGraph notification configurations + * * @return List of FunctionGraph notification configurations */ - public List getFunctionGraphConfigurations() - { - if(this.functionGraphConfigurations == null){ + public List getFunctionGraphConfigurations() { + if (this.functionGraphConfigurations == null) { this.functionGraphConfigurations = new ArrayList(); } return functionGraphConfigurations; @@ -75,19 +80,21 @@ public List getFunctionGraphConfigurations() /** * Configure event notification. - * @param topicConfigurations Event notification configuration list + * + * @param topicConfigurations + * Event notification configuration list */ - public void setTopicConfigurations(List topicConfigurations) - { + public void setTopicConfigurations(List topicConfigurations) { this.topicConfigurations = topicConfigurations; } - + /** * Set the list of FunctionGraph notification configurations. - * @param functionGraphConfigurations List of FunctionGraph notification configurations + * + * @param functionGraphConfigurations + * List of FunctionGraph notification configurations */ - public void setFunctionGraphConfigurations(List functionGraphConfigurations) - { + public void setFunctionGraphConfigurations(List functionGraphConfigurations) { this.functionGraphConfigurations = functionGraphConfigurations; } @@ -96,5 +103,5 @@ public String toString() { return "BucketNotificationConfiguration [topicConfigurations=" + topicConfigurations + ", functionGraphConfigurations=" + functionGraphConfigurations + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/BucketPolicyResponse.java b/app/src/main/java/com/obs/services/model/BucketPolicyResponse.java index b02a82e..762318e 100644 --- a/app/src/main/java/com/obs/services/model/BucketPolicyResponse.java +++ b/app/src/main/java/com/obs/services/model/BucketPolicyResponse.java @@ -18,30 +18,25 @@ * Response to a request for obtaining a bucket policy * */ -public class BucketPolicyResponse extends HeaderResponse -{ +public class BucketPolicyResponse extends HeaderResponse { private String policy; - + public BucketPolicyResponse(String policy) { - this.policy = policy; - } + this.policy = policy; + } - /** - * Obtain a bucket policy. + /** + * Obtain a bucket policy. + * * @return Bucket policy */ - public String getPolicy() - { + public String getPolicy() { return policy; } @Override - public String toString() - { + public String toString() { return "BucketPolicyResponse [policy=" + policy + "]"; } - - -} - +} diff --git a/app/src/main/java/com/obs/services/model/BucketQuota.java b/app/src/main/java/com/obs/services/model/BucketQuota.java index caa173c..2814881 100644 --- a/app/src/main/java/com/obs/services/model/BucketQuota.java +++ b/app/src/main/java/com/obs/services/model/BucketQuota.java @@ -17,50 +17,46 @@ /** * Bucket quota information */ -public class BucketQuota extends HeaderResponse -{ +public class BucketQuota extends HeaderResponse { private long bucketQuota; - - public BucketQuota(){ - + + public BucketQuota() { + } - + /** * Constructor - * @param bucketQuota Bucket quota + * + * @param bucketQuota + * Bucket quota */ - public BucketQuota(long bucketQuota) - { + public BucketQuota(long bucketQuota) { this.bucketQuota = bucketQuota; } - + /** - * Obtain the bucket quota (in bytes). + * Obtain the bucket quota (in bytes). * * @return Bucket quota */ - public long getBucketQuota() - { + public long getBucketQuota() { return bucketQuota; } - + /** - * Set the bucket quota (in bytes). + * Set the bucket quota (in bytes). * - * @param quota Bucket quota + * @param quota + * Bucket quota */ - public void setBucketQuota(long quota) - { + public void setBucketQuota(long quota) { this.bucketQuota = quota; } @Override - public String toString() - { + public String toString() { return "BucketQuota [bucketQuota=" + bucketQuota + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/BucketStorageInfo.java b/app/src/main/java/com/obs/services/model/BucketStorageInfo.java index 9250e30..1fa10d1 100644 --- a/app/src/main/java/com/obs/services/model/BucketStorageInfo.java +++ b/app/src/main/java/com/obs/services/model/BucketStorageInfo.java @@ -17,58 +17,52 @@ /** * Bucket storage information */ -public class BucketStorageInfo extends HeaderResponse -{ +public class BucketStorageInfo extends HeaderResponse { private long size; - + private long objectNum; - + /** - * Obtain the bucket quota (in bytes). + * Obtain the bucket quota (in bytes). * * @return Bucket size */ - public long getSize() - { + public long getSize() { return size; } - - /** - * Set the bucket size (in bytes). + + /** + * Set the bucket size (in bytes). * - * @param storageSize Bucket size + * @param storageSize + * Bucket size */ - public void setSize(long storageSize) - { + public void setSize(long storageSize) { this.size = storageSize; } - + /** * Obtain the number of objects in the bucket. * * @return Number of objects in the bucket */ - public long getObjectNumber() - { + public long getObjectNumber() { return objectNum; } - - /** + + /** * Set the number of objects in the bucket. * - * @param objectNumber Number of objects in the bucket + * @param objectNumber + * Number of objects in the bucket */ - public void setObjectNumber(long objectNumber) - { + public void setObjectNumber(long objectNumber) { this.objectNum = objectNumber; } @Override - public String toString() - { + public String toString() { return "BucketStorageInfo [size=" + size + ", objectNum=" + objectNum + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/BucketStoragePolicyConfiguration.java b/app/src/main/java/com/obs/services/model/BucketStoragePolicyConfiguration.java index 7951ead..60cea07 100644 --- a/app/src/main/java/com/obs/services/model/BucketStoragePolicyConfiguration.java +++ b/app/src/main/java/com/obs/services/model/BucketStoragePolicyConfiguration.java @@ -18,78 +18,79 @@ * Bucket storage policy */ public class BucketStoragePolicyConfiguration extends HeaderResponse { - + private StorageClassEnum storageClass; - + /** * Constructor - * @param storageClass Bucket storage class + * + * @param storageClass + * Bucket storage class */ @Deprecated - public BucketStoragePolicyConfiguration(String storageClass) - { + public BucketStoragePolicyConfiguration(String storageClass) { this.storageClass = StorageClassEnum.getValueFromCode(storageClass); } - + /** * Constructor - * @param storageClass Bucket storage class + * + * @param storageClass + * Bucket storage class */ - public BucketStoragePolicyConfiguration(StorageClassEnum storageClass) - { + public BucketStoragePolicyConfiguration(StorageClassEnum storageClass) { this.storageClass = storageClass; } - - public BucketStoragePolicyConfiguration(){ - + + public BucketStoragePolicyConfiguration() { + } @Override - public String toString() - { + public String toString() { return "BucketStoragePolicyConfiguration [storageClass=" + storageClass + "]"; } /** - * Obtain the bucket storage class. + * Obtain the bucket storage class. + * * @return storageClass Bucket storage class * @see #getBucketStorageClass() */ @Deprecated - public String getStorageClass() - { + public String getStorageClass() { return this.storageClass != null ? this.storageClass.getCode() : null; } /** - * Set the bucket storage class. - * @param storageClass Bucket storage class + * Set the bucket storage class. + * + * @param storageClass + * Bucket storage class * @see #setBucketStorageClass(StorageClassEnum storageClass) */ @Deprecated - public void setStorageClass(String storageClass) - { + public void setStorageClass(String storageClass) { this.storageClass = StorageClassEnum.getValueFromCode(storageClass); } - + /** - * Obtain the bucket storage class. + * Obtain the bucket storage class. + * * @return storageClass Bucket storage class */ - public StorageClassEnum getBucketStorageClass() - { + public StorageClassEnum getBucketStorageClass() { return storageClass; } /** - * Set the bucket storage class. - * @param storageClass Bucket storage class + * Set the bucket storage class. + * + * @param storageClass + * Bucket storage class */ - public void setBucketStorageClass(StorageClassEnum storageClass) - { + public void setBucketStorageClass(StorageClassEnum storageClass) { this.storageClass = storageClass; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/BucketTagInfo.java b/app/src/main/java/com/obs/services/model/BucketTagInfo.java index e7766c3..96a3e09 100644 --- a/app/src/main/java/com/obs/services/model/BucketTagInfo.java +++ b/app/src/main/java/com/obs/services/model/BucketTagInfo.java @@ -21,181 +21,188 @@ * Bucket tagging configuration * */ -public class BucketTagInfo extends HeaderResponse -{ +public class BucketTagInfo extends HeaderResponse { private TagSet tagSet; - - public BucketTagInfo(){ - + + public BucketTagInfo() { + } - + /** * Constructor - * @param tagSet Bucket tag set + * + * @param tagSet + * Bucket tag set */ - public BucketTagInfo(TagSet tagSet){ + public BucketTagInfo(TagSet tagSet) { this.tagSet = tagSet; } - + /** * Bucket tag set * */ - public static class TagSet - { + public static class TagSet { private List tags; - + /** * Bucket tag * */ - public static class Tag - { + public static class Tag { private String key; - + private String value; - - public Tag() - { - + + public Tag() { + } - + /** * Constructor - * @param key Tag key - * @param value Tag value + * + * @param key + * Tag key + * @param value + * Tag value */ - public Tag(String key, String value) - { + public Tag(String key, String value) { this.key = key; this.value = value; } - + /** - * Obtain the tag key. + * Obtain the tag key. + * * @return Tag key */ - public String getKey() - { + public String getKey() { return key; } - + /** * Set the tag key. - * @param key Tag key + * + * @param key + * Tag key */ - public void setKey(String key) - { + public void setKey(String key) { this.key = key; } - + /** - * Obtain the tag value. + * Obtain the tag value. + * * @return Tag value */ - public String getValue() - { + public String getValue() { return value; } - + /** * Set the tag value. - * @param value Tag value + * + * @param value + * Tag value */ - public void setValue(String value) - { + public void setValue(String value) { this.value = value; } - + @Override - public int hashCode() - { + public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((key == null) ? 0 : key.hashCode()); result = prime * result + ((value == null) ? 0 : value.hashCode()); return result; } - + @Override - public boolean equals(Object obj) - { - if (this == obj) + public boolean equals(Object obj) { + if (this == obj) { return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) + } + if (obj == null) { return false; - Tag other = (Tag)obj; - if (key == null) - { - if (other.key != null) - return false; } - else if (!key.equals(other.key)) + if (getClass() != obj.getClass()) { return false; - if (value == null) - { - if (other.value != null) + } + Tag other = (Tag) obj; + if (key == null) { + if (other.key != null) { return false; + } + } else if (!key.equals(other.key)) { + return false; } - else if (!value.equals(other.value)) + if (value == null) { + if (other.value != null) { + return false; + } + } else if (!value.equals(other.value)) { return false; + } return true; } - } - + /** * Obtain the tag list. + * * @return Tag list */ - public List getTags() - { - if (tags == null) - { + public List getTags() { + if (tags == null) { tags = new ArrayList(); } return tags; } - + /** * Add a tag. - * @param key Tag key - * @param value Tag value + * + * @param key + * Tag key + * @param value + * Tag value * @return Newly added tag */ - public Tag addTag(String key, String value) - { + public Tag addTag(String key, String value) { Tag t = new Tag(key, value); this.getTags().add(t); return t; } - + /** * Delete a tag. - * @param key Tag key - * @param value Tag value + * + * @param key + * Tag key + * @param value + * Tag value * @return Deleted tag */ - public Tag removeTag(String key, String value) - { + public Tag removeTag(String key, String value) { Tag t = new Tag(key, value); this.getTags().remove(t); return t; } - + /** * Delete a tag. - * @param key Tag key + * + * @param key + * Tag key * @return Deleted tag */ - public Tag removeTagByKey(String key) - { - for (Tag t : this.tags) - { - if (t.getKey().equals(key)) - { + public Tag removeTagByKey(String key) { + if (null == this.tags) { + return null; + } + for (Tag t : this.tags) { + if (t.getKey().equals(key)) { this.removeTag(t.getKey(), t.getValue()); return t; } @@ -205,12 +212,12 @@ public Tag removeTagByKey(String key) } /** - *Obtain the tag set of a bucket. + * Obtain the tag set of a bucket. + * * @return Tag set */ - public TagSet getTagSet() - { - if(tagSet == null){ + public TagSet getTagSet() { + if (tagSet == null) { tagSet = new TagSet(); } return tagSet; @@ -218,26 +225,23 @@ public TagSet getTagSet() /** * Configure the tag set for a bucket. - * @param tagSet Tag set + * + * @param tagSet + * Tag set */ - public void setTagSet(TagSet tagSet) - { + public void setTagSet(TagSet tagSet) { this.tagSet = tagSet; } - @Override - public String toString() - { + public String toString() { StringBuilder s = new StringBuilder("["); - if (tagSet != null) - { + if (tagSet != null) { int i = 0; - for (TagSet.Tag t : tagSet.getTags()) - { - s.append("[").append("key=").append(t.getKey()).append(",").append("value=").append(t.getValue()).append("]"); - if (i++ != tagSet.getTags().size() - 1) - { + for (TagSet.Tag t : tagSet.getTags()) { + s.append("[").append("key=").append(t.getKey()).append(",").append("value=").append(t.getValue()) + .append("]"); + if (i++ != tagSet.getTags().size() - 1) { s.append(","); } } @@ -246,5 +250,3 @@ public String toString() return "BucketTagInfo [tagSet=[tags=" + s.toString() + "]"; } } - - diff --git a/app/src/main/java/com/obs/services/model/BucketTypeEnum.java b/app/src/main/java/com/obs/services/model/BucketTypeEnum.java index c6fb2bd..98f6e4d 100644 --- a/app/src/main/java/com/obs/services/model/BucketTypeEnum.java +++ b/app/src/main/java/com/obs/services/model/BucketTypeEnum.java @@ -11,22 +11,22 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services.model; +package com.obs.services.model; public enum BucketTypeEnum { - - OBJECT("OBJECT"), - PFS("POSIX"); + OBJECT("OBJECT"), + + PFS("POSIX"); - private String code; + private String code; - private BucketTypeEnum(String code) { - this.code = code; - } + private BucketTypeEnum(String code) { + this.code = code; + } - public String getCode() { - return this.code; - } + public String getCode() { + return this.code; + } } diff --git a/app/src/main/java/com/obs/services/model/BucketVersioningConfiguration.java b/app/src/main/java/com/obs/services/model/BucketVersioningConfiguration.java index 0f9475c..c858253 100644 --- a/app/src/main/java/com/obs/services/model/BucketVersioningConfiguration.java +++ b/app/src/main/java/com/obs/services/model/BucketVersioningConfiguration.java @@ -17,95 +17,95 @@ /** * Bucket versioning status */ -public class BucketVersioningConfiguration extends HeaderResponse -{ +public class BucketVersioningConfiguration extends HeaderResponse { /** * Versioning is suspended. */ - @Deprecated + @Deprecated public static final String SUSPENDED = "Suspended"; - + /** * Versioning is enabled. */ - @Deprecated + @Deprecated public static final String ENABLED = "Enabled"; - + private VersioningStatusEnum status; - + /** - * Constructor - * If versioning is enabled for a bucket, it cannot be disabled or changed to {@link #SUSPENDED}. - * @param status Versioning status + * Constructor If versioning is enabled for a bucket, it cannot be disabled + * or changed to {@link #SUSPENDED}. + * + * @param status + * Versioning status * @see #ENABLED * @see #SUSPENDED */ @Deprecated - public BucketVersioningConfiguration(String status) - { + public BucketVersioningConfiguration(String status) { this.status = VersioningStatusEnum.getValueFromCode(status); } - + /** - * Constructor - * If versioning is enabled for a bucket, it cannot be disabled or changed to suspended. - * @param status Versioning status + * Constructor If versioning is enabled for a bucket, it cannot be disabled + * or changed to suspended. + * + * @param status + * Versioning status */ - public BucketVersioningConfiguration(VersioningStatusEnum status) - { + public BucketVersioningConfiguration(VersioningStatusEnum status) { this.status = status; } - - public BucketVersioningConfiguration(){ - + + public BucketVersioningConfiguration() { + } - + /** * Obtain the versioning status of a bucket. + * * @return status Versioning status * @see #getVersioningStatus() */ @Deprecated - public String getStatus() - { + public String getStatus() { return this.status != null ? this.status.getCode() : null; } - + /** * Set the versioning status. - * @param status Versioning status + * + * @param status + * Versioning status * @see #setVersioningStatus(VersioningStatusEnum status) */ @Deprecated - public void setStatus(String status) - { + public void setStatus(String status) { this.status = VersioningStatusEnum.getValueFromCode(status); } - + /** * Obtain the versioning status. + * * @return status Versioning status */ - public VersioningStatusEnum getVersioningStatus() - { + public VersioningStatusEnum getVersioningStatus() { return status; } - + /** * Set the versioning status. - * @param status Versioning status + * + * @param status + * Versioning status */ - public void setVersioningStatus(VersioningStatusEnum status) - { + public void setVersioningStatus(VersioningStatusEnum status) { this.status = status; } @Override - public String toString() - { + public String toString() { return "BucketVersioningConfiguration [status=" + status + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/CacheOptionEnum.java b/app/src/main/java/com/obs/services/model/CacheOptionEnum.java index b27a085..d24f578 100644 --- a/app/src/main/java/com/obs/services/model/CacheOptionEnum.java +++ b/app/src/main/java/com/obs/services/model/CacheOptionEnum.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** @@ -19,27 +20,28 @@ */ public enum CacheOptionEnum { - /** - * Prioritize performance of the read-ahead cache, but the consistency is not ensured. - */ - PERFORMANCE("cache-with-performance"); - - private String code; - - private CacheOptionEnum(String code) { - this.code = code; - } + /** + * Prioritize performance of the read-ahead cache, but the consistency is + * not ensured. + */ + PERFORMANCE("cache-with-performance"); + + private String code; + + private CacheOptionEnum(String code) { + this.code = code; + } + + public String getCode() { + return code; + } - public String getCode() { - return code; - } - - public static CacheOptionEnum getValueFromCode(String code) { - for (CacheOptionEnum val : CacheOptionEnum.values()) { - if (val.code.equals(code)) { - return val; - } - } - return null; - } + public static CacheOptionEnum getValueFromCode(String code) { + for (CacheOptionEnum val : CacheOptionEnum.values()) { + if (val.code.equals(code)) { + return val; + } + } + return null; + } } diff --git a/app/src/main/java/com/obs/services/model/Callback.java b/app/src/main/java/com/obs/services/model/Callback.java new file mode 100644 index 0000000..0ab0c4c --- /dev/null +++ b/app/src/main/java/com/obs/services/model/Callback.java @@ -0,0 +1,54 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +public class Callback { + private String callbackUrl; + private String callbackHost; + private String callbackBody; + private String callbackBodyType; + + public String getCallbackUrl() { + return callbackUrl; + } + + public void setCallbackUrl(String callbackUrl) { + this.callbackUrl = callbackUrl; + } + + public String getCallbackHost() { + return callbackHost; + } + + public void setCallbackHost(String callbackHost) { + this.callbackHost = callbackHost; + } + + public String getCallbackBody() { + return callbackBody; + } + + public void setCallbackBody(String callbackBody) { + this.callbackBody = callbackBody; + } + + public String getCallbackBodyType() { + return callbackBodyType; + } + + public void setCallbackBodyType(String callbackBodyType) { + this.callbackBodyType = callbackBodyType; + } +} diff --git a/app/src/main/java/com/obs/services/model/CanonicalGrantee.java b/app/src/main/java/com/obs/services/model/CanonicalGrantee.java index f77e418..b24e5e0 100644 --- a/app/src/main/java/com/obs/services/model/CanonicalGrantee.java +++ b/app/src/main/java/com/obs/services/model/CanonicalGrantee.java @@ -1,89 +1,80 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ package com.obs.services.model; /** * Grantee information in the ACL, {@link AccessControlList} */ -public class CanonicalGrantee implements GranteeInterface -{ +public class CanonicalGrantee implements GranteeInterface { private String grantId; - + private String displayName; - - public CanonicalGrantee() - { + + public CanonicalGrantee() { } - + /** * Constructor * - * @param identifier ID of the domain to which the grantee belongs + * @param identifier + * ID of the domain to which the grantee belongs */ - public CanonicalGrantee(String identifier) - { + public CanonicalGrantee(String identifier) { this.grantId = identifier; } - + /** * Set the ID of the domain to which the grantee belongs. * - * @param canonicalGrantId ID of the domain to which the grantee belongs + * @param canonicalGrantId + * ID of the domain to which the grantee belongs */ - public void setIdentifier(String canonicalGrantId) - { + public void setIdentifier(String canonicalGrantId) { this.grantId = canonicalGrantId; } - + /** * Obtain the ID of the domain to which the grantee belongs. * * @return ID of the domain to which the grantee belongs */ - public String getIdentifier() - { + public String getIdentifier() { return grantId; } - + /** * Set the username for the grantee. * - * @param displayName Username of the grantee + * @param displayName + * Username of the grantee */ - @Deprecated - public void setDisplayName(String displayName) - { + public void setDisplayName(String displayName) { this.displayName = displayName; } - + /** * Obtain the username of the grantee. * * @return Username of the grantee */ - @Deprecated - public String getDisplayName() - { + public String getDisplayName() { return this.displayName; } - - - + @Override - public int hashCode() - { + public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((grantId == null) ? 0 : grantId.hashCode()); @@ -91,29 +82,25 @@ public int hashCode() } @Override - public boolean equals(Object obj) - { - if (this == obj) + public boolean equals(Object obj) { + if (this == obj) { return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) + } + if (obj == null) { return false; - CanonicalGrantee other = (CanonicalGrantee)obj; - if (grantId == null) - { - if (other.grantId != null) - return false; } - else if (!grantId.equals(other.grantId)) + if (getClass() != obj.getClass()) { return false; - return true; + } + CanonicalGrantee other = (CanonicalGrantee) obj; + if (grantId == null) { + return other.grantId == null; + } else { + return grantId.equals(other.grantId); + } } - public String toString() - { + public String toString() { return "CanonicalGrantee [id=" + grantId + (displayName != null ? ", displayName=" + displayName : "") + "]"; } } - - diff --git a/app/src/main/java/com/obs/services/model/CompleteMultipartUploadRequest.java b/app/src/main/java/com/obs/services/model/CompleteMultipartUploadRequest.java index 6d45d96..4246459 100644 --- a/app/src/main/java/com/obs/services/model/CompleteMultipartUploadRequest.java +++ b/app/src/main/java/com/obs/services/model/CompleteMultipartUploadRequest.java @@ -1,16 +1,16 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + **/ package com.obs.services.model; @@ -20,127 +20,123 @@ /** * Parameters in a request for combining parts */ -public class CompleteMultipartUploadRequest -{ - private String uploadId; - - private String bucketName; - - private String objectKey; - - private List partEtag; - - public CompleteMultipartUploadRequest(){ - - } - - /** - * Constructor - * @param bucketName Bucket name - * @param objectKey Object name - * @param uploadId Multipart upload ID - * @param partEtag List of parts to be combined - */ - public CompleteMultipartUploadRequest(String bucketName, String objectKey, String uploadId, List partEtag) +public class CompleteMultipartUploadRequest extends AbstractMultipartRequest { + { - this.uploadId = uploadId; - this.bucketName = bucketName; - this.objectKey = objectKey; - this.partEtag = partEtag; + httpMethod = HttpMethodEnum.POST; } + + private List partEtag; + private String encodingType; + private Callback callback; - /** - * Obtain the multipart upload ID. - * - * @return Multipart upload ID - */ - public String getUploadId() - { - return uploadId; - } - - /** - * Set the multipart upload ID. - * - * @param uploadId Multipart upload ID - */ - public void setUploadId(String uploadId) - { - this.uploadId = uploadId; - } - - /** - * Obtain the name of the bucket to which the multipart upload belongs. - * - * @return Name of the bucket to which the multipart upload belongs - */ - public String getBucketName() - { - return bucketName; + public CompleteMultipartUploadRequest() { } - - /** - * Set the name for the bucket to which the multipart upload belongs. - * - * @param bucketName Name of the bucket to which the multipart upload belongs - */ - public void setBucketName(String bucketName) - { + + public CompleteMultipartUploadRequest(String bucketName) { this.bucketName = bucketName; } - + /** - * Obtain the name of the object involved in the multipart upload. - * - * @return Name of the object involved in the multipart upload + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param uploadId + * Multipart upload ID + * @param partEtag + * List of parts to be combined */ - public String getObjectKey() - { - return objectKey; + public CompleteMultipartUploadRequest(String bucketName, String objectKey, String uploadId, + List partEtag) { + super(); + this.setUploadId(uploadId); + this.setBucketName(bucketName); + this.setObjectKey(objectKey); + this.partEtag = partEtag; } - + /** - * Set the name for the object involved in the multipart upload. - * - @param objectKey Name of the object involved in the multipart upload + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param uploadId + * Multipart upload ID + * @param partEtag + * List of parts to be combined + * @param encodingType + * Use this encoding type to encode keys that contains invalid characters, the value could be "url" */ - public void setObjectKey(String objectKey) - { - this.objectKey = objectKey; + public CompleteMultipartUploadRequest(String bucketName, String objectKey, String uploadId, + List partEtag, String encodingType) { + super(); + this.setUploadId(uploadId); + this.setBucketName(bucketName); + this.setObjectKey(objectKey); + this.partEtag = partEtag; + this.encodingType = encodingType; } - + /** * Obtain the to-be-combined part list. - * + * * @return List of parts to be combined */ - public List getPartEtag() - { - if(this.partEtag == null){ + public List getPartEtag() { + if (this.partEtag == null) { this.partEtag = new ArrayList(); } return this.partEtag; } - + /** * Set the to-be-combined part list. - * - * @param partEtags List of parts to be combined + * + * @param partEtags + * List of parts to be combined */ - public void setPartEtag(List partEtags) - { + public void setPartEtag(List partEtags) { this.partEtag = partEtags; } - @Override - public String toString() - { - return "CompleteMultipartUploadRequest [uploadId=" + uploadId + ", bucketName=" + bucketName + ", objectKey=" + objectKey - + ", partEtag=" + partEtag + "]"; + /** + * Set encoding type to encode objectkeys, the value could be url + * + * @param encodingType + * encoding type for encoding + */ + public void setEncodingType(String encodingType) { + this.encodingType = encodingType; + } + + /** + * Get encoding type to encode objectkeys + * @return encoding type for encoding + */ + public String getEncodingType() { + return encodingType; + } + + public Callback getCallback() { + return callback; + } + + public void setCallback(Callback callback) { + this.callback = callback; } - -} + @Override + public String toString() { + return "CompleteMultipartUploadRequest [uploadId=" + this.getUploadId() + + ", bucketName=" + this.getBucketName() + ", objectKey=" + + this.getObjectKey() + ", partEtag=" + partEtag + ", encodingType=" + encodingType + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/CompleteMultipartUploadResult.java b/app/src/main/java/com/obs/services/model/CompleteMultipartUploadResult.java index 47a500f..7ec2177 100644 --- a/app/src/main/java/com/obs/services/model/CompleteMultipartUploadResult.java +++ b/app/src/main/java/com/obs/services/model/CompleteMultipartUploadResult.java @@ -11,100 +11,127 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; +import java.io.InputStream; + /** * Response to a request for combining parts */ -public class CompleteMultipartUploadResult extends HeaderResponse -{ +public class CompleteMultipartUploadResult extends HeaderResponse { private String bucketName; - + private String objectKey; - + private String etag; - + private String location; - + private String versionId; - + private String objectUrl; - + + private String encodingType; + + private InputStream callbackResponseBody; + public CompleteMultipartUploadResult(String bucketName, String objectKey, String etag, String location, - String versionId, String objectUrl) { - this.bucketName = bucketName; - this.objectKey = objectKey; - this.etag = etag; - this.location = location; - this.versionId = versionId; - this.objectUrl = objectUrl; - } - - /** + String versionId, String objectUrl) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.etag = etag; + this.location = location; + this.versionId = versionId; + this.objectUrl = objectUrl; + } + + /** * Obtain the name of the bucket to which the multipart upload belongs. * * @return Name of the bucket to which the multipart upload belongs */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } - + /** * Obtain the name of the object involved in the multipart upload. * * @return Name of the object involved in the multipart upload */ - public String getObjectKey() - { + public String getObjectKey() { return objectKey; } - + /** * Obtain the ETag of the object involved in the multipart upload. * * @return ETag of the object involved in the multipart upload */ - public String getEtag() - { + public String getEtag() { return etag; } - /** * Obtain the URI of the object after part combination. + * * @return URI of the object obtained after part combination */ - public String getLocation() { - return location; - } - - - /** - * Obtain the version ID of the object after part combination. - * @return Version ID of the object after part combination - */ - public String getVersionId() { - return versionId; - } - + public String getLocation() { + return location; + } + + /** + * Obtain the version ID of the object after part combination. + * + * @return Version ID of the object after part combination + */ + public String getVersionId() { + return versionId; + } + /** * Obtain the full path to the object after part combination. + * * @return Full path to the object */ - public String getObjectUrl() { - return objectUrl; - } + public String getObjectUrl() { + return objectUrl; + } - @Override - public String toString() { - return "CompleteMultipartUploadResult [bucketName=" + bucketName + ", objectKey=" + objectKey + ", etag=" + etag - + ", location=" + location + ", versionId=" + versionId + ", objectUrl=" + objectUrl + "]"; - } + public void setObjectKey(String objectKey) { + this.objectKey = objectKey; + } + /** + * Set encoding type for objectKey in xml + * @param encodingType encoding type for objectKey + */ + public void setEncodingType(String encodingType) { + this.encodingType = encodingType; + } - - -} + /** + * Get encoding type for objectKey in xml + * @return encodingType + */ + public String getEncodingType() { + return encodingType; + } + public InputStream getCallbackResponseBody() { + return callbackResponseBody; + } + public void setCallbackResponseBody(InputStream callbackResponseBody) { + this.callbackResponseBody = callbackResponseBody; + } + + @Override + public String toString() { + return "CompleteMultipartUploadResult [bucketName=" + bucketName + ", objectKey=" + objectKey + ", etag=" + etag + + ", location=" + location + ", versionId=" + versionId + ", objectUrl=" + objectUrl + "]"; + } + +} diff --git a/app/src/main/java/com/obs/services/model/CopyObjectRequest.java b/app/src/main/java/com/obs/services/model/CopyObjectRequest.java index 477252f..4111ae7 100644 --- a/app/src/main/java/com/obs/services/model/CopyObjectRequest.java +++ b/app/src/main/java/com/obs/services/model/CopyObjectRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.Date; @@ -20,330 +21,346 @@ /** * Parameters in a request for copying an object */ -public class CopyObjectRequest extends PutObjectBasicRequest -{ +public class CopyObjectRequest extends PutObjectBasicRequest { private String sourceBucketName; - + private String sourceObjectKey; - + private ObjectMetadata newObjectMetadata; - + private boolean replaceMetadata; - + private Date ifModifiedSince; - + private Date ifUnmodifiedSince; - + private String ifMatchTag; - + private String ifNoneMatchTag; - + private String versionId; - + private SseCHeader sseCHeaderSource; - - - + + public CopyObjectRequest() { + } + /** * Constructor * - * @param sourceBucketName Source bucket name - * @param sourceObjectKey Source object name - * @param destinationBucketName Destination bucket name - * @param destinationObjectKey Destination object name + * @param sourceBucketName + * Source bucket name + * @param sourceObjectKey + * Source object name + * @param destinationBucketName + * Destination bucket name + * @param destinationObjectKey + * Destination object name */ - public CopyObjectRequest(String sourceBucketName, String sourceObjectKey, String destinationBucketName, String destinationObjectKey) - { + public CopyObjectRequest(String sourceBucketName, String sourceObjectKey, String destinationBucketName, + String destinationObjectKey) { this.sourceBucketName = sourceBucketName; this.sourceObjectKey = sourceObjectKey; this.bucketName = destinationBucketName; this.objectKey = destinationObjectKey; } - - public CopyObjectRequest(){ - - } - - + /** - * Obtain SSE-C decryption headers of the source object. + * Obtain SSE-C decryption headers of the source object. * * @return SSE-C decryption headers of the source object */ - public SseCHeader getSseCHeaderSource() - { + public SseCHeader getSseCHeaderSource() { return sseCHeaderSource; } - + /** - * Set SSE-C decryption headers of the source object. + * Set SSE-C decryption headers of the source object. * - * @param sseCHeaderSource SSE-C decryption headers of the source object + * @param sseCHeaderSource + * SSE-C decryption headers of the source object */ - public void setSseCHeaderSource(SseCHeader sseCHeaderSource) - { + public void setSseCHeaderSource(SseCHeader sseCHeaderSource) { this.sseCHeaderSource = sseCHeaderSource; } - + /** - * Obtain SSE-C encryption headers of the destination object. + * Obtain SSE-C encryption headers of the destination object. * * @return SSE-C encryption headers */ @Deprecated - public SseCHeader getSseCHeaderDestination() - { + public SseCHeader getSseCHeaderDestination() { return this.sseCHeader; } - + /** - * Set SSE-C encryption headers for the destination object. + * Set SSE-C encryption headers for the destination object. * - * @param sseCHeaderDestination SSE-C encryption headers + * @param sseCHeaderDestination + * SSE-C encryption headers */ @Deprecated - public void setSseCHeaderDestination(SseCHeader sseCHeaderDestination) - { + public void setSseCHeaderDestination(SseCHeader sseCHeaderDestination) { this.sseCHeader = sseCHeaderDestination; } - + /** - * Obtain the time condition for copying the object: Only when the source object is modified after the point in time specified by this parameter, it can be copied; otherwise, "412 Precondition Failed" will be returned. + * Obtain the time condition for copying the object: Only when the source + * object is modified after the point in time specified by this parameter, + * it can be copied; otherwise, "412 Precondition Failed" will be returned. * * @return Time condition set for copying the object */ - public Date getIfModifiedSince() - { + public Date getIfModifiedSince() { return ServiceUtils.cloneDateIgnoreNull(this.ifModifiedSince); } - + /** - * Set the time condition for copying the object: Only when the source object is modified after the point in time specified by this parameter, it can be copied; otherwise, "412 Precondition Failed" will be returned. + * Set the time condition for copying the object: Only when the source + * object is modified after the point in time specified by this parameter, + * it can be copied; otherwise, "412 Precondition Failed" will be returned. * - * @param ifModifiedSince Time condition set for copying the object + * @param ifModifiedSince + * Time condition set for copying the object * */ - public void setIfModifiedSince(Date ifModifiedSince) - { + public void setIfModifiedSince(Date ifModifiedSince) { this.ifModifiedSince = ServiceUtils.cloneDateIgnoreNull(ifModifiedSince); } - + /** - * Obtain the time condition for copying the object: Only when the source object remains unchanged after the point in time specified by this parameter, it can be copied; otherwise, "412 Precondition Failed" will be returned. + * Obtain the time condition for copying the object: Only when the source + * object remains unchanged after the point in time specified by this + * parameter, it can be copied; otherwise, "412 Precondition Failed" will be + * returned. * * @return Time condition set for copying the object */ - public Date getIfUnmodifiedSince() - { + public Date getIfUnmodifiedSince() { return ServiceUtils.cloneDateIgnoreNull(this.ifUnmodifiedSince); } - + /** - * Set the time condition for copying the object: Only when the source object remains unchanged after the point in time specified by this parameter, it can be copied; otherwise, "412 Precondition Failed" will be returned. + * Set the time condition for copying the object: Only when the source + * object remains unchanged after the point in time specified by this + * parameter, it can be copied; otherwise, "412 Precondition Failed" will be + * returned. * - * @param ifUnmodifiedSince Time condition set for copying the object + * @param ifUnmodifiedSince + * Time condition set for copying the object */ - public void setIfUnmodifiedSince(Date ifUnmodifiedSince) - { + public void setIfUnmodifiedSince(Date ifUnmodifiedSince) { this.ifUnmodifiedSince = ServiceUtils.cloneDateIgnoreNull(ifUnmodifiedSince); } - + /** - * Obtain the ETag verification condition for copying the object: Only when the ETag of the source object is the same as that specified by this parameter, the object can be copied. Otherwise, "412 Precondition Failed" will be returned. + * Obtain the ETag verification condition for copying the object: Only when + * the ETag of the source object is the same as that specified by this + * parameter, the object can be copied. Otherwise, "412 Precondition Failed" + * will be returned. * * @return ETag verification condition set for copying the object */ - public String getIfMatchTag() - { + public String getIfMatchTag() { return ifMatchTag; } - + /** - * Set the ETag verification condition for copying the object: Only when the ETag of the source object is the same as that specified by this parameter, the object can be copied. Otherwise, "412 Precondition Failed" will be returned. + * Set the ETag verification condition for copying the object: Only when the + * ETag of the source object is the same as that specified by this + * parameter, the object can be copied. Otherwise, "412 Precondition Failed" + * will be returned. * - * @param ifMatchTag ETag verification condition set for copying the object + * @param ifMatchTag + * ETag verification condition set for copying the object */ - public void setIfMatchTag(String ifMatchTag) - { + public void setIfMatchTag(String ifMatchTag) { this.ifMatchTag = ifMatchTag; } - + /** - * Obtain the ETag verification condition for copying the object: Only when the ETag of the source object is different from that specified by this parameter, the object will be copied. Otherwise, "412 Precondition Failed" will be returned. + * Obtain the ETag verification condition for copying the object: Only when + * the ETag of the source object is different from that specified by this + * parameter, the object will be copied. Otherwise, + * "412 Precondition Failed" will be returned. * * @return ETag verification condition set for copying the object */ - public String getIfNoneMatchTag() - { + public String getIfNoneMatchTag() { return ifNoneMatchTag; } - + /** - * Set the ETag verification condition for copying the object: Only when the ETag of the source object is different from that specified by this parameter, the object will be copied. Otherwise, "412 Precondition Failed" will be returned. + * Set the ETag verification condition for copying the object: Only when the + * ETag of the source object is different from that specified by this + * parameter, the object will be copied. Otherwise, + * "412 Precondition Failed" will be returned. * - * @param ifNoneMatchTag ETag verification condition set for copying the object + * @param ifNoneMatchTag + * ETag verification condition set for copying the object * */ - public void setIfNoneMatchTag(String ifNoneMatchTag) - { + public void setIfNoneMatchTag(String ifNoneMatchTag) { this.ifNoneMatchTag = ifNoneMatchTag; } - + /** - * Obtain the version ID of the source object. + * Obtain the version ID of the source object. * * @return Version ID of the source object * */ - public String getVersionId() - { + public String getVersionId() { return versionId; } - + /** - * Set the version ID for the source object. + * Set the version ID for the source object. * - * @param versionId Version ID of the source object + * @param versionId + * Version ID of the source object * */ - public void setVersionId(String versionId) - { + public void setVersionId(String versionId) { this.versionId = versionId; } - + /** * Obtain the source bucket name. * * @return Source bucket name */ - public String getSourceBucketName() - { + public String getSourceBucketName() { return sourceBucketName; } - + /** * Set the source bucket name. * - * @param sourceBucketName Source bucket name + * @param sourceBucketName + * Source bucket name */ - public void setSourceBucketName(String sourceBucketName) - { + public void setSourceBucketName(String sourceBucketName) { this.sourceBucketName = sourceBucketName; } - + /** * Obtain the source object name. * * @return Source object name */ - public String getSourceObjectKey() - { + public String getSourceObjectKey() { return sourceObjectKey; } - + /** * Set the source object name. * - * @param sourceObjectKey Source object name + * @param sourceObjectKey + * Source object name */ - public void setSourceObjectKey(String sourceObjectKey) - { + public void setSourceObjectKey(String sourceObjectKey) { this.sourceObjectKey = sourceObjectKey; } - + /** * Obtain the destination bucket name. * * @return Destination bucket name */ - public String getDestinationBucketName() - { + public String getDestinationBucketName() { return this.bucketName; } - + /** * Set the destination bucket name. * - * @param destinationBucketName Destination bucket name + * @param destinationBucketName + * Destination bucket name */ - public void setDestinationBucketName(String destinationBucketName) - { + public void setDestinationBucketName(String destinationBucketName) { this.bucketName = destinationBucketName; } - + /** * Obtain the destination object name. * * @return Destination object name */ - public String getDestinationObjectKey() - { + public String getDestinationObjectKey() { return this.objectKey; } - + /** * Set the destination object name. * - * @param destinationObjectKey Destination object name + * @param destinationObjectKey + * Destination object name */ - public void setDestinationObjectKey(String destinationObjectKey) - { + public void setDestinationObjectKey(String destinationObjectKey) { this.objectKey = destinationObjectKey; } - + /** - * Obtain the properties, including customized metadata, of the destination object. + * Obtain the properties, including customized metadata, of the destination + * object. * * @return ObjectMetadata Properties of the destination object */ - public ObjectMetadata getNewObjectMetadata() - { + public ObjectMetadata getNewObjectMetadata() { return newObjectMetadata; } - + /** - * Set the properties, including customized metadata, of the destination object. + * Set the properties, including customized metadata, of the destination + * object. * - * @param newObjectMetadata Properties of the destination object + * @param newObjectMetadata + * Properties of the destination object */ - public void setNewObjectMetadata(ObjectMetadata newObjectMetadata) - { + public void setNewObjectMetadata(ObjectMetadata newObjectMetadata) { this.newObjectMetadata = newObjectMetadata; } - + /** - * Obtain the identifier specifying whether to replace properties of the destination object. "true" indicates that properties will be replaced (used together with "setNewObjectMetadata") and "false" indicates that the destination object inherits properties from the source object. + * Obtain the identifier specifying whether to replace properties of the + * destination object. "true" indicates that properties will be replaced + * (used together with "setNewObjectMetadata") and "false" indicates that + * the destination object inherits properties from the source object. * - * @return Identifier specifying whether to replace the properties of the destination object + * @return Identifier specifying whether to replace the properties of the + * destination object */ - public boolean isReplaceMetadata() - { + public boolean isReplaceMetadata() { return replaceMetadata; } - + /** - * Set the identifier specifying whether to replace properties of the destination object. "true" indicates that properties will be replaced (used together with "setNewObjectMetadata") and "false" indicates that the destination object inherits properties from the source object. + * Set the identifier specifying whether to replace properties of the + * destination object. "true" indicates that properties will be replaced + * (used together with "setNewObjectMetadata") and "false" indicates that + * the destination object inherits properties from the source object. + * + * @param replaceMetadata + * Identifier specifying whether to replace the properties of the + * destination object * - * @param replaceMetadata Identifier specifying whether to replace the properties of the destination object - * */ - public void setReplaceMetadata(boolean replaceMetadata) - { + public void setReplaceMetadata(boolean replaceMetadata) { this.replaceMetadata = replaceMetadata; } - - @Override - public String toString() { - return "CopyObjectRequest [sourceBucketName=" + sourceBucketName + ", sourceObjectKey=" + sourceObjectKey - + ", destinationBucketName=" + bucketName + ", destinationObjectKey=" + objectKey - + ", newObjectMetadata=" + newObjectMetadata + ", replaceMetadata=" + replaceMetadata - + ", ifModifiedSince=" + ifModifiedSince + ", ifUnmodifiedSince=" + ifUnmodifiedSince + ", ifMatchTag=" - + ifMatchTag + ", ifNoneMatchTag=" + ifNoneMatchTag + ", versionId=" + versionId + ", sseKmsHeader=" - + sseKmsHeader + ", sseCHeaderSource=" + sseCHeaderSource + ", sseCHeaderDestination=" - + sseCHeader + ", acl=" + acl + ", successRedirectLocation=" + successRedirectLocation + "]"; - } - - -} + @Override + public String toString() { + return "CopyObjectRequest [sourceBucketName=" + sourceBucketName + ", sourceObjectKey=" + sourceObjectKey + + ", destinationBucketName=" + bucketName + ", destinationObjectKey=" + objectKey + + ", newObjectMetadata=" + newObjectMetadata + ", replaceMetadata=" + replaceMetadata + + ", isEncodeHeaders=" + encodeHeaders + ", ifModifiedSince=" + ifModifiedSince + + ", ifUnmodifiedSince=" + ifUnmodifiedSince + ", ifMatchTag=" + ifMatchTag + + ", ifNoneMatchTag=" + ifNoneMatchTag + ", versionId=" + versionId + ", sseKmsHeader=" + + sseKmsHeader + ", sseCHeaderSource=" + sseCHeaderSource + ", sseCHeaderDestination=" + sseCHeader + + ", acl=" + acl + ", successRedirectLocation=" + successRedirectLocation + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/CopyObjectResult.java b/app/src/main/java/com/obs/services/model/CopyObjectResult.java index 31ea8ec..cb1abe7 100644 --- a/app/src/main/java/com/obs/services/model/CopyObjectResult.java +++ b/app/src/main/java/com/obs/services/model/CopyObjectResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.Date; @@ -20,85 +21,75 @@ /** * Response to a request for copying an object */ -public class CopyObjectResult extends HeaderResponse -{ +public class CopyObjectResult extends HeaderResponse { private String etag; - + private Date lastModified; - + private String versionId; - + private String copySourceVersionId; - + private StorageClassEnum storageClass; - - - public CopyObjectResult(String etag, Date lastModified, String versionId, String copySourceVersionId, - StorageClassEnum storageClass) { - this.etag = etag; - this.lastModified = ServiceUtils.cloneDateIgnoreNull(lastModified); - this.versionId = versionId; - this.copySourceVersionId = copySourceVersionId; - this.storageClass = storageClass; - } + public CopyObjectResult(String etag, Date lastModified, String versionId, String copySourceVersionId, + StorageClassEnum storageClass) { + this.etag = etag; + this.lastModified = ServiceUtils.cloneDateIgnoreNull(lastModified); + this.versionId = versionId; + this.copySourceVersionId = copySourceVersionId; + this.storageClass = storageClass; + } - /** + /** * Obtain the ETag of the destination object. * * @return ETag value of the destination object */ - public String getEtag() - { + public String getEtag() { return etag; } - - + /** * Last modification time of the destination object * * @return Last modification time of the destination object */ - public Date getLastModified() - { + public Date getLastModified() { return ServiceUtils.cloneDateIgnoreNull(this.lastModified); } - + /** - * Obtain the version ID of the destination object. + * Obtain the version ID of the destination object. + * * @return Version ID of the object */ - public String getVersionId() { - return versionId; - } - + public String getVersionId() { + return versionId; + } - /** - * Obtain the version ID of the source object. + /** + * Obtain the version ID of the source object. + * * @return Version ID of the object */ - public String getCopySourceVersionId() { - return copySourceVersionId; - } - + public String getCopySourceVersionId() { + return copySourceVersionId; + } - /** + /** * Obtain the storage class of the destination object. + * * @return Object storage class */ - public StorageClassEnum getObjectStorageClass() - { + public StorageClassEnum getObjectStorageClass() { return storageClass; } - - - @Override - public String toString() { - return "CopyObjectResult [etag=" + etag + ", lastModified=" + lastModified + ", versionId=" + versionId - + ", copySourceVersionId=" + copySourceVersionId + ", storageClass=" + storageClass + "]"; - } - - -} + @Override + public String toString() { + return "CopyObjectResult [etag=" + etag + ", lastModified=" + lastModified + ", versionId=" + versionId + + ", copySourceVersionId=" + copySourceVersionId + ", storageClass=" + storageClass + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/CopyPartRequest.java b/app/src/main/java/com/obs/services/model/CopyPartRequest.java index 6656a60..8e7ae5c 100644 --- a/app/src/main/java/com/obs/services/model/CopyPartRequest.java +++ b/app/src/main/java/com/obs/services/model/CopyPartRequest.java @@ -17,46 +17,54 @@ /** * Parameters in the request for copying a part */ -public class CopyPartRequest -{ - private String uploadId; - +public class CopyPartRequest extends AbstractMultipartRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + private String sourceBucketName; - + private String sourceObjectKey; - + private String destinationBucketName; - + private String destinationObjectKey; - + private Long byteRangeStart; - + private Long byteRangeEnd; - + private SseCHeader sseCHeaderSource; - + private SseCHeader sseCHeaderDestination; - + private String versionId; - + private int partNumber; - - public CopyPartRequest(){ - + + public CopyPartRequest() { + } - + /** * Constructor - * @param uploadId Multipart upload ID - * @param sourceBucketName Source bucket name - * @param sourceObjectKey Source object name - * @param destinationBucketName Destination bucket name - * @param destinationObjectKey Destination object name - * @param partNumber Part number + * + * @param uploadId + * Multipart upload ID + * @param sourceBucketName + * Source bucket name + * @param sourceObjectKey + * Source object name + * @param destinationBucketName + * Destination bucket name + * @param destinationObjectKey + * Destination object name + * @param partNumber + * Part number */ - public CopyPartRequest(String uploadId, String sourceBucketName, String sourceObjectKey, String destinationBucketName, - String destinationObjectKey, int partNumber) - { + public CopyPartRequest(String uploadId, String sourceBucketName, String sourceObjectKey, + String destinationBucketName, String destinationObjectKey, int partNumber) { this.uploadId = uploadId; this.sourceBucketName = sourceBucketName; this.sourceObjectKey = sourceObjectKey; @@ -65,248 +73,265 @@ public CopyPartRequest(String uploadId, String sourceBucketName, String sourceOb this.partNumber = partNumber; } - - /** - * Obtain SSE-C decryption headers of the source object. + * Obtain SSE-C decryption headers of the source object. * * @return SSE-C decryption headers of the source object */ - public SseCHeader getSseCHeaderSource() - { + public SseCHeader getSseCHeaderSource() { return sseCHeaderSource; } - + /** - * Set SSE-C decryption headers of the source object. + * Set SSE-C decryption headers of the source object. * - * @param sseCHeaderSource SSE-C decryption headers of the source object + * @param sseCHeaderSource + * SSE-C decryption headers of the source object */ - public void setSseCHeaderSource(SseCHeader sseCHeaderSource) - { + public void setSseCHeaderSource(SseCHeader sseCHeaderSource) { this.sseCHeaderSource = sseCHeaderSource; } - + /** - * Obtain SSE-C encryption headers of the destination object. + * Obtain SSE-C encryption headers of the destination object. * * @return SSE-C encryption headers of the destination object */ - public SseCHeader getSseCHeaderDestination() - { + public SseCHeader getSseCHeaderDestination() { return sseCHeaderDestination; } - + /** - * Set SSE-C encryption headers for the destination object. + * Set SSE-C encryption headers for the destination object. * - * @param sseCHeaderDestination SSE-C encryption headers of the destination object + * @param sseCHeaderDestination + * SSE-C encryption headers of the destination object */ - public void setSseCHeaderDestination(SseCHeader sseCHeaderDestination) - { + public void setSseCHeaderDestination(SseCHeader sseCHeaderDestination) { this.sseCHeaderDestination = sseCHeaderDestination; } - + /** * Obtain the start position for copying. * * @return Start position for copying */ - public Long getByteRangeStart() - { + public Long getByteRangeStart() { return byteRangeStart; } - + /** * Set the start position for copying. * - * @param byteRangeStart Start position for copying + * @param byteRangeStart + * Start position for copying * */ - public void setByteRangeStart(Long byteRangeStart) - { + public void setByteRangeStart(Long byteRangeStart) { this.byteRangeStart = byteRangeStart; } - + /** * Obtain the end position for copying. * * @return End position for copying */ - public Long getByteRangeEnd() - { + public Long getByteRangeEnd() { return byteRangeEnd; } - + /** * Set the end position for copying. * - * @param byteRangeEnd End position for copying + * @param byteRangeEnd + * End position for copying * */ - public void setByteRangeEnd(Long byteRangeEnd) - { + public void setByteRangeEnd(Long byteRangeEnd) { this.byteRangeEnd = byteRangeEnd; } - + /** * Obtain the part number of the to-be-copied part. * * @return Part number */ - public int getPartNumber() - { + public int getPartNumber() { return partNumber; } - + /** * Set the part number of the to-be-copied part. * - * @param partNumber Part number + * @param partNumber + * Part number * */ - public void setPartNumber(int partNumber) - { + public void setPartNumber(int partNumber) { this.partNumber = partNumber; } - - /** - * Obtain the multipart upload ID. - * - * @return Multipart upload ID - */ - public String getUploadId() - { - return uploadId; - } - - /** - * Set the multipart upload ID. - * - * @param uploadId Multipart upload ID - */ - public void setUploadId(String uploadId) - { - this.uploadId = uploadId; - } - + /** * Obtain the source bucket name. * * @return Source bucket name */ - public String getSourceBucketName() - { + public String getSourceBucketName() { return sourceBucketName; } - + /** * Set the source bucket name. * - * @param bucketName Source bucket name + * @param bucketName + * Source bucket name * */ - public void setSourceBucketName(String bucketName) - { + public void setSourceBucketName(String bucketName) { this.sourceBucketName = bucketName; } - + /** * Obtain the source object name. * * @return Source object name */ - public String getSourceObjectKey() - { + public String getSourceObjectKey() { return sourceObjectKey; } - + /** * Set the source object name. * - * @param objectKey Source object name + * @param objectKey + * Source object name * */ - public void setSourceObjectKey(String objectKey) - { + public void setSourceObjectKey(String objectKey) { this.sourceObjectKey = objectKey; } - + /** - * Obtain the name of the bucket (destination bucket) to which the multipart upload belongs. + * Obtain the name of the bucket (destination bucket) to which the multipart + * upload belongs. * * @return Name of the bucket to which the multipart upload belongs */ - public String getDestinationBucketName() - { + public String getDestinationBucketName() { return destinationBucketName; } - + /** - * Set the name of the bucket (destination bucket) to which the multipart upload belongs. + * Set the name of the bucket (destination bucket) to which the multipart + * upload belongs. * - * @param destBucketName Name of the bucket to which the multipart upload belongs + * @param destBucketName + * Name of the bucket to which the multipart upload belongs * */ - public void setDestinationBucketName(String destBucketName) - { + public void setDestinationBucketName(String destBucketName) { this.destinationBucketName = destBucketName; } - + /** - * Obtain the name of the object (destination object) involved in the multipart upload. + * Obtain the name of the object (destination object) involved in the + * multipart upload. * * @return Name of the object involved in the multipart upload * */ - public String getDestinationObjectKey() - { + public String getDestinationObjectKey() { return destinationObjectKey; } - + /** - * Set the name of the object (destination object) involved in the multipart upload. + * Set the name of the object (destination object) involved in the multipart + * upload. * - * @param destObjectKey Name of the object involved in the multipart upload + * @param destObjectKey + * Name of the object involved in the multipart upload * */ - public void setDestinationObjectKey(String destObjectKey) - { + public void setDestinationObjectKey(String destObjectKey) { this.destinationObjectKey = destObjectKey; } - - + /** - * Obtain the version ID of the source object. + * Obtain the version ID of the source object. * * @return Version ID of the source object * */ - public String getVersionId() - { + public String getVersionId() { return versionId; } - + /** - * Set the version ID for the source object. + * Set the version ID for the source object. * - * @param versionId Version ID of the source object + * @param versionId + * Version ID of the source object * */ - public void setVersionId(String versionId) - { + public void setVersionId(String versionId) { this.versionId = versionId; } - @Override - public String toString() { - return "CopyPartRequest [uploadId=" + uploadId + ", sourceBucketName=" + sourceBucketName + ", sourceObjectKey=" - + sourceObjectKey + ", destinationBucketName=" + destinationBucketName + ", destinationObjectKey=" - + destinationObjectKey + ", byteRangeStart=" + byteRangeStart + ", byteRangeEnd=" + byteRangeEnd - + ", sseCHeaderSource=" + sseCHeaderSource + ", sseCHeaderDestination=" + sseCHeaderDestination - + ", versionId=" + versionId + ", partNumber=" + partNumber + "]"; - } - -} + /** + * Obtain the name of the bucket (destination bucket) to which the multipart + * upload belongs. + * + * @return Name of the bucket to which the multipart upload belongs + */ + @Override + public String getBucketName() { + return this.destinationBucketName; + } + + /** + * Set the name of the bucket (destination bucket) to which the multipart + * upload belongs. + * + * @param destBucketName + * Name of the bucket to which the multipart upload belongs + * + */ + @Override + public void setBucketName(String bucketName) { + this.destinationBucketName = bucketName; + } + + /** + * Obtain the name of the object (destination object) involved in the + * multipart upload. + * + * @return Name of the object involved in the multipart upload + * + */ + @Override + public void setObjectKey(String objectKey) { + this.destinationObjectKey = objectKey; + } + + /** + * Obtain the name of the object (destination object) involved in the + * multipart upload. + * + * @return Name of the object involved in the multipart upload + * + */ + @Override + public String getObjectKey() { + return this.destinationObjectKey; + } + @Override + public String toString() { + return "CopyPartRequest [uploadId=" + uploadId + ", sourceBucketName=" + sourceBucketName + ", sourceObjectKey=" + + sourceObjectKey + ", destinationBucketName=" + destinationBucketName + ", destinationObjectKey=" + + destinationObjectKey + ", byteRangeStart=" + byteRangeStart + ", byteRangeEnd=" + byteRangeEnd + + ", sseCHeaderSource=" + sseCHeaderSource + ", sseCHeaderDestination=" + sseCHeaderDestination + + ", versionId=" + versionId + ", partNumber=" + partNumber + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/CopyPartResult.java b/app/src/main/java/com/obs/services/model/CopyPartResult.java index 148a6fd..358b201 100644 --- a/app/src/main/java/com/obs/services/model/CopyPartResult.java +++ b/app/src/main/java/com/obs/services/model/CopyPartResult.java @@ -21,61 +21,49 @@ /** * Response to a request for copying a part */ -public class CopyPartResult extends HeaderResponse -{ +public class CopyPartResult extends HeaderResponse { private int partNumber; - + private String etag; - + private Date lastModified; - - - public CopyPartResult(int partNumber, String etag, Date lastModified) { - this.partNumber = partNumber; - this.etag = etag; - this.lastModified = ServiceUtils.cloneDateIgnoreNull(lastModified); - } + public CopyPartResult(int partNumber, String etag, Date lastModified) { + this.partNumber = partNumber; + this.etag = etag; + this.lastModified = ServiceUtils.cloneDateIgnoreNull(lastModified); + } - /** + /** * Obtain the part number of the to-be-copied part. * * @return Part number */ - public int getPartNumber() - { + public int getPartNumber() { return partNumber; } - - - /** + + /** * Obtain the ETag of the to-be-copied part. * * @return ETag of the to-be-copied part */ - public String getEtag() - { + public String getEtag() { return etag; } - /** * Obtain the last modification time of the to-be-copied part. * * @return Last modification time of the to-be-copied part */ - public Date getLastModified() - { + public Date getLastModified() { return ServiceUtils.cloneDateIgnoreNull(this.lastModified); } - @Override - public String toString() - { + public String toString() { return "CopyPartResult [partNumber=" + partNumber + ", etag=" + etag + ", lastModified=" + lastModified + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/CreateBucketRequest.java b/app/src/main/java/com/obs/services/model/CreateBucketRequest.java index ae0fdeb..bca3702 100644 --- a/app/src/main/java/com/obs/services/model/CreateBucketRequest.java +++ b/app/src/main/java/com/obs/services/model/CreateBucketRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; import java.util.HashMap; @@ -21,253 +22,251 @@ import com.obs.services.internal.utils.ServiceUtils; /** - * Parameters in a bucket creation request + * Parameters in a bucket creation request * */ -public class CreateBucketRequest { - - private String bucketName; - - private String location; - - private String epid; - - private StorageClassEnum storageClass; - - private AccessControlList acl; - - private Map> extensionPermissionMap; - - private AvailableZoneEnum availableZone; - - private Map extensionHeaderMap; - - private BucketTypeEnum bucketType = BucketTypeEnum.OBJECT; - - public CreateBucketRequest() { - - } - - /** - * Constructor - * @param bucketName Bucket name - */ - public CreateBucketRequest(String bucketName) { - this.bucketName = bucketName; - } - - /** - * Constructor - * @param bucketName Bucket name - * @param location Bucket location - */ - public CreateBucketRequest(String bucketName, String location) { - this.bucketName = bucketName; - this.location = location; - } - - /** - * Grant the OBS extension permission to users. - * @param domainId ID of the domain to which the user belongs - * @param extensionPermissionEnum OBS extension permission - */ - public void grantExtensionPermission(String domainId, ExtensionBucketPermissionEnum extensionPermissionEnum) { - if(extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { - return; - } - Set users = getExtensionPermissionMap().get(extensionPermissionEnum); - if(users == null) { - users = new HashSet(); - getExtensionPermissionMap().put(extensionPermissionEnum, users); - } - users.add(domainId.trim()); - } - - /** - * Withdraw the OBS extension permission. - * @param domainId ID of the domain to which the user belongs - * @param extensionPermissionEnum OBS extension permission - */ - public void withdrawExtensionPermission(String domainId, ExtensionBucketPermissionEnum extensionPermissionEnum) { - if(extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { - return; - } - domainId = domainId.trim(); - Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); - if(domainIds != null && domainIds.contains(domainId)) { - domainIds.remove(domainId); - } - } - - /** - * Withdraw all OBS extension permissions. - * @param domainId ID of the domain to which the user belongs - */ - public void withdrawExtensionPermissions(String domainId) { - if(ServiceUtils.isValid(domainId)) { - domainId = domainId.trim(); - for(Map.Entry> entry : this.getExtensionPermissionMap().entrySet()) { - if(entry.getValue().contains(domainId)) { - entry.getValue().remove(domainId); - } - } - } - } - - public Set getAllGrantPermissions(){ - return this.getExtensionPermissionMap().keySet(); - } - - - public Set getDomainIdsByGrantPermission(ExtensionBucketPermissionEnum extensionPermissionEnum) { - Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); - if(domainIds == null) { - domainIds = new HashSet(); - } - return domainIds; - } - - public Set getGrantPermissionsByDomainId(String domainId) { - Set grantPermissions = new HashSet(); - if(ServiceUtils.isValid(domainId)) { - for(Map.Entry> entry : this.getExtensionPermissionMap().entrySet()) { - if(entry.getValue().contains(domainId.trim())) { - grantPermissions.add(entry.getKey()); - } - } - } - return grantPermissions; - } - - +public class CreateBucketRequest extends GenericRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private String location; + + private String epid; + + private StorageClassEnum storageClass; + + private AccessControlList acl; + + private Map> extensionPermissionMap; + + private AvailableZoneEnum availableZone; + + private Map extensionHeaderMap; + + private BucketTypeEnum bucketType = BucketTypeEnum.OBJECT; + + public CreateBucketRequest() { + + } + /** - * Obtain the bucket name. + * Constructor * - * @return Bucket name + * @param bucketName + * Bucket name */ - public String getBucketName() - { - return bucketName; + public CreateBucketRequest(String bucketName) { + this.bucketName = bucketName; } - + /** - * Set the bucket name. - * The value can contain only lowercase letters, digits, hyphens (-), and periods (.). - * @param bucketName Bucket name + * Constructor + * + * @param bucketName + * Bucket name + * @param location + * Bucket location */ - public void setBucketName(String bucketName) - { + public CreateBucketRequest(String bucketName, String location) { this.bucketName = bucketName; + this.location = location; + } + + /** + * Grant the OBS extension permission to users. + * + * @param domainId + * ID of the domain to which the user belongs + * @param extensionPermissionEnum + * OBS extension permission + */ + public void grantExtensionPermission(String domainId, ExtensionBucketPermissionEnum extensionPermissionEnum) { + if (extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { + return; + } + Set users = getExtensionPermissionMap().computeIfAbsent(extensionPermissionEnum, k -> new HashSet<>()); + users.add(domainId.trim()); + } + + /** + * Withdraw the OBS extension permission. + * + * @param domainId + * ID of the domain to which the user belongs + * @param extensionPermissionEnum + * OBS extension permission + */ + public void withdrawExtensionPermission(String domainId, ExtensionBucketPermissionEnum extensionPermissionEnum) { + if (extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { + return; + } + domainId = domainId.trim(); + Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); + if (domainIds != null) { + domainIds.remove(domainId); + } + } + + /** + * Withdraw all OBS extension permissions. + * + * @param domainId + * ID of the domain to which the user belongs + */ + public void withdrawExtensionPermissions(String domainId) { + if (ServiceUtils.isValid(domainId)) { + domainId = domainId.trim(); + for (Map.Entry> entry : this.getExtensionPermissionMap() + .entrySet()) { + entry.getValue().remove(domainId); + } + } + } + + public Set getAllGrantPermissions() { + return this.getExtensionPermissionMap().keySet(); + } + + public Set getDomainIdsByGrantPermission(ExtensionBucketPermissionEnum extensionPermissionEnum) { + Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); + if (domainIds == null) { + domainIds = new HashSet<>(); + } + return domainIds; + } + + public Set getGrantPermissionsByDomainId(String domainId) { + Set grantPermissions = new HashSet(); + if (ServiceUtils.isValid(domainId)) { + for (Map.Entry> entry : this.getExtensionPermissionMap() + .entrySet()) { + if (entry.getValue().contains(domainId.trim())) { + grantPermissions.add(entry.getKey()); + } + } + } + return grantPermissions; } - + /** * Obtain the bucket location. + * * @return Bucket location */ - public String getLocation() - { + public String getLocation() { return location; } - + /** * Set the bucket location. - * @param location Bucket location. This parameter is mandatory unless the endpoint belongs to the default region. + * + * @param location + * Bucket location. This parameter is mandatory unless the + * endpoint belongs to the default region. */ - public void setLocation(String location) - { + public void setLocation(String location) { this.location = location; } - + /** * Obtain the enterprise ID of a bucket. + * * @return Enterprise ID of the bucket */ - public String getEpid() - { + public String getEpid() { return epid; } - + /** * Set the enterprise ID of a bucket. - * @param epid Enterprise ID + * + * @param epid + * Enterprise ID */ - public void setEpid(String epid) - { + public void setEpid(String epid) { this.epid = epid; } - - public AccessControlList getAcl() - { + + public AccessControlList getAcl() { return acl; } - + /** * Set the bucket ACL. - * @param acl Bucket ACL + * + * @param acl + * Bucket ACL */ - public void setAcl(AccessControlList acl) - { + public void setAcl(AccessControlList acl) { this.acl = acl; } - + /** - * Obtain the bucket storage class. + * Obtain the bucket storage class. + * * @return Bucket storage class */ - public StorageClassEnum getBucketStorageClass() - { + public StorageClassEnum getBucketStorageClass() { return storageClass; } /** - * Set the bucket storage class. - * @param storageClass Bucket storage class + * Set the bucket storage class. + * + * @param storageClass + * Bucket storage class */ - public void setBucketStorageClass(StorageClassEnum storageClass) - { + public void setBucketStorageClass(StorageClassEnum storageClass) { this.storageClass = storageClass; } - + /** * Obtain the bucket cluster type. + * * @return Bucket cluster type */ - public AvailableZoneEnum getAvailableZone() { - return availableZone; - } - - /** - * Set bucket cluster type. - * @param availableZone Bucket cluster type - */ - public void setAvailableZone(AvailableZoneEnum availableZone) { - this.availableZone = availableZone; - } - - Map> getExtensionPermissionMap() { - if(extensionPermissionMap == null) { - extensionPermissionMap = new HashMap>(); - } - return extensionPermissionMap; - } - - public Map getExtensionHeaderMap() { - return extensionHeaderMap; + public AvailableZoneEnum getAvailableZone() { + return availableZone; + } + + /** + * Set bucket cluster type. + * + * @param availableZone + * Bucket cluster type + */ + public void setAvailableZone(AvailableZoneEnum availableZone) { + this.availableZone = availableZone; + } + + Map> getExtensionPermissionMap() { + if (extensionPermissionMap == null) { + extensionPermissionMap = new HashMap<>(); + } + return extensionPermissionMap; } - @Override - public String toString() { - return "CreateBucketRequest [bucketName=" + bucketName + ", location=" + location + ", storageClass=" - + storageClass + ", acl=" + acl + ", extensionPermissionMap=" + extensionPermissionMap - + ", availableZone=" + availableZone + ",epid=" + epid + "]"; - } + public Map getExtensionHeaderMap() { + if (extensionHeaderMap == null) { + extensionHeaderMap = new HashMap<>(); + } + return extensionHeaderMap; + } - public BucketTypeEnum getBucketType() { - return bucketType; - } + @Override + public String toString() { + return "CreateBucketRequest [bucketName=" + bucketName + ", location=" + location + ", storageClass=" + + storageClass + ", acl=" + acl + ", extensionPermissionMap=" + extensionPermissionMap + + ", availableZone=" + availableZone + ",epid=" + epid + "]"; + } + public BucketTypeEnum getBucketType() { + return bucketType; + } - public void setBucketType(BucketTypeEnum bucketType) { - this.bucketType = bucketType; - } + public void setBucketType(BucketTypeEnum bucketType) { + this.bucketType = bucketType; + } } diff --git a/app/src/main/java/com/obs/services/model/DeleteBucketCustomDomainRequest.java b/app/src/main/java/com/obs/services/model/DeleteBucketCustomDomainRequest.java new file mode 100644 index 0000000..beb4828 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/DeleteBucketCustomDomainRequest.java @@ -0,0 +1,46 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.model; + +public class DeleteBucketCustomDomainRequest extends BaseBucketRequest { + private String domainName; + + { + httpMethod = HttpMethodEnum.DELETE; + } + + public DeleteBucketCustomDomainRequest() { + } + + public DeleteBucketCustomDomainRequest(String bucketName, String domainName) { + this.bucketName = bucketName; + this.domainName = domainName; + } + + public String getDomainName() { + return domainName; + } + + public void setDomainName(String domainName) { + this.domainName = domainName; + } + + @Override + public String toString() { + return "DeleteBucketCustomDomainRequest [domainName=" + domainName + + ", bucketName=" + getBucketName() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/DeleteObjectRequest.java b/app/src/main/java/com/obs/services/model/DeleteObjectRequest.java new file mode 100644 index 0000000..cc3c7ea --- /dev/null +++ b/app/src/main/java/com/obs/services/model/DeleteObjectRequest.java @@ -0,0 +1,91 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Request parameters for deleting an object. + * + * @since 3.20.3 + */ +public class DeleteObjectRequest extends BaseObjectRequest { + + { + httpMethod = HttpMethodEnum.DELETE; + } + + private String versionId; + + public DeleteObjectRequest() { + } + + public DeleteObjectRequest(String bucketName) { + this.bucketName = bucketName; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + */ + public DeleteObjectRequest(String bucketName, String objectKey) { + this.bucketName = bucketName; + this.objectKey = objectKey; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param versionId + * Object version ID + */ + public DeleteObjectRequest(String bucketName, String objectKey, String versionId) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.versionId = versionId; + } + + /** + * Obtain the version ID of the object to be deleted. + * + * @return Version ID of the object to be deleted + */ + public String getVersionId() { + return versionId; + } + + /** + * Set the version ID of the object to be deleted. + + * @param versionId + * Version ID of the object to be deleted + */ + public void setVersionId(String versionId) { + this.versionId = versionId; + } + + @Override + public String toString() { + return "AbortMultipartUploadRequest [bucketName=" + bucketName + ", objectKey=" + + objectKey + ", versionId=" + versionId + "]"; + } + +} diff --git a/app/src/main/java/com/obs/services/model/DeleteObjectResult.java b/app/src/main/java/com/obs/services/model/DeleteObjectResult.java index aac9dca..13ed009 100644 --- a/app/src/main/java/com/obs/services/model/DeleteObjectResult.java +++ b/app/src/main/java/com/obs/services/model/DeleteObjectResult.java @@ -11,61 +11,61 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; /** * Response to an object deletion request */ -public class DeleteObjectResult extends HeaderResponse -{ +public class DeleteObjectResult extends HeaderResponse { private boolean deleteMarker; - + private String objectKey; - + private String versionId; - - + public DeleteObjectResult(boolean deleteMarker, String versionId) { - this.deleteMarker = deleteMarker; - this.versionId = versionId; - } - - - public DeleteObjectResult(boolean deleteMarker, String objectKey, String versionId) { this.deleteMarker = deleteMarker; - this.objectKey = objectKey; this.versionId = versionId; } + public DeleteObjectResult(boolean deleteMarker, String objectKey, String versionId) { + this.deleteMarker = deleteMarker; + this.objectKey = objectKey; + this.versionId = versionId; + } /** * Check whether a versioning object has been deleted. - * @return Identifier indicating whether the versioning object has been deleted + * + * @return Identifier indicating whether the versioning object has been + * deleted */ - public boolean isDeleteMarker() { - return deleteMarker; - } + public boolean isDeleteMarker() { + return deleteMarker; + } - /** - * Obtain the version ID of the deleted object. - * @return Version ID of the object - */ - public String getVersionId() { - return versionId; - } - - /** - * Obtain the name of the deleted object. - * @return Object name - */ - public String getObjectKey() { - return objectKey; + /** + * Obtain the version ID of the deleted object. + * + * @return Version ID of the object + */ + public String getVersionId() { + return versionId; } + /** + * Obtain the name of the deleted object. + * + * @return Object name + */ + public String getObjectKey() { + return objectKey; + } @Override public String toString() { return "DeleteObjectResult [deleteMarker=" + deleteMarker + ", objectKey=" + objectKey + ", versionId=" + versionId + "]"; - } + } } diff --git a/app/src/main/java/com/obs/services/model/DeleteObjectsRequest.java b/app/src/main/java/com/obs/services/model/DeleteObjectsRequest.java index 397abfe..de98bbd 100644 --- a/app/src/main/java/com/obs/services/model/DeleteObjectsRequest.java +++ b/app/src/main/java/com/obs/services/model/DeleteObjectsRequest.java @@ -21,145 +21,174 @@ /** * Parameters in an object batch deletion request */ -public class DeleteObjectsRequest -{ - private String bucketName; - +public class DeleteObjectsRequest extends GenericRequest { + + { + httpMethod = HttpMethodEnum.POST; + } + private boolean quiet; - + private List keyAndVersions; - - public DeleteObjectsRequest(){ - + + private String encodingType; + + public DeleteObjectsRequest() { } - + /** * Constructor - * @param bucketName Bucket name + * + * @param bucketName + * Bucket name */ - public DeleteObjectsRequest(String bucketName) - { + public DeleteObjectsRequest(String bucketName) { this.bucketName = bucketName; } - /** * Constructor - * @param bucketName Bucket name - * @param quiet Deletion response mode. "false" indicates that the "verbose" mode is used and "true" indicates that the "quiet" mode is used. - * @param keyAndVersions To-be-deleted object array + * + * @param bucketName + * Bucket name + * @param quiet + * Deletion response mode. "false" indicates that the "verbose" + * mode is used and "true" indicates that the "quiet" mode is + * used. + * @param keyAndVersions + * To-be-deleted object array */ - public DeleteObjectsRequest(String bucketName, boolean quiet, KeyAndVersion[] keyAndVersions) - { + public DeleteObjectsRequest(String bucketName, boolean quiet, KeyAndVersion[] keyAndVersions) { this.bucketName = bucketName; this.quiet = quiet; this.setKeyAndVersions(keyAndVersions); } - - /** - * Obtain the bucket name. - * - * @return Bucket name - */ - public String getBucketName() - { - return bucketName; - } - - /** - * Set the bucket name. + * Constructor * - * @param bucketName Bucket name + * @param bucketName + * Bucket name + * @param quiet + * Deletion response mode. "false" indicates that the "verbose" + * mode is used and "true" indicates that the "quiet" mode is + * used. + * @param keyAndVersions + * To-be-deleted object array + * @param encodingType + * The encoding type use for encode objectKey. */ - public void setBucketName(String bucketName) - { + public DeleteObjectsRequest(String bucketName, boolean quiet, KeyAndVersion[] keyAndVersions, String encodingType) { this.bucketName = bucketName; + this.quiet = quiet; + this.setKeyAndVersions(keyAndVersions); + this.setEncodingType(encodingType); } - + /** - * Obtain the response mode of the batch deletion. "false" indicates that the "verbose" mode is used and "true" indicates that the "quiet" mode is used. + * Obtain the response mode of the batch deletion. "false" indicates that + * the "verbose" mode is used and "true" indicates that the "quiet" mode is + * used. * * @return Response mode of the object batch deletion request */ - public boolean isQuiet() - { + public boolean isQuiet() { return quiet; } - + /** - * Set the response mode for the batch deletion. "false" indicates that the "verbose" mode is used and "true" indicates that the "quiet" mode is used. + * Set the response mode for the batch deletion. "false" indicates that the + * "verbose" mode is used and "true" indicates that the "quiet" mode is + * used. * - * @param quiet Response mode of the object batch deletion request + * @param quiet + * Response mode of the object batch deletion request */ - public void setQuiet(boolean quiet) - { + public void setQuiet(boolean quiet) { this.quiet = quiet; } - + + /** + * Set the encoding type that used for encode objectkey + * + * @param encodingType + * could chose url. + */ + public void setEncodingType(String encodingType) { + this.encodingType = encodingType; + } + + /** + * Get the encoding type that used for encode objectkey + * @return encodingType + */ + public String getEncodingType() { + return encodingType; + } + /** * Obtain the list of to-be-deleted objects. + * * @return List of to-be-deleted objects */ - public List getKeyAndVersionsList(){ - if(this.keyAndVersions == null) { - this.keyAndVersions = new ArrayList(); - } - return this.keyAndVersions; + public List getKeyAndVersionsList() { + if (this.keyAndVersions == null) { + this.keyAndVersions = new ArrayList<>(); + } + return this.keyAndVersions; } - + /** - * Add an object to be deleted. - * @param objectKey Object name - * @param versionId Version ID of the object - * @return Object newly added to the deletion list + * Add an object to be deleted. + * + * @param objectKey + * Object name + * @param versionId + * Version ID of the object + * @return Object newly added to the deletion list */ - public KeyAndVersion addKeyAndVersion(String objectKey, String versionId) - { - KeyAndVersion kv = new KeyAndVersion(objectKey, versionId); - this.getKeyAndVersionsList().add(kv); + public KeyAndVersion addKeyAndVersion(String objectKey, String versionId) { + KeyAndVersion kv = new KeyAndVersion(objectKey, versionId); + this.getKeyAndVersionsList().add(kv); return kv; } - + /** - * Add an object to be deleted. - * @param objectKey Object name - * @return Object newly added to the deletion list + * Add an object to be deleted. + * + * @param objectKey + * Object name + * @return Object newly added to the deletion list */ - public KeyAndVersion addKeyAndVersion(String objectKey) - { - return this.addKeyAndVersion(objectKey, null); + public KeyAndVersion addKeyAndVersion(String objectKey) { + return this.addKeyAndVersion(objectKey, null); } - + /** * Obtain the to-be-deleted object array. + * * @return To-be-deleted object array */ - public KeyAndVersion[] getKeyAndVersions() - { + public KeyAndVersion[] getKeyAndVersions() { return this.getKeyAndVersionsList().toArray(new KeyAndVersion[this.getKeyAndVersionsList().size()]); } - + /** * Specify the to-be-deleted object array. - * @param keyAndVersions To-be-deleted object array + * + * @param keyAndVersions + * To-be-deleted object array */ - public void setKeyAndVersions(KeyAndVersion[] keyAndVersions) - { - if(keyAndVersions != null && keyAndVersions.length > 0) { - this.keyAndVersions = new ArrayList(Arrays.asList(keyAndVersions)); - } + public void setKeyAndVersions(KeyAndVersion[] keyAndVersions) { + if (keyAndVersions != null && keyAndVersions.length > 0) { + this.keyAndVersions = new ArrayList<>(Arrays.asList(keyAndVersions)); + } } @Override - public String toString() - { - return "DeleteObjectsRequest [bucketName=" + bucketName + ", quiet=" + quiet + ", keyAndVersions=" + this.keyAndVersions - + "]"; + public String toString() { + return "DeleteObjectsRequest [bucketName=" + bucketName + ", quiet=" + quiet + ", encodingType=" + encodingType + + ", keyAndVersions=" + this.keyAndVersions + "]"; } - - -} - +} diff --git a/app/src/main/java/com/obs/services/model/DeleteObjectsResult.java b/app/src/main/java/com/obs/services/model/DeleteObjectsResult.java index de97655..97833a2 100644 --- a/app/src/main/java/com/obs/services/model/DeleteObjectsResult.java +++ b/app/src/main/java/com/obs/services/model/DeleteObjectsResult.java @@ -20,199 +20,195 @@ /** * Response to an object batch deletion request */ -public class DeleteObjectsResult extends HeaderResponse -{ +public class DeleteObjectsResult extends HeaderResponse { private List deletedObjectResults; - + private List errorResults; - + public DeleteObjectsResult() { - + } - + public DeleteObjectsResult(List deletedObjectResults, List errorResults) { - this.deletedObjectResults = deletedObjectResults; - this.errorResults = errorResults; - } + this.deletedObjectResults = deletedObjectResults; + this.errorResults = errorResults; + } - /** + /** * Obtain the list of objects that have been deleted successfully. * * @return List of successfully deleted objects */ - public List getDeletedObjectResults() - { - if(this.deletedObjectResults == null) { - this.deletedObjectResults = new ArrayList(); - } + public List getDeletedObjectResults() { + if (this.deletedObjectResults == null) { + this.deletedObjectResults = new ArrayList(); + } return deletedObjectResults; } - + /** * Obtain the list of objects failed to be deleted. * * @return List of objects failed to be deleted */ - public List getErrorResults() - { - if(this.errorResults == null) { - this.errorResults = new ArrayList(); - } + public List getErrorResults() { + if (this.errorResults == null) { + this.errorResults = new ArrayList(); + } return errorResults; } - + /** * Results returned if the deletion succeeds */ - public class DeleteObjectResult - { + public static class DeleteObjectResult { private String objectKey; - + private String version; - + private boolean deleteMarker; - + private String deleteMarkerVersion; - - + public DeleteObjectResult(String objectKey, String version, boolean deleteMarker, String deleteMarkerVersion) { - super(); - this.objectKey = objectKey; - this.version = version; - this.deleteMarker = deleteMarker; - this.deleteMarkerVersion = deleteMarkerVersion; - } - - /** + super(); + this.objectKey = objectKey; + this.version = version; + this.deleteMarker = deleteMarker; + this.deleteMarkerVersion = deleteMarkerVersion; + } + + /** * Obtain the object name. * * @return Object name */ - public String getObjectKey() - { + public String getObjectKey() { return objectKey; } - + /** * Obtain the object version ID. * * @return Version ID of the object */ - public String getVersion() - { + public String getVersion() { return version; } - + /** * Check whether the deleted object is a delete marker + * * @return Identifier specifying whether the object is a delete marker */ - public boolean isDeleteMarker() { - return deleteMarker; - } - - /** - * Obtain the version ID of the delete marker. - * @return Version ID of the delete marker - */ - public String getDeleteMarkerVersion() { - return deleteMarkerVersion; - } - - - @Override - public String toString() { - return "DeleteObjectResult [objectKey=" + objectKey + ", version=" + version + ", deleteMarker=" - + deleteMarker + ", deleteMarkerVersion=" + deleteMarkerVersion + "]"; - } - + public boolean isDeleteMarker() { + return deleteMarker; + } + + /** + * Obtain the version ID of the delete marker. + * + * @return Version ID of the delete marker + */ + public String getDeleteMarkerVersion() { + return deleteMarkerVersion; + } + + public void setObjectKey(String objectKey) { + this.objectKey = objectKey; + } + + @Override + public String toString() { + return "DeleteObjectResult [objectKey=" + objectKey + ", version=" + version + ", deleteMarker=" + + deleteMarker + ", deleteMarkerVersion=" + deleteMarkerVersion + "]"; + } + } - + /** * Results returned if the deletion fails */ - public class ErrorResult - { + public static class ErrorResult { private String objectKey; - + private String version; - + private String errorCode; - + private String message; - + /** * Constructor * - * @param objectKey Name of the object that fails to be deleted - * @param version Version ID of the object that fails to be deleted - * @param errorCode Error code returned after a deletion failure - * @param message Error information returned after a deletion failure + * @param objectKey + * Name of the object that fails to be deleted + * @param version + * Version ID of the object that fails to be deleted + * @param errorCode + * Error code returned after a deletion failure + * @param message + * Error information returned after a deletion failure */ - public ErrorResult(String objectKey, String version, String errorCode, String message) - { + public ErrorResult(String objectKey, String version, String errorCode, String message) { this.objectKey = objectKey; this.version = version; this.errorCode = errorCode; this.message = message; } - + /** * Obtain the name of the object that fails to be deleted. * * @return Name of the object that fails to be deleted */ - public String getObjectKey() - { + public String getObjectKey() { return objectKey; } - + /** * Obtain the version ID of the object that fails to be deleted. * * @return Version ID of the object that fails to be deleted */ - public String getVersion() - { + public String getVersion() { return version; } - + /** * Error code returned after a deletion failure * * @return Error code returned after a deletion failure */ - public String getErrorCode() - { + public String getErrorCode() { return errorCode; } - + /** * Obtain the error description returned after a deletion failure. * * @return Error information returned after a deletion failure */ - public String getMessage() - { + public String getMessage() { return message; } + public void setObjectKey(String objectKey) { + this.objectKey = objectKey; + } + @Override - public String toString() - { - return "ErrorResult [objectKey=" + objectKey + ", version=" + version + ", errorCode=" + errorCode + ", message=" + message - + "]"; + public String toString() { + return "ErrorResult [objectKey=" + objectKey + ", version=" + version + ", errorCode=" + errorCode + + ", message=" + message + "]"; } - + } @Override - public String toString() - { - return "DeleteObjectsResult [deletedObjectResults=" + deletedObjectResults + ", errorResults=" + errorResults + "]"; + public String toString() { + return "DeleteObjectsResult [deletedObjectResults=" + deletedObjectResults + ", errorResults=" + errorResults + + "]"; } - - -} - +} diff --git a/app/src/main/java/com/obs/services/model/DownloadFileRequest.java b/app/src/main/java/com/obs/services/model/DownloadFileRequest.java index f69fa73..d0e1268 100644 --- a/app/src/main/java/com/obs/services/model/DownloadFileRequest.java +++ b/app/src/main/java/com/obs/services/model/DownloadFileRequest.java @@ -1,16 +1,17 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + package com.obs.services.model; import java.util.Date; @@ -21,15 +22,10 @@ /** * Parameters in a request for downloading a file */ -public class DownloadFileRequest { - - private String bucketName; - - private String objectKey; - +public class DownloadFileRequest extends BaseObjectRequest { private String downloadFile; - private long partSize = 1024 * 1024 * 9l; + private long partSize = 1024 * 1024 * 9L; private int taskNum = 1; @@ -46,120 +42,163 @@ public class DownloadFileRequest { private String ifNoneMatchTag; private String versionId; - + private ProgressListener progressListener; - + private long progressInterval = ObsConstraint.DEFAULT_PROGRESS_INTERVAL; - + private CacheOptionEnum cacheOption; - + private long ttl; - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - */ - public DownloadFileRequest(String bucketName, String objectKey) { - this.bucketName = bucketName; - this.objectKey = objectKey; - this.downloadFile = objectKey; - } - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - * @param downloadFile Path to the to-be-downloaded file - */ - public DownloadFileRequest(String bucketName, String objectKey, String downloadFile) { - this(bucketName, objectKey); - this.downloadFile = downloadFile; - } - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - * @param downloadFile Path to the to-be-downloaded file - * @param partSize Part size - */ - public DownloadFileRequest(String bucketName, String objectKey, String downloadFile, long partSize) { - this(bucketName, objectKey); - this.downloadFile = downloadFile; - this.partSize = partSize; - } - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - * @param downloadFile Path to the to-be-downloaded file - * @param partSize Part size - * @param taskNum Maximum number of threads used for processing download tasks concurrently - */ - public DownloadFileRequest(String bucketName, String objectKey, String downloadFile, long partSize, int taskNum) { - this(bucketName, objectKey, downloadFile, partSize, taskNum, false); - } - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - * @param downloadFile Path to the to-be-downloaded file - * @param partSize Part size - * @param taskNum Maximum number of threads used for processing download tasks concurrently - * @param enableCheckpoint Whether to enable the resumable mode - * - */ - public DownloadFileRequest(String bucketName, String objectKey, String downloadFile, long partSize, int taskNum, - boolean enableCheckpoint) { - this(bucketName, objectKey, downloadFile, partSize, taskNum, enableCheckpoint, null); - } - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - * @param downloadFile Path to the to-be-downloaded file - * @param partSize Part size - * @param taskNum Maximum number of threads used for processing download tasks concurrently - * @param enableCheckpoint Whether to enable the resumable mode - * @param checkpointFile File used to record download progresses in resumable mode - * - */ - public DownloadFileRequest(String bucketName, String objectKey, String downloadFile, long partSize, int taskNum, - boolean enableCheckpoint, String checkpointFile) { - this(bucketName, objectKey); - this.partSize = partSize; - this.taskNum = taskNum; - this.downloadFile = downloadFile; - this.enableCheckpoint = enableCheckpoint; - this.checkpointFile = checkpointFile; - } - - /** + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + */ + public DownloadFileRequest(String bucketName, String objectKey) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.downloadFile = objectKey; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param downloadFile + * Path to the to-be-downloaded file + */ + public DownloadFileRequest(String bucketName, String objectKey, String downloadFile) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.downloadFile = downloadFile; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param downloadFile + * Path to the to-be-downloaded file + * @param partSize + * Part size + */ + public DownloadFileRequest(String bucketName, String objectKey, String downloadFile, long partSize) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.downloadFile = downloadFile; + this.partSize = partSize; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param downloadFile + * Path to the to-be-downloaded file + * @param partSize + * Part size + * @param taskNum + * Maximum number of threads used for processing download tasks + * concurrently + */ + public DownloadFileRequest(String bucketName, String objectKey, String downloadFile, long partSize, int taskNum) { + this(bucketName, objectKey, downloadFile, partSize, taskNum, false); + } + + /** * Constructor * - * @param bucketName Bucket name - * @param objectKey Object name - * @param downloadFile Path to the to-be-downloaded file - * @param partSize Part size - * @param taskNum Maximum number of threads used for processing download tasks concurrently - * @param enableCheckpoint Whether to enable the resumable mode - * @param checkpointFile File used to record download progresses in resumable mode - * @param versionId Version ID of the object + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param downloadFile + * Path to the to-be-downloaded file + * @param partSize + * Part size + * @param taskNum + * Maximum number of threads used for processing download tasks + * concurrently + * @param enableCheckpoint + * Whether to enable the resumable mode + * + */ + public DownloadFileRequest(String bucketName, String objectKey, String downloadFile, long partSize, int taskNum, + boolean enableCheckpoint) { + this(bucketName, objectKey, downloadFile, partSize, taskNum, enableCheckpoint, null); + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param downloadFile + * Path to the to-be-downloaded file + * @param partSize + * Part size + * @param taskNum + * Maximum number of threads used for processing download tasks + * concurrently + * @param enableCheckpoint + * Whether to enable the resumable mode + * @param checkpointFile + * File used to record download progresses in resumable mode + * + */ + public DownloadFileRequest(String bucketName, String objectKey, String downloadFile, long partSize, int taskNum, + boolean enableCheckpoint, String checkpointFile) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.partSize = partSize; + this.taskNum = taskNum; + this.downloadFile = downloadFile; + this.enableCheckpoint = enableCheckpoint; + this.checkpointFile = checkpointFile; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param downloadFile + * Path to the to-be-downloaded file + * @param partSize + * Part size + * @param taskNum + * Maximum number of threads used for processing download tasks + * concurrently + * @param enableCheckpoint + * Whether to enable the resumable mode + * @param checkpointFile + * File used to record download progresses in resumable mode + * @param versionId + * Version ID of the object * */ public DownloadFileRequest(String bucketName, String objectKey, String downloadFile, long partSize, int taskNum, boolean enableCheckpoint, String checkpointFile, String versionId) { - this(bucketName, objectKey); + this.bucketName = bucketName; + this.objectKey = objectKey; this.partSize = partSize; this.taskNum = taskNum; this.downloadFile = downloadFile; @@ -168,101 +207,70 @@ public DownloadFileRequest(String bucketName, String objectKey, String downloadF this.versionId = versionId; } - /** - * Obtain the bucket name. - * - * @return Bucket name - */ - public String getBucketName() { - return bucketName; - } - - /** - * Set the bucket name. - * - * @param bucketName Bucket name - */ - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } - - /** - * Obtain the object name. - * - * @return Object name - */ - public String getObjectKey() { - return objectKey; - } - - /** - * Set the object name. - * - * @param objectKey Object name - */ - public void setObjectKey(String objectKey) { - this.objectKey = objectKey; - } - - /** - * Obtain the path to the to-be-downloaded file. - * - * @return Path to the to-be-downloaded file - */ - public String getDownloadFile() { - return downloadFile; - } - - /** - * Set the path to the to-be-downloaded file. - * - * @param downloadFile Path to the to-be-downloaded file - */ - public void setDownloadFile(String downloadFile) { - this.downloadFile = downloadFile; - } - - /** - * Obtain the part size. - * - * @return Part size - */ - public long getPartSize() { - return partSize; - } - - /** - * Set the part size. - * - * @param partSize Part size - */ - public void setPartSize(long partSize) { - this.partSize = partSize; - } - - /** - * Obtain the maximum number of threads used for processing download tasks concurrently. - * - * @return Maximum number of threads used for processing download tasks concurrently - */ - public int getTaskNum() { - return taskNum; - } - - /** - * Set the maximum number of threads used for processing download tasks concurrently. - * - * @param taskNum Maximum number of threads used for processing download tasks concurrently - */ - public void setTaskNum(int taskNum) { - if (taskNum < 1) { - this.taskNum = 1; - } else if (taskNum > 1000) { - this.taskNum = 1000; - } else { - this.taskNum = taskNum; - } - } + /** + * Obtain the path to the to-be-downloaded file. + * + * @return Path to the to-be-downloaded file + */ + public String getDownloadFile() { + return downloadFile; + } + + /** + * Set the path to the to-be-downloaded file. + * + * @param downloadFile + * Path to the to-be-downloaded file + */ + public void setDownloadFile(String downloadFile) { + this.downloadFile = downloadFile; + } + + /** + * Obtain the part size. + * + * @return Part size + */ + public long getPartSize() { + return partSize; + } + + /** + * Set the part size. + * + * @param partSize + * Part size + */ + public void setPartSize(long partSize) { + this.partSize = partSize; + } + + /** + * Obtain the maximum number of threads used for processing download tasks + * concurrently. + * + * @return Maximum number of threads used for processing download tasks + * concurrently + */ + public int getTaskNum() { + return taskNum; + } + + /** + * Set the maximum number of threads used for processing download tasks + * concurrently. + * + * @param taskNum + * Maximum number of threads used for processing download tasks + * concurrently + */ + public void setTaskNum(int taskNum) { + if (taskNum < 1) { + this.taskNum = 1; + } else { + this.taskNum = Math.min(taskNum, 1000); + } + } /** * Identify whether the resumable mode is enabled. @@ -276,12 +284,13 @@ public boolean isEnableCheckpoint() { /** * Specify whether to enable the resumable mode. * - * @param enableCheckpoint Identifier specifying whether the resumable mode is enabled + * @param enableCheckpoint + * Identifier specifying whether the resumable mode is enabled */ public void setEnableCheckpoint(boolean enableCheckpoint) { this.enableCheckpoint = enableCheckpoint; } - + /** * File used to record download progresses in resumable mode * @@ -292,101 +301,120 @@ public String getCheckpointFile() { } /** - * Specify a file used to record resumable download progresses. + * Specify a file used to record resumable download progresses. * - * @param checkpointFile File used to record the download progress + * @param checkpointFile + * File used to record the download progress */ public void setCheckpointFile(String checkpointFile) { this.checkpointFile = checkpointFile; } - /** - * Obtain the temporary file generated during the download. - * - * @return Temporary file generated during the download - */ - public String getTempDownloadFile() { - return downloadFile + ".tmp"; - } + /** + * Obtain the temporary file generated during the download. + * + * @return Temporary file generated during the download + */ + public String getTempDownloadFile() { + return downloadFile + ".tmp"; + } /** - * Obtain the time conditions set for downloading the object. Only when the object is modified after the point in time specified by this parameter, it will be downloaded. Otherwise, "304 Not Modified" will be returned. + * Obtain the time conditions set for downloading the object. Only when the + * object is modified after the point in time specified by this parameter, + * it will be downloaded. Otherwise, "304 Not Modified" will be returned. * * @return Time condition set for downloading the object */ - public Date getIfModifiedSince() - { - return ServiceUtils.cloneDateIgnoreNull(this.ifModifiedSince); + public Date getIfModifiedSince() { + return ServiceUtils.cloneDateIgnoreNull(this.ifModifiedSince); } - + /** - * Set the time conditions set for downloading the object. Only when the object is modified after the point in time specified by this parameter, it will be downloaded. Otherwise, "304 Not Modified" will be returned. + * Set the time conditions set for downloading the object. Only when the + * object is modified after the point in time specified by this parameter, + * it will be downloaded. Otherwise, "304 Not Modified" will be returned. * - * @param ifModifiedSince Time condition set for downloading the object + * @param ifModifiedSince + * Time condition set for downloading the object */ - public void setIfModifiedSince(Date ifModifiedSince) - { + public void setIfModifiedSince(Date ifModifiedSince) { this.ifModifiedSince = ServiceUtils.cloneDateIgnoreNull(ifModifiedSince); } - + /** - * Obtain the time conditions for downloading the object. Only when the object remains unchanged after the point in time specified by this parameter, it will be downloaded; otherwise, "412 Precondition Failed" will be returned. + * Obtain the time conditions for downloading the object. Only when the + * object remains unchanged after the point in time specified by this + * parameter, it will be downloaded; otherwise, "412 Precondition Failed" + * will be returned. * * @return Time condition set for downloading the object */ - public Date getIfUnmodifiedSince() - { + public Date getIfUnmodifiedSince() { return ServiceUtils.cloneDateIgnoreNull(this.ifUnmodifiedSince); } - + /** - * Set the time conditions for downloading the object. Only when the object remains unchanged after the point in time specified by this parameter, it will be downloaded; otherwise, "412 Precondition Failed" will be returned. + * Set the time conditions for downloading the object. Only when the object + * remains unchanged after the point in time specified by this parameter, it + * will be downloaded; otherwise, "412 Precondition Failed" will be + * returned. * - * @param ifUnmodifiedSince Time condition set for downloading the object + * @param ifUnmodifiedSince + * Time condition set for downloading the object */ - public void setIfUnmodifiedSince(Date ifUnmodifiedSince) - { + public void setIfUnmodifiedSince(Date ifUnmodifiedSince) { this.ifUnmodifiedSince = ServiceUtils.cloneDateIgnoreNull(ifUnmodifiedSince); } - + /** - * Obtain the ETag verification conditions for downloading the object. Only when the ETag of the object is the same as that specified by this parameter, the object will be downloaded. Otherwise, "412 Precondition Failed" will be returned. + * Obtain the ETag verification conditions for downloading the object. Only + * when the ETag of the object is the same as that specified by this + * parameter, the object will be downloaded. Otherwise, + * "412 Precondition Failed" will be returned. * * @return ETag verification condition set for downloading the object */ - public String getIfMatchTag() - { + public String getIfMatchTag() { return ifMatchTag; } - + /** - * Set the ETag verification conditions for downloading the object. Only when the ETag of the object is the same as that specified by this parameter, the object will be downloaded. Otherwise, "412 Precondition Failed" will be returned. + * Set the ETag verification conditions for downloading the object. Only + * when the ETag of the object is the same as that specified by this + * parameter, the object will be downloaded. Otherwise, + * "412 Precondition Failed" will be returned. * - * @param ifMatchTag ETag verification condition set for downloading the object + * @param ifMatchTag + * ETag verification condition set for downloading the object */ - public void setIfMatchTag(String ifMatchTag) - { + public void setIfMatchTag(String ifMatchTag) { this.ifMatchTag = ifMatchTag; } - + /** - * Obtain the ETag verification conditions for downloading the object. Only when the ETag of the object is different from that specified by this parameter, the object will be downloaded. Otherwise, "304 Not Modified" will be returned. + * Obtain the ETag verification conditions for downloading the object. Only + * when the ETag of the object is different from that specified by this + * parameter, the object will be downloaded. Otherwise, "304 Not Modified" + * will be returned. * * @return ETag verification condition set for downloading the object */ - public String getIfNoneMatchTag() - { + public String getIfNoneMatchTag() { return ifNoneMatchTag; } - + /** - * Set the ETag verification conditions for downloading the object. Only when the ETag of the object is different from that specified by this parameter, the object will be downloaded. Otherwise, "304 Not Modified" will be returned. + * Set the ETag verification conditions for downloading the object. Only + * when the ETag of the object is different from that specified by this + * parameter, the object will be downloaded. Otherwise, "304 Not Modified" + * will be returned. * - * @param ifNoneMatchTag ETag verification condition set for downloading the object + * @param ifNoneMatchTag + * ETag verification condition set for downloading the object * */ - public void setIfNoneMatchTag(String ifNoneMatchTag) - { + public void setIfNoneMatchTag(String ifNoneMatchTag) { this.ifNoneMatchTag = ifNoneMatchTag; } @@ -395,95 +423,109 @@ public void setIfNoneMatchTag(String ifNoneMatchTag) * * @return Version ID of the object */ - public String getVersionId() - { + public String getVersionId() { return versionId; } - + /** - * Set the version ID of the object. + * Set the version ID of the object. * - * @param versionId Version ID of the object + * @param versionId + * Version ID of the object * */ - public void setVersionId(String versionId) - { + public void setVersionId(String versionId) { this.versionId = versionId; } - - /** - * Obtain the data transfer listener. - * @return Data transfer listener - */ - public ProgressListener getProgressListener() { - return progressListener; - } - - /** - * Set the data transfer listener. - * @param progressListener Data transfer listener - */ - public void setProgressListener(ProgressListener progressListener) { - this.progressListener = progressListener; - } - - /** - * Obtain the callback threshold of the data transfer listener. The default value is 100 KB. - * @return Callback threshold of the data transfer listener - */ - public long getProgressInterval() { - return progressInterval; - } - - /** - * Set the callback threshold of the data transfer listener. The default value is 100 KB. - * @param progressInterval Callback threshold of the data transfer listener - */ - public void setProgressInterval(long progressInterval) { - this.progressInterval = progressInterval; - } - - /** - * Obtain the control option of the read-ahead cache. - * @return Control option of the read-ahead cache - */ - public CacheOptionEnum getCacheOption() { - return cacheOption; - } - - /** - * Set the control option of the read-ahead cache. - * @param cacheOption Control option of the read-ahead cache - */ - public void setCacheOption(CacheOptionEnum cacheOption) { - this.cacheOption = cacheOption; - } - - /** - * Obtain the cache data expiration time. - * @return Cache data expiration time - */ - public long getTtl() { - return ttl; - } - - /** - * Set the cache data expiration time. - * @param ttl Cache data expiration time - */ - public void setTtl(long ttl) { - if(ttl < 0 || ttl > 259200) { - ttl = 60 * 60 * 24L; - } - this.ttl = ttl; - } - + + /** + * Obtain the data transfer listener. + * + * @return Data transfer listener + */ + public ProgressListener getProgressListener() { + return progressListener; + } + + /** + * Set the data transfer listener. + * + * @param progressListener + * Data transfer listener + */ + public void setProgressListener(ProgressListener progressListener) { + this.progressListener = progressListener; + } + + /** + * Obtain the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @return Callback threshold of the data transfer listener + */ + public long getProgressInterval() { + return progressInterval; + } + + /** + * Set the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @param progressInterval + * Callback threshold of the data transfer listener + */ + public void setProgressInterval(long progressInterval) { + this.progressInterval = progressInterval; + } + + /** + * Obtain the control option of the read-ahead cache. + * + * @return Control option of the read-ahead cache + */ + public CacheOptionEnum getCacheOption() { + return cacheOption; + } + + /** + * Set the control option of the read-ahead cache. + * + * @param cacheOption + * Control option of the read-ahead cache + */ + public void setCacheOption(CacheOptionEnum cacheOption) { + this.cacheOption = cacheOption; + } + + /** + * Obtain the cache data expiration time. + * + * @return Cache data expiration time + */ + public long getTtl() { + return ttl; + } + + /** + * Set the cache data expiration time. + * + * @param ttl + * Cache data expiration time + */ + public void setTtl(long ttl) { + if (ttl < 0 || ttl > 259200) { + ttl = 60 * 60 * 24L; + } + this.ttl = ttl; + } + + @Override - public String toString() - { - return "DownloadFileRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", downloadFile=" + downloadFile - + ", partSize=" + partSize + ", taskNum=" + taskNum + ", checkpointFile=" + checkpointFile + ", enableCheckpoint=" - + enableCheckpoint + ", ifModifiedSince=" + ifModifiedSince + ", ifUnmodifiedSince=" + ifUnmodifiedSince + ", ifMatchTag=" - + ifMatchTag + ", ifNoneMatchTag=" + ifNoneMatchTag + ", versionId=" + versionId + "]"; + public String toString() { + return "DownloadFileRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", downloadFile=" + + downloadFile + ", partSize=" + partSize + ", taskNum=" + taskNum + ", checkpointFile=" + + checkpointFile + ", enableCheckpoint=" + enableCheckpoint + ", ifModifiedSince=" + ifModifiedSince + + ", ifUnmodifiedSince=" + ifUnmodifiedSince + ", ifMatchTag=" + ifMatchTag + ", ifNoneMatchTag=" + + ifNoneMatchTag + ", versionId=" + versionId + ", isEncodeHeaders=" + encodeHeaders + "]"; } } diff --git a/app/src/main/java/com/obs/services/model/DownloadFileResult.java b/app/src/main/java/com/obs/services/model/DownloadFileResult.java index 5368dcd..e248806 100644 --- a/app/src/main/java/com/obs/services/model/DownloadFileResult.java +++ b/app/src/main/java/com/obs/services/model/DownloadFileResult.java @@ -11,15 +11,14 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services.model; -import com.obs.services.model.ObjectMetadata; +package com.obs.services.model; /** * Response to a file download request */ public class DownloadFileResult { - /** + /** * Obtain object properties. * * @return Object properties @@ -31,18 +30,18 @@ public ObjectMetadata getObjectMetadata() { /** * Set object properties. * - * @param objectMetadata Object properties + * @param objectMetadata + * Object properties */ public void setObjectMetadata(ObjectMetadata objectMetadata) { this.objectMetadata = objectMetadata; } - + private ObjectMetadata objectMetadata; @Override - public String toString() - { + public String toString() { return "DownloadFileResult [objectMetadata=" + objectMetadata + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/EventTypeEnum.java b/app/src/main/java/com/obs/services/model/EventTypeEnum.java index 86a73bb..f9c2dbf 100644 --- a/app/src/main/java/com/obs/services/model/EventTypeEnum.java +++ b/app/src/main/java/com/obs/services/model/EventTypeEnum.java @@ -21,70 +21,69 @@ * */ public enum EventTypeEnum { - - /** - * All events for creating objects - */ - OBJECT_CREATED_ALL, - /** - * PUT Object events - */ - OBJECT_CREATED_PUT, + /** + * All events for creating objects + */ + OBJECT_CREATED_ALL, - /** - * POST Object events - */ - OBJECT_CREATED_POST, + /** + * PUT Object events + */ + OBJECT_CREATED_PUT, - /** - * Events for copying objects - */ - OBJECT_CREATED_COPY, + /** + * POST Object events + */ + OBJECT_CREATED_POST, - /** - * Events for combining parts - */ - OBJECT_CREATED_COMPLETE_MULTIPART_UPLOAD, + /** + * Events for copying objects + */ + OBJECT_CREATED_COPY, - /** - * All events for deleting objects - */ - OBJECT_REMOVED_ALL, + /** + * Events for combining parts + */ + OBJECT_CREATED_COMPLETE_MULTIPART_UPLOAD, - /** - * Events for deleting objects by specifying object version IDs - */ - OBJECT_REMOVED_DELETE, + /** + * All events for deleting objects + */ + OBJECT_REMOVED_ALL, - /** - * Events for deleting objects without specifying version IDs after versioning is enabled - */ - OBJECT_REMOVED_DELETE_MARKER_CREATED; + /** + * Events for deleting objects by specifying object version IDs + */ + OBJECT_REMOVED_DELETE, - public static EventTypeEnum getValueFromCode(String code) { - if (ServiceUtils.isValid(code)) { - if (code.indexOf("ObjectCreated:*") >= 0) { - return OBJECT_CREATED_ALL; - } else if (code.indexOf("ObjectCreated:Put") >= 0) { - return OBJECT_CREATED_PUT; - } else if (code.indexOf("ObjectCreated:Post") >= 0) { - return OBJECT_CREATED_POST; - } else if (code.indexOf("ObjectCreated:Copy") >= 0) { - return OBJECT_CREATED_COPY; - } else if (code.indexOf("ObjectCreated:CompleteMultipartUpload") >= 0) { - return OBJECT_CREATED_COMPLETE_MULTIPART_UPLOAD; - } else if (code.indexOf("ObjectRemoved:*") >= 0) { - return OBJECT_REMOVED_ALL; - } else if (code.indexOf("ObjectRemoved:Delete") >= 0) { - return OBJECT_REMOVED_DELETE; - } else if (code.indexOf("ObjectRemoved:DeleteMarkerCreated") >= 0) { - return OBJECT_REMOVED_DELETE_MARKER_CREATED; - } - } - return null; - } - -} + /** + * Events for deleting objects without specifying version IDs after + * versioning is enabled + */ + OBJECT_REMOVED_DELETE_MARKER_CREATED; + public static EventTypeEnum getValueFromCode(String code) { + if (ServiceUtils.isValid(code)) { + if (code.indexOf("ObjectCreated:*") >= 0) { + return OBJECT_CREATED_ALL; + } else if (code.indexOf("ObjectCreated:Put") >= 0) { + return OBJECT_CREATED_PUT; + } else if (code.indexOf("ObjectCreated:Post") >= 0) { + return OBJECT_CREATED_POST; + } else if (code.indexOf("ObjectCreated:Copy") >= 0) { + return OBJECT_CREATED_COPY; + } else if (code.indexOf("ObjectCreated:CompleteMultipartUpload") >= 0) { + return OBJECT_CREATED_COMPLETE_MULTIPART_UPLOAD; + } else if (code.indexOf("ObjectRemoved:*") >= 0) { + return OBJECT_REMOVED_ALL; + } else if (code.indexOf("ObjectRemoved:Delete") >= 0) { + return OBJECT_REMOVED_DELETE; + } else if (code.indexOf("ObjectRemoved:DeleteMarkerCreated") >= 0) { + return OBJECT_REMOVED_DELETE_MARKER_CREATED; + } + } + return null; + } +} diff --git a/app/src/main/java/com/obs/services/model/ExtensionBucketPermissionEnum.java b/app/src/main/java/com/obs/services/model/ExtensionBucketPermissionEnum.java index 8aee689..44f143d 100644 --- a/app/src/main/java/com/obs/services/model/ExtensionBucketPermissionEnum.java +++ b/app/src/main/java/com/obs/services/model/ExtensionBucketPermissionEnum.java @@ -1,62 +1,81 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + **/ package com.obs.services.model; /** - * - * OBS Bucket extension permission + * + * OBS Bucket extension permission * */ -public enum ExtensionBucketPermissionEnum{ - /** - * Grant the read permission to all users belonging to the specified "domainId" for listing objects, listing multipart uploads, listing bucket versions, and obtaining bucket metadata. - */ - GRANT_READ("grantReadHeader"), - /** - * Grant the write permission to all users belonging to the specified "domainId" so that the users can create, delete, overwrite objects in buckets, as well as initialize, upload, copy, and combine parts and cancel multipart uploads. - */ - GRANT_WRITE("grantWriteHeader"), - /** - * Grant the "READ_ACP" permission to all users belonging to the specified "domainId" to obtain ACLs of objects. - */ - GRANT_READ_ACP("grantReadAcpHeader"), - /** - * Grant the "WRITE_ACP" permission to all users belonging to the specified "domainId" to modify bucket ACLs. - */ - GRANT_WRITE_ACP("grantWriteAcpHeader"), - /** - * Grant full control permissions to users in the domain of the specified ID. - */ - GRANT_FULL_CONTROL("grantFullControlHeader"), - /** - * Grant the read permission to all users belonging to the specified "domainId". By default, these users have the read permission on all objects in the bucket. - */ - GRANT_READ_DELIVERED("grantReadDeliveredHeader"), - /** - * Grant the full control permission to all users belonging to the specified "domainId". By default, these users have the full control permission on all objects in the bucket. - */ - GRANT_FULL_CONTROL_DELIVERED("grantFullControlDeliveredHeader"); - - private String code; - - private ExtensionBucketPermissionEnum(String code) { - this.code = code; - } - - public String getCode() { - return this.code; - } -} +public enum ExtensionBucketPermissionEnum { + /** + * Grant the read permission to all users belonging to the specified + * "domainId" for listing objects, listing multipart uploads, listing bucket + * versions, and obtaining bucket metadata. + */ + GRANT_READ("grantReadHeader"), + /** + * Grant the write permission to all users + * belonging to the specified "domainId" so + * that the users can create, delete, + * overwrite objects in buckets, as well as + * initialize, upload, copy, and combine + * parts and cancel multipart uploads. + */ + GRANT_WRITE("grantWriteHeader"), + /** + * Grant the "READ_ACP" permission to all + * users belonging to the specified + * "domainId" to obtain ACLs of objects. + */ + GRANT_READ_ACP("grantReadAcpHeader"), + /** + * Grant the "WRITE_ACP" permission to all + * users belonging to the specified + * "domainId" to modify bucket ACLs. + */ + GRANT_WRITE_ACP("grantWriteAcpHeader"), + /** + * Grant full control permissions to users + * in the domain of the specified ID. + */ + GRANT_FULL_CONTROL("grantFullControlHeader"), + /** + * Grant the read permission to all users + * belonging to the specified "domainId". + * By default, these users have the read + * permission on all objects in the + * bucket. + */ + GRANT_READ_DELIVERED("grantReadDeliveredHeader"), + /** + * Grant the full control permission to + * all users belonging to the specified + * "domainId". By default, these users + * have the full control permission on + * all objects in the bucket. + */ + GRANT_FULL_CONTROL_DELIVERED("grantFullControlDeliveredHeader"); + + private String code; + private ExtensionBucketPermissionEnum(String code) { + this.code = code; + } + + public String getCode() { + return this.code; + } +} diff --git a/app/src/main/java/com/obs/services/model/ExtensionObjectPermissionEnum.java b/app/src/main/java/com/obs/services/model/ExtensionObjectPermissionEnum.java index 5728e91..3fcaac5 100644 --- a/app/src/main/java/com/obs/services/model/ExtensionObjectPermissionEnum.java +++ b/app/src/main/java/com/obs/services/model/ExtensionObjectPermissionEnum.java @@ -1,50 +1,58 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + **/ package com.obs.services.model; /** - * - * OBS Bucket extension permission + * + * OBS Bucket extension permission * */ -public enum ExtensionObjectPermissionEnum{ - /** - * Grant the read permission on all users belonging to the specified "domainId" to read objects and obtain object metadata. - */ - GRANT_READ("grantReadHeader"), - /** - * Grant the "READ_ACP" permission to all users belonging to the specified "domainId" to obtain ACLs of objects. - */ - GRANT_READ_ACP("grantReadAcpHeader"), - /** - * Grant the "WRITE_ACP" permission to all users belonging to the specified "domainId" to modify ACLs of objects. - */ - GRANT_WRITE_ACP("grantWriteAcpHeader"), - /** - * Grant the full control permission to all users belonging to the specified "domainId" to read objects, obtain object metadata, as well as obtain and write object ACLs. - */ - GRANT_FULL_CONTROL("grantFullControlHeader"); - - private String code; - - private ExtensionObjectPermissionEnum(String code) { - this.code = code; - } - - public String getCode() { - return this.code; - } -} +public enum ExtensionObjectPermissionEnum { + /** + * Grant the read permission on all users belonging to the specified + * "domainId" to read objects and obtain object metadata. + */ + GRANT_READ("grantReadHeader"), + /** + * Grant the "READ_ACP" permission to all + * users belonging to the specified + * "domainId" to obtain ACLs of objects. + */ + GRANT_READ_ACP("grantReadAcpHeader"), + /** + * Grant the "WRITE_ACP" permission to all + * users belonging to the specified + * "domainId" to modify ACLs of objects. + */ + GRANT_WRITE_ACP("grantWriteAcpHeader"), + /** + * Grant the full control permission to all + * users belonging to the specified + * "domainId" to read objects, obtain object + * metadata, as well as obtain and write + * object ACLs. + */ + GRANT_FULL_CONTROL("grantFullControlHeader"); + + private String code; + private ExtensionObjectPermissionEnum(String code) { + this.code = code; + } + + public String getCode() { + return this.code; + } +} diff --git a/app/src/main/java/com/obs/services/model/FunctionGraphConfiguration.java b/app/src/main/java/com/obs/services/model/FunctionGraphConfiguration.java index 80b342b..1fccf5c 100644 --- a/app/src/main/java/com/obs/services/model/FunctionGraphConfiguration.java +++ b/app/src/main/java/com/obs/services/model/FunctionGraphConfiguration.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; import java.util.List; @@ -22,7 +23,7 @@ public class FunctionGraphConfiguration extends AbstractNotification { private String functionGraph; - + public FunctionGraphConfiguration() { } @@ -30,10 +31,14 @@ public FunctionGraphConfiguration() { /** * Constructor * - * @param id Event notification configuration ID - * @param filter Filtering rules - * @param functionGraph FunctionGraph URN - * @param events List of event types that need to be notified + * @param id + * Event notification configuration ID + * @param filter + * Filtering rules + * @param functionGraph + * FunctionGraph URN + * @param events + * List of event types that need to be notified */ public FunctionGraphConfiguration(String id, Filter filter, String functionGraph, List events) { super(id, filter, events); @@ -53,7 +58,8 @@ public String getFunctionGraph() { /** * Set the FunctionGraph URN. * - * @param functionGraph FunctionGraph URN + * @param functionGraph + * FunctionGraph URN */ public void setFunctionGraph(String functionGraph) { this.functionGraph = functionGraph; @@ -61,8 +67,8 @@ public void setFunctionGraph(String functionGraph) { @Override public String toString() { - return "FunctionGraphConfiguration [id=" + id + ", functionGraph=" + functionGraph + ", events=" + events + ", filter=" - + filter + "]"; + return "FunctionGraphConfiguration [id=" + id + ", functionGraph=" + functionGraph + ", events=" + events + + ", filter=" + filter + "]"; } } diff --git a/app/src/main/java/com/obs/services/model/GenericRequest.java b/app/src/main/java/com/obs/services/model/GenericRequest.java new file mode 100644 index 0000000..894f741 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/GenericRequest.java @@ -0,0 +1,114 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +import java.util.HashMap; + +/** + * Basic class of all requests, which encapsulates common parameters used by all requests. + * + * @since 3.20.3 + */ +public class GenericRequest { + protected String bucketName; + /** + * If the requester-pays function is enabled, the requester pays for his/her operations on the bucket. + */ + private boolean isRequesterPays; + + protected HashMap userHeaders = new HashMap<>(); + + protected HttpMethodEnum httpMethod; + + public GenericRequest() { + } + + public GenericRequest(String bucketName) { + this.bucketName = bucketName; + } + + public HttpMethodEnum getHttpMethod() { + return httpMethod; + } + + /** + * Set the bucket name. + * + * @param bucketName + * Bucket name + */ + public void setBucketName(String bucketName) { + this.bucketName = bucketName; + } + + /** + * Obtain the bucket name. + * + * @return Bucket name + */ + public String getBucketName() { + return bucketName; + } + + public void addUserHeaders(String key, String value) { + userHeaders.put(key, value); + } + + public void setUserHeaders(HashMap userHeaders) { + this.userHeaders = userHeaders; + } + + public HashMap getUserHeaders() { + return userHeaders; + } + + /** + * If the requester is allowed to pay, true is returned. Otherwise, false is returned. + * + *

    + * If the requester-pays function is enabled for a bucket, this attribute must be set to true when the bucket is + * requested by a requester other than the bucket owner. Otherwise, status code 403 is returned. + * + *

    + * After the requester-pays function is enabled, anonymous access to the bucket is not allowed. + * + * @return If the requester is allowed to pay, true is returned. Otherwise, false is returned. + */ + public boolean isRequesterPays() { + return isRequesterPays; + } + + /** + * Used to configure whether to enable the requester-pays function. + * + *

    + * If the requester-pays function is enabled for a bucket, this attribute must be set to true when the bucket is + * requested by a requester other than the bucket owner. Otherwise, status code 403 is returned. + * + *

    + * After the requester-pays function is enabled, anonymous access to the bucket is not allowed. + * + * @param isRequesterPays True indicates to enable the requester-pays function. + * False indicates to disable the requester-pays function. + */ + public void setRequesterPays(boolean isRequesterPays) { + this.isRequesterPays = isRequesterPays; + } + + @Override + public String toString() { + return "GenericRequest [isRequesterPays=" + isRequesterPays + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/GetBucketCustomDomainRequest.java b/app/src/main/java/com/obs/services/model/GetBucketCustomDomainRequest.java new file mode 100644 index 0000000..8bab087 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/GetBucketCustomDomainRequest.java @@ -0,0 +1,32 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.model; + +/** + * + * + * @since 3.21.8 + */ +public class GetBucketCustomDomainRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.GET; + } + + public GetBucketCustomDomainRequest(String bucketName) { + super(bucketName); + } +} diff --git a/app/src/main/java/com/obs/services/model/GetObjectAclRequest.java b/app/src/main/java/com/obs/services/model/GetObjectAclRequest.java new file mode 100644 index 0000000..5f3b072 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/GetObjectAclRequest.java @@ -0,0 +1,94 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Parameters in a request for obtaining object ACLs + * + * @since 3.20.3 + */ +public class GetObjectAclRequest extends BaseObjectRequest { + + { + httpMethod = HttpMethodEnum.GET; + } + + private String versionId; + + public GetObjectAclRequest() { + + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param versionId + * Object version ID + */ + public GetObjectAclRequest(String bucketName, String objectKey, String versionId) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.versionId = versionId; + } + + /** + * Obtain the object version ID. + * + * @return Version ID of the object + */ + public String getVersionId() { + return versionId; + } + + /** + * Set the version ID of the object. + * + * @param versionId + * Object version ID + */ + public void setVersionId(String versionId) { + this.versionId = versionId; + } + + /** + * Obtain the name of the object. + * + * @return Object name + */ + public String getObjectKey() { + return objectKey; + } + + /** + * Set a name for an object. + * + * @param objectKey + * Object name + */ + public void setObjectKey(String objectKey) { + this.objectKey = objectKey; + } + + @Override + public String toString() { + return "AbortMultipartUploadRequest [bucketName=" + bucketName + ", objectKey=" + + objectKey + ", versionId=" + versionId + "]"; + } + +} diff --git a/app/src/main/java/com/obs/services/model/GetObjectMetadataRequest.java b/app/src/main/java/com/obs/services/model/GetObjectMetadataRequest.java index bd49a07..3550ab4 100644 --- a/app/src/main/java/com/obs/services/model/GetObjectMetadataRequest.java +++ b/app/src/main/java/com/obs/services/model/GetObjectMetadataRequest.java @@ -17,127 +17,109 @@ /** * Parameters in a request for obtaining the properties of an object */ -public class GetObjectMetadataRequest -{ - private String bucketName; - - private String objectKey; - - private String versionId; - - private SseCHeader sseCHeader; - - public GetObjectMetadataRequest(){ - +public class GetObjectMetadataRequest extends BaseObjectRequest { + + { + httpMethod = HttpMethodEnum.HEAD; + } + + protected String versionId; + + protected SseCHeader sseCHeader; + + public GetObjectMetadataRequest() { } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name */ - public GetObjectMetadataRequest(String bucketName, String objectKey) - { + public GetObjectMetadataRequest(String bucketName, String objectKey) { this.bucketName = bucketName; this.objectKey = objectKey; } /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name - * @param versionId Version ID of the object + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param versionId + * Version ID of the object */ - public GetObjectMetadataRequest(String bucketName, String objectKey, String versionId) - { + public GetObjectMetadataRequest(String bucketName, String objectKey, String versionId) { this.bucketName = bucketName; this.objectKey = objectKey; this.versionId = versionId; } - - /** - * Obtain SSE-C decryption headers. + * Obtain SSE-C decryption headers. * * @return SSE-C decryption headers */ - public SseCHeader getSseCHeader() - { + public SseCHeader getSseCHeader() { return sseCHeader; } - + /** - * Set SSE-C decryption headers. + * Set SSE-C decryption headers. * - * @param sseCHeader SSE-C decryption headers + * @param sseCHeader + * SSE-C decryption headers */ - public void setSseCHeader(SseCHeader sseCHeader) - { + public void setSseCHeader(SseCHeader sseCHeader) { this.sseCHeader = sseCHeader; } - - /** - * Obtain the bucket name. - * @return Bucket name - */ - public String getBucketName() - { - return bucketName; - } - - /** - * Set the bucket name. - * @param bucketName Bucket name - */ - public void setBucketName(String bucketName) - { - this.bucketName = bucketName; - } - + /** * Obtain the object name. + * * @return Object name */ - public String getObjectKey() - { + public String getObjectKey() { return objectKey; } - + /** * Set the object name. - * @param objectKey Object name + * + * @param objectKey + * Object name */ - public void setObjectKey(String objectKey) - { + public void setObjectKey(String objectKey) { this.objectKey = objectKey; } - + /** * Obtain the object version ID. + * * @return Version ID of the object */ - public String getVersionId() - { + public String getVersionId() { return versionId; } - + /** - * Set the version ID of the object. - * @param versionId Version ID of the object + * Set the version ID of the object. + * + * @param versionId + * Version ID of the object */ - public void setVersionId(String versionId) - { + public void setVersionId(String versionId) { this.versionId = versionId; } @Override - public String toString() - { - return "GetObjectMetadataRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", versionId=" + versionId - + ", sseCHeader=" + sseCHeader + "]"; + public String toString() { + return "GetObjectMetadataRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", versionId=" + + ", isEncodeHeaders=" + encodeHeaders + versionId + ", sseCHeader=" + sseCHeader + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/GetObjectRequest.java b/app/src/main/java/com/obs/services/model/GetObjectRequest.java index f7d6674..b2b1630 100644 --- a/app/src/main/java/com/obs/services/model/GetObjectRequest.java +++ b/app/src/main/java/com/obs/services/model/GetObjectRequest.java @@ -1,16 +1,17 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + package com.obs.services.model; import java.util.Date; @@ -22,394 +23,437 @@ /** * Parameters in an object download request */ -public class GetObjectRequest -{ - private String bucketName; - - private String objectKey; - +public class GetObjectRequest extends GetObjectMetadataRequest { + + { + httpMethod = HttpMethodEnum.GET; + } + private Long rangeStart; - + private Long rangeEnd; - - private String versionId; - + private ObjectRepleaceMetadata replaceMetadata; - - private SseCHeader sseCHeader; - + private Date ifModifiedSince; - + private Date ifUnmodifiedSince; - + private String ifMatchTag; - + private String ifNoneMatchTag; - + private String imageProcess; - + private ProgressListener progressListener; - + private long progressInterval = ObsConstraint.DEFAULT_PROGRESS_INTERVAL; - + private CacheOptionEnum cacheOption; - + private long ttl; - + private Map requestParameters; - - public GetObjectRequest(){ - + + private boolean autoUnzipResponse = false; + + public GetObjectRequest() { + } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name */ - public GetObjectRequest(String bucketName, String objectKey) - { + public GetObjectRequest(String bucketName, String objectKey) { this.bucketName = bucketName; this.objectKey = objectKey; } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name - * @param versionId Version ID of the object + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param versionId + * Version ID of the object */ - public GetObjectRequest(String bucketName, String objectKey, String versionId) - { + public GetObjectRequest(String bucketName, String objectKey, String versionId) { this.bucketName = bucketName; this.objectKey = objectKey; this.versionId = versionId; } /** - * Obtain the request headers that need to be rewritten during object download. + * Obtain the request headers that need to be rewritten during object + * download. + * * @return Rewritten response headers */ - public ObjectRepleaceMetadata getReplaceMetadata() - { + public ObjectRepleaceMetadata getReplaceMetadata() { return replaceMetadata; } - + /** * Set the request headers that need to be rewritten during object download. - * @param replaceMetadata Rewritten response headers + * + * @param replaceMetadata + * Rewritten response headers */ - public void setReplaceMetadata(ObjectRepleaceMetadata replaceMetadata) - { + public void setReplaceMetadata(ObjectRepleaceMetadata replaceMetadata) { this.replaceMetadata = replaceMetadata; } - + /** - * Obtain SSE-C decryption headers. + * Obtain SSE-C decryption headers. * * @return SSE-C decryption headers */ - public SseCHeader getSseCHeader() - { + public SseCHeader getSseCHeader() { return sseCHeader; } - + /** - * Set SSE-C decryption headers. + * Set SSE-C decryption headers. * - * @param sseCHeader SSE-C decryption headers + * @param sseCHeader + * SSE-C decryption headers */ - public void setSseCHeader(SseCHeader sseCHeader) - { + public void setSseCHeader(SseCHeader sseCHeader) { this.sseCHeader = sseCHeader; } - - + /** * Obtain the start position for object download. * * @return Start position for object download */ - public Long getRangeStart() - { + public Long getRangeStart() { return rangeStart; } - + /** * Set the start position for object download. * - * @param rangeStart Start position for object download + * @param rangeStart + * Start position for object download */ - public void setRangeStart(Long rangeStart) - { + public void setRangeStart(Long rangeStart) { this.rangeStart = rangeStart; } - + /** * Obtain the end position for object download. * * @return End position for object download */ - public Long getRangeEnd() - { + public Long getRangeEnd() { return rangeEnd; } - + /** * Set the end position for object download. * - * @param rangeEnd End position for object download + * @param rangeEnd + * End position for object download * */ - public void setRangeEnd(Long rangeEnd) - { + public void setRangeEnd(Long rangeEnd) { this.rangeEnd = rangeEnd; } - - /** - * Obtain the bucket name. - * - * @return Bucket name - */ - public String getBucketName() - { - return bucketName; - } - - /** - * Set the bucket name. - * - * @param bucketName Bucket name - * - */ - public void setBucketName(String bucketName) - { - this.bucketName = bucketName; - } - - /** - * Obtain the object name. - * - * @return Object name - */ - public String getObjectKey() - { - return objectKey; - } - - /** - * Set the object name. - * - * @param objectKey Object name - * - */ - public void setObjectKey(String objectKey) - { - this.objectKey = objectKey; - } - + /** * Obtain the object version ID. * * @return Version ID of the object */ - public String getVersionId() - { + public String getVersionId() { return versionId; } - + /** - * Set the version ID of the object. + * Set the version ID of the object. * - * @param versionId Version ID of the object + * @param versionId + * Version ID of the object * */ - public void setVersionId(String versionId) - { + public void setVersionId(String versionId) { this.versionId = versionId; } /** - * Obtain the time condition set for downloading the object. Only when the object is modified after the point in time specified by this parameter, it will be downloaded. Otherwise, "304 Not Modified" will be returned. + * Obtain the time condition set for downloading the object. Only when the + * object is modified after the point in time specified by this parameter, + * it will be downloaded. Otherwise, "304 Not Modified" will be returned. * * @return Time condition set for downloading the object */ - public Date getIfModifiedSince() - { + public Date getIfModifiedSince() { return ServiceUtils.cloneDateIgnoreNull(this.ifModifiedSince); } - + /** - * Set the time condition set for downloading the object. Only when the object is modified after the point in time specified by this parameter, it will be downloaded. Otherwise, "304 Not Modified" will be returned. + * Set the time condition set for downloading the object. Only when the + * object is modified after the point in time specified by this parameter, + * it will be downloaded. Otherwise, "304 Not Modified" will be returned. * - * @param ifModifiedSince Time condition set for downloading the object + * @param ifModifiedSince + * Time condition set for downloading the object */ - public void setIfModifiedSince(Date ifModifiedSince) - { + public void setIfModifiedSince(Date ifModifiedSince) { this.ifModifiedSince = ServiceUtils.cloneDateIgnoreNull(ifModifiedSince); } - + /** - * Obtain the time condition for downloading the object. Only when the object remains unchanged after the point in time specified by this parameter, it will be downloaded; otherwise, "412 Precondition Failed" will be returned. + * Obtain the time condition for downloading the object. Only when the + * object remains unchanged after the point in time specified by this + * parameter, it will be downloaded; otherwise, "412 Precondition Failed" + * will be returned. * * @return Time condition set for downloading the object */ - public Date getIfUnmodifiedSince() - { - return ServiceUtils.cloneDateIgnoreNull(this.ifUnmodifiedSince); + public Date getIfUnmodifiedSince() { + return ServiceUtils.cloneDateIgnoreNull(this.ifUnmodifiedSince); } - + /** - * Set the time condition for downloading the object. Only when the object remains unchanged after the point in time specified by this parameter, it will be downloaded; otherwise, "412 Precondition Failed" will be returned. + * Set the time condition for downloading the object. Only when the object + * remains unchanged after the point in time specified by this parameter, it + * will be downloaded; otherwise, "412 Precondition Failed" will be + * returned. * - * @param ifUnmodifiedSince Time condition set for downloading the object + * @param ifUnmodifiedSince + * Time condition set for downloading the object */ - public void setIfUnmodifiedSince(Date ifUnmodifiedSince) - { + public void setIfUnmodifiedSince(Date ifUnmodifiedSince) { this.ifUnmodifiedSince = ServiceUtils.cloneDateIgnoreNull(ifUnmodifiedSince); } - + /** - * Obtain the ETag verification condition for downloading the object. Only when the ETag of the object is the same as that specified by this parameter, the object will be downloaded. Otherwise, "412 Precondition Failed" will be returned. + * Obtain the ETag verification condition for downloading the object. Only + * when the ETag of the object is the same as that specified by this + * parameter, the object will be downloaded. Otherwise, + * "412 Precondition Failed" will be returned. * * @return ETag verification condition set for downloading the object */ - public String getIfMatchTag() - { + public String getIfMatchTag() { return ifMatchTag; } - + /** - * Set the ETag verification condition for downloading the object. Only when the ETag of the object is the same as that specified by this parameter, the object will be downloaded. Otherwise, "412 Precondition Failed" will be returned. + * Set the ETag verification condition for downloading the object. Only when + * the ETag of the object is the same as that specified by this parameter, + * the object will be downloaded. Otherwise, "412 Precondition Failed" will + * be returned. * - * @param ifMatchTag ETag verification condition set for downloading the object + * @param ifMatchTag + * ETag verification condition set for downloading the object */ - public void setIfMatchTag(String ifMatchTag) - { + public void setIfMatchTag(String ifMatchTag) { this.ifMatchTag = ifMatchTag; } - + /** - * Obtain the ETag verification condition for downloading the object. Only when the ETag of the object is different from that specified by this parameter, the object will be downloaded. Otherwise, "304 Not Modified" will be returned. + * Obtain the ETag verification condition for downloading the object. Only + * when the ETag of the object is different from that specified by this + * parameter, the object will be downloaded. Otherwise, "304 Not Modified" + * will be returned. * * @return ETag verification condition set for downloading the object */ - public String getIfNoneMatchTag() - { + public String getIfNoneMatchTag() { return ifNoneMatchTag; } - + /** - * Set the ETag verification condition for downloading the object. Only when the ETag of the object is different from that specified by this parameter, the object will be downloaded. Otherwise, "304 Not Modified" will be returned. + * Set the ETag verification condition for downloading the object. Only when + * the ETag of the object is different from that specified by this + * parameter, the object will be downloaded. Otherwise, "304 Not Modified" + * will be returned. * - * @param ifNoneMatchTag ETag verification condition set for downloading the object + * @param ifNoneMatchTag + * ETag verification condition set for downloading the object * */ - public void setIfNoneMatchTag(String ifNoneMatchTag) - { + public void setIfNoneMatchTag(String ifNoneMatchTag) { this.ifNoneMatchTag = ifNoneMatchTag; } /** * Obtain image processing parameters. + * * @return Image processing parameters */ - public String getImageProcess() - { + public String getImageProcess() { return imageProcess; } /** * Set image processing parameters. - * @param imageProcess Image processing parameters + * + * @param imageProcess + * Image processing parameters */ - public void setImageProcess(String imageProcess) - { + public void setImageProcess(String imageProcess) { this.imageProcess = imageProcess; } + + /** + * Obtain the data transfer listener. + * + * @return Data transfer listener + */ + public ProgressListener getProgressListener() { + return progressListener; + } + + /** + * Set the data transfer listener. + * + * @param progressListener + * Data transfer listener + */ + public void setProgressListener(ProgressListener progressListener) { + this.progressListener = progressListener; + } + + /** + * Obtain the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @return Callback threshold of the data transfer listener + */ + public long getProgressInterval() { + return progressInterval; + } + + /** + * Set the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @param progressInterval + * Callback threshold of the data transfer listener + */ + public void setProgressInterval(long progressInterval) { + this.progressInterval = progressInterval; + } + + public Map getRequestParameters() { + return this.requestParameters; + } + + /** + * Obtain the control option of the read-ahead cache. + * + * @return Control option of the read-ahead cache + */ + public CacheOptionEnum getCacheOption() { + return cacheOption; + } + + /** + * Set the control option of the read-ahead cache. + * + * @param cacheOption + * Control option of the read-ahead cache + */ + public void setCacheOption(CacheOptionEnum cacheOption) { + this.cacheOption = cacheOption; + } + + /** + * Obtain the cache data expiration time. + * + * @return Cache data expiration time + */ + public long getTtl() { + return ttl; + } + + /** + * Set the cache data expiration time. + * + * @param ttl + * Cache data expiration time + */ + public void setTtl(long ttl) { + if (ttl < 0 || ttl > 259200) { + ttl = 60 * 60 * 24L; + } + this.ttl = ttl; + } + + public void setRequestParameters(Map requestParameters) { + this.requestParameters = requestParameters; + } /** - * Obtain the data transfer listener. - * @return Data transfer listener - */ - public ProgressListener getProgressListener() { - return progressListener; - } - - /** - * Set the data transfer listener. - * @param progressListener Data transfer listener - */ - public void setProgressListener(ProgressListener progressListener) { - this.progressListener = progressListener; - } - - /** - * Obtain the callback threshold of the data transfer listener. The default value is 100 KB. - * @return Callback threshold of the data transfer listener - */ - public long getProgressInterval() { - return progressInterval; - } - - /** - * Set the callback threshold of the data transfer listener. The default value is 100 KB. - * @param progressInterval Callback threshold of the data transfer listener - */ - public void setProgressInterval(long progressInterval) { - this.progressInterval = progressInterval; - } - - public Map getRequestParameters() { - return this.requestParameters; - } - - /** - * Obtain the control option of the read-ahead cache. - * @return Control option of the read-ahead cache - */ - public CacheOptionEnum getCacheOption() { - return cacheOption; - } - - /** - * Set the control option of the read-ahead cache. - * @param cacheOption Control option of the read-ahead cache - */ - public void setCacheOption(CacheOptionEnum cacheOption) { - this.cacheOption = cacheOption; - } - - /** - * Obtain the cache data expiration time. - * @return Cache data expiration time - */ - public long getTtl() { - return ttl; - } - - /** - * Set the cache data expiration time. - * @param ttl Cache data expiration time - */ - public void setTtl(long ttl) { - if(ttl < 0 || ttl > 259200) { - ttl = 60 * 60 * 24L; - } - this.ttl = ttl; - } - - public void setRequestParameters(Map requestParameters) { - this.requestParameters = requestParameters; - } + * Obtain the response to whether to automatically decompress the object compressed using gzip. + * The default response is false, indicating that the object is not automatically decompressed.
    + * Description: OBS SDK uses okhttp. If Accept-Encoding is not set in a request, + * okhttp automatically adds the Accept-Encoding:gzip header and decompresses the object after + * receiving the request.
    + * If the Accept-Encoding header is manually set in a request, + * the object will not be automatically decompressed. + * @see okhttp3.internal.http.BridgeInterceptor + * @return + * @since 3.20.5 + */ + public boolean isAutoUnzipResponse() { + return autoUnzipResponse; + } + + /** + * Set the response to whether to automatically decompress the object compressed using gzip. + * The default response is false, indicating that the object is not automatically decompressed.
    + * Description: OBS SDK uses okhttp. If Accept-Encoding is not set in a request, + * okhttp automatically adds the Accept-Encoding:gzip header and decompresses the object after + * receiving the request.
    + * If the Accept-Encoding header is manually set in a request, + * the object will not be automatically decompressed. + * @see okhttp3.internal.http.BridgeInterceptor + * @param autoUnzipResponse + * @since 3.20.5 + */ + public void setAutoUnzipResponse(boolean autoUnzipResponse) { + this.autoUnzipResponse = autoUnzipResponse; + } + + /** + * Specifies whether to encode and decode the returned header fields. + * + * @param encodeHeaders + * Specifies whether to encode and decode header fields. + */ + public void setIsEncodeHeaders(boolean encodeHeaders) { + this.encodeHeaders = encodeHeaders; + } + + /** + * Specifies whether to encode and decode the returned header fields. + * + * @return Specifies whether to encode and decode header fields. + */ + public boolean isEncodeHeaders() { + return encodeHeaders; + } @Override - public String toString() - { - return "GetObjectRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", rangeStart=" + rangeStart + ", rangeEnd=" - + rangeEnd + ", versionId=" + versionId + ", replaceMetadata=" + replaceMetadata + ", sseCHeader=" + sseCHeader - + ", ifModifiedSince=" + ifModifiedSince + ", ifUnmodifiedSince=" + ifUnmodifiedSince + ", ifMatchTag=" + ifMatchTag - + ", ifNoneMatchTag=" + ifNoneMatchTag + ", imageProcess=" + imageProcess + "]"; + public String toString() { + return "GetObjectRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", rangeStart=" + rangeStart + + ", rangeEnd=" + rangeEnd + ", versionId=" + versionId + ", replaceMetadata=" + replaceMetadata + + ", isEncodeHeaders=" + encodeHeaders + ", sseCHeader=" + sseCHeader + + ", ifModifiedSince=" + ifModifiedSince + ", ifUnmodifiedSince=" + ifUnmodifiedSince + + ", ifMatchTag=" + ifMatchTag + ", ifNoneMatchTag=" + ifNoneMatchTag + + ", imageProcess=" + imageProcess + ", autoUnzipResponse=" + autoUnzipResponse + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/GrantAndPermission.java b/app/src/main/java/com/obs/services/model/GrantAndPermission.java index cf0c833..7281153 100644 --- a/app/src/main/java/com/obs/services/model/GrantAndPermission.java +++ b/app/src/main/java/com/obs/services/model/GrantAndPermission.java @@ -15,108 +15,114 @@ package com.obs.services.model; /** - * Grantee or grantee group and permission information, {@link AccessControlList} + * Grantee or grantee group and permission information, + * {@link AccessControlList} */ -public class GrantAndPermission -{ +public class GrantAndPermission { private GranteeInterface grantee; - + private Permission permission; - + private boolean delivered; - + /** * Constructor * - * @param grantee Grantee (group) name - * @param permission Permission information + * @param grantee + * Grantee (group) name + * @param permission + * Permission information */ - public GrantAndPermission(GranteeInterface grantee, Permission permission) - { + public GrantAndPermission(GranteeInterface grantee, Permission permission) { this.grantee = grantee; this.permission = permission; } - + /** * Obtain the grantee (group) information. * * @return Grantee (group) information */ - public GranteeInterface getGrantee() - { + public GranteeInterface getGrantee() { return grantee; } - + /** * Obtain the permission information. * * @return Permission information */ - public Permission getPermission() - { + public Permission getPermission() { return permission; } - + /** * Check whether the bucket ACL is deliverable. + * * @return Identifier specifying whether the ACL is delivered */ - public boolean isDelivered() { - return delivered; - } - - /** - * Specify whether to deliver the bucket ACL. (This is only applicable to bucket ACLs.) - * @param delivered Whether to deliver the bucket ACL - */ - public void setDelivered(boolean delivered) { - this.delivered = delivered; - } - + public boolean isDelivered() { + return delivered; + } - @Override - public String toString() { - return "GrantAndPermission [grantee=" + grantee + ", permission=" + permission + ", delivered=" + delivered - + "]"; - } + /** + * Specify whether to deliver the bucket ACL. (This is only applicable to + * bucket ACLs.) + * + * @param delivered + * Whether to deliver the bucket ACL + */ + public void setDelivered(boolean delivered) { + this.delivered = delivered; + } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (delivered ? 1231 : 1237); - result = prime * result + ((grantee == null) ? 0 : grantee.hashCode()); - result = prime * result + ((permission == null) ? 0 : permission.hashCode()); - return result; - } + @Override + public String toString() { + return "GrantAndPermission [grantee=" + grantee + ", permission=" + permission + ", delivered=" + delivered + + "]"; + } - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - GrantAndPermission other = (GrantAndPermission) obj; - if (delivered != other.delivered) - return false; - if (grantee == null) { - if (other.grantee != null) - return false; - } else if (!grantee.equals(other.grantee)) - return false; - if (permission == null) { - if (other.permission != null) - return false; - } else if (!permission.equals(other.permission)) - return false; - return true; - } - - - -} + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (delivered ? 1231 : 1237); + result = prime * result + ((grantee == null) ? 0 : grantee.hashCode()); + result = prime * result + ((permission == null) ? 0 : permission.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + GrantAndPermission other = (GrantAndPermission) obj; + if (delivered != other.delivered) { + return false; + } + if (grantee == null) { + if (other.grantee != null) { + return false; + } + } else if (!grantee.equals(other.grantee)) { + return false; + } + if (permission == null) { + if (other.permission != null) { + return false; + } + } else if (!permission.equals(other.permission)) { + return false; + } + return true; + } +} diff --git a/app/src/main/java/com/obs/services/model/GranteeInterface.java b/app/src/main/java/com/obs/services/model/GranteeInterface.java index 78ef963..c998613 100644 --- a/app/src/main/java/com/obs/services/model/GranteeInterface.java +++ b/app/src/main/java/com/obs/services/model/GranteeInterface.java @@ -15,25 +15,23 @@ package com.obs.services.model; /** - * Interface abstraction of the grantee or grantee group in the ACL {@link AccessControlList} + * Interface abstraction of the grantee or grantee group in the ACL + * {@link AccessControlList} */ -public interface GranteeInterface -{ +public interface GranteeInterface { /** * Set the identifier marking the grantee (group). * - * @param id Identifier marking the grantee (group) + * @param id + * Identifier marking the grantee (group) */ public void setIdentifier(String id); - + /** * Obtain the identifier marking the grantee (group). * * @return Identifier marking the grantee (group) */ public String getIdentifier(); - - -} - +} diff --git a/app/src/main/java/com/obs/services/model/GroupGrantee.java b/app/src/main/java/com/obs/services/model/GroupGrantee.java index f039bb7..df19929 100644 --- a/app/src/main/java/com/obs/services/model/GroupGrantee.java +++ b/app/src/main/java/com/obs/services/model/GroupGrantee.java @@ -1,25 +1,24 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ package com.obs.services.model; /** * Grantee group information in the ACL, {@link AccessControlList} */ -public class GroupGrantee implements GranteeInterface -{ - +public class GroupGrantee implements GranteeInterface { + /** * Anonymous user group, indicating all users */ @@ -34,92 +33,87 @@ public class GroupGrantee implements GranteeInterface /** * Log delivery group, indicating common users who can configure access logs */ - @Deprecated public static final GroupGrantee LOG_DELIVERY = new GroupGrantee(GroupGranteeEnum.LOG_DELIVERY); - private GroupGranteeEnum groupGranteeType; - - public GroupGrantee() - { + + public GroupGrantee() { } - + /** * Constructor - * @param uri URI for the grantee group + * + * @param uri + * URI for the grantee group */ - public GroupGrantee(String uri) - { + public GroupGrantee(String uri) { this.groupGranteeType = GroupGranteeEnum.getValueFromCode(uri); } - - public GroupGrantee(GroupGranteeEnum groupGranteeType) - { + + public GroupGrantee(GroupGranteeEnum groupGranteeType) { this.groupGranteeType = groupGranteeType; } - + /** * Set the URI for the grantee group. - * @param uri URI for the grantee group + * + * @param uri + * URI for the grantee group */ @Override - public void setIdentifier(String uri) - { + public void setIdentifier(String uri) { this.groupGranteeType = GroupGranteeEnum.getValueFromCode(uri); } - + /** * Obtain the URI of the grantee group. * * @return URI of the grantee group. */ @Override - public String getIdentifier() - { + public String getIdentifier() { return this.groupGranteeType == null ? null : this.groupGranteeType.getCode(); } - + /** * Obtain type of the grantee group. + * * @return Type of the grantee group */ public GroupGranteeEnum getGroupGranteeType() { - return this.groupGranteeType; + return this.groupGranteeType; } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((groupGranteeType == null) ? 0 : groupGranteeType.hashCode()); - return result; - } + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((groupGranteeType == null) ? 0 : groupGranteeType.hashCode()); + return result; + } - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - GroupGrantee other = (GroupGrantee) obj; - if (groupGranteeType != other.groupGranteeType) - return false; - return true; - } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + GroupGrantee other = (GroupGrantee) obj; + return groupGranteeType == other.groupGranteeType; + } - /** - * Return the object description. + /** + * Return the object description. * * @return Object description */ @Override - public String toString() - { + public String toString() { return "GroupGrantee [" + groupGranteeType + "]"; } } - - diff --git a/app/src/main/java/com/obs/services/model/GroupGranteeEnum.java b/app/src/main/java/com/obs/services/model/GroupGranteeEnum.java index 437f04e..e162857 100644 --- a/app/src/main/java/com/obs/services/model/GroupGranteeEnum.java +++ b/app/src/main/java/com/obs/services/model/GroupGranteeEnum.java @@ -1,23 +1,23 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ package com.obs.services.model; import com.obs.services.internal.Constants; /** - * Type of the user group + * Type of the user group * */ public enum GroupGranteeEnum { @@ -29,29 +29,25 @@ public enum GroupGranteeEnum { /** * OBS authorized user group, indicating all users who own OBS accounts */ - @Deprecated - AUTHENTICATED_USERS, + @Deprecated AUTHENTICATED_USERS, /** * Log delivery group, indicating common users who can configure access logs */ - @Deprecated LOG_DELIVERY; - - public String getCode() { - return this.name(); - } - public static GroupGranteeEnum getValueFromCode(String code) { - if("Everyone".equals(code) || Constants.ALL_USERS_URI.equals(code)) { - return ALL_USERS; - }else if(Constants.AUTHENTICATED_USERS_URI.equals(code)) { - return AUTHENTICATED_USERS; - }else if(Constants.LOG_DELIVERY_URI.equals(code)) { - return LOG_DELIVERY; - } - return null; - } + public String getCode() { + return this.name(); + } + + public static GroupGranteeEnum getValueFromCode(String code) { + if ("Everyone".equals(code) || Constants.ALL_USERS_URI.equals(code)) { + return ALL_USERS; + } else if (Constants.AUTHENTICATED_USERS_URI.equals(code)) { + return AUTHENTICATED_USERS; + } else if (Constants.LOG_DELIVERY_URI.equals(code)) { + return LOG_DELIVERY; + } + return null; + } } - - diff --git a/app/src/main/java/com/obs/services/model/HeaderResponse.java b/app/src/main/java/com/obs/services/model/HeaderResponse.java index 7528c8f..11a758c 100644 --- a/app/src/main/java/com/obs/services/model/HeaderResponse.java +++ b/app/src/main/java/com/obs/services/model/HeaderResponse.java @@ -11,58 +11,71 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; import java.util.Map; import java.util.TreeMap; import com.obs.services.internal.Constants; -import com.obs.services.internal.InternalHeaderResponse; +import com.obs.services.internal.service.InternalHeaderResponse; /** * Public response result, including the request ID and response headers * */ -public class HeaderResponse extends InternalHeaderResponse -{ - - public HeaderResponse(){ - +public class HeaderResponse extends InternalHeaderResponse { + + public HeaderResponse() { + } - + /** * Obtain response headers. + * * @return Response headers */ - public Map getResponseHeaders() - { - if(responseHeaders == null){ + public Map getResponseHeaders() { + if (responseHeaders == null) { responseHeaders = new TreeMap(String.CASE_INSENSITIVE_ORDER); } return responseHeaders; } - - /** - * Obtain the request ID returned by the server. - * @return Request ID returned by the server - */ - public String getRequestId() { - Object id = this.getResponseHeaders().get(Constants.REQUEST_ID_HEADER); - return id == null ? "" : id.toString(); - } - - /** - * Obtain the HTTP status code returned by the server. - * @return HTTP status code returned by the server - */ - public int getStatusCode() { - return statusCode; - } + /** + * Obtain original response headers. + * + * @return original response headers + */ + public Map getOriginalHeaders() { + if (originalHeaders == null) { + originalHeaders = new TreeMap(String.CASE_INSENSITIVE_ORDER); + } + return originalHeaders; + } + + /** + * Obtain the request ID returned by the server. + * + * @return Request ID returned by the server + */ + public String getRequestId() { + Object id = this.getResponseHeaders().get(Constants.REQUEST_ID_HEADER); + return id == null ? "" : id.toString(); + } + + /** + * Obtain the HTTP status code returned by the server. + * + * @return HTTP status code returned by the server + */ + public int getStatusCode() { + return statusCode; + } + + @Override + public String toString() { + return "HeaderResponse [responseHeaders=" + responseHeaders + ", statusCode=" + statusCode + "]"; + } - @Override - public String toString() { - return "HeaderResponse [responseHeaders=" + responseHeaders + ", statusCode=" + statusCode + "]"; - } - } diff --git a/app/src/main/java/com/obs/services/model/HistoricalObjectReplicationEnum.java b/app/src/main/java/com/obs/services/model/HistoricalObjectReplicationEnum.java new file mode 100644 index 0000000..5f1a70d --- /dev/null +++ b/app/src/main/java/com/obs/services/model/HistoricalObjectReplicationEnum.java @@ -0,0 +1,52 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** +* +* Keyword for replicating historical objects. If the value is Enabled, +* historical objects that meet the rule are replicated. +* +*/ +public enum HistoricalObjectReplicationEnum { + /** + * Enable the rule. + */ + ENABLED("Enabled"), + + /** + * Disable the rule. + */ + DISABLED("Disabled"); + + private String code; + + private HistoricalObjectReplicationEnum(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + + public static HistoricalObjectReplicationEnum getValueFromCode(String code) { + for (HistoricalObjectReplicationEnum val : HistoricalObjectReplicationEnum.values()) { + if (val.code.equals(code)) { + return val; + } + } + return null; + } +} diff --git a/app/src/main/java/com/obs/services/model/HttpMethodEnum.java b/app/src/main/java/com/obs/services/model/HttpMethodEnum.java index cf8593f..a740930 100644 --- a/app/src/main/java/com/obs/services/model/HttpMethodEnum.java +++ b/app/src/main/java/com/obs/services/model/HttpMethodEnum.java @@ -17,8 +17,7 @@ /** * HTTP/HTTPS request method */ -public enum HttpMethodEnum -{ +public enum HttpMethodEnum { /** * GET method, normally used for query */ @@ -48,40 +47,31 @@ public enum HttpMethodEnum * OPTIONS method, normally used for preflight */ OPTIONS("Options"); - + private String operationType; - - private HttpMethodEnum(String operationType) - { - if (operationType == null) - { + + private HttpMethodEnum(String operationType) { + if (operationType == null) { throw new IllegalArgumentException("operation type code is null"); } this.operationType = operationType; } - - public String getOperationType() - { + + public String getOperationType() { return this.operationType.toUpperCase(); } - - public static HttpMethodEnum getValueFromStringCode(String operationType) - { - if (operationType == null) - { + + public static HttpMethodEnum getValueFromStringCode(String operationType) { + if (operationType == null) { throw new IllegalArgumentException("operation type is null"); } - - for (HttpMethodEnum installMode : HttpMethodEnum.values()) - { - if (installMode.getOperationType().equals(operationType.toUpperCase())) - { + + for (HttpMethodEnum installMode : HttpMethodEnum.values()) { + if (installMode.getOperationType().equals(operationType.toUpperCase())) { return installMode; } } - + throw new IllegalArgumentException("operation type is illegal"); } } - - diff --git a/app/src/main/java/com/obs/services/model/HttpProtocolTypeEnum.java b/app/src/main/java/com/obs/services/model/HttpProtocolTypeEnum.java index 4952a3f..c82757d 100644 --- a/app/src/main/java/com/obs/services/model/HttpProtocolTypeEnum.java +++ b/app/src/main/java/com/obs/services/model/HttpProtocolTypeEnum.java @@ -11,40 +11,40 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; /** * HTTP type * */ -public enum HttpProtocolTypeEnum{ - - /** - * HTTP 1.1 - */ - HTTP1_1("http1.1"), - /** - * HTTP 2.0 - */ - HTTP2_0("http2.0"); - - private String code; - - private HttpProtocolTypeEnum(String code) { - this.code = code; - } - - public String getCode() { - return code; - } - - public static HttpProtocolTypeEnum getValueFromCode(String code) { - for (HttpProtocolTypeEnum val : HttpProtocolTypeEnum.values()) { - if (val.code.equals(code)) { - return val; - } - } - return HTTP1_1; - } - +public enum HttpProtocolTypeEnum { + + /** + * HTTP 1.1 + */ + HTTP1_1("http1.1"), /** + * HTTP 2.0 + */ + HTTP2_0("http2.0"); + + private String code; + + private HttpProtocolTypeEnum(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + + public static HttpProtocolTypeEnum getValueFromCode(String code) { + for (HttpProtocolTypeEnum val : HttpProtocolTypeEnum.values()) { + if (val.code.equals(code)) { + return val; + } + } + return HTTP1_1; + } + } \ No newline at end of file diff --git a/app/src/main/java/com/obs/services/model/ISecurityKey.java b/app/src/main/java/com/obs/services/model/ISecurityKey.java index 9f00f5a..99b032c 100644 --- a/app/src/main/java/com/obs/services/model/ISecurityKey.java +++ b/app/src/main/java/com/obs/services/model/ISecurityKey.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; public interface ISecurityKey { diff --git a/app/src/main/java/com/obs/services/model/InitiateMultipartUploadRequest.java b/app/src/main/java/com/obs/services/model/InitiateMultipartUploadRequest.java index 7b01532..9ecc913 100644 --- a/app/src/main/java/com/obs/services/model/InitiateMultipartUploadRequest.java +++ b/app/src/main/java/com/obs/services/model/InitiateMultipartUploadRequest.java @@ -1,148 +1,158 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + **/ package com.obs.services.model; /** * Parameters in a request for initializing a multipart upload */ -public class InitiateMultipartUploadRequest extends PutObjectBasicRequest -{ +public class InitiateMultipartUploadRequest extends PutObjectBasicRequest { + + { + httpMethod = HttpMethodEnum.POST; + } + private ObjectMetadata metadata; - + private int expires; - - public InitiateMultipartUploadRequest(){ - + + private String encodingType; + + public InitiateMultipartUploadRequest() { + } - + /** * Constructor - * @param bucketName Name of the bucket to which the multipart upload belongs - @param objectKey Name of the object involved in the multipart upload + * + * @param bucketName + * Name of the bucket to which the multipart upload belongs + * @param objectKey + * Name of the object involved in the multipart upload */ - public InitiateMultipartUploadRequest(String bucketName, String objectKey) - { + public InitiateMultipartUploadRequest(String bucketName, String objectKey) { this.bucketName = bucketName; this.objectKey = objectKey; } /** - * Obtain the expiration time of the object generated after the multipart upload is complete. - * @return Expiration time of the object + * Constructor + * + * @param bucketName + * Name of the bucket to which the multipart upload belongs + * @param objectKey + * Name of the object involved in the multipart upload + * @param encodingType + * Use this encoding type to encode keys that contains invalid characters, the value could be "url" */ - public int getExpires() { - return expires; - } - - /** - * Set the expiration time of the object generated after the multipart upload is complete. The value must be an integer. - * @param expires Expiration time of the object - */ - public void setExpires(int expires) { - this.expires = expires; - } - + public InitiateMultipartUploadRequest(String bucketName, String objectKey, String encodingType) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.encodingType = encodingType; + } + /** - * Obtain the name of the bucket to which the multipart upload belongs. - * - * @return Name of the bucket to which the multipart upload belongs + * Obtain the expiration time of the object generated after the multipart + * upload is complete. + * + * @return Expiration time of the object */ - public String getBucketName() - { - return bucketName; + public int getExpires() { + return expires; } - + /** - * Set the name for the bucket to which the multipart upload belongs. - * - * @param bucketName Name of the bucket to which the multipart upload belongs + * Set the expiration time of the object generated after the multipart + * upload is complete. The value must be an integer. + * + * @param expires + * Expiration time of the object */ - public void setBucketName(String bucketName) - { - this.bucketName = bucketName; + public void setExpires(int expires) { + this.expires = expires; } - + /** - * Obtain the name of the object involved in the multipart upload. - * - * @return Name of the object involved in the multipart upload + * Set encoding type to encode objectkeys, the value could be url + * + * @param encodingType + * encoding type for encoding */ - public String getObjectKey() - { - return objectKey; + public void setEncodingType(String encodingType) { + this.encodingType = encodingType; } - + /** - * Set the name for the object involved in the multipart upload. - * - @param objectKey Name of the object involved in the multipart upload + * Get encoding type to encode objectkeys + * + * @return encoding type for encoding */ - public void setObjectKey(String objectKey) - { - this.objectKey = objectKey; + public String getEncodingType() { + return encodingType; } - + /** - * Set the redirection link which can redirect the request to another object in the bucket or to an external URL. - * + * Set the redirection link which can redirect the request to another object + * in the bucket or to an external URL. + * * @return Redirection link */ @Deprecated - public String getWebSiteRedirectLocation() - { + public String getWebSiteRedirectLocation() { return this.metadata != null ? this.metadata.getWebSiteRedirectLocation() : null; } - + /** - * Obtain the redirection link which can redirect the request to another object in the bucket or to an external URL. - * - * @param webSiteRedirectLocation Redirection link + * Obtain the redirection link which can redirect the request to another + * object in the bucket or to an external URL. + * + * @param webSiteRedirectLocation + * Redirection link */ @Deprecated - public void setWebSiteRedirectLocation(String webSiteRedirectLocation) - { - if(this.metadata != null) { - this.metadata.setWebSiteRedirectLocation(webSiteRedirectLocation); + public void setWebSiteRedirectLocation(String webSiteRedirectLocation) { + if (this.metadata != null) { + this.metadata.setWebSiteRedirectLocation(webSiteRedirectLocation); } } - - /** - * Set object properties, including customized metadata. "content-type" is supported. + + /** + * Set object properties, including customized metadata. "content-type" is + * supported. + * * @return Object properties */ - public ObjectMetadata getMetadata() - { + public ObjectMetadata getMetadata() { return metadata; } /** - * Obtain object properties, including customized metadata. "content-type" is supported. - * @param metadata Object properties + * Obtain object properties, including customized metadata. "content-type" + * is supported. + * + * @param metadata + * Object properties */ - public void setMetadata(ObjectMetadata metadata) - { + public void setMetadata(ObjectMetadata metadata) { this.metadata = metadata; } - @Override - public String toString() { - return "InitiateMultipartUploadRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", acl=" + acl - + ", sseKmsHeader=" + sseKmsHeader + ", sseCHeader=" + sseCHeader + ", metadata=" + metadata - + ", expires=" + expires + "]"; - } - -} - + @Override + public String toString() { + return "InitiateMultipartUploadRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", acl=" + acl + + ", sseKmsHeader=" + sseKmsHeader + ", sseCHeader=" + sseCHeader + ", metadata=" + metadata + + ", expires=" + expires + ", encodingType=" + encodingType + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/InitiateMultipartUploadResult.java b/app/src/main/java/com/obs/services/model/InitiateMultipartUploadResult.java index 508e51b..9ddf5c7 100644 --- a/app/src/main/java/com/obs/services/model/InitiateMultipartUploadResult.java +++ b/app/src/main/java/com/obs/services/model/InitiateMultipartUploadResult.java @@ -17,62 +17,50 @@ /** * Response to a request for initializing a multipart upload */ -public class InitiateMultipartUploadResult extends HeaderResponse -{ +public class InitiateMultipartUploadResult extends HeaderResponse { private String uploadId; - + private String bucketName; - + private String objectKey; - - - public InitiateMultipartUploadResult(String bucketName, String objectKey, String uploadId) - { + + public InitiateMultipartUploadResult(String bucketName, String objectKey, String uploadId) { this.bucketName = bucketName; this.objectKey = objectKey; this.uploadId = uploadId; } - + /** * Obtain the multipart upload ID. * * @return Multipart upload ID */ - public String getUploadId() - { + public String getUploadId() { return uploadId; } - - + /** * Obtain the name of the bucket to which the multipart upload belongs. * * @return Name of the bucket to which the multipart upload belongs */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } - - + /** * Obtain the name of the object involved in the multipart upload. * * @return Name of the object involved in the multipart upload */ - public String getObjectKey() - { + public String getObjectKey() { return objectKey; } - - @Override - public String toString() - { - return "InitiateMultipartUploadResult [uploadId=" + uploadId + ", bucketName=" + bucketName + ", objectKey=" + objectKey + "]"; + public String toString() { + return "InitiateMultipartUploadResult [uploadId=" + uploadId + ", bucketName=" + bucketName + ", objectKey=" + + objectKey + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/KeyAndVersion.java b/app/src/main/java/com/obs/services/model/KeyAndVersion.java index d48c6b2..a537e96 100644 --- a/app/src/main/java/com/obs/services/model/KeyAndVersion.java +++ b/app/src/main/java/com/obs/services/model/KeyAndVersion.java @@ -17,73 +17,74 @@ /** * Information about the versioning object to be deleted */ -public class KeyAndVersion -{ +public class KeyAndVersion { private String key; private String version; - + /** * Constructor - * @param key Object name - * @param version Version ID of the object + * + * @param key + * Object name + * @param version + * Version ID of the object */ - public KeyAndVersion(String key, String version) - { + public KeyAndVersion(String key, String version) { this.key = key; this.version = version; } - + /** * Constructor - * @param key Object name + * + * @param key + * Object name */ - public KeyAndVersion(String key) - { + public KeyAndVersion(String key) { this(key, null); } - + /** * Obtain the object name. + * * @return Object name */ - public String getKey() - { + public String getKey() { return key; } - + /** * Obtain the object version ID. + * * @return Version ID of the object */ - public String getVersion() - { + public String getVersion() { return version; } - + /** * Set the object name. - * @param key Object name + * + * @param key + * Object name */ - public void setKey(String key) - { + public void setKey(String key) { this.key = key; } - + /** - * Set the version ID of the object. - * @param version Version ID of the object + * Set the version ID of the object. + * + * @param version + * Version ID of the object */ - public void setVersion(String version) - { + public void setVersion(String version) { this.version = version; } @Override - public String toString() - { + public String toString() { return "KeyAndVersion [key=" + key + ", version=" + version + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/LifecycleConfiguration.java b/app/src/main/java/com/obs/services/model/LifecycleConfiguration.java index 23cdb40..34ac942 100644 --- a/app/src/main/java/com/obs/services/model/LifecycleConfiguration.java +++ b/app/src/main/java/com/obs/services/model/LifecycleConfiguration.java @@ -18,268 +18,287 @@ import java.util.Date; import java.util.List; +import com.obs.services.internal.utils.ObjectUtils; import com.obs.services.internal.utils.ServiceUtils; /** * Bucket lifecycle rules */ -public class LifecycleConfiguration extends HeaderResponse -{ +public class LifecycleConfiguration extends HeaderResponse { private List rules; - + /** * Constructor - * @param rules List of bucket lifecycle rules + * + * @param rules + * List of bucket lifecycle rules */ - public LifecycleConfiguration(List rules) - { + public LifecycleConfiguration(List rules) { this.rules = rules; } - - public LifecycleConfiguration() - { + + public LifecycleConfiguration() { } - + /** * Obtain the list of bucket lifecycle rules. + * * @return List of bucket lifecycle rules */ - public List getRules() - { - if(this.rules == null) { - this.rules = new ArrayList(); - } + public List getRules() { + if (this.rules == null) { + this.rules = new ArrayList(); + } return rules; } - + /** * Add a lifecycle rule. - * @param rule Lifecycle rule + * + * @param rule + * Lifecycle rule */ - public void addRule(Rule rule) - { - if(!getRules().contains(rule)){ - getRules().add(rule); + public void addRule(Rule rule) { + if (!getRules().contains(rule)) { + getRules().add(rule); } } - + /** * Create and add a lifecycle rule. - * @param id Rule ID - * @param prefix Object name prefix identifying one or more objects to which the rule applies - * @param enabled Identifier that specifies whether the rule is enabled + * + * @param id + * Rule ID + * @param prefix + * Object name prefix identifying one or more objects to which + * the rule applies + * @param enabled + * Identifier that specifies whether the rule is enabled * @return rule Lifecycle rule */ - public Rule newRule(String id, String prefix, Boolean enabled) - { + public Rule newRule(String id, String prefix, Boolean enabled) { Rule rule = this.new Rule(id, prefix, enabled); getRules().add(rule); return rule; } - - public static void setDays(TimeEvent timeEvent, Integer days){ - if(timeEvent != null){ + + public static void setDays(TimeEvent timeEvent, Integer days) { + if (timeEvent != null) { timeEvent.days = days; } } - - public static void setDate(TimeEvent timeEvent, Date date){ - if(timeEvent != null){ + + public static void setDate(TimeEvent timeEvent, Date date) { + if (timeEvent != null) { timeEvent.date = date; } } - - public static void setStorageClass(TimeEvent timeEvent, StorageClassEnum storageClass){ - if(timeEvent != null){ + + public static void setStorageClass(TimeEvent timeEvent, StorageClassEnum storageClass) { + if (timeEvent != null) { timeEvent.storageClass = storageClass; } } - - public abstract class TimeEvent - { + + public abstract class TimeEvent { protected Integer days; - + protected Date date; - + protected StorageClassEnum storageClass; - - public TimeEvent() - { + + public TimeEvent() { } - - protected TimeEvent(Integer days) - { + + protected TimeEvent(Integer days) { this.days = days; } - - protected TimeEvent(Date date) - { + + protected TimeEvent(Date date) { this.date = date; } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + getOuterType().hashCode(); - result = prime * result + ((date == null) ? 0 : date.hashCode()); - result = prime * result + ((days == null) ? 0 : days.hashCode()); - result = prime * result + ((storageClass == null) ? 0 : storageClass.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - TimeEvent other = (TimeEvent) obj; - if (!getOuterType().equals(other.getOuterType())) - return false; - if (date == null) { - if (other.date != null) - return false; - } else if (!date.equals(other.date)) - return false; - if (days == null) { - if (other.days != null) - return false; - } else if (!days.equals(other.days)) - return false; - if (storageClass != other.storageClass) - return false; - return true; - } - - private LifecycleConfiguration getOuterType() { - return LifecycleConfiguration.this; - } - + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + getOuterType().hashCode(); + result = prime * result + ((date == null) ? 0 : date.hashCode()); + result = prime * result + ((days == null) ? 0 : days.hashCode()); + result = prime * result + ((storageClass == null) ? 0 : storageClass.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + TimeEvent other = (TimeEvent) obj; + if (!getOuterType().equals(other.getOuterType())) { + return false; + } + if (date == null) { + if (other.date != null) { + return false; + } + } else if (!date.equals(other.date)) { + return false; + } + if (days == null) { + if (other.days != null) { + return false; + } + } else if (!days.equals(other.days)) { + return false; + } + if (storageClass != other.storageClass) { + return false; + } + return true; + } + + private LifecycleConfiguration getOuterType() { + return LifecycleConfiguration.this; + } + } - + /** * Expiration time of a noncurrent object version * */ - public class NoncurrentVersionExpiration extends TimeEvent - { - - public NoncurrentVersionExpiration() - { + public class NoncurrentVersionExpiration extends TimeEvent { + + public NoncurrentVersionExpiration() { } - + /** * Constructor - * @param days Expiration time of the noncurrent object version, which indicates the number of days after which the object expires since it becomes a noncurrent version + * + * @param days + * Expiration time of the noncurrent object version, which + * indicates the number of days after which the object + * expires since it becomes a noncurrent version */ - public NoncurrentVersionExpiration(Integer days) - { + public NoncurrentVersionExpiration(Integer days) { this.setDays(days); } /** * Obtain the expiration time of a noncurrent object version. - * @return Expiration time of the noncurrent object version, which indicates the number of days after which the object expires since it becomes a noncurrent version + * + * @return Expiration time of the noncurrent object version, which + * indicates the number of days after which the object expires + * since it becomes a noncurrent version */ - public Integer getDays() - { + public Integer getDays() { return days; } /** * Set the expiration time of a noncurrent object version. - * @param days Expiration time of the noncurrent object version, which indicates the number of days after which the object expires since it becomes a noncurrent version + * + * @param days + * Expiration time of the noncurrent object version, which + * indicates the number of days after which the object + * expires since it becomes a noncurrent version */ - public void setDays(Integer days) - { + public void setDays(Integer days) { this.days = days; } @Override - public String toString() - { + public String toString() { return "NoncurrentVersionExpiration [days=" + days + "]"; } - + } - + /** * Expiration time of an object */ - public class Expiration extends TimeEvent - { - - public Expiration() - { + public class Expiration extends TimeEvent { + + public Expiration() { } - + /** * Constructor - * @param date A specified date in which the object will expire + * + * @param date + * A specified date in which the object will expire */ - public Expiration(Date date) - { + public Expiration(Date date) { super(date); } - + /** * Constructor - * @param days Object expiration time, specifying how many days after creation will the object expire + * + * @param days + * Object expiration time, specifying how many days after + * creation will the object expire */ - public Expiration(Integer days) - { + public Expiration(Integer days) { super(days); } - + /** * Obtain the expiration time of the object. - * @return Object expiration time, specifying how many days after creation will the object expire + * + * @return Object expiration time, specifying how many days after + * creation will the object expire */ - public Integer getDays() - { + public Integer getDays() { return days; } - + /** * Set the object expiration time. - * @param days Object expiration time, specifying how many days after creation will the object expire + * + * @param days + * Object expiration time, specifying how many days after + * creation will the object expire */ - public void setDays(Integer days) - { + public void setDays(Integer days) { this.days = days; this.date = null; } - + /** * Obtain the object expiration date. + * * @return A specified date in which the object will expire */ - public Date getDate() - { + public Date getDate() { return ServiceUtils.cloneDateIgnoreNull(this.date); } - + /** * Obtain the object expiration date. - * @param date A specified date in which the object will expire + * + * @param date + * A specified date in which the object will expire */ - public void setDate(Date date) - { + public void setDate(Date date) { this.date = ServiceUtils.cloneDateIgnoreNull(date); this.days = null; } @Override - public String toString() - { + public String toString() { return "Expiration [days=" + days + ", date=" + date + "]"; } - + } - + /** * Object transition policy * @@ -289,22 +308,30 @@ public class Transition extends TimeEvent { public Transition() { super(); } - + /** * Constructor - * @param date Date when the object is transited - * @param storageClass Storage class of the object after it is transited. Possible values are "WARM" and "COLD". + * + * @param date + * Date when the object is transited + * @param storageClass + * Storage class of the object after it is transited. + * Possible values are "WARM" and "COLD". */ @Deprecated public Transition(Date date, String storageClass) { super(date); this.storageClass = StorageClassEnum.getValueFromCode(storageClass); } - + /** * Constructor - * @param date Date when the object is transited - * @param storageClass Storage class of the object after it is transited. Possible values are "WARM" and "COLD". + * + * @param date + * Date when the object is transited + * @param storageClass + * Storage class of the object after it is transited. + * Possible values are "WARM" and "COLD". */ public Transition(Date date, StorageClassEnum storageClass) { super(date); @@ -313,19 +340,31 @@ public Transition(Date date, StorageClassEnum storageClass) { /** * Constructor - * @param days Object transition time, which indicates the number of days when the object is automatically transited after being created. - * @param storageClass Storage class of the object after it is transited. Possible values are "WARM" and "COLD". + * + * @param days + * Object transition time, which indicates the number of days + * when the object is automatically transited after being + * created. + * @param storageClass + * Storage class of the object after it is transited. + * Possible values are "WARM" and "COLD". */ @Deprecated public Transition(Integer days, String storageClass) { super(days); this.storageClass = StorageClassEnum.getValueFromCode(storageClass); } - + /** * Constructor - * @param days Object transition time, which indicates the number of days when the object is automatically transited after being created. - * @param storageClass Storage class of the object after it is transited. Possible values are "WARM" and "COLD". + * + * @param days + * Object transition time, which indicates the number of days + * when the object is automatically transited after being + * created. + * @param storageClass + * Storage class of the object after it is transited. + * Possible values are "WARM" and "COLD". */ public Transition(Integer days, StorageClassEnum storageClass) { super(days); @@ -334,6 +373,7 @@ public Transition(Integer days, StorageClassEnum storageClass) { /** * Obtain the storage class of the object after transition. + * * @return Storage class of the object after transition * @see #getObjectStorageClass() */ @@ -344,16 +384,19 @@ public String getStorageClass() { /** * Set the storage class of the object after transition. - * @param storageClass Storage class of the object after transition + * + * @param storageClass + * Storage class of the object after transition * @see #setObjectStorageClass(StorageClassEnum storageClass) */ @Deprecated public void setStorageClass(String storageClass) { this.storageClass = StorageClassEnum.getValueFromCode(storageClass); } - + /** * Obtain the storage class of the object after transition. + * * @return Storage class of the object after transition */ public StorageClassEnum getObjectStorageClass() { @@ -362,335 +405,376 @@ public StorageClassEnum getObjectStorageClass() { /** * Set the storage class of the object after transition. - * @param storageClass Storage class of the object after transition + * + * @param storageClass + * Storage class of the object after transition */ public void setObjectStorageClass(StorageClassEnum storageClass) { this.storageClass = storageClass; } - + /** * Obtain the object transition time. - * @return Object transition time, which indicates the number of days when the object is automatically transited after being created. + * + * @return Object transition time, which indicates the number of days + * when the object is automatically transited after being + * created. */ - public Integer getDays() - { + public Integer getDays() { return days; } - + /** * Set the object transition time. - * @param days Object transition time, which indicates the number of days when the object is automatically transited after being created. + * + * @param days + * Object transition time, which indicates the number of days + * when the object is automatically transited after being + * created. */ - public void setDays(Integer days) - { + public void setDays(Integer days) { this.days = days; this.date = null; } - + /** - * Obtain the object transition date. + * Obtain the object transition date. + * * @return Date when the object is transited */ - public Date getDate() - { + public Date getDate() { return ServiceUtils.cloneDateIgnoreNull(this.date); } - + /** * Set the object transition date. - * @param date Date when the object is transited + * + * @param date + * Date when the object is transited */ - public void setDate(Date date) - { + public void setDate(Date date) { this.date = ServiceUtils.cloneDateIgnoreNull(date); this.days = null; } @Override - public String toString() - { + public String toString() { return "Transition [days=" + days + ", date=" + date + ", storageClass=" + storageClass + "]"; } } - + /** * Transition policy for noncurrent versions * */ - public class NoncurrentVersionTransition extends TimeEvent - { - public NoncurrentVersionTransition() - { + public class NoncurrentVersionTransition extends TimeEvent { + public NoncurrentVersionTransition() { } - + /** * Constructor - * @param days Transition time of the noncurrent object version, which indicates the number of days after which the object will be transit since it becomes a noncurrent version - * @param storageClass Storage class of the noncurrent object version after transition + * + * @param days + * Transition time of the noncurrent object version, which + * indicates the number of days after which the object will + * be transit since it becomes a noncurrent version + * @param storageClass + * Storage class of the noncurrent object version after + * transition */ @Deprecated - public NoncurrentVersionTransition(Integer days, String storageClass) - { + public NoncurrentVersionTransition(Integer days, String storageClass) { this.setDays(days); this.storageClass = StorageClassEnum.getValueFromCode(storageClass); } - + /** * Constructor - * @param days Transition time of the noncurrent object version, which indicates the number of days after which the object will be transit since it becomes a noncurrent version - * @param storageClass Storage class of the noncurrent object version after transition - */ - public NoncurrentVersionTransition(Integer days, StorageClassEnum storageClass) - { + * + * @param days + * Transition time of the noncurrent object version, which + * indicates the number of days after which the object will + * be transit since it becomes a noncurrent version + * @param storageClass + * Storage class of the noncurrent object version after + * transition + */ + public NoncurrentVersionTransition(Integer days, StorageClassEnum storageClass) { this.setDays(days); this.storageClass = storageClass; } /** * Obtain the transition time of a noncurrent object version. - * @return Transition time of the noncurrent object version, which indicates the number of days after which the object will be transit since it becomes a noncurrent version + * + * @return Transition time of the noncurrent object version, which + * indicates the number of days after which the object will be + * transit since it becomes a noncurrent version */ - public Integer getDays() - { + public Integer getDays() { return days; } /** * Set the transition time of a noncurrent object version. - * @param days Transition time of the noncurrent object version, which indicates the number of days after which the object will be transit since it becomes a noncurrent version + * + * @param days + * Transition time of the noncurrent object version, which + * indicates the number of days after which the object will + * be transit since it becomes a noncurrent version */ - public void setDays(Integer days) - { + public void setDays(Integer days) { this.days = days; } - + /** - * Obtain the storage class of the noncurrent object version after transition. - * @return Storage class of the noncurrent object version after transition + * Obtain the storage class of the noncurrent object version after + * transition. + * + * @return Storage class of the noncurrent object version after + * transition * @see #getObjectStorageClass() */ @Deprecated public String getStorageClass() { return storageClass != null ? this.storageClass.getCode() : null; } - + /** - * Set the storage class of the noncurrent object version after transition. - * @param storageClass Storage class of the noncurrent object version after transition + * Set the storage class of the noncurrent object version after + * transition. + * + * @param storageClass + * Storage class of the noncurrent object version after + * transition * @see #setObjectStorageClass(StorageClassEnum storageClass) */ @Deprecated public void setStorageClass(String storageClass) { this.storageClass = StorageClassEnum.getValueFromCode(storageClass); } - + /** - * Obtain the storage class of the noncurrent object version after transition. - * @return Storage class of the noncurrent object version after transition + * Obtain the storage class of the noncurrent object version after + * transition. + * + * @return Storage class of the noncurrent object version after + * transition */ public StorageClassEnum getObjectStorageClass() { return storageClass; } - + /** - * Set the storage class of the noncurrent object version after transition. - * @param storageClass Storage class of the noncurrent object version after transition + * Set the storage class of the noncurrent object version after + * transition. + * + * @param storageClass + * Storage class of the noncurrent object version after + * transition */ public void setObjectStorageClass(StorageClassEnum storageClass) { this.storageClass = storageClass; } @Override - public String toString() - { + public String toString() { return "NoncurrentVersionTransition [days=" + days + ", storageClass=" + storageClass + "]"; } - + } - - + /** * Bucket lifecycle rule */ - public class Rule - { + public class Rule { protected String id; - + protected String prefix; - + protected Boolean enabled; - + protected Expiration expiration; - + protected NoncurrentVersionExpiration noncurrentVersionExpiration; - + protected List transitions; - + protected List noncurrentVersionTransitions; - + /** * No-argument constructor */ - public Rule() - { + public Rule() { } - + /** - * @param id Rule ID - * @param prefix Object name prefix identifying one or more objects to which the rule applies - * @param enabled Identifier that specifies whether the rule is enabled + * @param id + * Rule ID + * @param prefix + * Object name prefix identifying one or more objects to + * which the rule applies + * @param enabled + * Identifier that specifies whether the rule is enabled */ - public Rule(String id, String prefix, Boolean enabled) - { + public Rule(String id, String prefix, Boolean enabled) { this.id = id; this.prefix = prefix; this.enabled = enabled; } - + /** * Expiration time of objects + * * @return Instance of expiration time configuration */ - public Expiration newExpiration() - { + public Expiration newExpiration() { this.expiration = new Expiration(); return this.expiration; } - + /** * Create the expiration time of a noncurrent object version. + * * @return Expiration time of a noncurrent object version */ - public NoncurrentVersionExpiration newNoncurrentVersionExpiration() - { + public NoncurrentVersionExpiration newNoncurrentVersionExpiration() { this.noncurrentVersionExpiration = new NoncurrentVersionExpiration(); return this.noncurrentVersionExpiration; } - + /** * Create an object transition policy. + * * @return Object transition policy */ - public Transition newTransition() - { - if(this.transitions == null){ + public Transition newTransition() { + if (this.transitions == null) { this.transitions = new ArrayList(); } Transition t = new Transition(); this.transitions.add(t); return t; } - + /** * Create the transition policy for noncurrent versions. + * * @return Transition policy for noncurrent versions */ - public NoncurrentVersionTransition newNoncurrentVersionTransition() - { - if(this.noncurrentVersionTransitions == null){ + public NoncurrentVersionTransition newNoncurrentVersionTransition() { + if (this.noncurrentVersionTransitions == null) { this.noncurrentVersionTransitions = new ArrayList(); } NoncurrentVersionTransition nt = new NoncurrentVersionTransition(); this.noncurrentVersionTransitions.add(nt); return nt; } - + /** * Obtain the rule ID. + * * @return Rule ID */ - public String getId() - { + public String getId() { return id; } - + /** * Set the rule ID. - * @param id Rule ID + * + * @param id + * Rule ID */ - public void setId(String id) - { + public void setId(String id) { this.id = id; } - + /** - * Obtain the object name prefix used to identify one or more objects to which the rule applies. + * Obtain the object name prefix used to identify one or more objects to + * which the rule applies. + * * @return Object name prefix */ - public String getPrefix() - { + public String getPrefix() { return prefix; } - + /** - * Set the object name used to identify one or more objects to which the rule applies. - * @param prefix Object name prefix + * Set the object name used to identify one or more objects to which the + * rule applies. + * + * @param prefix + * Object name prefix */ - public void setPrefix(String prefix) - { + public void setPrefix(String prefix) { this.prefix = prefix; } - + /** * Identify whether the rule is enabled. + * * @return Identifier that specifies whether the rule is enabled */ - public Boolean getEnabled() - { + public Boolean getEnabled() { return enabled; } - + /** * Specify whether to enable the rule. - * @param enabled Identifier that specifies whether the rule is enabled + * + * @param enabled + * Identifier that specifies whether the rule is enabled */ - public void setEnabled(Boolean enabled) - { + public void setEnabled(Boolean enabled) { this.enabled = enabled; } - + /** * Obtain the expiration time of an object. + * * @return Expiration time of the object */ - public Expiration getExpiration() - { + public Expiration getExpiration() { return expiration; } - + /** * Set the expiration time of an object. - * @param expiration Expiration time of an object + * + * @param expiration + * Expiration time of an object */ - public void setExpiration(Expiration expiration) - { + public void setExpiration(Expiration expiration) { this.expiration = expiration; } - + /** * Obtain the expiration time of a noncurrent object version. + * * @return Expiration time of a noncurrent object version */ - public NoncurrentVersionExpiration getNoncurrentVersionExpiration() - { + public NoncurrentVersionExpiration getNoncurrentVersionExpiration() { return noncurrentVersionExpiration; } /** * Set the expiration time of a noncurrent object version. - * @param noncurrentVersionExpiration Expiration time of a noncurrent object version + * + * @param noncurrentVersionExpiration + * Expiration time of a noncurrent object version */ - public void setNoncurrentVersionExpiration(NoncurrentVersionExpiration noncurrentVersionExpiration) - { + public void setNoncurrentVersionExpiration(NoncurrentVersionExpiration noncurrentVersionExpiration) { this.noncurrentVersionExpiration = noncurrentVersionExpiration; } - + /** * Obtain the transition policy of an object. + * * @return Object transition policy */ public List getTransitions() { - if(this.transitions == null){ + if (this.transitions == null) { this.transitions = new ArrayList(); } return transitions; @@ -698,19 +782,21 @@ public List getTransitions() { /** * Set the object transition policy. - * @param transitions Object transition policy + * + * @param transitions + * Object transition policy */ public void setTransitions(List transitions) { this.transitions = transitions; } - + /** * Obtain the transition policy of noncurrent versions. + * * @return Transition policy for noncurrent versions */ - public List getNoncurrentVersionTransitions() - { - if(this.noncurrentVersionTransitions == null){ + public List getNoncurrentVersionTransitions() { + if (this.noncurrentVersionTransitions == null) { this.noncurrentVersionTransitions = new ArrayList(); } return noncurrentVersionTransitions; @@ -718,141 +804,111 @@ public List getNoncurrentVersionTransitions() /** * Set the transition policy for noncurrent versions. - * @param noncurrentVersionTransitions Transition policy for noncurrent versions + * + * @param noncurrentVersionTransitions + * Transition policy for noncurrent versions */ - public void setNoncurrentVersionTransitions(List noncurrentVersionTransitions) - { + public void setNoncurrentVersionTransitions(List noncurrentVersionTransitions) { this.noncurrentVersionTransitions = noncurrentVersionTransitions; } - + @Override - public int hashCode() - { + public int hashCode() { final int prime = 31; int result = 1; result = prime * result + getOuterType().hashCode(); result = prime * result + ((enabled == null) ? 0 : enabled.hashCode()); result = prime * result + ((expiration == null) ? 0 : expiration.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode()); - result = prime * result + ((noncurrentVersionExpiration == null) ? 0 : noncurrentVersionExpiration.hashCode()); - result = prime * result + ((noncurrentVersionTransitions == null) ? 0 : noncurrentVersionTransitions.hashCode()); + result = prime * result + + ((noncurrentVersionExpiration == null) ? 0 : noncurrentVersionExpiration.hashCode()); + result = prime * result + + ((noncurrentVersionTransitions == null) ? 0 : noncurrentVersionTransitions.hashCode()); result = prime * result + ((prefix == null) ? 0 : prefix.hashCode()); result = prime * result + ((transitions == null) ? 0 : transitions.hashCode()); return result; } @Override - public boolean equals(Object obj) - { - if (this == obj) + public boolean equals(Object obj) { + if (this == obj) { return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) + } + if (obj == null || getClass() != obj.getClass()) { return false; - Rule other = (Rule)obj; - if (!getOuterType().equals(other.getOuterType())) + } + + Rule other = (Rule) obj; + if (!getOuterType().equals(other.getOuterType())) { return false; - if (enabled == null) - { - if (other.enabled != null) - return false; } - else if (!enabled.equals(other.enabled)) + + if (!ObjectUtils.isEquals(enabled, other.enabled)) { return false; - if (expiration == null) - { - if (other.expiration != null) - return false; } - else if (!expiration.equals(other.expiration)) + + if (!ObjectUtils.isEquals(expiration, other.expiration)) { return false; - if (id == null) - { - if (other.id != null) - return false; } - else if (!id.equals(other.id)) + + if (!ObjectUtils.isEquals(id, other.id)) { return false; - if (noncurrentVersionExpiration == null) - { - if (other.noncurrentVersionExpiration != null) - return false; } - else if (!noncurrentVersionExpiration.equals(other.noncurrentVersionExpiration)) + + if (!ObjectUtils.isEquals(noncurrentVersionExpiration, other.noncurrentVersionExpiration)) { return false; - if (noncurrentVersionTransitions == null) - { - if (other.noncurrentVersionTransitions != null) - return false; } - else if (!noncurrentVersionTransitions.equals(other.noncurrentVersionTransitions)) + + if (!ObjectUtils.isEquals(noncurrentVersionTransitions, other.noncurrentVersionTransitions)) { return false; - if (prefix == null) - { - if (other.prefix != null) - return false; } - else if (!prefix.equals(other.prefix)) + + if (!ObjectUtils.isEquals(prefix, other.prefix)) { return false; - if (transitions == null) - { - if (other.transitions != null) - return false; } - else if (!transitions.equals(other.transitions)) + + if (!ObjectUtils.isEquals(transitions, other.transitions)) { return false; + } return true; } - @Override - public String toString() - { + public String toString() { return "Rule [id=" + id + ", prefix=" + prefix + ", enabled=" + enabled + ", expiration=" + expiration - + ", noncurrentVersionExpiration=" + noncurrentVersionExpiration + ", transitions=" + transitions - + ", noncurrentVersionTransitions=" + noncurrentVersionTransitions + "]"; + + ", noncurrentVersionExpiration=" + noncurrentVersionExpiration + ", transitions=" + transitions + + ", noncurrentVersionTransitions=" + noncurrentVersionTransitions + "]"; } - private LifecycleConfiguration getOuterType() - { + private LifecycleConfiguration getOuterType() { return LifecycleConfiguration.this; } - + } @Override - public boolean equals(final Object o) - { - if (this == o) - { + public boolean equals(final Object o) { + if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) - { + if (o == null || getClass() != o.getClass()) { return false; } - final LifecycleConfiguration that = (LifecycleConfiguration)o; - if (rules != null ? !rules.equals(that.rules) : that.rules != null) - { + final LifecycleConfiguration that = (LifecycleConfiguration) o; + if (rules != null ? !rules.equals(that.rules) : that.rules != null) { return false; } return true; } - + @Override - public int hashCode() - { + public int hashCode() { return rules != null ? rules.hashCode() : 0; } @Override - public String toString() - { + public String toString() { return "LifecycleConfiguration [rules=" + rules + "]"; } - - -} - +} diff --git a/app/src/main/java/com/obs/services/model/ListBucketsRequest.java b/app/src/main/java/com/obs/services/model/ListBucketsRequest.java index 671c3d4..3662421 100644 --- a/app/src/main/java/com/obs/services/model/ListBucketsRequest.java +++ b/app/src/main/java/com/obs/services/model/ListBucketsRequest.java @@ -15,37 +15,38 @@ package com.obs.services.model; /** - * Parameters in a request for listing buckets + * Parameters in a request for listing buckets * */ public class ListBucketsRequest { - private boolean queryLocation = true; - - private BucketTypeEnum bucketType; - - public boolean isQueryLocation() { - return queryLocation; - } - - /** - * Specify whether to list the region information of all buckets. - * @param queryLocation Whether to list the region information of all buckets - */ - public void setQueryLocation(boolean queryLocation) { - this.queryLocation = queryLocation; - } - - @Override - public String toString() { - return "ListBucketsRequest [queryLocation=" + queryLocation + "]"; - } - - public BucketTypeEnum getBucketType() { - return bucketType; - } - - - public void setBucketType(BucketTypeEnum bucketType) { - this.bucketType = bucketType; - } + private boolean queryLocation = true; + + private BucketTypeEnum bucketType; + + public boolean isQueryLocation() { + return queryLocation; + } + + /** + * Specify whether to list the region information of all buckets. + * + * @param queryLocation + * Whether to list the region information of all buckets + */ + public void setQueryLocation(boolean queryLocation) { + this.queryLocation = queryLocation; + } + + @Override + public String toString() { + return "ListBucketsRequest [queryLocation=" + queryLocation + "]"; + } + + public BucketTypeEnum getBucketType() { + return bucketType; + } + + public void setBucketType(BucketTypeEnum bucketType) { + this.bucketType = bucketType; + } } diff --git a/app/src/main/java/com/obs/services/model/ListBucketsResult.java b/app/src/main/java/com/obs/services/model/ListBucketsResult.java index 5b44e60..27bdbe7 100644 --- a/app/src/main/java/com/obs/services/model/ListBucketsResult.java +++ b/app/src/main/java/com/obs/services/model/ListBucketsResult.java @@ -18,37 +18,33 @@ import java.util.List; /** - * Response to a request for listing buckets + * Response to a request for listing buckets * */ -public class ListBucketsResult extends HeaderResponse{ - private List buckets; - - private Owner owner; - - public ListBucketsResult(List buckets, Owner owner) { - this.buckets = buckets; - this.owner = owner; - } - - public List getBuckets() { - if(buckets == null) { - buckets = new ArrayList(); - } - return buckets; - } - - public Owner getOwner() { - return owner; - } - - - @Override - public String toString() { - return "ListBucketsResult [buckets=" + buckets + ", owner=" + owner + "]"; - } - - -} +public class ListBucketsResult extends HeaderResponse { + private List buckets; + + private Owner owner; + + public ListBucketsResult(List buckets, Owner owner) { + this.buckets = buckets; + this.owner = owner; + } + public List getBuckets() { + if (buckets == null) { + buckets = new ArrayList(); + } + return buckets; + } + public Owner getOwner() { + return owner; + } + + @Override + public String toString() { + return "ListBucketsResult [buckets=" + buckets + ", owner=" + owner + "]"; + } + +} diff --git a/app/src/main/java/com/obs/services/model/ListMultipartUploadsRequest.java b/app/src/main/java/com/obs/services/model/ListMultipartUploadsRequest.java index aab401b..ab8029c 100644 --- a/app/src/main/java/com/obs/services/model/ListMultipartUploadsRequest.java +++ b/app/src/main/java/com/obs/services/model/ListMultipartUploadsRequest.java @@ -1,210 +1,260 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + **/ package com.obs.services.model; /** * Parameters in a request for listing multipart uploads */ -public class ListMultipartUploadsRequest -{ - private String bucketName; - +public class ListMultipartUploadsRequest extends GenericRequest { + + { + httpMethod = HttpMethodEnum.GET; + } + private String prefix; - + private String delimiter; - + private Integer maxUploads; - + private String keyMarker; - + private String uploadIdMarker; - - public ListMultipartUploadsRequest(){ - + + private String encodingType; + + public ListMultipartUploadsRequest() { + } - + /** * Constructor - * @param bucketName Bucket name + * + * @param bucketName + * Bucket name */ - public ListMultipartUploadsRequest(String bucketName) - { + public ListMultipartUploadsRequest(String bucketName) { this.bucketName = bucketName; } - - + /** * Constructor - * @param bucketName Bucket name - * @param maxUploads Maximum number of listed multipart uploads + * + * @param bucketName + * Bucket name + * @param maxUploads + * Maximum number of listed multipart uploads */ - public ListMultipartUploadsRequest(String bucketName, Integer maxUploads) - { + public ListMultipartUploadsRequest(String bucketName, Integer maxUploads) { this.bucketName = bucketName; this.maxUploads = maxUploads; } - /** * Constructor - * @param bucketName Bucket name - * @param prefix Prefix of names of the returned objects involved in the multipart uploads - * @param delimiter Character used for sorting objects involved in the multipart uploads into different groups - * @param maxUploads Maximum number of listed multipart uploads - * @param keyMarker Start position for the query - * @param uploadIdMarker Start position of the return result. This parameter is valid only when used together with "keyMarker". Only multipart uploads after "uploadIdMarker" of the specified "keyMarker" will be returned. - */ - public ListMultipartUploadsRequest(String bucketName, String prefix, String delimiter, Integer maxUploads, String keyMarker, - String uploadIdMarker) - { + * + * @param bucketName + * Bucket name + * @param prefix + * Prefix of names of the returned objects involved in the + * multipart uploads + * @param delimiter + * Character used for sorting objects involved in the multipart + * uploads into different groups + * @param maxUploads + * Maximum number of listed multipart uploads + * @param keyMarker + * Start position for the query + * @param uploadIdMarker + * Start position of the return result. This parameter is valid + * only when used together with "keyMarker". Only multipart + * uploads after "uploadIdMarker" of the specified "keyMarker" + * will be returned. + */ + public ListMultipartUploadsRequest(String bucketName, String prefix, String delimiter, Integer maxUploads, + String keyMarker, String uploadIdMarker) { this.bucketName = bucketName; + this.maxUploads = maxUploads; this.prefix = prefix; this.delimiter = delimiter; - this.maxUploads = maxUploads; this.keyMarker = keyMarker; this.uploadIdMarker = uploadIdMarker; } + /** + ** + * Constructor + * + * @param bucketName + * Bucket name + * @param prefix + * Prefix of names of the returned objects involved in the + * multipart uploads + * @param delimiter + * Character used for sorting objects involved in the multipart + * uploads into different groups + * @param maxUploads + * Maximum number of listed multipart uploads + * @param keyMarker + * Start position for the query + * @param uploadIdMarker + * Start position of the return result. This parameter is valid + * only when used together with "keyMarker". Only multipart + * uploads after "uploadIdMarker" of the specified "keyMarker" + * will be returned. + * @param encodingType + * Use this encoding type to encode keys that contains invalid characters, the value could be "url" + */ + public ListMultipartUploadsRequest(String bucketName, String prefix, String delimiter, Integer maxUploads, + String keyMarker, String uploadIdMarker, String encodingType) { + this.bucketName = bucketName; + this.maxUploads = maxUploads; + this.prefix = prefix; + this.delimiter = delimiter; + this.keyMarker = keyMarker; + this.uploadIdMarker = uploadIdMarker; + this.encodingType = encodingType; + } /** - * Obtain the prefix of names of the returned objects involved in the multipart uploads. - * + * Obtain the prefix of names of the returned objects involved in the + * multipart uploads. + * * @return Object name prefix */ - public String getPrefix() - { + public String getPrefix() { return prefix; } - + /** - * Set the prefix of names of the returned objects involved in the multipart uploads. - * - * @param prefix Object name prefix + * Set the prefix of names of the returned objects involved in the multipart + * uploads. + * + * @param prefix + * Object name prefix */ - public void setPrefix(String prefix) - { + public void setPrefix(String prefix) { this.prefix = prefix; } - + /** - * Obtain the character used for sorting objects involved in the multipart uploads into different groups. - * + * Obtain the character used for sorting objects involved in the multipart + * uploads into different groups. + * * @return Character for grouping object names */ - public String getDelimiter() - { + public String getDelimiter() { return delimiter; } - + /** - * Set the character used for sorting objects involved in the multipart uploads into different groups - * - * @param delimiter Character for grouping object names + * Set the character used for sorting objects involved in the multipart + * uploads into different groups + * + * @param delimiter + * Character for grouping object names */ - public void setDelimiter(String delimiter) - { + public void setDelimiter(String delimiter) { this.delimiter = delimiter; } - + /** * Obtain the start position for query (sorted by object name). - * + * * @return Start position for query */ - public String getKeyMarker() - { + public String getKeyMarker() { return keyMarker; } - + /** * Set the start position for query (sorted by object name). - * - * @param keyMarker Start position for query + * + * @param keyMarker + * Start position for query */ - public void setKeyMarker(String keyMarker) - { + public void setKeyMarker(String keyMarker) { this.keyMarker = keyMarker; } - + /** - * Obtain the start position for query (sorted by multipart upload ID). This parameter is valid when used together with "keyMarker" and it specifies the start position of the returned result. - * + * Obtain the start position for query (sorted by multipart upload ID). This + * parameter is valid when used together with "keyMarker" and it specifies + * the start position of the returned result. + * * @return Start position for query */ - public String getUploadIdMarker() - { + public String getUploadIdMarker() { return uploadIdMarker; } - + /** - * Set the start position for query (sorted by multipart upload ID). This parameter is valid when used together with "keyMarker" and it specifies the start position of the returned result. - * - * @param uploadIdMarker Start position for query + * Set the start position for query (sorted by multipart upload ID). This + * parameter is valid when used together with "keyMarker" and it specifies + * the start position of the returned result. + * + * @param uploadIdMarker + * Start position for query */ - public void setUploadIdMarker(String uploadIdMarker) - { + public void setUploadIdMarker(String uploadIdMarker) { this.uploadIdMarker = uploadIdMarker; } - + /** - * Obtain the name of the bucket to which the multipart upload belongs. - * - * @return Name of the bucket to which the multipart upload belongs + * Obtain the maximum number of listed multipart uploads. + * + * @return Maximum number of listed multipart uploads */ - public String getBucketName() - { - return bucketName; + public Integer getMaxUploads() { + return maxUploads; } - + /** - * Set the name for the bucket to which the multipart upload belongs. - * - * @param bucketName Name of the bucket to which the multipart upload belongs + * Set the maximum number of listed multipart uploads. + * + * @param maxUploads + * Maximum number of listed multipart uploads */ - public void setBucketName(String bucketName) - { - this.bucketName = bucketName; + public void setMaxUploads(Integer maxUploads) { + this.maxUploads = maxUploads; } - + /** - * Obtain the maximum number of listed multipart uploads. - * - * @return Maximum number of listed multipart uploads + * Set encoding type to encode objectkeys, the value could be url + * + * @param encodingType + * encoding type for encoding */ - public Integer getMaxUploads() - { - return maxUploads; + public void setEncodingType(String encodingType) { + this.encodingType = encodingType; } - + /** - * Set the maximum number of listed multipart uploads. - * - * @param maxUploads Maximum number of listed multipart uploads + * Get encoding type to encode objectkeys + * + * @return encoding type for encoding */ - public void setMaxUploads(Integer maxUploads) - { - this.maxUploads = maxUploads; + public String getEncodingType() { + return encodingType; } @Override - public String toString() - { - return "ListMultipartUploadsRequest [bucketName=" + bucketName + ", prefix=" + prefix + ", delimiter=" + delimiter + ", maxUploads=" - + maxUploads + ", keyMarker=" + keyMarker + ", uploadIdMarker=" + uploadIdMarker + "]"; + public String toString() { + return "ListMultipartUploadsRequest [bucketName=" + bucketName + ", prefix=" + prefix + ", delimiter=" + + delimiter + ", maxUploads=" + maxUploads + ", keyMarker=" + keyMarker + ", uploadIdMarker=" + + uploadIdMarker + ", encodingType=" + encodingType + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/ListObjectsRequest.java b/app/src/main/java/com/obs/services/model/ListObjectsRequest.java index 765d5a3..7ca28e0 100644 --- a/app/src/main/java/com/obs/services/model/ListObjectsRequest.java +++ b/app/src/main/java/com/obs/services/model/ListObjectsRequest.java @@ -1,193 +1,224 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + **/ package com.obs.services.model; /** * Parameters in a request for listing objects in a bucket */ -public class ListObjectsRequest -{ - private String bucketName; - +public class ListObjectsRequest extends GenericRequest { + + { + httpMethod = HttpMethodEnum.GET; + } + private String prefix; - + private String marker; - + private int maxKeys; - + private String delimiter; - + private int listTimeout; - - public ListObjectsRequest() - { + + private String encodingType; + + public ListObjectsRequest() { } - + /** * Constructor - * - * @param bucketName Bucket name + * + * @param bucketName + * Bucket name */ - public ListObjectsRequest(String bucketName) - { + public ListObjectsRequest(String bucketName) { this.bucketName = bucketName; } - - + /** * Constructor - * @param bucketName Bucket name - * @param maxKeys Maximum number of objects to be listed + * + * @param bucketName + * Bucket name + * @param maxKeys + * Maximum number of objects to be listed */ - public ListObjectsRequest(String bucketName, int maxKeys) - { + public ListObjectsRequest(String bucketName, int maxKeys) { this.bucketName = bucketName; this.maxKeys = maxKeys; } - /** - * Constructor - * @param bucketName Bucket name - * @param prefix Object name prefix, used for filtering objects to be listed - * @param marker Start position for listing objects - * @param delimiter Character used for grouping object names - * @param maxKeys Maximum number of objects to be listed + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param prefix + * Object name prefix, used for filtering objects to be listed + * @param marker + * Start position for listing objects + * @param delimiter + * Character used for grouping object names + * @param maxKeys + * Maximum number of objects to be listed */ - public ListObjectsRequest(String bucketName, String prefix, String marker, String delimiter, int maxKeys) - { + public ListObjectsRequest(String bucketName, String prefix, String marker, String delimiter, int maxKeys) { this.bucketName = bucketName; - this.prefix = prefix; + this.maxKeys = maxKeys; this.marker = marker; + this.prefix = prefix; this.delimiter = delimiter; - this.maxKeys = maxKeys; - } - - /** - * Obtain the bucket name. - * - * @return Bucket name - */ - public String getBucketName() - { - return bucketName; + } - + /** - * Set the bucket name. - * - * @param bucketName Bucket name + Constructor + * + * @param bucketName + * Bucket name + * @param prefix + * Object name prefix, used for filtering objects to be listed + * @param marker + * Start position for listing objects + * @param delimiter + * Character used for grouping object names + * @param maxKeys + * Maximum number of objects to be listed + * @param encodingType + * Use this encoding type to encode keys that contains invalid characters, the value could be "url" */ - public void setBucketName(String bucketName) - { + public ListObjectsRequest(String bucketName, String prefix, String marker, String delimiter, int maxKeys, + String encodingType) { this.bucketName = bucketName; + this.maxKeys = maxKeys; + this.marker = marker; + this.delimiter = delimiter; + this.prefix = prefix; + this.encodingType = encodingType; } - + + /** * Obtain the object name prefix used for filtering objects to be listed. - * + * * @return Object name prefix */ - public String getPrefix() - { + public String getPrefix() { return prefix; } - + /** * Set the object name prefix used for filtering objects to be listed. - * - * @param prefix Object name prefix + * + * @param prefix + * Object name prefix */ - public void setPrefix(String prefix) - { + public void setPrefix(String prefix) { this.prefix = prefix; } - + /** * Obtain the start position for listing objects. - * + * * @return Start position marker */ - public String getMarker() - { + public String getMarker() { return marker; } - + /** * Set the start position for listing objects. - * - * @param marker Start position marker + * + * @param marker + * Start position marker */ - public void setMarker(String marker) - { + public void setMarker(String marker) { this.marker = marker; } - + /** * Obtain the maximum number of objects to be listed. - * + * * @return Maximum number of objects to be listed */ - public int getMaxKeys() - { + public int getMaxKeys() { return maxKeys; } - + /** * Set the maximum number of objects to be listed. - * - * @param maxKeys Maximum number of objects to be listed + * + * @param maxKeys + * Maximum number of objects to be listed */ - public void setMaxKeys(int maxKeys) - { + public void setMaxKeys(int maxKeys) { this.maxKeys = maxKeys; } - + + /** + * Set encoding type to encode objectkeys, the value could be url + * @param encodingType + * encoding type for encoding + */ + public void setEncodingType(String encodingType) { + this.encodingType = encodingType; + } + /** * Obtain the character used for grouping object names. - * + * * @return Character for grouping object names */ - public String getDelimiter() - { + public String getDelimiter() { return delimiter; } - + + /** + * Get encoding type to encode objectkeys + * + * @return encoding type for encoding + */ + public String getEncodingType() { + return encodingType; + } + + /** * Set the character used for grouping object names. - * - * @param delimiter Character for grouping object names + * + * @param delimiter + * Character for grouping object names */ - public void setDelimiter(String delimiter) - { + public void setDelimiter(String delimiter) { this.delimiter = delimiter; } public int getListTimeout() { - return listTimeout; - } - - public void setListTimeout(int listTimeout) { - this.listTimeout = listTimeout; - } - - @Override - public String toString() { - return "ListObjectsRequest [bucketName=" + bucketName + ", prefix=" + prefix + ", marker=" + marker - + ", maxKeys=" + maxKeys + ", delimiter=" + delimiter + ", listTimeout=" + listTimeout + "]"; - } - -} + return listTimeout; + } + public void setListTimeout(int listTimeout) { + this.listTimeout = listTimeout; + } + + @Override + public String toString() { + return "ListObjectsRequest [bucketName=" + bucketName + ", prefix=" + prefix + ", marker=" + marker + + ", maxKeys=" + maxKeys + ", delimiter=" + delimiter + ", listTimeout=" + listTimeout + + ", encodingType=" + encodingType + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/ListPartsRequest.java b/app/src/main/java/com/obs/services/model/ListPartsRequest.java index cfb46d8..c719162 100644 --- a/app/src/main/java/com/obs/services/model/ListPartsRequest.java +++ b/app/src/main/java/com/obs/services/model/ListPartsRequest.java @@ -1,192 +1,204 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + **/ package com.obs.services.model; /** * Parameters in a request for listing uploaded parts */ -public class ListPartsRequest -{ - private String bucketName; - - private String key; - +public class ListPartsRequest extends BaseObjectRequest { + + { + httpMethod = HttpMethodEnum.GET; + } + private String uploadId; - + private Integer maxParts; - + private Integer partNumberMarker; - - public ListPartsRequest(){ - + + private String encodingType; + + public ListPartsRequest() { + } - + /** * Constructor + * * @param bucketName Name of the bucket to which the multipart upload belongs - * @param key Name of the object involved in the multipart upload + * @param objectKey Name of the object involved in the multipart upload */ - public ListPartsRequest(String bucketName, String key){ + public ListPartsRequest(String bucketName, String objectKey) { this.bucketName = bucketName; - this.key = key; + this.objectKey = objectKey; } - + /** * Constructor + * * @param bucketName Name of the bucket to which the multipart upload belongs - * @param key Name of the object involved in the multipart upload - * @param uploadId Multipart upload ID + * @param objectKey Name of the object involved in the multipart upload + * @param uploadId Multipart upload ID */ - public ListPartsRequest(String bucketName, String key, String uploadId) - { + public ListPartsRequest(String bucketName, String objectKey, String uploadId) { this.bucketName = bucketName; - this.key = key; + this.objectKey = objectKey; this.uploadId = uploadId; } /** * Constructor + * * @param bucketName Name of the bucket to which the multipart upload belongs - * @param key Name of the object involved in the multipart upload - * @param uploadId Multipart upload ID - * @param maxParts Maximum number of uploaded parts that can be listed + * @param objectKey Name of the object involved in the multipart upload + * @param uploadId Multipart upload ID + * @param maxParts Maximum number of uploaded parts that can be listed */ - public ListPartsRequest(String bucketName, String key, String uploadId, Integer maxParts) - { + public ListPartsRequest(String bucketName, String objectKey, String uploadId, Integer maxParts) { this.bucketName = bucketName; - this.key = key; + this.objectKey = objectKey; this.uploadId = uploadId; this.maxParts = maxParts; } - /** * Constructor - * @param bucketName Name of the bucket to which the multipart upload belongs - * @param key Name of the object involved in the multipart upload - * @param uploadId Multipart upload ID - * @param maxParts Maximum number of uploaded parts that can be listed + * + * @param bucketName Name of the bucket to which the multipart upload belongs + * @param objectKey Name of the object involved in the multipart upload + * @param uploadId Multipart upload ID + * @param maxParts Maximum number of uploaded parts that can be listed * @param partNumberMarker Start position for listing parts */ - public ListPartsRequest(String bucketName, String key, String uploadId, Integer maxParts, Integer partNumberMarker) - { + public ListPartsRequest(String bucketName, String objectKey, String uploadId, Integer maxParts, + Integer partNumberMarker) { this.bucketName = bucketName; - this.key = key; + this.objectKey = objectKey; this.uploadId = uploadId; this.maxParts = maxParts; this.partNumberMarker = partNumberMarker; } - - - /** - * Obtain the name of the bucket to which the multipart upload belongs. - * @return Name of the bucket to which the multipart upload belongs - */ - public String getBucketName() - { - return bucketName; - } - /** - * Set the name for the bucket to which the multipart upload belongs. - * @param bucketName Name of the bucket to which the multipart upload belongs + * Constructor + * + * @param bucketName Name of the bucket to which the multipart upload belongs + * @param objectKey Name of the object involved in the multipart upload + * @param uploadId Multipart upload ID + * @param maxParts Maximum number of uploaded parts that can be listed + * @param partNumberMarker Start position for listing parts + * @param encodingType Use this encoding type to encode keys that contains invalid characters, + * the value could be "url" */ - public void setBucketName(String bucketName) - { + public ListPartsRequest(String bucketName, String objectKey, String uploadId, Integer maxParts, + Integer partNumberMarker, String encodingType) { this.bucketName = bucketName; + this.objectKey = objectKey; + this.uploadId = uploadId; + this.maxParts = maxParts; + this.partNumberMarker = partNumberMarker; + this.encodingType = encodingType; } - - /** - * Obtain the name of the bucket to which the multipart upload belongs. - * @return Name of the bucket to which the multipart upload belongs - */ - public String getKey() - { - return key; + + @Deprecated + public String getKey() { + return objectKey; } - - /** - * Set the name for the bucket to which the multipart upload belongs. - * @param key Name of the bucket to which the multipart upload belongs - */ - public void setKey(String key) - { - this.key = key; + + @Deprecated + public void setKey(String objectKey) { + this.objectKey = objectKey; } - + /** * Obtain the multipart upload ID. + * * @return Multipart upload ID */ - public String getUploadId() - { + public String getUploadId() { return uploadId; } - + /** * Set the multipart upload ID. + * * @param uploadId Multipart upload ID */ - public void setUploadId(String uploadId) - { + public void setUploadId(String uploadId) { this.uploadId = uploadId; } - + /** * Obtain the maximum number of uploaded parts that can be listed. + * * @return Maximum number of uploaded parts that can be listed */ - public Integer getMaxParts() - { + public Integer getMaxParts() { return maxParts; } - + /** * Set the maximum number of uploaded parts that can be listed. + * * @param maxParts Maximum number of uploaded parts that can be listed */ - public void setMaxParts(Integer maxParts) - { + public void setMaxParts(Integer maxParts) { this.maxParts = maxParts; } - + /** * Obtain the start position for listing parts. + * * @return Start position for listing parts */ - public Integer getPartNumberMarker() - { + public Integer getPartNumberMarker() { return partNumberMarker; } - + /** * Set the start position for listing parts. + * * @param partNumberMarker Start position for listing parts */ - public void setPartNumberMarker(Integer partNumberMarker) - { + public void setPartNumberMarker(Integer partNumberMarker) { this.partNumberMarker = partNumberMarker; } - - @Override - public String toString() - { - return "ListPartsRequest [bucketName=" + bucketName + ", key=" + key + ", uploadId=" + uploadId + ", maxParts=" + maxParts - + ", partNumberMarker=" + partNumberMarker + "]"; + + /** + * Set encoding type to encode objectkeys, the value could be url + * + * @param encodingType encoding type for encoding + */ + public void setEncodingType(String encodingType) { + this.encodingType = encodingType; } -} + /** + * Get encoding type to encode objectkeys + * + * @return encoding type for encoding + */ + public String getEncodingType() { + return encodingType; + } + @Override + public String toString() { + return "ListPartsRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", uploadId=" + + uploadId + ", maxParts=" + maxParts + ", partNumberMarker=" + partNumberMarker + + ", encodingType=" + encodingType + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/ListPartsResult.java b/app/src/main/java/com/obs/services/model/ListPartsResult.java index 661b85b..c2b58bc 100644 --- a/app/src/main/java/com/obs/services/model/ListPartsResult.java +++ b/app/src/main/java/com/obs/services/model/ListPartsResult.java @@ -20,180 +20,256 @@ /** * Response to a request for listing uploaded parts */ -public class ListPartsResult extends HeaderResponse -{ +public class ListPartsResult extends HeaderResponse { private String bucket; - + private String key; - + private String uploadId; - + private Owner initiator; - + private Owner owner; - + private StorageClassEnum storageClass; - + private List multipartList; - + private Integer maxParts; - + private boolean isTruncated; - + private String partNumberMarker; - + private String nextPartNumberMarker; + + @Deprecated + //CHECKSTYLE:OFF + public ListPartsResult(String bucket, String key, String uploadId, Owner initiator, Owner owner, + StorageClassEnum storageClass, List multipartList, Integer maxParts, boolean isTruncated, + String partNumberMarker, String nextPartNumberMarker) { + super(); + this.bucket = bucket; + this.key = key; + this.uploadId = uploadId; + this.initiator = initiator; + this.owner = owner; + this.storageClass = storageClass; + this.multipartList = multipartList; + this.maxParts = maxParts; + this.isTruncated = isTruncated; + this.partNumberMarker = partNumberMarker; + this.nextPartNumberMarker = nextPartNumberMarker; + } + + private ListPartsResult(Builder builder) { + super(); + this.bucket = builder.bucket; + this.key = builder.key; + this.uploadId = builder.uploadId; + this.initiator = builder.initiator; + this.owner = builder.owner; + this.storageClass = builder.storageClass; + this.multipartList = builder.multipartList; + this.maxParts = builder.maxParts; + this.isTruncated = builder.isTruncated; + this.partNumberMarker = builder.partNumberMarker; + this.nextPartNumberMarker = builder.nextPartNumberMarker; + } + public static final class Builder { + private String bucket; + private String key; + private String uploadId; + private Owner initiator; + private Owner owner; + private StorageClassEnum storageClass; + private List multipartList; + private Integer maxParts; + private boolean isTruncated; + private String partNumberMarker; + private String nextPartNumberMarker; + + public Builder bucket(String bucket) { + this.bucket = bucket; + return this; + } + + public Builder key(String key) { + this.key = key; + return this; + } + + public Builder uploadId(String uploadId) { + this.uploadId = uploadId; + return this; + } + + public Builder initiator(Owner initiator) { + this.initiator = initiator; + return this; + } + + public Builder owner(Owner owner) { + this.owner = owner; + return this; + } + + public Builder storageClass(StorageClassEnum storageClass) { + this.storageClass = storageClass; + return this; + } + + public Builder multipartList(List multipartList) { + this.multipartList = multipartList; + return this; + } + + public Builder maxParts(Integer maxParts) { + this.maxParts = maxParts; + return this; + } + + public Builder isTruncated(boolean isTruncated) { + this.isTruncated = isTruncated; + return this; + } + + public Builder partNumberMarker(String partNumberMarker) { + this.partNumberMarker = partNumberMarker; + return this; + } + + public Builder nextPartNumberMarker(String nextPartNumberMarker) { + this.nextPartNumberMarker = nextPartNumberMarker; + return this; + } + + public ListPartsResult builder() { + return new ListPartsResult(this); + } + } - public ListPartsResult(String bucket, String key, String uploadId, Owner initiator, Owner owner, - StorageClassEnum storageClass, List multipartList, Integer maxParts, boolean isTruncated, - String partNumberMarker, String nextPartNumberMarker) { - super(); - this.bucket = bucket; - this.key = key; - this.uploadId = uploadId; - this.initiator = initiator; - this.owner = owner; - this.storageClass = storageClass; - this.multipartList = multipartList; - this.maxParts = maxParts; - this.isTruncated = isTruncated; - this.partNumberMarker = partNumberMarker; - this.nextPartNumberMarker = nextPartNumberMarker; - } - - /** + /** * Obtain the name of the bucket to which the multipart upload belongs. + * * @return Name of the bucket to which the multipart upload belongs */ - public String getBucket() - { + public String getBucket() { return bucket; } - + /** * Obtain the name of the object involved in the multipart upload. + * * @return Name of the object involved in the multipart upload */ - public String getKey() - { + public String getKey() { return key; } - - + /** * Obtain the multipart upload ID. + * * @return Multipart upload ID */ - public String getUploadId() - { + public String getUploadId() { return uploadId; } - - + /** * Creator of the multipart upload + * * @return Creator of the multipart upload */ - public Owner getInitiator() - { + public Owner getInitiator() { return initiator; } - - + /** * Query the creator of the multipart upload. + * * @return Owner of the multipart upload */ - public Owner getOwner() - { + public Owner getOwner() { return owner; } - - + /** * Obtain the storage class of the object involved in the multipart upload. + * * @return Storage class of the object involved in the multipart upload */ @Deprecated - public String getStorageClass() - { + public String getStorageClass() { return this.storageClass == null ? null : this.storageClass.getCode(); } - - + /** * Obtain the storage class of the object involved in the multipart upload. + * * @return Storage class of the object involved in the multipart upload */ - public StorageClassEnum getObjectStorageClass() - { + public StorageClassEnum getObjectStorageClass() { return storageClass; } - - + /** * Obtain the maximum number of uploaded parts to be listed. + * * @return Maximum number of uploaded parts to be listed */ - public Integer getMaxParts() - { + public Integer getMaxParts() { return maxParts; } - - + /** * Obtain the list of uploaded parts. + * * @return List of uploaded parts */ - public List getMultipartList() - { - if(this.multipartList == null){ + public List getMultipartList() { + if (this.multipartList == null) { this.multipartList = new ArrayList(); } return multipartList; } - - /** - * Check whether the query result list is truncated. Value "true" indicates that the results are incomplete while value "false" indicates that the results are complete. + * Check whether the query result list is truncated. Value "true" indicates + * that the results are incomplete while value "false" indicates that the + * results are complete. + * * @return Truncation identifier */ - public boolean isTruncated() - { + public boolean isTruncated() { return isTruncated; } - /** * Obtain the start position for listing parts. + * * @return Start position for listing parts */ - public String getPartNumberMarker() - { + public String getPartNumberMarker() { return partNumberMarker; } - /** * Obtain the start position for next listing. + * * @return Start position for next listing */ - public String getNextPartNumberMarker() - { + public String getNextPartNumberMarker() { return nextPartNumberMarker; } - @Override - public String toString() - { - return "ListPartsResult [bucket=" + bucket + ", key=" + key + ", uploadId=" + uploadId + ", initiator=" + initiator + ", owner=" - + owner + ", storageClass=" + storageClass + ", multipartList=" + multipartList + ", maxParts=" + maxParts + ", isTruncated=" - + isTruncated + ", partNumberMarker=" + partNumberMarker + ", nextPartNumberMarker=" + nextPartNumberMarker + "]"; + public String toString() { + return "ListPartsResult [bucket=" + bucket + ", key=" + key + ", uploadId=" + uploadId + ", initiator=" + + initiator + ", owner=" + owner + ", storageClass=" + storageClass + ", multipartList=" + multipartList + + ", maxParts=" + maxParts + ", isTruncated=" + isTruncated + ", partNumberMarker=" + partNumberMarker + + ", nextPartNumberMarker=" + nextPartNumberMarker + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/ListVersionsRequest.java b/app/src/main/java/com/obs/services/model/ListVersionsRequest.java index d4bfa5f..9ae58b4 100644 --- a/app/src/main/java/com/obs/services/model/ListVersionsRequest.java +++ b/app/src/main/java/com/obs/services/model/ListVersionsRequest.java @@ -1,209 +1,241 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + **/ package com.obs.services.model; /** * Parameters in a request for listing versioning objects in a bucket */ -public class ListVersionsRequest -{ - private String bucketName; - +public class ListVersionsRequest extends GenericRequest { + + { + httpMethod = HttpMethodEnum.GET; + } + private String prefix; - + private String keyMarker; - + private String versionIdMarker; - + private int maxKeys; - + private String delimiter; - + private int listTimeout; - - public ListVersionsRequest() - { + + private String encodingType; + + public ListVersionsRequest() { } - + /** * Constructor - * - * @param bucketName Bucket name + * + * @param bucketName + * Bucket name */ - public ListVersionsRequest(String bucketName) - { + public ListVersionsRequest(String bucketName) { this.bucketName = bucketName; } - - + /** * Constructor - * @param bucketName Bucket name - * @param maxKeys Maximum number of versioning objects to be listed + * + * @param bucketName + * Bucket name + * @param maxKeys + * Maximum number of versioning objects to be listed */ - public ListVersionsRequest(String bucketName, int maxKeys) - { + public ListVersionsRequest(String bucketName, int maxKeys) { this.bucketName = bucketName; this.maxKeys = maxKeys; } - /** - * Constructor - * @param bucketName Bucket name - * @param prefix Oject name prefix used for listing versioning objects - * @param keyMarker Start position for listing versioning objects - * @param delimiter Character used for sorting versioning object names into groups - * @param maxKeys Maximum number of versioning objects to be listed + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param prefix + * Oject name prefix used for listing versioning objects + * @param keyMarker + * Start position for listing versioning objects + * @param delimiter + * Character used for sorting versioning object names into groups + * @param maxKeys + * Maximum number of versioning objects to be listed */ - public ListVersionsRequest(String bucketName, String prefix, String keyMarker, String delimiter, int maxKeys) - { + public ListVersionsRequest(String bucketName, String prefix, String keyMarker, String delimiter, int maxKeys) { this.bucketName = bucketName; - this.prefix = prefix; + this.maxKeys = maxKeys; this.keyMarker = keyMarker; this.delimiter = delimiter; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param prefix + * Oject name prefix used for listing versioning objects + * @param keyMarker + * Start position for listing versioning objects + * @param delimiter + * Character used for sorting versioning object names into groups + * @param maxKeys + * Maximum number of versioning objects to be listed + * @param encodingType + * Encoding type uesd for encoding the keys��the value can be url + */ + public ListVersionsRequest(String bucketName, String prefix, String keyMarker, String delimiter, + int maxKeys, String encodingType) { + this.bucketName = bucketName; this.maxKeys = maxKeys; + this.prefix = prefix; + this.keyMarker = keyMarker; + this.delimiter = delimiter; + this.encodingType = encodingType; } - + /** - * Obtain the bucket name. - * - * @return Bucket name + * Set encodingType used for encoding keys, the value can be url + * + * @param encodingType used for encoding keys, the value can be url */ - public String getBucketName() - { - return bucketName; + public void setEncodingType(String encodingType) { + this.encodingType = encodingType; } - + /** - * Set the bucket name. - * - * @param bucketName Bucket name + * Get encodingType + * + * @return encodingType */ - public void setBucketName(String bucketName) - { - this.bucketName = bucketName; + public String getEncodingType() { + return encodingType; } - + /** * Obtain the object name prefix used for listing versioning objects. - * + * * @return Object name prefix */ - public String getPrefix() - { + public String getPrefix() { return prefix; } - + /** * Set the object name prefix used for listing versioning objects. - * - * @param prefix Object name prefix + * + * @param prefix + * Object name prefix */ - public void setPrefix(String prefix) - { + public void setPrefix(String prefix) { this.prefix = prefix; } - + /** - * Obtain the maximum number of versioning objects to be listed. - * + * Obtain the maximum number of versioning objects to be listed. + * * @return Maximum number of versioning objects to be listed */ - public int getMaxKeys() - { + public int getMaxKeys() { return maxKeys; } - + /** * Set the maximum number of versioning objects to be listed. - * - * @param maxKeys Maximum number of versioning objects to be listed + * + * @param maxKeys + * Maximum number of versioning objects to be listed */ - public void setMaxKeys(int maxKeys) - { + public void setMaxKeys(int maxKeys) { this.maxKeys = maxKeys; } - + /** * Character used for grouping versioning object names. - * + * * @return Character for grouping object names */ - public String getDelimiter() - { + public String getDelimiter() { return delimiter; } - + /** * Set the character used for grouping versioning object names. - * - * @param delimiter Character for grouping objet names + * + * @param delimiter + * Character for grouping objet names */ - public void setDelimiter(String delimiter) - { + public void setDelimiter(String delimiter) { this.delimiter = delimiter; } - - /** - * Obtain the start position (versionId) for listing versioning objects. - * @return Start position for listing versioning objects - */ - public String getVersionIdMarker() { - return versionIdMarker; - } - - /** - * Set the start position (versionId) for listing versioning objects. - * @param versionIdMarker Start position for listing versioning objects - */ - public void setVersionIdMarker(String versionIdMarker) { - this.versionIdMarker = versionIdMarker; - } - - /** - * Obtain the start position for listing versioning objects. - * @return Start position for listing versioning objects - */ - public String getKeyMarker() { - return keyMarker; - } - - /** - * Set the start position for listing versioning objects. - * @param keyMarker Start position for listing versioning objects - */ - public void setKeyMarker(String keyMarker) { - this.keyMarker = keyMarker; - } - - public int getListTimeout() { - return listTimeout; - } - public void setListTimeout(int listTimeout) { - this.listTimeout = listTimeout; - } + /** + * Obtain the start position (versionId) for listing versioning objects. + * + * @return Start position for listing versioning objects + */ + public String getVersionIdMarker() { + return versionIdMarker; + } - @Override - public String toString() { - return "ListVersionsRequest [bucketName=" + bucketName + ", prefix=" + prefix + ", keyMarker=" + keyMarker - + ", versionIdMarker=" + versionIdMarker + ", maxKeys=" + maxKeys + ", delimiter=" + delimiter - + ", listTimeout=" + listTimeout + "]"; - } + /** + * Set the start position (versionId) for listing versioning objects. + * + * @param versionIdMarker + * Start position for listing versioning objects + */ + public void setVersionIdMarker(String versionIdMarker) { + this.versionIdMarker = versionIdMarker; + } - -} + /** + * Obtain the start position for listing versioning objects. + * + * @return Start position for listing versioning objects + */ + public String getKeyMarker() { + return keyMarker; + } + /** + * Set the start position for listing versioning objects. + * + * @param keyMarker + * Start position for listing versioning objects + */ + public void setKeyMarker(String keyMarker) { + this.keyMarker = keyMarker; + } + public int getListTimeout() { + return listTimeout; + } + + public void setListTimeout(int listTimeout) { + this.listTimeout = listTimeout; + } + + @Override + public String toString() { + return "ListVersionsRequest [bucketName=" + bucketName + ", prefix=" + prefix + ", keyMarker=" + keyMarker + + ", versionIdMarker=" + versionIdMarker + ", maxKeys=" + maxKeys + ", delimiter=" + delimiter + + ", listTimeout=" + listTimeout + ", encodingType=" + encodingType + "]"; + } + +} diff --git a/app/src/main/java/com/obs/services/model/ListVersionsResult.java b/app/src/main/java/com/obs/services/model/ListVersionsResult.java index 1568824..c54c082 100644 --- a/app/src/main/java/com/obs/services/model/ListVersionsResult.java +++ b/app/src/main/java/com/obs/services/model/ListVersionsResult.java @@ -21,194 +21,284 @@ /** * Response to a request for listing versioning objects in a bucket */ -public class ListVersionsResult extends HeaderResponse -{ +public class ListVersionsResult extends HeaderResponse { private String bucketName; - + private String prefix; - + private String keyMarker; - + private String nextKeyMarker; - + private String versionIdMarker; - + private String nextVersionIdMarker; - + private String maxKeys; - + private boolean isTruncated; - + private VersionOrDeleteMarker[] versions; - + private List commonPrefixes; - + private String location; - + private String delimiter; + + @Deprecated + //CHECKSTYLE:OFF + public ListVersionsResult(String bucketName, String prefix, String keyMarker, String nextKeyMarker, + String versionIdMarker, String nextVersionIdMarker, String maxKeys, boolean isTruncated, + VersionOrDeleteMarker[] versions, List commonPrefixes, String location, String delimiter) { + super(); + this.bucketName = bucketName; + this.prefix = prefix; + this.keyMarker = keyMarker; + this.nextKeyMarker = nextKeyMarker; + this.versionIdMarker = versionIdMarker; + this.nextVersionIdMarker = nextVersionIdMarker; + this.maxKeys = maxKeys; + this.isTruncated = isTruncated; + if (null != versions) { + this.versions = versions.clone(); + } else { + this.versions = null; + } + this.commonPrefixes = commonPrefixes; + this.location = location; + this.delimiter = delimiter; + } + + private ListVersionsResult(Builder builder) { + super(); + this.bucketName = builder.bucketName; + this.prefix = builder.prefix; + this.keyMarker = builder.keyMarker; + this.nextKeyMarker = builder.nextKeyMarker; + this.versionIdMarker = builder.versionIdMarker; + this.nextVersionIdMarker = builder.nextVersionIdMarker; + this.maxKeys = builder.maxKeys; + this.isTruncated = builder.isTruncated; + if (null != builder.versions) { + this.versions = builder.versions.clone(); + } else { + this.versions = null; + } + this.commonPrefixes = builder.commonPrefixes; + this.location = builder.location; + this.delimiter = builder.delimiter; + } - private boolean isAppendable; + public static final class Builder { + private String bucketName; + private String prefix; + private String keyMarker; + private String nextKeyMarker; + private String versionIdMarker; + private String nextVersionIdMarker; + private String maxKeys; + private boolean isTruncated; + private VersionOrDeleteMarker[] versions; + private List commonPrefixes; + private String location; + private String delimiter; + + public Builder bucketName(String bucketName) { + this.bucketName = bucketName; + return this; + } + + public Builder prefix(String prefix) { + this.prefix = prefix; + return this; + } + + public Builder keyMarker(String keyMarker) { + this.keyMarker = keyMarker; + return this; + } + + public Builder nextKeyMarker(String nextKeyMarker) { + this.nextKeyMarker = nextKeyMarker; + return this; + } + + public Builder versionIdMarker(String versionIdMarker) { + this.versionIdMarker = versionIdMarker; + return this; + } + + public Builder nextVersionIdMarker(String nextVersionIdMarker) { + this.nextVersionIdMarker = nextVersionIdMarker; + return this; + } + + public Builder maxKeys(String maxKeys) { + this.maxKeys = maxKeys; + return this; + } + + public Builder isTruncated(boolean isTruncated) { + this.isTruncated = isTruncated; + return this; + } + + public Builder versions(VersionOrDeleteMarker[] versions) { + if (null != versions) { + this.versions = versions.clone(); + } else { + this.versions = null; + } + return this; + } + + public Builder commonPrefixes(List commonPrefixes) { + this.commonPrefixes = commonPrefixes; + return this; + } + + public Builder location(String location) { + this.location = location; + return this; + } + + public Builder delimiter(String delimiter) { + this.delimiter = delimiter; + return this; + } + + public ListVersionsResult builder() { + return new ListVersionsResult(this); + } + } - public ListVersionsResult(String bucketName, String prefix, String keyMarker, String nextKeyMarker, - String versionIdMarker, String nextVersionIdMarker, String maxKeys, boolean isTruncated, - VersionOrDeleteMarker[] versions, List commonPrefixes, String location, String delimiter) { - super(); - this.bucketName = bucketName; - this.prefix = prefix; - this.keyMarker = keyMarker; - this.nextKeyMarker = nextKeyMarker; - this.versionIdMarker = versionIdMarker; - this.nextVersionIdMarker = nextVersionIdMarker; - this.maxKeys = maxKeys; - this.isTruncated = isTruncated; - this.versions = versions; - this.commonPrefixes = commonPrefixes; - this.location = location; - this.delimiter = delimiter; - } - - - /** + /** * Obtain the bucket name. + * * @return Bucket name */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } - - + /** * Obtain the object name prefix used for listing versioning objects. + * * @return Object name prefix used for listing versioning objects */ - public String getPrefix() - { + public String getPrefix() { return prefix; } - - + /** - * Obtain the start position for listing versioning objects (sorted by object name). + * Obtain the start position for listing versioning objects (sorted by + * object name). + * * @return Start position for listing objects */ - public String getKeyMarker() - { + public String getKeyMarker() { return keyMarker; } - - + /** - * Obtain the start position for listing versioning objects (sorted by version ID). + * Obtain the start position for listing versioning objects (sorted by + * version ID). + * * @return Start position for listing objects */ - public String getVersionIdMarker() - { + public String getVersionIdMarker() { return versionIdMarker; } - + /** - * Obtain the maximum number of versioning objects to be listed. + * Obtain the maximum number of versioning objects to be listed. + * * @return Maximum number of versioning objects to be listed */ - public String getMaxKeys() - { + public String getMaxKeys() { return maxKeys; } - - + /** - * Check whether the query result list is truncated. Value "true" indicates that the results are incomplete while value "false" indicates that the results are complete. + * Check whether the query result list is truncated. Value "true" indicates + * that the results are incomplete while value "false" indicates that the + * results are complete. + * * @return Truncation identifier */ - public boolean isTruncated() - { + public boolean isTruncated() { return isTruncated; } - - + /** * Obtain the versioning object array in the bucket. - * @return Versioning object array in the bucket. For details, see {@link VersionOrDeleteMarker}. + * + * @return Versioning object array in the bucket. For details, see + * {@link VersionOrDeleteMarker}. */ - public VersionOrDeleteMarker[] getVersions() - { - return versions; + public VersionOrDeleteMarker[] getVersions() { + if (null != versions) { + return versions.clone(); + } + return new VersionOrDeleteMarker[0]; } - - + /** * Start position for next listing (sorted by object name) + * * @return Start position for next listing */ - public String getNextKeyMarker() - { + public String getNextKeyMarker() { return nextKeyMarker; } - /** * Obtain the start position for next listing (sorted by version ID). + * * @return Start position for next listing */ - public String getNextVersionIdMarker() - { + public String getNextVersionIdMarker() { return nextVersionIdMarker; } - /** * Obtain the list of prefixes to the names of grouped objects. * * @return List of prefixes to the names of grouped objects */ - public List getCommonPrefixes() - { - if(commonPrefixes == null) { - commonPrefixes = new ArrayList(); - } + public List getCommonPrefixes() { + if (commonPrefixes == null) { + commonPrefixes = new ArrayList(); + } return commonPrefixes; } - + /** * Obtain the bucket location. + * * @return Bucket location */ - public String getLocation() - { + public String getLocation() { return location; } - - /** * Obtain the character for grouping object names. * * @return Character for grouping object names */ - public String getDelimiter() - { + public String getDelimiter() { return delimiter; } - - public boolean isAppendable() { - return isAppendable; - } - - - - @Override - public String toString() { - return "ListVersionsResult [bucketName=" + bucketName + ", prefix=" + prefix + ", keyMarker=" + keyMarker - + ", nextKeyMarker=" + nextKeyMarker + ", versionIdMarker=" + versionIdMarker + ", nextVersionIdMarker=" - + nextVersionIdMarker + ", maxKeys=" + maxKeys + ", isTruncated=" + isTruncated + ", versions=" - + Arrays.toString(versions) + ", commonPrefixes=" + commonPrefixes + ", location=" + location - + ", delimiter=" + delimiter + "]"; - } + @Override + public String toString() { + return "ListVersionsResult [bucketName=" + bucketName + ", prefix=" + prefix + ", keyMarker=" + keyMarker + + ", nextKeyMarker=" + nextKeyMarker + ", versionIdMarker=" + versionIdMarker + ", nextVersionIdMarker=" + + nextVersionIdMarker + ", maxKeys=" + maxKeys + ", isTruncated=" + isTruncated + ", versions=" + + Arrays.toString(versions) + ", commonPrefixes=" + commonPrefixes + ", location=" + location + + ", delimiter=" + delimiter + "]"; + } - - } - - diff --git a/app/src/main/java/com/obs/services/model/ModifyObjectRequest.java b/app/src/main/java/com/obs/services/model/ModifyObjectRequest.java index b95c656..ae65280 100644 --- a/app/src/main/java/com/obs/services/model/ModifyObjectRequest.java +++ b/app/src/main/java/com/obs/services/model/ModifyObjectRequest.java @@ -3,87 +3,113 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.io.File; import java.io.InputStream; /** - * 写文件内容的请求参数 + * Request parameters for writing data to a file * */ -public class ModifyObjectRequest extends AppendObjectRequest{ +public class ModifyObjectRequest extends AppendObjectRequest { - public ModifyObjectRequest() { - super(); - } + { + this.httpMethod = HttpMethodEnum.PUT; + } + + public ModifyObjectRequest() { + } /** - * 构造函数 - * @param bucketName 桶名 - * @param objectKey 文件名 + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File name */ - public ModifyObjectRequest(String bucketName, String objectKey) - { + public ModifyObjectRequest(String bucketName, String objectKey) { this.bucketName = bucketName; this.objectKey = objectKey; } /** - * 构造函数 - * @param bucketName 桶名 - * @param objectKey 文件名 - * @param file 本地文件路径 + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param file + * Local path to the file */ - public ModifyObjectRequest(String bucketName, String objectKey, File file) - { - this(bucketName, objectKey); + public ModifyObjectRequest(String bucketName, String objectKey, File file) { + this.bucketName = bucketName; + this.objectKey = objectKey; this.file = file; } /** - * 构造函数 - * @param bucketName 桶名 - * @param objectKey 文件名 - * @param file 本地文件路径 - * @param position 写文件的起始位置 + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param file + * Local path to the file + * @param position + * Start position for writing data to a file */ - public ModifyObjectRequest(String bucketName, String objectKey, File file, long position) - { - this(bucketName, objectKey, file); - this.position = position; + public ModifyObjectRequest(String bucketName, String objectKey, File file, long position) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.file = file; + this.position = position; } /** - * 构造函数 - * @param bucketName 桶名 - * @param objectKey 文件名 - * @param input 待上传的数据流 + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param input + * Data stream to be uploaded */ - public ModifyObjectRequest(String bucketName, String objectKey, InputStream input) - { - this(bucketName, objectKey); - this.input = input; + public ModifyObjectRequest(String bucketName, String objectKey, InputStream input) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.input = input; } /** - * 构造函数 - * @param bucketName 桶名 - * @param objectKey 文件名 - * @param input 待上传的数据流 - * @param position 写文件的起始位置 + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param input + * Data stream to be uploaded + * @param position + * Start position for writing data to a file */ - public ModifyObjectRequest(String bucketName, String objectKey, InputStream input, long position) - { - this(bucketName, objectKey, input); - this.position = position; + public ModifyObjectRequest(String bucketName, String objectKey, InputStream input, long position) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.input = input; + this.position = position; } } diff --git a/app/src/main/java/com/obs/services/model/ModifyObjectResult.java b/app/src/main/java/com/obs/services/model/ModifyObjectResult.java index 8d43686..867476d 100644 --- a/app/src/main/java/com/obs/services/model/ModifyObjectResult.java +++ b/app/src/main/java/com/obs/services/model/ModifyObjectResult.java @@ -11,10 +11,11 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** - * 修改写文件响应结果 + * Response to the file writing request. * */ public class ModifyObjectResult extends HeaderResponse { diff --git a/app/src/main/java/com/obs/services/model/MonitorableProgressListener.java b/app/src/main/java/com/obs/services/model/MonitorableProgressListener.java new file mode 100644 index 0000000..61e1ccd --- /dev/null +++ b/app/src/main/java/com/obs/services/model/MonitorableProgressListener.java @@ -0,0 +1,140 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +import java.util.concurrent.atomic.AtomicInteger; + +import com.obs.log.ILogger; +import com.obs.log.LoggerBuilder; + +/** + * Implementation class of the data transmission listener that can monitor the running status of subprocesses + * + * @since 3.20.3 + */ +public abstract class MonitorableProgressListener implements ProgressListener { + private static final ILogger ILOG = LoggerBuilder.getLogger(MonitorableProgressListener.class); + + // Record the number of running subtasks. + private AtomicInteger runningTask = new AtomicInteger(1); + + /** + * Check whether the request task is running. + *
    + * This method is used by the parent thread to monitor whether the current thread has completely stopped + * running after the current thread executes the interrupt() method. + * + * @return If there are still running subtasks, true is returned. Otherwise, false is returned. + * @since 3.20.3 + */ + public final boolean isRunning() { + return this.runningTask.get() > 0; + } + + /** + * Wait until the request task is completely executed. Before the task execution completes, + * this method is blocked.
    + *
    + * This method is used to check whether the upload subtask is complete after the current thread + * executes the interrupt() method.
    + * + * @return If the task is complete, true is returned. Otherwise, false is returned. + * @since 3.20.3 + * @throws InterruptedException + * This exception is thrown when the thread is in waiting, sleep, + * or occupied state before or during the activity and the thread is interrupted. + */ + public final boolean waitingFinish() throws InterruptedException { + return waitingFinish(-1L); + } + + /** + * Wait until the request task is completely executed. Before the task is completely executed, + * the method is blocked until the timeout interval is exceeded.
    + *
    + * This method is used to check whether the upload subtask is complete after the current thread + * executes the interrupt() method.
    + * + * @param timeout + * Timeout interval, in milliseconds. If the parameter value is smaller than or equal to 0, + * the waiting never times out. + * @return If the task is complete, true is returned. Otherwise, false is returned. + * @since 3.20.3 + * @throws InterruptedException + * This exception is thrown when the thread is in waiting, sleep, or occupied state before or during + * the activity and the thread is interrupted. + */ + public final boolean waitingFinish(long timeout) throws InterruptedException { + long start = System.currentTimeMillis(); + if (ILOG.isDebugEnabled()) { + ILOG.debug("this.runningTask = " + this.runningTask); + } + while (this.runningTask.get() > 0) { + if (System.currentTimeMillis() - start > timeout && timeout > 0) { + if (ILOG.isWarnEnabled()) { + ILOG.warn("DownloadFileReqeust is not finish. " + this.toString()); + } + return false; + } + + Thread.sleep(100L); + } + + return true; + } + + /** + * Start a subtask.
    + *
    + * Note: Generally, users are not advised to call this method, + * because calling this method may lead to failures of the waitingFinish and isRunning methods. + * The SDK uses this method to adjust the number of running subtasks. + * The user determines whether the requested task is complete.
    + *
    + * Reference: {@link #waitingFinish(long)}, {@link #isRunning()} + * + * @since 3.20.3 + */ + public final void startOneTask() { + this.runningTask.incrementAndGet(); + } + + /** + * End a subtask.
    + *
    + * Note: Generally, users are not advised to call this method, + * because calling this method may lead to failures of the waitingFinish and isRunning methods. + * The SDK uses this method to adjust the number of running subtasks. + * The user determines whether the requested task is complete.
    + *
    + * Reference: {@link #waitingFinish(long)}, {@link #isRunning()} + * + * @since 3.20.3 + */ + public final void finishOneTask() { + this.runningTask.decrementAndGet(); + } + + /** + * Reset the listener.
    + *
    + * This method is used when a request is repeatedly used. + * + * @since 3.20.3 + */ + public final void reset() { + this.runningTask.set(1); + } +} diff --git a/app/src/main/java/com/obs/services/model/Multipart.java b/app/src/main/java/com/obs/services/model/Multipart.java index 6ce94d5..1197673 100644 --- a/app/src/main/java/com/obs/services/model/Multipart.java +++ b/app/src/main/java/com/obs/services/model/Multipart.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.Date; @@ -20,29 +21,32 @@ /** * Part information in a multipart upload */ -public class Multipart extends HeaderResponse -{ +public class Multipart extends HeaderResponse { private Integer partNumber; - + private Date lastModified; - + private String etag; - + private Long size; - - public Multipart(){ - + + public Multipart() { + } - + /** * Constructor - * @param partNumber Part number - * @param lastModified Last modification time of the part - * @param etag Part ETag - * @param size Part size, in bytes + * + * @param partNumber + * Part number + * @param lastModified + * Last modification time of the part + * @param etag + * Part ETag + * @param size + * Part size, in bytes */ - public Multipart(Integer partNumber, Date lastModified, String etag, Long size) - { + public Multipart(Integer partNumber, Date lastModified, String etag, Long size) { this.partNumber = partNumber; this.lastModified = ServiceUtils.cloneDateIgnoreNull(lastModified); this.etag = etag; @@ -54,50 +58,40 @@ public Multipart(Integer partNumber, Date lastModified, String etag, Long size) * * @return Part number */ - public Integer getPartNumber() - { + public Integer getPartNumber() { return partNumber; } - - + /** * Obtain the last modification time of the part. * * @return Last modification time of the part */ - public Date getLastModified() - { + public Date getLastModified() { return ServiceUtils.cloneDateIgnoreNull(this.lastModified); } - - + /** * Obtain the ETag of the part. * * @return Part ETag */ - public String getEtag() - { + public String getEtag() { return etag; } - - + /** * Obtain the part size, in bytes. * * @return Part size */ - public Long getSize() - { + public Long getSize() { return size; } - @Override - public String toString() - { - return "Multipart [partNumber=" + partNumber + ", lastModified=" + lastModified + ", etag=" + etag + ", size=" + size + "]"; + public String toString() { + return "Multipart [partNumber=" + partNumber + ", lastModified=" + lastModified + ", etag=" + etag + ", size=" + + size + "]"; } } - - diff --git a/app/src/main/java/com/obs/services/model/MultipartUpload.java b/app/src/main/java/com/obs/services/model/MultipartUpload.java index ef8544f..60380d3 100644 --- a/app/src/main/java/com/obs/services/model/MultipartUpload.java +++ b/app/src/main/java/com/obs/services/model/MultipartUpload.java @@ -21,139 +21,125 @@ /** * Multipart upload */ -public class MultipartUpload -{ +public class MultipartUpload { private String uploadId; - + private String bucketName; - + private String objectKey; - + private Date initiatedDate; - + private StorageClassEnum storageClass; - + private Owner owner; - + private Owner initiator; - - public MultipartUpload(String uploadId, String objectKey, Date initiatedDate, - StorageClassEnum storageClass, Owner owner, Owner initiator) { - super(); - this.uploadId = uploadId; - this.objectKey = objectKey; - this.initiatedDate = ServiceUtils.cloneDateIgnoreNull(initiatedDate); - this.storageClass = storageClass; - this.owner = owner; - this.initiator = initiator; - } - + + public MultipartUpload(String uploadId, String objectKey, Date initiatedDate, StorageClassEnum storageClass, + Owner owner, Owner initiator) { + super(); + this.uploadId = uploadId; + this.objectKey = objectKey; + this.initiatedDate = ServiceUtils.cloneDateIgnoreNull(initiatedDate); + this.storageClass = storageClass; + this.owner = owner; + this.initiator = initiator; + } + public MultipartUpload(String uploadId, String bucketName, String objectKey, Date initiatedDate, - StorageClassEnum storageClass, Owner owner, Owner initiator) { - super(); - this.uploadId = uploadId; - this.bucketName = bucketName; - this.objectKey = objectKey; - this.initiatedDate = ServiceUtils.cloneDateIgnoreNull(initiatedDate); - this.storageClass = storageClass; - this.owner = owner; - this.initiator = initiator; - } - - - /** + StorageClassEnum storageClass, Owner owner, Owner initiator) { + super(); + this.uploadId = uploadId; + this.bucketName = bucketName; + this.objectKey = objectKey; + this.initiatedDate = ServiceUtils.cloneDateIgnoreNull(initiatedDate); + this.storageClass = storageClass; + this.owner = owner; + this.initiator = initiator; + } + + /** * Creator of the multipart upload * * @return Creator of the multipart upload */ - public Owner getInitiator() - { + public Owner getInitiator() { return initiator; } - - + /** * Query the creator of the multipart upload. * * @return Owner of the multipart upload */ - public Owner getOwner() - { + public Owner getOwner() { return owner; } - - + /** * Obtain the multipart upload ID. * * @return Multipart upload ID */ - public String getUploadId() - { + public String getUploadId() { return uploadId; } - - + /** * Obtain the name of the bucket to which the multipart upload belongs. * * @return Name of the bucket to which the multipart upload belongs */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } - + public void setBucketName(String bucketName) { - this.bucketName = bucketName; + this.bucketName = bucketName; } - - + /** * Obtain the name of the object involved in the multipart upload. * * @return Name of the object involved in the multipart upload */ - public String getObjectKey() - { + public String getObjectKey() { return objectKey; } - - + /** - * Obtain the storage class of the object generated via the multipart upload. + * Obtain the storage class of the object generated via the multipart + * upload. * * @return Storage class of the object generated via the multipart upload */ @Deprecated - public String getStorageClass() - { + public String getStorageClass() { return storageClass != null ? storageClass.getCode() : null; } - - + /** - * Obtain the storage class of the object generated via the multipart upload. + * Obtain the storage class of the object generated via the multipart + * upload. * * @return Storage class of the object generated via the multipart upload */ - public StorageClassEnum getObjectStorageClass() - { + public StorageClassEnum getObjectStorageClass() { return storageClass; } - - + /** * Obtain the creation time of the multipart upload. * * @return Creation time of the multipart upload */ - public Date getInitiatedDate() - { + public Date getInitiatedDate() { return ServiceUtils.cloneDateIgnoreNull(this.initiatedDate); } - - -} + public void setObjectKey(String objectKey) { + this.objectKey = objectKey; + } +} diff --git a/app/src/main/java/com/obs/services/model/MultipartUploadListing.java b/app/src/main/java/com/obs/services/model/MultipartUploadListing.java index a618a2d..766154f 100644 --- a/app/src/main/java/com/obs/services/model/MultipartUploadListing.java +++ b/app/src/main/java/com/obs/services/model/MultipartUploadListing.java @@ -21,179 +21,265 @@ /** * Response to a request for listing multipart uploads */ -public class MultipartUploadListing extends HeaderResponse -{ +public class MultipartUploadListing extends HeaderResponse { private String bucketName; - + private String keyMarker; - + private String uploadIdMarker; - + private String nextKeyMarker; - + private String nextUploadIdMarker; - + private String prefix; - + private int maxUploads; - + private boolean truncated; - + private List multipartTaskList; - + private String delimiter; - + private String[] commonPrefixes; + + @Deprecated + //CHECKSTYLE:OFF + public MultipartUploadListing(String bucketName, String keyMarker, String uploadIdMarker, String nextKeyMarker, + String nextUploadIdMarker, String prefix, int maxUploads, boolean truncated, + List multipartTaskList, String delimiter, String[] commonPrefixes) { + super(); + this.bucketName = bucketName; + this.keyMarker = keyMarker; + this.uploadIdMarker = uploadIdMarker; + this.nextKeyMarker = nextKeyMarker; + this.nextUploadIdMarker = nextUploadIdMarker; + this.prefix = prefix; + this.maxUploads = maxUploads; + this.truncated = truncated; + this.multipartTaskList = multipartTaskList; + this.delimiter = delimiter; + if (null != commonPrefixes) { + this.commonPrefixes = commonPrefixes.clone(); + } else { + this.commonPrefixes = null; + } + } + + private MultipartUploadListing(Builder builder) { + super(); + this.bucketName = builder.bucketName; + this.keyMarker = builder.keyMarker; + this.uploadIdMarker = builder.uploadIdMarker; + this.nextKeyMarker = builder.nextKeyMarker; + this.nextUploadIdMarker = builder.nextUploadIdMarker; + this.prefix = builder.prefix; + this.maxUploads = builder.maxUploads; + this.truncated = builder.truncated; + this.multipartTaskList = builder.multipartTaskList; + this.delimiter = builder.delimiter; + if (null != builder.commonPrefixes) { + this.commonPrefixes = builder.commonPrefixes.clone(); + } else { + this.commonPrefixes = null; + } + } + public static final class Builder { + private String bucketName; + private String keyMarker; + private String uploadIdMarker; + private String nextKeyMarker; + private String nextUploadIdMarker; + private String prefix; + private int maxUploads; + private boolean truncated; + private List multipartTaskList; + private String delimiter; + private String[] commonPrefixes; + + public Builder bucketName(String bucketName) { + this.bucketName = bucketName; + return this; + } + + public Builder keyMarker(String keyMarker) { + this.keyMarker = keyMarker; + return this; + } + + public Builder uploadIdMarker(String uploadIdMarker) { + this.uploadIdMarker = uploadIdMarker; + return this; + } + + public Builder nextKeyMarker(String nextKeyMarker) { + this.nextKeyMarker = nextKeyMarker; + return this; + } + + public Builder nextUploadIdMarker(String nextUploadIdMarker) { + this.nextUploadIdMarker = nextUploadIdMarker; + return this; + } + + public Builder prefix(String prefix) { + this.prefix = prefix; + return this; + } + + public Builder maxUploads(int maxUploads) { + this.maxUploads = maxUploads; + return this; + } + + public Builder truncated(boolean truncated) { + this.truncated = truncated; + return this; + } + + public Builder multipartTaskList(List multipartTaskList) { + this.multipartTaskList = multipartTaskList; + return this; + } + + public Builder delimiter(String delimiter) { + this.delimiter = delimiter; + return this; + } + + public Builder commonPrefixes(String[] commonPrefixes) { + if (null != commonPrefixes) { + this.commonPrefixes = commonPrefixes.clone(); + } else { + this.commonPrefixes = null; + } + return this; + } + + public MultipartUploadListing builder() { + return new MultipartUploadListing(this); + } + } - public MultipartUploadListing(String bucketName, String keyMarker, String uploadIdMarker, String nextKeyMarker, - String nextUploadIdMarker, String prefix, int maxUploads, boolean truncated, - List multipartTaskList, String delimiter, String[] commonPrefixes) { - super(); - this.bucketName = bucketName; - this.keyMarker = keyMarker; - this.uploadIdMarker = uploadIdMarker; - this.nextKeyMarker = nextKeyMarker; - this.nextUploadIdMarker = nextUploadIdMarker; - this.prefix = prefix; - this.maxUploads = maxUploads; - this.truncated = truncated; - this.multipartTaskList = multipartTaskList; - this.delimiter = delimiter; - this.commonPrefixes = commonPrefixes; - } - - - /** - * Check whether the query result list is truncated. Value "true" indicates that the results are incomplete while value "false" indicates that the results are complete. + /** + * Check whether the query result list is truncated. Value "true" indicates + * that the results are incomplete while value "false" indicates that the + * results are complete. + * * @return Truncation identifier */ - public boolean isTruncated() - { + public boolean isTruncated() { return truncated; } - - + /** * Obtain the list of prefixes to the names of grouped objects. * * @return List of prefixes to the names of grouped objects */ - public String[] getCommonPrefixes() - { - return commonPrefixes; + public String[] getCommonPrefixes() { + if (null != commonPrefixes) { + return commonPrefixes.clone(); + } + return new String[0]; } - - + /** - * Obtain the start position for listing multipart uploads in the request (sorted by multipart upload ID). + * Obtain the start position for listing multipart uploads in the request + * (sorted by multipart upload ID). * * @return Start position for listing multipart uploads in the request */ - public String getUploadIdMarker() - { + public String getUploadIdMarker() { return uploadIdMarker; } - - + /** * Start position for next listing (sorted by object name) * * @return Start position for next listing */ - public String getNextKeyMarker() - { + public String getNextKeyMarker() { return nextKeyMarker; } - - + /** - * Obtain the start position for next listing (sorted by multipart upload ID). + * Obtain the start position for next listing (sorted by multipart upload + * ID). * * @return Start position for next listing */ - public String getNextUploadIdMarker() - { + public String getNextUploadIdMarker() { return nextUploadIdMarker; } - - + /** * Obtain the list of multipart uploads unfinished in the bucket. * * @return List of multipart uploads unfinished in the bucket */ - public List getMultipartTaskList() - { - if(this.multipartTaskList == null) { - this.multipartTaskList = new ArrayList(); - } + public List getMultipartTaskList() { + if (this.multipartTaskList == null) { + this.multipartTaskList = new ArrayList(); + } return multipartTaskList; } - - + /** * Obtain the name of the bucket to which the multipart uploads belong. * * @return Name of the bucket to which the multipart uploads belong */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } - - + /** * Obtain the delimiter in the request for listing multipart uploads. * * @return Delimiter in the request for listing multipart uploads */ - public String getDelimiter() - { + public String getDelimiter() { return delimiter; } - - + /** - * Obtain the start position for listing multipart uploads (sorted by object name) + * Obtain the start position for listing multipart uploads (sorted by object + * name) * * @return Start position for listing multipart uploads */ - public String getKeyMarker() - { + public String getKeyMarker() { return keyMarker; } - - + /** - * Obtain the maximum number of multipart uploads to be listed. + * Obtain the maximum number of multipart uploads to be listed. * * @return Maximum number of multipart uploads to be listed */ - public int getMaxUploads() - { + public int getMaxUploads() { return maxUploads; } - - + /** * Obtain the prefix for listing multipart uploads. + * * @return Prefix for listing multipart uploads */ - public String getPrefix() { - return prefix; - } - - - @Override - public String toString() { - return "MultipartUploadListing [bucketName=" + bucketName + ", keyMarker=" + keyMarker + ", uploadIdMarker=" - + uploadIdMarker + ", nextKeyMarker=" + nextKeyMarker + ", nextUploadIdMarker=" + nextUploadIdMarker - + ", prefix=" + prefix + ", maxUploads=" + maxUploads + ", truncated=" + truncated - + ", multipartTaskList=" + multipartTaskList + ", delimiter=" + delimiter + ", commonPrefixes=" - + Arrays.toString(commonPrefixes) + "]"; - } - -} + public String getPrefix() { + return prefix; + } + @Override + public String toString() { + return "MultipartUploadListing [bucketName=" + bucketName + ", keyMarker=" + keyMarker + ", uploadIdMarker=" + + uploadIdMarker + ", nextKeyMarker=" + nextKeyMarker + ", nextUploadIdMarker=" + nextUploadIdMarker + + ", prefix=" + prefix + ", maxUploads=" + maxUploads + ", truncated=" + truncated + + ", multipartTaskList=" + multipartTaskList + ", delimiter=" + delimiter + ", commonPrefixes=" + + Arrays.toString(commonPrefixes) + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/ObjectListing.java b/app/src/main/java/com/obs/services/model/ObjectListing.java index aaa6c36..bfef07c 100644 --- a/app/src/main/java/com/obs/services/model/ObjectListing.java +++ b/app/src/main/java/com/obs/services/model/ObjectListing.java @@ -20,170 +20,283 @@ /** * Response to a request for listing objects in a bucket */ -public class ObjectListing extends HeaderResponse -{ +public class ObjectListing extends HeaderResponse { private List objectSummaries; - + private List commonPrefixes; - + + private List extendCommonPrefixes; + private String bucketName; - + private boolean truncated; - + private String prefix; - + private String marker; - + private int maxKeys; - + private String delimiter; - + private String nextMarker; - + private String location; + + @Deprecated + //CHECKSTYLE:OFF + public ObjectListing(List objectSummaries, List commonPrefixes, String bucketName, + boolean truncated, String prefix, String marker, int maxKeys, String delimiter, String nextMarker, + String location) { + super(); + this.objectSummaries = objectSummaries; + this.commonPrefixes = commonPrefixes; + this.bucketName = bucketName; + this.truncated = truncated; + this.prefix = prefix; + this.marker = marker; + this.maxKeys = maxKeys; + this.delimiter = delimiter; + this.nextMarker = nextMarker; + this.location = location; + } + + @Deprecated + //CHECKSTYLE:OFF + public ObjectListing(List objectSummaries, List commonPrefixes, String bucketName, + boolean truncated, String prefix, String marker, int maxKeys, String delimiter, String nextMarker, + String location, List extendCommonPrefixes) { + this(objectSummaries, commonPrefixes, bucketName, truncated, prefix, marker, maxKeys, delimiter, nextMarker, + location); + this.extendCommonPrefixes = extendCommonPrefixes; + } + + private ObjectListing(Builder builder) { + super(); + this.objectSummaries = builder.objectSummaries; + this.commonPrefixes = builder.commonPrefixes; + this.bucketName = builder.bucketName; + this.truncated = builder.truncated; + this.prefix = builder.prefix; + this.marker = builder.marker; + this.maxKeys = builder.maxKeys; + this.delimiter = builder.delimiter; + this.nextMarker = builder.nextMarker; + this.location = builder.location; + this.extendCommonPrefixes = builder.extendCommonPrefixes; + } + public static final class Builder { + private List objectSummaries; + private List commonPrefixes; + private List extendCommonPrefixes; + private String bucketName; + private boolean truncated; + private String prefix; + private String marker; + private int maxKeys; + private String delimiter; + private String nextMarker; + private String location; + + public Builder objectSummaries(List objectSummaries) { + this.objectSummaries = objectSummaries; + return this; + } + + public Builder commonPrefixes(List commonPrefixes) { + this.commonPrefixes = commonPrefixes; + return this; + } + + @Deprecated + public Builder extenedCommonPrefixes(List extendCommonPrefixes) { + this.extendCommonPrefixes = extendCommonPrefixes; + return this; + } + + public Builder extendCommonPrefixes(List extendCommonPrefixes) { + this.extendCommonPrefixes = extendCommonPrefixes; + return this; + } + + public Builder bucketName(String bucketName) { + this.bucketName = bucketName; + return this; + } + + public Builder truncated(boolean truncated) { + this.truncated = truncated; + return this; + } + + public Builder prefix(String prefix) { + this.prefix = prefix; + return this; + } + + public Builder marker(String marker) { + this.marker = marker; + return this; + } + + public Builder maxKeys(int maxKeys) { + this.maxKeys = maxKeys; + return this; + } + + public Builder delimiter(String delimiter) { + this.delimiter = delimiter; + return this; + } + + public Builder nextMarker(String nextMarker) { + this.nextMarker = nextMarker; + return this; + } + + public Builder location(String location) { + this.location = location; + return this; + } + + public ObjectListing builder() { + return new ObjectListing(this); + } + } - public ObjectListing(List objectSummaries, List commonPrefixes, String bucketName, - boolean truncated, String prefix, String marker, int maxKeys, String delimiter, String nextMarker, - String location) { - super(); - this.objectSummaries = objectSummaries; - this.commonPrefixes = commonPrefixes; - this.bucketName = bucketName; - this.truncated = truncated; - this.prefix = prefix; - this.marker = marker; - this.maxKeys = maxKeys; - this.delimiter = delimiter; - this.nextMarker = nextMarker; - this.location = location; - } - - /** + + /** * Obtain the start position for next listing. + * * @return Start position for next listing */ - public String getNextMarker() - { + public String getNextMarker() { return nextMarker; } - /** * Obtain the list of objects in the bucket. + * * @return List of objects in the bucket */ - public List getObjects() - { - if(this.objectSummaries == null) { - this.objectSummaries = new ArrayList(); - } + public List getObjects() { + if (this.objectSummaries == null) { + this.objectSummaries = new ArrayList(); + } return objectSummaries; } - + @Deprecated - public List getObjectSummaries() - { - List objects = new ArrayList(this.objectSummaries.size()); - objects.addAll(this.objectSummaries); + public List getObjectSummaries() { + List objects = new ArrayList(this.objectSummaries.size()); + objects.addAll(this.objectSummaries); return objects; } - + /** * Obtain the list of prefixes to the names of grouped objects. * * @return List of prefixes to the names of grouped objects */ - public List getCommonPrefixes() - { - if(this.commonPrefixes == null) { - this.commonPrefixes = new ArrayList(); - } + public List getCommonPrefixes() { + if (this.commonPrefixes == null) { + this.commonPrefixes = new ArrayList(); + } return commonPrefixes; } - - + + /** + * Obtain the list of prefixes to the names of grouped objects. + * + * @return List of prefixes to the names of grouped objects + */ + + public List getExtendCommonPrefixes() { + if (this.extendCommonPrefixes == null) { + this.extendCommonPrefixes = new ArrayList(); + } + return extendCommonPrefixes; + } + + @Deprecated + public List getExtenedCommonPrefixes() { + return getExtendCommonPrefixes(); + } + + + /** * Obtain the bucket name. * * @return Bucket name */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } - - + /** - * Check whether the query result list is truncated. Value "true" indicates that the results are incomplete while value "false" indicates that the results are complete. + * Check whether the query result list is truncated. Value "true" indicates + * that the results are incomplete while value "false" indicates that the + * results are complete. + * * @return Truncation identifier */ - public boolean isTruncated() - { + public boolean isTruncated() { return truncated; } - - + /** * Obtain the object name prefix used for filtering objects to be listed. + * * @return Object name prefix used for listing versioning objects */ - public String getPrefix() - { + public String getPrefix() { return prefix; } - - + /** * Obtain the start position for listing objects. + * * @return Start position for listing objects */ - public String getMarker() - { + public String getMarker() { return marker; } - - + /** * Obtain the maximum number of objects to be listed. + * * @return Maximum number of objects to be listed */ - public int getMaxKeys() - { + public int getMaxKeys() { return maxKeys; } - - + /** * Obtain the character for grouping object names. * * @return Character for grouping object names */ - public String getDelimiter() - { + public String getDelimiter() { return delimiter; } - - + /** * Obtain the bucket location. + * * @return Bucket location */ - public String getLocation() - { + public String getLocation() { return location; } - @Override - public String toString() - { - return "ObjectListing [objectSummaries=" + objectSummaries + ", commonPrefixes=" + commonPrefixes + ", bucketName=" + bucketName - + ", truncated=" + truncated + ", prefix=" + prefix + ", marker=" + marker + ", maxKeys=" + maxKeys + ", delimiter=" + delimiter - + ", nextMarker=" + nextMarker + ", location=" + location + "]"; + public String toString() { + return "ObjectListing [objectSummaries=" + objectSummaries + ", commonPrefixes=" + commonPrefixes + + ", bucketName=" + bucketName + ", truncated=" + truncated + ", prefix=" + prefix + ", marker=" + + marker + ", maxKeys=" + maxKeys + ", delimiter=" + delimiter + ", nextMarker=" + nextMarker + + ", location=" + location + "]"; } - - -} - +} diff --git a/app/src/main/java/com/obs/services/model/ObjectMetadata.java b/app/src/main/java/com/obs/services/model/ObjectMetadata.java index 558154c..b1185d1 100644 --- a/app/src/main/java/com/obs/services/model/ObjectMetadata.java +++ b/app/src/main/java/com/obs/services/model/ObjectMetadata.java @@ -1,318 +1,435 @@ /** -* -* JetS3t : Java S3 Toolkit -* Project hosted at http://bitbucket.org/jmurty/jets3t/ -* -* Copyright 2006-2010 James Murty -* -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ package com.obs.services.model; import java.util.Date; import java.util.Map; import java.util.Map.Entry; +import java.util.TreeMap; import com.obs.services.internal.utils.ServiceUtils; /** * Object properties */ -public class ObjectMetadata extends HeaderResponse -{ +public class ObjectMetadata extends HeaderResponse { private Date lastModified; - + private Long contentLength; - + private String contentType; - + private String contentEncoding; + + private String contentDisposition; - private String etag; + private String cacheControl; - private String contentMd5; + private String contentLanguage; - private StorageClassEnum storageClass; + private String expires; + private String etag; + + private String contentMd5; + + private String crc64; + + private StorageClassEnum storageClass; + private String webSiteRedirectLocation; - + private long nextPosition = -1; - + private boolean appendable; - + + private Map userMetadata; + public ObjectMetadata() { - - } - + + } + /** * Identify whether an object is appendable. + * * @return Identifier specifying whether the object is appendable */ - public boolean isAppendable() { - return appendable; - } - - public void setAppendable(boolean appendable) { - this.appendable = appendable; - } - - /** - * Obtain the start position for next appending. This setting is valid only when "isAppendable" is set to "true" and this parameter value is larger than "0". - * @return Position from which the next appending starts - */ - public long getNextPosition() { - return nextPosition; - } - - public void setNextPosition(long nextPosition) { - this.nextPosition = nextPosition; - } - - /** + public boolean isAppendable() { + return appendable; + } + + public void setAppendable(boolean appendable) { + this.appendable = appendable; + } + + /** + * Obtain the start position for next appending. This setting is valid only + * when "isAppendable" is set to "true" and this parameter value is larger + * than "0". + * + * @return Position from which the next appending starts + */ + public long getNextPosition() { + return nextPosition; + } + + public void setNextPosition(long nextPosition) { + this.nextPosition = nextPosition; + } + + /** * Obtain object properties. * * @return Object properties */ - public Map getMetadata() - { - return this.getResponseHeaders(); + public Map getAllMetadata() { + if (userMetadata == null) { + userMetadata = new TreeMap(String.CASE_INSENSITIVE_ORDER); + } + return this.userMetadata; } - + + @Deprecated + public Map getMetadata() { + return this.getResponseHeaders(); + } + /** * Add customized metadata for an object. - * @param key Keyword of the customized metadata - * @param value Value of the customized metadata + * + * @param key + * Keyword of the customized metadata + * @param value + * Value of the customized metadata */ - public void addUserMetadata(String key, String value){ - getMetadata().put(key, value); + public void addUserMetadata(String key, String value) { + getAllMetadata().put(key, value); } - + /** * Obtain the customized metadata of an object. - * @param key Keyword of the customized metadata + * + * @param key + * Keyword of the customized metadata * @return Value of the customized metadata */ - public Object getUserMetadata(String key){ - return getMetadata().get(key); + public Object getUserMetadata(String key) { + return getAllMetadata().get(key); } - - /** + + /** * Obtain the ETag of the object. * * @return ETag of the object */ - public String getEtag() - { + public String getEtag() { return etag; } - - public void setEtag(String objEtag) - { + + public void setEtag(String objEtag) { this.etag = objEtag; } - - - + /** * Set object properties. * - * @param metadata Object properties + * @param metadata + * Object properties */ - public void setMetadata(Map metadata) - { + public void setMetadata(Map metadata) { this.responseHeaders = metadata; } - + /** * Obtain the last modification time of the object. * * @return Last modification time of the object */ - public Date getLastModified() - { + public Date getLastModified() { return ServiceUtils.cloneDateIgnoreNull(this.lastModified); } - - public void setLastModified(Date lastModified) - { + + public void setLastModified(Date lastModified) { this.lastModified = ServiceUtils.cloneDateIgnoreNull(lastModified); } - - /** + + /** * Obtain the content encoding of the object. * * @return Content encoding */ - public String getContentEncoding() - { + public String getContentEncoding() { return contentEncoding; } - + /** * Set the content encoding of the object. - * @param contentEncoding Content encoding + * + * @param contentEncoding + * Content encoding */ - public void setContentEncoding(String contentEncoding) - { + public void setContentEncoding(String contentEncoding) { this.contentEncoding = contentEncoding; } + + /** + * + * @return + * + * @since 3.20.7 + */ + public String getContentDisposition() { + return contentDisposition; + } + + /** + * 设置对象的Content-Disposition + * + * @param contentDisposition + * + * @since 3.20.7 + */ + public void setContentDisposition(String contentDisposition) { + this.contentDisposition = contentDisposition; + } + + /** + * + * @return + * + * @since 3.20.7 + */ + public String getCacheControl() { + return cacheControl; + } + + /** + * + * @param cacheControl + * + * @since 3.20.7 + */ + public void setCacheControl(String cacheControl) { + this.cacheControl = cacheControl; + } + + /** + * + * @return + * + * @since 3.20.7 + */ + public String getContentLanguage() { + return contentLanguage; + } + + /** + * + * @param contentLanguage + * + * @since 3.20.7 + */ + public void setContentLanguage(String contentLanguage) { + this.contentLanguage = contentLanguage; + } + + /** + * + * @return + * + * @since 3.20.7 + */ + public String getExpires() { + return expires; + } + + /** + * + * @param expires + * + * @since 3.20.7 + */ + public void setExpires(String expires) { + this.expires = expires; + } - /** + /** * Obtain the content length of an object. * * @return Content length of the object */ - public Long getContentLength() - { + public Long getContentLength() { return contentLength; } - - /** + + /** * Set the content length of an object. * - * @param contentLength Content length of the object + * @param contentLength + * Content length of the object */ - public void setContentLength(Long contentLength) - { + public void setContentLength(Long contentLength) { this.contentLength = contentLength; } - - /** + + /** * Obtain the MIME type of an object. * * @return MIME type of the object */ - public String getContentType() - { + public String getContentType() { return contentType; } - - /** Set the MIME type for an object. + + /** + * Set the MIME type for an object. * - * @param contentType MIME type of the object + * @param contentType + * MIME type of the object */ - public void setContentType(String contentType) - { + public void setContentType(String contentType) { this.contentType = contentType; } /** - * Obtain the object storage class. + * Obtain the object storage class. + * * @return Object storage class */ @Deprecated - public String getStorageClass() - { + public String getStorageClass() { return this.storageClass != null ? this.storageClass.getCode() : null; } - + /** - * Set the object storage class. - * @param storageClass Object storage class + * Set the object storage class. + * + * @param storageClass + * Object storage class */ @Deprecated - public void setStorageClass(String storageClass) - { + public void setStorageClass(String storageClass) { this.storageClass = StorageClassEnum.getValueFromCode(storageClass); } - + /** - * Obtain the object storage class. + * Obtain the object storage class. + * * @return Object storage class */ - public StorageClassEnum getObjectStorageClass() - { + public StorageClassEnum getObjectStorageClass() { return storageClass; } - + /** - * Set the object storage class. - * @param storageClass Object storage class + * Set the object storage class. + * + * @param storageClass + * Object storage class */ - public void setObjectStorageClass(StorageClassEnum storageClass) - { + public void setObjectStorageClass(StorageClassEnum storageClass) { this.storageClass = storageClass; } - + public Object getValue(String name) { - for (Entry entry: this.getMetadata().entrySet()) { - if (isMatching(entry.getKey(), name)) { + for (Entry entry : this.getAllMetadata().entrySet()) { + if (isEqualsIgnoreCase(entry.getKey(), name)) { return entry.getValue(); } } return null; } - - private boolean isMatching(String name1, String name2) { - if (name1 == null && name2 == null) { + + private boolean isEqualsIgnoreCase(String key1, String key2) { + if (key1 == null && key2 == null) { return true; } - // No match if one or other is null, but both are not - if (name1 == null || name2 == null) { + if (key1 == null || key2 == null) { return false; } - // Match if lower-cased names are equivalent - return name1.toLowerCase().equals(name2.toLowerCase()); - } - + + return key1.equalsIgnoreCase(key2); + } + /** * Obtain Base64-encoded MD5 value of an object. + * * @return Base64-encoded MD5 value of the object */ - public String getContentMd5() - { + public String getContentMd5() { return contentMd5; } /** * Set the Base64-encoded MD5 value for an object. - * @param contentMd5 Base64-encoded MD5 value of the object + * + * @param contentMd5 + * Base64-encoded MD5 value of the object */ - public void setContentMd5(String contentMd5) - { + public void setContentMd5(String contentMd5) { this.contentMd5 = contentMd5; } + public String getCrc64() { + return crc64; + } + + public void setCrc64(String crc64) { + this.crc64 = crc64; + } + /** - * Set the redirection link which can redirect the request to another object in the bucket or to an external URL. + * Set the redirection link which can redirect the request to another object + * in the bucket or to an external URL. * * @return Redirection link */ - public String getWebSiteRedirectLocation() - { + public String getWebSiteRedirectLocation() { return webSiteRedirectLocation; } - + /** - * Obtain the redirection link which can redirect the request to another object in the bucket or to an external URL. + * Obtain the redirection link which can redirect the request to another + * object in the bucket or to an external URL. * - * @param webSiteRedirectLocation Redirection link + * @param webSiteRedirectLocation + * Redirection link */ - public void setWebSiteRedirectLocation(String webSiteRedirectLocation) - { + public void setWebSiteRedirectLocation(String webSiteRedirectLocation) { this.webSiteRedirectLocation = webSiteRedirectLocation; } - @Override - public String toString() { - return "ObjectMetadata [metadata=" + this.getMetadata() + ", lastModified=" + lastModified + ", contentLength=" - + contentLength + ", contentType=" + contentType + ", contentEncoding=" + contentEncoding + ", etag=" - + etag + ", contentMd5=" + contentMd5 + ", storageClass=" + storageClass + ", webSiteRedirectLocation=" - + webSiteRedirectLocation + ", nextPosition=" + nextPosition + ", appendable=" + appendable + "]"; - } - - -} + /** + * Set user custom metadata + * + * @param userMetadata + * user custom metadata + */ + public void setUserMetadata(Map userMetadata) { + this.userMetadata = userMetadata; + } + @Override + public String toString() { + return "ObjectMetadata [metadata=" + this.getAllMetadata() + ", lastModified=" + lastModified + + ", contentDisposition=" + contentDisposition + ", cacheControl=" + cacheControl + + ", expires=" + expires + ", contentLength=" + contentLength + ", contentType=" + + contentType + ", contentEncoding=" + contentEncoding + ", etag=" + etag + + ", contentMd5=" + contentMd5 + ", storageClass=" + storageClass + + ", webSiteRedirectLocation=" + webSiteRedirectLocation + ", nextPosition=" + + nextPosition + ", appendable=" + appendable + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/ObjectRepleaceMetadata.java b/app/src/main/java/com/obs/services/model/ObjectRepleaceMetadata.java index 8a1338e..0c42b4d 100644 --- a/app/src/main/java/com/obs/services/model/ObjectRepleaceMetadata.java +++ b/app/src/main/java/com/obs/services/model/ObjectRepleaceMetadata.java @@ -17,137 +17,139 @@ /** * Response headers that can be rewritten during object download */ -public class ObjectRepleaceMetadata -{ - +public class ObjectRepleaceMetadata { + private String contentType; - + private String contentLanguage; - + private String expires; - + private String cacheControl; - + private String contentDisposition; - + private String contentEncoding; - + /** * Obtain the rewritten "Content-Type" header in the response. + * * @return "Content-Type" header in the response */ - public String getContentType() - { + public String getContentType() { return contentType; } - + /** * Rewrite the "Content-Type" header in the response. - * @param contentType "Content-Type" header in the response + * + * @param contentType + * "Content-Type" header in the response */ - public void setContentType(String contentType) - { + public void setContentType(String contentType) { this.contentType = contentType; } - + /** * Obtain the rewritten "Content-Language" header in the response. + * * @return "Content-Language" header in the response */ - public String getContentLanguage() - { + public String getContentLanguage() { return contentLanguage; } - + /** * Rewrite the "Content-Language" header in the response. - * @param contentLanguage "Content-Language" header in the response + * + * @param contentLanguage + * "Content-Language" header in the response */ - public void setContentLanguage(String contentLanguage) - { + public void setContentLanguage(String contentLanguage) { this.contentLanguage = contentLanguage; } - + /** * Obtain the rewritten "Expires" header in the response. + * * @return "Expires" header in the response */ - public String getExpires() - { + public String getExpires() { return expires; } - + /** * Rewrite the "Expires" header in the response. - * @param expires Rewritten "Expires" header in the response + * + * @param expires + * Rewritten "Expires" header in the response */ - public void setExpires(String expires) - { + public void setExpires(String expires) { this.expires = expires; } - + /** * Obtain the rewritten "Cache-Control" header in the response. + * * @return "Cache-Control" header in the response */ - public String getCacheControl() - { + public String getCacheControl() { return cacheControl; } - + /** * Rewrite the "Cache-Control" header in the response. - * @param cacheControl "Cache-Control" header in the response + * + * @param cacheControl + * "Cache-Control" header in the response */ - public void setCacheControl(String cacheControl) - { + public void setCacheControl(String cacheControl) { this.cacheControl = cacheControl; } - + /** * Obtain the rewritten "Content-Disposition" header in the response. + * * @return "Content-Disposition" header in the response */ - public String getContentDisposition() - { + public String getContentDisposition() { return contentDisposition; } - + /** * Rewrite the "Content-Disposition" header in the response. - * @param contentDisposition "Content-Disposition" header in the response + * + * @param contentDisposition + * "Content-Disposition" header in the response */ - public void setContentDisposition(String contentDisposition) - { + public void setContentDisposition(String contentDisposition) { this.contentDisposition = contentDisposition; } - + /** * Obtain the rewritten "Content-Encoding" header in the response. + * * @return "Content-Encoding" header in the response */ - public String getContentEncoding() - { + public String getContentEncoding() { return contentEncoding; } - + /** * Rewrite the "Content-Encoding" header in the response. - * @param contentEncoding "Content-Encoding" header in the response + * + * @param contentEncoding + * "Content-Encoding" header in the response */ - public void setContentEncoding(String contentEncoding) - { + public void setContentEncoding(String contentEncoding) { this.contentEncoding = contentEncoding; } @Override - public String toString() - { - return "ObjectRepleaceMetadata [contentType=" + contentType + ", contentLanguage=" + contentLanguage + ", expires=" + expires - + ", cacheControl=" + cacheControl + ", contentDisposition=" + contentDisposition + ", contentEncoding=" + contentEncoding - + "]"; + public String toString() { + return "ObjectRepleaceMetadata [contentType=" + contentType + ", contentLanguage=" + contentLanguage + + ", expires=" + expires + ", cacheControl=" + cacheControl + ", contentDisposition=" + + contentDisposition + ", contentEncoding=" + contentEncoding + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/ObsBucket.java b/app/src/main/java/com/obs/services/model/ObsBucket.java index f0492b2..1232667 100644 --- a/app/src/main/java/com/obs/services/model/ObsBucket.java +++ b/app/src/main/java/com/obs/services/model/ObsBucket.java @@ -24,196 +24,193 @@ * Buckets in OBS * */ -public class ObsBucket extends S3Bucket -{ - - public ObsBucket(){ - +public class ObsBucket extends S3Bucket { + + public ObsBucket() { + } - + /** * Constructor - * @param bucketName Bucket name - * @param location Bucket location + * + * @param bucketName + * Bucket name + * @param location + * Bucket location */ - public ObsBucket(String bucketName, String location){ + public ObsBucket(String bucketName, String location) { this.bucketName = bucketName; this.location = location; } - - + /** * Obtain the bucket name. * * @return Bucket name */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } - + /** - * Set the bucket name. - * The value can contain only lowercase letters, digits, hyphens (-), and periods (.). - * @param bucketName Bucket name + * Set the bucket name. The value can contain only lowercase letters, + * digits, hyphens (-), and periods (.). + * + * @param bucketName + * Bucket name */ - public void setBucketName(String bucketName) - { + public void setBucketName(String bucketName) { this.bucketName = bucketName; } - + /** - * Obtain the owner of the bucket. + * Obtain the owner of the bucket. * - * @return Owner of the bucket + * @return Owner of the bucket */ - public Owner getOwner() - { + public Owner getOwner() { return owner; } - + /** - * Set the owner of the bucket. + * Set the owner of the bucket. * - * @param bucketOwner Owner of the bucket + * @param bucketOwner + * Owner of the bucket */ - public void setOwner(Owner bucketOwner) - { + public void setOwner(Owner bucketOwner) { this.owner = bucketOwner; } - + /** - Obtain the creation time of the bucket. + * Obtain the creation time of the bucket. * * @return Creation time of the bucket */ - public Date getCreationDate() - { - return ServiceUtils.cloneDateIgnoreNull(this.creationDate); + public Date getCreationDate() { + return ServiceUtils.cloneDateIgnoreNull(this.creationDate); } - + /** * Set the creation time of the bucket. * - * @param bucketCreationDate Creation time of the bucket + * @param bucketCreationDate + * Creation time of the bucket */ - public void setCreationDate(Date bucketCreationDate) - { - this.creationDate = ServiceUtils.cloneDateIgnoreNull(bucketCreationDate); + public void setCreationDate(Date bucketCreationDate) { + this.creationDate = ServiceUtils.cloneDateIgnoreNull(bucketCreationDate); } - + /** * Obtain bucket properties. + * * @return Bucket properties */ @Deprecated - public Map getMetadata() - { - if(this.metadata == null) { - this.metadata = new HashMap(); - } + public Map getMetadata() { + if (this.metadata == null) { + this.metadata = new HashMap(); + } return metadata; } - + /** * Set bucket properties. - * @param metadata Bucket properties + * + * @param metadata + * Bucket properties */ @Deprecated - public void setMetadata(Map metadata) - { + public void setMetadata(Map metadata) { this.metadata = metadata; } - + /** * Obtain the bucket location. + * * @return Bucket location */ - public String getLocation() - { + public String getLocation() { return location; } - + /** * Set the bucket location. - * @param location Bucket location. This parameter is mandatory unless the endpoint belongs to the default region. + * + * @param location + * Bucket location. This parameter is mandatory unless the + * endpoint belongs to the default region. */ - public void setLocation(String location) - { + public void setLocation(String location) { this.location = location; } - + /** * Obtain the bucket ACL. + * * @return Bucket ACL */ - public AccessControlList getAcl() - { + public AccessControlList getAcl() { return acl; } - + /** * Set the bucket ACL. - * @param acl Bucket ACL + * + * @param acl + * Bucket ACL */ - public void setAcl(AccessControlList acl) - { + public void setAcl(AccessControlList acl) { this.acl = acl; } /** - * Obtain the bucket storage class. + * Obtain the bucket storage class. + * * @return Bucket storage class */ @Deprecated - public String getStorageClass() - { + public String getStorageClass() { return this.storageClass != null ? this.storageClass.getCode() : null; } /** - * Set the bucket storage class. - * @param storageClass Bucket storage class + * Set the bucket storage class. + * + * @param storageClass + * Bucket storage class */ @Deprecated - public void setStorageClass(String storageClass) - { + public void setStorageClass(String storageClass) { this.storageClass = StorageClassEnum.getValueFromCode(storageClass); } - - public BucketTypeEnum getBucketType() - { + public BucketTypeEnum getBucketType() { return bucketTypeEnum; } - - public void setBucketType(BucketTypeEnum bucketTypeEnum) - { + public void setBucketType(BucketTypeEnum bucketTypeEnum) { this.bucketTypeEnum = bucketTypeEnum; } - - public StorageClassEnum getBucketStorageClass() - { + public StorageClassEnum getBucketStorageClass() { return storageClass; } /** - * Set the bucket storage class. - * @param storageClass Bucket storage class + * Set the bucket storage class. + * + * @param storageClass + * Bucket storage class */ - public void setBucketStorageClass(StorageClassEnum storageClass) - { + public void setBucketStorageClass(StorageClassEnum storageClass) { this.storageClass = storageClass; } @Override - public String toString() - { - return "ObsBucket [bucketName=" + bucketName + ", owner=" + owner + ", creationDate=" + creationDate + ", location=" + location - + ", storageClass=" + storageClass + ", metadata=" + metadata + ", acl=" + acl + "]"; + public String toString() { + return "ObsBucket [bucketName=" + bucketName + ", owner=" + owner + ", creationDate=" + creationDate + + ", location=" + location + ", storageClass=" + storageClass + ", metadata=" + metadata + ", acl=" + + acl + "]"; } } - - diff --git a/app/src/main/java/com/obs/services/model/ObsObject.java b/app/src/main/java/com/obs/services/model/ObsObject.java index be8d4e7..bf3eef6 100644 --- a/app/src/main/java/com/obs/services/model/ObsObject.java +++ b/app/src/main/java/com/obs/services/model/ObsObject.java @@ -19,111 +19,112 @@ /** * Objects in OBS */ -public class ObsObject extends S3Object{ - - /** - * Obtain the name of the bucket to which the object belongs. - * - * @return Name of the bucket to which the object belongs - */ - public String getBucketName() { - return bucketName; - } - - /** - * Set the bucket to which the object belongs. - * - * @param bucketName - * Name of the bucket to which the object belongs - */ - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } - - /** - * Obtain the object name. - * - * @return Object name - */ - public String getObjectKey() { - return objectKey; - } - - /** - * Set the object name. - * - * @param objectKey - * Object name - */ - public void setObjectKey(String objectKey) { - this.objectKey = objectKey; - } - - /** - * Obtain the object properties, including "content-type", "content-length", and customized metadata. - * - * @return Object properties - */ - public ObjectMetadata getMetadata() { - if (metadata == null) { - this.metadata = new ObjectMetadata(); - } - return metadata; - } - - /** - * Set the object properties, including "content-type", "content-length", and customized metadata. - * - * @param metadata - * Object properties - */ - public void setMetadata(ObjectMetadata metadata) { - this.metadata = metadata; - } - - /** - * Obtain the data stream of the object. - * - * @return Data stream of the object - */ - public InputStream getObjectContent() { - return objectContent; - } - - /** - * Set the data stream of the object. - * - * @param objectContent - * Object data stream - */ - public void setObjectContent(InputStream objectContent) { - this.objectContent = objectContent; - } - - /** - * Obtain the owner of the object. - * - * @return Owner of the object - */ - public Owner getOwner() { - return owner; - } - - /** - * Set the owner of the object. - * - * @param owner - * Owner of the object - */ - public void setOwner(Owner owner) { - this.owner = owner; - } - - @Override - public String toString() { - return "ObsObject [bucketName=" + bucketName + ", objectKey=" + objectKey + ", owner=" + owner + ", metadata=" - + metadata + ", objectContent=" + objectContent + "]"; - } +@SuppressWarnings("deprecation") +public class ObsObject extends S3Object { + + /** + * Obtain the name of the bucket to which the object belongs. + * + * @return Name of the bucket to which the object belongs + */ + public String getBucketName() { + return bucketName; + } + + /** + * Set the bucket to which the object belongs. + * + * @param bucketName + * Name of the bucket to which the object belongs + */ + public void setBucketName(String bucketName) { + this.bucketName = bucketName; + } + + /** + * Obtain the object name. + * + * @return Object name + */ + public String getObjectKey() { + return objectKey; + } + + /** + * Set the object name. + * + * @param objectKey + * Object name + */ + public void setObjectKey(String objectKey) { + this.objectKey = objectKey; + } + + /** + * Obtain the object properties, including "content-type", "content-length", + * and customized metadata. + * + * @return Object properties + */ + public ObjectMetadata getMetadata() { + if (metadata == null) { + this.metadata = new ObjectMetadata(); + } + return metadata; + } + + /** + * Set the object properties, including "content-type", "content-length", + * and customized metadata. + * + * @param metadata + * Object properties + */ + public void setMetadata(ObjectMetadata metadata) { + this.metadata = metadata; + } + + /** + * Obtain the data stream of the object. + * + * @return Data stream of the object + */ + public InputStream getObjectContent() { + return objectContent; + } + + /** + * Set the data stream of the object. + * + * @param objectContent + * Object data stream + */ + public void setObjectContent(InputStream objectContent) { + this.objectContent = objectContent; + } + + /** + * Obtain the owner of the object. + * + * @return Owner of the object + */ + public Owner getOwner() { + return owner; + } + + /** + * Set the owner of the object. + * + * @param owner + * Owner of the object + */ + public void setOwner(Owner owner) { + this.owner = owner; + } + + @Override + public String toString() { + return "ObsObject [bucketName=" + bucketName + ", objectKey=" + objectKey + ", owner=" + owner + ", metadata=" + + metadata + ", objectContent=" + objectContent + "]"; + } } - - diff --git a/app/src/main/java/com/obs/services/model/OptionsInfoRequest.java b/app/src/main/java/com/obs/services/model/OptionsInfoRequest.java index 2883e9a..4e1ae31 100644 --- a/app/src/main/java/com/obs/services/model/OptionsInfoRequest.java +++ b/app/src/main/java/com/obs/services/model/OptionsInfoRequest.java @@ -21,86 +21,92 @@ * Parameters in a bucket or object preflight request * */ -public class OptionsInfoRequest -{ +public class OptionsInfoRequest extends GenericRequest { + + { + httpMethod = HttpMethodEnum.OPTIONS; + } + private String origin; - + private List requestMethod; - + private List requestHeaders; - + + public OptionsInfoRequest() { + } + + public OptionsInfoRequest(String bucketName) { + this.bucketName = bucketName; + } + /** * Obtain the origin of the preflight request. * * @return Origin of the preflight request */ - public String getOrigin() - { + public String getOrigin() { return origin; } - + /** * Set the origin of the preflight request. * - * @param origin Origin of the preflight request + * @param origin + * Origin of the preflight request */ - public void setOrigin(String origin) - { + public void setOrigin(String origin) { this.origin = origin; } - + /** * Obtain the list of allowed cross-origin request methods. * * @return List of allowed cross-origin request methods */ - public List getRequestMethod() - { - if(this.requestMethod == null){ - this.requestMethod = new ArrayList(); + public List getRequestMethod() { + if (this.requestMethod == null) { + this.requestMethod = new ArrayList<>(); } return requestMethod; } - + /** * Set the list of allowed cross-origin request methods. * - * @param requestMethod List of allowed cross-origin request methods + * @param requestMethod + * List of allowed cross-origin request methods */ - public void setRequestMethod(List requestMethod) - { + public void setRequestMethod(List requestMethod) { this.requestMethod = requestMethod; } - + /** * Obtain the list of allowed request headers. * * @return List of allowed request headers */ - public List getRequestHeaders() - { - if(this.requestHeaders == null){ - this.requestHeaders = new ArrayList(); + public List getRequestHeaders() { + if (this.requestHeaders == null) { + this.requestHeaders = new ArrayList<>(); } return requestHeaders; } - + /** * Set the list of allowed request headers. * - * @param requestHeaders List of allowed request headers + * @param requestHeaders + * List of allowed request headers */ - public void setRequestHeaders(List requestHeaders) - { + public void setRequestHeaders(List requestHeaders) { this.requestHeaders = requestHeaders; } @Override - public String toString() - { - return "OptionsInfoRequest [origin=" + origin + ", requestMethod=" + requestMethod + ", requestHeaders=" + requestHeaders + "]"; + public String toString() { + return "OptionsInfoRequest [origin=" + origin + ", requestMethod=" + requestMethod + ", requestHeaders=" + + requestHeaders + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/OptionsInfoResult.java b/app/src/main/java/com/obs/services/model/OptionsInfoResult.java index ca0ae83..c98c0f6 100644 --- a/app/src/main/java/com/obs/services/model/OptionsInfoResult.java +++ b/app/src/main/java/com/obs/services/model/OptionsInfoResult.java @@ -21,100 +21,89 @@ * Response to a bucket or object preflight request * */ -public class OptionsInfoResult extends HeaderResponse -{ - private String allowOrigin; - - private List allowHeaders; - - private int maxAge; - - private List allowMethods; - - private List exposeHeaders; - - - - public OptionsInfoResult(String allowOrigin, List allowHeaders, int maxAge, List allowMethods, - List exposeHeaders) { - super(); - this.allowOrigin = allowOrigin; - this.allowHeaders = allowHeaders; - this.maxAge = maxAge; - this.allowMethods = allowMethods; - this.exposeHeaders = exposeHeaders; - } +public class OptionsInfoResult extends HeaderResponse { + protected String allowOrigin; + + protected List allowHeaders; + protected int maxAge; - /** + protected List allowMethods; + + protected List exposeHeaders; + + public OptionsInfoResult(String allowOrigin, List allowHeaders, int maxAge, List allowMethods, + List exposeHeaders) { + super(); + this.allowOrigin = allowOrigin; + this.allowHeaders = allowHeaders; + this.maxAge = maxAge; + this.allowMethods = allowMethods; + this.exposeHeaders = exposeHeaders; + } + + protected OptionsInfoResult() { + } + + /** * Obtain the origin of the allowed cross-origin request. * * @return Origin of the allowed cross-origin request */ - public String getAllowOrigin() - { + public String getAllowOrigin() { return allowOrigin; } - - - /** + + /** * Obtain the list of allowed request headers. * * @return List of allowed request headers */ - public List getAllowHeaders() - { - if(this.allowHeaders == null){ + public List getAllowHeaders() { + if (this.allowHeaders == null) { allowHeaders = new ArrayList(); } return allowHeaders; } - - - /** - * Obtain the cache duration (in seconds) of the request result in the instance of ObsClient. + + /** + * Obtain the cache duration (in seconds) of the request result in the + * instance of ObsClient. * * @return Cache duration of the request result in the instance of ObsClient */ - public int getMaxAge() - { + public int getMaxAge() { return maxAge; } + /** * Obtain the list of allowed cross-origin request methods. * * @return List of allowed cross-origin request methods */ - public List getAllowMethods() - { - if(this.allowMethods == null){ + public List getAllowMethods() { + if (this.allowMethods == null) { this.allowMethods = new ArrayList(); } return allowMethods; } - - + /** * Obtain the list of allowed additional headers in the response. * * @return List of allowed additional headers in the response */ - public List getExposeHeaders() - { - if(this.exposeHeaders == null){ + public List getExposeHeaders() { + if (this.exposeHeaders == null) { this.exposeHeaders = new ArrayList(); } return exposeHeaders; } - @Override - public String toString() - { - return "OptionsInfoResult [allowOrigin=" + allowOrigin + ", allowHeaders=" + allowHeaders + ", maxAge=" + maxAge + ", allowMethods=" - + allowMethods + ", exposeHeaders=" + exposeHeaders + "]"; + public String toString() { + return "OptionsInfoResult [allowOrigin=" + allowOrigin + ", allowHeaders=" + allowHeaders + ", maxAge=" + maxAge + + ", allowMethods=" + allowMethods + ", exposeHeaders=" + exposeHeaders + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/Owner.java b/app/src/main/java/com/obs/services/model/Owner.java index cb52422..8c07812 100644 --- a/app/src/main/java/com/obs/services/model/Owner.java +++ b/app/src/main/java/com/obs/services/model/Owner.java @@ -1,76 +1,68 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ package com.obs.services.model; /** * Bucket or object owner */ -public class Owner -{ +public class Owner { private String displayName; - + private String id; - + /** * Obtain the owner name. * * @return Owner name */ - @Deprecated - public String getDisplayName() - { + public String getDisplayName() { return displayName; } - + /** * Set the owner name. * - * @param displayName Owner name + * @param displayName + * Owner name */ - @Deprecated - public void setDisplayName(String displayName) - { + public void setDisplayName(String displayName) { this.displayName = displayName; } - + /** * Obtain the ID of the domain to which the owner belongs. * * @return ID of the domain to which the owner belongs */ - public String getId() - { + public String getId() { return id; } - + /** * Set the ID of the domain to which the owner belongs. * - * @param id ID of the domain to which the owner belongs + * @param id + * ID of the domain to which the owner belongs */ - public void setId(String id) - { + public void setId(String id) { this.id = id; } @Override - public String toString() - { + public String toString() { return "Owner [displayName=" + displayName + ", id=" + id + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/PartEtag.java b/app/src/main/java/com/obs/services/model/PartEtag.java index d093bd7..ae59495 100644 --- a/app/src/main/java/com/obs/services/model/PartEtag.java +++ b/app/src/main/java/com/obs/services/model/PartEtag.java @@ -19,119 +19,115 @@ /** * Part information, including the ETag and part number */ -public class PartEtag implements Serializable -{ - /** - * - */ - private static final long serialVersionUID = -2946156755118245847L; +public class PartEtag implements Serializable { + private static final long serialVersionUID = -2946156755118245847L; + + private String etag; - private String etag; - private Integer partNumber; - - public PartEtag(){ - + + public PartEtag() { + } - + /** * Constructor - * @param etag Part ETag - * @param partNumber Part number + * + * @param etag + * Part ETag + * @param partNumber + * Part number */ - public PartEtag(String etag, Integer partNumber) - { + public PartEtag(String etag, Integer partNumber) { this.etag = etag; this.partNumber = partNumber; } - /** * Obtain the ETag of the part. + * * @return Part ETag */ - public String getEtag() - { + public String getEtag() { return etag; } - + /** * Set the ETag of the part. - * @param etag Part ETag + * + * @param etag + * Part ETag */ - public void setEtag(String etag) - { + public void setEtag(String etag) { this.etag = etag; } - /** * Obtain the ETag of the part. + * * @return Part ETag */ @Deprecated - public String geteTag() - { + public String geteTag() { return this.getEtag(); } - + /** * Set the ETag of the part. - * @param etag Part ETag + * + * @param etag + * Part ETag */ @Deprecated - public void seteTag(String etag) - { - this.setEtag(etag); + public void seteTag(String etag) { + this.setEtag(etag); } - + /** * Obtain the part number. + * * @return Part number */ - public Integer getPartNumber() - { + public Integer getPartNumber() { return partNumber; } - + /** * Set the part number. - * @param partNumber Part number + * + * @param partNumber + * Part number */ - public void setPartNumber(Integer partNumber) - { + public void setPartNumber(Integer partNumber) { this.partNumber = partNumber; } @Override - public String toString() - { + public String toString() { return "PartEtag [etag=" + etag + ", partNumber=" + partNumber + "]"; } - + @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((etag == null) ? 0 : etag.hashCode()); - result = prime * result + partNumber; - return result; + final int prime = 31; + int result = 1; + result = prime * result + ((etag == null) ? 0 : etag.hashCode()); + result = prime * result + partNumber; + return result; } - + @Override public boolean equals(Object obj) { - if (obj == null) { - return false; - }else { - if (obj instanceof PartEtag) { - PartEtag partEtag = (PartEtag) obj; - if (partEtag.etag.equals(this.etag) && partEtag.partNumber.equals(this.partNumber)) { - return true; - } - } - } - return false; + if (obj == null) { + return false; + } else { + if (obj instanceof PartEtag) { + PartEtag partEtag = (PartEtag) obj; + if (partEtag.etag.equals(this.etag) && partEtag.partNumber.equals(this.partNumber)) { + return true; + } + } + } + return false; } } - - diff --git a/app/src/main/java/com/obs/services/model/Permission.java b/app/src/main/java/com/obs/services/model/Permission.java index 36202fd..b0211d2 100644 --- a/app/src/main/java/com/obs/services/model/Permission.java +++ b/app/src/main/java/com/obs/services/model/Permission.java @@ -19,114 +19,94 @@ /** * Permissions in the ACL */ -public final class Permission -{ +public final class Permission { /** * Full control permission */ public static final Permission PERMISSION_FULL_CONTROL = new Permission(Constants.PERMISSION_FULL_CONTROL); - + /** * Read permission */ public static final Permission PERMISSION_READ = new Permission(Constants.PERMISSION_READ); - + /** * Write permission */ public static final Permission PERMISSION_WRITE = new Permission(Constants.PERMISSION_WRITE); - + /** * ACL read permission */ public static final Permission PERMISSION_READ_ACP = new Permission(Constants.PERMISSION_READ_ACP); - + /** * ACL write permission */ public static final Permission PERMISSION_WRITE_ACP = new Permission(Constants.PERMISSION_WRITE_ACP); - + /** * Read permission on objects in the bucket */ @Deprecated public static final Permission PERMISSION_READ_OBJECT = new Permission(Constants.PERMISSION_READ_OBJECT); - + /** * Full control permission on objects in the bucket */ @Deprecated - public static final Permission PERMISSION_FULL_CONTROL_OBJECT = new Permission(Constants.PERMISSION_FULL_CONTROL_OBJECT); - + public static final Permission PERMISSION_FULL_CONTROL_OBJECT = new Permission( + Constants.PERMISSION_FULL_CONTROL_OBJECT); + private String permissionString = ""; - - private Permission(String permissionString) - { + + private Permission(String permissionString) { this.permissionString = permissionString; } - - public String getPermissionString() - { + + public String getPermissionString() { return permissionString; } - + /** * Obtain a permission object based on the string. * - * @param str Permission name + * @param str + * Permission name * @return Permission object corresponding to the permission name */ - public static Permission parsePermission(String str) - { + public static Permission parsePermission(String str) { Permission permission = null; - - if (str.equals(PERMISSION_FULL_CONTROL.toString())) - { + + if (str.equals(PERMISSION_FULL_CONTROL.toString())) { permission = PERMISSION_FULL_CONTROL; - } - else if (str.equals(PERMISSION_READ.toString())) - { + } else if (str.equals(PERMISSION_READ.toString())) { permission = PERMISSION_READ; - } - else if (str.equals(PERMISSION_WRITE.toString())) - { + } else if (str.equals(PERMISSION_WRITE.toString())) { permission = PERMISSION_WRITE; - } - else if (str.equals(PERMISSION_READ_ACP.toString())) - { + } else if (str.equals(PERMISSION_READ_ACP.toString())) { permission = PERMISSION_READ_ACP; - } - else if (str.equals(PERMISSION_WRITE_ACP.toString())) - { + } else if (str.equals(PERMISSION_WRITE_ACP.toString())) { permission = PERMISSION_WRITE_ACP; - } - else if (str.equals(PERMISSION_READ_OBJECT.toString())){ + } else if (str.equals(PERMISSION_READ_OBJECT.toString())) { permission = PERMISSION_READ_OBJECT; - } - else if (str.equals(PERMISSION_FULL_CONTROL_OBJECT.toString())){ + } else if (str.equals(PERMISSION_FULL_CONTROL_OBJECT.toString())) { permission = PERMISSION_FULL_CONTROL_OBJECT; - } - else - { + } else { permission = new Permission(str); } return permission; } - - public String toString() - { + + public String toString() { return permissionString; } - - public boolean equals(Object obj) - { + + public boolean equals(Object obj) { return (obj instanceof Permission) && toString().equals(obj.toString()); } - - public int hashCode() - { + + public int hashCode() { return permissionString.hashCode(); } } - - diff --git a/app/src/main/java/com/obs/services/model/PolicyConditionItem.java b/app/src/main/java/com/obs/services/model/PolicyConditionItem.java index 7656f2a..13ba76b 100644 --- a/app/src/main/java/com/obs/services/model/PolicyConditionItem.java +++ b/app/src/main/java/com/obs/services/model/PolicyConditionItem.java @@ -11,14 +11,15 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; public class PolicyConditionItem { - + private ConditionOperator operator; - + private String key; - + private String value; public PolicyConditionItem(ConditionOperator operator, String key, String value) { @@ -26,16 +27,14 @@ public PolicyConditionItem(ConditionOperator operator, String key, String value) this.key = key; this.value = value; } - - + public String toString() { - if (value == null) { + if (null == value) { value = ""; } return String.format("[\"%s\",\"$%s\",\"%s\"]", operator.getOperationCode(), key, value); } - public static enum ConditionOperator { EQUAL("eq"), diff --git a/app/src/main/java/com/obs/services/model/PolicyTempSignatureRequest.java b/app/src/main/java/com/obs/services/model/PolicyTempSignatureRequest.java index cefcaf5..e9692ff 100644 --- a/app/src/main/java/com/obs/services/model/PolicyTempSignatureRequest.java +++ b/app/src/main/java/com/obs/services/model/PolicyTempSignatureRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; import java.text.SimpleDateFormat; @@ -24,22 +25,26 @@ * Request parameters of policy-based temporary access authorization * */ -public class PolicyTempSignatureRequest extends AbstractTemporarySignatureRequest{ - +public class PolicyTempSignatureRequest extends AbstractTemporarySignatureRequest { + private Date expiryDate; - + private long expires = ObsConstraint.DEFAULT_EXPIRE_SECONEDS; - + private List conditions; - + public PolicyTempSignatureRequest() { } - + /** * Constructor - * @param method HTTP/HTTPS request method - * @param bucketName Bucket name - * @param objectKey Object name + * + * @param method + * HTTP/HTTPS request method + * @param bucketName + * Bucket name + * @param objectKey + * Object name */ public PolicyTempSignatureRequest(HttpMethodEnum method, String bucketName, String objectKey) { super(method, bucketName, objectKey); @@ -47,40 +52,52 @@ public PolicyTempSignatureRequest(HttpMethodEnum method, String bucketName, Stri /** * Constructor - * @param method HTTP/HTTPS request method - * @param bucketName Bucket name - * @param objectKey Object name - * @param expiryDate Expiration data + * + * @param method + * HTTP/HTTPS request method + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param expiryDate + * Expiration data */ public PolicyTempSignatureRequest(HttpMethodEnum method, String bucketName, String objectKey, Date expiryDate) { super(method, bucketName, objectKey); this.expiryDate = ServiceUtils.cloneDateIgnoreNull(expiryDate); } - + /** * Constructor - * @param method HTTP/HTTPS request method - * @param bucketName Bucket name - * @param objectKey Object name - * @param expires Validity period + * + * @param method + * HTTP/HTTPS request method + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param expires + * Validity period */ public PolicyTempSignatureRequest(HttpMethodEnum method, String bucketName, String objectKey, long expires) { super(method, bucketName, objectKey); this.expires = expires; } - + /** * Generate a policy based on the validity period and policy condition. + * * @return */ public String generatePolicy() { Date requestDate = new Date(); SimpleDateFormat expirationDateFormat = ServiceUtils.getExpirationDateFormat(); - Date expiryDate = this.expiryDate; - if (expiryDate == null) { - expiryDate = new Date(requestDate.getTime() + (this.expires <=0 ? ObsConstraint.DEFAULT_EXPIRE_SECONEDS : this.expires) * 1000); + Date expiry = this.expiryDate; + if (expiry == null) { + expiry = new Date(requestDate.getTime() + + (this.expires <= 0 ? ObsConstraint.DEFAULT_EXPIRE_SECONEDS : this.expires) * 1000); } - String expiration = expirationDateFormat.format(expiryDate); + String expiration = expirationDateFormat.format(expiry); StringBuilder policy = new StringBuilder(); policy.append("{\"expiration\":").append("\"").append(expiration).append("\",").append("\"conditions\":["); if (this.conditions != null && !this.conditions.isEmpty()) { @@ -90,45 +107,47 @@ public String generatePolicy() { return policy.toString(); } - /** * Set the expiration date. + * * @return Expiration date */ - public Date getExpiryDate() - { - return ServiceUtils.cloneDateIgnoreNull(this.expiryDate); + public Date getExpiryDate() { + return ServiceUtils.cloneDateIgnoreNull(this.expiryDate); } - + /** * Obtain the expiration date. - * @param expiryDate Expiration date + * + * @param expiryDate + * Expiration date */ - public void setExpiryDate(Date expiryDate) - { - this.expiryDate = ServiceUtils.cloneDateIgnoreNull(expiryDate); + public void setExpiryDate(Date expiryDate) { + this.expiryDate = ServiceUtils.cloneDateIgnoreNull(expiryDate); } - + /** * Obtain the validity period. The default value is 5 minutes (300 seconds). + * * @return Validity period */ - public long getExpires() - { + public long getExpires() { return expires; } - + /** * Set the validity period (seconds). - * @param expires Validity period + * + * @param expires + * Validity period */ - public void setExpires(long expires) - { + public void setExpires(long expires) { this.expires = expires; } /** * Obtain the condition set of the policy. + * * @return Policy condition set */ public List getConditions() { @@ -137,7 +156,9 @@ public List getConditions() { /** * Set the condition set of the policy. - * @param conditions Policy condition set + * + * @param conditions + * Policy condition set */ public void setConditions(List conditions) { this.conditions = conditions; diff --git a/app/src/main/java/com/obs/services/model/PostSignatureRequest.java b/app/src/main/java/com/obs/services/model/PostSignatureRequest.java index e08a85d..c3c7c76 100644 --- a/app/src/main/java/com/obs/services/model/PostSignatureRequest.java +++ b/app/src/main/java/com/obs/services/model/PostSignatureRequest.java @@ -27,221 +27,242 @@ * Parameters in a request for browser-based authorized access * */ -public class PostSignatureRequest -{ - +public class PostSignatureRequest { + private Date requestDate; - + private Date expiryDate; - + private String bucketName; - + private String objectKey; - + private long expires = ObsConstraint.DEFAULT_EXPIRE_SECONEDS; - + private Map formParams; - + private List conditions; - - public PostSignatureRequest() - { - + + public PostSignatureRequest() { + } - + /** * Constructor - * @param expires Expiration time (in seconds) - * @param bucketName Bucket name - * @param objectKey Object name + * + * @param expires + * Expiration time (in seconds) + * @param bucketName + * Bucket name + * @param objectKey + * Object name */ - public PostSignatureRequest(long expires, String bucketName, String objectKey) - { + public PostSignatureRequest(long expires, String bucketName, String objectKey) { this.expires = expires; this.bucketName = bucketName; this.objectKey = objectKey; } - + /** * Constructor - * @param expiryDate Expiration date - * @param bucketName Bucket name - * @param objectKey Object name + * + * @param expiryDate + * Expiration date + * @param bucketName + * Bucket name + * @param objectKey + * Object name */ - public PostSignatureRequest(Date expiryDate, String bucketName, String objectKey) - { + public PostSignatureRequest(Date expiryDate, String bucketName, String objectKey) { this.expiryDate = ServiceUtils.cloneDateIgnoreNull(expiryDate); this.bucketName = bucketName; this.objectKey = objectKey; } - + /** * - * @param expires Expiration time (in seconds) - * @param requestDate Request time - * @param bucketName Bucket name - * @param objectKey Object name + * @param expires + * Expiration time (in seconds) + * @param requestDate + * Request time + * @param bucketName + * Bucket name + * @param objectKey + * Object name */ - public PostSignatureRequest(long expires, Date requestDate, String bucketName, String objectKey) - { + public PostSignatureRequest(long expires, Date requestDate, String bucketName, String objectKey) { this.expires = expires; this.requestDate = ServiceUtils.cloneDateIgnoreNull(requestDate); this.bucketName = bucketName; this.objectKey = objectKey; } - + /** * - * @param expiryDate Expiration date - * @param requestDate Request time - * @param bucketName Bucket name - * @param objectKey Object name + * @param expiryDate + * Expiration date + * @param requestDate + * Request time + * @param bucketName + * Bucket name + * @param objectKey + * Object name */ - public PostSignatureRequest(Date expiryDate, Date requestDate, String bucketName, String objectKey) - { + public PostSignatureRequest(Date expiryDate, Date requestDate, String bucketName, String objectKey) { this.expiryDate = ServiceUtils.cloneDateIgnoreNull(expiryDate); this.requestDate = ServiceUtils.cloneDateIgnoreNull(requestDate); this.bucketName = bucketName; this.objectKey = objectKey; } - + /** * Set the request time. + * * @return Request time */ - public Date getRequestDate() - { - return ServiceUtils.cloneDateIgnoreNull(this.requestDate); + public Date getRequestDate() { + return ServiceUtils.cloneDateIgnoreNull(this.requestDate); } - + /** * Set the request time. - * @param requestDate Request time + * + * @param requestDate + * Request time */ - public void setRequestDate(Date requestDate) - { - this.requestDate = ServiceUtils.cloneDateIgnoreNull(requestDate); + public void setRequestDate(Date requestDate) { + this.requestDate = ServiceUtils.cloneDateIgnoreNull(requestDate); } - + /** - Set the expiration date. + * Set the expiration date. + * * @return Expiration date */ - public Date getExpiryDate() - { - return ServiceUtils.cloneDateIgnoreNull(this.expiryDate); + public Date getExpiryDate() { + return ServiceUtils.cloneDateIgnoreNull(this.expiryDate); } - + /** * Obtain the expiration date. - * @param expiryDate Expiration date + * + * @param expiryDate + * Expiration date */ - public void setExpiryDate(Date expiryDate) - { - this.expiryDate = ServiceUtils.cloneDateIgnoreNull(expiryDate); + public void setExpiryDate(Date expiryDate) { + this.expiryDate = ServiceUtils.cloneDateIgnoreNull(expiryDate); } - + /** * Obtain the validity period. The default value is 5 minutes (value "300"). + * * @return Validity period */ - public long getExpires() - { + public long getExpires() { return expires; } - + /** * Set the validity period (in seconds). - * @param expires Validity period + * + * @param expires + * Validity period */ - public void setExpires(long expires) - { + public void setExpires(long expires) { this.expires = expires; } - + /** - * Obtain the form parameters of the request. + * Obtain the form parameters of the request. + * * @return Form parameters of the request */ - public Map getFormParams() - { - if (formParams == null) - { + public Map getFormParams() { + if (formParams == null) { formParams = new HashMap(); } return formParams; } - + /** * Set the form parameters of the request. - * @param formParams Form parameters of the request + * + * @param formParams + * Form parameters of the request */ - public void setFormParams(Map formParams){ + public void setFormParams(Map formParams) { this.formParams = formParams; } /** * Obtain the bucket name. + * * @return Bucket name */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } /** * Set the bucket name. - * @param bucketName Bucket name + * + * @param bucketName + * Bucket name */ - public void setBucketName(String bucketName) - { + public void setBucketName(String bucketName) { this.bucketName = bucketName; } /** * Obtain the object name. + * * @return Object name */ - public String getObjectKey() - { + public String getObjectKey() { return objectKey; } /** * Set the object name. - * @param objectKey Object name + * + * @param objectKey + * Object name */ - public void setObjectKey(String objectKey) - { + public void setObjectKey(String objectKey) { this.objectKey = objectKey; } /** - * Obtain the restrictions of the form. If the value is set, it will be used to calculate "policy", while the form parameter configuration of the request will be ignored. + * Obtain the restrictions of the form. If the value is set, it will be used + * to calculate "policy", while the form parameter configuration of the + * request will be ignored. + * * @return Restrictions of the form */ - public List getConditions() - { - if(this.conditions == null){ + public List getConditions() { + if (this.conditions == null) { this.conditions = new ArrayList(); } return conditions; } /** - * Set the restrictions of the form. If the value is set, it will be used to calculate "policy", while the form parameter configuration of the request will be ignored. - * @param conditions Restrictions of the form + * Set the restrictions of the form. If the value is set, it will be used to + * calculate "policy", while the form parameter configuration of the request + * will be ignored. + * + * @param conditions + * Restrictions of the form */ - public void setConditions(List conditions) - { + public void setConditions(List conditions) { this.conditions = conditions; } @Override - public String toString() - { - return "PostSignatureRequest [requestDate=" + requestDate + ", expiryDate=" + expiryDate + ", bucketName=" + bucketName - + ", objectKey=" + objectKey + ", expires=" + expires + ", formParams=" + formParams + ", conditions=" + conditions + "]"; + public String toString() { + return "PostSignatureRequest [requestDate=" + requestDate + ", expiryDate=" + expiryDate + ", bucketName=" + + bucketName + ", objectKey=" + objectKey + ", expires=" + expires + ", formParams=" + formParams + + ", conditions=" + conditions + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/PostSignatureResponse.java b/app/src/main/java/com/obs/services/model/PostSignatureResponse.java index 43c746e..a9fdd3c 100644 --- a/app/src/main/java/com/obs/services/model/PostSignatureResponse.java +++ b/app/src/main/java/com/obs/services/model/PostSignatureResponse.java @@ -11,87 +11,87 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; /** * - * Response to a request for browser-based authorized access + * Response to a request for browser-based authorized access * */ -public class PostSignatureResponse -{ +public class PostSignatureResponse { protected String policy; - + protected String originPolicy; - + protected String signature; - + protected String expiration; - + protected String token; - - public PostSignatureResponse(){ - + + public PostSignatureResponse() { + } - - public PostSignatureResponse(String policy, String originPolicy, String signature, - String expiration, String accessKey) - { + + public PostSignatureResponse(String policy, String originPolicy, String signature, String expiration, + String accessKey) { this.policy = policy; this.originPolicy = originPolicy; this.signature = signature; this.expiration = expiration; this.token = accessKey + ":" + signature + ":" + policy; } - + /** * Obtain the security policy of the request in the Base64 format. + * * @return Security policy in the Base64 format */ - public String getPolicy() - { + public String getPolicy() { return policy; } - + /** * Obtain the security policy of the request in the original format. + * * @return Security policy in the original format */ - public String getOriginPolicy() - { + public String getOriginPolicy() { return originPolicy; } - + /** * Obtain the signature string. + * * @return Signature string */ - public String getSignature() - { + public String getSignature() { return signature; } - + /** * Obtain the expiration date of the request. + * * @return Expiration date */ - public String getExpiration() - { + public String getExpiration() { return expiration; } - + /** * Obtain the token + * * @return token */ - public String getToken() { + public String getToken() { return token; } @Override - public String toString() { - return "PostSignatureResponse [policy=" + policy + ", originPolicy=" + originPolicy + ", signature=" + signature - + ", expiration=" + expiration + ", token=" + token + "]"; - } - + public String toString() { + return "PostSignatureResponse [policy=" + policy + ", originPolicy=" + originPolicy + ", signature=" + signature + + ", expiration=" + expiration + ", token=" + token + "]"; + } + } diff --git a/app/src/main/java/com/obs/services/model/ProgressListener.java b/app/src/main/java/com/obs/services/model/ProgressListener.java index c1f6dfa..c5f788a 100644 --- a/app/src/main/java/com/obs/services/model/ProgressListener.java +++ b/app/src/main/java/com/obs/services/model/ProgressListener.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; /** @@ -18,11 +19,13 @@ * */ public interface ProgressListener { - - /** - * Data transfer callback function - * @param status Data transfer status - */ - public void progressChanged(ProgressStatus status); - + + /** + * Data transfer callback function + * + * @param status + * Data transfer status + */ + public void progressChanged(ProgressStatus status); + } diff --git a/app/src/main/java/com/obs/services/model/ProgressStatus.java b/app/src/main/java/com/obs/services/model/ProgressStatus.java index 3956d73..eff5319 100644 --- a/app/src/main/java/com/obs/services/model/ProgressStatus.java +++ b/app/src/main/java/com/obs/services/model/ProgressStatus.java @@ -11,48 +11,54 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; /** - * Data transfer status + * Data transfer status * */ public interface ProgressStatus { - - /** - * Obtain the instantaneous speed. - * @return Instantaneous speed - */ - public double getInstantaneousSpeed(); - - /** - * Obtain the average speed. - * @return Average speed - */ - public double getAverageSpeed(); - - - /** - * Obtain the transfer progress - * @return Transfer progress - */ - public int getTransferPercentage(); - - /** - * Obtain the number of new bytes. - * @return Number of bytes transferred since last progress refresh - */ - public long getNewlyTransferredBytes(); - - /** - * Obtain the number of transferred bytes. - * @return Number of bytes that have been transferred - */ - public long getTransferredBytes(); - - /** - * Obtain the number of bytes to be transferred. - * @return Number of the total bytes to be transferred - */ - public long getTotalBytes(); + + /** + * Obtain the instantaneous speed. + * + * @return Instantaneous speed + */ + public double getInstantaneousSpeed(); + + /** + * Obtain the average speed. + * + * @return Average speed + */ + public double getAverageSpeed(); + + /** + * Obtain the transfer progress + * + * @return Transfer progress + */ + public int getTransferPercentage(); + + /** + * Obtain the number of new bytes. + * + * @return Number of bytes transferred since last progress refresh + */ + public long getNewlyTransferredBytes(); + + /** + * Obtain the number of transferred bytes. + * + * @return Number of bytes that have been transferred + */ + public long getTransferredBytes(); + + /** + * Obtain the number of bytes to be transferred. + * + * @return Number of the total bytes to be transferred + */ + public long getTotalBytes(); } diff --git a/app/src/main/java/com/obs/services/model/ProtocolEnum.java b/app/src/main/java/com/obs/services/model/ProtocolEnum.java index fc9b063..ea8f753 100644 --- a/app/src/main/java/com/obs/services/model/ProtocolEnum.java +++ b/app/src/main/java/com/obs/services/model/ProtocolEnum.java @@ -20,35 +20,33 @@ * */ public enum ProtocolEnum { - - /** - * Use HTTP for redirection. - */ - HTTP("http"), - - /** - * Use HTTPS for redirection. - */ - HTTPS("https"); - - private String code; - - private ProtocolEnum(String code) { - this.code = code; - } - - public String getCode() { - return code; - } - - public static ProtocolEnum getValueFromCode(String code) { - for (ProtocolEnum val : ProtocolEnum.values()) { - if (val.code.equals(code)) { - return val; - } - } - return null; - } -} - + /** + * Use HTTP for redirection. + */ + HTTP("http"), + + /** + * Use HTTPS for redirection. + */ + HTTPS("https"); + + private String code; + + private ProtocolEnum(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + + public static ProtocolEnum getValueFromCode(String code) { + for (ProtocolEnum val : ProtocolEnum.values()) { + if (val.code.equals(code)) { + return val; + } + } + return null; + } +} diff --git a/app/src/main/java/com/obs/services/model/PutObjectBasicRequest.java b/app/src/main/java/com/obs/services/model/PutObjectBasicRequest.java index ef1b052..0675080 100644 --- a/app/src/main/java/com/obs/services/model/PutObjectBasicRequest.java +++ b/app/src/main/java/com/obs/services/model/PutObjectBasicRequest.java @@ -1,16 +1,16 @@ /** -* Copyright 2019 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); you may not use -* this file except in compliance with the License. You may obtain a copy of the -* License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software distributed -* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -* CONDITIONS OF ANY KIND, either express or implied. See the License for the -* specific language governing permissions and limitations under the License. -**/ + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + **/ package com.obs.services.model; @@ -21,225 +21,196 @@ import com.obs.services.internal.utils.ServiceUtils; -public abstract class PutObjectBasicRequest { +public abstract class PutObjectBasicRequest extends BaseObjectRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + protected Map> extensionPermissionMap; - - protected String bucketName; - - protected String objectKey; - - protected Map> extensionPermissionMap; - protected AccessControlList acl; - + protected String successRedirectLocation; - + protected SseKmsHeader sseKmsHeader; - + protected SseCHeader sseCHeader; - - - /** - * Obtain the bucket name. - * - * @return Bucket name - */ - public String getBucketName() - { - return bucketName; + + public PutObjectBasicRequest() { } - - /** - * Set the bucket name. - * - * @param bucketName Bucket name - */ - public void setBucketName(String bucketName) - { + + public PutObjectBasicRequest(String bucketName) { this.bucketName = bucketName; } - - /** - * Obtain the object name. - * - * @return Object name - */ - public String getObjectKey() - { - return objectKey; - } - - /** - * Set the object name. - * - * @param objectKey Object name - * - */ - public void setObjectKey(String objectKey) - { - this.objectKey = objectKey; - } - + + /** - * Obtain SSE-KMS encryption headers of the object. - * + * Obtain SSE-KMS encryption headers of the object. + * * @return SSE-KMS encryption headers */ - public SseKmsHeader getSseKmsHeader() - { + public SseKmsHeader getSseKmsHeader() { return sseKmsHeader; } - + /** - * Set SSE-KMS encryption headers of the object. - * - * @param sseKmsHeader SSE-KMS encryption headers + * Set SSE-KMS encryption headers of the object. + * + * @param sseKmsHeader + * SSE-KMS encryption headers */ - public void setSseKmsHeader(SseKmsHeader sseKmsHeader) - { + public void setSseKmsHeader(SseKmsHeader sseKmsHeader) { this.sseKmsHeader = sseKmsHeader; } - + /** - * Obtain SSE-C encryption headers of the object. - * + * Obtain SSE-C encryption headers of the object. + * * @return SSE-C encryption headers */ - public SseCHeader getSseCHeader() - { + public SseCHeader getSseCHeader() { return sseCHeader; } - + /** - * Set SSE-C encryption headers of the object. - * - * @param sseCHeader SSE-C encryption headers + * Set SSE-C encryption headers of the object. + * + * @param sseCHeader + * SSE-C encryption headers */ - public void setSseCHeader(SseCHeader sseCHeader) - { + public void setSseCHeader(SseCHeader sseCHeader) { this.sseCHeader = sseCHeader; } - + /** * Obtain the ACL of the object. + * * @return Object ACL */ - public AccessControlList getAcl() - { + public AccessControlList getAcl() { return acl; } - + /** * Set the object ACL. - * @param acl Bucket ACL + * + * @param acl + * Bucket ACL */ - public void setAcl(AccessControlList acl) - { + public void setAcl(AccessControlList acl) { this.acl = acl; } - + /** - * Obtain the redirection address after a successfully responded request. - * @return Redirection address - */ - public String getSuccessRedirectLocation() { - return successRedirectLocation; - } - - /** - * Set the redirection address after a successfully responded request. - * @param successRedirectLocation Redirection address - */ - public void setSuccessRedirectLocation(String successRedirectLocation) { - this.successRedirectLocation = successRedirectLocation; - } - + * Obtain the redirection address after a successfully responded request. + * + * @return Redirection address + */ + public String getSuccessRedirectLocation() { + return successRedirectLocation; + } + /** - * Grant the OBS extension permission to users. - * @param domainId ID of the domain to which the user belongs - * @param extensionPermissionEnum OBS extension permission - */ - public void grantExtensionPermission(String domainId, ExtensionObjectPermissionEnum extensionPermissionEnum) { - if(extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { - return; - } - Set users = getExtensionPermissionMap().get(extensionPermissionEnum); - if(users == null) { - users = new HashSet(); - getExtensionPermissionMap().put(extensionPermissionEnum, users); - } - users.add(domainId.trim()); - } - - /** - * Withdraw the OBS extension permission. - * @param domainId ID of the domain to which the user belongs - * @param extensionPermissionEnum OBS extension permission - */ - public void withdrawExtensionPermission(String domainId, ExtensionObjectPermissionEnum extensionPermissionEnum) { - if(extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { - return; - } - domainId = domainId.trim(); - Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); - if(domainIds != null && domainIds.contains(domainId)) { - domainIds.remove(domainId); - } - } - - /** - * Withdraw all OBS extension permissions. - * @param domainId ID of the domain to which the user belongs - */ - public void withdrawExtensionPermissions(String domainId) { - if(ServiceUtils.isValid(domainId)) { - for(Map.Entry> entry : this.getExtensionPermissionMap().entrySet()) { - if(entry.getValue().contains(domainId.trim())) { - entry.getValue().remove(domainId); - } - } - } - } - - public Set getAllGrantPermissions(){ - return this.getExtensionPermissionMap().keySet(); - } - - - public Set getDomainIdsByGrantPermission(ExtensionObjectPermissionEnum extensionPermissionEnum) { - Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); - if(domainIds == null) { - domainIds = new HashSet(); - } - return domainIds; - } - - public Set getGrantPermissionsByDomainId(String domainId) { - Set grantPermissions = new HashSet(); - if(ServiceUtils.isValid(domainId)) { - domainId = domainId.trim(); - for(Map.Entry> entry : this.getExtensionPermissionMap().entrySet()) { - if(entry.getValue().contains(domainId)) { - grantPermissions.add(entry.getKey()); - } - } - } - return grantPermissions; - } - - public Map> getExtensionPermissionMap() { - if(extensionPermissionMap == null) { - extensionPermissionMap = new HashMap>(); - } - return extensionPermissionMap; - } - - public void setExtensionPermissionMap(Map> extensionPermissionMap) { - if(extensionPermissionMap == null) { - return; - } - this.extensionPermissionMap = extensionPermissionMap; - } -} + * Set the redirection address after a successfully responded request. + * + * @param successRedirectLocation + * Redirection address + */ + public void setSuccessRedirectLocation(String successRedirectLocation) { + this.successRedirectLocation = successRedirectLocation; + } + + /** + * Grant the OBS extension permission to users. + * + * @param domainId + * ID of the domain to which the user belongs + * @param extensionPermissionEnum + * OBS extension permission + */ + public void grantExtensionPermission(String domainId, ExtensionObjectPermissionEnum extensionPermissionEnum) { + if (extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { + return; + } + Set users = getExtensionPermissionMap().computeIfAbsent(extensionPermissionEnum, k -> new HashSet<>()); + users.add(domainId.trim()); + } + + /** + * Withdraw the OBS extension permission. + * + * @param domainId + * ID of the domain to which the user belongs + * @param extensionPermissionEnum + * OBS extension permission + */ + public void withdrawExtensionPermission(String domainId, ExtensionObjectPermissionEnum extensionPermissionEnum) { + if (extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { + return; + } + domainId = domainId.trim(); + Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); + if (domainIds != null) { + domainIds.remove(domainId); + } + } + + /** + * Withdraw all OBS extension permissions. + * + * @param domainId + * ID of the domain to which the user belongs + */ + public void withdrawExtensionPermissions(String domainId) { + if (ServiceUtils.isValid(domainId)) { + for (Map.Entry> entry : this.getExtensionPermissionMap() + .entrySet()) { + if (entry.getValue().contains(domainId.trim())) { + entry.getValue().remove(domainId); + } + } + } + } + + public Set getAllGrantPermissions() { + return this.getExtensionPermissionMap().keySet(); + } + public Set getDomainIdsByGrantPermission(ExtensionObjectPermissionEnum extensionPermissionEnum) { + Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); + if (domainIds == null) { + domainIds = new HashSet<>(); + } + return domainIds; + } + + public Set getGrantPermissionsByDomainId(String domainId) { + Set grantPermissions = new HashSet(); + if (ServiceUtils.isValid(domainId)) { + domainId = domainId.trim(); + for (Map.Entry> entry : this.getExtensionPermissionMap() + .entrySet()) { + if (entry.getValue().contains(domainId)) { + grantPermissions.add(entry.getKey()); + } + } + } + return grantPermissions; + } + + public Map> getExtensionPermissionMap() { + if (extensionPermissionMap == null) { + extensionPermissionMap = new HashMap<>(); + } + return extensionPermissionMap; + } + + public void setExtensionPermissionMap(Map> extensionPermissionMap) { + if (extensionPermissionMap == null) { + return; + } + this.extensionPermissionMap = extensionPermissionMap; + } +} diff --git a/app/src/main/java/com/obs/services/model/PutObjectRequest.java b/app/src/main/java/com/obs/services/model/PutObjectRequest.java index 7377b09..524c683 100644 --- a/app/src/main/java/com/obs/services/model/PutObjectRequest.java +++ b/app/src/main/java/com/obs/services/model/PutObjectRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. **/ + package com.obs.services.model; import java.io.File; @@ -21,234 +22,269 @@ /** * Parameters in an object upload request */ -public class PutObjectRequest extends PutObjectBasicRequest -{ - protected File file; - - protected InputStream input; - - protected ObjectMetadata metadata; - - protected int expires = -1; - - protected long offset; - +public class PutObjectRequest extends PutObjectBasicRequest { + protected File file; + + protected InputStream input; + + protected ObjectMetadata metadata; + + protected int expires = -1; + + protected long offset; + private boolean autoClose = true; - + private ProgressListener progressListener; - + private long progressInterval = ObsConstraint.DEFAULT_PROGRESS_INTERVAL; - - public PutObjectRequest(){ - + + public PutObjectRequest() { } - - + + public PutObjectRequest(String bucketName) { + this.bucketName = bucketName; + } + public PutObjectRequest(PutObjectBasicRequest request) { - if(request != null) { - this.bucketName = request.getBucketName(); - this.objectKey = request.getObjectKey(); - this.acl = request.getAcl(); - this.extensionPermissionMap = request.getExtensionPermissionMap(); - this.sseCHeader = request.getSseCHeader(); - this.sseKmsHeader = request.getSseKmsHeader(); - this.successRedirectLocation = request.getSuccessRedirectLocation(); - } - } - + if (request != null) { + this.bucketName = request.getBucketName(); + this.objectKey = request.getObjectKey(); + this.acl = request.getAcl(); + this.extensionPermissionMap = request.getExtensionPermissionMap(); + this.sseCHeader = request.getSseCHeader(); + this.sseKmsHeader = request.getSseKmsHeader(); + this.successRedirectLocation = request.getSuccessRedirectLocation(); + this.setRequesterPays(request.isRequesterPays()); + } + } + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name */ - public PutObjectRequest(String bucketName, String objectKey) - { + public PutObjectRequest(String bucketName, String objectKey) { this.bucketName = bucketName; this.objectKey = objectKey; } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name - * @param file File to be uploaded + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param file + * File to be uploaded */ - public PutObjectRequest(String bucketName, String objectKey, File file) - { + public PutObjectRequest(String bucketName, String objectKey, File file) { this.bucketName = bucketName; this.objectKey = objectKey; this.file = file; } - - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name - * @param input Data stream to be uploaded + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param input + * Data stream to be uploaded */ - public PutObjectRequest(String bucketName, String objectKey, InputStream input) - { + public PutObjectRequest(String bucketName, String objectKey, InputStream input) { this.bucketName = bucketName; this.objectKey = objectKey; this.input = input; } - - + /** - * Obtain the start position of the to-be-uploaded content in the file. This parameter is effective only when the path where the file is to be uploaded is configured. + * Obtain the start position of the to-be-uploaded content in the file. This + * parameter is effective only when the path where the file is to be + * uploaded is configured. * * @return Start position of the content to be uploaded in the local file */ - public long getOffset() - { + public long getOffset() { return offset; } - + /** - * Set the start position of the to-be-uploaded content in the file. This parameter is effective only when the path where the file is to be uploaded is configured. The unit is byte and the default value is 0. + * Set the start position of the to-be-uploaded content in the file. This + * parameter is effective only when the path where the file is to be + * uploaded is configured. The unit is byte and the default value is 0. * - * @param offset Start position of the content to be uploaded in the local file + * @param offset + * Start position of the content to be uploaded in the local file */ - public void setOffset(long offset) - { + public void setOffset(long offset) { this.offset = offset; } - + /** - * Obtain the data stream to be uploaded, which cannot be used with the file to be uploaded. - * + * Obtain the data stream to be uploaded, which cannot be used with the file + * to be uploaded. * @return Data stream to be uploaded */ - public InputStream getInput() - { + public InputStream getInput() { return input; } - + /** - * Set the data stream to be uploaded, which cannot be used with the file to be uploaded. + * Set the data stream to be uploaded, which cannot be used with the file to + * be uploaded. * - * @param input Data stream to be uploaded + * @param input + * Data stream to be uploaded * */ - public void setInput(InputStream input) - { + public void setInput(InputStream input) { this.input = input; this.file = null; } - + /** - * Obtain object properties, including "content-type", "content-length", "content-md5", and customized metadata. + * Obtain object properties, including "content-type", "content-length", + * "content-md5", and customized metadata. * * @return Object properties */ - public ObjectMetadata getMetadata() - { + public ObjectMetadata getMetadata() { return metadata; } - + /** - * Set the object properties, including "content-type", "content-length", and customized metadata. + * Set the object properties, including "content-type", "content-length", + * and customized metadata. * - * @param metadata Object properties + * @param metadata + * Object properties */ - public void setMetadata(ObjectMetadata metadata) - { + public void setMetadata(ObjectMetadata metadata) { this.metadata = metadata; } - /** - * Obtain the file to be uploaded, which cannot be used with the data stream. + * Obtain the file to be uploaded, which cannot be used with the data + * stream. + * * @return File to be uploaded */ - public File getFile() - { + public File getFile() { return file; } /** * Set the file to be uploaded, which cannot be used with the data stream. - * @param file File to be uploaded + * + * @param file + * File to be uploaded */ - public void setFile(File file) - { + public void setFile(File file) { this.file = file; this.input = null; } - - + /** * Obtain the expiration time of the object. + * * @return Expiration time of the object */ - public int getExpires() { - return expires; - } + public int getExpires() { + return expires; + } - /** - * Set the expiration time of the object. The value must be a positive integer. - * @param expires Expiration time of the object - */ - public void setExpires(int expires) { - this.expires = expires; - } - /** - * Check whether the input stream will be automatically closed. The default value is "true". - * @return Identifier specifying whether the input stream will be automatically closed + * Set the expiration time of the object. The value must be a positive + * integer. + * + * @param expires + * Expiration time of the object + */ + public void setExpires(int expires) { + this.expires = expires; + } + + /** + * Check whether the input stream will be automatically closed. The default + * value is "true". + * + * @return Identifier specifying whether the input stream will be + * automatically closed */ public boolean isAutoClose() { - return autoClose; - } - - /** - * Specify whether to automatically close the input stream. The default value is "true". - * @param autoClose Identifier specifying whether the input stream will be automatically closed - */ - public void setAutoClose(boolean autoClose) { - this.autoClose = autoClose; - } - - /** - * Obtain the data transfer listener. - * @return Data transfer listener - */ - public ProgressListener getProgressListener() { - return progressListener; - } - - /** - * Set the data transfer listener. - * @param progressListener Data transfer listener - */ - public void setProgressListener(ProgressListener progressListener) { - this.progressListener = progressListener; - } - - /** - * Obtain the callback threshold of the data transfer listener. The default value is 100 KB. - * @return Callback threshold of the data transfer listener - */ - public long getProgressInterval() { - return progressInterval; - } - - /** - * Set the callback threshold of the data transfer listener. The default value is 100 KB. - * @param progressInterval Callback threshold of the data transfer listener - */ - public void setProgressInterval(long progressInterval) { - this.progressInterval = progressInterval; - } - - - @Override - public String toString() { - return "PutObjectRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", file=" + file + ", input=" - + input + ", metadata=" + metadata + ", sseKmsHeader=" + sseKmsHeader + ", sseCHeader=" + sseCHeader - + ", acl=" + acl + ", expires=" + expires + ", successRedirectLocation=" + successRedirectLocation - + "]"; - } - + return autoClose; + } + + /** + * Specify whether to automatically close the input stream. The default + * value is "true". + * + * @param autoClose + * Identifier specifying whether the input stream will be + * automatically closed + */ + public void setAutoClose(boolean autoClose) { + this.autoClose = autoClose; + } + + /** + * Obtain the data transfer listener. + * + * @return Data transfer listener + */ + public ProgressListener getProgressListener() { + return progressListener; + } + + /** + * Set the data transfer listener. + * + * @param progressListener + * Data transfer listener + */ + public void setProgressListener(ProgressListener progressListener) { + this.progressListener = progressListener; + } + + /** + * Obtain the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @return Callback threshold of the data transfer listener + */ + public long getProgressInterval() { + return progressInterval; + } + + /** + * Set the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @param progressInterval + * Callback threshold of the data transfer listener + */ + public void setProgressInterval(long progressInterval) { + this.progressInterval = progressInterval; + } + + @Override + public String toString() { + return "PutObjectRequest [file=" + file + ", input=" + input + ", metadata=" + metadata + + ", isEncodeHeaders=" + encodeHeaders + ", expires=" + expires + + ", offset=" + offset + ", autoClose=" + autoClose + ", progressListener=" + progressListener + + ", progressInterval=" + progressInterval + ", getBucketName()=" + getBucketName() + + ", getObjectKey()=" + getObjectKey() + ", getSseKmsHeader()=" + getSseKmsHeader() + + ", getSseCHeader()=" + getSseCHeader() + ", getAcl()=" + getAcl() + ", getSuccessRedirectLocation()=" + + getSuccessRedirectLocation() + ", getAllGrantPermissions()=" + getAllGrantPermissions() + + ", getExtensionPermissionMap()=" + getExtensionPermissionMap() + ", isRequesterPays()=" + + isRequesterPays() + "]"; + } + } diff --git a/app/src/main/java/com/obs/services/model/PutObjectResult.java b/app/src/main/java/com/obs/services/model/PutObjectResult.java index cf6cd29..5d9a2b7 100644 --- a/app/src/main/java/com/obs/services/model/PutObjectResult.java +++ b/app/src/main/java/com/obs/services/model/PutObjectResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.Map; @@ -18,113 +19,101 @@ /** * Response to an object upload request */ -public class PutObjectResult extends HeaderResponse -{ - +public class PutObjectResult extends HeaderResponse { + private String bucketName; - + private String objectKey; - + private String etag; - + private String versionId; - + private StorageClassEnum storageClass; - + private String objectUrl; - + public PutObjectResult(String bucketName, String objectKey, String etag, String versionId, - StorageClassEnum storageClass, String objectUrl) { - super(); - this.bucketName = bucketName; - this.objectKey = objectKey; - this.etag = etag; - this.versionId = versionId; - this.storageClass = storageClass; - this.objectUrl = objectUrl; - } - - public PutObjectResult(String bucketName, String objectKey, String etag, String versionId, - String objectUrl, Map responseHeaders, int statusCode) { - super(); - this.bucketName = bucketName; - this.objectKey = objectKey; - this.etag = etag; - this.versionId = versionId; - this.objectUrl = objectUrl; - this.responseHeaders = responseHeaders; - this.statusCode = statusCode; - } - - - /** + StorageClassEnum storageClass, String objectUrl) { + super(); + this.bucketName = bucketName; + this.objectKey = objectKey; + this.etag = etag; + this.versionId = versionId; + this.storageClass = storageClass; + this.objectUrl = objectUrl; + } + + public PutObjectResult(String bucketName, String objectKey, String etag, String versionId, String objectUrl, + Map responseHeaders, int statusCode) { + super(); + this.bucketName = bucketName; + this.objectKey = objectKey; + this.etag = etag; + this.versionId = versionId; + this.objectUrl = objectUrl; + this.responseHeaders = responseHeaders; + this.statusCode = statusCode; + } + + /** * Obtain the ETag of the object. * * @return ETag of the object */ - public String getEtag() - { + public String getEtag() { return etag; } - /** - * Obtain the name of the bucket to which the object belongs. + * Obtain the name of the bucket to which the object belongs. + * * @return Name of the bucket to which the object belongs */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } - /** * Obtain the object name. + * * @return Object name */ - public String getObjectKey() - { + public String getObjectKey() { return objectKey; } - - /** * Obtain the object version ID. + * * @return Version ID of the object */ - public String getVersionId() - { + public String getVersionId() { return versionId; } - - /** - * Obtain the object storage class. + * Obtain the object storage class. + * * @return Object storage class */ - public StorageClassEnum getObjectStorageClass() - { + public StorageClassEnum getObjectStorageClass() { return storageClass; } - /** * Obtain the full path to the object. + * * @return Full path to the object */ - public String getObjectUrl() { - return objectUrl; - } - - - @Override - public String toString() { - return "PutObjectResult [bucketName=" + bucketName + ", objectKey=" + objectKey + ", etag=" + etag - + ", versionId=" + versionId + ", storageClass=" + storageClass + ", objectUrl=" + objectUrl + "]"; - } + public String getObjectUrl() { + return objectUrl; + } + @Override + public String toString() { + return "PutObjectResult [bucketName=" + bucketName + ", objectKey=" + objectKey + ", etag=" + etag + + ", versionId=" + versionId + ", storageClass=" + storageClass + ", objectUrl=" + objectUrl + "]"; + } - } diff --git a/app/src/main/java/com/obs/services/model/PutObjectsRequest.java b/app/src/main/java/com/obs/services/model/PutObjectsRequest.java index 4f31974..38ca866 100644 --- a/app/src/main/java/com/obs/services/model/PutObjectsRequest.java +++ b/app/src/main/java/com/obs/services/model/PutObjectsRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.HashMap; @@ -19,6 +20,7 @@ import java.util.Map; import java.util.Set; +import com.obs.services.internal.Constants; import com.obs.services.internal.ObsConstraint; import com.obs.services.internal.utils.ServiceUtils; @@ -27,181 +29,203 @@ * */ public class PutObjectsRequest extends AbstractBulkRequest { - private String folderPath; - - private String prefix; - - private List filePaths; - - private TaskCallback callback; - - // Part size, in bytes, 5 MB by default. - private long partSize = 1024 * 1024 * 5l; - - // Threshold size of a file for multipart upload, in bytes, 100 MB by default. - private long bigfileThreshold = 1024 * 1024 * 100l; - + private String folderPath; + + private String prefix; + + private List filePaths; + + private TaskCallback callback; + + // Part size, in bytes, 5 MB by default. + private long partSize = 1024 * 1024 * 5L; + + // Threshold size of a file for multipart upload, in bytes, 100 MB by + // default. + private long bigfileThreshold = 1024 * 1024 * 100L; + // Number of threads for multipart upload, 1 by default. private int taskNum = 1; - + private UploadObjectsProgressListener listener; - + // Interval for updating the detailed information, 500 KB by default. private long taskProgressInterval = 5 * ObsConstraint.DEFAULT_PROGRESS_INTERVAL; - - // Callback threshold of the data transfer listener of each object, 100 KB by default + + // Callback threshold of the data transfer listener of each object, 100 KB + // by default private long detailProgressInterval = ObsConstraint.DEFAULT_PROGRESS_INTERVAL; - + private Map> extensionPermissionMap; - + private AccessControlList acl; - + private String successRedirectLocation; - + private SseKmsHeader sseKmsHeader; - + private SseCHeader sseCHeader; - - - /** - * Constructor - * @param bucketName Bucket name - * @param folderPath Local path from which the folder is uploaded - */ - public PutObjectsRequest(String bucketName, String folderPath) { - super(bucketName); - this.folderPath = folderPath; - } - - /** - * Constructor - * @param bucketName Bucket name - * @param filePaths List of local paths from which a batch of files are uploaded - */ - public PutObjectsRequest(String bucketName, List filePaths) { - super(bucketName); - this.filePaths = filePaths; - } - - /** - * Obtain the local path of the uploaded folder. - * @return folderPath Local path from which the folder is uploaded - */ - public String getFolderPath() { - return folderPath; - } - - - /** - * Obtain the list of local paths of the batch uploaded files. - * @return filePaths List of local paths from which a batch of files are uploaded - */ - public List getFilePaths() { - return filePaths; - } - - /** - * Obtain the callback object of an upload task. - * @return callback Callback object - */ - public TaskCallback getCallback() { - return callback; - } - - /** - * Set the callback object of an upload task. - * @param callback Callback object - */ - public void setCallback(TaskCallback callback) { - this.callback = callback; - } - - /** - * Obtain the part size set for uploading an object. - * @return partSize Part size - */ - public long getPartSize() { - return partSize; - } - - /** - * Set the part size for uploading the object. - * @param partSize Part size - */ - public void setPartSize(long partSize) { - this.partSize = partSize; - } - - /** - * Obtain the threshold size of a file for starting multipart upload. - * @return bigfileThreshold Threshold size of a file for multipart upload - */ - public long getBigfileThreshold() { - return bigfileThreshold; - } - - /** - * Set the threshold size of a file for starting multipart upload. - * @param bigfileThreshold Threshold size of a file for multipart upload - */ - public void setBigfileThreshold(long bigfileThreshold) { - if(bigfileThreshold < 100 * 1024l) { - this.bigfileThreshold = 100 * 1024l; - }else if (bigfileThreshold > 5 * 1024 * 1024 * 1024l) { - this.bigfileThreshold = 5 * 1024 * 1024 * 1024l; - }else { - this.bigfileThreshold = bigfileThreshold; - } - } - - /** - * Obtain the maximum number of threads used for processing upload tasks concurrently. - * - * @return Maximum number of threads used for processing upload tasks concurrently - */ - public int getTaskNum() { - return taskNum; - } - - /** - * Set the maximum number of threads used for processing upload tasks concurrently. - * - * @param taskNum Maximum number of threads used for processing upload tasks concurrently - */ - public void setTaskNum(int taskNum) { - if (taskNum < 1) { - this.taskNum = 1; - } else if (taskNum > 1000) { - this.taskNum = 1000; - } else { - this.taskNum = taskNum; - } - } - - /** - * Obtain the specific folder to which the file is uploaded. - * @return prefix Folder - */ - public String getPrefix() { - return prefix; - } - - /** - * Set the specific folder to which the file is uploaded. - * @param prefix Folder - */ - public void setPrefix(String prefix) { - if(null == prefix) { - return; - }else if(prefix.endsWith("/")) { - this.prefix = prefix; - }else { - this.prefix = prefix + "/"; - } - - } - - /** + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param folderPath + * Local path from which the folder is uploaded + */ + public PutObjectsRequest(String bucketName, String folderPath) { + this.bucketName = bucketName; + this.folderPath = folderPath; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param filePaths + * List of local paths from which a batch of files are uploaded + */ + public PutObjectsRequest(String bucketName, List filePaths) { + this.bucketName = bucketName; + this.filePaths = filePaths; + } + + /** + * Obtain the local path of the uploaded folder. + * + * @return folderPath Local path from which the folder is uploaded + */ + public String getFolderPath() { + return folderPath; + } + + /** + * Obtain the list of local paths of the batch uploaded files. + * + * @return filePaths List of local paths from which a batch of files are + * uploaded + */ + public List getFilePaths() { + return filePaths; + } + + /** + * Obtain the callback object of an upload task. + * + * @return callback Callback object + */ + public TaskCallback getCallback() { + return callback; + } + + /** + * Set the callback object of an upload task. + * + * @param callback + * Callback object + */ + public void setCallback(TaskCallback callback) { + this.callback = callback; + } + + /** + * Obtain the part size set for uploading an object. + * + * @return partSize Part size + */ + public long getPartSize() { + return partSize; + } + + /** + * Set the part size for uploading the object. + * + * @param partSize + * Part size + */ + public void setPartSize(long partSize) { + this.partSize = partSize; + } + + /** + * Obtain the threshold size of a file for starting multipart upload. + * + * @return bigfileThreshold Threshold size of a file for multipart upload + */ + public long getBigfileThreshold() { + return bigfileThreshold; + } + + /** + * Set the threshold size of a file for starting multipart upload. + * + * @param bigfileThreshold + * Threshold size of a file for multipart upload + */ + public void setBigfileThreshold(long bigfileThreshold) { + if (bigfileThreshold < Constants.MIN_PART_SIZE) { + this.bigfileThreshold = Constants.MIN_PART_SIZE; + } else { + this.bigfileThreshold = Math.min(bigfileThreshold, Constants.MAX_PART_SIZE); + } + } + + /** + * Obtain the maximum number of threads used for processing upload tasks + * concurrently. + * + * @return Maximum number of threads used for processing upload tasks + * concurrently + */ + public int getTaskNum() { + return taskNum; + } + + /** + * Set the maximum number of threads used for processing upload tasks + * concurrently. + * + * @param taskNum + * Maximum number of threads used for processing upload tasks + * concurrently + */ + public void setTaskNum(int taskNum) { + if (taskNum < 1) { + this.taskNum = 1; + } else { + this.taskNum = Math.min(taskNum, 1000); + } + } + + /** + * Obtain the specific folder to which the file is uploaded. + * + * @return prefix Folder + */ + public String getPrefix() { + return prefix; + } + + /** + * Set the specific folder to which the file is uploaded. + * + * @param prefix + * Folder + */ + public void setPrefix(String prefix) { + if (null == prefix) { + return; + } else if (prefix.endsWith("/")) { + this.prefix = prefix; + } else { + this.prefix = prefix + "/"; + } + + } + + /** * Obtain the progress listener of the bulk task. * * @return Progress listener @@ -213,247 +237,260 @@ public UploadObjectsProgressListener getUploadObjectsProgressListener() { /** * Set the progress listener of the bulk task. * - * @param listener Progress listener + * @param listener + * Progress listener */ public void setUploadObjectsProgressListener(UploadObjectsProgressListener listener) { this.listener = listener; } - /** - * Obtain the interval for updating the task progress information. - * @return taskProgressInterval Interval for updating the task progress - */ - public long getTaskProgressInterval() { - return taskProgressInterval; - } - - /** - * Set the interval for updating the task progress information. - * @param taskProgressInterval Interval for updating the task progress - */ - public void setTaskProgressInterval(long taskProgressInterval) { - if(taskProgressInterval < this.detailProgressInterval) - { - this.taskProgressInterval = this.detailProgressInterval; - }else { - this.taskProgressInterval = taskProgressInterval; - } - } - - /** - * Obtain the callback threshold of the data transfer listener. The default value is 100 KB. - * @return Callback threshold of the data transfer listener - */ - public long getDetailProgressInterval() { - return detailProgressInterval; - } - - /** - * Set the callback threshold of the data transfer listener. The default value is 100 KB. - * @param detailProgressInterval Callback threshold of the data transfer listener - */ - public void setDetailProgressInterval(long detailProgressInterval) { - this.detailProgressInterval = detailProgressInterval; - } - + /** + * Obtain the interval for updating the task progress information. + * + * @return taskProgressInterval Interval for updating the task progress + */ + public long getTaskProgressInterval() { + return taskProgressInterval; + } + + /** + * Set the interval for updating the task progress information. + * + * @param taskProgressInterval + * Interval for updating the task progress + */ + public void setTaskProgressInterval(long taskProgressInterval) { + this.taskProgressInterval = Math.max(taskProgressInterval, this.detailProgressInterval); + } + + /** + * Obtain the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @return Callback threshold of the data transfer listener + */ + public long getDetailProgressInterval() { + return detailProgressInterval; + } + + /** + * Set the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @param detailProgressInterval + * Callback threshold of the data transfer listener + */ + public void setDetailProgressInterval(long detailProgressInterval) { + this.detailProgressInterval = detailProgressInterval; + } + /** * Obtain SSE-KMS encryption headers of the object. * * @return SSE-KMS encryption headers */ - public SseKmsHeader getSseKmsHeader() - { + public SseKmsHeader getSseKmsHeader() { return sseKmsHeader; } - + /** * Set SSE-KMS encryption headers of the object. * - * @param sseKmsHeader SSE-KMS encryption headers + * @param sseKmsHeader + * SSE-KMS encryption headers */ - public void setSseKmsHeader(SseKmsHeader sseKmsHeader) - { + public void setSseKmsHeader(SseKmsHeader sseKmsHeader) { this.sseKmsHeader = sseKmsHeader; } - + /** * Obtain SSE-C encryption headers of the object. * * @return SSE-C encryption headers */ - public SseCHeader getSseCHeader() - { + public SseCHeader getSseCHeader() { return sseCHeader; } - + /** * Set SSE-C encryption headers of the object. * - * @param sseCHeader SSE-C encryption headers + * @param sseCHeader + * SSE-C encryption headers */ - public void setSseCHeader(SseCHeader sseCHeader) - { + public void setSseCHeader(SseCHeader sseCHeader) { this.sseCHeader = sseCHeader; } - + /** * Obtain the ACL of the object. + * * @return Object ACL */ - public AccessControlList getAcl() - { + public AccessControlList getAcl() { return acl; } - + /** * Set the object ACL. - * @param acl Object ACL + * + * @param acl + * Object ACL */ - public void setAcl(AccessControlList acl) - { + public void setAcl(AccessControlList acl) { this.acl = acl; } - - /** - * Obtain the redirection address after a successfully responded request. - * @return Redirection address - */ - public String getSuccessRedirectLocation() { - return successRedirectLocation; - } - - /** - * Set the redirection address after a successfully responded request. - * @param successRedirectLocation Redirection address - */ - public void setSuccessRedirectLocation(String successRedirectLocation) { - this.successRedirectLocation = successRedirectLocation; - } - - /** - * Grant the OBS extension permissions to a user. - * @param domainId User's domain ID - * @param extensionPermissionEnum OBS extension permissions - */ - public void grantExtensionPermission(String domainId, ExtensionObjectPermissionEnum extensionPermissionEnum) { - if(extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { - return; - } - Set users = getExtensionPermissionMap().get(extensionPermissionEnum); - if(users == null) { - users = new HashSet(); - getExtensionPermissionMap().put(extensionPermissionEnum, users); - } - users.add(domainId.trim()); - } - - /** - * Withdraw the OBS extension permissions from a user. - * @param domainId User's domain ID - * @param extensionPermissionEnum OBS extension permissions - */ - public void withdrawExtensionPermission(String domainId, ExtensionObjectPermissionEnum extensionPermissionEnum) { - if(extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { - return; - } - domainId = domainId.trim(); - Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); - if(domainIds != null && domainIds.contains(domainId)) { - domainIds.remove(domainId); - } - } - - /** - * Withdraw all OBS extension permissions from a user. - * @param domainId User's domain ID - */ - public void withdrawExtensionPermissions(String domainId) { - if(ServiceUtils.isValid(domainId)) { - for(Map.Entry> entry : this.getExtensionPermissionMap().entrySet()) { - if(entry.getValue().contains(domainId.trim())) { - entry.getValue().remove(domainId); - } - } - } - } - - /** - * Obtain all OBS extended permissions. - * @return OBS List of extension permissions - */ - public Set getAllGrantPermissions(){ - return this.getExtensionPermissionMap().keySet(); - } - - /** - * Obtain the list of user IDs with the specified OBS extension permissions. - * @param extensionPermissionEnum OBS extension permissions - * @return List of user IDs - */ - public Set getDomainIdsByGrantPermission(ExtensionObjectPermissionEnum extensionPermissionEnum) { - Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); - if(domainIds == null) { - domainIds = new HashSet(); - } - return domainIds; - } - - /** - * Obtain the OBS extension permissions of a specified user. - * @param domainId User ID - * @return OBS Extension permission set - */ - public Set getGrantPermissionsByDomainId(String domainId) { - Set grantPermissions = new HashSet(); - if(ServiceUtils.isValid(domainId)) { - domainId = domainId.trim(); - for(Map.Entry> entry : this.getExtensionPermissionMap().entrySet()) { - if(entry.getValue().contains(domainId)) { - grantPermissions.add(entry.getKey()); - } - } - } - return grantPermissions; - } - - /** - * Obtain the set of relationships between users and OBS extension permissions. - * @return Set of relationships between users and OBS extended permissions - */ + + /** + * Obtain the redirection address after a successfully responded request. + * + * @return Redirection address + */ + public String getSuccessRedirectLocation() { + return successRedirectLocation; + } + + /** + * Set the redirection address after a successfully responded request. + * + * @param successRedirectLocation + * Redirection address + */ + public void setSuccessRedirectLocation(String successRedirectLocation) { + this.successRedirectLocation = successRedirectLocation; + } + + /** + * Grant the OBS extension permissions to a user. + * + * @param domainId + * User's domain ID + * @param extensionPermissionEnum + * OBS extension permissions + */ + public void grantExtensionPermission(String domainId, ExtensionObjectPermissionEnum extensionPermissionEnum) { + if (extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { + return; + } + Set users = getExtensionPermissionMap().computeIfAbsent(extensionPermissionEnum, k -> new HashSet<>()); + users.add(domainId.trim()); + } + + /** + * Withdraw the OBS extension permissions from a user. + * + * @param domainId + * User's domain ID + * @param extensionPermissionEnum + * OBS extension permissions + */ + public void withdrawExtensionPermission(String domainId, ExtensionObjectPermissionEnum extensionPermissionEnum) { + if (extensionPermissionEnum == null || !ServiceUtils.isValid(domainId)) { + return; + } + domainId = domainId.trim(); + Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); + if (domainIds != null) { + domainIds.remove(domainId); + } + } + + /** + * Withdraw all OBS extension permissions from a user. + * + * @param domainId + * User's domain ID + */ + public void withdrawExtensionPermissions(String domainId) { + if (ServiceUtils.isValid(domainId)) { + for (Map.Entry> entry : this.getExtensionPermissionMap() + .entrySet()) { + if (entry.getValue().contains(domainId.trim())) { + entry.getValue().remove(domainId); + } + } + } + } + + /** + * Obtain all OBS extended permissions. + * + * @return OBS List of extension permissions + */ + public Set getAllGrantPermissions() { + return this.getExtensionPermissionMap().keySet(); + } + + /** + * Obtain the list of user IDs with the specified OBS extension permissions. + * + * @param extensionPermissionEnum + * OBS extension permissions + * @return List of user IDs + */ + public Set getDomainIdsByGrantPermission(ExtensionObjectPermissionEnum extensionPermissionEnum) { + Set domainIds = getExtensionPermissionMap().get(extensionPermissionEnum); + if (domainIds == null) { + domainIds = new HashSet(); + } + return domainIds; + } + + /** + * Obtain the OBS extension permissions of a specified user. + * + * @param domainId + * User ID + * @return OBS Extension permission set + */ + public Set getGrantPermissionsByDomainId(String domainId) { + Set grantPermissions = new HashSet(); + if (ServiceUtils.isValid(domainId)) { + domainId = domainId.trim(); + for (Map.Entry> entry : this.getExtensionPermissionMap() + .entrySet()) { + if (entry.getValue().contains(domainId)) { + grantPermissions.add(entry.getKey()); + } + } + } + return grantPermissions; + } + + /** + * Obtain the set of relationships between users and OBS extension + * permissions. + * + * @return Set of relationships between users and OBS extended permissions + */ public Map> getExtensionPermissionMap() { - if(extensionPermissionMap == null) { - extensionPermissionMap = new HashMap>(); - } - return extensionPermissionMap; - } + if (extensionPermissionMap == null) { + extensionPermissionMap = new HashMap>(); + } + return extensionPermissionMap; + } /** * Set the set of relationships between users and OBS extension permissions. - * @param extensionPermissionMap Set of relationships between users and OBS extended permissions - */ - public void setExtensionPermissionMap(Map> extensionPermissionMap) { - if(extensionPermissionMap == null) { - return; - } - this.extensionPermissionMap = extensionPermissionMap; - } - - @Override - public String toString() { - return "PutObjectsRequest [bucketName=" + bucketName + ", folderPath=" + folderPath + ", listFilePath=" + getFilePathsString() + "]"; - } - - private String getFilePathsString() { - if(null == this.filePaths || this.filePaths.isEmpty()) { - return ""; - }else { - final String SEPARATOR = ", "; - StringBuffer fileBuffer = new StringBuffer(); - for(String filePatg: filePaths) { - fileBuffer.append(filePatg); - fileBuffer.append(SEPARATOR); - } - return fileBuffer.substring(0, fileBuffer.length() - SEPARATOR.length()); - } - } + * + * @param extensionPermissionMap + * Set of relationships between users and OBS extended + * permissions + */ + public void setExtensionPermissionMap(Map> extensionPermissionMap) { + if (extensionPermissionMap == null) { + return; + } + this.extensionPermissionMap = extensionPermissionMap; + } + + @Override + public String toString() { + return "PutObjectsRequest [folderPath=" + folderPath + ", prefix=" + prefix + ", filePaths=" + filePaths + + ", callback=" + callback + ", partSize=" + partSize + ", bigfileThreshold=" + bigfileThreshold + + ", taskNum=" + taskNum + ", listener=" + listener + ", taskProgressInterval=" + taskProgressInterval + + ", detailProgressInterval=" + detailProgressInterval + ", extensionPermissionMap=" + + extensionPermissionMap + ", acl=" + acl + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } } diff --git a/app/src/main/java/com/obs/services/model/ReadAheadQueryResult.java b/app/src/main/java/com/obs/services/model/ReadAheadQueryResult.java index a84125c..6fbf3ba 100644 --- a/app/src/main/java/com/obs/services/model/ReadAheadQueryResult.java +++ b/app/src/main/java/com/obs/services/model/ReadAheadQueryResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import com.fasterxml.jackson.annotation.JsonProperty; @@ -20,150 +21,175 @@ * */ public class ReadAheadQueryResult extends HeaderResponse { - @JsonProperty(value = "bucket") - private String bucketName; - - @JsonProperty(value = "prefix") - private String prefix; - - @JsonProperty(value = "consumedTime") - private long consumedTime; - - @JsonProperty(value = "finishedObjectNum") - private long finishedObjectNum; - - @JsonProperty(value = "finishedSize") - private long finishedSize; - - @JsonProperty(value = "status") - private String status; - - /** - * Constructor - */ - public ReadAheadQueryResult() { - - } - - /** - * Constructor - * @param bucketName Bucket name - * @param prefix Name prefix of objects to be read ahead - * @param consumedTime Consumed time (seconds) - * @param finishedObjectNum Number of finished objects - * @param finishedSize Size of finished objects - * @param status Task status - */ - public ReadAheadQueryResult(String bucketName, String prefix, long consumedTime, - long finishedObjectNum, long finishedSize, String status) { - this.bucketName = bucketName; - this.prefix = prefix; - this.consumedTime = consumedTime; - this.finishedObjectNum = finishedObjectNum; - this.finishedSize = finishedSize; - this.status = status; - } - - /** - * Obtain the bucket name. - * @return Bucket name - */ - public String getBucketName() { - return bucketName; - } - - /** - * Set the bucket name. - * @param bucketName Bucket name - */ - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } - - /** - * Obtain the name prefix of objects to be read ahead. - * @return Name prefix of objects to be read ahead - */ - public String getPrefix() { - return prefix; - } - - /** - * Set the name prefix of objects to be read ahead. - * @param prefix Name prefix of objects to be read ahead - */ - public void setPrefix(String prefix) { - this.prefix = prefix; - } - - /** - * Obtain the consumed time (seconds) - * @return Consumed time (seconds) - */ - public long getConsumedTime() { - return consumedTime; - } - - /** - * Set the consumed time (seconds) - * @param consumedTime Consumed time (seconds) - */ - public void setConsumedTime(long consumedTime) { - this.consumedTime = consumedTime; - } - - /** - * Obtain the number of finished objects. - * @return Number of finished objects - */ - public long getFinishedObjectNum() { - return finishedObjectNum; - } - - /** - * Set the number of finished objects. - * @param finishedObjectNum Number of finished objects - */ - public void setFinishedObjectNum(long finishedObjectNum) { - this.finishedObjectNum = finishedObjectNum; - } - - /** - * Obtain the size of finished objects. - * @return Size of finished objects - */ - public long getFinishedSize() { - return finishedSize; - } - - /** - * Set the size of finished objects. - * @param finishedSize Size of finished objects - */ - public void setFinishedSize(long finishedSize) { - this.finishedSize = finishedSize; - } - - /** - * Obtain the task status. - * @return Task status - */ - public String getStatus() { - return status; - } - - /** - * Set the task status. - * @param status Task status - */ - public void setStatus(String status) { - this.status = status; - } - - @Override - public String toString() { - return "ReadAheadQueryResult [bucketName=" + bucketName + ", prefix=" + prefix - +", consumedTime=" + consumedTime + ", finishedObjectNum=" + finishedObjectNum - + ", finishedSize=" + finishedSize + ", status=" + status + "]"; - } + @JsonProperty(value = "bucket") + private String bucketName; + + @JsonProperty(value = "prefix") + private String prefix; + + @JsonProperty(value = "consumedTime") + private long consumedTime; + + @JsonProperty(value = "finishedObjectNum") + private long finishedObjectNum; + + @JsonProperty(value = "finishedSize") + private long finishedSize; + + @JsonProperty(value = "status") + private String status; + + /** + * Constructor + */ + public ReadAheadQueryResult() { + + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param prefix + * Name prefix of objects to be read ahead + * @param consumedTime + * Consumed time (seconds) + * @param finishedObjectNum + * Number of finished objects + * @param finishedSize + * Size of finished objects + * @param status + * Task status + */ + public ReadAheadQueryResult(String bucketName, String prefix, long consumedTime, long finishedObjectNum, + long finishedSize, String status) { + this.bucketName = bucketName; + this.prefix = prefix; + this.consumedTime = consumedTime; + this.finishedObjectNum = finishedObjectNum; + this.finishedSize = finishedSize; + this.status = status; + } + + /** + * Obtain the bucket name. + * + * @return Bucket name + */ + public String getBucketName() { + return bucketName; + } + + /** + * Set the bucket name. + * + * @param bucketName + * Bucket name + */ + public void setBucketName(String bucketName) { + this.bucketName = bucketName; + } + + /** + * Obtain the name prefix of objects to be read ahead. + * + * @return Name prefix of objects to be read ahead + */ + public String getPrefix() { + return prefix; + } + + /** + * Set the name prefix of objects to be read ahead. + * + * @param prefix + * Name prefix of objects to be read ahead + */ + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + /** + * Obtain the consumed time (seconds) + * + * @return Consumed time (seconds) + */ + public long getConsumedTime() { + return consumedTime; + } + + /** + * Set the consumed time (seconds) + * + * @param consumedTime + * Consumed time (seconds) + */ + public void setConsumedTime(long consumedTime) { + this.consumedTime = consumedTime; + } + + /** + * Obtain the number of finished objects. + * + * @return Number of finished objects + */ + public long getFinishedObjectNum() { + return finishedObjectNum; + } + + /** + * Set the number of finished objects. + * + * @param finishedObjectNum + * Number of finished objects + */ + public void setFinishedObjectNum(long finishedObjectNum) { + this.finishedObjectNum = finishedObjectNum; + } + + /** + * Obtain the size of finished objects. + * + * @return Size of finished objects + */ + public long getFinishedSize() { + return finishedSize; + } + + /** + * Set the size of finished objects. + * + * @param finishedSize + * Size of finished objects + */ + public void setFinishedSize(long finishedSize) { + this.finishedSize = finishedSize; + } + + /** + * Obtain the task status. + * + * @return Task status + */ + public String getStatus() { + return status; + } + + /** + * Set the task status. + * + * @param status + * Task status + */ + public void setStatus(String status) { + this.status = status; + } + + @Override + public String toString() { + return "ReadAheadQueryResult [bucketName=" + bucketName + ", prefix=" + prefix + ", consumedTime=" + + consumedTime + ", finishedObjectNum=" + finishedObjectNum + ", finishedSize=" + finishedSize + + ", status=" + status + "]"; + } } diff --git a/app/src/main/java/com/obs/services/model/ReadAheadRequest.java b/app/src/main/java/com/obs/services/model/ReadAheadRequest.java index f9dab3b..f79bb34 100644 --- a/app/src/main/java/com/obs/services/model/ReadAheadRequest.java +++ b/app/src/main/java/com/obs/services/model/ReadAheadRequest.java @@ -3,116 +3,123 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * Request parameters for reading ahead objects */ -public class ReadAheadRequest { - private String bucketName; - - private String prefix; - - private CacheOptionEnum cacheOption; - - private long ttl = 60 * 60 * 24L; - - /** - * Constructor - * @param bucketName Bucket name - * @param prefix Name prefix of objects to be read ahead - */ - public ReadAheadRequest(String bucketName, String prefix) { - this.setBucketName(bucketName); - this.setPrefix(prefix); - } - - /** - * Constructor - * @param bucketName Bucket name - * @param prefix Name prefix of objects to be read ahead - * @param cacheOption Control option of the read-ahead cache - * @param ttl Expiration time of cached data, in seconds. The value ranges from 0 to 259200 (3 days) - */ - public ReadAheadRequest(String bucketName, String prefix, CacheOptionEnum cacheOption, long ttl) { - this.setBucketName(bucketName); - this.setPrefix(prefix); - this.setCacheOption(cacheOption); - this.setTtl(ttl); - } +public class ReadAheadRequest extends GenericRequest { + + { + httpMethod = HttpMethodEnum.POST; + } + + private String prefix; + + private CacheOptionEnum cacheOption; + + private long ttl = 60 * 60 * 24L; - /** - * Obtain the bucket name. - * @return Bucket name - */ - public String getBucketName() { - return bucketName; - } + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param prefix + * Name prefix of objects to be read ahead + */ + public ReadAheadRequest(String bucketName, String prefix) { + this.bucketName = bucketName; + this.prefix = prefix; + } - /** - * Set the bucket name. - * @param bucketName Bucket name - */ - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param prefix + * Name prefix of objects to be read ahead + * @param cacheOption + * Control option of the read-ahead cache + * @param ttl + * Expiration time of cached data, in seconds. The value ranges + * from 0 to 259200 (3 days) + */ + public ReadAheadRequest(String bucketName, String prefix, CacheOptionEnum cacheOption, long ttl) { + this.bucketName = bucketName; + this.prefix = prefix; + this.cacheOption = cacheOption; + this.ttl = ttl; + } - /** - * Obtain the name prefix of objects to be read ahead. - * @return Name prefix of objects to be read ahead - */ - public String getPrefix() { - return prefix; - } + /** + * Obtain the name prefix of objects to be read ahead. + * + * @return Name prefix of objects to be read ahead + */ + public String getPrefix() { + return prefix; + } - /** - * Set the name prefix of objects to be read ahead. - * @param prefix Name prefix of objects to be read ahead - */ - public void setPrefix(String prefix) { - this.prefix = prefix; - } + /** + * Set the name prefix of objects to be read ahead. + * + * @param prefix + * Name prefix of objects to be read ahead + */ + public void setPrefix(String prefix) { + this.prefix = prefix; + } - /** - * Obtain the control option of the read-ahead cache. - * @return Control option of the read-ahead cache - */ - public CacheOptionEnum getCacheOption() { - return cacheOption; - } + /** + * Obtain the control option of the read-ahead cache. + * + * @return Control option of the read-ahead cache + */ + public CacheOptionEnum getCacheOption() { + return cacheOption; + } - /** - * Set the control option of the read-ahead cache. - * @param cacheOption Control option of the read-ahead cache - */ - public void setCacheOption(CacheOptionEnum cacheOption) { - this.cacheOption = cacheOption; - } + /** + * Set the control option of the read-ahead cache. + * + * @param cacheOption + * Control option of the read-ahead cache + */ + public void setCacheOption(CacheOptionEnum cacheOption) { + this.cacheOption = cacheOption; + } - /** - * Obtain the cache data expiration time. - * @return Expiration time of cached data, in seconds - */ - public long getTtl() { - return ttl; - } + /** + * Obtain the cache data expiration time. + * + * @return Expiration time of cached data, in seconds + */ + public long getTtl() { + return ttl; + } - /** - * Set the cache data expiration time. - * @param ttl Expiration time of cached data, in seconds. The value ranges from 0 to 259200 (72 hours). The default value is 24 hours. - */ - public void setTtl(long ttl) { - if(ttl < 0 || ttl > 259200) { - return; - } - this.ttl = ttl; - } + /** + * Set the cache data expiration time. + * + * @param ttl + * Expiration time of cached data, in seconds. The value ranges + * from 0 to 259200 (72 hours). The default value is 24 hours. + */ + public void setTtl(long ttl) { + if (ttl < 0 || ttl > 259200) { + return; + } + this.ttl = ttl; + } } diff --git a/app/src/main/java/com/obs/services/model/ReadAheadResult.java b/app/src/main/java/com/obs/services/model/ReadAheadResult.java index fd14176..20634a0 100644 --- a/app/src/main/java/com/obs/services/model/ReadAheadResult.java +++ b/app/src/main/java/com/obs/services/model/ReadAheadResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import com.fasterxml.jackson.annotation.JsonProperty; @@ -20,85 +21,97 @@ * */ public class ReadAheadResult extends HeaderResponse { - @JsonProperty(value = "bucket") - private String bucketName; + @JsonProperty(value = "bucket") + private String bucketName; + + @JsonProperty(value = "prefix") + private String prefix; - @JsonProperty(value = "prefix") - private String prefix; - @JsonProperty(value = "taskID") - private String taskId; - + private String taskId; + /** * Constructor */ public ReadAheadResult() { - + } - + /** * Constructor - * @param bucketName Bucket name - * @param prefix Name prefix of objects to be read ahead - * @param taskId ID of the read-ahead task + * + * @param bucketName + * Bucket name + * @param prefix + * Name prefix of objects to be read ahead + * @param taskId + * ID of the read-ahead task */ public ReadAheadResult(String bucketName, String prefix, String taskId) { - this.bucketName = bucketName; - this.prefix = prefix; - this.taskId = taskId; + this.bucketName = bucketName; + this.prefix = prefix; + this.taskId = taskId; } - + /** - * Obtain the bucket name. + * Obtain the bucket name. + * * @return Bucket name */ public String getBucketName() { - return bucketName; - } + return bucketName; + } + + /** + * Set the bucket name. + * + * @param bucketName + * Bucket name + */ + public void setBucketName(String bucketName) { + this.bucketName = bucketName; + } /** - * Set the bucket name. - * @param bucketName Bucket name + * Obtain the name prefix of objects to be read ahead. + * + * @return Name prefix of objects to be read ahead */ - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } + public String getPrefix() { + return prefix; + } - /** - * Obtain the name prefix of objects to be read ahead. - * @return Name prefix of objects to be read ahead - */ - public String getPrefix() { - return prefix; - } + /** + * Set the name prefix of objects to be read ahead. + * + * @param prefix + * Name prefix of objects to be read ahead + */ + public void setPrefix(String prefix) { + this.prefix = prefix; + } - /** - * Set the name prefix of objects to be read ahead. - * @param prefix Name prefix of objects to be read ahead - */ - public void setPrefix(String prefix) { - this.prefix = prefix; - } + /** + * Obtain the ID of the read-ahead task. + * + * @return ID of the read-ahead task + */ + public String getTaskId() { + return taskId; + } - /** - * Obtain the ID of the read-ahead task. - * @return ID of the read-ahead task - */ - public String getTaskId() { - return taskId; - } + /** + * Set the ID of the read-ahead task. + * + * @param taskId + * ID of the read-ahead task + */ + public void setTaskId(String taskId) { + this.taskId = taskId; + } - /** - * Set the ID of the read-ahead task. - * @param taskId ID of the read-ahead task - */ - public void setTaskId(String taskId) { - this.taskId = taskId; - } - - @Override - public String toString() { - return "ReadAheadResult [bucketName=" + bucketName + ", prefix=" + prefix - +", taskId=" + taskId + "]"; - } + @Override + public String toString() { + return "ReadAheadResult [bucketName=" + bucketName + ", prefix=" + prefix + ", taskId=" + taskId + "]"; + } } diff --git a/app/src/main/java/com/obs/services/model/Redirect.java b/app/src/main/java/com/obs/services/model/Redirect.java index 6a62264..59db9b3 100644 --- a/app/src/main/java/com/obs/services/model/Redirect.java +++ b/app/src/main/java/com/obs/services/model/Redirect.java @@ -11,139 +11,145 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * Request redirection configuration */ -public class Redirect -{ +public class Redirect { private ProtocolEnum protocol; - + private String hostName; - + private String replaceKeyPrefixWith; - + private String replaceKeyWith; - + private String httpRedirectCode; - + /** * Obtain the protocol used for redirecting the request. + * * @return Protocol used for redirecting the request * @see #getRedirectProtocol() */ @Deprecated - public String getProtocol() - { + public String getProtocol() { return this.protocol != null ? this.protocol.getCode() : null; } - + /** * Set the protocol used for redirecting the request. - * @param protocol Protocol used for redirecting the request + * + * @param protocol + * Protocol used for redirecting the request * @see #setRedirectProtocol(ProtocolEnum protocol) */ @Deprecated - public void setProtocol(String protocol) - { + public void setProtocol(String protocol) { this.protocol = ProtocolEnum.getValueFromCode(protocol); } - + /** * Obtain the protocol used for redirecting the request. + * * @return Protocol used for redirecting the request */ - public ProtocolEnum getRedirectProtocol() - { + public ProtocolEnum getRedirectProtocol() { return protocol; } - + /** * Set the protocol used for redirecting the request. - * @param protocol Protocol used for redirecting the request + * + * @param protocol + * Protocol used for redirecting the request */ - public void setRedirectProtocol(ProtocolEnum protocol) - { + public void setRedirectProtocol(ProtocolEnum protocol) { this.protocol = protocol; } - + /** * Obtain the host name used for redirecting the request. + * * @return Host name used for redirecting the request */ - public String getHostName() - { + public String getHostName() { return hostName; } - + /** * Set the host name used for redirecting the request. - * @param hostName Host name used for redirecting the request + * + * @param hostName + * Host name used for redirecting the request */ - public void setHostName(String hostName) - { + public void setHostName(String hostName) { this.hostName = hostName; } - + /** * Obtain the object name prefix used for redirecting the request. + * * @return Object name prefix used for redirecting the request */ - public String getReplaceKeyPrefixWith() - { + public String getReplaceKeyPrefixWith() { return replaceKeyPrefixWith; } - + /** * Set the object name prefix used for redirecting the request. - * @param replaceKeyPrefixWith Object name prefix used for redirecting the request + * + * @param replaceKeyPrefixWith + * Object name prefix used for redirecting the request */ - public void setReplaceKeyPrefixWith(String replaceKeyPrefixWith) - { + public void setReplaceKeyPrefixWith(String replaceKeyPrefixWith) { this.replaceKeyPrefixWith = replaceKeyPrefixWith; } - + /** * Obtain the object name used for redirecting the request. + * * @return Object name used for redirecting the request */ - public String getReplaceKeyWith() - { + public String getReplaceKeyWith() { return replaceKeyWith; } - + /** * Set the object name used for redirecting the request. - * @param replaceKeyWith Object name used for redirecting the request + * + * @param replaceKeyWith + * Object name used for redirecting the request */ - public void setReplaceKeyWith(String replaceKeyWith) - { + public void setReplaceKeyWith(String replaceKeyWith) { this.replaceKeyWith = replaceKeyWith; } - + /** * Obtain the configuration of the HTTP status code in the response. + * * @return Configuration of the HTTP status code */ - public String getHttpRedirectCode() - { + public String getHttpRedirectCode() { return httpRedirectCode; } - + /** * Configure the HTTP status code in the response. - * @param httpRedirectCode Configuration of the HTTP status code + * + * @param httpRedirectCode + * Configuration of the HTTP status code */ - public void setHttpRedirectCode(String httpRedirectCode) - { + public void setHttpRedirectCode(String httpRedirectCode) { this.httpRedirectCode = httpRedirectCode; } - + @Override - public String toString() - { - return "RedirectRule [protocol=" + protocol + ", hostName=" + hostName + ", replaceKeyPrefixWith=" + replaceKeyPrefixWith - + ", replaceKeyWith=" + replaceKeyWith + ", httpRedirectCode=" + httpRedirectCode + "]"; + public String toString() { + return "RedirectRule [protocol=" + protocol + ", hostName=" + hostName + ", replaceKeyPrefixWith=" + + replaceKeyPrefixWith + ", replaceKeyWith=" + replaceKeyWith + ", httpRedirectCode=" + httpRedirectCode + + "]"; } } diff --git a/app/src/main/java/com/obs/services/model/RedirectAllRequest.java b/app/src/main/java/com/obs/services/model/RedirectAllRequest.java index 53d0dad..59b3398 100644 --- a/app/src/main/java/com/obs/services/model/RedirectAllRequest.java +++ b/app/src/main/java/com/obs/services/model/RedirectAllRequest.java @@ -11,81 +11,81 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * Redirection configuration for all requests */ -public class RedirectAllRequest -{ +public class RedirectAllRequest { private ProtocolEnum protocol; - + private String hostName; - + /** * Obtain the protocol used for redirecting requests. + * * @return Protocol used for redirecting requests * @see #getRedirectProtocol() */ @Deprecated - public String getProtocol() - { + public String getProtocol() { return this.protocol != null ? this.protocol.getCode() : null; } - + /** * Set the protocol used for redirecting requests. - * @param protocol Protocol used for redirecting requests + * + * @param protocol + * Protocol used for redirecting requests * @see #setRedirectProtocol(ProtocolEnum protocol) */ @Deprecated - public void setProtocol(String protocol) - { + public void setProtocol(String protocol) { this.protocol = ProtocolEnum.getValueFromCode(protocol); } - + /** * Obtain the protocol used for redirecting requests. + * * @return Protocol used for redirecting requests */ - public ProtocolEnum getRedirectProtocol() - { + public ProtocolEnum getRedirectProtocol() { return protocol; } - + /** * Set the protocol used for redirecting requests. - * @param protocol Protocol used for redirecting requests + * + * @param protocol + * Protocol used for redirecting requests */ - public void setRedirectProtocol(ProtocolEnum protocol) - { + public void setRedirectProtocol(ProtocolEnum protocol) { this.protocol = protocol; } - + /** * Obtain the host name used for redirecting requests. + * * @return Host name used for redirecting requests */ - public String getHostName() - { + public String getHostName() { return hostName; } - + /** * Set the host name used for redirecting requests. - * @param hostName Host name used for redirecting requests + * + * @param hostName + * Host name used for redirecting requests */ - public void setHostName(String hostName) - { + public void setHostName(String hostName) { this.hostName = hostName; } @Override - public String toString() - { + public String toString() { return "RedirectAllRequest [protocol=" + protocol + ", hostName=" + hostName + "]"; } - -} - +} diff --git a/app/src/main/java/com/obs/services/model/RenameObjectRequest.java b/app/src/main/java/com/obs/services/model/RenameObjectRequest.java index 3b65a83..ce921bd 100644 --- a/app/src/main/java/com/obs/services/model/RenameObjectRequest.java +++ b/app/src/main/java/com/obs/services/model/RenameObjectRequest.java @@ -11,93 +11,65 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** - * 重命名文件/文件夹请求参数 - * + * Request parameters for renaming a file or folder
    + * Only the parallel file system supports this interface. + * @since 3.20.3 */ -public class RenameObjectRequest { - - private String bucketName; - - private String objectKey; +public class RenameObjectRequest extends BaseObjectRequest { - private String newObjectKey; - - public RenameObjectRequest() { + { + httpMethod = HttpMethodEnum.POST; + } - } + private String newObjectKey; - /** - * 构造函数 - * @param bucketName 桶名 - * @param objectKey 文件/文件夹名 - * @param newObjectKey 新的文件/文件夹名 - */ - public RenameObjectRequest(String bucketName, String objectKey, String newObjectKey) { - super(); - this.bucketName = bucketName; - this.objectKey = objectKey; - this.newObjectKey = newObjectKey; - } + public RenameObjectRequest() { - /** - * 获取桶名 - * - * @return 桶名 - */ - public String getBucketName() - { - return bucketName; } - + /** - * 设置桶名 + * Constructor * - * @param bucketName 桶名 + * @param bucketName + * Bucket name + * @param objectKey + * File or folder name + * @param newObjectKey + * Name of the new file or folder */ - public void setBucketName(String bucketName) - { + public RenameObjectRequest(String bucketName, String objectKey, String newObjectKey) { this.bucketName = bucketName; + this.objectKey = objectKey; + this.newObjectKey = newObjectKey; } - + + /** - * 获取文件/文件夹名 + * Obtain the new file or folder name. * - * @return 文件/文件夹名 + * @return New file/folder name */ - public String getObjectKey() - { - return objectKey; + public String getNewObjectKey() { + return newObjectKey; } - + /** - * 设置文件/文件夹名 + * Set the new file or folder name. * - * @param objectKey 文件/文件夹名 - * + * @param newObjectKey + * Name of the new file or folder */ - public void setObjectKey(String objectKey) - { - this.objectKey = objectKey; + public void setNewObjectKey(String newObjectKey) { + this.newObjectKey = newObjectKey; } - /** - * 获取新的文件/文件夹名 - * @return 新的文件/文件夹名 - */ - public String getNewObjectKey() { - return newObjectKey; - } - - /** - * 设置新的文件/文件夹名 - * @param newObjectKey 新的文件/文件夹名 - */ - public void setNewObjectKey(String newObjectKey) { - this.newObjectKey = newObjectKey; - } - - + @Override + public String toString() { + return "RenameObjectRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", newObjectKey=" + + newObjectKey + ", isRequesterPays()=" + isRequesterPays() + "]"; + } } diff --git a/app/src/main/java/com/obs/services/model/RenameObjectResult.java b/app/src/main/java/com/obs/services/model/RenameObjectResult.java index 0efd88d..4e1c86c 100644 --- a/app/src/main/java/com/obs/services/model/RenameObjectResult.java +++ b/app/src/main/java/com/obs/services/model/RenameObjectResult.java @@ -11,10 +11,11 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** - * 重命名文件/文件夹响应结果 + * Response to the request for renaming a file/folder * */ public class RenameObjectResult extends HeaderResponse{ diff --git a/app/src/main/java/com/obs/services/model/ReplicationConfiguration.java b/app/src/main/java/com/obs/services/model/ReplicationConfiguration.java index 778ec01..d82346a 100644 --- a/app/src/main/java/com/obs/services/model/ReplicationConfiguration.java +++ b/app/src/main/java/com/obs/services/model/ReplicationConfiguration.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.ArrayList; @@ -20,170 +21,215 @@ * Cross-region replication configuration of a bucket * */ -public class ReplicationConfiguration extends HeaderResponse{ - - private String agency; - - private List rules; - - public static class Rule { - private String id; - private RuleStatusEnum status; - private String prefix; - private Destination destination; - - /** - * Obtain the rule ID. - * @return Rule ID - */ - public String getId() { - return id; - } - - /** - * Set the rule ID. - * @param id Rule ID - */ - public void setId(String id) { - this.id = id; - } - - /** - * Obtain the rule status. - * @return Rule status - */ - public RuleStatusEnum getStatus() { - return status; - } - - /** - * Set the rule status. - * @param status Rule status - */ - public void setStatus(RuleStatusEnum status) { - this.status = status; - } - - /** - * Obtain the object name prefix that identifies objects to which the rule applies. - * @return Object name prefix - */ - public String getPrefix() { - return prefix; - } - - /** - * Set the object name prefix that identifies objects to which the rule applies. - * @param prefix Object name prefix - */ - public void setPrefix(String prefix) { - this.prefix = prefix; - } - - /** - * Obtain the information about the replication destination. - * @return Destination information - */ - public Destination getDestination() { - return destination; - } - - /** - * Set the information about the replication target. - * @param destination Destination information - */ - public void setDestination(Destination destination) { - this.destination = destination; - } - - @Override - public String toString() { - return "Rule [id=" + id + ", status=" + status + ", prefix=" + prefix + ", destination=" + destination - + "]"; - } - } - - public static class Destination { - private String bucket; - private StorageClassEnum storageClass; - - /** - * Obtain the information about the destination bucket. - * @return Destination bucket name - */ - public String getBucket() { - return bucket; - } - - /** - * Set the destination bucket name. - * @param bucket Destination bucket name - */ - public void setBucket(String bucket) { - this.bucket = bucket; - } - - /** - * Obtain the storage class of the object generated after the replication. - * @return Object storage class - */ - public StorageClassEnum getObjectStorageClass() { - return storageClass; - } - - /** - * Set the storage class of the object generated after the replication. - * @param storageClass Object storage class - */ - public void setObjectStorageClass(StorageClassEnum storageClass) { - this.storageClass = storageClass; - } - - @Override - public String toString() { - return "Destination [bucket=" + bucket + ", storageClass=" + storageClass + "]"; - } - } - - /** - * Obtain the replication rule list. - * @return Replication rule list - */ - public List getRules() { - if (rules == null) { - rules = new ArrayList(); - } - return rules; - } - - /** - * Set the replication rule list. - * @param rules Replication rule list - */ - public void setRules(List rules) { - this.rules = rules; - } - - /** - * Set the agent name. - * @return Agent name - */ - public String getAgency() { - return agency; - } - - /** - * Obtain the agent name - * @param agency Agent name - */ - public void setAgency(String agency) { - this.agency = agency; - } - - @Override - public String toString() { - return "ReplicationConfiguration [agency=" + agency + ", rules=" + rules + "]"; - } -} +public class ReplicationConfiguration extends HeaderResponse { + + private String agency; + + private List rules; + + public static class Rule { + private String id; + private RuleStatusEnum status; + private String prefix; + private Destination destination; + private HistoricalObjectReplicationEnum historicalObjectReplication; + + /** + * Obtain the rule ID. + * + * @return Rule ID + */ + public String getId() { + return id; + } + + /** + * Set the rule ID. + * + * @param id + * Rule ID + */ + public void setId(String id) { + this.id = id; + } + + /** + * Obtain the rule status. + * + * @return Rule status + */ + public RuleStatusEnum getStatus() { + return status; + } + + /** + * Set the rule status. + * + * @param status + * Rule status + */ + public void setStatus(RuleStatusEnum status) { + this.status = status; + } + + /** + * Obtain the replication status of historical objects. + * + * @return Rule status + */ + public HistoricalObjectReplicationEnum getHistoricalObjectReplication() { + return historicalObjectReplication; + } + + /** + * Set historical object replication rule. + * + * @param historicalObjectReplication + * Rule status + */ + public void setHistoricalObjectReplication(HistoricalObjectReplicationEnum historicalObjectReplication) { + this.historicalObjectReplication = historicalObjectReplication; + } + + /** + * Obtain the object name prefix that identifies objects to which the + * rule applies. + * + * @return Object name prefix + */ + public String getPrefix() { + return prefix; + } + + /** + * Set the object name prefix that identifies objects to which the rule + * applies. + * + * @param prefix + * Object name prefix + */ + public void setPrefix(String prefix) { + this.prefix = prefix; + } + /** + * Obtain the information about the replication destination. + * + * @return Destination information + */ + public Destination getDestination() { + return destination; + } + /** + * Set the information about the replication target. + * + * @param destination + * Destination information + */ + public void setDestination(Destination destination) { + this.destination = destination; + } + + @Override + public String toString() { + return "Rule [id=" + id + ", status=" + status + ", prefix=" + prefix + ", destination=" + destination + + "]"; + } + } + + public static class Destination { + private String bucket; + private StorageClassEnum storageClass; + + /** + * Obtain the information about the destination bucket. + * + * @return Destination bucket name + */ + public String getBucket() { + return bucket; + } + + /** + * Set the destination bucket name. + * + * @param bucket + * Destination bucket name + */ + public void setBucket(String bucket) { + this.bucket = bucket; + } + + /** + * Obtain the storage class of the object generated after the + * replication. + * + * @return Object storage class + */ + public StorageClassEnum getObjectStorageClass() { + return storageClass; + } + + /** + * Set the storage class of the object generated after the replication. + * + * @param storageClass + * Object storage class + */ + public void setObjectStorageClass(StorageClassEnum storageClass) { + this.storageClass = storageClass; + } + + @Override + public String toString() { + return "Destination [bucket=" + bucket + ", storageClass=" + storageClass + "]"; + } + } + + /** + * Obtain the replication rule list. + * + * @return Replication rule list + */ + public List getRules() { + if (rules == null) { + rules = new ArrayList(); + } + return rules; + } + + /** + * Set the replication rule list. + * + * @param rules + * Replication rule list + */ + public void setRules(List rules) { + this.rules = rules; + } + + /** + * Set the agent name. + * + * @return Agent name + */ + public String getAgency() { + return agency; + } + + /** + * Obtain the agent name + * + * @param agency + * Agent name + */ + public void setAgency(String agency) { + this.agency = agency; + } + + @Override + public String toString() { + return "ReplicationConfiguration [agency=" + agency + ", rules=" + rules + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/RequestPaymentConfiguration.java b/app/src/main/java/com/obs/services/model/RequestPaymentConfiguration.java new file mode 100644 index 0000000..cdce8e1 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/RequestPaymentConfiguration.java @@ -0,0 +1,63 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Configuration of the requester-pays function of a bucket + * + * @since 3.20.3 + */ +public class RequestPaymentConfiguration extends HeaderResponse { + private RequestPaymentEnum payer; + + /** + * Constructor + * + * @param payer + * Status of the requester-pays function + */ + public RequestPaymentConfiguration(RequestPaymentEnum payer) { + this.payer = payer; + } + + public RequestPaymentConfiguration() { + + } + + /** + * Obtain the status of the requester-pays function. + * + * @return The status of the requester-pays function + */ + public RequestPaymentEnum getPayer() { + return payer; + } + + /** + * Configure the status of the requester-pays function. + * + * @param payer + * Status of the requester-pays function + */ + public void setPayer(RequestPaymentEnum payer) { + this.payer = payer; + } + + @Override + public String toString() { + return "RequestPaymentConfiguration [payer=" + payer.getCode() + "]"; + } + +} diff --git a/app/src/main/java/com/obs/services/model/RequestPaymentEnum.java b/app/src/main/java/com/obs/services/model/RequestPaymentEnum.java new file mode 100644 index 0000000..fab9e14 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/RequestPaymentEnum.java @@ -0,0 +1,53 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Status of the requester-pays function of a bucket + * + * @since 3.20.3 + */ +public enum RequestPaymentEnum { + + /** + * The bucket owner pays. + */ + BUCKET_OWNER("BucketOwner"), + + /** + * The requester pays. + */ + REQUESTER("Requester"); + + private String code; + + private RequestPaymentEnum(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + + public static RequestPaymentEnum getValueFromCode(String code) { + for (RequestPaymentEnum val : RequestPaymentEnum.values()) { + if (val.code.equals(code)) { + return val; + } + } + return null; + } + +} diff --git a/app/src/main/java/com/obs/services/model/RestoreObjectRequest.java b/app/src/main/java/com/obs/services/model/RestoreObjectRequest.java index 52d84fe..5fa5c44 100644 --- a/app/src/main/java/com/obs/services/model/RestoreObjectRequest.java +++ b/app/src/main/java/com/obs/services/model/RestoreObjectRequest.java @@ -3,267 +3,257 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** -* -* Parameters in a request for restoring an Archive object -* -*/ -public class RestoreObjectRequest -{ + * + * Parameters in a request for restoring an Archive object + * + */ +public class RestoreObjectRequest extends BaseObjectRequest { + + { + httpMethod = HttpMethodEnum.POST; + } + + private String versionId; + + /** + * Obtain the object version ID. + * + * @return Version ID of the object + */ + public String getVersionId() { + return versionId; + } + + /** + * Set the version ID of the object. + * + * @param versionId + * Version ID of the object + */ + public void setVersionId(String versionId) { + this.versionId = versionId; + } + /** * Expedited restoration, which restores an object in 1 to 5 minutes */ - @Deprecated + @Deprecated public static final String EXPEDITED = "Expedited"; - + /** * Standard restoration, which restores the object in 3 to 5 hours */ - @Deprecated + @Deprecated public static final String STANDARD = "Standard"; - + /** * Batch restoration, which restores objects in 5 to 12 hours */ - @Deprecated + @Deprecated public static final String BULK = "Bulk"; - + /** - * + * * Status of the Archive object * */ - public static class RestoreObjectStatus extends HeaderResponse - { - + public static class RestoreObjectStatus extends HeaderResponse { + private int code; /** - * The object has been restored and can be downloaded. + * The object has been restored and can be downloaded. */ public static final RestoreObjectStatus AVALIABLE = new RestoreObjectStatus(200); /** - * The object is being restored and cannot be downloaded. + * The object is being restored and cannot be downloaded. */ public static final RestoreObjectStatus INPROGRESS = new RestoreObjectStatus(202); - - private RestoreObjectStatus(int code) - { + + private RestoreObjectStatus(int code) { this.code = code; } - + /** * Obtain the status code of the object. + * * @return Status code of the object */ - public int getCode() - { + public int getCode() { return this.code; } - - public static RestoreObjectStatus valueOf(int retCode) - { + + public static RestoreObjectStatus valueOf(int retCode) { return retCode == 200 ? AVALIABLE : retCode == 202 ? INPROGRESS : new RestoreObjectStatus(retCode); } } - - private String bucketName; - - private String objectKey; - - private String versionId; - + private int days; - + private RestoreTierEnum tier; - - public RestoreObjectRequest() - { - + + public RestoreObjectRequest() { + } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name - * @param days Retention period of the restored object + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param days + * Retention period of the restored object */ - public RestoreObjectRequest(String bucketName, String objectKey, int days) - { + public RestoreObjectRequest(String bucketName, String objectKey, int days) { this.bucketName = bucketName; this.objectKey = objectKey; this.days = days; } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name - * @param versionId Version ID of the object - * @param days Retention period of the restored object + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param versionId + * Version ID of the object + * @param days + * Retention period of the restored object */ - public RestoreObjectRequest(String bucketName, String objectKey, String versionId, int days) - { - this(bucketName, objectKey, days); + public RestoreObjectRequest(String bucketName, String objectKey, String versionId, int days) { + this.bucketName = bucketName; + this.objectKey = objectKey; this.versionId = versionId; + this.days = days; } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name - * @param versionId Version ID of the object - * @param days Retention period of the restored object - * @param tier Restore option + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param versionId + * Version ID of the object + * @param days + * Retention period of the restored object + * @param tier + * Restore option */ @Deprecated - public RestoreObjectRequest(String bucketName, String objectKey, String versionId, int days, String tier) - { - this(bucketName, objectKey, versionId, days); + public RestoreObjectRequest(String bucketName, String objectKey, String versionId, int days, String tier) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.versionId = versionId; + this.days = days; this.tier = RestoreTierEnum.getValueFromCode(tier); } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name - * @param versionId Version ID of the object - * @param days Retention period of the restored object - * @param tier Restore option - */ - public RestoreObjectRequest(String bucketName, String objectKey, String versionId, int days, RestoreTierEnum tier) - { - this(bucketName, objectKey, versionId, days); - this.tier = tier; - } - - /** - * Obtain the bucket name. - * @return Bucket name - */ - public String getBucketName() - { - return bucketName; - } - - /** - * Set the bucket name. - * @param bucketName Bucket name + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param versionId + * Version ID of the object + * @param days + * Retention period of the restored object + * @param tier + * Restore option */ - public void setBucketName(String bucketName) - { + public RestoreObjectRequest(String bucketName, String objectKey, String versionId, int days, RestoreTierEnum tier) { this.bucketName = bucketName; - } - - /** - * Obtain the object name. - * - * @return Object name - */ - public String getObjectKey() - { - return objectKey; - } - - /** - * Set the object name. - * - * @param objectKey Object name - */ - public void setObjectKey(String objectKey) - { this.objectKey = objectKey; - } - - /** - * Obtain the object version ID. - * @return Version ID of the object - */ - public String getVersionId() - { - return versionId; - } - - /** - * Set the version ID of the object. - * @param versionId Version ID of the object - */ - public void setVersionId(String versionId) - { this.versionId = versionId; + this.days = days; + this.tier = tier; } - + /** - * Obtain the retention period of the restored object. The value ranges from 1 to 30 (in days). + * Obtain the retention period of the restored object. The value ranges from + * 1 to 30 (in days). + * * @return Retention period of the restored object */ - public int getDays() - { + public int getDays() { return days; } - + /** - * Set the retention period of the restored object. The value ranges from 1 to 30 (in days). - * @param days Retention period of the restored object + * Set the retention period of the restored object. The value ranges from 1 + * to 30 (in days). + * + * @param days + * Retention period of the restored object */ - public void setDays(int days) - { + public void setDays(int days) { this.days = days; } - + /** * Obtain the restore option. + * * @see #getRestoreTier() * @return Restore option */ @Deprecated - public String getTier() - { + public String getTier() { return this.tier != null ? this.tier.getCode() : null; } - + /** * Set the restore option. + * * @see #setRestoreTier(RestoreTierEnum tier) - * @param tier Restore option + * @param tier + * Restore option */ @Deprecated - public void setTier(String tier) - { + public void setTier(String tier) { this.tier = RestoreTierEnum.getValueFromCode(tier); } - + /** * Obtain the restore option. + * * @return Restore option */ - public RestoreTierEnum getRestoreTier() - { + public RestoreTierEnum getRestoreTier() { return tier; } - + /** * Set the restore option. - * @param tier Restore option + * + * @param tier + * Restore option */ - public void setRestoreTier(RestoreTierEnum tier) - { + public void setRestoreTier(RestoreTierEnum tier) { this.tier = tier; } @Override - public String toString() - { - return "RestoreObjectRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", versionId=" + versionId + ", days=" + days - + ", tier=" + tier + "]"; + public String toString() { + return "RestoreObjectRequest [days=" + days + ", tier=" + tier + ", getBucketName()=" + getBucketName() + + ", getObjectKey()=" + getObjectKey() + ", getVersionId()=" + getVersionId() + ", isRequesterPays()=" + + isRequesterPays() + "]"; } } diff --git a/app/src/main/java/com/obs/services/model/RestoreObjectResult.java b/app/src/main/java/com/obs/services/model/RestoreObjectResult.java index bc44818..9bae242 100644 --- a/app/src/main/java/com/obs/services/model/RestoreObjectResult.java +++ b/app/src/main/java/com/obs/services/model/RestoreObjectResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** diff --git a/app/src/main/java/com/obs/services/model/RestoreObjectsRequest.java b/app/src/main/java/com/obs/services/model/RestoreObjectsRequest.java index fc1d35d..a4d4437 100644 --- a/app/src/main/java/com/obs/services/model/RestoreObjectsRequest.java +++ b/app/src/main/java/com/obs/services/model/RestoreObjectsRequest.java @@ -11,25 +11,28 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.List; /** - * Request parameters of restoring Archive objects in a batch. - * The "prefix" and "keyAndVersions" parameters cannot be set in the same request. - * If both parameters are empty, all Archive objects in the bucket are restored. + * Request parameters of restoring Archive objects in a batch. The "prefix" and + * "keyAndVersions" parameters cannot be set in the same request. If both + * parameters are empty, all Archive objects in the bucket are restored. */ -public class RestoreObjectsRequest extends AbstractBulkRequest{ - +public class RestoreObjectsRequest extends AbstractBulkRequest { + private int days; private RestoreTierEnum tier; private String prefix; - + private boolean versionRestored; + private String encodingType; + private List keyAndVersions; private TaskCallback callback; @@ -41,28 +44,51 @@ public RestoreObjectsRequest() { /** * Constructor * - * @param bucketName Bucket name + * @param bucketName + * Bucket name */ public RestoreObjectsRequest(String bucketName) { - super(bucketName); + this.bucketName = bucketName; } /** * Constructor * - * @param bucketName Bucket name - * @param days Retention period of the restored objects - * @param tier Restore option + * @param bucketName + * Bucket name + * @param days + * Retention period of the restored objects + * @param tier + * Restore option */ public RestoreObjectsRequest(String bucketName, int days, RestoreTierEnum tier) { - super(bucketName); + this.bucketName = bucketName; + this.days = days; + this.tier = tier; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param days + * Retention period of the restored objects + * @param tier + * Restore option + * @param encodingType + * The encoding type use for encode objectKey. + */ + public RestoreObjectsRequest(String bucketName, int days, RestoreTierEnum tier, String encodingType) { + this.bucketName = bucketName; this.days = days; this.tier = tier; + this.encodingType = encodingType; } - /** - * Obtain the retention period of the restored objects. The value ranges from 1 to 30 (in days). + * Obtain the retention period of the restored objects. The value ranges + * from 1 to 30 (in days). * * @return Retention period of the restored objects */ @@ -71,9 +97,11 @@ public int getDays() { } /** - * Set the retention period of the restored objects. The value ranges from 1 to 30 (in days). + * Set the retention period of the restored objects. The value ranges from 1 + * to 30 (in days). * - * @param days Retention period of the restored objects + * @param days + * Retention period of the restored objects */ public void setDays(int days) { this.days = days; @@ -91,7 +119,8 @@ public RestoreTierEnum getRestoreTier() { /** * Set the restore option. * - * @param tier Restore option + * @param tier + * Restore option */ public void setRestoreTier(RestoreTierEnum tier) { this.tier = tier; @@ -100,7 +129,8 @@ public void setRestoreTier(RestoreTierEnum tier) { /** * Set the name prefix of the objects to be restored in a batch. * - * @param prefix Object name prefix + * @param prefix + * Object name prefix */ public void setPrefix(String prefix) { this.prefix = prefix; @@ -114,10 +144,11 @@ public void setPrefix(String prefix) { public String getPrefix() { return prefix; } - + /** - * Obtain whether to restore all versions of Archive objects. - * The default value is "false", indicating that only latest versions of Archive objects are restored. + * Obtain whether to restore all versions of Archive objects. The default + * value is "false", indicating that only latest versions of Archive objects + * are restored. * * @return Identifier of version restore */ @@ -128,7 +159,8 @@ public boolean isVersionRestored() { /** * Set whether to restore all versions of Archive objects. * - * @param versionRestored Identifier of version restore + * @param versionRestored + * Identifier of version restore */ public void setVersionRestored(boolean versionRestored) { this.versionRestored = versionRestored; @@ -137,7 +169,8 @@ public void setVersionRestored(boolean versionRestored) { /** * Set the list of objects to be restored. * - * @param keyAndVersions List of objects to be restored + * @param keyAndVersions + * List of objects to be restored */ public void setKeyAndVersions(List keyAndVersions) { this.keyAndVersions = keyAndVersions; @@ -155,8 +188,10 @@ public List getKeyAndVersions() { /** * Add an object to be restored. * - * @param objectKey Object name - * @param versionId Object version + * @param objectKey + * Object name + * @param versionId + * Object version * @return Object that has been added to be restored */ public KeyAndVersion addKeyAndVersion(String objectKey, String versionId) { @@ -168,7 +203,8 @@ public KeyAndVersion addKeyAndVersion(String objectKey, String versionId) { /** * Add an object to be restored. * - * @param objectKey Object name + * @param objectKey + * Object name * @return Object that has been added to be restored */ public KeyAndVersion addKeyAndVersion(String objectKey) { @@ -187,16 +223,36 @@ public TaskCallback getCallback() { /** * Set the callback object of a batch task. * - * @param callback Callback object + * @param callback + * Callback object */ public void setCallback(TaskCallback callback) { this.callback = callback; } + /** + * Set the encoding type that used for encode objectkey + * + * @param encodingType + * could chose url. + */ + public void setEncodingType(String encodingType) { + this.encodingType = encodingType; + } + + /** + * Obtain the list of to-be-deleted objects. + * + * @return List of to-be-deleted objects + */ + public String getEncodingType() { + return encodingType; + } @Override public String toString() { - return "RestoreObjectsRequest [bucketName=" + bucketName + ", days=" + days + ", tier=" + tier + "]"; + return "RestoreObjectsRequest [bucketName=" + bucketName + ", days=" + days + ", tier=" + tier + + ", encodingType=" + encodingType + "]"; } } diff --git a/app/src/main/java/com/obs/services/model/RestoreTierEnum.java b/app/src/main/java/com/obs/services/model/RestoreTierEnum.java index c50e2a0..ca7efb2 100644 --- a/app/src/main/java/com/obs/services/model/RestoreTierEnum.java +++ b/app/src/main/java/com/obs/services/model/RestoreTierEnum.java @@ -3,51 +3,52 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * Restoration option - * */ public enum RestoreTierEnum { - /** - * Expedited restoration, which restores an object in 1 to 5 minutes - */ - EXPEDITED("Expedited"), - /** - * Standard restoration, which restores the object in 3 to 5 hours - */ - STANDARD("Standard"), - /** - * Batch restoration, which restores objects in 5 to 12 hours - */ - @Deprecated - BULK("Bulk"); + /** + * Expedited restoration, which restores an object in 1 to 5 minutes + */ + EXPEDITED("Expedited"), + /** + * Standard restoration, which restores the object + * in 3 to 5 hours + */ + STANDARD("Standard"), + /** + * Batch restoration, which restores objects in 5 to + * 12 hours + */ + BULK("Bulk"); - private String code; + private String code; - private RestoreTierEnum(String code) { - this.code = code; - } + RestoreTierEnum(String code) { + this.code = code; + } - public String getCode() { - return code; - } + public String getCode() { + return code; + } - public static RestoreTierEnum getValueFromCode(String code) { - for (RestoreTierEnum val : RestoreTierEnum.values()) { - if (val.code.equals(code)) { - return val; - } - } - return null; - } + public static RestoreTierEnum getValueFromCode(String code) { + for (RestoreTierEnum val : RestoreTierEnum.values()) { + if (val.code.equals(code)) { + return val; + } + } + return null; + } } diff --git a/app/src/main/java/com/obs/services/model/RouteRule.java b/app/src/main/java/com/obs/services/model/RouteRule.java index c4e4843..797bf90 100644 --- a/app/src/main/java/com/obs/services/model/RouteRule.java +++ b/app/src/main/java/com/obs/services/model/RouteRule.java @@ -11,57 +11,58 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * Route rules for redirection */ -public class RouteRule -{ +public class RouteRule { private RouteRuleCondition condition; - + private Redirect redirect; - + /** * Obtain the redirection condition. - * @return Redirection condition + * + * @return Redirection condition */ - public RouteRuleCondition getCondition() - { + public RouteRuleCondition getCondition() { return condition; } - + /** * Set the redirection condition - * @param condition Redirection condition + * + * @param condition + * Redirection condition */ - public void setCondition(RouteRuleCondition condition) - { + public void setCondition(RouteRuleCondition condition) { this.condition = condition; } - + /** * Obtain the redirection configuration. - * @return Redirection configuration + * + * @return Redirection configuration */ - public Redirect getRedirect() - { + public Redirect getRedirect() { return redirect; } - + /** * Configure the redirection. - * @param redirect Redirection configuration + * + * @param redirect + * Redirection configuration */ - public void setRedirect(Redirect redirect) - { + public void setRedirect(Redirect redirect) { this.redirect = redirect; } @Override - public String toString() - { + public String toString() { return "RouteRule [condition=" + condition + ", redirect=" + redirect + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/RouteRuleCondition.java b/app/src/main/java/com/obs/services/model/RouteRuleCondition.java index 0b71fb9..c8181eb 100644 --- a/app/src/main/java/com/obs/services/model/RouteRuleCondition.java +++ b/app/src/main/java/com/obs/services/model/RouteRuleCondition.java @@ -11,58 +11,61 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** - * Redirection condition + * Redirection condition */ -public class RouteRuleCondition -{ +public class RouteRuleCondition { private String keyPrefixEquals; - + private String httpErrorCodeReturnedEquals; - + /** * Obtain the object name prefix when the redirection takes effect. + * * @return Object name prefix when the redirection takes effect */ - public String getKeyPrefixEquals() - { + public String getKeyPrefixEquals() { return keyPrefixEquals; } - + /** * Set the object name prefix when the redirection takes effect. - * @param keyPrefixEquals Object name prefix when the redirection takes effect + * + * @param keyPrefixEquals + * Object name prefix when the redirection takes effect */ - public void setKeyPrefixEquals(String keyPrefixEquals) - { + public void setKeyPrefixEquals(String keyPrefixEquals) { this.keyPrefixEquals = keyPrefixEquals; } - + /** - * Obtain the HTTP error code configuration when the redirection takes effect. + * Obtain the HTTP error code configuration when the redirection takes + * effect. + * * @return HTTP error code configuration when the redirection takes effect */ - public String getHttpErrorCodeReturnedEquals() - { + public String getHttpErrorCodeReturnedEquals() { return httpErrorCodeReturnedEquals; } - + /** * Configure the HTTP error code when the redirection takes effect. - * @param httpErrorCodeReturnedEquals HTTP error code configuration when the redirection takes effect. + * + * @param httpErrorCodeReturnedEquals + * HTTP error code configuration when the redirection takes + * effect. */ - public void setHttpErrorCodeReturnedEquals(String httpErrorCodeReturnedEquals) - { + public void setHttpErrorCodeReturnedEquals(String httpErrorCodeReturnedEquals) { this.httpErrorCodeReturnedEquals = httpErrorCodeReturnedEquals; } - + @Override - public String toString() - { - return "RouteRuleCondition [keyPrefixEquals=" + keyPrefixEquals + ", httpErrorCodeReturnedEquals=" + httpErrorCodeReturnedEquals - + "]"; + public String toString() { + return "RouteRuleCondition [keyPrefixEquals=" + keyPrefixEquals + ", httpErrorCodeReturnedEquals=" + + httpErrorCodeReturnedEquals + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/RuleStatusEnum.java b/app/src/main/java/com/obs/services/model/RuleStatusEnum.java index 75f44cb..0dd3c31 100644 --- a/app/src/main/java/com/obs/services/model/RuleStatusEnum.java +++ b/app/src/main/java/com/obs/services/model/RuleStatusEnum.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** @@ -19,33 +20,33 @@ * */ public enum RuleStatusEnum { - - /** - * Enable a rule. - */ - ENABLED("Enabled"), - - /** - * Disable a rule. - */ - DISABLED("Disabled"); - - private String code; - - private RuleStatusEnum(String code) { - this.code = code; - } - - public String getCode() { - return code; - } - - public static RuleStatusEnum getValueFromCode(String code) { - for (RuleStatusEnum val : RuleStatusEnum.values()) { - if (val.code.equals(code)) { - return val; - } - } - return null; - } + + /** + * Enable a rule. + */ + ENABLED("Enabled"), + + /** + * Disable a rule. + */ + DISABLED("Disabled"); + + private String code; + + private RuleStatusEnum(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + + public static RuleStatusEnum getValueFromCode(String code) { + for (RuleStatusEnum val : RuleStatusEnum.values()) { + if (val.code.equals(code)) { + return val; + } + } + return null; + } } diff --git a/app/src/main/java/com/obs/services/model/S3Bucket.java b/app/src/main/java/com/obs/services/model/S3Bucket.java index 7fc5b99..c66f3f4 100644 --- a/app/src/main/java/com/obs/services/model/S3Bucket.java +++ b/app/src/main/java/com/obs/services/model/S3Bucket.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.Date; @@ -19,132 +20,113 @@ import com.obs.services.internal.utils.ServiceUtils; -@Deprecated -public class S3Bucket extends HeaderResponse -{ - - @Deprecated + +public class S3Bucket extends HeaderResponse { + + @Deprecated public static final String STANDARD = "STANDARD"; - - @Deprecated + + @Deprecated public static final String STANDARD_IA = "STANDARD_IA"; - - @Deprecated + + @Deprecated public static final String GLACIER = "GLACIER"; - + protected String bucketName; - + protected Owner owner; - + protected Date creationDate; - + protected String location; - + protected StorageClassEnum storageClass; - + protected Map metadata = new HashMap(); - + protected AccessControlList acl; protected BucketTypeEnum bucketTypeEnum; - - - public S3Bucket(){ - + + public S3Bucket() { + } - - public S3Bucket(String bucketName, String location){ + + public S3Bucket(String bucketName, String location) { this.bucketName = bucketName; this.location = location; } - - - public String getBucketName() - { + + public String getBucketName() { return bucketName; } - - public void setBucketName(String bucketName) - { + + public void setBucketName(String bucketName) { this.bucketName = bucketName; } - - public Owner getOwner() - { + + public Owner getOwner() { return owner; } - - public void setOwner(Owner bucketOwner) - { + + public void setOwner(Owner bucketOwner) { this.owner = bucketOwner; } - - public Date getCreationDate() - { - return ServiceUtils.cloneDateIgnoreNull(this.creationDate); + + public Date getCreationDate() { + return ServiceUtils.cloneDateIgnoreNull(this.creationDate); } - - public void setCreationDate(Date bucketCreationDate) - { + + public void setCreationDate(Date bucketCreationDate) { this.creationDate = ServiceUtils.cloneDateIgnoreNull(bucketCreationDate); } - - public Map getMetadata() - { + + public Map getMetadata() { return metadata; } - - public void setMetadata(Map metadata) - { + + public void setMetadata(Map metadata) { this.metadata.putAll(metadata); } - - public String getLocation() - { + + public String getLocation() { return location; } - - public void setLocation(String location) - { + + public void setLocation(String location) { this.location = location; } - - public AccessControlList getAcl() - { + + public AccessControlList getAcl() { return acl; } - - public void setAcl(AccessControlList acl) - { + + public void setAcl(AccessControlList acl) { this.acl = acl; } @Deprecated - public String getStorageClass() - { + public String getStorageClass() { return this.storageClass != null ? this.storageClass.getCode() : null; } @Deprecated - public void setStorageClass(String storageClass) - { + public void setStorageClass(String storageClass) { this.storageClass = StorageClassEnum.getValueFromCode(storageClass); } - - public StorageClassEnum getBucketStorageClass() - { + + public StorageClassEnum getBucketStorageClass() { return storageClass; } - public void setBucketStorageClass(StorageClassEnum storageClass) - { + public void setBucketStorageClass(StorageClassEnum storageClass) { this.storageClass = storageClass; } @Override - public String toString() - { - return "ObsBucket [bucketName=" + bucketName + ", owner=" + owner + ", creationDate=" + creationDate + ", location=" + location - + ", storageClass=" + storageClass + ", metadata=" + metadata + ", acl=" + acl + "]"; + public String toString() { + return "ObsBucket [bucketName=" + bucketName + ", owner=" + owner + ", creationDate=" + creationDate + + ", location=" + location + ", storageClass=" + storageClass + ", metadata=" + metadata + ", acl=" + + acl + "]"; } } diff --git a/app/src/main/java/com/obs/services/model/S3BucketCors.java b/app/src/main/java/com/obs/services/model/S3BucketCors.java index 36f41e6..f78ba1b 100644 --- a/app/src/main/java/com/obs/services/model/S3BucketCors.java +++ b/app/src/main/java/com/obs/services/model/S3BucketCors.java @@ -11,45 +11,49 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.ArrayList; import java.util.List; -@Deprecated -public class S3BucketCors extends HeaderResponse -{ - + +public class S3BucketCors extends HeaderResponse { + private List rules; - - public S3BucketCors() - { + + public S3BucketCors() { } - - public S3BucketCors(List rules) - { + + public S3BucketCors(List rules) { this.rules = rules; } - - public List getRules() - { - if (null == rules) - { + + /** + * Obtain the bucket CORS rule list. + * + * @return Bucket CORS rule list + */ + public List getRules() { + if (null == rules) { rules = new ArrayList(); } return rules; } - - public void setRules(List rules) - { + + /** + * Configure the bucket CORS rule list. + * + * @param rules + * Bucket CORS rule list + */ + public void setRules(List rules) { this.rules = rules; } - - + @Override - public String toString() - { + public String toString() { return "ObsBucketCors [rules=" + rules + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/S3Object.java b/app/src/main/java/com/obs/services/model/S3Object.java index f3cbe80..94cfd36 100644 --- a/app/src/main/java/com/obs/services/model/S3Object.java +++ b/app/src/main/java/com/obs/services/model/S3Object.java @@ -11,89 +11,79 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.io.InputStream; @Deprecated -public class S3Object -{ - - @Deprecated +public class S3Object { + + @Deprecated public static final String STANDARD = "STANDARD"; - - @Deprecated + + @Deprecated public static final String STANDARD_IA = "STANDARD_IA"; - - @Deprecated + + @Deprecated public static final String GLACIER = "GLACIER"; - - protected String bucketName; - - protected String objectKey; - - protected Owner owner; - - protected ObjectMetadata metadata; - - protected InputStream objectContent; - - public String getBucketName() - { + + protected String bucketName; + + protected String objectKey; + + protected Owner owner; + + protected ObjectMetadata metadata; + + protected InputStream objectContent; + + public String getBucketName() { return bucketName; } - - public void setBucketName(String bucketName) - { + + public void setBucketName(String bucketName) { this.bucketName = bucketName; } - - public String getObjectKey() - { + + public String getObjectKey() { return objectKey; } - - public void setObjectKey(String objectKey) - { + + public void setObjectKey(String objectKey) { this.objectKey = objectKey; } - - public ObjectMetadata getMetadata() { - if (metadata == null) { - this.metadata = new ObjectMetadata(); - } - return metadata; - } - - public void setMetadata(ObjectMetadata metadata) - { + + public ObjectMetadata getMetadata() { + if (metadata == null) { + this.metadata = new ObjectMetadata(); + } + return metadata; + } + + public void setMetadata(ObjectMetadata metadata) { this.metadata = metadata; } - - public InputStream getObjectContent() - { + + public InputStream getObjectContent() { return objectContent; } - - public void setObjectContent(InputStream objectContent) - { + + public void setObjectContent(InputStream objectContent) { this.objectContent = objectContent; } - - public Owner getOwner() - { + + public Owner getOwner() { return owner; } - - public void setOwner(Owner owner) - { + + public void setOwner(Owner owner) { this.owner = owner; } @Override - public String toString() - { - return "ObsObject [bucketName=" + bucketName + ", objectKey=" + objectKey + ", owner=" + owner + ", metadata=" + metadata - + ", objectContent=" + objectContent + "]"; + public String toString() { + return "ObsObject [bucketName=" + bucketName + ", objectKey=" + objectKey + ", owner=" + owner + ", metadata=" + + metadata + ", objectContent=" + objectContent + "]"; } } diff --git a/app/src/main/java/com/obs/services/model/SSEAlgorithmEnum.java b/app/src/main/java/com/obs/services/model/SSEAlgorithmEnum.java index 104a584..4495077 100644 --- a/app/src/main/java/com/obs/services/model/SSEAlgorithmEnum.java +++ b/app/src/main/java/com/obs/services/model/SSEAlgorithmEnum.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** diff --git a/app/src/main/java/com/obs/services/model/ServerAlgorithm.java b/app/src/main/java/com/obs/services/model/ServerAlgorithm.java index 0bb6cf5..f575893 100644 --- a/app/src/main/java/com/obs/services/model/ServerAlgorithm.java +++ b/app/src/main/java/com/obs/services/model/ServerAlgorithm.java @@ -3,63 +3,55 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * SSE-C encryption algorithm */ -@Deprecated -public final class ServerAlgorithm -{ +public final class ServerAlgorithm { /** * SSE-C supports only AES256. */ public static final ServerAlgorithm AES256 = new ServerAlgorithm("AES256"); - + private String serverAlgorithm = ""; - - private ServerAlgorithm(String serverAlgorithm) - { + + private ServerAlgorithm(String serverAlgorithm) { this.serverAlgorithm = serverAlgorithm; } - - public String getServerAlgorithm() - { + + public String getServerAlgorithm() { return serverAlgorithm; } - - public static ServerAlgorithm parseServerAlgorithm(String str) - { + + public static ServerAlgorithm parseServerAlgorithm(String str) { ServerAlgorithm serverAlgorithm = null; - - if (null != str && str.equals(AES256.toString())) - { + + if (null != str && str.equals(AES256.toString())) { serverAlgorithm = AES256; } return serverAlgorithm; } - + @Override - public String toString() - { + public String toString() { return serverAlgorithm; } - - public boolean equals(Object obj) - { + + public boolean equals(Object obj) { return (obj instanceof ServerAlgorithm) && toString().equals(obj.toString()); } - - public int hashCode() - { + + public int hashCode() { return serverAlgorithm.hashCode(); } } diff --git a/app/src/main/java/com/obs/services/model/ServerEncryption.java b/app/src/main/java/com/obs/services/model/ServerEncryption.java index b3b9da5..878b642 100644 --- a/app/src/main/java/com/obs/services/model/ServerEncryption.java +++ b/app/src/main/java/com/obs/services/model/ServerEncryption.java @@ -11,55 +11,48 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * Encryption mode of SSE-KMS */ @Deprecated -public final class ServerEncryption -{ +public final class ServerEncryption { /** * SSE-KMS supports kms only. */ public static final ServerEncryption OBS_KMS = new ServerEncryption("kms"); - + private String serverEncryption = ""; - - private ServerEncryption(String serverEncryption) - { + + private ServerEncryption(String serverEncryption) { this.serverEncryption = serverEncryption; } - - public String getServerEncryption() - { + + public String getServerEncryption() { return serverEncryption; } - - public static ServerEncryption parseServerEncryption(String str) - { + + public static ServerEncryption parseServerEncryption(String str) { ServerEncryption serverEncryption = null; - - if (null != str && str.equals(OBS_KMS.toString())) - { + + if (null != str && str.equals(OBS_KMS.toString())) { serverEncryption = OBS_KMS; } return serverEncryption; } - + @Override - public String toString() - { + public String toString() { return serverEncryption; } - - public boolean equals(Object obj) - { + + public boolean equals(Object obj) { return (obj instanceof ServerEncryption) && toString().equals(obj.toString()); } - - public int hashCode() - { + + public int hashCode() { return serverEncryption.hashCode(); } } diff --git a/app/src/main/java/com/obs/services/model/SetBucketAclRequest.java b/app/src/main/java/com/obs/services/model/SetBucketAclRequest.java new file mode 100644 index 0000000..b652814 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketAclRequest.java @@ -0,0 +1,62 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Request for configuring bucket ACL. + * + * @since 3.20.3 + * + */ +public class SetBucketAclRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private AccessControlList acl; + private String cannedACL; + + public SetBucketAclRequest() { + + } + + public SetBucketAclRequest(String bucketName, AccessControlList acl) { + this.bucketName = bucketName; + this.acl = acl; + } + + public AccessControlList getAcl() { + return acl; + } + + public void setAcl(AccessControlList acl) { + this.acl = acl; + } + + public String getCannedACL() { + return cannedACL; + } + + public void setCannedACL(String cannedACL) { + this.cannedACL = cannedACL; + } + + @Override + public String toString() { + return "SetBucketAclRequest [acl=" + acl + ", cannedACL=" + cannedACL + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketCorsRequest.java b/app/src/main/java/com/obs/services/model/SetBucketCorsRequest.java new file mode 100644 index 0000000..bd528b2 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketCorsRequest.java @@ -0,0 +1,48 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Configure CORS for a bucket. + * + * @since 3.20.3 + */ +public class SetBucketCorsRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private BucketCors bucketCors; + + public SetBucketCorsRequest(String bucketName, BucketCors bucketCors) { + this.bucketName = bucketName; + this.bucketCors = bucketCors; + } + + public BucketCors getBucketCors() { + return bucketCors; + } + + public void setBucketCors(BucketCors bucketCors) { + this.bucketCors = bucketCors; + } + + @Override + public String toString() { + return "SetBucketCorsRequest [bucketCors=" + bucketCors + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketCustomDomainRequest.java b/app/src/main/java/com/obs/services/model/SetBucketCustomDomainRequest.java new file mode 100644 index 0000000..489540a --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketCustomDomainRequest.java @@ -0,0 +1,47 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.services.model; + +public class SetBucketCustomDomainRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private String domainName; + + public SetBucketCustomDomainRequest() { + + } + + public SetBucketCustomDomainRequest(String bucketName, String domainName) { + this.bucketName = bucketName; + this.domainName = domainName; + } + + public String getDomainName() { + return domainName; + } + + public void setDomainName(String domainName) { + this.domainName = domainName; + } + + @Override + public String toString() { + return "SetBucketCustomDomainRequest [domainName=" + domainName + ", bucketName=" + getBucketName() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketDirectColdAccessRequest.java b/app/src/main/java/com/obs/services/model/SetBucketDirectColdAccessRequest.java new file mode 100644 index 0000000..fb7b61c --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketDirectColdAccessRequest.java @@ -0,0 +1,48 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Configure the direct reading policy for Archive objects in a bucket. + * + * @since 3.20.3 + */ +public class SetBucketDirectColdAccessRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private BucketDirectColdAccess access; + + public SetBucketDirectColdAccessRequest(String bucketName, BucketDirectColdAccess access) { + this.bucketName = bucketName; + this.access = access; + } + + public BucketDirectColdAccess getAccess() { + return access; + } + + public void setAccess(BucketDirectColdAccess access) { + this.access = access; + } + + @Override + public String toString() { + return "SetBucketDirectColdAccessRequest [access=" + access + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketEncryptionRequest.java b/app/src/main/java/com/obs/services/model/SetBucketEncryptionRequest.java new file mode 100644 index 0000000..8179904 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketEncryptionRequest.java @@ -0,0 +1,48 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Configure encryption for a bucket. + * + * @since 3.20.3 + */ +public class SetBucketEncryptionRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private BucketEncryption bucketEncryption; + + public SetBucketEncryptionRequest(String bucketName, BucketEncryption bucketEncryption) { + this.bucketName = bucketName; + this.bucketEncryption = bucketEncryption; + } + + public BucketEncryption getBucketEncryption() { + return bucketEncryption; + } + + public void setBucketEncryption(BucketEncryption bucketEncryption) { + this.bucketEncryption = bucketEncryption; + } + + @Override + public String toString() { + return "SetBucketEncryptionRequest [bucketEncryption=" + bucketEncryption + ", getBucketName()=" + + getBucketName() + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketLifecycleRequest.java b/app/src/main/java/com/obs/services/model/SetBucketLifecycleRequest.java new file mode 100644 index 0000000..4d93222 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketLifecycleRequest.java @@ -0,0 +1,48 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Configure lifecycle rules for a bucket. + * + * @since 3.20.3 + */ +public class SetBucketLifecycleRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private LifecycleConfiguration lifecycleConfig; + + public SetBucketLifecycleRequest(String bucketName, LifecycleConfiguration lifecycleConfig) { + this.bucketName = bucketName; + this.lifecycleConfig = lifecycleConfig; + } + + public LifecycleConfiguration getLifecycleConfig() { + return lifecycleConfig; + } + + public void setLifecycleConfig(LifecycleConfiguration lifecycleConfig) { + this.lifecycleConfig = lifecycleConfig; + } + + @Override + public String toString() { + return "SetBucketLifecycleRequest [lifecycleConfig=" + lifecycleConfig + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketLoggingRequest.java b/app/src/main/java/com/obs/services/model/SetBucketLoggingRequest.java new file mode 100644 index 0000000..b9f0f4b --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketLoggingRequest.java @@ -0,0 +1,65 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Set logging for a bucket. + * + * @since 3.20.3 + */ +public class SetBucketLoggingRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private BucketLoggingConfiguration loggingConfiguration; + private boolean updateTargetACLifRequired = false; + + public SetBucketLoggingRequest(String bucketName, BucketLoggingConfiguration loggingConfiguration) { + this.bucketName = bucketName; + this.loggingConfiguration = loggingConfiguration; + } + + public SetBucketLoggingRequest(String bucketName, + BucketLoggingConfiguration loggingConfiguration, boolean updateTargetACLifRequired) { + this.bucketName = bucketName; + this.loggingConfiguration = loggingConfiguration; + this.updateTargetACLifRequired = updateTargetACLifRequired; + } + + public BucketLoggingConfiguration getLoggingConfiguration() { + return loggingConfiguration; + } + + public void setLoggingConfiguration(BucketLoggingConfiguration loggingConfiguration) { + this.loggingConfiguration = loggingConfiguration; + } + + public boolean isUpdateTargetACLifRequired() { + return updateTargetACLifRequired; + } + + public void setUpdateTargetACLifRequired(boolean updateTargetACLifRequired) { + this.updateTargetACLifRequired = updateTargetACLifRequired; + } + + @Override + public String toString() { + return "SetBucketLoggingRequest [loggingConfiguration=" + loggingConfiguration + ", updateTargetACLifRequired=" + + updateTargetACLifRequired + ", getBucketName()=" + getBucketName() + ", isRequesterPays()=" + + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketNotificationRequest.java b/app/src/main/java/com/obs/services/model/SetBucketNotificationRequest.java new file mode 100644 index 0000000..6d8ab97 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketNotificationRequest.java @@ -0,0 +1,49 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Set event notification for a bucket. + * + * @since 3.20.3 + */ +public class SetBucketNotificationRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private BucketNotificationConfiguration bucketNotificationConfiguration; + + public SetBucketNotificationRequest(String bucketName, + BucketNotificationConfiguration bucketNotificationConfiguration) { + this.bucketName = bucketName; + this.bucketNotificationConfiguration = bucketNotificationConfiguration; + } + + public BucketNotificationConfiguration getBucketNotificationConfiguration() { + return bucketNotificationConfiguration; + } + + public void setBucketNotificationConfiguration(BucketNotificationConfiguration bucketNotificationConfiguration) { + this.bucketNotificationConfiguration = bucketNotificationConfiguration; + } + + @Override + public String toString() { + return "SetBucketNotificationRequest [bucketNotificationConfiguration=" + bucketNotificationConfiguration + + ", getBucketName()=" + getBucketName() + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketPolicyRequest.java b/app/src/main/java/com/obs/services/model/SetBucketPolicyRequest.java new file mode 100644 index 0000000..1c9b36e --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketPolicyRequest.java @@ -0,0 +1,56 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * + * Request parameters for setting a bucket policy + * + * @since 3.20.3 + */ +public class SetBucketPolicyRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private String policy; + + public SetBucketPolicyRequest() { + } + + public SetBucketPolicyRequest(String bucketName) { + this.bucketName = bucketName; + } + + public SetBucketPolicyRequest(String bucketName, String policy) { + this.bucketName = bucketName; + this.policy = policy; + } + + public String getPolicy() { + return policy; + } + + public void setPolicy(String policy) { + this.policy = policy; + } + + @Override + public String toString() { + return "SetBucketPolicyRequest [policy=" + policy + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketQuotaRequest.java b/app/src/main/java/com/obs/services/model/SetBucketQuotaRequest.java new file mode 100644 index 0000000..0f1821e --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketQuotaRequest.java @@ -0,0 +1,56 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Set the bucket quota. + * + * @since 3.20.3 + */ +public class SetBucketQuotaRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private BucketQuota bucketQuota; + + public SetBucketQuotaRequest() { + } + + public SetBucketQuotaRequest(String bucketName) { + this.bucketName = bucketName; + } + + public SetBucketQuotaRequest(String bucketName, BucketQuota bucketQuota) { + this.bucketName = bucketName; + this.bucketQuota = bucketQuota; + } + + public BucketQuota getBucketQuota() { + return bucketQuota; + } + + public void setBucketQuota(BucketQuota bucketQuota) { + this.bucketQuota = bucketQuota; + } + + @Override + public String toString() { + return "SetBucketQuotaRequest [bucketQuota=" + bucketQuota + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } + +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketReplicationRequest.java b/app/src/main/java/com/obs/services/model/SetBucketReplicationRequest.java new file mode 100644 index 0000000..a30eb5c --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketReplicationRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Configure cross-region replication for a bucket. + * @since 3.20.3 + * + */ +public class SetBucketReplicationRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private ReplicationConfiguration replicationConfiguration; + + public SetBucketReplicationRequest() { + } + + public SetBucketReplicationRequest(String bucketName) { + this.bucketName = bucketName; + } + + public SetBucketReplicationRequest(String bucketName, ReplicationConfiguration replicationConfiguration) { + this.bucketName = bucketName; + this.replicationConfiguration = replicationConfiguration; + } + + public ReplicationConfiguration getReplicationConfiguration() { + return replicationConfiguration; + } + + public void setReplicationConfiguration(ReplicationConfiguration replicationConfiguration) { + this.replicationConfiguration = replicationConfiguration; + } + + @Override + public String toString() { + return "SetBucketReplicationRequest [replicationConfiguration=" + replicationConfiguration + + ", getBucketName()=" + getBucketName() + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketRequestPaymentRequest.java b/app/src/main/java/com/obs/services/model/SetBucketRequestPaymentRequest.java new file mode 100644 index 0000000..e447991 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketRequestPaymentRequest.java @@ -0,0 +1,66 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Request for setting the requester-pays function for a bucket + * + * @since 3.20.3 + */ +public class SetBucketRequestPaymentRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private RequestPaymentEnum payer; + + public SetBucketRequestPaymentRequest() { + } + + public SetBucketRequestPaymentRequest(String bucketName) { + this.bucketName = bucketName; + } + + public SetBucketRequestPaymentRequest(String bucketName, RequestPaymentEnum payer) { + this.bucketName = bucketName; + this.payer = payer; + } + + /** + * Obtain the status of the requester-pays function. + * + * @return The status of the requester-pays function + */ + public RequestPaymentEnum getPayer() { + return payer; + } + + /** + * Set the status of the requester-pays function. + * + * @param payer + * Status of the requester-pays function + */ + public void setPayer(RequestPaymentEnum payer) { + this.payer = payer; + } + + @Override + public String toString() { + return "SetBucketRequestPaymentRequest [payer=" + payer + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketStoragePolicyRequest.java b/app/src/main/java/com/obs/services/model/SetBucketStoragePolicyRequest.java new file mode 100644 index 0000000..34210a8 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketStoragePolicyRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Set a bucket policy. + * + * @since 3.20.3 + */ +public class SetBucketStoragePolicyRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private BucketStoragePolicyConfiguration bucketStorage; + + public SetBucketStoragePolicyRequest() { + } + + public SetBucketStoragePolicyRequest(String bucketName) { + this.bucketName = bucketName; + } + + public SetBucketStoragePolicyRequest(String bucketName, BucketStoragePolicyConfiguration bucketStorage) { + this.bucketName = bucketName; + this.bucketStorage = bucketStorage; + } + + public BucketStoragePolicyConfiguration getBucketStorage() { + return bucketStorage; + } + + public void setBucketStorage(BucketStoragePolicyConfiguration bucketStorage) { + this.bucketStorage = bucketStorage; + } + + @Override + public String toString() { + return "SetBucketStoragePolicyRequest [bucketStorage=" + bucketStorage + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketTaggingRequest.java b/app/src/main/java/com/obs/services/model/SetBucketTaggingRequest.java new file mode 100644 index 0000000..e4aabe1 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketTaggingRequest.java @@ -0,0 +1,52 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Request for setting a tag for a bucket + * + * @since 3.20.3 + * + */ +public class SetBucketTaggingRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private BucketTagInfo bucketTagInfo; + + public SetBucketTaggingRequest() { + } + + public SetBucketTaggingRequest(String bucketName, BucketTagInfo bucketTagInfo) { + this.bucketName = bucketName; + this.bucketTagInfo = bucketTagInfo; + } + + public BucketTagInfo getBucketTagInfo() { + return bucketTagInfo; + } + + public void setBucketTagInfo(BucketTagInfo bucketTagInfo) { + this.bucketTagInfo = bucketTagInfo; + } + + @Override + public String toString() { + return "SetBucketTaggingRequest [bucketTagInfo=" + bucketTagInfo + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketVersioningRequest.java b/app/src/main/java/com/obs/services/model/SetBucketVersioningRequest.java new file mode 100644 index 0000000..76c7956 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketVersioningRequest.java @@ -0,0 +1,52 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Request for setting the versioning status of a bucket + * + * @since 3.20.3 + * + */ +public class SetBucketVersioningRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private VersioningStatusEnum status; + + public SetBucketVersioningRequest() { + } + + public SetBucketVersioningRequest(String bucketName, VersioningStatusEnum status) { + this.bucketName = bucketName; + this.status = status; + } + + public VersioningStatusEnum getStatus() { + return status; + } + + public void setStatus(VersioningStatusEnum status) { + this.status = status; + } + + @Override + public String toString() { + return "SetBucketVersioningRequest [status=" + status + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetBucketWebsiteRequest.java b/app/src/main/java/com/obs/services/model/SetBucketWebsiteRequest.java new file mode 100644 index 0000000..fc53e50 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetBucketWebsiteRequest.java @@ -0,0 +1,49 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Configure the static website hosting of a bucket. + * + * @since 3.20.3 + * + */ +public class SetBucketWebsiteRequest extends BaseBucketRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private WebsiteConfiguration websiteConfig; + + public SetBucketWebsiteRequest(String bucketName, WebsiteConfiguration websiteConfig) { + this.bucketName = bucketName; + this.websiteConfig = websiteConfig; + } + + public WebsiteConfiguration getWebsiteConfig() { + return websiteConfig; + } + + public void setWebsiteConfig(WebsiteConfiguration websiteConfig) { + this.websiteConfig = websiteConfig; + } + + @Override + public String toString() { + return "SetBucketWebsiteRequest [websiteConfig=" + websiteConfig + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetObjectAclRequest.java b/app/src/main/java/com/obs/services/model/SetObjectAclRequest.java new file mode 100644 index 0000000..c1edc10 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/SetObjectAclRequest.java @@ -0,0 +1,88 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model; + +/** + * Bucket or object access control list (ACL) + * + * @since 3.20.3 + */ +public class SetObjectAclRequest extends BaseObjectRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + private AccessControlList acl; + + private String cannedACL; + + private String versionId; + + public SetObjectAclRequest(String bucketName, String objectKey, AccessControlList acl) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.acl = acl; + } + + public SetObjectAclRequest(String bucketName, String objectKey, AccessControlList acl, String versionId) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.versionId = versionId; + this.acl = acl; + } + + public AccessControlList getAcl() { + return acl; + } + + public void setAcl(AccessControlList acl) { + this.acl = acl; + } + + public String getCannedACL() { + return cannedACL; + } + + public void setCannedACL(String cannedACL) { + this.cannedACL = cannedACL; + } + + /** + * Obtain the object version ID. + * + * @return Version ID of the object + */ + public String getVersionId() { + return versionId; + } + + /** + * Set the version ID of the object. + * + * @param versionId + * Version ID of the object + */ + public void setVersionId(String versionId) { + this.versionId = versionId; + } + + @Override + public String toString() { + return "SetObjectAclRequest [acl=" + acl + ", cannedACL=" + cannedACL + ", getBucketName()=" + getBucketName() + + ", getObjectKey()=" + getObjectKey() + ", getVersionId()=" + getVersionId() + ", isRequesterPays()=" + + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SetObjectMetadataRequest.java b/app/src/main/java/com/obs/services/model/SetObjectMetadataRequest.java index 6d520f4..d122f3c 100644 --- a/app/src/main/java/com/obs/services/model/SetObjectMetadataRequest.java +++ b/app/src/main/java/com/obs/services/model/SetObjectMetadataRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.HashMap; @@ -19,316 +20,330 @@ /** * Parameters in a request for setting object properties */ -public class SetObjectMetadataRequest{ - - private String bucketName; - - private String objectKey; - +public class SetObjectMetadataRequest extends BaseObjectRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + private String versionId; - private StorageClassEnum storageClass; - + private StorageClassEnum storageClass; + private String webSiteRedirectLocation; - + private boolean removeUnset; - - private Map metadata; - - private ObjectRepleaceMetadata replaceMetadata = new ObjectRepleaceMetadata(); - + + private Map userMetadata; + + private boolean encodeHeaders = true; + + private final ObjectRepleaceMetadata replaceMetadata = new ObjectRepleaceMetadata(); + public SetObjectMetadataRequest() { } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name */ - public SetObjectMetadataRequest(String bucketName, String objectKey) - { + public SetObjectMetadataRequest(String bucketName, String objectKey) { this.bucketName = bucketName; this.objectKey = objectKey; } /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name - * @param versionId Version ID of the object + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param versionId + * Version ID of the object */ - public SetObjectMetadataRequest(String bucketName, String objectKey, String versionId) - { + public SetObjectMetadataRequest(String bucketName, String objectKey, String versionId) { this.bucketName = bucketName; this.objectKey = objectKey; this.versionId = versionId; } - - - /** - * Obtain the bucket name. - * @return Bucket name - */ - public String getBucketName() - { - return bucketName; - } - - /** - * Set the bucket name. - * @param bucketName Bucket name - */ - public void setBucketName(String bucketName) - { - this.bucketName = bucketName; - } - - /** - * Obtain the object name. - * @return Object name - */ - public String getObjectKey() - { - return objectKey; - } - - /** - * Set the object name. - * @param objectKey Object name - */ - public void setObjectKey(String objectKey) - { - this.objectKey = objectKey; - } - /** * Obtain the object version ID. + * * @return Version ID of the object */ - public String getVersionId() - { + public String getVersionId() { return versionId; } - + /** - * Set the version ID of the object. - * @param versionId Version ID of the object + * Set the version ID of the object. + * + * @param versionId + * Version ID of the object */ - public void setVersionId(String versionId) - { + public void setVersionId(String versionId) { this.versionId = versionId; } - + /** - * Set the redirection link which can redirect the request to another object in the bucket or to an external URL. + * Set the redirection link which can redirect the request to another object + * in the bucket or to an external URL. * * @return Redirection link */ - public String getWebSiteRedirectLocation() - { + public String getWebSiteRedirectLocation() { return webSiteRedirectLocation; } - + /** - * Obtain the redirection link which can redirect the request to another object in the bucket or to an external URL. + * Obtain the redirection link which can redirect the request to another + * object in the bucket or to an external URL. * - * @param webSiteRedirectLocation Redirection link + * @param webSiteRedirectLocation + * Redirection link */ - public void setWebSiteRedirectLocation(String webSiteRedirectLocation) - { + public void setWebSiteRedirectLocation(String webSiteRedirectLocation) { this.webSiteRedirectLocation = webSiteRedirectLocation; } - + /** - * Obtain the object storage class. + * Obtain the object storage class. + * * @return Object storage class */ - public StorageClassEnum getObjectStorageClass() - { + public StorageClassEnum getObjectStorageClass() { return storageClass; } - + /** - * Set the object storage class. - * @param storageClass Object storage class + * Set the object storage class. + * + * @param storageClass + * Object storage class */ - public void setObjectStorageClass(StorageClassEnum storageClass) - { + public void setObjectStorageClass(StorageClassEnum storageClass) { this.storageClass = storageClass; } /** * Specify whether to delete not specified properties. The default value is "false." - * true: Use properties set in the request parameters to overwrite the existing property values. Properties not specified in the request will be deleted. - * false: Use properties set in the request parameters to overwrite the existing property values. For properties not specified in the request, their existing values are retained. + * true: Use properties set in the request parameters to overwrite + * the existing property values. Properties not specified in the request + * will be deleted. + * false: Use properties set in the request parameters to + * overwrite the existing property values. For properties not specified in + * the request, their existing values are retained. + * * @return Identifier specifying whether to delete not specified properties */ - public boolean isRemoveUnset() { - return removeUnset; - } - - /** - * Specify whether to delete not specified properties. - * @param removeUnset Identifier specifying whether to delete not specified properties - */ - public void setRemoveUnset(boolean removeUnset) { - this.removeUnset = removeUnset; - } - + public boolean isRemoveUnset() { + return removeUnset; + } + + /** + * Specify whether to delete not specified properties. + * + * @param removeUnset + * Identifier specifying whether to delete not specified + * properties + */ + public void setRemoveUnset(boolean removeUnset) { + this.removeUnset = removeUnset; + } + /** * Add customized metadata for an object. - * @param key Keyword of the customized metadata - * @param value Value of the customized metadata + * + * @param key + * Keyword of the customized metadata + * @param value + * Value of the customized metadata */ - public void addUserMetadata(String key, String value){ - getMetadata().put(key, value); + public void addUserMetadata(String key, String value) { + getAllUserMetadata().put(key, value); } - + /** * Add customized metadata for a group of objects. - * @param userMetadata Customized metadata for a group of objects + * + * @param userMetadata + * Customized metadata for a group of objects */ public void addAllUserMetadata(Map userMetadata) { - if(userMetadata != null) { - getMetadata().putAll(userMetadata); - } + if (userMetadata != null) { + getAllUserMetadata().putAll(userMetadata); + } } - + /** * Obtain the customized metadata of an object. - * @param key Keyword of the customized metadata + * + * @param key + * Keyword of the customized metadata * @return Value of the customized metadata */ - public Object getUserMetadata(String key){ - return getMetadata().get(key); + public Object getUserMetadata(String key) { + return getAllUserMetadata().get(key); } - + /** * Obtain the rewritten "Content-Type" header in the response. + * * @return "Content-Type" header in the response */ - public String getContentType() - { + public String getContentType() { return replaceMetadata.getContentType(); } - + /** * Rewrite the "Content-Type" header in the response. - * @param contentType "Content-Type" header in the response + * + * @param contentType + * "Content-Type" header in the response */ - public void setContentType(String contentType) - { - replaceMetadata.setContentType(contentType); + public void setContentType(String contentType) { + replaceMetadata.setContentType(contentType); } - + /** * Obtain the rewritten "Content-Language" header in the response. + * * @return "Content-Language" header in the response */ - public String getContentLanguage() - { + public String getContentLanguage() { return replaceMetadata.getContentLanguage(); } - + /** * Rewrite the "Content-Language" header in the response. - * @param contentLanguage "Content-Language" header in the response + * + * @param contentLanguage + * "Content-Language" header in the response */ - public void setContentLanguage(String contentLanguage) - { - replaceMetadata.setContentLanguage(contentLanguage); + public void setContentLanguage(String contentLanguage) { + replaceMetadata.setContentLanguage(contentLanguage); } - + /** * Obtain the rewritten "Expires" header in the response. + * * @return "Expires" header in the response */ - public String getExpires() - { + public String getExpires() { return replaceMetadata.getExpires(); } - + /** * Rewrite the "Expires" header in the response. - * @param expires Rewritten "Expires" header in the response + * + * @param expires + * Rewritten "Expires" header in the response */ - public void setExpires(String expires) - { - replaceMetadata.setExpires(expires); + public void setExpires(String expires) { + replaceMetadata.setExpires(expires); } - + /** * Obtain the rewritten "Cache-Control" header in the response. + * * @return "Cache-Control" header in the response */ - public String getCacheControl() - { + public String getCacheControl() { return replaceMetadata.getCacheControl(); } - + /** * Rewrite the "Cache-Control" header in the response. - * @param cacheControl "Cache-Control" header in the response + * + * @param cacheControl + * "Cache-Control" header in the response */ - public void setCacheControl(String cacheControl) - { - replaceMetadata.setCacheControl(cacheControl); + public void setCacheControl(String cacheControl) { + replaceMetadata.setCacheControl(cacheControl); } - + /** * Obtain the rewritten "Content-Disposition" header in the response. + * * @return "Content-Disposition" header in the response */ - public String getContentDisposition() - { + public String getContentDisposition() { return replaceMetadata.getContentDisposition(); } - + /** * Rewrite the "Content-Disposition" header in the response. - * @param contentDisposition "Content-Disposition" header in the response + * + * @param contentDisposition + * "Content-Disposition" header in the response */ - public void setContentDisposition(String contentDisposition) - { - replaceMetadata.setContentDisposition(contentDisposition); + public void setContentDisposition(String contentDisposition) { + replaceMetadata.setContentDisposition(contentDisposition); } - + /** * Obtain the rewritten "Content-Encoding" header in the response. + * * @return "Content-Encoding" header in the response */ - public String getContentEncoding() - { + public String getContentEncoding() { return replaceMetadata.getContentEncoding(); } - + /** * Rewrite the "Content-Encoding" header in the response. - * @param contentEncoding "Content-Encoding" header in the response + * + * @param contentEncoding + * "Content-Encoding" header in the response */ - public void setContentEncoding(String contentEncoding) - { - replaceMetadata.setContentEncoding(contentEncoding); + public void setContentEncoding(String contentEncoding) { + replaceMetadata.setContentEncoding(contentEncoding); } - - public Map getMetadata() - { - if(metadata == null) { - metadata = new HashMap(); - } - return this.metadata; + + public Map getAllUserMetadata() { + if (userMetadata == null) { + userMetadata = new HashMap(); + } + return this.userMetadata; } - - - @Override - public String toString() { - return "SetObjectMetadataRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", versionId=" - + versionId + ", storageClass=" + storageClass + ", webSiteRedirectLocation=" + webSiteRedirectLocation - + ", removeUnset=" + removeUnset + ", metadata=" + metadata + ", replaceMetadata=" + replaceMetadata - + "]"; - } -} + @Deprecated + public Map getMetadata() { + return getAllUserMetadata(); + } + + /** + * Specifies whether to encode and decode the returned header fields. + * + * @param encodeHeaders + * Specifies whether to encode and decode header fields. + */ + public void setIsEncodeHeaders(boolean encodeHeaders) { + this.encodeHeaders = encodeHeaders; + } + /** + * Specifies whether to encode and decode the returned header fields. + * + * @return Specifies whether to encode and decode header fields. + */ + public boolean isEncodeHeaders() { + return encodeHeaders; + } + @Override + public String toString() { + return "SetObjectMetadataRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", versionId=" + + versionId + ", storageClass=" + storageClass + ", webSiteRedirectLocation=" + webSiteRedirectLocation + + ", removeUnset=" + removeUnset + ", userMetadata=" + userMetadata + + ", replaceMetadata=" + replaceMetadata + + ", isEncodeHeaders=" + encodeHeaders + "]"; + } + +} diff --git a/app/src/main/java/com/obs/services/model/SpecialParamEnum.java b/app/src/main/java/com/obs/services/model/SpecialParamEnum.java index 7cb2d22..6015610 100644 --- a/app/src/main/java/com/obs/services/model/SpecialParamEnum.java +++ b/app/src/main/java/com/obs/services/model/SpecialParamEnum.java @@ -11,13 +11,13 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * Special operator, which indicates the sub-resource to be operated */ -public enum SpecialParamEnum -{ +public enum SpecialParamEnum { /** * Obtain the bucket location information. @@ -55,10 +55,13 @@ public enum SpecialParamEnum * Obtain or set the versioning status of a bucket. */ VERSIONING("versioning"), + /** + * Obtain or set requester payment status of a bucket. + */ + REQUEST_PAYMENT("requestPayment"), /** * Obtain or set bucket storage policies. */ - @Deprecated STORAGEPOLICY("storagePolicy"), /** * Obtain or set the bucket storage class. @@ -95,8 +98,8 @@ public enum SpecialParamEnum * Configure bucket notification or obtain bucket notification configuration. */ NOTIFICATION("notification"), - - /** + + /** * Set, obtain, or delete the cross-region replication configuration of a bucket. */ REPLICATION("replication"), @@ -119,63 +122,75 @@ public enum SpecialParamEnum /** * Modify a file. */ - MODIFY("modify"), - - /** - * Configure the file gateway feature. - */ - FILEINTERFACE("fileinterface"), - - /** - * Set or delete object properties. - */ - METADATA("metadata"), - - /** - * Set, obtain, or delete the encryption configuration of a bucket. - */ - ENCRYPTION("encryption"), - - /** - * Set, obtain, or delete the direct reading policy for Archive objects in a bucket. - */ - DIRECTCOLDACCESS("directcoldaccess"); + MODIFY("modify"), + + /** + * Configure the file gateway feature. + */ + FILEINTERFACE("fileinterface"), + + /** + * Set or delete object properties. + */ + METADATA("metadata"), + + /** + * Set, obtain, or delete the encryption configuration of a bucket. + */ + ENCRYPTION("encryption"), + + /** + * obtain folder contentSummary + */ + LISTCONTENTSUMMARY("listcontentsummary"), + + /** + * obtain folder contentSummary of fs + */ + LISTCONTENTSUMMARYFS("multilistcontentsummary"), + + /** + * get content summary of fs + */ + GETCONTENTSUMMARY("getcontentsummary"), + + /** + * Set, obtain, or delete the direct reading policy for Archive objects in a bucket. + */ + DIRECTCOLDACCESS("directcoldaccess"), + + /** + * Set, obtain, or delete the Custom Domain Name of a Bucket + */ + CUSTOMDOMAIN("customdomain"); /** * Specify the corresponding code in the database and the external code. */ private String stringCode; - private SpecialParamEnum(String stringCode) - { - if (stringCode == null) - { + private SpecialParamEnum(String stringCode) { + if (stringCode == null) { throw new IllegalArgumentException("stringCode is null"); } this.stringCode = stringCode; } - public String getStringCode() - { + public String getStringCode() { return this.stringCode.toLowerCase(); } - public String getOriginalStringCode() - { + public String getOriginalStringCode() { return this.stringCode; } - public static SpecialParamEnum getValueFromStringCode(String stringCode) - { - if (stringCode == null) - { + public static SpecialParamEnum getValueFromStringCode(String stringCode) { + if (stringCode == null) { throw new IllegalArgumentException("string code is null"); } - for (SpecialParamEnum installMode : SpecialParamEnum.values()) - { - if (installMode.getStringCode().equals(stringCode.toLowerCase())) - { + for (SpecialParamEnum installMode : SpecialParamEnum.values()) { + if (installMode.getStringCode().equals(stringCode.toLowerCase())) { return installMode; } } diff --git a/app/src/main/java/com/obs/services/model/SseCHeader.java b/app/src/main/java/com/obs/services/model/SseCHeader.java index 3c7f6bc..c77e375 100644 --- a/app/src/main/java/com/obs/services/model/SseCHeader.java +++ b/app/src/main/java/com/obs/services/model/SseCHeader.java @@ -11,120 +11,128 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.Arrays; -import com.obs.services.internal.Constants; - /** * SSE-C encryption/decryption headers */ -public class SseCHeader -{ - +public class SseCHeader { + private ServerAlgorithm algorithm; - + private SSEAlgorithmEnum sseAlgorithm = SSEAlgorithmEnum.AES256; - + private byte[] sseCKey; - + private String sseCKeyBase64; - + /** - * Obtain the encryption algorithm type. Only AES256 is supported. This parameter must be used together with "sseCKey." + * Obtain the encryption algorithm type. Only AES256 is supported. This + * parameter must be used together with "sseCKey." * * @return Encryption algorithm type */ @Deprecated - public ServerAlgorithm getAlgorithm() - { + public ServerAlgorithm getAlgorithm() { return algorithm; } - + /** - * Set the encryption algorithm type. Only AES256 is supported. This parameter must be used together with "sseCKey." + * Set the encryption algorithm type. Only AES256 is supported. This + * parameter must be used together with "sseCKey." * - * @param algorithm Encryption algorithm type + * @param algorithm + * Encryption algorithm type */ @Deprecated - public void setAlgorithm(ServerAlgorithm algorithm) - { + public void setAlgorithm(ServerAlgorithm algorithm) { this.algorithm = algorithm; } - - + /** - * Obtain the encryption algorithm type. Only AES256 is supported. This parameter must be used together with "sseCKey". + * Obtain the encryption algorithm type. Only AES256 is supported. This + * parameter must be used together with "sseCKey". * * @return Encryption algorithm type */ public SSEAlgorithmEnum getSSEAlgorithm() { return sseAlgorithm; } - + /** - * Obtain the key used in the SSE-C mode. The key is used to encrypt and decrypt an object. The value is not encoded using Base64. + * Obtain the key used in the SSE-C mode. The key is used to encrypt and + * decrypt an object. The value is not encoded using Base64. * * @return Key used in the SSE-C mode */ - public byte[] getSseCKey() - { - return this.sseCKey; + public byte[] getSseCKey() { + if (null != this.sseCKey) { + return this.sseCKey.clone(); + } + return new byte[0]; } - + /** - * Set the key used in the SSE-C mode. The key is used to encrypt and decrypt an object. The value is not encoded using Base64. + * Set the key used in the SSE-C mode. The key is used to encrypt and + * decrypt an object. The value is not encoded using Base64. * - * @param sseCKey Key used in the SSE-C mode. The key is used to encrypt and decrypt an object. + * @param sseCKey + * Key used in the SSE-C mode. The key is used to encrypt and + * decrypt an object. */ @Deprecated - public void setSseCKey(String sseCKey) - { - if(sseCKey != null) { - try { - this.sseCKey = sseCKey.getBytes(Constants.ISO_8859_1_ENCOING); - } catch (UnsupportedEncodingException e) { - throw new IllegalStateException("fail to read sseCkey", e); - } - } + public void setSseCKey(String sseCKey) { + if (sseCKey != null) { + this.sseCKey = sseCKey.getBytes(StandardCharsets.ISO_8859_1); + } } - + /** - * Set the key used in the SSE-C mode. The key is used to encrypt and decrypt an object. The value is not encoded using Base64. + * Set the key used in the SSE-C mode. The key is used to encrypt and + * decrypt an object. The value is not encoded using Base64. * - * @param sseCKey Key used in the SSE-C mode. The key is used to encrypt and decrypt an object. + * @param sseCKey + * Key used in the SSE-C mode. The key is used to encrypt and + * decrypt an object. */ - public void setSseCKey(byte[] sseCKey) - { - this.sseCKey = sseCKey; + public void setSseCKey(byte[] sseCKey) { + if (null != sseCKey) { + this.sseCKey = sseCKey.clone(); + } else { + this.sseCKey = null; + } } - + /** - * Obtain the key used in the SSE-C mode. The key is used to encrypt and decrypt an object. The value is a Base64-encoded value. + * Obtain the key used in the SSE-C mode. The key is used to encrypt and + * decrypt an object. The value is a Base64-encoded value. * * @return Key used in the SSE-C mode */ - public String getSseCKeyBase64() { - return sseCKeyBase64; - } + public String getSseCKeyBase64() { + return sseCKeyBase64; + } - /** - * Set the key used in the SSE-C mode. The key is used to encrypt and decrypt an object. The value is a Base64-encoded value. + /** + * Set the key used in the SSE-C mode. The key is used to encrypt and + * decrypt an object. The value is a Base64-encoded value. * - * @param sseCKeyBase64 Key used in the SSE-C mode. The key is used to encrypt and decrypt an object. + * @param sseCKeyBase64 + * Key used in the SSE-C mode. The key is used to encrypt and + * decrypt an object. */ - public void setSseCKeyBase64(String sseCKeyBase64) { - this.sseCKeyBase64 = sseCKeyBase64; - } - - @Override - public String toString() { - return "SseCHeader [algorithm=" + algorithm + ", sseCKey=" + Arrays.toString(sseCKey) + ", sseCKeyBase64=" + sseCKeyBase64 + "]"; - } - - -} + public void setSseCKeyBase64(String sseCKeyBase64) { + this.sseCKeyBase64 = sseCKeyBase64; + } + @Override + public String toString() { + return "SseCHeader [algorithm=" + algorithm + ", sseCKey=" + Arrays.toString(sseCKey) + ", sseCKeyBase64=" + + sseCKeyBase64 + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/SseKmsHeader.java b/app/src/main/java/com/obs/services/model/SseKmsHeader.java index a2fe906..d287c0b 100644 --- a/app/src/main/java/com/obs/services/model/SseKmsHeader.java +++ b/app/src/main/java/com/obs/services/model/SseKmsHeader.java @@ -11,62 +11,64 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * SSE-KMS encryption headers */ -public class SseKmsHeader -{ +public class SseKmsHeader { @SuppressWarnings("deprecation") - private ServerEncryption encryption; - + private ServerEncryption encryption; + private SSEAlgorithmEnum sseAlgorithm = SSEAlgorithmEnum.KMS; - + private String kmsKeyId; - + private String context; - + private String projectId; - + /** * Obtain the project ID. + * * @return projectId Project ID */ public String getProjectId() { - return projectId; - } + return projectId; + } /** * Set the project ID. - * @param projectId Project ID + * + * @param projectId + * Project ID */ - public void setProjectId(String projectId) { - this.projectId = projectId; - } - + public void setProjectId(String projectId) { + this.projectId = projectId; + } + /** * Obtain the encryption algorithm type. Only kms is supported. * * @return Encryption algorithm type */ @Deprecated - public ServerEncryption getEncryption() - { + public ServerEncryption getEncryption() { return encryption; } - + /** * Set the encryption algorithm type. Only kms is supported. * - * @param encryption Encryption algorithm type + * @param encryption + * Encryption algorithm type */ @Deprecated - public void setEncryption(ServerEncryption encryption) - { + public void setEncryption(ServerEncryption encryption) { this.encryption = encryption; } - + /** * Obtain the encryption algorithm type. Only KMS is supported. * @@ -75,43 +77,41 @@ public void setEncryption(ServerEncryption encryption) public SSEAlgorithmEnum getSSEAlgorithm() { return sseAlgorithm; } - + /** - * Obtain the master key used in the SSE-KMS mode. If the value is blank, the default master key will be used. + * Obtain the master key used in the SSE-KMS mode. If the value is blank, + * the default master key will be used. * * @return Master key used in the SSE-KMS mode */ - public String getKmsKeyId() - { + public String getKmsKeyId() { return kmsKeyId; } - + /** - * Set the master key used in the SSE-KMS mode. If the value is blank, the default master key will be used. + * Set the master key used in the SSE-KMS mode. If the value is blank, the + * default master key will be used. * - * @param kmsKeyId Master key used in the SSE-KMS mode + * @param kmsKeyId + * Master key used in the SSE-KMS mode */ - public void setKmsKeyId(String kmsKeyId) - { + public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; } - + @Deprecated - public String getContext() - { + public String getContext() { return context; } - + @Deprecated - public void setContext(String context) - { + public void setContext(String context) { this.context = context; } @Override - public String toString() - { + public String toString() { return "SseKmsHeader [encryption=" + encryption + ", kmsKeyId=" + kmsKeyId + ", context=" + context + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/StorageClassEnum.java b/app/src/main/java/com/obs/services/model/StorageClassEnum.java index 1980681..33b0e19 100644 --- a/app/src/main/java/com/obs/services/model/StorageClassEnum.java +++ b/app/src/main/java/com/obs/services/model/StorageClassEnum.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** @@ -19,37 +20,34 @@ * */ public enum StorageClassEnum { - - /** - * Standard - */ - STANDARD, - - /** - * Infrequent Access - */ - WARM, - - /** - * Archive - */ - COLD; + /** + * Standard + */ + STANDARD, - public String getCode() { - return this.name(); - } + /** + * Infrequent Access + */ + WARM, - public static StorageClassEnum getValueFromCode(String code) { - if("STANDARD".equals(code)) { - return STANDARD; - }else if("WARM".equals(code) || "STANDARD_IA".equals(code)) { - return WARM; - }else if("COLD".equals(code) || "GLACIER".equals(code)) { - return COLD; - } - return null; - } -} + /** + * Archive + */ + COLD; + public String getCode() { + return this.name(); + } + public static StorageClassEnum getValueFromCode(String code) { + if ("STANDARD".equals(code)) { + return STANDARD; + } else if ("WARM".equals(code) || "STANDARD_IA".equals(code)) { + return WARM; + } else if ("COLD".equals(code) || "GLACIER".equals(code)) { + return COLD; + } + return null; + } +} diff --git a/app/src/main/java/com/obs/services/model/TaskCallback.java b/app/src/main/java/com/obs/services/model/TaskCallback.java index 616abaa..ac4082a 100644 --- a/app/src/main/java/com/obs/services/model/TaskCallback.java +++ b/app/src/main/java/com/obs/services/model/TaskCallback.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import com.obs.services.exception.ObsException; @@ -23,15 +24,19 @@ public interface TaskCallback { /** * Callback when the task is executed successfully. * - * @param result Callback parameter. Generally, the return type of a specific operation is used. + * @param result + * Callback parameter. Generally, the return type of a specific + * operation is used. */ void onSuccess(K result); /** * Callback when an exception is thrown during task execution. * - * @param exception Exception information - * @param singleRequest The request that causes an exception + * @param exception + * Exception information + * @param singleRequest + * The request that causes an exception * */ void onException(ObsException exception, V singleRequest); diff --git a/app/src/main/java/com/obs/services/model/TaskProgressListener.java b/app/src/main/java/com/obs/services/model/TaskProgressListener.java index 983c321..e4db9bc 100644 --- a/app/src/main/java/com/obs/services/model/TaskProgressListener.java +++ b/app/src/main/java/com/obs/services/model/TaskProgressListener.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; public interface TaskProgressListener { diff --git a/app/src/main/java/com/obs/services/model/TaskProgressStatus.java b/app/src/main/java/com/obs/services/model/TaskProgressStatus.java index eb192f3..c0631ae 100644 --- a/app/src/main/java/com/obs/services/model/TaskProgressStatus.java +++ b/app/src/main/java/com/obs/services/model/TaskProgressStatus.java @@ -11,37 +11,43 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; public interface TaskProgressStatus { - - /** - * Obtain the upload progress in percentage. - * @return Upload progress in percentage - */ + + /** + * Obtain the upload progress in percentage. + * + * @return Upload progress in percentage + */ public int getExecPercentage(); - + /** - * Obtain the number of objects being uploaded. - * @return Number of objects being uploaded - */ + * Obtain the number of objects being uploaded. + * + * @return Number of objects being uploaded + */ public int getExecTaskNum(); - + /** - * Obtain the number of objects that have been successfully uploaded. - * @return Number of objects that have been successfully uploaded - */ + * Obtain the number of objects that have been successfully uploaded. + * + * @return Number of objects that have been successfully uploaded + */ public int getSucceedTaskNum(); - + /** - * Obtain the number of objects that fail to be uploaded. - * @return Number of objects that fail to be uploaded - */ + * Obtain the number of objects that fail to be uploaded. + * + * @return Number of objects that fail to be uploaded + */ public int getFailTaskNum(); - + /** - * Obtain the total number of objects in the upload task. - * @return Total number of objects in the upload task - */ + * Obtain the total number of objects in the upload task. + * + * @return Total number of objects in the upload task + */ public int getTotalTaskNum(); } diff --git a/app/src/main/java/com/obs/services/model/TemporarySignatureRequest.java b/app/src/main/java/com/obs/services/model/TemporarySignatureRequest.java index 9539177..070078f 100644 --- a/app/src/main/java/com/obs/services/model/TemporarySignatureRequest.java +++ b/app/src/main/java/com/obs/services/model/TemporarySignatureRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import com.obs.services.internal.ObsConstraint; @@ -33,8 +34,11 @@ public TemporarySignatureRequest() { /** * Constructor - * @param method HTTP/HTTPS request method - * @param expires Expiration time (in seconds) + * + * @param method + * HTTP/HTTPS request method + * @param expires + * Expiration time (in seconds) */ public TemporarySignatureRequest(HttpMethodEnum method, long expires) { this(method, null, null, null, expires); @@ -42,11 +46,17 @@ public TemporarySignatureRequest(HttpMethodEnum method, long expires) { /** * Constructor - * @param method HTTP/HTTPS request method - * @param bucketName Bucket name - * @param objectKey Object name - * @param specialParam Special operator - * @param expires Expiration time (in seconds) + * + * @param method + * HTTP/HTTPS request method + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param specialParam + * Special operator + * @param expires + * Expiration time (in seconds) */ public TemporarySignatureRequest(HttpMethodEnum method, String bucketName, String objectKey, SpecialParamEnum specialParam, long expires) { @@ -55,12 +65,19 @@ public TemporarySignatureRequest(HttpMethodEnum method, String bucketName, Strin /** * Constructor - * @param method HTTP/HTTPS request method - * @param bucketName Bucket name - * @param objectKey Object name - * @param specialParam Special operator - * @param expires Expiration time (in seconds) - * @param requestDate Request date + * + * @param method + * HTTP/HTTPS request method + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param specialParam + * Special operator + * @param expires + * Expiration time (in seconds) + * @param requestDate + * Request date */ public TemporarySignatureRequest(HttpMethodEnum method, String bucketName, String objectKey, SpecialParamEnum specialParam, long expires, Date requestDate) { @@ -73,15 +90,23 @@ public TemporarySignatureRequest(HttpMethodEnum method, String bucketName, Strin } /** - * Obtain the validity period of the temporary authorization (in seconds). The devalue value is 5 minutes (value "300") and the maximum value is 7 days ("604800"). + * Obtain the validity period of the temporary authorization (in seconds). + * The devalue value is 5 minutes (value "300") and the maximum value is 7 + * days ("604800"). + * * @return Validity period - */ public long getExpires() { + */ + public long getExpires() { return expires; } /** - * Obtain the validity period of the temporary authorization (in seconds). The devalue value is 5 minutes (value "300") and the maximum value is 7 days ("604800"). - * @param expires Validity period + * Obtain the validity period of the temporary authorization (in seconds). + * The devalue value is 5 minutes (value "300") and the maximum value is 7 + * days ("604800"). + * + * @param expires + * Validity period */ public void setExpires(long expires) { this.expires = expires; @@ -89,22 +114,25 @@ public void setExpires(long expires) { /** * Set the request time. + * * @return Request time */ public Date getRequestDate() { - return ServiceUtils.cloneDateIgnoreNull(requestDate); + return ServiceUtils.cloneDateIgnoreNull(requestDate); } - /** + /** * Set the request time. - * @param requestDate Request date + * + * @param requestDate + * Request date */ public void setRequestDate(Date requestDate) { - if(null != requestDate) { - this.requestDate = (Date) requestDate.clone(); - } else { - this.requestDate = null; - } + if (null != requestDate) { + this.requestDate = (Date) requestDate.clone(); + } else { + this.requestDate = null; + } } @Override diff --git a/app/src/main/java/com/obs/services/model/TemporarySignatureResponse.java b/app/src/main/java/com/obs/services/model/TemporarySignatureResponse.java index 897a50b..d74abfa 100644 --- a/app/src/main/java/com/obs/services/model/TemporarySignatureResponse.java +++ b/app/src/main/java/com/obs/services/model/TemporarySignatureResponse.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.HashMap; @@ -21,44 +22,40 @@ * Response to a request for temporarily authorized access * */ -public class TemporarySignatureResponse -{ +public class TemporarySignatureResponse { private String signedUrl; - + private Map actualSignedRequestHeaders; - - public TemporarySignatureResponse(String signedUrl) - { + + public TemporarySignatureResponse(String signedUrl) { this.signedUrl = signedUrl; } - + /** * Obtain the URL of the temporarily authorized access. + * * @return URL of the temporarily authorized access */ - public String getSignedUrl() - { + public String getSignedUrl() { return signedUrl; } - + /** * Obtain the request headers. + * * @return Request headers */ - public Map getActualSignedRequestHeaders() - { - if (actualSignedRequestHeaders == null) - { + public Map getActualSignedRequestHeaders() { + if (actualSignedRequestHeaders == null) { this.actualSignedRequestHeaders = new HashMap(); } return actualSignedRequestHeaders; } @Override - public String toString() - { - return "TemporarySignatureResponse [signedUrl=" + signedUrl + ", actualSignedRequestHeaders=" + actualSignedRequestHeaders + "]"; + public String toString() { + return "TemporarySignatureResponse [signedUrl=" + signedUrl + ", actualSignedRequestHeaders=" + + actualSignedRequestHeaders + "]"; } - - + } diff --git a/app/src/main/java/com/obs/services/model/TopicConfiguration.java b/app/src/main/java/com/obs/services/model/TopicConfiguration.java index c274b61..37bcd41 100644 --- a/app/src/main/java/com/obs/services/model/TopicConfiguration.java +++ b/app/src/main/java/com/obs/services/model/TopicConfiguration.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.List; @@ -19,54 +20,56 @@ * Event notification configuration * */ -public class TopicConfiguration extends AbstractNotification -{ - - public TopicConfiguration(){ - +public class TopicConfiguration extends AbstractNotification { + + public TopicConfiguration() { + } - + /** * Constructor - * @param id Event notification configuration ID - * @param filter Filtering rule group - * @param topic URN of the event notification topic - * @param events List of the event types that require the release of notification messages + * + * @param id + * Event notification configuration ID + * @param filter + * Filtering rule group + * @param topic + * URN of the event notification topic + * @param events + * List of the event types that require the release of + * notification messages */ - public TopicConfiguration(String id, Filter filter, String topic, List events) - { + public TopicConfiguration(String id, Filter filter, String topic, List events) { super(id, filter, events); this.topic = topic; - + } - + private String topic; - /** * Obtain the URN of the event notification topic. + * * @return URN of the event notification topic - + * */ - public String getTopic() - { + public String getTopic() { return topic; } /** - * Set the URN of the event notification topic. - * @param topic URN of the event notification topic + * Set the URN of the event notification topic. + * + * @param topic + * URN of the event notification topic */ - public void setTopic(String topic) - { + public void setTopic(String topic) { this.topic = topic; } - @Override - public String toString() - { + public String toString() { return "TopicConfiguration [id=" + id + ", topic=" + topic + ", events=" + events + ", filter=" + filter + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/TruncateObjectRequest.java b/app/src/main/java/com/obs/services/model/TruncateObjectRequest.java index 10525ad..85455bc 100644 --- a/app/src/main/java/com/obs/services/model/TruncateObjectRequest.java +++ b/app/src/main/java/com/obs/services/model/TruncateObjectRequest.java @@ -11,89 +11,57 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** - * 截断文件的请求参数 - * + * Request parameters for truncating a file + * + * @since 3.20.3 */ -public class TruncateObjectRequest { - - private String bucketName; - - private String objectKey; - - private long newLength; - - public TruncateObjectRequest() { - - } +public class TruncateObjectRequest extends BaseObjectRequest { - /** - * 构造函数 - * @param bucketName 桶名 - * @param objectKey 文件名 - * @param newLength 文件截断后的大小 - */ - public TruncateObjectRequest(String bucketName, String objectKey, long newLength) { - super(); - this.bucketName = bucketName; - this.objectKey = objectKey; - this.newLength = newLength; - } + { + httpMethod = HttpMethodEnum.PUT; + } - /** - * 获取桶名 - * - * @return 桶名 - */ - public String getBucketName() { - return bucketName; - } + private long newLength; - /** - * 设置桶名 - * - * @param bucketName - * 桶名 - */ - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } + public TruncateObjectRequest() { + } - /** - * 获取文件名 - * - * @return 文件名 - */ - public String getObjectKey() { - return objectKey; - } + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param newLength + * File size after the truncation + */ + public TruncateObjectRequest(String bucketName, String objectKey, long newLength) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.newLength = newLength; + } - /** - * 设置文件名 - * - * @param objectKey - * 文件名 - * - */ - public void setObjectKey(String objectKey) { - this.objectKey = objectKey; - } - - /** - * 获取文件截断后的大小 - * @return 文件截断后的大小 - */ - public long getNewLength() { - return newLength; - } + /** + * Obtain the file size after the truncation. + * + * @return File size after the truncation + */ + public long getNewLength() { + return newLength; + } - /** - * 设置文件截断后的大小 - * @param newLength 文件截断后的大小 - */ - public void setNewLength(long newLength) { - this.newLength = newLength; - } + /** + * Set the post-truncation file size. + * + * @param newLength + * File size after the truncation + */ + public void setNewLength(long newLength) { + this.newLength = newLength; + } } diff --git a/app/src/main/java/com/obs/services/model/TruncateObjectResult.java b/app/src/main/java/com/obs/services/model/TruncateObjectResult.java index ca0223f..ff61cd4 100644 --- a/app/src/main/java/com/obs/services/model/TruncateObjectResult.java +++ b/app/src/main/java/com/obs/services/model/TruncateObjectResult.java @@ -11,10 +11,11 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** - * 截断文件的响应结果 + * Response to the request for truncating a file * */ public class TruncateObjectResult extends HeaderResponse{ diff --git a/app/src/main/java/com/obs/services/model/UploadFileRequest.java b/app/src/main/java/com/obs/services/model/UploadFileRequest.java index 10c80ef..d3d81e2 100644 --- a/app/src/main/java/com/obs/services/model/UploadFileRequest.java +++ b/app/src/main/java/com/obs/services/model/UploadFileRequest.java @@ -11,314 +11,430 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; +import com.obs.services.internal.Constants; import com.obs.services.internal.ObsConstraint; /** * Parameters in a file upload request */ public class UploadFileRequest extends PutObjectBasicRequest { - + // Part size, in bytes. The default value is 9 MB. - private long partSize = 1024 * 1024 * 9l; + private long partSize = 1024 * 1024 * 9L; // Number of threads for uploading parts. The default value is 1. private int taskNum = 1; - + private String uploadFile; - + private boolean enableCheckpoint = false; - + private String checkpointFile; - + private ObjectMetadata objectMetadata; - + private boolean enableCheckSum = false; - + private ProgressListener progressListener; - + + private String encodingType; + + private Callback callback; + private long progressInterval = ObsConstraint.DEFAULT_PROGRESS_INTERVAL; - + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + */ + public UploadFileRequest(String bucketName, String objectKey) { + this.bucketName = bucketName; + this.objectKey = objectKey; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param uploadFile + * To-be-uploaded local file + */ + public UploadFileRequest(String bucketName, String objectKey, String uploadFile) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.uploadFile = uploadFile; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param uploadFile + * To-be-uploaded local file + * @param encodingType + * Encoding type used for encoding objectKey + */ + public UploadFileRequest(String bucketName, String objectKey, String uploadFile, String encodingType) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.uploadFile = uploadFile; + this.encodingType = encodingType; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param uploadFile + * To-be-uploaded local file + * @param partSize + * Part size + */ + public UploadFileRequest(String bucketName, String objectKey, String uploadFile, long partSize) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.uploadFile = uploadFile; + this.partSize = partSize; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param uploadFile + * To-be-uploaded local file + * @param partSize + * Part size + * @param taskNum + * Maximum number of threads used for processing upload tasks + * concurrently + */ + public UploadFileRequest(String bucketName, String objectKey, String uploadFile, long partSize, int taskNum) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.uploadFile = uploadFile; + this.partSize = partSize; + this.taskNum = taskNum; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param uploadFile + * To-be-uploaded local file + * @param partSize + * Part size + * @param taskNum + * Maximum number of threads used for processing upload tasks + * concurrently + * @param enableCheckpoint + * Whether to enable the resumable mode + */ + public UploadFileRequest(String bucketName, String objectKey, String uploadFile, long partSize, int taskNum, + boolean enableCheckpoint) { + this(bucketName, objectKey, uploadFile, partSize, taskNum, enableCheckpoint, null); + + } + /** * Constructor - * @param bucketName Bucket name - * @param objectKey Object name + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param uploadFile + * To-be-uploaded local file + * @param partSize + * Part size + * @param taskNum + * Maximum number of threads used for processing upload tasks + * concurrently + * @param enableCheckpoint + * Whether to enable the resumable mode + * @param checkpointFile + * File used to record resumable upload progresses + */ + public UploadFileRequest(String bucketName, String objectKey, String uploadFile, long partSize, int taskNum, + boolean enableCheckpoint, String checkpointFile) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.partSize = partSize; + this.taskNum = taskNum; + this.uploadFile = uploadFile; + this.enableCheckpoint = enableCheckpoint; + this.checkpointFile = checkpointFile; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param uploadFile + * To-be-uploaded local file + * @param partSize + * Part size + * @param taskNum + * Maximum number of threads used for processing upload tasks + * concurrently + * @param enableCheckpoint + * Whether to enable the resumable mode + * @param checkpointFile + * File used to record resumable upload progresses + * @param enableCheckSum + * Whether to verify the to-be-uploaded file upon non-initial + * uploads in resumable upload mode + */ + public UploadFileRequest(String bucketName, String objectKey, String uploadFile, long partSize, int taskNum, + boolean enableCheckpoint, String checkpointFile, boolean enableCheckSum) { + this(bucketName, objectKey, uploadFile, partSize, taskNum, enableCheckpoint, checkpointFile); + this.enableCheckSum = enableCheckSum; + } + + /** + * Obtain the part size set for uploading the object. + * + * @return Part size + */ + public long getPartSize() { + return partSize; + } + + /** + * Set the part size for uploading the object. + * + * @param partSize + * Part size */ - public UploadFileRequest(String bucketName, String objectKey) { - this.bucketName = bucketName; - this.objectKey = objectKey; - } - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - * @param uploadFile To-be-uploaded local file - */ - public UploadFileRequest(String bucketName, String objectKey, String uploadFile) { - this(bucketName, objectKey); - this.uploadFile = uploadFile; - } - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - * @param uploadFile To-be-uploaded local file - * @param partSize Part size - */ - public UploadFileRequest(String bucketName, String objectKey, String uploadFile, long partSize) { - this(bucketName, objectKey, uploadFile); - this.partSize = partSize; - } - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - * @param uploadFile To-be-uploaded local file - * @param partSize Part size - * @param taskNum Maximum number of threads used for processing upload tasks concurrently - */ - public UploadFileRequest(String bucketName, String objectKey, String uploadFile, long partSize,int taskNum) { - this(bucketName, objectKey, uploadFile, partSize); - this.taskNum = taskNum; - } - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - * @param uploadFile To-be-uploaded local file - * @param partSize Part size - * @param taskNum Maximum number of threads used for processing upload tasks concurrently - * @param enableCheckpoint Whether to enable the resumable mode - */ - public UploadFileRequest(String bucketName, String objectKey, String uploadFile, long partSize, int taskNum, - boolean enableCheckpoint) { - this(bucketName, objectKey, uploadFile, partSize, taskNum, enableCheckpoint, null); - - } - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - * @param uploadFile To-be-uploaded local file - * @param partSize Part size - * @param taskNum Maximum number of threads used for processing upload tasks concurrently - * @param enableCheckpoint Whether to enable the resumable mode - * @param checkpointFile File used to record resumable upload progresses - */ - public UploadFileRequest(String bucketName, String objectKey, String uploadFile, long partSize, int taskNum, - boolean enableCheckpoint, String checkpointFile) { - this(bucketName, objectKey); - this.partSize = partSize; - this.taskNum = taskNum; - this.uploadFile = uploadFile; - this.enableCheckpoint = enableCheckpoint; - this.checkpointFile = checkpointFile; - } - - /** - * Constructor - * - * @param bucketName Bucket name - * @param objectKey Object name - * @param uploadFile To-be-uploaded local file - * @param partSize Part size - * @param taskNum Maximum number of threads used for processing upload tasks concurrently - * @param enableCheckpoint Whether to enable the resumable mode - * @param checkpointFile File used to record resumable upload progresses - * @param enableCheckSum Whether to verify the to-be-uploaded file upon non-initial uploads in resumable upload mode - */ - public UploadFileRequest(String bucketName, String objectKey, String uploadFile, long partSize, int taskNum, - boolean enableCheckpoint, String checkpointFile, boolean enableCheckSum) { - this(bucketName, objectKey,uploadFile,partSize,taskNum,enableCheckpoint,checkpointFile); - this.enableCheckSum = enableCheckSum; - } - - /** - * Obtain the part size set for uploading the object. - * - * @return Part size - */ - public long getPartSize() { - return partSize; - } - - /** - * Set the part size for uploading the object. - * - * @param partSize Part size - */ - public void setPartSize(long partSize) { - if (partSize < 100 * 1024l) { - this.partSize = 100 * 1024l; - } else if (partSize > 5 * 1024 * 1024 * 1024l) { - this.partSize = 5 * 1024 * 1024 * 1024l; - }else { - this.partSize = partSize; + public void setPartSize(long partSize) { + if (partSize < Constants.MIN_PART_SIZE) { + this.partSize = Constants.MIN_PART_SIZE; + } else { + this.partSize = Math.min(partSize, Constants.MAX_PART_SIZE); } - } - - /** - * Obtain the maximum number of threads used for processing upload tasks concurrently. - * - * @return Maximum number of threads used for processing upload tasks concurrently - */ - public int getTaskNum() { - return taskNum; - } - - /** - * Set the maximum number of threads used for executing upload tasks concurrently. - * - * @param taskNum Maximum number of threads used for processing upload tasks concurrently - */ - public void setTaskNum(int taskNum) { - if (taskNum < 1) { - this.taskNum = 1; - } else if (taskNum > 1000) { - this.taskNum = 1000; - } else { - this.taskNum = taskNum; - } - } - - /** - * Obtain the to-be-uploaded local file. - * - * @return To-be-uploaded local file - */ - public String getUploadFile() { - return uploadFile; - } - - /** - * Specify the local file to be uploaded. - * - * @param uploadFile To-be-uploaded local file - */ - public void setUploadFile(String uploadFile) { - this.uploadFile = uploadFile; - } - - /** - * Identify whether the resumable mode is enabled. - * - * @return Identifier specifying whether the resumable mode is enabled - */ - public boolean isEnableCheckpoint() { - return enableCheckpoint; - } - - /** - * Specify whether to enable the resumable mode. - * - * @param enableCheckpoint Identifier specifying whether the resumable mode is enabled - */ - public void setEnableCheckpoint(boolean enableCheckpoint) { - this.enableCheckpoint = enableCheckpoint; - } - - /** - * Obtain the file used to record resumable upload progresses. - * - * @return File used to record upload progresses - */ - public String getCheckpointFile() { - return checkpointFile; - } - - /** - * Specify a file used to record resumable upload progresses. - * - * @param checkpointFile File used to record upload progresses - */ - public void setCheckpointFile(String checkpointFile) { - this.checkpointFile = checkpointFile; - } - - /** - * Obtain object properties. - * - * @return Object properties - */ - public ObjectMetadata getObjectMetadata() { - return objectMetadata; - } - - /** - * Set object properties. - * - * @param objectMetadata Object properties - */ - public void setObjectMetadata(ObjectMetadata objectMetadata) { - this.objectMetadata = objectMetadata; - } - - /** - * Identify whether the file to be uploaded in resumable mode will be verified. - * - * @return Identifier specifying whether to verify the to-be-uploaded file - */ - public boolean isEnableCheckSum() { - return enableCheckSum; - } - - /** - * Specify whether to verify the file to be uploaded in resumable mode. - * - * @param enableCheckSum Identifier specifying whether to verify the to-be-uploaded file - */ - public void setEnableCheckSum(boolean enableCheckSum) { - this.enableCheckSum = enableCheckSum; - } - - /** - * Obtain the data transfer listener. - * @return Data transfer listener - */ - public ProgressListener getProgressListener() { - return progressListener; - } - - /** - * Set the data transfer listener. - * @param progressListener Data transfer listener - */ - public void setProgressListener(ProgressListener progressListener) { - this.progressListener = progressListener; - } - - /** - * Obtain the callback threshold of the data transfer listener. The default value is 100 KB. - * @return Callback threshold of the data transfer listener - */ - public long getProgressInterval() { - return progressInterval; - } - - /** - * Set the callback threshold of the data transfer listener. The default value is 100 KB. - * @param progressInterval Callback threshold of the data transfer listener - */ - public void setProgressInterval(long progressInterval) { - this.progressInterval = progressInterval; - } + } + + /** + * Obtain the maximum number of threads used for processing upload tasks + * concurrently. + * + * @return Maximum number of threads used for processing upload tasks + * concurrently + */ + public int getTaskNum() { + return taskNum; + } + + /** + * Set the maximum number of threads used for executing upload tasks + * concurrently. + * + * @param taskNum + * Maximum number of threads used for processing upload tasks + * concurrently + */ + public void setTaskNum(int taskNum) { + if (taskNum < 1) { + this.taskNum = 1; + } else { + this.taskNum = Math.min(taskNum, 1000); + } + } + + /** + * Obtain the to-be-uploaded local file. + * + * @return To-be-uploaded local file + */ + public String getUploadFile() { + return uploadFile; + } + + /** + * Specify the local file to be uploaded. + * + * @param uploadFile + * To-be-uploaded local file + */ + public void setUploadFile(String uploadFile) { + this.uploadFile = uploadFile; + } + + /** + * Identify whether the resumable mode is enabled. + * + * @return Identifier specifying whether the resumable mode is enabled + */ + public boolean isEnableCheckpoint() { + return enableCheckpoint; + } + + /** + * Specify whether to enable the resumable mode. + * + * @param enableCheckpoint + * Identifier specifying whether the resumable mode is enabled + */ + public void setEnableCheckpoint(boolean enableCheckpoint) { + this.enableCheckpoint = enableCheckpoint; + } + + /** + * Obtain the file used to record resumable upload progresses. + * + * @return File used to record upload progresses + */ + public String getCheckpointFile() { + return checkpointFile; + } + + /** + * Specify a file used to record resumable upload progresses. + * + * @param checkpointFile + * File used to record upload progresses + */ + public void setCheckpointFile(String checkpointFile) { + this.checkpointFile = checkpointFile; + } + + /** + * Obtain object properties. + * + * @return Object properties + */ + public ObjectMetadata getObjectMetadata() { + return objectMetadata; + } + + /** + * Set object properties. + * + * @param objectMetadata + * Object properties + */ + public void setObjectMetadata(ObjectMetadata objectMetadata) { + this.objectMetadata = objectMetadata; + } + + /** + * Identify whether the file to be uploaded in resumable mode will be + * verified. + * + * @return Identifier specifying whether to verify the to-be-uploaded file + */ + public boolean isEnableCheckSum() { + return enableCheckSum; + } + + /** + * Specify whether to verify the file to be uploaded in resumable mode. + * + * @param enableCheckSum + * Identifier specifying whether to verify the to-be-uploaded + * file + */ + public void setEnableCheckSum(boolean enableCheckSum) { + this.enableCheckSum = enableCheckSum; + } + + /** + * Obtain the data transfer listener. + * + * @return Data transfer listener + */ + public ProgressListener getProgressListener() { + return progressListener; + } + + /** + * Set the data transfer listener. + * + * @param progressListener + * Data transfer listener + */ + public void setProgressListener(ProgressListener progressListener) { + this.progressListener = progressListener; + } + + /** + * Obtain the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @return Callback threshold of the data transfer listener + */ + public long getProgressInterval() { + return progressInterval; + } + + /** + * Set the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @param progressInterval + * Callback threshold of the data transfer listener + */ + public void setProgressInterval(long progressInterval) { + this.progressInterval = progressInterval; + } + + /** + * Set encoding type for encoding objectKey, could choose "url" + * @param encodingType + * encoding type for encoding objectKey + */ + public void setEncodingType(String encodingType) { + this.encodingType = encodingType; + } + + /** + * Get encoding type for encoding objectKey + * @return encodingType + */ + public String getEncodingType() { + return encodingType; + } + + public Callback getCallback() { + return callback; + } + + public void setCallback(Callback callback) { + this.callback = callback; + } @Override - public String toString() - { - return "UploadFileRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", partSize=" + partSize + ", taskNum=" - + taskNum + ", uploadFile=" + uploadFile + ", enableCheckpoint=" + enableCheckpoint + ", checkpointFile=" + checkpointFile - + ", objectMetadata=" + objectMetadata + ", enableCheckSum=" + enableCheckSum + "]"; + public String toString() { + return "UploadFileRequest [bucketName=" + bucketName + ", objectKey=" + objectKey + ", partSize=" + partSize + + ", taskNum=" + taskNum + ", uploadFile=" + uploadFile + ", enableCheckpoint=" + enableCheckpoint + + ", checkpointFile=" + checkpointFile + ", objectMetadata=" + objectMetadata + + ", isEncodeHeaders=" + encodeHeaders + ", enableCheckSum=" + enableCheckSum + + ", encodingType=" + encodingType + "]"; } - + } diff --git a/app/src/main/java/com/obs/services/model/UploadObjectsProgressListener.java b/app/src/main/java/com/obs/services/model/UploadObjectsProgressListener.java index 96b9283..c94628a 100644 --- a/app/src/main/java/com/obs/services/model/UploadObjectsProgressListener.java +++ b/app/src/main/java/com/obs/services/model/UploadObjectsProgressListener.java @@ -11,10 +11,11 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; public interface UploadObjectsProgressListener { - - public void progressChanged(UploadProgressStatus status); - + + public void progressChanged(UploadProgressStatus status); + } diff --git a/app/src/main/java/com/obs/services/model/UploadPartRequest.java b/app/src/main/java/com/obs/services/model/UploadPartRequest.java index 63c8d56..50a0c60 100644 --- a/app/src/main/java/com/obs/services/model/UploadPartRequest.java +++ b/app/src/main/java/com/obs/services/model/UploadPartRequest.java @@ -3,14 +3,15 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the * License at - * + *

    * http://www.apache.org/licenses/LICENSE-2.0 - * + *

    * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.io.File; @@ -20,387 +21,361 @@ /** * Parameters in a part upload request - * + * */ -public class UploadPartRequest -{ - private String uploadId; - - private String bucketName; - - private String objectKey; - +public class UploadPartRequest extends AbstractMultipartRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + private int partNumber; - + private Long partSize; - + private long offset; - + private SseCHeader sseCHeader; - + private String contentMd5; - + private boolean attachMd5 = false; - + private File file; - + private InputStream input; - + private boolean autoClose = true; - + private ProgressListener progressListener; - + private long progressInterval = ObsConstraint.DEFAULT_PROGRESS_INTERVAL; - - public UploadPartRequest() - { + + public UploadPartRequest() { } - + /** * Constructor - * @param bucketName Name of the bucket to which the multipart upload belongs - * @param objectKey Name of the object involved in the multipart upload + * + * @param bucketName + * Name of the bucket to which the multipart upload belongs + * @param objectKey + * Name of the object involved in the multipart upload */ - public UploadPartRequest(String bucketName, String objectKey) - { + public UploadPartRequest(String bucketName, String objectKey) { this.bucketName = bucketName; this.objectKey = objectKey; } - + /** * Constructor - * - * @param bucketName Name of the bucket to which the multipart upload belongs - * @param objectKey Name of the object involved in the multipart upload - * @param fileName File name to be uploaded + * + * @param bucketName + * Name of the bucket to which the multipart upload belongs + * @param objectKey + * Name of the object involved in the multipart upload + * @param fileName + * File name to be uploaded */ - public UploadPartRequest(String bucketName, String objectKey, String fileName) - { + public UploadPartRequest(String bucketName, String objectKey, String fileName) { this.bucketName = bucketName; this.objectKey = objectKey; this.file = new File(fileName); } - + /** * Constructor - * - * @param bucketName Name of the bucket to which the multipart upload belongs - * @param objectKey Name of the object involved in the multipart upload - * @param file File to be uploaded + * + * @param bucketName + * Name of the bucket to which the multipart upload belongs + * @param objectKey + * Name of the object involved in the multipart upload + * @param file + * File to be uploaded */ - public UploadPartRequest(String bucketName, String objectKey, File file) - { + public UploadPartRequest(String bucketName, String objectKey, File file) { this.bucketName = bucketName; this.objectKey = objectKey; this.file = file; } - + /** * Constructor - * @param bucketName Name of the bucket to which the multipart upload belongs - * @param objectKey Name of the object involved in the multipart upload - * @param partSize Part size (in bytes) - * @param input Data stream to be uploaded + * + * @param bucketName + * Name of the bucket to which the multipart upload belongs + * @param objectKey + * Name of the object involved in the multipart upload + * @param partSize + * Part size (in bytes) + * @param input + * Data stream to be uploaded */ - public UploadPartRequest(String bucketName, String objectKey, Long partSize, InputStream input) - { + public UploadPartRequest(String bucketName, String objectKey, Long partSize, InputStream input) { this.bucketName = bucketName; this.objectKey = objectKey; this.partSize = partSize; this.input = input; } - + /** * Constructor - * @param bucketName Name of the bucket to which the multipart upload belongs - * @param objectKey Name of the object involved in the multipart upload - * @param partSize Part size (in bytes) - * @param offset Offset of the part in the file. The default value is 0 (in bytes). - * @param file File to be uploaded + * + * @param bucketName + * Name of the bucket to which the multipart upload belongs + * @param objectKey + * Name of the object involved in the multipart upload + * @param partSize + * Part size (in bytes) + * @param offset + * Offset of the part in the file. The default value is 0 (in + * bytes). + * @param file + * File to be uploaded */ - public UploadPartRequest(String bucketName, String objectKey, Long partSize, long offset, File file) - { + public UploadPartRequest(String bucketName, String objectKey, Long partSize, long offset, File file) { this.bucketName = bucketName; this.objectKey = objectKey; this.partSize = partSize; this.offset = offset; this.file = file; } - + /** * Obtain SSE-C encryption headers. - * + * * @return SSE-C encryption headers */ - public SseCHeader getSseCHeader() - { + public SseCHeader getSseCHeader() { return sseCHeader; } - + /** * Set SSE-C encryption headers. - * - * @param sseCHeader SSE-C encryption headers + * + * @param sseCHeader + * SSE-C encryption headers */ - public void setSseCHeader(SseCHeader sseCHeader) - { + public void setSseCHeader(SseCHeader sseCHeader) { this.sseCHeader = sseCHeader; } - + /** - * Obtain the offset of the part in the file. The default value is 0 (in bytes). - * + * Obtain the offset of the part in the file. The default value is 0 (in + * bytes). + * * @return Offset of the part in the file */ - public long getOffset() - { + public long getOffset() { return offset; } - + /** - * Set the start position of the to-be-uploaded content in the file. This parameter is effective only when the path where the file is to be uploaded is configured. The unit is byte and the default value is 0. - * - * @param offset Offset of the part in the file + * Set the start position of the to-be-uploaded content in the file. This + * parameter is effective only when the path where the file is to be + * uploaded is configured. The unit is byte and the default value is 0. + * + * @param offset + * Offset of the part in the file */ - public void setOffset(long offset) - { + public void setOffset(long offset) { this.offset = offset; } - + /** * Obtain the part number. - * + * * @return Part number */ - public int getPartNumber() - { + public int getPartNumber() { return partNumber; } - + /** * Set the part number. - * - * @param partNumber Part number + * + * @param partNumber + * Part number */ - public void setPartNumber(int partNumber) - { + public void setPartNumber(int partNumber) { this.partNumber = partNumber; } - - /** - * Obtain the multipart upload ID. - * - * @return Multipart upload ID - */ - public String getUploadId() - { - return uploadId; - } - - /** - * Set the multipart upload ID. - * - * @param uploadId Multipart upload ID - */ - public void setUploadId(String uploadId) - { - this.uploadId = uploadId; - } - - /** - * Obtain the name of the bucket to which the multipart upload belongs. - * - * @return Name of the bucket to which the multipart upload belongs - */ - public String getBucketName() - { - return bucketName; - } - - /** - * Set the name for the bucket to which the multipart upload belongs. - * - * @param bucketName Name of the bucket to which the multipart upload belongs - */ - public void setBucketName(String bucketName) - { - this.bucketName = bucketName; - } - - /** - * Obtain the name of the object involved in the multipart upload. - * - * @return Name of the object involved in the multipart upload - */ - public String getObjectKey() - { - return objectKey; - } - - /** - * Set the name for the object involved in the multipart upload. - * - * @param objectKey Name of the object involved in the multipart upload - */ - public void setObjectKey(String objectKey) - { - this.objectKey = objectKey; - } - + /** - * Set the part size (in bytes). - * - * @param partSize Part size + * Set the part size (in bytes). + * + * @param partSize + * Part size */ - public void setPartSize(Long partSize) - { + public void setPartSize(Long partSize) { this.partSize = partSize; } - + /** * Obtain the part size, in bytes. - * + * * @return Part size */ - public Long getPartSize() - { + public Long getPartSize() { return partSize; } - + /** - * Obtain the file to be uploaded, which cannot be used with the data stream. - * + * Obtain the file to be uploaded, which cannot be used with the data + * stream. + * * @return File to be uploaded */ - public File getFile() - { + public File getFile() { return file; } - + /** * Set the file to be uploaded, which cannot be used with the data stream. - * - * @param file File to be uploaded + * + * @param file + * File to be uploaded */ - public void setFile(File file) - { + public void setFile(File file) { this.file = file; this.input = null; } - + /** - * Obtain the data stream to be uploaded, which cannot be used with the file to be uploaded. - * + * Obtain the data stream to be uploaded, which cannot be used with the file + * to be uploaded. + * * @return Data stream to be uploaded */ - public InputStream getInput() - { + public InputStream getInput() { return input; } - + /** - * Set the data stream to be uploaded, which cannot be used with the file to be uploaded. - * - * @param input Data stream to be uploaded + * Set the data stream to be uploaded, which cannot be used with the file to + * be uploaded. + * + * @param input + * Data stream to be uploaded */ - public void setInput(InputStream input) - { + public void setInput(InputStream input) { this.input = input; this.file = null; } - + /** - * Check whether the MD5 value of the data to be uploaded will be automatically calculated. If the MD5 value is set, this parameter can be ignored. - * @return Identifier specifying whether to automatically calculate the MD5 value of the data to be uploaded + * Check whether the MD5 value of the data to be uploaded will be + * automatically calculated. If the MD5 value is set, this parameter can be + * ignored. + * + * @return Identifier specifying whether to automatically calculate the MD5 + * value of the data to be uploaded */ - public boolean isAttachMd5() - { + public boolean isAttachMd5() { return attachMd5; } /** - * Specify whether to automatically calculate the MD5 value of the data to be uploaded. If the MD5 value is set, this parameter can be ignored. - * @param attachMd5 Identifier specifying whether to automatically calculate the MD5 value of the data to be uploaded + * Specify whether to automatically calculate the MD5 value of the data to + * be uploaded. If the MD5 value is set, this parameter can be ignored. + * + * @param attachMd5 + * Identifier specifying whether to automatically calculate the + * MD5 value of the data to be uploaded */ - public void setAttachMd5(boolean attachMd5) - { + public void setAttachMd5(boolean attachMd5) { this.attachMd5 = attachMd5; } /** - * Set the MD5 value of the data to be uploaded. + * Set the MD5 value of the data to be uploaded. + * * @return MD5 value of the data to be uploaded */ - public String getContentMd5() - { + public String getContentMd5() { return contentMd5; } /** * Obtain the MD5 value of the data to be uploaded. - * @param contentMd5 MD5 value of the data to be uploaded + * + * @param contentMd5 + * MD5 value of the data to be uploaded */ - public void setContentMd5(String contentMd5) - { + public void setContentMd5(String contentMd5) { this.contentMd5 = contentMd5; } - + /** - * Check whether the input stream will be automatically closed. The default value is "true". - * @return Identifier specifying whether the input stream will be automatically closed + * Check whether the input stream will be automatically closed. The default + * value is "true". + * + * @return Identifier specifying whether the input stream will be + * automatically closed */ public boolean isAutoClose() { - return autoClose; - } + return autoClose; + } /** - * Specify whether to automatically close the input stream. The default value is "true". - * @param autoClose Identifier specifying whether the input stream will be automatically closed + * Specify whether to automatically close the input stream. The default + * value is "true". + * + * @param autoClose + * Identifier specifying whether the input stream will be + * automatically closed */ - public void setAutoClose(boolean autoClose) { - this.autoClose = autoClose; - } - - /** - * Obtain the data transfer listener. - * @return Data transfer listener - */ - public ProgressListener getProgressListener() { - return progressListener; - } - - /** - * Set the data transfer listener. - * @param progressListener Data transfer listener - */ - public void setProgressListener(ProgressListener progressListener) { - this.progressListener = progressListener; - } - - /** - * Obtain the callback threshold of the data transfer listener. The default value is 100 KB. - * @return Callback threshold of the data transfer listener - */ - public long getProgressInterval() { - return progressInterval; - } - - /** - * Set the callback threshold of the data transfer listener. The default value is 100 KB. - * @param progressInterval Callback threshold of the data transfer listener - */ - public void setProgressInterval(long progressInterval) { - this.progressInterval = progressInterval; - } + public void setAutoClose(boolean autoClose) { + this.autoClose = autoClose; + } - @Override - public String toString() - { - return "UploadPartRequest [uploadId=" + uploadId + ", bucketName=" + bucketName + ", objectKey=" + objectKey + ", partNumber=" - + partNumber + ", partSize=" + partSize + ", offset=" + offset + ", sseCHeader=" + sseCHeader + ", contentMd5=" + contentMd5 - + ", attachMd5=" + attachMd5 + ", file=" + file + ", input=" + input + "]"; + /** + * Obtain the data transfer listener. + * + * @return Data transfer listener + */ + public ProgressListener getProgressListener() { + return progressListener; } - + /** + * Set the data transfer listener. + * + * @param progressListener + * Data transfer listener + */ + public void setProgressListener(ProgressListener progressListener) { + this.progressListener = progressListener; + } + + /** + * Obtain the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @return Callback threshold of the data transfer listener + */ + public long getProgressInterval() { + return progressInterval; + } + + /** + * Set the callback threshold of the data transfer listener. The default + * value is 100 KB. + * + * @param progressInterval + * Callback threshold of the data transfer listener + */ + public void setProgressInterval(long progressInterval) { + this.progressInterval = progressInterval; + } + + @Override + public String toString() { + return "UploadPartRequest [uploadId=" + this.getUploadId() + ", bucketName=" + this.getBucketName() + + ", objectKey=" + this.getObjectKey() + + ", partNumber=" + partNumber + ", partSize=" + partSize + ", offset=" + offset + ", sseCHeader=" + + sseCHeader + ", contentMd5=" + contentMd5 + ", attachMd5=" + attachMd5 + ", file=" + file + ", input=" + + input + "]"; + } } diff --git a/app/src/main/java/com/obs/services/model/UploadPartResult.java b/app/src/main/java/com/obs/services/model/UploadPartResult.java index 2e93600..7225ed8 100644 --- a/app/src/main/java/com/obs/services/model/UploadPartResult.java +++ b/app/src/main/java/com/obs/services/model/UploadPartResult.java @@ -11,52 +11,45 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * Response to a part upload request */ -public class UploadPartResult extends HeaderResponse -{ +public class UploadPartResult extends HeaderResponse { private int partNumber; - + private String etag; - + /** * Obtain the part number. * * @return Part number */ - public int getPartNumber() - { + public int getPartNumber() { return partNumber; } - - public void setPartNumber(int partNumber) - { + + public void setPartNumber(int partNumber) { this.partNumber = partNumber; } - - /** + + /** * Obtain the ETag of the part. * * @return Part ETag */ - public String getEtag() - { + public String getEtag() { return etag; } - - public void setEtag(String objEtag) - { + + public void setEtag(String objEtag) { this.etag = objEtag; } @Override - public String toString() - { + public String toString() { return "UploadPartResult [partNumber=" + partNumber + ", etag=" + etag + "]"; } } - - diff --git a/app/src/main/java/com/obs/services/model/UploadProgressStatus.java b/app/src/main/java/com/obs/services/model/UploadProgressStatus.java index 31ee922..db52c9d 100644 --- a/app/src/main/java/com/obs/services/model/UploadProgressStatus.java +++ b/app/src/main/java/com/obs/services/model/UploadProgressStatus.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.concurrent.ConcurrentHashMap; @@ -19,41 +20,49 @@ * Status information of the batch uploading of objects */ public interface UploadProgressStatus extends TaskProgressStatus { - + /** * Obtain the total size of uploaded objects. - * @return Total size of uploaded objects. The value -1 indicates that the total size is still being calculated. + * + * @return Total size of uploaded objects. The value -1 indicates that the + * total size is still being calculated. */ public long getTotalSize(); - + /** * Obtain the size of transferred data in bytes. + * * @return Size of data in bytes that have been transferred */ public long getTransferredSize(); - + + /** + * Obtain the instantaneous speed. + * + * @return Instantaneous speed + */ + public double getInstantaneousSpeed(); + /** - * Obtain the instantaneous speed. - * @return Instantaneous speed - */ - public double getInstantaneousSpeed(); - - /** - * Obtain the average speed. - * @return Average speed - */ - public double getAverageSpeed(); - - /** - * Obtain the progress of the current uploading task. - * @return taskTable Progress of the current uploading task - */ - public ConcurrentHashMap getTaskTable(); - - /** - * Obtain the upload progress of a specified object. - * @param key Object name - * @return Upload progress of a specified object - */ - public ProgressStatus getTaskStatus(String key); + * Obtain the average speed. + * + * @return Average speed + */ + public double getAverageSpeed(); + + /** + * Obtain the progress of the current uploading task. + * + * @return taskTable Progress of the current uploading task + */ + public ConcurrentHashMap getTaskTable(); + + /** + * Obtain the upload progress of a specified object. + * + * @param key + * Object name + * @return Upload progress of a specified object + */ + public ProgressStatus getTaskStatus(String key); } diff --git a/app/src/main/java/com/obs/services/model/V4PostSignatureRequest.java b/app/src/main/java/com/obs/services/model/V4PostSignatureRequest.java index 826a0b9..727a1ef 100644 --- a/app/src/main/java/com/obs/services/model/V4PostSignatureRequest.java +++ b/app/src/main/java/com/obs/services/model/V4PostSignatureRequest.java @@ -11,39 +11,32 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.Date; -@Deprecated -public class V4PostSignatureRequest extends PostSignatureRequest -{ - - - public V4PostSignatureRequest() - { - + +public class V4PostSignatureRequest extends PostSignatureRequest { + + public V4PostSignatureRequest() { + } - - public V4PostSignatureRequest(long expires, String bucketName, String objectKey) - { + + public V4PostSignatureRequest(long expires, String bucketName, String objectKey) { super(expires, bucketName, objectKey); } - - public V4PostSignatureRequest(Date expiryDate, String bucketName, String objectKey) - { - super(expiryDate, bucketName, objectKey); + + public V4PostSignatureRequest(Date expiryDate, String bucketName, String objectKey) { + super(expiryDate, bucketName, objectKey); } - - public V4PostSignatureRequest(long expires, Date requestDate, String bucketName, String objectKey) - { - super(expires, requestDate, bucketName, objectKey); + + public V4PostSignatureRequest(long expires, Date requestDate, String bucketName, String objectKey) { + super(expires, requestDate, bucketName, objectKey); } - - public V4PostSignatureRequest(Date expiryDate, Date requestDate, String bucketName, String objectKey) - { - super(expiryDate,requestDate, bucketName, objectKey); + + public V4PostSignatureRequest(Date expiryDate, Date requestDate, String bucketName, String objectKey) { + super(expiryDate, requestDate, bucketName, objectKey); } - - + } diff --git a/app/src/main/java/com/obs/services/model/V4PostSignatureResponse.java b/app/src/main/java/com/obs/services/model/V4PostSignatureResponse.java index c2236b0..a5a6a6d 100644 --- a/app/src/main/java/com/obs/services/model/V4PostSignatureResponse.java +++ b/app/src/main/java/com/obs/services/model/V4PostSignatureResponse.java @@ -11,24 +11,23 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** * - * Response to a request for browser-based authorized access + * Response to a request for browser-based authorized access * */ -public class V4PostSignatureResponse extends PostSignatureResponse -{ +public class V4PostSignatureResponse extends PostSignatureResponse { private String algorithm; - + private String credential; - + private String date; - - public V4PostSignatureResponse(String policy, String originPolicy, String algorithm, String credential, String date, String signature, - String expiration) - { + + public V4PostSignatureResponse(String policy, String originPolicy, String algorithm, String credential, String date, + String signature, String expiration) { this.policy = policy; this.originPolicy = originPolicy; this.algorithm = algorithm; @@ -37,40 +36,39 @@ public V4PostSignatureResponse(String policy, String originPolicy, String algori this.signature = signature; this.expiration = expiration; } - + /** * Obtain the signature algorithm. + * * @return Signature algorithm */ - public String getAlgorithm() - { + public String getAlgorithm() { return algorithm; } - + /** * Obtain the credential information + * * @return Credential information */ - public String getCredential() - { + public String getCredential() { return credential; } - + /** * Obtain the date in the ISO 8601 format. + * * @return Date in the ISO 8601 format */ - public String getDate() - { + public String getDate() { return date; } - - @Override - public String toString() { - return "V4PostSignatureResponse [algorithm=" + algorithm + ", credential=" + credential + ", date=" + date - + ", expiration=" + expiration + ", policy=" + policy + ", originPolicy=" + originPolicy - + ", signature=" + signature + "]"; - } - + @Override + public String toString() { + return "V4PostSignatureResponse [algorithm=" + algorithm + ", credential=" + credential + ", date=" + date + + ", expiration=" + expiration + ", policy=" + policy + ", originPolicy=" + originPolicy + + ", signature=" + signature + "]"; + } + } diff --git a/app/src/main/java/com/obs/services/model/V4TemporarySignatureRequest.java b/app/src/main/java/com/obs/services/model/V4TemporarySignatureRequest.java index c357d11..45b934a 100644 --- a/app/src/main/java/com/obs/services/model/V4TemporarySignatureRequest.java +++ b/app/src/main/java/com/obs/services/model/V4TemporarySignatureRequest.java @@ -11,28 +11,29 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.Date; -@Deprecated + public class V4TemporarySignatureRequest extends TemporarySignatureRequest { - public V4TemporarySignatureRequest() { - } + public V4TemporarySignatureRequest() { + } - public V4TemporarySignatureRequest(HttpMethodEnum method, long expires) { - super(method, null, null, null, expires); - } + public V4TemporarySignatureRequest(HttpMethodEnum method, long expires) { + super(method, null, null, null, expires); + } - public V4TemporarySignatureRequest(HttpMethodEnum method, String bucketName, String objectKey, - SpecialParamEnum specialParam, long expires) { - super(method, bucketName, objectKey, specialParam, expires, null); - } + public V4TemporarySignatureRequest(HttpMethodEnum method, String bucketName, String objectKey, + SpecialParamEnum specialParam, long expires) { + super(method, bucketName, objectKey, specialParam, expires, null); + } - public V4TemporarySignatureRequest(HttpMethodEnum method, String bucketName, String objectKey, - SpecialParamEnum specialParam, long expires, Date requestDate) { - super(method, bucketName, objectKey, specialParam, expires, requestDate); - } + public V4TemporarySignatureRequest(HttpMethodEnum method, String bucketName, String objectKey, + SpecialParamEnum specialParam, long expires, Date requestDate) { + super(method, bucketName, objectKey, specialParam, expires, requestDate); + } } diff --git a/app/src/main/java/com/obs/services/model/V4TemporarySignatureResponse.java b/app/src/main/java/com/obs/services/model/V4TemporarySignatureResponse.java index 546ecac..897700f 100644 --- a/app/src/main/java/com/obs/services/model/V4TemporarySignatureResponse.java +++ b/app/src/main/java/com/obs/services/model/V4TemporarySignatureResponse.java @@ -11,17 +11,14 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; -@Deprecated -public class V4TemporarySignatureResponse extends TemporarySignatureResponse -{ - - public V4TemporarySignatureResponse(String signedUrl) - { + +public class V4TemporarySignatureResponse extends TemporarySignatureResponse { + + public V4TemporarySignatureResponse(String signedUrl) { super(signedUrl); } - - } diff --git a/app/src/main/java/com/obs/services/model/VersionOrDeleteMarker.java b/app/src/main/java/com/obs/services/model/VersionOrDeleteMarker.java index 493cbae..f33542d 100644 --- a/app/src/main/java/com/obs/services/model/VersionOrDeleteMarker.java +++ b/app/src/main/java/com/obs/services/model/VersionOrDeleteMarker.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.Date; @@ -18,57 +19,70 @@ /** * Versioning object information */ -public class VersionOrDeleteMarker -{ - private String bucketName; - +public class VersionOrDeleteMarker { + private String bucketName; + private String key; - + private String versionId; - + private boolean isLatest; - + private Date lastModified; - + private Owner owner; - + private String etag; - + private long size; - + private StorageClassEnum storageClass; - + private boolean isDeleteMarker; - + private boolean appendable; - - + /** * * Constructor - * @param bucketName Bucket name - * @param key Object name - * @param versionId Version ID - * @param isLatest Identifier indicating whether the object is of the current version - * @param lastModified Last modification date - * @param owner Owner - * @param etag ETag of the object - * @param size Object size (in bytes) - * @param storageClass Storage class of the object - * @param isDeleteMarker Whether the versioning object has been deleted - * @param appendable Whether object is appendable + * + * @param bucketName + * Bucket name + * @param key + * Object name + * @param versionId + * Version ID + * @param isLatest + * Identifier indicating whether the object is of the current + * version + * @param lastModified + * Last modification date + * @param owner + * Owner + * @param etag + * ETag of the object + * @param size + * Object size (in bytes) + * @param storageClass + * Storage class of the object + * @param isDeleteMarker + * Whether the versioning object has been deleted + * @param appendable + * Whether object is appendable */ - public VersionOrDeleteMarker(String bucketName, String key, String versionId, boolean isLatest, Date lastModified, Owner owner, String etag, long size, - StorageClassEnum storageClass, boolean isDeleteMarker, boolean appendable) - { - this.bucketName = bucketName; + @Deprecated + //CHECKSTYLE:OFF + public VersionOrDeleteMarker(String bucketName, String key, String versionId, boolean isLatest, Date lastModified, + Owner owner, String etag, long size, StorageClassEnum storageClass, boolean isDeleteMarker, + boolean appendable) { + this.bucketName = bucketName; this.key = key; this.versionId = versionId; this.isLatest = isLatest; - if(null != lastModified) { - this.lastModified = (Date) lastModified.clone(); + if (null != lastModified) { + this.lastModified = (Date) lastModified.clone(); } else { - this.lastModified = null; + this.lastModified = null; } this.owner = owner; this.etag = etag; @@ -78,141 +92,234 @@ public VersionOrDeleteMarker(String bucketName, String key, String versionId, bo this.appendable = appendable; } + private VersionOrDeleteMarker(Builder builder) { + this.bucketName = builder.bucketName; + this.key = builder.key; + this.versionId = builder.versionId; + this.isLatest = builder.isLatest; + if (null != builder.lastModified) { + this.lastModified = (Date) builder.lastModified.clone(); + } else { + this.lastModified = null; + } + this.owner = builder.owner; + this.etag = builder.etag; + this.size = builder.size; + this.storageClass = builder.storageClass; + this.isDeleteMarker = builder.isDeleteMarker; + this.appendable = builder.appendable; + } + + public static final class Builder { + private String bucketName; + private String key; + private String versionId; + private boolean isLatest; + private Date lastModified; + private Owner owner; + private String etag; + private long size; + private StorageClassEnum storageClass; + private boolean isDeleteMarker; + private boolean appendable; + + public Builder bucketName(String bucketName) { + this.bucketName = bucketName; + return this; + } + + public Builder key(String key) { + this.key = key; + return this; + } + + public Builder versionId(String versionId) { + this.versionId = versionId; + return this; + } + + public Builder isLatest(boolean isLatest) { + this.isLatest = isLatest; + return this; + } + + public Builder lastModified(Date lastModified) { + if (null != lastModified) { + this.lastModified = (Date) lastModified.clone(); + } else { + this.lastModified = null; + } + return this; + } + + public Builder owner(Owner owner) { + this.owner = owner; + return this; + } + + public Builder etag(String etag) { + this.etag = etag; + return this; + } + + public Builder size(long size) { + this.size = size; + return this; + } + + public Builder storageClass(StorageClassEnum storageClass) { + this.storageClass = storageClass; + return this; + } + + public Builder isDeleteMarker(boolean isDeleteMarker) { + this.isDeleteMarker = isDeleteMarker; + return this; + } + + public Builder appendable(boolean appendable) { + this.appendable = appendable; + return this; + } + + public VersionOrDeleteMarker builder() { + return new VersionOrDeleteMarker(this); + } + } + /** * Obtain the object name. + * * @return Object name */ - public String getKey() - { + public String getKey() { return key; } - + /** * Obtain the object name. + * * @return Object name */ - public String getObjectKey() - { + public String getObjectKey() { return key; } - + /** * Obtain the object version ID. + * * @return Version ID of the object */ - public String getVersionId() - { + public String getVersionId() { return versionId; } - + /** * Check whether the object is of the current version. - * @return Identifier indicating whether the object is of the current version + * + * @return Identifier indicating whether the object is of the current + * version */ - public boolean isLatest() - { + public boolean isLatest() { return isLatest; } - - + /** * Obtain the last modification date of the object. + * * @return Last modification date of the object */ - public Date getLastModified() - { - if(null != this.lastModified) { - return (Date) this.lastModified.clone(); - } else { - return null; - } + public Date getLastModified() { + if (null != this.lastModified) { + return (Date) this.lastModified.clone(); + } else { + return null; + } } - - + /** - * Obtain the owner of the object. - * @return Owner of the object + * Obtain the owner of the object. + * + * @return Owner of the object */ - public Owner getOwner() - { + public Owner getOwner() { return owner; } - + /** * Obtain the storage class of the object. + * * @return Storage class of the object */ - @Deprecated - public String getStorageClass() - { + public String getStorageClass() { return this.storageClass != null ? this.storageClass.getCode() : null; } - /** * Obtain the storage class of the object. + * * @return Storage class of the object */ - public StorageClassEnum getObjectStorageClass() - { + public StorageClassEnum getObjectStorageClass() { return storageClass; } - + /** * Obtain ETag of the object. * * @return Object ETag */ - public String getEtag() - { + public String getEtag() { return etag; } - /** - * Obtain the object size (in bytes). + * Obtain the object size (in bytes). + * * @return Object size */ - public long getSize() - { + public long getSize() { return size; } - /** * Check whether the versioning object has been deleted. - * @return Identifier indicating whether the versioning object has been deleted + * + * @return Identifier indicating whether the versioning object has been + * deleted */ - public boolean isDeleteMarker() - { + public boolean isDeleteMarker() { return isDeleteMarker; } - /** * Identify whether an object is appendable. + * * @return Identifier specifying whether the object is an appendable object */ - public boolean isAppendable() { - return appendable; - } - + public boolean isAppendable() { + return appendable; + } + /** * Obtain the name of the bucket to which the versioning object belongs. + * * @return Name of the versioning object-residing bucket */ - public String getBucketName() { - return bucketName; - } - - @Override - public String toString() { - return "VersionOrDeleteMarker [bucketName=" + bucketName + ", key=" + key + ", versionId=" + versionId - + ", isLatest=" + isLatest + ", lastModified=" + lastModified + ", owner=" + owner + ", etag=" + etag - + ", size=" + size + ", storageClass=" + storageClass + ", isDeleteMarker=" + isDeleteMarker - + ", appendable=" + appendable + "]"; - } + public String getBucketName() { + return bucketName; + } -} + public void setKey(String key) { + this.key = key; + } + @Override + public String toString() { + return "VersionOrDeleteMarker [bucketName=" + bucketName + ", key=" + key + ", versionId=" + versionId + + ", isLatest=" + isLatest + ", lastModified=" + lastModified + ", owner=" + owner + ", etag=" + etag + + ", size=" + size + ", storageClass=" + storageClass + ", isDeleteMarker=" + isDeleteMarker + + ", appendable=" + appendable + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/VersioningStatusEnum.java b/app/src/main/java/com/obs/services/model/VersioningStatusEnum.java index 78380b8..dde98c0 100644 --- a/app/src/main/java/com/obs/services/model/VersioningStatusEnum.java +++ b/app/src/main/java/com/obs/services/model/VersioningStatusEnum.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; /** @@ -18,36 +19,34 @@ * */ public enum VersioningStatusEnum { - - /** - * Suspended - */ - SUSPENDED("Suspended"), - - /** - * Enabled - */ - ENABLED("Enabled"); - - private String code; - - private VersioningStatusEnum(String code) { - this.code = code; - } - - public String getCode() { - return code; - } - - public static VersioningStatusEnum getValueFromCode(String code) { - for (VersioningStatusEnum val : VersioningStatusEnum.values()) { - if (val.code.equals(code)) { - return val; - } - } - return null; - } -} + /** + * Suspended + */ + SUSPENDED("Suspended"), + + /** + * Enabled + */ + ENABLED("Enabled"); + + private String code; + private VersioningStatusEnum(String code) { + this.code = code; + } + public String getCode() { + return code; + } + + public static VersioningStatusEnum getValueFromCode(String code) { + for (VersioningStatusEnum val : VersioningStatusEnum.values()) { + if (val.code.equals(code)) { + return val; + } + } + return null; + } + +} diff --git a/app/src/main/java/com/obs/services/model/WebsiteConfiguration.java b/app/src/main/java/com/obs/services/model/WebsiteConfiguration.java index 4dc8c73..554b298 100644 --- a/app/src/main/java/com/obs/services/model/WebsiteConfiguration.java +++ b/app/src/main/java/com/obs/services/model/WebsiteConfiguration.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model; import java.util.ArrayList; @@ -19,100 +20,99 @@ /** * Website hosting configuration of a bucket */ -public class WebsiteConfiguration extends HeaderResponse -{ +public class WebsiteConfiguration extends HeaderResponse { private String suffix; - + private String key; - + private RedirectAllRequest redirectAllRequestsTo; - + private List routeRules; - + /** * Obtain the hosting homepage. - * @return Hosting homepage + * + * @return Hosting homepage * */ - public String getSuffix() - { + public String getSuffix() { return suffix; } - + /** * Set the hosting homepage. - * @param suffix Hosting homepage + * + * @param suffix + * Hosting homepage * */ - public void setSuffix(String suffix) - { + public void setSuffix(String suffix) { this.suffix = suffix; } - + /** * Obtain the error page of the hosting. - ** @return key Error page of the hosting. + ** + * @return key Error page of the hosting. */ - public String getKey() - { + public String getKey() { return key; } - + /** * Set the error page of the hosting. - * @param key Error page of the hosting + * + * @param key + * Error page of the hosting */ - public void setKey(String key) - { + public void setKey(String key) { this.key = key; } - + /** * Set the redirection route rules. + * * @return routeRules Redirection route rule list */ - public List getRouteRules() - { - if(this.routeRules == null) { - this.routeRules = new ArrayList(); - } + public List getRouteRules() { + if (this.routeRules == null) { + this.routeRules = new ArrayList(); + } return routeRules; } - + /** * Set the redirection route rule list. - * @param routeRules Redirection route rule list + * + * @param routeRules + * Redirection route rule list */ - public void setRouteRules(List routeRules) - { + public void setRouteRules(List routeRules) { this.routeRules = routeRules; } - + /** * Obtain the redirection rules of all requests. + * * @return Redirection rules of all requests */ - public RedirectAllRequest getRedirectAllRequestsTo() - { + public RedirectAllRequest getRedirectAllRequestsTo() { return redirectAllRequestsTo; } - + /** * Set redirection rules of all requests. - * @param redirectAllRequestsTo Redirection rules of all requests + * + * @param redirectAllRequestsTo + * Redirection rules of all requests */ - public void setRedirectAllRequestsTo(RedirectAllRequest redirectAllRequestsTo) - { + public void setRedirectAllRequestsTo(RedirectAllRequest redirectAllRequestsTo) { this.redirectAllRequestsTo = redirectAllRequestsTo; } - - + @Override - public String toString() - { - return "WebsiteConfigration [suffix=" + suffix + ", key=" + key + ", redirectAllRequestsTo=" + redirectAllRequestsTo - + ", routeRules=" + routeRules + "]"; + public String toString() { + return "WebsiteConfigration [suffix=" + suffix + ", key=" + key + ", redirectAllRequestsTo=" + + redirectAllRequestsTo + ", routeRules=" + routeRules + "]"; } } - - diff --git a/app/src/main/java/com/obs/services/model/fs/ContentSummaryFsRequest.java b/app/src/main/java/com/obs/services/model/fs/ContentSummaryFsRequest.java new file mode 100644 index 0000000..8978586 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/fs/ContentSummaryFsRequest.java @@ -0,0 +1,29 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model.fs; + +import com.obs.services.model.GenericRequest; + +public class ContentSummaryFsRequest extends GenericRequest { + private String dirName; + + public String getDirName() { + return dirName; + } + + public void setDirName(String dirName) { + this.dirName = dirName; + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/ContentSummaryFsResult.java b/app/src/main/java/com/obs/services/model/fs/ContentSummaryFsResult.java new file mode 100644 index 0000000..32a8c41 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/fs/ContentSummaryFsResult.java @@ -0,0 +1,29 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model.fs; + +import com.obs.services.model.HeaderResponse; + +public class ContentSummaryFsResult extends HeaderResponse { + private DirSummary contentSummary; + + public DirSummary getContentSummary() { + return contentSummary; + } + + public void setContentSummary(DirSummary contentSummary) { + this.contentSummary = contentSummary; + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/DirContentSummary.java b/app/src/main/java/com/obs/services/model/fs/DirContentSummary.java new file mode 100644 index 0000000..99e4a16 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/fs/DirContentSummary.java @@ -0,0 +1,65 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model.fs; + +import java.util.List; + +public class DirContentSummary { + private String key; + private boolean isTruncated; + private String nextMarker; + private long inode; + private List subDir; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public long getInode() { + return inode; + } + + public void setInode(long inode) { + this.inode = inode; + } + + public boolean isTruncated() { + return isTruncated; + } + + public void setTruncated(boolean truncated) { + isTruncated = truncated; + } + + public String getNextMarker() { + return nextMarker; + } + + public void setNextMarker(String nextMarker) { + this.nextMarker = nextMarker; + } + + public List getSubDir() { + return subDir; + } + + public void setSubDir(List subDir) { + this.subDir = subDir; + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/DirSummary.java b/app/src/main/java/com/obs/services/model/fs/DirSummary.java new file mode 100644 index 0000000..0ce9838 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/fs/DirSummary.java @@ -0,0 +1,74 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model.fs; + +public class DirSummary { + private String name; + private long dirCount; + private long fileCount; + private long fileSize; + private long inode; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getDirCount() { + return dirCount; + } + + public void setDirCount(long dirCount) { + this.dirCount = dirCount; + } + + public long getFileCount() { + return fileCount; + } + + public void setFileCount(long fileCount) { + this.fileCount = fileCount; + } + + public long getFileSize() { + return fileSize; + } + + public void setFileSize(long fileSize) { + this.fileSize = fileSize; + } + + public long getInode() { + return inode; + } + + public void setInode(long inode) { + this.inode = inode; + } + + @Override + public String toString() { + return "DirSummary{" + + "name='" + name + '\'' + + ", dirCount=" + dirCount + + ", fileCount=" + fileCount + + ", fileSize=" + fileSize + + ", inode=" + inode + + '}'; + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/DropFileRequest.java b/app/src/main/java/com/obs/services/model/fs/DropFileRequest.java index 22929ad..7a397f9 100644 --- a/app/src/main/java/com/obs/services/model/fs/DropFileRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/DropFileRequest.java @@ -11,17 +11,16 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; +import com.obs.services.model.BaseObjectRequest; + /** * Request parameters for deleting objects */ -public class DropFileRequest { - - private String bucketName; - - private String objectKey; +public class DropFileRequest extends BaseObjectRequest { private String versionId; @@ -31,8 +30,10 @@ public DropFileRequest() { /** * Constructor * - * @param bucketName Bucket name - * @param objectKey Object name + * @param bucketName + * Bucket name + * @param objectKey + * Object name */ public DropFileRequest(String bucketName, String objectKey) { this.bucketName = bucketName; @@ -42,9 +43,12 @@ public DropFileRequest(String bucketName, String objectKey) { /** * Constructor * - * @param bucketName Bucket name - * @param objectKey Object name - * @param versionId Object version + * @param bucketName + * Bucket name + * @param objectKey + * Object name + * @param versionId + * Object version */ public DropFileRequest(String bucketName, String objectKey, String versionId) { this.bucketName = bucketName; @@ -52,42 +56,7 @@ public DropFileRequest(String bucketName, String objectKey, String versionId) { this.versionId = versionId; } - /** - * Obtain the bucket name. - * - * @return Bucket name - */ - public String getBucketName() { - return bucketName; - } - - /** - * Set the bucket name. - * - * @param bucketName Bucket name - */ - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } - - /** - * Obtain the object name. - * - * @return Object name - */ - public String getObjectKey() { - return objectKey; - } - - /** - * Set the object name. - * - * @param objectKey Object name - */ - public void setObjectKey(String objectKey) { - this.objectKey = objectKey; - } - + /** * Obtain the object version ID. * diff --git a/app/src/main/java/com/obs/services/model/fs/DropFileResult.java b/app/src/main/java/com/obs/services/model/fs/DropFileResult.java index d0ee737..b69dc22 100644 --- a/app/src/main/java/com/obs/services/model/fs/DropFileResult.java +++ b/app/src/main/java/com/obs/services/model/fs/DropFileResult.java @@ -11,11 +11,12 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.model.DeleteObjectResult; -public class DropFileResult extends DeleteObjectResult{ +public class DropFileResult extends DeleteObjectResult { public DropFileResult(boolean deleteMarker, String versionId) { super(deleteMarker, versionId); @@ -24,7 +25,5 @@ public DropFileResult(boolean deleteMarker, String versionId) { public DropFileResult(boolean deleteMarker, String objectKey, String versionId) { super(deleteMarker, objectKey, versionId); } - - } diff --git a/app/src/main/java/com/obs/services/model/fs/DropFolderRequest.java b/app/src/main/java/com/obs/services/model/fs/DropFolderRequest.java index aef7f3c..b9c7fcf 100644 --- a/app/src/main/java/com/obs/services/model/fs/DropFolderRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/DropFolderRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.model.AbstractBulkRequest; @@ -18,13 +19,16 @@ import com.obs.services.model.TaskCallback; /** - * Request parameters for deleting folders. Deleting a folder will delete all sub-folders and files in the folder. - * This function does not support buckets with versioning enabled. + * Request parameters for deleting folders. Deleting a folder will delete all + * sub-folders and files in the folder. This function does not support buckets + * with versioning enabled. */ public class DropFolderRequest extends AbstractBulkRequest { private String folderName; + private String encodingType; + private TaskCallback callback; public DropFolderRequest() { @@ -33,15 +37,30 @@ public DropFolderRequest() { /** * Constructor * - * @param bucketName Bucket name + * @param bucketName + * Bucket name */ public DropFolderRequest(String bucketName) { - super(bucketName); + this.bucketName = bucketName; } public DropFolderRequest(String bucketName, String folderName) { - super(bucketName); + this.bucketName = bucketName; + this.folderName = folderName; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param encodingType + * The encoding type use for encode objectKey. + */ + public DropFolderRequest(String bucketName, String folderName, String encodingType) { + this.bucketName = bucketName; this.folderName = folderName; + this.encodingType = encodingType; } /** @@ -56,7 +75,8 @@ public String getFolderName() { /** * Set the folder name. * - * @param folderName Folder name + * @param folderName + * Folder name */ public void setFolderName(String folderName) { this.folderName = folderName; @@ -74,14 +94,35 @@ public TaskCallback getCallback() { /** * Set the callback object of a batch task. * - * @param callback Callback object + * @param callback + * Callback object */ public void setCallback(TaskCallback callback) { this.callback = callback; } + /** + * Set the encoding type that used for encode objectkey + * + * @param encodingType + * could chose url. + */ + public void setEncodingType(String encodingType) { + this.encodingType = encodingType; + } + + /** + * Obtain the list of to-be-deleted objects. + * + * @return List of to-be-deleted objects + */ + public String getEncodingType() { + return encodingType; + } + @Override public String toString() { - return "DropFolderRequest [bucketName=" + bucketName + ", folderName=" + folderName + "]"; + return "DropFolderRequest [bucketName=" + bucketName + ", folderName=" + folderName + + ", encodingType=" + encodingType + "]"; } } diff --git a/app/src/main/java/com/obs/services/model/fs/FSStatusEnum.java b/app/src/main/java/com/obs/services/model/fs/FSStatusEnum.java index dc46317..574319d 100644 --- a/app/src/main/java/com/obs/services/model/fs/FSStatusEnum.java +++ b/app/src/main/java/com/obs/services/model/fs/FSStatusEnum.java @@ -11,8 +11,8 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ -package com.obs.services.model.fs; +package com.obs.services.model.fs; /** * File gateway feature of the bucket @@ -20,34 +20,32 @@ */ public enum FSStatusEnum { - /** - * Enable the file gateway feature. - */ - ENABLED("Enabled"), - - /** - * Disable the file gateway feature. - */ - DISABLED("Disabled"); - - private String code; - - private FSStatusEnum(String code) { - this.code = code; - } - - public String getCode() { - return code; - } - - public static FSStatusEnum getValueFromCode(String code) { - for (FSStatusEnum val : FSStatusEnum.values()) { - if (val.code.equals(code)) { - return val; - } - } - return null; - } + /** + * Enable the file gateway feature. + */ + ENABLED("Enabled"), + + /** + * Disable the file gateway feature. + */ + DISABLED("Disabled"); + + private String code; + + private FSStatusEnum(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + + public static FSStatusEnum getValueFromCode(String code) { + for (FSStatusEnum val : FSStatusEnum.values()) { + if (val.code.equals(code)) { + return val; + } + } + return null; + } } - - diff --git a/app/src/main/java/com/obs/services/model/fs/FolderContentSummary.java b/app/src/main/java/com/obs/services/model/fs/FolderContentSummary.java new file mode 100644 index 0000000..bda15e3 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/fs/FolderContentSummary.java @@ -0,0 +1,84 @@ +package com.obs.services.model.fs; + +import java.util.ArrayList; +import java.util.List; + +public class FolderContentSummary { + private String dir; + private long dirHeight; + private List layerSummaries; + + public String getDir() { + return dir; + } + + public void setDir(String dir) { + this.dir = dir; + } + + public long getDirHeight() { + return dirHeight; + } + + public void setDirHeight(long dirHeight) { + this.dirHeight = dirHeight; + } + + public List getLayerSummaries() { + if (layerSummaries == null) { + this.layerSummaries = new ArrayList(); + } + return layerSummaries; + } + + @Override + public String toString() { + return "FolderContentSummary{" + "dir='" + dir + '\'' + ", dirHeight=" + dirHeight + ", LayerSummaries=" + + layerSummaries + '}'; + } + + static public class LayerSummary { + private long summaryHeight; + private long dirCount; + private long fileCount; + private long fileSize; + + public long getSummaryHeight() { + return summaryHeight; + } + + public void setSummaryHeight(long summaryHeight) { + this.summaryHeight = summaryHeight; + } + + public long getDirCount() { + return dirCount; + } + + public void setDirCount(long dirCount) { + this.dirCount = dirCount; + } + + public long getFileCount() { + return fileCount; + } + + public void setFileCount(long fileCount) { + this.fileCount = fileCount; + } + + public long getFileSize() { + return fileSize; + } + + public void setFileSize(long fileSize) { + this.fileSize = fileSize; + } + + @Override + public String toString() { + return "LayerSummary{" + "summaryHeight=" + summaryHeight + ", dirCount=" + dirCount + ", fileCount=" + + fileCount + ", fileSize=" + fileSize + '}'; + } + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/GetAttributeRequest.java b/app/src/main/java/com/obs/services/model/fs/GetAttributeRequest.java index a8127e5..068b77e 100644 --- a/app/src/main/java/com/obs/services/model/fs/GetAttributeRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/GetAttributeRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.model.GetObjectMetadataRequest; @@ -19,21 +20,21 @@ * Parameters in a request for obtaining filer or folder properties * */ -public class GetAttributeRequest extends GetObjectMetadataRequest{ - - public GetAttributeRequest() { - super(); - } +public class GetAttributeRequest extends GetObjectMetadataRequest { - /** - * Constructor - * @param bucketName Bucket name - * @param objectKey File or folder name - */ - public GetAttributeRequest(String bucketName, String objectKey) { - super(bucketName, objectKey); - } - -} + public GetAttributeRequest() { + } + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File or folder name + */ + public GetAttributeRequest(String bucketName, String objectKey) { + super(bucketName, objectKey); + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/GetBucketFSStatusRequest.java b/app/src/main/java/com/obs/services/model/fs/GetBucketFSStatusRequest.java index f8f4b6d..2dbd286 100644 --- a/app/src/main/java/com/obs/services/model/fs/GetBucketFSStatusRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/GetBucketFSStatusRequest.java @@ -11,27 +11,29 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.model.BucketMetadataInfoRequest; /** - * Parameters in a request for obtaining status of the file gateway feature of the bucket + * Parameters in a request for obtaining status of the file gateway feature of + * the bucket * */ -public class GetBucketFSStatusRequest extends BucketMetadataInfoRequest{ - - public GetBucketFSStatusRequest() { - - } - - /** - * Constructor - * @param bucketName Bucket name - */ - public GetBucketFSStatusRequest(String bucketName) { - this.bucketName = bucketName; - } -} +public class GetBucketFSStatusRequest extends BucketMetadataInfoRequest { + public GetBucketFSStatusRequest() { + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + */ + public GetBucketFSStatusRequest(String bucketName) { + this.bucketName = bucketName; + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/GetBucketFSStatusResult.java b/app/src/main/java/com/obs/services/model/fs/GetBucketFSStatusResult.java index e891186..9535684 100644 --- a/app/src/main/java/com/obs/services/model/fs/GetBucketFSStatusResult.java +++ b/app/src/main/java/com/obs/services/model/fs/GetBucketFSStatusResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import java.util.List; @@ -21,43 +22,167 @@ import com.obs.services.model.StorageClassEnum; /** - * Response to a request of obtaining status of the file gateway feature of a bucket + * Response to a request of obtaining status of the file gateway feature of a + * bucket * */ -public class GetBucketFSStatusResult extends BucketMetadataInfoResult{ - - private FSStatusEnum status; - - public GetBucketFSStatusResult(String allowOrigin, List allowHeaders, int maxAge, List allowMethods, - List exposeHeaders, StorageClassEnum storageClass, String location, String obsVersion) { - super(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion); - } - - public GetBucketFSStatusResult(String allowOrigin, List allowHeaders, int maxAge, List allowMethods, - List exposeHeaders, StorageClassEnum storageClass, String location, String obsVersion, FSStatusEnum status) { - this(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion); - this.status = status; - } - - public GetBucketFSStatusResult(String allowOrigin, List allowHeaders, int maxAge, List allowMethods, - List exposeHeaders, StorageClassEnum storageClass, String location, String obsVersion, FSStatusEnum status, AvailableZoneEnum availableZone) { - super(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion, availableZone); - this.status = status; - } - - public GetBucketFSStatusResult(String allowOrigin, List allowHeaders, int maxAge, List allowMethods, - List exposeHeaders, StorageClassEnum storageClass, String location, String obsVersion, FSStatusEnum status, - AvailableZoneEnum availableZone, String epid, BucketTypeEnum bucketTypeEnum) { - super(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion, availableZone, epid, bucketTypeEnum); - this.status = status; - } - - /** - * Obtain the status of the file gateway feature of a bucket. - * @return Status of the file gateway feature - */ - public FSStatusEnum getStatus() { - return status; - } +public class GetBucketFSStatusResult extends BucketMetadataInfoResult { + + private FSStatusEnum status; + + public GetBucketFSStatusResult(String allowOrigin, List allowHeaders, int maxAge, List allowMethods, + List exposeHeaders, StorageClassEnum storageClass, String location, String obsVersion) { + super(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion); + } + + @Deprecated + //CHECKSTYLE:OFF + public GetBucketFSStatusResult(String allowOrigin, List allowHeaders, int maxAge, List allowMethods, + List exposeHeaders, StorageClassEnum storageClass, String location, String obsVersion, + FSStatusEnum status) { + this(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion); + this.status = status; + } + + @Deprecated + //CHECKSTYLE:OFF + public GetBucketFSStatusResult(String allowOrigin, List allowHeaders, int maxAge, List allowMethods, + List exposeHeaders, StorageClassEnum storageClass, String location, String obsVersion, + FSStatusEnum status, AvailableZoneEnum availableZone) { + this(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion); + this.availableZone = availableZone; + this.status = status; + } + + @Deprecated + //CHECKSTYLE:OFF + public GetBucketFSStatusResult(String allowOrigin, List allowHeaders, int maxAge, List allowMethods, + List exposeHeaders, StorageClassEnum storageClass, String location, String obsVersion, + FSStatusEnum status, AvailableZoneEnum availableZone, String epid, BucketTypeEnum bucketTypeEnum) { + this(allowOrigin, allowHeaders, maxAge, allowMethods, exposeHeaders, storageClass, location, obsVersion); + this.availableZone = availableZone; + this.epid = epid; + this.bucketType = bucketTypeEnum; + this.status = status; + } + + private GetBucketFSStatusResult(Builder builder) { + super(); + this.allowOrigin = builder.allowOrigin; + this.allowHeaders = builder.allowHeaders; + this.maxAge = builder.maxAge; + this.allowMethods = builder.allowMethods; + this.exposeHeaders = builder.exposeHeaders; + this.storageClass = builder.storageClass; + this.location = builder.location; + this.obsVersion = builder.obsVersion; + this.availableZone = builder.availableZone; + this.epid = builder.epid; + this.bucketType = builder.bucketType; + this.status = builder.status; + } + + public static final class Builder { + private String allowOrigin; + private List allowHeaders; + private int maxAge; + private List allowMethods; + private List exposeHeaders; + private StorageClassEnum storageClass; + private String location; + private String obsVersion; + private AvailableZoneEnum availableZone; + private String epid; + private BucketTypeEnum bucketType = BucketTypeEnum.OBJECT; + private FSStatusEnum status; + + public Builder allowOrigin(String allowOrigin) { + this.allowOrigin = allowOrigin; + return this; + } + + public Builder allowHeaders(List allowHeaders) { + this.allowHeaders = allowHeaders; + return this; + } + + public Builder maxAge(int maxAge) { + this.maxAge = maxAge; + return this; + } + + public Builder allowMethods(List allowMethods) { + this.allowMethods = allowMethods; + return this; + } + + public Builder exposeHeaders(List exposeHeaders) { + this.exposeHeaders = exposeHeaders; + return this; + } + + public Builder storageClass(StorageClassEnum storageClass) { + this.storageClass = storageClass; + return this; + } + + public Builder location(String location) { + this.location = location; + return this; + } + + public Builder obsVersion(String obsVersion) { + this.obsVersion = obsVersion; + return this; + } + + public Builder availableZone(AvailableZoneEnum availableZone) { + this.availableZone = availableZone; + return this; + } + + public Builder epid(String epid) { + this.epid = epid; + return this; + } + + public Builder bucketType(BucketTypeEnum bucketType) { + this.bucketType = bucketType; + return this; + } + + public Builder status(FSStatusEnum status) { + this.status = status; + return this; + } + + public GetBucketFSStatusResult build() { + return new GetBucketFSStatusResult(this); + } + + public BucketMetadataInfoResult.Builder builderBucketMetadataInfo() { + return new BucketMetadataInfoResult.Builder() + .allowOrigin(this.allowOrigin) + .allowHeaders(this.allowHeaders) + .maxAge(this.maxAge) + .allowMethods(this.allowMethods) + .exposeHeaders(this.exposeHeaders) + .storageClass(this.storageClass) + .location(this.location) + .obsVersion(this.obsVersion) + .availableZone(this.availableZone) + .epid(this.epid) + .bucketType(this.bucketType); + } + } + + /** + * Obtain the status of the file gateway feature of a bucket. + * + * @return Status of the file gateway feature + */ + public FSStatusEnum getStatus() { + return status; + } } diff --git a/app/src/main/java/com/obs/services/model/fs/ListContentSummaryFsRequest.java b/app/src/main/java/com/obs/services/model/fs/ListContentSummaryFsRequest.java new file mode 100644 index 0000000..c045e6f --- /dev/null +++ b/app/src/main/java/com/obs/services/model/fs/ListContentSummaryFsRequest.java @@ -0,0 +1,84 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model.fs; + +import com.obs.services.model.GenericRequest; + +import java.util.List; + +/** + * Parameters in a request for obtaining fs folder contentSummary + * + */ +public class ListContentSummaryFsRequest extends GenericRequest { + private int maxKeys; + + private List dirLayers; + + public int getMaxKeys() { + return maxKeys; + } + + public void setMaxKeys(int maxKeys) { + this.maxKeys = maxKeys; + } + + public List getDirLayers() { + return dirLayers; + } + + public void setDirLayers(List dirLayers) { + this.dirLayers = dirLayers; + } + + static public class DirLayer { + private String key; + private String marker; + private long inode; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getMarker() { + return marker; + } + + public void setMarker(String marker) { + this.marker = marker; + } + + public long getInode() { + return inode; + } + + public void setInode(long inode) { + this.inode = inode; + } + + @Override + public String toString() { + return "DirLayer{" + + "key='" + key + '\'' + + ", marker='" + marker + '\'' + + ", inode=" + inode + + '}'; + } + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/ListContentSummaryFsResult.java b/app/src/main/java/com/obs/services/model/fs/ListContentSummaryFsResult.java new file mode 100644 index 0000000..cf63e20 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/fs/ListContentSummaryFsResult.java @@ -0,0 +1,126 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model.fs; + +import com.obs.services.model.HeaderResponse; + +import java.util.List; + +/** + * Response to a request for obtaining fs folder contentSummary + * + */ +public class ListContentSummaryFsResult extends HeaderResponse { + private List dirContentSummaries; + + private List errorResults; + + public List getDirContentSummaries() { + return dirContentSummaries; + } + + public void setDirContentSummaries(List dirContentSummaries) { + this.dirContentSummaries = dirContentSummaries; + } + + public List getErrorResults() { + return errorResults; + } + + public void setErrorResults(List errorResults) { + this.errorResults = errorResults; + } + + /** + * Results returned if the deletion fails + */ + public static class ErrorResult { + private String key; + + private String statusCode; + + private String errorCode; + + private String message; + + private long inode; + + /** + * Dir returned after a deletion failure + * + * @return Dir returned after a deletion failure + */ + public String getKey() { + return key; + } + + /** + * Error code returned after a deletion failure + * + * @return Error code returned after a deletion failure + */ + public String getErrorCode() { + return errorCode; + } + + public String getStatusCode() { + return statusCode; + } + + /** + * Obtain the error description returned after a deletion failure. + * + * @return Error information returned after a deletion failure + */ + public String getMessage() { + return message; + } + + public void setKey(String key) { + this.key = key; + } + + public void setStatusCode(String statusCode) { + this.statusCode = statusCode; + } + + public void setErrorCode(String errorCode) { + this.errorCode = errorCode; + } + + public void setMessage(String message) { + this.message = message; + } + + public long getInode() { + return inode; + } + + public void setInode(long inode) { + this.inode = inode; + } + + @Override + public String toString() { + return "ErrorResult{" + + "key='" + key + '\'' + + ", statusCode='" + statusCode + '\'' + + ", errorCode='" + errorCode + '\'' + + ", message='" + message + '\'' + + ", inode=" + inode + + '}'; + } + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/ListContentSummaryRequest.java b/app/src/main/java/com/obs/services/model/fs/ListContentSummaryRequest.java new file mode 100644 index 0000000..86cd8d6 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/fs/ListContentSummaryRequest.java @@ -0,0 +1,123 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model.fs; + +import com.obs.services.model.GenericRequest; + +/** + * Parameters in a request for obtaining folder contentSummary + * + */ +public class ListContentSummaryRequest extends GenericRequest { + private String prefix; + + private String marker; + + private int maxKeys; + + private String delimiter; + + private int listTimeout; + + public ListContentSummaryRequest() { + } + + /** + * 构造函数 + * + * @param bucketName 桶名 + */ + public ListContentSummaryRequest(String bucketName) { + this.bucketName = bucketName; + this.delimiter = "/"; + } + + /** + * 获取列举对象时的对象名前缀 + * + * @return 对象名前缀 + */ + public String getPrefix() { + return prefix; + } + + /** + * 设置列举对象时的对象名前缀 + * + * @param prefix 对象名前缀 + */ + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + /** + * 获取列举对象时的起始位置 + * + * @return 起始位置标识 + */ + public String getMarker() { + return marker; + } + + /** + * 设置列举对象时的起始位置 + * + * @param marker 起始位置标识 + */ + public void setMarker(String marker) { + this.marker = marker; + } + + /** + * 获取列举对象的最大条目数 + * + * @return 列举对象的最大条目数 + */ + public int getMaxKeys() { + return maxKeys; + } + + /** + * 设置列举对象的最大条目数 + * + * @param maxKeys 列举对象的最大条目数 + */ + public void setMaxKeys(int maxKeys) { + this.maxKeys = maxKeys; + } + + /** + * 获取用于对对象名进行分组的字符 + * + * @return 分组字符 + */ + public String getDelimiter() { + return delimiter; + } + + @Override + public String toString() { + return "ListContentSummaryRequest [bucketName=" + bucketName + ", prefix=" + prefix + ", marker=" + marker + + ", maxKeys=" + maxKeys + ", delimiter=" + delimiter + ", listTimeout=" + listTimeout + "]"; + } + + public int getListTimeout() { + return listTimeout; + } + + public void setListTimeout(int listTimeout) { + this.listTimeout = listTimeout; + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/ListContentSummaryResult.java b/app/src/main/java/com/obs/services/model/fs/ListContentSummaryResult.java new file mode 100644 index 0000000..77cae28 --- /dev/null +++ b/app/src/main/java/com/obs/services/model/fs/ListContentSummaryResult.java @@ -0,0 +1,221 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + *

    + * http://www.apache.org/licenses/LICENSE-2.0 + *

    + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.services.model.fs; + +import com.obs.services.model.HeaderResponse; + +import java.util.ArrayList; +import java.util.List; + +/** + * Response to a request for obtaining folder contentSummary + * + */ +public class ListContentSummaryResult extends HeaderResponse { + private List folderContentSummaries; + + private String bucketName; + + private boolean truncated; + + private String prefix; + + private String marker; + + private int maxKeys; + + private String delimiter; + + private String nextMarker; + + private String location; + + @Deprecated + //CHECKSTYLE:OFF + public ListContentSummaryResult(List folderContentSummaries, String bucketName, + boolean truncated, String prefix, String marker, int maxKeys, String delimiter, String nextMarker, + String location) { + super(); + this.folderContentSummaries = folderContentSummaries; + this.bucketName = bucketName; + this.truncated = truncated; + this.prefix = prefix; + this.marker = marker; + this.maxKeys = maxKeys; + this.delimiter = delimiter; + this.nextMarker = nextMarker; + this.location = location; + } + + private ListContentSummaryResult(Builder builder) { + super(); + this.folderContentSummaries = builder.folderContentSummaries; + this.bucketName = builder.bucketName; + this.truncated = builder.truncated; + this.prefix = builder.prefix; + this.marker = builder.marker; + this.maxKeys = builder.maxKeys; + this.delimiter = builder.delimiter; + this.nextMarker = builder.nextMarker; + this.location = builder.location; + } + + public static final class Builder { + private List folderContentSummaries; + private String bucketName; + private boolean truncated; + private String prefix; + private String marker; + private int maxKeys; + private String delimiter; + private String nextMarker; + private String location; + + public Builder folderContentSummaries(List folderContentSummaries) { + this.folderContentSummaries = folderContentSummaries; + return this; + } + + public Builder bucketName(String bucketName) { + this.bucketName = bucketName; + return this; + } + + public Builder truncated(boolean truncated) { + this.truncated = truncated; + return this; + } + + public Builder prefix(String prefix) { + this.prefix = prefix; + return this; + } + + public Builder marker(String marker) { + this.marker = marker; + return this; + } + + public Builder maxKeys(int maxKeys) { + this.maxKeys = maxKeys; + return this; + } + + public Builder delimiter(String delimiter) { + this.delimiter = delimiter; + return this; + } + + public Builder nextMarker(String nextMarker) { + this.nextMarker = nextMarker; + return this; + } + + public Builder location(String location) { + this.location = location; + return this; + } + + public ListContentSummaryResult builder() { + return new ListContentSummaryResult(this); + } + } + + /** + * 获取下次请求的起始位置 + * @return 下次请求的起始位置标识 + */ + public String getNextMarker() { + return nextMarker; + } + + /** + * 获取桶内目录统计信息列表 + * @return 桶内目录统计信息列表 + */ + public List getFolderContentSummaries() { + if (this.folderContentSummaries == null) { + this.folderContentSummaries = new ArrayList(); + } + return folderContentSummaries; + } + + /** + * 获取桶名 + * + * @return 桶名 + */ + public String getBucketName() { + return bucketName; + } + + /** + * 判断查询结果列表是否被截断。true表示截断,本次没有返回全部结果;false表示未截断,本次已经返回了全部结果。 + * @return 截断标识 + */ + public boolean isTruncated() { + return truncated; + } + + /** + * 获取列举目录统计信息请求中的对象名前缀 + * @return 请求中的对象名前缀 + */ + public String getPrefix() { + return prefix; + } + + /** + * 获取列举目录统计信息请求中的起始位置 + * @return 请求中的起始位置标识 + */ + public String getMarker() { + return marker; + } + + /** + * 获取列举目录统计信息的最大条目数 + * @return 列举目录统计信息的最大条目数 + */ + public int getMaxKeys() { + return maxKeys; + } + + /** + * 获取列举目录统计信息时请求中的分组字符 + * + * @return 分组字符 + */ + public String getDelimiter() { + return delimiter; + } + + /** + * 获取桶的区域位置 + * @return 桶的区域位置 + */ + public String getLocation() { + return location; + } + + @Override + public String toString() { + return "ContentSummaryResult [folderContentSummaries=" + folderContentSummaries + ", bucketName=" + bucketName + + ", truncated=" + truncated + ", prefix=" + prefix + ", marker=" + marker + ", maxKeys=" + maxKeys + + ", delimiter=" + delimiter + + ", nextMarker=" + nextMarker + ", location=" + location + "]"; + } + +} diff --git a/app/src/main/java/com/obs/services/model/fs/NewBucketRequest.java b/app/src/main/java/com/obs/services/model/fs/NewBucketRequest.java index 641ed2e..e92aacc 100644 --- a/app/src/main/java/com/obs/services/model/fs/NewBucketRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/NewBucketRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.model.CreateBucketRequest; @@ -19,29 +20,32 @@ * Parameters in a bucket creation request * */ -public class NewBucketRequest extends CreateBucketRequest{ - - public NewBucketRequest() { - super(); - } +public class NewBucketRequest extends CreateBucketRequest { - /** - * Constructor - * @param bucketName Bucket name - * @param location Bucket location - */ - public NewBucketRequest(String bucketName, String location) { - super(bucketName, location); - } + public NewBucketRequest() { + super(); + } - /** - * Constructor - * @param bucketName Bucket name - */ - public NewBucketRequest(String bucketName) { - super(bucketName); - } - -} + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param location + * Bucket location + */ + public NewBucketRequest(String bucketName, String location) { + super(bucketName, location); + } + /** + * Constructor + * + * @param bucketName + * Bucket name + */ + public NewBucketRequest(String bucketName) { + super(bucketName); + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/NewFileRequest.java b/app/src/main/java/com/obs/services/model/fs/NewFileRequest.java index 3dd5a5e..42a9cea 100644 --- a/app/src/main/java/com/obs/services/model/fs/NewFileRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/NewFileRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import java.io.File; @@ -19,44 +20,61 @@ import com.obs.services.model.PutObjectRequest; /** - * Parameters in a file creation request + * Parameters in a file creation request * */ -public class NewFileRequest extends PutObjectRequest{ - - public NewFileRequest() { - super(); - } - - /** - * Constructor - * @param bucketName Bucket name - * @param objectKey File name - * @param file Local path to the file - */ - public NewFileRequest(String bucketName, String objectKey, File file) { - super(bucketName, objectKey, file); - } - - /** - * Constructor - * @param bucketName Bucket name - * @param objectKey File name - * @param input Data stream to be uploaded - */ - public NewFileRequest(String bucketName, String objectKey, InputStream input) { - super(bucketName, objectKey, input); - } - - /** - * Constructor - * @param bucketName Bucket name - * @param objectKey File name - */ - public NewFileRequest(String bucketName, String objectKey) { - super(bucketName, objectKey); - } - -} +public class NewFileRequest extends PutObjectRequest { + + public NewFileRequest() { + } + + public NewFileRequest(String bucketName) { + this.bucketName = bucketName; + } + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param file + * Local path to the file + */ + public NewFileRequest(String bucketName, String objectKey, File file) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.file = file; + } + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param input + * Data stream to be uploaded + */ + public NewFileRequest(String bucketName, String objectKey, InputStream input) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.input = input; + } + + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + */ + public NewFileRequest(String bucketName, String objectKey) { + this.bucketName = bucketName; + this.objectKey = objectKey; + } + +} diff --git a/app/src/main/java/com/obs/services/model/fs/NewFolderRequest.java b/app/src/main/java/com/obs/services/model/fs/NewFolderRequest.java index b35165b..1edd860 100644 --- a/app/src/main/java/com/obs/services/model/fs/NewFolderRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/NewFolderRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.model.PutObjectBasicRequest; @@ -19,23 +20,22 @@ * Parameters in a folder creation request * */ -public class NewFolderRequest extends PutObjectBasicRequest{ - - public NewFolderRequest() { - super(); - } - - /** - * Constructor - * @param bucketName Bucket name - * @param objectKey Folder name - */ - public NewFolderRequest(String bucketName, String objectKey) { - super(); - this.bucketName = bucketName; - this.objectKey = objectKey; - } - -} +public class NewFolderRequest extends PutObjectBasicRequest { + + public NewFolderRequest() { + } + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * Folder name + */ + public NewFolderRequest(String bucketName, String objectKey) { + this.bucketName = bucketName; + this.objectKey = objectKey; + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/ObsFSAttribute.java b/app/src/main/java/com/obs/services/model/fs/ObsFSAttribute.java index ed20dd2..6df9d11 100644 --- a/app/src/main/java/com/obs/services/model/fs/ObsFSAttribute.java +++ b/app/src/main/java/com/obs/services/model/fs/ObsFSAttribute.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.model.ObjectMetadata; @@ -19,22 +20,25 @@ * File or folder properties * */ -public class ObsFSAttribute extends ObjectMetadata{ - private int mode = -1; - - /** - * Query the file or folder type. - * @return File or folder type - */ - public int getMode() { - return mode; - } +public class ObsFSAttribute extends ObjectMetadata { + private int mode = -1; + + /** + * Query the file or folder type. + * + * @return File or folder type + */ + public int getMode() { + return mode; + } - /** - * Set the file or folder type. - * @param mode File or folder type - */ - public void setMode(int mode) { - this.mode = mode; - } + /** + * Set the file or folder type. + * + * @param mode + * File or folder type + */ + public void setMode(int mode) { + this.mode = mode; + } } diff --git a/app/src/main/java/com/obs/services/model/fs/ObsFSBucket.java b/app/src/main/java/com/obs/services/model/fs/ObsFSBucket.java index a09c7fb..b4478b5 100644 --- a/app/src/main/java/com/obs/services/model/fs/ObsFSBucket.java +++ b/app/src/main/java/com/obs/services/model/fs/ObsFSBucket.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import java.io.File; @@ -26,121 +27,148 @@ * Buckets that support the file interface * */ -public class ObsFSBucket{ - - protected ObsClient innerClient; - +public class ObsFSBucket { + + protected ObsClient innerClient; + private String bucketName; - + private String location; - - public ObsFSBucket(String bucketName, String location) { - super(); - this.bucketName = bucketName; - this.location = location; - } - - /** - * Set status of the file gateway feature for a bucket. - * @param status Status of the file gateway feature - * @return Response to the request for setting status of the file gateway feature for the bucket - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public HeaderResponse setFSStatus(FSStatusEnum status) throws ObsException{ - this.checkInternalClient(); - return this.innerClient.setBucketFSStatus(new SetBucketFSStatusRequest(this.bucketName, status)); - } - - /** - * Create a folder. - * @param folderName Folder name - * @return Folders in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSFolder newFolder(String folderName) throws ObsException{ - this.checkInternalClient(); - return this.innerClient.newFolder(new NewFolderRequest(this.bucketName, folderName)); - } - - /** - * Create a file. - * @param fileName File name - * @param input File input stream - * @param metadata File properties - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSFile newFile(String fileName, InputStream input, ObjectMetadata metadata) throws ObsException{ - this.checkInternalClient(); - NewFileRequest request = new NewFileRequest(this.bucketName, fileName); - request.setInput(input); - return this.innerClient.newFile(request); - } - - /** - * Create a file. - * @param fileName File name - * @param input File input stream - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSFile newFile(String fileName, InputStream input) throws ObsException{ - return this.newFile(fileName, input, null); - } - - /** - * Create a file. - * @param fileName File name - * @param file Local path to the file - * @param metadata File properties - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSFile newFile(String fileName, File file, ObjectMetadata metadata) throws ObsException{ - this.checkInternalClient(); - NewFileRequest request = new NewFileRequest(this.bucketName, fileName); - request.setFile(file); - return this.innerClient.newFile(request); - } - - /** - * Create a file. - * @param fileName File name - * @param file Local path to the file - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSFile newFile(String fileName, File file) throws ObsException{ - return this.newFile(fileName, file, null); - } + + public ObsFSBucket(String bucketName, String location) { + super(); + this.bucketName = bucketName; + this.location = location; + } + + protected void setInnerClient(ObsClient innerClient) { + this.innerClient = innerClient; + } + + /** + * Set status of the file gateway feature for a bucket. + * + * @param status + * Status of the file gateway feature + * @return Response to the request for setting status of the file gateway + * feature for the bucket + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public HeaderResponse setFSStatus(FSStatusEnum status) throws ObsException { + this.checkInternalClient(); + return this.innerClient.setBucketFSStatus(new SetBucketFSStatusRequest(this.bucketName, status)); + } + + /** + * Create a folder. + * + * @param folderName + * Folder name + * @return Folders in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSFolder newFolder(String folderName) throws ObsException { + this.checkInternalClient(); + return this.innerClient.newFolder(new NewFolderRequest(this.bucketName, folderName)); + } + + /** + * Create a file. + * + * @param fileName + * File name + * @param input + * File input stream + * @param metadata + * File properties + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSFile newFile(String fileName, InputStream input, ObjectMetadata metadata) throws ObsException { + this.checkInternalClient(); + NewFileRequest request = new NewFileRequest(this.bucketName, fileName); + request.setInput(input); + return this.innerClient.newFile(request); + } + + /** + * Create a file. + * + * @param fileName + * File name + * @param input + * File input stream + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSFile newFile(String fileName, InputStream input) throws ObsException { + return this.newFile(fileName, input, null); + } + + /** + * Create a file. + * + * @param fileName + * File name + * @param file + * Local path to the file + * @param metadata + * File properties + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSFile newFile(String fileName, File file, ObjectMetadata metadata) throws ObsException { + this.checkInternalClient(); + NewFileRequest request = new NewFileRequest(this.bucketName, fileName); + request.setFile(file); + return this.innerClient.newFile(request); + } + + /** + * Create a file. + * + * @param fileName + * File name + * @param file + * Local path to the file + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSFile newFile(String fileName, File file) throws ObsException { + return this.newFile(fileName, file, null); + } /** * Obtain the bucket name. * * @return Bucket name */ - public String getBucketName() - { + public String getBucketName() { return bucketName; } /** * Obtain the bucket location. + * * @return Bucket location */ - public String getLocation() - { + public String getLocation() { return location; } protected void checkInternalClient() { - ServiceUtils.asserParameterNotNull(this.innerClient, "ObsClient is null"); - } - - @Override - protected void finalize() throws Throwable { - this.innerClient = null; - super.finalize(); - } - + ServiceUtils.assertParameterNotNull(this.innerClient, "ObsClient is null"); + } } diff --git a/app/src/main/java/com/obs/services/model/fs/ObsFSFile.java b/app/src/main/java/com/obs/services/model/fs/ObsFSFile.java index 7c602a2..35c8445 100644 --- a/app/src/main/java/com/obs/services/model/fs/ObsFSFile.java +++ b/app/src/main/java/com/obs/services/model/fs/ObsFSFile.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import java.io.File; @@ -23,150 +24,197 @@ * Files in a bucket that supports the file interface * */ -public class ObsFSFile extends ObsFSFolder{ - - public ObsFSFile(String bucketName, String objectKey, String etag, String versionId, StorageClassEnum storageClass, - String objectUrl) { - super(bucketName, objectKey, etag, versionId, storageClass, objectUrl); - } - - /** - * Obtains file properties. - * @return File properties - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSAttribute attribute() throws ObsException { - return super.attribute(); - } - - /** - * Obtain the file content. - * @return Response to the request for obtaining file content - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ReadFileResult read() throws ObsException{ - this.checkInternalClient(); - ReadFileRequest request = new ReadFileRequest(this.getBucketName(), this.getObjectKey()); - return this.innerClient.readFile(request); - } - - /** - * Obtain the file content. - * @param rangeStart Start position for reading file content - * @param rangeEnd End position for reading file content - * @return ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ReadFileResult read(long rangeStart, long rangeEnd) throws ObsException{ - this.checkInternalClient(); - ReadFileRequest request = new ReadFileRequest(this.getBucketName(), this.getObjectKey()); - request.setRangeStart(rangeStart); - request.setRangeEnd(rangeEnd); - return this.innerClient.readFile(request); - } - - /** - * Write data to a file. - * @param file Local path to the file - * @param position Start position for writing data to a file - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSFile write(File file, long position) throws ObsException { - this.checkInternalClient(); - WriteFileRequest request = new WriteFileRequest(this.getBucketName(), this.getObjectKey(), file, position); - return this.innerClient.writeFile(request); - } - - /** - * Write data to a file. - * @param file Local path to the file - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSFile write(File file) throws ObsException{ - return this.write(file, 0); - } - - /** - * Write data to a file. - * @param input Data stream to be uploaded - * @param position Start position for writing data to a file - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSFile write(InputStream input, long position) throws ObsException { - this.checkInternalClient(); - WriteFileRequest request = new WriteFileRequest(this.getBucketName(), this.getObjectKey(), input, position); - return this.innerClient.writeFile(request); - } - - /** - * Append data to a file. - * @param file Local path to the file - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSFile append(File file) throws ObsException{ - this.checkInternalClient(); - WriteFileRequest request = new WriteFileRequest(this.getBucketName(), this.getObjectKey(), file); - return this.innerClient.appendFile(request); - } - - /** - * Append data to a file. - * @param input Data stream to be uploaded - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSFile append(InputStream input) throws ObsException{ - this.checkInternalClient(); - WriteFileRequest request = new WriteFileRequest(this.getBucketName(), this.getObjectKey(), input); - return this.innerClient.appendFile(request); - } - - /** - * Write data to a file. - * @param input Data stream to be uploaded - * @return Files in the bucket that supports the file interface - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSFile write(InputStream input) throws ObsException{ - return this.write(input, 0); - } - - /** - * Rename a file. - * @param newName New file name - * @return Response to the request for renaming a file - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public RenameResult rename(String newName) throws ObsException{ - this.checkInternalClient(); - RenameRequest request = new RenameRequest(this.getBucketName(), this.getObjectKey(), newName); - return this.innerClient.renameFile(request); - } - - /** - * Truncate a file. - * @param newLength File size after the truncation - * @return Response to the request for truncating a file - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public TruncateFileResult truncate(long newLength) throws ObsException{ - this.checkInternalClient(); - TruncateFileRequest request = new TruncateFileRequest(this.getBucketName(), this.getObjectKey(), newLength); - return this.innerClient.truncateFile(request); - } - - /** - * Delete a file. - * @return Response to the request for deleting a file - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public DropFileResult drop() throws ObsException { - this.checkInternalClient(); - DropFileRequest request = new DropFileRequest(this.getBucketName(), this.getObjectKey(), this.getVersionId()); - return this.innerClient.dropFile(request); - } +public class ObsFSFile extends ObsFSFolder { + + public ObsFSFile(String bucketName, String objectKey, String etag, String versionId, StorageClassEnum storageClass, + String objectUrl) { + super(bucketName, objectKey, etag, versionId, storageClass, objectUrl); + } + + /** + * Obtains file properties. + * + * @return File properties + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSAttribute attribute() throws ObsException { + return super.attribute(); + } + + /** + * Obtain the file content. + * + * @return Response to the request for obtaining file content + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ReadFileResult read() throws ObsException { + this.checkInternalClient(); + ReadFileRequest request = new ReadFileRequest(this.getBucketName(), this.getObjectKey()); + return this.innerClient.readFile(request); + } + + /** + * Obtain the file content. + * + * @param rangeStart + * Start position for reading file content + * @param rangeEnd + * End position for reading file content + * @return ObsException OBS SDK self-defined exception, thrown when the + * interface fails to be called or access to OBS fails + */ + public ReadFileResult read(long rangeStart, long rangeEnd) throws ObsException { + this.checkInternalClient(); + ReadFileRequest request = new ReadFileRequest(this.getBucketName(), this.getObjectKey()); + request.setRangeStart(rangeStart); + request.setRangeEnd(rangeEnd); + return this.innerClient.readFile(request); + } + + /** + * Write data to a file. + * + * @param file + * Local path to the file + * @param position + * Start position for writing data to a file + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSFile write(File file, long position) throws ObsException { + this.checkInternalClient(); + WriteFileRequest request = new WriteFileRequest(this.getBucketName(), this.getObjectKey(), file, position); + return this.innerClient.writeFile(request); + } + + /** + * Write data to a file. + * + * @param file + * Local path to the file + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSFile write(File file) throws ObsException { + return this.write(file, 0); + } + + /** + * Write data to a file. + * + * @param input + * Data stream to be uploaded + * @param position + * Start position for writing data to a file + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSFile write(InputStream input, long position) throws ObsException { + this.checkInternalClient(); + WriteFileRequest request = new WriteFileRequest(this.getBucketName(), this.getObjectKey(), input, position); + return this.innerClient.writeFile(request); + } + + /** + * Append data to a file. + * + * @param file + * Local path to the file + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSFile append(File file) throws ObsException { + this.checkInternalClient(); + WriteFileRequest request = new WriteFileRequest(this.getBucketName(), this.getObjectKey(), file); + return this.innerClient.appendFile(request); + } + + /** + * Append data to a file. + * + * @param input + * Data stream to be uploaded + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSFile append(InputStream input) throws ObsException { + this.checkInternalClient(); + WriteFileRequest request = new WriteFileRequest(this.getBucketName(), this.getObjectKey(), input); + return this.innerClient.appendFile(request); + } + + /** + * Write data to a file. + * + * @param input + * Data stream to be uploaded + * @return Files in the bucket that supports the file interface + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSFile write(InputStream input) throws ObsException { + return this.write(input, 0); + } + + /** + * Rename a file. + * + * @param newName + * New file name + * @return Response to the request for renaming a file + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public RenameResult rename(String newName) throws ObsException { + this.checkInternalClient(); + RenameRequest request = new RenameRequest(this.getBucketName(), this.getObjectKey(), newName); + return this.innerClient.renameFile(request); + } + + /** + * Truncate a file. + * + * @param newLength + * File size after the truncation + * @return Response to the request for truncating a file + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public TruncateFileResult truncate(long newLength) throws ObsException { + this.checkInternalClient(); + TruncateFileRequest request = new TruncateFileRequest(this.getBucketName(), this.getObjectKey(), newLength); + return this.innerClient.truncateFile(request); + } + + /** + * Delete a file. + * + * @return Response to the request for deleting a file + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public DropFileResult drop() throws ObsException { + this.checkInternalClient(); + DropFileRequest request = new DropFileRequest(this.getBucketName(), this.getObjectKey(), this.getVersionId()); + return this.innerClient.dropFile(request); + } } diff --git a/app/src/main/java/com/obs/services/model/fs/ObsFSFolder.java b/app/src/main/java/com/obs/services/model/fs/ObsFSFolder.java index fe7d41c..4b12fc5 100644 --- a/app/src/main/java/com/obs/services/model/fs/ObsFSFolder.java +++ b/app/src/main/java/com/obs/services/model/fs/ObsFSFolder.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.ObsClient; @@ -24,55 +25,62 @@ * Folders in a bucket that supports the file interface * */ -public class ObsFSFolder extends PutObjectResult{ - - protected ObsClient innerClient; - - public ObsFSFolder(String bucketName, String objectKey, String etag, String versionId, StorageClassEnum storageClass, - String objectUrl) { - super(bucketName, objectKey, etag, versionId, storageClass, objectUrl); - } +public class ObsFSFolder extends PutObjectResult { + + protected ObsClient innerClient; + + public ObsFSFolder(String bucketName, String objectKey, String etag, String versionId, + StorageClassEnum storageClass, String objectUrl) { + super(bucketName, objectKey, etag, versionId, storageClass, objectUrl); + } + + protected void setInnerClient(ObsClient innerClient) { + this.innerClient = innerClient; + } + + /** + * Obtain folder properties. + * + * @return Folder properties + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public ObsFSAttribute attribute() throws ObsException { + this.checkInternalClient(); + return (ObsFSAttribute) this.innerClient.getObjectMetadata(this.getBucketName(), this.getObjectKey()); + } + + /** + * Rename a folder. + * + * @param newName + * New folder name + * @return Response to the request for renaming a folder + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public RenameResult rename(String newName) throws ObsException { + this.checkInternalClient(); + RenameRequest request = new RenameRequest(this.getBucketName(), this.getObjectKey(), newName); + return this.innerClient.renameFolder(request); + } - /** - * Obtain folder properties. - * @return Folder properties - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public ObsFSAttribute attribute() throws ObsException{ - this.checkInternalClient(); - return (ObsFSAttribute)this.innerClient.getObjectMetadata(this.getBucketName(), this.getObjectKey()); - } - - /** - * Rename a folder. - * @param newName New folder name - * @return Response to the request for renaming a folder - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public RenameResult rename(String newName) throws ObsException{ - this.checkInternalClient(); - RenameRequest request = new RenameRequest(this.getBucketName(), this.getObjectKey(), newName); - return this.innerClient.renameFolder(request); - } - - /** - * Delete a folder. - * @throws ObsException OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - public TaskProgressStatus dropFolder() throws ObsException { - this.checkInternalClient(); - DropFolderRequest request = new DropFolderRequest(this.getBucketName(), this.getObjectKey()); - return this.innerClient.dropFolder(request); - } - - protected void checkInternalClient() { - ServiceUtils.asserParameterNotNull(this.innerClient, "ObsClient is null"); - } + /** + * Delete a folder. + * + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + public TaskProgressStatus dropFolder() throws ObsException { + this.checkInternalClient(); + DropFolderRequest request = new DropFolderRequest(this.getBucketName(), this.getObjectKey()); + return this.innerClient.dropFolder(request); + } - @Override - protected void finalize() throws Throwable { - this.innerClient = null; - super.finalize(); - } - + protected void checkInternalClient() { + ServiceUtils.assertParameterNotNull(this.innerClient, "ObsClient is null"); + } } diff --git a/app/src/main/java/com/obs/services/model/fs/ReadFileRequest.java b/app/src/main/java/com/obs/services/model/fs/ReadFileRequest.java index 498523c..b993c4a 100644 --- a/app/src/main/java/com/obs/services/model/fs/ReadFileRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/ReadFileRequest.java @@ -11,29 +11,30 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.model.GetObjectRequest; /** - * Parameters in a request for obtaining the file content + * Parameters in a request for obtaining the file content * */ -public class ReadFileRequest extends GetObjectRequest{ - - public ReadFileRequest() { - super(); - } +public class ReadFileRequest extends GetObjectRequest { - /** - * Constructor - * @param bucketName Bucket name - * @param objectKey File name - */ - public ReadFileRequest(String bucketName, String objectKey) { - super(bucketName, objectKey); - } - -} + public ReadFileRequest() { + } + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + */ + public ReadFileRequest(String bucketName, String objectKey) { + super(bucketName, objectKey); + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/ReadFileResult.java b/app/src/main/java/com/obs/services/model/fs/ReadFileResult.java index 52ae6a7..9e89d64 100644 --- a/app/src/main/java/com/obs/services/model/fs/ReadFileResult.java +++ b/app/src/main/java/com/obs/services/model/fs/ReadFileResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.model.ObsObject; diff --git a/app/src/main/java/com/obs/services/model/fs/RenameRequest.java b/app/src/main/java/com/obs/services/model/fs/RenameRequest.java index 5afe78d..03fc213 100644 --- a/app/src/main/java/com/obs/services/model/fs/RenameRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/RenameRequest.java @@ -11,96 +11,61 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; +import com.obs.services.model.BaseObjectRequest; +import com.obs.services.model.HttpMethodEnum; + /** * Parameters in a request for renaming a file or folder * */ -public class RenameRequest { - - private String bucketName; - - private String objectKey; - - private String newObjectKey; - - - public RenameRequest() { - - } - - /** - * Constructor - * @param bucketName Bucket name - * @param objectKey File or folder name - * @param newObjectKey New file or folder name - */ - public RenameRequest(String bucketName, String objectKey, String newObjectKey) { - super(); - this.bucketName = bucketName; - this.objectKey = objectKey; - this.newObjectKey = newObjectKey; - } +public class RenameRequest extends BaseObjectRequest { - /** - * Obtain the bucket name. - * - * @return Bucket name - */ - public String getBucketName() { - return bucketName; + httpMethod = HttpMethodEnum.POST; } - - /** - * Set the bucket name. - * - * @param bucketName Bucket name - */ - public void setBucketName(String bucketName) - { - this.bucketName = bucketName; - } - - /** - * Obtain the file or folder name. - * - * @return File or folder name - */ - public String getObjectKey() - { - return objectKey; + + private String newObjectKey; + + public RenameRequest() { + } - + /** - * Set the file or folder name. + * Constructor * - * @param objectKey File or folder name - * + * @param bucketName + * Bucket name + * @param objectKey + * File or folder name + * @param newObjectKey + * New file or folder name */ - public void setObjectKey(String objectKey) - { + public RenameRequest(String bucketName, String objectKey, String newObjectKey) { + this.bucketName = bucketName; this.objectKey = objectKey; + this.newObjectKey = newObjectKey; } /** * Obtain the new file or folder name. + * * @return New file or folder name */ - public String getNewObjectKey() { - return newObjectKey; - } + public String getNewObjectKey() { + return newObjectKey; + } - /** + /** * Set the new file or folder name. - * @param newObjectKey New file or folder name + * + * @param newObjectKey + * New file or folder name */ - public void setNewObjectKey(String newObjectKey) { - this.newObjectKey = newObjectKey; - } - - -} - + public void setNewObjectKey(String newObjectKey) { + this.newObjectKey = newObjectKey; + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/RenameResult.java b/app/src/main/java/com/obs/services/model/fs/RenameResult.java index cebcd20..f323a04 100644 --- a/app/src/main/java/com/obs/services/model/fs/RenameResult.java +++ b/app/src/main/java/com/obs/services/model/fs/RenameResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.model.HeaderResponse; diff --git a/app/src/main/java/com/obs/services/model/fs/SetBucketFSStatusRequest.java b/app/src/main/java/com/obs/services/model/fs/SetBucketFSStatusRequest.java index 3b81d71..f143d9b 100644 --- a/app/src/main/java/com/obs/services/model/fs/SetBucketFSStatusRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/SetBucketFSStatusRequest.java @@ -11,67 +11,59 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; +import com.obs.services.model.BaseBucketRequest; + /** - * Parameters in a request for setting status of the file gateway feature for a bucket + * Parameters in a request for setting status of the file gateway feature for a + * bucket * */ -public class SetBucketFSStatusRequest { - - private String bucketName; - - private FSStatusEnum status; - - public SetBucketFSStatusRequest() { - - } - - /** - * Constructor - * @param bucketName Bucket name - * @param status Status of the file gateway feature - */ - public SetBucketFSStatusRequest(String bucketName, FSStatusEnum status) { - super(); - this.bucketName = bucketName; - this.setStatus(status); - } +public class SetBucketFSStatusRequest extends BaseBucketRequest { - /** - * Obtain the bucket name. - * @return Bucket name - */ - public String getBucketName() { - return bucketName; - } + private FSStatusEnum status; - /** - * Set the bucket name. - * @param bucketName Bucket name - */ - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } + public SetBucketFSStatusRequest() { - /** - * Obtain the status of the file gateway feature of a bucket. - * @return Status of the file gateway feature - */ - public FSStatusEnum getStatus() { - return status; - } + } - /** - * Set status of the file gateway feature for a bucket. - * @param status Status of the file gateway feature - */ - public void setStatus(FSStatusEnum status) { - this.status = status; - } + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param status + * Status of the file gateway feature + */ + public SetBucketFSStatusRequest(String bucketName, FSStatusEnum status) { + this.bucketName = bucketName; + this.setStatus(status); + } + /** + * Obtain the status of the file gateway feature of a bucket. + * + * @return Status of the file gateway feature + */ + public FSStatusEnum getStatus() { + return status; + } - -} - + /** + * Set status of the file gateway feature for a bucket. + * + * @param status + * Status of the file gateway feature + */ + public void setStatus(FSStatusEnum status) { + this.status = status; + } + @Override + public String toString() { + return "SetBucketFSStatusRequest [status=" + status + ", getBucketName()=" + getBucketName() + + ", isRequesterPays()=" + isRequesterPays() + "]"; + } +} diff --git a/app/src/main/java/com/obs/services/model/fs/TruncateFileRequest.java b/app/src/main/java/com/obs/services/model/fs/TruncateFileRequest.java index 3bd2fac..c40f027 100644 --- a/app/src/main/java/com/obs/services/model/fs/TruncateFileRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/TruncateFileRequest.java @@ -11,93 +11,59 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; +import com.obs.services.model.BaseObjectRequest; +import com.obs.services.model.HttpMethodEnum; + /** * Parameters in a request for truncating a file * */ -public class TruncateFileRequest { - - private String bucketName; - - private String objectKey; - - private long newLength; - - public TruncateFileRequest() { - - } - - /** - * Constructor - * @param bucketName Bucket name - * @param objectKey File name - * @param newLength File size after the truncation - */ - public TruncateFileRequest(String bucketName, String objectKey, long newLength) { - super(); - this.bucketName = bucketName; - this.objectKey = objectKey; - this.newLength = newLength; - } +public class TruncateFileRequest extends BaseObjectRequest { + { + httpMethod = HttpMethodEnum.PUT; + } + private long newLength; - /** - * Obtain the bucket name. - * - * @return Bucket name - */ - public String getBucketName() { - return bucketName; - } + public TruncateFileRequest() { + } - /** - * Set the bucket name. - * - * @param bucketName - * Bucket name - */ - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } + /** + * Constructor + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param newLength + * File size after the truncation + */ + public TruncateFileRequest(String bucketName, String objectKey, long newLength) { + this.bucketName = bucketName; + this.objectKey = objectKey; + this.newLength = newLength; + } - /** - * Obtain the file name. - * - * @return File name - */ - public String getObjectKey() { - return objectKey; - } + /** + * Obtain the file size after the truncation. + * + * @return File size after the truncation + */ + public long getNewLength() { + return newLength; + } - /** - * Set the file name. - * - * @param objectKey - * File name - * - */ - public void setObjectKey(String objectKey) { - this.objectKey = objectKey; - } - - /** - * Obtain the file size after the truncation. - * @return File size after the truncation - */ - public long getNewLength() { - return newLength; - } - - /** - * Set the post-truncation file size. - * @param newLength File size after the truncation - */ - public void setNewLength(long newLength) { - this.newLength = newLength; - } + /** + * Set the post-truncation file size. + * + * @param newLength + * File size after the truncation + */ + public void setNewLength(long newLength) { + this.newLength = newLength; + } } - - diff --git a/app/src/main/java/com/obs/services/model/fs/TruncateFileResult.java b/app/src/main/java/com/obs/services/model/fs/TruncateFileResult.java index 0617be2..f92a408 100644 --- a/app/src/main/java/com/obs/services/model/fs/TruncateFileResult.java +++ b/app/src/main/java/com/obs/services/model/fs/TruncateFileResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import com.obs.services.model.HeaderResponse; diff --git a/app/src/main/java/com/obs/services/model/fs/WriteFileRequest.java b/app/src/main/java/com/obs/services/model/fs/WriteFileRequest.java index 040519a..f964565 100644 --- a/app/src/main/java/com/obs/services/model/fs/WriteFileRequest.java +++ b/app/src/main/java/com/obs/services/model/fs/WriteFileRequest.java @@ -11,83 +11,102 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.obs.services.model.fs; import java.io.File; import java.io.InputStream; import com.obs.services.model.AppendObjectRequest; +import com.obs.services.model.HttpMethodEnum; /** * Parameters in a request for writing data to a file * */ -public class WriteFileRequest extends AppendObjectRequest{ +public class WriteFileRequest extends AppendObjectRequest { + + { + httpMethod = HttpMethodEnum.PUT; + } + + public WriteFileRequest() { + } - public WriteFileRequest() { - super(); - } - - /** + /** * Constructor - * @param bucketName Bucket name - * @param objectKey File name + * + * @param bucketName + * Bucket name + * @param objectKey + * File name */ - public WriteFileRequest(String bucketName, String objectKey) - { + public WriteFileRequest(String bucketName, String objectKey) { this.bucketName = bucketName; this.objectKey = objectKey; } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey File name - * @param file Local path to the file + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param file + * Local path to the file */ - public WriteFileRequest(String bucketName, String objectKey, File file) - { + public WriteFileRequest(String bucketName, String objectKey, File file) { this(bucketName, objectKey); this.file = file; } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey File name - * @param file Local path to the file - * @param position Start position for writing data to a file + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param file + * Local path to the file + * @param position + * Start position for writing data to a file */ - public WriteFileRequest(String bucketName, String objectKey, File file, long position) - { - this(bucketName, objectKey, file); - this.position = position; + public WriteFileRequest(String bucketName, String objectKey, File file, long position) { + this(bucketName, objectKey, file); + this.position = position; } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey File name - * @param input Data stream to be uploaded + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param input + * Data stream to be uploaded */ - public WriteFileRequest(String bucketName, String objectKey, InputStream input) - { - this(bucketName, objectKey); - this.input = input; + public WriteFileRequest(String bucketName, String objectKey, InputStream input) { + this(bucketName, objectKey); + this.input = input; } - + /** * Constructor - * @param bucketName Bucket name - * @param objectKey File name - * @param input Data stream to be uploaded - * @param position Start position for writing data to a file + * + * @param bucketName + * Bucket name + * @param objectKey + * File name + * @param input + * Data stream to be uploaded + * @param position + * Start position for writing data to a file */ - public WriteFileRequest(String bucketName, String objectKey, InputStream input, long position) - { - this(bucketName, objectKey, input); - this.position = position; + public WriteFileRequest(String bucketName, String objectKey, InputStream input, long position) { + this(bucketName, objectKey, input); + this.position = position; } } - - diff --git a/app/src/main/java/com/oef/services/IOefClient.java b/app/src/main/java/com/oef/services/IOefClient.java index 50a860a..aefeb42 100644 --- a/app/src/main/java/com/oef/services/IOefClient.java +++ b/app/src/main/java/com/oef/services/IOefClient.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services; import java.io.IOException; @@ -28,67 +29,77 @@ * */ public interface IOefClient { - /** + /** * Close OEF client and release connection resources. - * @throws IOException I/O exception. This exception is thrown when resources fail to be closed. + * + * @throws IOException + * I/O exception. This exception is thrown when resources fail + * to be closed. + */ + void close() throws IOException; + + /** + * Configure an asynchronous policy. + * + * @param bucketName + * Bucket name + * @param request + * Asynchronous policy + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + HeaderResponse putExtensionPolicy(final String bucketName, final PutExtensionPolicyRequest request) + throws ObsException; + + /** + * Query an asynchronous policy + * + * @param bucketName + * Bucket name + * @return ExtensionPolicyResult + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails */ - void close() throws IOException; - - /** - * Configure an asynchronous policy. - * @param bucketName - * Bucket name - * @param request - * Asynchronous policy - * @return Common response headers - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - HeaderResponse putExtensionPolicy(final String bucketName, final PutExtensionPolicyRequest request) - throws ObsException; + QueryExtensionPolicyResult queryExtensionPolicy(final String bucketName) throws ObsException; - /** - * Query an asynchronous policy - * @param bucketName - * Bucket name - * @return ExtensionPolicyResult - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - QueryExtensionPolicyResult queryExtensionPolicy(final String bucketName) - throws ObsException; - - /** - * Delete an asynchronous policy - * @param bucketName - * Bucket name - * @return Common response headers - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - HeaderResponse deleteExtensionPolicy(final String bucketName) - throws ObsException; - /** - * Create an asynchronous fetch job - * @param request - * Asynchronous fetch job - * @return CreateAsynchFetchJobsResult - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - CreateAsynchFetchJobsResult createFetchJob(final CreateAsyncFetchJobsRequest request) - throws ObsException; - - /** - * Query an asynchronous fetch job - * @param bucketName - * Bucket name - * @param jobId - * Job ID - * @return QueryAsynchFetchJobsResult - * @throws ObsException - * OBS SDK self-defined exception, thrown when the interface fails to be called or access to OBS fails - */ - QueryAsynchFetchJobsResult queryFetchJob(final String bucketName, final String jobId) - throws ObsException; + /** + * Delete an asynchronous policy + * + * @param bucketName + * Bucket name + * @return Common response headers + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + HeaderResponse deleteExtensionPolicy(final String bucketName) throws ObsException; + + /** + * Create an asynchronous fetch job + * + * @param request + * Asynchronous fetch job + * @return CreateAsynchFetchJobsResult + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + CreateAsynchFetchJobsResult createFetchJob(final CreateAsyncFetchJobsRequest request) throws ObsException; + + /** + * Query an asynchronous fetch job + * + * @param bucketName + * Bucket name + * @param jobId + * Job ID + * @return QueryAsynchFetchJobsResult + * @throws ObsException + * OBS SDK self-defined exception, thrown when the interface + * fails to be called or access to OBS fails + */ + QueryAsynchFetchJobsResult queryFetchJob(final String bucketName, final String jobId) throws ObsException; } diff --git a/app/src/main/java/com/oef/services/OefClient.java b/app/src/main/java/com/oef/services/OefClient.java index c9ecd8b..0e861a1 100644 --- a/app/src/main/java/com/oef/services/OefClient.java +++ b/app/src/main/java/com/oef/services/OefClient.java @@ -11,245 +11,192 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services; -import java.util.Date; import com.obs.log.ILogger; -import com.obs.log.InterfaceLogBean; import com.obs.log.LoggerBuilder; import com.obs.services.ObsClient; import com.obs.services.ObsConfiguration; import com.obs.services.exception.ObsException; -import com.obs.services.internal.Constants; import com.obs.services.internal.ServiceException; -import com.obs.services.internal.utils.AccessLoggerUtils; import com.obs.services.internal.utils.JSONChange; import com.obs.services.internal.utils.ServiceUtils; -import com.obs.services.model.AuthTypeEnum; import com.obs.services.model.HeaderResponse; import com.oef.services.model.CreateAsyncFetchJobsRequest; import com.oef.services.model.CreateAsynchFetchJobsResult; import com.oef.services.model.PutExtensionPolicyRequest; -import com.oef.services.model.QueryExtensionPolicyResult; import com.oef.services.model.QueryAsynchFetchJobsResult; +import com.oef.services.model.QueryExtensionPolicyResult; /** * OEF client */ public class OefClient extends ObsClient implements IOefClient { - private static final ILogger ILOG = LoggerBuilder.getLogger(OefClient.class); - - - /** - * Constructor - * - * @param endPoint - * OEF service address - * - */ - public OefClient(String endPoint) { - super(endPoint); - } - - /** - * Constructor - * - * @param config - * Configuration parameters of OEF client - * - */ - public OefClient(ObsConfiguration config) { - super(config); - } - - /** - * Constructor - * - * @param accessKey - * Access key ID - * @param secretKey - * Secret access key - * @param endPoint - * OEF service address - * - */ - public OefClient(String accessKey, String secretKey, String endPoint) { - super(accessKey, secretKey, endPoint); - } - - /** - * Constructor - * - * @param accessKey - * Access key ID - * @param secretKey - * Secret access key - * @param config - * Configuration parameters of OEF client - * - */ - public OefClient(String accessKey, String secretKey, ObsConfiguration config) { - super(accessKey, secretKey, config); - } - - /** - * Constructor - * - * @param accessKey - * AK in the temporary access keys - * @param secretKey - * SK in the temporary access keys - * @param securityToken - * Security token - * @param endPoint - * OEF service address - * - */ - public OefClient(String accessKey, String secretKey, String securityToken, String endPoint) { - super(accessKey, secretKey, securityToken, endPoint); - } - - /** - * Constructor - * - * @param accessKey - * AK in the temporary access keys - * @param secretKey - * SK in the temporary access keys - * @param securityToken - * Security Token - * @param config - * Configuration parameters of OEF client - * - */ - public OefClient(String accessKey, String secretKey, String securityToken, ObsConfiguration config) { - super(accessKey, secretKey, securityToken, config); - } - - - @Override - public HeaderResponse putExtensionPolicy(final String bucketName, final PutExtensionPolicyRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(bucketName, "bucket is null"); - ServiceUtils.asserParameterNotNull(request, "policy is null"); - if(null == request.getCompress() && null == request.getFetch() && null == request.getTranscode()) { - throw new IllegalArgumentException("putExtensionPolicy failed: compress, fetch and transcode cannot be empty at the same time"); - } - return this.doActionWithResult("putExtensionPolicy", bucketName, new ActionCallbackWithResult() { - @Override - public HeaderResponse action() throws ServiceException { - String policy = JSONChange.objToJson(request); - return OefClient.this.setExtensionPolicyImpl(bucketName, policy); - } - }); - } - - @Override - public QueryExtensionPolicyResult queryExtensionPolicy(final String bucketName) throws ObsException { - ServiceUtils.asserParameterNotNull(bucketName, "bucket is null"); - return this.doActionWithResult("queryExtensionPolicy", bucketName, new ActionCallbackWithResult() { - @Override - public QueryExtensionPolicyResult action() throws ServiceException { - return OefClient.this.queryExtensionPolicyImpl(bucketName); - - } - }); - } - - @Override - public HeaderResponse deleteExtensionPolicy(final String bucketName) throws ObsException { - ServiceUtils.asserParameterNotNull(bucketName, "bucket is null"); - return this.doActionWithResult("deleteExtensionPolicy", bucketName, new ActionCallbackWithResult() { - @Override - public HeaderResponse action() throws ServiceException { - return OefClient.this.deleteExtensionPolicyImpl(bucketName); - } - }); - } - - @Override - public CreateAsynchFetchJobsResult createFetchJob(final CreateAsyncFetchJobsRequest request) throws ObsException { - ServiceUtils.asserParameterNotNull(request.getBucketName(), "bucket is null"); - ServiceUtils.asserParameterNotNull(request, "policy is null"); - ServiceUtils.asserParameterNotNull(request.getUrl(), "url is null"); - if(request.getCallBackUrl() != null) { - ServiceUtils.asserParameterNotNull(request.getCallBackBody(), "callbackbody is null when callbackurl is not null"); - } - return this.doActionWithResult("CreateFetchJob", request.getBucketName(), new ActionCallbackWithResult() { - @Override - public CreateAsynchFetchJobsResult action() throws ServiceException { - String policy = JSONChange.objToJson(request); - return OefClient.this.createFetchJobImpl(request.getBucketName(), policy); - - } - }); - } - - @Override - public QueryAsynchFetchJobsResult queryFetchJob(final String bucketName, final String jobId) throws ObsException{ - ServiceUtils.asserParameterNotNull(bucketName, "bucket is null"); - ServiceUtils.asserParameterNotNull(jobId, "jobId is null"); - return this.doActionWithResult("queryFetchJob", bucketName, new ActionCallbackWithResult() { - @Override - public QueryAsynchFetchJobsResult action() throws ServiceException { - return OefClient.this.queryFetchJobImpl(bucketName, jobId); - - } - }); - } - - private abstract class ActionCallbackWithResult { - - abstract T action() throws ServiceException; - - void authTypeNegotiate(String bucketName) throws ServiceException { - AuthTypeEnum authTypeEnum = OefClient.this.getApiVersion(bucketName); - OefClient.this.getProviderCredentials().setThreadLocalAuthType(authTypeEnum); - } - } - - private T doActionWithResult(String action, String bucketName, ActionCallbackWithResult callback) - throws ObsException { - if (! this.isCname()) { - ServiceUtils.asserParameterNotNull(bucketName, "bucketName is null"); + private static final ILogger ILOG = LoggerBuilder.getLogger(OefClient.class); + + /** + * Constructor + * + * @param endPoint + * OEF service address + * + */ + public OefClient(String endPoint) { + super(endPoint); + } + + /** + * Constructor + * + * @param config + * Configuration parameters of OEF client + * + */ + public OefClient(ObsConfiguration config) { + super(config); + } + + /** + * Constructor + * + * @param accessKey + * Access key ID + * @param secretKey + * Secret access key + * @param endPoint + * OEF service address + * + */ + public OefClient(String accessKey, String secretKey, String endPoint) { + super(accessKey, secretKey, endPoint); + } + + /** + * Constructor + * + * @param accessKey + * Access key ID + * @param secretKey + * Secret access key + * @param config + * Configuration parameters of OEF client + * + */ + public OefClient(String accessKey, String secretKey, ObsConfiguration config) { + super(accessKey, secretKey, config); + } + + /** + * Constructor + * + * @param accessKey + * AK in the temporary access keys + * @param secretKey + * SK in the temporary access keys + * @param securityToken + * Security token + * @param endPoint + * OEF service address + * + */ + public OefClient(String accessKey, String secretKey, String securityToken, String endPoint) { + super(accessKey, secretKey, securityToken, endPoint); + } + + /** + * Constructor + * + * @param accessKey + * AK in the temporary access keys + * @param secretKey + * SK in the temporary access keys + * @param securityToken + * Security Token + * @param config + * Configuration parameters of OEF client + * + */ + public OefClient(String accessKey, String secretKey, String securityToken, ObsConfiguration config) { + super(accessKey, secretKey, securityToken, config); + } + + @Override + public HeaderResponse putExtensionPolicy(final String bucketName, final PutExtensionPolicyRequest request) + throws ObsException { + ServiceUtils.assertParameterNotNull(bucketName, "bucket is null"); + ServiceUtils.assertParameterNotNull(request, "policy is null"); + if (null == request.getCompress() && null == request.getFetch() && null == request.getTranscode()) { + throw new IllegalArgumentException( + "putExtensionPolicy failed: compress, fetch and transcode cannot be empty at the same time"); } - InterfaceLogBean reqBean = new InterfaceLogBean(action, this.getEndpoint(), ""); - try { - long start = System.currentTimeMillis(); - if (this.isAuthTypeNegotiation()) { - callback.authTypeNegotiate(bucketName); - } - T ret = callback.action(); - reqBean.setRespTime(new Date()); - reqBean.setResultCode(Constants.RESULTCODE_SUCCESS); - if (ILOG.isInfoEnabled()) { - ILOG.info(reqBean); - } - if (ILOG.isInfoEnabled()) { - ILOG.info("ObsClient [" + action + "] cost " + (System.currentTimeMillis() - start) + " ms"); - } - return ret; - } catch (ServiceException e) { - - ObsException ex = ServiceUtils.changeFromServiceException(e); - if(ex.getResponseCode() >= 400 && ex.getResponseCode() < 500) { - if(ILOG.isWarnEnabled()) { - reqBean.setRespTime(new Date()); - reqBean.setResultCode(String.valueOf(e.getResponseCode())); - ILOG.warn(reqBean); - } - }else if (ILOG.isErrorEnabled()) { - reqBean.setRespTime(new Date()); - reqBean.setResultCode(String.valueOf(ex.getResponseCode())); - ILOG.error(reqBean); - } - throw ex; - } finally { - if(this.isAuthTypeNegotiation()) { - this.getProviderCredentials().removeThreadLocalAuthType(); - } - AccessLoggerUtils.printLog(); - } - } - + return this.doActionWithResult("putExtensionPolicy", bucketName, + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + String policy = JSONChange.objToJson(request); + return OefClient.this.setExtensionPolicyImpl(bucketName, policy); + } + }); + } + + @Override + public QueryExtensionPolicyResult queryExtensionPolicy(final String bucketName) throws ObsException { + ServiceUtils.assertParameterNotNull(bucketName, "bucket is null"); + return this.doActionWithResult("queryExtensionPolicy", bucketName, + new ActionCallbackWithResult() { + @Override + public QueryExtensionPolicyResult action() throws ServiceException { + return OefClient.this.queryExtensionPolicyImpl(bucketName); + + } + }); + } + + @Override + public HeaderResponse deleteExtensionPolicy(final String bucketName) throws ObsException { + ServiceUtils.assertParameterNotNull(bucketName, "bucket is null"); + return this.doActionWithResult("deleteExtensionPolicy", bucketName, + new ActionCallbackWithResult() { + @Override + public HeaderResponse action() throws ServiceException { + return OefClient.this.deleteExtensionPolicyImpl(bucketName); + } + }); + } + + @Override + public CreateAsynchFetchJobsResult createFetchJob(final CreateAsyncFetchJobsRequest request) throws ObsException { + ServiceUtils.assertParameterNotNull(request.getBucketName(), "bucket is null"); + ServiceUtils.assertParameterNotNull(request, "policy is null"); + ServiceUtils.assertParameterNotNull(request.getUrl(), "url is null"); + if (request.getCallBackUrl() != null) { + ServiceUtils.assertParameterNotNull(request.getCallBackBody(), + "callbackbody is null when callbackurl is not null"); + } + return this.doActionWithResult("CreateFetchJob", request.getBucketName(), + new ActionCallbackWithResult() { + @Override + public CreateAsynchFetchJobsResult action() throws ServiceException { + String policy = JSONChange.objToJson(request); + return OefClient.this.createFetchJobImpl(request.getBucketName(), policy); + + } + }); + } + + @Override + public QueryAsynchFetchJobsResult queryFetchJob(final String bucketName, final String jobId) throws ObsException { + ServiceUtils.assertParameterNotNull(bucketName, "bucket is null"); + ServiceUtils.assertParameterNotNull(jobId, "jobId is null"); + return this.doActionWithResult("queryFetchJob", bucketName, + new ActionCallbackWithResult() { + @Override + public QueryAsynchFetchJobsResult action() throws ServiceException { + return OefClient.this.queryFetchJobImpl(bucketName, jobId); + + } + }); + } } diff --git a/app/src/main/java/com/oef/services/model/CompressBean.java b/app/src/main/java/com/oef/services/model/CompressBean.java index aa283a4..de79365 100644 --- a/app/src/main/java/com/oef/services/model/CompressBean.java +++ b/app/src/main/java/com/oef/services/model/CompressBean.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services.model; import com.fasterxml.jackson.annotation.JsonProperty; @@ -20,61 +21,69 @@ * */ public class CompressBean { - @JsonProperty(value = "status") - private String status; - - @JsonProperty(value = "agency") - private String agency; - + @JsonProperty(value = "status") + private String status; + + @JsonProperty(value = "agency") + private String agency; + public CompressBean() { - + + } + + /** + * Constructor + * + * @param status + * Policy status + * @param agency + * IAM agency + */ + public CompressBean(String status, String agency) { + this.status = status; + this.agency = agency; } - + /** - * Constructor - * @param status Policy status - * @param agency IAM agency - */ - public CompressBean(String status, String agency) { - this.status = status; - this.agency = agency; - } - - /** - * Obtain the policy status. - * @return Policy status - */ - public String getStatus() { - return status; - } - - /** - * Set the policy status. - * @param status Policy status - */ - public void setStatus(String status) { - this.status = status; - } - - /** - * Obtain the IAM agency. - * @return IAM agency - */ - public String getAgency() { - return agency; - } - - /** - * Set the IAM agency. - * @param agency IAM agency - */ - public void setAgency(String agency) { - this.agency = agency; - } - - @Override - public String toString() - { + * Obtain the policy status. + * + * @return Policy status + */ + public String getStatus() { + return status; + } + + /** + * Set the policy status. + * + * @param status + * Policy status + */ + public void setStatus(String status) { + this.status = status; + } + + /** + * Obtain the IAM agency. + * + * @return IAM agency + */ + public String getAgency() { + return agency; + } + + /** + * Set the IAM agency. + * + * @param agency + * IAM agency + */ + public void setAgency(String agency) { + this.agency = agency; + } + + @Override + public String toString() { return "CompressBean [status=" + status + ", agency=" + agency + "]"; } } diff --git a/app/src/main/java/com/oef/services/model/CreateAsyncFetchJobsRequest.java b/app/src/main/java/com/oef/services/model/CreateAsyncFetchJobsRequest.java index 944c47f..898a961 100644 --- a/app/src/main/java/com/oef/services/model/CreateAsyncFetchJobsRequest.java +++ b/app/src/main/java/com/oef/services/model/CreateAsyncFetchJobsRequest.java @@ -11,12 +11,12 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services.model; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import com.fasterxml.jackson.annotation.JsonProperty; -import com.obs.services.internal.Constants; import com.obs.services.internal.ServiceException; import com.obs.services.internal.utils.ServiceUtils; @@ -25,240 +25,268 @@ * */ public class CreateAsyncFetchJobsRequest { - @JsonProperty(value = "url") - private String url; - - @JsonProperty(value = "bucket") - private String bucket; - - @JsonProperty(value = "host") - private String host; - - @JsonProperty(value = "key") - private String key; - - @JsonProperty(value = "md5") - private String md5; - - @JsonProperty(value = "callbackurl") - private String callBackUrl; - - @JsonProperty(value = "callbackbody") - private String callBackBody; - - @JsonProperty(value = "callbackbodytype") - private String callBackBodyType; - - @JsonProperty(value = "callbackhost") - private String callBackHost; - - @JsonProperty(value = "file_type") - private String fileType; - - @JsonProperty(value = "ignore_same_key") - private boolean ignoreSameKey; - - - - public CreateAsyncFetchJobsRequest(){ - } - - /** - * Constructor - * @param url URL to be fetched. You can set multiple URLs and separate them with semicolons (;). - * @param bucket Bucket name - */ - public CreateAsyncFetchJobsRequest(String url, String bucket){ - this.setUrl(url); - this.setBucketName(bucket); - } - - /** - * Obtain the URL. - * @return url - */ - public String getUrl() { - return url; - } - - /** - * Set the URL. - * @param url - */ - public void setUrl(String url) { - this.url = url; - } - - /** - * Obtain the bucket name. - * @return Bucket name - */ - public String getBucketName() { - return bucket; - } - - /** - * Set the bucket name. - * @param bucket Bucket name - */ - public void setBucketName(String bucket) { - this.bucket = bucket; - } - - /** - * Obtain the host used for downloading data from a specified URL. - * @return Host used for downloading data from a specified URL - */ - public String getHost() { - return host; - } - - /** - * Set the host used for downloading data from a specified URL. - * @param host Host used for downloading data from a specified URL - */ - public void setHost(String host) { - this.host = host; - } - - /** - * Obtain the object name. - * @return Object name - */ - public String getObjectKey() { - return key; - } - - /** - * Set the object name. - * @param key Object name - */ - public void setObjectKey(String key) { - this.key = key; - } - - /** - * Obtain the MD5 file. - * @return File MD5 - */ - public String getMd5() { - return md5; - } - - /** - * Set the MD5 file. - * @param md5 - */ - public void setMd5(String md5) { - this.md5 = md5; - } - - /** - * Obtain the callback URL. - * @return Callback URL. - */ - public String getCallBackUrl() { - return callBackUrl; - } - - /** - * Set the callback URL. - * @param callBackUrl Callback URL. - */ - public void setCallBackUrl(String callBackUrl) { - this.callBackUrl = callBackUrl; - } - - /** - * Obtain the callback body. - * @return Callback body - */ - public String getCallBackBody() { - return callBackBody; - } - - /** - * Set the callback body (Base64 encoded). - * @param callBackBody Callback body - * @throws ServiceException - */ - public void setCallBackBody(String callBackBody) throws ServiceException { - try { - this.callBackBody = ServiceUtils.toBase64(callBackBody.getBytes(Constants.DEFAULT_ENCODING)); - } catch (UnsupportedEncodingException e) { - throw new ServiceException("Unable to get bytes from canonical string", e); - }; - } - - /** - * Obtain the content type of the callback body. - * @return Content type of the callback body - */ - public String getCallbackBodyType() { - return callBackBodyType; - } - - /** - * Set the content type of the callback body. - * @param callBackBodyType Content type of the callback body - */ - public void setCallbackBodyType(String callBackBodyType) { - this.callBackBodyType = callBackBodyType; - } - - /** - * Obtain the host used for callback. - * @return Host used for callback - */ - public String getCallBackHost() { - return callBackHost; - } - - /** - * Set the host used for callback. - * @param callBackHost Host used for callback - */ - public void setCallBackHost(String callBackHost) { - this.callBackHost = callBackHost; - } - - /** - * Obtain the storage file type - * @return Storage file type - */ - public String getFileType() { - return fileType; - } - - /** - * Set the storage file type - * @param fileType Storage file type - */ - public void setFileType(String fileType) { - this.fileType = fileType; - } - - /** - * If a file with the same name already exists in the namespace, the current fetch job is canceled. - * @return ignoreSameKey - */ - public boolean isignoreSameKey() { - return ignoreSameKey; - } - - /** - * @param ignoreSameKey - */ - public void setignoreSameKey(boolean ignoreSameKey) { - this.ignoreSameKey = ignoreSameKey; - } - - @Override - public String toString() { - return "CreateAsyncFetchJobsRequest [url=" + url + ", bucket=" + bucket + ", host=" + host + ", key=" + key - + ", md5=" + md5 + ", callBackUrl=" + callBackUrl + ", callBackBody=" + callBackBody - + ", callBackBodyType=" + callBackBodyType + ", callBackHost=" + callBackHost - + ", fileType=" + fileType + ", ignoreSameKey=" + ignoreSameKey + "]"; - } + @JsonProperty(value = "url") + private String url; + + @JsonProperty(value = "bucket") + private String bucket; + + @JsonProperty(value = "host") + private String host; + + @JsonProperty(value = "key") + private String key; + + @JsonProperty(value = "md5") + private String md5; + + @JsonProperty(value = "callbackurl") + private String callBackUrl; + + @JsonProperty(value = "callbackbody") + private String callBackBody; + + @JsonProperty(value = "callbackbodytype") + private String callBackBodyType; + + @JsonProperty(value = "callbackhost") + private String callBackHost; + + @JsonProperty(value = "file_type") + private String fileType; + + @JsonProperty(value = "ignore_same_key") + private boolean ignoreSameKey; + + public CreateAsyncFetchJobsRequest() { + } + + /** + * Constructor + * + * @param url + * URL to be fetched. You can set multiple URLs and separate them + * with semicolons (;). + * @param bucket + * Bucket name + */ + public CreateAsyncFetchJobsRequest(String url, String bucket) { + this.setUrl(url); + this.setBucketName(bucket); + } + + /** + * Obtain the URL. + * + * @return url + */ + public String getUrl() { + return url; + } + + /** + * Set the URL. + * + * @param url + */ + public void setUrl(String url) { + this.url = url; + } + + /** + * Obtain the bucket name. + * + * @return Bucket name + */ + public String getBucketName() { + return bucket; + } + + /** + * Set the bucket name. + * + * @param bucket + * Bucket name + */ + public void setBucketName(String bucket) { + this.bucket = bucket; + } + + /** + * Obtain the host used for downloading data from a specified URL. + * + * @return Host used for downloading data from a specified URL + */ + public String getHost() { + return host; + } + + /** + * Set the host used for downloading data from a specified URL. + * + * @param host + * Host used for downloading data from a specified URL + */ + public void setHost(String host) { + this.host = host; + } + + /** + * Obtain the object name. + * + * @return Object name + */ + public String getObjectKey() { + return key; + } + + /** + * Set the object name. + * + * @param key + * Object name + */ + public void setObjectKey(String key) { + this.key = key; + } + + /** + * Obtain the MD5 file. + * + * @return File MD5 + */ + public String getMd5() { + return md5; + } + + /** + * Set the MD5 file. + * + * @param md5 + */ + public void setMd5(String md5) { + this.md5 = md5; + } + + /** + * Obtain the callback URL. + * + * @return Callback URL. + */ + public String getCallBackUrl() { + return callBackUrl; + } + + /** + * Set the callback URL. + * + * @param callBackUrl + * Callback URL. + */ + public void setCallBackUrl(String callBackUrl) { + this.callBackUrl = callBackUrl; + } + + /** + * Obtain the callback body. + * + * @return Callback body + */ + public String getCallBackBody() { + return callBackBody; + } + + /** + * Set the callback body (Base64 encoded). + * + * @param callBackBody + * Callback body + * @throws ServiceException + */ + public void setCallBackBody(String callBackBody) throws ServiceException { + this.callBackBody = ServiceUtils.toBase64(callBackBody.getBytes(StandardCharsets.UTF_8)); + } + + /** + * Obtain the content type of the callback body. + * + * @return Content type of the callback body + */ + public String getCallbackBodyType() { + return callBackBodyType; + } + + /** + * Set the content type of the callback body. + * + * @param callBackBodyType + * Content type of the callback body + */ + public void setCallbackBodyType(String callBackBodyType) { + this.callBackBodyType = callBackBodyType; + } + + /** + * Obtain the host used for callback. + * + * @return Host used for callback + */ + public String getCallBackHost() { + return callBackHost; + } + + /** + * Set the host used for callback. + * + * @param callBackHost + * Host used for callback + */ + public void setCallBackHost(String callBackHost) { + this.callBackHost = callBackHost; + } + + /** + * Obtain the storage file type + * + * @return Storage file type + */ + public String getFileType() { + return fileType; + } + + /** + * Set the storage file type + * + * @param fileType + * Storage file type + */ + public void setFileType(String fileType) { + this.fileType = fileType; + } + + /** + * If a file with the same name already exists in the namespace, the current + * fetch job is canceled. + * + * @return ignoreSameKey + */ + public boolean isignoreSameKey() { + return ignoreSameKey; + } + + /** + * @param ignoreSameKey + */ + public void setignoreSameKey(boolean ignoreSameKey) { + this.ignoreSameKey = ignoreSameKey; + } + + @Override + public String toString() { + return "CreateAsyncFetchJobsRequest [url=" + url + ", bucket=" + bucket + ", host=" + host + ", key=" + key + + ", md5=" + md5 + ", callBackUrl=" + callBackUrl + ", callBackBody=" + callBackBody + + ", callBackBodyType=" + callBackBodyType + ", callBackHost=" + callBackHost + ", fileType=" + fileType + + ", ignoreSameKey=" + ignoreSameKey + "]"; + } } diff --git a/app/src/main/java/com/oef/services/model/CreateAsynchFetchJobsResult.java b/app/src/main/java/com/oef/services/model/CreateAsynchFetchJobsResult.java index 9b7591f..2c937fd 100644 --- a/app/src/main/java/com/oef/services/model/CreateAsynchFetchJobsResult.java +++ b/app/src/main/java/com/oef/services/model/CreateAsynchFetchJobsResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services.model; import com.fasterxml.jackson.annotation.JsonProperty; @@ -21,60 +22,72 @@ * */ public class CreateAsynchFetchJobsResult extends HeaderResponse { - @JsonProperty(value = "id") - private String id; + @JsonProperty(value = "id") + private String id; @JsonProperty(value = "Wait") - private int Wait; - - public CreateAsynchFetchJobsResult() { - - } - - /** - * Constructor - * @param id Job ID - * @param Wait Number of queuing jobs before the current job. The value 0 indicates that the current job is being executed, and the value -1 indicates that the job has been executed at least once (the retry logic may be triggered). - */ - public CreateAsynchFetchJobsResult(String id, int Wait) { - this.setId(id); - this.setWait(Wait); - } + private int wait; + + public CreateAsynchFetchJobsResult() { + + } + + /** + * Constructor + * + * @param id + * Job ID + * @param wait + * Number of queuing jobs before the current job. The value 0 + * indicates that the current job is being executed, and the + * value -1 indicates that the job has been executed at least + * once (the retry logic may be triggered). + */ + public CreateAsynchFetchJobsResult(String id, int wait) { + this.setId(id); + this.setWait(wait); + } /** * Obtain the job ID. + * * @return Job ID */ - public String getId() { - return id; - } + public String getId() { + return id; + } - /** - * Set the job ID. - * @param id Job ID - */ - public void setId(String id) { - this.id = id; - } + /** + * Set the job ID. + * + * @param id + * Job ID + */ + public void setId(String id) { + this.id = id; + } + + /** + * Obtain the number of queuing jobs. + * + * @return Number of queuing jobs + */ + public int getWait() { + return wait; + } - /** - * Obtain the number of queuing jobs. - * @return Number of queuing jobs - */ - public int getWait() { - return Wait; - } + /** + * Set the number of queuing jobs. + * + * @param wait + * Number of queuing jobs + */ + public void setWait(int wait) { + this.wait = wait; + } - /** - * Set the number of queuing jobs. - * @param Wait Number of queuing jobs - */ - public void setWait(int Wait) { - this.Wait = Wait; - } - - @Override - public String toString() { - return "CreateAsynchFetchJobsResult [id=" + id + ", Wait=" + Wait + "]"; - } + @Override + public String toString() { + return "CreateAsynchFetchJobsResult [id=" + id + ", Wait=" + wait + "]"; + } } diff --git a/app/src/main/java/com/oef/services/model/DisPolicy.java b/app/src/main/java/com/oef/services/model/DisPolicy.java index 6b21a13..5000f44 100644 --- a/app/src/main/java/com/oef/services/model/DisPolicy.java +++ b/app/src/main/java/com/oef/services/model/DisPolicy.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services.model; import java.util.ArrayList; @@ -18,41 +19,41 @@ import com.fasterxml.jackson.annotation.JsonProperty; public class DisPolicy { - @JsonProperty(value = "rules") - private ArrayList rules; - - public DisPolicy() { - - } - - public DisPolicy(ArrayList rules) { - this.rules = rules; - } - - public ArrayList getRules() { - return rules; - } - - public void setRules(ArrayList rules) { - this.rules = rules; - } - - public void addRules(DisPolicyBean rule) { - if(this.rules == null) { - this.rules = new ArrayList(); - } - this.rules.add(rule); - } - - @Override - public String toString() { - StringBuilder strRules = new StringBuilder(); - if(rules != null) { - for(DisPolicyBean rule : rules) { - strRules.append(rule.toString()); - strRules.append("\n"); - } - } - return "DisPolicy["+ strRules +"]"; - } + @JsonProperty(value = "rules") + private ArrayList rules; + + public DisPolicy() { + + } + + public DisPolicy(ArrayList rules) { + this.rules = rules; + } + + public ArrayList getRules() { + return rules; + } + + public void setRules(ArrayList rules) { + this.rules = rules; + } + + public void addRules(DisPolicyBean rule) { + if (this.rules == null) { + this.rules = new ArrayList(); + } + this.rules.add(rule); + } + + @Override + public String toString() { + StringBuilder strRules = new StringBuilder(); + if (rules != null) { + for (DisPolicyBean rule : rules) { + strRules.append(rule.toString()); + strRules.append("\n"); + } + } + return "DisPolicy[" + strRules + "]"; + } } diff --git a/app/src/main/java/com/oef/services/model/DisPolicyBean.java b/app/src/main/java/com/oef/services/model/DisPolicyBean.java index 059b9d3..33368b0 100644 --- a/app/src/main/java/com/oef/services/model/DisPolicyBean.java +++ b/app/src/main/java/com/oef/services/model/DisPolicyBean.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services.model; import java.util.ArrayList; @@ -18,109 +19,108 @@ import com.fasterxml.jackson.annotation.JsonProperty; public class DisPolicyBean { - @JsonProperty(value = "id") - private String id; - - @JsonProperty(value = "stream") - private String stream; - - @JsonProperty(value = "project") - private String project; - - @JsonProperty(value = "events") - private ArrayList events; - - @JsonProperty(value = "prefix") - private String prefix; - - @JsonProperty(value = "suffix") - private String suffix; - - @JsonProperty(value = "agency") - private String agency; - - public DisPolicyBean() { - - } - - public DisPolicyBean(String id, String stream, String project, ArrayList events, String agency) { - this.setId(id); - this.setStream(stream); - this.setProject(project); - this.setEvents(events); - this.setAgency(agency); - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getStream() { - return stream; - } - - public void setStream(String stream) { - this.stream = stream; - } - - public String getProject() { - return project; - } - - public void setProject(String project) { - this.project = project; - } - - public ArrayList getEvents() { - return events; - } - - public void setEvents(ArrayList events) { - this.events = events; - } - - public String getPrefix() { - return prefix; - } - - public void setPrefix(String prefix) { - this.prefix = prefix; - } - - public String getSuffix() { - return suffix; - } - - public void setSuffix(String suffix) { - this.suffix = suffix; - } - - public String getAgency() { - return agency; - } - - public void setAgency(String agency) { - this.agency = agency; - } - - @Override - public String toString() { - StringBuilder strEvents = new StringBuilder(); - if(events != null) { - for(String event : events) { - strEvents.append(event.toString()); - strEvents.append(","); - } - if(strEvents.length() > 0) { - strEvents = strEvents.deleteCharAt(strEvents.length() - 1); - } - } - return "DisPolicyBean [id = " + id + ", stream = " + stream - + ", project = " + project + ", events = " + strEvents - + ", prefix = " + prefix + ", suffix = " + suffix + ", agency = " + agency +"]"; - } + @JsonProperty(value = "id") + private String id; + + @JsonProperty(value = "stream") + private String stream; + + @JsonProperty(value = "project") + private String project; + + @JsonProperty(value = "events") + private ArrayList events; + + @JsonProperty(value = "prefix") + private String prefix; + + @JsonProperty(value = "suffix") + private String suffix; + + @JsonProperty(value = "agency") + private String agency; + + public DisPolicyBean() { + + } + + public DisPolicyBean(String id, String stream, String project, ArrayList events, String agency) { + this.setId(id); + this.setStream(stream); + this.setProject(project); + this.setEvents(events); + this.setAgency(agency); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getStream() { + return stream; + } + + public void setStream(String stream) { + this.stream = stream; + } + + public String getProject() { + return project; + } + + public void setProject(String project) { + this.project = project; + } + + public ArrayList getEvents() { + return events; + } + + public void setEvents(ArrayList events) { + this.events = events; + } + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public String getSuffix() { + return suffix; + } + + public void setSuffix(String suffix) { + this.suffix = suffix; + } + + public String getAgency() { + return agency; + } + + public void setAgency(String agency) { + this.agency = agency; + } + + @Override + public String toString() { + StringBuilder strEvents = new StringBuilder(); + if (events != null) { + for (String event : events) { + strEvents.append(event); + strEvents.append(","); + } + if (strEvents.length() > 0) { + strEvents = strEvents.deleteCharAt(strEvents.length() - 1); + } + } + return "DisPolicyBean [id = " + id + ", stream = " + stream + ", project = " + project + ", events = " + + strEvents + ", prefix = " + prefix + ", suffix = " + suffix + ", agency = " + agency + "]"; + } } diff --git a/app/src/main/java/com/oef/services/model/FetchBean.java b/app/src/main/java/com/oef/services/model/FetchBean.java index 168c03f..aefd0f3 100644 --- a/app/src/main/java/com/oef/services/model/FetchBean.java +++ b/app/src/main/java/com/oef/services/model/FetchBean.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services.model; import com.fasterxml.jackson.annotation.JsonProperty; @@ -20,61 +21,69 @@ * */ public class FetchBean { - @JsonProperty(value = "status") - private String status; - - @JsonProperty(value = "agency") - private String agency; - - public FetchBean() { - - } - - /** - * Constructor - * @param status Policy status - * @param agency IAM agency - */ - public FetchBean(String status, String agency) { - this.status = status; - this.agency = agency; - } - - /** - * Obtain the policy status. - * @return Policy status - */ - public String getStatus() { - return status; - } - - /** - * Set the policy status. - * @param status Policy status - */ - public void setStatus(String status) { - this.status = status; - } - - /** - * Obtain the IAM agency. - * @return IAM agency - */ - public String getAgency() { - return agency; - } - - /** - * Set the IAM agency. - * @param agency IAM agency - */ - public void setAgency(String agency) { - this.agency = agency; - } - - @Override - public String toString() - { + @JsonProperty(value = "status") + private String status; + + @JsonProperty(value = "agency") + private String agency; + + public FetchBean() { + + } + + /** + * Constructor + * + * @param status + * Policy status + * @param agency + * IAM agency + */ + public FetchBean(String status, String agency) { + this.status = status; + this.agency = agency; + } + + /** + * Obtain the policy status. + * + * @return Policy status + */ + public String getStatus() { + return status; + } + + /** + * Set the policy status. + * + * @param status + * Policy status + */ + public void setStatus(String status) { + this.status = status; + } + + /** + * Obtain the IAM agency. + * + * @return IAM agency + */ + public String getAgency() { + return agency; + } + + /** + * Set the IAM agency. + * + * @param agency + * IAM agency + */ + public void setAgency(String agency) { + this.agency = agency; + } + + @Override + public String toString() { return "FetchBean [status=" + status + ", agency=" + agency + "]"; } diff --git a/app/src/main/java/com/oef/services/model/GetDisPolicyResult.java b/app/src/main/java/com/oef/services/model/GetDisPolicyResult.java index 6b4e325..f5580da 100644 --- a/app/src/main/java/com/oef/services/model/GetDisPolicyResult.java +++ b/app/src/main/java/com/oef/services/model/GetDisPolicyResult.java @@ -3,21 +3,21 @@ import com.obs.services.model.HeaderResponse; public class GetDisPolicyResult extends HeaderResponse { - private DisPolicy policy; - - public GetDisPolicyResult() { - - } - - public GetDisPolicyResult(DisPolicy policy) { - this.setPolicy(policy); - } + private DisPolicy policy; - public DisPolicy getPolicy() { - return policy; - } + public GetDisPolicyResult() { - public void setPolicy(DisPolicy policy) { - this.policy = policy; - } + } + + public GetDisPolicyResult(DisPolicy policy) { + this.setPolicy(policy); + } + + public DisPolicy getPolicy() { + return policy; + } + + public void setPolicy(DisPolicy policy) { + this.policy = policy; + } } diff --git a/app/src/main/java/com/oef/services/model/PutExtensionPolicyRequest.java b/app/src/main/java/com/oef/services/model/PutExtensionPolicyRequest.java index 86088cb..be7331f 100644 --- a/app/src/main/java/com/oef/services/model/PutExtensionPolicyRequest.java +++ b/app/src/main/java/com/oef/services/model/PutExtensionPolicyRequest.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services.model; import com.fasterxml.jackson.annotation.JsonProperty; @@ -20,79 +21,87 @@ * */ public class PutExtensionPolicyRequest { - @JsonProperty(value = "fetch") - private FetchBean fetch; - - @JsonProperty(value = "transcode") - private TranscodeBean transcode; - - @JsonProperty(value = "compress") - private CompressBean compress; - - public PutExtensionPolicyRequest() { + @JsonProperty(value = "fetch") + private FetchBean fetch; + + @JsonProperty(value = "transcode") + private TranscodeBean transcode; + + @JsonProperty(value = "compress") + private CompressBean compress; + + public PutExtensionPolicyRequest() { + + } - } + /** + * Obtain the content of the asynchronous fetch policy. + * + * @return Content of the asynchronous fetch policy + */ + public FetchBean getFetch() { + return fetch; + } - /** - * Obtain the content of the asynchronous fetch policy. - * @return Content of the asynchronous fetch policy - */ - public FetchBean getFetch() { - return fetch; - } + /** + * Set the content of the asynchronous fetch policy. + * + * @param fetch + * Content of the asynchronous fetch policy + */ + public void setFetch(FetchBean fetch) { + this.fetch = fetch; + } - /** - * Set the content of the asynchronous fetch policy. - * @param fetch Content of the asynchronous fetch policy - */ - public void setFetch(FetchBean fetch) { - this.fetch = fetch; - } + /** + * Obtain the content of the asynchronous transcode policy. + * + * @return Content of the asynchronous transcode policy + */ + public TranscodeBean getTranscode() { + return transcode; + } - /** - * Obtain the content of the asynchronous transcode policy. - * @return Content of the asynchronous transcode policy - */ - public TranscodeBean getTranscode() { - return transcode; - } + /** + * Set the content of the asynchronous transcode policy. + * + * @param transcode + * Content of the asynchronous transcode policy + */ + public void setTranscode(TranscodeBean transcode) { + this.transcode = transcode; + } - /** - * Set the content of the asynchronous transcode policy. - * @param transcode Content of the asynchronous transcode policy - */ - public void setTranscode(TranscodeBean transcode) { - this.transcode = transcode; - } + /** + * Obtain the content of the file compression policy. + * + * @return Content of the file compression policy + */ + public CompressBean getCompress() { + return compress; + } - /** - * Obtain the content of the file compression policy. - * @return Content of the file compression policy - */ - public CompressBean getCompress() { - return compress; - } + /** + * Set the content of the file compression policy. + * + * @param compress + * Content of the file compression policy + */ + public void setCompress(CompressBean compress) { + this.compress = compress; + } - /** - * Set the content of the file compression policy. - * @param compress Content of the file compression policy - */ - public void setCompress(CompressBean compress) { - this.compress = compress; - } - - @Override - public String toString() - { - String fetchStatus = fetch == null? null: fetch.getStatus(); - String fetchAgency = fetch == null? null: fetch.getAgency(); - String transcodeStatus = transcode == null? null: transcode.getStatus(); - String transcodeAgency = transcode == null? null: transcode.getAgency(); - String compressStatus = compress == null? null: compress.getStatus(); - String compressAgency = compress == null? null: compress.getAgency(); + @Override + public String toString() { + String fetchStatus = fetch == null ? null : fetch.getStatus(); + String fetchAgency = fetch == null ? null : fetch.getAgency(); + String transcodeStatus = transcode == null ? null : transcode.getStatus(); + String transcodeAgency = transcode == null ? null : transcode.getAgency(); + String compressStatus = compress == null ? null : compress.getStatus(); + String compressAgency = compress == null ? null : compress.getAgency(); - return "ExtensionPolicyRequest [fetch status=" + fetchStatus + ", fetch agency=" + fetchAgency - + ", transcode status=" + transcodeStatus + ", transcode agency=" + transcodeAgency - + ", compress status=" + compressStatus + ", compress agency=" + compressAgency + "]"; + return "ExtensionPolicyRequest [fetch status=" + fetchStatus + ", fetch agency=" + fetchAgency + + ", transcode status=" + transcodeStatus + ", transcode agency=" + transcodeAgency + + ", compress status=" + compressStatus + ", compress agency=" + compressAgency + "]"; } } diff --git a/app/src/main/java/com/oef/services/model/QueryAsynchFetchJobsResult.java b/app/src/main/java/com/oef/services/model/QueryAsynchFetchJobsResult.java index 92bac80..71b65f8 100644 --- a/app/src/main/java/com/oef/services/model/QueryAsynchFetchJobsResult.java +++ b/app/src/main/java/com/oef/services/model/QueryAsynchFetchJobsResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services.model; import com.fasterxml.jackson.annotation.JsonProperty; @@ -21,148 +22,176 @@ * */ public class QueryAsynchFetchJobsResult extends HeaderResponse { - @JsonProperty(value = "request_Id") - private String requestId; - - @JsonProperty(value = "err") - private String err; - - @JsonProperty(value = "code") - private String code; - - @JsonProperty(value = "status") - private String status; - - @JsonProperty(value = "wait") - private int wait; - - @JsonProperty(value = "job") - private CreateAsyncFetchJobsRequest job; - - public QueryAsynchFetchJobsResult() { - job = new CreateAsyncFetchJobsRequest(); - } - - /** - * Constructor - * @param requestId Unique ID of a request - * @param err Error description - * @param code Error code - * @param status Job status - * @param wait Number of queuing jobs before the current job. The value 0 indicates that the current job is being executed, and the value -1 indicates that the job has been executed at least once (the retry logic may be triggered). - * @param job Job details - */ - public QueryAsynchFetchJobsResult(String requestId, String err, String code, String status, int wait, CreateAsyncFetchJobsRequest job) { - this.setRequestId(requestId); - this.setErr(err); - this.setCode(code); - this.setStatus(status); - this.setWait(wait); - this.setJob(job); - } - - /** - * Obtain the unique ID of a request. - * @return Unique ID of a request - */ - public String getRequestId() { - return requestId; - } - - /** - * Set the unique ID of a request. - * @param requestId Unique ID of a request - */ - public void setRequestId(String requestId) { - this.requestId = requestId; - } - - /** - * Obtain the error description. - * @return Error description - */ - public String getErr() { - return err; - } - - /** - * Set the error description. - * @param err Error description - */ - public void setErr(String err) { - this.err = err; - } - - /** - * Obtain the error code. - * @return Error code - */ - public String getCode() { - return code; - } - - /** - * Set the error code. - * @param code Error code - */ - public void setCode(String code) { - this.code = code; - } - - /** - * Obtain the job status. - * @return Job status - */ - public String getStatus() { - return status; - } - - /** - * Set the job status. - * @param status Job status - */ - public void setStatus(String status) { - this.status = status; - } - - /** - * Obtain the number of queuing jobs. - * @return Number of queuing jobs - */ - public int getWait() { - return wait; - } - - /** - * Set the number of queuing jobs. - * @param wait Number of queuing jobs - */ - public void setWait(int wait) { - this.wait = wait; - } - - /** - * Obtain the job details. - * @return Job details - */ - public CreateAsyncFetchJobsRequest getJob() { - return job; - } - - /** - * Set the job details. - * @param job Job details - */ - public void setJob(CreateAsyncFetchJobsRequest job) { - this.job = job; - } - - @Override - public String toString() { - return "QueryAsynchFetchJobsResult [requestId=" + requestId + ", err=" + err - +", code=" + code + ", status=" + status + ", wait=" + wait - + ", job url=" + job.getUrl() + ", job bucket=" + job.getBucketName() - + ", job key=" + job.getObjectKey() + ", job callbackurl=" + job.getCallBackUrl() - + ", job callbackbody=" + job.getCallBackBody() + "]"; - } + @JsonProperty(value = "request_Id") + private String requestId; + + @JsonProperty(value = "err") + private String err; + + @JsonProperty(value = "code") + private String code; + + @JsonProperty(value = "status") + private String status; + + @JsonProperty(value = "wait") + private int wait; + + @JsonProperty(value = "job") + private CreateAsyncFetchJobsRequest job; + + public QueryAsynchFetchJobsResult() { + job = new CreateAsyncFetchJobsRequest(); + } + + /** + * Constructor + * + * @param requestId + * Unique ID of a request + * @param err + * Error description + * @param code + * Error code + * @param status + * Job status + * @param wait + * Number of queuing jobs before the current job. The value 0 + * indicates that the current job is being executed, and the + * value -1 indicates that the job has been executed at least + * once (the retry logic may be triggered). + * @param job + * Job details + */ + public QueryAsynchFetchJobsResult(String requestId, String err, String code, String status, int wait, + CreateAsyncFetchJobsRequest job) { + this.setRequestId(requestId); + this.setErr(err); + this.setCode(code); + this.setStatus(status); + this.setWait(wait); + this.setJob(job); + } + + /** + * Obtain the unique ID of a request. + * + * @return Unique ID of a request + */ + public String getRequestId() { + return requestId; + } + + /** + * Set the unique ID of a request. + * + * @param requestId + * Unique ID of a request + */ + public void setRequestId(String requestId) { + this.requestId = requestId; + } + + /** + * Obtain the error description. + * + * @return Error description + */ + public String getErr() { + return err; + } + + /** + * Set the error description. + * + * @param err + * Error description + */ + public void setErr(String err) { + this.err = err; + } + + /** + * Obtain the error code. + * + * @return Error code + */ + public String getCode() { + return code; + } + + /** + * Set the error code. + * + * @param code + * Error code + */ + public void setCode(String code) { + this.code = code; + } + + /** + * Obtain the job status. + * + * @return Job status + */ + public String getStatus() { + return status; + } + + /** + * Set the job status. + * + * @param status + * Job status + */ + public void setStatus(String status) { + this.status = status; + } + + /** + * Obtain the number of queuing jobs. + * + * @return Number of queuing jobs + */ + public int getWait() { + return wait; + } + + /** + * Set the number of queuing jobs. + * + * @param wait + * Number of queuing jobs + */ + public void setWait(int wait) { + this.wait = wait; + } + + /** + * Obtain the job details. + * + * @return Job details + */ + public CreateAsyncFetchJobsRequest getJob() { + return job; + } + + /** + * Set the job details. + * + * @param job + * Job details + */ + public void setJob(CreateAsyncFetchJobsRequest job) { + this.job = job; + } + + @Override + public String toString() { + return "QueryAsynchFetchJobsResult [requestId=" + requestId + ", err=" + err + ", code=" + code + ", status=" + + status + ", wait=" + wait + ", job url=" + job.getUrl() + ", job bucket=" + job.getBucketName() + + ", job key=" + job.getObjectKey() + ", job callbackurl=" + job.getCallBackUrl() + + ", job callbackbody=" + job.getCallBackBody() + "]"; + } } diff --git a/app/src/main/java/com/oef/services/model/QueryExtensionPolicyResult.java b/app/src/main/java/com/oef/services/model/QueryExtensionPolicyResult.java index 445837a..6f5108b 100644 --- a/app/src/main/java/com/oef/services/model/QueryExtensionPolicyResult.java +++ b/app/src/main/java/com/oef/services/model/QueryExtensionPolicyResult.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services.model; import com.fasterxml.jackson.annotation.JsonProperty; @@ -21,87 +22,98 @@ * */ public class QueryExtensionPolicyResult extends HeaderResponse { - @JsonProperty(value = "fetch") - private FetchBean fetch; - - @JsonProperty(value = "transcode") - private TranscodeBean transcode; - - @JsonProperty(value = "compress") - private CompressBean compress; - - public QueryExtensionPolicyResult() { - fetch = new FetchBean(); - transcode = new TranscodeBean(); - compress = new CompressBean(); - } - - /** - * Constructor - * @param fetch Content of the asynchronous fetch policy - * @param transcode Content of the asynchronous transcode policy - * @param compress Content of the file compression policy - */ - public QueryExtensionPolicyResult(FetchBean fetch, TranscodeBean transcode, CompressBean compress) { - this.fetch = fetch; - this.transcode = transcode; - this.compress = compress; - } + @JsonProperty(value = "fetch") + private FetchBean fetch; - /** - * Obtain the content of the asynchronous fetch policy. - * @return Content of the asynchronous fetch policy - */ - public FetchBean getFetch() { - return fetch; - } + @JsonProperty(value = "transcode") + private TranscodeBean transcode; - /** - * Set the content of the asynchronous fetch policy. - * @param fetch Content of the asynchronous fetch policy - */ - public void setFetch(FetchBean fetch) { - this.fetch = fetch; - } + @JsonProperty(value = "compress") + private CompressBean compress; - /** - * Obtain the content of the asynchronous transcode policy. - * @return Content of the asynchronous transcode policy - */ - public TranscodeBean getTranscode() { - return transcode; - } + public QueryExtensionPolicyResult() { + fetch = new FetchBean(); + transcode = new TranscodeBean(); + compress = new CompressBean(); + } - /** - * Set the content of the asynchronous transcode policy. - * @param transcode Content of the asynchronous transcode policy - */ - public void setTranscode(TranscodeBean transcode) { - this.transcode = transcode; - } + /** + * Constructor + * + * @param fetch + * Content of the asynchronous fetch policy + * @param transcode + * Content of the asynchronous transcode policy + * @param compress + * Content of the file compression policy + */ + public QueryExtensionPolicyResult(FetchBean fetch, TranscodeBean transcode, CompressBean compress) { + this.fetch = fetch; + this.transcode = transcode; + this.compress = compress; + } - /** - * Obtain the content of the file compression policy. - * @return Content of the file compression policy - */ - public CompressBean getCompress() { - return compress; - } + /** + * Obtain the content of the asynchronous fetch policy. + * + * @return Content of the asynchronous fetch policy + */ + public FetchBean getFetch() { + return fetch; + } - /** - * Set the content of the file compression policy. - * @param compress Content of the file compression policy - */ - public void setCompress(CompressBean compress) { - this.compress = compress; - } - + /** + * Set the content of the asynchronous fetch policy. + * + * @param fetch + * Content of the asynchronous fetch policy + */ + public void setFetch(FetchBean fetch) { + this.fetch = fetch; + } + + /** + * Obtain the content of the asynchronous transcode policy. + * + * @return Content of the asynchronous transcode policy + */ + public TranscodeBean getTranscode() { + return transcode; + } + + /** + * Set the content of the asynchronous transcode policy. + * + * @param transcode + * Content of the asynchronous transcode policy + */ + public void setTranscode(TranscodeBean transcode) { + this.transcode = transcode; + } + + /** + * Obtain the content of the file compression policy. + * + * @return Content of the file compression policy + */ + public CompressBean getCompress() { + return compress; + } + + /** + * Set the content of the file compression policy. + * + * @param compress + * Content of the file compression policy + */ + public void setCompress(CompressBean compress) { + this.compress = compress; + } - @Override - public String toString() - { - return "ExtensionPolicyResult [fetch status=" + fetch.getStatus() + ", fetch agency=" + fetch.getAgency() - + ", transcode status=" + transcode.getStatus() + ", transcode agency=" + transcode.getAgency() - + ", compress status=" + compress.getStatus() + ", compress agency=" + compress.getAgency() +"]"; + @Override + public String toString() { + return "ExtensionPolicyResult [fetch status=" + fetch.getStatus() + ", fetch agency=" + fetch.getAgency() + + ", transcode status=" + transcode.getStatus() + ", transcode agency=" + transcode.getAgency() + + ", compress status=" + compress.getStatus() + ", compress agency=" + compress.getAgency() + "]"; } } diff --git a/app/src/main/java/com/oef/services/model/RequestParamEnum.java b/app/src/main/java/com/oef/services/model/RequestParamEnum.java index 505a72f..5bbb09a 100644 --- a/app/src/main/java/com/oef/services/model/RequestParamEnum.java +++ b/app/src/main/java/com/oef/services/model/RequestParamEnum.java @@ -11,58 +11,51 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services.model; import com.obs.services.model.SpecialParamEnum; public enum RequestParamEnum { - /** + /** * Obtain, set, or delete an asynchronous policy. */ - EXTENSION_POLICY("v1/extension_policy"), + EXTENSION_POLICY("v1/extension_policy"), /** * Obtain, set, or delete an asynchronous job. */ - ASYNC_FETCH_JOBS("v1/async-fetch/jobs"), - - DIS_POLICIES("v1/dis_policies"); - + ASYNC_FETCH_JOBS("v1/async-fetch/jobs"), + + DIS_POLICIES("v1/dis_policies"); + private String stringCode; - - private RequestParamEnum(String stringCode) - { - if (stringCode == null) - { + + private RequestParamEnum(String stringCode) { + if (stringCode == null) { throw new IllegalArgumentException("stringCode is null"); } this.stringCode = stringCode; } - - public String getStringCode() - { + + public String getStringCode() { return this.stringCode.toLowerCase(); } - - public String getOriginalStringCode() - { + + public String getOriginalStringCode() { return this.stringCode; } - - public static SpecialParamEnum getValueFromStringCode(String stringCode) - { - if (stringCode == null) - { + + public static SpecialParamEnum getValueFromStringCode(String stringCode) { + if (stringCode == null) { throw new IllegalArgumentException("string code is null"); } - - for (SpecialParamEnum installMode : SpecialParamEnum.values()) - { - if (installMode.getStringCode().equals(stringCode.toLowerCase())) - { + + for (SpecialParamEnum installMode : SpecialParamEnum.values()) { + if (installMode.getStringCode().equals(stringCode.toLowerCase())) { return installMode; } } - + throw new IllegalArgumentException("string code is illegal"); } } diff --git a/app/src/main/java/com/oef/services/model/TranscodeBean.java b/app/src/main/java/com/oef/services/model/TranscodeBean.java index 3d37814..2e00b72 100644 --- a/app/src/main/java/com/oef/services/model/TranscodeBean.java +++ b/app/src/main/java/com/oef/services/model/TranscodeBean.java @@ -11,6 +11,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package com.oef.services.model; import com.fasterxml.jackson.annotation.JsonProperty; @@ -20,61 +21,69 @@ * */ public class TranscodeBean { - @JsonProperty(value = "status") - private String status; - - @JsonProperty(value = "agency") - private String agency; - + @JsonProperty(value = "status") + private String status; + + @JsonProperty(value = "agency") + private String agency; + public TranscodeBean() { - + + } + + /** + * Constructor + * + * @param status + * Policy status + * @param agency + * IAM agency + */ + public TranscodeBean(String status, String agency) { + this.status = status; + this.agency = agency; } - + /** - * Constructor - * @param status Policy status - * @param agency IAM agency - */ - public TranscodeBean(String status, String agency) { - this.status = status; - this.agency = agency; - } - - /** - * Obtain the policy status. - * @return Policy status - */ - public String getStatus() { - return status; - } - - /** - * Set the policy status. - * @param status Policy status - */ - public void setStatus(String status) { - this.status = status; - } - - /** - * Obtain the IAM agency. - * @return IAM agency - */ - public String getAgency() { - return agency; - } - - /** - * Set the IAM agency. - * @param agency IAM agency - */ - public void setAgency(String agency) { - this.agency = agency; - } - - @Override - public String toString() - { + * Obtain the policy status. + * + * @return Policy status + */ + public String getStatus() { + return status; + } + + /** + * Set the policy status. + * + * @param status + * Policy status + */ + public void setStatus(String status) { + this.status = status; + } + + /** + * Obtain the IAM agency. + * + * @return IAM agency + */ + public String getAgency() { + return agency; + } + + /** + * Set the IAM agency. + * + * @param agency + * IAM agency + */ + public void setAgency(String agency) { + this.agency = agency; + } + + @Override + public String toString() { return "TranscodeBean [status=" + status + ", agency=" + agency + "]"; } } diff --git a/app/src/main/java/okio/AsyncTimeout.java b/app/src/main/java/okio/AsyncTimeout.java deleted file mode 100644 index 0c8d493..0000000 --- a/app/src/main/java/okio/AsyncTimeout.java +++ /dev/null @@ -1,518 +0,0 @@ -/* - * Copyright (C) 2014 Square, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package okio; - -import static okio.Util.checkOffsetAndCount; - -import java.io.IOException; -import java.io.InterruptedIOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Random; -import java.util.concurrent.TimeUnit; - -import com.obs.log.ILogger; -import com.obs.log.LoggerBuilder; - -/** - * This timeout uses a background thread to take action exactly when the timeout - * occurs. Use this to implement timeouts where they aren't supported natively, - * such as to sockets that are blocked on writing. - * - *

    - * Subclasses should override {@link #timedOut} to take action when a timeout - * occurs. This method will be invoked by the shared watchdog thread so it - * should not do any long-running operations. Otherwise we risk starving other - * timeouts from being triggered. - * - *

    - * Use {@link #sink} and {@link #source} to apply this timeout to a stream. The - * returned value will apply the timeout to each operation on the wrapped - * stream. - * - *

    - * Callers should call {@link #enter} before doing work that is subject to - * timeouts, and {@link #exit} afterwards. The return value of {@link #exit} - * indicates whether a timeout was triggered. Note that the call to - * {@link #timedOut} is asynchronous, and may be called after {@link #exit}. - */ -public class AsyncTimeout extends Timeout { - private static final ILogger ILOG = LoggerBuilder.getLogger(AsyncTimeout.class); - /** - * Don't write more than 64 KiB of data at a time, give or take a segment. - * Otherwise slow connections may suffer timeouts even when they're making - * (slow) progress. Without this, writing a single 1 MiB buffer may never - * succeed on a sufficiently slow connection. - */ - private static final int TIMEOUT_WRITE_SIZE = 64 * 1024; - - /** - * Duration for the watchdog thread to be idle before it shuts itself down. - */ - private static final long IDLE_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(60); - private static final long IDLE_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(IDLE_TIMEOUT_MILLIS); - - /** - * The watchdog thread processes a linked list of pending timeouts, sorted - * in the order to be triggered. This class synchronizes on - * AsyncTimeout.class. This lock guards the queue. - * - *

    - * Head's 'next' points to the first element of the linked list. The first - * element is the next node to time out, or null if the queue is empty. The - * head is null until the watchdog thread is started and also after being - * idle for {@link #IDLE_TIMEOUT_MILLIS}. - */ - // static AsyncTimeout head; - - /** True if this node is currently in the queue. */ - private boolean inQueue; - - /** The next node in the linked list. */ - private AsyncTimeout next; - - /** - * If scheduled, this is the time that the watchdog should time this out. - */ - private long timeoutAt; - - - /** /////////////////////////////////////////////////////////////////////////// **/ - private static final int LOCK_SIZE = Runtime.getRuntime().availableProcessors(); - - private static final Random RANDOM = new Random(System.currentTimeMillis()); - - private Locker locker; - - private int id; - - public AsyncTimeout() { - this.id = RANDOM.nextInt(LOCK_SIZE); - this.locker = LOCKERS.get(this.id); - if(ILOG.isDebugEnabled()) { - ILOG.debug("id is : " + this.id + "; and locker is : " + this.locker); - } - } - - Locker getLocker() { - return this.locker; - } - - private static final Map LOCKERS = new HashMap(); - static { - for(int i=0; i 0L) { - // Count how many bytes to write. This loop guarantees we - // split on a segment boundary. - long toWrite = 0L; - for (Segment s = source.head; toWrite < TIMEOUT_WRITE_SIZE; s = s.next) { - int segmentSize = s.limit - s.pos; - toWrite += segmentSize; - if (toWrite >= byteCount) { - toWrite = byteCount; - break; - } - } - - // Emit one write. Only this section is subject to the - // timeout. - boolean throwOnTimeout = false; - enter(); - try { - sink.write(source, toWrite); - byteCount -= toWrite; - throwOnTimeout = true; - } catch (IOException e) { - throw exit(e); - } finally { - exit(throwOnTimeout); - } - } - } - - @Override - public void flush() throws IOException { - boolean throwOnTimeout = false; - enter(); - try { - sink.flush(); - throwOnTimeout = true; - } catch (IOException e) { - throw exit(e); - } finally { - exit(throwOnTimeout); - } - } - - @Override - public void close() throws IOException { - boolean throwOnTimeout = false; - enter(); - try { - sink.close(); - throwOnTimeout = true; - } catch (IOException e) { - throw exit(e); - } finally { - exit(throwOnTimeout); - } - } - - @Override - public Timeout timeout() { - return AsyncTimeout.this; - } - - @Override - public String toString() { - return "AsyncTimeout.sink(" + sink + ")"; - } - }; - } - - /** - * Returns a new source that delegates to {@code source}, using this to - * implement timeouts. This works best if {@link #timedOut} is overridden to - * interrupt {@code sink}'s current operation. - */ - public final Source source(final Source source) { - return new Source() { - @Override - public long read(Buffer sink, long byteCount) throws IOException { - boolean throwOnTimeout = false; - long start = System.nanoTime(); - enter(); - if(ILOG.isDebugEnabled()) { - ILOG.debug("Thread : " + Thread.currentThread().getName() + " - " + (System.nanoTime() - start) - + " - Method: enter()"); - } - try { - - start = System.nanoTime(); - long result = source.read(sink, byteCount); - if(ILOG.isDebugEnabled()) { - ILOG.debug("Thread : " + Thread.currentThread().getName() + " - " + (System.nanoTime() - start) - + " - Method: source.read(); result: " + result + "; byteCount: " + byteCount); - } - - throwOnTimeout = true; - return result; - } catch (IOException e) { - throw exit(e); - } finally { - - start = System.nanoTime(); - exit(throwOnTimeout); - if(ILOG.isDebugEnabled()) { - ILOG.debug("Thread : " + Thread.currentThread().getName() + " - " + (System.nanoTime() - start) - + " - Method: exit()"); - } - } - } - - @Override - public void close() throws IOException { - boolean throwOnTimeout = false; - try { - source.close(); - throwOnTimeout = true; - } catch (IOException e) { - throw exit(e); - } finally { - exit(throwOnTimeout); - } - } - - @Override - public Timeout timeout() { - return AsyncTimeout.this; - } - - @Override - public String toString() { - return "AsyncTimeout.source(" + source + ")"; - } - }; - } - - /** - * Throws an IOException if {@code throwOnTimeout} is {@code true} and a - * timeout occurred. See {@link #newTimeoutException(java.io.IOException)} - * for the type of exception thrown. - */ - final void exit(boolean throwOnTimeout) throws IOException { - boolean timedOut = exit(); - if (timedOut && throwOnTimeout) - throw newTimeoutException(null); - } - - /** - * Returns either {@code cause} or an IOException that's caused by - * {@code cause} if a timeout occurred. See - * {@link #newTimeoutException(java.io.IOException)} for the type of - * exception returned. - */ - final IOException exit(IOException cause) throws IOException { - if (!exit()) - return cause; - return newTimeoutException(cause); - } - - /** - * Returns an {@link IOException} to represent a timeout. By default this - * method returns {@link java.io.InterruptedIOException}. If {@code cause} - * is non-null it is set as the cause of the returned exception. - */ - protected IOException newTimeoutException(IOException cause) { - InterruptedIOException e = new InterruptedIOException("timeout"); - if (cause != null) { - e.initCause(cause); - } - return e; - } - - private static final class Watchdog extends Thread { - private Locker locker; - - Watchdog(Locker locker) { - super("Okio Watchdog : " + locker.toString()); - setDaemon(true); - this.locker = locker; - } - - public void run() { - while (true) { - try { - AsyncTimeout timedOut; - synchronized (locker) { - timedOut = awaitTimeout(locker); - - // Didn't find a node to interrupt. Try again. - if (timedOut == null) - continue; - - // The queue is completely empty. Let this thread exit - // and let another watchdog thread - // get created on the next call to scheduleTimeout(). - if (timedOut == locker.head) { - locker.head = null; - return; - } - } - - // Close the timed out node. - timedOut.timedOut(); - } catch (InterruptedException ignored) { - } - } - } - } - - /** - * Removes and returns the node at the head of the list, waiting for it to - * time out if necessary. This returns {@link #head} if there was no node at - * the head of the list when starting, and there continues to be no node - * after waiting {@code IDLE_TIMEOUT_NANOS}. It returns null if a new node - * was inserted while waiting. Otherwise this returns the node being waited - * on that has been removed. - */ - static AsyncTimeout awaitTimeout(Locker locker) throws InterruptedException { - // Get the next eligible node. - AsyncTimeout node = locker.head.next; - - // The queue is empty. Wait until either something is enqueued or the - // idle timeout elapses. - if (node == null) { - long startNanos = System.nanoTime(); - locker.wait(IDLE_TIMEOUT_MILLIS); - return locker.head.next == null && (System.nanoTime() - startNanos) >= IDLE_TIMEOUT_NANOS ? locker.head // The - // idle - // timeout - // elapsed. - : null; // The situation has changed. - } - - long waitNanos = node.remainingNanos(System.nanoTime()); - - // The head of the queue hasn't timed out yet. Await that. - if (waitNanos > 0) { - // Waiting is made complicated by the fact that we work in - // nanoseconds, - // but the API wants (millis, nanos) in two arguments. - long waitMillis = waitNanos / 1000000L; - waitNanos -= (waitMillis * 1000000L); - locker.wait(waitMillis, (int) waitNanos); - return null; - } - - // The head of the queue has timed out. Remove it. - locker.head.next = node.next; - node.next = null; - return node; - } -} diff --git a/app/src/main/java/okio/SegmentPool.java b/app/src/main/java/okio/SegmentPool.java deleted file mode 100644 index 5ddc204..0000000 --- a/app/src/main/java/okio/SegmentPool.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2014 Square, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package okio; - -/** - * A collection of unused segments, necessary to avoid GC churn and zero-fill. - * This pool is a thread-safe static singleton. - */ -final class SegmentPool { - /** The maximum number of bytes to pool. */ - // TODO: Is 64 KiB a good maximum size? Do we ever have that many idle segments? - static final long MAX_SIZE = 64 * 1024; // 64 KiB. - - /** Singly-linked list of segments. */ - static Segment next; - - /** Total bytes in this pool. */ - static long byteCount; - - private SegmentPool() { - } - - static Segment take() { -// synchronized (SegmentPool.class) { -// if (next != null) { -// Segment result = next; -// next = result.next; -// result.next = null; -// byteCount -= Segment.SIZE; -// return result; -// } -// } - return new Segment(); // Pool is empty. Don't zero-fill while holding a lock. - } - - static void recycle(Segment segment) { - if (segment.next != null || segment.prev != null) throw new IllegalArgumentException(); - if (segment.shared) return; // This segment cannot be recycled. -// synchronized (SegmentPool.class) { -// if (byteCount + Segment.SIZE > MAX_SIZE) return; // Pool is full. -// byteCount += Segment.SIZE; -// segment.next = next; -// segment.pos = segment.limit = 0; -// next = segment; -// } - } -} diff --git a/app/src/main/resource/log4j2.xml b/app/src/main/resource/log4j2.xml index d193f33..92646e0 100644 --- a/app/src/main/resource/log4j2.xml +++ b/app/src/main/resource/log4j2.xml @@ -8,7 +8,7 @@ + filePattern="${sys:user.dir}/logs/OBS-SDK.interface_north-%i.log" filePermissions="rw-------"> @@ -16,7 +16,7 @@ + filePattern="${sys:user.dir}/logs/OBS-SDK.interface_south-%i.log" filePermissions="rw-------"> @@ -24,7 +24,7 @@ + filePattern="${sys:user.dir}/logs/OBS-SDK.access-%i.log" filePermissions="rw-------"> @@ -34,7 +34,7 @@ - @@ -46,7 +46,7 @@ - diff --git a/app/src/test/java/com/obs/test/ApiVersionTest.java b/app/src/test/java/com/obs/test/ApiVersionTest.java new file mode 100644 index 0000000..ba594bb --- /dev/null +++ b/app/src/test/java/com/obs/test/ApiVersionTest.java @@ -0,0 +1,80 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test; + +import static org.junit.Assert.assertEquals; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.junit.Test; + +import com.obs.services.ObsClient; +import com.obs.services.internal.service.AbstractRequestConvertor; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.BucketTypeEnum; +import com.obs.test.tools.BucketTools; + +public class ApiVersionTest { + + private String endpint_ip = "http://X.X.X.X"; + private String endpint_dns = "http://obs.r1.huawei.com"; + private String ak = "XXXXXXXXXXX"; + private String sk = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; + + class ApiVersionClient extends ObsClient { + public ApiVersionClient(String accessKey, String secretKey, String endPoint) { + super(accessKey, secretKey, endPoint); + } + } + + @Test + public void test_while_bucketname_is_null() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + String bucketName = ""; + + ObsClient obsClient = TestTools.getRequestPaymentEnvironment_User1(); + + Class clazz_1 = ObsClient.class; + +// Class clazz_2 = clazz_1.getSuperclass(); + Class clazz_2 = AbstractRequestConvertor.class; + + Method method = clazz_2.getDeclaredMethod("getApiVersion", new Class[]{String.class}); + method.setAccessible(true); + Object result = method.invoke(obsClient, new Object[]{bucketName}); + + System.out.println(result); + + assertEquals(result, AuthTypeEnum.OBS); + } + + @Test + public void test_while_bucketname_not_null() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + String bucketName = "test-api-version"; + + ObsClient obsClient = TestTools.getRequestPaymentEnvironment_User1(); + + Class clazz_1 = ObsClient.class; + + Class clazz_2 = AbstractRequestConvertor.class; + + Method method = clazz_2.getDeclaredMethod("getApiVersion", new Class[]{String.class}); + method.setAccessible(true); + Object result = method.invoke(obsClient, new Object[]{bucketName}); + + System.out.println(result); + + assertEquals(result, AuthTypeEnum.OBS); + } +} diff --git a/app/src/test/java/com/obs/test/BucketTest.java b/app/src/test/java/com/obs/test/BucketTest.java new file mode 100644 index 0000000..c2d5fab --- /dev/null +++ b/app/src/test/java/com/obs/test/BucketTest.java @@ -0,0 +1,285 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test; + +import com.obs.services.ObsClient; +import com.obs.services.exception.ObsException; +import com.obs.services.model.AvailableZoneEnum; +import com.obs.services.model.BucketCustomDomainInfo; +import com.obs.services.model.BucketTypeEnum; +import com.obs.services.model.BucketVersioningConfiguration; +import com.obs.services.model.CreateBucketRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.ListBucketsResult; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ReplicationConfiguration; +import com.obs.services.model.ReplicationConfiguration.Destination; +import com.obs.services.model.ReplicationConfiguration.Rule; +import com.obs.services.model.RuleStatusEnum; +import com.obs.services.model.SetBucketReplicationRequest; +import com.obs.services.model.SetBucketVersioningRequest; +import com.obs.services.model.VersioningStatusEnum; +import com.obs.services.model.fs.NewBucketRequest; +import com.obs.services.model.fs.ObsFSBucket; +import com.obs.test.tools.PropertiesTools; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class BucketTest { + private static final File file = new File("./app/src/test/resource/test_data.properties"); + private static final ArrayList createdBuckets = new ArrayList<>(); + + @BeforeClass + public static void create_demo_bucket() throws IOException { + String beforeBucket = PropertiesTools.getInstance(file).getProperties("beforeBucket"); + String location = PropertiesTools.getInstance(file).getProperties("environment.location"); + CreateBucketRequest request = new CreateBucketRequest(); + request.setBucketName(beforeBucket); + request.setBucketType(BucketTypeEnum.OBJECT); + request.setLocation(location); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + HeaderResponse response = obsClient.createBucket(request); + assertEquals(200, response.getStatusCode()); + createdBuckets.add(beforeBucket); + } + + @AfterClass + public static void delete_created_buckets() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + for (String bucket : createdBuckets) { + obsClient.deleteBucket(bucket); + } + } + + @Test + public void test_create_bucket_obs() throws IOException { + String bucketName = PropertiesTools.getInstance(file).getProperties("bucketPrefix") + + "creat-bucket001"; + String location = PropertiesTools.getInstance(file).getProperties("environment.location"); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + CreateBucketRequest request = new CreateBucketRequest(); + request.setBucketName(bucketName); + request.setBucketType(BucketTypeEnum.OBJECT); + request.setLocation(location); + HeaderResponse response = obsClient.createBucket(request); + + assertEquals(200, response.getStatusCode()); + createdBuckets.add(bucketName); + } + + @Test + public void test_create_bucket_3az() throws IOException { + String bucketName = PropertiesTools.getInstance(file).getProperties("bucketPrefix") + + "test-sdk-obs-3az"; + String location = PropertiesTools.getInstance(file).getProperties("location"); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + CreateBucketRequest request = new CreateBucketRequest(); + request.setBucketName(bucketName); + request.setBucketType(BucketTypeEnum.OBJECT); + request.setAvailableZone(AvailableZoneEnum.MULTI_AZ); + request.setLocation(location); + HeaderResponse response = obsClient.createBucket(request); + + assertEquals(200, response.getStatusCode()); + createdBuckets.add(bucketName); + } + + @Test + public void test_create_bucket_pfs() throws IOException { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String bucketName = PropertiesTools.getInstance(file).getProperties("bucketPrefix") + + "test-sdk-pfs"; + String location = PropertiesTools.getInstance(file).getProperties("environment.location"); + + CreateBucketRequest request = new CreateBucketRequest(); + request.setBucketName(bucketName); + request.setBucketType(BucketTypeEnum.PFS); + request.setLocation(location); + HeaderResponse response = obsClient.createBucket(request); + + assertEquals(200, response.getStatusCode()); + createdBuckets.add(bucketName); + } + + @Test + public void test_create_bucket_new_bucket() throws IOException { + String bucketName = PropertiesTools.getInstance(file).getProperties("bucketPrefix") + + "test-sdk-pfs-2"; + String location = PropertiesTools.getInstance(file).getProperties("environment.location"); +// ObsClient obsClient = TestTools.getExternalEnvironment(); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + +// CreateBucketRequest request = new CreateBucketRequest(); +// request.setBucketName("test-sdk-pfs-0000003"); +// request.setBucketType(BucketTypeEnum.PFS); +// request.setLocation("cn-north-4"); + + NewBucketRequest request = new NewBucketRequest(bucketName, location); + ObsFSBucket response = obsClient.newBucket(request); + + assertEquals(bucketName, response.getBucketName()); + createdBuckets.add(bucketName); + } + + @Test + public void test_head_bucket() throws IOException { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String beforeBucket = PropertiesTools.getInstance(file).getProperties("beforeBucket"); + boolean result = obsClient.headBucket(beforeBucket); + + assertTrue(result); + } + + @Test + public void test_list_bucket() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + ListBucketsResult result = obsClient.listBucketsV2(null); + + assertEquals(result.getBuckets().size(), 5); + } + + @Test + public void test_list_objects_in_bucket() throws IOException { + + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String beforeBucket = PropertiesTools.getInstance(file).getProperties("beforeBucket"); + + ObjectListing result = obsClient.listObjects(beforeBucket); + + assertEquals(result.getBucketName(), beforeBucket); + + assertEquals(result.getObjects().size(), 1000); + } + + @Test + public void test_set_bucket_version() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + String bucketName = "putobject-bucket-0603051314"; + + SetBucketVersioningRequest request = new SetBucketVersioningRequest(bucketName, VersioningStatusEnum.ENABLED); + HeaderResponse result = obsClient.setBucketVersioning(bucketName, new BucketVersioningConfiguration(VersioningStatusEnum.ENABLED)); + assertEquals(result.getStatusCode(), 200); + BucketVersioningConfiguration config = obsClient.getBucketVersioning(bucketName); + assertEquals(config.getVersioningStatus().getCode(), "Enabled"); + + result = obsClient.setBucketVersioning(bucketName, new BucketVersioningConfiguration(VersioningStatusEnum.SUSPENDED)); + assertEquals(result.getStatusCode(), 200); + config = obsClient.getBucketVersioning(bucketName); + assertEquals(config.getVersioningStatus().getCode(), "Suspended"); + } + + @Test(expected = ObsException.class) + public void test_set_bucket_version_exception() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + String bucketName = "putobject-bucket-0603051314"; + + HeaderResponse result = obsClient.setBucketVersioning(bucketName, new BucketVersioningConfiguration(VersioningStatusEnum.getValueFromCode("ERROR"))); + } + + @Test + public void test_set_bucket_replication() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + ReplicationConfiguration replicationConfiguration = new ReplicationConfiguration(); + Rule rule = new Rule(); + rule.setId("test_rule11"); // id为一个自定义的唯一字符串 + rule.setPrefix("test"); // 前缀 + rule.setStatus(RuleStatusEnum.ENABLED); + Destination dest = new Destination(); + dest.setBucket("sadfxx-xxx-xxx"); // 目标桶名称 + rule.setDestination(dest); + replicationConfiguration.getRules().add(rule); + replicationConfiguration.setAgency("obs-mirror"); // IAM委托名称 + + SetBucketReplicationRequest request = new SetBucketReplicationRequest("wwwwwwweeee", replicationConfiguration); + + + obsClient.setBucketReplication(request); + } + + @Test + public void test_get_bucket_replication() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + ReplicationConfiguration replicationConfiguration = obsClient.getBucketReplication("wwwwwwweeee"); + System.out.println(replicationConfiguration.toString()); + } +/** + * 测试获取自定义域名 + */ + @Test + public void test_get_bucket_customdomain() { + ObsClient obsClient = TestTools.getExternalEnvironment(); + + BucketCustomDomainInfo bucketCustomDomainInfo = obsClient.getBucketCustomDomain("testBucket"); + System.out.println(bucketCustomDomainInfo.toString()); + } + + /** + * 测试设置自定义域名 + */ + @Test + public void test_set_bucket_customdomain() { + ObsClient obsClient = TestTools.getExternalEnvironment(); + +// BucketCustomDomainInfo bucketCustomDomainInfo = obsClient.getBucketCustomDomain("shenqing-oms-test-003"); + + HeaderResponse response = obsClient.setBucketCustomDomain("testBucket", "test.huawei.com"); + System.out.println(response.toString()); + } + + /** + * 测试删除桶自定义域名 + */ + @Test + public void test_delete_bucket_customdomain() { + ObsClient obsClient = TestTools.getExternalEnvironment(); + + HeaderResponse response = obsClient.deleteBucketCustomDomain("testBucket", "test.huawei.com"); + System.out.println(response.toString()); + } + + + @Test + public void test_close_obsclient() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + + String bucketName = "putobject-bucket-0603051314"; + + obsClient.headBucket(bucketName); + + try { + obsClient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + obsClient.headBucket(bucketName); + } +} diff --git a/app/src/test/java/com/obs/test/DownloadFileRequestTest.java b/app/src/test/java/com/obs/test/DownloadFileRequestTest.java new file mode 100644 index 0000000..c911001 --- /dev/null +++ b/app/src/test/java/com/obs/test/DownloadFileRequestTest.java @@ -0,0 +1,255 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test; + +import java.io.IOException; +import java.util.Date; + +import org.junit.Before; +import org.junit.Test; + +import com.obs.services.ObsClient; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.ObsConstraint; +import com.obs.services.model.BucketTypeEnum; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.CompleteMultipartUploadResult; +import com.obs.services.model.DownloadFileRequest; +import com.obs.services.model.DownloadFileResult; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ListPartsRequest; +import com.obs.services.model.ListPartsResult; +import com.obs.services.model.MonitorableProgressListener; +import com.obs.services.model.ProgressListener; +import com.obs.services.model.ProgressStatus; +import com.obs.services.model.UploadPartRequest; +import com.obs.test.objects.BaseObjectTest; +import com.obs.test.tools.BucketTools; + +public class DownloadFileRequestTest extends BaseObjectTest { + + private String bucketName; + private String objectKey; + private ObsClient obsClient = null; + + @Before + public void init() { + bucketName = DownloadFileRequestTest.class.getName().replaceAll("\\.", "-").toLowerCase() + "-obs"; + objectKey = "for_download_2"; + + obsClient = TestTools.getEnvironment_User3(); + + BucketTools.createBucket(obsClient, bucketName, BucketTypeEnum.OBJECT); + } + + @Test + public void test_upload_big_file() throws IOException { + InitiateMultipartUploadRequest initrequest = generateInitiateMultipartUploadRequest(bucketName, objectKey); + InitiateMultipartUploadResult initResult = obsClient.initiateMultipartUpload(initrequest); + + for (int i = 1; i < 20; i++) { + UploadPartRequest uploadParerequest = generateUploadPartRequest(bucketName, objectKey, + initResult.getUploadId(), i, (byte) i); + obsClient.uploadPart(uploadParerequest); + + System.out.println("upload part : " + i); + } + + ListPartsRequest listRequest = generateListPartsRequest(bucketName, objectKey, initResult.getUploadId()); + ListPartsResult listResult = obsClient.listParts(listRequest); + + CompleteMultipartUploadRequest completeRequest = generateCompleteMultipartUploadRequest(listResult); + CompleteMultipartUploadResult completeResult = obsClient.completeMultipartUpload(completeRequest); + } + + @Test + public void test_pause() { + DownloadFileRequest request = new DownloadFileRequest(bucketName, objectKey); + // 设置下载对象的本地文件路径 + request.setDownloadFile("D:\\test\\test_bigfile_download_1"); + // 设置分段下载时的最大并发数 + request.setTaskNum(5); + // 设置分段大小为10MB + request.setPartSize(10 * 1024 * 1024); + // 开启断点续传模式 + request.setEnableCheckpoint(true); + + PauseAbleProgressListener pauseAbleProgressListener = new PauseAbleProgressListener(); + + request.setProgressListener(pauseAbleProgressListener); + + // new Thread(new Runnable() { + // @Override + // public void run() { + // + // try { + // // 等10秒钟,尝试暂停 + // Thread.sleep(5 * 1000L); + // } catch (InterruptedException e) { + // e.printStackTrace(); + // } + // + // // 暂停 + // pauseAbleProgressListener.setPause(true); + // try { + // obsClient.close(); + // } catch (IOException e) { + // e.printStackTrace(); + // } + // } + // }).start();; + + try { + // 进行断点续传下载 + DownloadFileResult result = obsClient.downloadFile(request); + } catch (ObsException e) { + // 发生异常时可再次调用断点续传下载接口进行重新下载 + e.printStackTrace(); + } + + } + + class PauseAbleProgressListener implements ProgressListener { + + private boolean isPause = false; + + public void setPause(boolean isPause) { + this.isPause = isPause; + } + + @Override + public void progressChanged(ProgressStatus status) { + System.out.println("TransferPercentage:" + status.getTransferPercentage()); + if (isPause) { + System.out.println("isPause : " + isPause + "TransferPercentage:" + status.getTransferPercentage()); + // throw new PauseException(); + } + } + } + + class PauseException extends RuntimeException { + public PauseException() { + super(); + } + + @Override + public String toString() { + return "now is pause"; + } + } + + @Test + public void test_pause_2() { + DownloadFileRequest request = new DownloadFileRequest(this.bucketName, this.objectKey); + // 设置下载对象的本地文件路径 + request.setDownloadFile("D:\\test\\test_bigfile_download_5"); + // 设置分段下载时的最大并发数 + request.setTaskNum(5); + // 设置分段大小为10MB + request.setPartSize(2 * 1024 * 1024); + // 开启断点续传模式 + request.setEnableCheckpoint(true); + + request.setProgressInterval(ObsConstraint.DEFAULT_PROGRESS_INTERVAL * 50); + + MonitorableProgressListener progressListener = new MonitorableProgressListener() { + @Override + public void progressChanged(ProgressStatus status) { + System.out.println("TransferPercentage:" + status.getTransferPercentage()); + } + }; + + request.setProgressListener(progressListener); + + progressListener.reset(); + DownloadThread upload = new DownloadThread(obsClient, request); + + upload.start(); + + + try { + int s = 3; + System.out.println(new Date() + " Start Wait " + s + " seconds"); + Thread.sleep(s * 1000); + System.out.println(new Date() + " End wait " + s + " seconds"); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + upload.interrupt(); + System.out.println(new Date() + " end interrupt thread"); + +// while(request.isRunning()) { +// System.out.println(new Date() + " request is running "); +// try { +// Thread.sleep(100); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// } + + try { + progressListener.waitingFinish(Long.MAX_VALUE); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } + + System.out.println(new Date() + " request is not running "); + + System.out.println("==================================== Restart ===================================="); + upload = new DownloadThread(obsClient, request); + progressListener.reset(); + upload.start(); +// try { +// upload.join(); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + + try { + progressListener.waitingFinish(Long.MAX_VALUE); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("==== end"); + } + + class DownloadThread extends Thread { + private DownloadFileRequest request; + private ObsClient obsClient; + + public DownloadThread(ObsClient obsClient, DownloadFileRequest request) { + this.obsClient = obsClient; + this.request = request; + } + + @Override + public void run() { + try { + // 进行断点续传下载 + DownloadFileResult result = this.obsClient.downloadFile(this.request); + } catch (ObsException e) { + // 发生异常时可再次调用断点续传下载接口进行重新下载 + e.printStackTrace(); + if (null != e.getCause() + && e.getCause() instanceof InterruptedException) { + System.out.println("========================== pause =========================="); + } else { + } + } + } + } +} diff --git a/app/src/test/java/com/obs/test/FileTest.java b/app/src/test/java/com/obs/test/FileTest.java new file mode 100644 index 0000000..1f8c34d --- /dev/null +++ b/app/src/test/java/com/obs/test/FileTest.java @@ -0,0 +1,47 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import com.obs.services.ObsClient; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.fs.NewFolderRequest; +import com.obs.services.model.fs.ObsFSFolder; + +public class FileTest { + @Test + public void test_create_newfolder_1() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + String bucketName = "test"; + String objectKey = "%#123"; + NewFolderRequest request = new NewFolderRequest(bucketName, objectKey); + + ObsFSFolder folder = obsClient.newFolder(request); + + System.out.println(folder); + + assertEquals(folder.getObjectKey(), objectKey + "/"); + + ObjectMetadata metadata1 = obsClient.getObjectMetadata(bucketName, objectKey + "/"); + System.out.println(metadata1); + + ObjectMetadata metadata2 = obsClient.getObjectMetadata(bucketName, objectKey); + } +} diff --git a/app/src/test/java/com/obs/test/LimitedTimeSecurityKeyTest.java b/app/src/test/java/com/obs/test/LimitedTimeSecurityKeyTest.java new file mode 100644 index 0000000..ee024bd --- /dev/null +++ b/app/src/test/java/com/obs/test/LimitedTimeSecurityKeyTest.java @@ -0,0 +1,77 @@ +package com.obs.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Calendar; +import java.util.Date; + +import org.junit.Test; + +import com.obs.services.internal.security.LimitedTimeSecurityKey; + +public class LimitedTimeSecurityKeyTest { + @Test + public void test_get_set() { + LimitedTimeSecurityKey limitedTimeSecurityKey = new LimitedTimeSecurityKey("accessKey", "secretKey", "securityToken"); + + assertEquals(limitedTimeSecurityKey.getAccessKey(), "accessKey"); + assertEquals(limitedTimeSecurityKey.getSecretKey(), "secretKey"); + assertEquals(limitedTimeSecurityKey.getSecurityToken(), "securityToken"); + } + + @Test + public void now_is_about_to_expire_false() { + // 获取当前时间 + Date expiryDate = getUtcTime(0); + + LimitedTimeSecurityKey limitedTimeSecurityKey = new LimitedTimeSecurityKey("accessKey", "secretKey", "securityToken", expiryDate); + + assertFalse(limitedTimeSecurityKey.aboutToExpire()); + assertTrue(limitedTimeSecurityKey.willSoonExpire()); + } + + @Test + public void before_10s_about_to_expire_false() { + // 获取当前时间 + Date expiryDate = getUtcTime(10); + + LimitedTimeSecurityKey limitedTimeSecurityKey = new LimitedTimeSecurityKey("accessKey", "secretKey", "securityToken", expiryDate); + + assertFalse(limitedTimeSecurityKey.aboutToExpire()); + assertTrue(limitedTimeSecurityKey.willSoonExpire()); + } + + @Test + public void before_70s_about_to_expire_false() { + // 获取当前时间 + Date expiryDate = getUtcTime(130); + + LimitedTimeSecurityKey limitedTimeSecurityKey = new LimitedTimeSecurityKey("accessKey", "secretKey", "securityToken", expiryDate); + + assertTrue(limitedTimeSecurityKey.aboutToExpire()); + assertFalse(limitedTimeSecurityKey.willSoonExpire()); + } + + @Test + public void before_350s_about_to_expire_false() { + // 获取当前时间 + Date expiryDate = getUtcTime(350); + + LimitedTimeSecurityKey limitedTimeSecurityKey = new LimitedTimeSecurityKey("accessKey", "secretKey", "securityToken", expiryDate); + + assertFalse(limitedTimeSecurityKey.aboutToExpire()); + assertFalse(limitedTimeSecurityKey.willSoonExpire()); + } + + private Date getUtcTime(int amount) { + Calendar calendar = Calendar.getInstance(); + int offset = calendar.get(Calendar.ZONE_OFFSET); + calendar.add(Calendar.MILLISECOND, -offset); + + calendar.add(Calendar.SECOND, amount); + + return calendar.getTime(); + } +} diff --git a/app/src/test/java/com/obs/test/LogableCase.java b/app/src/test/java/com/obs/test/LogableCase.java new file mode 100644 index 0000000..43b78bf --- /dev/null +++ b/app/src/test/java/com/obs/test/LogableCase.java @@ -0,0 +1,46 @@ +package com.obs.test; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.ThreadContext; + +public abstract class LogableCase { + + private Logger logger; + + private String caseName; + + public LogableCase(Logger logger, String caseName) { + this.logger = logger; + this.caseName = caseName; + } + + abstract void action(); + + /** + * 执行测试,并记录日志 + */ + public final void doTest() { + ThreadContext.put("caseName", this.getCaseName()); + logger.info("===================== Start TestCase. ====================="); + String result = "successfully"; + try{ + action(); + } catch(Exception e) { + logger.error("Failed."); + e.printStackTrace(); + result = "failure"; + throw e; + } finally { + logger.info("End TestCase, Execute " + result); + ThreadContext.put("caseName", "No Case."); + } + } + + public final Logger getLogger() { + return logger; + } + + public final String getCaseName() { + return caseName; + } +} diff --git a/app/src/test/java/com/obs/test/OBSClientInitTest.java b/app/src/test/java/com/obs/test/OBSClientInitTest.java new file mode 100644 index 0000000..4ab87dd --- /dev/null +++ b/app/src/test/java/com/obs/test/OBSClientInitTest.java @@ -0,0 +1,47 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.test; + +import org.junit.Test; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.SecretFlexibleObsClient; + +public class OBSClientInitTest { + @Test + public void init_client_1() { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint("test"); + ObsClient client = new ObsClient(config); + } + + @Test + public void init_client_2() { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint("test"); + SecretFlexibleObsClient client = new SecretFlexibleObsClient(config); + } + + @Test + public void init_client_3() { + SecretFlexibleObsClient client = new SecretFlexibleObsClient("endpoint"); + } +} diff --git a/app/src/test/java/com/obs/test/ObjectsTest.java b/app/src/test/java/com/obs/test/ObjectsTest.java new file mode 100644 index 0000000..1977a75 --- /dev/null +++ b/app/src/test/java/com/obs/test/ObjectsTest.java @@ -0,0 +1,298 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test; + +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import com.obs.services.model.ListVersionsRequest; +import org.junit.Test; + +import com.obs.services.ObsClient; +import com.obs.services.exception.ObsException; +import com.obs.services.model.CopyObjectResult; +import com.obs.services.model.GetObjectRequest; +import com.obs.services.model.HttpMethodEnum; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ListVersionsResult; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.ObsObject; +import com.obs.services.model.PutObjectRequest; +import com.obs.services.model.SetObjectMetadataRequest; +import com.obs.services.model.TemporarySignatureRequest; +import com.obs.services.model.TemporarySignatureResponse; + +public class ObjectsTest { + private static String bucketName = "osm-bj4"; + + @Test + public void test_set_object_metadata() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + SetObjectMetadataRequest request = new SetObjectMetadataRequest(bucketName, "1"); + request.getMetadata().put("property1", "property-value1"); + request.getMetadata().put("property2", "%#123"); + ObjectMetadata metadata = obsClient.setObjectMetadata(request); + + System.out.println(metadata); + + System.out.println(metadata.getUserMetadata("property1")); + System.out.println(metadata.getMetadata().get("property1")); + } + + @Test + public void test_get_object_metadata_1() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + ObjectMetadata metadata = obsClient.getObjectMetadata(bucketName, "s3curl.txt"); + + System.out.println(metadata); + + assertNotNull(metadata); + } + + @Test + public void test_get_object_metadata_2() { + ObsClient obsClient = TestTools.getExternalEnvironment(); + + ObjectMetadata metadata = obsClient.getObjectMetadata(bucketName, "/DES1597308083694/第一次备份文件/10.70.3.41/F/F/产品处/02_产品开发/04 结项存档/2012年/银代/金如意D/1130/新光海航金如意D款两全保险(分红型)-67LOADING/新光海航金如意D款两全保险(分红型)精算报告.doc"); + + System.out.println(metadata); + + assertNotNull(metadata); + } + + @Test + public void test_download_object_metadata() { + ObsClient obsClient = TestTools.getInnerTempEnvironment(); + + ObsObject metadata = obsClient.getObject(bucketName, + "00d451d484662462a0ea6d7e507dd48c-87-tcmeitulongmixwithobsfunc002-long-1-1"); + + System.out.println(metadata); + + assertNotNull(metadata); + } + + @Test + public void test_put_object_and_set_acl() { + ObsClient obsClient = TestTools.getInnerTempEnvironment(); + + ObsObject metadata = obsClient.getObject(bucketName, + "00d451d484662462a0ea6d7e507dd48c-87-tcmeitulongmixwithobsfunc002-long-1-1"); + + System.out.println(metadata); + + assertNotNull(metadata); + } + + @Test + public void test_put_object_base() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + File file = new File("C:\\Users\\xxx\\Desktop\\现网问题\\123\\test.file.css"); + + PutObjectRequest request = new PutObjectRequest(bucketName, "test.file.css", file); + + obsClient.putObject(request); + + } + + @Test + public void test_put_object_metadata() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + File file = new File("D:\\5MB"); + + // Case-1 + PutObjectRequest request = new PutObjectRequest(bucketName, "test.file.css", file); + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("mytest,file=test"); + metadata.setCacheControl("no-cache-me"); + metadata.setContentEncoding("test-encoding"); + metadata.setContentType("test/type"); + metadata.setContentLanguage("test-language-zh-CN"); + metadata.setExpires("test-expires"); + request.setMetadata(metadata); + + obsClient.putObject(request); + + ObjectMetadata metadata2 = obsClient.getObjectMetadata(bucketName, "test.file.css"); + + System.out.println(metadata2); + assertEquals(metadata2.getContentEncoding(), "test-encoding"); + assertEquals(metadata2.getContentType(), "test/type"); + assertEquals(metadata2.getContentDisposition(), "mytest,file=test"); + assertEquals(metadata2.getCacheControl(), "no-cache-me"); + assertEquals(metadata2.getContentLanguage(), "test-language-zh-CN"); + assertEquals(metadata2.getExpires(), "test-expires"); + + + // Case-2 + request = new PutObjectRequest(bucketName, "test.file.css", file); + + metadata = new ObjectMetadata(); + request.setMetadata(metadata); + + obsClient.putObject(request); + + metadata2 = obsClient.getObjectMetadata(bucketName, "test.file.css"); + System.out.println(metadata2); + assertEquals(metadata2.getContentEncoding(), null); + assertEquals(metadata2.getContentDisposition(), null); + assertEquals(metadata2.getCacheControl(), null); + assertEquals(metadata2.getExpires(), null); + assertEquals(metadata2.getContentLanguage(), null); + assertEquals(metadata2.getContentType(), "text/css"); + } + + @Test + public void test_get_object_base() throws IOException { + ObsClient obsClient = TestTools.getExternalEnvironment(); + + ObsObject obsObject = obsClient.getObject(bucketName, "/202009070920312367/20200906122041.zip"); + + // 读取对象内容 + System.out.println("Object content:"); + InputStream input = obsObject.getObjectContent(); + + int byteread = 0; + + FileOutputStream fs = null; + try { + fs = new FileOutputStream("C:\\Users\\xxxxxxxxxx\\Desktop\\abc.zip"); + + byte[] buffer = new byte[1204]; + int length; + while ((byteread = input.read(buffer)) != -1) { + fs.write(buffer, 0, byteread); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + fs.close(); + } + } + + @Test + public void test_get_object_by_range() throws IOException { + ObsClient obsClient = TestTools.getExternalEnvironment(); + + GetObjectRequest request = new GetObjectRequest("wjb-test-browserplus-cn-north-1", "obs_browser-plus.rar"); + request.setRangeStart(10L); + request.setRangeEnd(1000L); + ObsObject obsObject = obsClient.getObject(request); + + // 读取对象内容 + System.out.println("Object content:"); + InputStream input = obsObject.getObjectContent(); + + int byteread = 0; + + FileOutputStream fs = null; + try { + fs = new FileOutputStream("C:\\Users\\xxxxxxxxxxx\\Desktop\\png\\abc.gif"); + + byte[] buffer = new byte[1204]; + int length; + while ((byteread = input.read(buffer)) != -1) { + fs.write(buffer, 0, byteread); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + fs.close(); + } + } + + @Test + public void test_copy_object() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + + // ObsObject metadata = obsClient.getObject(bucketName, + // "00d451d484662462a0ea6d7e507dd48c-87-tcmeitulongmixwithobsfunc002-long-1-1"); + + try { + CopyObjectResult result = obsClient.copyObject("sourcebucketname", "sourceobjectname", "destbucketname", + "destobjectname"); + + System.out.println("\t" + result.getStatusCode()); + System.out.println("\t" + result.getEtag()); + } catch (ObsException e) { + System.out.println("HTTP Code: " + e.getResponseCode()); + System.out.println("Error Code:" + e.getErrorCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + + System.out.println("Request ID:" + e.getErrorRequestId()); + System.out.println("Host ID:" + e.getErrorHostId()); + } + } + + @Test + public void test_temporarySignatureRequest_for_image() { + ObsClient obsClient = TestTools.getExternalEnvironment(); + + long expireSeconds = 3600L; + + TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds); + request.setBucketName("bucketname"); + request.setObjectKey("objectname.jpg"); + + // 设置图片处理参数,对图片依次进行缩放、旋转 + Map queryParams = new HashMap(); + queryParams.put("x-image-process", "image/resize,m_fixed,w_100,h_100/rotate,90"); + request.setQueryParams(queryParams); + + // 生成临时授权URL + TemporarySignatureResponse response = obsClient.createTemporarySignature(request); + System.out.println(response.getSignedUrl()); + } + + @Test + public void initiateMultipartUpload() { + ObsClient obsClient = TestTools.getExternalEnvironment(); + + String objectKey = "initiateMultipartUpload_test_"; + for (int i = 0; i < 1000; i++) { + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest("shenqing-test-sdk-obs-0000001", + objectKey + "-" + i); + InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request); + System.out.println(result.getObjectKey() + "; " + result.getUploadId()); + } + } + + @Test + public void test_list_version_base() throws IOException { + ObsClient obsClient = TestTools.getExternalEnvironment(); + + ListVersionsResult result = obsClient.listVersions("test-version-002"); + } +} diff --git a/app/src/test/java/com/obs/test/RequestPaymentTest.java b/app/src/test/java/com/obs/test/RequestPaymentTest.java new file mode 100644 index 0000000..de02b41 --- /dev/null +++ b/app/src/test/java/com/obs/test/RequestPaymentTest.java @@ -0,0 +1,2316 @@ +package com.obs.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +import com.obs.services.ObsClient; +import com.obs.services.exception.ObsException; +import com.obs.services.model.AbortMultipartUploadRequest; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.AppendObjectRequest; +import com.obs.services.model.BaseBucketRequest; +import com.obs.services.model.BucketDirectColdAccess; +import com.obs.services.model.BucketEncryption; +import com.obs.services.model.BucketLoggingConfiguration; +import com.obs.services.model.BucketMetadataInfoRequest; +import com.obs.services.model.BucketNotificationConfiguration; +import com.obs.services.model.BucketQuota; +import com.obs.services.model.BucketStoragePolicyConfiguration; +import com.obs.services.model.BucketTagInfo; +import com.obs.services.model.BucketTagInfo.TagSet; +import com.obs.services.model.BucketTypeEnum; +import com.obs.services.model.BucketVersioningConfiguration; +import com.obs.services.model.CanonicalGrantee; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.CompleteMultipartUploadResult; +import com.obs.services.model.CopyObjectRequest; +import com.obs.services.model.CopyObjectResult; +import com.obs.services.model.DeleteObjectRequest; +import com.obs.services.model.DeleteObjectResult; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.DeleteObjectsResult; +import com.obs.services.model.DownloadFileRequest; +import com.obs.services.model.DownloadFileResult; +import com.obs.services.model.GenericRequest; +import com.obs.services.model.GetObjectAclRequest; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.GetObjectRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ListMultipartUploadsRequest; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ListPartsRequest; +import com.obs.services.model.ListPartsResult; +import com.obs.services.model.ListVersionsRequest; +import com.obs.services.model.ListVersionsResult; +import com.obs.services.model.ModifyObjectRequest; +import com.obs.services.model.MultipartUploadListing; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.ObsObject; +import com.obs.services.model.OptionsInfoRequest; +import com.obs.services.model.Permission; +import com.obs.services.model.ProtocolEnum; +import com.obs.services.model.PutObjectResult; +import com.obs.services.model.PutObjectsRequest; +import com.obs.services.model.Redirect; +import com.obs.services.model.RenameObjectRequest; +import com.obs.services.model.ReplicationConfiguration; +import com.obs.services.model.RequestPaymentConfiguration; +import com.obs.services.model.RequestPaymentEnum; +import com.obs.services.model.RestoreObjectRequest; +import com.obs.services.model.RestoreObjectsRequest; +import com.obs.services.model.RestoreTierEnum; +import com.obs.services.model.RouteRule; +import com.obs.services.model.RouteRuleCondition; +import com.obs.services.model.RuleStatusEnum; +import com.obs.services.model.SSEAlgorithmEnum; +import com.obs.services.model.SetBucketAclRequest; +import com.obs.services.model.SetBucketCorsRequest; +import com.obs.services.model.SetBucketDirectColdAccessRequest; +import com.obs.services.model.SetBucketEncryptionRequest; +import com.obs.services.model.SetBucketLifecycleRequest; +import com.obs.services.model.SetBucketLoggingRequest; +import com.obs.services.model.SetBucketNotificationRequest; +import com.obs.services.model.SetBucketPolicyRequest; +import com.obs.services.model.SetBucketQuotaRequest; +import com.obs.services.model.SetBucketReplicationRequest; +import com.obs.services.model.SetBucketStoragePolicyRequest; +import com.obs.services.model.SetBucketTaggingRequest; +import com.obs.services.model.SetBucketVersioningRequest; +import com.obs.services.model.SetBucketWebsiteRequest; +import com.obs.services.model.SetObjectAclRequest; +import com.obs.services.model.SetObjectMetadataRequest; +import com.obs.services.model.StorageClassEnum; +import com.obs.services.model.TaskProgressStatus; +import com.obs.services.model.TruncateObjectRequest; +import com.obs.services.model.UploadFileRequest; +import com.obs.services.model.UploadPartRequest; +import com.obs.services.model.UploadPartResult; +import com.obs.services.model.VersioningStatusEnum; +import com.obs.services.model.WebsiteConfiguration; +import com.obs.services.model.fs.DropFolderRequest; +import com.obs.services.model.fs.FSStatusEnum; +import com.obs.services.model.fs.GetBucketFSStatusRequest; +import com.obs.services.model.fs.NewFileRequest; +import com.obs.services.model.fs.NewFolderRequest; +import com.obs.services.model.fs.ReadFileRequest; +import com.obs.services.model.fs.ReadFileResult; +import com.obs.services.model.fs.RenameRequest; +import com.obs.services.model.fs.SetBucketFSStatusRequest; +import com.obs.services.model.fs.TruncateFileRequest; +import com.obs.services.model.fs.WriteFileRequest; +import com.obs.test.objects.BaseObjectTest; +import com.obs.test.tools.BucketTools; +import com.obs.test.tools.FileTools; +import com.obs.test.tools.ObjectTools; + +/** + * 405:
    +
    test_get_bucket_tagging +
    test_delete_bucket_tagging +
    test_set_bucket_tagging +
    test_rename_object +
    test_rename_file +
    test_rename_folder +
    test_delete_bucket_direct_cold_access +
    test_set_bucket_direct_cold_access +
    test_get_bucket_direct_cold_access +
    test_get_bucket_storage_policy +
    test_set_bucket_storage_policy +
    test_restore_object + * + */ + +public class RequestPaymentTest extends BaseObjectTest { + + private static final Logger logger = LogManager.getLogger(RequestPaymentTest.class); + + static UserInfo owner; + static UserInfo requester; + + static String bucketName_obs; + static String bucketName_pfs; + +public static class CaseInfo { + + private int expectSuccessCode; + private GenericRequest request; + private String bucketName; + + private UserInfo owner; + private UserInfo requester; + + public CaseInfo(UserInfo _owner, UserInfo _requester, String _bucketName, GenericRequest _request, + int _expectSuccessCode) { + this.expectSuccessCode = _expectSuccessCode; + this.request = _request; + this.bucketName = _bucketName; + this.owner = _owner; + this.requester = _requester; + } + + public int getExpectSuccessCode() { + return expectSuccessCode; + } + + public GenericRequest getRequest() { + return request; + } + + public String getBucketName() { + return bucketName; + } + + public UserInfo getOwner() { + return owner; + } + + public UserInfo getRequester() { + return requester; + } + } + + abstract class TestRequestPayment extends LogableCase { + private CaseInfo caseInfo; + + public TestRequestPayment(CaseInfo caseInfo, Logger logger, String caseName) { + super(logger, caseName); + this.caseInfo = caseInfo; + } + + public void doBeforeAction() { + } + + abstract T action(ObsClient obsClient); + +// public void doAfterException(ObsException e) { +// +// } + + /** + * 当前关闭请求者付费的时候,实施的检测 + * @param response + */ + public void checkResultWhilePaymentByOwner(T response) { + if(response instanceof HeaderResponse) { + assertEquals(this.getExpectSuccessCode(), ((HeaderResponse)response).getStatusCode()); + } + } + + /** + * 当前开启请求者付费的时候,实施的检测 + * @param response + */ + public void checkResultWhilePaymentByRequester(T response) { + if(response instanceof HeaderResponse) { + assertRequestPayerResult(this.getExpectSuccessCode(), ((HeaderResponse)response)); + } + } + + /** + * 当前开启请求者付费,失败之后的检测 + * @param response + */ + public void checkPaymentByRequesterException(ObsException exception) { + assertNotNull(exception); + exception = isRequestPayerDeniedException(exception); + } + + /** + * 执行测试 + */ + public final void action() { + // step-1:关闭请求者付费,通过其他请求者来请求的时候,请求正常 + BucketTools.setBucketRequestPayment(this.getOwner().getObsClient(), this.getBucketName(), + RequestPaymentEnum.BUCKET_OWNER, false); + this.getLogger().info("close bucket request payment."); + + doBeforeAction(); + this.getLogger().info("do case : " + this.getCaseName() + " while bucket requestpayment is closed , and request parameter is : " + this.getRequest()); + T response = this.action(this.getRequester().getObsClient()); + this.getLogger().info("do case : " + this.getCaseName() + " while bucket requestpayment is closed , and result is : " + response); + checkResultWhilePaymentByOwner(response); +// doAfterAction(response); + + // step-2:开启请求者付费,不设置头域,请求预期收到403 RequestPayerDenied + BucketTools.setBucketRequestPayment(this.getOwner().getObsClient(), this.getBucketName(), + RequestPaymentEnum.REQUESTER, false); + this.getLogger().info("open bucket request payment."); + + ObsException exception = null; + try { + this.getRequest().setRequesterPays(false); + this.getLogger().info("do case : " + this.getCaseName() + " while bucket requestpayment is open , and request parameter is : " + this.getRequest()); + + doBeforeAction(); + response = this.action(this.getRequester().getObsClient()); + } catch (ObsException e) { + exception = e; + } + checkPaymentByRequesterException(exception); +// doAfterException(exception); + + // step-3:开启请求者付费,设置头域,请求预期正常 + this.getRequest().setRequesterPays(true); + doBeforeAction(); + this.getLogger().info("do case : " + this.getCaseName() + " while bucket requestpayment is open , and request parameter is : " + this.getRequest()); + response = this.action(this.getRequester().getObsClient()); + checkResultWhilePaymentByRequester(response); + this.getLogger().info("do case : " + this.getCaseName() + " while bucket requestpayment is open , and result is : " + response); +// doAfterAction(response); + } + + public int getExpectSuccessCode() { + return this.caseInfo.getExpectSuccessCode(); + } + + public GenericRequest getRequest() { + return this.caseInfo.getRequest(); + } + + public String getBucketName() { + return this.caseInfo.getBucketName(); + } + + public UserInfo getOwner() { + return this.caseInfo.getOwner(); + } + + public UserInfo getRequester() { + return this.caseInfo.getRequester(); + } + } + + @BeforeClass + public static void init() { + + owner = new UserInfo(TestTools.getRequestPaymentEnvironment_User1(), "domainiddomainiddomainiddo000012", + "xxxxxxxxxxxxxxxxxxx"); + requester = new UserInfo(TestTools.getRequestPaymentEnvironment_User2(), "domainiddomainiddomainiddo000011", + "xxxxxxxxxxxxxxxxxxxxx"); + + bucketName_obs = RequestPaymentTest.class.getName().replaceAll("\\.", "-").toLowerCase() + "-obs"; + + bucketName_pfs = RequestPaymentTest.class.getName().replaceAll("\\.", "-").toLowerCase() + "-pfs"; + + // 强制上一个桶 + BucketTools.deleteBucket(owner.getObsClient(), bucketName_obs, true); + BucketTools.deleteBucket(owner.getObsClient(), bucketName_pfs, true); + + // 创建桶 + BucketTools.createBucket(owner.getObsClient(), bucketName_obs, BucketTypeEnum.OBJECT); + BucketTools.createBucket(owner.getObsClient(), bucketName_pfs, BucketTypeEnum.PFS); + + // 设置桶权限 + BucketTools.setBucketAcl(owner.getObsClient(), owner.getDomainId(), requester.getDomainId(), bucketName_obs, true, + false); + BucketTools.setBucketAcl(owner.getObsClient(), owner.getDomainId(), requester.getDomainId(), bucketName_pfs, true, + false); + } + + // @AfterClass + public static void clear() { + // 强制删除一个桶 + BucketTools.deleteBucket(owner.getObsClient(), bucketName_obs, true); + BucketTools.deleteBucket(owner.getObsClient(), bucketName_pfs, true); + } + + @Before + public void beforeCase() { + // 设置桶权限 + logger.info("set all bucket policy"); + setAllBucketPolicy(owner, requester, bucketName_obs); + setAllBucketPolicy(owner, requester, bucketName_pfs); + } + + @After + public void afterCase() { + // 删除桶策略 + logger.info("delete all bucket policy"); + deleteBucketPolicy(owner, bucketName_obs); + deleteBucketPolicy(owner, bucketName_pfs); + + // 关闭桶的请求者付费 + logger.info("close bucket request payment"); + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.BUCKET_OWNER, false); + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_pfs, RequestPaymentEnum.BUCKET_OWNER, false); + } + + /** + * 验证是否是请求者付费的异常,并将异常设置为null + * + * @param exception + */ + private ObsException isRequestPayerDeniedException(ObsException exception) { + if(null != exception.getErrorCode()) { + assertEquals("RequestPayerDenied", exception.getErrorCode()); + } else { + assertEquals("RequestPayerDenied", exception.getResponseHeaders().get("error-code")); + } + + return null; + } + + private void assertRequestPayerResult(int expectCode, HeaderResponse result) { + assertEquals(expectCode, result.getStatusCode()); + assertEquals("requester", result.getResponseHeaders().get("request-charged")); + } + + @Test + public void test_set_get_bucket_request_payment_by_owner() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.BUCKET_OWNER, false); + RequestPaymentConfiguration config = BucketTools.getBucketRequestPayment(owner.getObsClient(), bucketName_obs, + false); + assertEquals(config.getPayer(), RequestPaymentEnum.BUCKET_OWNER); + + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + config = BucketTools.getBucketRequestPayment(owner.getObsClient(), bucketName_obs, false); + assertEquals(config.getPayer(), RequestPaymentEnum.REQUESTER); + } + + /** + * + */ + @Test + public void test_set_get_bucket_request_payment_by_requester() { + setBucketRequestPaymentPolicy(owner, requester, bucketName_obs); + BucketTools.setBucketRequestPayment(requester.getObsClient(), bucketName_obs, RequestPaymentEnum.BUCKET_OWNER, + false); + RequestPaymentConfiguration config = BucketTools.getBucketRequestPayment(requester.getObsClient(), bucketName_obs, + false); + assertEquals(config.getPayer(), RequestPaymentEnum.BUCKET_OWNER); + + BucketTools.setBucketRequestPayment(requester.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + ObsException exception = null; + try { + BucketTools.setBucketRequestPayment(requester.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, + false); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + HeaderResponse result = BucketTools.setBucketRequestPayment(requester.getObsClient(), bucketName_obs, + RequestPaymentEnum.REQUESTER, true); + assertRequestPayerResult(200, result); + + exception = null; + try { + config = BucketTools.getBucketRequestPayment(requester.getObsClient(), bucketName_obs, false); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + config = BucketTools.getBucketRequestPayment(requester.getObsClient(), bucketName_obs, true); + assertEquals(config.getPayer(), RequestPaymentEnum.REQUESTER); + } + + /** + * CaseName:未开启请求者付费的情况下,上传对象
    + * Step.:
    + * 1、关闭请求者付费;
    + * 2、采用requester上传对象;
    + * 3、预期成功
    + */ + @Test + public void test_put_object_on_bucket_requestpayment_off() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.BUCKET_OWNER, false); + + PutObjectResult result = null; + try { + String objectKey = "test_put_object_on_bucket_requestpayment_off"; + result = generateTestObject(requester, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + } catch (ObsException e) { + e.printStackTrace(); + } + + assertEquals(200, result.getStatusCode()); + } + + /** + * CaseName:开启请求者付费的情况下,上传对象
    + * Step.:
    + * 1、开启请求者付费;
    + * 2、采用requester上传对象,并且不设置请求者付费头域;
    + * 3、预期失败:403 RequestPayerDenied
    + */ + @Test + public void test_put_object_on_bucket_requestpayment_on_and_request_off() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + PutObjectResult result = null; + ObsException exception = null; + try { + String objectKey = "test_put_object_on_bucket_requestpayment_on_and_request_off"; + result = generateTestObject(requester, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + } catch (ObsException e) { + exception = e; + } + + exception = isRequestPayerDeniedException(exception); + } + + /** + * CaseName:开启请求者付费的情况下,上传对象
    + * Step.:
    + * 1、开启请求者付费;
    + * 2、采用requester上传对象,并且设置请求者付费头域;
    + * 3、预期成功
    + */ + @Test + public void test_put_object_on_bucket_requestpayment_on_and_request_on() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + PutObjectResult result = null; + try { + String objectKey = "test_put_object_on_bucket_requestpayment_on_and_request_on"; + result = generateTestObject(requester, bucketName_obs, objectKey, true, 1 * 1024 * 1024); + } catch (ObsException e) { + e.printStackTrace(); + } + + assertRequestPayerResult(200, result); + } + + /** + * CaseName:删除对象
    + * Step.:
    + * 1、开启请求者付费;
    + * 2、通过owner上传一个对象;
    + * 3、通过requester删除对象,并且不设置请求者付费头域,预期失败:403 RequestPayerDenied
    + * 4、通过requester删除对象,并且设置请求者付费头域,预期成功。 + */ + @Test + public void test_delete_object_and_object_create_by_owner() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_delete_object_and_object_create_by_owner"; + + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + + ObsException exception = null; + DeleteObjectRequest deleteRequest = new DeleteObjectRequest(bucketName_obs, objectKey, null); + try { + requester.getObsClient().deleteObject(deleteRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + deleteRequest.setRequesterPays(true); + DeleteObjectResult deleteResult = requester.getObsClient().deleteObject(deleteRequest); + assertRequestPayerResult(204, deleteResult); + } + + /** + * CaseName:删除对象
    + * Step.:
    + * 1、开启请求者付费;
    + * 2、通过requester上传一个对象;
    + * 3、通过requester删除对象,并且不设置请求者付费头域,预期失败:403 RequestPayerDenied
    + * 4、通过requester删除对象,并且设置请求者付费头域,预期成功。 + */ + @Test + public void test_delete_object_and_object_create_by_requester() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_delete_object_and_object_create_by_requester"; + + PutObjectResult putResult = generateTestObject(requester, bucketName_obs, objectKey, true, 1 * 1024 * 1024); + assertRequestPayerResult(200, putResult); + + ObsException exception = null; + DeleteObjectRequest deleteRequest = new DeleteObjectRequest(bucketName_obs, objectKey, null); + try { + requester.getObsClient().deleteObject(deleteRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + deleteRequest.setRequesterPays(true); + DeleteObjectResult deleteResult = requester.getObsClient().deleteObject(deleteRequest); + assertRequestPayerResult(204, deleteResult); + } + + /** + * CaseName:批量删除对象
    + * Step.:
    + * 1、开启请求者付费;
    + * 2、通过owner上传两个个对象;
    + * 3、通过requester批量删除对象,并且不设置请求者付费头域,预期失败:403 RequestPayerDenied
    + * 4、通过requester批量删除对象,并且设置请求者付费头域,预期成功。 + */ + @Test + public void test_batch_delete_object_and_object_create_by_owner() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_batch_delete_object_and_object_create_by_owner"; + + generateTestObject(owner, bucketName_obs, objectKey + "-1", false, 1 * 1024 * 1024); + generateTestObject(owner, bucketName_obs, objectKey + "-2", false, 1 * 1024 * 1024); + + ObsException exception = null; + DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName_obs); + deleteRequest.addKeyAndVersion(objectKey + "-1"); + deleteRequest.addKeyAndVersion(objectKey + "-2"); + try { + requester.getObsClient().deleteObjects(deleteRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + deleteRequest.setRequesterPays(true); + DeleteObjectsResult deleteResult = requester.getObsClient().deleteObjects(deleteRequest); + assertRequestPayerResult(200, deleteResult); + } + + /** + * CaseName:批量删除对象
    + * Step.:
    + * 1、开启请求者付费;
    + * 2、通过requester上传两个个对象;
    + * 3、通过requester批量删除对象,并且不设置请求者付费头域,预期失败:403 RequestPayerDenied
    + * 4、通过requester批量删除对象,并且设置请求者付费头域,预期成功。 + */ + @Test + public void test_batch_delete_object_and_object_create_by_requester() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_batch_delete_object_and_object_create_by_requester()"; + + PutObjectResult putResult = generateTestObject(requester, bucketName_obs, objectKey + "-1", true, 1 * 1024 * 1024); + assertRequestPayerResult(200, putResult); + + putResult = generateTestObject(requester, bucketName_obs, objectKey + "-2", true, 1 * 1024 * 1024); + assertRequestPayerResult(200, putResult); + + ObsException exception = null; + DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName_obs); + deleteRequest.addKeyAndVersion(objectKey + "-1"); + deleteRequest.addKeyAndVersion(objectKey + "-2"); + try { + requester.getObsClient().deleteObjects(deleteRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + deleteRequest.setRequesterPays(true); + DeleteObjectsResult deleteResult = requester.getObsClient().deleteObjects(deleteRequest); + assertRequestPayerResult(200, deleteResult); + } + + /** + * CaseName:列举对象
    + * Step.:
    + * 1、开启请求者付费;
    + * 2、通过owner上传两个个对象;
    + * 3、通过requester列举对象,并且不设置请求者付费头域,预期失败:403 RequestPayerDenied
    + * 4、通过requester列举对象,并且设置请求者付费头域,预期成功。 + */ + @Test + public void test_list_object_and_object_create_by_owner() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_list_object_and_object_create_by_owner"; + + generateTestObject(owner, bucketName_obs, objectKey + "-1", false, 1 * 1024 * 1024); + generateTestObject(owner, bucketName_obs, objectKey + "-2", false, 1 * 1024 * 1024); + + ObsException exception = null; + ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName_obs); + try { + requester.getObsClient().listObjects(listObjectsRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + listObjectsRequest.setRequesterPays(true); + ObjectListing listResult = requester.getObsClient().listObjects(listObjectsRequest); + assertRequestPayerResult(200, listResult); + } + + /** + * CaseName:列举对象
    + * Step.:
    + * 1、开启请求者付费;
    + * 2、通过requester上传两个个对象;
    + * 3、通过requester列举对象,并且不设置请求者付费头域,预期失败:403 RequestPayerDenied
    + * 4、通过requester列举对象,并且设置请求者付费头域,预期成功。 + */ + @Test + public void test_list_object_and_object_create_by_requester() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_list_object_and_object_create_by_requester()"; + + PutObjectResult putResult = generateTestObject(requester, bucketName_obs, objectKey + "-1", true, 1 * 1024 * 1024); + assertEquals(200, putResult.getStatusCode()); + assertEquals("requester", putResult.getResponseHeaders().get("request-charged")); + + putResult = generateTestObject(requester, bucketName_obs, objectKey + "-2", true, 1 * 1024 * 1024); + assertRequestPayerResult(200, putResult); + + ObsException exception = null; + ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName_obs); + try { + requester.getObsClient().listObjects(listObjectsRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + listObjectsRequest.setRequesterPays(true); + ObjectListing listResult = requester.getObsClient().listObjects(listObjectsRequest); + assertRequestPayerResult(200, listResult); + } + + /** + * CaseName:获取对象acl
    + * Step.:
    + * 1、开启请求者付费;
    + * 2、通过owner上传一个对象;
    + * 3、通过owner给requester设置acl权限;
    + * 4、通过requester获取acl,并且不设置请求者付费头域,预期失败:403 RequestPayerDenied
    + * 5、通过requester获取acl,并且设置请求者付费头域,预期成功。 + */ + @Test + public void test_get_acl_and_object_create_by_owner() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_set_acl_and_object_create_by_owner"; + + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + // 给requester 设置权限 + ObjectTools.setObjectAcl(owner, requester, bucketName_obs, objectKey); + + GetObjectAclRequest getRequest = new GetObjectAclRequest(bucketName_obs, objectKey, null); + ObsException exception = null; + try { + requester.getObsClient().getObjectAcl(getRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + getRequest.setRequesterPays(true); + requester.getObsClient().getObjectAcl(getRequest); + } + + /** + * CaseName:判断对象是否存在接口
    + * Step.:
    + * 1、开启请求者付费;
    + * 2、通过owner上传一个对象;
    + * 3、通过requester调用判断对象是否存在接口,并且不设置请求者付费头域,预期失败:403 RequestPayerDenied
    + * 4、通过requester调用判断对象是否存在接口,并且设置请求者付费头域,预期成功。 + */ + @Test + public void test_check_object_exist_and_object_create_by_owner() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_check_object_exist_and_object_create_by_owner"; + + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + // 给requester 设置权限 + ObjectTools.setObjectAcl(owner, requester, bucketName_obs, objectKey); + + GetObjectMetadataRequest checkRequest = new GetObjectMetadataRequest(bucketName_obs, objectKey); + Exception exception = null; + try { + requester.getObsClient().doesObjectExist(checkRequest); + } catch (Exception e) { + exception = e; + } + assertNotNull(exception); + exception = null; + + checkRequest.setRequesterPays(true); + boolean result = requester.getObsClient().doesObjectExist(checkRequest); + assertTrue(result); + } + + @Test + public void test_get_object_metadata_exist_and_object_create_by_owner() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_get_object_metadata_exist_and_object_create_by_owner"; + + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + // 给requester 设置权限 + ObjectTools.setObjectAcl(owner, requester, bucketName_obs, objectKey); + + GetObjectMetadataRequest getRequest = new GetObjectMetadataRequest(bucketName_obs, objectKey); + ObsException exception = null; + try { + requester.getObsClient().getObjectMetadata(getRequest); + } catch (ObsException e) { + exception = e; + } + assertNotNull(exception); + exception = null; + + getRequest.setRequesterPays(true); + ObjectMetadata objectMetadata = requester.getObsClient().getObjectMetadata(getRequest); + assertRequestPayerResult(200, objectMetadata); + } + + @Test + public void test_set_object_metadata_exist_and_object_create_by_owner() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_set_object_metadata_exist_and_object_create_by_owner"; + + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + // 给requester 设置权限 + ObjectTools.setObjectAcl(owner, requester, bucketName_obs, objectKey); + setModifyObjectMetaDataPolicy(owner, requester, bucketName_obs); + + SetObjectMetadataRequest setRequest = new SetObjectMetadataRequest(bucketName_obs, objectKey); + setRequest.getMetadata().put("test", "test"); + ObsException exception = null; + try { + requester.getObsClient().setObjectMetadata(setRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + setRequest.getMetadata().put("test-requester", "test-requester"); + setRequest.setRequesterPays(true); + ObjectMetadata metadata = requester.getObsClient().setObjectMetadata(setRequest); + } + + @Test + public void test_download_object_and_object_create_by_owner() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_download_object_and_object_create_by_owner"; + + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + // 给requester 设置权限 + ObjectTools.setObjectAcl(owner, requester, bucketName_obs, objectKey); + + GetObjectRequest getRequest = new GetObjectRequest(bucketName_obs, objectKey); + ObsException exception = null; + try { + requester.getObsClient().getObject(getRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + getRequest.setRequesterPays(true); + ObsObject obsObject = requester.getObsClient().getObject(getRequest); + + assertEquals("requester", obsObject.getMetadata().getMetadata().get("request-charged")); + } + + @Test + public void test_list_versions() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + String objectKey = "test_list_versions"; + + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + // 给requester 设置权限 + ObjectTools.setObjectAcl(owner, requester, bucketName_obs, objectKey); + + ListVersionsRequest listRequest = new ListVersionsRequest(bucketName_obs); + + ObsException exception = null; + try { + requester.getObsClient().listVersions(listRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + listRequest.setRequesterPays(true); + ListVersionsResult result = requester.getObsClient().listVersions(listRequest); + + assertRequestPayerResult(200, result); + } + + @Test + public void test_copy_object_on_bucket_requestpayment_off() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.BUCKET_OWNER, false); + + CopyObjectResult result = null; + try { + + String objectKey = "test_copy_object_on_bucket_requestpayment_off"; + + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + + ObjectTools.setObjectAcl(owner, requester, bucketName_obs, objectKey); + + result = requester.getObsClient().copyObject(generateCopyObjectRequest(bucketName_obs, objectKey)); + + } catch (ObsException e) { + e.printStackTrace(); + } + + assertEquals(200, result.getStatusCode()); + } + + @Test + public void test_copy_object_on_bucket_requestpayment_on_and_request_off() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + CopyObjectResult result = null; + ObsException exception = null; + try { + String objectKey = "test_copy_object_on_bucket_requestpayment_on_and_request_off"; + + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + + ObjectTools.setObjectAcl(owner, requester, bucketName_obs, objectKey); + + CopyObjectRequest copyRequest = generateCopyObjectRequest(bucketName_obs, objectKey); + copyRequest.setRequesterPays(false); + result = requester.getObsClient().copyObject(copyRequest); + } catch (ObsException e) { + exception = e; + } + + exception = isRequestPayerDeniedException(exception); + } + + @Test + public void test_copy_object_on_bucket_requestpayment_on_and_request_on() { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + CopyObjectResult result = null; + try { + String objectKey = "test_copy_object_on_bucket_requestpayment_on_and_request_on"; + + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024 * 1024); + + ObjectTools.setObjectAcl(owner, requester, bucketName_obs, objectKey); + + CopyObjectRequest copyRequest = generateCopyObjectRequest(bucketName_obs, objectKey); + copyRequest.setRequesterPays(true); + result = requester.getObsClient().copyObject(copyRequest); + } catch (ObsException e) { + e.printStackTrace(); + } + + assertRequestPayerResult(200, result); + } + + /** + * 测试初始化分段、上传分段、列举分段、取消分段、合并分段的请求者付费 + * + * @throws IOException + */ + @Test + public void test_multipartUpload_object_on_bucket_requestpayment_on_and_request_on() throws IOException { + BucketTools.setBucketRequestPayment(owner.getObsClient(), bucketName_obs, RequestPaymentEnum.REQUESTER, false); + + ObsException exception = null; + String objectKey = "test_multipartUpload_object_on_bucket_requestpayment_on_and_request_on"; + + ListPartsResult listResult = init_for_test_mulitpartUload(bucketName_obs, objectKey); + AbortMultipartUploadRequest abortRequest = generateAbortMultipartUploadRequest(bucketName_obs, objectKey, + listResult.getUploadId()); + try { + requester.getObsClient().abortMultipartUpload(abortRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + abortRequest.setRequesterPays(true); + HeaderResponse abortResult = requester.getObsClient().abortMultipartUpload(abortRequest); + assertRequestPayerResult(204, abortResult); + + listResult = init_for_test_mulitpartUload(bucketName_obs, objectKey); + CompleteMultipartUploadRequest completeRequest = generateCompleteMultipartUploadRequest(listResult); + try { + requester.getObsClient().completeMultipartUpload(completeRequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + completeRequest.setRequesterPays(true); + assertEquals(listResult.getMultipartList().size(), completeRequest.getPartEtag().size()); + + CompleteMultipartUploadResult completeResult = requester.getObsClient() + .completeMultipartUpload(completeRequest); + assertRequestPayerResult(200, completeResult); + + assertEquals(null, exception); + } + + private ListPartsResult init_for_test_mulitpartUload(String bucketName, String objectKey) throws IOException { + ObsException exception = null; + + InitiateMultipartUploadRequest initrequest = generateInitiateMultipartUploadRequest(bucketName, objectKey); + try { + requester.getObsClient().initiateMultipartUpload(initrequest); + } catch (ObsException e) { + exception = e; + } + assertEquals("RequestPayerDenied", exception.getErrorCode()); + exception = null; + + initrequest.setRequesterPays(true); + InitiateMultipartUploadResult initResult = requester.getObsClient().initiateMultipartUpload(initrequest); + assertRequestPayerResult(200, initResult); + + UploadPartRequest uploadParerequest = generateUploadPartRequest(bucketName, objectKey, initResult.getUploadId(), + 1); + try { + requester.getObsClient().uploadPart(uploadParerequest); + } catch (ObsException e) { + exception = e; + } + exception = isRequestPayerDeniedException(exception); + + uploadParerequest = generateUploadPartRequest(bucketName, objectKey, initResult.getUploadId(), 1); + uploadParerequest.setRequesterPays(true); + UploadPartResult uploadPartResult = requester.getObsClient().uploadPart(uploadParerequest); + assertRequestPayerResult(200, uploadPartResult); + + uploadParerequest = generateUploadPartRequest(bucketName, objectKey, initResult.getUploadId(), 2); + uploadParerequest.setRequesterPays(true); + uploadPartResult = requester.getObsClient().uploadPart(uploadParerequest); + assertRequestPayerResult(200, uploadPartResult); + + ListPartsRequest listRequest = generateListPartsRequest(bucketName, objectKey, initResult.getUploadId()); + try { + requester.getObsClient().listParts(listRequest); + } catch (ObsException e) { + exception = e; + } + isRequestPayerDeniedException(exception); + + listRequest.setRequesterPays(true); + ListPartsResult listResult = requester.getObsClient().listParts(listRequest); + assertRequestPayerResult(200, listResult); + assertEquals(2, listResult.getMultipartList().size()); + + ListMultipartUploadsRequest listMultipartUploadsRequest = generateListMultipartUploadsRequest(bucketName); + try { + requester.getObsClient().listMultipartUploads(listMultipartUploadsRequest); + } catch (ObsException e) { + exception = e; + } + isRequestPayerDeniedException(exception); + + listMultipartUploadsRequest.setRequesterPays(true); + MultipartUploadListing listMultipartUploadsResult = requester.getObsClient() + .listMultipartUploads(listMultipartUploadsRequest); + assertEquals(200, listMultipartUploadsResult.getStatusCode()); + + return listResult; + } + + /** + * CaseName:删除桶
    + * Step.:
    + * 1、owner创建一个桶;
    + * 2、开启请求者付费;
    + * 3、给requester设置权限;
    + * 4、不设置请求者付费头域,采用requester删除桶,预期报错:403 RequestPayerDenied
    + * 5、设置请求者付费头域,采用requester删除桶,预期成功
    + */ + @Test + public void test_delete_bucket() { + String testBucketName = "test-delete-bucket-11111"; + BucketTools.createBucket(owner.getObsClient(), testBucketName, BucketTypeEnum.OBJECT); + setBucketAcl(owner, requester, testBucketName); + setAllBucketPolicy(owner, requester, testBucketName); + + BaseBucketRequest request = new BaseBucketRequest(testBucketName); + + new TestRequestPayment(new CaseInfo(owner, requester, testBucketName, request, 204), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.deleteBucket((BaseBucketRequest) this.getRequest()); + } + + @Override + public void checkResultWhilePaymentByOwner(HeaderResponse response) { + super.checkResultWhilePaymentByOwner(response); + BucketTools.createBucket(this.getOwner().getObsClient(), this.getBucketName(), BucketTypeEnum.OBJECT); + setBucketAcl(this.getOwner(), this.getRequester(), this.getBucketName()); + setAllBucketPolicy(this.getOwner(), this.getRequester(), this.getBucketName()); + }; + + @Override + public void checkResultWhilePaymentByRequester(HeaderResponse response) { + super.checkResultWhilePaymentByRequester(response); + BucketTools.createBucket(this.getOwner().getObsClient(), this.getBucketName(), BucketTypeEnum.OBJECT); + setBucketAcl(this.getOwner(), this.getRequester(), this.getBucketName()); + setAllBucketPolicy(this.getOwner(), this.getRequester(), this.getBucketName()); + }; + }.doTest(); + } + + @Test + public void test_get_bucket_acl() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketAcl((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_set_bucket_acl() { + AccessControlList accessControlList = owner.getObsClient().getBucketAcl(bucketName_obs); + + accessControlList.grantPermission(new CanonicalGrantee(requester.getDomainId()), + Permission.PERMISSION_FULL_CONTROL, true); + + SetBucketAclRequest request = new SetBucketAclRequest(bucketName_obs, accessControlList); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketAcl((SetBucketAclRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_bucket_version() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketVersioning((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_set_bucket_version() { + SetBucketVersioningRequest request = new SetBucketVersioningRequest(bucketName_obs, VersioningStatusEnum.ENABLED); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketVersioning((SetBucketVersioningRequest) this.getRequest()); + } + + @Override + public void checkResultWhilePaymentByOwner(HeaderResponse response) { + super.checkResultWhilePaymentByOwner(response); + BucketVersioningConfiguration status = this.getOwner().getObsClient() + .getBucketVersioning(this.getBucketName()); + assertEquals(VersioningStatusEnum.ENABLED, status.getVersioningStatus()); + } + + @Override + public void checkResultWhilePaymentByRequester(HeaderResponse response) { + super.checkResultWhilePaymentByRequester(response); + BucketVersioningConfiguration status = this.getOwner().getObsClient() + .getBucketVersioning(this.getBucketName()); + assertEquals(VersioningStatusEnum.ENABLED, status.getVersioningStatus()); + } + }.doTest(); + + request = new SetBucketVersioningRequest(bucketName_obs, VersioningStatusEnum.SUSPENDED); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketVersioning((SetBucketVersioningRequest) this.getRequest()); + } + + @Override + public void checkResultWhilePaymentByOwner(HeaderResponse response) { + super.checkResultWhilePaymentByOwner(response); + BucketVersioningConfiguration status = this.getOwner().getObsClient() + .getBucketVersioning(this.getBucketName()); + assertEquals(VersioningStatusEnum.SUSPENDED, status.getVersioningStatus()); + } + + @Override + public void checkResultWhilePaymentByRequester(HeaderResponse response) { + super.checkResultWhilePaymentByRequester(response); + BucketVersioningConfiguration status = this.getOwner().getObsClient() + .getBucketVersioning(this.getBucketName()); + assertEquals(VersioningStatusEnum.SUSPENDED, status.getVersioningStatus()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_get_bucket_tagging() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketTagging((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_set_bucket_tagging() { + TagSet tagSet = new TagSet(); + tagSet.addTag("test1", "test_tag_1"); + BucketTagInfo bucketTagInfo = new BucketTagInfo(tagSet); + SetBucketTaggingRequest request = new SetBucketTaggingRequest(bucketName_obs, bucketTagInfo); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketTagging((SetBucketTaggingRequest) this.getRequest()); + } + + @Override + public void checkResultWhilePaymentByOwner(HeaderResponse response) { + super.checkResultWhilePaymentByOwner(response); + BucketTagInfo tagInfo = this.getOwner().getObsClient() + .getBucketTagging(this.getBucketName()); + assertEquals(1, tagInfo.getTagSet().getTags().size()); + assertEquals("test_tag_1", tagInfo.getTagSet().getTags().get(0).getValue()); + } + + @Override + public void checkResultWhilePaymentByRequester(HeaderResponse response) { + super.checkResultWhilePaymentByRequester(response); + BucketTagInfo tagInfo = this.getOwner().getObsClient() + .getBucketTagging(this.getBucketName()); + assertEquals(1, tagInfo.getTagSet().getTags().size()); + assertEquals("test_tag_1", tagInfo.getTagSet().getTags().get(0).getValue()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_delete_bucket_tagging() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.deleteBucketTagging((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_set_bucket_website() { + WebsiteConfiguration websiteConfig = new WebsiteConfiguration(); + websiteConfig.setSuffix("test.html"); + websiteConfig.setKey("error_test.html"); + SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName_obs, websiteConfig); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketWebsite((SetBucketWebsiteRequest) this.getRequest()); + } + + @Override + public void checkResultWhilePaymentByOwner(HeaderResponse response) { + super.checkResultWhilePaymentByOwner(response); + WebsiteConfiguration config = this.getOwner().getObsClient().getBucketWebsite(new BaseBucketRequest(this.getBucketName())); + assertEquals("test.html", config.getSuffix()); + assertEquals("error_test.html", config.getKey()); + } + + @Override + public void checkResultWhilePaymentByRequester(HeaderResponse response) { + super.checkResultWhilePaymentByRequester(response); + WebsiteConfiguration config = this.getOwner().getObsClient().getBucketWebsite(new BaseBucketRequest(this.getBucketName())); + assertEquals("test.html", config.getSuffix()); + assertEquals("error_test.html", config.getKey()); + } + }.doTest(); + } + + @Test + public void test_delete_bucket_website() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 204), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.deleteBucketWebsite((BaseBucketRequest) this.getRequest()); + } + + @Override + public void doBeforeAction() { + WebsiteConfiguration websiteConfig = new WebsiteConfiguration(); + websiteConfig.setSuffix("test.html"); + websiteConfig.setKey("error_test.html"); + SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName_obs, websiteConfig); + this.getOwner().getObsClient().setBucketWebsite(request); + } + + }.doTest(); + } + + @Test + public void test_get_bucket_website() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketWebsite((BaseBucketRequest) this.getRequest()); + } + + @Override + public void doBeforeAction() { + WebsiteConfiguration websiteConfig = new WebsiteConfiguration(); + websiteConfig.setSuffix("test.html"); + websiteConfig.setKey("error_test.html"); + + RouteRule rule = new RouteRule(); + Redirect r = new Redirect(); + r.setHostName("www.example.com"); + r.setHttpRedirectCode("305"); + r.setRedirectProtocol(ProtocolEnum.HTTP); + r.setReplaceKeyPrefixWith("replacekeyprefix"); + rule.setRedirect(r); + RouteRuleCondition condition = new RouteRuleCondition(); + condition.setHttpErrorCodeReturnedEquals("404"); + condition.setKeyPrefixEquals("keyprefix"); + rule.setCondition(condition); + websiteConfig.getRouteRules().add(rule); + + SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName_obs, websiteConfig); + this.getOwner().getObsClient().setBucketWebsite(request); + } + }.doTest(); + } + + @Test + @Ignore + public void test_set_bucket_replication() { + ReplicationConfiguration replicationConfiguration = new ReplicationConfiguration(); + SetBucketReplicationRequest request = new SetBucketReplicationRequest(bucketName_obs, replicationConfiguration); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketReplication((SetBucketReplicationRequest) this.getRequest()); + } + + @Override + public void checkResultWhilePaymentByOwner(HeaderResponse response) { + super.checkResultWhilePaymentByOwner(response); + ReplicationConfiguration config = this.getOwner().getObsClient().getBucketReplication(new BaseBucketRequest(this.getBucketName())); + } + + @Override + public void checkResultWhilePaymentByRequester(HeaderResponse response) { + super.checkResultWhilePaymentByRequester(response); + ReplicationConfiguration config = this.getOwner().getObsClient().getBucketReplication(new BaseBucketRequest(this.getBucketName())); + } + }.doTest(); + } + + @Test + @Ignore + public void test_get_bucket_replication() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketReplication((BaseBucketRequest) this.getRequest()); + } + + @Override + public void doBeforeAction() { +// this.getOwner().getObsClient().setBucketReplication(request); + } + }.doTest(); + } + + @Test + @Ignore + public void test_delete_bucket_replication() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 204), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.deleteBucketReplication((BaseBucketRequest) this.getRequest()); + } + + @Override + public void doBeforeAction() { +// this.getOwner().getObsClient().setBucketReplication(request); + } + }.doTest(); + } + + @Test + @Ignore + public void test_get_bucket_policy() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketPolicyV2((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_delete_bucket_policy() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.deleteBucketPolicy((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_set_bucket_policy() { + SetBucketPolicyRequest request = new SetBucketPolicyRequest(bucketName_obs, generateS3PolicyString(bucketName_obs, "*", "*")); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketPolicy((SetBucketPolicyRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_bucket_logging() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketLogging((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_set_bucket_logging() { + BucketLoggingConfiguration loggingConfiguration = new BucketLoggingConfiguration(); + SetBucketLoggingRequest request = new SetBucketLoggingRequest(bucketName_obs, loggingConfiguration); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketLogging((SetBucketLoggingRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_head_bucket() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + Boolean action(ObsClient obsClient) { + return new Boolean(obsClient.headBucket((BaseBucketRequest) this.getRequest())); + } + + @Override + public void checkResultWhilePaymentByOwner(Boolean resule) { + assertTrue(resule); + } + + @Override + public void checkResultWhilePaymentByRequester(Boolean resule) { + assertTrue(resule); + } + }.doTest(); + } + + @Test + public void test_set_bucket_lifecycle() { + SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName_obs, createLifecycleConfiguration()); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketLifecycle((SetBucketLifecycleRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_bucket_lifecycle() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketLifecycle((BaseBucketRequest) this.getRequest()); + } + + @Override + public void doBeforeAction() { + SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName_obs, createLifecycleConfiguration()); + this.getOwner().getObsClient().setBucketLifecycle(request); + } + }.doTest(); + } + + @Test + public void test_delete_bucket_lifecycle() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 204), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.deleteBucketLifecycle((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_bucket_metadata() { + BucketMetadataInfoRequest request = new BucketMetadataInfoRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketMetadata((BucketMetadataInfoRequest) this.getRequest()); + } + + @Override + public void checkPaymentByRequesterException(ObsException exception) { + exception = isRequestPayerDeniedException(exception); + // TODO +// assertNotNull(exception); + } + }.doTest(); + } + + @Test + public void test_set_bucket_Cors() { + SetBucketCorsRequest request = new SetBucketCorsRequest(bucketName_obs, createSetBucketCorsRequest()); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketCors((SetBucketCorsRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_bucket_Cors() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketCors((BaseBucketRequest) this.getRequest()); + } + + @Override + public void doBeforeAction() { + SetBucketCorsRequest request = new SetBucketCorsRequest(bucketName_obs, createSetBucketCorsRequest()); + this.getOwner().getObsClient().setBucketCors(request); + } + }.doTest(); + } + + @Test + public void test_delete_bucket_Cors() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 204), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.deleteBucketCors((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_set_bucket_storage_policy() { + BucketStoragePolicyConfiguration config = new BucketStoragePolicyConfiguration(StorageClassEnum.WARM); + SetBucketStoragePolicyRequest request = new SetBucketStoragePolicyRequest(bucketName_obs, config); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketStoragePolicy((SetBucketStoragePolicyRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_get_bucket_storage_policy() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketStoragePolicy((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_bucket_storage_info() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketStorageInfo((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_bucket_quota() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketQuota((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_set_bucket_quota() { + BucketQuota bucketQuota = new BucketQuota(10 * 1024 * 1024 * 1024L); + SetBucketQuotaRequest request = new SetBucketQuotaRequest(bucketName_obs, bucketQuota); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketQuota((SetBucketQuotaRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_bucket_encryption() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + try { + BucketEncryption bucketEncryption = new BucketEncryption(SSEAlgorithmEnum.AES256); + SetBucketEncryptionRequest setRequest = new SetBucketEncryptionRequest(bucketName_obs, bucketEncryption); + owner.getObsClient().setBucketEncryption(setRequest); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketEncryption((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } finally { + BaseBucketRequest deleteRequest = new BaseBucketRequest(bucketName_obs); + owner.getObsClient().deleteBucketEncryption(deleteRequest); + } + + } + + @Test + public void test_set_bucket_encryption() { + BucketEncryption bucketEncryption = new BucketEncryption(SSEAlgorithmEnum.AES256); + SetBucketEncryptionRequest request = new SetBucketEncryptionRequest(bucketName_obs, bucketEncryption); + try { + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketEncryption((SetBucketEncryptionRequest) this.getRequest()); + } + }.doTest(); + } finally { + BaseBucketRequest deleteRequest = new BaseBucketRequest(bucketName_obs); + owner.getObsClient().deleteBucketEncryption(deleteRequest); + } + } + + @Test + public void test_delete_bucket_encryption() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 204), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.deleteBucketEncryption((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_bucket_location() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketLocation((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_get_bucket_direct_cold_access() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketDirectColdAccess((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_bucket_notification() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketNotification((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_set_bucket_notification() { + BucketNotificationConfiguration bucketNotificationConfiguration = new BucketNotificationConfiguration(); + + SetBucketNotificationRequest request = new SetBucketNotificationRequest(bucketName_obs, bucketNotificationConfiguration); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketNotification((SetBucketNotificationRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_set_bucket_direct_cold_access() { + BucketDirectColdAccess access = new BucketDirectColdAccess(RuleStatusEnum.DISABLED); + SetBucketDirectColdAccessRequest request = new SetBucketDirectColdAccessRequest(bucketName_obs, access); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketDirectColdAccess((SetBucketDirectColdAccessRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_delete_bucket_direct_cold_access() { + BaseBucketRequest request = new BaseBucketRequest(bucketName_obs); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.deleteBucketDirectColdAccess((BaseBucketRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_object_acl() { + String objectKey = "test_object_acl"; + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024); + GetObjectAclRequest request = new GetObjectAclRequest(bucketName_obs, objectKey, null); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getObjectAcl((GetObjectAclRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_set_object_acl() { + String objectKey = "test_object_acl_set"; + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024); + AccessControlList accessControlList = owner.getObsClient().getObjectAcl(bucketName_obs, objectKey); + accessControlList.grantPermission(new CanonicalGrantee(requester.getDomainId()), + Permission.PERMISSION_FULL_CONTROL, true); + + SetObjectAclRequest request = new SetObjectAclRequest(bucketName_obs, objectKey, accessControlList); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setObjectAcl((SetObjectAclRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_rename_object() { + String objectKey = "test_rename_object_before"; + String newObjectKey = "test_rename_object_before_after_name"; + generateTestObject(owner, bucketName_pfs, objectKey, false, 1 * 1024); + + RenameObjectRequest request = new RenameObjectRequest(bucketName_obs, objectKey, newObjectKey); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_pfs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.renameObject((RenameObjectRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_modify_object() { + String objectKey = "test_modify_object_before"; + generateTestObject(owner, bucketName_obs, objectKey, false, 2 * 1024); + + ModifyObjectRequest request = new ModifyObjectRequest(bucketName_obs, objectKey, new ByteArrayInputStream(getByte(3 * 1024))); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.modifyObject((ModifyObjectRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_append_object() { + String objectKey = "test_append_object_before"; + // bucketName, objectKey, new ByteArrayInputStream(getByte(3 * 1024)) + AppendObjectRequest request = new AppendObjectRequest(); + request.setBucketName(bucketName_obs); + request.setObjectKey(objectKey); + request.setPosition(0); + request.setInput(new ByteArrayInputStream(getByte(2 * 1024))); + owner.getObsClient().appendObject(request); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + AppendObjectRequest appendRequest = (AppendObjectRequest)this.getRequest(); + ObjectMetadata metadata = this.getOwner().getObsClient().getObjectMetadata(appendRequest.getBucketName(), appendRequest.getObjectKey()); + + appendRequest.setPosition(metadata.getNextPosition()); + appendRequest.setInput(new ByteArrayInputStream(getByte(1 * 1024))); + return obsClient.appendObject(appendRequest); + } + }.doTest(); + } + + @Test + public void test_append_file() { + String objectKey = "test_append_file_before"; + // bucketName, objectKey, new ByteArrayInputStream(getByte(3 * 1024)) + AppendObjectRequest request = new AppendObjectRequest(); + request.setBucketName(bucketName_obs); + request.setObjectKey(objectKey); + request.setPosition(0); + request.setInput(new ByteArrayInputStream(getByte(2 * 1024))); + owner.getObsClient().appendObject(request); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + AppendObjectRequest appendRequest = (AppendObjectRequest)this.getRequest(); + ObjectMetadata metadata = this.getOwner().getObsClient().getObjectMetadata(appendRequest.getBucketName(), appendRequest.getObjectKey()); + + appendRequest.setPosition(metadata.getNextPosition()); + appendRequest.setInput(new ByteArrayInputStream(getByte(1 * 1024))); + return obsClient.appendObject(appendRequest); + } + }.doTest(); + } + + @Test + @Ignore + public void test_restore_object() { + String objectKey = "test_restore_object_before"; + generateTestObject(owner, bucketName_obs, objectKey, false, 2 * 1024); + + RestoreObjectRequest request = new RestoreObjectRequest(bucketName_obs, objectKey, 15); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.restoreObjectV2((RestoreObjectRequest)this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_restore_objects() { + String objectKey = "test_restore_objects"; + generateTestObject(owner, bucketName_obs, objectKey, false, 2 * 1024); + + RestoreObjectsRequest request = new RestoreObjectsRequest(bucketName_obs, 15, RestoreTierEnum.STANDARD); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + TaskProgressStatus action(ObsClient obsClient) { + return obsClient.restoreObjects((RestoreObjectsRequest)this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_options_object() { + final String objectKey = "test_options_object"; + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024); + + String allowOrigin = "http://baidu.com"; + List allowHeaders = new ArrayList(); + allowHeaders.add("x-obs-header"); + + List allowedMethod = new ArrayList(); + // 指定允许的跨域请求方法(GET/PUT/DELETE/POST/HEAD) + allowedMethod.add("GET"); + allowedMethod.add("HEAD"); + allowedMethod.add("PUT"); + + List exposeHeaders = new ArrayList(); + // 指定允许用户从应用程序中访问的header + exposeHeaders.add("x-obs-expose-header"); + + OptionsInfoRequest optionInfo = new OptionsInfoRequest(); + optionInfo.setOrigin(allowOrigin); + optionInfo.setRequestHeaders(allowHeaders); + optionInfo.setRequestMethod(allowedMethod); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, optionInfo, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.optionsObject(bucketName_obs, objectKey, (OptionsInfoRequest)this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_options_bucket() { + String objectKey = "test_options_object"; + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024); + + String allowOrigin = "http://baidu.com"; + List allowHeaders = new ArrayList(); + allowHeaders.add("x-obs-header"); + + List allowedMethod = new ArrayList(); + // 指定允许的跨域请求方法(GET/PUT/DELETE/POST/HEAD) + allowedMethod.add("GET"); + allowedMethod.add("HEAD"); + allowedMethod.add("PUT"); + + List exposeHeaders = new ArrayList(); + // 指定允许用户从应用程序中访问的header + exposeHeaders.add("x-obs-expose-header"); + + OptionsInfoRequest optionInfo = new OptionsInfoRequest(); + optionInfo.setOrigin(allowOrigin); + optionInfo.setRequestHeaders(allowHeaders); + optionInfo.setRequestMethod(allowedMethod); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, optionInfo, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.optionsBucket(bucketName_obs, (OptionsInfoRequest)this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_get_bucket_fsstatus() { + GetBucketFSStatusRequest request = new GetBucketFSStatusRequest(bucketName_obs); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.getBucketFSStatus((GetBucketFSStatusRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_set_bucket_fsstatus() { + SetBucketFSStatusRequest request = new SetBucketFSStatusRequest(bucketName_obs, FSStatusEnum.ENABLED); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.setBucketFSStatus((SetBucketFSStatusRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_new_file() { + String objectKey = "test_new_file"; + generateTestObject(owner, bucketName_pfs, objectKey, false, 1 * 1024); + NewFileRequest request = new NewFileRequest(bucketName_pfs, objectKey, new ByteArrayInputStream(getByte(1 * 1024))); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_pfs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.newFile((NewFileRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_read_file() { + String objectKey = "test_read_file"; + generateTestObject(owner, bucketName_pfs, objectKey, false, 1 * 1024); + ReadFileRequest request = new ReadFileRequest(bucketName_pfs, objectKey); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_pfs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + ReadFileResult action(ObsClient obsClient) { + return obsClient.readFile((ReadFileRequest) this.getRequest()); + } + }.doTest(); + } + + /** + * 关闭断点续传 + */ + @Test + public void test_download_file_checkpoint_false() { + String objectKey = "test_download_file_checkpoint_false"; + generateTestObject(owner, bucketName_obs, objectKey, false, 10 * 1024 * 1024); + DownloadFileRequest request = new DownloadFileRequest(bucketName_obs, objectKey); + request.setEnableCheckpoint(false); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + DownloadFileResult action(ObsClient obsClient) { + return obsClient.downloadFile((DownloadFileRequest) this.getRequest()); + } + }.doTest(); + } + + /** + * 开启断点续传 + */ + @Test + public void test_download_file_checkpoint_true() { + String objectKey = "test_download_file_checkpoint_true"; + generateTestObject(owner, bucketName_obs, objectKey, false, 10 * 1024 * 1024); + DownloadFileRequest request = new DownloadFileRequest(bucketName_obs, objectKey); + request.setEnableCheckpoint(true); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + DownloadFileResult action(ObsClient obsClient) { + return obsClient.downloadFile((DownloadFileRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_rename_file() { + String objectKey = "test_rename_file"; + generateTestObject(owner, bucketName_pfs, objectKey, false, 1 * 1024); + RenameRequest request = new RenameRequest(bucketName_pfs, objectKey, objectKey + "_after_rename"); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_pfs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.renameFile((RenameRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_upload_file_checkpoint_false() throws IOException { + String objectKey = "test_upload_file"; + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024); + + File file = FileTools.createALocalTempFile(objectKey+"_local_file", 10 * 1024 * 1024); + + UploadFileRequest request = new UploadFileRequest(bucketName_obs, objectKey, file.getPath()); + + request.setEnableCheckpoint(false); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.uploadFile((UploadFileRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_upload_file_checkpoint_true() throws IOException { + String objectKey = "test_upload_file"; + generateTestObject(owner, bucketName_obs, objectKey, false, 1 * 1024); + + File file = FileTools.createALocalTempFile(objectKey+"_local_file", 10 * 1024 * 1024); + + UploadFileRequest request = new UploadFileRequest(bucketName_obs, objectKey, file.getPath()); + + request.setEnableCheckpoint(true); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.uploadFile((UploadFileRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_write_file() throws IOException { + String objectKey = "test_write_file"; + + WriteFileRequest request = new WriteFileRequest(bucketName_obs, objectKey, new ByteArrayInputStream(getByte(1 * 1024))); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.writeFile((WriteFileRequest)this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_new_folder() { + String objectKey = "test_new_folder"; + generateTestObject(owner, bucketName_pfs, objectKey, false, 1 * 1024); + NewFolderRequest request = new NewFolderRequest(bucketName_pfs, objectKey); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_pfs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.newFolder((NewFolderRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_rename_folder() { + String objectKey = "test_rename_folder"; + generateTestObject(owner, bucketName_pfs, objectKey, false, 1 * 1024); + RenameRequest request = new RenameRequest(bucketName_pfs, objectKey, objectKey + "_after_rename"); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_pfs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.renameFolder((RenameRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + public void test_drop_folder() { + final String folder = "test_drop_folder/"; + + DropFolderRequest request = new DropFolderRequest(bucketName_pfs, folder); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_pfs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + TaskProgressStatus action(ObsClient obsClient) { + for(int i=0; i<5; i++) { + generateTestObject(owner, bucketName_pfs, folder + i, false, 1 * 1); + for(int j=0; j<5; j++) { + generateTestObject(owner, bucketName_pfs, folder + i + "/" + j, false, 1 * 1); + } + } + + return obsClient.dropFolder((DropFolderRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + // 批量上传接口还有问题,上传失败后,无法将错误信息抛出来 + public void test_put_objects_by_filepath() throws IOException { + List paths = new ArrayList(); + String path = ""; + for(int i=1; i<=3; i++) { + path = "test_put_objects/" + i + "_i"; + paths.add(path); + + FileTools.createALocalTempFile(path, 1); + for(int j=1; j<=3; j++) { + path = "test_put_objects/" + i + "path/" + j + "_j"; + paths.add(path); + + FileTools.createALocalTempFile(path, 1); + } + } + + PutObjectsRequest request = new PutObjectsRequest(bucketName_obs, paths); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + TaskProgressStatus action(ObsClient obsClient) { + return obsClient.putObjects((PutObjectsRequest) this.getRequest()); + } + + @Override + public void checkResultWhilePaymentByOwner(TaskProgressStatus response) { + super.checkResultWhilePaymentByOwner(response); + BucketTools.clearBucket(owner.getObsClient(), bucketName_obs); + }; + }.doTest(); + } + + @Test + @Ignore + // 批量上传接口还有问题,上传失败后,无法将错误信息抛出来 + public void test_put_objects_by_folderpath() throws IOException { + String folder = "test_put_objects"; + List paths = new ArrayList(); + String path = ""; + for(int i=1; i<=3; i++) { + path = folder + "/" + i + "_i"; + paths.add(path); + + FileTools.createALocalTempFile(path, 1); + for(int j=1; j<=3; j++) { + path = folder + "/" + i + "path/" + j + "_j"; + paths.add(path); + + FileTools.createALocalTempFile(path, 1); + } + } + + PutObjectsRequest request = new PutObjectsRequest(bucketName_obs, folder); + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + TaskProgressStatus action(ObsClient obsClient) { + return obsClient.putObjects((PutObjectsRequest) this.getRequest()); + } + }.doTest(); + } + + @Test + @Ignore + public void test_truncate_file() { + String objectKey = "test_truncate_file"; + generateTestObject(owner, bucketName_pfs, objectKey, false, 1 * 1024); + TruncateFileRequest request = new TruncateFileRequest(bucketName_pfs, objectKey, 512); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_pfs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.truncateFile((TruncateFileRequest) this.getRequest()); + } + }.doTest(); + } + + @Test +// @Ignore + public void test_truncate_object() { + String objectKey = "test_truncate_file"; + generateTestObject(owner, bucketName_pfs, objectKey, false, 1 * 1024); + TruncateObjectRequest request = new TruncateObjectRequest(bucketName_obs, objectKey, 512); + + new TestRequestPayment(new CaseInfo(owner, requester, bucketName_obs, request, 200), + logger, Thread.currentThread().getStackTrace()[1].getMethodName()) { + @Override + HeaderResponse action(ObsClient obsClient) { + return obsClient.truncateObject((TruncateObjectRequest) this.getRequest()); + } + }.doTest(); + } + + +} diff --git a/app/src/test/java/com/obs/test/SignedUrlTest.java b/app/src/test/java/com/obs/test/SignedUrlTest.java new file mode 100644 index 0000000..eb6329f --- /dev/null +++ b/app/src/test/java/com/obs/test/SignedUrlTest.java @@ -0,0 +1,391 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test; + +import static org.junit.Assert.assertEquals; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.obs.services.ObsClient; +import com.obs.services.internal.handler.XmlResponsesSaxParser; +import com.obs.services.internal.handler.XmlResponsesSaxParser.InitiateMultipartUploadHandler; +import com.obs.services.internal.handler.XmlResponsesSaxParser.ListPartsHandler; +import com.obs.services.internal.utils.RestUtils; +import com.obs.services.model.BucketTypeEnum; +import com.obs.services.model.HttpMethodEnum; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ListPartsResult; +import com.obs.services.model.Multipart; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.StorageClassEnum; +import com.obs.services.model.TemporarySignatureRequest; +import com.obs.services.model.TemporarySignatureResponse; +import com.obs.test.tools.BucketTools; +import com.obs.test.tools.PropertiesTools; + +import okhttp3.Call; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +public class SignedUrlTest { + private static final Logger logger = LogManager.getLogger(SignedUrlTest.class); + + private static String endpint_ip = null; + private static String endpint_dns = null; + private static String ak = null; + private static String sk = null; + + private static String bucketName; + + private static String objectKey; + + static ObsClient obsClient = null; + + @BeforeClass + public static void init() throws FileNotFoundException, IllegalArgumentException, IOException { + endpint_ip = PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.1.endpoint"); + endpint_dns = PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.1.endpoint.dns"); + ak = PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.1.ak");; + sk = PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.1.sk");; + + bucketName = SignedUrlTest.class.getName().replaceAll("\\.", "-").toLowerCase() + "-obs"; + + objectKey = "mulitpart_test_001"; + + obsClient = TestTools.getPipelineEnvironment(); + + // 强制删除一个桶 + // BucketTools.deleteBucket(obsClient, bucketName, true); + + // 创建桶 + BucketTools.createBucket(obsClient, bucketName, BucketTypeEnum.OBJECT); + } + + @Test + public void test_get_url() throws FileNotFoundException, IllegalArgumentException, IOException { + ObsClient obsClient = new ObsClient(PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.sdk.pipeline.ak"), + PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.sdk.pipeline.sk"), + PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.sdk.pipeline.endpoint")); + + // URL有效期,3600秒 + long expireSeconds = 3600L; + + TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds); + request.setBucketName("com-obs-test-requestpaymenttest-pfs"); + request.setObjectKey("test_rename_object_before"); + + TemporarySignatureResponse response = obsClient.createTemporarySignature(request); + + System.out.println("Getting object using temporary signature url:"); + System.out.println("\t" + response.getSignedUrl()); + } + + @Test + public void test_create_bucket_url() throws FileNotFoundException, IllegalArgumentException, IOException { +// ObsClient obsClient = new ObsClient(PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.sdk.pipeline.ak"), +// PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.sdk.pipeline.sk"), +// PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.sdk.pipeline.endpoint")); + + ObsClient obsClient = new ObsClient(PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.me.ak"), + PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.me.sk"), + PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.me.endpoint")); + + // URL有效期,3600秒 + long expireSeconds = 3600L; + + TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.PUT, expireSeconds); + request.setBucketName("test-create-bucket-001"); + + TemporarySignatureResponse response = obsClient.createTemporarySignature(request); + + System.out.println("Creating bucket using temporary signature url:"); + System.out.println("\t" + response.getSignedUrl()); + + Request.Builder builder = new Request.Builder(); + for (Map.Entry entry : response.getActualSignedRequestHeaders().entrySet()) { + builder.header(entry.getKey(), entry.getValue()); + } + // 使用PUT请求创建桶 + String location = "cn-north-4"; + String createXml = "" + location + ""; + System.out.println("Create Body: " + createXml); + + OkHttpClient.Builder clientBuiler = new OkHttpClient.Builder(); + + RestUtils.initHttpProxy(clientBuiler, + PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.me.proxyaddr"), + Integer.parseInt(PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.me.proxyport")), + PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.me.username"), + PropertiesTools.getInstance(TestTools.getPropertiesFile()).getProperties("environment.me.password")); + + Request httpRequest = builder.url(response.getSignedUrl()).put(RequestBody.create(null, createXml.getBytes())).build(); + + OkHttpClient httpClient = clientBuiler.followRedirects(false).retryOnConnectionFailure(false) + .cache(null).build(); + + Call c = httpClient.newCall(httpRequest); + Response res = c.execute(); + System.out.println("\tStatus:" + res.code()); + if (res.body() != null) { + System.out.println("\tContent:" + res.body().string() + "\n"); + } + res.close(); + } + + @Test + public void test_temporary_signature_for_init_mulitpartupload() throws IOException { + init_mulitpartupload(bucketName, objectKey); + } + + @Test + public void test_temporary_signature_for_upload_part() throws IOException { + InitiateMultipartUploadResult initResult = init_mulitpartupload(bucketName, objectKey); + + uploadPart(bucketName, objectKey, initResult.getUploadId(), 1); + uploadPart(bucketName, objectKey, initResult.getUploadId(), 2); + } + + @Test + public void test_temporary_signature_for_list_parts() throws IOException { + InitiateMultipartUploadResult initResult = init_mulitpartupload(bucketName, objectKey); + + uploadPart(bucketName, objectKey, initResult.getUploadId(), 1); + uploadPart(bucketName, objectKey, initResult.getUploadId(), 2); + + ListPartsResult result = listParts(bucketName, objectKey, initResult.getUploadId()); + + assertEquals(2, result.getMultipartList().size()); + } + + @Test + public void test_temporary_signature_for_complete_multipartupload() throws IOException { + InitiateMultipartUploadResult initResult = init_mulitpartupload(bucketName, objectKey); + + uploadPart(bucketName, objectKey, initResult.getUploadId(), 1); + uploadPart(bucketName, objectKey, initResult.getUploadId(), 2); + + ListPartsResult result = listParts(bucketName, objectKey, initResult.getUploadId()); + + assertEquals(2, result.getMultipartList().size()); + + completeMultipartupload(bucketName, objectKey, initResult.getUploadId(), result); + } + + private InitiateMultipartUploadResult init_mulitpartupload(String bucketName, String objectKey) throws IOException { + ObsClient client = new ObsClient(ak, sk, endpint_ip); + long expireSeconds = 3600L; + + TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.POST, expireSeconds); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setSpecialParam(SpecialParamEnum.UPLOADS); + TemporarySignatureResponse response = client.createTemporarySignature(request); + + logger.info("\t" + response.getSignedUrl()); + + Request.Builder builder = new Request.Builder(); + for (Map.Entry entry : response.getActualSignedRequestHeaders().entrySet()) { + builder.header(entry.getKey(), entry.getValue()); + } + Response res = null; + try { + Request httpRequest = builder.url(response.getSignedUrl()).post(RequestBody.create(null, "")).build(); + OkHttpClient httpClient = new OkHttpClient.Builder().followRedirects(false).retryOnConnectionFailure(false) + .cache(null).build(); + + Call c = httpClient.newCall(httpRequest); + res = c.execute(); + int responseCode = res.code(); + logger.info("\tStatus:" + res.code()); + assertEquals(responseCode, 200); + + InitiateMultipartUploadResult multipartUpload = new XmlResponsesSaxParser() + .parse(res.body().byteStream(), InitiateMultipartUploadHandler.class, true) + .getInitiateMultipartUploadResult(); + logger.info("\tContent:" + multipartUpload + "\n"); + return multipartUpload; + } finally { + if (null != res) { + res.close(); + } + } + } + + private void uploadPart(String bucketName, String objectKey, String uploadId, int partNumber) throws IOException { + ObsClient client = new ObsClient(ak, sk, endpint_ip); + long expireSeconds = 3600L; + + Map queryParams = new HashMap(); + queryParams.put("partNumber", partNumber); + queryParams.put("uploadId", uploadId); + + TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.PUT, expireSeconds); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + + request.setQueryParams(queryParams); + + TemporarySignatureResponse response = client.createTemporarySignature(request); + + logger.info("\t" + response.getSignedUrl()); + Request.Builder builder = new Request.Builder(); + for (Map.Entry entry : response.getActualSignedRequestHeaders().entrySet()) { + builder.header(entry.getKey(), entry.getValue()); + } + + Response res = null; + try { + // 使用PUT请求上传段 + Request httpRequest = builder.url(response.getSignedUrl()) + .put(RequestBody.create(null, new byte[6 * 1024 * 1024])).build(); + OkHttpClient httpClient = new OkHttpClient.Builder().followRedirects(false).retryOnConnectionFailure(false) + .cache(null).build(); + + Call c = httpClient.newCall(httpRequest); + res = c.execute(); + System.out.println("\tStatus:" + res.code()); + if (res.body() != null) { + System.out.println("\tContent:" + res.body().string() + "\n"); + } + res.close(); + } finally { + if (null != res) { + res.close(); + } + } + } + + private ListPartsResult listParts(String bucketName, String objectKey, String uploadId) throws IOException { + // String endPoint = "http://your-endpoint"; + // String ak = "*** Provide your Access Key ***"; + // String sk = "*** Provide your Secret Key ***"; + + // 创建ObsClient实例 + ObsClient obsClient = new ObsClient(ak, sk, endpint_ip); + // URL有效期,3600秒 + long expireSeconds = 3600L; + + TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + + Map queryParams = new HashMap(); + queryParams.put("uploadId", uploadId); + request.setQueryParams(queryParams); + + TemporarySignatureResponse response = obsClient.createTemporarySignature(request); + + logger.info("list parts using temporary signature url:"); + logger.info("\t" + response.getSignedUrl()); + + Response res = null; + try { + Request.Builder builder = new Request.Builder(); + for (Map.Entry entry : response.getActualSignedRequestHeaders().entrySet()) { + builder.header(entry.getKey(), entry.getValue()); + } + + Request httpRequest = builder.url(response.getSignedUrl()).get().build(); + OkHttpClient httpClient = new OkHttpClient.Builder().followRedirects(false).retryOnConnectionFailure(false) + .cache(null).build(); + + Call c = httpClient.newCall(httpRequest); + res = c.execute(); + int responseCode = res.code(); + logger.info("\tStatus:" + res.code()); + assertEquals(responseCode, 200); + + ListPartsHandler handler = new XmlResponsesSaxParser().parse(res.body().byteStream(), + ListPartsHandler.class, true); + + ListPartsResult result = new ListPartsResult(handler.getBucketName(), handler.getObjectKey(), + handler.getUploadId(), handler.getInitiator(), handler.getOwner(), + StorageClassEnum.getValueFromCode(handler.getStorageClass()), handler.getMultiPartList(), + handler.getMaxParts(), handler.isTruncated(), + handler.getPartNumberMarker(), + handler.getNextPartNumberMarker()); + + logger.info("\tContent:" + result + "\n"); + return result; + } finally { + if(null != res) { + res.close(); + } + } + } + + private void completeMultipartupload(String bucketName, String objectKey, String uploadId, ListPartsResult result) throws IOException { + // 创建ObsClient实例 + ObsClient obsClient = new ObsClient(ak, sk, endpint_ip); + // URL有效期,3600秒 + long expireSeconds = 3600L; + + TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.POST, expireSeconds); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + + Map headers = new HashMap(); + String contentType = "application/xml"; + headers.put("Content-Type", contentType); + request.setHeaders(headers); + + Map queryParams = new HashMap(); + queryParams.put("uploadId", uploadId); + request.setQueryParams(queryParams); + + TemporarySignatureResponse response = obsClient.createTemporarySignature(request); + + System.out.println("complete multipart upload using temporary signature url:"); + System.out.println("\t" + response.getSignedUrl()); + + Request.Builder builder = new Request.Builder(); + for (Map.Entry entry : response.getActualSignedRequestHeaders().entrySet()) { + builder.header(entry.getKey(), entry.getValue()); + } + + StringBuilder sb = new StringBuilder(""); + for(Multipart part : result.getMultipartList()) { + sb.append(""); + sb.append("").append(part.getPartNumber()).append(""); + sb.append("").append(part.getEtag()).append(""); + sb.append(""); + } + sb.append(""); + + Request httpRequest = builder.url(response.getSignedUrl()) + .post(RequestBody.create(MediaType.parse(contentType), sb.toString().getBytes("UTF-8"))).build(); + OkHttpClient httpClient = new OkHttpClient.Builder().followRedirects(false).retryOnConnectionFailure(false) + .cache(null).build(); + + Call c = httpClient.newCall(httpRequest); + Response res = c.execute(); + System.out.println("\tStatus:" + res.code()); + if (res.body() != null) { + System.out.println("\tContent:" + res.body().string() + "\n"); + } + res.close(); + } +} diff --git a/app/src/test/java/com/obs/test/TestTools.java b/app/src/test/java/com/obs/test/TestTools.java new file mode 100644 index 0000000..1db481f --- /dev/null +++ b/app/src/test/java/com/obs/test/TestTools.java @@ -0,0 +1,333 @@ +package com.obs.test; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.model.AbortMultipartUploadRequest; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.BucketTypeEnum; +import com.obs.services.model.CreateBucketRequest; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.ListMultipartUploadsRequest; +import com.obs.services.model.ListVersionsRequest; +import com.obs.services.model.ListVersionsResult; +import com.obs.services.model.MultipartUpload; +import com.obs.services.model.MultipartUploadListing; +import com.obs.services.model.VersionOrDeleteMarker; +import com.obs.test.tools.PropertiesTools; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.WriterAppender; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.Configurator; +import org.apache.logging.log4j.core.config.LoggerConfig; +import org.apache.logging.log4j.core.layout.PatternLayout; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.junit.Assert.assertTrue; + +public class TestTools { + + private static final File file = new File("app/src/test/resource/test_data.properties"); + + public static File getPropertiesFile() { + return file; + } + + /** + * 获取自定义内部环境 + */ + public static ObsClient getCustomPipelineEnvironment() { + try { + String endPoint = PropertiesTools.getInstance(file).getProperties("environment.endpoint"); + String ak = PropertiesTools.getInstance(file).getProperties("environment.ak"); + String sk = PropertiesTools.getInstance(file).getProperties("environment.sk"); + String authType = PropertiesTools.getInstance(file).getProperties("environment.authType"); + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + config.setLocalAuthTypeCacheCapacity(3); + if (authType.equals("v2")) { + config.setAuthType(AuthTypeEnum.V2); + } else { + config.setAuthType(AuthTypeEnum.OBS); + } + return new ObsClient(ak, sk, config); + + } catch (IllegalArgumentException | IOException e) { + e.printStackTrace(); + } + + return null; + } + + /** + * 获取内部环境 + */ + public static ObsClient getPipelineEnvironment() { + try { + String endPoint = PropertiesTools.getInstance(file).getProperties("environment.endpoint"); + String ak = PropertiesTools.getInstance(file).getProperties("environment.ak"); + String sk = PropertiesTools.getInstance(file).getProperties("environment.sk"); + String authType = PropertiesTools.getInstance(file).getProperties("environment.authType"); + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + config.setAuthTypeNegotiation(false); + if (authType.equals("v2")) { + config.setAuthType(AuthTypeEnum.V2); + } else { + config.setAuthType(AuthTypeEnum.OBS); + } + return new ObsClient(ak, sk, config); + + } catch (IllegalArgumentException | IOException e) { + e.printStackTrace(); + } + + return null; + } + + public static String getKMSID() { + try { + return PropertiesTools.getInstance(file).getProperties("kmsID"); + } catch (IOException e) { + e.printStackTrace(); + return ""; + } + } + + public static String getAuthType() { + try { + return PropertiesTools.getInstance(file).getProperties("environment.authType"); + } catch (IOException e) { + e.printStackTrace(); + return ""; + } + } + + /** + * 获取请求者付费的测试环境 + */ + public static ObsClient getRequestPaymentEnvironment_User1() { + try { + String endPoint = PropertiesTools.getInstance(file).getProperties("environment.1.endpoint"); + String ak = PropertiesTools.getInstance(file).getProperties("environment.1.ak"); + String sk = PropertiesTools.getInstance(file).getProperties("environment.1.sk"); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + return new ObsClient(ak, sk, config); + + } catch (IllegalArgumentException | IOException e) { + e.printStackTrace(); + } + + return null; + } + + /** + * 获取请求者付费的测试环境 + */ + public static ObsClient getRequestPaymentEnvironment_User2() { + try { + String endPoint = PropertiesTools.getInstance(file).getProperties("environment.2.endpoint"); + String ak = PropertiesTools.getInstance(file).getProperties("environment.2.ak"); + String sk = PropertiesTools.getInstance(file).getProperties("environment.2.sk"); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + return new ObsClient(ak, sk, config); + + } catch (IllegalArgumentException | IOException e) { + e.printStackTrace(); + } + + return null; + } + + /** + * 获取请求者付费的测试环境 + */ + public static ObsClient getEnvironment_User3() { + try { + String endPoint = PropertiesTools.getInstance(file).getProperties("environment.3.endpoint"); + String ak = PropertiesTools.getInstance(file).getProperties("environment.3.ak"); + String sk = PropertiesTools.getInstance(file).getProperties("environment.3.sk"); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + return new ObsClient(ak, sk, config); + + } catch (IllegalArgumentException | IOException e) { + e.printStackTrace(); + } + + return null; + } + + /** + * 获取外部环境 + */ + public static ObsClient getExternalEnvironment() { + try { + String endPoint = PropertiesTools.getInstance(file).getProperties("environment.me.endpoint"); + String ak = PropertiesTools.getInstance(file).getProperties("environment.me.ak"); + String sk = PropertiesTools.getInstance(file).getProperties("environment.me.sk"); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + config.setHttpProxy(PropertiesTools.getInstance(file).getProperties("environment.me.proxyaddr"), + Integer.parseInt(PropertiesTools.getInstance(file).getProperties("environment.me.proxyport")), + PropertiesTools.getInstance(file).getProperties("environment.me.username"), + PropertiesTools.getInstance(file).getProperties("environment.me.password")); + + return new ObsClient(ak, sk, config); + + } catch (IllegalArgumentException | IOException e) { + e.printStackTrace(); + } + + return null; + } + + /** + * 获取请求者付费的测试环境 + */ + public static ObsClient getInnerTempEnvironment() { + try { + String endPoint = PropertiesTools.getInstance(file).getProperties("environment.4.endpoint"); + String ak = PropertiesTools.getInstance(file).getProperties("environment.4.ak"); + String sk = PropertiesTools.getInstance(file).getProperties("environment.4.sk"); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + return new ObsClient(ak, sk, config); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + + public static void deleteObjects(ObsClient obsClient, String bucketName) { + ListVersionsRequest request = new ListVersionsRequest(bucketName); + request.setEncodingType("url"); + request.setMaxKeys(100); + ListVersionsResult result; + ListMultipartUploadsRequest request2 = new ListMultipartUploadsRequest(); + request2.setEncodingType("url"); + request2.setBucketName(bucketName); + MultipartUploadListing listing = obsClient.listMultipartUploads(request2); + AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest(); + abortRequest.setBucketName(bucketName); + for (MultipartUpload upload : listing.getMultipartTaskList()) { + abortRequest.setUploadId(upload.getUploadId()); + abortRequest.setObjectKey(upload.getObjectKey()); + obsClient.abortMultipartUpload(abortRequest); + } + do { + result = obsClient.listVersions(request); + if (result.getVersions().length == 0) { + break; + } + DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName); + deleteRequest.setEncodingType("url"); + for (VersionOrDeleteMarker v : result.getVersions()) { + deleteRequest.addKeyAndVersion(v.getKey(), v.getVersionId()); + } + obsClient.deleteObjects(deleteRequest); + + request.setKeyMarker(result.getNextKeyMarker()); + request.setVersionIdMarker(result.getNextVersionIdMarker()); + } while (result.isTruncated()); + } + + public static void delete_buckets(ObsClient obsClient, List bucketList) { + for (String bucket : bucketList) { + delete_bucket(obsClient, bucket); + } + } + + public static HeaderResponse delete_bucket(ObsClient obsClient, String bucketName) { + System.out.println("Deleting " + bucketName); + deleteObjects(obsClient, bucketName); + return obsClient.deleteBucket(bucketName); + } + + public static HeaderResponse createBucket(ObsClient obsClient, String bucketName, String location, boolean isPosix) { + CreateBucketRequest request = new CreateBucketRequest(); + request.setBucketName(bucketName); + request.setBucketType(BucketTypeEnum.OBJECT); + request.setLocation(location); + if (isPosix) { + request.setBucketType(BucketTypeEnum.PFS); + } + return obsClient.createBucket(request); + } + + public static void genTestFile(String filePath, int fileSize) throws IOException { + File testFile = new File(filePath); + if (!testFile.exists()) { + assertTrue(testFile.createNewFile()); + } + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < 128; i++) { + stringBuilder.append("TestOBS!"); + } + FileOutputStream outputStream = new FileOutputStream(filePath); + for (int i = 0; i < fileSize; i++) { + outputStream.write(stringBuilder.toString().getBytes(StandardCharsets.UTF_8)); + } + outputStream.close(); + } + + public static void initLog(StringWriter writer) { + LoggerContext context = LoggerContext.getContext(false); + Configuration config = context.getConfiguration(); + LoggerConfig LoggerConfig = config.getLoggerConfig("com.obs.services.internal.RestStorageService"); + if (!LoggerConfig.getName().equals("com.obs.services.internal.RestStorageService")) { + LoggerConfig = new LoggerConfig("com.obs.services.internal.RestStorageService", + Level.DEBUG, true); + } + config.addLogger("com.obs.services.internal.RestStorageService", LoggerConfig); + + PatternLayout layout = PatternLayout.createDefaultLayout(config); + Appender appender = WriterAppender.createAppender(layout, null, writer, "StringWriter", + false, true); + config.addAppender(appender); + LoggerConfig.addAppender(appender, Level.DEBUG, null); + appender.start(); + context.updateLoggers(); + Configurator.setLevel("com.obs.services.internal.RestStorageService", Level.DEBUG); + } +} diff --git a/app/src/test/java/com/obs/test/UserHeaderTest.java b/app/src/test/java/com/obs/test/UserHeaderTest.java new file mode 100644 index 0000000..60645d0 --- /dev/null +++ b/app/src/test/java/com/obs/test/UserHeaderTest.java @@ -0,0 +1,189 @@ +package com.obs.test; + +import com.obs.services.ObsClient; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.CompleteMultipartUploadResult; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.PartEtag; +import com.obs.services.model.UploadPartRequest; +import com.obs.services.model.UploadPartResult; +import com.obs.test.tools.PrepareTestBucket; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class UserHeaderTest { + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + @Test + public void test_add_user_headers() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_add_user_headers"; + String headerKey = "test-user-headers"; + + // 初始化 log + StringWriter writer = new StringWriter(); + + TestTools.initLog(writer); + + // 测试覆盖所有 performRequest 和 trans 函数 + InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(); + initiateMultipartUploadRequest.setBucketName(bucketName); + initiateMultipartUploadRequest.setObjectKey(objectKey); + initiateMultipartUploadRequest.addUserHeaders(headerKey, "test-value-initiateMultipartUpload"); + InitiateMultipartUploadResult initResult = obsClient.initiateMultipartUpload(initiateMultipartUploadRequest); + assertTrue(writer.toString().contains("|" + headerKey + ": test-value-initiateMultipartUpload|")); + writer.flush(); + assertEquals(200, initResult.getStatusCode()); + + UploadPartRequest uploadPartRequest = new UploadPartRequest(); + uploadPartRequest.setUploadId(initResult.getUploadId()); + uploadPartRequest.setPartNumber(1); + uploadPartRequest.setBucketName(bucketName); + uploadPartRequest.setObjectKey(objectKey); + uploadPartRequest.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + uploadPartRequest.addUserHeaders(headerKey, "test-value-uploadPart"); + UploadPartResult uploadResult = obsClient.uploadPart(uploadPartRequest); + assertTrue(writer.toString().contains("|" + headerKey + ": test-value-uploadPart|")); + writer.flush(); + assertEquals(200, uploadResult.getStatusCode()); + + CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(); + completeRequest.setObjectKey(objectKey); + completeRequest.setBucketName(bucketName); + completeRequest.setUploadId(initResult.getUploadId()); + completeRequest.addUserHeaders(headerKey, "test-value-completeMultipartUpload"); + PartEtag partEtag = new PartEtag(); + partEtag.setPartNumber(uploadResult.getPartNumber()); + partEtag.setEtag(uploadResult.getEtag()); + completeRequest.getPartEtag().add(partEtag); + CompleteMultipartUploadResult completeResult = obsClient.completeMultipartUpload(completeRequest); + assertTrue(writer.toString().contains("|" + headerKey + ": test-value-completeMultipartUpload|")); + writer.flush(); + assertEquals(200, completeResult.getStatusCode()); + } + + @Test + public void test_add_user_headers_need_auth() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_add_user_headers"; + String headerKey = TestTools.getAuthType().equals("v2") ? "x-amz-test-auth-header" + : "x-obs-test-auth-header"; + + // 初始化 log + StringWriter writer = new StringWriter(); + + TestTools.initLog(writer); + + // 测试覆盖所有 performRequest 和 trans 函数 + InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(); + initiateMultipartUploadRequest.setBucketName(bucketName); + initiateMultipartUploadRequest.setObjectKey(objectKey); + initiateMultipartUploadRequest.addUserHeaders(headerKey, "test-value-initiateMultipartUpload"); + InitiateMultipartUploadResult initResult = obsClient.initiateMultipartUpload(initiateMultipartUploadRequest); + // 签名计算字段 + assertTrue(writer.toString().contains("|" + headerKey + ":test-value-initiateMultipartUpload|")); + // 请求头域 + assertTrue(writer.toString().contains("|" + headerKey + ": test-value-initiateMultipartUpload")); + writer.flush(); + assertEquals(200, initResult.getStatusCode()); + + UploadPartRequest uploadPartRequest = new UploadPartRequest(); + uploadPartRequest.setUploadId(initResult.getUploadId()); + uploadPartRequest.setPartNumber(1); + uploadPartRequest.setBucketName(bucketName); + uploadPartRequest.setObjectKey(objectKey); + uploadPartRequest.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + uploadPartRequest.addUserHeaders(headerKey, "test-value-uploadPart"); + UploadPartResult uploadResult = obsClient.uploadPart(uploadPartRequest); + // 签名计算字段 + assertTrue(writer.toString().contains("|" + headerKey + ":test-value-uploadPart|")); + // 请求头域 + assertTrue(writer.toString().contains("|" + headerKey + ": test-value-uploadPart")); + writer.flush(); + assertEquals(200, uploadResult.getStatusCode()); + + CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(); + completeRequest.setObjectKey(objectKey); + completeRequest.setBucketName(bucketName); + completeRequest.setUploadId(initResult.getUploadId()); + completeRequest.addUserHeaders(headerKey, "test-value-completeMultipartUpload"); + PartEtag partEtag = new PartEtag(); + partEtag.setPartNumber(uploadResult.getPartNumber()); + partEtag.setEtag(uploadResult.getEtag()); + completeRequest.getPartEtag().add(partEtag); + CompleteMultipartUploadResult completeResult = obsClient.completeMultipartUpload(completeRequest); + // 签名计算字段 + assertTrue(writer.toString().contains("|" + headerKey + ":test-value-completeMultipartUpload|")); + // 请求头域 + assertTrue(writer.toString().contains("|" + headerKey + ": test-value-completeMultipartUpload")); + writer.flush(); + assertEquals(200, completeResult.getStatusCode()); + } + + @Test + public void test_add_empty_user_headers() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_add_empty_user_headers"; + String headerKey = "test-add-empty-user-headers"; + + // 初始化 log + StringWriter writer = new StringWriter(); + + TestTools.initLog(writer); + + // 测试覆盖所有 performRequest 和 trans 函数 + InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(); + initiateMultipartUploadRequest.setBucketName(bucketName); + initiateMultipartUploadRequest.setObjectKey(objectKey); + initiateMultipartUploadRequest.addUserHeaders(headerKey, ""); + InitiateMultipartUploadResult initResult = obsClient.initiateMultipartUpload(initiateMultipartUploadRequest); + assertTrue(writer.toString().contains("|" + headerKey + ": |")); + writer.flush(); + assertEquals(200, initResult.getStatusCode()); + + UploadPartRequest uploadPartRequest = new UploadPartRequest(); + uploadPartRequest.setUploadId(initResult.getUploadId()); + uploadPartRequest.setPartNumber(1); + uploadPartRequest.setBucketName(bucketName); + uploadPartRequest.setObjectKey(objectKey); + uploadPartRequest.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + uploadPartRequest.addUserHeaders("test-user-headers", ""); + UploadPartResult uploadResult = obsClient.uploadPart(uploadPartRequest); + assertTrue(writer.toString().contains("|" + headerKey + ": |")); + writer.flush(); + assertEquals(200, uploadResult.getStatusCode()); + + CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(); + completeRequest.setObjectKey(objectKey); + completeRequest.setBucketName(bucketName); + completeRequest.setUploadId(initResult.getUploadId()); + completeRequest.addUserHeaders("test-user-headers", ""); + PartEtag partEtag = new PartEtag(); + partEtag.setPartNumber(uploadResult.getPartNumber()); + partEtag.setEtag(uploadResult.getEtag()); + completeRequest.getPartEtag().add(partEtag); + CompleteMultipartUploadResult completeResult = obsClient.completeMultipartUpload(completeRequest); + assertTrue(writer.toString().contains("|" + headerKey + ": |")); + writer.flush(); + assertEquals(200, completeResult.getStatusCode()); + } + +} diff --git a/app/src/test/java/com/obs/test/UserInfo.java b/app/src/test/java/com/obs/test/UserInfo.java new file mode 100644 index 0000000..d450788 --- /dev/null +++ b/app/src/test/java/com/obs/test/UserInfo.java @@ -0,0 +1,27 @@ +package com.obs.test; + +import com.obs.services.ObsClient; + +public class UserInfo { + private ObsClient obsClient; + private String domainId; + private String ownerId; + + public UserInfo(ObsClient obsClient, String domainId, String ownerId) { + this.obsClient = obsClient; + this.domainId = domainId; + this.ownerId = ownerId; + } + + public ObsClient getObsClient() { + return obsClient; + } + + public String getDomainId() { + return domainId; + } + + public String getOwnerId() { + return this.ownerId; + } +} diff --git a/app/src/test/java/com/obs/test/V2ConvertorTest.java b/app/src/test/java/com/obs/test/V2ConvertorTest.java new file mode 100644 index 0000000..2257575 --- /dev/null +++ b/app/src/test/java/com/obs/test/V2ConvertorTest.java @@ -0,0 +1,30 @@ +package com.obs.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import com.obs.services.internal.V2Convertor; +import com.obs.services.model.RequestPaymentEnum; + +public class V2ConvertorTest { + @Test + public void test_bucket_owner_xml() { + RequestPaymentEnum payer = RequestPaymentEnum.BUCKET_OWNER; + + String content = V2Convertor.getInstance().transRequestPaymentConfiguration("test", payer.getCode()); + assertEquals(getRequestPaymentXML(payer), content); + } + + @Test + public void test_requester_xml() { + RequestPaymentEnum payer = RequestPaymentEnum.REQUESTER; + + String content = V2Convertor.getInstance().transRequestPaymentConfiguration("test", payer.getCode()); + assertEquals(getRequestPaymentXML(payer), content); + } + + public String getRequestPaymentXML(RequestPaymentEnum payer) { + return "" + payer.getCode() + ""; + } +} diff --git a/app/src/test/java/com/obs/test/buckets/BaseBucketTest.java b/app/src/test/java/com/obs/test/buckets/BaseBucketTest.java new file mode 100644 index 0000000..6255239 --- /dev/null +++ b/app/src/test/java/com/obs/test/buckets/BaseBucketTest.java @@ -0,0 +1,192 @@ +package com.obs.test.buckets; + +import java.util.ArrayList; +import java.util.List; + +import com.obs.services.model.BucketCors; +import com.obs.services.model.BucketCorsRule; +import com.obs.services.model.LifecycleConfiguration; +import com.obs.services.model.LifecycleConfiguration.NoncurrentVersionTransition; +import com.obs.services.model.LifecycleConfiguration.Rule; +import com.obs.services.model.LifecycleConfiguration.Transition; +import com.obs.services.model.StorageClassEnum; + +import com.obs.test.UserInfo; +import com.obs.test.tools.BucketTools; + +public abstract class BaseBucketTest { + + protected String generateS3PolicyString(String bucketName, String action, String userId) { + return "{" + "\"Version\":\"2008-10-17\"," + "\"Id\":\"Policy1584074750088\"," + "\"Statement\":[" + " {" + + " \"Sid\":\"Customized1584072051364\"," + " \"Effect\":\"Allow\"," + + " \"Principal\":{" + " \"AWS\":[\"arn:aws:iam::" + userId + "\"]" + " }," + + " \"Action\":[\"s3:" + action + "\"]," + " \"Resource\":[\"arn:aws:s3:::" + bucketName + + "\"]" + " }" + "]}"; + } + + protected void setModifyObjectMetaDataPolicy(UserInfo srcUser, UserInfo destUser, String bucketName) { + // String policy = + // "{" + // + "\"Statement\":[" + // + " {" + // + " \"Sid\":\"Customized1584072051364\"," + // + " \"Effect\":\"Allow\"," + // + " \"Principal\":{" + // + " \"ID\":[\"domain/" + destUser.getOwnerId() + ":user/" + + // destUser.getDomainId() + "\"]" + // + " }," + // + " \"Action\":[\"ModifyObjectMetaData\"]," + // + " \"Resource\":[\"" + bucketName + "/*\"]" + " }" + // + "]}"; + + // 采用S3协议 + String policy = "{" + "\"Version\":\"2008-10-17\"," + "\"Id\":\"Policy1584074750088\"," + "\"Statement\":[" + + " {" + " \"Sid\":\"Customized1584072051364\"," + " \"Effect\":\"Allow\"," + + " \"Principal\":{" + " \"AWS\":[\"arn:aws:iam::" + destUser.getDomainId() + ":user/" + + destUser.getOwnerId() + "\"]" + " }," + " \"Action\":[\"s3:ModifyObjectMetaData\"]," + + " \"Resource\":[\"arn:aws:s3:::" + bucketName + "/*\"]" + " }" + "]}"; + + BucketTools.setBucketPolicy(srcUser.getObsClient(), bucketName, policy); + } + + /** + * 设置桶策略,使其具备设置桶请求者付费的权限 + * + * @param srcUser + * @param destUser + * @param bucketName + */ + protected void setBucketRequestPaymentPolicy(UserInfo srcUser, UserInfo destUser, String bucketName) { + // 采用S3协议 + String policy = "{" + "\"Version\":\"2008-10-17\"," + "\"Id\":\"Policy1584074750088\"," + "\"Statement\":[" + + " {" + " \"Sid\":\"Customized1584072051364\"," + " \"Effect\":\"Allow\"," + + " \"Principal\":{" + " \"AWS\":[\"arn:aws:iam::" + destUser.getDomainId() + ":user/" + + destUser.getOwnerId() + "\"]" + " }," + + " \"Action\":[\"s3:GetBucketRequestPayment\",\"s3:PutBucketRequestPayment\"]," + + " \"Resource\":[\"arn:aws:s3:::" + bucketName + "\"]" + " }" + "]}"; + + BucketTools.setBucketPolicy(srcUser.getObsClient(), bucketName, policy); + } + + /** + * 设置桶策略,使其具备设置桶请求者付费的权限 + * + * @param srcUser + * @param destUser + * @param bucketName + */ + protected void setAllBucketPolicy(UserInfo srcUser, UserInfo destUser, String bucketName) { + // 采用S3协议 + String policy = "{" + "\"Version\":\"2008-10-17\"," + "\"Id\":\"Policy1584074750088\"," + "\"Statement\":[" + + " {" + " \"Sid\":\"Customized1584072051364\"," + " \"Effect\":\"Allow\"," + + " \"Principal\":{" + " \"AWS\":[\"arn:aws:iam::" + destUser.getDomainId() + ":user/" + + destUser.getOwnerId() + "\"]" + " }," + " \"Action\":[\"s3:*\"]," + + " \"Resource\":[\"arn:aws:s3:::" + bucketName + "\"]" + " }," + + + " {" + " \"Sid\":\"Customized1584072051365\"," + " \"Effect\":\"Allow\"," + + " \"Principal\":{" + " \"AWS\":[\"arn:aws:iam::" + destUser.getDomainId() + ":user/" + + destUser.getOwnerId() + "\"]" + " }," + " \"Action\":[\"s3:*\"]," + + " \"Resource\":[\"arn:aws:s3:::" + bucketName + "/*\"]" + " }" + + + "]}"; + + BucketTools.setBucketPolicy(srcUser.getObsClient(), bucketName, policy); + } + + /** + * 清理所有桶策略 + * + * @param user + * @param bucketName + */ + protected void deleteBucketPolicy(UserInfo user, String bucketName) { + BucketTools.deleteBucketPolicy(user.getObsClient(), bucketName); + } + + /** + * 设置桶的ACL + * + * @param obsClient + * @param bucketName + * @param objectKey + */ + protected void setBucketAcl(UserInfo srcUser, UserInfo destUser, String bucketName) { + BucketTools.setBucketAcl(srcUser.getObsClient(), srcUser.getDomainId(), destUser.getDomainId(), bucketName, + true, false); + } + + /** + * 生成一个LifecycleConfiguration + * + * @return + */ + protected LifecycleConfiguration createLifecycleConfiguration() { + LifecycleConfiguration config = new LifecycleConfiguration(); + Rule rule = config.new Rule(); + rule.setEnabled(true); + rule.setId("rule1"); + rule.setPrefix("prefix"); + Transition transition = config.new Transition(); + // 指定满足前缀的对象创建30天后转换 + transition.setDays(29); + // 指定对象转换后的存储类型 + transition.setObjectStorageClass(StorageClassEnum.COLD); + // 直接指定满足前缀的对象转换日期 + // transition.setDate(new + // SimpleDateFormat("yyyy-MM-dd").parse("2018-10-31")); + rule.getTransitions().add(transition); + + NoncurrentVersionTransition noncurrentVersionTransition = config.new NoncurrentVersionTransition(); + // 指定满足前缀的对象成为历史版本30天后转换 + noncurrentVersionTransition.setDays(30); + // 指定历史版本对象转换后的存储类型 + noncurrentVersionTransition.setObjectStorageClass(StorageClassEnum.COLD); +// rule.getNoncurrentVersionTransitions().add(noncurrentVersionTransition); + + config.addRule(rule); + + return config; + } + + /** + * 创建一个BucketCors 对象 + * + * @return + */ + protected BucketCors createSetBucketCorsRequest() { + BucketCors cors = new BucketCors(); + + List rules = new ArrayList(); + BucketCorsRule rule = new BucketCorsRule(); + + ArrayList allowedOrigin = new ArrayList(); + // 指定允许跨域请求的来源 + allowedOrigin.add("http://www.a.com"); + allowedOrigin.add("http://www.b.com"); + rule.setAllowedOrigin(allowedOrigin); + + ArrayList allowedMethod = new ArrayList(); + // 指定允许的跨域请求方法(GET/PUT/DELETE/POST/HEAD) + allowedMethod.add("GET"); + allowedMethod.add("HEAD"); + allowedMethod.add("PUT"); + rule.setAllowedMethod(allowedMethod); + + ArrayList allowedHeader = new ArrayList(); + // 控制在OPTIONS预取指令中Access-Control-Request-Headers头中指定的header是否被允许使用 + allowedHeader.add("x-obs-header"); + rule.setAllowedHeader(allowedHeader); + + ArrayList exposeHeader = new ArrayList(); + // 指定允许用户从应用程序中访问的header + exposeHeader.add("x-obs-expose-header"); + rule.setExposeHeader(exposeHeader); + + // 指定浏览器对特定资源的预取(OPTIONS)请求返回结果的缓存时间,单位为秒 + rule.setMaxAgeSecond(10); + rules.add(rule); + cors.setRules(rules); + + return cors; + } +} diff --git a/app/src/test/java/com/obs/test/buckets/BucketEncryptionTest.java b/app/src/test/java/com/obs/test/buckets/BucketEncryptionTest.java new file mode 100644 index 0000000..c40151b --- /dev/null +++ b/app/src/test/java/com/obs/test/buckets/BucketEncryptionTest.java @@ -0,0 +1,91 @@ +package com.obs.test.buckets; + +import com.obs.services.ObsClient; +import com.obs.services.exception.ObsException; +import com.obs.services.model.BucketEncryption; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.SSEAlgorithmEnum; +import com.obs.services.model.SetBucketEncryptionRequest; +import com.obs.test.TestTools; +import com.obs.test.tools.PrepareTestBucket; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; + +import java.util.Locale; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +public class BucketEncryptionTest { + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + @Test + public void test_set_bucket_encryption_with_kmsID_and_delete() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + BucketEncryption encryption = new BucketEncryption(); + encryption.setKmsKeyId(TestTools.getKMSID()); + encryption.setSseAlgorithm(SSEAlgorithmEnum.KMS); + SetBucketEncryptionRequest request = new SetBucketEncryptionRequest(bucketName, encryption); + HeaderResponse set_response = obsClient.setBucketEncryption(request); + assertEquals(200, set_response.getStatusCode()); + + BucketEncryption encryption_result = obsClient.getBucketEncryption(bucketName); + assertEquals(200, encryption_result.getStatusCode()); + assertEquals(encryption.getKmsKeyId(), encryption_result.getKmsKeyId()); + assertEquals(encryption.getSseAlgorithm(), encryption_result.getSseAlgorithm()); + + HeaderResponse delete_result = obsClient.deleteBucketEncryption(bucketName); + assertEquals(204, delete_result.getStatusCode()); + try { + obsClient.getBucketEncryption(bucketName); + fail("No exception"); + } catch (ObsException e) { + assertEquals(404, e.getResponseCode()); + } + delete_result = obsClient.deleteBucketEncryption(bucketName); + assertEquals(204, delete_result.getStatusCode()); + } + + @Test + public void test_set_bucket_encryption_without_kmsID() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + BucketEncryption encryption = new BucketEncryption(); + encryption.setSseAlgorithm(SSEAlgorithmEnum.KMS); + SetBucketEncryptionRequest request = new SetBucketEncryptionRequest(bucketName, encryption); + + HeaderResponse set_response = obsClient.setBucketEncryption(request); + assertEquals(200, set_response.getStatusCode()); + + BucketEncryption encryption_result = obsClient.getBucketEncryption(bucketName); + assertEquals(200, encryption_result.getStatusCode()); + assertEquals(encryption.getKmsKeyId(), encryption_result.getKmsKeyId()); + assertEquals(encryption.getSseAlgorithm(), encryption_result.getSseAlgorithm()); + } + + @Test + public void test_set_bucket_encryption_with_wrong_kmsID() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + BucketEncryption encryption = new BucketEncryption(); + encryption.setKmsKeyId("WrongKMSID"); + encryption.setSseAlgorithm(SSEAlgorithmEnum.KMS); + SetBucketEncryptionRequest request = new SetBucketEncryptionRequest(bucketName, encryption); + try { + obsClient.setBucketEncryption(request); + fail("No exception"); + } catch (ObsException e) { + assertEquals(400, e.getResponseCode()); + assertTrue(e.getXmlMessage().contains("The configuration of bucket encryption " + + "contains illegal KMSMasterKeyID")); + } + } + +} diff --git a/app/src/test/java/com/obs/test/internal/ObsConvertorTest.java b/app/src/test/java/com/obs/test/internal/ObsConvertorTest.java new file mode 100644 index 0000000..c9521f9 --- /dev/null +++ b/app/src/test/java/com/obs/test/internal/ObsConvertorTest.java @@ -0,0 +1,72 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test.internal; + +import org.junit.Test; + +import com.obs.services.internal.IConvertor; +import com.obs.services.internal.ObsConvertor; +import com.obs.services.model.BucketLoggingConfiguration; +import com.obs.services.model.CanonicalGrantee; +import com.obs.services.model.GrantAndPermission; +import com.obs.services.model.GranteeInterface; +import com.obs.services.model.GroupGrantee; +import com.obs.services.model.Permission; +import com.obs.test.RequestPaymentTest; + +import static org.junit.Assert.assertEquals; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class ObsConvertorTest { + private static final Logger logger = LogManager.getLogger(ObsConvertorTest.class); + + @Test + public void test_transBucketLoggingConfiguration_1() { + IConvertor obsConvertor = ObsConvertor.getInstance(); + + BucketLoggingConfiguration config = new BucketLoggingConfiguration("test_bucket", "object/prefix"); + config.setAgency("log_config_agency"); + + CanonicalGrantee grantee = new CanonicalGrantee("domain_id_grantee"); + + GrantAndPermission targetGrants = new GrantAndPermission(grantee, Permission.PERMISSION_READ); + config.addTargetGrant(targetGrants); + String result = obsConvertor.transBucketLoggingConfiguration(config); + + logger.info(result); + String assertStr = "log_config_agencytest_bucketobject/prefixdomain_id_granteeREAD"; + + assertEquals(assertStr, result); + } + + @Test + public void test_transBucketLoggingConfiguration_2() { + IConvertor obsConvertor = ObsConvertor.getInstance(); + + BucketLoggingConfiguration config = new BucketLoggingConfiguration("test_bucket", "object/prefix"); + config.setAgency("log_config_agency"); + + GrantAndPermission targetGrants = new GrantAndPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ); + config.addTargetGrant(targetGrants); + String result = obsConvertor.transBucketLoggingConfiguration(config); + + logger.info(result); + String assertStr = "log_config_agencytest_bucketobject/prefixEveryoneREAD"; + + assertEquals(assertStr, result); + } +} diff --git a/app/src/test/java/com/obs/test/internal/ObsPropertiesTest.java b/app/src/test/java/com/obs/test/internal/ObsPropertiesTest.java new file mode 100644 index 0000000..207efdb --- /dev/null +++ b/app/src/test/java/com/obs/test/internal/ObsPropertiesTest.java @@ -0,0 +1,41 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test.internal; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.obs.services.internal.ObsProperties; + +public class ObsPropertiesTest { + @Test + public void test_getBoolProperty() { + ObsProperties obsProperties = new ObsProperties(); + obsProperties.setProperty("testKey", "true"); + assertTrue(obsProperties.getBoolProperty("testKey", true)); + + obsProperties.setProperty("testKey", "false"); + assertFalse(obsProperties.getBoolProperty("testKey", true)); + } + + @Test(expected = IllegalArgumentException.class) + public void test_getBoolProperty_IllegalArgumentException() { + ObsProperties obsProperties = new ObsProperties(); + + obsProperties.setProperty("testKey", "ff"); + assertFalse(obsProperties.getBoolProperty("testKey", true)); + } +} diff --git a/app/src/test/java/com/obs/test/internal/V2ConvertorTest.java b/app/src/test/java/com/obs/test/internal/V2ConvertorTest.java new file mode 100644 index 0000000..d3fdc8d --- /dev/null +++ b/app/src/test/java/com/obs/test/internal/V2ConvertorTest.java @@ -0,0 +1,148 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test.internal; + +import static org.junit.Assert.assertEquals; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Test; + +import com.obs.services.internal.IConvertor; +import com.obs.services.internal.V2Convertor; +import com.obs.services.model.AbstractNotification.Filter; +import com.obs.services.model.AbstractNotification.Filter.FilterRule; +import com.obs.services.model.BucketNotificationConfiguration; +import com.obs.services.model.EventTypeEnum; +import com.obs.services.model.FunctionGraphConfiguration; +import com.obs.services.model.ProtocolEnum; +import com.obs.services.model.Redirect; +import com.obs.services.model.RedirectAllRequest; +import com.obs.services.model.RouteRule; +import com.obs.services.model.RouteRuleCondition; +import com.obs.services.model.TopicConfiguration; +import com.obs.services.model.WebsiteConfiguration; + +public class V2ConvertorTest { + private static final Logger logger = LogManager.getLogger(V2ConvertorTest.class); + + @Test + public void test_transBucketNotificationConfiguration() { + IConvertor v2Convertor = V2Convertor.getInstance(); + + BucketNotificationConfiguration config = new BucketNotificationConfiguration(); + + TopicConfiguration topicConfig = new TopicConfiguration(); + topicConfig.setId("id_topicConfig"); + topicConfig.setTopic("your topic"); + topicConfig.getEventTypes().add(EventTypeEnum.OBJECT_CREATED_ALL); + topicConfig.getEventTypes().add(EventTypeEnum.OBJECT_CREATED_COPY); + Filter topicFilter = new Filter(); + topicFilter.getFilterRules().add(new FilterRule("prefix", "topicConfigSmn")); + topicFilter.getFilterRules().add(new FilterRule("suffix", ".topicConfigJpg")); + topicConfig.setFilter(topicFilter); + config.addTopicConfiguration(topicConfig); + + FunctionGraphConfiguration functionConfig = new FunctionGraphConfiguration(); + functionConfig.setId("id_functionConfig"); + functionConfig.setFunctionGraph("your function"); + functionConfig.getEventTypes().add(EventTypeEnum.OBJECT_CREATED_ALL); + functionConfig.getEventTypes().add(EventTypeEnum.OBJECT_CREATED_POST); + Filter functionFilter = new Filter(); + functionFilter.getFilterRules().add(new FilterRule("prefix", "functionConfigFunction")); + functionFilter.getFilterRules().add(new FilterRule("suffix", ".functionConfigMp4")); + functionConfig.setFilter(functionFilter); + config.addFunctionGraphConfiguration(functionConfig); + + String result = v2Convertor.transBucketNotificationConfiguration(config); + + logger.info(result); + + String assertStr = "id_topicConfigprefixtopicConfigSmnsuffix.topicConfigJpgyour topics3:ObjectCreated:*s3:ObjectCreated:Copyid_functionConfigprefixfunctionConfigFunctionsuffix.functionConfigMp4your functions3:ObjectCreated:*s3:ObjectCreated:Post"; + + assertEquals(assertStr, result); + } + + @Test + public void test_transWebsiteConfiguration_with_redirectAllRequestsTo() { + IConvertor v2Convertor = V2Convertor.getInstance(); + + WebsiteConfiguration config = new WebsiteConfiguration(); + + RedirectAllRequest redirectAllRequestsTo = new RedirectAllRequest(); + redirectAllRequestsTo.setHostName("test.host.com"); + redirectAllRequestsTo.setRedirectProtocol(ProtocolEnum.HTTP); + config.setRedirectAllRequestsTo(redirectAllRequestsTo); + + String result = v2Convertor.transWebsiteConfiguration(config); + + logger.info(result); + + String assertStr = "test.host.comhttp"; + + assertEquals(assertStr, result); + } + + @Test + public void test_transWebsiteConfiguration_without_redirectAllRequestsTo() { + IConvertor v2Convertor = V2Convertor.getInstance(); + + WebsiteConfiguration config = new WebsiteConfiguration(); + +// RedirectAllRequest redirectAllRequestsTo = new RedirectAllRequest(); +// redirectAllRequestsTo.setHostName("test.host.com"); +// redirectAllRequestsTo.setRedirectProtocol(ProtocolEnum.HTTP); +// config.setRedirectAllRequestsTo(redirectAllRequestsTo); + + config.setKey("key_test_transWebsiteConfiguration_without_redirectAllRequestsTo"); + + config.setSuffix("suffix_test_transWebsiteConfiguration_without_redirectAllRequestsTo"); + + RouteRule routeRule_1 = new RouteRule(); + Redirect r_1 = new Redirect(); + r_1.setHostName("www.routeRule_1.com"); + r_1.setHttpRedirectCode("306"); + r_1.setRedirectProtocol(ProtocolEnum.HTTP); + r_1.setReplaceKeyPrefixWith("replacekeyprefix_routeRule_1"); + routeRule_1.setRedirect(r_1); + RouteRuleCondition condition_1 = new RouteRuleCondition(); + condition_1.setHttpErrorCodeReturnedEquals("405"); + condition_1.setKeyPrefixEquals("keyprefix_routeRule_1"); + routeRule_1.setCondition(condition_1); + config.getRouteRules().add(routeRule_1); + + + RouteRule routeRule_2 = new RouteRule(); + Redirect r_2 = new Redirect(); + r_2.setHostName("www.routeRule_2.com"); + r_2.setHttpRedirectCode("304"); + r_2.setRedirectProtocol(ProtocolEnum.HTTPS); + r_2.setReplaceKeyPrefixWith("replacekeyprefix_routeRule_2"); + routeRule_2.setRedirect(r_2); + RouteRuleCondition condition_2 = new RouteRuleCondition(); + condition_2.setHttpErrorCodeReturnedEquals("404"); + condition_2.setKeyPrefixEquals("keyprefix_routeRule_2"); + routeRule_2.setCondition(condition_2); + config.getRouteRules().add(routeRule_2); + + String result = v2Convertor.transWebsiteConfiguration(config); + + logger.info(result); + + String assertStr = "suffix_test_transWebsiteConfiguration_without_redirectAllRequestsTokey_test_transWebsiteConfiguration_without_redirectAllRequestsTokeyprefix_routeRule_1405www.routeRule_1.com306replacekeyprefix_routeRule_1httpkeyprefix_routeRule_2404www.routeRule_2.com304replacekeyprefix_routeRule_2https"; + + assertEquals(assertStr, result); + } +} diff --git a/app/src/test/java/com/obs/test/internal/XmlResponsesSaxParserTest.java b/app/src/test/java/com/obs/test/internal/XmlResponsesSaxParserTest.java new file mode 100644 index 0000000..2baeb43 --- /dev/null +++ b/app/src/test/java/com/obs/test/internal/XmlResponsesSaxParserTest.java @@ -0,0 +1,942 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test.internal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.Arrays; +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Ignore; +import org.junit.Test; + +import com.obs.services.internal.handler.XmlResponsesSaxParser; +import com.obs.services.internal.handler.XmlResponsesSaxParser.BucketEncryptionHandler; +import com.obs.services.internal.handler.XmlResponsesSaxParser.BucketNotificationConfigurationHandler; +import com.obs.services.internal.handler.XmlResponsesSaxParser.BucketReplicationConfigurationHandler; +import com.obs.services.internal.handler.XmlResponsesSaxParser.BucketStorageInfoHandler; +import com.obs.services.internal.handler.XmlResponsesSaxParser.BucketWebsiteConfigurationHandler; +import com.obs.services.internal.handler.XmlResponsesSaxParser.ListBucketsHandler; +import com.obs.services.internal.handler.XmlResponsesSaxParser.ListMultipartUploadsHandler; +import com.obs.services.internal.handler.XmlResponsesSaxParser.ListObjectsHandler; +import com.obs.services.internal.handler.XmlResponsesSaxParser.ListPartsHandler; +import com.obs.services.internal.handler.XmlResponsesSaxParser.ListVersionsHandler; +import com.obs.services.internal.io.HttpMethodReleaseInputStream; +import com.obs.services.model.BucketEncryption; +import com.obs.services.model.BucketNotificationConfiguration; +import com.obs.services.model.BucketStorageInfo; +import com.obs.services.model.BucketTypeEnum; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ListBucketsRequest; +import com.obs.services.model.ListBucketsResult; +import com.obs.services.model.ListMultipartUploadsRequest; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ListPartsRequest; +import com.obs.services.model.ListPartsResult; +import com.obs.services.model.ListVersionsRequest; +import com.obs.services.model.ListVersionsResult; +import com.obs.services.model.Multipart; +import com.obs.services.model.MultipartUpload; +import com.obs.services.model.MultipartUploadListing; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.ObsObject; +import com.obs.services.model.Owner; +import com.obs.services.model.ProtocolEnum; +import com.obs.services.model.Redirect; +import com.obs.services.model.RedirectAllRequest; +import com.obs.services.model.ReplicationConfiguration; +import com.obs.services.model.RouteRule; +import com.obs.services.model.RouteRuleCondition; +import com.obs.services.model.SetBucketWebsiteRequest; +import com.obs.services.model.UploadPartRequest; +import com.obs.services.model.VersionOrDeleteMarker; +import com.obs.services.model.WebsiteConfiguration; +import com.obs.test.TestTools; +import com.obs.test.objects.BaseObjectTest; + +public class XmlResponsesSaxParserTest extends BaseObjectTest { + private static final Logger logger = LogManager.getLogger(XmlResponsesSaxParserTest.class); + + @Test + public void test_BucketEncryptionHandler() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("") + .append("") + .append("") + .append("kms") + .append("kmskeyid-value") + .append("") + .append("") + .append("") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + BucketEncryption bucketEncryption = xmlResponsesSaxParser.parse(inputStream, BucketEncryptionHandler.class, false) + .getEncryption(); + + logger.info(bucketEncryption); + + assertEquals("kms", bucketEncryption.getSseAlgorithm().getCode()); + assertEquals("kmskeyid-value", bucketEncryption.getKmsKeyId()); + } + + @Test + public void test_BucketStoragePolicyHandler() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("") + .append("10") + .append("20") + .append("") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + BucketStorageInfo bucketStorageInfo = xmlResponsesSaxParser.parse(inputStream, BucketStorageInfoHandler.class, false) + .getStorageInfo(); + + logger.info(bucketStorageInfo); + + assertEquals(10, bucketStorageInfo.getSize()); + assertEquals(20, bucketStorageInfo.getObjectNumber()); + } + + @Test + public void test_BucketNotificationConfigurationHandler() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("") + .append("") + .append("urn:smn:cn-east-3:4b29a3cb5bd64581bda5714566814bb7:tet522") + .append("ConfigurationId") + .append("") + .append("") + .append("") + .append("prefix") + .append("object") + .append("") + .append("") + .append("suffix") + .append("txt") + .append("") + .append("") + .append("") + .append("ObjectCreated:Put") + .append("") + .append("") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + BucketNotificationConfiguration bucketNotificationConfiguration = xmlResponsesSaxParser.parse(inputStream, BucketNotificationConfigurationHandler.class, false) + .getBucketNotificationConfiguration(); + + logger.info(bucketNotificationConfiguration); + + assertEquals("ConfigurationId", bucketNotificationConfiguration.getTopicConfigurations().get(0).getId()); + assertEquals("urn:smn:cn-east-3:4b29a3cb5bd64581bda5714566814bb7:tet522", bucketNotificationConfiguration.getTopicConfigurations().get(0).getTopic()); + assertEquals("ObjectCreated:Put", bucketNotificationConfiguration.getTopicConfigurations().get(0).getEvents().get(0)); + assertEquals("prefix", bucketNotificationConfiguration.getTopicConfigurations().get(0).getFilter().getFilterRules().get(0).getName()); + assertEquals("object", bucketNotificationConfiguration.getTopicConfigurations().get(0).getFilter().getFilterRules().get(0).getValue()); + assertEquals("suffix", bucketNotificationConfiguration.getTopicConfigurations().get(0).getFilter().getFilterRules().get(1).getName()); + assertEquals("txt", bucketNotificationConfiguration.getTopicConfigurations().get(0).getFilter().getFilterRules().get(1).getValue()); + } + + @Test + public void test_BucketReplicationConfigurationHandler() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("") + .append("") + .append("Rule-1") + .append("Enabled") + .append("") + .append("") + .append("dstbucket") + .append("STANDARD") + .append("") + .append("Enabled") + .append("") + .append("testAcy") + .append("") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + ReplicationConfiguration replicationConfiguration = xmlResponsesSaxParser.parse(inputStream, BucketReplicationConfigurationHandler.class, false) + .getReplicationConfiguration(); + + logger.info(replicationConfiguration); + + assertEquals("testAcy", replicationConfiguration.getAgency()); + assertEquals("Rule-1", replicationConfiguration.getRules().get(0).getId()); + assertEquals("Enabled", replicationConfiguration.getRules().get(0).getStatus().getCode()); + assertEquals("", replicationConfiguration.getRules().get(0).getPrefix()); + assertEquals("dstbucket", replicationConfiguration.getRules().get(0).getDestination().getBucket()); + assertEquals("STANDARD", replicationConfiguration.getRules().get(0).getDestination().getObjectStorageClass().getCode()); + } + + @Test + public void test_BucketWebsiteConfigurationHandler_1() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("" + + "" + + "test.html" + + "error_test.html" + + "" + + "" + + "404keyprefix-1" + + "" + + "replacekeyprefix-1305" + + "www.example.com" + + "http" + + "" + + "" + + "" + + "405keyprefix-2" + + "" + + "replacekeyprefix-2302" + + "www.example.cn" + + "https" + + "" + + "" + + "" + + "") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + WebsiteConfiguration websiteConfiguration = xmlResponsesSaxParser.parse(inputStream, BucketWebsiteConfigurationHandler.class, false) + .getWebsiteConfig(); + + logger.info(websiteConfiguration); + + assertEquals("test.html", websiteConfiguration.getSuffix()); + assertEquals("error_test.html", websiteConfiguration.getKey()); + assertNull(websiteConfiguration.getRedirectAllRequestsTo()); + + assertEquals("404", websiteConfiguration.getRouteRules().get(0).getCondition().getHttpErrorCodeReturnedEquals()); + assertEquals("keyprefix-1", websiteConfiguration.getRouteRules().get(0).getCondition().getKeyPrefixEquals()); + assertEquals("www.example.com", websiteConfiguration.getRouteRules().get(0).getRedirect().getHostName()); + assertEquals("replacekeyprefix-1", websiteConfiguration.getRouteRules().get(0).getRedirect().getReplaceKeyPrefixWith()); + assertEquals("305", websiteConfiguration.getRouteRules().get(0).getRedirect().getHttpRedirectCode()); + assertEquals("http", websiteConfiguration.getRouteRules().get(0).getRedirect().getProtocol()); + + assertEquals("405", websiteConfiguration.getRouteRules().get(1).getCondition().getHttpErrorCodeReturnedEquals()); + assertEquals("keyprefix-2", websiteConfiguration.getRouteRules().get(1).getCondition().getKeyPrefixEquals()); + assertEquals("www.example.cn", websiteConfiguration.getRouteRules().get(1).getRedirect().getHostName()); + assertEquals("replacekeyprefix-2", websiteConfiguration.getRouteRules().get(1).getRedirect().getReplaceKeyPrefixWith()); + assertEquals("302", websiteConfiguration.getRouteRules().get(1).getRedirect().getHttpRedirectCode()); + assertEquals("https", websiteConfiguration.getRouteRules().get(1).getRedirect().getProtocol()); + } + + @Test + public void test_BucketWebsiteConfigurationHandler_2() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("" + + "www.example.comhttp" + + "") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + WebsiteConfiguration websiteConfiguration = xmlResponsesSaxParser.parse(inputStream, BucketWebsiteConfigurationHandler.class, false) + .getWebsiteConfig(); + + logger.info(websiteConfiguration); + + assertEquals("http", websiteConfiguration.getRedirectAllRequestsTo().getRedirectProtocol().getCode()); + assertEquals("www.example.com", websiteConfiguration.getRedirectAllRequestsTo().getHostName()); + assertNull(websiteConfiguration.getSuffix()); + assertNull(websiteConfiguration.getKey()); + assertEquals(0, websiteConfiguration.getRouteRules().size()); + } + + @Test + public void test_ListMultipartUploadsHandler_1() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("" + + "" + + "www.example.comhttp" + + "testpayer" + + "" + + "" + + "" + + "" + + "1000" + + "false" + + "" + + "object-1" + + "000001724B4BA4B39051228929217877" + + "domainID/75b07a9802444d969df2f4326420db96:userID/e1a0cc5f033f4a9aaa3419b963e91c61" + + "75b07a9802444d969df2f4326420db96" + + "STANDARD" + + "2020-05-25T10:05:48.083Z" + + "" + + "" + + "object-2" + + "000001724B4BA6E6904725F03B654010" + + "domainID/75b07a9802444d969df2f4326420db96:userID/e1a0cc5f033f4a9aaa3419b963e91c61" + + "75b07a9802444d969df2f4326420db96" + + "STANDARD" + + "2020-05-25T10:05:48.646Z" + + "" + + "" + + "object-3" + + "000001724B4BAB5A90551D35696950F1" + + "domainID/75b07a9802444d969df2f4326420db96:userID/e1a0cc5f033f4a9aaa3419b963e91c61" + + "75b07a9802444d969df2f4326420db96" + + "STANDARD" + + "2020-05-25T10:05:49.786Z" + + "" + + "") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + ListMultipartUploadsHandler handler = xmlResponsesSaxParser.parse(inputStream, ListMultipartUploadsHandler.class, true); + + assertEquals("testpayer", handler.getBucketName()); + assertEquals("", handler.getKeyMarker()); + assertEquals("", handler.getUploadIdMarker()); + assertEquals(3, handler.getMultipartUploadList().size()); + assertEquals("000001724B4BA4B39051228929217877", handler.getMultipartUploadList().get(0).getUploadId()); + assertEquals("object-1", handler.getMultipartUploadList().get(0).getObjectKey()); + assertEquals("75b07a9802444d969df2f4326420db96", handler.getMultipartUploadList().get(0).getOwner().getId()); + assertEquals("domainID/75b07a9802444d969df2f4326420db96:userID/e1a0cc5f033f4a9aaa3419b963e91c61", handler.getMultipartUploadList().get(0).getInitiator().getId()); + + assertEquals("000001724B4BA6E6904725F03B654010", handler.getMultipartUploadList().get(1).getUploadId()); + assertEquals("object-2", handler.getMultipartUploadList().get(1).getObjectKey()); + assertEquals("75b07a9802444d969df2f4326420db96", handler.getMultipartUploadList().get(1).getOwner().getId()); + assertEquals("domainID/75b07a9802444d969df2f4326420db96:userID/e1a0cc5f033f4a9aaa3419b963e91c61", handler.getMultipartUploadList().get(1).getInitiator().getId()); + + assertEquals("000001724B4BAB5A90551D35696950F1", handler.getMultipartUploadList().get(2).getUploadId()); + assertEquals("object-3", handler.getMultipartUploadList().get(2).getObjectKey()); + assertEquals("75b07a9802444d969df2f4326420db96", handler.getMultipartUploadList().get(2).getOwner().getId()); + assertEquals("domainID/75b07a9802444d969df2f4326420db96:userID/e1a0cc5f033f4a9aaa3419b963e91c61", handler.getMultipartUploadList().get(2).getInitiator().getId()); + } + + @Test + public void test_ListPartsHandler_1() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("" + + "" + + "shenqing-backup-test" + + "object-1" + + "000001724C1622A08048311C9082E710" + + "xxxxxxxxxxxxxxxxxxxxxxxxxxx" + + "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" + + "STANDARD" + + "0" + + "2" + + "1000" + + "false" + + "" + + "1" + + "2020-05-25T13:46:58.722Z" + + "\"1111--097e307ac52ed9b4ad551801fc\"" + + "6291456" + + "" + + "" + + "2" + + "2020-05-25T13:46:58.722Z" + + "\"2222--d097e307ac52ed9b4ad551801fc\"" + + "6291456" + + "" + + "") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + ListPartsHandler handler = xmlResponsesSaxParser.parse(inputStream, + ListPartsHandler.class, true); + + assertEquals("shenqing-backup-test", handler.getBucketName()); + assertEquals("object-1", handler.getObjectKey()); + assertEquals("000001724C1622A08048311C9082E710", handler.getUploadId()); + assertEquals("xxxxxxxxxxxxxxxxxxx", handler.getInitiator().getId()); + assertEquals("xxxxxxxxxxxxxxxxx", handler.getInitiator().getDisplayName()); + assertEquals("xxxxxxxxxxxx", handler.getOwner().getId()); + assertEquals("xxxxxxxxxxx", handler.getOwner().getDisplayName()); + + assertEquals(2, handler.getMultiPartList().size()); + + assertEquals(new Integer(1), handler.getMultiPartList().get(0).getPartNumber()); + assertEquals("\"1111--097e307ac52ed9b4ad551801fc\"", handler.getMultiPartList().get(0).getEtag()); + + assertEquals(new Integer(2), handler.getMultiPartList().get(1).getPartNumber()); + assertEquals("\"2222--d097e307ac52ed9b4ad551801fc\"", handler.getMultiPartList().get(1).getEtag()); + } + + @Test + public void test_ListBucketsHandler() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("" + + "" + + "" + + "75b07a9802444d969df2f4326420db96" + + "" + + "" + + "" + + "0hecmirrorsourcecnnorth4demodfv0" + + "2020-04-26T07:09:26.633Z" + + "cn-north-4" + + "" + + "" + + "20190415" + + "2019-04-19T02:18:51.885Z" + + "cn-north-1" + + "" + + "" + + "wl.posix" + + "2020-03-17T03:35:30.688Z" + + "cn-east-2" + + "" + + "" + + "") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + ListBucketsHandler handler = xmlResponsesSaxParser.parse(inputStream, ListBucketsHandler.class, true); + + List buckets = handler.getBuckets(); + Owner owner = handler.getOwner(); + + logger.info(owner); + logger.info(Arrays.toString(buckets.toArray())); + + assertEquals("75b07a9802444d969df2f4326420db96", owner.getId()); + assertEquals(3, buckets.size()); + assertEquals("75b07a9802444d969df2f4326420db96", buckets.get(0).getOwner().getId()); + assertEquals("75b07a9802444d969df2f4326420db96", buckets.get(1).getOwner().getId()); + assertEquals("75b07a9802444d969df2f4326420db96", buckets.get(2).getOwner().getId()); + + assertEquals("0hecmirrorsourcecnnorth4demodfv0", buckets.get(0).getBucketName()); + assertEquals("20190415", buckets.get(1).getBucketName()); + assertEquals("wl.posix", buckets.get(2).getBucketName()); + + assertEquals("cn-north-4", buckets.get(0).getLocation()); + assertEquals("cn-north-1", buckets.get(1).getLocation()); + assertEquals("cn-east-2", buckets.get(2).getLocation()); + + assertEquals("Sun Apr 26 15:09:26 CST 2020", buckets.get(0).getCreationDate().toString()); + assertEquals("Fri Apr 19 10:18:51 CST 2019", buckets.get(1).getCreationDate().toString()); + assertEquals("Tue Mar 17 11:35:30 CST 2020", buckets.get(2).getCreationDate().toString()); + } + + @Test + public void test_ListObjectsHandler_1() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("" + + "" + + "obssftp-dbbkt" + + "obssftp-dbbkt-log/2020" + + "obssftp-dbbkt-log/2020" + + "obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS" + + "2" + + "/" + + "true" + + "" + + "obssftp-dbbkt-log/2020-02-25-02-58-23-LYXAHNPQ70EELSXE" + + "2020-02-25T02:58:23.694Z" + + "\"84ef7d8a93ec03307404f9625754340f\"" + + "104712" + + "00000000000000000000000000000000" + + "STANDARD" + + "" + + "" + + "obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS" + + "2020-02-25T02:58:49.993Z" + + "\"1bee45a60f4aaef23305d217b0653c6a\"" + + "254" + + "00000000000000000000000000000000" + + "STANDARD" + + "" + + "") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + ListObjectsHandler listObjectsHandler = xmlResponsesSaxParser.parse(inputStream, ListObjectsHandler.class, true); + + assertEquals("obssftp-dbbkt", listObjectsHandler.getBucketName()); + assertEquals("obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS", listObjectsHandler.getMarkerForNextListing()); + assertEquals("obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS", listObjectsHandler.getNextMarker()); + assertEquals("/", listObjectsHandler.getRequestDelimiter()); + assertTrue(listObjectsHandler.isListingTruncated()); + assertEquals("obssftp-dbbkt-log/2020", listObjectsHandler.getRequestMarker()); + assertEquals(2, listObjectsHandler.getRequestMaxKeys()); + assertEquals("obssftp-dbbkt-log/2020", listObjectsHandler.getRequestPrefix()); + assertEquals(0, listObjectsHandler.getCommonPrefixes().size()); + assertEquals(0, listObjectsHandler.getExtenedCommonPrefixes().size()); + assertEquals(2, listObjectsHandler.getObjects().size()); + + assertEquals("obssftp-dbbkt", listObjectsHandler.getObjects().get(0).getBucketName()); + assertEquals("obssftp-dbbkt-log/2020-02-25-02-58-23-LYXAHNPQ70EELSXE", listObjectsHandler.getObjects().get(0).getObjectKey()); + assertEquals("Tue Feb 25 10:58:23 CST 2020", listObjectsHandler.getObjects().get(0).getMetadata().getLastModified().toString()); + assertEquals("\"84ef7d8a93ec03307404f9625754340f\"", listObjectsHandler.getObjects().get(0).getMetadata().getEtag()); + assertEquals(Long.valueOf(104712), listObjectsHandler.getObjects().get(0).getMetadata().getContentLength()); + assertEquals("00000000000000000000000000000000", listObjectsHandler.getObjects().get(0).getOwner().getId()); + assertEquals("STANDARD", listObjectsHandler.getObjects().get(0).getMetadata().getStorageClass()); + + assertEquals("obssftp-dbbkt", listObjectsHandler.getObjects().get(1).getBucketName()); + assertEquals("obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS", listObjectsHandler.getObjects().get(1).getObjectKey()); + assertEquals("Tue Feb 25 10:58:49 CST 2020", listObjectsHandler.getObjects().get(1).getMetadata().getLastModified().toString()); + assertEquals("\"1bee45a60f4aaef23305d217b0653c6a\"", listObjectsHandler.getObjects().get(1).getMetadata().getEtag()); + assertEquals(Long.valueOf(254), listObjectsHandler.getObjects().get(1).getMetadata().getContentLength()); + assertEquals("00000000000000000000000000000000", listObjectsHandler.getObjects().get(1).getOwner().getId()); + assertEquals("STANDARD", listObjectsHandler.getObjects().get(1).getMetadata().getStorageClass()); + } + + @Test + public void test_ListObjectsHandler_2() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("" + + "" + + "obssftp-dbbkt" + + "" + + "" + + "obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS" + + "2" + + "true" + + "" + + "obssftp-dbbkt-log/2020-02-25-02-58-23-LYXAHNPQ70EELSXE" + + "2020-02-25T02:58:23.694Z" + + "\"84ef7d8a93ec03307404f9625754340f\"" + + "104712" + + "00000000000000000000000000000000" + + "STANDARD" + + "" + + "" + + "obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS" + + "2020-02-25T02:58:49.993Z" + + "\"1bee45a60f4aaef23305d217b0653c6a\"" + + "254" + + "00000000000000000000000000000000" + + "STANDARD" + + "" + + "") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + ListObjectsHandler listObjectsHandler = xmlResponsesSaxParser.parse(inputStream, ListObjectsHandler.class, true); + + assertEquals("obssftp-dbbkt", listObjectsHandler.getBucketName()); + assertEquals("obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS", listObjectsHandler.getMarkerForNextListing()); + assertEquals("obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS", listObjectsHandler.getNextMarker()); + assertNull(listObjectsHandler.getRequestDelimiter()); + assertTrue(listObjectsHandler.isListingTruncated()); + assertEquals("", listObjectsHandler.getRequestMarker()); + assertEquals(2, listObjectsHandler.getRequestMaxKeys()); + assertEquals("", listObjectsHandler.getRequestPrefix()); + assertEquals(0, listObjectsHandler.getCommonPrefixes().size()); + assertEquals(0, listObjectsHandler.getExtenedCommonPrefixes().size()); + assertEquals(2, listObjectsHandler.getObjects().size()); + + assertEquals("obssftp-dbbkt", listObjectsHandler.getObjects().get(0).getBucketName()); + assertEquals("obssftp-dbbkt-log/2020-02-25-02-58-23-LYXAHNPQ70EELSXE", listObjectsHandler.getObjects().get(0).getObjectKey()); + assertEquals("Tue Feb 25 10:58:23 CST 2020", listObjectsHandler.getObjects().get(0).getMetadata().getLastModified().toString()); + assertEquals("\"84ef7d8a93ec03307404f9625754340f\"", listObjectsHandler.getObjects().get(0).getMetadata().getEtag()); + assertEquals(Long.valueOf(104712), listObjectsHandler.getObjects().get(0).getMetadata().getContentLength()); + assertEquals("00000000000000000000000000000000", listObjectsHandler.getObjects().get(0).getOwner().getId()); + assertEquals("STANDARD", listObjectsHandler.getObjects().get(0).getMetadata().getStorageClass()); + + assertEquals("obssftp-dbbkt", listObjectsHandler.getObjects().get(1).getBucketName()); + assertEquals("obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS", listObjectsHandler.getObjects().get(1).getObjectKey()); + assertEquals("Tue Feb 25 10:58:49 CST 2020", listObjectsHandler.getObjects().get(1).getMetadata().getLastModified().toString()); + assertEquals("\"1bee45a60f4aaef23305d217b0653c6a\"", listObjectsHandler.getObjects().get(1).getMetadata().getEtag()); + assertEquals(Long.valueOf(254), listObjectsHandler.getObjects().get(1).getMetadata().getContentLength()); + assertEquals("00000000000000000000000000000000", listObjectsHandler.getObjects().get(1).getOwner().getId()); + assertEquals("STANDARD", listObjectsHandler.getObjects().get(1).getMetadata().getStorageClass()); + } + + @Test + public void test_ListObjectsHandler_3() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("" + + "" + + "obssftp-dbbkt" + + "" + + "" + + "obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS" + + "2" + + "true" + + "") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + ListObjectsHandler listObjectsHandler = xmlResponsesSaxParser.parse(inputStream, ListObjectsHandler.class, true); + + assertEquals("obssftp-dbbkt", listObjectsHandler.getBucketName()); + assertEquals("obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS", listObjectsHandler.getMarkerForNextListing()); + assertEquals("obssftp-dbbkt-log/2020-02-25-02-58-49-AMONRZDP0YLD0RBS", listObjectsHandler.getNextMarker()); + assertNull(listObjectsHandler.getRequestDelimiter()); + assertTrue(listObjectsHandler.isListingTruncated()); + assertEquals("", listObjectsHandler.getRequestMarker()); + assertEquals(2, listObjectsHandler.getRequestMaxKeys()); + assertEquals("", listObjectsHandler.getRequestPrefix()); + assertEquals(0, listObjectsHandler.getCommonPrefixes().size()); + assertEquals(0, listObjectsHandler.getExtenedCommonPrefixes().size()); + assertEquals(0, listObjectsHandler.getObjects().size()); + } + + @Test + public void test_ListVersionsHandler_1() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("" + + "" + + "obssftp-dbbkt" + + "11/" + + "11" + + "" + + "11/1.jpg" + + "G0011171B9720C8FFFFF801B04AED369" + + "2" + + "/" + + "true" + + "" + + "11/" + + "G0011171B9718417FFFF801A04B805CE" + + "true" + + "2020-04-27T02:22:36.567Z" + + "\"d41d8cd98f00b204e9800998ecf8427e\"" + + "0" + + "75b07a9802444d969df2f4326420db96" + + "STANDARD" + + "" + + + "" + + "11/1.jpg" + + "G0011171B9720C8FFFFF801B04AED369" + + "true" + + "2020-04-27T02:23:11.503Z" + + "\"7ea44103eacc6027668bda886c9dda47\"" + + "82740" + + "75b07a9802444d969df2f4326420db96" + + "STANDARD" + + "" + + "") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + ListVersionsHandler listVersionsHandler = xmlResponsesSaxParser.parse(inputStream, ListVersionsHandler.class, true); + + assertEquals("obssftp-dbbkt", listVersionsHandler.getBucketName()); + assertEquals("/", listVersionsHandler.getDelimiter()); + assertEquals("11", listVersionsHandler.getKeyMarker()); + assertEquals("11/1.jpg", listVersionsHandler.getNextKeyMarker()); + assertEquals("G0011171B9720C8FFFFF801B04AED369", listVersionsHandler.getNextVersionIdMarker()); + assertEquals(2, listVersionsHandler.getRequestMaxKeys()); + assertEquals("11/", listVersionsHandler.getRequestPrefix()); + assertTrue(listVersionsHandler.isListingTruncated()); + + assertEquals(0, listVersionsHandler.getCommonPrefixes().size()); + assertEquals(2, listVersionsHandler.getItems().size()); + + assertEquals("obssftp-dbbkt", listVersionsHandler.getItems().get(0).getBucketName()); + assertEquals("11/", listVersionsHandler.getItems().get(0).getKey()); + assertEquals("\"d41d8cd98f00b204e9800998ecf8427e\"", listVersionsHandler.getItems().get(0).getEtag()); + assertEquals("G0011171B9718417FFFF801A04B805CE", listVersionsHandler.getItems().get(0).getVersionId()); + assertTrue(listVersionsHandler.getItems().get(0).isLatest()); + assertEquals("Mon Apr 27 10:22:36 CST 2020", listVersionsHandler.getItems().get(0).getLastModified().toString()); + assertEquals(0L, listVersionsHandler.getItems().get(0).getSize()); + assertEquals("75b07a9802444d969df2f4326420db96", listVersionsHandler.getItems().get(0).getOwner().getId()); + assertEquals("STANDARD", listVersionsHandler.getItems().get(0).getStorageClass()); + + assertEquals("obssftp-dbbkt", listVersionsHandler.getItems().get(1).getBucketName()); + assertEquals("11/1.jpg", listVersionsHandler.getItems().get(1).getKey()); + assertEquals("\"7ea44103eacc6027668bda886c9dda47\"", listVersionsHandler.getItems().get(1).getEtag()); + assertEquals("G0011171B9720C8FFFFF801B04AED369", listVersionsHandler.getItems().get(1).getVersionId()); + assertTrue(listVersionsHandler.getItems().get(1).isLatest()); + assertEquals("Mon Apr 27 10:23:11 CST 2020", listVersionsHandler.getItems().get(1).getLastModified().toString()); + assertEquals(82740L, listVersionsHandler.getItems().get(1).getSize()); + assertEquals("75b07a9802444d969df2f4326420db96", listVersionsHandler.getItems().get(1).getOwner().getId()); + assertEquals("STANDARD", listVersionsHandler.getItems().get(1).getStorageClass()); + } + + @Test + public void test_ListVersionsHandler_2() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("" + + "" + + "obssftp-dbbkt" + + "" + + "" + + "" + + "2" + + "true" + + "" + + "11/" + + "G0011171B9718417FFFF801A04B805CE" + + "true" + + "2020-04-27T02:22:36.567Z" + + "\"d41d8cd98f00b204e9800998ecf8427e\"" + + "0" + + "75b07a9802444d969df2f4326420db96" + + "STANDARD" + + "" + + + "" + + "11/1.jpg" + + "G0011171B9720C8FFFFF801B04AED369" + + "true" + + "2020-04-27T02:23:11.503Z" + + "\"7ea44103eacc6027668bda886c9dda47\"" + + "82740" + + "75b07a9802444d969df2f4326420db96" + + "STANDARD" + + "" + + "") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + ListVersionsHandler listVersionsHandler = xmlResponsesSaxParser.parse(inputStream, ListVersionsHandler.class, true); + + assertEquals("obssftp-dbbkt", listVersionsHandler.getBucketName()); + assertNull(listVersionsHandler.getDelimiter()); + assertEquals("", listVersionsHandler.getKeyMarker()); + assertNull(listVersionsHandler.getNextKeyMarker()); + assertNull(listVersionsHandler.getNextVersionIdMarker()); + assertEquals(2, listVersionsHandler.getRequestMaxKeys()); + assertEquals("", listVersionsHandler.getRequestPrefix()); + assertTrue(listVersionsHandler.isListingTruncated()); + + assertEquals(0, listVersionsHandler.getCommonPrefixes().size()); + assertEquals(2, listVersionsHandler.getItems().size()); + + assertEquals("obssftp-dbbkt", listVersionsHandler.getItems().get(0).getBucketName()); + assertEquals("11/", listVersionsHandler.getItems().get(0).getKey()); + assertEquals("\"d41d8cd98f00b204e9800998ecf8427e\"", listVersionsHandler.getItems().get(0).getEtag()); + assertEquals("G0011171B9718417FFFF801A04B805CE", listVersionsHandler.getItems().get(0).getVersionId()); + assertTrue(listVersionsHandler.getItems().get(0).isLatest()); + assertEquals("Mon Apr 27 10:22:36 CST 2020", listVersionsHandler.getItems().get(0).getLastModified().toString()); + assertEquals(0L, listVersionsHandler.getItems().get(0).getSize()); + assertEquals("75b07a9802444d969df2f4326420db96", listVersionsHandler.getItems().get(0).getOwner().getId()); + assertEquals("STANDARD", listVersionsHandler.getItems().get(0).getStorageClass()); + + assertEquals("obssftp-dbbkt", listVersionsHandler.getItems().get(1).getBucketName()); + assertEquals("11/1.jpg", listVersionsHandler.getItems().get(1).getKey()); + assertEquals("\"7ea44103eacc6027668bda886c9dda47\"", listVersionsHandler.getItems().get(1).getEtag()); + assertEquals("G0011171B9720C8FFFFF801B04AED369", listVersionsHandler.getItems().get(1).getVersionId()); + assertTrue(listVersionsHandler.getItems().get(1).isLatest()); + assertEquals("Mon Apr 27 10:23:11 CST 2020", listVersionsHandler.getItems().get(1).getLastModified().toString()); + assertEquals(82740L, listVersionsHandler.getItems().get(1).getSize()); + assertEquals("75b07a9802444d969df2f4326420db96", listVersionsHandler.getItems().get(1).getOwner().getId()); + assertEquals("STANDARD", listVersionsHandler.getItems().get(1).getStorageClass()); + } + + @Test + public void test_ListVersionsHandler_3() { + XmlResponsesSaxParser xmlResponsesSaxParser = new XmlResponsesSaxParser(); + + String xml = new StringBuilder("" + + "" + + "obssftp-dbbkt" + + "" + + "" + + "" + + "2" + + "true" + + "") + .toString(); + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + ListVersionsHandler listVersionsHandler = xmlResponsesSaxParser.parse(inputStream, ListVersionsHandler.class, true); + + assertEquals("obssftp-dbbkt", listVersionsHandler.getBucketName()); + assertNull(listVersionsHandler.getDelimiter()); + assertEquals("", listVersionsHandler.getKeyMarker()); + assertNull(listVersionsHandler.getNextKeyMarker()); + assertNull(listVersionsHandler.getNextVersionIdMarker()); + assertEquals(2, listVersionsHandler.getRequestMaxKeys()); + assertEquals("", listVersionsHandler.getRequestPrefix()); + assertTrue(listVersionsHandler.isListingTruncated()); + + assertEquals(0, listVersionsHandler.getCommonPrefixes().size()); + assertEquals(0, listVersionsHandler.getItems().size()); + } + + @Test + @Ignore + public void test_set_bucket_website_1() { + WebsiteConfiguration websiteConfig = new WebsiteConfiguration(); + websiteConfig.setSuffix("test.html"); + websiteConfig.setKey("error_test.html"); + + RouteRule rule1 = new RouteRule(); + Redirect r1 = new Redirect(); + r1.setHostName("www.example.com"); + r1.setHttpRedirectCode("305"); + r1.setRedirectProtocol(ProtocolEnum.HTTP); + r1.setReplaceKeyPrefixWith("replacekeyprefix-1"); + rule1.setRedirect(r1); + RouteRuleCondition condition1 = new RouteRuleCondition(); + condition1.setHttpErrorCodeReturnedEquals("404"); + condition1.setKeyPrefixEquals("keyprefix-1"); + rule1.setCondition(condition1); + + RouteRule rule2 = new RouteRule(); + Redirect r2 = new Redirect(); + r2.setHostName("www.example.cn"); + r2.setHttpRedirectCode("302"); + r2.setRedirectProtocol(ProtocolEnum.HTTPS); + r2.setReplaceKeyPrefixWith("replacekeyprefix-2"); + rule2.setRedirect(r2); + RouteRuleCondition condition2 = new RouteRuleCondition(); + condition2.setHttpErrorCodeReturnedEquals("405"); + condition2.setKeyPrefixEquals("keyprefix-2"); + rule2.setCondition(condition2); + + websiteConfig.getRouteRules().add(rule1); + websiteConfig.getRouteRules().add(rule2); + + SetBucketWebsiteRequest request = new SetBucketWebsiteRequest("shenqing-test-bucket", websiteConfig); + + TestTools.getExternalEnvironment().setBucketWebsite(request); + } + + @Test + @Ignore + public void test_set_bucket_website_2() { + WebsiteConfiguration websiteConfig = new WebsiteConfiguration(); + RedirectAllRequest redirectAll = new RedirectAllRequest(); + redirectAll.setHostName("www.example.com"); + redirectAll.setRedirectProtocol(ProtocolEnum.HTTP); + websiteConfig.setRedirectAllRequestsTo(redirectAll); + + SetBucketWebsiteRequest request = new SetBucketWebsiteRequest("shenqing-test-bucket", websiteConfig); + + TestTools.getExternalEnvironment().setBucketWebsite(request); + } + + @Test + @Ignore + public void test_list_bucket() { + ListBucketsRequest request = new ListBucketsRequest(); + request.setQueryLocation(true); + request.setBucketType(BucketTypeEnum.PFS); + List buckets = TestTools.getExternalEnvironment().listBuckets(request); + for(ObsBucket bucket : buckets){ + System.out.println("BucketName:" + bucket.getBucketName() + " CreationDate:" + bucket.getCreationDate() + " Location:" + bucket.getLocation()); + } + } + + @Test + @Ignore + public void test_list_objects() { + ListObjectsRequest request = new ListObjectsRequest("0hecmirrortargetcnnorth4demodfv0"); + request.setMaxKeys(2); + request.setMarker("obssftp-dbbkt-log/2020"); + request.setPrefix("obssftp-dbbkt-log/2020"); + request.setDelimiter("/"); + ObjectListing result = TestTools.getExternalEnvironment().listObjects(request); + for(ObsObject obsObject : result.getObjects()){ + System.out.println(obsObject); + } + } + + @Test + @Ignore + public void test_list_versions() { + ListVersionsRequest request = new ListVersionsRequest("shenqing-test-bucket"); + request.setMaxKeys(2); + request.setKeyMarker("11"); + request.setPrefix("11/"); + request.setVersionIdMarker(""); + request.setDelimiter("/"); + ListVersionsResult result = TestTools.getExternalEnvironment().listVersions(request); + for(VersionOrDeleteMarker obsObject : result.getVersions()){ + System.out.println(obsObject); + } + } + + @Test + @Ignore + public void test_list_multipartuploads() { +// InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest("testpayer", "object-1"); +// TestTools.getExternalEnvironment().initiateMultipartUpload(initRequest); +// initRequest = new InitiateMultipartUploadRequest("testpayer", "object-2"); +// TestTools.getExternalEnvironment().initiateMultipartUpload(initRequest); +// initRequest = new InitiateMultipartUploadRequest("testpayer", "object-3"); +// TestTools.getExternalEnvironment().initiateMultipartUpload(initRequest); +// initRequest = new InitiateMultipartUploadRequest("testpayer", "object-4"); +// TestTools.getExternalEnvironment().initiateMultipartUpload(initRequest); + + ListMultipartUploadsRequest request = new ListMultipartUploadsRequest("testpayer"); + MultipartUploadListing result = TestTools.getExternalEnvironment().listMultipartUploads(request); + for(MultipartUpload multipartUpload : result.getMultipartTaskList()){ + System.out.println(multipartUpload); + } + } + + @Test + @Ignore + public void test_list_parts() { + + String bucketName = "shenqing-backup-test"; + String objectKey = "object-1"; + /** + InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, objectKey); + InitiateMultipartUploadResult initResult = TestTools.getInnerEnvironment().initiateMultipartUpload(initRequest); + + UploadPartRequest request = new UploadPartRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setUploadId(initResult.getUploadId()); + request.setPartNumber(1); + request.setInput(new ByteArrayInputStream(getByte(6 * 1024 * 1024))); + TestTools.getInnerEnvironment().uploadPart(request); + + request = new UploadPartRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setUploadId(initResult.getUploadId()); + request.setPartNumber(2); + request.setInput(new ByteArrayInputStream(getByte(6 * 1024 * 1024))); + TestTools.getInnerEnvironment().uploadPart(request); + */ + ListPartsRequest listPartRequest = new ListPartsRequest(bucketName, objectKey, "000001724C1622A08048311C9082E710"); + ListPartsResult result = TestTools.getPipelineEnvironment().listParts(listPartRequest); + for(Multipart part : result.getMultipartList()){ + System.out.println(part.getPartNumber()); + } + } +} diff --git a/app/src/test/java/com/obs/test/internal/util/JSONChangeTest.java b/app/src/test/java/com/obs/test/internal/util/JSONChangeTest.java new file mode 100644 index 0000000..c271765 --- /dev/null +++ b/app/src/test/java/com/obs/test/internal/util/JSONChangeTest.java @@ -0,0 +1,38 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.test.internal.util; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +import com.obs.services.internal.OefExceptionMessage; +import com.obs.services.internal.utils.JSONChange; + +public class JSONChangeTest { + @Test + public void test_trans_oef_exception_message() { + String jsonTxt = "{\"code\": \"OEF.0006\", " + + "\"message\": \"Attribute not found.\", " + + "\"request_id\": \"test_request_id\"}"; + + OefExceptionMessage oefException = (OefExceptionMessage) JSONChange + .jsonToObj(new OefExceptionMessage(), jsonTxt); + + assertEquals(oefException.getCode(), "OEF.0006"); + assertEquals(oefException.getMessage(), "Attribute not found."); + assertEquals(oefException.getRequestId(), "test_request_id"); + } +} diff --git a/app/src/test/java/com/obs/test/internal/util/ObjectUtilsTest.java b/app/src/test/java/com/obs/test/internal/util/ObjectUtilsTest.java new file mode 100644 index 0000000..6c220f0 --- /dev/null +++ b/app/src/test/java/com/obs/test/internal/util/ObjectUtilsTest.java @@ -0,0 +1,59 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test.internal.util; + +import org.junit.Test; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +import com.obs.services.internal.utils.ObjectUtils; + +public class ObjectUtilsTest { + + @Test + public void test_isEquals() { + String str1 = null; + String str2 = null; + assertTrue(ObjectUtils.isEquals(str1, str2)); + + str1 = null; + str2 = ""; + assertFalse(ObjectUtils.isEquals(str1, str2)); + + str1 = null; + str2 = "1"; + assertFalse(ObjectUtils.isEquals(str1, str2)); + + str1 = ""; + str2 = null; + assertFalse(ObjectUtils.isEquals(str1, str2)); + + str1 = "1"; + str2 = null; + assertFalse(ObjectUtils.isEquals(str1, str2)); + + str1 = ""; + str2 = ""; + assertTrue(ObjectUtils.isEquals(str1, str2)); + + str1 = "1"; + str2 = "1"; + assertTrue(ObjectUtils.isEquals(str1, str2)); + + str1 = "1"; + str2 = "2"; + assertFalse(ObjectUtils.isEquals(str1, str2)); + } +} diff --git a/app/src/test/java/com/obs/test/internal/util/ReflectUtilsTest.java b/app/src/test/java/com/obs/test/internal/util/ReflectUtilsTest.java new file mode 100644 index 0000000..171d6f4 --- /dev/null +++ b/app/src/test/java/com/obs/test/internal/util/ReflectUtilsTest.java @@ -0,0 +1,42 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.test.internal.util; + +public class ReflectUtilsTest { +// @Test +// public void test_set_ObsFSBucket_innerClient() { +// ObsClient obsClient = TestTools.getExternalEnvironment(); +// +// ObsFSBucket fsBucket = new ObsFSBucket("bucketName", "location"); +// ReflectUtils.setInnerClient(fsBucket, obsClient); +// } +// +// @Test +// public void test_set_ObsFSFile_innerClient() { +// ObsClient obsClient = TestTools.getExternalEnvironment(); +// +// ObsFSFile obsFile = new ObsFSFile("bucketName", "objectKey", "etag", "versionId", StorageClassEnum.STANDARD, "objectUrl"); +// ReflectUtils.setInnerClient(obsFile, obsClient); +// } +// +// @Test +// public void test_set_ObsFSFolder_innerClient() { +// ObsClient obsClient = TestTools.getExternalEnvironment(); +// +// ObsFSFolder obsFolder = new ObsFSFolder("bucketName", "objectKey", "etag", "versionId", StorageClassEnum.STANDARD, "objectUrl"); +// ReflectUtils.setInnerClient(obsFolder, obsClient); +// } +} diff --git a/app/src/test/java/com/obs/test/internal/util/RestUtilsTest.java b/app/src/test/java/com/obs/test/internal/util/RestUtilsTest.java new file mode 100644 index 0000000..4401fe5 --- /dev/null +++ b/app/src/test/java/com/obs/test/internal/util/RestUtilsTest.java @@ -0,0 +1,67 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.obs.test.internal.util; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import com.obs.services.internal.utils.RestUtils; + +public class RestUtilsTest { + @Test + public void test_encodeUrlString() { + String url = "aa bb.jpg?query=aa bb~*"; + String result = RestUtils.encodeUrlString(url); + System.out.println(result); + assertEquals("aa%20bb.jpg%3Fquery%3Daa%20bb~%2A", result); + + url = "aa+bb.jpg?query=aa+bb"; + result = RestUtils.encodeUrlString(url); + System.out.println(result); + assertEquals("aa%2Bbb.jpg%3Fquery%3Daa%2Bbb", result); + + } + + @Test + public void test_uriEncode() { + String url = "aa bb.jpg?query=aa bb~*"; + String result = RestUtils.uriEncode(url, true); + System.out.println(result); + assertEquals("aa bb.jpg?query=aa bb~*", result); + + url = "aa+bb.jpg?query=aa+bb"; + result = RestUtils.uriEncode(url, true); + System.out.println(result); + assertEquals("aa+bb.jpg?query=aa+bb", result); + + url = "aa+bb.jpg?中文=aa+bb"; + result = RestUtils.uriEncode(url, true); + System.out.println(result); + assertEquals("aa+bb.jpg?%E4%B8%AD%E6%96%87=aa+bb", result); + + url = "aa+bb.jpg?中afda文=aa+b一b"; + result = RestUtils.uriEncode(url, true); + System.out.println(result); + assertEquals("aa+bb.jpg?%E4%B8%ADafda%E6%96%87=aa+b%E4%B8%80b", result); + + url = "aa+bb.jpg?䲀afda文=aa+b一b"; + result = RestUtils.uriEncode(url, true); + System.out.println(result); + assertEquals("aa+bb.jpg?䲀afda%E6%96%87=aa+b%E4%B8%80b", result); + + } +} diff --git a/app/src/test/java/com/obs/test/internal/util/ServiceUtilsTest.java b/app/src/test/java/com/obs/test/internal/util/ServiceUtilsTest.java new file mode 100644 index 0000000..bf7bd05 --- /dev/null +++ b/app/src/test/java/com/obs/test/internal/util/ServiceUtilsTest.java @@ -0,0 +1,185 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test.internal.util; + +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import org.junit.Test; + +import com.obs.services.internal.utils.ServiceUtils; + +public class ServiceUtilsTest { + @Test + public void test_isBucketNameValidDNSName() { + assertFalse(ServiceUtils.isBucketNameValidDNSName("Test")); + assertFalse(ServiceUtils.isBucketNameValidDNSName("aa")); + assertFalse(ServiceUtils.isBucketNameValidDNSName(".")); + + assertFalse(ServiceUtils.isBucketNameValidDNSName("192.168.0.1")); + assertFalse(ServiceUtils.isBucketNameValidDNSName("0.0.0.0")); + assertFalse(ServiceUtils.isBucketNameValidDNSName("127.0.0.1")); + + assertFalse(ServiceUtils.isBucketNameValidDNSName("test..t")); + assertFalse(ServiceUtils.isBucketNameValidDNSName("test.-t")); + assertFalse(ServiceUtils.isBucketNameValidDNSName("test.t-")); + assertFalse(ServiceUtils.isBucketNameValidDNSName(".test")); + assertFalse(ServiceUtils.isBucketNameValidDNSName("test-")); + assertFalse(ServiceUtils.isBucketNameValidDNSName("test-")); + + + assertTrue(ServiceUtils.isBucketNameValidDNSName("test.t-t")); + assertTrue(ServiceUtils.isBucketNameValidDNSName("test")); + assertTrue(ServiceUtils.isBucketNameValidDNSName("127.0.0")); + } + + @Test + public void test_toHex() { + byte[] hash = new byte[]{-123, 119, 127, 39, 10, -41, -49, 42, 121, 9, -127, -69, -82, 60, 78, 72, 74, 29, -59, 94, 36, -89, 115, -112, -42, -110, -5, -15, -49, -6, 18, -6}; + String result = ServiceUtils.toHex(hash); + System.out.println(result); + assertEquals("85777f270ad7cf2a790981bbae3c4e484a1dc55e24a77390d692fbf1cffa12fa", result); + + hash = new byte[]{}; + result = ServiceUtils.toHex(hash); + System.out.println(result); + assertEquals("", result); + + hash = new byte[]{0}; + result = ServiceUtils.toHex(hash); + System.out.println(result); + assertEquals("00", result); + + hash = new byte[]{9}; + result = ServiceUtils.toHex(hash); + System.out.println(result); + assertEquals("09", result); + + hash = new byte[]{-1}; + result = ServiceUtils.toHex(hash); + System.out.println(result); + assertEquals("ff", result); + + hash = new byte[]{-111}; + result = ServiceUtils.toHex(hash); + System.out.println(result); + assertEquals("91", result); + + hash = null; + result = ServiceUtils.toHex(hash); + System.out.println(result); + assertNull(result); + } + + @Test + public void test_fromHex() { + String hexData = "85777f270ad7cf2a790981bbae3c4e484a1dc55e24a77390d692fbf1cffa12fa"; + byte[] result = ServiceUtils.fromHex(hexData); + System.out.println(Arrays.toString(result)); + assertEquals("[-123, 119, 127, 39, 10, -41, -49, 42, 121, 9, -127, -69, -82, 60, 78, 72, 74, 29, -59, 94, 36, -89, 115, -112, -42, -110, -5, -15, -49, -6, 18, -6]", Arrays.toString(result)); + + hexData = "85777f"; + result = ServiceUtils.fromHex(hexData); + System.out.println(Arrays.toString(result)); + assertEquals("[-123, 119, 127]", Arrays.toString(result)); + + hexData = ""; + result = ServiceUtils.fromHex(hexData); + System.out.println(Arrays.toString(result)); + assertEquals("[]", Arrays.toString(result)); + + hexData = null; + result = ServiceUtils.fromHex(hexData); + System.out.println(Arrays.toString(result)); + assertEquals("[]", Arrays.toString(result)); + + hexData = "526ec0573003f19b67d9cdf3a419628a"; + result = ServiceUtils.fromHex(hexData); + System.out.println(Arrays.toString(result)); + assertEquals("[]", Arrays.toString(result)); + } + + @Test + public void test_fromHex_2() { + String hexData = "526ec0573003f19b67d9cdf3a419628a"; + byte[] result = ServiceUtils.fromHex(hexData); + System.out.println(Arrays.toString(result)); + assertEquals("[82, 110, -64, 87, 48, 3, -15, -101, 103, -39, -51, -13, -92, 25, 98, -118]", Arrays.toString(result)); + + String result2 = ServiceUtils.toBase64(result); + System.out.println(result2); + + assertEquals("Um7AVzAD8Ztn2c3zpBliig==", result2); + } + + @Test + public void test_isValid() { + assertFalse(ServiceUtils.isValid(null)); + assertFalse(ServiceUtils.isValid("")); + assertFalse(ServiceUtils.isValid(" ")); + assertFalse(ServiceUtils.isValid(" ")); + assertTrue(ServiceUtils.isValid(" . ")); + } + + @Test + public void test_isValid2() { + assertFalse(ServiceUtils.isValid2(null)); + assertFalse(ServiceUtils.isValid2("")); + assertTrue(ServiceUtils.isValid2(" ")); + assertTrue(ServiceUtils.isValid2(" ")); + assertTrue(ServiceUtils.isValid2(" . ")); + } + + @Test + public void test_toValid() { + assertEquals("", ServiceUtils.toValid(null)); + assertEquals(" ", ServiceUtils.toValid(" ")); + assertEquals(" a ", ServiceUtils.toValid(" a ")); + } + + @Test(expected = IllegalArgumentException.class) + public void test_asserParameterNotNull_1() { + ServiceUtils.asserParameterNotNull(null, "false"); + } + + @Test(expected = IllegalArgumentException.class) + public void test_asserParameterNotNull_2() { + ServiceUtils.asserParameterNotNull("", "false"); + } + + @Test(expected = IllegalArgumentException.class) + public void test_asserParameterNotNull_3() { + ServiceUtils.asserParameterNotNull(" ", "false"); + } + + @Test + public void test_asserParameterNotNull_4() { + ServiceUtils.asserParameterNotNull(" . ", "false"); + } + + @Test(expected = IllegalArgumentException.class) + public void test_asserParameterNotNull2_1() { + ServiceUtils.asserParameterNotNull2(null, "false"); + } + + @Test + public void test_asserParameterNotNull2_2() { + ServiceUtils.asserParameterNotNull2(" . ", "false"); + } +} diff --git a/app/src/test/java/com/obs/test/objects/AppendObjectTest.java b/app/src/test/java/com/obs/test/objects/AppendObjectTest.java new file mode 100644 index 0000000..8c87046 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/AppendObjectTest.java @@ -0,0 +1,111 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.AppendObjectRequest; +import com.obs.services.model.AppendObjectResult; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.test.tools.PrepareTestBucket; +import com.obs.test.TestTools; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class AppendObjectTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + @Test + public void test_appendObject_with_chinese_metadata_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_appendObject_with_chinese_metadata_001"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("测试中文"); + metadata.addUserMetadata("test-chinese", "测试中文"); + + AppendObjectRequest request = new AppendObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + AppendObjectResult appendResult = obsClient.appendObject(request); + + assertEquals(200, appendResult.getStatusCode()); + + GetObjectMetadataRequest get_request = new GetObjectMetadataRequest(); + get_request.setIsEncodeHeaders(false); + get_request.setObjectKey(objectKey); + get_request.setBucketName(bucketName); + ObjectMetadata get_result = obsClient.getObjectMetadata(get_request); + + assertEquals(200, get_result.getStatusCode()); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", get_result.getUserMetadata("test-chinese")); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", get_result.getContentDisposition()); + } + + @Test + public void test_appendObject_with_chinese_metadata_002() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_appendObject_with_chinese_metadata_002"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("【】,"); + metadata.addUserMetadata("test-chinese", "【】,"); + + AppendObjectRequest request = new AppendObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + try { + obsClient.appendObject(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } + + @Test + public void test_appendObject_with_chinese_metadata_003() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_appendObject_with_chinese_metadata_003"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("测试中文"); + metadata.addUserMetadata("test-chinese", "测试中文"); + + AppendObjectRequest request = new AppendObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setIsEncodeHeaders(false); + request.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + try { + obsClient.appendObject(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } +} diff --git a/app/src/test/java/com/obs/test/objects/BaseObjectTest.java b/app/src/test/java/com/obs/test/objects/BaseObjectTest.java new file mode 100644 index 0000000..bbf99f6 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/BaseObjectTest.java @@ -0,0 +1,229 @@ +package com.obs.test.objects; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import com.obs.services.model.AbortMultipartUploadRequest; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.CopyObjectRequest; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.ListMultipartUploadsRequest; +import com.obs.services.model.ListPartsRequest; +import com.obs.services.model.ListPartsResult; +import com.obs.services.model.Multipart; +import com.obs.services.model.PartEtag; +import com.obs.services.model.PutObjectRequest; +import com.obs.services.model.PutObjectResult; +import com.obs.services.model.UploadPartRequest; + +import com.obs.test.UserInfo; +import com.obs.test.buckets.BaseBucketTest; + +public abstract class BaseObjectTest extends BaseBucketTest { + protected byte[] getByte(int size) { + return getByte(size, (byte) 0); + } + + protected byte[] getByte(int size, byte defaultValue) { + byte[] v = new byte[size]; + for (int i = 0; i < size; i++) { + v[i] = defaultValue; + } + return v; + } + + /** + * 使用指定的用户,生产一个对象 + * + * @param oper + * @param bucketName + * @param objectKey + * @return + */ + protected PutObjectResult generateTestObject(UserInfo oper, String bucketName, String objectKey, + boolean isRequesterPays, int size) { + PutObjectRequest putRequest = generatePutObjectRequest(bucketName, objectKey, size); + putRequest.setRequesterPays(isRequesterPays); + return oper.getObsClient().putObject(putRequest); + } + + /** + * 生成一个上传对象请求 + * + * @param bucketName + * @param objectKey + * @return + * @throws UnsupportedEncodingException + */ + protected PutObjectRequest generatePutObjectRequest(String bucketName, String objectKey, int size) { + PutObjectRequest request = new PutObjectRequest(); + request.setBucketName(bucketName); + + request.setInput(new ByteArrayInputStream(getByte(size))); + + request.setObjectKey(objectKey); + + return request; + } + + /** + * 生成复制对象请求 + * + * @param bucketName + * @param objectKey + * @return + * @throws UnsupportedEncodingException + */ + protected CopyObjectRequest generateCopyObjectRequest(String bucketName, String objectKey) { + CopyObjectRequest request = new CopyObjectRequest(bucketName, objectKey, bucketName, objectKey + "_copy_dest"); + request.setBucketName(bucketName); + + request.setReplaceMetadata(true); + + return request; + } + + /** + * 生成初始化分段上次请求 + * + * @param bucketName + * @param objectKey + * @return + * @throws UnsupportedEncodingException + */ + protected InitiateMultipartUploadRequest generateInitiateMultipartUploadRequest(String bucketName, + String objectKey) { + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectKey); + return request; + } + + /** + * 生成列举分段请求 + * + * @param bucketName + * @param objectKey + * @return + * @throws UnsupportedEncodingException + */ + protected ListPartsRequest generateListPartsRequest(String bucketName, String objectKey, String uploadId) { + ListPartsRequest request = new ListPartsRequest(bucketName, objectKey, uploadId); + return request; + } + + /** + * 生成上传分段的请求 + * + * @param bucketName + * @param objectKey + * @param uploadId + * @param partNumber + * @return + * @throws IOException + */ + protected UploadPartRequest generateUploadPartRequest(String bucketName, String objectKey, String uploadId, + int partNumber) throws IOException { + UploadPartRequest request = new UploadPartRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setUploadId(uploadId); + request.setPartNumber(partNumber); + + request.setInput(new ByteArrayInputStream(getByte(6 * 1024 * 1024))); + + return request; + } + + /** + * 生成上传分段的请求 + * + * @param bucketName + * @param objectKey + * @param uploadId + * @param partNumber + * @return + * @throws IOException + */ + protected UploadPartRequest generateUploadPartRequest(String bucketName, String objectKey, String uploadId, + int partNumber, byte defaultValue) throws IOException { + UploadPartRequest request = new UploadPartRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setUploadId(uploadId); + request.setPartNumber(partNumber); + + request.setInput(new ByteArrayInputStream(getByte(6 * 1024 * 1024, defaultValue))); + + return request; + } + + private File createSampleFile() throws IOException { + File file = File.createTempFile("obs-java-sdk-", ".txt"); + file.deleteOnExit(); + + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + for (int i = 0; i < 1000000; i++) { + writer.write(UUID.randomUUID() + "\n"); + writer.write(UUID.randomUUID() + "\n"); + } + writer.flush(); + writer.close(); + + return file; + } + + /** + * 生成取消分段请求 + * + * @param bucketName + * @param objectKey + * @return + * @throws UnsupportedEncodingException + */ + protected AbortMultipartUploadRequest generateAbortMultipartUploadRequest(String bucketName, String objectKey, + String uploadId) { + AbortMultipartUploadRequest request = new AbortMultipartUploadRequest(bucketName, objectKey, uploadId); + return request; + } + + /** + * 生成合并分段请求 + * + * @param bucketName + * @param objectKey + * @return + * @throws UnsupportedEncodingException + */ + protected CompleteMultipartUploadRequest generateCompleteMultipartUploadRequest(ListPartsResult listResult) { + List partEtags = new ArrayList(); + for (Multipart m : listResult.getMultipartList()) { + PartEtag part1 = new PartEtag(); + part1.setPartNumber(m.getPartNumber()); + part1.setEtag(m.getEtag()); + partEtags.add(part1); + } + + CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(listResult.getBucket(), + listResult.getKey(), listResult.getUploadId(), partEtags); + return request; + } + + /** + * 生成列举分段任务请求 + * + * @param bucketName + * @param objectKey + * @return + * @throws UnsupportedEncodingException + */ + protected ListMultipartUploadsRequest generateListMultipartUploadsRequest(String bucketName) { + return new ListMultipartUploadsRequest(bucketName); + } +} diff --git a/app/src/test/java/com/obs/test/objects/CopyObjectTest.java b/app/src/test/java/com/obs/test/objects/CopyObjectTest.java new file mode 100644 index 0000000..a251373 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/CopyObjectTest.java @@ -0,0 +1,118 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.CopyObjectRequest; +import com.obs.services.model.CopyObjectResult; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.PutObjectResult; +import com.obs.services.model.SetObjectMetadataRequest; +import com.obs.test.tools.PrepareTestBucket; +import com.obs.test.TestTools; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class CopyObjectTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + @Test + public void test_copyObject_with_chinese_metadata_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_copyObject_with_chinese_metadata_001"; + String copyObjectKey = "test_copyObject_with_chinese_metadata_001_copy"; + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + assertEquals(200, putResult.getStatusCode()); + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("测试中文"); + metadata.addUserMetadata("test-chinese", "测试中文"); + CopyObjectRequest request = new CopyObjectRequest(); + request.setBucketName(bucketName); + request.setObjectKey(copyObjectKey); + request.setSourceBucketName(bucketName); + request.setSourceObjectKey(objectKey); + request.setNewObjectMetadata(metadata); + request.setReplaceMetadata(true); + CopyObjectResult copyResult = obsClient.copyObject(request); + assertEquals(200, copyResult.getStatusCode()); + + GetObjectMetadataRequest getRequest = new GetObjectMetadataRequest(); + getRequest.setIsEncodeHeaders(false); + getRequest.setObjectKey(copyObjectKey); + getRequest.setBucketName(bucketName); + ObjectMetadata getResult = obsClient.getObjectMetadata(getRequest); + + assertEquals(200, getResult.getStatusCode()); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", getResult.getUserMetadata("test-chinese")); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", getResult.getContentDisposition()); + } + + @Test + public void test_copyObject_with_chinese_metadata_002() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_copyObject_with_chinese_metadata_002"; + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + assertEquals(200, putResult.getStatusCode()); + + SetObjectMetadataRequest request = new SetObjectMetadataRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.addUserMetadata("test-chinese", "【】,"); + request.setContentDisposition("【】,"); + try { + obsClient.setObjectMetadata(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } + + @Test + public void test_copyObject_with_chinese_metadata_003() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_copyObject_with_chinese_metadata_003"; + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + assertEquals(200, putResult.getStatusCode()); + + SetObjectMetadataRequest request = new SetObjectMetadataRequest(); + request.setObjectKey(objectKey); + request.setIsEncodeHeaders(false); + request.setBucketName(bucketName); + request.addUserMetadata("test-chinese", "测试中文"); + request.setContentDisposition("测试中文"); + try { + obsClient.setObjectMetadata(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } + +} diff --git a/app/src/test/java/com/obs/test/objects/DownloadFileTest.java b/app/src/test/java/com/obs/test/objects/DownloadFileTest.java new file mode 100644 index 0000000..5dd7946 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/DownloadFileTest.java @@ -0,0 +1,84 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.exception.ObsException; +import com.obs.services.model.DownloadFileRequest; +import com.obs.services.model.DownloadFileResult; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.PutObjectResult; +import com.obs.test.tools.PrepareTestBucket; +import com.obs.test.TestTools; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class DownloadFileTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + + @Test + public void test_downloadFile_with_url_encode_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_downloadFile_with_url_encode_001"; + ObjectMetadata metadata = new ObjectMetadata(); + metadata.addUserMetadata("test-url-encode", "%^&"); + metadata.setContentDisposition("%^&"); + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8)), metadata); + + assertEquals(200, putResult.getStatusCode()); + + DownloadFileRequest request = new DownloadFileRequest(bucketName, objectKey); + request.setDownloadFile("test_downloadFile_with_url_encode_001"); + try { + obsClient.downloadFile(request); + } catch (ObsException e) { + assertTrue(e.getMessage().contains("URLDecoder: Illegal hex characters in escape (%) pattern")); + } + request.setIsEncodeHeaders(false); + DownloadFileResult downloadResult = obsClient.downloadFile(request); + + assertEquals("%^&", downloadResult.getObjectMetadata().getUserMetadata("test-url-encode")); + assertEquals("%^&", downloadResult.getObjectMetadata().getResponseHeaders().get("content-disposition")); + } + + @Test + public void test_downloadFile_with_url_encode_002() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_downloadFile_with_url_encode_002"; + ObjectMetadata metadata = new ObjectMetadata(); + metadata.addUserMetadata("test-url-encode", "%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87"); + metadata.setContentDisposition("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87"); + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8)), metadata); + + assertEquals(200, putResult.getStatusCode()); + + DownloadFileRequest request = new DownloadFileRequest(bucketName, objectKey); + request.setDownloadFile("test_downloadFile_with_url_encode_002"); + DownloadFileResult downloadResult = obsClient.downloadFile(request); + + assertEquals("测试中文", downloadResult.getObjectMetadata().getUserMetadata("test-url-encode")); + assertEquals("测试中文", + downloadResult.getObjectMetadata().getResponseHeaders().get("content-disposition")); + } +} diff --git a/app/src/test/java/com/obs/test/objects/GetObjcetMetadataTest.java b/app/src/test/java/com/obs/test/objects/GetObjcetMetadataTest.java new file mode 100644 index 0000000..8bdbb81 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/GetObjcetMetadataTest.java @@ -0,0 +1,77 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.PutObjectResult; +import com.obs.test.tools.PrepareTestBucket; +import com.obs.test.TestTools; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class GetObjcetMetadataTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + + @Test + public void test_getObjectMetadata_with_url_encode_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_getObjectMetadata_with_url_encode_001"; + ObjectMetadata metadata = new ObjectMetadata(); + metadata.addUserMetadata("test-url-encode", "%^&"); + metadata.setContentDisposition("%^&"); + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8)), metadata); + + assertEquals(200, putResult.getStatusCode()); + + GetObjectMetadataRequest request = new GetObjectMetadataRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + try { + obsClient.getObjectMetadata(request); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("URLDecoder: Illegal hex characters in escape (%) pattern")); + } + } + + @Test + public void test_getObjectMetadata_with_url_encode_002() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_getObjectMetadata_with_url_encode_002"; + ObjectMetadata metadata = new ObjectMetadata(); + metadata.addUserMetadata("test-url-encode", "%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87"); + metadata.setContentDisposition("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87"); + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8)), metadata); + + assertEquals(200, putResult.getStatusCode()); + + GetObjectMetadataRequest request = new GetObjectMetadataRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + ObjectMetadata getResult = obsClient.getObjectMetadata(request); + assertEquals("测试中文", getResult.getUserMetadata("test-url-encode")); + assertEquals("测试中文", getResult.getResponseHeaders().get("content-disposition")); + } +} diff --git a/app/src/test/java/com/obs/test/objects/GetObjectTest.java b/app/src/test/java/com/obs/test/objects/GetObjectTest.java new file mode 100644 index 0000000..6600b11 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/GetObjectTest.java @@ -0,0 +1,83 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.GetObjectRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.ObsObject; +import com.obs.services.model.PutObjectResult; +import com.obs.test.tools.PrepareTestBucket; +import com.obs.test.TestTools; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class GetObjectTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + + @Test + public void test_getObject_with_url_encode_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_getObject_with_url_encode_001"; + ObjectMetadata metadata = new ObjectMetadata(); + metadata.addUserMetadata("test-url-encode", "%^&"); + metadata.setContentDisposition("%^&"); + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8)), metadata); + + assertEquals(200, putResult.getStatusCode()); + + GetObjectRequest request = new GetObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + try { + obsClient.getObject(request); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("URLDecoder: Illegal hex characters in escape (%) pattern")); + } + request.setIsEncodeHeaders(false); + + ObsObject getResult = obsClient.getObject(request); + assertEquals("%^&", getResult.getMetadata().getUserMetadata("test-url-encode")); + assertEquals("%^&", getResult.getMetadata().getResponseHeaders().get("content-disposition")); + } + + @Test + public void test_getObject_with_url_encode_002() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_getObject_with_url_encode_002"; + ObjectMetadata metadata = new ObjectMetadata(); + metadata.addUserMetadata("test-url-encode", "%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87"); + metadata.setContentDisposition("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87"); + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8)),metadata); + + assertEquals(200, putResult.getStatusCode()); + + GetObjectRequest request = new GetObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + ObsObject getResult = obsClient.getObject(request); + assertEquals("测试中文", getResult.getMetadata().getUserMetadata("test-url-encode")); + assertEquals("测试中文", getResult.getMetadata().getResponseHeaders().get("content-disposition")); + } +} diff --git a/app/src/test/java/com/obs/test/objects/ListObjectTest.java b/app/src/test/java/com/obs/test/objects/ListObjectTest.java new file mode 100644 index 0000000..29f5afc --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/ListObjectTest.java @@ -0,0 +1,58 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.AbortMultipartUploadRequest; +import com.obs.services.model.BucketTypeEnum; +import com.obs.services.model.CreateBucketRequest; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.ListMultipartUploadsRequest; +import com.obs.services.model.ListVersionsRequest; +import com.obs.services.model.ListVersionsResult; +import com.obs.services.model.MultipartUpload; +import com.obs.services.model.MultipartUploadListing; +import com.obs.services.model.VersionOrDeleteMarker; +import com.obs.test.TestTools; +import com.obs.test.tools.PropertiesTools; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +import static org.junit.Assert.assertEquals; + +public class ListObjectTest { + private static final File configFile = new File("./app/src/test/resource/test_data.properties"); + private static final ArrayList createdBuckets = new ArrayList<>(); + + @BeforeClass + public static void prepareTestObjects() throws IOException { + String location = PropertiesTools.getInstance(configFile).getProperties("environment.location"); + String bucketName = PropertiesTools.getInstance(configFile).getProperties("bucketPrefix") + "list-objects"; + ObsClient obsClient = TestTools.getPipelineEnvironment(); + CreateBucketRequest request = new CreateBucketRequest(); + request.setBucketName(bucketName); + request.setBucketType(BucketTypeEnum.OBJECT); + request.setLocation(location); + HeaderResponse response = obsClient.createBucket(bucketName); + assertEquals(200, response.getStatusCode()); + createdBuckets.add(bucketName); + + } + + @AfterClass + public static void delete_buckets() { + ObsClient obsClient = TestTools.getPipelineEnvironment(); + TestTools.delete_buckets(obsClient, createdBuckets); + } + + @Test + public void test_list_objects() { + + } + + +} diff --git a/app/src/test/java/com/obs/test/objects/ListObjectsWithEncodingTypeTest.java b/app/src/test/java/com/obs/test/objects/ListObjectsWithEncodingTypeTest.java new file mode 100644 index 0000000..e2c7eb7 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/ListObjectsWithEncodingTypeTest.java @@ -0,0 +1,68 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObsObject; +import com.obs.test.TestTools; +import com.obs.test.tools.PropertiesTools; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class ListObjectsWithEncodingTypeTest extends ListObjectTest { + private static final File configFile = new File("./app/src/test/resource/test_data.properties"); + public static final ArrayList illegalXml = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127)); + + @BeforeClass + public static void prepareTestObjects() throws IOException { + ListObjectTest.prepareTestObjects(); + String bucketName = PropertiesTools.getInstance(configFile).getProperties("bucketPrefix") + "list-objects"; + ObsClient obsClient = TestTools.getPipelineEnvironment(); + for (int c : illegalXml) { + obsClient.putObject(bucketName, "illegal_" + (char) c, new ByteArrayInputStream("Hello OBS".getBytes())); + } + } + + @Test + public void test_list_objects_with_xml() throws IOException { + String bucketName = PropertiesTools.getInstance(configFile).getProperties("bucketPrefix") + "list-objects"; + ObsClient obsClient = TestTools.getPipelineEnvironment(); + ListObjectsRequest request = new ListObjectsRequest(); + request.setBucketName(bucketName); + request.setEncodingType("url"); + ObjectListing objects = obsClient.listObjects(request); + ArrayList objectNames = new ArrayList<>(); + for (ObsObject object : objects.getObjects()) { + objectNames.add(object.getObjectKey()); + objectNames.add(object.getObjectKey()); + } + assertTrue(objects.getObjects().size() > 32); + for (int c : illegalXml) { + assertTrue(objectNames.contains("illegal_" + (char) c)); + } + } + + @Test + public void test_list_objects_with_prefix() throws IOException { + String bucketName = PropertiesTools.getInstance(configFile).getProperties("bucketPrefix") + "list-objects"; + ObsClient obsClient = TestTools.getPipelineEnvironment(); + ListObjectsRequest request = new ListObjectsRequest(); + request.setBucketName(bucketName); + request.setEncodingType("url"); + request.setPrefix("illegal_\u0010"); + ObjectListing objects = obsClient.listObjects(request); + for (ObsObject object : objects.getObjects()) { + assertEquals("illegal_\u0010", object.getObjectKey()); + } + } +} diff --git a/app/src/test/java/com/obs/test/objects/ModifyObjectTest.java b/app/src/test/java/com/obs/test/objects/ModifyObjectTest.java new file mode 100644 index 0000000..db836f6 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/ModifyObjectTest.java @@ -0,0 +1,112 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.HttpMethodEnum; +import com.obs.services.model.ModifyObjectRequest; +import com.obs.services.model.ModifyObjectResult; +import com.obs.services.model.ObjectMetadata; +import com.obs.test.tools.PrepareTestBucket; +import com.obs.test.TestTools; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class ModifyObjectTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + @Test + public void test_modifyObject_with_chinese_metadata_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_ModifyObject_with_chinese_metadata_001"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("测试中文"); + metadata.addUserMetadata("test-chinese", "测试中文"); + + ModifyObjectRequest request = new ModifyObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + ModifyObjectResult ModifyResult = obsClient.modifyObject(request); + + assertEquals(200, ModifyResult.getStatusCode()); + + GetObjectMetadataRequest get_request = new GetObjectMetadataRequest(); + get_request.setIsEncodeHeaders(false); + get_request.setObjectKey(objectKey); + get_request.setBucketName(bucketName); + ObjectMetadata get_result = obsClient.getObjectMetadata(get_request); + + assertEquals(200, get_result.getStatusCode()); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", get_result.getUserMetadata("test-chinese")); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", get_result.getContentDisposition()); + } + + @Test + public void test_modifyObject_with_chinese_metadata_002() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_ModifyObject_with_chinese_metadata_002"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("【】,"); + metadata.addUserMetadata("test-chinese", "【】,"); + + ModifyObjectRequest request = new ModifyObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + try { + obsClient.modifyObject(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } + + @Test + public void test_modifyObject_with_chinese_metadata_003() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_ModifyObject_with_chinese_metadata_003"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("测试中文"); + metadata.addUserMetadata("test-chinese", "测试中文"); + + ModifyObjectRequest request = new ModifyObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setIsEncodeHeaders(false); + request.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + try { + obsClient.modifyObject(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } +} diff --git a/app/src/test/java/com/obs/test/objects/MultipartTest.java b/app/src/test/java/com/obs/test/objects/MultipartTest.java new file mode 100644 index 0000000..cef8927 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/MultipartTest.java @@ -0,0 +1,175 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.CompleteMultipartUploadResult; +import com.obs.services.model.CopyPartRequest; +import com.obs.services.model.CopyPartResult; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.PartEtag; +import com.obs.services.model.PutObjectRequest; +import com.obs.services.model.PutObjectResult; +import com.obs.services.model.UploadPartRequest; +import com.obs.services.model.UploadPartResult; +import com.obs.test.tools.PrepareTestBucket; +import com.obs.test.TestTools; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class MultipartTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + + @Test + public void test_initiate_multipart_upload_with_chinese_metadata_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_initiate_multipart_upload_with_chinese_metadata_001"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("测试中文"); + metadata.addUserMetadata("test-chinese", "测试中文"); + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setObjectKey(objectKey); + InitiateMultipartUploadResult initiateResult = obsClient.initiateMultipartUpload(request); + + assertEquals(200, initiateResult.getStatusCode()); + + UploadPartRequest uploadPartRequest = new UploadPartRequest(); + uploadPartRequest.setUploadId(initiateResult.getUploadId()); + uploadPartRequest.setPartNumber(1); + uploadPartRequest.setBucketName(bucketName); + uploadPartRequest.setObjectKey(objectKey); + uploadPartRequest.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + UploadPartResult uploadResult = obsClient.uploadPart(uploadPartRequest); + + assertEquals(200, uploadResult.getStatusCode()); + + CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(); + completeRequest.setObjectKey(objectKey); + completeRequest.setBucketName(bucketName); + completeRequest.setUploadId(initiateResult.getUploadId()); + PartEtag partEtag = new PartEtag(); + partEtag.setPartNumber(uploadResult.getPartNumber()); + partEtag.setEtag(uploadResult.getEtag()); + completeRequest.getPartEtag().add(partEtag); + CompleteMultipartUploadResult completeResult = obsClient.completeMultipartUpload(completeRequest); + assertEquals(200, completeResult.getStatusCode()); + + + GetObjectMetadataRequest get_request = new GetObjectMetadataRequest(); + get_request.setIsEncodeHeaders(false); + get_request.setObjectKey(objectKey); + get_request.setBucketName(bucketName); + ObjectMetadata getResult = obsClient.getObjectMetadata(get_request); + + assertEquals(200, getResult.getStatusCode()); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", getResult.getUserMetadata("test-chinese")); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", getResult.getContentDisposition()); + } + + @Test + public void test_initiate_multipart_upload_with_chinese_metadata_002() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_initiate_multipart_upload_with_chinese_metadata_002"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("【】,"); + metadata.addUserMetadata("test-chinese", "【】,"); + + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setObjectKey(objectKey); + + try { + obsClient.initiateMultipartUpload(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } + + @Test + public void test_initiate_multipart_upload_with_chinese_metadata_003() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_initiate_multipart_upload_with_chinese_metadata_003"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("测试中文"); + metadata.addUserMetadata("test-chinese", "测试中文"); + + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setObjectKey(objectKey); + request.setIsEncodeHeaders(false); + + try { + obsClient.initiateMultipartUpload(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } + + @Test + public void test_copy_part_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_copy_part_001"; + + PutObjectRequest putRequest = new PutObjectRequest(); + putRequest.setObjectKey(objectKey); + putRequest.setBucketName(bucketName); + putRequest.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + PutObjectResult putResult = obsClient.putObject(putRequest); + assertEquals(200, putResult.getStatusCode()); + + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + InitiateMultipartUploadResult initiateResult = obsClient.initiateMultipartUpload(request); + + assertEquals(200, initiateResult.getStatusCode()); + + CopyPartRequest copyRequest = new CopyPartRequest(); + copyRequest.setPartNumber(1); + copyRequest.setSourceObjectKey(objectKey); + copyRequest.setSourceBucketName(bucketName); + copyRequest.setBucketName(bucketName); + copyRequest.setUploadId(initiateResult.getUploadId()); + copyRequest.setDestinationBucketName(bucketName); + copyRequest.setDestinationObjectKey(objectKey); + CopyPartResult result = obsClient.copyPart(copyRequest); + + assertEquals(200, result.getStatusCode()); + + } +} diff --git a/app/src/test/java/com/obs/test/objects/PutObjectTest.java b/app/src/test/java/com/obs/test/objects/PutObjectTest.java new file mode 100644 index 0000000..3f3e44d --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/PutObjectTest.java @@ -0,0 +1,113 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.PutObjectRequest; +import com.obs.services.model.PutObjectResult; +import com.obs.test.tools.PrepareTestBucket; +import com.obs.test.TestTools; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class PutObjectTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + + @Test + public void test_putObject_with_chinese_metadata_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_putObject_with_chinese_metadata_001"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("测试中文"); + metadata.addUserMetadata("test-chinese", "测试中文"); + + PutObjectRequest request = new PutObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + PutObjectResult putResult = obsClient.putObject(request); + + assertEquals(200, putResult.getStatusCode()); + + GetObjectMetadataRequest getRequest = new GetObjectMetadataRequest(); + getRequest.setIsEncodeHeaders(false); + getRequest.setObjectKey(objectKey); + getRequest.setBucketName(bucketName); + ObjectMetadata get_result = obsClient.getObjectMetadata(getRequest); + + assertEquals(200, get_result.getStatusCode()); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", get_result.getUserMetadata("test-chinese")); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", get_result.getContentDisposition()); + } + + @Test + public void test_putObject_with_chinese_metadata_002() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_putObject_with_chinese_metadata_002"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("【】,"); + metadata.addUserMetadata("test-chinese", "【】,"); + + PutObjectRequest request = new PutObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + try { + obsClient.putObject(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } + + @Test + public void test_putObject_with_chinese_metadata_003() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_putObject_with_chinese_metadata_003"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("测试中文"); + metadata.addUserMetadata("test-chinese", "测试中文"); + + PutObjectRequest request = new PutObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.setMetadata(metadata); + request.setIsEncodeHeaders(false); + request.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + try { + obsClient.putObject(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } +} diff --git a/app/src/test/java/com/obs/test/objects/RestoreObjectTest.java b/app/src/test/java/com/obs/test/objects/RestoreObjectTest.java new file mode 100644 index 0000000..f769d17 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/RestoreObjectTest.java @@ -0,0 +1,61 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.BaseObjectRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.PutObjectRequest; +import com.obs.services.model.PutObjectResult; +import com.obs.services.model.RestoreObjectRequest; +import com.obs.services.model.RestoreTierEnum; +import com.obs.services.model.StorageClassEnum; +import com.obs.test.TestTools; +import com.obs.test.tools.PrepareTestBucket; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; + +public class RestoreObjectTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + @Test + public void test_restore_object_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_restore_object_001"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setObjectStorageClass(StorageClassEnum.COLD); + + PutObjectRequest putRequest = new PutObjectRequest(); + putRequest.setObjectKey(objectKey); + putRequest.setBucketName(bucketName); + putRequest.setMetadata(metadata); + putRequest.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + PutObjectResult putResult = obsClient.putObject(putRequest); + assertEquals(200, putResult.getStatusCode()); + + RestoreObjectRequest request = new RestoreObjectRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.setRestoreTier(RestoreTierEnum.EXPEDITED); + request.setDays(10); + RestoreObjectRequest.RestoreObjectStatus result = obsClient.restoreObject(request); + assertEquals(202, result.getStatusCode()); + } +} diff --git a/app/src/test/java/com/obs/test/objects/SetObjectMetadataTest.java b/app/src/test/java/com/obs/test/objects/SetObjectMetadataTest.java new file mode 100644 index 0000000..a90d30e --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/SetObjectMetadataTest.java @@ -0,0 +1,180 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.PutObjectResult; +import com.obs.services.model.SetObjectMetadataRequest; +import com.obs.test.tools.PrepareTestBucket; +import com.obs.test.TestTools; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class SetObjectMetadataTest { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + @Test + public void test_setObjectMetadata_with_chinese_metadata_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_setObjectMetadata_with_chinese_001"; + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + assertEquals(200, putResult.getStatusCode()); + + SetObjectMetadataRequest request = new SetObjectMetadataRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.addUserMetadata("test-chinese", "测试中文"); + request.setContentDisposition("测试中文"); + ObjectMetadata setResult = obsClient.setObjectMetadata(request); + + assertEquals(200, setResult.getStatusCode()); + + GetObjectMetadataRequest getRequest = new GetObjectMetadataRequest(); + getRequest.setIsEncodeHeaders(false); + getRequest.setObjectKey(objectKey); + getRequest.setBucketName(bucketName); + ObjectMetadata getResult = obsClient.getObjectMetadata(getRequest); + + assertEquals(200, getResult.getStatusCode()); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", getResult.getUserMetadata("test-chinese")); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", getResult.getContentDisposition()); + } + + @Test + public void test_setObjectMetadata_with_chinese_metadata_002() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_setObjectMetadata_with_chinese_002"; + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + assertEquals(200, putResult.getStatusCode()); + + SetObjectMetadataRequest request = new SetObjectMetadataRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.addUserMetadata("test-chinese", "【】,"); + request.setContentDisposition("【】,"); + try { + obsClient.setObjectMetadata(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } + + @Test + public void test_setObjectMetadata_with_chinese_metadata_003() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_setObjectMetadata_with_chinese_002"; + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + assertEquals(200, putResult.getStatusCode()); + + SetObjectMetadataRequest request = new SetObjectMetadataRequest(); + request.setObjectKey(objectKey); + request.setIsEncodeHeaders(false); + request.setBucketName(bucketName); + request.addUserMetadata("test-chinese", "测试中文"); + request.setContentDisposition("测试中文"); + try { + obsClient.setObjectMetadata(request); + fail("No exception thrown."); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } + + @Test + public void test_setObjectMetadata_with_url_encode_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_setObjectMetadata_with_url_encode_001"; + PutObjectResult put_result = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + assertEquals(200, put_result.getStatusCode()); + + SetObjectMetadataRequest request = new SetObjectMetadataRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.addUserMetadata("test-url-encode", "%^&"); + request.setContentDisposition("%^&"); + try { + obsClient.setObjectMetadata(request); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("URLDecoder: Illegal hex characters in escape (%) pattern")); + } + + GetObjectMetadataRequest getRequest = new GetObjectMetadataRequest(); + getRequest.setIsEncodeHeaders(false); + getRequest.setObjectKey(objectKey); + getRequest.setBucketName(bucketName); + ObjectMetadata getResult = obsClient.getObjectMetadata(getRequest); + + assertEquals(200, getResult.getStatusCode()); + assertEquals("%^&", getResult.getUserMetadata("test-url-encode")); + assertEquals("%^&", getResult.getContentDisposition()); + + request.setIsEncodeHeaders(false); + ObjectMetadata setResult = obsClient.setObjectMetadata(request); + + assertEquals(200, setResult.getStatusCode()); + assertEquals("%^&", setResult.getUserMetadata("test-url-encode")); + assertEquals("%^&", setResult.getResponseHeaders().get("content-disposition")); + } + + @Test + public void test_setObjectMetadata_with_url_encode_002() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_setObjectMetadata_with_url_encode_002"; + PutObjectResult putResult = obsClient.putObject(bucketName, objectKey, + new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + assertEquals(200, putResult.getStatusCode()); + + SetObjectMetadataRequest request = new SetObjectMetadataRequest(); + request.setObjectKey(objectKey); + request.setBucketName(bucketName); + request.addUserMetadata("test-url-encode", "%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87"); + request.setContentDisposition("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87"); + ObjectMetadata setResult = obsClient.setObjectMetadata(request); + + assertEquals(200, setResult.getStatusCode()); + assertEquals("测试中文", setResult.getUserMetadata("test-url-encode")); + assertEquals("测试中文", setResult.getResponseHeaders().get("content-disposition")); + + GetObjectMetadataRequest getRequest = new GetObjectMetadataRequest(); + getRequest.setIsEncodeHeaders(false); + getRequest.setObjectKey(objectKey); + getRequest.setBucketName(bucketName); + ObjectMetadata getResult = obsClient.getObjectMetadata(getRequest); + + assertEquals(200, getResult.getStatusCode()); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", getResult.getUserMetadata("test-url-encode")); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", getResult.getContentDisposition()); + } +} diff --git a/app/src/test/java/com/obs/test/objects/TruncateFileTest.java b/app/src/test/java/com/obs/test/objects/TruncateFileTest.java new file mode 100644 index 0000000..cc760d6 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/TruncateFileTest.java @@ -0,0 +1,57 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.PutObjectRequest; +import com.obs.services.model.PutObjectResult; +import com.obs.services.model.fs.TruncateFileRequest; +import com.obs.services.model.fs.TruncateFileResult; +import com.obs.test.TestTools; +import com.obs.test.tools.PrepareTestBucket; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; + +public class TruncateFileTest { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + @Test + public void truncate_file_test_001() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "truncate_file_test_001"; + + PutObjectRequest putRequest = new PutObjectRequest(); + putRequest.setObjectKey(objectKey); + putRequest.setBucketName(bucketName); + putRequest.setInput(new ByteArrayInputStream("testObject".getBytes(StandardCharsets.UTF_8))); + + PutObjectResult putResult = obsClient.putObject(putRequest); + assertEquals(200, putResult.getStatusCode()); + + TruncateFileRequest request = new TruncateFileRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + request.setNewLength(4); + + TruncateFileResult result = obsClient.truncateFile(request); + assertEquals(204, result.getStatusCode()); + + ObjectMetadata metadata = obsClient.getObjectMetadata(bucketName, objectKey); + assertEquals(new Long(4), metadata.getContentLength()); + } +} diff --git a/app/src/test/java/com/obs/test/objects/UploadFileTest.java b/app/src/test/java/com/obs/test/objects/UploadFileTest.java new file mode 100644 index 0000000..ac66f22 --- /dev/null +++ b/app/src/test/java/com/obs/test/objects/UploadFileTest.java @@ -0,0 +1,109 @@ +package com.obs.test.objects; + +import com.obs.services.ObsClient; +import com.obs.services.exception.ObsException; +import com.obs.services.model.CompleteMultipartUploadResult; +import com.obs.services.model.GetObjectMetadataRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.UploadFileRequest; +import com.obs.test.tools.PrepareTestBucket; +import com.obs.test.TestTools; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import java.io.IOException; +import java.util.Locale; + +import static com.obs.test.TestTools.genTestFile; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class UploadFileTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public PrepareTestBucket prepareTestBucket = new PrepareTestBucket(); + + @Rule + public TestName testName = new TestName(); + + @Test + public void test_uploadFile_with_chinese_metadata_001() throws IOException { + genTestFile("test_uploadFile_with_chinese_metadata_001", 1024 * 100); + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_uploadFile_with_chinese_metadata_001"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("测试中文"); + metadata.addUserMetadata("test-chinese", "测试中文"); + + UploadFileRequest request = new UploadFileRequest(bucketName, objectKey); + request.setObjectMetadata(metadata); + request.setUploadFile("test_uploadFile_with_chinese_metadata_001"); + CompleteMultipartUploadResult upload_result = obsClient.uploadFile(request); + + assertEquals(200, upload_result.getStatusCode()); + + GetObjectMetadataRequest get_request = new GetObjectMetadataRequest(); + get_request.setIsEncodeHeaders(false); + get_request.setObjectKey(objectKey); + get_request.setBucketName(bucketName); + ObjectMetadata get_result = obsClient.getObjectMetadata(get_request); + + assertEquals(200, get_result.getStatusCode()); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", get_result.getUserMetadata("test-chinese")); + assertEquals("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87", get_result.getContentDisposition()); + } + + @Test + public void test_uploadFile_with_chinese_metadata_002() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_uploadFile_with_chinese_metadata_002"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("【】,"); + metadata.addUserMetadata("test-chinese", "【】,"); + + UploadFileRequest request = new UploadFileRequest(bucketName, objectKey); + request.setObjectMetadata(metadata); + request.setUploadFile("test_uploadFile_with_chinese"); + + try { + obsClient.uploadFile(request); + fail("No exception thrown."); + } catch (ObsException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } + + @Test + public void test_uploadFile_with_chinese_metadata_003() { + String bucketName = testName.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + String objectKey = "test_uploadFile_with_chinese_metadata_003"; + + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentDisposition("测试中文"); + metadata.addUserMetadata("test-chinese", "测试中文"); + + UploadFileRequest request = new UploadFileRequest(bucketName, objectKey); + request.setObjectMetadata(metadata); + request.setUploadFile("test_uploadFile_with_chinese"); + request.setIsEncodeHeaders(false); + + try { + obsClient.uploadFile(request); + fail("No exception thrown."); + } catch (ObsException e) { + assertTrue(e.getMessage().contains("Unexpected char ")); + } + } + +} diff --git a/app/src/test/java/com/obs/test/tools/BucketTools.java b/app/src/test/java/com/obs/test/tools/BucketTools.java new file mode 100644 index 0000000..27f68a8 --- /dev/null +++ b/app/src/test/java/com/obs/test/tools/BucketTools.java @@ -0,0 +1,159 @@ +package com.obs.test.tools; + +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import com.obs.services.ObsClient; +import com.obs.services.exception.ObsException; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.BaseBucketRequest; +import com.obs.services.model.BucketTypeEnum; +import com.obs.services.model.CanonicalGrantee; +import com.obs.services.model.CreateBucketRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.ListBucketsRequest; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.Owner; +import com.obs.services.model.Permission; +import com.obs.services.model.RequestPaymentConfiguration; +import com.obs.services.model.RequestPaymentEnum; +import com.obs.services.model.SetBucketAclRequest; +import com.obs.services.model.SetBucketRequestPaymentRequest; + +public class BucketTools { + private static final Logger logger = LogManager.getLogger(BucketTools.class); + + /** + * 创建桶 + * @param obsClient + * @param bucketName + * @return + */ + public static ObsBucket createBucket(ObsClient obsClient, String bucketName, BucketTypeEnum bucketType) { + CreateBucketRequest request = new CreateBucketRequest(); + request.setBucketName("bucketname"); + request.setBucketType(bucketType); + // 创建桶 + return obsClient.createBucket(bucketName); + } + + /** + * 设置桶的ACL + * @param obsClient + * @param bucketName + * @param objectKey + * @param isRecover 是否覆盖 + * @param isRequesterPays 是否开启请求者付费头域 + */ + public static HeaderResponse setBucketAcl(ObsClient obsClient, String ownerId, String userId, String bucketName, + boolean isRecover, boolean isRequesterPays) { + + AccessControlList accessControlList = null; + if(isRecover) { + accessControlList = obsClient.getBucketAcl(bucketName); + } else { + accessControlList = new AccessControlList(); + Owner owner = new Owner(); + owner.setId(ownerId); + accessControlList.setOwner(owner); + } + + accessControlList.grantPermission(new CanonicalGrantee(userId), Permission.PERMISSION_FULL_CONTROL, true); + + SetBucketAclRequest request = new SetBucketAclRequest(bucketName, accessControlList); + request.setRequesterPays(isRequesterPays); + return obsClient.setBucketAcl(request); + } + + /** + * 设置桶策略 + * @param srcUser + * @param bucketName + * @param policy + */ + public static void setBucketPolicy (ObsClient obsClient, String bucketName, String policy) { + obsClient.setBucketPolicy(bucketName, policy); + } + + /** + * 删除桶策略 + * @param srcUser + * @param bucketName + * @param policy + */ + public static void deleteBucketPolicy (ObsClient obsClient, String bucketName) { + obsClient.deleteBucketPolicy(bucketName); + } + + /** + * 删除桶 + * + * @param forceDelete 是否强制删除,true:强制删除(如果非空,责先删除对象后再删除) + */ + public static void deleteBucket(ObsClient obsClient, String bucketName, boolean forceDelete) { + logger.warn("delete bucket : " + bucketName); + try { + if(forceDelete) { + clearBucket(obsClient, bucketName); + } + + obsClient.deleteBucket(bucketName); + } catch (ObsException e) { + if(e.getResponseCode() == 404) { + logger.warn("bucket : " + bucketName + " not exists."); + } else { + throw e; + } + } + } + + /** + * 清空桶,将桶内的对象、未合并的分段对象都删除掉 + * @param obsClient + * @param bucketName + */ + public static void clearBucket(ObsClient obsClient, String bucketName) { + // 列举所有对象,并删除 + ObjectTools.deleteAllObjects(obsClient, bucketName); + + // 取消所有分段任务 + MultipartTools.abortAllMultipartUpload(obsClient, bucketName); + + // 删除所有多版本对象 + ObjectTools.deleteAllVersions(obsClient, bucketName); + } + + /** + * 列举所有桶 + * + * @param obsClient + * @return + */ + public static List listAllBucket(ObsClient obsClient) { + ListBucketsRequest request = new ListBucketsRequest(); + request.setQueryLocation(true); + return obsClient.listBuckets(request); + } + + /** + * 设置请求者这付费状态 + * @param now + */ + public static HeaderResponse setBucketRequestPayment(ObsClient obsClient, String bucketName, RequestPaymentEnum now, boolean isRequesterPays) { + SetBucketRequestPaymentRequest request = new SetBucketRequestPaymentRequest(bucketName, now); + request.setRequesterPays(isRequesterPays); + return obsClient.setBucketRequestPayment(request); + } + + /** + * 设置请求者这付费状态 + * @param now + */ + public static RequestPaymentConfiguration getBucketRequestPayment(ObsClient obsClient, String bucketName, boolean isRequesterPays) { + BaseBucketRequest request = new BaseBucketRequest(bucketName); + request.setRequesterPays(isRequesterPays); + return obsClient.getBucketRequestPayment(request); + } +} diff --git a/app/src/test/java/com/obs/test/tools/FileTools.java b/app/src/test/java/com/obs/test/tools/FileTools.java new file mode 100644 index 0000000..f56608c --- /dev/null +++ b/app/src/test/java/com/obs/test/tools/FileTools.java @@ -0,0 +1,50 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test.tools; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; + +public class FileTools { + /** + * 生成一个临时文件 + * @param path + * @return + * @throws IOException + */ + public static File createALocalTempFile(String path, long size) throws IOException { + File tmpfile = new File(path); + if(null != tmpfile.getParentFile()) + { + tmpfile.getParentFile().mkdirs(); + } + tmpfile.deleteOnExit(); + if(null != tmpfile.getParentFile()) { + tmpfile.getParentFile().mkdirs(); + } + RandomAccessFile randomAccessFile = null; + try { + randomAccessFile = new RandomAccessFile(tmpfile, "rw"); + randomAccessFile.setLength(size); + } finally { + if(null != randomAccessFile) { + randomAccessFile.close(); + } + } + + return tmpfile; + } +} diff --git a/app/src/test/java/com/obs/test/tools/MultipartTools.java b/app/src/test/java/com/obs/test/tools/MultipartTools.java new file mode 100644 index 0000000..dffad8a --- /dev/null +++ b/app/src/test/java/com/obs/test/tools/MultipartTools.java @@ -0,0 +1,68 @@ +package com.obs.test.tools; + +import com.obs.services.ObsClient; +import com.obs.services.model.AbortMultipartUploadRequest; +import com.obs.services.model.ListMultipartUploadsRequest; +import com.obs.services.model.MultipartUpload; +import com.obs.services.model.MultipartUploadListing; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.List; + +public class MultipartTools { + private static final Logger logger = LogManager.getLogger(MultipartTools.class); + /** + * 列举所有分段对象 + * @param obsClient + * @param bucketName + * @return + */ + public static List listAllMultipartUpload(ObsClient obsClient, String bucketName) { + ListMultipartUploadsRequest request = new ListMultipartUploadsRequest(bucketName); + MultipartUploadListing result; + + List multipartUploads = new ArrayList<>(); + do{ + result = obsClient.listMultipartUploads(request); + for(MultipartUpload upload : result.getMultipartTaskList()){ + multipartUploads.add(upload); + } + request.setKeyMarker(result.getNextKeyMarker()); + request.setUploadIdMarker(result.getNextUploadIdMarker()); + }while(result.isTruncated()); + + return multipartUploads; + } + + /** + * 取消一个分段任务 + * @param obsClient + * @param bucketName + * @param objectKey + * @param uploadId + */ + public static void abortMultipartUpload(ObsClient obsClient, String bucketName, String objectKey, String uploadId) { + logger.warn("abort multipart upload : " + bucketName + " " + uploadId + " " + objectKey); + AbortMultipartUploadRequest request = new AbortMultipartUploadRequest(bucketName, objectKey, uploadId); + obsClient.abortMultipartUpload(request); + } + + /** + * 删除所有分段任务 + * @param obsClient + * @param bucketName + */ + public static void abortAllMultipartUpload(ObsClient obsClient, String bucketName) { + // 列举所有分段对象 + List multipartUploads = listAllMultipartUpload(obsClient, bucketName); + if(null == multipartUploads) { + return; + } + + for(MultipartUpload upload : multipartUploads) { + abortMultipartUpload(obsClient, bucketName, upload.getObjectKey(), upload.getUploadId()); + } + } +} diff --git a/app/src/test/java/com/obs/test/tools/ObjectTools.java b/app/src/test/java/com/obs/test/tools/ObjectTools.java new file mode 100644 index 0000000..d7f0c29 --- /dev/null +++ b/app/src/test/java/com/obs/test/tools/ObjectTools.java @@ -0,0 +1,130 @@ +package com.obs.test.tools; + +import com.obs.services.ObsClient; +import com.obs.services.model.*; + +import com.obs.test.UserInfo; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.List; + +public class ObjectTools { + private static final Logger logger = LogManager.getLogger(ObjectTools.class); + /** + * 列举桶内所有对象 + * @param obsClient + * @param bucketName + * @return + */ + public static List listAllObject(ObsClient obsClient, String bucketName) { + ListObjectsRequest request = new ListObjectsRequest(bucketName); + // 设置每页1000个对象 + request.setMaxKeys(1000); + + List objects = new ArrayList<>(); + ObjectListing result; + do { + result = obsClient.listObjects(request); + for (ObsObject obsObject : result.getObjects()) { + objects.add(obsObject); + } + request.setMarker(result.getNextMarker()); + } while (result.isTruncated()); + + return objects; + } + + /** + * 列举所有多版本对象 + * @param obsClient + * @param bucketName + * @return + */ + public static List listAllVersions(ObsClient obsClient, String bucketName) { + ListVersionsRequest request = new ListVersionsRequest (bucketName); + // 设置每页1000个对象 + request.setMaxKeys(1000); + + List objects = new ArrayList<>(); + ListVersionsResult result; + do { + result = obsClient.listVersions(bucketName); + for (VersionOrDeleteMarker versionOrDeleteMarker : result.getVersions()) { + objects.add(versionOrDeleteMarker); + } + request.setKeyMarker(result.getNextKeyMarker()); + request.setVersionIdMarker(result.getNextVersionIdMarker()); + } while (result.isTruncated()); + + return objects; + } + + /** + * 删除指定的对象 + * @param obsClient + * @param bucketName + * @param objectKey + */ + public static void deleteObject(ObsClient obsClient, String bucketName, String objectKey) { + logger.warn("delete object : " + bucketName + " " + objectKey); + obsClient.deleteObject(bucketName, objectKey); + } + + /** + * 删除指定版本的对象 + * @param obsClient + * @param bucketName + * @param objectKey + * @param versionid + */ + public static void deleteObject(ObsClient obsClient, String bucketName, String objectKey, String versionid) { + logger.warn("delete object : " + bucketName + " " + objectKey + " " + versionid); + obsClient.deleteObject(bucketName, objectKey, versionid); + } + + /** + * 删除桶内的所有对象 + * @param obsClient + * @param bucketName + */ + public static void deleteAllObjects(ObsClient obsClient, String bucketName) { + List obsObjects = ObjectTools.listAllObject(obsClient, bucketName); + if(null == obsObjects) { + return; + } + for(ObsObject obsObject : obsObjects) { + ObjectTools.deleteObject(obsClient, bucketName, obsObject.getObjectKey()); + } + } + + /** + * 删除桶内的所有历史版本对象 + * @param obsClient + * @param bucketName + */ + public static void deleteAllVersions(ObsClient obsClient, String bucketName) { + List versions = ObjectTools.listAllVersions(obsClient, bucketName); + if(null == versions) { + return; + } + for(VersionOrDeleteMarker version : versions) { + ObjectTools.deleteObject(obsClient, bucketName, version.getObjectKey(), version.getVersionId()); + } + } + + /** + * 设置对象的ACL + * @param obsClient + * @param bucketName + * @param objectKey + */ + public static void setObjectAcl(UserInfo srcUser, UserInfo destUser, String bucketName, String objectKey) { + AccessControlList accessControlList = srcUser.getObsClient().getObjectAcl(bucketName, objectKey); + accessControlList.grantPermission(new CanonicalGrantee(destUser.getDomainId()), Permission.PERMISSION_FULL_CONTROL, true); + + srcUser.getObsClient().setObjectAcl(bucketName, objectKey, accessControlList); + } +} diff --git a/app/src/test/java/com/obs/test/tools/PrepareTestBucket.java b/app/src/test/java/com/obs/test/tools/PrepareTestBucket.java new file mode 100644 index 0000000..04f8ef9 --- /dev/null +++ b/app/src/test/java/com/obs/test/tools/PrepareTestBucket.java @@ -0,0 +1,43 @@ +package com.obs.test.tools; + +import com.obs.services.ObsClient; +import com.obs.test.TestTools; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +import java.io.File; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; + +public class PrepareTestBucket implements TestRule { + File configFile = new File("./app/src/test/resource/test_data.properties"); + + /** + * 在用例开始前创建桶,并在用例执行完成后将桶删除 + * + * @param statement 用例 + * @param description 用例描述,可以获取用例名 + * @return + */ + @Override + public Statement apply(Statement statement, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + String location = PropertiesTools.getInstance(configFile).getProperties("environment.location"); + String bucketName = description.getMethodName().replace("_", "-").toLowerCase(Locale.ROOT); + boolean isPosix = Boolean.parseBoolean(PropertiesTools.getInstance(configFile).getProperties("isPosix")); + ObsClient obsClient = TestTools.getPipelineEnvironment(); + assertEquals(200, TestTools.createBucket(obsClient, bucketName, location, isPosix).getStatusCode()); + try { + statement.evaluate(); + } finally { + assertEquals(204, TestTools.delete_bucket(obsClient, bucketName).getStatusCode()); + } + + } + }; + } +} diff --git a/app/src/test/java/com/obs/test/tools/PropertiesTools.java b/app/src/test/java/com/obs/test/tools/PropertiesTools.java new file mode 100644 index 0000000..672486d --- /dev/null +++ b/app/src/test/java/com/obs/test/tools/PropertiesTools.java @@ -0,0 +1,59 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test.tools; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Properties; + +public class PropertiesTools { + private Properties testProperties = new Properties(); + + private static PropertiesTools tools = null; + + public String getProperties(String key) { + return this.testProperties.getProperty(key); + } + + public String getProperties(String key, String defaultValue) { + return this.testProperties.getProperty(key, defaultValue); + } + + public static PropertiesTools getInstance(final File file) throws FileNotFoundException, IOException, IllegalArgumentException { + if(null == tools) { + tools = new PropertiesTools(); + } + + if (!file.exists()) { + throw new FileNotFoundException("File doesn't exist: " + + file.getAbsolutePath()); + } + + FileInputStream stream = new FileInputStream(file); + try { + + tools.testProperties.load(stream); + } finally { + try { + stream.close(); + } catch (IOException e) { + } + } + + return tools; + } +} diff --git a/app/src/test/java/com/obs/test/tools/ToolsTest.java b/app/src/test/java/com/obs/test/tools/ToolsTest.java new file mode 100644 index 0000000..6eb3369 --- /dev/null +++ b/app/src/test/java/com/obs/test/tools/ToolsTest.java @@ -0,0 +1,33 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.obs.test.tools; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ToolsTest { + @Test + public void test_properties_tools() throws FileNotFoundException, IllegalArgumentException, IOException { + File file = new File("./app/src/test/resource/test_data_me.properties"); + + PropertiesTools tools = PropertiesTools.getInstance(file); + + assertEquals("http://8.45.130.11", tools.getProperties("environment.1.endpoint")); + } +} diff --git a/app/src/test/java/samples_android/BucketOperationsSample.java b/app/src/test/java/samples_android/BucketOperationsSample.java new file mode 100644 index 0000000..87317b1 --- /dev/null +++ b/app/src/test/java/samples_android/BucketOperationsSample.java @@ -0,0 +1,501 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.BucketCorsRule; +import com.obs.services.model.BucketEncryption; +import com.obs.services.model.BucketLoggingConfiguration; +import com.obs.services.model.BucketMetadataInfoRequest; +import com.obs.services.model.BucketMetadataInfoResult; +import com.obs.services.model.BucketQuota; +import com.obs.services.model.BucketStorageInfo; +import com.obs.services.model.BucketTagInfo; +import com.obs.services.model.BucketVersioningConfiguration; +import com.obs.services.model.LifecycleConfiguration; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.OptionsInfoRequest; +import com.obs.services.model.SSEAlgorithmEnum; +import com.obs.services.model.VersioningStatusEnum; +import com.obs.services.model.BucketCors; +import com.obs.services.model.WebsiteConfiguration; + +import java.io.IOException; +import java.text.ParseException; + +/** + * This sample demonstrates how to do bucket-related operations + * (such as do bucket ACL/CORS/Lifecycle/Logging/Website/Location/Tagging/OPTIONS) + * on OBS using the OBS SDK for Android. + */ +public class BucketOperationsSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb = new StringBuffer(); + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new BucketOperationsTask(); + task.execute(); + } + }); + } + + class BucketOperationsTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + /* + * Put bucket operation + */ + createBucket(); + + /* + * Get bucket location operation + */ + getBucketLocation(); + + /* + * Get bucket storageInfo operation + */ + getBucketStorageInfo(); + + /* + * Put/Get bucket quota operations + */ + doBucketQuotaOperation(); + + /* + * Put/Get bucket versioning operations + */ + doBucketVersioningOperation(); + + /* + * Put/Get bucket acl operations + */ + doBucketAclOperation(); + + /* + * Put/Get/Delete bucket cors operations + */ + doBucketCorsOperation(); + + /* + * Options bucket operation + */ + optionsBucket(); + + /* + * Get bucket metadata operation + */ + getBucketMetadata(); + + /* + * Put/Get/Delete bucket lifecycle operations + */ + doBucketLifecycleOperation(); + + /* + * Put/Get/Delete bucket logging operations + */ + doBucketLoggingOperation(); + + /* + * Put/Get/Delete bucket website operations + */ + doBucketWebsiteOperation(); + + /* + * Put/Get/Delete bucket tagging operations + */ + doBucketTaggingOperation(); + + /* + * Put/Get/Delete bucket encryption operations + */ + doBucketEncryptionOperation(); + + /* + * Delete bucket operation + */ + deleteBucket(); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + private void optionsBucket() + throws ObsException + { + sb.append("Options bucket\n\n"); + OptionsInfoRequest optionInfo = new OptionsInfoRequest(); + optionInfo.setOrigin("http://www.a.com"); + optionInfo.getRequestHeaders().add("Authorization"); + optionInfo.getRequestMethod().add("PUT"); + sb.append(obsClient.optionsBucket(bucketName, optionInfo)); + } + + private void getBucketMetadata() + throws ObsException + { + sb.append("Getting bucket metadata\n\n"); + BucketMetadataInfoRequest request = new BucketMetadataInfoRequest(bucketName); + request.setOrigin("http://www.a.com"); + request.getRequestHeaders().add("Authorization"); + BucketMetadataInfoResult result = obsClient.getBucketMetadata(request); + sb.append("StorageClass:" + result.getBucketStorageClass()); + sb.append("\tAllowedOrigins " + result.getAllowOrigin()); + sb.append("\tAllowedMethods " + result.getAllowMethods()); + sb.append("\tAllowedHeaders " + result.getAllowHeaders()); + sb.append("\tExposeHeaders " + result.getExposeHeaders()); + sb.append("\tMaxAgeSeconds " + result.getMaxAge() + "\n\n"); + + sb.append("Deleting bucket CORS\n\n"); + obsClient.deleteBucketCors(bucketName); + } + + private void doBucketTaggingOperation() + throws ObsException + { + sb.append("Setting bucket tagging\n\n"); + + BucketTagInfo tagInfo = new BucketTagInfo(); + BucketTagInfo.TagSet tagSet = new BucketTagInfo.TagSet(); + tagSet.addTag("key1", "value1"); + tagSet.addTag("key2", "value2"); + tagInfo.setTagSet(tagSet); + + obsClient.setBucketTagging(bucketName, tagInfo); + + sb.append("Getting bucket tagging\n\n"); + + sb.append(obsClient.getBucketTagging(bucketName)); + + sb.append("Deleting bucket tagging\n\n"); + obsClient.deleteBucketTagging(bucketName); + } + + private void doBucketEncryptionOperation() + throws ObsException + { + System.out.println("Setting bucket encryption\n"); + + BucketEncryption encryption = new BucketEncryption(SSEAlgorithmEnum.KMS); +// encryption.setKmsKeyId("your kmsKeyId"); + obsClient.setBucketEncryption(bucketName, encryption); + + System.out.println("Gettting bucket encryption\n"); + System.out.println(obsClient.getBucketEncryption(bucketName)); + + System.out.println("Deleting bucket encryption\n"); + obsClient.deleteBucketEncryption(bucketName); + + } + + private void doBucketVersioningOperation() + throws ObsException + { + sb.append("Getting bucket versioning config " + obsClient.getBucketVersioning(bucketName) + "\n\n"); + //Enable bucket versioning + obsClient.setBucketVersioning(bucketName, new BucketVersioningConfiguration(VersioningStatusEnum.ENABLED)); + sb.append("Current bucket versioning config " + obsClient.getBucketVersioning(bucketName) + "\n\n"); + //Suspend bucket versioning + BucketVersioningConfiguration suspended = new BucketVersioningConfiguration(VersioningStatusEnum.SUSPENDED); + obsClient.setBucketVersioning(bucketName, suspended); + sb.append("Current bucket versioning config " + obsClient.getBucketVersioning(bucketName) + "\n\n"); + } + + private void doBucketQuotaOperation() + throws ObsException + { + BucketQuota quota = new BucketQuota(); + //Set bucket quota to 1GB + quota.setBucketQuota(1024 * 1024 * 1024); + obsClient.setBucketQuota(bucketName, quota); + sb.append("Getting bucket quota " + obsClient.getBucketQuota(bucketName) + "\n\n"); + } + + private void getBucketStorageInfo() + throws ObsException + { + BucketStorageInfo storageInfo = obsClient.getBucketStorageInfo(bucketName); + sb.append("Getting bucket storageInfo " + storageInfo + "\n\n"); + } + + private void doBucketAclOperation() + throws ObsException + { + sb.append("Setting bucket ACL to public-read \n\n"); + + obsClient.setBucketAcl(bucketName, AccessControlList.REST_CANNED_PUBLIC_READ); + + sb.append("Getting bucket ACL " + obsClient.getBucketAcl(bucketName) + "\n\n"); + + sb.append("Setting bucket ACL to private \n\n"); + + obsClient.setBucketAcl(bucketName, AccessControlList.REST_CANNED_PRIVATE); + + sb.append("Getting bucket ACL " + obsClient.getBucketAcl(bucketName) + "\n\n"); + } + + private void doBucketCorsOperation() + throws ObsException + { + BucketCors bucketCors = new BucketCors(); + BucketCorsRule rule = new BucketCorsRule(); + rule.getAllowedHeader().add("Authorization"); + rule.getAllowedOrigin().add("http://www.a.com"); + rule.getAllowedOrigin().add("http://www.b.com"); + rule.getExposeHeader().add("x-obs-test1"); + rule.getExposeHeader().add("x-obs-test2"); + rule.setMaxAgeSecond(100); + rule.getAllowedMethod().add("HEAD"); + rule.getAllowedMethod().add("GET"); + rule.getAllowedMethod().add("PUT"); + bucketCors.getRules().add(rule); + + sb.append("Setting bucket CORS\n\n"); + obsClient.setBucketCors(bucketName, bucketCors); + + sb.append("Getting bucket CORS:" + obsClient.getBucketCors(bucketName) + "\n\n"); + + } + + private void doBucketLifecycleOperation() + throws ObsException + { + final String ruleId0 = "delete obsoleted files"; + final String matchPrefix0 = "obsoleted/"; + final String ruleId1 = "delete temporary files"; + final String matchPrefix1 = "temporary/"; + final String ruleId2 = "delete temp files"; + final String matchPrefix2 = "temp/"; + + LifecycleConfiguration lifecycleConfig = new LifecycleConfiguration(); + LifecycleConfiguration.Rule rule0 = lifecycleConfig.new Rule(); + rule0.setEnabled(true); + rule0.setId(ruleId0); + rule0.setPrefix(matchPrefix0); + LifecycleConfiguration.Expiration expiration0 = lifecycleConfig.new Expiration(); + expiration0.setDays(10); + + rule0.setExpiration(expiration0); + lifecycleConfig.addRule(rule0); + + LifecycleConfiguration.Rule rule1 = lifecycleConfig.new Rule(); + rule1.setEnabled(true); + rule1.setId(ruleId1); + rule1.setPrefix(matchPrefix1); + LifecycleConfiguration.Expiration expiration1 = lifecycleConfig.new Expiration(); + try + { + expiration1.setDate(ServiceUtils.parseIso8601Date("2018-12-31T00:00:00")); + } + catch (ParseException e) + { + e.printStackTrace(); + } + rule1.setExpiration(expiration1); + lifecycleConfig.addRule(rule1); + + LifecycleConfiguration.Rule rule2 = lifecycleConfig.new Rule(); + rule2.setEnabled(true); + rule2.setId(ruleId2); + rule2.setPrefix(matchPrefix2); + LifecycleConfiguration.NoncurrentVersionExpiration noncurrentVersionExpiration = + lifecycleConfig.new NoncurrentVersionExpiration(); + noncurrentVersionExpiration.setDays(10); + rule2.setNoncurrentVersionExpiration(noncurrentVersionExpiration); + lifecycleConfig.addRule(rule2); + + sb.append("Setting bucket lifecycle\n\n"); + obsClient.setBucketLifecycleConfiguration(bucketName, lifecycleConfig); + + sb.append("Getting bucket lifecycle:"); + LifecycleConfiguration result = obsClient.getBucketLifecycleConfiguration(bucketName); + LifecycleConfiguration.Rule r0 = result.getRules().get(0); + LifecycleConfiguration.Rule r1 = result.getRules().get(1); + LifecycleConfiguration.Rule r2 = result.getRules().get(2); + sb.append("\tRule0: Id=" + r0.getId() + ", Prefix=" + r0.getPrefix() + ", Status=" + r0.getEnabled() + ", ExpirationDays=" + + r0.getExpiration().getDays()); + sb.append("\tRule1: Id=" + r1.getId() + ", Prefix=" + r1.getPrefix() + ", Status=" + r1.getEnabled() + ", ExpirationTime=" + + r1.getExpiration().getDate()); + + sb.append("\tRule1: Id=" + r2.getId() + ", Prefix=" + r2.getPrefix() + ", Status=" + r2.getEnabled() + + ", NocurrentExpirationDays=" + r2.getNoncurrentVersionExpiration().getDays()); + sb.append("\n\n"); + + sb.append("Deleting bucket lifecycle\n\n"); + obsClient.deleteBucketLifecycleConfiguration(bucketName); + } + + private void doBucketLoggingOperation() + throws ObsException + { + final String targetBucket = bucketName; + final String targetPrefix = "log-"; + BucketLoggingConfiguration configuration = new BucketLoggingConfiguration(); + configuration.setTargetBucketName(targetBucket); + configuration.setLogfilePrefix(targetPrefix); + configuration.setAgency("test"); + + sb.append("Setting bucket logging\n\n"); + obsClient.setBucketLoggingConfiguration(bucketName, configuration, true); + + sb.append("Getting bucket logging:"); + BucketLoggingConfiguration result = obsClient.getBucketLoggingConfiguration(bucketName); + sb.append("\tTarget bucket=" + result.getTargetBucketName() + ", target prefix=" + result.getLogfilePrefix() + "\n\n"); + sb.append("\n\n"); + + sb.append("Deleting bucket logging\n\n"); + obsClient.setBucketLoggingConfiguration(targetBucket, new BucketLoggingConfiguration()); + } + + private void doBucketWebsiteOperation() + throws ObsException + { + WebsiteConfiguration websiteConfig = new WebsiteConfiguration(); + websiteConfig.setSuffix("index.html"); + websiteConfig.setKey("error.html"); + + sb.append("Setting bucket website\n\n"); + obsClient.setBucketWebsiteConfiguration(bucketName, websiteConfig); + + sb.append("Getting bucket website:"); + WebsiteConfiguration result = obsClient.getBucketWebsiteConfiguration(bucketName); + sb.append("\tIndex document=" + result.getKey() + ", error document=" + result.getSuffix() + "\n\n"); + + sb.append("Deleting bucket website\n\n"); + obsClient.deleteBucketWebsiteConfiguration(bucketName); + } + + private void deleteBucket() + throws ObsException + { + sb.append("Deleting bucket " + bucketName + "\n\n"); + obsClient.deleteBucket(bucketName); + } + + private void getBucketLocation() + throws ObsException + { + String location = obsClient.getBucketLocation(bucketName); + sb.append("Getting bucket location " + location + "\n\n"); + } + + private void createBucket() + throws ObsException + { + ObsBucket obsBucket = new ObsBucket(); + obsBucket.setBucketName(bucketName); + obsClient.createBucket(obsBucket); + sb.append("Create bucket:" + bucketName + " successfully!\n\n"); + } + + } + +} diff --git a/app/src/test/java/samples_android/ConcurrentCopyPartSample.java b/app/src/test/java/samples_android/ConcurrentCopyPartSample.java new file mode 100644 index 0000000..7dcb7d1 --- /dev/null +++ b/app/src/test/java/samples_android/ConcurrentCopyPartSample.java @@ -0,0 +1,364 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.CopyPartRequest; +import com.obs.services.model.CopyPartResult; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ListPartsRequest; +import com.obs.services.model.ListPartsResult; +import com.obs.services.model.Multipart; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.PartEtag; +import com.obs.services.model.PutObjectRequest; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +/** + * This sample demonstrates how to multipart upload an object concurrently by copy mode + * to OBS using the OBS SDK for Android. + */ +public class ConcurrentCopyPartSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String sourceBucketName = bucketName; + + private static String sourceObjectKey = "my-obs-object-key-demo"; + + private static String objectKey = sourceObjectKey + "-back"; + + private static ExecutorService executorService = Executors.newFixedThreadPool(5); + + private static List partETags = Collections.synchronizedList(new ArrayList()); + + private static ObsClient obsClient; + + private static StringBuffer sb = new StringBuffer(); + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new ConcurrentCopyPartTask(); + task.execute(); + } + }); + } + + private static class PartCopier implements Runnable + { + + private String sourceBucketName; + + private String sourceObjectKey; + + private long rangeStart; + + private long rangeEnd; + + private int partNumber; + + private String uploadId; + + public PartCopier(String sourceBucketName, String sourceObjectKey, long rangeStart, long rangeEnd, int partNumber, String uploadId) + { + this.sourceBucketName = sourceBucketName; + this.sourceObjectKey = sourceObjectKey; + this.rangeStart = rangeStart; + this.rangeEnd = rangeEnd; + this.partNumber = partNumber; + this.uploadId = uploadId; + } + + @Override + public void run() + { + try + { + CopyPartRequest request = new CopyPartRequest(); + request.setUploadId(this.uploadId); + request.setSourceBucketName(this.sourceBucketName); + request.setSourceObjectKey(this.sourceObjectKey); + request.setDestinationBucketName(bucketName); + request.setDestinationObjectKey(objectKey); + request.setByteRangeStart(this.rangeStart); + request.setByteRangeEnd(this.rangeEnd); + request.setPartNumber(this.partNumber); + CopyPartResult result = obsClient.copyPart(request); + sb.append("Part#" + this.partNumber + " done\n\n"); + partETags.add(new PartEtag(result.getEtag(), result.getPartNumber())); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + } + + class ConcurrentCopyPartTask extends AsyncTask + { + @Override + protected String doInBackground(Void... params) + { + try + { + /* + * Create bucket + */ + sb.append("Create a new bucket for demo\n\n"); + obsClient.createBucket(bucketName); + + /* + * Upload an object to your source bucket + */ + sb.append("Uploading a new object to OBS from a file\n\n"); + obsClient.putObject(new PutObjectRequest(sourceBucketName, sourceObjectKey, createSampleFile())); + + /* + * Claim a upload id firstly + */ + String uploadId = claimUploadId(); + sb.append("Claiming a new upload id " + uploadId + "\n\n"); + + long partSize = 5 * 1024 * 1024l;// 5MB + ObjectMetadata metadata = obsClient.getObjectMetadata(sourceBucketName, sourceObjectKey); + + long objectSize = metadata.getContentLength(); + + long partCount = objectSize % partSize == 0 ? objectSize / partSize : objectSize / partSize + 1; + + if (partCount > 10000) + { + throw new RuntimeException("Total parts count should not exceed 10000"); + } + else + { + sb.append("Total parts count " + partCount + "\n\n"); + } + + /* + * Upload multiparts by copy mode + */ + sb.append("Begin to upload multiparts to OBS by copy mode \n\n"); + for (int i = 0; i < partCount; i++) + { + + long rangeStart = i * partSize; + long rangeEnd = (i + 1 == partCount) ? objectSize - 1 : rangeStart + partSize - 1; + executorService.execute(new PartCopier(sourceBucketName, sourceObjectKey, rangeStart, rangeEnd, i + 1, uploadId)); + } + + /* + * Waiting for all parts finished + */ + executorService.shutdown(); + while (!executorService.isTerminated()) + { + try + { + executorService.awaitTermination(3, TimeUnit.SECONDS); + } + catch (InterruptedException e) + { + sb.append(e.getMessage()).append("\n\n"); + } + } + + /* + * Verify whether all parts are finished + */ + if (partETags.size() != partCount) + { + throw new IllegalStateException("Upload multiparts fail due to some parts are not finished yet"); + } + else + { + sb.append("Succeed to complete multiparts into an object named " + objectKey + "\n\n"); + } + + /* + * View all parts uploaded recently + */ + listAllParts(uploadId); + + /* + * Complete to upload multiparts + */ + completeMultipartUpload(uploadId); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + private String claimUploadId() + throws ObsException + { + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectKey); + InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request); + return result.getUploadId(); + } + + private File createSampleFile() + throws IOException + { + File file = File.createTempFile("obs-android-sdk-", ".txt"); + file.deleteOnExit(); + + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + for (int i = 0; i < 1000000; i++) + { + writer.write(UUID.randomUUID() + "\n\n"); + writer.write(UUID.randomUUID() + "\n\n"); + } + writer.flush(); + writer.close(); + + return file; + } + + private void completeMultipartUpload(String uploadId) + throws ObsException + { + // Make part numbers in ascending order + Collections.sort(partETags, new Comparator() + { + + @Override + public int compare(PartEtag o1, PartEtag o2) + { + return o1.getPartNumber() - o2.getPartNumber(); + } + }); + + sb.append("Completing to upload multiparts\n\n"); + CompleteMultipartUploadRequest completeMultipartUploadRequest = + new CompleteMultipartUploadRequest(bucketName, objectKey, uploadId, partETags); + obsClient.completeMultipartUpload(completeMultipartUploadRequest); + } + + private void listAllParts(String uploadId) + throws ObsException + { + sb.append("Listing all parts......"); + ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, objectKey, uploadId); + ListPartsResult partListing = obsClient.listParts(listPartsRequest); + + for (Multipart part : partListing.getMultipartList()) + { + sb.append("\tPart#" + part.getPartNumber() + ", ETag=" + part.getEtag()); + } + sb.append("\n"); + } + + } + +} diff --git a/app/src/test/java/samples_android/ConcurrentDownloadObjectSample.java b/app/src/test/java/samples_android/ConcurrentDownloadObjectSample.java new file mode 100644 index 0000000..cd440b3 --- /dev/null +++ b/app/src/test/java/samples_android/ConcurrentDownloadObjectSample.java @@ -0,0 +1,332 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.RandomAccessFile; +import java.io.Writer; +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.GetObjectRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.ObsObject; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +/** + * This sample demonstrates how to download an object concurrently + * from OBS using the OBS SDK for Android. + */ +public class ConcurrentDownloadObjectSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static String localFilePath = "/storage/sdcard/" + objectKey; + + private static ExecutorService executorService = Executors.newFixedThreadPool(5); + + private static AtomicInteger blocks = new AtomicInteger(0); + + private static ObsClient obsClient; + + private static StringBuffer sb = new StringBuffer(); + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new ConcurrentDownloadTask(); + task.execute(); + } + }); + } + + private static class BlockFetcher implements Runnable + { + private long rangeStart; + + private long rangeEnd; + + private int blockNumber; + + public BlockFetcher(long rangeStart, long rangeEnd, int blockNumber) + { + this.rangeStart = rangeStart; + this.rangeEnd = rangeEnd; + this.blockNumber = blockNumber; + } + + @Override + public void run() + { + RandomAccessFile raf = null; + try + { + raf = new RandomAccessFile(localFilePath, "rw"); + raf.seek(rangeStart); + + GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectKey); + getObjectRequest.setRangeStart(rangeStart); + getObjectRequest.setRangeEnd(rangeEnd); + ObsObject object = obsClient.getObject(getObjectRequest); + + InputStream content = object.getObjectContent(); + try + { + byte[] buf = new byte[8196]; + int bytes = 0; + while ((bytes = content.read(buf)) != -1) + { + raf.write(buf, 0, bytes); + } + blocks.incrementAndGet(); + System.out.println("Block : " + blockNumber + " Finish \n"); + } + catch (IOException e) + { + e.printStackTrace(); + } + finally + { + content.close(); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + finally + { + if (raf != null) + { + try + { + raf.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } + } + } + + class ConcurrentDownloadTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + + /* + * Create bucket + */ + sb.append("Create a new bucket to upload file\n\n"); + obsClient.createBucket(bucketName); + + /* + * Upload an object to your bucket + */ + sb.append("Uploading a new object to OBS from a file\n\n"); + obsClient.putObject(bucketName, objectKey, createSampleFile()); + + /* + * Get size of the object and pre-create a random access file to hold object data + */ + ObjectMetadata metadata = obsClient.getObjectMetadata(bucketName, objectKey); + long objectSize = metadata.getContentLength(); + + sb.append("Object size from metadata:" + objectSize + "\n\n"); + + File localFile = new File(localFilePath); + if (!localFile.getParentFile().exists()) + { + localFile.getParentFile().mkdirs(); + } + RandomAccessFile raf = new RandomAccessFile(localFile, "rw"); + raf.setLength(objectSize); + raf.close(); + + /* + * Calculate how many blocks to be divided + */ + final long blockSize = 5 * 1024 * 1024L; // 5MB + int blockCount = (int)(objectSize / blockSize); + if (objectSize % blockSize != 0) + { + blockCount++; + } + sb.append("Total blocks count " + blockCount + "\n\n"); + + /* + * Download the object concurrently + */ + sb.append("Start to download " + objectKey + "\n\n"); + for (int i = 0; i < blockCount;) + { + long rangeStart = i++ * blockSize; + long rangeEnd = (i == blockCount) ? objectSize - 1 : i * blockSize - 1; + executorService.execute(new BlockFetcher(rangeStart, rangeEnd, i)); + } + + /* + * Waiting for all blocks finished + */ + executorService.shutdown(); + while (!executorService.isTerminated()) + { + try + { + executorService.awaitTermination(5, TimeUnit.SECONDS); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + + /* + * Verify whether all blocks are finished + */ + if (blocks.intValue() != blockCount) + { + throw new IllegalStateException("Some blocks are not finished"); + } + else + { + sb.append("Succeed to download object " + objectKey); + } + + sb.append("Deleting object " + objectKey + "\n\n"); + obsClient.deleteObject(bucketName, objectKey, null); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + private File createSampleFile() + throws IOException + { + File file = File.createTempFile("obs-android-sdk-", ".txt"); + file.deleteOnExit(); + + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + for (int i = 0; i < 1000000; i++) + { + writer.write(UUID.randomUUID() + "\n\n"); + writer.write(UUID.randomUUID() + "\n\n"); + } + writer.flush(); + writer.close(); + + return file; + } + + } + +} diff --git a/app/src/test/java/samples_android/ConcurrentUploadPartSample.java b/app/src/test/java/samples_android/ConcurrentUploadPartSample.java new file mode 100644 index 0000000..76c06f2 --- /dev/null +++ b/app/src/test/java/samples_android/ConcurrentUploadPartSample.java @@ -0,0 +1,348 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ListPartsRequest; +import com.obs.services.model.ListPartsResult; +import com.obs.services.model.Multipart; +import com.obs.services.model.PartEtag; +import com.obs.services.model.UploadPartRequest; +import com.obs.services.model.UploadPartResult; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +/** + * This sample demonstrates how to multipart upload an object concurrently + * from OBS using the OBS SDK for Android. + */ +public class ConcurrentUploadPartSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static ExecutorService executorService = Executors.newFixedThreadPool(5); + + private static List partETags = Collections.synchronizedList(new ArrayList()); + + private static ObsClient obsClient; + + private static StringBuffer sb = new StringBuffer(); + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new ConcurrentUploadPartTask(); + task.execute(); + } + }); + } + + private static class PartUploader implements Runnable + { + + private File sampleFile; + + private long offset; + + private long partSize; + + private int partNumber; + + private String uploadId; + + public PartUploader(File sampleFile, long offset, long partSize, int partNumber, String uploadId) + { + this.sampleFile = sampleFile; + this.offset = offset; + this.partSize = partSize; + this.partNumber = partNumber; + this.uploadId = uploadId; + } + + @Override + public void run() + { + try + { + UploadPartRequest uploadPartRequest = new UploadPartRequest(); + uploadPartRequest.setBucketName(bucketName); + uploadPartRequest.setObjectKey(objectKey); + uploadPartRequest.setUploadId(this.uploadId); + uploadPartRequest.setFile(this.sampleFile); + uploadPartRequest.setPartSize(this.partSize); + uploadPartRequest.setOffset(this.offset); + uploadPartRequest.setPartNumber(this.partNumber); + + UploadPartResult uploadPartResult = obsClient.uploadPart(uploadPartRequest); + sb.append("Part#" + this.partNumber + " done\n\n"); + partETags.add(new PartEtag(uploadPartResult.getEtag(), uploadPartResult.getPartNumber())); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + } + + class ConcurrentUploadPartTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + /* + * Create bucket + */ + sb.append("Create a new bucket for demo\n\n"); + obsClient.createBucket(bucketName); + + /* + * Claim a upload id firstly + */ + String uploadId = claimUploadId(); + sb.append("Claiming a new upload id " + uploadId + "\n\n"); + + long partSize = 5 * 1024 * 1024l;// 5MB + File sampleFile = createSampleFile(); + long fileLength = sampleFile.length(); + + long partCount = fileLength % partSize == 0 ? fileLength / partSize : fileLength / partSize + 1; + + if (partCount > 10000) + { + throw new RuntimeException("Total parts count should not exceed 10000"); + } + else + { + sb.append("Total parts count " + partCount + "\n\n"); + } + + /* + * Upload multiparts to your bucket + */ + sb.append("Begin to upload multiparts to OBS from a file\n\n"); + for (int i = 0; i < partCount; i++) + { + long offset = i * partSize; + long currPartSize = (i + 1 == partCount) ? fileLength - offset : partSize; + executorService.execute(new PartUploader(sampleFile, offset, currPartSize, i + 1, uploadId)); + } + + /* + * Wait for all tasks to finish + */ + executorService.shutdown(); + while (!executorService.isTerminated()) + { + try + { + executorService.awaitTermination(5, TimeUnit.SECONDS); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + + /* + * Verify whether all tasks are finished + */ + if (partETags.size() != partCount) + { + throw new IllegalStateException("Some parts are not finished"); + } + else + { + sb.append("Succeed to complete multiparts into an object named " + objectKey + "\n\n"); + } + + /* + * View all parts uploaded recently + */ + listAllParts(uploadId); + + /* + * Complete to upload multiparts + */ + completeMultipartUpload(uploadId); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + private String claimUploadId() + throws ObsException + { + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectKey); + InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request); + return result.getUploadId(); + } + + private File createSampleFile() + throws IOException + { + File file = File.createTempFile("obs-android-sdk-", ".txt"); + file.deleteOnExit(); + + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + for (int i = 0; i < 1000000; i++) + { + writer.write(UUID.randomUUID() + "\n\n"); + writer.write(UUID.randomUUID() + "\n\n"); + } + writer.flush(); + writer.close(); + + return file; + } + + private void completeMultipartUpload(String uploadId) + throws ObsException + { + // Make part numbers in ascending order + Collections.sort(partETags, new Comparator() + { + + @Override + public int compare(PartEtag o1, PartEtag o2) + { + return o1.getPartNumber() - o2.getPartNumber(); + } + }); + + sb.append("Completing to upload multiparts\n\n"); + CompleteMultipartUploadRequest completeMultipartUploadRequest = + new CompleteMultipartUploadRequest(bucketName, objectKey, uploadId, partETags); + obsClient.completeMultipartUpload(completeMultipartUploadRequest); + } + + private void listAllParts(String uploadId) + throws ObsException + { + sb.append("Listing all parts......"); + ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, objectKey, uploadId); + ListPartsResult partListing = obsClient.listParts(listPartsRequest); + + for (Multipart part : partListing.getMultipartList()) + { + sb.append("\tPart#" + part.getPartNumber() + ", ETag=" + part.getEtag()); + } + sb.append("\n"); + } + + } + +} diff --git a/app/src/test/java/samples_android/CreateFolderSample.java b/app/src/test/java/samples_android/CreateFolderSample.java new file mode 100644 index 0000000..e0f044c --- /dev/null +++ b/app/src/test/java/samples_android/CreateFolderSample.java @@ -0,0 +1,183 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.ObsObject; +import com.obs.services.model.PutObjectRequest; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +/** + * This sample demonstrates how to create an empty folder under + * specified bucket to OBS using the OBS SDK for Android. + */ +public class CreateFolderSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb = new StringBuffer(); + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new CreateFolderTask(); + task.execute(); + } + }); + } + + class CreateFolderTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + /* + * Create bucket + */ + sb.append("Create a new bucket for demo\n\n"); + obsClient.createBucket(bucketName); + + /* + * Way 1: + * Create an empty folder without request body, note that the key must be + * suffixed with a slash + */ + final String keySuffixWithSlash1 = "MyObjectKey1/"; + obsClient.putObject(bucketName, keySuffixWithSlash1, new ByteArrayInputStream(new byte[0])); + sb.append("Creating an empty folder " + keySuffixWithSlash1 + "\n\n"); + + /* + * Verify whether the size of the empty folder is zero + */ + ObsObject object = obsClient.getObject(bucketName, keySuffixWithSlash1); + sb.append( + "Size of the empty folder '" + object.getObjectKey() + "' is " + object.getMetadata().getContentLength() + "\n\n"); + object.getObjectContent().close(); + + /* + * Way 2: + * Create an empty folder without request body, note that the key must be + * suffixed with a slash + */ + final String keySuffixWithSlash2 = "MyObjectKey2/"; + PutObjectRequest request = new PutObjectRequest(); + request.setBucketName(bucketName); + request.setObjectKey(keySuffixWithSlash2); + obsClient.putObject(request); + sb.append("Creating an empty folder " + keySuffixWithSlash2 + "\n\n"); + + /* + * Verify whether the size of the empty folder is zero + */ + object = obsClient.getObject(bucketName, keySuffixWithSlash2, null); + sb.append("Size of the empty folder '" + object.getObjectKey() + "' is " + object.getMetadata().getContentLength()); + object.getObjectContent().close(); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + } + +} diff --git a/app/src/test/java/samples_android/DeleteObjectsSample.java b/app/src/test/java/samples_android/DeleteObjectsSample.java new file mode 100644 index 0000000..d111435 --- /dev/null +++ b/app/src/test/java/samples_android/DeleteObjectsSample.java @@ -0,0 +1,204 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.DeleteObjectsResult; +import com.obs.services.model.KeyAndVersion; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +/** + * This sample demonstrates how to delete objects under specified bucket + * from OBS using the OBS SDK for Android. + */ +public class DeleteObjectsSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + sb = new StringBuffer(); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new DeleteObjectsTask(); + task.execute(); + } + }); + } + + class DeleteObjectsTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + /* + * Create bucket + */ + sb.append("Create a new bucket for demo\n\n"); + obsClient.createBucket(bucketName); + + /* + * Batch put objects into the bucket + */ + final String content = "Thank you for using Object Storage Service"; + final String keyPrefix = "MyObjectKey"; + List keys = new ArrayList(); + for (int i = 0; i < 100; i++) + { + String key = keyPrefix + i; + InputStream instream = new ByteArrayInputStream(content.getBytes()); + obsClient.putObject(bucketName, key, instream, null); + sb.append("Succeed to put object " + key); + keys.add(key); + } + sb.append("\n"); + + /* + * Delete all objects uploaded recently under the bucket + */ + sb.append("\n\nDeleting all objects\n\n"); + + DeleteObjectsRequest request = new DeleteObjectsRequest(); + request.setBucketName(bucketName); + request.setQuiet(false); + + KeyAndVersion[] kvs = new KeyAndVersion[keys.size()]; + int index = 0; + for (String key : keys) + { + kvs[index++] = new KeyAndVersion(key); + } + + request.setKeyAndVersions(kvs); + + sb.append("Delete results:"); + + DeleteObjectsResult deleteObjectsResult = obsClient.deleteObjects(request); + for (DeleteObjectsResult.DeleteObjectResult object : deleteObjectsResult.getDeletedObjectResults()) + { + sb.append("\t" + object); + } + + sb.append("\n\nError results:"); + + for (DeleteObjectsResult.ErrorResult error : deleteObjectsResult.getErrorResults()) + { + sb.append("\t" + error); + } + + sb.append("\n"); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + } + +} diff --git a/app/src/test/java/samples_android/DownloadSample.java b/app/src/test/java/samples_android/DownloadSample.java new file mode 100644 index 0000000..85d664e --- /dev/null +++ b/app/src/test/java/samples_android/DownloadSample.java @@ -0,0 +1,246 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.ByteBuffer; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.WritableByteChannel; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.ObsObject; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +/** + * This sample demonstrates how to download an object + * from OBS in different ways using the OBS SDK for Android. + */ +public class DownloadSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static String localFilePath = "/storage/sdcard/" + objectKey; + + private static ObsClient obsClient; + + private static StringBuffer sb; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + sb = new StringBuffer(); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new DownloadTask(); + task.execute(); + } + }); + } + + class DownloadTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + /* + * Create bucket + */ + sb.append("Create a new bucket for demo\n\n"); + obsClient.createBucket(bucketName); + + /* + * Upload an object to your bucket + */ + sb.append("Uploading a new object to OBS from a file\n\n"); + obsClient.putObject(bucketName, objectKey, createSampleFile()); + + sb.append("Downloading an object\n\n"); + + /* + * Download the object as an inputstream and display it directly + */ + simpleDownload(); + + File localFile = new File(localFilePath); + if (!localFile.getParentFile().exists()) + { + localFile.getParentFile().mkdirs(); + } + + sb.append("Downloading an object to file:" + localFilePath + "\n\n"); + /* + * Download the object to a file + */ + downloadToLocalFile(); + + sb.append("Deleting object " + objectKey + "\n\n"); + obsClient.deleteObject(bucketName, objectKey, null); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + private void downloadToLocalFile() + throws ObsException, IOException + { + ObsObject obsObject = obsClient.getObject(bucketName, objectKey, null); + ReadableByteChannel rchannel = Channels.newChannel(obsObject.getObjectContent()); + + ByteBuffer buffer = ByteBuffer.allocate(4096); + WritableByteChannel wchannel = Channels.newChannel(new FileOutputStream(new File(localFilePath))); + + while (rchannel.read(buffer) != -1) + { + buffer.flip(); + wchannel.write(buffer); + buffer.clear(); + } + rchannel.close(); + wchannel.close(); + } + + private void simpleDownload() + throws ObsException, IOException + { + ObsObject obsObject = obsClient.getObject(bucketName, objectKey, null); + displayTextInputStream(obsObject.getObjectContent()); + } + + private void displayTextInputStream(InputStream input) + throws IOException + { + BufferedReader reader = new BufferedReader(new InputStreamReader(input)); + while (true) + { + String line = reader.readLine(); + if (line == null) + break; + + sb.append("\t" + line); + } + sb.append("\n"); + + reader.close(); + } + + private File createSampleFile() + throws IOException + { + File file = File.createTempFile("obs-android-sdk-", ".txt"); + file.deleteOnExit(); + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + writer.write("abcdefghijklmnopqrstuvwxyz\n\n"); + writer.write("0123456789011234567890\n\n"); + writer.close(); + + return file; + } + + } + +} diff --git a/app/src/test/java/samples_android/ListObjectsInFolderSample.java b/app/src/test/java/samples_android/ListObjectsInFolderSample.java new file mode 100644 index 0000000..484ae2e --- /dev/null +++ b/app/src/test/java/samples_android/ListObjectsInFolderSample.java @@ -0,0 +1,286 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.DeleteObjectsResult; +import com.obs.services.model.DeleteObjectsResult.DeleteObjectResult; +import com.obs.services.model.KeyAndVersion; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObsObject; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +/** + * This sample demonstrates how to list objects under a specified folder of a bucket + * from OBS using the OBS SDK for Android. + */ +public class ListObjectsInFolderSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + sb = new StringBuffer(); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new ListObjectsInFolderTask(); + task.execute(); + } + }); + } + + class ListObjectsInFolderTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + + /* + * Create bucket + */ + obsClient.createBucket(bucketName); + + final String content = "Hello OBS"; + final String keyPrefix = "MyObjectKey"; + final String folderPrefix = "src"; + final String subFolderPrefix = "test"; + ObjectListing objectListing = null; + ListObjectsRequest listObjectsRequest = null; + List keys = new ArrayList(); + /* + * First prepare folders and sub folders + */ + + for (int i = 0; i < 5; i++) + { + String key = folderPrefix + i + "/"; + obsClient.putObject(bucketName, key, new ByteArrayInputStream(new byte[0]), null); + keys.add(key); + + for (int j = 0; j < 3; j++) + { + String subKey = key + subFolderPrefix + j + "/"; + obsClient.putObject(bucketName, subKey, new ByteArrayInputStream(new byte[0])); + keys.add(subKey); + } + } + + /* + * Insert 2 objects in each folder + */ + objectListing = obsClient.listObjects(bucketName); + for (ObsObject object : objectListing.getObjects()) + { + for (int i = 0; i < 2; i++) + { + String objectKey = object.getObjectKey() + keyPrefix + i; + obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + keys.add(objectKey); + } + } + /* + * Insert 2 objects in root path + */ + obsClient.putObject(bucketName, keyPrefix + 0, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + obsClient.putObject(bucketName, keyPrefix + 1, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + keys.add(keyPrefix + 0); + keys.add(keyPrefix + 1); + sb.append("Put " + keys.size() + " objects completed."); + sb.append("\n"); + + /* + * List all objects in folder src0/ + */ + sb.append("List all objects in folder src0/ \n\n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setPrefix("src0/"); + objectListing = obsClient.listObjects(listObjectsRequest); + for (ObsObject object : objectListing.getObjects()) + { + sb.append("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + sb.append("\n"); + + /* + * List all objects in sub folder src0/test0/ + */ + sb.append("List all objects in folder src0/test0/ \n\n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setPrefix("src0/test0/"); + objectListing = obsClient.listObjects(listObjectsRequest); + for (ObsObject object : objectListing.getObjects()) + { + sb.append("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + sb.append("\n"); + + /* + * List all objects group by folder + */ + sb.append("List all objects group by folder \n\n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setDelimiter("/"); + + objectListing = obsClient.listObjects(listObjectsRequest); + sb.append("Root path:"); + for (ObsObject object : objectListing.getObjects()) + { + sb.append("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + listObjectsByPrefix(listObjectsRequest, objectListing); + + sb.append("\n"); + + /* + * Delete all the objects created + */ + DeleteObjectsRequest request = new DeleteObjectsRequest(); + request.setBucketName(bucketName); + request.setQuiet(false); + KeyAndVersion[] kvs = new KeyAndVersion[keys.size()]; + int index = 0; + for (String key : keys) + { + kvs[index++] = new KeyAndVersion(key); + } + + request.setKeyAndVersions(kvs); + + sb.append("\n\nDelete results:"); + + DeleteObjectsResult deleteObjectsResult = obsClient.deleteObjects(request); + for (DeleteObjectResult object : deleteObjectsResult.getDeletedObjectResults()) + { + sb.append("\t" + object); + } + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + private void listObjectsByPrefix(ListObjectsRequest listObjectsRequest, ObjectListing objectListing) + throws ObsException + { + for (String prefix : objectListing.getCommonPrefixes()) + { + sb.append("Folder " + prefix + ":"); + listObjectsRequest.setPrefix(prefix); + objectListing = obsClient.listObjects(listObjectsRequest); + for (ObsObject object : objectListing.getObjects()) + { + sb.append("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + listObjectsByPrefix(listObjectsRequest, objectListing); + } + } + + } + +} diff --git a/app/src/test/java/samples_android/ListObjectsSample.java b/app/src/test/java/samples_android/ListObjectsSample.java new file mode 100644 index 0000000..e91ec89 --- /dev/null +++ b/app/src/test/java/samples_android/ListObjectsSample.java @@ -0,0 +1,272 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.DeleteObjectsResult; +import com.obs.services.model.DeleteObjectsResult.DeleteObjectResult; +import com.obs.services.model.KeyAndVersion; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObsObject; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +/** + * This sample demonstrates how to list objects under specified bucket + * from OBS using the OBS SDK for Android. + */ +public class ListObjectsSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + sb = new StringBuffer(); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new ListObjectsTask(); + task.execute(); + } + }); + } + + class ListObjectsTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + + /* + * Create bucket + */ + obsClient.createBucket(bucketName); + + final String content = "Hello OBS"; + final String keyPrefix = "MyObjectKey"; + ObjectListing objectListing = null; + ListObjectsRequest listObjectsRequest = null; + /* + * First insert 100 objects for demo + */ + List keys = new ArrayList(); + for (int i = 0; i < 100; i++) + { + String key = keyPrefix + i; + InputStream instream = new ByteArrayInputStream(content.getBytes("UTF-8")); + obsClient.putObject(bucketName, key, instream, null); + keys.add(key); + } + sb.append("Put " + keys.size() + " objects completed."); + + /* + * List objects using default parameters, will return up to 1000 objects + */ + sb.append("List objects using default parameters:\n\n"); + objectListing = obsClient.listObjects(bucketName); + for (ObsObject object : objectListing.getObjects()) + { + sb.append("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + sb.append("\n"); + + /* + * List the first 10 objects + */ + sb.append("List the first 10 objects :\n\n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setMaxKeys(10); + objectListing = obsClient.listObjects(listObjectsRequest); + for (ObsObject object : objectListing.getObjects()) + { + sb.append("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + sb.append("\n"); + + String theSecond10ObjectsMarker = objectListing.getNextMarker(); + + /* + * List the second 10 objects using marker + */ + sb.append("List the second 10 objects using marker:\n\n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setMaxKeys(10); + listObjectsRequest.setMarker(theSecond10ObjectsMarker); + objectListing = obsClient.listObjects(listObjectsRequest); + for (ObsObject object : objectListing.getObjects()) + { + sb.append("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + sb.append("\n"); + + /* + * List objects with prefix and max keys + */ + sb.append("List objects with prefix and max keys:\n\n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setPrefix(keyPrefix + "2"); + listObjectsRequest.setMaxKeys(5); + objectListing = obsClient.listObjects(listObjectsRequest); + for (ObsObject object : objectListing.getObjects()) + { + sb.append("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + sb.append("\n"); + + /* + * List all the objects in way of pagination + */ + sb.append("List all the objects in way of pagination:\n\n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setMaxKeys(10); + String nextMarker = null; + int index = 1; + do + { + listObjectsRequest.setMarker(nextMarker); + objectListing = obsClient.listObjects(listObjectsRequest); + sb.append("Page:" + index++ + "\n\n"); + for (ObsObject object : objectListing.getObjects()) + { + sb.append("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + nextMarker = objectListing.getNextMarker(); + } while (objectListing.isTruncated()); + sb.append("\n"); + + /* + * Delete all the objects created + */ + DeleteObjectsRequest request = new DeleteObjectsRequest(); + request.setBucketName(bucketName); + request.setQuiet(false); + KeyAndVersion[] kvs = new KeyAndVersion[keys.size()]; + index = 0; + for (String key : keys) + { + kvs[index++] = new KeyAndVersion(key); + } + + request.setKeyAndVersions(kvs); + + sb.append("Delete results:"); + + DeleteObjectsResult deleteObjectsResult = obsClient.deleteObjects(request); + for (DeleteObjectResult object : deleteObjectsResult.getDeletedObjectResults()) + { + sb.append("\t" + object); + } + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + } + +} diff --git a/app/src/test/java/samples_android/ListVersionsSample.java b/app/src/test/java/samples_android/ListVersionsSample.java new file mode 100644 index 0000000..e612d97 --- /dev/null +++ b/app/src/test/java/samples_android/ListVersionsSample.java @@ -0,0 +1,309 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.BucketVersioningConfiguration; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.DeleteObjectsResult; +import com.obs.services.model.DeleteObjectsResult.DeleteObjectResult; +import com.obs.services.model.KeyAndVersion; +import com.obs.services.model.ListVersionsRequest; +import com.obs.services.model.ListVersionsResult; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObsObject; +import com.obs.services.model.VersionOrDeleteMarker; +import com.obs.services.model.VersioningStatusEnum; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +/** + * This sample demonstrates how to list versions under specified bucket + * from OBS using the OBS SDK for Android. + */ +public class ListVersionsSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + sb = new StringBuffer(); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new ListVersionsTask(); + task.execute(); + } + }); + } + + class ListVersionsTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + + /* + * Create bucket + */ + obsClient.createBucket(bucketName); + + /* + * Enable bucket versioning + */ + obsClient.setBucketVersioning(bucketName, new BucketVersioningConfiguration(VersioningStatusEnum.ENABLED)); + + final String content = "Hello OBS"; + final String keyPrefix = "MyObjectKey"; + final String folderPrefix = "src"; + final String subFolderPrefix = "test"; + ObjectListing objectListing = null; + ListVersionsResult listVersionResult = null; + List keys = new ArrayList(); + VersionOrDeleteMarker[] versionsToDelete; + /* + * First prepare folders and sub folders + */ + + for (int i = 0; i < 5; i++) + { + String key = folderPrefix + i + "/"; + obsClient.putObject(bucketName, key, new ByteArrayInputStream(new byte[0]), null); + keys.add(key); + + for (int j = 0; j < 3; j++) + { + String subKey = key + subFolderPrefix + j + "/"; + obsClient.putObject(bucketName, subKey, new ByteArrayInputStream(new byte[0])); + keys.add(subKey); + } + } + + /* + * Insert 2 objects in each folder + */ + objectListing = obsClient.listObjects(bucketName); + for (ObsObject object : objectListing.getObjects()) + { + for (int i = 0; i < 2; i++) + { + String objectKey = object.getObjectKey() + keyPrefix + i; + obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + keys.add(objectKey); + } + } + /* + * Insert 2 objects in root path + */ + obsClient.putObject(bucketName, keyPrefix + 0, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + obsClient.putObject(bucketName, keyPrefix + 1, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + keys.add(keyPrefix + 0); + keys.add(keyPrefix + 1); + sb.append("Put " + keys.size() + " objects completed."); + sb.append("\n"); + + /* + * List versions using default parameters, will return up to 1000 objects + */ + sb.append("List versions using default parameters:\n\n"); + listVersionResult = obsClient.listVersions(bucketName); + versionsToDelete = listVersionResult.getVersions(); + for (VersionOrDeleteMarker versionOrDeleteMarker : listVersionResult.getVersions()) + { + sb.append("\t" + versionOrDeleteMarker.getKey() + " etag[" + versionOrDeleteMarker.getEtag() + "]" + " versionid[" + + versionOrDeleteMarker.getVersionId() + "]"); + } + sb.append("\n"); + + /* + * List all the versions in way of pagination + */ + sb.append("List all the versions in way of pagination:\n\n"); + ListVersionsRequest listRequest = new ListVersionsRequest(bucketName); + listRequest.setMaxKeys(10); + int index = 1; + do + { + listVersionResult = obsClient.listVersions(listRequest); + sb.append("Page:" + index++ + "\n\n"); + for (VersionOrDeleteMarker versionOrDeleteMarker : listVersionResult.getVersions()) + { + sb.append("\t" + versionOrDeleteMarker.getKey() + " etag[" + versionOrDeleteMarker.getEtag() + "]" + " versionid[" + + versionOrDeleteMarker.getVersionId() + "]"); + } + listRequest.setKeyMarker(listVersionResult.getNextKeyMarker()); + listRequest.setVersionIdMarker(listVersionResult.getNextVersionIdMarker()); + } while (listVersionResult.isTruncated()); + sb.append("\n"); + + /* + * List all versions group by folder + */ + sb.append("List all versions group by folder \n\n"); + listRequest = new ListVersionsRequest(bucketName); + listRequest.setMaxKeys(1000); + listRequest.setDelimiter("/"); + listVersionResult = obsClient.listVersions(listRequest); + sb.append("Root path:"); + for (VersionOrDeleteMarker versionOrDeleteMarker : listVersionResult.getVersions()) + { + sb.append("\t" + versionOrDeleteMarker.getKey() + " etag[" + versionOrDeleteMarker.getEtag() + "]" + " versionid[" + + versionOrDeleteMarker.getVersionId() + "]"); + } + listVersionsByPrefix(listVersionResult); + + sb.append("\n"); + + /* + * Delete all the objects created + */ + DeleteObjectsRequest request = new DeleteObjectsRequest(); + request.setBucketName(bucketName); + request.setQuiet(false); + index = 0; + KeyAndVersion[] kvs = new KeyAndVersion[versionsToDelete.length]; + for (VersionOrDeleteMarker versionOrDeleteMarker : versionsToDelete) + { + kvs[index++] = new KeyAndVersion(versionOrDeleteMarker.getKey(), versionOrDeleteMarker.getVersionId()); + } + + request.setKeyAndVersions(kvs); + + sb.append("\n\nDelete results:"); + + DeleteObjectsResult deleteObjectsResult = obsClient.deleteObjects(request); + for (DeleteObjectResult object : deleteObjectsResult.getDeletedObjectResults()) + { + sb.append("\t" + object); + } + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + private void listVersionsByPrefix(ListVersionsResult listVersionResult) + throws ObsException + { + for (String prefix : listVersionResult.getCommonPrefixes()) + { + sb.append("Folder " + prefix + ":"); + ListVersionsRequest request = new ListVersionsRequest(bucketName); + request.setPrefix(prefix); + request.setDelimiter("/"); + request.setMaxKeys(1000); + listVersionResult = obsClient.listVersions(request); + for (VersionOrDeleteMarker versionOrDeleteMarker : listVersionResult.getVersions()) + { + sb.append("\t" + versionOrDeleteMarker.getKey() + " etag[" + versionOrDeleteMarker.getEtag() + "]" + " versionid[" + + versionOrDeleteMarker.getVersionId() + "]"); + } + listVersionsByPrefix(listVersionResult); + } + } + + } + +} diff --git a/app/src/test/java/samples_android/ObjectMetaSample.java b/app/src/test/java/samples_android/ObjectMetaSample.java new file mode 100644 index 0000000..2facc78 --- /dev/null +++ b/app/src/test/java/samples_android/ObjectMetaSample.java @@ -0,0 +1,179 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.ObjectMetadata; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +/** + * This sample demonstrates how to set/get self-defined metadata for object + * on OBS using the OBS SDK for Android. + */ +public class ObjectMetaSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + sb = new StringBuffer(); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new ObjectMetaTask(); + task.execute(); + } + }); + } + + class ObjectMetaTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + + /* + * Create bucket + */ + sb.append("Create a new bucket for demo\n\n"); + obsClient.createBucket(bucketName); + + ObjectMetadata meta = new ObjectMetadata(); + /* + * Setting object mime type + */ + meta.setContentType("text/plain"); + /* + * Setting self-defined metadata + */ + meta.getMetadata().put("meta1", "value1"); + meta.addUserMetadata("meta2", "value2"); + String content = "Hello OBS"; + obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(content.getBytes("UTF-8")), meta); + sb.append("Create object " + objectKey + " successfully!\n\n"); + + /* + * Get object metadata + */ + ObjectMetadata result = obsClient.getObjectMetadata(bucketName, objectKey); + sb.append("Getting object metadata:"); + sb.append("\tContentType:" + result.getContentType()); + sb.append("\tmeta1:" + result.getUserMetadata("meta1")); + sb.append("\tmeta2:" + result.getUserMetadata("meta2")); + + /* + * Delete object + */ + obsClient.deleteObject(bucketName, objectKey); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + } + +} diff --git a/app/src/test/java/samples_android/ObjectOperationsSample.java b/app/src/test/java/samples_android/ObjectOperationsSample.java new file mode 100644 index 0000000..588d5a4 --- /dev/null +++ b/app/src/test/java/samples_android/ObjectOperationsSample.java @@ -0,0 +1,247 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.BucketCorsRule; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.OptionsInfoRequest; +import com.obs.services.model.BucketCors; +import com.obs.services.model.ObsObject; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +/** + * This sample demonstrates how to do object-related operations + * (such as create/delete/get/copy object, do object ACL/OPTIONS) + * on OBS using the OBS SDK for Android. + */ +public class ObjectOperationsSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + sb = new StringBuffer(); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new ObjectOperationsTask(); + task.execute(); + } + }); + } + + class ObjectOperationsTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + + /* + * Create bucket + */ + obsClient.createBucket(bucketName); + + /* + * Create object + */ + String content = "Hello OBS"; + obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + sb.append("Create object:" + objectKey + " successfully!\n\n"); + + /* + * Get object metadata + */ + sb.append("Getting object metadata"); + ObjectMetadata metadata = obsClient.getObjectMetadata(bucketName, objectKey, null); + sb.append("\t" + metadata); + + /* + * Get object + */ + sb.append("\n\nGetting object content"); + ObsObject obsObject = obsClient.getObject(bucketName, objectKey, null); + sb.append("\tobject content:" + ServiceUtils.toString(obsObject.getObjectContent())); + + /* + * Copy object + */ + String sourceBucketName = bucketName; + String destBucketName = bucketName; + String sourceObjectKey = objectKey; + String destObjectKey = objectKey + "-back"; + sb.append("Copying object\n\n"); + obsClient.copyObject(sourceBucketName, sourceObjectKey, destBucketName, destObjectKey); + + /* + * Options object + */ + doObjectOptions(); + + /* + * Put/Get object acl operations + */ + doObjectAclOperations(); + + /* + * Delete object + */ + sb.append("Deleting objects\n\n"); + obsClient.deleteObject(bucketName, objectKey, null); + obsClient.deleteObject(bucketName, destObjectKey, null); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + private void doObjectOptions() + throws ObsException + { + + BucketCors bucketCors = new BucketCors(); + BucketCorsRule rule = new BucketCorsRule(); + rule.getAllowedHeader().add("Authorization"); + rule.getAllowedOrigin().add("http://www.a.com"); + rule.getAllowedOrigin().add("http://www.b.com"); + rule.getExposeHeader().add("x-obs-test1"); + rule.getExposeHeader().add("x-obs-test2"); + rule.setMaxAgeSecond(100); + rule.getAllowedMethod().add("HEAD"); + rule.getAllowedMethod().add("GET"); + rule.getAllowedMethod().add("PUT"); + bucketCors.getRules().add(rule); + obsClient.setBucketCors(bucketName, bucketCors); + + sb.append("Options object\n\n"); + OptionsInfoRequest optionInfo = new OptionsInfoRequest(); + optionInfo.setOrigin("http://www.a.com"); + optionInfo.getRequestHeaders().add("Authorization"); + optionInfo.getRequestMethod().add("PUT"); + sb.append(obsClient.optionsObject(bucketName, objectKey, optionInfo)); + } + + private void doObjectAclOperations() + throws ObsException + { + sb.append("Setting object ACL to public-read \n\n"); + + obsClient.setObjectAcl(bucketName, objectKey, AccessControlList.REST_CANNED_PUBLIC_READ); + + sb.append("Getting object ACL " + obsClient.getObjectAcl(bucketName, objectKey) + "\n\n"); + + sb.append("Setting object ACL to private \n\n"); + + obsClient.setObjectAcl(bucketName, objectKey, AccessControlList.REST_CANNED_PRIVATE); + + sb.append("Getting object ACL " + obsClient.getObjectAcl(bucketName, objectKey) + "\n\n"); + } + + } + +} diff --git a/app/src/test/java/samples_android/PostObjectSample.java b/app/src/test/java/samples_android/PostObjectSample.java new file mode 100644 index 0000000..112a981 --- /dev/null +++ b/app/src/test/java/samples_android/PostObjectSample.java @@ -0,0 +1,367 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import java.io.BufferedReader; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.PostSignatureRequest; +import com.obs.services.model.PostSignatureResponse; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +/** + * This sample demonstrates how to post object under specified bucket from + * OBS using the OBS SDK for Android. + */ +public class PostObjectSample extends Activity +{ + + private static final String endPoint = "your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb; + + private static AuthTypeEnum authType = AuthTypeEnum.OBS; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + sb = new StringBuffer(); + + ObsConfiguration config = new ObsConfiguration(); + config.setEndPoint(endPoint); + config.setAuthType(authType); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new PostObjectTask(); + task.execute(); + } + }); + } + + class PostObjectTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + + /* + * Create bucket + */ + sb.append("Create a new bucket for demo\n\n"); + obsClient.createBucket(bucketName); + + /* + * Create sample file + */ + File sampleFile = createSampleFile(); + + /* + * Claim a post object request + */ + PostSignatureRequest request = new PostSignatureRequest(); + request.setExpires(3600); + + Map formParams = new HashMap(); + + String contentType = "text/plain"; + if(authType == AuthTypeEnum.OBS) { + formParams.put("x-obs-acl", "public-read"); + }else { + formParams.put("acl", "public-read"); + } + formParams.put("content-type", contentType); + + request.setFormParams(formParams); + + PostSignatureResponse response = obsClient.createPostSignature(request); + + formParams.put("key", objectKey); + formParams.put("policy", response.getPolicy()); + + if(authType == AuthTypeEnum.OBS) { + formParams.put("signature", response.getSignature()); + formParams.put("accesskeyid", ak); + }else { + formParams.put("signature", response.getSignature()); + formParams.put("AwsAccesskeyid", ak); + } + + String postUrl = bucketName + "." + endPoint; + sb.append("Creating object in browser-based way"); + sb.append("\tpost url:" + postUrl); + + String res = formUpload(postUrl, formParams, sampleFile, contentType); + sb.append("\tresponse:" + res); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + private String formUpload(String postUrl, Map formFields, File sampleFile, String contentType) + { + String res = ""; + HttpURLConnection conn = null; + String boundary = "9431149156168"; + BufferedReader reader = null; + DataInputStream in = null; + OutputStream out = null; + try + { + URL url = new URL(postUrl); + conn = (HttpURLConnection)url.openConnection(); + conn.setConnectTimeout(10000); + conn.setReadTimeout(30000); + conn.setDoOutput(true); + conn.setDoInput(true); + conn.setRequestMethod("POST"); + conn.setRequestProperty("User-Agent", "OBS/Test"); + conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); + out = new DataOutputStream(conn.getOutputStream()); + + // text + if (formFields != null) + { + StringBuffer strBuf = new StringBuffer(); + Iterator> iter = formFields.entrySet().iterator(); + int i = 0; + + while (iter.hasNext()) + { + Entry entry = iter.next(); + String inputName = entry.getKey(); + Object inputValue = entry.getValue(); + + if (inputValue == null) + { + continue; + } + + if (i == 0) + { + strBuf.append("--").append(boundary).append("\r\n"); + strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n"); + strBuf.append(inputValue); + } + else + { + strBuf.append("\r\n").append("--").append(boundary).append("\r\n"); + strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n"); + strBuf.append(inputValue); + } + + i++; + } + out.write(strBuf.toString().getBytes()); + } + + // file + String filename = sampleFile.getName(); + if (contentType == null || contentType.equals("")) + { + contentType = "application/octet-stream"; + } + + StringBuffer strBuf = new StringBuffer(); + strBuf.append("\r\n").append("--").append(boundary).append("\r\n"); + strBuf.append("Content-Disposition: form-data; name=\"file\"; " + "filename=\"" + filename + "\"\r\n"); + strBuf.append("Content-Type: " + contentType + "\r\n\r\n"); + + out.write(strBuf.toString().getBytes()); + + in = new DataInputStream(new FileInputStream(sampleFile)); + int bytes = 0; + byte[] bufferOut = new byte[1024]; + while ((bytes = in.read(bufferOut)) != -1) + { + out.write(bufferOut, 0, bytes); + } + + byte[] endData = ("\r\n--" + boundary + "--\r\n").getBytes(); + out.write(endData); + out.flush(); + + // Read data returned. + strBuf = new StringBuffer(); + reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); + String line = null; + while ((line = reader.readLine()) != null) + { + strBuf.append(line).append("\n"); + } + res = strBuf.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append("Send post request exception: " + e); + e.printStackTrace(); + } + finally + { + if (out != null) + { + try + { + out.close(); + } + catch (IOException e) + { + } + } + + if (in != null) + { + try + { + in.close(); + } + catch (IOException e) + { + } + } + if (reader != null) + { + try + { + reader.close(); + } + catch (IOException e) + { + } + } + if (conn != null) + { + conn.disconnect(); + conn = null; + } + } + + return res; + } + + private File createSampleFile() + throws IOException + { + File file = File.createTempFile("obs-android-sdk-", ".txt"); + file.deleteOnExit(); + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + writer.write("abcdefghijklmnopqrstuvwxyz\n"); + writer.write("0123456789011234567890\n"); + writer.close(); + + return file; + } + + } + +} diff --git a/app/src/test/java/samples_android/RestoreObjectSample.java b/app/src/test/java/samples_android/RestoreObjectSample.java new file mode 100644 index 0000000..c163657 --- /dev/null +++ b/app/src/test/java/samples_android/RestoreObjectSample.java @@ -0,0 +1,197 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.RestoreObjectRequest; +import com.obs.services.model.RestoreObjectRequest.RestoreObjectStatus; +import com.obs.services.model.RestoreTierEnum; +import com.obs.services.model.StorageClassEnum; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +/** + * This sample demonstrates how to download an cold object + * from OBS using the OBS SDK for Android. + */ +public class RestoreObjectSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-cold-bucket-demo"; + + private static String objectKey = "my-obs-cold-object-key-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + sb = new StringBuffer(); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new RestoreObjectTask(); + task.execute(); + } + }); + } + + class RestoreObjectTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + + /* + * Create a cold bucket + */ + sb.append("Create a new cold bucket for demo\n\n"); + ObsBucket bucket = new ObsBucket(); + bucket.setBucketName(bucketName); + bucket.setBucketStorageClass(StorageClassEnum.COLD); + obsClient.createBucket(bucket); + + /* + * Create a cold object + */ + sb.append("Create a new cold object for demo\n\n"); + String content = "Hello OBS"; + obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + + /* + * Restore the cold object + */ + sb.append("Restore the cold object"); + RestoreObjectRequest restoreObjectRequest = + new RestoreObjectRequest(bucketName, objectKey, null, 1, RestoreTierEnum.EXPEDITED); + sb.append("\t" + (obsClient.restoreObject(restoreObjectRequest) == RestoreObjectStatus.INPROGRESS)); + + /* + * Wait 6 minute to get the object + */ + Thread.sleep(60 * 6 * 1000); + + /* + * Get the cold object status + */ + sb.append("Get the cold object status"); + restoreObjectRequest = new RestoreObjectRequest(bucketName, objectKey, null, 1, RestoreTierEnum.EXPEDITED); + sb.append("\t" + (obsClient.restoreObject(restoreObjectRequest) == RestoreObjectStatus.AVALIABLE) + "\n\n"); + + /* + * Get the cold object + */ + sb.append("Get the cold object"); + sb.append("\tcontent:" + ServiceUtils.toString(obsClient.getObject(bucketName, objectKey, null).getObjectContent())); + + /* + * Delete the cold object + */ + obsClient.deleteObject(bucketName, objectKey, null); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + } + +} diff --git a/app/src/test/java/samples_android/SimpleMultipartUploadSample.java b/app/src/test/java/samples_android/SimpleMultipartUploadSample.java new file mode 100644 index 0000000..830578d --- /dev/null +++ b/app/src/test/java/samples_android/SimpleMultipartUploadSample.java @@ -0,0 +1,206 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.PartEtag; +import com.obs.services.model.UploadPartResult; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.UUID; + +/** + * This sample demonstrates how to upload multiparts to OBS + * using the OBS SDK for Android. + */ +public class SimpleMultipartUploadSample extends Activity +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb; + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + sb = new StringBuffer(); + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new SimpleMultipartUploadTask(); + task.execute(); + } + }); + } + + class SimpleMultipartUploadTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + + /* + * Create bucket + */ + obsClient.createBucket(bucketName); + + /* + * Step 1: initiate multipart upload + */ + sb.append("Step 1: initiate multipart upload \n\n"); + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request); + + /* + * Step 2: upload a part + */ + sb.append("Step 2: upload part \n\n"); + UploadPartResult uploadPartResult = + obsClient.uploadPart(bucketName, objectKey, result.getUploadId(), 1, new FileInputStream(createSampleFile())); + + /* + * Step 3: complete multipart upload + */ + sb.append("Step 3: complete multipart upload \n\n"); + CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(); + completeMultipartUploadRequest.setBucketName(bucketName); + completeMultipartUploadRequest.setObjectKey(objectKey); + completeMultipartUploadRequest.setUploadId(result.getUploadId()); + PartEtag partEtag = new PartEtag(); + partEtag.setPartNumber(uploadPartResult.getPartNumber()); + partEtag.seteTag(uploadPartResult.getEtag()); + completeMultipartUploadRequest.getPartEtag().add(partEtag); + obsClient.completeMultipartUpload(completeMultipartUploadRequest); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + private File createSampleFile() + throws IOException + { + File file = File.createTempFile("obs-android-sdk-", ".txt"); + file.deleteOnExit(); + + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + for (int i = 0; i < 1000000; i++) + { + writer.write(UUID.randomUUID() + "\n\n"); + writer.write(UUID.randomUUID() + "\n\n"); + } + writer.flush(); + writer.close(); + + return file; + } + } + +} diff --git a/app/src/test/java/samples_android/TemporarySignatureSample.java b/app/src/test/java/samples_android/TemporarySignatureSample.java new file mode 100644 index 0000000..c0ce08c --- /dev/null +++ b/app/src/test/java/samples_android/TemporarySignatureSample.java @@ -0,0 +1,389 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_android; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.ObsConvertor; +import com.obs.services.internal.V2Convertor; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.BucketCors; +import com.obs.services.model.BucketCorsRule; +import com.obs.services.model.HttpMethodEnum; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.TemporarySignatureRequest; +import com.obs.services.model.TemporarySignatureResponse; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.view.View; +import android.widget.TextView; +import okhttp3.Call; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +/** + * This sample demonstrates how to do common operations in temporary signature way + * on OBS using the OBS SDK for Android. + */ +public class TemporarySignatureSample extends Activity +{ + + private static final String endPoint = "http://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static ObsClient obsClient; + + private static StringBuffer sb; + + private static OkHttpClient httpClient; + + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + sb = new StringBuffer(); + + httpClient = new OkHttpClient.Builder().followRedirects(false) + .retryOnConnectionFailure(false) + .cache(null) + .build(); + + ObsConfiguration config = new ObsConfiguration(); + config.setEndPoint(endPoint); + config.setAuthType(AuthTypeEnum.OBS); + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + final TextView tv = (TextView)findViewById(R.id.tv); + tv.setText("Click to start test"); + + tv.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + tv.setClickable(false); + AsyncTask task = new TemporarySignatureTask(); + task.execute(); + } + }); + } + + class TemporarySignatureTask extends AsyncTask + { + + @Override + protected String doInBackground(Void... params) + { + try + { + + /* + * Create bucket + */ + doCreateBucket(); + + /* + * Set/Get/Delete bucket cors + */ + doBucketCorsOperations(); + + /* + * Create object + */ + doCreateObject(); + + /* + * Get object + */ + doGetObject(); + + /* + * Set/Get object acl + */ + doObjectAclOperations(); + + /* + * Delete object + */ + doDeleteObject(); + + /* + * Delete bucket + */ + doDeleteBucket(); + + return sb.toString(); + } + catch (ObsException e) + { + sb.append("\n\n"); + sb.append("Response Code:" + e.getResponseCode()) + .append("\n\n") + .append("Error Message:" + e.getErrorMessage()) + .append("\n\n") + .append("Error Code:" + e.getErrorCode()) + .append("\n\n") + .append("Request ID:" + e.getErrorRequestId()) + .append("\n\n") + .append("Host ID:" + e.getErrorHostId()); + return sb.toString(); + } + catch (Exception e) + { + sb.append("\n\n"); + sb.append(e.getMessage()); + return sb.toString(); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + @Override + protected void onPostExecute(String result) + { + TextView tv = (TextView)findViewById(R.id.tv); + tv.setText(result); + tv.setOnClickListener(null); + tv.setMovementMethod(ScrollingMovementMethod.getInstance()); + } + + private Request.Builder getBuilder(TemporarySignatureResponse res) { + Request.Builder builder = new Request.Builder(); + for (Map.Entry entry : res.getActualSignedRequestHeaders().entrySet()) { + builder.header(entry.getKey(), entry.getValue()); + } + return builder.url(res.getSignedUrl()); + } + + private void doDeleteObject() + throws IOException, ObsException + { + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.DELETE, 300); + req.setBucketName(bucketName); + req.setObjectKey(objectKey); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + sb.append("Deleting object using temporary signature url:"); + sb.append("\t" + res.getSignedUrl() + "\n"); + getResponse(getBuilder(res).delete().build()); + sb.append("\n\n"); + + } + + private void doObjectAclOperations() + throws ObsException, IOException + { + + Map headers = new HashMap(); + headers.put("x-obs-acl", "public-read"); + + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.PUT, 300); + req.setBucketName(bucketName); + req.setObjectKey(objectKey); + req.setHeaders(headers); + req.setSpecialParam(SpecialParamEnum.ACL); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + + sb.append("Setting object ACL to public-read using temporary signature url:"); + sb.append("\t" + res.getSignedUrl() + "\n"); + getResponse(getBuilder(res).put(RequestBody.create(null, "".getBytes("UTF-8"))).build()); + sb.append("\n\n"); + + TemporarySignatureRequest req2 = new TemporarySignatureRequest(HttpMethodEnum.GET, 300); + req2.setBucketName(bucketName); + req2.setObjectKey(objectKey); + req2.setSpecialParam(SpecialParamEnum.ACL); + TemporarySignatureResponse res2 = obsClient.createTemporarySignature(req2); + sb.append("Getting object ACL using temporary signature url:"); + sb.append("\t" + res2.getSignedUrl() + "\n"); + + getResponse(getBuilder(res2).get().build()); + sb.append("\n\n"); + } + + private void doGetObject() + throws ObsException, IOException + { + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.GET, 300); + req.setBucketName(bucketName); + req.setObjectKey(objectKey); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + sb.append("Getting object using temporary signature url:"); + sb.append("\t" + res.getSignedUrl() + "\n"); + getResponse(getBuilder(res).get().build()); + sb.append("\n\n"); + } + + private void doCreateObject() + throws ObsException, IOException + { + Map headers = new HashMap(); + headers.put("Content-Type", "text/plain"); + + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.PUT, 300); + req.setBucketName(bucketName); + req.setObjectKey(objectKey); + req.setHeaders(headers); + + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + + sb.append("Createing object using temporary signature url:"); + sb.append("\t" + res.getSignedUrl() + "\n"); + getResponse(getBuilder(res).put(RequestBody.create(MediaType.parse("text/plain"), "Hello OBS".getBytes("UTF-8"))).build()); + sb.append("\n\n"); + } + + private void doBucketCorsOperations() + throws ObsException, IOException, ServiceException + { + BucketCors bucketCors = new BucketCors(); + BucketCorsRule rule = new BucketCorsRule(); + rule.getAllowedHeader().add("Authorization"); + rule.getAllowedOrigin().add("http://www.a.com"); + rule.getAllowedOrigin().add("http://www.b.com"); + rule.getExposeHeader().add("x-obs-test1"); + rule.getExposeHeader().add("x-obs-test2"); + rule.setMaxAgeSecond(100); + rule.getAllowedMethod().add("HEAD"); + rule.getAllowedMethod().add("GET"); + rule.getAllowedMethod().add("PUT"); + bucketCors.getRules().add(rule); + + String requestXml = ObsConvertor.getInstance().transBucketCors(bucketCors); + String requestXmlMd5 = ServiceUtils.computeMD5(requestXml); + + Map headers = new HashMap(); + + headers.put("Content-Type", "application/xml"); + headers.put("Content-MD5", requestXmlMd5); + + + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.PUT, 300); + req.setBucketName(bucketName); + req.setHeaders(headers); + req.setSpecialParam(SpecialParamEnum.CORS); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + + sb.append("Setting bucket CORS using temporary signature url:"); + sb.append("\t" + res.getSignedUrl() + "\n"); + + getResponse(getBuilder(res).put(RequestBody.create(MediaType.parse("application/xml"), requestXml.getBytes("UTF-8"))).build()); + sb.append("\n\n"); + + TemporarySignatureRequest req2 = new TemporarySignatureRequest(HttpMethodEnum.GET, 300); + req2.setBucketName(bucketName); + req2.setSpecialParam(SpecialParamEnum.CORS); + TemporarySignatureResponse res2 = obsClient.createTemporarySignature(req2); + sb.append("Getting bucket CORS using temporary signature url:"); + sb.append("\t" + res2.getSignedUrl() + "\n"); + + getResponse(getBuilder(res2).get().build()); + sb.append("\n\n"); + + } + + private void doDeleteBucket() + throws ObsException, IOException + { + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.DELETE, 300); + req.setBucketName(bucketName); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + sb.append("Deleting bucket using temporary signature url:"); + sb.append("\t" + res.getSignedUrl() + "\ns"); + getResponse(getBuilder(res).delete().build()); + sb.append("\n\n"); + } + + private void doCreateBucket() + throws ObsException, IOException + { + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.PUT, 300); + req.setBucketName(bucketName); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + sb.append("Creating bucket using temporary signature url:"); + sb.append("\t" + res.getSignedUrl() + "\n"); + String location = "your-location"; + Request request = getBuilder(res).put(RequestBody.create(null, "" +location+ "".getBytes())).url(res.getSignedUrl()).build(); + getResponse(request); + sb.append("\n\n"); + } + + private void getResponse(Request request) + throws IOException + { + Call c = httpClient.newCall(request); + Response res = c.execute(); + sb.append("\tStatus:" + res.code()); + if (res.body() != null) + { + String content = res.body().string(); + if (content == null || content.trim().equals("")) + { + sb.append("\n"); + } + else + { + sb.append("\tContent:" + content + "\n\n"); + } + } + else + { + sb.append("\n"); + } + res.close(); + } + + } + +} diff --git a/app/src/test/java/samples_android/activity_main_for_demo.xml b/app/src/test/java/samples_android/activity_main_for_demo.xml new file mode 100644 index 0000000..1435501 --- /dev/null +++ b/app/src/test/java/samples_android/activity_main_for_demo.xml @@ -0,0 +1,20 @@ + + + + + + diff --git a/app/src/test/java/samples_java/BucketOperationsSample.java b/app/src/test/java/samples_java/BucketOperationsSample.java new file mode 100644 index 0000000..da07fc3 --- /dev/null +++ b/app/src/test/java/samples_java/BucketOperationsSample.java @@ -0,0 +1,438 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.IOException; +import java.text.ParseException; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.BucketCors; +import com.obs.services.model.BucketCorsRule; +import com.obs.services.model.BucketEncryption; +import com.obs.services.model.BucketLoggingConfiguration; +import com.obs.services.model.BucketMetadataInfoRequest; +import com.obs.services.model.BucketMetadataInfoResult; +import com.obs.services.model.BucketQuota; +import com.obs.services.model.BucketStorageInfo; +import com.obs.services.model.BucketTagInfo; +import com.obs.services.model.BucketVersioningConfiguration; +import com.obs.services.model.LifecycleConfiguration; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.OptionsInfoRequest; +import com.obs.services.model.SSEAlgorithmEnum; +import com.obs.services.model.VersioningStatusEnum; +import com.obs.services.model.WebsiteConfiguration; + +/** + * This sample demonstrates how to do bucket-related operations + * (such as do bucket ACL/CORS/Lifecycle/Logging/Website/Location/Tagging/OPTIONS) + * on OBS using the OBS SDK for Java. + */ +public class BucketOperationsSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + public static void main(String[] args) + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Put bucket operation + */ + createBucket(); + + /* + * Get bucket location operation + */ + getBucketLocation(); + + /* + * Get bucket storageInfo operation + */ + getBucketStorageInfo(); + + /* + * Put/Get bucket quota operations + */ + doBucketQuotaOperation(); + + /* + * Put/Get bucket versioning operations + */ + doBucketVersioningOperation(); + + /* + * Put/Get bucket acl operations + */ + doBucketAclOperation(); + + /* + * Put/Get/Delete bucket cors operations + */ + doBucketCorsOperation(); + + /* + * Options bucket operation + */ + optionsBucket(); + + /* + * Get bucket metadata operation + */ + getBucketMetadata(); + + /* + * Put/Get/Delete bucket lifecycle operations + */ + doBucketLifecycleOperation(); + + /* + * Put/Get/Delete bucket logging operations + */ + doBucketLoggingOperation(); + + /* + * Put/Get/Delete bucket website operations + */ + doBucketWebsiteOperation(); + + /* + * Put/Get/Delete bucket tagging operations + */ + doBucketTaggingOperation(); + + /* + * Put/Get/Delete bucket encryption operations + */ + doBucketEncryptionOperation(); + + /* + * Delete bucket operation + */ + deleteBucket(); + + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + } + + private static void optionsBucket() throws ObsException + { + System.out.println("Options bucket\n"); + OptionsInfoRequest optionInfo = new OptionsInfoRequest(); + optionInfo.setOrigin("http://www.a.com"); + optionInfo.getRequestHeaders().add("Authorization"); + optionInfo.getRequestMethod().add("PUT"); + System.out.println(obsClient.optionsBucket(bucketName, optionInfo)); + } + + private static void getBucketMetadata() + throws ObsException + { + System.out.println("Getting bucket metadata\n"); + BucketMetadataInfoRequest request = new BucketMetadataInfoRequest(bucketName); + request.setOrigin("http://www.a.com"); + request.getRequestHeaders().add("Authorization"); + BucketMetadataInfoResult result = obsClient.getBucketMetadata(request); + System.out.println("StorageClass:" + result.getBucketStorageClass()); + System.out.println("\tAllowedOrigins " + result.getAllowOrigin()); + System.out.println("\tAllowedMethods " + result.getAllowMethods()); + System.out.println("\tAllowedHeaders " + result.getAllowHeaders()); + System.out.println("\tExposeHeaders " + result.getExposeHeaders()); + System.out.println("\tMaxAgeSeconds " + result.getMaxAge() + "\n"); + + System.out.println("Deleting bucket CORS\n"); + obsClient.deleteBucketCors(bucketName); + } + + private static void doBucketTaggingOperation() + throws ObsException + { + System.out.println("Setting bucket tagging\n"); + + BucketTagInfo tagInfo = new BucketTagInfo(); + BucketTagInfo.TagSet tagSet = new BucketTagInfo.TagSet(); + tagSet.addTag("key1", "value1"); + tagSet.addTag("key2", "value2"); + tagInfo.setTagSet(tagSet); + + obsClient.setBucketTagging(bucketName, tagInfo); + + System.out.println("Getting bucket tagging\n"); + + System.out.println(obsClient.getBucketTagging(bucketName)); + + System.out.println("Deleting bucket tagging\n"); + obsClient.deleteBucketTagging(bucketName); + } + + private static void doBucketEncryptionOperation() + throws ObsException + { + System.out.println("Setting bucket encryption\n"); + + BucketEncryption encryption = new BucketEncryption(SSEAlgorithmEnum.KMS); +// encryption.setKmsKeyId("your kmsKeyId"); + obsClient.setBucketEncryption(bucketName, encryption); + + System.out.println("Gettting bucket encryption\n"); + System.out.println(obsClient.getBucketEncryption(bucketName)); + + System.out.println("Deleting bucket encryption\n"); + obsClient.deleteBucketEncryption(bucketName); + + } + + private static void doBucketVersioningOperation() + throws ObsException + { + System.out.println("Getting bucket versioning config " + obsClient.getBucketVersioning(bucketName) + "\n"); + //Enable bucket versioning + obsClient.setBucketVersioning(bucketName, new BucketVersioningConfiguration(VersioningStatusEnum.ENABLED)); + System.out.println("Current bucket versioning config " + obsClient.getBucketVersioning(bucketName) + "\n"); + //Suspend bucket versioning + BucketVersioningConfiguration suspended = new BucketVersioningConfiguration(VersioningStatusEnum.SUSPENDED); + obsClient.setBucketVersioning(bucketName, suspended); + System.out.println("Current bucket versioning config " + obsClient.getBucketVersioning(bucketName) + "\n"); + } + + private static void doBucketQuotaOperation() + throws ObsException + { + BucketQuota quota = new BucketQuota(); + //Set bucket quota to 1GB + quota.setBucketQuota(1024 * 1024 * 1024l); + obsClient.setBucketQuota(bucketName, quota); + System.out.println("Getting bucket quota " + obsClient.getBucketQuota(bucketName) + "\n"); + } + + private static void getBucketStorageInfo() + throws ObsException + { + BucketStorageInfo storageInfo = obsClient.getBucketStorageInfo(bucketName); + System.out.println("Getting bucket storageInfo " + storageInfo + "\n"); + } + + private static void doBucketAclOperation() + throws ObsException + { + System.out.println("Setting bucket ACL to public-read \n"); + + obsClient.setBucketAcl(bucketName, AccessControlList.REST_CANNED_PUBLIC_READ); + + System.out.println("Getting bucket ACL " + obsClient.getBucketAcl(bucketName) + "\n"); + + System.out.println("Setting bucket ACL to private \n"); + + obsClient.setBucketAcl(bucketName, AccessControlList.REST_CANNED_PRIVATE); + + System.out.println("Getting bucket ACL " + obsClient.getBucketAcl(bucketName) + "\n"); + } + + private static void doBucketCorsOperation() + throws ObsException + { + BucketCors bucketCors = new BucketCors(); + BucketCorsRule rule = new BucketCorsRule(); + rule.getAllowedHeader().add("Authorization"); + rule.getAllowedOrigin().add("http://www.a.com"); + rule.getAllowedOrigin().add("http://www.b.com"); + rule.getExposeHeader().add("x-obs-test1"); + rule.getExposeHeader().add("x-obs-test2"); + rule.setMaxAgeSecond(100); + rule.getAllowedMethod().add("HEAD"); + rule.getAllowedMethod().add("GET"); + rule.getAllowedMethod().add("PUT"); + bucketCors.getRules().add(rule); + + System.out.println("Setting bucket CORS\n"); + obsClient.setBucketCors(bucketName, bucketCors); + + System.out.println("Getting bucket CORS:" + obsClient.getBucketCors(bucketName) + "\n"); + + } + + private static void doBucketLifecycleOperation() + throws ObsException + { + final String ruleId0 = "delete obsoleted files"; + final String matchPrefix0 = "obsoleted/"; + final String ruleId1 = "delete temporary files"; + final String matchPrefix1 = "temporary/"; + final String ruleId2 = "delete temp files"; + final String matchPrefix2 = "temp/"; + + LifecycleConfiguration lifecycleConfig = new LifecycleConfiguration(); + LifecycleConfiguration.Rule rule0 = lifecycleConfig.new Rule(); + rule0.setEnabled(true); + rule0.setId(ruleId0); + rule0.setPrefix(matchPrefix0); + LifecycleConfiguration.Expiration expiration0 = lifecycleConfig.new Expiration(); + expiration0.setDays(10); + + rule0.setExpiration(expiration0); + lifecycleConfig.addRule(rule0); + + LifecycleConfiguration.Rule rule1 = lifecycleConfig.new Rule(); + rule1.setEnabled(true); + rule1.setId(ruleId1); + rule1.setPrefix(matchPrefix1); + LifecycleConfiguration.Expiration expiration1 = lifecycleConfig.new Expiration(); + try + { + expiration1.setDate(ServiceUtils.parseIso8601Date("2018-12-31T00:00:00")); + } + catch (ParseException e) + { + e.printStackTrace(); + } + rule1.setExpiration(expiration1); + lifecycleConfig.addRule(rule1); + + LifecycleConfiguration.Rule rule2 = lifecycleConfig.new Rule(); + rule2.setEnabled(true); + rule2.setId(ruleId2); + rule2.setPrefix(matchPrefix2); + LifecycleConfiguration.NoncurrentVersionExpiration noncurrentVersionExpiration = lifecycleConfig.new NoncurrentVersionExpiration(); + noncurrentVersionExpiration.setDays(10); + rule2.setNoncurrentVersionExpiration(noncurrentVersionExpiration); + lifecycleConfig.addRule(rule2); + + System.out.println("Setting bucket lifecycle\n"); + obsClient.setBucketLifecycleConfiguration(bucketName, lifecycleConfig); + + System.out.println("Getting bucket lifecycle:"); + LifecycleConfiguration result = obsClient.getBucketLifecycleConfiguration(bucketName); + LifecycleConfiguration.Rule r0 = result.getRules().get(0); + LifecycleConfiguration.Rule r1 = result.getRules().get(1); + LifecycleConfiguration.Rule r2 = result.getRules().get(2); + System.out.println("\tRule0: Id=" + r0.getId() + ", Prefix=" + r0.getPrefix() + ", Status=" + r0.getEnabled() + ", ExpirationDays=" + + r0.getExpiration().getDays()); + System.out.println("\tRule1: Id=" + r1.getId() + ", Prefix=" + r1.getPrefix() + ", Status=" + r1.getEnabled() + ", ExpirationTime=" + + r1.getExpiration().getDate()); + + System.out.println("\tRule1: Id=" + r2.getId() + ", Prefix=" + r2.getPrefix() + ", Status=" + r2.getEnabled() + + ", NocurrentExpirationDays=" + r2.getNoncurrentVersionExpiration().getDays()); + System.out.println(); + + System.out.println("Deleting bucket lifecycle\n"); + obsClient.deleteBucketLifecycleConfiguration(bucketName); + } + + private static void doBucketLoggingOperation() + throws ObsException + { + final String targetBucket = bucketName; + final String targetPrefix = "log-"; + BucketLoggingConfiguration configuration = new BucketLoggingConfiguration(); + configuration.setTargetBucketName(targetBucket); + configuration.setLogfilePrefix(targetPrefix); + configuration.setAgency("test"); + + System.out.println("Setting bucket logging\n"); + obsClient.setBucketLoggingConfiguration(bucketName, configuration, true); + + System.out.println("Getting bucket logging:"); + BucketLoggingConfiguration result = obsClient.getBucketLoggingConfiguration(bucketName); + System.out.println("\tTarget bucket=" + result.getTargetBucketName() + ", target prefix=" + result.getLogfilePrefix() + "\n"); + System.out.println(); + + System.out.println("Deleting bucket logging\n"); + obsClient.setBucketLoggingConfiguration(targetBucket, new BucketLoggingConfiguration()); + } + + private static void doBucketWebsiteOperation() + throws ObsException + { + WebsiteConfiguration websiteConfig = new WebsiteConfiguration(); + websiteConfig.setSuffix("index.html"); + websiteConfig.setKey("error.html"); + + System.out.println("Setting bucket website\n"); + obsClient.setBucketWebsiteConfiguration(bucketName, websiteConfig); + + System.out.println("Getting bucket website:"); + WebsiteConfiguration result = obsClient.getBucketWebsiteConfiguration(bucketName); + System.out.println("\tIndex document=" + result.getKey() + ", error document=" + result.getSuffix() + "\n"); + + System.out.println("Deleting bucket website\n"); + obsClient.deleteBucketWebsiteConfiguration(bucketName); + } + + private static void deleteBucket() + throws ObsException + { + System.out.println("Deleting bucket " + bucketName + "\n"); + obsClient.deleteBucket(bucketName); + } + + private static void getBucketLocation() + throws ObsException + { + String location = obsClient.getBucketLocation(bucketName); + System.out.println("Getting bucket location " + location + "\n"); + } + + private static void createBucket() + throws ObsException + { + ObsBucket obsBucket = new ObsBucket(); + obsBucket.setBucketName(bucketName); + obsClient.createBucket(obsBucket); + System.out.println("Create bucket:" + bucketName + " successfully!\n"); + } +} diff --git a/app/src/test/java/samples_java/ConcurrentCopyPartSample.java b/app/src/test/java/samples_java/ConcurrentCopyPartSample.java new file mode 100644 index 0000000..86e9135 --- /dev/null +++ b/app/src/test/java/samples_java/ConcurrentCopyPartSample.java @@ -0,0 +1,305 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.CopyPartRequest; +import com.obs.services.model.CopyPartResult; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ListPartsRequest; +import com.obs.services.model.ListPartsResult; +import com.obs.services.model.Multipart; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.PartEtag; +import com.obs.services.model.PutObjectRequest; + +/** + * This sample demonstrates how to multipart upload an object concurrently by copy mode + * to OBS using the OBS SDK for Java. + */ +public class ConcurrentCopyPartSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String sourceBucketName = bucketName; + + private static String sourceObjectKey = "my-obs-object-key-demo"; + + private static String objectKey = sourceObjectKey + "-back"; + + private static ExecutorService executorService = Executors.newFixedThreadPool(5); + + private static List partETags = Collections.synchronizedList(new ArrayList()); + + public static void main(String[] args) + throws IOException + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + System.out.println("Create a new bucket for demo\n"); + obsClient.createBucket(bucketName); + + /* + * Upload an object to your source bucket + */ + System.out.println("Uploading a new object to OBS from a file\n"); + obsClient.putObject(new PutObjectRequest(sourceBucketName, sourceObjectKey, createSampleFile())); + + /* + * Claim a upload id firstly + */ + String uploadId = claimUploadId(); + System.out.println("Claiming a new upload id " + uploadId + "\n"); + + long partSize = 5 * 1024 * 1024l;// 5MB + ObjectMetadata metadata = obsClient.getObjectMetadata(sourceBucketName, sourceObjectKey); + + long objectSize = metadata.getContentLength(); + + long partCount = objectSize % partSize == 0 ? objectSize / partSize : objectSize / partSize + 1; + + if (partCount > 10000) + { + throw new RuntimeException("Total parts count should not exceed 10000"); + } + else + { + System.out.println("Total parts count " + partCount + "\n"); + } + + /* + * Upload multiparts by copy mode + */ + System.out.println("Begin to upload multiparts to OBS by copy mode \n"); + for (int i = 0; i < partCount; i++) + { + + long rangeStart = i * partSize; + long rangeEnd = (i + 1 == partCount) ? objectSize - 1 : rangeStart + partSize - 1; + executorService.execute(new PartCopier(sourceBucketName, sourceObjectKey, rangeStart, rangeEnd, i + 1, uploadId)); + } + + /* + * Waiting for all parts finished + */ + executorService.shutdown(); + while (!executorService.isTerminated()) + { + try + { + executorService.awaitTermination(3, TimeUnit.SECONDS); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + + /* + * Verify whether all parts are finished + */ + if (partETags.size() != partCount) + { + throw new IllegalStateException("Upload multiparts fail due to some parts are not finished yet"); + } + else + { + System.out.println("Succeed to complete multiparts into an object named " + objectKey + "\n"); + } + + /* + * View all parts uploaded recently + */ + listAllParts(uploadId); + + /* + * Complete to upload multiparts + */ + completeMultipartUpload(uploadId); + + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + private static class PartCopier implements Runnable + { + + private String sourceBucketName; + + private String sourceObjectKey; + + private long rangeStart; + + private long rangeEnd; + + private int partNumber; + + private String uploadId; + + public PartCopier(String sourceBucketName, String sourceObjectKey, long rangeStart, long rangeEnd, int partNumber, String uploadId) + { + this.sourceBucketName = sourceBucketName; + this.sourceObjectKey = sourceObjectKey; + this.rangeStart = rangeStart; + this.rangeEnd = rangeEnd; + this.partNumber = partNumber; + this.uploadId = uploadId; + } + + @Override + public void run() + { + try + { + CopyPartRequest request = new CopyPartRequest(); + request.setUploadId(this.uploadId); + request.setSourceBucketName(this.sourceBucketName); + request.setSourceObjectKey(this.sourceObjectKey); + request.setDestinationBucketName(bucketName); + request.setDestinationObjectKey(objectKey); + request.setByteRangeStart(this.rangeStart); + request.setByteRangeEnd(this.rangeEnd); + request.setPartNumber(this.partNumber); + CopyPartResult result = obsClient.copyPart(request); + System.out.println("Part#" + this.partNumber + " done\n"); + partETags.add(new PartEtag(result.getEtag(), result.getPartNumber())); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + } + + private static String claimUploadId() + throws ObsException + { + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectKey); + InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request); + return result.getUploadId(); + } + + private static File createSampleFile() throws IOException + { + File file = File.createTempFile("obs-java-sdk-", ".txt"); + file.deleteOnExit(); + + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + for (int i = 0; i < 1000000; i++) + { + writer.write(UUID.randomUUID() + "\n"); + writer.write(UUID.randomUUID() + "\n"); + } + writer.flush(); + writer.close(); + + return file; + } + + private static void completeMultipartUpload(String uploadId) + throws ObsException + { + // Make part numbers in ascending order + Collections.sort(partETags, new Comparator() + { + + @Override + public int compare(PartEtag o1, PartEtag o2) + { + return o1.getPartNumber() - o2.getPartNumber(); + } + }); + + System.out.println("Completing to upload multiparts\n"); + CompleteMultipartUploadRequest completeMultipartUploadRequest = + new CompleteMultipartUploadRequest(bucketName, objectKey, uploadId, partETags); + obsClient.completeMultipartUpload(completeMultipartUploadRequest); + } + + private static void listAllParts(String uploadId) + throws ObsException + { + System.out.println("Listing all parts......"); + ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, objectKey, uploadId); + ListPartsResult partListing = obsClient.listParts(listPartsRequest); + + for (Multipart part : partListing.getMultipartList()) + { + System.out.println("\tPart#" + part.getPartNumber() + ", ETag=" + part.getEtag()); + } + System.out.println(); + } +} diff --git a/app/src/test/java/samples_java/ConcurrentDownloadObjectSample.java b/app/src/test/java/samples_java/ConcurrentDownloadObjectSample.java new file mode 100644 index 0000000..2b5da36 --- /dev/null +++ b/app/src/test/java/samples_java/ConcurrentDownloadObjectSample.java @@ -0,0 +1,276 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.RandomAccessFile; +import java.io.Writer; +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.GetObjectRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.ObsObject; + +/** + * This sample demonstrates how to download an object concurrently + * from OBS using the OBS SDK for Java. + */ +public class ConcurrentDownloadObjectSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static String localFilePath = "/temp/" + objectKey; + + private static ExecutorService executorService = Executors.newFixedThreadPool(5); + + private static AtomicInteger blocks = new AtomicInteger(0); + + public static void main(String[] args) + throws IOException + { + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + try + { + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + System.out.println("Create a new bucket to upload file\n"); + obsClient.createBucket(bucketName); + + /* + * Upload an object to your bucket + */ + System.out.println("Uploading a new object to OBS from a file\n"); + obsClient.putObject(bucketName, objectKey, createSampleFile()); + + /* + * Get size of the object and pre-create a random access file to hold object data + */ + ObjectMetadata metadata = obsClient.getObjectMetadata(bucketName, objectKey, null); + long objectSize = metadata.getContentLength(); + + System.out.println("Object size from metadata:" + objectSize + "\n"); + + File localFile = new File(localFilePath); + if (!localFile.getParentFile().exists()) + { + localFile.getParentFile().mkdirs(); + } + RandomAccessFile raf = new RandomAccessFile(localFile, "rw"); + raf.setLength(objectSize); + raf.close(); + + /* + * Calculate how many blocks to be divided + */ + final long blockSize = 5 * 1024 * 1024L; // 5MB + int blockCount = (int)(objectSize / blockSize); + if (objectSize % blockSize != 0) + { + blockCount++; + } + System.out.println("Total blocks count " + blockCount + "\n"); + + /* + * Download the object concurrently + */ + System.out.println("Start to download " + objectKey + "\n"); + for (int i = 0; i < blockCount;) + { + long rangeStart = i++ * blockSize; + long rangeEnd = (i == blockCount) ? objectSize - 1 : i * blockSize - 1; + executorService.execute(new BlockFetcher(rangeStart, rangeEnd, i)); + } + + /* + * Waiting for all blocks finished + */ + executorService.shutdown(); + while (!executorService.isTerminated()) + { + try + { + executorService.awaitTermination(5, TimeUnit.SECONDS); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + + /* + * Verify whether all blocks are finished + */ + if (blocks.intValue() != blockCount) + { + throw new IllegalStateException("Some blocks are not finished"); + } + else + { + System.out.println("Succeed to download object " + objectKey); + } + + System.out.println("Deleting object " + objectKey + "\n"); + obsClient.deleteObject(bucketName, objectKey, null); + + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + } + + private static class BlockFetcher implements Runnable + { + private long rangeStart; + + private long rangeEnd; + + private int blockNumber; + + public BlockFetcher(long rangeStart, long rangeEnd, int blockNumber) + { + this.rangeStart = rangeStart; + this.rangeEnd = rangeEnd; + this.blockNumber = blockNumber; + } + + @Override + public void run() + { + RandomAccessFile raf = null; + try + { + raf = new RandomAccessFile(localFilePath, "rw"); + raf.seek(rangeStart); + + GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectKey); + getObjectRequest.setRangeStart(rangeStart); + getObjectRequest.setRangeEnd(rangeEnd); + ObsObject object = obsClient.getObject(getObjectRequest); + + InputStream content = object.getObjectContent(); + try + { + byte[] buf = new byte[8196]; + int bytes = 0; + while ((bytes = content.read(buf)) != -1) + { + raf.write(buf, 0, bytes); + } + blocks.incrementAndGet(); + System.out.println("Block : " + blockNumber + " Finish \n"); + } + catch (IOException e) + { + e.printStackTrace(); + } + finally + { + content.close(); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + finally + { + if (raf != null) + { + try + { + raf.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } + } + } + + private static File createSampleFile() + throws IOException + { + File file = File.createTempFile("obs-java-sdk-", ".txt"); + file.deleteOnExit(); + + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + for (int i = 0; i < 1000000; i++) + { + writer.write(UUID.randomUUID() + "\n"); + writer.write(UUID.randomUUID() + "\n"); + } + writer.flush(); + writer.close(); + + return file; + } + +} diff --git a/app/src/test/java/samples_java/ConcurrentUploadPartSample.java b/app/src/test/java/samples_java/ConcurrentUploadPartSample.java new file mode 100644 index 0000000..2ed20ba --- /dev/null +++ b/app/src/test/java/samples_java/ConcurrentUploadPartSample.java @@ -0,0 +1,290 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.ListPartsRequest; +import com.obs.services.model.ListPartsResult; +import com.obs.services.model.Multipart; +import com.obs.services.model.PartEtag; +import com.obs.services.model.UploadPartRequest; +import com.obs.services.model.UploadPartResult; + +/** + * This sample demonstrates how to multipart upload an object concurrently + * from OBS using the OBS SDK for Java. + */ +public class ConcurrentUploadPartSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static ExecutorService executorService = Executors.newFixedThreadPool(5); + + private static List partETags = Collections.synchronizedList(new ArrayList()); + + public static void main(String[] args) + throws IOException + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + System.out.println("Create a new bucket for demo\n"); + obsClient.createBucket(bucketName); + + /* + * Claim a upload id firstly + */ + String uploadId = claimUploadId(); + System.out.println("Claiming a new upload id " + uploadId + "\n"); + + long partSize = 5 * 1024 * 1024l;// 5MB + File sampleFile = createSampleFile(); + long fileLength = sampleFile.length(); + + long partCount = fileLength % partSize == 0 ? fileLength / partSize : fileLength / partSize + 1; + + if (partCount > 10000) + { + throw new RuntimeException("Total parts count should not exceed 10000"); + } + else + { + System.out.println("Total parts count " + partCount + "\n"); + } + + /* + * Upload multiparts to your bucket + */ + System.out.println("Begin to upload multiparts to OBS from a file\n"); + for (int i = 0; i < partCount; i++) + { + long offset = i * partSize; + long currPartSize = (i + 1 == partCount) ? fileLength - offset : partSize; + executorService.execute(new PartUploader(sampleFile, offset, currPartSize, i + 1, uploadId)); + } + + /* + * Wait for all tasks to finish + */ + executorService.shutdown(); + while (!executorService.isTerminated()) + { + try + { + executorService.awaitTermination(5, TimeUnit.SECONDS); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + + /* + * Verify whether all tasks are finished + */ + if (partETags.size() != partCount) + { + throw new IllegalStateException("Some parts are not finished"); + } + else + { + System.out.println("Succeed to complete multiparts into an object named " + objectKey + "\n"); + } + + /* + * View all parts uploaded recently + */ + listAllParts(uploadId); + + /* + * Complete to upload multiparts + */ + completeMultipartUpload(uploadId); + + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + private static class PartUploader implements Runnable + { + + private File sampleFile; + + private long offset; + + private long partSize; + + private int partNumber; + + private String uploadId; + + public PartUploader(File sampleFile, long offset, long partSize, int partNumber, String uploadId) + { + this.sampleFile = sampleFile; + this.offset = offset; + this.partSize = partSize; + this.partNumber = partNumber; + this.uploadId = uploadId; + } + + @Override + public void run() + { + try + { + UploadPartRequest uploadPartRequest = new UploadPartRequest(); + uploadPartRequest.setBucketName(bucketName); + uploadPartRequest.setObjectKey(objectKey); + uploadPartRequest.setUploadId(this.uploadId); + uploadPartRequest.setFile(this.sampleFile); + uploadPartRequest.setPartSize(this.partSize); + uploadPartRequest.setOffset(this.offset); + uploadPartRequest.setPartNumber(this.partNumber); + + UploadPartResult uploadPartResult = obsClient.uploadPart(uploadPartRequest); + System.out.println("Part#" + this.partNumber + " done\n"); + partETags.add(new PartEtag(uploadPartResult.getEtag(), uploadPartResult.getPartNumber())); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + } + + private static String claimUploadId() + throws ObsException + { + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectKey); + InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request); + return result.getUploadId(); + } + + private static File createSampleFile() + throws IOException + { + File file = File.createTempFile("obs-java-sdk-", ".txt"); + file.deleteOnExit(); + + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + for (int i = 0; i < 1000000; i++) + { + writer.write(UUID.randomUUID() + "\n"); + writer.write(UUID.randomUUID() + "\n"); + } + writer.flush(); + writer.close(); + + return file; + } + + private static void completeMultipartUpload(String uploadId) + throws ObsException + { + // Make part numbers in ascending order + Collections.sort(partETags, new Comparator() + { + + @Override + public int compare(PartEtag o1, PartEtag o2) + { + return o1.getPartNumber() - o2.getPartNumber(); + } + }); + + System.out.println("Completing to upload multiparts\n"); + CompleteMultipartUploadRequest completeMultipartUploadRequest = + new CompleteMultipartUploadRequest(bucketName, objectKey, uploadId, partETags); + obsClient.completeMultipartUpload(completeMultipartUploadRequest); + } + + private static void listAllParts(String uploadId) + throws ObsException + { + System.out.println("Listing all parts......"); + ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, objectKey, uploadId); + ListPartsResult partListing = obsClient.listParts(listPartsRequest); + + for (Multipart part : partListing.getMultipartList()) + { + System.out.println("\tPart#" + part.getPartNumber() + ", ETag=" + part.getEtag()); + } + System.out.println(); + } + +} diff --git a/app/src/test/java/samples_java/CreateFolderSample.java b/app/src/test/java/samples_java/CreateFolderSample.java new file mode 100644 index 0000000..87efd66 --- /dev/null +++ b/app/src/test/java/samples_java/CreateFolderSample.java @@ -0,0 +1,125 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.ObsObject; +import com.obs.services.model.PutObjectRequest; + +/** + * This sample demonstrates how to create an empty folder under + * specified bucket to OBS using the OBS SDK for Java. + */ +public class CreateFolderSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + public static void main(String[] args) + throws IOException + { + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + System.out.println("Create a new bucket for demo\n"); + obsClient.createBucket(bucketName); + + /* + * Way 1: + * Create an empty folder without request body, note that the key must be + * suffixed with a slash + */ + final String keySuffixWithSlash1 = "MyObjectKey1/"; + obsClient.putObject(bucketName, keySuffixWithSlash1, new ByteArrayInputStream(new byte[0])); + System.out.println("Creating an empty folder " + keySuffixWithSlash1 + "\n"); + + /* + * Verify whether the size of the empty folder is zero + */ + ObsObject object = obsClient.getObject(bucketName, keySuffixWithSlash1, null); + System.out.println("Size of the empty folder '" + object.getObjectKey() + "' is " + object.getMetadata().getContentLength()); + object.getObjectContent().close(); + + /* + * Way 2: + * Create an empty folder without request body, note that the key must be + * suffixed with a slash + */ + final String keySuffixWithSlash2 = "MyObjectKey2/"; + PutObjectRequest request = new PutObjectRequest(); + request.setBucketName(bucketName); + request.setObjectKey(keySuffixWithSlash2); + request.setInput(new ByteArrayInputStream(new byte[0])); + obsClient.putObject(request); + System.out.println("Creating an empty folder " + keySuffixWithSlash2 + "\n"); + + /* + * Verify whether the size of the empty folder is zero + */ + object = obsClient.getObject(bucketName, keySuffixWithSlash2, null); + System.out.println("Size of the empty folder '" + object.getObjectKey() + "' is " + object.getMetadata().getContentLength()); + object.getObjectContent().close(); + + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + } +} diff --git a/app/src/test/java/samples_java/DeleteObjectsSample.java b/app/src/test/java/samples_java/DeleteObjectsSample.java new file mode 100644 index 0000000..102e588 --- /dev/null +++ b/app/src/test/java/samples_java/DeleteObjectsSample.java @@ -0,0 +1,146 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.DeleteObjectsResult; +import com.obs.services.model.DeleteObjectsResult.DeleteObjectResult; +import com.obs.services.model.DeleteObjectsResult.ErrorResult; +import com.obs.services.model.KeyAndVersion; + +/** + * This sample demonstrates how to delete objects under specified bucket + * from OBS using the OBS SDK for Java. + */ +public class DeleteObjectsSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + public static void main(String[] args) + throws IOException + { + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + System.out.println("Create a new bucket for demo\n"); + obsClient.createBucket(bucketName); + + /* + * Batch put objects into the bucket + */ + final String content = "Thank you for using Object Storage Service"; + final String keyPrefix = "MyObjectKey"; + List keys = new ArrayList(); + for (int i = 0; i < 100; i++) + { + String key = keyPrefix + i; + InputStream instream = new ByteArrayInputStream(content.getBytes()); + obsClient.putObject(bucketName, key, instream, null); + System.out.println("Succeed to put object " + key); + keys.add(key); + } + System.out.println(); + + /* + * Delete all objects uploaded recently under the bucket + */ + System.out.println("\nDeleting all objects\n"); + + DeleteObjectsRequest request = new DeleteObjectsRequest(); + request.setBucketName(bucketName); + request.setQuiet(false); + + KeyAndVersion[] kvs = new KeyAndVersion[keys.size()]; + int index = 0; + for (String key : keys) + { + kvs[index++] = new KeyAndVersion(key); + } + + request.setKeyAndVersions(kvs); + + System.out.println("Delete results:"); + + DeleteObjectsResult deleteObjectsResult = obsClient.deleteObjects(request); + for (DeleteObjectResult object : deleteObjectsResult.getDeletedObjectResults()) + { + System.out.println("\t" + object); + } + + System.out.println("Error results:"); + + for (ErrorResult error : deleteObjectsResult.getErrorResults()) + { + System.out.println("\t" + error); + } + + System.out.println(); + + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + } +} diff --git a/app/src/test/java/samples_java/DownloadFileSample.java b/app/src/test/java/samples_java/DownloadFileSample.java new file mode 100644 index 0000000..fa2d5d2 --- /dev/null +++ b/app/src/test/java/samples_java/DownloadFileSample.java @@ -0,0 +1,187 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package samples_java; + +import java.io.IOException; +import java.util.Date; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.DownloadFileRequest; +import com.obs.services.model.MonitorableProgressListener; +import com.obs.services.model.ProgressStatus; + +public class DownloadFileSample { + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static String localSavePath = "local save path"; + + public static void main(String[] args) { + + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + + try { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + DownloadFileRequest request = new DownloadFileRequest(bucketName, objectKey); + // Set the local path to which the object is downloaded. + request.setDownloadFile(localSavePath); + // Set the maximum number of parts that can be concurrently downloaded. + request.setTaskNum(5); + // Set the part size to 1 MB. + request.setPartSize(1 * 1024 * 1024); + // Enable resumable upload. + request.setEnableCheckpoint(true); + // Trigger the listener callback every 100 KB. + request.setProgressInterval(100 * 1024L); + + MonitorableProgressListener progressListener = new MonitorableProgressListener() { + @Override + public void progressChanged(ProgressStatus status) { + System.out.println(new Date() + " TransferPercentage:" + status.getTransferPercentage()); + } + }; + // Set a data transmission listener that can monitor the running status of subprocesses. + request.setProgressListener(progressListener); + + // Start a thread to download an object. + DownloadFileManager downloadManager = new DownloadFileManager(obsClient, request, progressListener); + downloadManager.download(); + + // Wait for 3 seconds. + try { + Thread.sleep(3 * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + // Interrupt the thread to simulate the suspension of the download task. + // Note: After this method is called, the download task does not stop immediately. You are advised to call the progressListener.waitingFinish method to wait until the task is complete. + // In addition, after this method is called, "java.lang.RuntimeException: Abort io due to thread interrupted” is displayed in the log. This is a normal phenomenon after the thread is interrupted. You can ignore this exception. + downloadManager.pause(); + System.out.println(new Date() + " download thread is stop. \n"); + + // Wait for 3 seconds. + try { + Thread.sleep(3 * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.out.println(new Date() + " restart request. \n"); + downloadManager.download(); + + // Wait until the download task is complete. + downloadManager.waitingFinish(); + } catch (ObsException e) { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } catch (InterruptedException e) { + + } finally { + if (obsClient != null) { + try { + /* + * Close obs client + */ + obsClient.close(); + } catch (IOException e) { + } + } + } + } +} + +class DownloadFileManager { + private DownloadFileRequest request; + private ObsClient obsClient; + private MonitorableProgressListener progressListener; + private Thread currentThread; + + public DownloadFileManager(ObsClient obsClient, DownloadFileRequest request, MonitorableProgressListener progressListener) { + this.obsClient = obsClient; + this.request = request; + this.progressListener = progressListener; + request.setProgressListener(progressListener); + } + + /** + * Start a download task. + */ + public void download() { + this.progressListener.reset(); + this.currentThread = new Thread(new Runnable() { + @Override + public void run() { + try { + // Use resumable download. + obsClient.downloadFile(request); + } catch (ObsException e) { + // When an exception occurs, you can call the interface for resumable download to download the file again. + if (null != e.getCause() + && e.getCause() instanceof InterruptedException) { + System.out.println(new Date() + " current thread is interrupted. \n"); + } else { + e.printStackTrace(); + } + } + } + }); + + this.currentThread.start(); + } + + /** + * Pause a task. + * @throws InterruptedException + */ + public void pause() throws InterruptedException { + // Interrupt the thread to simulate the suspension of the download task. + // Note: After this method is called, the download task does not stop immediately. You are advised to call the progressListener.waitingFinish method to wait until the task is complete. + // In addition, after this method is called, "java.lang.RuntimeException: Abort io due to thread interrupted” is displayed in the log. This is a normal phenomenon after the thread is interrupted. You can ignore this exception. + this.currentThread.interrupt(); + + // Wait until the download task is complete. + this.progressListener.waitingFinish(); + } + + /** + * Wait until the download task is complete. + * @throws InterruptedException + */ + public void waitingFinish() throws InterruptedException { + this.currentThread.join(); + } +} diff --git a/app/src/test/java/samples_java/DownloadSample.java b/app/src/test/java/samples_java/DownloadSample.java new file mode 100644 index 0000000..5ac2934 --- /dev/null +++ b/app/src/test/java/samples_java/DownloadSample.java @@ -0,0 +1,185 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.ByteBuffer; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.WritableByteChannel; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.ObsObject; + +/** + * This sample demonstrates how to download an object + * from OBS in different ways using the OBS SDK for Java. + */ +public class DownloadSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static String localFilePath = "/temp/" + objectKey; + + public static void main(String[] args) + throws IOException + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + System.out.println("Create a new bucket for demo\n"); + obsClient.createBucket(bucketName); + + /* + * Upload an object to your bucket + */ + System.out.println("Uploading a new object to OBS from a file\n"); + obsClient.putObject(bucketName, objectKey, createSampleFile()); + + System.out.println("Downloading an object\n"); + + /* + * Download the object as an inputstream and display it directly + */ + simpleDownload(); + + File localFile = new File(localFilePath); + if (!localFile.getParentFile().exists()) + { + localFile.getParentFile().mkdirs(); + } + + System.out.println("Downloading an object to file:" + localFilePath + "\n"); + /* + * Download the object to a file + */ + downloadToLocalFile(); + + System.out.println("Deleting object " + objectKey + "\n"); + obsClient.deleteObject(bucketName, objectKey, null); + + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + } + + private static void downloadToLocalFile() + throws ObsException, IOException + { + ObsObject obsObject = obsClient.getObject(bucketName, objectKey, null); + ReadableByteChannel rchannel = Channels.newChannel(obsObject.getObjectContent()); + + ByteBuffer buffer = ByteBuffer.allocate(4096); + WritableByteChannel wchannel = Channels.newChannel(new FileOutputStream(new File(localFilePath))); + + while (rchannel.read(buffer) != -1) + { + buffer.flip(); + wchannel.write(buffer); + buffer.clear(); + } + rchannel.close(); + wchannel.close(); + } + + private static void simpleDownload() + throws ObsException, IOException + { + ObsObject obsObject = obsClient.getObject(bucketName, objectKey, null); + displayTextInputStream(obsObject.getObjectContent()); + } + + private static void displayTextInputStream(InputStream input) + throws IOException + { + BufferedReader reader = new BufferedReader(new InputStreamReader(input)); + while (true) + { + String line = reader.readLine(); + if (line == null) + break; + + System.out.println("\t" + line); + } + System.out.println(); + + reader.close(); + } + + private static File createSampleFile() + throws IOException + { + File file = File.createTempFile("obs-java-sdk-", ".txt"); + file.deleteOnExit(); + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + writer.write("abcdefghijklmnopqrstuvwxyz\n"); + writer.write("0123456789011234567890\n"); + writer.close(); + + return file; + } + +} diff --git a/app/src/test/java/samples_java/GetTokenSample.java b/app/src/test/java/samples_java/GetTokenSample.java new file mode 100644 index 0000000..2c9b6fc --- /dev/null +++ b/app/src/test/java/samples_java/GetTokenSample.java @@ -0,0 +1,260 @@ +package samples_java; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.InetSocketAddress; +import java.net.Proxy.Type; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.cert.X509Certificate; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import com.obs.services.internal.Constants.CommonHeaders; + +import okhttp3.Authenticator; +import okhttp3.Call; +import okhttp3.Credentials; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.Route; + +public class GetTokenSample { + private static final String endPoint = "https://your-iam-endpoint"; + + private static final String userName = "*** Provide your user name ***"; + + private static final String passWord = "*** Provide your password ***"; + + private static final String domainName = "*** Provide your domain name ***"; + + private static final long durationSeconds = 900L; + + private static String token; + + private static final boolean proxyIsable = false; + + private static final String proxyHostAddress = "*** Provide your proxy host address ***"; + + private static final int proxyPort = 8080; + + private static final String proxyUser = "*** Provide your proxy user ***"; + + private static final String proxyPassword = "*** Provide your proxy password ***"; + + public static void main(String[] args) { + getToken(); + + getSecurityToken(token); + } + + private static void getToken() { + Request.Builder builder = new Request.Builder(); + builder.addHeader("Content-Type", "application/json;charset=utf8"); + builder.url(endPoint + "/v3/auth/tokens"); + String mimeType = "application/json"; + /* request body sample + { + "auth": { + "identity": { + "methods": ["password"], + "password": { + "user": { + "name": "***userName***", + "password": "***passWord***", + "domain": { + "name": "***domainName***" + } + } + } + }, + "scope": { + "domain": { + "name": "***domainName***" + } + } + } + } + */ + String content = "{\r\n" + + " \"auth\": {\r\n" + + " \"identity\": {\r\n" + + " \"methods\": [\"password\"],\r\n" + + " \"password\": {\r\n" + + " \"user\": {\r\n" + + " \"name\": \"" + userName + "\",\r\n" + + " \"password\": \"" + passWord + "\",\r\n" + + " \"domain\": {\r\n" + + " \"name\": \"" + domainName + "\"\r\n" + + " }\r\n" + + " }\r\n" + + " }\r\n" + + " },\r\n" + + " \"scope\": {\r\n" + + " \"domain\": {\r\n" + + " \"name\": \"" + domainName + "\"\r\n" + + " }\r\n" + + " }\r\n" + + " }\r\n" + + " }"; + try { + builder.post(createRequestBody(mimeType, content)); + } catch (UnsupportedEncodingException e1) { + e1.printStackTrace(); + } + try { + getTokeResponse(builder.build()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void getTokeResponse(Request request) throws IOException { + Call c = GetHttpClient().newCall(request); + Response res = c.execute(); + if (res.headers() != null) { + String header = res.headers().toString(); + if (header == null || header.trim().equals("")) { + System.out.println("\n"); + } else { + String subjectToken = res.header("X-Subject-Token").toString(); + System.out.println("the Token :" + subjectToken); + token = subjectToken; + } + } + res.close(); + } + + private static void getSecurityToken(String token) { + if(token == null) { + + } + Request.Builder builder = new Request.Builder(); + builder.addHeader("Content-Type", "application/json;charset=utf8"); + builder.url(endPoint + "/v3.0/OS-CREDENTIAL/securitytokens"); + String mimeType = "application/json"; + + /* request body sample + { + "auth": { + "identity": { + "methods": [ + "token" + ], + "token": { + "id": "***yourToken***", + "duration-seconds": "***your-duration-seconds***" + } + } + } + } + */ + String content = "{\r\n" + + " \"auth\": {\r\n" + + " \"identity\": {\r\n" + + " \"methods\": [\r\n" + + " \"token\"\r\n" + + " ],\r\n" + + " \"token\": {\r\n" + + " \"id\": \""+ token +"\",\r\n" + + " \"duration-seconds\": \""+ durationSeconds +"\"\r\n" + + "\r\n" + + " }\r\n" + + " }\r\n" + + " }\r\n" + + "}"; + + try { + builder.post(createRequestBody(mimeType, content)); + } catch (UnsupportedEncodingException e1) { + e1.printStackTrace(); + } + try { + getSecurityTokenResponse(builder.build()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void getSecurityTokenResponse(Request request) throws IOException { + Call c = GetHttpClient().newCall(request); + Response res = c.execute(); + if (res.body() != null) { + String content = res.body().string(); + if (content == null || content.trim().equals("")) { + System.out.println("\n"); + } else { + System.out.println("Content:" + content + "\n\n"); + } + } else { + System.out.println("\n"); + } + res.close(); + } + + private static OkHttpClient GetHttpClient() { + X509TrustManager xtm = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + X509Certificate[] x509Certificates = new X509Certificate[0]; + return x509Certificates; + } + }; + + SSLContext sslContext = null; + try { + sslContext = SSLContext.getInstance("SSL"); + sslContext.init(null, new TrustManager[] { xtm }, new SecureRandom()); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } + + HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { + @Override + public boolean verify(String arg0, SSLSession arg1) { + return true; + } + }; + + OkHttpClient.Builder builder = new OkHttpClient.Builder().followRedirects(false).retryOnConnectionFailure(false) + .sslSocketFactory(sslContext.getSocketFactory()).hostnameVerifier(DO_NOT_VERIFY).cache(null); + + if(proxyIsable) { + builder.proxy(new java.net.Proxy(Type.HTTP, new InetSocketAddress(proxyHostAddress, proxyPort))); + + Authenticator proxyAuthenticator = new Authenticator() { + @Override public Request authenticate(Route route, Response response) throws IOException { + String credential = Credentials.basic(proxyUser, proxyPassword); + return response.request().newBuilder() + .header(CommonHeaders.PROXY_AUTHORIZATION, credential) + .build(); + } + }; + builder.proxyAuthenticator(proxyAuthenticator); + } + + return builder.build(); + } + + private static RequestBody createRequestBody(String mimeType, String content) throws UnsupportedEncodingException { + return RequestBody.create(MediaType.parse(mimeType), content.getBytes("UTF-8")); + } +} diff --git a/app/src/test/java/samples_java/ListObjectsInFolderSample.java b/app/src/test/java/samples_java/ListObjectsInFolderSample.java new file mode 100644 index 0000000..7954556 --- /dev/null +++ b/app/src/test/java/samples_java/ListObjectsInFolderSample.java @@ -0,0 +1,218 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.DeleteObjectsResult; +import com.obs.services.model.DeleteObjectsResult.DeleteObjectResult; +import com.obs.services.model.KeyAndVersion; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObsObject; + +/** + * This sample demonstrates how to list objects under a specified folder of a bucket + * from OBS using the OBS SDK for Java. + */ +public class ListObjectsInFolderSample +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + public static void main(String[] args) throws UnsupportedEncodingException + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + obsClient.createBucket(bucketName); + + final String content = "Hello OBS"; + final String keyPrefix = "MyObjectKey"; + final String folderPrefix = "src"; + final String subFolderPrefix = "test"; + ObjectListing objectListing = null; + ListObjectsRequest listObjectsRequest = null; + List keys = new ArrayList(); + /* + * First prepare folders and sub folders + */ + + for (int i = 0; i < 5; i++) + { + String key = folderPrefix + i + "/"; + obsClient.putObject(bucketName, key, new ByteArrayInputStream(new byte[0]), null); + keys.add(key); + + for (int j = 0; j < 3; j++) + { + String subKey = key + subFolderPrefix + j + "/"; + obsClient.putObject(bucketName, subKey, new ByteArrayInputStream(new byte[0])); + keys.add(subKey); + } + } + + /* + * Insert 2 objects in each folder + */ + objectListing = obsClient.listObjects(bucketName); + for(ObsObject object : objectListing.getObjects()){ + for(int i=0;i<2;i++){ + String objectKey = object.getObjectKey() + keyPrefix + i; + obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + keys.add(objectKey); + } + } + /* + * Insert 2 objects in root path + */ + obsClient.putObject(bucketName, keyPrefix+0, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + obsClient.putObject(bucketName, keyPrefix+1, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + keys.add(keyPrefix+0); + keys.add(keyPrefix+1); + System.out.println("Put " + keys.size() + " objects completed."); + System.out.println(); + + /* + * List all objects in folder src0/ + */ + System.out.println("List all objects in folder src0/ \n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setPrefix("src0/"); + objectListing = obsClient.listObjects(listObjectsRequest); + for(ObsObject object : objectListing.getObjects()){ + System.out.println("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag()+ "]"); + } + System.out.println(); + + /* + * List all objects in sub folder src0/test0/ + */ + System.out.println("List all objects in folder src0/test0/ \n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setPrefix("src0/test0/"); + objectListing = obsClient.listObjects(listObjectsRequest); + for(ObsObject object : objectListing.getObjects()){ + System.out.println("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag()+ "]"); + } + System.out.println(); + + /* + * List all objects group by folder + */ + System.out.println("List all objects group by folder \n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setDelimiter("/"); + + objectListing = obsClient.listObjects(listObjectsRequest); + System.out.println("Root path:"); + for(ObsObject object : objectListing.getObjects()){ + System.out.println("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag()+ "]"); + } + listObjectsByPrefix(listObjectsRequest, objectListing); + + System.out.println(); + + + /* + * Delete all the objects created + */ + DeleteObjectsRequest request = new DeleteObjectsRequest(); + request.setBucketName(bucketName); + request.setQuiet(false); + KeyAndVersion[] kvs = new KeyAndVersion[keys.size()]; + int index = 0; + for (String key : keys) + { + kvs[index++] = new KeyAndVersion(key); + } + + request.setKeyAndVersions(kvs); + + System.out.println("Delete results:"); + + DeleteObjectsResult deleteObjectsResult = obsClient.deleteObjects(request); + for (DeleteObjectResult object : deleteObjectsResult.getDeletedObjectResults()) + { + System.out.println("\t" + object); + } + + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + private static void listObjectsByPrefix(ListObjectsRequest listObjectsRequest, ObjectListing objectListing) throws ObsException + { + for(String prefix : objectListing.getCommonPrefixes()){ + System.out.println("Folder " + prefix + ":"); + listObjectsRequest.setPrefix(prefix); + objectListing = obsClient.listObjects(listObjectsRequest); + for(ObsObject object : objectListing.getObjects()){ + System.out.println("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag()+ "]"); + } + listObjectsByPrefix(listObjectsRequest, objectListing); + } + } +} diff --git a/app/src/test/java/samples_java/ListObjectsSample.java b/app/src/test/java/samples_java/ListObjectsSample.java new file mode 100644 index 0000000..46436c7 --- /dev/null +++ b/app/src/test/java/samples_java/ListObjectsSample.java @@ -0,0 +1,212 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.DeleteObjectsResult; +import com.obs.services.model.DeleteObjectsResult.DeleteObjectResult; +import com.obs.services.model.KeyAndVersion; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObsObject; + +/** + * This sample demonstrates how to list objects under specified bucket + * from OBS using the OBS SDK for Java. + */ +public class ListObjectsSample +{ + + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + public static void main(String[] args) + throws UnsupportedEncodingException + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + obsClient.createBucket(bucketName); + + final String content = "Hello OBS"; + final String keyPrefix = "MyObjectKey"; + ObjectListing objectListing = null; + ListObjectsRequest listObjectsRequest = null; + /* + * First insert 100 objects for demo + */ + List keys = new ArrayList(); + for (int i = 0; i < 100; i++) + { + String key = keyPrefix + i; + InputStream instream = new ByteArrayInputStream(content.getBytes("UTF-8")); + obsClient.putObject(bucketName, key, instream, null); + keys.add(key); + } + System.out.println("Put " + keys.size() + " objects completed."); + + /* + * List objects using default parameters, will return up to 1000 objects + */ + System.out.println("List objects using default parameters:\n"); + objectListing = obsClient.listObjects(bucketName); + for (ObsObject object : objectListing.getObjects()) + { + System.out.println("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + System.out.println(); + + /* + * List the first 10 objects + */ + System.out.println("List the first 10 objects :\n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setMaxKeys(10); + objectListing = obsClient.listObjects(listObjectsRequest); + for (ObsObject object : objectListing.getObjects()) + { + System.out.println("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + System.out.println(); + + String theSecond10ObjectsMarker = objectListing.getNextMarker(); + + /* + * List the second 10 objects using marker + */ + System.out.println("List the second 10 objects using marker:\n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setMaxKeys(10); + listObjectsRequest.setMarker(theSecond10ObjectsMarker); + objectListing = obsClient.listObjects(listObjectsRequest); + for (ObsObject object : objectListing.getObjects()) + { + System.out.println("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + System.out.println(); + + /* + * List objects with prefix and max keys + */ + System.out.println("List objects with prefix and max keys:\n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setPrefix(keyPrefix + "2"); + listObjectsRequest.setMaxKeys(5); + objectListing = obsClient.listObjects(listObjectsRequest); + for (ObsObject object : objectListing.getObjects()) + { + System.out.println("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + System.out.println(); + + /* + * List all the objects in way of pagination + */ + System.out.println("List all the objects in way of pagination:\n"); + listObjectsRequest = new ListObjectsRequest(bucketName); + listObjectsRequest.setMaxKeys(10); + String nextMarker = null; + int index = 1; + do + { + listObjectsRequest.setMarker(nextMarker); + objectListing = obsClient.listObjects(listObjectsRequest); + System.out.println("Page:" + index++ + "\n"); + for (ObsObject object : objectListing.getObjects()) + { + System.out.println("\t" + object.getObjectKey() + " etag[" + object.getMetadata().getEtag() + "]"); + } + nextMarker = objectListing.getNextMarker(); + } while (objectListing.isTruncated()); + System.out.println(); + + /* + * Delete all the objects created + */ + DeleteObjectsRequest request = new DeleteObjectsRequest(); + request.setBucketName(bucketName); + request.setQuiet(false); + KeyAndVersion[] kvs = new KeyAndVersion[keys.size()]; + index = 0; + for (String key : keys) + { + kvs[index++] = new KeyAndVersion(key); + } + + request.setKeyAndVersions(kvs); + + System.out.println("Delete results:"); + + DeleteObjectsResult deleteObjectsResult = obsClient.deleteObjects(request); + for (DeleteObjectResult object : deleteObjectsResult.getDeletedObjectResults()) + { + System.out.println("\t" + object); + } + + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } +} diff --git a/app/src/test/java/samples_java/ListVersionsSample.java b/app/src/test/java/samples_java/ListVersionsSample.java new file mode 100644 index 0000000..b92038b --- /dev/null +++ b/app/src/test/java/samples_java/ListVersionsSample.java @@ -0,0 +1,249 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.BucketVersioningConfiguration; +import com.obs.services.model.DeleteObjectsRequest; +import com.obs.services.model.DeleteObjectsResult; +import com.obs.services.model.DeleteObjectsResult.DeleteObjectResult; +import com.obs.services.model.KeyAndVersion; +import com.obs.services.model.ListVersionsRequest; +import com.obs.services.model.ListVersionsResult; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.ObsObject; +import com.obs.services.model.VersionOrDeleteMarker; +import com.obs.services.model.VersioningStatusEnum; + + +/** + * This sample demonstrates how to list versions under specified bucket + * from OBS using the OBS SDK for Java. + */ +public class ListVersionsSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + public static void main(String[] args) + throws UnsupportedEncodingException + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + obsClient.createBucket(bucketName); + + /* + * Enable bucket versioning + */ + obsClient.setBucketVersioning(bucketName, new BucketVersioningConfiguration(VersioningStatusEnum.ENABLED)); + + final String content = "Hello OBS"; + final String keyPrefix = "MyObjectKey"; + final String folderPrefix = "src"; + final String subFolderPrefix = "test"; + ObjectListing objectListing = null; + ListVersionsResult listVersionResult = null; + List keys = new ArrayList(); + VersionOrDeleteMarker[] versionsToDelete; + /* + * First prepare folders and sub folders + */ + + for (int i = 0; i < 5; i++) + { + String key = folderPrefix + i + "/"; + obsClient.putObject(bucketName, key, new ByteArrayInputStream(new byte[0]), null); + keys.add(key); + + for (int j = 0; j < 3; j++) + { + String subKey = key + subFolderPrefix + j + "/"; + obsClient.putObject(bucketName, subKey, new ByteArrayInputStream(new byte[0])); + keys.add(subKey); + } + } + + /* + * Insert 2 objects in each folder + */ + objectListing = obsClient.listObjects(bucketName); + for (ObsObject object : objectListing.getObjects()) + { + for (int i = 0; i < 2; i++) + { + String objectKey = object.getObjectKey() + keyPrefix + i; + obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + keys.add(objectKey); + } + } + /* + * Insert 2 objects in root path + */ + obsClient.putObject(bucketName, keyPrefix + 0, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + obsClient.putObject(bucketName, keyPrefix + 1, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + keys.add(keyPrefix + 0); + keys.add(keyPrefix + 1); + System.out.println("Put " + keys.size() + " objects completed."); + System.out.println(); + + /* + * List versions using default parameters, will return up to 1000 objects + */ + System.out.println("List versions using default parameters:\n"); + listVersionResult = obsClient.listVersions(bucketName); + versionsToDelete = listVersionResult.getVersions(); + for (VersionOrDeleteMarker versionOrDeleteMarker : listVersionResult.getVersions()) + { + System.out.println("\t" + versionOrDeleteMarker.getKey() + " etag[" + versionOrDeleteMarker.getEtag() + "]" + " versionid[" + + versionOrDeleteMarker.getVersionId() + "]"); + } + System.out.println(); + + /* + * List all the versions in way of pagination + */ + System.out.println("List all the versions in way of pagination:\n"); + int index = 1; + ListVersionsRequest listRequest = new ListVersionsRequest(bucketName); + listRequest.setMaxKeys(10); + do + { + listVersionResult = obsClient.listVersions(listRequest); + System.out.println("Page:" + index++ + "\n"); + for (VersionOrDeleteMarker versionOrDeleteMarker : listVersionResult.getVersions()) + { + System.out.println("\t" + versionOrDeleteMarker.getKey() + " etag[" + versionOrDeleteMarker.getEtag() + "]" + + " versionid[" + versionOrDeleteMarker.getVersionId() + "]"); + } + listRequest.setKeyMarker(listVersionResult.getNextKeyMarker()); + listRequest.setVersionIdMarker(listVersionResult.getNextVersionIdMarker()); + } while (listVersionResult.isTruncated()); + System.out.println(); + + /* + * List all versions group by folder + */ + System.out.println("List all versions group by folder \n"); + listRequest = new ListVersionsRequest(bucketName); + listRequest.setDelimiter("/"); + listRequest.setMaxKeys(1000); + listVersionResult = obsClient.listVersions(listRequest); + System.out.println("Root path:"); + for (VersionOrDeleteMarker versionOrDeleteMarker : listVersionResult.getVersions()) + { + System.out.println("\t" + versionOrDeleteMarker.getKey() + " etag[" + versionOrDeleteMarker.getEtag() + "]" + " versionid[" + + versionOrDeleteMarker.getVersionId() + "]"); + } + listVersionsByPrefix(listVersionResult); + + System.out.println(); + + /* + * Delete all the objects created + */ + DeleteObjectsRequest request = new DeleteObjectsRequest(); + request.setBucketName(bucketName); + request.setQuiet(false); + index = 0; + KeyAndVersion[] kvs = new KeyAndVersion[versionsToDelete.length]; + for (VersionOrDeleteMarker versionOrDeleteMarker : versionsToDelete) + { + kvs[index++] = new KeyAndVersion(versionOrDeleteMarker.getKey(), versionOrDeleteMarker.getVersionId()); + } + + request.setKeyAndVersions(kvs); + + System.out.println("Delete results:"); + + DeleteObjectsResult deleteObjectsResult = obsClient.deleteObjects(request); + for (DeleteObjectResult object : deleteObjectsResult.getDeletedObjectResults()) + { + System.out.println("\t" + object); + } + + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + private static void listVersionsByPrefix(ListVersionsResult listVersionResult) + throws ObsException + { + for (String prefix : listVersionResult.getCommonPrefixes()) + { + System.out.println("Folder " + prefix + ":"); + ListVersionsRequest request = new ListVersionsRequest(bucketName); + request.setPrefix(prefix); + request.setDelimiter("/"); + request.setMaxKeys(1000); + listVersionResult = obsClient.listVersions(request); + for (VersionOrDeleteMarker versionOrDeleteMarker : listVersionResult.getVersions()) + { + System.out.println("\t" + versionOrDeleteMarker.getKey() + " etag[" + versionOrDeleteMarker.getEtag() + "]" + + " versionid[" + versionOrDeleteMarker.getVersionId() + "]"); + } + listVersionsByPrefix(listVersionResult); + } + } +} diff --git a/app/src/test/java/samples_java/ObjectMetaSample.java b/app/src/test/java/samples_java/ObjectMetaSample.java new file mode 100644 index 0000000..311be77 --- /dev/null +++ b/app/src/test/java/samples_java/ObjectMetaSample.java @@ -0,0 +1,117 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.ObjectMetadata; + +/** + * This sample demonstrates how to set/get self-defined metadata for object + * on OBS using the OBS SDK for Java. + */ +public class ObjectMetaSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + public static void main(String[] args) + throws UnsupportedEncodingException + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + System.out.println("Create a new bucket for demo\n"); + obsClient.createBucket(bucketName); + + ObjectMetadata meta = new ObjectMetadata(); + /* + * Setting object mime type + */ + meta.setContentType("text/plain"); + /* + * Setting self-defined metadata + */ + meta.getMetadata().put("meta1", "value1"); + meta.addUserMetadata("meta2", "value2"); + String content = "Hello OBS"; + obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(content.getBytes("UTF-8")), meta); + System.out.println("Create object " + objectKey + " successfully!\n"); + + /* + * Get object metadata + */ + ObjectMetadata result = obsClient.getObjectMetadata(bucketName, objectKey); + System.out.println("Getting object metadata:"); + System.out.println("\tContentType:" + result.getContentType()); + System.out.println("\tmeta1:" + result.getUserMetadata("meta1")); + System.out.println("\tmeta2:" + result.getUserMetadata("meta2")); + + /* + * Delete object + */ + obsClient.deleteObject(bucketName, objectKey); + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } +} diff --git a/app/src/test/java/samples_java/ObjectOperationsSample.java b/app/src/test/java/samples_java/ObjectOperationsSample.java new file mode 100644 index 0000000..9c5445d --- /dev/null +++ b/app/src/test/java/samples_java/ObjectOperationsSample.java @@ -0,0 +1,184 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.BucketCors; +import com.obs.services.model.BucketCorsRule; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.ObsObject; +import com.obs.services.model.OptionsInfoRequest; + +/** + * This sample demonstrates how to do object-related operations + * (such as create/delete/get/copy object, do object ACL/OPTIONS) + * on OBS using the OBS SDK for Java. + */ +public class ObjectOperationsSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + public static void main(String[] args) + throws IOException + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + obsClient.createBucket(bucketName); + + /* + * Create object + */ + String content = "Hello OBS"; + obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + System.out.println("Create object:" + objectKey + " successfully!\n"); + + /* + * Get object metadata + */ + System.out.println("Getting object metadata"); + ObjectMetadata metadata = obsClient.getObjectMetadata(bucketName, objectKey, null); + System.out.println("\t" + metadata); + + /* + * Get object + */ + System.out.println("Getting object content"); + ObsObject obsObject = obsClient.getObject(bucketName, objectKey, null); + System.out.println("\tobject content:" + ServiceUtils.toString(obsObject.getObjectContent())); + + /* + * Copy object + */ + String sourceBucketName = bucketName; + String destBucketName = bucketName; + String sourceObjectKey = objectKey; + String destObjectKey = objectKey + "-back"; + System.out.println("Copying object\n"); + obsClient.copyObject(sourceBucketName, sourceObjectKey, destBucketName, destObjectKey); + + /* + * Options object + */ + doObjectOptions(); + + /* + * Put/Get object acl operations + */ + doObjectAclOperations(); + + /* + * Delete object + */ + System.out.println("Deleting objects\n"); + obsClient.deleteObject(bucketName, objectKey, null); + obsClient.deleteObject(bucketName, destObjectKey, null); + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + private static void doObjectOptions() + throws ObsException + { + + BucketCors bucketCors = new BucketCors(); + BucketCorsRule rule = new BucketCorsRule(); + rule.getAllowedHeader().add("Authorization"); + rule.getAllowedOrigin().add("http://www.a.com"); + rule.getAllowedOrigin().add("http://www.b.com"); + rule.getExposeHeader().add("x-obs-test1"); + rule.getExposeHeader().add("x-obs-test2"); + rule.setMaxAgeSecond(100); + rule.getAllowedMethod().add("HEAD"); + rule.getAllowedMethod().add("GET"); + rule.getAllowedMethod().add("PUT"); + bucketCors.getRules().add(rule); + obsClient.setBucketCors(bucketName, bucketCors); + + System.out.println("Options object\n"); + OptionsInfoRequest optionInfo = new OptionsInfoRequest(); + optionInfo.setOrigin("http://www.a.com"); + optionInfo.getRequestHeaders().add("Authorization"); + optionInfo.getRequestMethod().add("PUT"); + System.out.println(obsClient.optionsObject(bucketName, objectKey, optionInfo)); + } + + private static void doObjectAclOperations() + throws ObsException + { + System.out.println("Setting object ACL to public-read \n"); + + obsClient.setObjectAcl(bucketName, objectKey, AccessControlList.REST_CANNED_PUBLIC_READ); + + System.out.println("Getting object ACL " + obsClient.getObjectAcl(bucketName, objectKey) + "\n"); + + System.out.println("Setting object ACL to private \n"); + + obsClient.setObjectAcl(bucketName, objectKey, AccessControlList.REST_CANNED_PRIVATE); + + System.out.println("Getting object ACL " + obsClient.getObjectAcl(bucketName, objectKey) + "\n"); + } +} diff --git a/app/src/test/java/samples_java/OefOperationsSample.java b/app/src/test/java/samples_java/OefOperationsSample.java new file mode 100644 index 0000000..c64821d --- /dev/null +++ b/app/src/test/java/samples_java/OefOperationsSample.java @@ -0,0 +1,141 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.IOException; + +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.oef.services.OefClient; +import com.oef.services.model.CompressBean; +import com.oef.services.model.CreateAsyncFetchJobsRequest; +import com.oef.services.model.CreateAsynchFetchJobsResult; +import com.oef.services.model.PutExtensionPolicyRequest; +import com.oef.services.model.QueryExtensionPolicyResult; +import com.oef.services.model.FetchBean; +import com.oef.services.model.QueryAsynchFetchJobsResult; +import com.oef.services.model.TranscodeBean; + +public class OefOperationsSample { + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static OefClient oefClient; + + private static String bucketName = "my-obs-bucket-demo"; + + public static void main(String[] args) + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + oefClient = new OefClient(ak, sk, config); + + /* + * put bucket extension policy + */ + putExtensionPolicy(); + + /* + * get bucket extension policy + */ + queryExtensionPolicy(); + + /* + * delete bucket extension policy + */ + deleteExtensionPolicy(); + + /* + * create asynch fetch job + */ + createFetchJob(); + + /* + * query asynch fetch job + */ + queryFetchJob(); + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (oefClient != null) + { + try + { + /* + * Close obs client + */ + oefClient.close(); + } + catch (IOException e) + { + } + } + } + } + + private static void putExtensionPolicy() { + System.out.println("putExtensionPolicy\n"); + PutExtensionPolicyRequest request = new PutExtensionPolicyRequest(); + request.setFetch(new FetchBean("open","xxx")); + request.setTranscode(new TranscodeBean("closed", "xxx")); + request.setCompress(new CompressBean("open", "xxx")); + System.out.println(request.toString()); + oefClient.putExtensionPolicy(bucketName, request); + } + + private static void queryExtensionPolicy() { + System.out.println("queryExtensionPolicy\n"); + QueryExtensionPolicyResult ret = oefClient.queryExtensionPolicy(bucketName); + System.out.println(ret.toString()); + } + + private static void deleteExtensionPolicy() { + System.out.println("deleteExtensionPolicy\n"); + oefClient.deleteExtensionPolicy(bucketName); + } + + private static void createFetchJob() { + System.out.println("createFetchJob\n"); + CreateAsyncFetchJobsRequest request = new CreateAsyncFetchJobsRequest("https://your-url", bucketName); + System.out.println(request.toString()); + CreateAsynchFetchJobsResult ret = oefClient.createFetchJob(request); + System.out.println(ret.toString()); + } + + private static void queryFetchJob() { + System.out.println("queryFetchJob\n"); + String jobId = "your-jobId"; + QueryAsynchFetchJobsResult ret = oefClient.queryFetchJob(bucketName, jobId); + System.out.println(ret.toString()); + } + +} diff --git a/app/src/test/java/samples_java/PFSBucketAndObjectOperationSample.java b/app/src/test/java/samples_java/PFSBucketAndObjectOperationSample.java new file mode 100644 index 0000000..a7c605b --- /dev/null +++ b/app/src/test/java/samples_java/PFSBucketAndObjectOperationSample.java @@ -0,0 +1,267 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.List; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AccessControlList; +import com.obs.services.model.BucketMetadataInfoRequest; +import com.obs.services.model.BucketMetadataInfoResult; +import com.obs.services.model.BucketTypeEnum; +import com.obs.services.model.CreateBucketRequest; +import com.obs.services.model.ListBucketsRequest; +import com.obs.services.model.ObjectMetadata; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.ObsObject; + +/** + * This sample demonstrates how to do object-related operations + * (such as create/delete/get/copy object, do object ACL/OPTIONS) + * on OBS using the OBS SDK for Java. + */ +public class PFSBucketAndObjectOperationSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static String newObjectKey = "new-my-obs-object-key-demo"; + + private static long newLength = 1024L; + + public static void main(String[] args) + throws IOException + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create PFS bucket + */ + CreateBucketRequest request = new CreateBucketRequest(); + request.setBucketName(bucketName); + request.setBucketType(BucketTypeEnum.PFS); + obsClient.createBucket(request); + + /* + * Head PFS bucket + */ + BucketMetadataInfoRequest bucketMetadataInfoRequest = new BucketMetadataInfoRequest(); + bucketMetadataInfoRequest.setBucketName(bucketName); + BucketMetadataInfoResult bucketMetadataInfoResult = obsClient.getBucketMetadata(bucketMetadataInfoRequest); + System.out.println(bucketMetadataInfoResult); + + /* + * List buckets + */ + doListBucketOperations(); + + /* + * Create object + */ + String content = "Hello OBS"; + obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + System.out.println("Create object:" + objectKey + " successfully!\n"); + + /* + * Get object metadata + */ + System.out.println("Getting object metadata"); + ObjectMetadata metadata = obsClient.getObjectMetadata(bucketName, objectKey, null); + System.out.println("\t" + metadata); + + /* + * Get object + */ + System.out.println("Getting object content"); + ObsObject obsObject = obsClient.getObject(bucketName, objectKey, null); + System.out.println("\tobject content:" + ServiceUtils.toString(obsObject.getObjectContent())); + + /* + * Copy object + */ + String sourceBucketName = bucketName; + String destBucketName = bucketName; + String sourceObjectKey = objectKey; + String destObjectKey = objectKey + "-back"; + System.out.println("Copying object\n"); + obsClient.copyObject(sourceBucketName, sourceObjectKey, destBucketName, destObjectKey); + + /* + * Put/Get object acl operations + */ + doObjectAclOperations(); + + /* + * Modify object operations + */ + doModifyFileOperations(); + + /* + * Rename object operations + */ + doRenameFileOperations(); + + /* + * Truncate object operations + */ + doTruncateFileOperations(); + + /* + * Get object metadata + */ + System.out.println("Getting object metadata"); + metadata = obsClient.getObjectMetadata(bucketName, objectKey, null); + System.out.println("\t" + metadata); + + /* + * Delete object + */ + System.out.println("Deleting objects\n"); + obsClient.deleteObject(bucketName, objectKey, null); + obsClient.deleteObject(bucketName, destObjectKey, null); + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + } + + private static void doListBucketOperations() { + /* + * Create OBJCET bucket + */ + obsClient.createBucket(bucketName + "-object"); + + /* + * List all buckets + */ + ListBucketsRequest request = new ListBucketsRequest(); + List allBuckets = obsClient.listBuckets(request); + System.out.println("----------allBuckets----------\n"); + for (int i = 0; i < allBuckets.size(); i++) { + System.out.println(allBuckets.get(i)); + } + + /* + * List OBJECT buckets + */ + request.setBucketType(BucketTypeEnum.OBJECT); + List objectBuckets = obsClient.listBuckets(request); + System.out.println("-----------objectBuckets---------\n"); + for (int i = 0; i < objectBuckets.size(); i++) { + System.out.println(objectBuckets.get(i)); + } + + /* + * List PFS buckets + */ + request.setBucketType(BucketTypeEnum.PFS); + List PFSBuckets = obsClient.listBuckets(request); + System.out.println("-----------PFSBuckets---------\n"); + for (int i = 0; i < PFSBuckets.size(); i++) { + System.out.println(PFSBuckets.get(i)); + } + } + + private static void doObjectAclOperations() + throws ObsException + { + System.out.println("Setting object ACL to public-read \n"); + + obsClient.setObjectAcl(bucketName, objectKey, AccessControlList.REST_CANNED_PUBLIC_READ); + + System.out.println("Getting object ACL " + obsClient.getObjectAcl(bucketName, objectKey) + "\n"); + + System.out.println("Setting object ACL to private \n"); + + obsClient.setObjectAcl(bucketName, objectKey, AccessControlList.REST_CANNED_PRIVATE); + + System.out.println("Getting object ACL " + obsClient.getObjectAcl(bucketName, objectKey) + "\n"); + } + + private static void doRenameFileOperations() throws ObsException + { + System.out.println("rename " + objectKey + " to " + newObjectKey + "\n"); + + obsClient.renameObject(bucketName, objectKey, newObjectKey); + + System.out.println("rename " + newObjectKey + " to " + objectKey + "\n"); + + obsClient.renameObject(bucketName, newObjectKey, objectKey); + } + + private static void doTruncateFileOperations() throws ObsException + { + System.out.println("truncate " + objectKey + " length to " + newLength + "\n"); + + obsClient.truncateObject(bucketName, objectKey, newLength); + } + + private static void doModifyFileOperations() throws ObsException, IOException + { + System.out.println("modify " + objectKey + " position 0\n"); + + String content = "Hello obs"; + obsClient.modifyObject(bucketName, objectKey, 0, new ByteArrayInputStream(content.getBytes("UTF-8"))); + ObsObject obsObject = obsClient.getObject(bucketName, objectKey, null); + System.out.println("\tobject content:" + ServiceUtils.toString(obsObject.getObjectContent())); + + obsClient.modifyObject(bucketName, objectKey, 9, new ByteArrayInputStream(content.getBytes("UTF-8"))); + obsObject = obsClient.getObject(bucketName, objectKey, null); + System.out.println("\tobject content:" + ServiceUtils.toString(obsObject.getObjectContent())); + } +} \ No newline at end of file diff --git a/app/src/test/java/samples_java/PostObjectSample.java b/app/src/test/java/samples_java/PostObjectSample.java new file mode 100644 index 0000000..aab8dd9 --- /dev/null +++ b/app/src/test/java/samples_java/PostObjectSample.java @@ -0,0 +1,314 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.BufferedReader; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.PostSignatureRequest; +import com.obs.services.model.PostSignatureResponse; + +/** + * This sample demonstrates how to post object under specified bucket from + * OBS using the OBS SDK for Java. + */ +public class PostObjectSample +{ + private static final String protocol = "https://"; + + /* + * Example: obs.cn-north-1.myhuaweicloud.com + */ + private static final String endPoint = "your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static AuthTypeEnum authType = AuthTypeEnum.OBS; + + public static void main(String[] args) + throws IOException + { + ObsConfiguration config = new ObsConfiguration(); + config.setEndPoint(endPoint); + config.setAuthType(authType); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + System.out.println("Create a new bucket for demo\n"); + obsClient.createBucket(bucketName); + + /* + * Create sample file + */ + File sampleFile = createSampleFile(); + + /* + * Claim a post object request + */ + PostSignatureRequest request = new PostSignatureRequest(); + request.setExpires(3600); + + Map formParams = new HashMap(); + + String contentType = "text/plain"; + if(authType == AuthTypeEnum.OBS) { + formParams.put("x-obs-acl", "public-read"); + }else { + formParams.put("acl", "public-read"); + } + formParams.put("content-type", contentType); + + request.setFormParams(formParams); + + PostSignatureResponse response = obsClient.createPostSignature(request); + + + formParams.put("key", objectKey); + formParams.put("policy", response.getPolicy()); + + if(authType == AuthTypeEnum.OBS) { + formParams.put("signature", response.getSignature()); + formParams.put("accesskeyid", ak); + }else { + formParams.put("signature", response.getSignature()); + formParams.put("AwsAccesskeyid", ak); + } + + String postUrl = protocol + bucketName + "." + endPoint; + System.out.println("Creating object in browser-based way"); + System.out.println("\tpost url:" + postUrl); + + String res = formUpload(postUrl, formParams, sampleFile, contentType); + System.out.println("\tresponse:"+ res); + } + catch (Exception ex) + { + if (ex instanceof ObsException) + { + ObsException e = (ObsException)ex; + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + else + { + ex.printStackTrace(); + } + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + } + + private static String formUpload(String postUrl, Map formFields, File sampleFile, String contentType) + { + String res = ""; + HttpURLConnection conn = null; + String boundary = "9431149156168"; + BufferedReader reader = null; + DataInputStream in = null; + OutputStream out = null; + try + { + URL url = new URL(postUrl); + conn = (HttpURLConnection)url.openConnection(); + conn.setConnectTimeout(5000); + conn.setReadTimeout(30000); + conn.setDoOutput(true); + conn.setDoInput(true); + conn.setRequestMethod("POST"); + conn.setRequestProperty("User-Agent", "OBS/Test"); + conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); + out = new DataOutputStream(conn.getOutputStream()); + + // text + if (formFields != null) + { + StringBuffer strBuf = new StringBuffer(); + Iterator> iter = formFields.entrySet().iterator(); + int i = 0; + + while (iter.hasNext()) + { + Entry entry = iter.next(); + String inputName = entry.getKey(); + Object inputValue = entry.getValue(); + + if (inputValue == null) + { + continue; + } + + if (i == 0) + { + strBuf.append("--").append(boundary).append("\r\n"); + strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n"); + strBuf.append(inputValue); + } + else + { + strBuf.append("\r\n").append("--").append(boundary).append("\r\n"); + strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n"); + strBuf.append(inputValue); + } + + i++; + } + out.write(strBuf.toString().getBytes()); + } + + // file + String filename = sampleFile.getName(); + if (contentType == null || contentType.equals("")) + { + contentType = "application/octet-stream"; + } + + StringBuffer strBuf = new StringBuffer(); + strBuf.append("\r\n").append("--").append(boundary).append("\r\n"); + strBuf.append("Content-Disposition: form-data; name=\"file\"; " + "filename=\"" + filename + "\"\r\n"); + strBuf.append("Content-Type: " + contentType + "\r\n\r\n"); + + out.write(strBuf.toString().getBytes()); + + in = new DataInputStream(new FileInputStream(sampleFile)); + int bytes = 0; + byte[] bufferOut = new byte[1024]; + while ((bytes = in.read(bufferOut)) != -1) + { + out.write(bufferOut, 0, bytes); + } + + byte[] endData = ("\r\n--" + boundary + "--\r\n").getBytes(); + out.write(endData); + out.flush(); + + // Read the returned data. + strBuf = new StringBuffer(); + reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); + String line = null; + while ((line = reader.readLine()) != null) + { + strBuf.append(line).append("\n"); + } + res = strBuf.toString(); + } + catch (Exception e) + { + System.out.println("Send post request exception: " + e); + e.printStackTrace(); + } + finally + { + if(out != null){ + try + { + out.close(); + } + catch (IOException e) + { + } + } + + if(in != null){ + try + { + in.close(); + } + catch (IOException e) + { + } + } + if(reader != null){ + try + { + reader.close(); + } + catch (IOException e) + { + } + } + if (conn != null) + { + conn.disconnect(); + conn = null; + } + } + + return res; + } + + private static File createSampleFile() + throws IOException + { + File file = File.createTempFile("obs-java-sdk-", ".txt"); + file.deleteOnExit(); + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + writer.write("abcdefghijklmnopqrstuvwxyz\n"); + writer.write("0123456789011234567890\n"); + writer.close(); + + return file; + } + +} diff --git a/app/src/test/java/samples_java/RestoreObjectSample.java b/app/src/test/java/samples_java/RestoreObjectSample.java new file mode 100644 index 0000000..60e7d36 --- /dev/null +++ b/app/src/test/java/samples_java/RestoreObjectSample.java @@ -0,0 +1,135 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.ObsBucket; +import com.obs.services.model.RestoreObjectRequest; +import com.obs.services.model.RestoreObjectRequest.RestoreObjectStatus; +import com.obs.services.model.RestoreTierEnum; +import com.obs.services.model.StorageClassEnum; + +/** + * This sample demonstrates how to download an cold object + * from OBS using the OBS SDK for Java. + */ +public class RestoreObjectSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-cold-bucket-demo"; + + private static String objectKey = "my-obs-cold-object-key-demo"; + + public static void main(String[] args) throws InterruptedException, IOException + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create a cold bucket + */ + System.out.println("Create a new cold bucket for demo\n"); + ObsBucket bucket = new ObsBucket(); + bucket.setBucketName(bucketName); + bucket.setBucketStorageClass(StorageClassEnum.COLD); + obsClient.createBucket(bucket); + + /* + * Create a cold object + */ + System.out.println("Create a new cold object for demo\n"); + String content = "Hello OBS"; + obsClient.putObject(bucketName, objectKey, new ByteArrayInputStream(content.getBytes("UTF-8")), null); + + /* + * Restore the cold object + */ + System.out.println("Restore the cold object"); + RestoreObjectRequest restoreObjectRequest = new RestoreObjectRequest(bucketName, objectKey,null, 1, RestoreTierEnum.EXPEDITED); + System.out.println("\t"+(obsClient.restoreObject(restoreObjectRequest) == RestoreObjectStatus.INPROGRESS)); + + /* + * Wait 6 minute to get the object + */ + Thread.sleep(60 * 6 * 1000); + + /* + * Get the cold object status + */ + System.out.println("Get the cold object status"); + restoreObjectRequest = new RestoreObjectRequest(bucketName, objectKey,null, 1, RestoreTierEnum.EXPEDITED); + System.out.println("\t"+(obsClient.restoreObject(restoreObjectRequest) == RestoreObjectStatus.AVALIABLE) + "\n"); + + /* + * Get the cold object + */ + System.out.println("Get the cold object"); + System.out.println("\tcontent:" + ServiceUtils.toString(obsClient.getObject(bucketName, objectKey, null).getObjectContent())); + + + /* + * Delete the cold object + */ + obsClient.deleteObject(bucketName, objectKey, null); + + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } +} diff --git a/app/src/test/java/samples_java/SimpleMultipartUploadSample.java b/app/src/test/java/samples_java/SimpleMultipartUploadSample.java new file mode 100644 index 0000000..df1d568 --- /dev/null +++ b/app/src/test/java/samples_java/SimpleMultipartUploadSample.java @@ -0,0 +1,144 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.UUID; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.CompleteMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadRequest; +import com.obs.services.model.InitiateMultipartUploadResult; +import com.obs.services.model.PartEtag; +import com.obs.services.model.UploadPartResult; + + +/** + * This sample demonstrates how to upload multiparts to OBS + * using the OBS SDK for Java. + */ +public class SimpleMultipartUploadSample +{ + private static final String endPoint = "https://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + public static void main(String[] args) throws IOException + { + ObsConfiguration config = new ObsConfiguration(); + config.setSocketTimeout(30000); + config.setConnectionTimeout(10000); + config.setEndPoint(endPoint); + try + { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + obsClient.createBucket(bucketName); + + /* + * Step 1: initiate multipart upload + */ + System.out.println("Step 1: initiate multipart upload \n"); + InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(); + request.setBucketName(bucketName); + request.setObjectKey(objectKey); + InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request); + + /* + * Step 2: upload a part + */ + System.out.println("Step 2: upload part \n"); + UploadPartResult uploadPartResult = obsClient.uploadPart(bucketName, objectKey, result.getUploadId(), 1, new FileInputStream(createSampleFile())); + + /* + * Step 3: complete multipart upload + */ + System.out.println("Step 3: complete multipart upload \n"); + CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(); + completeMultipartUploadRequest.setBucketName(bucketName); + completeMultipartUploadRequest.setObjectKey(objectKey); + completeMultipartUploadRequest.setUploadId(result.getUploadId()); + PartEtag partEtag = new PartEtag(); + partEtag.setPartNumber(uploadPartResult.getPartNumber()); + partEtag.setEtag(uploadPartResult.getEtag()); + completeMultipartUploadRequest.getPartEtag().add(partEtag); + obsClient.completeMultipartUpload(completeMultipartUploadRequest); + } + catch (ObsException e) + { + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } + finally + { + if (obsClient != null) + { + try + { + /* + * Close obs client + */ + obsClient.close(); + } + catch (IOException e) + { + } + } + } + + } + + private static File createSampleFile() + throws IOException + { + File file = File.createTempFile("obs-java-sdk-", ".txt"); + file.deleteOnExit(); + + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); + for (int i = 0; i < 1000000; i++) + { + writer.write(UUID.randomUUID() + "\n"); + writer.write(UUID.randomUUID() + "\n"); + } + writer.flush(); + writer.close(); + + return file; + } +} diff --git a/app/src/test/java/samples_java/TemporarySignatureSample.java b/app/src/test/java/samples_java/TemporarySignatureSample.java new file mode 100644 index 0000000..2996bbd --- /dev/null +++ b/app/src/test/java/samples_java/TemporarySignatureSample.java @@ -0,0 +1,289 @@ +/** + * Copyright 2019 Huawei Technologies Co.,Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use + * this file except in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package samples_java; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import com.obs.services.ObsClient; +import com.obs.services.ObsConfiguration; +import com.obs.services.exception.ObsException; +import com.obs.services.internal.ObsConvertor; +import com.obs.services.internal.ServiceException; +import com.obs.services.internal.utils.ServiceUtils; +import com.obs.services.model.AuthTypeEnum; +import com.obs.services.model.BucketCors; +import com.obs.services.model.BucketCorsRule; +import com.obs.services.model.HttpMethodEnum; +import com.obs.services.model.SpecialParamEnum; +import com.obs.services.model.TemporarySignatureRequest; +import com.obs.services.model.TemporarySignatureResponse; + +import okhttp3.Call; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +/** + * This sample demonstrates how to do common operations in temporary signature + * way on OBS using the OBS SDK for Java. + */ +public class TemporarySignatureSample { + private static final String endPoint = "http://your-endpoint"; + + private static final String ak = "*** Provide your Access Key ***"; + + private static final String sk = "*** Provide your Secret Key ***"; + + private static ObsClient obsClient; + + private static String bucketName = "my-obs-bucket-demo"; + + private static String objectKey = "my-obs-object-key-demo"; + + private static OkHttpClient httpClient = new OkHttpClient.Builder().followRedirects(false) + .retryOnConnectionFailure(false).cache(null).build(); + + + public static void main(String[] args) { + ObsConfiguration config = new ObsConfiguration(); + config.setEndPoint(endPoint); + config.setAuthType(AuthTypeEnum.OBS); + try { + /* + * Constructs a obs client instance with your account for accessing OBS + */ + obsClient = new ObsClient(ak, sk, config); + + /* + * Create bucket + */ + doCreateBucket(); + + /* + * Set/Get/Delete bucket cors + */ + doBucketCorsOperations(); + + /* + * Create object + */ + doCreateObject(); + + /* + * Get object + */ + doGetObject(); + + /* + * Set/Get object acl + */ + doObjectAclOperations(); + + /* + * Delete object + */ + doDeleteObject(); + + /* + * Delete bucket + */ + doDeleteBucket(); + + } catch (Exception ex) { + if (ex instanceof ObsException) { + ObsException e = (ObsException) ex; + System.out.println("Response Code: " + e.getResponseCode()); + System.out.println("Error Message: " + e.getErrorMessage()); + System.out.println("Error Code: " + e.getErrorCode()); + System.out.println("Request ID: " + e.getErrorRequestId()); + System.out.println("Host ID: " + e.getErrorHostId()); + } else { + ex.printStackTrace(); + } + } finally { + if (obsClient != null) { + try { + /* + * Close obs client + */ + obsClient.close(); + } catch (IOException e) { + } + } + } + + } + + private static void getResponse(Request request) throws IOException { + Call c = httpClient.newCall(request); + Response res = c.execute(); + System.out.println("\tStatus:" + res.code()); + if (res.body() != null) { + String content = res.body().string(); + if (content == null || content.trim().equals("")) { + System.out.println("\n"); + } else { + System.out.println("\tContent:" + content + "\n\n"); + } + } else { + System.out.println("\n"); + } + res.close(); + } + + private static Request.Builder getBuilder(TemporarySignatureResponse res) { + Request.Builder builder = new Request.Builder(); + for (Map.Entry entry : res.getActualSignedRequestHeaders().entrySet()) { + builder.header(entry.getKey(), entry.getValue()); + } + return builder.url(res.getSignedUrl()); + } + + private static void doDeleteObject() throws IOException { + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.DELETE, 300); + req.setBucketName(bucketName); + req.setObjectKey(objectKey); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + System.out.println("Deleting object using temporary signature url:"); + System.out.println("\t" + res.getSignedUrl()); + + getResponse(getBuilder(res).delete().build()); + } + + private static void doObjectAclOperations() throws ObsException, IOException { + + Map headers = new HashMap(); + headers.put("x-obs-acl", "public-read"); + + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.PUT, 300); + req.setBucketName(bucketName); + req.setObjectKey(objectKey); + req.setHeaders(headers); + req.setSpecialParam(SpecialParamEnum.ACL); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + + System.out.println("Setting object ACL to public-read using temporary signature url:"); + System.out.println("\t" + res.getSignedUrl()); + Request.Builder builder = getBuilder(res); + builder.url(res.getSignedUrl()).put(RequestBody.create(null, "".getBytes("UTF-8"))); + getResponse(builder.build()); + + TemporarySignatureRequest req2 = new TemporarySignatureRequest(HttpMethodEnum.GET, 300); + req2.setBucketName(bucketName); + req2.setObjectKey(objectKey); + req2.setSpecialParam(SpecialParamEnum.ACL); + TemporarySignatureResponse res2 = obsClient.createTemporarySignature(req2); + System.out.println("Getting object ACL using temporary signature url:"); + System.out.println("\t" + res2.getSignedUrl()); + getResponse(getBuilder(res2).get().build()); + } + + private static void doGetObject() throws ObsException, IOException { + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.GET, 300); + req.setBucketName(bucketName); + req.setObjectKey(objectKey); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + System.out.println("Getting object using temporary signature url:"); + System.out.println("\t" + res.getSignedUrl()); + getResponse(getBuilder(res).get().build()); + } + + private static void doCreateObject() throws ObsException, IOException { + Map headers = new HashMap(); + String contentType = "text/plain"; + headers.put("Content-Type", contentType); + + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.PUT, 300); + req.setBucketName(bucketName); + req.setObjectKey(objectKey); + req.setHeaders(headers); + + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + + System.out.println("Createing object using temporary signature url:"); + System.out.println("\t" + res.getSignedUrl()); + Request.Builder builder = getBuilder(res); + builder.put(RequestBody.create(MediaType.parse(contentType), "Hello OBS".getBytes("UTF-8"))); + getResponse(builder.build()); + } + + private static void doBucketCorsOperations() throws ObsException, IOException, ServiceException { + BucketCors bucketCors = new BucketCors(); + BucketCorsRule rule = new BucketCorsRule(); + rule.getAllowedHeader().add("Authorization"); + rule.getAllowedOrigin().add("http://www.a.com"); + rule.getAllowedOrigin().add("http://www.b.com"); + rule.getExposeHeader().add("x-obs-test1"); + rule.getExposeHeader().add("x-obs-test2"); + rule.setMaxAgeSecond(100); + rule.getAllowedMethod().add("HEAD"); + rule.getAllowedMethod().add("GET"); + rule.getAllowedMethod().add("PUT"); + bucketCors.getRules().add(rule); + + String requestXml = ObsConvertor.getInstance().transBucketCors(bucketCors); + String requestXmlMd5 = ServiceUtils.computeMD5(requestXml); + + Map headers = new HashMap(); + + headers.put("Content-Type", "application/xml"); + headers.put("Content-MD5", requestXmlMd5); + + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.PUT, 300); + req.setBucketName(bucketName); + req.setHeaders(headers); + req.setSpecialParam(SpecialParamEnum.CORS); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + + System.out.println("Setting bucket CORS using temporary signature url:"); + System.out.println("\t" + res.getSignedUrl()); + + Request.Builder builder = getBuilder(res); + builder.put(RequestBody.create(MediaType.parse("application/xml"), requestXml.getBytes("UTF-8"))); + getResponse(builder.build()); + + TemporarySignatureRequest req2 = new TemporarySignatureRequest(HttpMethodEnum.GET, 300); + req2.setBucketName(bucketName); + req2.setSpecialParam(SpecialParamEnum.CORS); + TemporarySignatureResponse res2 = obsClient.createTemporarySignature(req2); + System.out.println("Getting bucket CORS using temporary signature url:"); + System.out.println("\t" + res2.getSignedUrl()); + getResponse(getBuilder(res2).get().build()); + } + + private static void doDeleteBucket() throws ObsException, IOException { + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.DELETE, 300); + req.setBucketName(bucketName); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + System.out.println("Deleting bucket using temporary signature url:"); + System.out.println("\t" + res.getSignedUrl()); + getResponse(getBuilder(res).delete().build()); + } + + private static void doCreateBucket() throws ObsException, IOException { + TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.PUT, 300); + req.setBucketName(bucketName); + TemporarySignatureResponse res = obsClient.createTemporarySignature(req); + System.out.println("Creating bucket using temporary signature url:"); + System.out.println("\t" + res.getSignedUrl()); + String location = "your-location"; + String content = "" +location+ ""; + getResponse(getBuilder(res).put(RequestBody.create(null, content.getBytes("UTF-8"))).build()); + } + +} diff --git a/app/src/test/resource/test_data.properties b/app/src/test/resource/test_data.properties new file mode 100644 index 0000000..53088cc --- /dev/null +++ b/app/src/test/resource/test_data.properties @@ -0,0 +1,10 @@ +environment.endpoint=http://obs.xxxxxxxxxxxxxx +environment.location=xxxxxxxxxxxxxxxx +environment.dns=http://xxxxxxxxxxxxx +environment.ak=xxxxxxx +environment.sk=xxxxxxxx +environment.authType=OBS +bucketPrefix=xxxxxxxxxxxxx- +beforeBucket=xxxxxxxxxxxxx +kmsID=xxxxxxxxxxxxxxxxx +isPosix=true \ No newline at end of file diff --git a/assembly-android.xml b/assembly-android.xml index e1aece6..7b7ba8d 100644 --- a/assembly-android.xml +++ b/assembly-android.xml @@ -1,36 +1,72 @@ - assembly_zip - - zip - - - false - - - ${project.build.directory}/${project.artifactId}-${project.version}-javadoc.jar - / - - - ${project.basedir}/app/build/intermediates/bundles/release/classes.jar - / - ${project.artifactId}-${project.version}.jar - - - ${project.basedir}/README.MD - / - - - - - ${project.basedir}/app/src/test/java/samples_android - /samples_android - - - ${project.build.directory}/doc - /doc - - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> + assembly_zip + + zip + + + false + + + + true + /libs + false + runtime + + + + + + + + + + ${project.build.directory}/${project.artifactId}-${project.version}-javadoc.jar + / + esdk-obs-android-${project.version}-javadoc.jar + + + + ${project.basedir}/LICENSE + / + + + ${project.basedir}/Notice.MD + / + + + ${project.basedir}/Help on License + / + + + + + ${project.basedir}/app/src/test/java/samples_android + /samples_android + + + ${project.basedir}/app/src/third_part + /third_part + + + ${project.build.directory}/doc + /doc + + + ${project.basedir}/app + /source + + src/test/** + build/** + .gitignore + build.gradle + proguard-rules.pro + + + \ No newline at end of file diff --git a/assembly-java.xml b/assembly-java.xml index d6cbecc..d94d9c5 100644 --- a/assembly-java.xml +++ b/assembly-java.xml @@ -1,72 +1,110 @@ - assembly_zip - - zip - - - false - - - true - /libs - false - runtime - - com.jamesmurty.utils:java-xmlbuilder - - - - - - ${project.build.directory}/${project.artifactId}-${project.version}-javadoc.jar - / - esdk-obs-java-${project.version}-javadoc.jar - - - ${project.build.directory}/${project.artifactId}-${project.version}.jar - / - esdk-obs-java-${project.version}.jar - - - ${project.basedir}/README.MD - / - - - ${project.basedir}/app/src/main/resource/log4j2.xml - / - - - ${project.basedir}/pom-dependencies.xml - / - pom.xml - - - ${project.basedir}/Notice.MD - / - - - - - ${project.basedir}/app/src/test/java/samples_java - /samples_java - - - ${project.build.directory}/doc - /doc - - - ${project.basedir}/app - /source - - src/test/** - build/** - .gitignore - build.gradle - proguard-rules.pro + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> + assembly_zip + + zip + + + false + + + + true + /libs + false + runtime + + com.jamesmurty.utils:java-xmlbuilder - - + + + + + + ${project.build.directory}/${project.artifactId}-${project.version}-javadoc.jar + / + esdk-obs-java-${project.version}-javadoc.jar + + + ${project.basedir}/app/src/main/resource/log4j2.xml + / + + + ${project.basedir}/assembly-android.xml + / + + + ${project.basedir}/assembly-java.xml + / + + + ${project.basedir}/pom-android.xml + / + + + ${project.basedir}/pom-dependencies.xml + / + + + ${project.basedir}/pom-java-optimization.xml + / + + + ${project.basedir}/pom-java.xml + / + + + ${project.basedir}/LICENSE + / + + + ${project.basedir}/Notice.MD + / + + + ${project.basedir}/Help on License + / + + + + ${project.basedir}/README-Java.md + / + + + + ${project.basedir}/README-Android.md + / + + + + + ${project.basedir}/app/src/test/java/samples_java + /samples_java + + + ${project.basedir}/app/src/test/java/samples_android + /samples_android + + + ${project.basedir}/app/src/third_part + /third_part + + + ${project.build.directory}/doc + /doc + + + ${project.basedir}/app + /source + + src/test/** + build/** + .gitignore + build.gradle + proguard-rules.pro + + + \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..cc4fdc2 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..b5a514e --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..9d82f78 --- /dev/null +++ b/gradlew @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/log4j2.xml b/log4j2.xml new file mode 100644 index 0000000..92646e0 --- /dev/null +++ b/log4j2.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom-android.xml b/pom-android.xml index 5f10edd..2f5f503 100644 --- a/pom-android.xml +++ b/pom-android.xml @@ -2,23 +2,24 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.huawei.storage + com.huaweicloud esdk-obs-android - 3.19.12 + 3.22.3 + jar OBS SDK for Android The OBS SDK for Android used for accessing Object Storage Service UTF-8 + true - com.jamesmurty.utils java-xmlbuilder - 1.2 + 1.3 net.iharder @@ -26,59 +27,38 @@ - com.squareup.okhttp3 okhttp - 3.11.0 + 4.9.3 com.squareup.okio okio - 1.17.2 + 2.10.0 com.fasterxml.jackson.core jackson-core - 2.10.0 + 2.13.0 com.fasterxml.jackson.core jackson-databind - 2.10.0 + 2.13.0 com.fasterxml.jackson.core jackson-annotations - 2.10.0 + 2.13.0 - - app/src/main/java + app/src/main/java/com app/src/test/java - - org.codehaus.mojo - exec-maven-plugin - 1.2 - - - package - - exec - - - ${basedir}/gradlew.bat - - build - - - - - org.apache.maven.plugins @@ -97,9 +77,9 @@ org.apache.maven.plugins maven-compiler-plugin - 1.7 - 1.7 UTF-8 + 1.8 + 1.8 @@ -112,11 +92,10 @@ maven-javadoc-plugin 3.0.0-M1 - 1.7 - 1.7 + 1.8 UTF-8 UTF-8 - *.internal.*:*.log:*.proxy + *.internal.*:*.log:*.proxy:okio ${project.build.directory}/doc @@ -129,23 +108,39 @@ + + + org.apache.maven.plugins + maven-jar-plugin + + + okio/* + + + + org.apache.maven.plugins maven-assembly-plugin + + + + com.test.MainClassName + + + + jar-with-dependencies + + - install + make-assembly + package single - - false - - assembly-android.xml - - diff --git a/pom-dependencies.xml b/pom-dependencies.xml index fe30fe8..45310b4 100644 --- a/pom-dependencies.xml +++ b/pom-dependencies.xml @@ -4,7 +4,7 @@ com.huawei.storage esdk-obs-java - 3.19.12 + 3.21.4.1 jar OBS SDK for Java @@ -18,7 +18,7 @@ com.jamesmurty.utils java-xmlbuilder - 1.2 + 1.3 net.iharder @@ -26,42 +26,43 @@ + com.squareup.okhttp3 okhttp - 3.11.0 + 3.14.9 com.squareup.okio okio - 1.17.2 + 1.17.5 com.fasterxml.jackson.core jackson-core - 2.10.0 + 2.11.1 com.fasterxml.jackson.core jackson-databind - 2.10.0 + 2.11.1 com.fasterxml.jackson.core jackson-annotations - 2.10.0 + 2.11.1 org.apache.logging.log4j log4j-core - 2.12.0 + 2.13.2 org.apache.logging.log4j log4j-api - 2.12.0 + 2.13.2 diff --git a/pom-java-optimization.xml b/pom-java-optimization.xml index 0d7ec8b..ec65507 100644 --- a/pom-java-optimization.xml +++ b/pom-java-optimization.xml @@ -2,9 +2,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.huawei.storage - esdk-obs-java - optimization-3.19.12 + com.huaweicloud + esdk-obs-java-optimised + 3.22.3 jar OBS SDK for Java @@ -18,7 +18,7 @@ com.jamesmurty.utils java-xmlbuilder - 1.2 + 1.3 net.iharder @@ -29,50 +29,51 @@ com.squareup.okhttp3 okhttp - 3.11.0 + 3.14.9 com.squareup.okio okio - 1.17.2 + 1.17.5 com.fasterxml.jackson.core jackson-core - 2.10.0 + 2.13.0 com.fasterxml.jackson.core jackson-databind - 2.10.0 + 2.13.0 com.fasterxml.jackson.core jackson-annotations - 2.10.0 + 2.13.0 org.apache.logging.log4j log4j-core - 2.12.0 + 2.17.1 org.apache.logging.log4j log4j-api - 2.12.0 + 2.17.1 - app/src/main/java + app/src/test/java @@ -94,8 +95,8 @@ maven-compiler-plugin UTF-8 - 1.7 - 1.7 + 1.8 + 1.8 @@ -103,7 +104,51 @@ + + + org.apache.maven.plugins maven-shade-plugin @@ -146,4 +191,4 @@ - \ No newline at end of file + diff --git a/pom-java.xml b/pom-java.xml index 78a19d0..b04984c 100644 --- a/pom-java.xml +++ b/pom-java.xml @@ -1,193 +1,221 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.huawei.storage - esdk-obs-java - 3.19.12 - jar + com.huaweicloud + esdk-obs-java + 3.22.3 + jar - OBS SDK for Java - The OBS SDK for Java used for accessing Object Storage Service + OBS SDK for Java + The OBS SDK for Java used for accessing Object Storage Service + + + UTF-8 + + + + + com.jamesmurty.utils + java-xmlbuilder + 1.3 + + + net.iharder + base64 + + + + + com.squareup.okhttp3 + okhttp + 4.9.3 + + + com.squareup.okio + okio + 2.10.0 + + + + com.fasterxml.jackson.core + jackson-core + 2.13.0 + + + com.fasterxml.jackson.core + jackson-databind + 2.13.0 + + + com.fasterxml.jackson.core + jackson-annotations + 2.13.0 + + + + org.apache.logging.log4j + log4j-core + 2.17.1 + + + org.apache.logging.log4j + log4j-api + 2.17.1 + - - UTF-8 - - - - com.jamesmurty.utils - java-xmlbuilder - 1.2 - - - net.iharder - base64 - - - - - com.squareup.okhttp3 - okhttp - 3.11.0 - - - com.squareup.okio - okio - 1.17.2 - - - - com.fasterxml.jackson.core - jackson-core - 2.10.0 - - - com.fasterxml.jackson.core - jackson-databind - 2.10.0 - - - com.fasterxml.jackson.core - jackson-annotations - 2.10.0 - - - - org.apache.logging.log4j - log4j-core - 2.12.0 - - org.apache.logging.log4j - log4j-api - 2.12.0 - - - + org.mockito + mockito-core + 1.10.19 + test + + + + org.powermock + powermock-api-mockito + 1.6.5 + test + + + org.powermock + powermock-module-junit4 + 1.6.5 + test + + + + + app/src/main/java + + app/src/test/java + + + org.apache.maven.plugins + maven-clean-plugin + + + + logs + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + UTF-8 + 1.8 + 1.8 + + -Xlint:deprecation + -Xlint:unchecked + + + + + compile + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.0.1 + + 1.8 + UTF-8 + UTF-8 + none + *.internal.*:*.log:*.proxy:okio + ${project.build.directory}/doc + + + + attach-javadocs + install + + jar + + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + okio/* + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + install + + single + + + + + false + + assembly-java.xml + + + - - app/src/main/java - - - app/src/main/resource - - - app/src/test/java - - - org.apache.maven.plugins - maven-clean-plugin - - - - logs - - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.7 - 1.7 - UTF-8 - - - - compile - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.0.0-M1 - - 1.7 - 1.7 - UTF-8 - UTF-8 - *.internal.*:*.log:*.proxy:okio - ${project.build.directory}/doc - - - - attach-javadocs - install - - jar - - - - - - - org.apache.maven.plugins - maven-jar-plugin - - - okio/* - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - install - - single - - - - - false - - assembly-java.xml - - - - - - org.apache.maven.plugins - maven-shade-plugin - 3.2.1 - - - package - - shade - - - - - com.squareup.okhttp3:okhttp:jar: - com.squareup.okio:okio:jar: - com.fasterxml.jackson.core:jackson-core:jar: - com.fasterxml.jackson.core:jackson-databind:jar: - com.fasterxml.jackson.core:jackson-annotations:jar: - org.apache.logging.log4j:log4j-core:jar: - org.apache.logging.log4j:log4j-api:jar: - - - - - com.jamesmurty.utils - com.obs.jamesmurty.utils - - - - - - - - - + + org.apache.maven.plugins + maven-shade-plugin + 3.2.1 + + + package + + shade + + + + + com.squareup.okhttp3:okhttp:jar: + com.fasterxml.jackson.core:jackson-core:jar: + com.fasterxml.jackson.core:jackson-databind:jar: + com.fasterxml.jackson.core:jackson-annotations:jar: + org.jetbrains.kotlin:kotlin-stdlib:jar: + org.jetbrains:annotations:jar: + org.jetbrains.kotlin:kotlin-stdlib-common:jar: + com.squareup.okio:okio:jar: + org.apache.logging.log4j:log4j-core:jar: + org.apache.logging.log4j:log4j-api:jar: + + + + + com.jamesmurty.utils + com.obs.shade.jamesmurty.utils + + + + + + + + diff --git a/pom.xml b/pom.xml index 3aeb7a5..ba02b74 100644 --- a/pom.xml +++ b/pom.xml @@ -1,24 +1,24 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.huawei.storage - esdk-obs-java - 3.19.12 - jar + com.huaweicloud + esdk-obs-java + 3.22.3 + jar - OBS SDK for Java - The OBS SDK for Java used for accessing Object Storage Service + OBS SDK for Java + The OBS SDK for Java used for accessing Object Storage Service - - UTF-8 - + + UTF-8 + - + com.jamesmurty.utils java-xmlbuilder - 1.2 + 1.3 net.iharder @@ -26,47 +26,190 @@ - + com.squareup.okhttp3 okhttp - 3.11.0 + 4.9.3 com.squareup.okio okio - 1.17.2 + 2.10.0 com.fasterxml.jackson.core jackson-core - 2.10.0 + 2.13.0 com.fasterxml.jackson.core jackson-databind - 2.10.0 + 2.13.4.1 com.fasterxml.jackson.core jackson-annotations - 2.10.0 + 2.13.0 - + org.apache.logging.log4j log4j-core - 2.12.0 + 2.17.1 org.apache.logging.log4j log4j-api - 2.12.0 + 2.17.1 + + + + org.powermock + powermock-module-junit4 + 1.6.5 + test - + + org.powermock + powermock-api-mockito + 1.6.5 + test + + + + + app/src/main/java + + app/src/test/java + + + org.apache.maven.plugins + maven-clean-plugin + + + + logs + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + UTF-8 + 1.8 + 1.8 + + -Xlint:deprecation + -Xlint:unchecked + + + + + compile + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.0.1 + + 1.8 + UTF-8 + UTF-8 + none + *.internal.*:*.log:*.proxy:okio + ${project.build.directory}/doc + + + + attach-javadocs + install + + jar + + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + okio/* + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + install + + single + + + + + false + + assembly-java.xml + + + - - + + org.apache.maven.plugins + maven-shade-plugin + 3.2.1 + + + package + + shade + + + + + com.squareup.okhttp3:okhttp:jar: + com.fasterxml.jackson.core:jackson-core:jar: + com.fasterxml.jackson.core:jackson-databind:jar: + com.fasterxml.jackson.core:jackson-annotations:jar: + org.jetbrains.kotlin:kotlin-stdlib:jar: + org.jetbrains:annotations:jar: + org.jetbrains.kotlin:kotlin-stdlib-common:jar: + com.squareup.okio:okio:jar: + org.apache.logging.log4j:log4j-core:jar: + org.apache.logging.log4j:log4j-api:jar: + + + + + com.jamesmurty.utils + com.obs.shade.jamesmurty.utils + + + + + + + + diff --git a/release/huaweicloud-obs-sdk-java_3.19.11.1.tar.gz b/release/huaweicloud-obs-sdk-java_3.19.11.1.tar.gz deleted file mode 100644 index 8650f80..0000000 Binary files a/release/huaweicloud-obs-sdk-java_3.19.11.1.tar.gz and /dev/null differ diff --git a/release/huaweicloud-obs-sdk-java_3.19.11.1.tar.gz.sha256 b/release/huaweicloud-obs-sdk-java_3.19.11.1.tar.gz.sha256 deleted file mode 100644 index 46fc41a..0000000 --- a/release/huaweicloud-obs-sdk-java_3.19.11.1.tar.gz.sha256 +++ /dev/null @@ -1 +0,0 @@ -9c7d95d45e68a4fed9786a976fda786f7972324ab977325cb21de8f1a2997dec *huaweicloud-obs-sdk-java_3.19.11.1.tar.gz diff --git a/release/huaweicloud-obs-sdk-java_3.19.11.tar.gz b/release/huaweicloud-obs-sdk-java_3.19.11.tar.gz deleted file mode 100644 index 2715723..0000000 Binary files a/release/huaweicloud-obs-sdk-java_3.19.11.tar.gz and /dev/null differ diff --git a/release/huaweicloud-obs-sdk-java_3.19.11.tar.gz.sha256.txt b/release/huaweicloud-obs-sdk-java_3.19.11.tar.gz.sha256.txt deleted file mode 100644 index 24476c7..0000000 --- a/release/huaweicloud-obs-sdk-java_3.19.11.tar.gz.sha256.txt +++ /dev/null @@ -1 +0,0 @@ -239506dba0e801b708be1ee54ad4662419a549466d82b4e893a329a89ea3d34b *huaweicloud-obs-sdk-java_3.19.11.tar.gz diff --git a/release/huaweicloud-obs-sdk-java_3.19.12.tar.gz b/release/huaweicloud-obs-sdk-java_3.19.12.tar.gz deleted file mode 100644 index 44ced02..0000000 Binary files a/release/huaweicloud-obs-sdk-java_3.19.12.tar.gz and /dev/null differ diff --git a/release/huaweicloud-obs-sdk-java_3.19.12.tar.gz.sha256 b/release/huaweicloud-obs-sdk-java_3.19.12.tar.gz.sha256 deleted file mode 100644 index 7068011..0000000 --- a/release/huaweicloud-obs-sdk-java_3.19.12.tar.gz.sha256 +++ /dev/null @@ -1 +0,0 @@ -323f2f48f0e61b34d503934a3e4c1bf58b87a2a01996888f2748bcc8b5ba5d23 *huaweicloud-obs-sdk-java_3.19.12.tar.gz diff --git a/release/huaweicloud-obs-sdk-java_3.19.5.2.zip b/release/huaweicloud-obs-sdk-java_3.19.5.2.zip deleted file mode 100644 index c7481cf..0000000 Binary files a/release/huaweicloud-obs-sdk-java_3.19.5.2.zip and /dev/null differ diff --git a/release/huaweicloud-obs-sdk-java_3.19.5.3.zip b/release/huaweicloud-obs-sdk-java_3.19.5.3.zip deleted file mode 100644 index 5e46e09..0000000 Binary files a/release/huaweicloud-obs-sdk-java_3.19.5.3.zip and /dev/null differ diff --git a/release/huaweicloud-obs-sdk-java_3.19.5.zip b/release/huaweicloud-obs-sdk-java_3.19.5.zip deleted file mode 100644 index e2c9c31..0000000 Binary files a/release/huaweicloud-obs-sdk-java_3.19.5.zip and /dev/null differ diff --git a/release/huaweicloud-obs-sdk-java_3.19.7.1.tar.gz b/release/huaweicloud-obs-sdk-java_3.19.7.1.tar.gz deleted file mode 100644 index 7895102..0000000 Binary files a/release/huaweicloud-obs-sdk-java_3.19.7.1.tar.gz and /dev/null differ diff --git a/release/huaweicloud-obs-sdk-java_3.19.7.3.tar.gz b/release/huaweicloud-obs-sdk-java_3.19.7.3.tar.gz deleted file mode 100644 index bb4ce8a..0000000 Binary files a/release/huaweicloud-obs-sdk-java_3.19.7.3.tar.gz and /dev/null differ diff --git a/release/huaweicloud-obs-sdk-java_3.19.7.3.tar.gz.sha256 b/release/huaweicloud-obs-sdk-java_3.19.7.3.tar.gz.sha256 deleted file mode 100644 index 3d825c0..0000000 --- a/release/huaweicloud-obs-sdk-java_3.19.7.3.tar.gz.sha256 +++ /dev/null @@ -1 +0,0 @@ -72157cae61fd1dca5bafa81454d0a23bdf4078744a950a20029e2cf4b559c6d4 *huaweicloud-obs-sdk-java_3.19.7.3.tar.gz diff --git a/release/huaweicloud-obs-sdk-java_3.19.7.zip b/release/huaweicloud-obs-sdk-java_3.19.7.zip deleted file mode 100644 index b3a3711..0000000 Binary files a/release/huaweicloud-obs-sdk-java_3.19.7.zip and /dev/null differ diff --git a/release/huaweicloud-obs-sdk-java_3.19.9.tar.gz b/release/huaweicloud-obs-sdk-java_3.19.9.tar.gz deleted file mode 100644 index 2173e62..0000000 Binary files a/release/huaweicloud-obs-sdk-java_3.19.9.tar.gz and /dev/null differ diff --git a/release/huaweicloud-obs-sdk-java_3.19.9.tar.gz.sha256 b/release/huaweicloud-obs-sdk-java_3.19.9.tar.gz.sha256 deleted file mode 100644 index adc415c..0000000 --- a/release/huaweicloud-obs-sdk-java_3.19.9.tar.gz.sha256 +++ /dev/null @@ -1 +0,0 @@ -3575b950b8540be48365f8647693caa1f687541e9c32034434208d582fd274c5 *huaweicloud-obs-sdk-java_3.19.9.tar.gz diff --git a/release/readme.md b/release/readme.md new file mode 100644 index 0000000..0e48831 --- /dev/null +++ b/release/readme.md @@ -0,0 +1 @@ +### Download Url:https://github.com/huaweicloud/huaweicloud-sdk-java-obs/tree/release/release diff --git a/test/test.txt b/test/test.txt deleted file mode 100644 index e69de29..0000000