You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2014/11/26 02:07:32 UTC
[5/6] incubator-brooklyn git commit: some base64 / data uri scheme
tidy up as per code review
some base64 / data uri scheme tidy up as per code review
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/53ea4102
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/53ea4102
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/53ea4102
Branch: refs/heads/master
Commit: 53ea41021aaef10de3585c66512f440e6339d68e
Parents: ff0abc3
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Authored: Fri Nov 21 15:22:55 2014 +0000
Committer: Alex Heneveld <al...@cloudsoftcorp.com>
Committed: Fri Nov 21 15:26:18 2014 +0000
----------------------------------------------------------------------
.../brooklyn/util/text/DataUriSchemeParser.java | 28 +++++++++++++++-----
.../java/brooklyn/util/ResourceUtilsTest.java | 7 ++++-
.../src/main/java/brooklyn/util/net/Urls.java | 21 ++++++++++-----
3 files changed, 41 insertions(+), 15 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/53ea4102/core/src/main/java/brooklyn/util/text/DataUriSchemeParser.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/text/DataUriSchemeParser.java b/core/src/main/java/brooklyn/util/text/DataUriSchemeParser.java
index 2ce339d..393ab20 100644
--- a/core/src/main/java/brooklyn/util/text/DataUriSchemeParser.java
+++ b/core/src/main/java/brooklyn/util/text/DataUriSchemeParser.java
@@ -26,12 +26,11 @@ import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.Map;
-import org.bouncycastle.util.encoders.Base64;
-
import brooklyn.util.exceptions.Exceptions;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
+import com.google.common.io.BaseEncoding;
//import com.sun.jersey.core.util.Base64;
/** implementation (currently hokey) of RFC-2397 data: URI scheme.
@@ -47,6 +46,7 @@ public class DataUriSchemeParser {
private boolean isParsed = false;
private boolean allowMissingComma = false;
private boolean allowSlashesAfterColon = false;
+ private boolean allowOtherLaxities = false;
private String mimeType;
private byte[] data;
@@ -100,7 +100,7 @@ public class DataUriSchemeParser {
// ---- config ------------------
public synchronized DataUriSchemeParser lax() {
- return allowMissingComma(true).allowSlashesAfterColon(true);
+ return allowMissingComma(true).allowSlashesAfterColon(true).allowOtherLaxities(true);
}
public synchronized DataUriSchemeParser allowMissingComma(boolean allowMissingComma) {
@@ -115,6 +115,12 @@ public class DataUriSchemeParser {
return this;
}
+ private synchronized DataUriSchemeParser allowOtherLaxities(boolean allowOtherLaxities) {
+ assertNotParsed();
+ this.allowOtherLaxities = allowOtherLaxities;
+ return this;
+ }
+
private void assertNotParsed() {
if (isParsed) throw new IllegalStateException("Operation not permitted after parsing");
}
@@ -223,15 +229,23 @@ public class DataUriSchemeParser {
private void parseData() throws UnsupportedEncodingException, MalformedURLException {
if (parameters.containsKey("base64")) {
- String base64value = parameters.get("base64");
- if (base64value!=null)
- throw new MalformedURLException("base64 parameter must not take a value ("+base64value+") in data: URL");
- data = Base64.decode(remainder());
+ checkNoParamValue("base64");
+ data = BaseEncoding.base64().decode(remainder());
+ } else if (parameters.containsKey("base64url")) {
+ checkNoParamValue("base64url");
+ data = BaseEncoding.base64Url().decode(remainder());
} else {
data = URLDecoder.decode(remainder(), getCharset()).getBytes(Charset.forName(getCharset()));
}
}
+ private void checkNoParamValue(String param) throws MalformedURLException {
+ if (allowOtherLaxities) return;
+ String value = parameters.get(param);
+ if (value!=null)
+ throw new MalformedURLException(param+" parameter must not take a value ("+value+") in data: URL");
+ }
+
private String remainder() {
return url.substring(parseIndex);
}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/53ea4102/core/src/test/java/brooklyn/util/ResourceUtilsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/util/ResourceUtilsTest.java b/core/src/test/java/brooklyn/util/ResourceUtilsTest.java
index 0d4ddc1..a588513 100644
--- a/core/src/test/java/brooklyn/util/ResourceUtilsTest.java
+++ b/core/src/test/java/brooklyn/util/ResourceUtilsTest.java
@@ -39,6 +39,7 @@ import org.testng.annotations.Test;
import brooklyn.util.net.Urls;
import brooklyn.util.os.Os;
import brooklyn.util.stream.Streams;
+import brooklyn.util.text.Identifiers;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
@@ -145,7 +146,7 @@ public class ResourceUtilsTest {
public void testClassLoaderDirNotFound() throws Exception {
String d = utils.getClassLoaderDir("/somewhere/not/found/XXX.xxx");
// above should fail
- log.warn("Uh oh found iamginary resource in: "+d);
+ log.warn("Uh oh found imaginary resource in: "+d);
}
@Test(groups="Integration")
@@ -170,6 +171,10 @@ public class ResourceUtilsTest {
assertEquals(utils.getResourceAsString("data://hello"), "hello");
assertEquals(utils.getResourceAsString("data:hello world"), "hello world");
assertEquals(utils.getResourceAsString(Urls.asDataUrlBase64("hello world")), "hello world");
+
+ String longString = Identifiers.makeRandomId(256);
+ for (int a=32; a<128; a++) longString += (char)a;
+ assertEquals(utils.getResourceAsString(Urls.asDataUrlBase64(longString)), longString);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/53ea4102/utils/common/src/main/java/brooklyn/util/net/Urls.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/brooklyn/util/net/Urls.java b/utils/common/src/main/java/brooklyn/util/net/Urls.java
index 63a60f9..917b58a 100644
--- a/utils/common/src/main/java/brooklyn/util/net/Urls.java
+++ b/utils/common/src/main/java/brooklyn/util/net/Urls.java
@@ -28,12 +28,11 @@ import java.net.URLEncoder;
import javax.annotation.Nullable;
-import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
-
import brooklyn.util.text.Strings;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
+import com.google.common.io.BaseEncoding;
import com.google.common.net.MediaType;
public class Urls {
@@ -217,12 +216,20 @@ public class Urls {
}
/**
- * Creates a "data:..." scheme URL for use with supported parsers.
- * (But note, by default Java's URL is not one of them.)
- * It is not necessary to base64 encode it, but good practise;
- * null type means no type included. */
+ * Creates a "data:..." scheme URL for use with supported parsers, using Base64 encoding.
+ * (But note, by default Java's URL is not one of them, although Brooklyn's ResourceUtils does support it.)
+ * <p>
+ * It is not necessary (at least for Brookyn's routines) to base64 encode it, but recommended as that is likely more
+ * portable and easier to work with if odd characters are included.
+ * <p>
+ * It is worth noting that Base64 uses '+' which can be replaced by ' ' in some URL parsing.
+ * But in practice it does not seem to cause issues.
+ * An alternative is to use {@link BaseEncoding#base64Url()} but it is not clear how widely that is supported
+ * (nor what parameter should be given to indicate that type of encoding, as the spec calls for 'base64'!)
+ * <p>
+ * null type means no type info will be included in the URL. */
public static String asDataUrlBase64(MediaType type, byte[] bytes) {
- return "data:"+(type!=null ? type.withoutParameters().toString() : "")+";base64,"+new String(Base64Coder.encode(bytes));
+ return "data:"+(type!=null ? type.withoutParameters().toString() : "")+";base64,"+new String(BaseEncoding.base64().encode(bytes));
}
}