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

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