Java Code Examples for org.apache.cxf.jaxrs.utils.JAXRSUtils#toResponseBuilder()

The following examples show how to use org.apache.cxf.jaxrs.utils.JAXRSUtils#toResponseBuilder() . 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: AuthorizationUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static void throwAuthorizationFailure(Set<String> challenges, String realm, Throwable cause) {
    ResponseBuilder rb = JAXRSUtils.toResponseBuilder(401);

    StringBuilder sb = new StringBuilder();
    for (String challenge : challenges) {
        if ("*".equals(challenge)) {
            continue;
        }
        if (sb.length() > 0) {
            sb.append(',');
        }
        sb.append(challenge);
    }
    if (sb.length() > 0) {
        if (realm != null) {
            sb.append(" realm=\"").append(realm).append('"');
        }
        rb.header(HttpHeaders.WWW_AUTHENTICATE, sb.toString());
    }
    if (cause != null) {
        rb.entity(cause.getMessage());
    }
    throw ExceptionUtils.toNotAuthorizedException(cause, rb.build());
}
 
Example 2
Source File: ClientResponseFilterInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected Response getResponse(Message inMessage) {
    Response resp = inMessage.getExchange().get(Response.class);
    if (resp != null) {
        return JAXRSUtils.copyResponseIfNeeded(resp);
    }
    ResponseBuilder rb = JAXRSUtils.toResponseBuilder((Integer)inMessage.get(Message.RESPONSE_CODE));
    rb.entity(inMessage.get(InputStream.class));

    @SuppressWarnings("unchecked")
    Map<String, List<String>> protocolHeaders =
        (Map<String, List<String>>)inMessage.get(Message.PROTOCOL_HEADERS);
    for (Map.Entry<String, List<String>> entry : protocolHeaders.entrySet()) {
        if (null == entry.getKey()) {
            continue;
        }
        if (entry.getValue().size() > 0) {
            for (String val : entry.getValue()) {
                rb.header(entry.getKey(), val);
            }
        }
    }


    return rb.build();
}
 
Example 3
Source File: ValidationExceptionMapper.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Response buildResponse(Response.Status errorStatus, String responseText) {
    ResponseBuilder rb = JAXRSUtils.toResponseBuilder(errorStatus);
    if (responseText != null) {
        rb.type(MediaType.TEXT_PLAIN).entity(responseText);
    }
    return rb.build();
}
 
Example 4
Source File: DynamicRegistrationService.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void reportInvalidRequestError(OAuthError entity, MediaType mt) {
    ResponseBuilder rb = JAXRSUtils.toResponseBuilder(400);
    if (mt != null) {
        rb.type(mt);
    }
    throw ExceptionUtils.toBadRequestException(null, rb.entity(entity).build());
}
 
Example 5
Source File: AbstractOAuthService.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void reportInvalidRequestError(OAuthError entity, MediaType mt) {
    ResponseBuilder rb = JAXRSUtils.toResponseBuilder(400);
    if (mt != null) {
        rb.type(mt);
    }
    throw ExceptionUtils.toBadRequestException(null, rb.entity(entity).build());
}
 
Example 6
Source File: AbstractTokenService.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected void reportInvalidClient(OAuthError error) {
    ResponseBuilder rb = JAXRSUtils.toResponseBuilder(401);
    throw ExceptionUtils.toNotAuthorizedException(null,
        rb.type(MediaType.APPLICATION_JSON_TYPE).entity(error).build());
}
 
Example 7
Source File: AbstractClient.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected ResponseBuilder setResponseBuilder(Message outMessage, Exchange exchange) throws Exception {
    Response response = exchange.get(Response.class);
    if (response != null) {
        outMessage.getExchange().getInMessage().put(Message.PROTOCOL_HEADERS, response.getStringHeaders());
        return JAXRSUtils.fromResponse(JAXRSUtils.copyResponseIfNeeded(response));
    }

    Integer status = getResponseCode(exchange);
    ResponseBuilder currentResponseBuilder = JAXRSUtils.toResponseBuilder(status);

    Message responseMessage = exchange.getInMessage() != null
        ? exchange.getInMessage() : exchange.getInFaultMessage();
    // if there is no response message, we just send the response back directly
    if (responseMessage == null) {
        return currentResponseBuilder;
    }

    Map<String, List<Object>> protocolHeaders =
        CastUtils.cast((Map<?, ?>)responseMessage.get(Message.PROTOCOL_HEADERS));

    boolean splitHeaders = MessageUtils.getContextualBoolean(outMessage, HEADER_SPLIT_PROPERTY);
    for (Map.Entry<String, List<Object>> entry : protocolHeaders.entrySet()) {
        if (null == entry.getKey()) {
            continue;
        }
        if (entry.getValue().size() > 0) {
            if (HttpUtils.isDateRelatedHeader(entry.getKey())) {
                currentResponseBuilder.header(entry.getKey(), entry.getValue().get(0));
                continue;
            }
            entry.getValue().forEach(valObject -> {
                if (splitHeaders && valObject instanceof String) {
                    String val = (String) valObject;
                    final String[] values;
                    if (val.isEmpty()) {
                        values = new String[]{""};
                    } else if (val.charAt(0) == '"' && val.charAt(val.length() - 1) == '"') {
                        // if the value starts with a quote and ends with a quote, we do a best
                        // effort attempt to determine what the individual values are.
                        values = parseQuotedHeaderValue(val);
                    } else {
                        boolean splitPossible = !(HttpHeaders.SET_COOKIE.equalsIgnoreCase(entry.getKey())
                                && val.toUpperCase().contains(HttpHeaders.EXPIRES.toUpperCase()));
                        values = splitPossible ? val.split(",") : new String[]{val};
                    }
                    for (String s : values) {
                        String theValue = s.trim();
                        if (!theValue.isEmpty()) {
                            currentResponseBuilder.header(entry.getKey(), theValue);
                        }
                    }
                } else {
                    currentResponseBuilder.header(entry.getKey(), valObject);
                }
            });
        }
    }
    String ct = (String)responseMessage.get(Message.CONTENT_TYPE);
    if (ct != null) {
        currentResponseBuilder.type(ct);
    }
    InputStream mStream = responseMessage.getContent(InputStream.class);
    currentResponseBuilder.entity(mStream);

    return currentResponseBuilder;
}