mirror of https://github.com/jacekkow/keycloak-protocol-cas

Doccrazy
2017-01-27 1482f2bf59b0e26b74f1e34631d5366e1abaff96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package org.keycloak.protocol.cas.utils;
 
import org.keycloak.protocol.cas.representations.CasServiceResponse;
import org.keycloak.protocol.cas.representations.CasServiceResponseAuthenticationFailure;
import org.keycloak.protocol.cas.representations.CasServiceResponseAuthenticationSuccess;
 
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;
 
public final class ServiceResponseHelper {
    private ServiceResponseHelper() {
    }
 
    public static CasServiceResponse createSuccess(String username, Map<String, Object> attributes) {
        return createSuccess(username, attributes, null, null);
    }
 
    public static CasServiceResponse createSuccess(String username, Map<String, Object> attributes,
                                                   String proxyGrantingTicket, List<String> proxies) {
        CasServiceResponse response = new CasServiceResponse();
        CasServiceResponseAuthenticationSuccess success = new CasServiceResponseAuthenticationSuccess();
        success.setUser(username);
        success.setProxies(proxies);
        success.setProxyGrantingTicket(proxyGrantingTicket);
        success.setAttributes(attributes);
 
        response.setAuthenticationSuccess(success);
 
        return response;
    }
 
    public static CasServiceResponse createFailure(String errorCode, String errorDescription) {
        CasServiceResponse response = new CasServiceResponse();
        CasServiceResponseAuthenticationFailure failure = new CasServiceResponseAuthenticationFailure();
        failure.setCode(errorCode);
        failure.setDescription(errorDescription);
        response.setAuthenticationFailure(failure);
 
        return response;
    }
 
    public static Response createResponse(Response.Status status, MediaType mediaType, CasServiceResponse serviceResponse) {
        Response.ResponseBuilder builder = Response.status(status)
                .header(HttpHeaders.CONTENT_TYPE, mediaType.withCharset("utf-8"));
        if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
            return builder.entity(ServiceResponseMarshaller.marshalJson(serviceResponse)).build();
        } else {
            return builder.entity(ServiceResponseMarshaller.marshalXml(serviceResponse)).build();
        }
    }
}