diff --git a/nuxeo-java-client-test/src/test/java/org/nuxeo/client/ITUpload.java b/nuxeo-java-client-test/src/test/java/org/nuxeo/client/ITUpload.java index 627b5b3f..334ca729 100644 --- a/nuxeo-java-client-test/src/test/java/org/nuxeo/client/ITUpload.java +++ b/nuxeo-java-client-test/src/test/java/org/nuxeo/client/ITUpload.java @@ -229,4 +229,28 @@ public void itCanUploadBatchFileThroughRestMapping() { assertContentEquals("sample.jpg", blob); } + /* + * JAVACLIENT-208 + */ + @Test + public void itCanUploadFileWithNonAsciiCharacter() { + BatchUploadManager batchUploadManager = nuxeoClient.batchUploadManager(); + BatchUpload batchUpload = batchUploadManager.createBatch(); + assertNotNull(batchUpload); + File file = FileUtils.getResourceFileFromContext("sample.jpg"); + FileBlob fileBlob = new FileBlob(file, "Ümlaut.pdf"); + batchUpload = batchUpload.upload("1", fileBlob); + assertNotNull(batchUpload); + + // Getting a doc and attaching the batch file + Document doc = Document.createWithName("file", "File"); + doc.setPropertyValue("dc:title", "new title"); + doc = nuxeoClient.repository().createDocumentByPath("/", doc); + assertNotNull(doc); + doc.setPropertyValue("file:content", batchUpload.getBatchBlob()); + doc = doc.updateDocument(); + assertEquals("Ümlaut.pdf", doc.getPropertyValue("file:content/name")); + + } + } diff --git a/nuxeo-java-client/src/main/java/org/nuxeo/client/objects/upload/BatchUpload.java b/nuxeo-java-client/src/main/java/org/nuxeo/client/objects/upload/BatchUpload.java index 7cb8cda7..41b187f4 100644 --- a/nuxeo-java-client/src/main/java/org/nuxeo/client/objects/upload/BatchUpload.java +++ b/nuxeo-java-client/src/main/java/org/nuxeo/client/objects/upload/BatchUpload.java @@ -25,6 +25,8 @@ import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.util.List; import java.util.Map; @@ -161,13 +163,15 @@ public BatchUpload upload(String fileIdx, File file, String name, String fileTyp */ public BatchUpload upload(String fileIdx, Blob blob) { String filename = blob.getFilename(); + String encodedFilename = encodeToAsciiForHeader(filename); String mimeType = blob.getMimeType(); long length = blob.getContentLength(); if (chunkSize == 0) { + // Post blob RequestBody fbody = Requests.create(blob); BatchUpload response = fetchResponse( - api.upload(filename, length, mimeType, UPLOAD_NORMAL_TYPE, 0, 1, batchId, fileIdx, fbody)); + api.upload(encodedFilename, length, mimeType, UPLOAD_NORMAL_TYPE, 0, 1, batchId, fileIdx, fbody)); response.name = filename; response.batchId = batchId; response.fileIdx = fileIdx; @@ -186,7 +190,7 @@ public BatchUpload upload(String fileIdx, Blob blob) { // Post chunk as a stream RequestBody requestBody = RequestBody.create(MediaTypes.APPLICATION_OCTET_STREAM.toOkHttpMediaType(), buffer, 0, bufferLength); - response = fetchResponse(api.upload(filename, length, mimeType, UPLOAD_CHUNKED_TYPE, chunkIndex, + response = fetchResponse(api.upload(encodedFilename, length, mimeType, UPLOAD_CHUNKED_TYPE, chunkIndex, chunkNumber, batchId, fileIdx, requestBody)); chunkIndex++; } @@ -204,6 +208,14 @@ public BatchUpload upload(String fileIdx, Blob blob) { } } + protected String encodeToAsciiForHeader(String string) { + try { + return URLEncoder.encode(string, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new NuxeoClientException("Unable to encode the header: " + string, e); + } + } + public void cancel() { fetchResponse(api.cancel(batchId)); }