Commit d7201736 authored by Miro Eklund's avatar Miro Eklund Committed by Tuukka Lehtonen
Browse files

Allow SCL functions to use chunked HTTP send requests

Java-side implementation of complex chunked target. Also added properties
for Client implementation for further configurability.

closes #724
parent 9f4f62a0
......@@ -91,9 +91,18 @@ importJava "javax.ws.rs.client.Entity" where
importJava "javax.ws.rs.client.Client" where
data Client
property :: Client -> String -> a -> <Proc>Dynamic
target :: Client -> String -> <Proc> WebTarget
close :: Client -> <Proc> ()
importJava "org.simantics.scl.rest.ChunkedRequests" where
chunkedPost :: Client -> String -> File -> Integer -> <Proc, Exception>Response
chunkedPut :: Client -> String -> File -> Integer -> <Proc, Exception>Response
// Response chunkedPostAuth(Client httpClient, final String url, final File f, final int chunk_size, String authHeaderField) throws Exception
chunkedPostAuth :: Client -> String -> File -> Integer -> String -> <Proc, Exception>Response
// Response chunkedPuttAuth(Client httpClient, final String url, final File f, final int chunk_size, String authHeaderField) throws Exception
chunkedPutAuth :: Client -> String -> File -> Integer -> String -> <Proc, Exception>Response
/*
Example usage:
......
package org.simantics.scl.rest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class ChunkedRequests {
public static Response chunkedPost(Client httpClient, final String url, final File f, final int chunk_size) throws Exception {
InputStream fileInStream = new FileInputStream(f);
String contentDisposition = "attachment; filename=\"" + f.getName() + "\"";
WebTarget webTarget = prepare(httpClient, url, f, chunk_size);
Response r = webTarget
.request(MediaType.APPLICATION_OCTET_STREAM_TYPE)
.header("Content-Disposition", contentDisposition)
.header("Content-Length", (int) f.length())
.post(Entity.entity(fileInStream, MediaType.APPLICATION_OCTET_STREAM_TYPE));
return r;
}
public static Response chunkedPostAuth(Client httpClient, final String url, final File f, final int chunk_size, String authHeaderField) throws Exception {
InputStream fileInStream = new FileInputStream(f);
String contentDisposition = "attachment; filename=\"" + f.getName() + "\"";
WebTarget webTarget = prepare(httpClient, url, f, chunk_size);
Response r = webTarget
.request(MediaType.APPLICATION_OCTET_STREAM_TYPE)
.header("Content-Disposition", contentDisposition)
.header("Content-Length", (int) f.length())
.header("Authorization", authHeaderField)
.post(Entity.entity(fileInStream, MediaType.APPLICATION_OCTET_STREAM_TYPE));
return r;
}
public static Response chunkedPut(Client httpClient, final String url, final File f, final int chunk_size) throws Exception {
InputStream fileInStream = new FileInputStream(f);
String contentDisposition = "attachment; filename=\"" + f.getName() + "\"";
WebTarget webTarget = prepare(httpClient, url, f, chunk_size);
Response r = webTarget
.request(MediaType.APPLICATION_OCTET_STREAM_TYPE)
.header("Content-Disposition", contentDisposition)
.header("Content-Length", (int) f.length())
.put(Entity.entity(fileInStream, MediaType.APPLICATION_OCTET_STREAM_TYPE));
return r;
}
public static Response chunkedPutAuth(Client httpClient, final String url, final File f, final int chunk_size, String authHeaderField) throws Exception {
InputStream fileInStream = new FileInputStream(f);
String contentDisposition = "attachment; filename=\"" + f.getName() + "\"";
WebTarget webTarget = prepare(httpClient, url, f, chunk_size);
Response r = webTarget
.request(MediaType.APPLICATION_OCTET_STREAM_TYPE)
.header("Content-Disposition", contentDisposition)
.header("Content-Length", (int) f.length())
.header("Authorization", authHeaderField)
.put(Entity.entity(fileInStream, MediaType.APPLICATION_OCTET_STREAM_TYPE));
return r;
}
private static WebTarget prepare(Client httpClient, String url, File f, int chunk_size) {
httpClient.property(org.glassfish.jersey.client.ClientProperties.CHUNKED_ENCODING_SIZE, chunk_size + "");
httpClient.property(org.glassfish.jersey.client.ClientProperties.REQUEST_ENTITY_PROCESSING, "CHUNKED");
WebTarget webTarget = httpClient.target(url);
javax.ws.rs.client.Invocation.Builder builder = webTarget.request();
builder.accept(MediaType.WILDCARD_TYPE);
return webTarget;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment