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); } }
@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); }
@JsonCreator private Tokens(String accessToken, String refreshToken, Optional<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; expiresInInstant = expiresIn.map(seconds -> Instant.now().plusSeconds(seconds)); }
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); } }
@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); }
@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); }
public static SphereClientFactory of() { try { Class<?> clazz; try { clazz = Class.forName("io.sphere.sdk.client.SphereAsyncHttpClientFactory"); } catch (final ClassNotFoundException e) { clazz = Class.forName("io.sphere.sdk.client.SphereApacheHttpClientFactory"); } final Method create = clazz.getMethod("create"); final Supplier<HttpClient> supplier = () -> { try { return (HttpClient) create.invoke(null); } catch (IllegalAccessException | InvocationTargetException e) { throw new SphereException(e); } }; return of(supplier); } catch (ClassNotFoundException | NoSuchMethodException e) { throw new SphereException(e); } }
@Override public Function<HttpResponse, CustomerSignInResult> resultMapper() { return httpResponse -> { if (httpResponse.getStatusCode() == 400) { //TODO this code needs reworking final ErrorResponse errorResponse = resultMapperOf(ErrorResponse.typeReference()).apply(httpResponse); if (errorResponse.getErrors().stream().anyMatch(error -> error.getCode().equals("InvalidCredentials"))) { throw new ErrorResponseException(errorResponse); } else { throw new SphereException(errorResponse.toString()); } } else { return super.resultMapper().apply(httpResponse); } }; }
throw e; } catch (final Exception e) { throw new SphereException(e);
throw new SphereException("negative HTTP response", new HttpException("status code is " + httpResponse.getStatusCode())); throw e; } catch (final Exception e) { throw new SphereException(e);
throw new SphereException("negative HTTP response", new HttpException("status code is " + httpResponse.getStatusCode())); throw e; } catch (final Exception e) { throw new SphereException(e);
public static ExceptionFactory of() { final ExceptionFactory exceptionFactory = new ExceptionFactory() .when(r -> isServiceNotAvailable(r), r -> new ServiceUnavailableException(extractBody(r))) .whenStatus(401, r -> { final String body = extractBody(r); return body.contains("invalid_token") ? new InvalidTokenException() : new UnauthorizedException(body); }) .whenStatus(500, r -> new InternalServerErrorException(extractBody(r))) .whenStatus(502, r -> new BadGatewayException(extractBody(r))) .whenStatus(503, r -> new ServiceUnavailableException(extractBody(r))) .whenStatus(504, r -> new GatewayTimeoutException(extractBody(r))) .whenStatus(409, r -> new ConcurrentModificationException()) .whenStatus(400, r -> { final ErrorResponse errorResponse = JsonUtils.readObject(ErrorResponse.typeReference(), r.getResponseBody().get()); return new ErrorResponseException(errorResponse); } ) .whenStatus(404, r -> new NotFoundException()) //default .when(response -> true, r -> new SphereException("Can't parse backend response.")); return exceptionFactory; }
public static ExceptionFactory of() { final ExceptionFactory exceptionFactory = new ExceptionFactory() .when(r -> isServiceNotAvailable(r), r -> new ServiceUnavailableException(extractBody(r))) .whenStatus(401, r -> { final String body = extractBody(r); return body.contains("invalid_token") ? new InvalidTokenException() : new UnauthorizedException(body); }) .whenStatus(500, r -> new InternalServerErrorException(extractBody(r))) .whenStatus(502, r -> new BadGatewayException(extractBody(r))) .whenStatus(503, r -> new ServiceUnavailableException(extractBody(r))) .whenStatus(504, r -> new GatewayTimeoutException(extractBody(r))) .whenStatus(409, r -> new ConcurrentModificationException()) .whenStatus(413, r -> new RequestEntityTooLargeException()) .whenStatus(400, r -> { final ErrorResponse errorResponse = SphereJsonUtils.readObject(r.getResponseBody(), ErrorResponse.typeReference()); return new ErrorResponseException(errorResponse); } ) .whenStatus(404, r -> new NotFoundException()) //default .when(response -> true, r -> new SphereException("Can't parse backend response.")); return exceptionFactory; }
public static ExceptionFactory of() { final ExceptionFactory exceptionFactory = new ExceptionFactory() .when(r -> isServiceNotAvailable(r), r -> new ServiceUnavailableException(extractBody(r))) .whenStatus(401, r -> { final String body = extractBody(r); return body.contains("invalid_token") ? new InvalidTokenException() : new UnauthorizedException(body,401); }) .whenStatus(403, r -> new ForbiddenException(extractBody(r))) .whenStatus(500, r -> new InternalServerErrorException(extractBody(r))) .whenStatus(502, r -> new BadGatewayException(extractBody(r))) .whenStatus(503, r -> new ServiceUnavailableException(extractBody(r))) .whenStatus(504, r -> new GatewayTimeoutException(extractBody(r))) .whenStatus(409, r -> { final ErrorResponse errorResponse = SphereJsonUtils.readObject(r.getResponseBody(), ErrorResponse.typeReference()); return new ConcurrentModificationException(errorResponse); }) .whenStatus(413, r -> new RequestEntityTooLargeException()) .whenStatus(400, r -> { final String body = extractBody(r); if (body.contains("invalid_scope")) { return new InvalidTokenException(); } final ErrorResponse errorResponse = SphereJsonUtils.readObject(r.getResponseBody(), ErrorResponse.typeReference()); return new ErrorResponseException(errorResponse); }) .whenStatus(404, r -> new NotFoundException()) //default .when(response -> true, r -> new SphereException("Can't parse backend response.")); return exceptionFactory; }
public static ExceptionFactory of() { final ExceptionFactory exceptionFactory = new ExceptionFactory() .when(r -> isServiceNotAvailable(r), r -> new ServiceUnavailableException(extractBody(r))) .whenStatus(401, r -> { final String body = extractBody(r); return body.contains("invalid_token") ? new InvalidTokenException() : new UnauthorizedException(body,401); }) .whenStatus(403, r -> new ForbiddenException(extractBody(r))) .whenStatus(500, r -> new InternalServerErrorException(extractBody(r))) .whenStatus(502, r -> new BadGatewayException(extractBody(r))) .whenStatus(503, r -> new ServiceUnavailableException(extractBody(r))) .whenStatus(504, r -> new GatewayTimeoutException(extractBody(r))) .whenStatus(409, r -> { final ErrorResponse errorResponse = SphereJsonUtils.readObject(r.getResponseBody(), ErrorResponse.typeReference()); return new ConcurrentModificationException(errorResponse); }) .whenStatus(413, r -> new RequestEntityTooLargeException()) .whenStatus(400, r -> { final String body = extractBody(r); if (body.contains("invalid_scope")) { return new InvalidTokenException(); } final ErrorResponse errorResponse = SphereJsonUtils.readObject(r.getResponseBody(), ErrorResponse.typeReference()); return new ErrorResponseException(errorResponse); }) .whenStatus(404, r -> new NotFoundException()) //default .when(response -> true, r -> new SphereException("Can't parse backend response.")); return exceptionFactory; }