added custom client view for API

pull/306/merge
Justin Richer 2013-03-04 17:33:18 -05:00
parent a0c2e94922
commit 4095f2179c
3 changed files with 191 additions and 29 deletions

View File

@ -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<JWSAlgorithmEntity>() {
@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<JWEAlgorithmEntity>() {
@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<JWEEncryptionMethodEntity>() {
@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<String, Object> 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);
}
}
}

View File

@ -31,30 +31,30 @@ public class JsonEntityView extends AbstractView {
private static Logger logger = LoggerFactory.getLogger(JsonEntityView.class);
protected void renderMergedOutputModel(Map<String, Object> 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<String, Object> 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");

View File

@ -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 <mjett@mitre.org>
@ -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<JWSAlgorithmEntity>() {
@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<JWEAlgorithmEntity>() {
@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<JWEEncryptionMethodEntity>() {
@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<ClientDetailsEntity> 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;
}