Skip to content

Commit 2813514

Browse files
author
ehennum
committed
support for reading Files in dynamic Data Services #1063
1 parent 6dcadb6 commit 2813514

File tree

4 files changed

+58
-28
lines changed

4 files changed

+58
-28
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/CallManagerImpl.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.marklogic.client.SessionState;
2222
import com.marklogic.client.impl.BaseProxy;
2323
import com.marklogic.client.impl.NodeConverter;
24-
import com.marklogic.client.impl.RESTServices;
2524
import com.marklogic.client.impl.RESTServices.CallField;
2625
import com.marklogic.client.impl.RESTServices.MultipleAtomicCallField;
2726
import com.marklogic.client.impl.RESTServices.MultipleCallResponse;
@@ -1620,18 +1619,16 @@ public Stream<byte[]> many(MultipleCallResponse response) {
16201619
return response.asStreamOfBytes();
16211620
}
16221621
});
1623-
/* TODO: related to #1055
16241622
put(File.class, new ReturnConverter<File>() {
16251623
@Override
16261624
public File one(SingleCallResponse response) {
1627-
return null;
1625+
return NodeConverter.InputStreamToFile(response.asInputStream());
16281626
}
16291627
@Override
16301628
public Stream<File> many(MultipleCallResponse response) {
1631-
return null;
1629+
return NodeConverter.InputStreamToFile(response.asStreamOfInputStream());
16321630
}
16331631
});
1634-
*/
16351632
put(InputStream.class, new ReturnConverter<InputStream>() {
16361633
@Override
16371634
public InputStream one(SingleCallResponse response) {

marklogic-client-api/src/main/java/com/marklogic/client/impl/NodeConverter.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
import javax.xml.transform.Source;
3737
import javax.xml.transform.stream.StreamSource;
3838
import java.io.*;
39+
import java.nio.file.Files;
40+
import java.nio.file.Path;
41+
import java.nio.file.StandardCopyOption;
3942
import java.util.function.BiFunction;
4043
import java.util.function.Function;
4144
import java.util.stream.Stream;
@@ -342,6 +345,21 @@ static public Document InputStreamToDocument(InputStream inputStream) {
342345
static public Stream<Document> InputStreamToDocument(Stream<? extends InputStream> values) {
343346
return (values == null) ? null : values.map(NodeConverter::InputStreamToDocument);
344347
}
348+
static public File InputStreamToFile(InputStream inputStream) {
349+
if (inputStream == null) {
350+
return null;
351+
}
352+
try {
353+
Path tempFile = Files.createTempFile("tmp", null);
354+
Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
355+
return tempFile.toFile();
356+
} catch (IOException e) {
357+
throw new RuntimeException(e);
358+
}
359+
}
360+
static public Stream<File> InputStreamToFile(Stream<? extends InputStream> values) {
361+
return (values == null) ? null : values.map(NodeConverter::InputStreamToFile);
362+
}
345363
static public InputSource ReaderToInputSource(Reader reader) {
346364
return (reader == null) ? null : new InputSource(reader);
347365
}

marklogic-client-api/src/test/java/com/marklogic/client/test/dataservices/CallManagerTest.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public static void setup() {
117117
setupEndpointSingleRequired(docMgr, docMeta, "singleAtomic", "dateTime");
118118
setupEndpointSingleRequired(docMgr, docMeta, "singleNode", "object");
119119

120+
setupEndpointSingleRequired(docMgr, docMeta, "singleBinary", "binaryDocument");
121+
setupEndpointSingleRequired(docMgr, docMeta, "singleJson", "jsonDocument");
122+
setupEndpointSingleRequired(docMgr, docMeta, "singleText", "textDocument");
123+
setupEndpointSingleRequired(docMgr, docMeta, "singleXml", "xmlDocument");
124+
120125
setupEndpointSingleNulled(docMgr, docMeta, "nullAtomic", "decimal");
121126
setupEndpointSingleNulled(docMgr, docMeta, "nullNode", "xmlDocument");
122127

@@ -382,7 +387,7 @@ public void testBadParamAtomicType() {
382387
public void testBadParamNodeType() {
383388
String functionName = "jsonDocument";
384389

385-
CallManager.CallableEndpoint callableEndpoint = makeCallableEndpoint(functionName );
390+
CallManager.CallableEndpoint callableEndpoint = makeCallableEndpoint(functionName);
386391

387392
CallManager.ManyCaller<String> caller = makeManyCaller(callableEndpoint, String.class);
388393

@@ -796,13 +801,16 @@ public void testBinaryDocument() {
796801
functionName, caller2, caller2.args().param("param1", values2), values, CallManagerTest::string
797802
);
798803

799-
/* TODO:
804+
String functionName3 = "singleBinary";
805+
806+
CallManager.CallableEndpoint callableEndpoint3 = makeCallableEndpoint(functionName3);
807+
800808
File value3 = new File("src/test/resources/test.bin");
801-
CallManager.ManyCaller<File> caller3 = makeManyCaller(callableEndpoint, File.class);
809+
CallManager.OneCaller<File> caller3 = makeOneCaller(callableEndpoint3, File.class);
802810
testConvertedCall(
803-
functionName, caller3, caller3.args().param("param1", value3), new String[]{CallManagerTest.string(value3)}, CallManagerTest::string
811+
functionName3, caller3, caller3.args().param("param1", value3),
812+
CallManagerTest.string(value3), CallManagerTest::string
804813
);
805-
*/
806814
}
807815
@Test
808816
public void testJsonDocument() {
@@ -832,14 +840,16 @@ public void testJsonDocument() {
832840
functionName, caller4, caller4.args().param("param1", values4), values, CallManagerTest::string
833841
);
834842

835-
/* TODO:
843+
844+
String functionName5 = "singleJson";
845+
CallManager.CallableEndpoint callableEndpoint5 = makeCallableEndpoint(functionName5);
846+
836847
File value5 = new File("src/test/resources/basic1.json");
837-
CallManager.ManyCaller<File> caller5 = makeManyCaller(callableEndpoint, File.class);
848+
CallManager.OneCaller<File> caller5 = makeOneCaller(callableEndpoint5, File.class);
838849
testConvertedCall(
839-
functionName, caller3, caller3.args().param("param1", value5),
840-
new String[]{CallManagerTest.string(value5)}, CallManagerTest::string
850+
functionName5, caller5, caller5.args().param("param1", value5),
851+
CallManagerTest.string(value5).trim(), CallManagerTest::string
841852
);
842-
*/
843853
}
844854
@Test
845855
public void testArray() {
@@ -958,14 +968,15 @@ public void testTextDocument() {
958968
functionName, caller2, caller2.args().param("param1", values2), values, CallManagerTest::string
959969
);
960970

961-
/* TODO:
971+
String functionName3 = "singleText";
972+
CallManager.CallableEndpoint callableEndpoint3 = makeCallableEndpoint(functionName3);
973+
962974
File value3 = new File("src/test/resources/hola.txt");
963-
CallManager.ManyCaller<File> caller3 = makeManyCaller(callableEndpoint, File.class);
975+
CallManager.OneCaller<File> caller3 = makeOneCaller(callableEndpoint3, File.class);
964976
testConvertedCall(
965-
functionName, caller3, caller3.args().param("param1", value3),
966-
new String[]{CallManagerTest.string(value3)}, CallManagerTest::string
977+
functionName3, caller3, caller3.args().param("param1", value3),
978+
CallManagerTest.string(value3), CallManagerTest::string
967979
);
968-
*/
969980
}
970981
@Test
971982
public void testMultipleNullNode() {
@@ -979,7 +990,7 @@ public void testMultipleNullNode() {
979990
public void testXmlDocument() {
980991
String functionName = "xmlDocument";
981992

982-
CallManager.CallableEndpoint callableEndpoint = makeCallableEndpoint(functionName );
993+
CallManager.CallableEndpoint callableEndpoint = makeCallableEndpoint(functionName);
983994

984995
String[] values = new String[]{"<root><child>text1</child></root>", "<root><child>text2</child></root>"};
985996

@@ -1044,20 +1055,21 @@ public void testXmlDocument() {
10441055
functionName, caller11, caller11.args().param("param1", values11), values, CallManagerTest::xmlString
10451056
);
10461057

1047-
/* TODO:
1058+
String functionName12 = "singleXml";
1059+
CallManager.CallableEndpoint callableEndpoint12 = makeCallableEndpoint(functionName12);
1060+
10481061
File value12 = new File("src/test/resources/test.xml");
1049-
CallManager.ManyCaller<File> caller12 = makeManyCaller(callableEndpoint, File.class);
1062+
CallManager.OneCaller<File> caller12 = makeOneCaller(callableEndpoint12, File.class);
10501063
testConvertedCall(
1051-
functionName, caller3, caller3.args().param("param1", value12),
1052-
new String[]{CallManagerTest.string(value12)}, CallManagerTest::string
1064+
functionName12, caller12, caller12.args().param("param1", value12),
1065+
CallManagerTest.string(value12).trim(), CallManagerTest::xmlString
10531066
);
1054-
*/
10551067
}
10561068
@Test
10571069
public void testNullNode() {
10581070
String functionName = "nullNode";
10591071

1060-
CallManager.CallableEndpoint callableEndpoint = makeCallableEndpoint(functionName );
1072+
CallManager.CallableEndpoint callableEndpoint = makeCallableEndpoint(functionName);
10611073

10621074
testCharacterNodeOne(functionName, callableEndpoint);
10631075

@@ -1263,6 +1275,9 @@ private static XMLStreamReader xmlStreamReader(String value) {
12631275
private static String xmlString(byte[] value) {
12641276
return string(new StreamSource(new ByteArrayInputStream(value)));
12651277
}
1278+
private static String xmlString(File value) {
1279+
return string(new StreamSource(value));
1280+
}
12661281
private static String xmlString(InputStream value) {
12671282
return string(new StreamSource(value));
12681283
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{a:{b1:{c:"jsonValue1"}, b2:["b2 val1", "b2 val2"]}}
1+
{"a":{"b1":{"c":"jsonValue1"}, "b2":["b2 val1", "b2 val2"]}}

0 commit comments

Comments
 (0)