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

Jacek Kowalski
2023-11-24 6fff26d1517dbcec6dfb3f181bda418577ea3b17
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
package org.keycloak.protocol.cas.utils;
 
import jakarta.ws.rs.core.*;
import org.keycloak.protocol.cas.CASLoginProtocol;
import org.keycloak.protocol.cas.representations.CASErrorCode;
 
public class ContentTypeHelper {
    private final UriInfo uriInfo;
 
    public ContentTypeHelper(UriInfo uriInfo) {
        this.uriInfo = uriInfo;
    }
 
    public MediaType selectResponseType() {
        String format = uriInfo.getQueryParameters().getFirst(CASLoginProtocol.FORMAT_PARAM);
        if (format != null && !format.isEmpty()) {
            if (format.equalsIgnoreCase("json")) {
                return MediaType.APPLICATION_JSON_TYPE;
            } else if (format.equalsIgnoreCase("xml")) {
                return MediaType.APPLICATION_XML_TYPE;
            } else {
                throw new CASValidationException(CASErrorCode.INVALID_REQUEST, "Unsupported value of parameter " + CASLoginProtocol.FORMAT_PARAM, Response.Status.BAD_REQUEST);
            }
        }
        return MediaType.APPLICATION_XML_TYPE;
    }
}