Skip to content

Commit

Permalink
Enhancement: Adds binary data support for GET(). Activated by setting…
Browse files Browse the repository at this point in the history
… the content-type 'application/octet-stream' as second parameter.
  • Loading branch information
amorgner committed Apr 9, 2020
1 parent 4ab3911 commit 0390805
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ public Object apply(final ActionContext ctx, final Object caller, final Object[]
assertArrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 3);

final Charset charset = (sources.length == 3) ? Charset.forName(sources[2].toString()) : Charset.defaultCharset();
final byte[] input = sources[0].toString().getBytes(charset);

byte[] input;
if (sources[0] instanceof byte[]) {
input = (byte[]) sources[0];
} else {
input = sources[0].toString().getBytes(charset);
}

String encodingScheme = "basic";
if (sources.length >= 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ public Object unwrap(final Object source) {

return unwrap(((Wrapper)source).unwrap());

} else if (source instanceof byte[]) {

return source;

} else if (source.getClass().isArray()) {

final List list = new ArrayList();
Expand Down
23 changes: 23 additions & 0 deletions structr-rest/src/main/java/org/structr/rest/common/HttpHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public static String get(final String address, final String username, final Stri
return get(address, username, password, null, null, null, null, headers);
}

public static byte[] getBinary(final String address, final String username, final String password, final Map<String, String> headers)
throws FrameworkException {
return getBinary(address, username, password, null, null, null, null, headers);
}

public static String get(final String address, final String proxyUrl, final String proxyUsername, final String proxyPassword, final String cookie, final Map<String, String> headers)
throws FrameworkException {
return get(address, null, null, proxyUrl, proxyUsername, proxyPassword, cookie, headers);
Expand Down Expand Up @@ -202,6 +207,24 @@ public static String get(final String address, final String username, final Stri
return content;
}

public static byte[] getBinary(final String address, final String username, final String password, final String proxyUrl, final String proxyUsername, final String proxyPassword, final String cookie, final Map<String, String> headers)
throws FrameworkException {

try {

final URI url = URI.create(address);
final HttpGet req = new HttpGet(url);

configure(req, username, password, proxyUrl, proxyUsername, proxyPassword, cookie, headers, true);

return IOUtils.toByteArray(getAsStream(address, username, password, proxyUrl, proxyUsername, proxyPassword, cookie, headers));

} catch (final Throwable t) {
logger.error("Error while dowloading binary data from " + address, t);
throw new FrameworkException(422, "Unable to fetch binary data from address " + address + ": " + t.getMessage());
}
}

public static Map<String, String> head(final String address) {
return head(address, null, null, null, null, Collections.EMPTY_MAP);
}
Expand Down
13 changes: 12 additions & 1 deletion structr-ui/src/main/java/org/structr/web/entity/dom/Content.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import org.apache.commons.lang3.StringUtils;
import org.apache.tika.io.IOUtils;
import org.structr.api.schema.JsonObjectType;
import org.structr.api.schema.JsonSchema;
import org.structr.common.ConstantBooleanTrue;
Expand Down Expand Up @@ -744,7 +746,16 @@ public void handleScript(final String script) throws FrameworkException, IOExcep
final Object value = Scripting.evaluate(renderContext, node, script, "script source");
if (value != null) {

final String content = value.toString();
String content = null;

// Convert binary data to String with charset from response
if (value instanceof byte[]) {
//StringUtils.toEncodedString((byte[]) value, renderContext.getPage().getProperty(StructrApp.key(Page.class, "contentType")));
content = StringUtils.toEncodedString((byte[]) value, Charset.forName(renderContext.getResponse().getCharacterEncoding()));
} else {
content = value.toString();
}

if (StringUtils.isNotBlank(content)) {

renderContext.getBuffer().append(transform(content));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public Object apply(final ActionContext ctx, final Object caller, final Object[]

return doc.html();
}

} else if ("application/octet-stream".equals(contentType)) {

return getBinaryFromUrl(ctx, address, username, password);

} else {

return getFromUrl(ctx, address, username, password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,32 @@ public Object apply(final ActionContext ctx, final Object caller, final Object[]
if (sources[0] instanceof File) {

final File file = (File)sources[0];
final String content = (String)sources[1];
final String encoding = (sources.length == 3 && sources[2] != null) ? sources[2].toString() : "UTF-8";

if (sources[1] instanceof byte[]) {

try (final FileOutputStream fos = file.getOutputStream(true, false)) {

try (final FileOutputStream fos = file.getOutputStream(true, false)) {
fos.write((byte[]) sources[1]);

fos.write(content.getBytes(encoding));
} catch (IOException ioex) {
logger.warn("set_content(): Unable to write binary data to file '{}'", file.getPath(), ioex);
}

} catch (IOException ioex) {
logger.warn("set_content(): Unable to write to file '{}'", file.getPath(), ioex);
} else {

final String content = (String)sources[1];

try (final FileOutputStream fos = file.getOutputStream(true, false)) {

fos.write(content.getBytes(encoding));

} catch (IOException ioex) {
logger.warn("set_content(): Unable to write content to file '{}'", file.getPath(), ioex);
}
}


}

} catch (ArgumentNullException pe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ protected String getFromUrl(final ActionContext ctx, final String requestUrl, fi
return HttpHelper.get(requestUrl, username, password, ctx.getHeaders());
}

protected byte[] getBinaryFromUrl(final ActionContext ctx, final String requestUrl, final String username, final String password) throws IOException, FrameworkException {

return HttpHelper.getBinary(requestUrl, username, password, ctx.getHeaders());
}

protected GraphObjectMap headFromUrl(final ActionContext ctx, final String requestUrl, final String username, final String password) throws IOException, FrameworkException {

final Map<String, String> headers = HttpHelper.head(requestUrl, password, username, ctx.getHeaders());
Expand Down

0 comments on commit 0390805

Please sign in to comment.