Skip to content

Commit 1c1c575

Browse files
author
Nakul Sabharwal
committed
added dependency on core, added okhttpprovider
1 parent decf5d5 commit 1c1c575

File tree

7 files changed

+872
-9
lines changed

7 files changed

+872
-9
lines changed

build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ repositories {
2020
// You can declare any Maven/Ivy/file repository here.
2121
jcenter()
2222
mavenCentral()
23+
jcenter{
24+
url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
25+
}
2326
}
2427

2528
dependencies {
@@ -35,6 +38,9 @@ dependencies {
3538
compile 'com.google.code.gson:gson:2.8.2'
3639

3740
compile 'com.sun.jersey:jersey-server:1.19.4'
41+
42+
// Core Http library
43+
compile('com.microsoft.graph:microsoft-graph-core:0.1.0-SNAPSHOT')
3844
}
3945

4046
def pomConfig = {

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

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@
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.InputStream;
27+
2728
import com.microsoft.graph.http.DefaultHttpProvider;
2829
import com.microsoft.graph.http.GraphServiceException;
2930
import com.microsoft.graph.http.HttpResponseCode;
3031
import com.microsoft.graph.http.IConnection;
3132
import com.microsoft.graph.http.IHttpRequest;
3233
import com.microsoft.graph.http.IStatefulResponseHandler;
3334
import com.microsoft.graph.logger.ILogger;
35+
import com.microsoft.graph.models.extensions.UploadSession;
36+
import com.microsoft.graph.requests.extensions.ChunkedUploadResult;
3437
import com.microsoft.graph.serializer.ISerializer;
3538

36-
import java.io.BufferedInputStream;
37-
import java.io.InputStream;
39+
import okhttp3.Response;
3840

3941
/**
4042
* Handles the stateful response from the OneDrive upload session
@@ -118,4 +120,56 @@ public ChunkedUploadResult<UploadType> generateResult(
118120

119121
return null;
120122
}
123+
124+
/**
125+
* Generate the chunked upload response result
126+
*
127+
* @param request the HTTP request
128+
* @param connection the HTTP connection
129+
* @param serializer the serializer
130+
* @param logger the system logger
131+
* @return the chunked upload result, which could be either an uploaded item or error
132+
* @throws Exception an exception occurs if the request was unable to complete for any reason
133+
*/
134+
@Override
135+
public ChunkedUploadResult<UploadType> generateResult(
136+
final IHttpRequest request,
137+
final Response response,
138+
final ISerializer serializer,
139+
final ILogger logger) throws Exception {
140+
InputStream in = null;
141+
142+
try {
143+
if (response.code() == HttpResponseCode.HTTP_ACCEPTED) {
144+
logger.logDebug("Chunk bytes has been accepted by the server.");
145+
in = new BufferedInputStream(response.body().byteStream());
146+
final UploadSession session = serializer.deserializeObject(
147+
DefaultHttpProvider.streamToString(in), UploadSession.class);
148+
149+
return new ChunkedUploadResult<UploadType>(session);
150+
151+
} else if (response.code() == HttpResponseCode.HTTP_CREATED
152+
|| response.code() == HttpResponseCode.HTTP_OK) {
153+
logger.logDebug("Upload session is completed, uploaded item returned.");
154+
in = new BufferedInputStream(response.body().byteStream());
155+
String rawJson = DefaultHttpProvider.streamToString(in);
156+
UploadType uploadedItem = serializer.deserializeObject(rawJson,
157+
this.deserializeTypeClass);
158+
159+
return new ChunkedUploadResult<UploadType>(uploadedItem);
160+
} else if (response.code() >= HttpResponseCode.HTTP_CLIENT_ERROR) {
161+
logger.logDebug("Receiving error during upload, see detail on result error");
162+
163+
return new ChunkedUploadResult<UploadType>(
164+
GraphServiceException.createFromConnection(request, null, serializer,
165+
response, logger));
166+
}
167+
} finally {
168+
if (in != null) {
169+
in.close();
170+
}
171+
}
172+
173+
return null;
174+
}
121175
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
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;
2928
import com.microsoft.graph.http.IHttpProvider;
3029
import com.microsoft.graph.logger.DefaultLogger;
3130
import com.microsoft.graph.logger.ILogger;
3231
import com.microsoft.graph.serializer.DefaultSerializer;
3332
import com.microsoft.graph.serializer.ISerializer;
3433

34+
import demo.OkHttpProvider;
35+
3536
/**
3637
* The default configuration for a service client
3738
*/
@@ -45,7 +46,7 @@ public abstract class DefaultClientConfig implements IClientConfig {
4546
/**
4647
* The HTTP provider instance
4748
*/
48-
private DefaultHttpProvider httpProvider;
49+
private IHttpProvider httpProvider;
4950

5051
/**
5152
* The logger
@@ -98,11 +99,12 @@ public IAuthenticationProvider getAuthenticationProvider() {
9899
@Override
99100
public IHttpProvider getHttpProvider() {
100101
if (httpProvider == null) {
101-
httpProvider = new DefaultHttpProvider(getSerializer(),
102+
System.out.println("---------------------------USING OKHTTP PROVIDER----------------------------");
103+
httpProvider = new OkHttpProvider(getSerializer(),
102104
getAuthenticationProvider(),
103105
getExecutors(),
104106
getLogger());
105-
getLogger().logDebug("Created DefaultHttpProvider");
107+
getLogger().logDebug("Created OkHttpProvider");
106108
}
107109
return httpProvider;
108110
}

src/main/java/com/microsoft/graph/http/BaseStreamRequest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.microsoft.graph.concurrency.ICallback;
2626
import com.microsoft.graph.core.ClientException;
2727
import com.microsoft.graph.core.IBaseClient;
28+
import com.microsoft.graph.httpcore.middlewareoption.IShouldRedirect;
29+
import com.microsoft.graph.httpcore.middlewareoption.IShouldRetry;
2830
import com.microsoft.graph.options.HeaderOption;
2931
import com.microsoft.graph.options.Option;
3032

@@ -43,6 +45,12 @@ public abstract class BaseStreamRequest<T> implements IHttpStreamRequest {
4345
* The base request for this collection request
4446
*/
4547
private final BaseRequest baseRequest;
48+
49+
private int maxRedirect;
50+
private IShouldRedirect shouldRedirect;
51+
private long delay;
52+
private int maxRetries;
53+
private IShouldRetry shouldRetry;
4654

4755
/**
4856
* Creates the stream request.
@@ -175,4 +183,49 @@ public List<HeaderOption> getHeaders() {
175183
public List<Option> getOptions() {
176184
return baseRequest.getOptions();
177185
}
186+
187+
/**
188+
* Sets maximum number of redirects
189+
* @param maxRedirect Maximum number of redirects <= 20
190+
*/
191+
public IHttpRequest setMaxRedirect(int maxRedirect) {
192+
this.maxRedirect = maxRedirect;
193+
return this;
194+
}
195+
196+
/**
197+
* Sets IShouldRedirect callback
198+
* @param shouldRedirect Callbacks to IShouldRedirect to decides to redirect or not
199+
*/
200+
public IHttpRequest setShouldRedirect(IShouldRedirect shouldRedirect) {
201+
this.shouldRedirect = shouldRedirect;
202+
return this;
203+
}
204+
205+
/**
206+
* Sets Delay in seconds between retries
207+
* @param delay Delay in seconds between retries
208+
*/
209+
public IHttpRequest setDelay(long delay) {
210+
this.delay = delay;
211+
return this;
212+
}
213+
214+
/**
215+
* Sets maximum number of retries
216+
* @param maxRetries Maximum number of retries <= 10
217+
*/
218+
public IHttpRequest setMaxRetries(int maxRetries) {
219+
this.maxRetries = maxRetries;
220+
return this;
221+
}
222+
223+
/**
224+
* Sets IShouldRetry callback
225+
* @param shouldRetry Callback to decide to retry or not
226+
*/
227+
public IHttpRequest setShouldRetry(IShouldRetry shouldRetry) {
228+
this.shouldRetry = shouldRetry;
229+
return this;
230+
}
178231
}

src/main/java/com/microsoft/graph/http/GraphServiceException.java

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131
import com.google.gson.Gson;
3232
import com.google.gson.GsonBuilder;
3333
import com.microsoft.graph.core.ClientException;
34-
import com.microsoft.graph.core.GraphErrorCodes;
3534
import com.microsoft.graph.logger.ILogger;
3635
import com.microsoft.graph.logger.LoggerLevel;
3736
import com.microsoft.graph.options.HeaderOption;
3837
import com.microsoft.graph.serializer.ISerializer;
3938

39+
import demo.OkHttpProvider;
40+
import okhttp3.Response;
41+
4042
/**
4143
* An exception from the Graph service
4244
*/
@@ -346,4 +348,103 @@ public static <T> GraphServiceException createFromConnection(final IHttpRequest
346348
error,
347349
isVerbose);
348350
}
351+
352+
/**
353+
* Creates a Graph service exception from a given failed HTTP request
354+
*
355+
* @param request the request that resulted in this failure
356+
* @param serializable the serialized object that was sent with this request
357+
* @param serializer the serializer to re-create the option in its over the wire state
358+
* @param connection the connection that was used to extract the response information from
359+
* @param logger the logger to log exception information to
360+
* @param <T> the type of the serializable object
361+
* @return the new GraphServiceException instance
362+
* @throws IOException an exception occurs if there were any problems processing the connection
363+
*/
364+
public static <T> GraphServiceException createFromConnection(final IHttpRequest request,
365+
final T serializable,
366+
final ISerializer serializer,
367+
final Response response,
368+
final ILogger logger)
369+
throws IOException {
370+
final String method = response.request().method();
371+
final String url = request.getRequestUrl().toString();
372+
final List<String> requestHeaders = new LinkedList<>();
373+
for (final HeaderOption option : request.getHeaders()) {
374+
requestHeaders.add(option.getName() + " : " + option.getValue());
375+
}
376+
boolean isVerbose = logger.getLoggingLevel() == LoggerLevel.DEBUG;
377+
final String requestBody;
378+
if (serializable instanceof byte[]) {
379+
final byte[] bytes = (byte[]) serializable;
380+
StringBuilder sb = new StringBuilder();
381+
sb.append("byte[").append(bytes.length).append("]");
382+
383+
sb.append(" {");
384+
if (isVerbose) {
385+
sb.append(bytes);
386+
} else {
387+
for (int i = 0; i < MAX_BYTE_COUNT_BEFORE_TRUNCATION && i < bytes.length; i++) {
388+
sb.append(bytes[i]).append(", ");
389+
}
390+
if (bytes.length > MAX_BYTE_COUNT_BEFORE_TRUNCATION) {
391+
sb.append(TRUNCATION_MARKER).append("}");
392+
}
393+
}
394+
requestBody = sb.toString();
395+
} else if (serializable != null) {
396+
requestBody = serializer.serializeObject(serializable);
397+
} else {
398+
requestBody = null;
399+
}
400+
401+
final int responseCode = response.code();
402+
final List<String> responseHeaders = new LinkedList<>();
403+
final Map<String, String> headers = OkHttpProvider.getResponseHeadersAsMapStringString(response);
404+
for (final String key : headers.keySet()) {
405+
final String fieldPrefix;
406+
if (key == null) {
407+
fieldPrefix = "";
408+
} else {
409+
fieldPrefix = key + " : ";
410+
}
411+
responseHeaders.add(fieldPrefix + headers.get(key));
412+
}
413+
414+
final String responseMessage = response.message();
415+
final String rawOutput = DefaultHttpProvider.streamToString(response.body().byteStream());
416+
GraphErrorResponse error;
417+
try {
418+
error = serializer.deserializeObject(rawOutput, GraphErrorResponse.class, OkHttpProvider.getResponseHeadersAsMapOfStringList(response));
419+
} catch (final Exception ex) {
420+
error = new GraphErrorResponse();
421+
error.error = new GraphError();
422+
error.error.code = "Unable to parse error response message";
423+
error.error.message = "Raw error: " + rawOutput;
424+
error.error.innererror = new GraphInnerError();
425+
error.error.innererror.code = ex.getMessage();
426+
}
427+
428+
if (responseCode >= INTERNAL_SERVER_ERROR) {
429+
return new GraphFatalServiceException(method,
430+
url,
431+
requestHeaders,
432+
requestBody,
433+
responseCode,
434+
responseMessage,
435+
responseHeaders,
436+
error,
437+
isVerbose);
438+
}
439+
440+
return new GraphServiceException(method,
441+
url,
442+
requestHeaders,
443+
requestBody,
444+
responseCode,
445+
responseMessage,
446+
responseHeaders,
447+
error,
448+
isVerbose);
449+
}
349450
}

src/main/java/com/microsoft/graph/http/IStatefulResponseHandler.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import com.microsoft.graph.logger.ILogger;
2727
import com.microsoft.graph.serializer.ISerializer;
2828

29+
import okhttp3.Response;
30+
2931
/**
3032
* The handler interface for requests having stateful response from server.
3133
* The handler will custom the HTTP connection if needed and generate request
@@ -56,4 +58,19 @@ ResultType generateResult(final IHttpRequest request,
5658
final IConnection connection,
5759
final ISerializer serializer,
5860
final ILogger logger) throws Exception;
61+
62+
/**
63+
* Generate result after receiving response
64+
*
65+
* @param request the HTTP request
66+
* @param connection the HTTP connection
67+
* @param serializer the serializer for parsing response
68+
* @param logger the logger
69+
* @return the result generated by this handler
70+
* @throws Exception an exception occurs if the request was unable to complete for any reason
71+
*/
72+
ResultType generateResult(final IHttpRequest request,
73+
final Response response,
74+
final ISerializer serializer,
75+
final ILogger logger) throws Exception;
5976
}

0 commit comments

Comments
 (0)