@Override public ResponseBuilder status(int status, final String reasonPhrase) { if (status < 100 || status > 599) { throw new IllegalArgumentException("Response status must not be less than '100' or greater than '599'"); } final Status.Family family = Status.Family.familyOf(status); this.status = new StatusType() { @Override public int getStatusCode() { return status; } @Override public Status.Family getFamily() { return family; } @Override public String getReasonPhrase() { return reasonPhrase; } }; return this; }
final RemoteNiFiUtils utils = new RemoteNiFiUtils(isApiSecure ? sslContext : null); final Response requestAccountResponse = utils.issueRegistrationRequest(apiClient.getBaseUrl()); if (Response.Status.Family.SUCCESSFUL.equals(requestAccountResponse.getStatusInfo().getFamily())) { logger.info("{} Issued a Request to communicate with remote instance", this); } else {
@Override public int hashCode() { int result = code; result = 31 * result + (reason != null ? reason.hashCode() : 0); result = 31 * result + family.hashCode(); return result; } }
public Model loadModel(Query query, MultivaluedMap<String, String> params, MultivaluedMap<String, String> headers) { ClientResponse cr = null; try { if (getQueryURLLength(query, params) > getMaxGetRequestSize()) cr = post(query, getReadableMediaTypes(Model.class), params, headers); else cr = get(query, getReadableMediaTypes(Model.class), params, headers); if (!cr.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) { if (log.isErrorEnabled()) log.error("Query request to endpoint: {} unsuccessful. Reason: {}", getWebResource().getURI(), cr.getStatusInfo().getReasonPhrase()); throw new ClientException(cr); } return cr.getEntity(Model.class); } finally { if (cr != null) cr.close(); } }
public void deleteDefault(MultivaluedMap<String, String> params, MultivaluedMap<String, String> headers) { ClientResponse cr = null; try { MultivaluedMap<String, String> mergedParams = new MultivaluedMapImpl(); if (params != null) mergedParams.putAll(params); mergedParams.putSingle("default", ""); if (log.isDebugEnabled()) log.debug("DELETE default graph from Graph Store {}", getWebResource().getURI()); cr = delete(mergedParams, headers); if (!cr.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) { if (log.isErrorEnabled()) log.error("Request to graph store: {} unsuccessful. Reason: {}", getWebResource().getURI(), cr.getStatusInfo().getReasonPhrase()); throw new ClientException(cr); } } finally { if (cr != null) cr.close(); } }
public ResultSetRewindable select(Query query, MultivaluedMap<String, String> params, MultivaluedMap<String, String> headers) { ClientResponse cr = null; try { if (getQueryURLLength(query, params) > getMaxGetRequestSize()) cr = post(query, getReadableMediaTypes(ResultSet.class), params, headers); else cr = get(query, getReadableMediaTypes(ResultSet.class), params, headers); if (!cr.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) { if (log.isErrorEnabled()) log.error("Query request to endpoint: {} unsuccessful. Reason: {}", getWebResource().getURI(), cr.getStatusInfo().getReasonPhrase()); throw new ClientException(cr); } return cr.getEntity(ResultSetRewindable.class); } finally { if (cr != null) cr.close(); } }
public void add(String uri, Model model, MultivaluedMap<String, String> params, MultivaluedMap<String, String> headers) { ClientResponse cr = null; try { MultivaluedMap<String, String> mergedParams = new MultivaluedMapImpl(); if (params != null) mergedParams.putAll(params); mergedParams.putSingle("graph", uri); if (log.isDebugEnabled()) log.debug("POST Model to Graph Store {} with named graph URI: {}", getWebResource().getURI(), uri); cr = post(getDefaultMediaType(), model, mergedParams, headers); if (!cr.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) { if (log.isErrorEnabled()) log.error("Request to graph store: {} unsuccessful. Reason: {}", getWebResource().getURI(), cr.getStatusInfo().getReasonPhrase()); throw new ClientException(cr); } } finally { if (cr != null) cr.close(); } }
/** * Test normal redirection of a timegate request. */ @Test @DisplayName("Test normal redirection of a timegate request") default void testTimeGateRedirected() { final String date = RFC_1123_DATE_TIME.withZone(UTC).format(now()); final String location; try (final Response res = target(getResourceLocation()).request().header(ACCEPT_DATETIME, date).head()) { if (REDIRECTION.equals(res.getStatusInfo().getFamily())) { location = res.getLocation().toString(); } else { location = getResourceLocation(); } } try (final Response res = target(location).request().header(ACCEPT_DATETIME, date).head()) { assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a valid response"); assertNotNull(res.getHeaderString(MEMENTO_DATETIME), "Check for a Memento-Datetime header"); } }
public <T> Result<T> execute(URI uri, Function_WithExceptions<HttpResponse, T, ?> func, String authString) { currentUri = uri; Result<T> result = new Result<T>(uri); HttpGet request = new HttpGet(uri); if (authString != null) { request.addHeader("Authorization", authString); } try (CloseableHttpResponse response = httpClient.execute(request)) { int statusCode = response.getStatusLine().getStatusCode(); result.setStatusCode(statusCode); if (!Response.Status.Family.SUCCESSFUL.equals(Response.Status.Family.familyOf(statusCode))) { result.addError(new RemoteException(statusCode, response.getStatusLine().getReasonPhrase(), uri)); } else { result.accept(func.apply(response)); } } catch (Exception e) { result.addError(e); } return result; }
@Override public Optional<Keyspace> keyspace(String keyspace) throws GraknClientException { URI fullURI = UriBuilder.fromUri(uri.toURI()) .path(REST.resolveTemplate(REST.WebPath.KB_KEYSPACE, keyspace)) .build(); ClientResponse response = client.resource(fullURI) .accept(APPLICATION_JSON) .get(ClientResponse.class); Response.StatusType status = response.getStatusInfo(); LOG.debug("Received {}", status.getStatusCode()); if (status.getStatusCode() == Status.NOT_FOUND.getStatusCode()) { return Optional.empty(); } String entity = response.getEntity(String.class); if (!status.getFamily().equals(Family.SUCCESSFUL)) { throw new GraknClientException("Failed keyspace. Error status: " + status.getStatusCode() + ", error info: " + entity, response.getStatusInfo()); } response.close(); return Optional.of(Keyspace.of(keyspace)); } }
/** * Test normal redirection of a timegate request. */ @Test @DisplayName("Test normal redirection of a timegate request") default void testTimeGateRedirected() { final String date = RFC_1123_DATE_TIME.withZone(UTC).format(now()); final String location; try (final Response res = target(getResourceLocation()).request().header(ACCEPT_DATETIME, date).head()) { if (REDIRECTION.equals(res.getStatusInfo().getFamily())) { location = res.getLocation().toString(); } else { location = getResourceLocation(); } } try (final Response res = target(location).request().header(ACCEPT_DATETIME, date).head()) { assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a valid response"); assertNotNull(res.getHeaderString(MEMENTO_DATETIME), "Check for a Memento-Datetime header"); } }
/** * Try to determine response by status code, and send response. */ private void sendFailureRespDeterminedByStatus(RoutingContext context) { Family statusFamily = Family.familyOf(context.statusCode()); if (Family.CLIENT_ERROR.equals(statusFamily) || Family.SERVER_ERROR.equals(statusFamily) || Family.OTHER .equals(statusFamily)) { context.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.WILDCARD) .setStatusCode(context.statusCode()).end(); } else { // it seems the status code is not set properly context.response().putHeader(HttpHeaders.CONTENT_TYPE, MediaType.WILDCARD) .setStatusCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()) .setStatusMessage(Status.INTERNAL_SERVER_ERROR.getReasonPhrase()) .end(wrapResponseBody(Status.INTERNAL_SERVER_ERROR.getReasonPhrase())); } context.response().close(); }
@Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { WebResource resource = getResource(listener); if (resource != null) { try { ClientResponse response = resource.post(ClientResponse.class); if (Response.Status.Family.SUCCESSFUL.equals(response.getClientResponseStatus().getFamily())) { listener.getLogger().printf("Successfully triggered JIRA to update builds of this job\n"); } else { listener.error("Unable to trigger JIRA to update builds of this job, JIRA responded with [%d] %s", response.getClientResponseStatus().getStatusCode(), response.getClientResponseStatus().getReasonPhrase()); } } catch (Exception e) { listener.error("Failed to triggered JIRA to update builds of this job -> %s", e.getMessage()); } } return true; }
public void update(UpdateRequest updateRequest, MultivaluedMap<String, String> params, MultivaluedMap<String, String> headers) { ClientResponse cr = null; try { cr = post(updateRequest, params, headers); if (!cr.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) { if (log.isErrorEnabled()) log.error("Query request to endpoint: {} unsuccessful. Reason: {}", getWebResource().getURI(), cr.getStatusInfo().getReasonPhrase()); throw new ClientException(cr); } } finally { if (cr != null) cr.close(); } }
@Override @SuppressFBWarnings("SLF4J_FORMAT_SHOULD_BE_CONST") public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { if (responseContext.getStatusInfo().getFamily().equals( Response.Status.Family.SUCCESSFUL) && !isContentTypeCompatible(responseContext.getMediaType())) { log.error(ERROR_MSG); String snippet = String.format( "Wrong content type received: [%s]", responseContext.getMediaType()); log.error(snippet); throw new IllegalStateException(snippet); } } }
/** * Checks whether a given resource (with absolute URL!) is available with a HEAD request on it. */ public static boolean isResourceAvailable(String path) { Client client = Client.create(); WebResource wr = client.resource(path); boolean res; try { ClientResponse response = wr.head(); res = (response.getStatusInfo().getFamily().equals(Family.SUCCESSFUL)); } catch (com.sun.jersey.api.client.ClientHandlerException ex) { // In the case of a java.net.ConnectException, return false res = false; } return res; }
@Override @SuppressFBWarnings("SLF4J_FORMAT_SHOULD_BE_CONST") public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { if (responseContext.getStatusInfo().getFamily().equals( Response.Status.Family.SUCCESSFUL) && !isContentTypeCompatible(responseContext.getMediaType())) { log.error(ERROR_MSG); String snippet = String.format( "Wrong content type received: [%s]", responseContext.getMediaType()); log.error(snippet); throw new IllegalStateException(snippet); } } }
private boolean isSuccessResponseCode(final String key) { final int responseCode; try { responseCode = Integer.parseInt(key); } catch (final NumberFormatException e) { // ok, not a response code at all return false; } final Status status = Status.fromStatusCode(responseCode); return status != null && Status.Family.SUCCESSFUL.equals(status.getFamily()); }
public GraknClientException(String s, StatusType statusInfo) { this(s, statusInfo.getFamily().equals(Family.SERVER_ERROR)); }
@Override public boolean isSuccess() { return Response.Status.Family.SUCCESSFUL.equals(delegate.getClientResponseStatus().getFamily()); }