pax_global_header 0000666 0000000 0000000 00000000064 13414154036 0014513 g ustar 00root root 0000000 0000000 52 comment=05eea9a846f61e8c729439b1bfad181b45367166
data-url-data-url-1.0.1/ 0000775 0000000 0000000 00000000000 13414154036 0014732 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/.gitignore 0000664 0000000 0000000 00000000114 13414154036 0016716 0 ustar 00root root 0000000 0000000 .settings
.checkstyle
.classpath
.project
src/site/markdown/index.md
target
data-url-data-url-1.0.1/LICENSE.txt 0000664 0000000 0000000 00000026136 13414154036 0016565 0 ustar 00root root 0000000 0000000
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
data-url-data-url-1.0.1/README.md 0000664 0000000 0000000 00000003556 13414154036 0016222 0 ustar 00root root 0000000 0000000 # data-url
The `data-url` library adds support for the `data` protocol as specified in [RFC 2397](https://www.ietf.org/rfc/rfc2397.txt).
There are several ways to create data URLs. Most are described by the [URL](https://docs.oracle.com/javase/8/docs/api/java/net/URL.html#URL-java.lang.String-java.lang.String-int-java.lang.String-) class.
1. Make use of a shared [URLStreamHandlerFactory](https://docs.oracle.com/javase/8/docs/api/java/net/URLStreamHandlerFactory.html) set on the [URL](https://docs.oracle.com/javase/8/docs/api/java/net/URL.html#setURLStreamHandlerFactory-java.net.URLStreamHandlerFactory-) class. This must return an instance of [Handler](https://robtimus.github.io/data-url/apidocs/com/github/robtimus/net/protocol/data/Handler.html) for the `data` protocol.
2. Add package `com.github.robtimus.net.protocol` to system property `java.protocol.handler.pkgs`.
3. Use [this](https://docs.oracle.com/javase/8/docs/api/java/net/URL.html#URL-java.net.URL-java.lang.String-java.net.URLStreamHandler-) URL constructor, and provide an instance of [Handler](https://robtimus.github.io/data-url/apidocs/com/github/robtimus/net/protocol/data/Handler.html) as the [URLStreamHandler](https://docs.oracle.com/javase/8/docs/api/java/net/URLStreamHandler.html). The `context` argument can remain `null`.
* It's ill-advised to use [this](https://docs.oracle.com/javase/8/docs/api/java/net/URL.html#URL-java.lang.String-java.lang.String-int-java.lang.String-java.net.URLStreamHandler-) constructor, because it does not ensure the data URL is correctly formatted.
4. Use utility class [DataURLs](https://robtimus.github.io/data-url/apidocs/com/github/robtimus/net/protocol/data/DataURLs.html).
Note that class [Handler](https://robtimus.github.io/data-url/apidocs/com/github/robtimus/net/protocol/data/Handler.html) is stateless, and therefore instances can be shared among multiple threads.
data-url-data-url-1.0.1/pom.xml 0000664 0000000 0000000 00000034675 13414154036 0016266 0 ustar 00root root 0000000 0000000
4.0.0com.github.robtimusdata-url1.0.1jardata-urlSupport for data URLs as specified in RFC 2397 (https://www.ietf.org/rfc/rfc2397.txt)https://robtimus.github.io/data-url/2017The Apache Software License, Version 2.0http://www.apache.org/licenses/LICENSE-2.0.txtRob Spoorrobtimus@users.noreply.github.comscm:git:git@github.com:robtimus/data-url.gitscm:git:git@github.com:robtimus/data-url.gitdata-url-1.0.1https://github.com/robtimus/data-urlGitHubhttps://github.com/robtimus/data-url/issuesossrhhttps://oss.sonatype.org/service/local/staging/deploy/maven2/ossrhhttps://oss.sonatype.org/content/repositories/snapshotsUTF-81.81.34.121.10.191.18.01.83.1.03.1.03.7.03.1.12.8.21.61.6.81.03.0.02.5.23.1.03.0.12.5.33.1.03.7.13.0.12.22.0junitjunit${version.junit}testorg.hamcresthamcrest-library${version.hamcrest}testorg.mockitomockito-all${version.mockito}testcom.github.stefanbirknersystem-rules${version.system-rules}testsrc/main/resources${basedir}META-INFLICENSE.txtorg.apache.maven.pluginsmaven-javadoc-plugin${version.plugin.javadoc}UTF-8UTF-8truetruetruetrue
https://docs.oracle.com/javase/8/docs/api/
org.eclipse.m2elifecycle-mapping1.0.0org.apache.maven.pluginsmaven-antrun-plugin[1.0,)runorg.apache.maven.pluginsmaven-compiler-plugin${version.plugin.compiler}${version.java}${version.java}org.apache.maven.pluginsmaven-dependency-plugin${version.plugin.dependency}org.apache.maven.pluginsmaven-jar-plugin${version.plugin.jar}org.apache.maven.pluginsmaven-source-plugin${version.plugin.source}verifyjar-no-forkorg.apache.maven.pluginsmaven-resources-plugin${version.plugin.resources}org.apache.maven.pluginsmaven-javadoc-pluginverifyjarcom.github.robtimusi18n-maven-plugin${version.plugin.i18n}generatecom.github.robtimus.net.protocol.data.data-urlcom.github.robtimus.net.protocol.data.MessagesApache-2.0Rob Spoor2017falsenlsorg.apache.maven.pluginsmaven-antrun-plugin${version.plugin.antrun}copy-licensegenerate-sourcesruncopy-readmepre-siterunorg.apache.maven.pluginsmaven-assembly-plugin${version.plugin.assembly}src/main/assembly/src.xmlsrc/main/assembly/bin.xmlfalseverifysingleorg.apache.maven.pluginsmaven-clean-plugin${version.plugin.clean}org.apache.maven.pluginsmaven-surefire-plugin${version.plugin.surefire}org.apache.maven.pluginsmaven-install-plugin${version.plugin.install}org.apache.maven.pluginsmaven-release-plugin${version.plugin.release}falsereleasedeployorg.apache.maven.pluginsmaven-gpg-plugin${version.plugin.gpg}verifysignorg.sonatype.pluginsnexus-staging-maven-plugin${version.plugin.nexus-staging}trueossrhhttps://oss.sonatype.org/trueorg.apache.maven.pluginsmaven-deploy-plugin${version.plugin.deploy}deploydeployorg.apache.maven.pluginsmaven-site-plugin${version.plugin.site}org.apache.maven.pluginsmaven-project-info-reports-plugin${version.plugin.info-reports}org.apache.maven.pluginsmaven-project-info-reports-plugin${version.plugin.info-reports}summarydependency-infodependencieslicensesteamscmissue-managementorg.apache.maven.pluginsmaven-javadoc-plugin${version.plugin.javadoc}javadoc
data-url-data-url-1.0.1/src/ 0000775 0000000 0000000 00000000000 13414154036 0015521 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/ 0000775 0000000 0000000 00000000000 13414154036 0016445 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/assembly/ 0000775 0000000 0000000 00000000000 13414154036 0020264 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/assembly/bin.xml 0000664 0000000 0000000 00000001762 13414154036 0021564 0 ustar 00root root 0000000 0000000
binzipfalseREADME.mdLICENSE.txtlibtruetrueruntimelibtruecompile
data-url-data-url-1.0.1/src/main/assembly/src.xml 0000664 0000000 0000000 00000001250 13414154036 0021573 0 ustar 00root root 0000000 0000000
srcziptruepom.xmlREADME.mdLICENSE.txtsrc
data-url-data-url-1.0.1/src/main/java/ 0000775 0000000 0000000 00000000000 13414154036 0017366 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/java/com/ 0000775 0000000 0000000 00000000000 13414154036 0020144 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/java/com/github/ 0000775 0000000 0000000 00000000000 13414154036 0021426 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/java/com/github/robtimus/ 0000775 0000000 0000000 00000000000 13414154036 0023272 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/java/com/github/robtimus/net/ 0000775 0000000 0000000 00000000000 13414154036 0024060 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/java/com/github/robtimus/net/protocol/ 0000775 0000000 0000000 00000000000 13414154036 0025721 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/java/com/github/robtimus/net/protocol/data/ 0000775 0000000 0000000 00000000000 13414154036 0026632 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/java/com/github/robtimus/net/protocol/data/DataURLConnection.java 0000664 0000000 0000000 00000006166 13414154036 0032762 0 ustar 00root root 0000000 0000000 /*
* DataURLConnection.java
* Copyright 2017 Rob Spoor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.robtimus.net.protocol.data;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
/**
* A connection to a {@code data:} URL as specified in RFC 2397.
*
* @author Rob Spoor
*/
public class DataURLConnection extends URLConnection {
private final MediaType mediaType;
private final byte[] data;
private InputStream inputStream;
DataURLConnection(URL url, MediaType mediaType, byte[] data) {
super(url);
this.mediaType = mediaType;
this.data = data;
}
/** {@inheritDoc} */
@Override
public void connect() {
connected = true;
}
/**
* Returns the length of the decoded data part of the data URL.
*
* @return The length of the decoded data part of the data URL.
*/
@Override
public int getContentLength() {
return data.length;
}
/**
* Returns the length of the decoded data part of the data URL.
*
* @return The length of the decoded data part of the data URL.
*/
@Override
public long getContentLengthLong() {
return getContentLength();
}
/**
* Returns the media type of the data URL. If none is specified it will be {@code text/plain;charset=US-ASCII}, as specified in
* RFC 2397.
*
* @return The media type of the data URL.
*/
@Override
public String getContentType() {
return mediaType.toString();
}
/**
* Returns the content encoding of the data URL. This is taken from the data URL's media type.
*
* If no media type is specified, it is assumed to be {@code text/plain;charset=US-ASCII}, as specified in
* RFC 2397. As a result, the content encoding will then be {@code US-ASCII}.
*
* @return The content encoding of the data URL.
*/
@Override
public String getContentEncoding() {
return mediaType.getCharset();
}
/** {@inheritDoc} */
@Override
public InputStream getInputStream() throws IOException {
if (!doInput) {
throw new ProtocolException(Messages.dataURLConnection.getInputStream.falseDoInput.get());
}
if (inputStream == null) {
inputStream = new ByteArrayInputStream(data);
}
return inputStream;
}
}
data-url-data-url-1.0.1/src/main/java/com/github/robtimus/net/protocol/data/DataURLs.java 0000664 0000000 0000000 00000026517 13414154036 0031127 0 ustar 00root root 0000000 0000000 /*
* DataURLs.java
* Copyright 2017 Rob Spoor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.robtimus.net.protocol.data;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* A utility class for data URLs.
*
* @author Rob Spoor
*/
public final class DataURLs {
private static final Handler SHARED_HANDLER = new Handler();
private DataURLs() {
throw new InternalError("cannot create instances of " + getClass().getName()); //$NON-NLS-1$
}
/**
* Creates a new data URL for the given full URL string.
*
* @param spec The string to create a new data URL for.
* @return The created data URL.
* @throws MalformedURLException If the given URL string is invalid.
*/
public static URL create(String spec) throws MalformedURLException {
return new URL(null, spec, SHARED_HANDLER);
}
/**
* Creates a new data URL builder.
*
* @param data The data for created data URLs.
* @return The created builder.
*/
public static Builder.FromText builder(String data) {
Objects.requireNonNull(data);
return new Builder.FromText(sb -> sb.append(data));
}
/**
* Creates a new data URL builder.
*
* Note: the builder's {@link DataURLs.Builder#build()} method will fail with an {@link UncheckedIOException} if an I/O error occurs while
* reading from the data stream.
*
* @param data The data for created data URLs.
* @return The created builder.
*/
public static Builder.FromText builder(Reader data) {
Objects.requireNonNull(data);
return new Builder.FromText(sb -> copyData(data, sb));
}
/**
* Creates a new data URL builder.
*
* @param data The data for created data URLs.
* @return The created builder.
*/
public static Builder.FromBytes builder(byte[] data) {
Objects.requireNonNull(data);
return new Builder.FromBytes(() -> new ByteArrayInputStream(data));
}
/**
* Creates a new data URL builder.
*
* Note: the builder's {@link DataURLs.Builder#build()} method will fail with an {@link UncheckedIOException} if an I/O error occurs while
* reading from the data stream.
*
* @param data The data for created data URLs.
* @return The created builder.
*/
public static Builder.FromBytes builder(InputStream data) {
Objects.requireNonNull(data);
return new Builder.FromBytes(() -> data);
}
private static void copyData(Reader data, StringBuilder dest) {
char[] buffer = new char[4096];
int len;
try {
while ((len = data.read(buffer)) != -1) {
dest.append(buffer, 0, len);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private static void copyData(InputStream data, OutputStream dest) {
byte[] buffer = new byte[4096];
int len;
try {
while ((len = data.read(buffer)) != -1) {
dest.write(buffer, 0, len);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* A class that can be used to build data URLs.
*
* @author Rob Spoor
*/
public abstract static class Builder {
private Builder() {
}
/**
* Specifies the media type of the data URL.
*
* @param mediaType The media type for the data URL.
* @return This builder object.
* @throws NullPointerException If the media type is {@code null}.
* @throws IllegalArgumentException If the media type is invalid.
*/
public WithMediaType withMediaType(String mediaType) {
Objects.requireNonNull(mediaType);
return new WithMediaType(this, MediaType.parse(mediaType));
}
/**
* Creates a new data URL.
*
* @return The created data URL.
*/
public URL build() {
return build(null);
}
abstract URL build(MediaType mediaType);
final URL createURL(String file) {
try {
return new URL(Handler.PROTOCOL, null, -1, file, SHARED_HANDLER);
} catch (MalformedURLException e) {
throw new IllegalStateException(e);
}
}
/**
* A class that can be used to build data URLs from text.
*
* @author Rob Spoor
*/
public static final class FromText extends Builder {
private final Consumer dataAppender;
private FromText(Consumer dataAppender) {
this.dataAppender = dataAppender;
}
@Override
URL build(MediaType mediaType) {
StringBuilder file = new StringBuilder();
if (mediaType != null) {
file.append(mediaType);
}
file.append(',');
dataAppender.accept(file);
return createURL(file.toString());
}
}
/**
* A class that can be used to build data URLs from bytes.
*
* @author Rob Spoor
*/
public static final class FromBytes extends Builder {
private final Supplier dataSupplier;
private boolean base64Data;
private FromBytes(Supplier dataSupplier) {
this.dataSupplier = dataSupplier;
base64Data = true;
}
/**
* Specifies whether or not the data should be base64 encoded. The default is {@code true}.
*
* @param base64Data {@code true} to base64 encode the data, or {@code false} otherwise.
* @return This builder object.
*/
public Builder withBase64Data(boolean base64Data) {
this.base64Data = base64Data;
return this;
}
@Override
URL build(MediaType mediaType) {
StringBuilder file = new StringBuilder();
if (mediaType != null) {
file.append(mediaType);
}
Charset charset = Handler.getCharset(mediaType);
if (base64Data) {
file.append(Handler.BASE64_POSTFIX);
file.append(',');
try (OutputStream appender = new Base64Appender(file);
OutputStream dest = Base64.getEncoder().wrap(appender)) {
copyData(dataSupplier.get(), dest);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} else {
file.append(',');
Reader data = new InputStreamReader(dataSupplier.get(), charset);
copyData(data, file);
}
return createURL(file.toString());
}
}
/**
* A class that can be used to build data URLs with media types.
*
* @author Rob Spoor
*/
public static final class WithMediaType {
private final Builder parent;
private final String mimeType;
private final Map parameters;
private WithMediaType(Builder parent, MediaType mediaType) {
this.parent = parent;
this.mimeType = mediaType.getMimeType();
this.parameters = new LinkedHashMap<>(mediaType.getParameters());
}
/**
* Sets the value for a media type parameter.
*
* @param name The parameter name.
* @param value The parameter value. Use {@code null} to remove an existing parameter.
* @return This builder object.
*/
public WithMediaType withMediaTypeParameter(String name, String value) {
Objects.requireNonNull(name);
if (value == null) {
parameters.remove(name);
} else {
parameters.put(name, value);
}
return this;
}
/**
* Sets the charset of the media type.
* This method is shorthand for {@link #withMediaTypeParameter(String, String) withMediaTypeParameter("charset", charset.name())}.
*
* @param charset The charset.
* @return This builder object.
* @throws NullPointerException If the charset is {@code null}.
*/
public WithMediaType withCharset(Charset charset) {
String value = charset.name();
return withMediaTypeParameter("charset", value); //$NON-NLS-1$
}
/**
* Creates a new data URL.
*
* @return The created data URL.
*/
public URL build() {
MediaType mediaType = MediaType.create(mimeType, parameters);
return parent.build(mediaType);
}
}
}
static final class Base64Appender extends OutputStream {
private final StringBuilder dest;
// lazy initialize the buffer; right now Base64 only uses write(int), but that may change
private char[] buffer;
Base64Appender(StringBuilder dest) {
this.dest = dest;
}
@Override
public void write(int b) throws IOException {
char c = convert(b);
dest.append(c);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
int offset = off;
int remaining = len;
while (remaining > 0) {
int n = convertToBuffer(b, offset, remaining);
dest.append(buffer, 0, n);
offset += n;
remaining -= n;
}
}
private char convert(int b) {
return (char) b;
}
private int convertToBuffer(byte[] b, int off, int len) {
if (buffer == null) {
buffer = new char[1024];
}
for (int i = off, j = 0; i < off + len && j < buffer.length; j++, i++) {
buffer[j] = convert(b[i]);
}
return Math.min(buffer.length, len);
}
}
}
data-url-data-url-1.0.1/src/main/java/com/github/robtimus/net/protocol/data/Handler.java 0000664 0000000 0000000 00000011010 13414154036 0031043 0 ustar 00root root 0000000 0000000 /*
* Handler.java
* Copyright 2017 Rob Spoor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.robtimus.net.protocol.data;
import java.io.UnsupportedEncodingException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLStreamHandler;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* A stream protocol handler for the {@code data:} protocol as specified in RFC 2397.
*
* @author Rob Spoor
*/
public class Handler extends URLStreamHandler {
/** The protocol name. */
public static final String PROTOCOL = "data"; //$NON-NLS-1$
static final String BASE64_POSTFIX = ";base64"; //$NON-NLS-1$
private static final int BASE64_POSTFIX_LENGTH = BASE64_POSTFIX.length();
/** {@inheritDoc} */
@Override
protected DataURLConnection openConnection(URL u) {
validateProtocol(u);
String spec = u.toExternalForm();
int start = PROTOCOL.length() + 1;
int limit = spec.length();
int indexOfComma = validateCommaPresent(spec, start, limit);
MediaType mediaType = getMediaType(spec, start, indexOfComma);
boolean base64Data = isBase64Data(spec, indexOfComma);
// ignore the data
byte[] data = getData(spec, indexOfComma, limit, mediaType, base64Data);
return new DataURLConnection(u, mediaType, data);
}
/** {@inheritDoc} */
@Override
protected DataURLConnection openConnection(URL u, Proxy p) {
return openConnection(u);
}
/** {@inheritDoc} */
@Override
protected void parseURL(URL u, String spec, int start, int limit) {
validateProtocol(u);
int indexOfComma = validateCommaPresent(spec, start, limit);
MediaType mediaType = getMediaType(spec, start, indexOfComma);
boolean base64Data = isBase64Data(spec, indexOfComma);
// ignore the data
getData(spec, indexOfComma, limit, mediaType, base64Data);
String path = spec.substring(start, limit);
setURL(u, u.getProtocol(), null, -1, null, null, path, null, null);
}
private void validateProtocol(URL u) {
if (!PROTOCOL.equals(u.getProtocol())) {
throw new IllegalArgumentException(Messages.handler.invalidProtocol.get(PROTOCOL, u.getProtocol()));
}
}
private int validateCommaPresent(String spec, int start, int limit) {
int indexOfComma = spec.indexOf(',', start);
if (indexOfComma == -1 || indexOfComma > limit) {
throw new IllegalArgumentException(Messages.handler.missingComma.get(spec));
}
return indexOfComma;
}
private MediaType getMediaType(String spec, int start, int indexOfComma) {
int end = indexOfComma;
if (isBase64Data(spec, indexOfComma)) {
end -= BASE64_POSTFIX_LENGTH;
}
return start == end ? MediaType.DEFAULT : MediaType.parse(spec, start, end);
}
private boolean isBase64Data(String spec, int indexOfComma) {
return spec.regionMatches(indexOfComma - BASE64_POSTFIX_LENGTH, BASE64_POSTFIX, 0, BASE64_POSTFIX_LENGTH);
}
private byte[] getData(String spec, int indexOfComma, int limit, MediaType mediaType, boolean base64Data) {
String dataPart = spec.substring(indexOfComma + 1, limit);
if (base64Data) {
String s = dataPart.replaceAll("\\s+", ""); //$NON-NLS-1$ //$NON-NLS-2$
return Base64.getDecoder().decode(s);
}
try {
Charset charset = getCharset(mediaType);
String s = URLDecoder.decode(dataPart, charset.name());
return s.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
static Charset getCharset(MediaType mediaType) {
String encoding = mediaType == null ? null : mediaType.getCharset();
return encoding == null ? StandardCharsets.US_ASCII : Charset.forName(encoding);
}
}
data-url-data-url-1.0.1/src/main/java/com/github/robtimus/net/protocol/data/MediaType.java 0000664 0000000 0000000 00000021251 13414154036 0031357 0 ustar 00root root 0000000 0000000 /*
* MediaType.java
* Copyright 2017 Rob Spoor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.robtimus.net.protocol.data;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;
final class MediaType {
private static final String DEFAULT_MIME_TYPE = "text/plain"; //$NON-NLS-1$
private static final String DEFAULT_CHARSET = "US-ASCII"; //$NON-NLS-1$
private static final String DEFAULT_MEDIA_TYPE = DEFAULT_MIME_TYPE + ";charset=" + DEFAULT_CHARSET; //$NON-NLS-1$
private static final Map DEFAULT_PARAMETERS = Collections.singletonMap("charset", DEFAULT_CHARSET); //$NON-NLS-1$
static final MediaType DEFAULT = new MediaType(DEFAULT_MEDIA_TYPE, DEFAULT_MIME_TYPE, DEFAULT_PARAMETERS);
private final String mediaType;
private final String mimeType;
private final Map parameters;
private final Map parameterLookup;
private MediaType(String mediaType, String mimeType, Map parameters) {
this.mediaType = mediaType;
this.mimeType = mimeType;
this.parameters = Collections.unmodifiableMap(parameters);
parameterLookup = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
parameterLookup.putAll(parameters);
}
static MediaType create(String mimeType, Map parameters) {
validateMimeType(mimeType, 0, mimeType.length());
String mediaType = buildMediaType(mimeType, parameters);
return new MediaType(mediaType, mimeType, new LinkedHashMap<>(parameters));
}
private static String buildMediaType(String mimeType, Map parameters) {
StringBuilder mediaType = new StringBuilder();
mediaType.append(mimeType);
for (Map.Entry entry : parameters.entrySet()) {
mediaType.append(';').append(entry.getKey()).append('=');
String value = entry.getValue();
if (value != null) {
boolean containsSemicolon = value.indexOf(';') != -1;
if (containsSemicolon) {
mediaType.append('"');
}
appendValue(value, mediaType);
if (containsSemicolon) {
mediaType.append('"');
}
}
}
return mediaType.toString();
}
private static void appendValue(String value, StringBuilder mediaType) {
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (c == '"' || c == '\\') {
mediaType.append('\\');
}
mediaType.append(c);
}
}
static MediaType parse(String type) {
return parse(type, 0, type.length());
}
static MediaType parse(String type, int start, int end) {
int index = type.indexOf(';', start);
if (isNotFound(index, end)) {
validateMimeType(type, start, end);
String mimeType = type.substring(start, end);
return new MediaType(mimeType, mimeType, Collections.emptyMap());
}
int mimeTypeStart = start;
int mimeTypeEnd = index;
validateMimeType(type, mimeTypeStart, mimeTypeEnd);
int paramStart = skipStartingWhitespace(type, index + 1, end);
int paramEnd = end;
Map parameters = parseParameters(type, paramStart, paramEnd);
String mimeType = type.substring(mimeTypeStart, mimeTypeEnd);
return new MediaType(type, mimeType, parameters);
}
private static final String TOKEN = "[\u0021-\u007e&&[^()<>@,;:\\\\\"/\\[\\]?=]]"; //$NON-NLS-1$
private static final Pattern MIME_TYPE_PATTERN = Pattern.compile(TOKEN + "+/" + TOKEN + "+"); //$NON-NLS-1$ //$NON-NLS-2$
private static void validateMimeType(String mimeType, int start, int end) {
// Use a CharSequence wrapper to cap the limits of the mime type, to prevent the matcher from going out of bounds.
// This wrapper is used instead of mimeType.substring because that copies part of the mime type's contents.
CharSequence s = start == 0 && end == mimeType.length() ? mimeType : new SubString(mimeType, start, end);
if (!MIME_TYPE_PATTERN.matcher(s).matches()) {
throw new IllegalArgumentException(Messages.mediaType.invalidMimeType.get(mimeType));
}
}
private static final class SubString implements CharSequence {
private final String s;
private final int offset;
private final int limit;
private SubString(String s, int offset, int limit) {
this.s = s;
this.offset = offset;
this.limit = limit;
}
@Override
public int length() {
return limit - offset;
}
@Override
public char charAt(int index) {
return s.charAt(index + offset);
}
@Override
public CharSequence subSequence(int start, int end) {
return s.substring(start + offset, end - offset);
}
}
private static Map parseParameters(String paramString, int start, int end) {
if (start == end) {
return Collections.emptyMap();
}
Map parameters = new LinkedHashMap<>();
int index = start;
while (index < end) {
index = parseNextParameter(paramString, index, end, parameters);
index = skipStartingWhitespace(paramString, index, end);
}
return parameters;
}
private static int parseNextParameter(String paramString, int start, int end, Map parameters) {
boolean quote = false;
boolean backslash = false;
int nameEnd = getNameEnd(paramString, start, end);
String name = paramString.substring(start, nameEnd);
int valueStart = nameEnd;
if (valueStart < end && paramString.charAt(valueStart) == '=') {
valueStart++;
}
StringBuilder value = new StringBuilder(end - valueStart);
for (int i = valueStart; i < end; i++) {
char c = paramString.charAt(i);
switch (c) {
case '"':
if (backslash) {
backslash = false;
value.append(c);
} else {
quote = !quote;
}
break;
case '\\':
if (backslash) {
backslash = false;
value.append(c);
} else {
backslash = true;
}
break;
case ';':
if (!quote) {
parameters.put(name, value.toString());
return i + 1;
}
value.append(c);
break;
default:
value.append(c);
break;
}
}
parameters.put(name, value.toString());
return end;
}
private static int getNameEnd(String params, int start, int end) {
int indexOfEquals = params.indexOf('=', start);
int indexOfSemicolon = params.indexOf(';', start);
if (isNotFound(indexOfEquals, end) && isNotFound(indexOfSemicolon, end)) {
return end;
}
if (isNotFound(indexOfEquals, end)) {
return indexOfSemicolon;
}
if (isNotFound(indexOfSemicolon, end)) {
return indexOfEquals;
}
return Math.min(indexOfEquals, indexOfSemicolon);
}
private static boolean isNotFound(int index, int end) {
return index == -1 || index >= end;
}
private static int skipStartingWhitespace(String s, int index, int end) {
int i = index;
while (i < end && Character.isWhitespace(s.charAt(i))) {
i++;
}
return i;
}
String getMimeType() {
return mimeType;
}
Map getParameters() {
return parameters;
}
String getCharset() {
return parameterLookup.get("charset"); //$NON-NLS-1$
}
@Override
public String toString() {
return mediaType;
}
}
data-url-data-url-1.0.1/src/main/java/com/github/robtimus/net/protocol/data/package-info.java 0000664 0000000 0000000 00000001434 13414154036 0032023 0 ustar 00root root 0000000 0000000 /*
* package-info.java
* Copyright 2017 Rob Spoor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Classes to support the {@code data:} protocol as specified in RFC 2397.
*/
package com.github.robtimus.net.protocol.data;
data-url-data-url-1.0.1/src/main/resources/ 0000775 0000000 0000000 00000000000 13414154036 0020457 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/resources/com/ 0000775 0000000 0000000 00000000000 13414154036 0021235 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/resources/com/github/ 0000775 0000000 0000000 00000000000 13414154036 0022517 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/resources/com/github/robtimus/ 0000775 0000000 0000000 00000000000 13414154036 0024363 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/resources/com/github/robtimus/net/ 0000775 0000000 0000000 00000000000 13414154036 0025151 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/resources/com/github/robtimus/net/protocol/ 0000775 0000000 0000000 00000000000 13414154036 0027012 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/resources/com/github/robtimus/net/protocol/data/ 0000775 0000000 0000000 00000000000 13414154036 0027723 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/main/resources/com/github/robtimus/net/protocol/data/data-url.properties0000664 0000000 0000000 00000000431 13414154036 0033550 0 ustar 00root root 0000000 0000000 dataURLConnection.getInputStream.falseDoInput=Cannot read from URLConnection if doInput=false (call setDoInput(true))
handler.invalidProtocol=The URL protocol must be '%s' but is '%s'
handler.missingComma=No ',' found in data URL %s
mediaType.invalidMimeType=Invalid mime type: %s
data-url-data-url-1.0.1/src/site/ 0000775 0000000 0000000 00000000000 13414154036 0016465 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/site/markdown/ 0000775 0000000 0000000 00000000000 13414154036 0020307 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/site/markdown/download.md 0000664 0000000 0000000 00000000447 13414154036 0022445 0 ustar 00root root 0000000 0000000
Download
## Download from Git
See [Source Code Management](source-repository.html) for more information.
## Download binaries / sources
Download the latest binary or source bundle from the [GitHub release page](https://github.com/robtimus/data-url/releases).
data-url-data-url-1.0.1/src/site/markdown/index.header.md 0000664 0000000 0000000 00000000052 13414154036 0023164 0 ustar 00root root 0000000 0000000
Overview
data-url-data-url-1.0.1/src/site/site.xml 0000664 0000000 0000000 00000002765 13414154036 0020165 0 ustar 00root root 0000000 0000000
org.apache.maven.skinsmaven-fluido-skin1.6true
data-url-data-url-1.0.1/src/test/ 0000775 0000000 0000000 00000000000 13414154036 0016500 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/test/java/ 0000775 0000000 0000000 00000000000 13414154036 0017421 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/test/java/com/ 0000775 0000000 0000000 00000000000 13414154036 0020177 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/test/java/com/github/ 0000775 0000000 0000000 00000000000 13414154036 0021461 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/test/java/com/github/robtimus/ 0000775 0000000 0000000 00000000000 13414154036 0023325 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/test/java/com/github/robtimus/net/ 0000775 0000000 0000000 00000000000 13414154036 0024113 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/test/java/com/github/robtimus/net/protocol/ 0000775 0000000 0000000 00000000000 13414154036 0025754 5 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/test/java/com/github/robtimus/net/protocol/data/ 0000775 0000000 0000000 00000000000 13414154036 0026665 5 ustar 00root root 0000000 0000000 DataURLConnectionTest.java 0000664 0000000 0000000 00000011663 13414154036 0033574 0 ustar 00root root 0000000 0000000 data-url-data-url-1.0.1/src/test/java/com/github/robtimus/net/protocol/data /*
* DataURLConnectionTest.java
* Copyright 2017 Rob Spoor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.robtimus.net.protocol.data;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Random;
import org.junit.BeforeClass;
import org.junit.Test;
@SuppressWarnings({ "nls", "javadoc" })
public class DataURLConnectionTest {
private static URL testURL;
@BeforeClass
public static void initTestURL() throws MalformedURLException {
testURL = new URL(null, "data:,", new Handler());
}
@Test(expected = IllegalStateException.class)
public void testConnect() {
DataURLConnection connection = new DataURLConnection(testURL, MediaType.DEFAULT, new byte[0]);
connection.connect();
// can't call setDoInput on connected URLConnections
connection.setDoInput(true);
}
@Test
public void testGetContentLength() {
int length = 1024;
DataURLConnection connection = new DataURLConnection(testURL, MediaType.DEFAULT, new byte[length]);
assertEquals(length, connection.getContentLength());
assertEquals(length, connection.getContentLengthLong());
}
@Test
public void testGetContentType() {
String contentType = "application/json; charset=UTF-8";
DataURLConnection connection = new DataURLConnection(testURL, MediaType.parse(contentType), new byte[0]);
assertEquals(contentType, connection.getContentType());
}
@Test
public void testGetContentTypeDefaultValue() {
DataURLConnection connection = new Handler().openConnection(testURL);
assertEquals(MediaType.DEFAULT.toString(), connection.getContentType());
}
@Test
public void testGetContentEncoding() {
String contentType = "application/json; charset=UTF-8";
DataURLConnection connection = new DataURLConnection(testURL, MediaType.parse(contentType), new byte[0]);
assertEquals("UTF-8", connection.getContentEncoding());
}
@Test
public void testGetContentEncodingNotSet() {
String contentType = "application/json";
DataURLConnection connection = new DataURLConnection(testURL, MediaType.parse(contentType), new byte[0]);
assertNull(connection.getContentEncoding());
}
@Test
public void testGetContentEncodingDefaultValue() {
DataURLConnection connection = new Handler().openConnection(testURL);
assertEquals("US-ASCII", connection.getContentEncoding());
}
@Test
public void testGetInputStream() throws IOException {
int length = 1024;
byte[] data = new byte[length];
new Random().nextBytes(data);
DataURLConnection connection = new DataURLConnection(testURL, MediaType.DEFAULT, data);
assertArrayEquals(data, readData(connection));
}
@Test(expected = ProtocolException.class)
public void testGetInputStreamNoDoInput() throws IOException {
int length = 1024;
byte[] data = new byte[length];
new Random().nextBytes(data);
DataURLConnection connection = new DataURLConnection(testURL, MediaType.DEFAULT, data);
connection.setDoInput(false);
connection.getInputStream();
}
@Test
public void testGetInputStreamTwice() throws IOException {
int length = 1024;
byte[] data = new byte[length];
new Random().nextBytes(data);
DataURLConnection connection = new DataURLConnection(testURL, MediaType.DEFAULT, data);
try (InputStream first = connection.getInputStream();
InputStream second = connection.getInputStream()) {
assertSame(first, second);
}
}
private byte[] readData(DataURLConnection connection) throws IOException {
try (InputStream input = connection.getInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
output.flush();
return output.toByteArray();
}
}
}
data-url-data-url-1.0.1/src/test/java/com/github/robtimus/net/protocol/data/DataURLsTest.java 0000664 0000000 0000000 00000041417 13414154036 0032016 0 ustar 00root root 0000000 0000000 /*
* DataURLsTest.java
* Copyright 2017 Rob Spoor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.robtimus.net.protocol.data;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Random;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import com.github.robtimus.net.protocol.data.DataURLs.Base64Appender;
@SuppressWarnings({ "nls", "javadoc" })
public class DataURLsTest {
@Rule public ExpectedException expectedException = ExpectedException.none();
@Test
public void testCreateNoDataProtocol() throws MalformedURLException {
String spec = "http://www.google.com/";
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
DataURLs.create(spec);
}
@Test
public void testCreateNoCommaPresent() throws MalformedURLException {
String path = "hello+world";
String spec = "data:" + path;
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
DataURLs.create(spec);
}
@Test
public void testCreateInvalidCharset() throws MalformedURLException {
String path = "text/plain;charset=something+invalid,hello+world";
String spec = "data:" + path;
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
DataURLs.create(spec);
}
@Test
public void testCreateNoMediaType() throws MalformedURLException {
String path = ",hello+world";
String spec = "data:" + path;
URL url = DataURLs.create(spec);
assertURL(url, path);
}
@Test
public void testCreateBase64NoMediaType() throws MalformedURLException {
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = ";base64," + Base64.getEncoder().encodeToString(bytes);
String spec = "data:" + path;
URL url = DataURLs.create(spec);
assertURL(url, path);
}
@Test
public void testCreateInvalidBase64NoMediaType() throws MalformedURLException {
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = ";base64," + Base64.getEncoder().encodeToString(bytes) + "%";
String spec = "data:" + path;
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
DataURLs.create(spec);
}
@Test
public void testCreateMediaTypeNoParameters() throws MalformedURLException {
String path = "text/plain,hello+world";
String spec = "data:" + path;
URL url = DataURLs.create(spec);
assertURL(url, path);
}
@Test
public void testCreateBase64MediaTypeNoParameters() throws MalformedURLException {
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octet-stream;base64," + Base64.getEncoder().encodeToString(bytes);
String spec = "data:" + path;
URL url = DataURLs.create(spec);
assertURL(url, path);
}
@Test
public void testCreateInvalidBase64MediaTypeNoParameters() throws MalformedURLException {
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octect-stream;base64," + Base64.getEncoder().encodeToString(bytes) + "%";
String spec = "data:" + path;
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
DataURLs.create(spec);
}
@Test
public void testCreateMediaTypeWithParameters() throws MalformedURLException {
String path = "text/plain;charset=UTF-8,hello+world";
String spec = "data:" + path;
URL url = DataURLs.create(spec);
assertURL(url, path);
}
@Test
public void testCreateBase64MediaTypeWithParameters() throws MalformedURLException {
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octet-stream;charset=UTF-8;base64," + Base64.getEncoder().encodeToString(bytes);
String spec = "data:" + path;
URL url = DataURLs.create(spec);
assertURL(url, path);
}
@Test
public void testCreateInvalidBase64MediaTypeWithParameters() throws MalformedURLException {
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octect-stream;charset=UTF-8;base64," + Base64.getEncoder().encodeToString(bytes) + "%";
String spec = "data:" + path;
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
DataURLs.create(spec);
}
@Test
public void testBuilderFromStringBare() {
String data = "hello+world";
String path = "," + data;
URL url = DataURLs.builder(data)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromStringWithMediaType() {
String mediaType = "application/json";
String data = "hello+world";
String path = mediaType + "," + data;
URL url = DataURLs.builder(data)
.withMediaType(mediaType)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromStringWithMediaTypeAndParams() {
String mediaType = "application/json;charset=UTF-8";
String paramName = "last-modified";
String paramValue = "0";
String data = "hello+world";
String path = mediaType + ";" + paramName + "=" + paramValue + "," + data;
URL url = DataURLs.builder(data)
.withMediaType(mediaType)
.withMediaTypeParameter(paramName, paramValue)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromReaderBare() {
String data = "hello+world";
String path = "," + data;
URL url = DataURLs.builder(new StringReader(data))
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromReaderWithMediaType() {
String mediaType = "application/json";
String data = "hello+world";
String path = mediaType + "," + data;
URL url = DataURLs.builder(new StringReader(data))
.withMediaType(mediaType)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromReaderWithMediaTypeAndParams() {
String mediaType = "application/json;charset=UTF-8";
String paramName = "last-modified";
String paramValue = "0";
String data = "hello+world";
String path = mediaType + ";" + paramName + "=" + paramValue + "," + data;
URL url = DataURLs.builder(new StringReader(data))
.withMediaType(mediaType)
.withMediaTypeParameter(paramName, paramValue)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromBytesBase64Bare() {
byte[] data = new byte[1024];
new Random().nextBytes(data);
String path = ";base64," + Base64.getEncoder().encodeToString(data);
URL url = DataURLs.builder(data)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromBytesBase64WithMediaType() {
String mediaType = "application/json";
byte[] data = new byte[1024];
new Random().nextBytes(data);
String path = mediaType + ";base64," + Base64.getEncoder().encodeToString(data);
URL url = DataURLs.builder(data)
.withMediaType(mediaType)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromBytesBase64WithMediaTypeAndParams() {
String mediaType = "application/json;charset=UTF-8";
String paramName = "last-modified";
String paramValue = "0";
byte[] data = new byte[1024];
new Random().nextBytes(data);
String path = mediaType + ";" + paramName + "=" + paramValue + ";base64," + Base64.getEncoder().encodeToString(data);
URL url = DataURLs.builder(data)
.withMediaType(mediaType)
.withMediaTypeParameter(paramName, paramValue)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromBytesNoBase64Bare() {
String data = "hello+world";
String path = "," + data;
URL url = DataURLs.builder(data.getBytes(StandardCharsets.US_ASCII))
.withBase64Data(false)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromBytesNoBase64WithMediaType() {
String mediaType = "application/json";
String data = "hello+world";
String path = mediaType + "," + data;
URL url = DataURLs.builder(data.getBytes(StandardCharsets.US_ASCII))
.withBase64Data(false)
.withMediaType(mediaType)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromBytesNoBase64WithMediaTypeAndParams() {
String mediaType = "application/json;charset=UTF-8";
String paramName = "last-modified";
String paramValue = "0";
String data = "hello+world";
String path = mediaType + ";" + paramName + "=" + paramValue + "," + data;
URL url = DataURLs.builder(data.getBytes(StandardCharsets.US_ASCII))
.withBase64Data(false)
.withMediaType(mediaType)
.withMediaTypeParameter(paramName, paramValue)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromStreamBase64Bare() {
byte[] data = new byte[1024];
new Random().nextBytes(data);
String path = ";base64," + Base64.getEncoder().encodeToString(data);
URL url = DataURLs.builder(new ByteArrayInputStream(data))
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromStreamBase64WithMediaType() {
String mediaType = "application/json";
byte[] data = new byte[1024];
new Random().nextBytes(data);
String path = mediaType + ";base64," + Base64.getEncoder().encodeToString(data);
URL url = DataURLs.builder(new ByteArrayInputStream(data))
.withMediaType(mediaType)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromStreamBase64WithMediaTypeAndParams() {
String mediaType = "application/json;charset=UTF-8";
String paramName = "last-modified";
String paramValue = "0";
byte[] data = new byte[1024];
new Random().nextBytes(data);
String path = mediaType + ";" + paramName + "=" + paramValue + ";base64," + Base64.getEncoder().encodeToString(data);
URL url = DataURLs.builder(new ByteArrayInputStream(data))
.withMediaType(mediaType)
.withMediaTypeParameter(paramName, paramValue)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromStreamNoBase64Bare() {
String data = "hello+world";
String path = "," + data;
URL url = DataURLs.builder(new ByteArrayInputStream(data.getBytes(StandardCharsets.US_ASCII)))
.withBase64Data(false)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromStreamNoBase64WithMediaType() {
String mediaType = "application/json";
String data = "hello+world";
String path = mediaType + "," + data;
URL url = DataURLs.builder(new ByteArrayInputStream(data.getBytes(StandardCharsets.US_ASCII)))
.withBase64Data(false)
.withMediaType(mediaType)
.build();
assertURL(url, path);
}
@Test
public void testBuilderFromStreamNoBase64WithMediaTypeAndParams() {
String mediaType = "application/json;charset=UTF-8";
String paramName = "last-modified";
String paramValue = "0";
String data = "hello+world";
String path = mediaType + ";" + paramName + "=" + paramValue + "," + data;
URL url = DataURLs.builder(new ByteArrayInputStream(data.getBytes(StandardCharsets.US_ASCII)))
.withBase64Data(false)
.withMediaType(mediaType)
.withMediaTypeParameter(paramName, paramValue)
.build();
assertURL(url, path);
}
@Test
public void testBuilderWithMediaTypeWithNullMediaTypeParameter() {
String bareMediaType = "application/json";
String mediaType = bareMediaType + ";charset=UTF-8";
String paramName = "last-modified";
String paramValue = "0";
String data = "hello+world";
String path = bareMediaType + ";" + paramName + "=" + paramValue + "," + data;
URL url = DataURLs.builder(data)
.withMediaType(mediaType)
.withMediaTypeParameter(paramName, paramValue)
.withCharset(StandardCharsets.US_ASCII)
.withMediaTypeParameter("charset", null)
.build();
assertURL(url, path);
}
@Test
public void testBuilderWithMediaTypeWithCharset() {
String mediaType = "application/json;charset=UTF-8";
String paramName = "last-modified";
String paramValue = "0";
String data = "hello+world";
String path = mediaType.replace("UTF-8", "US-ASCII") + ";" + paramName + "=" + paramValue + "," + data;
URL url = DataURLs.builder(data)
.withMediaType(mediaType)
.withMediaTypeParameter(paramName, paramValue)
.withCharset(StandardCharsets.US_ASCII)
.build();
assertURL(url, path);
}
private void assertURL(URL url, String path) {
assertEquals(Handler.PROTOCOL, url.getProtocol());
assertEquals(path, url.getPath());
assertEquals(path, url.getFile());
assertNull(url.getQuery());
assertNull(url.getUserInfo());
assertNull(url.getAuthority());
assertEquals(-1, url.getPort());
assertEquals(-1, url.getDefaultPort());
assertNull(url.getHost());
assertNull(url.getRef());
}
@Test
public void testBase64AppenderWriteByte() throws IOException {
StringBuilder sb = new StringBuilder();
StringBuilder expected = new StringBuilder();
try (OutputStream appender = new Base64Appender(sb)) {
for (byte b = 'A'; b <= 'Z'; b++) {
appender.write(b);
expected.append((char) b);
}
}
assertEquals(expected.toString(), sb.toString());
}
@Test
public void testBase64AppenderWriteArray() throws IOException {
StringBuilder sb = new StringBuilder();
StringBuilder expected = new StringBuilder();
try (OutputStream appender = new Base64Appender(sb)) {
// equal buffer sizes
byte[] buffer = new byte[1024];
fill(buffer, expected);
appender.write(buffer);
// smaller buffer size
buffer = new byte[512];
fill(buffer, expected);
appender.write(buffer);
// larger buffer size
buffer = new byte[5632];
fill(buffer, expected);
appender.write(buffer);
}
assertEquals(expected.toString(), sb.toString());
}
private static final Random RANDOM = new Random();
private void fill(byte[] buffer, StringBuilder expected) {
for (int i = 0; i < buffer.length; i++) {
int c = RANDOM.nextInt(26) + 'A';
buffer[i] = (byte) c;
expected.append((char) c);
}
}
}
data-url-data-url-1.0.1/src/test/java/com/github/robtimus/net/protocol/data/HandlerTest.java 0000664 0000000 0000000 00000045160 13414154036 0031753 0 ustar 00root root 0000000 0000000 /*
* HandlerTest.java
* Copyright 2017 Rob Spoor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.robtimus.net.protocol.data;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Base64;
import java.util.Random;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.rules.ExpectedException;
@SuppressWarnings({ "nls", "javadoc" })
public class HandlerTest {
@Rule public ExpectedException expectedException = ExpectedException.none();
@Rule public RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();
@Test
public void testRegistration() throws MalformedURLException {
String packages = System.getProperty("java.protocol.handler.pkgs");
packages = (packages == null ? "" : packages + "|") + "com.github.robtimus.net.protocol";
System.setProperty("java.protocol.handler.pkgs", packages);
String path = "text/plain,hello+world";
String spec = "data:" + path;
URL url = new URL(spec);
assertURL(url, path);
}
@Test
public void testDataURLWithDataURLContext() throws MalformedURLException {
String packages = System.getProperty("java.protocol.handler.pkgs");
packages = (packages == null ? "" : packages + "|") + "com.github.robtimus.net.protocol";
System.setProperty("java.protocol.handler.pkgs", packages);
URL context = new URL("data:,hello+world");
URL url = new URL(context, "data:,foo+bar");
assertURL(context, ",hello+world");
assertURL(url, ",foo+bar");
}
@Test
public void testDataURLWithHttpURLContext() throws MalformedURLException {
String packages = System.getProperty("java.protocol.handler.pkgs");
packages = (packages == null ? "" : packages + "|") + "com.github.robtimus.net.protocol";
System.setProperty("java.protocol.handler.pkgs", packages);
URL context = new URL("http://www.google.com/");
URL url = new URL(context, "data:,foo+bar");
assertURL(url, ",foo+bar");
}
@Test
public void testHttpURLWithDataURLContext() throws MalformedURLException {
String packages = System.getProperty("java.protocol.handler.pkgs");
packages = (packages == null ? "" : packages + "|") + "com.github.robtimus.net.protocol";
System.setProperty("java.protocol.handler.pkgs", packages);
URL context = new URL("data:,hello+world");
URL url = new URL(context, "http://www.google.com/");
assertURL(context, ",hello+world");
assertEquals("http", url.getProtocol());
assertEquals("www.google.com", url.getHost());
assertEquals(-1, url.getPort());
assertEquals("/", url.getPath());
}
@Test
public void testOpenConnectionNoDataProtocol() throws MalformedURLException {
Handler handler = spy(new Handler());
URL url = new URL("http://www.google.com/");
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(Messages.handler.invalidProtocol.get(Handler.PROTOCOL, url.getProtocol()));
handler.openConnection(url);
}
@Test
public void testOpenConnectionNoCommaPresent() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = "hello+world";
URL url = createDataURL(path);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(Messages.handler.missingComma.get("data:" + path));
handler.openConnection(url);
}
@Test
public void testOpenConnectionInvalidCharset() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = "text/plain;charset=something+invalid,hello+world";
URL url = createDataURL(path);
expectedException.expect(UnsupportedCharsetException.class);
expectedException.expectMessage("something+invalid");
handler.openConnection(url);
}
@Test
public void testOpenConnectionNoMediaType() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = ",hello+world";
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
DataURLConnection connection = handler.openConnection(url);
assertSame(url, connection.getURL());
}
@Test
public void testOpenConnectionBase64NoMediaType() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = ";base64," + Base64.getEncoder().encodeToString(bytes);
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
DataURLConnection connection = handler.openConnection(url);
assertSame(url, connection.getURL());
}
@Test
public void testOpenConnectionInvalidBase64NoMediaType() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = ";base64," + Base64.getEncoder().encodeToString(bytes) + "%";
URL url = createDataURL(path);
expectedException.expect(IllegalArgumentException.class);
handler.openConnection(url);
}
@Test
public void testOpenConnectionMediaTypeNoParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = "text/plain,hello+world";
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
DataURLConnection connection = handler.openConnection(url);
assertSame(url, connection.getURL());
}
@Test
public void testOpenConnectionBase64MediaTypeNoParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octet-stream;base64," + Base64.getEncoder().encodeToString(bytes);
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
DataURLConnection connection = handler.openConnection(url);
assertSame(url, connection.getURL());
}
@Test
public void testOpenConnectionInvalidBase64MediaTypeNoParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octect-stream;base64," + Base64.getEncoder().encodeToString(bytes) + "%";
URL url = createDataURL(path);
expectedException.expect(IllegalArgumentException.class);
handler.openConnection(url);
}
@Test
public void testOpenConnectionMediaTypeWithParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = "text/plain;charset=UTF-8,hello+world";
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
DataURLConnection connection = handler.openConnection(url);
assertSame(url, connection.getURL());
}
@Test
public void testOpenConnectionBase64MediaTypeWithParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octet-stream;charset=UTF-8;base64," + Base64.getEncoder().encodeToString(bytes);
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
DataURLConnection connection = handler.openConnection(url);
assertSame(url, connection.getURL());
}
@Test
public void testOpenConnectionInvalidBase64MediaTypeWithParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octect-stream;charset=UTF-8;base64," + Base64.getEncoder().encodeToString(bytes) + "%";
URL url = createDataURL(path);
expectedException.expect(IllegalArgumentException.class);
handler.openConnection(url);
}
@Test
public void testOpenConnectionWithProxy() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = ",hello+world";
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
DataURLConnection connection = handler.openConnection(url, Proxy.NO_PROXY);
assertSame(url, connection.getURL());
verify(handler).openConnection(url, Proxy.NO_PROXY);
verify(handler).openConnection(url);
}
@SuppressWarnings("unused")
@Test
public void testParseURLNoDataProtocol() throws MalformedURLException {
Handler handler = spy(new Handler());
String spec = "http://www.google.com/";
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
try {
new URL(null, spec, handler);
} catch (MalformedURLException e) {
verify(handler).parseURL(any(URL.class), eq(spec), eq(5), eq(spec.length()));
throw e;
}
}
@SuppressWarnings("unused")
@Test
public void testParseURLCommaPresentInAnchor() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = "hello+world";
String spec = "data:" + path;
String specWithAnchor = spec + "#anchor,";
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
try {
new URL(null, specWithAnchor, handler);
} catch (MalformedURLException e) {
verify(handler).parseURL(any(URL.class), eq(specWithAnchor), eq(5), eq(spec.length()));
throw e;
}
}
@SuppressWarnings("unused")
@Test
public void testParseURLNoCommaPresent() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = "hello+world";
String spec = "data:" + path;
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
try {
new URL(null, spec, handler);
} catch (MalformedURLException e) {
verify(handler).parseURL(any(URL.class), eq(spec), eq(5), eq(spec.length()));
throw e;
}
}
@SuppressWarnings("unused")
@Test
public void testParseURLInvalidCharset() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = "text/plain;charset=something+invalid,hello+world";
String spec = "data:" + path;
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
try {
new URL(null, spec, handler);
} catch (MalformedURLException e) {
verify(handler).parseURL(any(URL.class), eq(spec), eq(5), eq(spec.length()));
throw e;
}
}
@Test
public void testParseURLNoMediaTypeWithAnchor() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = ",hello+world";
String spec = "data:" + path;
String specWithAnchor = spec + "#anchor";
URL url = new URL(null, specWithAnchor, handler);
assertURL(url, path);
verify(handler).parseURL(url, specWithAnchor, 5, spec.length());
}
@Test
public void testParseURLNoMediaType() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = ",hello+world";
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
assertURL(url, path);
verify(handler).parseURL(url, spec, 5, spec.length());
}
@Test
public void testParseURLBase64NoMediaType() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = ";base64," + Base64.getEncoder().encodeToString(bytes);
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
assertURL(url, path);
verify(handler).parseURL(url, spec, 5, spec.length());
}
@SuppressWarnings("unused")
@Test
public void testParseURLInvalidBase64NoMediaType() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = ";base64," + Base64.getEncoder().encodeToString(bytes) + "%";
String spec = "data:" + path;
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
try {
new URL(null, spec, handler);
} catch (MalformedURLException e) {
verify(handler).parseURL(any(URL.class), eq(spec), eq(5), eq(spec.length()));
throw e;
}
}
@Test
public void testParseURLMediaTypeNoParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = "text/plain,hello+world";
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
assertURL(url, path);
verify(handler).parseURL(url, spec, 5, spec.length());
}
@Test
public void testParseURLBase64MediaTypeNoParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octet-stream;base64," + Base64.getEncoder().encodeToString(bytes);
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
assertURL(url, path);
verify(handler).parseURL(url, spec, 5, spec.length());
}
@SuppressWarnings("unused")
@Test
public void testParseURLInvalidBase64MediaTypeNoParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octect-stream;base64," + Base64.getEncoder().encodeToString(bytes) + "%";
String spec = "data:" + path;
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
try {
new URL(null, spec, handler);
} catch (MalformedURLException e) {
verify(handler).parseURL(any(URL.class), eq(spec), eq(5), eq(spec.length()));
throw e;
}
}
@Test
public void testParseURLMediaTypeWithParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
String path = "text/plain;charset=UTF-8,hello+world";
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
assertURL(url, path);
verify(handler).parseURL(url, spec, 5, spec.length());
}
@Test
public void testParseURLBase64MediaTypeWithParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octet-stream;charset=UTF-8;base64," + Base64.getEncoder().encodeToString(bytes);
String spec = "data:" + path;
URL url = new URL(null, spec, handler);
assertURL(url, path);
verify(handler).parseURL(url, spec, 5, spec.length());
}
@SuppressWarnings("unused")
@Test
public void testParseURLInvalidBase64MediaTypeWithParameters() throws MalformedURLException {
Handler handler = spy(new Handler());
byte[] bytes = new byte[1024];
new Random().nextBytes(bytes);
String path = "application/octect-stream;charset=UTF-8;base64," + Base64.getEncoder().encodeToString(bytes) + "%";
String spec = "data:" + path;
expectedException.expect(MalformedURLException.class);
expectedException.expectCause(instanceOf(IllegalArgumentException.class));
try {
new URL(null, spec, handler);
} catch (MalformedURLException e) {
verify(handler).parseURL(any(URL.class), eq(spec), eq(5), eq(spec.length()));
throw e;
}
}
@Test
public void testGetCharsetNoMediaType() {
assertEquals(StandardCharsets.US_ASCII, Handler.getCharset(null));
}
@Test
public void testGetCharsetMediaTypeNoCharset() {
MediaType mediaType = MediaType.parse("application/json");
assertEquals(StandardCharsets.US_ASCII, Handler.getCharset(mediaType));
}
@Test
public void testGetCharsetMediaTypeWithCharset() {
MediaType mediaType = MediaType.parse("application/json;charset=UTF-8");
assertEquals(StandardCharsets.UTF_8, Handler.getCharset(mediaType));
}
private void assertURL(URL url, String path) {
assertEquals(Handler.PROTOCOL, url.getProtocol());
assertEquals(path, url.getPath());
assertEquals(path, url.getFile());
assertNull(url.getQuery());
assertNull(url.getUserInfo());
assertNull(url.getAuthority());
assertEquals(-1, url.getPort());
assertEquals(-1, url.getDefaultPort());
assertNull(url.getHost());
assertNull(url.getRef());
}
private URL createDataURL(String path) throws MalformedURLException {
return new URL(Handler.PROTOCOL, null, -1, path, new Handler());
}
}
data-url-data-url-1.0.1/src/test/java/com/github/robtimus/net/protocol/data/MediaTypeTest.java 0000664 0000000 0000000 00000025251 13414154036 0032256 0 ustar 00root root 0000000 0000000 /*
* MediaTypeTest.java
* Copyright 2017 Rob Spoor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.robtimus.net.protocol.data;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@SuppressWarnings({ "nls", "javadoc" })
public class MediaTypeTest {
@Rule public ExpectedException expectedException = ExpectedException.none();
@Test
public void testConstructNoParameters() {
String type = "application/json";
MediaType mediaType = MediaType.create(type, Collections.emptyMap());
assertEquals(type, mediaType.toString());
assertEquals(type, mediaType.getMimeType());
assertEquals(Collections.emptyMap(), mediaType.getParameters());
assertNull(mediaType.getCharset());
}
@Test
public void testConstructWithOneParameters() {
String type = "application/json";
Map parameters = Collections.singletonMap("charset", "UTF-8");
MediaType mediaType = MediaType.create(type, parameters);
assertEquals("application/json;charset=UTF-8", mediaType.toString());
assertEquals(type, mediaType.getMimeType());
assertEquals(parameters, mediaType.getParameters());
assertEquals("UTF-8", mediaType.getCharset());
}
@Test
public void testConstructWithMultipleParameters() {
String type = "application/json";
Map parameters = new LinkedHashMap<>();
parameters.put("CHARSET", "UTF-8;");
parameters.put("last-modified", "\"\\0");
MediaType mediaType = MediaType.create(type, parameters);
assertEquals("application/json;CHARSET=\"UTF-8;\";last-modified=\\\"\\\\0", mediaType.toString());
assertEquals(type, mediaType.getMimeType());
assertEquals(parameters, mediaType.getParameters());
assertEquals("UTF-8;", mediaType.getCharset());
}
@Test
public void testConstructWithNullParameterValue() {
String type = "application/json";
Map parameters = new LinkedHashMap<>();
parameters.put("CHARSET", "UTF-8;");
parameters.put("last-modified", null);
MediaType mediaType = MediaType.create(type, parameters);
assertEquals("application/json;CHARSET=\"UTF-8;\";last-modified=", mediaType.toString());
assertEquals(type, mediaType.getMimeType());
assertEquals(parameters, mediaType.getParameters());
assertEquals("UTF-8;", mediaType.getCharset());
}
@Test
public void testConstructInvalidMimeTypeNoSubType() {
String type = "application";
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(Messages.mediaType.invalidMimeType.get(type));
MediaType.create(type, Collections.emptyMap());
}
@Test
public void testConstructInvalidMimeTypeInvalidTokenChar() {
String type = "application/json@";
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(Messages.mediaType.invalidMimeType.get(type));
MediaType.create(type, Collections.emptyMap());
}
@Test
public void testParseNoParameters() {
String type = "application/json";
MediaType mediaType = MediaType.parse(type);
assertEquals(type, mediaType.toString());
assertEquals(type, mediaType.getMimeType());
assertEquals(Collections.emptyMap(), mediaType.getParameters());
assertNull(mediaType.getCharset());
}
@Test
public void testParseStartOnly() {
String type = "application/json";
MediaType mediaType = MediaType.parse(type + "!", 0, type.length());
assertEquals(type, mediaType.toString());
assertEquals(type, mediaType.getMimeType());
assertEquals(Collections.emptyMap(), mediaType.getParameters());
assertNull(mediaType.getCharset());
}
@Test
public void testParseEndOnly() {
String type = "application/json";
MediaType mediaType = MediaType.parse("!" + type, 1, type.length() + 1);
assertEquals(type, mediaType.toString());
assertEquals(type, mediaType.getMimeType());
assertEquals(Collections.emptyMap(), mediaType.getParameters());
assertNull(mediaType.getCharset());
}
@Test
public void testParseMiddleOnly() {
String type = "application/json";
MediaType mediaType = MediaType.parse("!" + type + "!", 1, type.length() + 1);
assertEquals(type, mediaType.toString());
assertEquals(type, mediaType.getMimeType());
assertEquals(Collections.emptyMap(), mediaType.getParameters());
assertNull(mediaType.getCharset());
}
@Test
public void testParseEmptyParameters() {
String type = "application/json";
MediaType mediaType = MediaType.parse(type + ";");
assertEquals(type + ";", mediaType.toString());
assertEquals(type, mediaType.getMimeType());
assertEquals(Collections.emptyMap(), mediaType.getParameters());
assertNull(mediaType.getCharset());
}
@Test
public void testParseWithOneParameter() {
String type = "application/json; CHARSET=UTF-8";
MediaType mediaType = MediaType.parse(type);
assertEquals(type, mediaType.toString());
assertEquals("application/json", mediaType.getMimeType());
assertEquals(Collections.singletonMap("CHARSET", "UTF-8"), mediaType.getParameters());
assertEquals("UTF-8", mediaType.getCharset());
}
@Test
public void testParseWithMultipleParameter() {
String type = "application/json; CHARSET=UTF-8; last-modified=0";
MediaType mediaType = MediaType.parse(type);
Map parameters = new HashMap<>();
parameters.put("CHARSET", "UTF-8");
parameters.put("last-modified", "0");
assertEquals(type, mediaType.toString());
assertEquals("application/json", mediaType.getMimeType());
assertEquals(parameters, mediaType.getParameters());
assertEquals("UTF-8", mediaType.getCharset());
}
@Test
public void testParseWithEscapedAndQuotedParameter() {
String type = "application/json; CHARSET=\"UTF-8;\"; last-modified=\\\"\\\\0";
MediaType mediaType = MediaType.parse(type);
Map parameters = new HashMap<>();
parameters.put("CHARSET", "UTF-8;");
parameters.put("last-modified", "\"\\0");
assertEquals(type, mediaType.toString());
assertEquals("application/json", mediaType.getMimeType());
assertEquals(parameters, mediaType.getParameters());
assertEquals("UTF-8;", mediaType.getCharset());
}
@Test
public void testParseWithEmptyValueParameterInMiddle() {
String type = "application/json; dummy=; CHARSET=UTF-8";
MediaType mediaType = MediaType.parse(type);
Map parameters = new HashMap<>();
parameters.put("dummy", "");
parameters.put("CHARSET", "UTF-8");
assertEquals(type, mediaType.toString());
assertEquals("application/json", mediaType.getMimeType());
assertEquals(parameters, mediaType.getParameters());
assertEquals("UTF-8", mediaType.getCharset());
}
@Test
public void testParseWithEmptyValueParameterAtEnd() {
String type = "application/json; CHARSET=UTF-8; dummy=";
MediaType mediaType = MediaType.parse(type);
Map parameters = new HashMap<>();
parameters.put("dummy", "");
parameters.put("CHARSET", "UTF-8");
assertEquals(type, mediaType.toString());
assertEquals("application/json", mediaType.getMimeType());
assertEquals(parameters, mediaType.getParameters());
assertEquals("UTF-8", mediaType.getCharset());
}
@Test
public void testParseWithNoValueParameterInMiddle() {
String type = "application/json; dummy; CHARSET=UTF-8";
MediaType mediaType = MediaType.parse(type);
Map parameters = new HashMap<>();
parameters.put("dummy", "");
parameters.put("CHARSET", "UTF-8");
assertEquals(type, mediaType.toString());
assertEquals("application/json", mediaType.getMimeType());
assertEquals(parameters, mediaType.getParameters());
assertEquals("UTF-8", mediaType.getCharset());
}
@Test
public void testParseWithNoValueParameterAtEnd() {
String type = "application/json; CHARSET=UTF-8; dummy";
MediaType mediaType = MediaType.parse(type);
Map parameters = new HashMap<>();
parameters.put("dummy", "");
parameters.put("CHARSET", "UTF-8");
assertEquals(type, mediaType.toString());
assertEquals("application/json", mediaType.getMimeType());
assertEquals(parameters, mediaType.getParameters());
assertEquals("UTF-8", mediaType.getCharset());
}
@Test
public void testParseWithOnlyNoParametera() {
String type = "application/json; CHARSET; dummy";
MediaType mediaType = MediaType.parse(type);
Map parameters = new HashMap<>();
parameters.put("dummy", "");
parameters.put("CHARSET", "");
assertEquals(type, mediaType.toString());
assertEquals("application/json", mediaType.getMimeType());
assertEquals(parameters, mediaType.getParameters());
assertEquals("", mediaType.getCharset());
}
@Test
public void testParseInvalidMimeTypeNoSubType() {
String type = "application";
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(Messages.mediaType.invalidMimeType.get(type));
MediaType.parse(type);
}
@Test
public void testParseInvalidMimeTypeInvalidTokenChar() {
String type = "application/json@";
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(Messages.mediaType.invalidMimeType.get(type));
MediaType.parse(type);
}
}