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

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