/**
* 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.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;
import java.io.*;
import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
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<String, String> headers;
private Map<String, String> params;
private RequestBody body;
TransResult(Map<String, String> headers) {
this(headers, null, null);
}
TransResult(Map<String, String> headers, RequestBody body) {
this(headers, null, body);
}
TransResult(Map<String, String> headers, Map<String, String> params, RequestBody body) {
this.headers = headers;
this.params = params;
this.body = body;
}
Map<String, String> getHeaders() {
if (this.headers == null) {
headers = new HashMap<String, String>();
}
return this.headers;
}
Map<String, String> getParams() {
if (this.params == null) {
params = new HashMap<String, String>();
}
return this.params;
}
}
protected HeaderResponse setBucketVersioningImpl(String bucketName, VersioningStatusEnum status)
throws ServiceException {
Map<String, String> requestParams = new HashMap<String, String>();
requestParams.put(SpecialParamEnum.VERSIONING.getOriginalStringCode(), "");
Map<String, String> metadata = new HashMap<String, String>();
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<String, String> requestParams = new HashMap<String, String>();
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<String, String> params = new HashMap<String, String>();
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<String, String> headers = new HashMap<String, String>();
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<VersionOrDeleteMarker> 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<String, String> requestParameters = new HashMap<String, String>();
requestParameters.put(SpecialParamEnum.POLICY.getOriginalStringCode(), "");
Response response = performRestGet(buc