diff --git a/src/main/java/com/ebay/feed/api/FeedImpl.java b/src/main/java/com/ebay/feed/api/FeedImpl.java index 6d22fc9..3145188 100755 --- a/src/main/java/com/ebay/feed/api/FeedImpl.java +++ b/src/main/java/com/ebay/feed/api/FeedImpl.java @@ -1,11 +1,11 @@ /* * Copyright 2018 eBay Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under @@ -50,6 +50,8 @@ import com.ebay.feed.validator.FeedValidator; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.ebay.feed.auth.CredentialLoader; +import com.ebay.feed.model.oauth.AuthRequest; /** *
@@ -61,7 +63,7 @@ * contents * *
- * + * * @author shanganesh * */ @@ -72,6 +74,7 @@ public class FeedImpl implements Feed { private FeedUtil feedUtils = null; private FilterUtil filterUtils = null; private FeedValidator feedValidator = null; + static String credentialFilePath = "../../../../../../../sample-credentials/credentials.yaml"; public FeedImpl() { client = @@ -86,7 +89,7 @@ public FeedImpl() { /* * (non-Javadoc) - * + * * @see com.ebay.feed.api.Feed#filter(java.lang.String, com.ebay.feed.model.FeedFilterRequest) */ @Override @@ -125,7 +128,7 @@ public com.ebay.feed.model.feed.operation.filter.Response filter(FeedFilterReque /* * (non-Javadoc) - * + * * @see com.ebay.feed.api.Feed#unzip(java.lang.String, boolean) */ @Override @@ -159,7 +162,7 @@ public com.ebay.feed.model.feed.operation.filter.Response unzip(String filePath) /* * (non-Javadoc) - * + * * @see com.ebay.feed.api.Feed#get(com.ebay.feed.model.FeedRequest) */ @Override @@ -202,7 +205,7 @@ public GetFeedResponse get(FeedRequest feedRequest, String downloadDirectory) { * additional headers including range - Creates default file/folder path and cleansup if already * present *

- * + * * @param feedRequest * @param downloadDirectory Optional local directory where files can be downloaded. Default is * current working directory @@ -237,7 +240,7 @@ private GetFeedResponse process(FeedRequest feedRequest, Path downloadDirectory) // generate static request requestBuilder = feedUtils.generateRequest(feedRequest, requestBuilder); - // generate dynamic header + // generate dynamic header Long chunkSizeLimit = feedUtils.getChunkSizeLimit(feedRequest); requestBuilder.addHeader(Constants.RANGE_HEADER, Constants.RANGE_PREFIX + chunkSizeLimit); @@ -249,13 +252,13 @@ private GetFeedResponse process(FeedRequest feedRequest, Path downloadDirectory) *

* Invokes the feed API with the max range value of 100 MB. If the file is lesser than 100 MB, * then it returns the downloaded file path along with the status. - * + * * If the file is greater than 100 MB - Iteratively calls feed API, with incrementing range * headers - Appends content to file - Downloads entire content and returns with downloaded file * path. - * + * *

- * + * * @param request The API request * @param path Path of the downloaded or partially downloading file, where contents need to be * appended @@ -295,6 +298,24 @@ private GetFeedResponse invoker(Request.Builder requestBuilder, Path path, boole requestBuilder.removeHeader(Constants.RANGE_HEADER); requestBuilder.addHeader(Constants.RANGE_HEADER, val); + LOGGER.debug("Firing the request for next batch" + newUpperLimit); + AuthRequest authRequest = new AuthRequest(credentialFilePath, null); + + // load credentials and generate token + CredentialLoader credentialLoader = new CredentialLoader(authRequest); + String token = "foobar"; + try { + credentialLoader.loadCredentials(); + token = credentialLoader.getOauthResponse().getAccessToken().get().getToken(); + } catch(Exception e){ + LOGGER.debug("Exception in fetching the new access token"); + return new GetFeedResponse(Constants.FAILURE_CODE, Constants.FAILURE, null, null); + } + token = Constants.TOKEN_BEARER_PREFIX + token; + + requestBuilder.removeHeader(Constants.AUTHORIZATION_HEADER); + requestBuilder.addHeader(Constants.AUTHORIZATION_HEADER, token); + requestBuilder.addHeader(Constants.RANGE_HEADER, val); responseFlag = invokeIteratively(requestBuilder.build(), path); @@ -325,23 +346,23 @@ private GetFeedResponse invoker(Request.Builder requestBuilder, Path path, boole */ private String fixFilePath(Path originalFilePath, InvokeResponse invokeResponse) { Path newFilePath = originalFilePath; - if(originalFilePath.toString().contains("null") && !StringUtils.isEmpty(invokeResponse.getLastModified())){ + if(originalFilePath.toString().contains("null") && !StringUtils.isEmpty(invokeResponse.getLastModified())){ String newPath = originalFilePath.toString().replace("null", invokeResponse.getLastModified()); newFilePath = Paths.get(newPath); try { Files.move(originalFilePath, newFilePath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { - LOGGER.error("Unable to rename the bootstrap item feed file with date field", e); + LOGGER.error("Unable to rename the bootstrap item feed file with date field", e); } } return newFilePath.toString(); } - + /** *

* Invoked, only if the file size is greater than max chunk size *

- * + * * @param request The API request * @param path Path of the downloaded or partially downloading file, where contents need to be * appended @@ -382,7 +403,7 @@ private InvokeResponse invokeIteratively(Request request, Path path) { new InvokeResponse(response.header(Constants.CONTENT_RANGE_HEADER), response.code(), lastModifiedDate); } catch (Throwable t) { - LOGGER.error("Exception in feed.invokeIteratively()", t); + LOGGER.error("Exception in feed.invokeIteratively()", t); responseFlag = new InvokeResponse(null, 400); } return responseFlag; @@ -390,7 +411,7 @@ private InvokeResponse invokeIteratively(Request request, Path path) { /* * (non-Javadoc) - * + * * @see com.ebay.feed.api.Feed#processConfigFile(java.lang.String, java.lang.String) */ @Override @@ -496,7 +517,7 @@ public List processConfigFil *

* Returns a new instance of response, based on the provided inputs *

- * + * * @param statusCode * @param message * @param filePath