diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/view/ClientEntityView.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/view/ClientEntityView.java new file mode 100644 index 000000000..dc544e3fd --- /dev/null +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/view/ClientEntityView.java @@ -0,0 +1,120 @@ +/** + * + */ +package org.mitre.openid.connect.view; + +import java.io.IOException; +import java.io.Writer; +import java.lang.reflect.Type; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.mitre.jose.JWEAlgorithmEntity; +import org.mitre.jose.JWEEncryptionMethodEntity; +import org.mitre.jose.JWSAlgorithmEntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; +import org.springframework.validation.BeanPropertyBindingResult; +import org.springframework.web.servlet.view.AbstractView; + +import com.google.gson.ExclusionStrategy; +import com.google.gson.FieldAttributes; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +/** + * @author jricher + * + */ +@Component("clientEntityView") +public class ClientEntityView extends AbstractView { + + private static Logger logger = LoggerFactory.getLogger(ClientEntityView.class); + + private Gson gson = new GsonBuilder() + .setExclusionStrategies(new ExclusionStrategy() { + + public boolean shouldSkipField(FieldAttributes f) { + + return false; + } + + public boolean shouldSkipClass(Class clazz) { + // skip the JPA binding wrapper + if (clazz.equals(BeanPropertyBindingResult.class)) { + return true; + } + return false; + } + + }) + .registerTypeAdapter(JWSAlgorithmEntity.class, new JsonSerializer() { + @Override + public JsonElement serialize(JWSAlgorithmEntity src, Type typeOfSrc, JsonSerializationContext context) { + if (src != null) { + return new JsonPrimitive(src.getAlgorithmName()); + } else { + return null; + } + } + }) + .registerTypeAdapter(JWEAlgorithmEntity.class, new JsonSerializer() { + @Override + public JsonElement serialize(JWEAlgorithmEntity src, Type typeOfSrc, JsonSerializationContext context) { + if (src != null) { + return new JsonPrimitive(src.getAlgorithmName()); + } else { + return null; + } + } + }) + .registerTypeAdapter(JWEEncryptionMethodEntity.class, new JsonSerializer() { + @Override + public JsonElement serialize(JWEEncryptionMethodEntity src, Type typeOfSrc, JsonSerializationContext context) { + if (src != null) { + return new JsonPrimitive(src.getAlgorithmName()); + } else { + return null; + } + } + }) + .serializeNulls() + .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") + .create(); + + + protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) { + + + response.setContentType("application/json"); + + + HttpStatus code = (HttpStatus) model.get("code"); + if (code == null) { + code = HttpStatus.OK; // default to 200 + } + + response.setStatus(code.value()); + + try { + + Writer out = response.getWriter(); + Object obj = model.get("entity"); + gson.toJson(obj, out); + + } catch (IOException e) { + + logger.error("IOException in JsonEntityView.java: ", e); + + } + } + +} diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/view/JsonEntityView.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/view/JsonEntityView.java index c2a695a5d..7dfa846d0 100644 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/view/JsonEntityView.java +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/view/JsonEntityView.java @@ -31,30 +31,30 @@ public class JsonEntityView extends AbstractView { private static Logger logger = LoggerFactory.getLogger(JsonEntityView.class); - protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) { + private Gson gson = new GsonBuilder() + .setExclusionStrategies(new ExclusionStrategy() { + + public boolean shouldSkipField(FieldAttributes f) { + + return false; + } + + public boolean shouldSkipClass(Class clazz) { + // skip the JPA binding wrapper + if (clazz.equals(BeanPropertyBindingResult.class)) { + return true; + } + return false; + } + + }) + .serializeNulls() + .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") + .create(); - Gson gson = new GsonBuilder() - .setExclusionStrategies(new ExclusionStrategy() { + protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) { - public boolean shouldSkipField(FieldAttributes f) { - - return false; - } - - public boolean shouldSkipClass(Class clazz) { - // skip the JPA binding wrapper - if (clazz.equals(BeanPropertyBindingResult.class)) { - return true; - } - return false; - } - - }) - .serializeNulls() - .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") - .create(); - - response.setContentType("application/json"); + response.setContentType("application/json"); HttpStatus code = (HttpStatus) model.get("code"); diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ClientAPI.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ClientAPI.java index da47bc4ca..0d8eb9cad 100644 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ClientAPI.java +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ClientAPI.java @@ -15,9 +15,13 @@ ******************************************************************************/ package org.mitre.openid.connect.web; +import java.lang.reflect.Type; import java.security.Principal; import java.util.Collection; +import org.mitre.jose.JWEAlgorithmEntity; +import org.mitre.jose.JWEEncryptionMethodEntity; +import org.mitre.jose.JWSAlgorithmEntity; import org.mitre.oauth2.exception.ClientNotFoundException; import org.mitre.oauth2.model.ClientDetailsEntity; import org.mitre.oauth2.service.ClientDetailsEntityService; @@ -35,8 +39,15 @@ import org.springframework.web.servlet.ModelAndView; import com.google.common.base.Strings; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import com.google.gson.JsonParser; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; /** * @author Michael Jett @@ -50,9 +61,40 @@ public class ClientAPI { @Autowired private ClientDetailsEntityService clientService; private JsonParser parser = new JsonParser(); - private Gson gson = new GsonBuilder().serializeNulls() - .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") - .create(); + private Gson gson = new GsonBuilder() + .serializeNulls() + .registerTypeAdapter(JWSAlgorithmEntity.class, new JsonDeserializer() { + @Override + public JWSAlgorithmEntity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + if (json.isJsonPrimitive()) { + return new JWSAlgorithmEntity(json.getAsString()); + } else { + return null; + } + } + }) + .registerTypeAdapter(JWEAlgorithmEntity.class, new JsonDeserializer() { + @Override + public JWEAlgorithmEntity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + if (json.isJsonPrimitive()) { + return new JWEAlgorithmEntity(json.getAsString()); + } else { + return null; + } + } + }) + .registerTypeAdapter(JWEEncryptionMethodEntity.class, new JsonDeserializer() { + @Override + public JWEEncryptionMethodEntity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + if (json.isJsonPrimitive()) { + return new JWEEncryptionMethodEntity(json.getAsString()); + } else { + return null; + } + } + }) + .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") + .create(); /** * Get a list of all clients @@ -64,7 +106,7 @@ public class ClientAPI { Collection clients = clientService.getAllClients(); modelAndView.addObject("entity", clients); - modelAndView.setViewName("jsonEntityView"); + modelAndView.setViewName("clientEntityView"); return modelAndView; } @@ -101,7 +143,7 @@ public class ClientAPI { ClientDetailsEntity newClient = clientService.saveNewClient(client); m.addAttribute("entity", newClient); - return "jsonEntityView"; + return "clientEntityView"; } /** @@ -143,7 +185,7 @@ public class ClientAPI { ClientDetailsEntity newClient = clientService.updateClient(oldClient, client); m.addAttribute("entity", newClient); - return "jsonEntityView"; + return "clientEntityView"; } /** @@ -182,7 +224,7 @@ public class ClientAPI { } modelAndView.addObject("entity", client); - modelAndView.setViewName("jsonEntityView"); + modelAndView.setViewName("clientEntityView"); return modelAndView; }