@Test public void Conversions_toMap_DoesNotAllowReplacement() { ObjectMapper o = new ObjectMapperImpl(); Stuffing stuffing = new Stuffing(); stuffing.name = "i'm Stuffed!"; stuffing.stuff = Stuff.THIS_KIND_OF_STUFF; stuffing.stuffed = new Stuffing.Stuffed(); stuffing.stuffed.dateStuffed = new Date(); // Make it a string to start with. (pretend this is JSON coming over the wire as a string, for // example String jsonVersion = o.toJson(stuffing); // Now lets make it a map to grab stuff and change stuff Map<String, Object> requestObject = o.fromJson(jsonVersion, Map.class); puts("Before", requestObject); /* It is not a real map. It is a facade over a index overlay. */ requestObject = new HashMap<>(requestObject); Map<String, Object> stuffed = new HashMap<>((Map<String, Object>) requestObject.get("stuffed")); stuffed.put("dateStuffed", System.currentTimeMillis()); stuffed.put("bacon", "yummy"); requestObject.put("stuffed", stuffed); puts("After", requestObject); }
@Override public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { BufferedWriter writer = IOHelper.buffered(new OutputStreamWriter(stream, IOHelper.getCharsetName(exchange))); objectMapper.toJson(graph, writer); writer.close(); }
@Override public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { BufferedReader reader = IOHelper.buffered(new InputStreamReader(stream, IOHelper.getCharsetName(exchange))); Object result = objectMapper.fromJson(reader, this.unmarshalType); reader.close(); return result; }
@Override public void writeSystemProperties(Map<String, String> updatedSystemProperties) { if (updatedSystemProperties == null) { return; } // Get system.properties file // save off the current/old hostname before we make any changes oldHostName = SystemBaseUrl.getHost(); String etcDir = System.getProperty(KARAF_ETC); String systemPropertyFilename = etcDir + File.separator + SYSTEM_PROPERTIES_FILE; String userPropertiesFilename = etcDir + File.separator + USERS_PROPERTIES_FILE; String userAttributesFilename = etcDir + File.separator + USERS_ATTRIBUTES_FILE; File systemPropertiesFile = new File(systemPropertyFilename); File userPropertiesFile = new File(userPropertiesFilename); File userAttributesFile = new File(userAttributesFilename); try { Properties systemDotProperties = new Properties(systemPropertiesFile); updateProperty(SystemBaseUrl.PORT, updatedSystemProperties, systemDotProperties); updateProperty(SystemBaseUrl.HOST, updatedSystemProperties, systemDotProperties); updateProperty(SystemBaseUrl.PROTOCOL, updatedSystemProperties, systemDotProperties); updateProperty(SystemBaseUrl.HTTP_PORT, updatedSystemProperties, systemDotProperties); updateProperty(SystemBaseUrl.HTTPS_PORT, updatedSystemProperties, systemDotProperties); updateProperty(SystemInfo.ORGANIZATION, updatedSystemProperties, systemDotProperties); updateProperty(SystemInfo.SITE_CONTACT, updatedSystemProperties, systemDotProperties); updateProperty(SystemInfo.SITE_NAME, updatedSystemProperties, systemDotProperties); updateProperty(SystemInfo.VERSION, updatedSystemProperties, systemDotProperties); systemDotProperties.save(); } catch (IOException e) { LOGGER.warn("Exception while writing to system.properties file.", e); } try { Properties userDotProperties = new Properties(userPropertiesFile); if (!userDotProperties.isEmpty()) { String oldHostValue = userDotProperties.getProperty(oldHostName); if (oldHostValue != null) { userDotProperties.remove(oldHostName); userDotProperties.setProperty(System.getProperty(SystemBaseUrl.HOST), oldHostValue); userDotProperties.save(); } } } catch (IOException e) { LOGGER.warn("Exception while writing to users.properties file.", e); } Map<String, Object> json = null; try (InputStream stream = Files.newInputStream(Paths.get(userAttributesFile.toURI()))) { json = MAPPER.parser().parseMap(stream); if (json.containsKey(oldHostName)) { json.put(System.getProperty(SystemBaseUrl.HOST), json.remove(oldHostName)); } } catch (IOException e) { LOGGER.error("Unable to read system user attribute file for hostname update.", e); } if (json != null) { try (OutputStream stream = Files.newOutputStream(Paths.get(userAttributesFile.toURI()))) { MAPPER.writeValue(stream, json); } catch (IOException e) { LOGGER.error("Unable to write system user attribute file for hostname update.", e); } } }