-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ContextTypeHelper: do account for the "Accept" header (per CAS specs)
- Loading branch information
Showing
3 changed files
with
18 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 10 additions & 16 deletions
26
src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,27 @@ | ||
package org.keycloak.protocol.cas.utils; | ||
|
||
import jakarta.ws.rs.BadRequestException; | ||
import jakarta.ws.rs.core.*; | ||
import org.jboss.resteasy.spi.HttpRequest; | ||
import org.keycloak.protocol.cas.CASLoginProtocol; | ||
import org.keycloak.protocol.cas.representations.CASErrorCode; | ||
|
||
public class ContentTypeHelper { | ||
private final HttpRequest request; | ||
private final Request restRequest; | ||
private final UriInfo uriInfo; | ||
|
||
public ContentTypeHelper(HttpRequest request, Request restRequest, UriInfo uriInfo) { | ||
this.request = request; | ||
this.restRequest = restRequest; | ||
public ContentTypeHelper(UriInfo uriInfo) { | ||
this.uriInfo = uriInfo; | ||
} | ||
|
||
public MediaType selectResponseType() { | ||
String format = uriInfo.getQueryParameters().getFirst(CASLoginProtocol.FORMAT_PARAM); | ||
if (format != null && !format.isEmpty()) { | ||
//if parameter is set, it overrides all header values (see spec section 2.5.1) | ||
request.getMutableHeaders().putSingle(HttpHeaders.ACCEPT, "application/" + format.toLowerCase()); | ||
} | ||
try { | ||
Variant variant = restRequest.selectVariant(Variant.mediaTypes(MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).build()); | ||
return variant == null ? MediaType.APPLICATION_XML_TYPE : variant.getMediaType(); | ||
} catch (BadRequestException e) { | ||
//the default Accept header set by java.net.HttpURLConnection is invalid (cf. RESTEASY-960) | ||
return MediaType.APPLICATION_XML_TYPE; | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters