static SphereClientFactory of() { ServiceLoader<SphereHttpClientFactory> loader = ServiceLoader.load(SphereHttpClientFactory.class,SphereClientFactory.class.getClassLoader()); SphereHttpClientFactory httpClientFactory = loader.iterator().next(); if(httpClientFactory == null){ throw new SphereException(new NoClassDefFoundError(SphereHttpClientFactory.class.getCanonicalName())); } return of(httpClientFactory::getClient); } }
private static <T> void fillExceptionWithData(final SphereRequest<T> sphereRequest, final HttpResponse httpResponse, final SphereException exception, final SphereApiConfig config, final HttpRequest httpRequest) { exception.setSphereRequest(sphereRequest); exception.setUnderlyingHttpResponse(httpResponse); exception.setProjectKey(config.getProjectKey()); exception.setHttpRequest(httpRequest); }
@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(); }
private static <T> void fillExceptionWithData(final SphereRequest<T> sphereRequest, final HttpResponse httpResponse, final SphereException exception, final SphereApiConfig config) { exception.setSphereRequest(sphereRequest); exception.setUnderlyingHttpResponse(httpResponse); exception.setProjectKey(config.getProjectKey()); }
private static <T> void fillExceptionWithData(final SphereRequest<T> sphereRequest, final HttpResponse httpResponse, final SphereException exception, final SphereApiConfig config) { exception.setSphereRequest(sphereRequest); exception.setUnderlyingHttpResponse(httpResponse); exception.setProjectKey(config.getProjectKey()); final List<String> errorMessagesDueToMappingError = asList("SearchPhaseExecutionException", "query_fetch", "RemoteTransportException", "SearchParseException", "search/phase/query+fetch"); Optional.ofNullable(httpResponse.getResponseBody()) .map(bytes -> bytesToString(bytes)) .map(body -> errorMessagesDueToMappingError.stream().anyMatch(errorWord -> body.contains(errorWord)) && body.toLowerCase().contains("product")) .ifPresent(containsTerm -> { if (containsTerm) { exception.addNote(format("Maybe it helps to reindex the products https://admin.sphere.io/%s/developers/danger but this may take a while.", config.getProjectKey())); } }); }
throw e; } catch (final Exception e) { throw new SphereException(e); exception.setProjectKey(config.getProjectKey()); exception.setUnderlyingHttpResponse(httpResponse); exception.setHttpRequest(httpRequest); throw exception;
@Override public T deserialize(final HttpResponse httpResponse) { final List<String> deprecationNoticeHeaderValues = getDeprecationNoticeHeaderValues(httpResponse); if (deprecationNoticeHeaderValues.isEmpty()) { return sphereRequest.deserialize(httpResponse); } else { final SphereException sphereException = new SphereDeprecationException(deprecationNoticeHeaderValues); sphereException.setSphereRequest(sphereRequest); sphereException.setUnderlyingHttpResponse(httpResponse); throw sphereException; } }
@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 static String getVersionForJavadoc() { return getVersionForJavadoc(BuildInfo.version()); }
private String responseBodyFormatted() { try { return Optional.ofNullable(getHttpResponse()) .map(r -> r.getResponseBody()) .map(b -> SphereJsonUtils.prettyPrint(new String(b, StandardCharsets.UTF_8))) .map(s -> "http response formatted body: " + s + "\n") .orElse(""); } catch (final Exception e) { return ""; } }
throw new SphereException("negative HTTP response", new HttpException("status code is " + httpResponse.getStatusCode())); throw e; } catch (final Exception e) { throw new SphereException(e); exception.setProjectKey(config.getProjectKey()); exception.setUnderlyingHttpResponse(httpResponse); exception.setHttpRequest(httpRequest); throw exception;
@Override public T deserialize(final HttpResponse httpResponse) { final List<String> deprecationNoticeHeaderValues = getDeprecationNoticeHeaderValues(httpResponse); if (deprecationNoticeHeaderValues.isEmpty()) { return sphereRequest.deserialize(httpResponse); } else { final SphereException sphereException = new SphereDeprecationException(deprecationNoticeHeaderValues); sphereException.setSphereRequest(sphereRequest); sphereException.setUnderlyingHttpResponse(httpResponse); throw sphereException; } }
private void check(final String version, final String expected) { final String versionForJavadoc = SphereException.getVersionForJavadoc(version); assertThat(versionForJavadoc).isEqualTo(expected); } }
throw new SphereException("negative HTTP response", new HttpException("status code is " + httpResponse.getStatusCode())); throw e; } catch (final Exception e) { throw new SphereException(e); exception.setProjectKey(config.getProjectKey()); exception.setUnderlyingHttpResponse(httpResponse); exception.setHttpRequest(httpRequest); throw exception;
private static <T> void fillExceptionWithData(final SphereRequest<T> sphereRequest, final HttpResponse httpResponse, final SphereException exception, final SphereApiConfig config, final HttpRequest httpRequest) { exception.setSphereRequest(sphereRequest); exception.setUnderlyingHttpResponse(httpResponse); exception.setProjectKey(config.getProjectKey()); exception.setHttpRequest(httpRequest); }
@JsonCreator private Tokens(String accessToken, String refreshToken, Long expiresIn) { if (isEmpty(accessToken)) throw new SphereException("OAuth response must contain an access_token. Was empty."); this.accessToken = accessToken; this.refreshToken = refreshToken; this.expiresIn = expiresIn; expiresInZonedDateTime = Optional.ofNullable(expiresIn).map(seconds -> ZonedDateTime.now().plusSeconds(seconds)).orElse(null); }
@Override public T deserialize(final HttpResponse httpResponse) { final List<String> deprecationNoticeHeaderValues = getDeprecationNoticeHeaderValues(httpResponse); if (deprecationNoticeHeaderValues.isEmpty()) { return sphereRequest.deserialize(httpResponse); } else { final SphereException sphereException = new SphereDeprecationException(deprecationNoticeHeaderValues); sphereException.setSphereRequest(sphereRequest); sphereException.setUnderlyingHttpResponse(httpResponse); throw sphereException; } }
@JsonCreator TokensImpl(String accessToken, String refreshToken, @Nullable Long expiresIn) { if (isEmpty(accessToken)) throw new SphereException("OAuth response must contain an access_token. Was empty."); this.accessToken = accessToken; this.refreshToken = refreshToken; this.expiresIn = expiresIn; expiresInZonedDateTime = Optional.ofNullable(expiresIn).map(seconds -> ZonedDateTime.now().plusSeconds(seconds)).orElse(null); }
@Override public Function<HttpResponse, T> resultMapper() { return response -> { final List<String> deprecationNoticeHeaderValues = getDeprecationNoticeHeaderValues(response); if (deprecationNoticeHeaderValues.isEmpty()) { return sphereRequest.resultMapper().apply(response); } else { final SphereException sphereException = new SphereDeprecationException(deprecationNoticeHeaderValues); sphereException.setSphereRequest(sphereRequest); sphereException.setUnderlyingHttpResponse(response); throw sphereException; } }; }
@JsonCreator TokensImpl(String accessToken, String refreshToken, @Nullable Long expiresIn) { if (isEmpty(accessToken)) throw new SphereException("OAuth response must contain an access_token. Was empty."); this.accessToken = accessToken; this.refreshToken = refreshToken; this.expiresIn = expiresIn; expiresInZonedDateTime = Optional.ofNullable(expiresIn).map(seconds -> ZonedDateTime.now().plusSeconds(seconds)).orElse(null); }