Skip to content

Commit 9c39c6e

Browse files
zhuxiaolong37huiguangjun
authored andcommitted
add WriteGetObjectResponse (#495)
1 parent f0453c5 commit 9c39c6e

File tree

9 files changed

+318
-24
lines changed

9 files changed

+318
-24
lines changed

src/main/java/com/aliyun/oss/OSS.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5184,4 +5184,20 @@ public UdfApplicationLog getUdfApplicationLog(GetUdfApplicationLogRequest getUdf
51845184
* If any errors occurred in OSS while processing the request.
51855185
*/
51865186
public AsyncProcessObjectResult asyncProcessObject(AsyncProcessObjectRequest asyncProcessObjectRequest) throws OSSException, ClientException;
5187+
5188+
/**
5189+
* write get object response.
5190+
*
5191+
* @param writeGetObjectResponseRequest
5192+
*
5193+
* @return A {@link VoidResult} instance wrapped void return and
5194+
* contains some basic response options, such as requestId.
5195+
*
5196+
* @throws OSSException
5197+
* If any errors are encountered in the client while making the
5198+
* request or handling the response.
5199+
* @throws ClientException
5200+
* If any errors occurred in OSS while processing the request.
5201+
*/
5202+
public VoidResult writeGetObjectResponse(WriteGetObjectResponseRequest writeGetObjectResponseRequest) throws OSSException, ClientException;
51875203
}

src/main/java/com/aliyun/oss/OSSClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,11 @@ public AsyncProcessObjectResult asyncProcessObject(AsyncProcessObjectRequest asy
19821982
return this.objectOperation.asyncProcessObject(asyncProcessObjectRequest);
19831983
}
19841984

1985+
@Override
1986+
public VoidResult writeGetObjectResponse(WriteGetObjectResponseRequest writeGetObjectResponseRequest) throws OSSException, ClientException {
1987+
return this.objectOperation.writeGetObjectResponse(writeGetObjectResponseRequest);
1988+
}
1989+
19851990
@Override
19861991
public void shutdown() {
19871992
try {

src/main/java/com/aliyun/oss/internal/OSSHeaders.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,9 @@ public interface OSSHeaders extends HttpHeaders {
117117
static final String OSS_CONTENT_SHA256 = "x-oss-content-sha256";
118118

119119
static final String OSS_ERROR = "x-oss-err";
120+
121+
static final String OSS_REQUEST_ROUTE = "x-oss-request-route";
122+
static final String OSS_REQUEST_TOKEN = "x-oss-request-token";
123+
static final String OSS_FWD_STATUS = "x-oss-fwd-status";
124+
120125
}

src/main/java/com/aliyun/oss/internal/OSSObjectOperation.java

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,8 @@
3232
import static com.aliyun.oss.internal.OSSConstants.DEFAULT_BUFFER_SIZE;
3333
import static com.aliyun.oss.internal.OSSConstants.DEFAULT_CHARSET_NAME;
3434
import static com.aliyun.oss.internal.OSSHeaders.OSS_SELECT_OUTPUT_RAW;
35-
import static com.aliyun.oss.internal.OSSUtils.OSS_RESOURCE_MANAGER;
36-
import static com.aliyun.oss.internal.OSSUtils.addDateHeader;
37-
import static com.aliyun.oss.internal.OSSUtils.addHeader;
38-
import static com.aliyun.oss.internal.OSSUtils.addStringListHeader;
39-
import static com.aliyun.oss.internal.OSSUtils.determineInputStreamLength;
40-
import static com.aliyun.oss.internal.OSSUtils.ensureBucketNameValid;
41-
import static com.aliyun.oss.internal.OSSUtils.ensureObjectKeyValid;
42-
import static com.aliyun.oss.internal.OSSUtils.ensureCallbackValid;
43-
import static com.aliyun.oss.internal.OSSUtils.joinETags;
44-
import static com.aliyun.oss.internal.OSSUtils.populateRequestMetadata;
45-
import static com.aliyun.oss.internal.OSSUtils.populateResponseHeaderParameters;
46-
import static com.aliyun.oss.internal.OSSUtils.populateRequestCallback;
47-
import static com.aliyun.oss.internal.OSSUtils.removeHeader;
48-
import static com.aliyun.oss.internal.OSSUtils.safeCloseResponse;
49-
import static com.aliyun.oss.internal.RequestParameters.ENCODING_TYPE;
50-
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_ACL;
51-
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_DELETE;
52-
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_OBJECTMETA;
53-
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_SYMLINK;
54-
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_TAGGING;
55-
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_DIR;
56-
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_RENAME;
57-
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_DIR_DELETE;
35+
import static com.aliyun.oss.internal.OSSUtils.*;
36+
import static com.aliyun.oss.internal.RequestParameters.*;
5837
import static com.aliyun.oss.internal.ResponseParsers.appendObjectResponseParser;
5938
import static com.aliyun.oss.internal.ResponseParsers.copyObjectResponseParser;
6039
import static com.aliyun.oss.internal.ResponseParsers.deleteObjectsResponseParser;
@@ -1312,6 +1291,76 @@ public AsyncProcessObjectResult asyncProcessObject(AsyncProcessObjectRequest asy
13121291
return doOperation(request, ResponseParsers.asyncProcessObjectResponseParser, bucketName, key, true);
13131292
}
13141293

1294+
public VoidResult writeGetObjectResponse(WriteGetObjectResponseRequest writeGetObjectResponseRequest) throws OSSException, ClientException {
1295+
1296+
assertParameterNotNull(writeGetObjectResponseRequest, "writeGetObjectResponseRequest");
1297+
assertParameterNotNull(writeGetObjectResponseRequest.getRoute(), "route");
1298+
1299+
ObjectMetadata metadata = writeGetObjectResponseRequest.getMetadata();
1300+
Map<String, String> params = new HashMap<String, String>();
1301+
params.put(WRITE_GET_OBJECT_RESPONSE, null);
1302+
1303+
if (metadata == null) {
1304+
metadata = new ObjectMetadata();
1305+
}
1306+
1307+
Map<String, String> headers = writeGetObjectResponseRequest.getHeaders();
1308+
addHeaderIfNotNull(headers, OSSHeaders.OSS_REQUEST_ROUTE, writeGetObjectResponseRequest.getRoute());
1309+
addHeaderIfNotNull(headers, OSSHeaders.OSS_REQUEST_TOKEN, writeGetObjectResponseRequest.getToken());
1310+
addHeaderIfNotNull(headers, OSSHeaders.OSS_FWD_STATUS, String.valueOf(writeGetObjectResponseRequest.getStatus()));
1311+
populateRequestMetadata(headers, metadata);
1312+
1313+
1314+
InputStream originalInputStream = writeGetObjectResponseRequest.getInputStream();
1315+
InputStream repeatableInputStream = null;
1316+
if (writeGetObjectResponseRequest.getFile() != null) {
1317+
File toUpload = writeGetObjectResponseRequest.getFile();
1318+
1319+
if (!checkFile(toUpload)) {
1320+
getLog().info("Illegal file path: " + toUpload.getPath());
1321+
throw new ClientException("Illegal file path: " + toUpload.getPath());
1322+
}
1323+
1324+
metadata.setContentLength(toUpload.length());
1325+
1326+
try {
1327+
repeatableInputStream = new RepeatableFileInputStream(toUpload);
1328+
} catch (IOException ex) {
1329+
logException("Cannot locate file to upload: ", ex);
1330+
throw new ClientException("Cannot locate file to upload: ", ex);
1331+
}
1332+
} else {
1333+
assertTrue(originalInputStream != null, "Please specify input stream or file to upload");
1334+
try {
1335+
metadata.setContentLength(Long.valueOf(originalInputStream.available()));
1336+
repeatableInputStream = newRepeatableInputStream(originalInputStream);
1337+
} catch (IOException ex) {
1338+
logException("Cannot wrap to repeatable input stream: ", ex);
1339+
throw new ClientException("Cannot wrap to repeatable input stream: ", ex);
1340+
}
1341+
}
1342+
1343+
Long contentLength = (Long) metadata.getRawMetadata().get(OSSHeaders.CONTENT_LENGTH);
1344+
contentLength = contentLength == null ? -1 : contentLength.longValue();
1345+
1346+
if (contentLength < 0 || !repeatableInputStream.markSupported()) {
1347+
contentLength = Long.valueOf(-1);
1348+
}
1349+
1350+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(OSSUtils.toEndpointURI(writeGetObjectResponseRequest.getRoute(), this.client.getClientConfiguration().getProtocol().toString()))
1351+
.setMethod(HttpMethod.POST).setParameters(params).setHeaders(headers)
1352+
.setInputStream(repeatableInputStream).setInputSize(contentLength)
1353+
.setOriginalRequest(writeGetObjectResponseRequest).build();
1354+
1355+
return doOperation(request, requestIdResponseParser, null, null, true);
1356+
}
1357+
1358+
private static void addHeaderIfNotNull(Map<String, String> headers, String header, String value) {
1359+
if (value != null) {
1360+
headers.put(header, value);
1361+
}
1362+
}
1363+
13151364
private static void addDeleteObjectsRequiredHeaders(Map<String, String> headers, byte[] rawContent) {
13161365
headers.put(HttpHeaders.CONTENT_LENGTH, String.valueOf(rawContent.length));
13171366

src/main/java/com/aliyun/oss/internal/RequestParameters.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,6 @@ public final class RequestParameters {
158158
public static final String SUBRESOURCE_REGION_LIST = "regionList";
159159
public static final String REGIONS = "regions";
160160
public static final String X_OSS_ASYNC_PROCESS = "x-oss-async-process";
161+
public static final String WRITE_GET_OBJECT_RESPONSE = "x-oss-write-get-object-response";
161162

162163
}

src/main/java/com/aliyun/oss/internal/SignParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ public class SignParameters {
4343
SUBRESOURCE_WORM_ID, SUBRESOURCE_WORM_EXTEND, SUBRESOURCE_CALLBACK, SUBRESOURCE_CALLBACK_VAR,
4444
SUBRESOURCE_DIR, SUBRESOURCE_RENAME, SUBRESOURCE_DIR_DELETE, SUBRESOURCE_TRANSFER_ACCELERATION,
4545
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,
46-
SUBRESOURCE_REGION_LIST, X_OSS_ASYNC_PROCESS});
46+
SUBRESOURCE_REGION_LIST, X_OSS_ASYNC_PROCESS, WRITE_GET_OBJECT_RESPONSE});
4747

4848
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.aliyun.oss.model;
2+
3+
import java.io.File;
4+
import java.io.InputStream;
5+
6+
public class WriteGetObjectResponseRequest extends WebServiceRequest{
7+
8+
private String route;
9+
private String token;
10+
private int status;
11+
private File file;
12+
private InputStream inputStream;
13+
private ObjectMetadata metadata;
14+
15+
public WriteGetObjectResponseRequest(String route, String token, int status, File file) {
16+
this(route, token, status, file, null);
17+
}
18+
19+
public WriteGetObjectResponseRequest(String route, String token, int status, File file, ObjectMetadata metadata) {
20+
this.route = route;
21+
this.token = token;
22+
this.status = status;
23+
this.file = file;
24+
this.metadata = metadata;
25+
}
26+
27+
public WriteGetObjectResponseRequest(String route, String token, int status, InputStream input) {
28+
this(route, token, status, input, null);
29+
}
30+
31+
public WriteGetObjectResponseRequest(String route, String token, int status, InputStream input, ObjectMetadata metadata) {
32+
this.route = route;
33+
this.token = token;
34+
this.status = status;
35+
this.inputStream = input;
36+
this.metadata = metadata;
37+
}
38+
39+
public String getRoute() {
40+
return route;
41+
}
42+
43+
public void setRoute(String route) {
44+
this.route = route;
45+
}
46+
47+
public WriteGetObjectResponseRequest withRoute(String route) {
48+
this.route = route;
49+
return this;
50+
}
51+
52+
public String getToken() {
53+
return token;
54+
}
55+
56+
public void setToken(String token) {
57+
this.token = token;
58+
}
59+
60+
public WriteGetObjectResponseRequest withToken(String token) {
61+
this.token = token;
62+
return this;
63+
}
64+
65+
public int getStatus() {
66+
return status;
67+
}
68+
69+
public void setStatus(int status) {
70+
this.status = status;
71+
}
72+
73+
public WriteGetObjectResponseRequest withStatus(int status) {
74+
this.status = status;
75+
return this;
76+
}
77+
78+
public File getFile() {
79+
return file;
80+
}
81+
82+
public void setFile(File file) {
83+
this.file = file;
84+
}
85+
86+
public WriteGetObjectResponseRequest withFile(File file) {
87+
this.file = file;
88+
return this;
89+
}
90+
91+
public InputStream getInputStream() {
92+
return inputStream;
93+
}
94+
95+
public void setInputStream(InputStream inputStream) {
96+
this.inputStream = inputStream;
97+
}
98+
99+
public WriteGetObjectResponseRequest withInputStream(InputStream inputStream) {
100+
this.inputStream = inputStream;
101+
return this;
102+
}
103+
104+
public ObjectMetadata getMetadata() {
105+
return metadata;
106+
}
107+
108+
public void setMetadata(ObjectMetadata metadata) {
109+
this.metadata = metadata;
110+
}
111+
112+
public WriteGetObjectResponseRequest withMetadata(ObjectMetadata metadata) {
113+
this.metadata = metadata;
114+
return this;
115+
}
116+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package samples;
2+
3+
import com.aliyun.oss.model.*;
4+
import java.io.ByteArrayInputStream;
5+
import java.io.InputStream;
6+
7+
public class WriteGetObjectResponseSample {
8+
private static String endpoint = "*** Provide OSS endpoint ***";
9+
private static String accessKeyId = "*** Provide your AccessKeyId ***";
10+
private static String accessKeySecret = "*** Provide your AccessKeySecret ***";
11+
private static String bucketName = "*** Provide bucket name ***";
12+
13+
public static void main(String[] args) throws InterruptedException {
14+
15+
/*
16+
* Constructs a client instance with your account for accessing OSS
17+
*/
18+
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
19+
20+
try {
21+
final int instreamLength = 128 * 1024;
22+
InputStream instream = null;
23+
String route = "test-ap-process-name-128364***6515-opap.oss-cn-beijing-internal.oss-object-process.aliyuncs.com";
24+
String token = "OSSV1#UMoA43+Bi9b6Q1Lu6UjhLXnmq4I/wIFac3uZfBkgJtg2xtHkZJ4bZglDWyOgWRlGTrA8y/i6D9eH8PmAiq2NL2R/MD/UX6zvRhT8WMHUewgc9QWPs9LPHiZytkUZnGa39mnv/73cyPWTuxgxyk4dNhlzEE6U7PdzmCCu8gIrjuYLPrA9psRn0ZC8J2/DCZGVx0BE7AmIJTcNtLKTSjxsJyTts******";
25+
int status = 200;
26+
27+
instream = genFixedLengthInputStream(instreamLength);
28+
WriteGetObjectResponseRequest writeGetObjectResponseRequest = new WriteGetObjectResponseRequest(route, token, status, instream);
29+
// add CommonHeader
30+
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Accept-Ranges", "*** Provide your Accept Ranges ***");
31+
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Cache-Control", "*** Provide your Cache Control ***");
32+
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Content-Disposition", "*** Provide your Content Disposition ***");
33+
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Content-Encoding", "*** Provide your Content Encoding ***");
34+
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Content-Range", "*** Provide your Content Range ***");
35+
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Content-Type", "*** Provide your Content Type ***");
36+
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-ETag", "*** Provide your ETag ***");
37+
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Expires", "*** Provide your Expires ***");
38+
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Last-Modified", "*** Provide your Last Modified ***");
39+
// writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-Location", "*** Provide your Location ***");
40+
41+
ObjectMetadata metadata = new ObjectMetadata();
42+
metadata.setContentLength(instreamLength);
43+
writeGetObjectResponseRequest.setMetadata(metadata);
44+
45+
ossClient.writeGetObjectResponse(writeGetObjectResponseRequest);
46+
} catch (OSSException oe) {
47+
System.out.println("Caught an OSSException, which means your request made it to OSS, "
48+
+ "but was rejected with an error response for some reason.");
49+
System.out.println("Error Message: " + oe.getMessage());
50+
System.out.println("Error Code: " + oe.getErrorCode());
51+
System.out.println("Request ID: " + oe.getRequestId());
52+
System.out.println("Host ID: " + oe.getHostId());
53+
} catch (ClientException ce) {
54+
System.out.println("Caught an ClientException, which means the client encountered "
55+
+ "a serious internal problem while trying to communicate with OSS, "
56+
+ "such as not being able to access the network.");
57+
System.out.println("Error Message: " + ce.getMessage());
58+
} finally {
59+
/*
60+
* Do not forget to shut down the client finally to release all allocated resources.
61+
*/
62+
ossClient.shutdown();
63+
}
64+
}
65+
66+
public static InputStream genFixedLengthInputStream(long fixedLength) {
67+
byte[] buf = new byte[(int) fixedLength];
68+
for (int i = 0; i < buf.length; i++)
69+
buf[i] = 'a';
70+
return new ByteArrayInputStream(buf);
71+
}
72+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.aliyun.oss.integrationtests;
2+
3+
import com.aliyun.oss.ClientException;
4+
import com.aliyun.oss.model.*;
5+
import junit.framework.Assert;
6+
import org.junit.Test;
7+
import java.io.InputStream;
8+
import static com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream;
9+
10+
public class WriteGetObjectResponseTest extends TestBase{
11+
12+
@Test
13+
public void testWriteGetObjectResponse() {
14+
final int instreamLength = 128 * 1024;
15+
InputStream instream = null;
16+
String route = "test-ap-process-name-12836***16515-opap.oss-cn-beijing-internal.oss-object-process.aliyuncs.com";
17+
String token = "OSSV1#UMoA43+Bi9b6Q1Lu6UjhLXnmq4I/wIFac3uZfBkgJtg2xtHkZJ4bZglDWyOgWRlGTrA8y/i6D9eH8PmAiq2NL2R/MD/UX6zvRhT8WMHUewgc9QWPs9LPHiZytkUZnGa39mnv/73cyPWTuxgxyk4dNhlzEE6U7PdzmCCu8gIrjuYLPrA9psRn0ZC8J2/DCZGVx0BE7AmIJTcNtLKTSjxsJyTts/******";
18+
int status = 200;
19+
20+
try {
21+
instream = genFixedLengthInputStream(instreamLength);
22+
WriteGetObjectResponseRequest writeGetObjectResponseRequest = new WriteGetObjectResponseRequest(route, token, status, instream);
23+
writeGetObjectResponseRequest.addHeader("x-oss-fwd-header-ETag", "D41D8CD98F00B204E9800998ECF8****");
24+
ossClient.writeGetObjectResponse(writeGetObjectResponseRequest);
25+
} catch (ClientException e) {
26+
Assert.assertEquals(e.getErrorCode(), "UnknownHost");
27+
e.printStackTrace();
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)