diff options
author | Gerwin Gsenger <g.gsenger@datentechnik-innovation.at> | 2015-01-13 09:22:39 +0100 |
---|---|---|
committer | Thomas Lenz <tlenz@iaik.tugraz.at> | 2015-06-19 11:05:54 +0200 |
commit | 70721aa00fa8eea76e9b632e7f17dd4424d2ad4e (patch) | |
tree | 61121a92995956c59b04293ffcd53415ca86e936 | |
parent | 3d8ea79b6167a2e4784beaafe5596cc78519e358 (diff) | |
download | moa-id-spss-70721aa00fa8eea76e9b632e7f17dd4424d2ad4e.tar.gz moa-id-spss-70721aa00fa8eea76e9b632e7f17dd4424d2ad4e.tar.bz2 moa-id-spss-70721aa00fa8eea76e9b632e7f17dd4424d2ad4e.zip |
use Jackson 2.5.0, write JSON to file, evaluate @JsonIgnore Annotation
2 files changed, 46 insertions, 24 deletions
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<String> jsonResults = converter.convertConfigToJSONStrings(); + List<String> 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<String> jsonResults = converter.convertConfigToJSONStrings(isPrettyPrint); + for (String json : jsonResults) { + outStream.println(json); + } + } } public void writeConfigToJSONDB() throws ConfigurationException { ConfigurationToJSONConverter converter = new ConfigurationToJSONConverter(); - List<String> jsonResults = converter.convertConfigToJSONStrings(); + List<String> jsonResults = converter.convertConfigToJSONStrings(false); + if(jsonResults.isEmpty()){ + System.out.println("WARN: writing EMPTY configuration to database"); + } // TODO: write to db } - public List<String> convertConfigToJSONStrings() { + public List<String> convertConfigToJSONStrings(boolean prettyPrint) { List<String> result = new ArrayList<String>(); 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<String>(); + System.out.println("Problems while writing JSON as String"); + return new ArrayList<String>(); } } } @@ -92,7 +112,6 @@ public class ConfigurationToJSONConverter { return new ArrayList<String>(); } - System.out.println("=== END called:'" + numberOfCalledGetterMethods + "' getter-methods ==="); return result; } |