Skip to content

Commit 686f46f

Browse files
author
vijayattri27
committed
If the bootstrap file is very large, the access token expires during downloading in a random batch. Added code for access token refresh
1 parent 4ddf5a6 commit 686f46f

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

src/main/java/com/ebay/feed/api/FeedImpl.java

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
* Copyright 2018 eBay Inc.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
6-
*
6+
*
77
* http://www.apache.org/licenses/LICENSE-2.0
8-
*
8+
*
99
* Unless required by applicable law or agreed to in writing, software distributed under the License
1010
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1111
* or implied. See the License for the specific language governing permissions and limitations under
@@ -50,6 +50,8 @@
5050
import com.ebay.feed.validator.FeedValidator;
5151
import com.google.gson.Gson;
5252
import com.google.gson.GsonBuilder;
53+
import com.ebay.feed.auth.CredentialLoader;
54+
import com.ebay.feed.model.oauth.AuthRequest;
5355

5456
/**
5557
* <div>
@@ -61,7 +63,7 @@
6163
* contents</li>
6264
* </ul>
6365
* </div>
64-
*
66+
*
6567
* @author shanganesh
6668
*
6769
*/
@@ -72,6 +74,7 @@ public class FeedImpl implements Feed {
7274
private FeedUtil feedUtils = null;
7375
private FilterUtil filterUtils = null;
7476
private FeedValidator feedValidator = null;
77+
static String credentialFilePath = "sample-credentials/credentials.yaml";
7578

7679
public FeedImpl() {
7780
client =
@@ -86,7 +89,7 @@ public FeedImpl() {
8689

8790
/*
8891
* (non-Javadoc)
89-
*
92+
*
9093
* @see com.ebay.feed.api.Feed#filter(java.lang.String, com.ebay.feed.model.FeedFilterRequest)
9194
*/
9295
@Override
@@ -125,7 +128,7 @@ public com.ebay.feed.model.feed.operation.filter.Response filter(FeedFilterReque
125128

126129
/*
127130
* (non-Javadoc)
128-
*
131+
*
129132
* @see com.ebay.feed.api.Feed#unzip(java.lang.String, boolean)
130133
*/
131134
@Override
@@ -159,7 +162,7 @@ public com.ebay.feed.model.feed.operation.filter.Response unzip(String filePath)
159162

160163
/*
161164
* (non-Javadoc)
162-
*
165+
*
163166
* @see com.ebay.feed.api.Feed#get(com.ebay.feed.model.FeedRequest)
164167
*/
165168
@Override
@@ -202,7 +205,7 @@ public GetFeedResponse get(FeedRequest feedRequest, String downloadDirectory) {
202205
* additional headers including range - Creates default file/folder path and cleansup if already
203206
* present
204207
* </p>
205-
*
208+
*
206209
* @param feedRequest
207210
* @param downloadDirectory Optional local directory where files can be downloaded. Default is
208211
* current working directory
@@ -237,7 +240,7 @@ private GetFeedResponse process(FeedRequest feedRequest, Path downloadDirectory)
237240
// generate static request
238241
requestBuilder = feedUtils.generateRequest(feedRequest, requestBuilder);
239242

240-
// generate dynamic header
243+
// generate dynamic header
241244
Long chunkSizeLimit = feedUtils.getChunkSizeLimit(feedRequest);
242245
requestBuilder.addHeader(Constants.RANGE_HEADER, Constants.RANGE_PREFIX + chunkSizeLimit);
243246

@@ -249,13 +252,13 @@ private GetFeedResponse process(FeedRequest feedRequest, Path downloadDirectory)
249252
* <p>
250253
* Invokes the feed API with the max range value of 100 MB. If the file is lesser than 100 MB,
251254
* then it returns the downloaded file path along with the status.
252-
*
255+
*
253256
* If the file is greater than 100 MB - Iteratively calls feed API, with incrementing range
254257
* headers - Appends content to file - Downloads entire content and returns with downloaded file
255258
* path.
256-
*
259+
*
257260
* </p>
258-
*
261+
*
259262
* @param request The API request
260263
* @param path Path of the downloaded or partially downloading file, where contents need to be
261264
* appended
@@ -295,6 +298,24 @@ private GetFeedResponse invoker(Request.Builder requestBuilder, Path path, boole
295298

296299
requestBuilder.removeHeader(Constants.RANGE_HEADER);
297300
requestBuilder.addHeader(Constants.RANGE_HEADER, val);
301+
LOGGER.debug("Firing the request for next batch" + newUpperLimit);
302+
AuthRequest authRequest = new AuthRequest(credentialFilePath, null);
303+
304+
// load credentials and generate token
305+
CredentialLoader credentialLoader = new CredentialLoader(authRequest);
306+
String token = "foobar";
307+
try {
308+
credentialLoader.loadCredentials();
309+
token = credentialLoader.getOauthResponse().getAccessToken().get().getToken();
310+
} catch(Exception e){
311+
LOGGER.debug("Exception in fetching the new access token");
312+
return new GetFeedResponse(Constants.FAILURE_CODE, Constants.FAILURE, null, null);
313+
}
314+
token = Constants.TOKEN_BEARER_PREFIX + token;
315+
316+
requestBuilder.removeHeader(Constants.AUTHORIZATION_HEADER);
317+
requestBuilder.addHeader(Constants.AUTHORIZATION_HEADER, token);
318+
requestBuilder.addHeader(Constants.RANGE_HEADER, val);
298319

299320
responseFlag = invokeIteratively(requestBuilder.build(), path);
300321

@@ -325,23 +346,23 @@ private GetFeedResponse invoker(Request.Builder requestBuilder, Path path, boole
325346
*/
326347
private String fixFilePath(Path originalFilePath, InvokeResponse invokeResponse) {
327348
Path newFilePath = originalFilePath;
328-
if(originalFilePath.toString().contains("null") && !StringUtils.isEmpty(invokeResponse.getLastModified())){
349+
if(originalFilePath.toString().contains("null") && !StringUtils.isEmpty(invokeResponse.getLastModified())){
329350
String newPath = originalFilePath.toString().replace("null", invokeResponse.getLastModified());
330351
newFilePath = Paths.get(newPath);
331352
try {
332353
Files.move(originalFilePath, newFilePath, StandardCopyOption.REPLACE_EXISTING);
333354
} catch (IOException e) {
334-
LOGGER.error("Unable to rename the bootstrap item feed file with date field", e);
355+
LOGGER.error("Unable to rename the bootstrap item feed file with date field", e);
335356
}
336357
}
337358
return newFilePath.toString();
338359
}
339-
360+
340361
/**
341362
* <p>
342363
* Invoked, only if the file size is greater than max chunk size
343364
* </p>
344-
*
365+
*
345366
* @param request The API request
346367
* @param path Path of the downloaded or partially downloading file, where contents need to be
347368
* appended
@@ -382,15 +403,15 @@ private InvokeResponse invokeIteratively(Request request, Path path) {
382403
new InvokeResponse(response.header(Constants.CONTENT_RANGE_HEADER), response.code(), lastModifiedDate);
383404

384405
} catch (Throwable t) {
385-
LOGGER.error("Exception in feed.invokeIteratively()", t);
406+
LOGGER.error("Exception in feed.invokeIteratively()", t);
386407
responseFlag = new InvokeResponse(null, 400);
387408
}
388409
return responseFlag;
389410
}
390411

391412
/*
392413
* (non-Javadoc)
393-
*
414+
*
394415
* @see com.ebay.feed.api.Feed#processConfigFile(java.lang.String, java.lang.String)
395416
*/
396417
@Override
@@ -496,7 +517,7 @@ public List<com.ebay.feed.model.feed.operation.filter.Response> processConfigFil
496517
* <p>
497518
* Returns a new instance of response, based on the provided inputs
498519
* </p>
499-
*
520+
*
500521
* @param statusCode
501522
* @param message
502523
* @param filePath

0 commit comments

Comments
 (0)