From de7d254562ee04401894b82e3ee18f46d79ea357 Mon Sep 17 00:00:00 2001
From: Matthias Piepkorn <mpiepk@gmail.com>
Date: Tue, 14 Feb 2017 10:32:07 +0000
Subject: [PATCH] Do not fail on invalid Accept header

---
 src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java |   10 ++++++++--
 src/test/java/org/keycloak/protocol/cas/ContentTypeHelperTest.java   |    1 +
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java b/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
index 0e13ca6..5096474 100644
--- a/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
+++ b/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
@@ -1,5 +1,6 @@
 package org.keycloak.protocol.cas.utils;
 
+import org.jboss.resteasy.spi.BadRequestException;
 import org.jboss.resteasy.spi.HttpRequest;
 import org.keycloak.protocol.cas.CASLoginProtocol;
 
@@ -22,7 +23,12 @@
             //if parameter is set, it overrides all header values (see spec section 2.5.1)
             request.getMutableHeaders().putSingle(HttpHeaders.ACCEPT, "application/" + format.toLowerCase());
         }
-        Variant variant = restRequest.selectVariant(Variant.mediaTypes(MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).build());
-        return variant == null ? MediaType.APPLICATION_XML_TYPE : variant.getMediaType();
+        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;
+        }
     }
 }
diff --git a/src/test/java/org/keycloak/protocol/cas/ContentTypeHelperTest.java b/src/test/java/org/keycloak/protocol/cas/ContentTypeHelperTest.java
index c08ab47..11b7b4f 100644
--- a/src/test/java/org/keycloak/protocol/cas/ContentTypeHelperTest.java
+++ b/src/test/java/org/keycloak/protocol/cas/ContentTypeHelperTest.java
@@ -29,6 +29,7 @@
         assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/?format=xml", MediaType.APPLICATION_JSON).selectResponseType());
 
         assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/", MediaType.TEXT_PLAIN).selectResponseType());
+        assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2").selectResponseType());
     }
 
     private ContentTypeHelper get(String uri, String acceptHeader) throws Exception {

--
Gitblit v1.9.1