Skip to content

Commit 91146e6

Browse files
Merge pull request #254 from microsoftgraph/core-integration
Core integration
2 parents 348d9dd + c8124ea commit 91146e6

17 files changed

+1656
-17
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ dependencies {
3535
compile 'com.google.code.gson:gson:2.8.2'
3636

3737
compile 'com.sun.jersey:jersey-server:1.19.4'
38+
39+
// Core Http library
40+
compile('com.microsoft.graph:microsoft-graph-core:1.0.0')
3841
}
3942

4043
def pomConfig = {

src/main/java/com/microsoft/graph/concurrency/ChunkedUploadResponseHandler.java

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,22 @@
2222

2323
package com.microsoft.graph.concurrency;
2424

25-
import com.microsoft.graph.requests.extensions.ChunkedUploadResult;
26-
import com.microsoft.graph.models.extensions.UploadSession;
25+
import java.io.BufferedInputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
2729
import com.microsoft.graph.http.DefaultHttpProvider;
2830
import com.microsoft.graph.http.GraphServiceException;
2931
import com.microsoft.graph.http.HttpResponseCode;
3032
import com.microsoft.graph.http.IConnection;
3133
import com.microsoft.graph.http.IHttpRequest;
3234
import com.microsoft.graph.http.IStatefulResponseHandler;
3335
import com.microsoft.graph.logger.ILogger;
36+
import com.microsoft.graph.models.extensions.UploadSession;
37+
import com.microsoft.graph.requests.extensions.ChunkedUploadResult;
3438
import com.microsoft.graph.serializer.ISerializer;
3539

36-
import java.io.BufferedInputStream;
37-
import java.io.InputStream;
40+
import okhttp3.Response;
3841

3942
/**
4043
* Handles the stateful response from the OneDrive upload session
@@ -66,6 +69,16 @@ public ChunkedUploadResponseHandler(final Class<UploadType> uploadType) {
6669
public void configConnection(final IConnection connection) {
6770
return;
6871
}
72+
73+
/**
74+
* Do nothing before getting the response
75+
*
76+
* @param response The response
77+
*/
78+
@Override
79+
public void configConnection(final Response response) {
80+
return;
81+
}
6982

7083
/**
7184
* Generate the chunked upload response result
@@ -112,10 +125,68 @@ public ChunkedUploadResult<UploadType> generateResult(
112125
}
113126
} finally {
114127
if (in != null) {
115-
in.close();
128+
try{
129+
in.close();
130+
} catch(IOException e) {
131+
logger.logError(e.getMessage(), e);
132+
}
116133
}
117134
}
118135

119136
return null;
120137
}
138+
139+
/**
140+
* Generate the chunked upload response result
141+
*
142+
* @param request the HTTP request
143+
* @param response the HTTP response
144+
* @param serializer the serializer
145+
* @param logger the system logger
146+
* @return the chunked upload result, which could be either an uploaded item or error
147+
* @throws Exception an exception occurs if the request was unable to complete for any reason
148+
*/
149+
@Override
150+
public ChunkedUploadResult<UploadType> generateResult(
151+
final IHttpRequest request,
152+
final Response response,
153+
final ISerializer serializer,
154+
final ILogger logger) throws Exception {
155+
InputStream in = null;
156+
try {
157+
if (response.code() == HttpResponseCode.HTTP_ACCEPTED) {
158+
logger.logDebug("Chunk bytes has been accepted by the server.");
159+
in = new BufferedInputStream(response.body().byteStream());
160+
final UploadSession session = serializer.deserializeObject(
161+
DefaultHttpProvider.streamToString(in), UploadSession.class);
162+
163+
return new ChunkedUploadResult<UploadType>(session);
164+
165+
} else if (response.code() == HttpResponseCode.HTTP_CREATED
166+
|| response.code() == HttpResponseCode.HTTP_OK) {
167+
logger.logDebug("Upload session is completed, uploaded item returned.");
168+
in = new BufferedInputStream(response.body().byteStream());
169+
String rawJson = DefaultHttpProvider.streamToString(in);
170+
UploadType uploadedItem = serializer.deserializeObject(rawJson,
171+
this.deserializeTypeClass);
172+
173+
return new ChunkedUploadResult<UploadType>(uploadedItem);
174+
} else if (response.code() >= HttpResponseCode.HTTP_CLIENT_ERROR) {
175+
logger.logDebug("Receiving error during upload, see detail on result error");
176+
177+
return new ChunkedUploadResult<UploadType>(
178+
GraphServiceException.createFromConnection(request, null, serializer,
179+
response, logger));
180+
}
181+
} finally {
182+
if (in != null) {
183+
try{
184+
in.close();
185+
} catch(IOException e) {
186+
logger.logError(e.getMessage(), e);
187+
}
188+
}
189+
}
190+
return null;
191+
}
121192
}

src/main/java/com/microsoft/graph/core/Constants.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ private Constants() {
55
}
66

77
public static final String APPROOT = "approot";
8+
/**
9+
* The content type header
10+
*/
11+
public static final String CONTENT_TYPE_HEADER_NAME = "Content-Type";
12+
/**
13+
* The encoding type for getBytes
14+
*/
15+
public static final String JSON_ENCODING = "UTF-8";
16+
/**
17+
* The content type for JSON responses
18+
*/
19+
public static final String JSON_CONTENT_TYPE = "application/json";
20+
/**
21+
* The binary content type header's value
22+
*/
23+
public static final String BINARY_CONTENT_TYPE = "application/octet-stream";
824

925
// Constants for functional tests
1026
// TO-DO: document how to register an application for functional

src/main/java/com/microsoft/graph/core/DefaultClientConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.microsoft.graph.authentication.IAuthenticationProvider;
2626
import com.microsoft.graph.concurrency.DefaultExecutors;
2727
import com.microsoft.graph.concurrency.IExecutors;
28-
import com.microsoft.graph.http.DefaultHttpProvider;
28+
import com.microsoft.graph.http.CoreHttpProvider;
2929
import com.microsoft.graph.http.IHttpProvider;
3030
import com.microsoft.graph.logger.DefaultLogger;
3131
import com.microsoft.graph.logger.ILogger;
@@ -45,7 +45,7 @@ public abstract class DefaultClientConfig implements IClientConfig {
4545
/**
4646
* The HTTP provider instance
4747
*/
48-
private DefaultHttpProvider httpProvider;
48+
private IHttpProvider httpProvider;
4949

5050
/**
5151
* The logger
@@ -98,11 +98,11 @@ public IAuthenticationProvider getAuthenticationProvider() {
9898
@Override
9999
public IHttpProvider getHttpProvider() {
100100
if (httpProvider == null) {
101-
httpProvider = new DefaultHttpProvider(getSerializer(),
101+
httpProvider = new CoreHttpProvider(getSerializer(),
102102
getAuthenticationProvider(),
103103
getExecutors(),
104104
getLogger());
105-
getLogger().logDebug("Created DefaultHttpProvider");
105+
getLogger().logDebug("Created CoreHttpProvider");
106106
}
107107
return httpProvider;
108108
}

src/main/java/com/microsoft/graph/core/DefaultConnectionConfig.java

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
package com.microsoft.graph.core;
2424

25+
import com.microsoft.graph.httpcore.middlewareoption.IShouldRedirect;
26+
import com.microsoft.graph.httpcore.middlewareoption.IShouldRetry;
27+
import com.microsoft.graph.httpcore.middlewareoption.RedirectOptions;
28+
import com.microsoft.graph.httpcore.middlewareoption.RetryOptions;
29+
2530
public class DefaultConnectionConfig implements IConnectionConfig{
2631

2732
/**
@@ -34,6 +39,31 @@ public class DefaultConnectionConfig implements IConnectionConfig{
3439
*/
3540
private static int DEFAULT_READ_TIMEOUT_MS = 30_000;
3641

42+
/**
43+
* MaxRedirects of every request
44+
*/
45+
private static int maxRedirects = RedirectOptions.DEFAULT_MAX_REDIRECTS;
46+
47+
/**
48+
* ShouldRedirect callback for every request
49+
*/
50+
private static IShouldRedirect shouldRedirect = RedirectOptions.DEFAULT_SHOULD_REDIRECT;
51+
52+
/**
53+
* Max redirects for every request
54+
*/
55+
private static int maxRetries = RetryOptions.DEFAULT_MAX_RETRIES;
56+
57+
/**
58+
* Delay in seconds for every request
59+
*/
60+
private static long delay = RetryOptions.DEFAULT_DELAY;
61+
62+
/**
63+
* Callback called when doing a retry
64+
*/
65+
private static IShouldRetry shouldRetry = RetryOptions.DEFAULT_SHOULD_RETRY;
66+
3767
/**
3868
* Gets the connect timeout
3969
*
@@ -73,5 +103,95 @@ public int getReadTimeout() {
73103
public void setReadTimeout(int readTimeoutValue) {
74104
DEFAULT_READ_TIMEOUT_MS = readTimeoutValue;
75105
}
106+
107+
/**
108+
* Sets the max redirects
109+
*
110+
* @param maxRedirects Max redirects that a request can take
111+
*/
112+
public void setMaxRedirects(int maxRedirects) {
113+
this.maxRedirects = maxRedirects;
114+
}
115+
116+
/**
117+
* Gets the max redirects
118+
*
119+
* @return Max redirects that a request can take
120+
*/
121+
public int getMaxRedirects() {
122+
return maxRedirects;
123+
}
124+
125+
/**
126+
* Sets the should redirect callback
127+
*
128+
* @param shouldRedirect Callback called before doing a redirect
129+
*/
130+
public void setShouldRedirect(IShouldRedirect shouldRedirect) {
131+
this.shouldRedirect = shouldRedirect;
132+
}
133+
134+
/**
135+
* Gets the should redirect callback
136+
*
137+
* @return Callback which is called before redirect
138+
*/
139+
public IShouldRedirect getShouldRedirect() {
140+
return shouldRedirect;
141+
}
142+
143+
/**
144+
* Sets the should retry callback
145+
*
146+
* @param shouldretry The callback called before retry
147+
*/
148+
public void setShouldRetry(IShouldRetry shouldretry) {
149+
this.shouldRetry = shouldretry;
150+
}
151+
152+
/**
153+
* Gets the should retry callback
154+
*
155+
* @return Callback called before retry
156+
*/
157+
public IShouldRetry getShouldRetry() {
158+
return shouldRetry;
159+
}
160+
161+
/**
162+
* Sets the max retries
163+
*
164+
* @param maxRetries Max retries for a request
165+
*/
166+
public void setMaxRetries(int maxRetries) {
167+
this.maxRedirects = maxRedirects;
168+
}
169+
170+
/**
171+
* Gets max retries
172+
*
173+
* @return Max retries for a request
174+
*/
175+
public int getMaxRetries() {
176+
return maxRetries;
177+
}
178+
179+
/**
180+
* Sets the delay in seconds between retires
181+
*
182+
* @param delay Delay in seconds between retries
183+
*/
184+
public void setDelay(long delay) {
185+
this.delay = delay;
186+
}
187+
188+
/**
189+
* Gets delay between retries
190+
*
191+
* @return Delay between retries in seconds
192+
*/
193+
public long getDelay() {
194+
return delay;
195+
}
76196

77197
}

0 commit comments

Comments
 (0)