Skip to content

Commit

Permalink
add WriteGetObjectResponse (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiaolong37 authored and huiguangjun committed Oct 30, 2023
1 parent f0453c5 commit 9c39c6e
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 24 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/aliyun/oss/OSS.java
Original file line number Diff line number Diff line change
Expand Up @@ -5184,4 +5184,20 @@ public UdfApplicationLog getUdfApplicationLog(GetUdfApplicationLogRequest getUdf
* If any errors occurred in OSS while processing the request.
*/
public AsyncProcessObjectResult asyncProcessObject(AsyncProcessObjectRequest asyncProcessObjectRequest) throws OSSException, ClientException;

/**
* write get object response.
*
* @param writeGetObjectResponseRequest
*
* @return A {@link VoidResult} instance wrapped void return and
* contains some basic response options, such as requestId.
*
* @throws OSSException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws ClientException
* If any errors occurred in OSS while processing the request.
*/
public VoidResult writeGetObjectResponse(WriteGetObjectResponseRequest writeGetObjectResponseRequest) throws OSSException, ClientException;
}
5 changes: 5 additions & 0 deletions src/main/java/com/aliyun/oss/OSSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,11 @@ public AsyncProcessObjectResult asyncProcessObject(AsyncProcessObjectRequest asy
return this.objectOperation.asyncProcessObject(asyncProcessObjectRequest);
}

@Override
public VoidResult writeGetObjectResponse(WriteGetObjectResponseRequest writeGetObjectResponseRequest) throws OSSException, ClientException {
return this.objectOperation.writeGetObjectResponse(writeGetObjectResponseRequest);
}

@Override
public void shutdown() {
try {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/aliyun/oss/internal/OSSHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,9 @@ public interface OSSHeaders extends HttpHeaders {
static final String OSS_CONTENT_SHA256 = "x-oss-content-sha256";

static final String OSS_ERROR = "x-oss-err";

static final String OSS_REQUEST_ROUTE = "x-oss-request-route";
static final String OSS_REQUEST_TOKEN = "x-oss-request-token";
static final String OSS_FWD_STATUS = "x-oss-fwd-status";

}
95 changes: 72 additions & 23 deletions src/main/java/com/aliyun/oss/internal/OSSObjectOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,8 @@
import static com.aliyun.oss.internal.OSSConstants.DEFAULT_BUFFER_SIZE;
import static com.aliyun.oss.internal.OSSConstants.DEFAULT_CHARSET_NAME;
import static com.aliyun.oss.internal.OSSHeaders.OSS_SELECT_OUTPUT_RAW;
import static com.aliyun.oss.internal.OSSUtils.OSS_RESOURCE_MANAGER;
import static com.aliyun.oss.internal.OSSUtils.addDateHeader;
import static com.aliyun.oss.internal.OSSUtils.addHeader;
import static com.aliyun.oss.internal.OSSUtils.addStringListHeader;
import static com.aliyun.oss.internal.OSSUtils.determineInputStreamLength;
import static com.aliyun.oss.internal.OSSUtils.ensureBucketNameValid;
import static com.aliyun.oss.internal.OSSUtils.ensureObjectKeyValid;
import static com.aliyun.oss.internal.OSSUtils.ensureCallbackValid;
import static com.aliyun.oss.internal.OSSUtils.joinETags;
import static com.aliyun.oss.internal.OSSUtils.populateRequestMetadata;
import static com.aliyun.oss.internal.OSSUtils.populateResponseHeaderParameters;
import static com.aliyun.oss.internal.OSSUtils.populateRequestCallback;
import static com.aliyun.oss.internal.OSSUtils.removeHeader;
import static com.aliyun.oss.internal.OSSUtils.safeCloseResponse;
import static com.aliyun.oss.internal.RequestParameters.ENCODING_TYPE;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_ACL;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_DELETE;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_OBJECTMETA;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_SYMLINK;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_TAGGING;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_DIR;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_RENAME;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_DIR_DELETE;
import static com.aliyun.oss.internal.OSSUtils.*;
import static com.aliyun.oss.internal.RequestParameters.*;
import static com.aliyun.oss.internal.ResponseParsers.appendObjectResponseParser;
import static com.aliyun.oss.internal.ResponseParsers.copyObjectResponseParser;
import static com.aliyun.oss.internal.ResponseParsers.deleteObjectsResponseParser;
Expand Down Expand Up @@ -1312,6 +1291,76 @@ public AsyncProcessObjectResult asyncProcessObject(AsyncProcessObjectRequest asy
return doOperation(request, ResponseParsers.asyncProcessObjectResponseParser, bucketName, key, true);
}

public VoidResult writeGetObjectResponse(WriteGetObjectResponseRequest writeGetObjectResponseRequest) throws OSSException, ClientException {

assertParameterNotNull(writeGetObjectResponseRequest, "writeGetObjectResponseRequest");
assertParameterNotNull(writeGetObjectResponseRequest.getRoute(), "route");

ObjectMetadata metadata = writeGetObjectResponseRequest.getMetadata();
Map<String, String> params = new HashMap<String, String>();
params.put(WRITE_GET_OBJECT_RESPONSE, null);

if (metadata == null) {
metadata = new ObjectMetadata();
}

Map<String, String> headers = writeGetObjectResponseRequest.getHeaders();
addHeaderIfNotNull(headers, OSSHeaders.OSS_REQUEST_ROUTE, writeGetObjectResponseRequest.getRoute());
addHeaderIfNotNull(headers, OSSHeaders.OSS_REQUEST_TOKEN, writeGetObjectResponseRequest.getToken());
addHeaderIfNotNull(headers, OSSHeaders.OSS_FWD_STATUS, String.valueOf(writeGetObjectResponseRequest.getStatus()));
populateRequestMetadata(headers, metadata);


InputStream originalInputStream = writeGetObjectResponseRequest.getInputStream();
InputStream repeatableInputStream = null;
if (writeGetObjectResponseRequest.getFile() != null) {
File toUpload = writeGetObjectResponseRequest.getFile();

if (!checkFile(toUpload)) {
getLog().info("Illegal file path: " + toUpload.getPath());
throw new ClientException("Illegal file path: " + toUpload.getPath());
}

metadata.setContentLength(toUpload.length());

try {
repeatableInputStream = new RepeatableFileInputStream(toUpload);
} catch (IOException ex) {
logException("Cannot locate file to upload: ", ex);
throw new ClientException("Cannot locate file to upload: ", ex);
}
} else {
assertTrue(originalInputStream != null, "Please specify input stream or file to upload");
try {
metadata.setContentLength(Long.valueOf(originalInputStream.available()));
repeatableInputStream = newRepeatableInputStream(originalInputStream);
} catch (IOException ex) {
logException("Cannot wrap to repeatable input stream: ", ex);
throw new ClientException("Cannot wrap to repeatable input stream: ", ex);
}
}

Long contentLength = (Long) metadata.getRawMetadata().get(OSSHeaders.CONTENT_LENGTH);
contentLength = contentLength == null ? -1 : contentLength.longValue();

if (contentLength < 0 || !repeatableInputStream.markSupported()) {
contentLength = Long.valueOf(-1);
}

RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(OSSUtils.toEndpointURI(writeGetObjectResponseRequest.getRoute(), this.client.getClientConfiguration().getProtocol().toString()))
.setMethod(HttpMethod.POST).setParameters(params).setHeaders(headers)
.setInputStream(repeatableInputStream).setInputSize(contentLength)
.setOriginalRequest(writeGetObjectResponseRequest).build();

return doOperation(request, requestIdResponseParser, null, null, true);
}

private static void addHeaderIfNotNull(Map<String, String> headers, String header, String value) {
if (value != null) {
headers.put(header, value);
}
}

private static void addDeleteObjectsRequiredHeaders(Map<String, String> headers, byte[] rawContent) {
headers.put(HttpHeaders.CONTENT_LENGTH, String.valueOf(rawContent.length));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,6 @@ public final class RequestParameters {
public static final String SUBRESOURCE_REGION_LIST = "regionList";
public static final String REGIONS = "regions";
public static final String X_OSS_ASYNC_PROCESS = "x-oss-async-process";
public static final String WRITE_GET_OBJECT_RESPONSE = "x-oss-write-get-object-response";

}
2 changes: 1 addition & 1 deletion src/main/java/com/aliyun/oss/internal/SignParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public class SignParameters {
SUBRESOURCE_WORM_ID, SUBRESOURCE_WORM_EXTEND, SUBRESOURCE_CALLBACK, SUBRESOURCE_CALLBACK_VAR,
SUBRESOURCE_DIR, SUBRESOURCE_RENAME, SUBRESOURCE_DIR_DELETE, SUBRESOURCE_TRANSFER_ACCELERATION,
X_OSS_AC_SOURCE_IP, X_OSS_AC_SUBNET_MASK, X_OSS_AC_VPC_ID, X_OSS_AC_FORWARD_ALLOW, META_QUERY, SUBRESOURCE_RESOURCE_GROUP,
SUBRESOURCE_REGION_LIST, X_OSS_ASYNC_PROCESS});
SUBRESOURCE_REGION_LIST, X_OSS_ASYNC_PROCESS, WRITE_GET_OBJECT_RESPONSE});

}
116 changes: 116 additions & 0 deletions src/main/java/com/aliyun/oss/model/WriteGetObjectResponseRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.aliyun.oss.model;

import java.io.File;
import java.io.InputStream;

public class WriteGetObjectResponseRequest extends WebServiceRequest{

private String route;
private String token;
private int status;
private File file;
private InputStream inputStream;
private ObjectMetadata metadata;

public WriteGetObjectResponseRequest(String route, String token, int status, File file) {
this(route, token, status, file, null);
}

public WriteGetObjectResponseRequest(String route, String token, int status, File file, ObjectMetadata metadata) {
this.route = route;
this.token = token;
this.status = status;
this.file = file;
this.metadata = metadata;
}

public WriteGetObjectResponseRequest(String route, String token, int status, InputStream input) {
this(route, token, status, input, null);
}

public WriteGetObjectResponseRequest(String route, String token, int status, InputStream input, ObjectMetadata metadata) {
this.route = route;
this.token = token;
this.status = status;
this.inputStream = input;
this.metadata = metadata;
}

public String getRoute() {
return route;
}

public void setRoute(String route) {
this.route = route;
}

public WriteGetObjectResponseRequest withRoute(String route) {
this.route = route;
return this;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public WriteGetObjectResponseRequest withToken(String token) {
this.token = token;
return this;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public WriteGetObjectResponseRequest withStatus(int status) {
this.status = status;
return this;
}

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

public WriteGetObjectResponseRequest withFile(File file) {
this.file = file;
return this;
}

public InputStream getInputStream() {
return inputStream;
}

public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}

public WriteGetObjectResponseRequest withInputStream(InputStream inputStream) {
this.inputStream = inputStream;
return this;
}

public ObjectMetadata getMetadata() {
return metadata;
}

public void setMetadata(ObjectMetadata metadata) {
this.metadata = metadata;
}

public WriteGetObjectResponseRequest withMetadata(ObjectMetadata metadata) {
this.metadata = metadata;
return this;
}
}
72 changes: 72 additions & 0 deletions src/samples/WriteGetObjectResponseSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package samples;

import com.aliyun.oss.model.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class WriteGetObjectResponseSample {
private static String endpoint = "*** Provide OSS endpoint ***";
private static String accessKeyId = "*** Provide your AccessKeyId ***";
private static String accessKeySecret = "*** Provide your AccessKeySecret ***";
private static String bucketName = "*** Provide bucket name ***";

public static void main(String[] args) throws InterruptedException {

/*
* Constructs a client instance with your account for accessing OSS
*/
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

try {
final int instreamLength = 128 * 1024;
InputStream instream = null;
String route = "test-ap-process-name-128364***6515-opap.oss-cn-beijing-internal.oss-object-process.aliyuncs.com";
String token = "OSSV1#UMoA43+Bi9b6Q1Lu6UjhLXnmq4I/wIFac3uZfBkgJtg2xtHkZJ4bZglDWyOgWRlGTrA8y/i6D9eH8PmAiq2NL2R/MD/UX6zvRhT8WMHUewgc9QWPs9LPHiZytkUZnGa39mnv/73cyPWTuxgxyk4dNhlzEE6U7PdzmCCu8gIrjuYLPrA9psRn0ZC8J2/DCZGVx0BE7AmIJTcNtLKTSjxsJyTts******";
int status = 200;

instream = genFixedLengthInputStream(instreamLength);
WriteGetObjectResponseRequest writeGetObjectResponseRequest = new WriteGetObjectResponseRequest(route, token, status, instream);
// add CommonHeader
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Accept-Ranges", "*** Provide your Accept Ranges ***");
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Cache-Control", "*** Provide your Cache Control ***");
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Content-Disposition", "*** Provide your Content Disposition ***");
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Content-Encoding", "*** Provide your Content Encoding ***");
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Content-Range", "*** Provide your Content Range ***");
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Content-Type", "*** Provide your Content Type ***");
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-ETag", "*** Provide your ETag ***");
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Expires", "*** Provide your Expires ***");
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Last-Modified", "*** Provide your Last Modified ***");
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Location", "*** Provide your Location ***");

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(instreamLength);
writeGetObjectResponseRequest.setMetadata(metadata);

ossClient.writeGetObjectResponse(writeGetObjectResponseRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getMessage());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
ossClient.shutdown();
}
}

public static InputStream genFixedLengthInputStream(long fixedLength) {
byte[] buf = new byte[(int) fixedLength];
for (int i = 0; i < buf.length; i++)
buf[i] = 'a';
return new ByteArrayInputStream(buf);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.aliyun.oss.integrationtests;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.model.*;
import junit.framework.Assert;
import org.junit.Test;
import java.io.InputStream;
import static com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream;

public class WriteGetObjectResponseTest extends TestBase{

@Test
public void testWriteGetObjectResponse() {
final int instreamLength = 128 * 1024;
InputStream instream = null;
String route = "test-ap-process-name-12836***16515-opap.oss-cn-beijing-internal.oss-object-process.aliyuncs.com";
String token = "OSSV1#UMoA43+Bi9b6Q1Lu6UjhLXnmq4I/wIFac3uZfBkgJtg2xtHkZJ4bZglDWyOgWRlGTrA8y/i6D9eH8PmAiq2NL2R/MD/UX6zvRhT8WMHUewgc9QWPs9LPHiZytkUZnGa39mnv/73cyPWTuxgxyk4dNhlzEE6U7PdzmCCu8gIrjuYLPrA9psRn0ZC8J2/DCZGVx0BE7AmIJTcNtLKTSjxsJyTts/******";
int status = 200;

try {
instream = genFixedLengthInputStream(instreamLength);
WriteGetObjectResponseRequest writeGetObjectResponseRequest = new WriteGetObjectResponseRequest(route, token, status, instream);
writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-ETag", "D41D8CD98F00B204E9800998ECF8****");
ossClient.writeGetObjectResponse(writeGetObjectResponseRequest);
} catch (ClientException e) {
Assert.assertEquals(e.getErrorCode(), "UnknownHost");
e.printStackTrace();
}
}
}

0 comments on commit 9c39c6e

Please sign in to comment.