if (waeResponse.hasEntity()) { LOGGER.log(Level.FINE, LocalizationMessages .EXCEPTION_MAPPING_WAE_ENTITY(waeResponse.getStatus()), throwable); return waeResponse; tracingLogger.logDuration(ServerTraceEvent.EXCEPTION_MAPPING, timestamp, mapper, throwable, throwable.getLocalizedMessage(), mappedResponse != null ? mappedResponse.getStatusInfo() : "-no-response-"); throwable.getLocalizedMessage(), mapper.getClass().getName(), mappedResponse.getStatusInfo().getReasonPhrase(), mappedResponse.getStatusInfo().getStatusCode(), mappedResponse.getStatusInfo().getFamily()); LOGGER.log(Level.FINER, message); return Response.noContent().build(); mapperThrowable); LOGGER.log(Level.SEVERE, LocalizationMessages.EXCEPTION_MAPPER_FAILED_FOR_EXCEPTION(), throwable); return Response.serverError().build(); return Response.status(Response.Status.BAD_REQUEST).build();
if (entity == null) builtResponse = (BuiltResponse) Response.noContent().build(); metadata.putAll(r.getMetadata()); builtResponse = new BuiltResponse(r.getStatus(), r.getStatusInfo().getReasonPhrase(), metadata, r.getEntity(), method.getMethodAnnotations()); BuiltResponse jaxrsResponse = (BuiltResponse) Response.ok(entity).build();
private Optional<SignedJwt> getAndCacheAppTokenFromServer() { MultivaluedMap<String, String> formData = new MultivaluedHashMap<>(); formData.putSingle("grant_type", "client_credentials"); formData.putSingle("scope", "urn:opc:idm:__myscopes__"); Response tokenResponse = tokenEndpoint .request() .accept(MediaType.APPLICATION_JSON_TYPE) .post(Entity.form(formData)); if (tokenResponse.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { JsonObject response = tokenResponse.readEntity(JsonObject.class); String accessToken = response.getString(ACCESS_TOKEN_KEY); LOGGER.finest(() -> "Access token: " + accessToken); SignedJwt signedJwt = SignedJwt.parseToken(accessToken); this.appToken = signedJwt; this.appJwt = signedJwt.getJwt(); return Optional.of(signedJwt); } else { LOGGER.severe("Failed to obtain access token for application to read groups" + " from IDCS. Response code: " + tokenResponse.getStatus() + ", entity: " + tokenResponse.readEntity(String.class)); return Optional.empty(); } }
@Override public Response toResponse(E exception) { // If we're dealing with a web exception, we can service certain types of request (like // redirection or server errors) better and also propagate properties of the inner response. if (exception instanceof WebApplicationException) { final Response response = ((WebApplicationException) exception).getResponse(); Response.Status.Family family = response.getStatusInfo().getFamily(); if (family.equals(Response.Status.Family.REDIRECTION)) { return response; } if (family.equals(Response.Status.Family.SERVER_ERROR)) { logException(exception); } return Response.fromResponse(response) .type(MediaType.APPLICATION_JSON_TYPE) .entity(new ErrorMessage(response.getStatus(), exception.getLocalizedMessage())) .build(); } // Else the thrown exception is a not a web exception, so the exception is most likely // unexpected. We'll create a unique id in the server error response that is also logged for // correlation final long id = logException(exception); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) .type(MediaType.APPLICATION_JSON_TYPE) .entity(new ErrorMessage(formatErrorMessage(id, exception))) .build(); }
@Override public Response toResponse(final Exception ex) { final String errorTrackingId = UUID.randomUUID().toString(); Tuple2<String, Status> error = new Tuple2<>(MapOfExceptionsToErrorCodes.INTERNAL_SERVER_ERROR, Status.INTERNAL_SERVER_ERROR); Optional<Tuple2<String, Status>> errorFromLookup = find(ex.getClass()); if (errorFromLookup.isPresent()) { error = errorFromLookup.get(); } else { if(ex instanceof javax.ws.rs.WebApplicationException){ javax.ws.rs.WebApplicationException rsEx = ((javax.ws.rs.WebApplicationException)ex); error = tuple(rsEx.getResponse().getStatusInfo().getReasonPhrase(),Status.fromStatusCode(rsEx.getResponse().getStatus())); } } logger.error(String.format("%s Error id: %s, %s", error._1(), errorTrackingId, ex.getMessage()), ex); Response.ResponseBuilder responseBuilder = Response.status(error._2()).type(MediaType.APPLICATION_JSON_TYPE); if (showDetails) { responseBuilder.entity(new ExceptionWrapper(error._1(), String.format("Error id: %s %s", errorTrackingId, ex.getMessage()))); } else { responseBuilder.entity(new ExceptionWrapper(MapOfExceptionsToErrorCodes.INTERNAL_SERVER_ERROR, errorTrackingId)); } return responseBuilder.build(); } }
if (enabled == null || !Boolean.valueOf(enabled)) { final Response r = request.put(Entity.text(Boolean.TRUE.toString())); if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { throw new WebApplicationException(String.format("Unable to enable projects: %s", r.getStatusInfo().getReasonPhrase()), r.getStatus());
response.bufferEntity(); final int statusCode = response.getStatus(); final Response.Status status = Response.Status.fromStatusCode(statusCode); final Response.Status.Family statusFamily = response.getStatusInfo().getFamily(); webAppException = createExceptionForFamily(response, statusFamily); } else { break; default: final Response.Status.Family statusFamily = response.getStatusInfo().getFamily(); webAppException = createExceptionForFamily(response, statusFamily); return new ResponseProcessingException(response, webAppException); } catch (final Throwable t) { return new ResponseProcessingException(response, LocalizationMessages.RESPONSE_TO_EXCEPTION_CONVERSION_FAILED(), t);
@Override public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { if (!responseContext.getStatusInfo().getFamily().equals(Response.Status.Family.REDIRECTION)) { return; } Response resp = requestContext.getClient().target(responseContext.getLocation()).request() .method(requestContext.getMethod()); responseContext.setEntityStream((InputStream) resp.getEntity()); responseContext.setStatusInfo(resp.getStatusInfo()); responseContext.setStatus(resp.getStatus()); } }
final MultivaluedMap<String, String> responseHeaders = clientResponse.getStringHeaders(); if (Response.Status.NOT_FOUND.getStatusCode() == clientResponse.getStatusInfo().getStatusCode()) { throw new ResourceNotFoundException(clientResponse.readEntity(String.class)); } else if (Response.Status.FORBIDDEN.getStatusCode() == clientResponse.getStatusInfo().getStatusCode() || Response.Status.UNAUTHORIZED.getStatusCode() == clientResponse.getStatusInfo().getStatusCode()) { throw new AccessDeniedException(clientResponse.readEntity(String.class)); } else if (Response.Status.OK.getStatusCode() != clientResponse.getStatusInfo().getStatusCode()) { throw new IllegalStateException(clientResponse.readEntity(String.class));
.post(Entity.form(formValues)); if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { JsonObject jsonResponse = response.readEntity(JsonObject.class); String tokenValue = jsonResponse.getString("access_token"); String entity = response.readEntity(String.class); LOGGER.log(Level.FINE, "Invalid token or failed request when connecting to OIDC Token Endpoint. Response: " + entity); res.status(Http.Status.UNAUTHORIZED_401);
private T getResponse(Response response) { Family statusFamily = response.getStatusInfo().getFamily(); if (response.getMediaType().getSubtype().equals(MediaType.APPLICATION_JSON_TYPE.getSubtype())) { try { if (statusFamily == Family.INFORMATIONAL || statusFamily == Family.SUCCESSFUL) { T content = response.readEntity(respContentClass); return content; } else { JsonResponse jsonRes = response.readEntity(JsonResponse.class); throw new IllegalStateException(jsonRes.getErrorMsg()); } } catch (ProcessingException e) { throw new IllegalStateException(e.getMessage()); } } else { throw new IllegalStateException("Cannot Connect To Server."); } }
private Response _follow3Redirects(Response currentResponse) { Response.StatusType statusType = currentResponse.getStatusInfo(); if (statusType.getFamily() != Response.Status.Family.REDIRECTION) { return currentResponse; } AtomicInteger counter = new AtomicInteger(); Response response = currentResponse; while ((statusType.getFamily() == Response.Status.Family.REDIRECTION) && (counter.incrementAndGet() <= 3)) { String location = response.getHeaderString(HttpHeaders.LOCATION); if (StringUtils.isEmpty(location)) { return response; } if (_log.isDebugEnabled()) { _log.debug( "Redirect location {}#: {}", counter.get(), location); } response.close(); WebTarget webTarget = _client.target(location); Invocation.Builder builder = webTarget.request(APPLICATION_JSON_LD); response = builder.get(); } return response; }
/** * Send message to webapp to refresh SearcherManagers for given projects. * This is used for partial reindex. * * @param subFiles list of directories to refresh corresponding SearcherManagers * @param host the host address to receive the configuration */ public void signalTorefreshSearcherManagers(List<String> subFiles, String host) { // subFile entries start with path separator so get basename // to convert them to project names. subFiles.stream().map(proj -> new File(proj).getName()).forEach(project -> { Response r = ClientBuilder.newClient() .target(host) .path("api") .path("v1") .path("system") .path("refresh") .request() .put(Entity.text(project)); if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { LOGGER.log(Level.WARNING, "Could not refresh search manager for {0}", project); } }); }
@Override public void completed(final ClientResponse response, final RequestScope scope) { if (responseFuture.isCancelled()) { response.close(); failed(new ProcessingException( new CancellationException(LocalizationMessages.ERROR_REQUEST_CANCELLED()))); return; } final T result; if (callbackParamClass == Response.class) { result = callbackParamClass.cast(new InboundJaxrsResponse(response, scope)); responseFuture.complete(result); callback.completed(result); } else if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { result = response.readEntity(new GenericType<T>(callbackParamType)); responseFuture.complete(result); callback.completed(result); } else { failed(convertToException(new InboundJaxrsResponse(response, scope))); } }
private <T> T translate(final ClientResponse response, final RequestScope scope, final Class<T> responseType) throws ProcessingException { if (responseType == Response.class) { return responseType.cast(new InboundJaxrsResponse(response, scope)); } if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { try { return response.readEntity(responseType); } catch (final ProcessingException ex) { if (ex.getClass() == ProcessingException.class) { throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), ex.getCause()); } throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), ex); } catch (final WebApplicationException ex) { throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), ex); } catch (final Exception ex) { throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), LocalizationMessages.UNEXPECTED_ERROR_RESPONSE_PROCESSING(), ex); } } else { throw convertToException(new InboundJaxrsResponse(response, scope)); } }
@Override public String toString() { return "ClientResponse{" + "method=" + requestContext.getMethod() + ", uri=" + requestContext.getUri() + ", status=" + status.getStatusCode() + ", reason=" + status.getReasonPhrase() + "}"; }
@Override public String toString() { return "OutboundJaxrsResponse{" + "status=" + status.getStatusCode() + ", reason=" + status.getReasonPhrase() + ", hasEntity=" + context.hasEntity() + ", closed=" + closed + ", buffered=" + buffered + "}"; }
@Override public String toString() { return "OutboundJaxrsResponse{" + "status=" + status.getStatusCode() + ", reason=" + status.getReasonPhrase() + ", hasEntity=" + context.hasEntity() + ", closed=" + closed + ", buffered=" + buffered + "}"; }
@Override public String toString() { return "ClientResponse{" + "method=" + requestContext.getMethod() + ", uri=" + requestContext.getUri() + ", status=" + status.getStatusCode() + ", reason=" + status.getReasonPhrase() + "}"; }
/** * Create a new status type instance with a custom reason phrase. * * @param status response status type. * @param reason custom response status reason phrase. * @return new status type instance representing a given response status code and custom reason phrase. */ public static StatusType from(StatusType status, String reason) { return new StatusImpl(status.getStatusCode(), reason); }