Java Code Examples for javax.ws.rs.core.Response#StatusType

The following examples show how to use javax.ws.rs.core.Response#StatusType . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MSF4JResponse.java    From msf4j with Apache License 2.0 6 votes vote down vote up
@Override
public StatusType getStatusInfo() {
    return new Response.StatusType() {

        public Status.Family getFamily() {
            return Response.Status.Family.familyOf(MSF4JResponse.this.status);
        }

        public String getReasonPhrase() {
            Response.Status statusEnum = Response.Status.fromStatusCode(MSF4JResponse.this.status);
            return statusEnum != null ? statusEnum.getReasonPhrase() : "";
        }

        public int getStatusCode() {
            return MSF4JResponse.this.status;
        }

    };
}
 
Example 2
Source File: JSONResponse.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * create JSON Response
 */

@Override
public Response toResponse(Exception e) {
    logger.debug("exception during REST Handling", e);

    Response.StatusType status = Response.Status.INTERNAL_SERVER_ERROR;

    // in case the Exception is a WebApplicationException, it already carries a Status
    if (e instanceof WebApplicationException) {
        status = ((WebApplicationException) e).getResponse().getStatusInfo();
    }

    JsonElement ret = INSTANCE.createErrorJson(e.getMessage(), status, null, e);
    return INSTANCE.responseBuilder(status).entity(INSTANCE.gson.toJson(ret)).build();
}
 
Example 3
Source File: Exec.java    From raml-java-client-generator with Apache License 2.0 6 votes vote down vote up
public void post(ExecPOSTBody body) {
    WebTarget target = this._client.target(getBaseUri());
    final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
    FormDataMultiPart multiPart = new FormDataMultiPart();
    if (body.getFile()!= null) {
        multiPart.bodyPart(new FileDataBodyPart("file", body.getFile()));
    }
    if (body.getFrom()!= null) {
        multiPart.field("From", body.getFrom().toString());
    }
    Response response = invocationBuilder.post(Entity.entity(multiPart, multiPart.getMediaType()));
    if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
        Response.StatusType statusInfo = response.getStatusInfo();
        throw new DataWeaveAPIException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
    }
}
 
Example 4
Source File: CustomStatus.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
private static Response.StatusType buildStatusType(final int code, final String reasonPhrase, final Status.Family family) {
    return new Response.StatusType() {
        
        @Override
        public int getStatusCode() {
            return code;
        }
        
        @Override
        public String getReasonPhrase() {
            return reasonPhrase;
        }
        
        @Override
        public Family getFamily() {
            return family;
        }
    };
}
 
Example 5
Source File: FnRequestHandler.java    From jrestless with Apache License 2.0 6 votes vote down vote up
@Override
public void writeResponse(Response.StatusType statusType,
						Map<String, List<String>> headers,
						OutputStream outputStream)
	throws IOException {
	// NOTE: This is a safe cast as it is set to a ByteArrayOutputStream by getEntityOutputStream
	// See JRestlessHandlerContainer class for more details
	String responseBody = ((ByteArrayOutputStream) outputStream).toString(StandardCharsets.UTF_8.name());
	String contentType = headers
		.getOrDefault(HttpHeaders.CONTENT_TYPE, Collections.singletonList(DEFAULT_CONTENT_TYPE))
		.get(0);
	Map<String, String> outHeaders = new HashMap<>();
	headers.forEach((k, v) -> outHeaders.put(k, v.stream().collect(Collectors.joining(","))));

	OutputEvent outputEvent = OutputEvent.fromBytes(
		responseBody.getBytes(StandardCharsets.UTF_8),
		statusType.getStatusCode(),
		contentType,
		Headers.fromMap(outHeaders));
	response = new WrappedOutput(outputEvent, responseBody, statusType);
}
 
Example 6
Source File: Api.java    From raml-java-client-generator with Apache License 2.0 5 votes vote down vote up
/**
     * Returns the list of all users
     * 
     */
    public List<ApiGETResponse> get(String authorizationToken) {
        WebTarget target = this._client.target(getBaseUri());
        final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
        invocationBuilder.header("Authorization", ("Bearer "+ authorizationToken));
        Response response = invocationBuilder.get();
        if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
            Response.StatusType statusInfo = response.getStatusInfo();
            throw new CoreServicesAPIReferenceException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
        }
        return response.readEntity((
new javax.ws.rs.core.GenericType<java.util.List<oauth20.resource.api.model.ApiGETResponse>>() {}));
    }
 
Example 7
Source File: Foo.java    From raml-java-client-generator with Apache License 2.0 5 votes vote down vote up
public same_path_multiple_times.resource.foo.model.FooGETResponse get() {
    WebTarget target = this._client.target(getBaseUri());
    final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
    Response response = invocationBuilder.get();
    if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
        Response.StatusType statusInfo = response.getStatusInfo();
        throw new FooException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
    }
    return response.readEntity(same_path_multiple_times.resource.foo.model.FooGETResponse.class);
}
 
Example 8
Source File: Login.java    From raml-java-client-generator with Apache License 2.0 5 votes vote down vote up
public CoreServicesAPIReferenceResponse<Void> get() {
    WebTarget target = this._client.target(getBaseUri());
    final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
    Response response = invocationBuilder.get();
    if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
        Response.StatusType statusInfo = response.getStatusInfo();
        throw new CoreServicesAPIReferenceException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
    }
    CoreServicesAPIReferenceResponse<Void> apiResponse = new CoreServicesAPIReferenceResponse<Void>(null, response.getStringHeaders(), response);
    return apiResponse;
}
 
Example 9
Source File: Api.java    From raml-java-client-generator with Apache License 2.0 5 votes vote down vote up
/**
     * Returns the list of all users
     * 
     */
    public CoreServicesAPIReferenceResponse<List<ApiGETResponseBody>> get(String authorizationToken) {
        WebTarget target = this._client.target(getBaseUri());
        final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
        invocationBuilder.header("Authorization", ("Bearer "+ authorizationToken));
        Response response = invocationBuilder.get();
        if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
            Response.StatusType statusInfo = response.getStatusInfo();
            throw new CoreServicesAPIReferenceException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
        }
        CoreServicesAPIReferenceResponse<List<ApiGETResponseBody>> apiResponse = new CoreServicesAPIReferenceResponse<List<ApiGETResponseBody>>(response.readEntity((
new javax.ws.rs.core.GenericType<java.util.List<oauth20.resource.api.model.ApiGETResponseBody>>() {})), response.getStringHeaders(), response);
        return apiResponse;
    }
 
Example 10
Source File: A.java    From raml-java-client-generator with Apache License 2.0 5 votes vote down vote up
public same_path_multiple_times.resource.foo.a.model.AGETResponse get() {
    WebTarget target = this._client.target(getBaseUri());
    final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
    Response response = invocationBuilder.get();
    if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
        Response.StatusType statusInfo = response.getStatusInfo();
        throw new FooException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
    }
    return response.readEntity(same_path_multiple_times.resource.foo.a.model.AGETResponse.class);
}
 
Example 11
Source File: Login.java    From raml-java-client-generator with Apache License 2.0 5 votes vote down vote up
public FooResponse<String> post() {
    WebTarget target = this._client.target(getBaseUri());
    final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
    Response response = invocationBuilder.post(null);
    if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
        Response.StatusType statusInfo = response.getStatusInfo();
        throw new FooException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
    }
    FooResponse<String> apiResponse = new FooResponse<String>(((String) response.readEntity(Object.class)), response.getStringHeaders(), response);
    return apiResponse;
}
 
Example 12
Source File: Login.java    From raml-java-client-generator with Apache License 2.0 5 votes vote down vote up
public MultiBodyResponse<multi_body.resource.cs.login.model.LoginGETResponseBody> get() {
    WebTarget target = this._client.target(getBaseUri());
    final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE);
    Response response = invocationBuilder.get();
    if (response.getStatusInfo().getFamily()!= javax.ws.rs.core.Response.Status.Family.SUCCESSFUL) {
        Response.StatusType statusInfo = response.getStatusInfo();
        throw new MultiBodyException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
    }
    MultiBodyResponse<multi_body.resource.cs.login.model.LoginGETResponseBody> apiResponse = new MultiBodyResponse<multi_body.resource.cs.login.model.LoginGETResponseBody>(response.readEntity(multi_body.resource.cs.login.model.LoginGETResponseBody.class), response.getStringHeaders(), response);
    return apiResponse;
}
 
Example 13
Source File: Users.java    From raml-java-client-generator with Apache License 2.0 5 votes vote down vote up
/**
     * Returns the list of all users
     * 
     */
    public List<UsersGETResponse> get() {
        WebTarget target = this._client.target(getBaseUri());
        final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
        Response response = invocationBuilder.get();
        if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
            Response.StatusType statusInfo = response.getStatusInfo();
            throw new FooException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
        }
        return response.readEntity((
new javax.ws.rs.core.GenericType<java.util.List<list.resource.users.model.UsersGETResponse>>() {}));
    }
 
Example 14
Source File: Rename.java    From raml-java-client-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Rename a project
 * 
 */
public DesignCenterProjectsServicewithsubresourceonsamelineResponse<Void> put(Object body) {
    WebTarget target = this._client.target(getBaseUri());
    final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
    Response response = invocationBuilder.put(Entity.json(body));
    if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
        Response.StatusType statusInfo = response.getStatusInfo();
        throw new DesignCenterProjectsServicewithsubresourceonsamelineException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
    }
    DesignCenterProjectsServicewithsubresourceonsamelineResponse<Void> apiResponse = new DesignCenterProjectsServicewithsubresourceonsamelineResponse<Void>(null, response.getStringHeaders(), response);
    return apiResponse;
}
 
Example 15
Source File: BaseService.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
protected static void checkImageTargetingResponse(final String id, final Response.StatusType statusInfo) {
    if (statusInfo.getFamily() == Family.SUCCESSFUL) {
        // no error
        return;
    }

    switch (statusInfo.getStatusCode()) {
        case 404:
            throw new ImageNotFoundException(id);
        default:
            throw new DockerException(statusInfo.getReasonPhrase());
    }
}
 
Example 16
Source File: HttpMatchersTest.java    From rulz with Apache License 2.0 5 votes vote down vote up
@Test
public void redirectionErrorMatcher() {
    Matcher<Response> redirectionMatcher = HttpMatchers.redirection();
    Response response = mock(Response.class);
    Response.StatusType statusType = mock(Response.StatusType.class);
    when(response.getStatusInfo()).thenReturn(statusType);
    when(statusType.getFamily()).thenReturn(Response.Status.Family.REDIRECTION);
    assertThat(response, is(redirectionMatcher));
}
 
Example 17
Source File: Login.java    From raml-java-client-generator with Apache License 2.0 5 votes vote down vote up
public MultiBodyResponse<multi_body.resource.cs.login.model.LoginPOSTResponseBody> post(InputStream body) {
    WebTarget target = this._client.target(getBaseUri());
    final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE);
    Response response = invocationBuilder.post(javax.ws.rs.client.Entity.entity(body, javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE));
    if (response.getStatusInfo().getFamily()!= javax.ws.rs.core.Response.Status.Family.SUCCESSFUL) {
        Response.StatusType statusInfo = response.getStatusInfo();
        throw new MultiBodyException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase(), response.getStringHeaders(), response);
    }
    MultiBodyResponse<multi_body.resource.cs.login.model.LoginPOSTResponseBody> apiResponse = new MultiBodyResponse<multi_body.resource.cs.login.model.LoginPOSTResponseBody>(response.readEntity(multi_body.resource.cs.login.model.LoginPOSTResponseBody.class), response.getStringHeaders(), response);
    return apiResponse;
}
 
Example 18
Source File: JSONResponse.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
private Response createErrorResponse(Response.StatusType status, Object entity, String errormessage) {
    ResponseBuilder rp = responseBuilder(status);
    JsonElement errorJson = createErrorJson(errormessage, status, entity, null);
    rp.entity(errorJson);
    return rp.build();
}
 
Example 19
Source File: StatusExpectation.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
StatusExpectation(Response.StatusType status) {
  this.status = status;
}
 
Example 20
Source File: HttpComponentsConnector.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException {
    final HttpUriRequest request = this.toUriHttpRequest(clientRequest);
    final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request);

    try {
        final CloseableHttpResponse response;
        response = client.execute(new HttpHost(request.getURI().getHost(), request.getURI().getPort(), request.getURI().getScheme()), request, new BasicHttpContext(context));
        HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName());

        final Response.StatusType status = response.getStatusLine().getReasonPhrase() == null
            ? Statuses.from(response.getStatusLine().getStatusCode())
            : Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());

        final ClientResponse responseContext = new ClientResponse(status, clientRequest);
        final List<URI> redirectLocations = context.getRedirectLocations();
        if(redirectLocations != null && !redirectLocations.isEmpty()) {
            responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1));
        }

        final Header[] respHeaders = response.getAllHeaders();
        final MultivaluedMap<String, String> headers = responseContext.getHeaders();
        for(final Header header : respHeaders) {
            final String headerName = header.getName();
            List<String> list = headers.get(headerName);
            if(list == null) {
                list = new ArrayList<>();
            }
            list.add(header.getValue());
            headers.put(headerName, list);
        }

        final HttpEntity entity = response.getEntity();

        if(entity != null) {
            if(headers.get(HttpHeaders.CONTENT_LENGTH) == null) {
                headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength()));
            }

            final Header contentEncoding = entity.getContentEncoding();
            if(headers.get(HttpHeaders.CONTENT_ENCODING) == null && contentEncoding != null) {
                headers.add(HttpHeaders.CONTENT_ENCODING, contentEncoding.getValue());
            }
        }
        responseContext.setEntityStream(this.toInputStream(response));
        return responseContext;
    }
    catch(final Exception e) {
        throw new ProcessingException(e);
    }
}