Viewer Java SDK requests sporadically returning java.net.SocketException

I’m using the GroupDocs Viewer Java SDK to preview documents in our web app. For some documents, I am sometimes (not always) receiving the following error from GroupDocs when I attempt to upload the document:

Jun 25 15:28:50 : com.sun.jersey.api.client.ClientHandlerException: java.net.SocketException: Connection reset
Jun 25 15:28:50 : at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151)
Jun 25 15:28:50 : at com.sun.jersey.api.client.Client.handle(Client.java:648)
Jun 25 15:28:50 : at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
Jun 25 15:28:50 : at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
Jun 25 15:28:50 : at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:568)
Jun 25 15:28:50 : at com.groupdocs.sdk.common.ApiInvoker.invokeAPI(ApiInvoker.java:218)
Jun 25 15:28:50 : at com.groupdocs.sdk.api.StorageApi.Upload(StorageApi.java:196)


This is the code snippet that is creating the error, taken almost exactly from your Java SDK samples:

ApiInvoker.getInstance().setRequestSigner( new GroupDocsRequestSigner( PRIVATE_KEY ));
final StorageApi storageApi = new StorageApi();
storageApi.setBasePath( PREVIEW_URL );
final FileStream fileStream = new FileStream( dataStream );
fileStream.setFileName( encodedFileName );
final UploadResponse uploadResponse = storageApi.Upload( CLIENT_ID, encodedFileName, null, null, fileStream );

I have tested this over and over with the same document. Sometimes it returns successfully and I am able to preview it, and other times I get the above SocketException. It seems to happen for certain documents more than others, but I haven’t yet been able to find a pattern.
When I upload this same document and try to preview it there, sometimes it works and sometimes it doesn’t. Does anyone have any idea what might be causing this? If there was a problem with the document I would assume that it would never work, but this is not the case.

This query has been answered via email.

The final response from the customer is pasted below:

“Hi Derek,

I think I managed to resolve most of my issues with the SDK. That SocketException was being caused by something to do with the file name. Basically I have to create a temporary file, which I am then passing to your SDK as a FileInputStream. But the file name I was passing to this line:
final UploadResponse uploadResponse = storageApi.Upload( CLIENT_ID, file.getName(), null, null,
fileStream );

was NOT the name of the temporary file (as that was just being generated), but the name of the actual original file. When I changed this to the name of the temporary file, everything worked like I expected.

Thanks”

This SocketException occurs on the server side when the client closed the socket connection before the response could be returned over the socket. For example, by quitting the browser before the reponse was retrieved. Connection reset simply means that a TCP RST was received. TCP RST packet is that the remote side telling you the connection on which the previous TCP packet is sent is not recognized, maybe the connection has closed, maybe the port is not open, and something like these. A reset packet is simply one with no payload and with the RST bit set in the TCP header flags. There are several possible causes.

  • The other end has deliberately reset the connection, in a way which I will not document here. It is rare, and generally incorrect, for application software to do this, but it is not unknown for commercial software.

  • More commonly, it is caused by writing to a connection that the other end has already closed normally. In other words an application protocol error.

  • It can also be caused by closing a socket when there is unread data in the socket receive buffer.