moved and consolidated json utilities
parent
19879c20c2
commit
7a1480bb07
|
@ -19,12 +19,12 @@
|
|||
*/
|
||||
package org.mitre.openid.connect.client.service.impl;
|
||||
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsBoolean;
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsEncryptionMethodList;
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsJweAlgorithmList;
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsJwsAlgorithmList;
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsString;
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsStringList;
|
||||
import static org.mitre.util.JsonUtils.getAsBoolean;
|
||||
import static org.mitre.util.JsonUtils.getAsEncryptionMethodList;
|
||||
import static org.mitre.util.JsonUtils.getAsJweAlgorithmList;
|
||||
import static org.mitre.util.JsonUtils.getAsJwsAlgorithmList;
|
||||
import static org.mitre.util.JsonUtils.getAsString;
|
||||
import static org.mitre.util.JsonUtils.getAsStringList;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
|
|
@ -20,13 +20,6 @@
|
|||
package org.mitre.openid.connect;
|
||||
|
||||
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsArray;
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsDate;
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsJweAlgorithm;
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsJweEncryptionMethod;
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsJwsAlgorithm;
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsString;
|
||||
import static org.mitre.discovery.util.JsonUtils.getAsStringSet;
|
||||
import static org.mitre.oauth2.model.RegisteredClientFields.APPLICATION_TYPE;
|
||||
import static org.mitre.oauth2.model.RegisteredClientFields.CLIENT_ID;
|
||||
import static org.mitre.oauth2.model.RegisteredClientFields.CLIENT_ID_ISSUED_AT;
|
||||
|
@ -63,6 +56,13 @@ import static org.mitre.oauth2.model.RegisteredClientFields.TOS_URI;
|
|||
import static org.mitre.oauth2.model.RegisteredClientFields.USERINFO_ENCRYPTED_RESPONSE_ALG;
|
||||
import static org.mitre.oauth2.model.RegisteredClientFields.USERINFO_ENCRYPTED_RESPONSE_ENC;
|
||||
import static org.mitre.oauth2.model.RegisteredClientFields.USERINFO_SIGNED_RESPONSE_ALG;
|
||||
import static org.mitre.util.JsonUtils.getAsArray;
|
||||
import static org.mitre.util.JsonUtils.getAsDate;
|
||||
import static org.mitre.util.JsonUtils.getAsJweAlgorithm;
|
||||
import static org.mitre.util.JsonUtils.getAsJweEncryptionMethod;
|
||||
import static org.mitre.util.JsonUtils.getAsJwsAlgorithm;
|
||||
import static org.mitre.util.JsonUtils.getAsString;
|
||||
import static org.mitre.util.JsonUtils.getAsStringSet;
|
||||
|
||||
import org.mitre.oauth2.model.ClientDetailsEntity;
|
||||
import org.mitre.oauth2.model.ClientDetailsEntity.AppType;
|
||||
|
|
|
@ -17,20 +17,35 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.mitre.discovery.util;
|
||||
package org.mitre.util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import com.nimbusds.jose.EncryptionMethod;
|
||||
import com.nimbusds.jose.JWEAlgorithm;
|
||||
import com.nimbusds.jose.JWSAlgorithm;
|
||||
|
@ -41,8 +56,11 @@ import com.nimbusds.jose.JWSAlgorithm;
|
|||
* @author jricher
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings(value = {"rawtypes", "unchecked"})
|
||||
public class JsonUtils {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(JsonUtils.class);
|
||||
|
||||
private static Gson gson = new Gson();
|
||||
|
||||
/**
|
||||
|
@ -216,4 +234,101 @@ public class JsonUtils {
|
|||
}
|
||||
}
|
||||
|
||||
public static <T> T base64UrlDecodeObject(String encoded, Class<T> type) {
|
||||
if (encoded == null) {
|
||||
return null;
|
||||
} else {
|
||||
T deserialized = null;
|
||||
try {
|
||||
byte[] decoded = BaseEncoding.base64Url().decode(encoded);
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
|
||||
ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
deserialized = type.cast(ois.readObject());
|
||||
ois.close();
|
||||
bais.close();
|
||||
} catch (Exception ex) {
|
||||
logger.error("Unable to decode object", ex);
|
||||
}
|
||||
return deserialized;
|
||||
}
|
||||
}
|
||||
|
||||
public static String base64UrlEncodeObject(Serializable obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
} else {
|
||||
String encoded = null;
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(obj);
|
||||
encoded = BaseEncoding.base64Url().encode(baos.toByteArray());
|
||||
oos.close();
|
||||
baos.close();
|
||||
} catch (IOException ex) {
|
||||
logger.error("Unable to encode object", ex);
|
||||
}
|
||||
return encoded;
|
||||
}
|
||||
}
|
||||
|
||||
public static Map readMap(JsonReader reader) throws IOException {
|
||||
Map map = new HashMap<String, Object>();
|
||||
reader.beginObject();
|
||||
while(reader.hasNext()) {
|
||||
String name = reader.nextName();
|
||||
Object value = null;
|
||||
switch(reader.peek()) {
|
||||
case STRING:
|
||||
value = reader.nextString();
|
||||
break;
|
||||
case BOOLEAN:
|
||||
value = reader.nextBoolean();
|
||||
break;
|
||||
case NUMBER:
|
||||
value = reader.nextLong();
|
||||
break;
|
||||
}
|
||||
map.put(name, value);
|
||||
}
|
||||
reader.endObject();
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Set readSet(JsonReader reader) throws IOException {
|
||||
Set arraySet = null;
|
||||
reader.beginArray();
|
||||
switch (reader.peek()) {
|
||||
case STRING:
|
||||
arraySet = new HashSet<String>();
|
||||
while (reader.hasNext()) {
|
||||
arraySet.add(reader.nextString());
|
||||
}
|
||||
break;
|
||||
case NUMBER:
|
||||
arraySet = new HashSet<Long>();
|
||||
while (reader.hasNext()) {
|
||||
arraySet.add(reader.nextLong());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
arraySet = new HashSet();
|
||||
break;
|
||||
}
|
||||
reader.endArray();
|
||||
return arraySet;
|
||||
}
|
||||
|
||||
public static void writeNullSafeArray(JsonWriter writer, Set<String> items) throws IOException {
|
||||
if (items != null) {
|
||||
writer.beginArray();
|
||||
for (String s : items) {
|
||||
writer.value(s);
|
||||
}
|
||||
writer.endArray();
|
||||
} else {
|
||||
writer.nullValue();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,142 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright 2015 The MITRE Corporation
|
||||
* and the MIT Kerberos and Internet Trust Consortium
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*******************************************************************************/
|
||||
package org.mitre.openid.connect.service.impl;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mitre.openid.connect.service.MITREidDataService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author arielak
|
||||
*/
|
||||
@SuppressWarnings(value = {"rawtypes", "unchecked"})
|
||||
public abstract class AbstractMITREidDataService implements MITREidDataService {
|
||||
private static Logger logger = LoggerFactory.getLogger(AbstractMITREidDataService.class);
|
||||
|
||||
protected static <T> T base64UrlDecodeObject(String encoded, Class<T> type) {
|
||||
if (encoded == null) {
|
||||
return null;
|
||||
} else {
|
||||
T deserialized = null;
|
||||
try {
|
||||
byte[] decoded = BaseEncoding.base64Url().decode(encoded);
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
|
||||
ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
deserialized = type.cast(ois.readObject());
|
||||
ois.close();
|
||||
bais.close();
|
||||
} catch (Exception ex) {
|
||||
logger.error("Unable to decode object", ex);
|
||||
}
|
||||
return deserialized;
|
||||
}
|
||||
}
|
||||
|
||||
protected static String base64UrlEncodeObject(Serializable obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
} else {
|
||||
String encoded = null;
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(obj);
|
||||
encoded = BaseEncoding.base64Url().encode(baos.toByteArray());
|
||||
oos.close();
|
||||
baos.close();
|
||||
} catch (IOException ex) {
|
||||
logger.error("Unable to encode object", ex);
|
||||
}
|
||||
return encoded;
|
||||
}
|
||||
}
|
||||
protected static Set readSet(JsonReader reader) throws IOException {
|
||||
Set arraySet = null;
|
||||
reader.beginArray();
|
||||
switch (reader.peek()) {
|
||||
case STRING:
|
||||
arraySet = new HashSet<String>();
|
||||
while (reader.hasNext()) {
|
||||
arraySet.add(reader.nextString());
|
||||
}
|
||||
break;
|
||||
case NUMBER:
|
||||
arraySet = new HashSet<Long>();
|
||||
while (reader.hasNext()) {
|
||||
arraySet.add(reader.nextLong());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
arraySet = new HashSet();
|
||||
break;
|
||||
}
|
||||
reader.endArray();
|
||||
return arraySet;
|
||||
}
|
||||
|
||||
protected static Map readMap(JsonReader reader) throws IOException {
|
||||
Map map = new HashMap<String, Object>();
|
||||
reader.beginObject();
|
||||
while(reader.hasNext()) {
|
||||
String name = reader.nextName();
|
||||
Object value = null;
|
||||
switch(reader.peek()) {
|
||||
case STRING:
|
||||
value = reader.nextString();
|
||||
break;
|
||||
case BOOLEAN:
|
||||
value = reader.nextBoolean();
|
||||
break;
|
||||
case NUMBER:
|
||||
value = reader.nextLong();
|
||||
break;
|
||||
}
|
||||
map.put(name, value);
|
||||
}
|
||||
reader.endObject();
|
||||
return map;
|
||||
}
|
||||
|
||||
protected void writeNullSafeArray(JsonWriter writer, Set<String> items)
|
||||
throws IOException {
|
||||
if (items != null) {
|
||||
writer.beginArray();
|
||||
for (String s : items) {
|
||||
writer.value(s);
|
||||
}
|
||||
writer.endArray();
|
||||
} else {
|
||||
writer.nullValue();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,6 +16,10 @@
|
|||
*******************************************************************************/
|
||||
package org.mitre.openid.connect.service.impl;
|
||||
|
||||
import static org.mitre.util.JsonUtils.base64UrlDecodeObject;
|
||||
import static org.mitre.util.JsonUtils.readMap;
|
||||
import static org.mitre.util.JsonUtils.readSet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.util.Collection;
|
||||
|
@ -47,6 +51,7 @@ import org.mitre.openid.connect.model.WhitelistedSite;
|
|||
import org.mitre.openid.connect.repository.ApprovedSiteRepository;
|
||||
import org.mitre.openid.connect.repository.BlacklistedSiteRepository;
|
||||
import org.mitre.openid.connect.repository.WhitelistedSiteRepository;
|
||||
import org.mitre.openid.connect.service.MITREidDataService;
|
||||
import org.mitre.openid.connect.util.DateUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -71,7 +76,7 @@ import com.google.gson.stream.JsonWriter;
|
|||
*/
|
||||
@Service
|
||||
@SuppressWarnings(value = {"unchecked"})
|
||||
public class MITREidDataService_1_0 extends AbstractMITREidDataService {
|
||||
public class MITREidDataService_1_0 implements MITREidDataService {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(MITREidDataService_1_0.class);
|
||||
@Autowired
|
||||
|
|
|
@ -16,6 +16,10 @@
|
|||
*******************************************************************************/
|
||||
package org.mitre.openid.connect.service.impl;
|
||||
|
||||
import static org.mitre.util.JsonUtils.base64UrlDecodeObject;
|
||||
import static org.mitre.util.JsonUtils.readMap;
|
||||
import static org.mitre.util.JsonUtils.readSet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.text.ParseException;
|
||||
|
@ -49,6 +53,7 @@ import org.mitre.openid.connect.model.WhitelistedSite;
|
|||
import org.mitre.openid.connect.repository.ApprovedSiteRepository;
|
||||
import org.mitre.openid.connect.repository.BlacklistedSiteRepository;
|
||||
import org.mitre.openid.connect.repository.WhitelistedSiteRepository;
|
||||
import org.mitre.openid.connect.service.MITREidDataService;
|
||||
import org.mitre.openid.connect.util.DateUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -74,7 +79,7 @@ import com.google.gson.stream.JsonWriter;
|
|||
*/
|
||||
@Service
|
||||
@SuppressWarnings(value = {"unchecked"})
|
||||
public class MITREidDataService_1_1 extends AbstractMITREidDataService {
|
||||
public class MITREidDataService_1_1 implements MITREidDataService {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(MITREidDataService_1_1.class);
|
||||
@Autowired
|
||||
|
|
|
@ -16,6 +16,12 @@
|
|||
*******************************************************************************/
|
||||
package org.mitre.openid.connect.service.impl;
|
||||
|
||||
import static org.mitre.util.JsonUtils.base64UrlDecodeObject;
|
||||
import static org.mitre.util.JsonUtils.base64UrlEncodeObject;
|
||||
import static org.mitre.util.JsonUtils.readMap;
|
||||
import static org.mitre.util.JsonUtils.readSet;
|
||||
import static org.mitre.util.JsonUtils.writeNullSafeArray;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.text.ParseException;
|
||||
|
@ -49,6 +55,7 @@ import org.mitre.openid.connect.model.WhitelistedSite;
|
|||
import org.mitre.openid.connect.repository.ApprovedSiteRepository;
|
||||
import org.mitre.openid.connect.repository.BlacklistedSiteRepository;
|
||||
import org.mitre.openid.connect.repository.WhitelistedSiteRepository;
|
||||
import org.mitre.openid.connect.service.MITREidDataService;
|
||||
import org.mitre.openid.connect.util.DateUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -74,7 +81,7 @@ import com.google.gson.stream.JsonWriter;
|
|||
*/
|
||||
@Service
|
||||
@SuppressWarnings(value = {"unchecked"})
|
||||
public class MITREidDataService_1_2 extends AbstractMITREidDataService {
|
||||
public class MITREidDataService_1_2 implements MITREidDataService {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(MITREidDataService_1_2.class);
|
||||
@Autowired
|
||||
|
|
|
@ -50,27 +50,27 @@ public class JsonEntityView extends AbstractView {
|
|||
public static final String VIEWNAME = "jsonEntityView";
|
||||
|
||||
private Gson gson = new GsonBuilder()
|
||||
.setExclusionStrategies(new ExclusionStrategy() {
|
||||
|
||||
@Override
|
||||
public boolean shouldSkipField(FieldAttributes f) {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldSkipClass(Class<?> clazz) {
|
||||
// skip the JPA binding wrapper
|
||||
if (clazz.equals(BeanPropertyBindingResult.class)) {
|
||||
return true;
|
||||
.setExclusionStrategies(new ExclusionStrategy() {
|
||||
|
||||
@Override
|
||||
public boolean shouldSkipField(FieldAttributes f) {
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
})
|
||||
.serializeNulls()
|
||||
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
|
||||
.create();
|
||||
|
||||
@Override
|
||||
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();
|
||||
|
||||
@Override
|
||||
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
|
||||
|
|
Loading…
Reference in New Issue