protected void delete(T item) { try { execute(deleteCommand(item)); } catch (final Exception e) { getLogger("test.fixtures").warn(() -> String.format("tried to delete %s but an Exception occurred: %s", item, e.toString())); } }
private static List<Category> categoriesOrganizedAsTree(List<Category> allCategoriesAsFlatListWithoutChildrenSettings) { final List<Category> categoriesOrganizedInTrees; if (allCategoriesAsFlatListWithoutChildrenSettings == null) { categoriesOrganizedInTrees = Collections.emptyList(); LOGGER.warn(() -> "null passed for categories."); } else if (allCategoriesAsFlatListWithoutChildrenSettings.size() == 0) { categoriesOrganizedInTrees = Collections.emptyList(); } else { final Pair<List<Category>, List<Category>> partition = partition(allCategoriesAsFlatListWithoutChildrenSettings, new CategoryHasParentPredicate()); final List<Category> rootCategories = partition.getLeft(); final List<Category> categoriesWithoutParents = partition.getRight(); final Multimap<String, Category> categoriesByParentId = buildParentMultiMap(rootCategories); categoriesOrganizedInTrees = buildTreeRecursive(Optional.<Category>empty(), categoriesWithoutParents, new ArrayList<>(), categoriesByParentId); } return categoriesOrganizedInTrees; }
private static List<Category> categoriesOrganizedAsTree(List<Category> allCategoriesAsFlatListWithoutChildrenSettings) { final List<Category> categoriesOrganizedInTrees; if (allCategoriesAsFlatListWithoutChildrenSettings == null) { categoriesOrganizedInTrees = Collections.emptyList(); LOGGER.warn(() -> "null passed for categories."); } else if (allCategoriesAsFlatListWithoutChildrenSettings.size() == 0) { categoriesOrganizedInTrees = Collections.emptyList(); } else { final Pair<List<Category>, List<Category>> partition = partition(allCategoriesAsFlatListWithoutChildrenSettings, new CategoryHasParentPredicate()); final List<Category> rootCategories = partition.getLeft(); final List<Category> categoriesWithoutParents = partition.getRight(); final Multimap<String, Category> categoriesByParentId = buildParentMultiMap(rootCategories); categoriesOrganizedInTrees = buildTreeRecursive(Optional.<Category>empty(), categoriesWithoutParents, new ArrayList<>(), categoriesByParentId); } return categoriesOrganizedInTrees; }
private static List<Category> categoriesOrganizedAsTree(List<Category> allCategoriesAsFlatListWithoutChildrenSettings) { final List<Category> categoriesOrganizedInTrees; if (allCategoriesAsFlatListWithoutChildrenSettings == null) { categoriesOrganizedInTrees = Collections.emptyList(); LOGGER.warn(() -> "null passed for categories."); } else if (allCategoriesAsFlatListWithoutChildrenSettings.size() == 0) { categoriesOrganizedInTrees = Collections.emptyList(); } else { final Pair<List<Category>, List<Category>> partition = partition(allCategoriesAsFlatListWithoutChildrenSettings, new CategoryHasParentPredicate()); final List<Category> rootCategories = partition.getLeft(); final List<Category> categoriesWithoutParents = partition.getRight(); final Multimap<String, Category> categoriesByParentId = buildParentMultiMap(rootCategories); categoriesOrganizedInTrees = buildTreeRecursive(Optional.<Category>empty(), categoriesWithoutParents, new ArrayList<>(), categoriesByParentId); } return categoriesOrganizedInTrees; }
private void scheduleNextRefresh(Tokens tokens) { if (!tokens.getExpiresIn().isPresent()) { AUTH_LOGGER.warn(() -> "Authorization server did not provide expires_in for the access token."); return; } if (tokens.getExpiresIn().get() * 1000 < TOKEN_ABOUT_TO_EXPIRE_MS) { AUTH_LOGGER.warn(() -> "Authorization server returned an access token with a very short validity of " + tokens.getExpiresIn().get() + "s!"); return; } long refreshTimeout = tokens.getExpiresIn().get() * 1000 - TOKEN_ABOUT_TO_EXPIRE_MS; AUTH_LOGGER.debug(() -> "Scheduling next token refresh " + refreshTimeout / 1000 + "s from now."); refreshTimer.schedule(new TimerTask() { public void run() { beginRefresh(); } }, refreshTimeout); }
public <T extends SphereError> Optional<T> as(final Class<T> errorClass) { final Optional<String> classErrorCodeOption = codeValueOf(errorClass); Optional<T> result = Optional.empty(); if (classErrorCodeOption.map(classErrCode -> classErrCode.equals(code)).orElse(true)) { final ObjectMapper objectMapper = JsonUtils.newObjectMapper(); final JsonNode jsonNode = objectMapper.createObjectNode() .put("code", code) .put("message", message) .setAll(furtherFields); try { final T object = JsonUtils.readObject(errorClass, jsonNode); result = code.equals(object.getCode()) ? Optional.of(object) : Optional.<T>empty(); } catch (final JsonException e) { SphereInternalLogger.getLogger(SphereError.class).warn(() -> "cannot cast error: " + e); result = Optional.empty(); } } return result; }
protected void delete(T item) { try { client().execute(deleteCommand(item)); } catch (final Exception e) { getLogger("test.fixtures").warn(() -> String.format("tried to delete %s but an Exception occurred: %s", item, e.toString())); } }
private static <T> T processHttpResponse(final SphereRequest<T> sphereRequest, final ObjectMapper objectMapper, final SphereApiConfig config, final HttpResponse httpResponse) { final SphereInternalLogger logger = getLogger(httpResponse); logger.debug(() -> httpResponse); logger.trace(() -> httpResponse.getStatusCode() + "\n" + Optional.ofNullable(httpResponse.getResponseBody()).map(body -> SphereJsonUtils.prettyPrint(bytesToString(body))).orElse("No body present.") + "\n"); final List<String> notices = httpResponse.getHeaders().getHeadersAsMap().get(SphereHttpHeaders.X_DEPRECATION_NOTICE); if (notices != null) { notices.stream().forEach(message -> logger.warn(() -> "Deprecation notice : " + message)); } return parse(sphereRequest, objectMapper, config, httpResponse); }
private static <T> T processHttpResponse(final SphereRequest<T> sphereRequest, final ObjectMapper objectMapper, final SphereApiConfig config, final HttpResponse httpResponse, final HttpRequest httpRequest) { final SphereInternalLogger logger = getLogger(httpResponse); logger.debug(() -> httpResponse); logger.trace(() -> httpResponse.getStatusCode() + "\n" + Optional.ofNullable(httpResponse.getResponseBody()).map(body -> SphereJsonUtils.prettyPrint(bytesToString(body))).orElse("No body present.")); final List<String> notices = httpResponse.getHeaders().getHeadersAsMap().get(SphereHttpHeaders.X_DEPRECATION_NOTICE); if (notices != null) { notices.forEach(message -> logger.warn(() -> "Deprecation notice : " + message)); } return parse(sphereRequest, objectMapper, config, httpResponse, httpRequest); }
private static <T> T processHttpResponse(final SphereRequest<T> sphereRequest, final ObjectMapper objectMapper, final SphereApiConfig config, final HttpResponse httpResponse, final HttpRequest httpRequest) { final SphereInternalLogger logger = getLogger(httpResponse); logger.debug(() -> httpResponse); logger.trace(() -> httpResponse.getStatusCode() + "\n" + Optional.ofNullable(httpResponse.getResponseBody()).map(body -> SphereJsonUtils.prettyPrint(bytesToString(body))).orElse("No body present.")); final List<String> notices = httpResponse.getHeaders().getHeadersAsMap().get(SphereHttpHeaders.X_DEPRECATION_NOTICE); if (notices != null) { notices.forEach(message -> logger.warn(() -> "Deprecation notice : " + message)); } return parse(sphereRequest, objectMapper, config, httpResponse, httpRequest); }
static <T> Function<HttpResponse, T> preProcess(final SphereRequest<T> sphereRequest, final ObjectMapper objectMapper, final SphereApiConfig config) { return httpResponse -> { final SphereInternalLogger logger = getLogger(httpResponse); logger.debug(() -> httpResponse); logger.trace(() -> httpResponse.getStatusCode() + "\n" + httpResponse.getResponseBody().map(body -> JsonUtils.prettyPrintJsonStringSecure(bytesToString(body))).orElse("No body present.") + "\n"); final List<String> notices = httpResponse.getHeaders().getHeadersAsMap().get(SphereHttpHeaders.X_DEPRECATION_NOTICE); if (notices != null) { notices.stream().forEach(message -> logger.warn(() -> "Deprecation notice : " + message)); } return parse(httpResponse, sphereRequest, objectMapper, config); }; }
@Override public String getAccessToken() { synchronized (accessTokenLock) { Optional<ValidationE<AccessToken>> tokenResult = waitForToken(); if (!tokenResult.isPresent()) { // Shouldn't happen as the timer should refresh the token soon enough. AUTH_LOGGER.warn(() -> "Access token expired, blocking until a new one is available."); beginRefresh(); tokenResult = waitForToken(); if (!tokenResult.isPresent()) { throw new SphereClientException("Access token expired immediately after refresh."); } } if (tokenResult.get().isError()) { beginRefresh(); // retry on error (essential to recover from backend errors) throw tokenResult.get().getError(); } return tokenResult.get().getValue().getAccessToken(); } }
protected void delete(Product item) { try { client().executeBlocking(deleteCommand(item)); } catch (final Exception e) { getLogger("test.fixtures").warn(() -> String.format("tried to delete %s but an Exception occurred: %s", item, e.toString())); } }