Skip to content

Commit 313a2d5

Browse files
committed
Synching changes from TFS
Changes to ConnectionPolicy and FeedOptions: 1) Removes not-used properties in ConnectionPolicy. 2) Uses mediaRequestTimeout and mediaReadMode. 3) Renames maxItemCount in FeedOptions to be pageSize. Changes to JsonSerializable 1) Supports putting null value. 2) Supports putting collection of collections.
1 parent 1e581e4 commit 313a2d5

File tree

7 files changed

+140
-92
lines changed

7 files changed

+140
-92
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,7 @@ public ResourceResponse<Attachment> createAttachment(String documentLink,
12001200
path,
12011201
mediaStream,
12021202
requestHeaders);
1203+
request.setIsMedia(true);
12031204
return new ResourceResponse<Attachment>(this.doCreate(request), Attachment.class);
12041205
}
12051206

@@ -1218,7 +1219,9 @@ public MediaResponse readMedia(String mediaLink) throws DocumentClientException
12181219

12191220
String path = DocumentClient.joinPath(mediaLink, null);
12201221
DocumentServiceRequest request = DocumentServiceRequest.create(ResourceType.Media, path, null);
1221-
return new MediaResponse(this.doRead(request));
1222+
request.setIsMedia(true);
1223+
return new MediaResponse(this.doRead(request),
1224+
this.connectionPolicy.getMediaReadMode() == MediaReadMode.Buffered);
12221225
}
12231226

12241227
/**
@@ -1246,7 +1249,9 @@ public MediaResponse updateMedia(String mediaLink, InputStream mediaStream, Medi
12461249
path,
12471250
mediaStream,
12481251
requestHeaders);
1249-
return new MediaResponse(this.doReplace(request));
1252+
request.setIsMedia(true);
1253+
return new MediaResponse(this.doReplace(request),
1254+
this.connectionPolicy.getMediaReadMode() == MediaReadMode.Buffered);
12501255
}
12511256

12521257
/**
@@ -1751,8 +1756,8 @@ private Map<String, String> getFeedHeaders(FeedOptions options) {
17511756

17521757
Map<String, String> headers = new HashMap<String, String>();
17531758

1754-
if (options.getMaxItemCount() != null) {
1755-
headers.put(HttpConstants.HttpHeaders.PAGE_SIZE, options.getMaxItemCount().toString());
1759+
if (options.getPageSize() != null) {
1760+
headers.put(HttpConstants.HttpHeaders.PAGE_SIZE, options.getPageSize().toString());
17561761
}
17571762

17581763
if (options.getRequestContinuation() != null) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,14 @@ public String getContinuation() {
266266
public void setContinuation(String continuation) {
267267
this.continuation = continuation;
268268
}
269+
270+
private boolean isMedia = false;
271+
272+
public void setIsMedia(boolean isMedia) {
273+
this.isMedia = isMedia;
274+
}
275+
276+
public boolean getIsMedia() {
277+
return this.isMedia;
278+
}
269279
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@
99
*/
1010
public final class FeedOptions {
1111

12-
private Integer maxItemCount;
12+
private Integer pageSize;
1313

1414
/**
1515
* Gets the maximum number of items to be returned in the enumeration operation.
1616
*
17-
* @return the max item count.
17+
* @return the page size.
1818
*/
19-
public Integer getMaxItemCount() {
20-
return this.maxItemCount;
19+
public Integer getPageSize() {
20+
return this.pageSize;
2121
}
2222

2323
/**
2424
* Sets the maximum number of items to be returned in the enumeration operation.
2525
*
26-
* @param maxItemCount the max item count.
26+
* @param pageSize the page size.
2727
*/
28-
public void setMaxItemCount(Integer maxItemCount) {
29-
this.maxItemCount = maxItemCount;
28+
public void setPageSize(Integer pageSize) {
29+
this.pageSize = pageSize;
3030
}
3131

3232
private String requestContinuation;

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

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ final class GatewayProxy {
4141
private Map<String, String> defaultHeaders;
4242
private String masterKey;
4343
private Map<String, String> resourceTokens;
44-
private int requestTimeout;
44+
private ConnectionPolicy connectionPolicy;
4545
private HttpClient httpClient;
46-
private int maxPoolSize;
47-
private int idleConnectionTimeout;
46+
private HttpClient mediaHttpClient;
4847

4948
public GatewayProxy(URI serviceEndpoint,
50-
ConnectionPolicy policy,
49+
ConnectionPolicy connectionPolicy,
5150
ConsistencyLevel consistencyLevel,
5251
String masterKey,
5352
Map<String, String> resourceTokens) {
@@ -65,12 +64,9 @@ public GatewayProxy(URI serviceEndpoint,
6564
consistencyLevel.toString());
6665
}
6766

67+
this.connectionPolicy = connectionPolicy;
6868
this.masterKey = masterKey;
6969
this.resourceTokens = resourceTokens;
70-
this.requestTimeout = policy.getRequestTimeout();
71-
this.maxPoolSize = policy.getMaxPoolSize();
72-
this.idleConnectionTimeout = policy.getIdleConnectionTimeout();
73-
this.httpClient = this.createHttpClient();
7470
}
7571

7672
public DocumentServiceResponse doCreate(DocumentServiceRequest request)
@@ -111,30 +107,45 @@ public DocumentServiceResponse doSQLQuery(DocumentServiceRequest request)
111107
return this.performPostRequest(request);
112108
}
113109

110+
private HttpClient getHttpClient(boolean isForMedia) {
111+
if (isForMedia) {
112+
if (this.mediaHttpClient == null) this.mediaHttpClient = this.createHttpClient(isForMedia);
113+
return this.mediaHttpClient;
114+
} else {
115+
if (this.httpClient == null) this.httpClient = this.createHttpClient(isForMedia);
116+
return this.httpClient;
117+
}
118+
}
119+
114120
/**
115121
* Only one instance is created for the httpClient for optimization.
116122
* A PoolingClientConnectionManager is used with the Http client
117123
* to be able to reuse connections and execute requests concurrently.
118124
* A timeout for closing each connection is set so that connections don't leak.
119125
* A timeout is set for requests to avoid deadlocks.
120-
* @return
126+
* @return the created HttpClient
121127
*/
122-
private HttpClient createHttpClient() {
123-
if(this.httpClient == null) {
124-
PoolingClientConnectionManager conMan = new PoolingClientConnectionManager( SchemeRegistryFactory.createDefault() );
125-
conMan.setMaxTotal(maxPoolSize);
126-
conMan.setDefaultMaxPerRoute(maxPoolSize);
127-
conMan.closeIdleConnections(this.idleConnectionTimeout, TimeUnit.SECONDS);
128-
this.httpClient = new DefaultHttpClient(conMan);
129-
final HttpParams httpParams = httpClient.getParams();
130-
HttpConnectionParams.setConnectionTimeout(httpParams, requestTimeout * 1000);
131-
HttpConnectionParams.setSoTimeout(httpParams, requestTimeout * 1000);
128+
private HttpClient createHttpClient(boolean isForMedia) {
129+
PoolingClientConnectionManager conMan = new PoolingClientConnectionManager(
130+
SchemeRegistryFactory.createDefault());
131+
conMan.setMaxTotal(this.connectionPolicy.getMaxPoolSize());
132+
conMan.setDefaultMaxPerRoute(this.connectionPolicy.getMaxPoolSize());
133+
conMan.closeIdleConnections(this.connectionPolicy.getIdleConnectionTimeout(), TimeUnit.SECONDS);
134+
HttpClient httpClient = new DefaultHttpClient(conMan);
135+
HttpParams httpParams = httpClient.getParams();
136+
137+
if (isForMedia) {
138+
HttpConnectionParams.setConnectionTimeout(httpParams,
139+
this.connectionPolicy.getMediaRequestTimeout() * 1000);
140+
HttpConnectionParams.setSoTimeout(httpParams, this.connectionPolicy.getMediaRequestTimeout() * 1000);
141+
} else {
142+
HttpConnectionParams.setConnectionTimeout(httpParams, this.connectionPolicy.getRequestTimeout() * 1000);
143+
HttpConnectionParams.setSoTimeout(httpParams, this.connectionPolicy.getRequestTimeout() * 1000);
132144
}
133-
134-
return this.httpClient;
145+
146+
return httpClient;
135147
}
136148

137-
138149
private void putMoreContentIntoDocumentServiceRequest(
139150
DocumentServiceRequest request,
140151
String httpMethod) {
@@ -181,8 +192,7 @@ private void putMoreContentIntoDocumentServiceRequest(
181192
}
182193
}
183194

184-
private void fillHttpRequestBaseWithHeaders(Map<String, String> headers,
185-
HttpRequestBase httpBase) {
195+
private void fillHttpRequestBaseWithHeaders(Map<String, String> headers, HttpRequestBase httpBase) {
186196
// Add default headers.
187197
for (Map.Entry<String, String> entry : this.defaultHeaders.entrySet()) {
188198
httpBase.setHeader(entry.getKey(), entry.getValue());
@@ -195,8 +205,7 @@ private void fillHttpRequestBaseWithHeaders(Map<String, String> headers,
195205
}
196206
}
197207

198-
private void maybeThrowException(HttpResponse response)
199-
throws DocumentClientException {
208+
private void maybeThrowException(HttpResponse response) throws DocumentClientException {
200209
int statusCode = response.getStatusLine().getStatusCode();
201210

202211
if (statusCode >= HttpConstants.StatusCodes.MINIMUM_STATUSCODE_AS_ERROR_GATEWAY) {
@@ -218,14 +227,12 @@ private void maybeThrowException(HttpResponse response)
218227
responseHeaders.put(header.getName(), header.getValue());
219228
}
220229

221-
throw new DocumentClientException(statusCode,
222-
new Error(body),
223-
responseHeaders);
230+
throw new DocumentClientException(statusCode, new Error(body), responseHeaders);
224231
}
225232
}
226233

227234
private DocumentServiceResponse performDeleteRequest(
228-
DocumentServiceRequest request) throws DocumentClientException {
235+
DocumentServiceRequest request) throws DocumentClientException {
229236
putMoreContentIntoDocumentServiceRequest(
230237
request,
231238
HttpConstants.HttpMethods.DELETE);
@@ -246,9 +253,8 @@ private DocumentServiceResponse performDeleteRequest(
246253
HttpDelete httpDelete = new HttpDelete(uri);
247254
this.fillHttpRequestBaseWithHeaders(request.getHeaders(), httpDelete);
248255
HttpResponse response = null;
249-
try
250-
{
251-
response = this.httpClient.execute(httpDelete);
256+
try {
257+
response = this.getHttpClient(request.getIsMedia()).execute(httpDelete);
252258
} catch (IOException e) {
253259
httpDelete.releaseConnection();
254260
throw new IllegalStateException("Http client execution failed.", e);
@@ -261,8 +267,7 @@ private DocumentServiceResponse performDeleteRequest(
261267
return new DocumentServiceResponse(response);
262268
}
263269

264-
private DocumentServiceResponse performGetRequest(
265-
DocumentServiceRequest request) throws DocumentClientException {
270+
private DocumentServiceResponse performGetRequest(DocumentServiceRequest request) throws DocumentClientException {
266271
putMoreContentIntoDocumentServiceRequest(request,
267272
HttpConstants.HttpMethods.GET);
268273
URI uri;
@@ -283,7 +288,7 @@ private DocumentServiceResponse performGetRequest(
283288
this.fillHttpRequestBaseWithHeaders(request.getHeaders(), httpGet);
284289
HttpResponse response = null;
285290
try {
286-
response = this.httpClient.execute(httpGet);
291+
response = this.getHttpClient(request.getIsMedia()).execute(httpGet);
287292
} catch (IOException e) {
288293
httpGet.releaseConnection();
289294
throw new IllegalStateException("Http client execution failed.", e);
@@ -293,8 +298,7 @@ private DocumentServiceResponse performGetRequest(
293298
return new DocumentServiceResponse(response);
294299
}
295300

296-
private DocumentServiceResponse performPostRequest(
297-
DocumentServiceRequest request) throws DocumentClientException {
301+
private DocumentServiceResponse performPostRequest(DocumentServiceRequest request) throws DocumentClientException {
298302
putMoreContentIntoDocumentServiceRequest(
299303
request,
300304
HttpConstants.HttpMethods.POST);
@@ -317,7 +321,7 @@ private DocumentServiceResponse performPostRequest(
317321
httpPost.setEntity(request.getBody());
318322
HttpResponse response = null;
319323
try {
320-
response = httpClient.execute(httpPost);
324+
response = this.getHttpClient(request.getIsMedia()).execute(httpPost);
321325
} catch (IOException e) {
322326
httpPost.releaseConnection();
323327
throw new IllegalStateException("Http client execution failed.", e);
@@ -327,8 +331,7 @@ private DocumentServiceResponse performPostRequest(
327331
return new DocumentServiceResponse(response);
328332
}
329333

330-
private DocumentServiceResponse performPutRequest(
331-
DocumentServiceRequest request) throws DocumentClientException {
334+
private DocumentServiceResponse performPutRequest(DocumentServiceRequest request) throws DocumentClientException {
332335
putMoreContentIntoDocumentServiceRequest(request,
333336
HttpConstants.HttpMethods.PUT);
334337
URI uri;
@@ -351,7 +354,7 @@ private DocumentServiceResponse performPutRequest(
351354
HttpResponse response = null;
352355
try {
353356
httpPut.releaseConnection();
354-
response = this.httpClient.execute(httpPut);
357+
response = this.getHttpClient(request.getIsMedia()).execute(httpPut);
355358
} catch (IOException e) {
356359
throw new IllegalStateException("Http client execution failed.", e);
357360
}

0 commit comments

Comments
 (0)