Skip to content

Commit fe18726

Browse files
committed
Merge pull request #20 from shipunyc/master
ResourceResponse automatically getResource and close
2 parents ec66388 + 5318cf3 commit fe18726

File tree

3 files changed

+47
-54
lines changed

3 files changed

+47
-54
lines changed

src/com/microsoft/azure/documentdb/DocumentServiceResponse.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ public Map<String, String> getResponseHeaders() {
5454
}
5555

5656
public String getReponseBodyAsString() {
57+
if (this.httpEntity == null) {
58+
this.close();
59+
return null;
60+
}
61+
5762
try {
58-
String responseBody = EntityUtils.toString(this.httpEntity, StandardCharsets.UTF_8);
59-
return responseBody;
63+
return EntityUtils.toString(this.httpEntity, StandardCharsets.UTF_8);
6064
} catch (ParseException | IOException e) {
6165
throw new IllegalStateException("Failed to get UTF-8 string from http entity.", e);
6266
} finally {
@@ -65,14 +69,8 @@ public String getReponseBodyAsString() {
6569
}
6670

6771
public <T extends Resource> T getResource(Class<T> c) {
68-
String responseBody = null;
69-
try {
70-
responseBody = EntityUtils.toString(this.httpEntity, StandardCharsets.UTF_8);
71-
} catch (ParseException | IOException e) {
72-
throw new IllegalStateException("Failed to get UTF-8 string from http entity.", e);
73-
} finally {
74-
this.close();
75-
}
72+
String responseBody = this.getReponseBodyAsString();
73+
if (responseBody == null) return null;
7674

7775
try {
7876
return c.getConstructor(String.class).newInstance(responseBody);
@@ -83,14 +81,8 @@ public <T extends Resource> T getResource(Class<T> c) {
8381
}
8482

8583
public <T extends Resource> List<T> getQueryResponse(Class<T> c) {
86-
String responseBody = null;
87-
try {
88-
responseBody = EntityUtils.toString(this.httpEntity, StandardCharsets.UTF_8);
89-
} catch (ParseException | IOException e) {
90-
throw new IllegalStateException("Failed to get UTF-8 string from http entity.", e);
91-
} finally {
92-
this.close();
93-
}
84+
String responseBody = this.getReponseBodyAsString();
85+
if (responseBody == null) return null;
9486

9587
JSONObject jobject = new JSONObject(responseBody);
9688
String resourceKey = DocumentServiceResponse.getResourceKey(c);
@@ -117,7 +109,10 @@ public <T extends Resource> List<T> getQueryResponse(Class<T> c) {
117109
}
118110

119111
public InputStream getContentStream() {
120-
if (this.httpEntity == null) return null;
112+
if (this.httpEntity == null) {
113+
this.close();
114+
return null;
115+
}
121116

122117
try {
123118
if (this.httpEntity.isStreaming()) {

src/com/microsoft/azure/documentdb/ResourceResponse.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @param <T> the resource type of the resource response.
1414
*/
15-
public final class ResourceResponse<T extends Resource> implements AutoCloseable {
15+
public final class ResourceResponse<T extends Resource> {
1616
private Class<T> cls;
1717
private T resource;
1818
private DocumentServiceResponse response;
@@ -24,6 +24,8 @@ public final class ResourceResponse<T extends Resource> implements AutoCloseable
2424
this.usageHeaders = new HashMap<String, Long>();
2525
this.quotaHeaders = new HashMap<String, Long>();
2626
this.cls = cls;
27+
this.resource = this.response.getResource(this.cls);
28+
this.response.close();
2729
}
2830

2931
/**
@@ -242,9 +244,6 @@ public String getCurrentResourceQuotaUsage() {
242244
* @return the resource.
243245
*/
244246
public T getResource() {
245-
if (this.resource == null) {
246-
this.resource = this.response.getResource(this.cls);
247-
}
248247
return this.resource;
249248
}
250249

@@ -271,13 +270,12 @@ public Map<String, String> getResponseHeaders() {
271270
}
272271

273272
/**
274-
* Closes the connection for this response,
275-
* This connection is automatically closed if the underlying stream is already read.
273+
* Deprecated.
276274
*/
275+
@Deprecated
277276
public void close() {
278-
this.response.close();
279277
}
280-
278+
281279
private long getCurrentQuotaHeader(String headerName) {
282280
if (this.usageHeaders.size() == 0 &&
283281
!this.getMaxResourceQuota().isEmpty() &&

src/com/microsoft/azure/documentdb/test/GatewayTests.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void cleanUpGeneratedDatabases() throws DocumentClientException {
139139
new SqlParameterCollection(new SqlParameter("@id", id))),
140140
null).getQueryIterable().iterator().next();
141141
if (database != null) {
142-
client.deleteDatabase(database.getSelfLink(), null).close();;
142+
client.deleteDatabase(database.getSelfLink(), null);
143143
}
144144
}
145145
}
@@ -161,6 +161,7 @@ public void setUp() throws DocumentClientException {
161161
// Create the collection for test.
162162
DocumentCollection collectionDefinition = new DocumentCollection();
163163
collectionDefinition.setId(GatewayTests.getUID());
164+
164165
this.collectionForTest = client.createCollection(this.databaseForTest.getSelfLink(),
165166
collectionDefinition,
166167
null).getResource();
@@ -338,11 +339,11 @@ public void testDatabaseCrud() throws DocumentClientException {
338339
Assert.assertTrue(databases.size() > 0);
339340

340341
// Delete database.
341-
client.deleteDatabase(createdDb.getSelfLink(), null).close();
342+
client.deleteDatabase(createdDb.getSelfLink(), null);
342343

343344
// Read database after deletion.
344345
try {
345-
client.readDatabase(createdDb.getSelfLink(), null).close();
346+
client.readDatabase(createdDb.getSelfLink(), null);
346347
Assert.fail("Exception didn't happen.");
347348
} catch (DocumentClientException e) {
348349
Assert.assertEquals(404, e.getStatusCode());
@@ -378,11 +379,11 @@ public void testCollectionCrud() throws DocumentClientException {
378379
null).getQueryIterable().toList();
379380
Assert.assertTrue(collections.size() > 0);
380381
// Delete collection.
381-
client.deleteCollection(createdCollection.getSelfLink(), null).close();
382+
client.deleteCollection(createdCollection.getSelfLink(), null);
382383
// Read collection after deletion.
383384

384385
try {
385-
client.readCollection(createdCollection.getSelfLink(), null).close();
386+
client.readCollection(createdCollection.getSelfLink(), null);
386387
Assert.fail("Exception didn't happen.");
387388
} catch (DocumentClientException e) {
388389
Assert.assertEquals(404, e.getStatusCode());
@@ -403,7 +404,7 @@ public void testQueryIterableCrud() throws DocumentClientException {
403404
// Create 10 documents.
404405
for (int i = 0; i < numOfDocuments; ++i) {
405406
Document documentDefinition = new Document("{ 'name': 'For paging test' }");
406-
client.createDocument(this.collectionForTest.getSelfLink(), documentDefinition, null, false).close();
407+
client.createDocument(this.collectionForTest.getSelfLink(), documentDefinition, null, false);
407408
}
408409

409410
int numOfDocumentsPerPage = numOfDocuments / 5;
@@ -477,7 +478,7 @@ public void testCollectionIndexingPolicy() throws DocumentClientException {
477478
" 'indexingMode': 'Lazy'" +
478479
" }" +
479480
"}");
480-
client.deleteCollection(this.collectionForTest.getSelfLink(), null).close();
481+
client.deleteCollection(this.collectionForTest.getSelfLink(), null);
481482
DocumentCollection lazyCollection = client.createCollection(this.databaseForTest.getSelfLink(),
482483
lazyCollectionDefinition,
483484
null).getResource();
@@ -491,7 +492,7 @@ public void testCollectionIndexingPolicy() throws DocumentClientException {
491492
" 'indexingMode': 'Consistent'" +
492493
" }" +
493494
"}");
494-
client.deleteCollection(lazyCollection.getSelfLink(), null).close();
495+
client.deleteCollection(lazyCollection.getSelfLink(), null);
495496
DocumentCollection consistentCollection = client.createCollection(this.databaseForTest.getSelfLink(),
496497
consistentCollectionDefinition,
497498
null).getResource();
@@ -515,7 +516,7 @@ public void testCollectionIndexingPolicy() throws DocumentClientException {
515516
" ]" +
516517
" }" +
517518
"}");
518-
client.deleteCollection(consistentCollection.getSelfLink(), null).close();
519+
client.deleteCollection(consistentCollection.getSelfLink(), null);
519520
DocumentCollection collectionWithSecondaryIndex = client.createCollection(this.databaseForTest.getSelfLink(),
520521
collectionDefinition,
521522
null).getResource();
@@ -600,11 +601,11 @@ public void testDocumentCrud() throws DocumentClientException {
600601
Assert.assertEquals(rr.getStatusCode(), HttpStatus.SC_NOT_MODIFIED);
601602

602603
// delete document
603-
client.deleteDocument(replacedDocument.getSelfLink(), null).close();
604+
client.deleteDocument(replacedDocument.getSelfLink(), null);
604605

605606
// read documents after deletion
606607
try {
607-
client.readDocument(replacedDocument.getSelfLink(), null).close();
608+
client.readDocument(replacedDocument.getSelfLink(), null);
608609
Assert.fail("Exception didn't happen.");
609610
} catch (DocumentClientException e) {
610611
Assert.assertEquals(404, e.getStatusCode());
@@ -746,7 +747,7 @@ public int read() throws IOException {
746747
// Create attachment with invalid content type.
747748
ReadableStream mediaStream = new ReadableStream("stream content.");
748749
try {
749-
client.createAttachment(document.getSelfLink(), mediaStream, invalidMediaOptions).close();
750+
client.createAttachment(document.getSelfLink(), mediaStream, invalidMediaOptions);
750751
Assert.assertTrue(false); // This line shouldn't execute.
751752
} catch (DocumentClientException e) {
752753
Assert.assertEquals(400, e.getStatusCode());
@@ -763,7 +764,7 @@ public int read() throws IOException {
763764
mediaStream = new ReadableStream("stream content");
764765
// Create colliding attachment.
765766
try {
766-
client.createAttachment(document.getSelfLink(), mediaStream, validMediaOptions).close();
767+
client.createAttachment(document.getSelfLink(), mediaStream, validMediaOptions);
767768
Assert.fail("Exception didn't happen.");
768769
} catch (DocumentClientException e) {
769770
Assert.assertEquals(409, e.getStatusCode());
@@ -794,7 +795,7 @@ public int read() throws IOException {
794795
attachment.set("Author", "new author");
795796

796797
// Replace the attachment.
797-
client.replaceAttachment(attachment, null).close();
798+
client.replaceAttachment(attachment, null);
798799
Assert.assertEquals("Book", attachment.getString("MediaType"));
799800
Assert.assertEquals("new author", attachment.getString("Author"));
800801
// Read attachment media.
@@ -837,7 +838,7 @@ public int read() throws IOException {
837838
Assert.assertEquals(validAttachment.getMediaLink(), attachment.getMediaLink());
838839
Assert.assertEquals(validAttachment.getContentType(), attachment.getContentType());
839840
// Deleting attachment.
840-
client.deleteAttachment(attachment.getSelfLink(), null).close();
841+
client.deleteAttachment(attachment.getSelfLink(), null);
841842
// read attachments after deletion
842843
attachments = client.readAttachments(document.getSelfLink(), null).getQueryIterable().toList();
843844
Assert.assertEquals(0, attachments.size());
@@ -869,7 +870,7 @@ public void testTriggerCrud() throws DocumentClientException {
869870

870871
Document document = new Document();
871872
document.setId("noname");
872-
client.createDocument(this.collectionForTest.getSelfLink(), document, options, false).close();
873+
client.createDocument(this.collectionForTest.getSelfLink(), document, options, false);
873874

874875
// replace...
875876
String id = GatewayTests.getUID();
@@ -900,7 +901,7 @@ public void testTriggerCrud() throws DocumentClientException {
900901
Assert.fail("Query fail to find trigger");
901902
}
902903

903-
client.deleteTrigger(newTrigger.getSelfLink(), null).close();
904+
client.deleteTrigger(newTrigger.getSelfLink(), null);
904905
}
905906

906907
@Test
@@ -950,7 +951,7 @@ public void testStoredProcedureCrud() throws DocumentClientException {
950951
Assert.fail("Query fail to find StoredProcedure");
951952
}
952953

953-
client.deleteStoredProcedure(newStoredProcedure.getSelfLink(), null).close();
954+
client.deleteStoredProcedure(newStoredProcedure.getSelfLink(), null);
954955
}
955956

956957
@Test
@@ -1096,7 +1097,7 @@ public void testUserDefinedFunctionCrud() throws DocumentClientException {
10961097
Assert.fail("Query fail to find UserDefinedFunction");
10971098
}
10981099
}
1099-
client.deleteUserDefinedFunction(newUdf.getSelfLink(), null).close();
1100+
client.deleteUserDefinedFunction(newUdf.getSelfLink(), null);
11001101
}
11011102

11021103
@Test
@@ -1134,10 +1135,10 @@ public void testUserCrud() throws DocumentClientException {
11341135
user = client.readUser(replacedUser.getSelfLink(), null).getResource();
11351136
Assert.assertEquals(replacedUser.getId(), user.getId());
11361137
// Delete user.
1137-
client.deleteUser(user.getSelfLink(), null).close();
1138+
client.deleteUser(user.getSelfLink(), null);
11381139
// Read user after deletion.
11391140
try {
1140-
client.readUser(user.getSelfLink(), null).close();
1141+
client.readUser(user.getSelfLink(), null);
11411142
Assert.fail("Exception didn't happen.");
11421143
} catch (DocumentClientException e) {
11431144
Assert.assertEquals(404, e.getStatusCode());
@@ -1191,10 +1192,10 @@ public void testPermissionCrud() throws DocumentClientException {
11911192
permission = client.readPermission(replacedPermission.getSelfLink(), null).getResource();
11921193
Assert.assertEquals(replacedPermission.getId(), permission.getId());
11931194
// Delete permission.
1194-
client.deletePermission(replacedPermission.getSelfLink(), null).close();
1195+
client.deletePermission(replacedPermission.getSelfLink(), null);
11951196
// Read permission after deletion.
11961197
try {
1197-
client.readPermission(permission.getSelfLink(), null).close();
1198+
client.readPermission(permission.getSelfLink(), null);
11981199
Assert.fail("Exception didn't happen.");
11991200
} catch (DocumentClientException e) {
12001201
Assert.assertEquals(404, e.getStatusCode());
@@ -1263,7 +1264,7 @@ public void testOfferReadAndQuery() throws DocumentClientException {
12631264
Assert.assertEquals(400, ex.getStatusCode());
12641265
}
12651266

1266-
client.deleteCollection(this.collectionForTest.getSelfLink(), null).close();
1267+
client.deleteCollection(this.collectionForTest.getSelfLink(), null);
12671268

12681269
// Now try to get the read the offer after the collection is deleted
12691270
try {
@@ -1342,7 +1343,7 @@ public void testCreateCollectionWithOfferType() throws DocumentClientException {
13421343
collectionDefinition.setId(GatewayTests.getUID());
13431344
RequestOptions requestOptions = new RequestOptions();
13441345
requestOptions.setOfferType("S2");
1345-
client.createCollection(this.databaseForTest.getSelfLink(), collectionDefinition, requestOptions).close();
1346+
client.createCollection(this.databaseForTest.getSelfLink(), collectionDefinition, requestOptions);
13461347

13471348
// We should have an offer of type S2.
13481349
List<Offer> offerList = client.readOffers(null).getQueryIterable().toList();
@@ -1457,7 +1458,7 @@ public void testAuthorization() throws DocumentClientException {
14571458
clientForCollection1.readCollection(collectionForTest.getSelfLink(), null).getResource();
14581459
// 2. Failure-- Use Col1 Permission to delete
14591460
try {
1460-
clientForCollection1.deleteCollection(obtainedCollection1.getSelfLink(), null).close();
1461+
clientForCollection1.deleteCollection(obtainedCollection1.getSelfLink(), null);
14611462
Assert.fail("Exception didn't happen.");
14621463
} catch (DocumentClientException e) {
14631464
Assert.assertEquals(403, e.getStatusCode());
@@ -1565,7 +1566,6 @@ public void TestQuotaHeaders() {
15651566
Assert.assertTrue(response.getCollectionSizeUsage() > response.getDocumentUsage());
15661567
Assert.assertTrue(response.getCollectionSizeUsage() == 4 * 1024);
15671568
Assert.assertTrue(response.getDocumentUsage() == 2 * 1024);
1568-
response.close();
15691569
} catch (DocumentClientException e) {
15701570
e.printStackTrace();
15711571
} catch (InterruptedException e) {

0 commit comments

Comments
 (0)