From eb83f17c019b9226afbe9391d00b9f1f26d3ac11 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 18 May 2015 20:09:49 -0700 Subject: [PATCH 1/3] GUAC-587: Implement byte array resource. --- .../net/basic/resource/ByteArrayResource.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 guacamole/src/main/java/org/glyptodon/guacamole/net/basic/resource/ByteArrayResource.java diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/resource/ByteArrayResource.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/resource/ByteArrayResource.java new file mode 100644 index 000000000..86bfb568d --- /dev/null +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/resource/ByteArrayResource.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2015 Glyptodon LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.glyptodon.guacamole.net.basic.resource; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +/** + * A resource which contains a defined byte array. + * + * @author Michael Jumper + */ +public class ByteArrayResource extends AbstractResource { + + /** + * The bytes contained by this resource. + */ + private final byte[] bytes; + + /** + * Creates a new ByteArrayResource which provides access to the given byte + * array. Changes to the given byte array will affect this resource even + * after the resource is created. Changing the byte array while an input + * stream from this resource is in use has undefined behavior. + * + * @param mimetype + * The mimetype of the resource. + * + * @param bytes + * The bytes that this resource should contain. + */ + public ByteArrayResource(String mimetype, byte[] bytes) { + super(mimetype); + this.bytes = bytes; + } + + @Override + public InputStream asStream() { + return new ByteArrayInputStream(bytes); + } + +} From c10cbc3a68bd81d49db98e73af1adf663cd6e32c Mon Sep 17 00:00:00 2001 From: James Muehlner Date: Mon, 18 May 2015 21:22:29 -0700 Subject: [PATCH 2/3] GUAC-587: Merge translation files when loading extensions. --- .../extension/LanguageResourceService.java | 75 ++++++++++++++++++- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/extension/LanguageResourceService.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/extension/LanguageResourceService.java index e2bac09b0..481f8e613 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/extension/LanguageResourceService.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/extension/LanguageResourceService.java @@ -24,15 +24,22 @@ package org.glyptodon.guacamole.net.basic.extension; import java.io.IOException; import java.io.InputStream; +import java.math.BigDecimal; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Set; +import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.ServletContext; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.node.JsonNodeFactory; +import org.codehaus.jackson.node.ObjectNode; +import org.codehaus.jackson.type.TypeReference; +import org.glyptodon.guacamole.net.basic.resource.ByteArrayResource; import org.glyptodon.guacamole.net.basic.resource.Resource; import org.glyptodon.guacamole.net.basic.resource.WebApplicationResource; import org.slf4j.Logger; @@ -102,6 +109,45 @@ public class LanguageResourceService { } + /** + * Merges the given JSON objects. Any leaf node in overlay will overwrite + * the corresponding path in original. + * + * @param original + * The original JSON object to which changes should be applied. + * + * @param overlay + * The JSON object containing changes that should be applied. + * + * @return + * The newly constructed JSON object that is the result of merging + * original and overlay. + */ + private JsonNode mergeTranslations(JsonNode original, JsonNode overlay) { + + // If we are at a leaf node, the result of merging is simply the overlay + if (!overlay.isObject() || original == null) + return overlay; + + // Create mutable copy of original + ObjectNode newNode = JsonNodeFactory.instance.objectNode(); + Iterator fieldNames = original.getFieldNames(); + while (fieldNames.hasNext()) { + String fieldName = fieldNames.next(); + newNode.put(fieldName, original.get(fieldName)); + } + + // Merge each field + fieldNames = overlay.getFieldNames(); + while (fieldNames.hasNext()) { + String fieldName = fieldNames.next(); + newNode.put(fieldName, mergeTranslations(original.get(fieldName), overlay.get(fieldName))); + } + + return newNode; + + } + /** * Adds or overlays the given language resource, which need not exist in * the ServletContext. If a language resource is already defined for the @@ -123,8 +169,33 @@ public class LanguageResourceService { // Merge language resources if already defined Resource existing = resources.get(key); if (existing != null) { - // TODO: Merge - logger.debug("Merged strings with existing language: \"{}\"", key); + + try { + + // Get resource stream + InputStream existingStream = existing.asStream(); + InputStream resourceStream = resource.asStream(); + if (existingStream == null || resourceStream == null) { + logger.warn("Language resource \"{}\" does not exist.", key); + return; + } + + // Read the original and new language resources + JsonNode existingTree = mapper.readTree(existingStream); + JsonNode resourceTree = mapper.readTree(resourceStream); + + // Merge the language resources + JsonNode mergedTree = mergeTranslations(existingTree, resourceTree); + resources.put(key, new ByteArrayResource("application/json", mapper.writeValueAsBytes(mergedTree))); + + logger.debug("Merged strings with existing language: \"{}\"", key); + + } + catch (IOException e) { + logger.error("Unable to merge language resource \"{}\": {}", key, e.getMessage()); + logger.debug("Error merging language resource.", e); + } + } // Otherwise, add new language resource From e06a5fb2c48d945ae154e3cf6ef01d6dcc2d82c1 Mon Sep 17 00:00:00 2001 From: James Muehlner Date: Mon, 18 May 2015 21:29:18 -0700 Subject: [PATCH 3/3] GUAC-587 Removed pointless imports. --- .../guacamole/net/basic/extension/LanguageResourceService.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/extension/LanguageResourceService.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/extension/LanguageResourceService.java index 481f8e613..56cf14ea8 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/extension/LanguageResourceService.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/extension/LanguageResourceService.java @@ -24,13 +24,11 @@ package org.glyptodon.guacamole.net.basic.extension; import java.io.IOException; import java.io.InputStream; -import java.math.BigDecimal; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; -import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.ServletContext; @@ -38,7 +36,6 @@ import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.node.JsonNodeFactory; import org.codehaus.jackson.node.ObjectNode; -import org.codehaus.jackson.type.TypeReference; import org.glyptodon.guacamole.net.basic.resource.ByteArrayResource; import org.glyptodon.guacamole.net.basic.resource.Resource; import org.glyptodon.guacamole.net.basic.resource.WebApplicationResource;