@Override public HttpRequestIntent httpRequestIntent() { return HttpRequestIntent.of(HttpMethod.GET, "/"); } });
public HttpRequestIntent prefixPath(final String prefix) { return HttpRequestIntent.of(getHttpMethod(), prefix + getPath(), getHeaders(), getBody()); }
private <T> HttpRequest createHttpRequest(final SphereRequest<T> sphereRequest, final String token) { final String correlationId = correlationIdGenerator.get(); return sphereRequest .httpRequestIntent() .plusHeader(HttpHeaders.X_CORRELATION_ID, correlationId) .plusHeader(HttpHeaders.USER_AGENT, userAgent) .plusHeader(HttpHeaders.ACCEPT_ENCODING, "gzip") .plusHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token) .prefixPath("/" + config.getProjectKey()) .toHttpRequest(config.getApiUrl()); }
public HttpRequestIntent withHeaders(final HttpHeaders headers) { return HttpRequestIntent.of(getHttpMethod(), path, headers, getBody()); }
public static void logProductRequest(final Logger logger, final ProductProjectionSearch request, final PagedSearchResult<ProductProjection> result) { final HttpRequestIntent httpRequest = request.httpRequestIntent(); final String requestBody = printableRequestBody(httpRequest) .map(body -> " with body {" + body + "}") .orElse(""); logger.debug("Fetched {} out of {} products with request {} {}", result.getCount(), result.getTotal(), httpRequest.getHttpMethod(), httpRequest.getPath() + requestBody); }
@Test public void invalidJsonInHttpRequestIntent() throws Throwable { final HttpHeaders headers = VrapHeaders.disableValidation("request", "response"); executing(() -> TestSphereRequest.of(HttpRequestIntent.of(POST, "/categories", "{invalidJson :)").withHeaders(headers))) .resultsInA(ErrorResponseException.class, InvalidJsonInputError.class); }
private static Optional<String> printableRequestBody(final HttpRequestIntent httpRequest) { return Optional.ofNullable(httpRequest.getBody()) .map(body -> { final String bodyAsString; if (httpRequest.getBody() instanceof StringHttpRequestBody) { bodyAsString = ((StringHttpRequestBody) httpRequest.getBody()).getSecuredBody(); } else if (httpRequest.getBody() instanceof FormUrlEncodedHttpRequestBody) { bodyAsString = ((FormUrlEncodedHttpRequestBody) httpRequest.getBody()).getParameters().stream() .map(pair -> pair.getName() + "=" + pair.getValue()) .collect(Collectors.joining("&")); } else { bodyAsString = "**omitted output**"; } return bodyAsString; }); } }
@Override public final int hashCode() { return httpRequestIntent().getPath().hashCode(); }
private static Predicate<RetryContext> isDeleteAndNewVersionIsKnown() { return retryContext -> retryContext.getLatestError() instanceof ConcurrentModificationException && ((ConcurrentModificationException) retryContext.getLatestError()).getCurrentVersion() != null && retryContext.getLatestParameter() instanceof SphereRequest && ((SphereRequest) retryContext.getLatestParameter()).httpRequestIntent().getHttpMethod() == HttpMethod.DELETE; }
@Override public final String getMessage() { StringBuilder builder = new StringBuilder("\n===== BEGIN EXCEPTION OUTPUT =====").append("\n"); final String httpRequest = getSphereRequest().map(x -> x.httpRequestIntent()).map(Object::toString).orElse("<unknown>"); return builder .append("SDK: ").append(BuildInfo.version()).append("\n") .append("project: ").append(getProjectKey().orElse("<unknown>")).append("\n") .append(getSphereRequest().map(x -> x.httpRequestIntent()).map(x -> "" + x.getHttpMethod() + " " + x.getPath()).map(x -> "endpoint: " + x + "\n").orElse("")) .append("Java: ").append(System.getProperty("java.version")).append("\n") .append("cwd: ").append(System.getProperty("user.dir")).append("\n") .append("date: ").append(new Date()).append("\n") .append("sphere request: ").append(getSphereRequest().map(Object::toString).orElse("<unknown>")).append("\n") //duplicated in case SphereRequest does not implement a proper to String .append("http request: ").append(httpRequest).append("\n") .append("http response: ").append(getHttpResponse().map(Object::toString).orElse("<unknown>")).append("\n") .append(Optional.ofNullable(super.getMessage()).map(s -> "detailMessage: " + s + "\n").orElse("")) .append("additional notes: ").append(additionalNotes).append("\n") .append("Javadoc: ").append("http://sphereio.github.io/sphere-jvm-sdk/javadoc/").append(BuildInfo.version()).append("/").append(this.getClass().getCanonicalName().replace('.', '/')).append(".html").append("\n") .append("===== END EXCEPTION OUTPUT =====").toString(); }
private String requestBodyFormatted() { try { final Optional<String> stringBodyOfHttpRequest = stringBodyOfHttpRequest(); final Optional<String> stringBodyOfHttpRequestIntentSupplier = Optional.ofNullable(httpRequestIntent) .map(r -> r.getBody()) .filter(r -> r instanceof StringHttpRequestBody) .map(r -> ((StringHttpRequestBody) r).getSecuredBody()); return Optional.ofNullable(stringBodyOfHttpRequest.orElse(stringBodyOfHttpRequestIntentSupplier.orElse(null))) .map(SphereJsonUtils::prettyPrint) .map(s -> "http request formatted body: " + s + "\n") .orElse(""); } catch (final Exception e) { return ""; } }
@Override public HttpRequestIntent httpRequestIntent() { return HttpRequestIntent.of(HttpMethod.GET, "/"); } });
public HttpRequestIntent prefixPath(final String prefix) { return HttpRequestIntent.of(getHttpMethod(), prefix + getPath(), getHeaders(), getBody()); }
private <T> HttpRequest createHttpRequest(final SphereRequest<T> sphereRequest, final String token) { final String correlationId = correlationIdGenerator.get(); return sphereRequest .httpRequestIntent() .plusHeader(HttpHeaders.X_CORRELATION_ID, correlationId) .plusHeader(HttpHeaders.USER_AGENT, userAgent) .plusHeader(HttpHeaders.ACCEPT_ENCODING, "gzip") .plusHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token) .prefixPath("/" + config.getProjectKey()) .toHttpRequest(config.getApiUrl()); }
@Override public final String getMessage() { StringBuilder builder = new StringBuilder(); return builder .append(Optional.ofNullable(super.getMessage()).map(s -> "detailMessage: " + s + "\n").orElse("")) .append(httpSummary()) .append(responseBodyFormatted()) .append("http response: ").append(Optional.ofNullable(getHttpResponse()).map(Object::toString).orElse("<unknown>")).append("\n") .append("SDK: ").append(BuildInfo.version()).append("\n") .append("project: ").append(Optional.ofNullable(getProjectKey()).orElse("<unknown>")).append("\n") .append(Optional.ofNullable(getSphereRequest()).map(x -> x.httpRequestIntent()).map(x -> "" + x.getHttpMethod() + " " + x.getPath()).map(x -> "endpoint: " + x + "\n").orElse("")) .append("Java: ").append(System.getProperty("java.version")).append("\n") .append("cwd: ").append(System.getProperty("user.dir")).append("\n") .append("sphere request: ").append(Optional.ofNullable(getSphereRequest()).map(Object::toString).orElse("<unknown>")).append("\n") //duplicated in case SphereRequest does not implement a proper to String .append(httpRequestLine()) .append(requestBodyFormatted()) .append("additional notes: ").append(additionalNotes).append("\n") .append("Javadoc: ").append("http://commercetools.github.io/commercetools-jvm-sdk/apidocs/").append(this.getClass().getCanonicalName().replace('.', '/')).append(".html").append("\n") .toString(); }
@Test public void priceSelectionParameterCanBeAdded() { assertThat(SEARCH_WITH_FULL_PRICE_SELECTION.httpRequestIntent().getBody().toString()) .contains("priceCurrency=EUR&priceCountry=DE&priceCustomerGroup=customer-group-id&priceChannel=channel-id"); assertThat(SEARCH_WITH_FULL_PRICE_SELECTION.getPriceSelection()).isEqualTo(FULL_PRICE_SELECTION); }
@Override public final int hashCode() { return httpRequestIntent().getPath().hashCode(); }
@Override public HttpRequestIntent httpRequestIntent() { return HttpRequestIntent.of(GET, ""); }
public HttpRequestIntent withPath(final String path) { return HttpRequestIntent.of(getHttpMethod(), path, getHeaders(), getBody()); }