Skip to content

Commit 23948c6

Browse files
Fixed github issue #7 (writing JSON from a stream throws an exception) (#8)
1 parent 760a7b7 commit 23948c6

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/main/java/com/emc/rest/smart/SmartClientFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import org.slf4j.Logger;
4141
import org.slf4j.LoggerFactory;
4242

43+
import java.io.*;
44+
4345
public final class SmartClientFactory {
4446

4547
private static final Logger log = LoggerFactory.getLogger(SmartClientFactory.class);
@@ -106,7 +108,12 @@ public static Client createStandardClient(SmartConfig smartConfig,
106108
clientConfig.getClasses().add(OctetStreamXmlProvider.class);
107109

108110
// add JSON support (using Jackson's ObjectMapper instead of JAXB marshalling)
109-
clientConfig.getClasses().add(JacksonJaxbJsonProvider.class);
111+
JacksonJaxbJsonProvider jsonProvider = new JacksonJaxbJsonProvider();
112+
// make sure we don't try to serialize any of these type hierarchies (clearly a bug in JacksonJsonProvider)
113+
jsonProvider.addUntouchable(InputStream.class);
114+
jsonProvider.addUntouchable(OutputStream.class);
115+
jsonProvider.addUntouchable(File.class);
116+
clientConfig.getSingletons().add(jsonProvider);
110117

111118
// build Jersey client
112119
return new Client(clientHandler, clientConfig);

src/test/java/com/emc/rest/smart/SmartClientTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
import javax.crypto.Mac;
4545
import javax.crypto.spec.SecretKeySpec;
46+
import java.io.ByteArrayInputStream;
4647
import java.net.URI;
4748
import java.text.DateFormat;
4849
import java.text.SimpleDateFormat;
@@ -108,6 +109,27 @@ public void run() {
108109
Assert.assertEquals("at least one task failed", 100, successCount.intValue());
109110
}
110111

112+
@Test
113+
public void testPutJsonStream() throws Exception {
114+
String endpointStr = TestConfig.getPropertyNotEmpty(PROP_ATMOS_ENDPOINTS);
115+
String[] endpoints = endpointStr.split(",");
116+
List<Host> initialHosts = new ArrayList<Host>();
117+
for (String endpoint : endpoints) {
118+
initialHosts.add(new Host(new URI(endpoint).getHost()));
119+
}
120+
byte[] data = "JSON Stream Test".getBytes();
121+
122+
SmartConfig smartConfig = new SmartConfig(initialHosts);
123+
Client client = SmartClientFactory.createSmartClient(smartConfig);
124+
125+
// this is an illegal use of this resource, but we just want to make sure the request is sent
126+
// (no exception when finding a MessageBodyWriter)
127+
ClientResponse response = client.resource(endpoints[0]).path("/rest/service").type("application/json")
128+
.put(ClientResponse.class, new ByteArrayInputStream(data));
129+
130+
Assert.assertEquals(403, response.getStatus());
131+
}
132+
111133
@Test
112134
public void testConnTimeout() throws Exception {
113135
int CONNECTION_TIMEOUT_MILLIS = 10000; // 10 seconds

0 commit comments

Comments
 (0)