From 70721aa00fa8eea76e9b632e7f17dd4424d2ad4e Mon Sep 17 00:00:00 2001 From: Gerwin Gsenger Date: Tue, 13 Jan 2015 09:22:39 +0100 Subject: use Jackson 2.5.0, write JSON to file, evaluate @JsonIgnore Annotation --- .../id/config/auth/AuthConfigurationProvider.java | 3 + .../config/auth/ConfigurationToJSONConverter.java | 67 ++++++++++++++-------- 2 files changed, 46 insertions(+), 24 deletions(-) (limited to 'id/server/idserverlib/src/main') diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/AuthConfigurationProvider.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/AuthConfigurationProvider.java index d33a9ea92..0049813d3 100644 --- a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/AuthConfigurationProvider.java +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/AuthConfigurationProvider.java @@ -117,6 +117,8 @@ import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.util.MiscUtil; import at.gv.util.config.EgovUtilPropertiesConfiguration; +import com.fasterxml.jackson.annotation.JsonIgnore; + /** * A class providing access to the Auth Part of the MOA-ID configuration data. * @@ -1118,6 +1120,7 @@ public class AuthConfigurationProvider extends ConfigurationProvider { /** * @return the eGovUtilsConfig */ +@JsonIgnore public EgovUtilPropertiesConfiguration geteGovUtilsConfig() { return eGovUtilsConfig; } diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java index 36063ca2c..2efb1e251 100644 --- a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/config/auth/ConfigurationToJSONConverter.java @@ -3,20 +3,25 @@ package at.gv.egovernment.moa.id.config.auth; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; +import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; -import org.codehaus.jackson.JsonGenerationException; -import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility; -import org.codehaus.jackson.annotate.JsonMethod; -import org.codehaus.jackson.map.JsonMappingException; -import org.codehaus.jackson.map.ObjectMapper; - import at.gv.egovernment.moa.id.config.ConfigurationException; import at.gv.egovernment.moa.id.config.ConfigurationProvider; +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + public class ConfigurationToJSONConverter { AuthConfigurationProvider config; @@ -25,8 +30,7 @@ public class ConfigurationToJSONConverter { try { ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); - - List jsonResults = converter.convertConfigToJSONStrings(); + List jsonResults = converter.convertConfigToJSONStrings(true); System.out.println(jsonResults); } catch (ConfigurationException e) { @@ -41,44 +45,60 @@ public class ConfigurationToJSONConverter { config = AuthConfigurationProvider.getInstance(); } - public void writeConfigToJSONFile(String jsonFileName) { - // get JSON - // prettyprint and write to file + public void writeConfigToJSONFile(String jsonFileName, boolean isPrettyPrint) throws FileNotFoundException, + IOException, ConfigurationException { + File out = new File(jsonFileName); + try (PrintWriter outStream = new PrintWriter(out)) { + // get pretty printed JSON + ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); + List jsonResults = converter.convertConfigToJSONStrings(isPrettyPrint); + for (String json : jsonResults) { + outStream.println(json); + } + } } public void writeConfigToJSONDB() throws ConfigurationException { ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); - List jsonResults = converter.convertConfigToJSONStrings(); + List jsonResults = converter.convertConfigToJSONStrings(false); + if(jsonResults.isEmpty()){ + System.out.println("WARN: writing EMPTY configuration to database"); + } // TODO: write to db } - public List convertConfigToJSONStrings() { + public List convertConfigToJSONStrings(boolean prettyPrint) { List result = new ArrayList(); ObjectMapper mapper = new ObjectMapper(); - mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY); + mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); + mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY); + mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY); + + if (prettyPrint) { + mapper.enable(SerializationFeature.INDENT_OUTPUT); + } - int numberOfCalledGetterMethods = 0; try { - System.out.println("=== Start ==="); // find all getter methods for (PropertyDescriptor pd : Introspector.getBeanInfo(AuthConfigurationProvider.class) .getPropertyDescriptors()) { - if (pd.getReadMethod() != null && !"class".equals(pd.getName())) { - + // check if correct methods, and not annotated with @JsonIgnore + if ((pd.getReadMethod() != null) + && (!"class".equals(pd.getName())) + && (pd.getReadMethod().getAnnotation(JsonIgnore.class) == null)) { try { // get result of get method Object tmp = pd.getReadMethod().invoke(config); - // result to JSON + // convert result to JSON String show = mapper.writeValueAsString(tmp); + System.out.println("Method: " + pd.getDisplayName()); System.out.println(show); result.add(show); - numberOfCalledGetterMethods++; } catch (JsonGenerationException | JsonMappingException e) { - e.printStackTrace(); - // System.out.println("Problems while writing JSON as String"); - // return new ArrayList(); + System.out.println("Problems while writing JSON as String"); + return new ArrayList(); } } } @@ -92,7 +112,6 @@ public class ConfigurationToJSONConverter { return new ArrayList(); } - System.out.println("=== END called:'" + numberOfCalledGetterMethods + "' getter-methods ==="); return result; } -- cgit v1.2.3