Skip to content

Commit

Permalink
Fixed github issue #7 (writing JSON from a stream throws an exception) (
Browse files Browse the repository at this point in the history
  • Loading branch information
twincitiesguy authored Aug 8, 2018
1 parent 760a7b7 commit 23948c6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/java/com/emc/rest/smart/SmartClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public final class SmartClientFactory {

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

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

// build Jersey client
return new Client(clientHandler, clientConfig);
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/emc/rest/smart/SmartClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream;
import java.net.URI;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -108,6 +109,27 @@ public void run() {
Assert.assertEquals("at least one task failed", 100, successCount.intValue());
}

@Test
public void testPutJsonStream() throws Exception {
String endpointStr = TestConfig.getPropertyNotEmpty(PROP_ATMOS_ENDPOINTS);
String[] endpoints = endpointStr.split(",");
List<Host> initialHosts = new ArrayList<Host>();
for (String endpoint : endpoints) {
initialHosts.add(new Host(new URI(endpoint).getHost()));
}
byte[] data = "JSON Stream Test".getBytes();

SmartConfig smartConfig = new SmartConfig(initialHosts);
Client client = SmartClientFactory.createSmartClient(smartConfig);

// this is an illegal use of this resource, but we just want to make sure the request is sent
// (no exception when finding a MessageBodyWriter)
ClientResponse response = client.resource(endpoints[0]).path("/rest/service").type("application/json")
.put(ClientResponse.class, new ByteArrayInputStream(data));

Assert.assertEquals(403, response.getStatus());
}

@Test
public void testConnTimeout() throws Exception {
int CONNECTION_TIMEOUT_MILLIS = 10000; // 10 seconds
Expand Down

0 comments on commit 23948c6

Please sign in to comment.