Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)

121563 jkaabimofrad: RA-640: Fixed encoding issue when updating binary content, as well as, multipart uploading.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@126412 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jamal Kaabi-Mofrad
2016-05-10 10:49:57 +00:00
parent 9bcca7763d
commit ca1de05fe7
4 changed files with 155 additions and 71 deletions

View File

@@ -788,6 +788,8 @@ public class NodeApiTest extends AbstractBaseApiTest
ContentInfo contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType());
// Default encoding
assertEquals("UTF-8", contentInfo.getEncoding());
// Check there is no path info returned.
// The path info should only be returned when it is requested via a select statement.
assertNull(document.getPath());
@@ -866,6 +868,26 @@ public class NodeApiTest extends AbstractBaseApiTest
.build();
post(getChildrenUrl(user1Home.getId()), user1, reqBody.getBody(), null, reqBody.getContentType(), 400);
// User1 uploads a new file
reqBody = MultiPartBuilder.create()
.setFileData(new FileData(fileName2, file2, MimetypeMap.MIMETYPE_TEXT_PLAIN, "windows-1252"))
.build();
response = post(getChildrenUrl(user1Home.getId()), user1, reqBody.getBody(), null, reqBody.getContentType(), 201);
document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
// Check the upload response
assertEquals(fileName2, document.getName());
contentInfo = document.getContent();
assertNotNull(contentInfo);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertEquals("windows-1252", contentInfo.getEncoding());
// Test invalid mimeType
reqBody = MultiPartBuilder.create()
.setFileData(new FileData(fileName2, file2, "*/invalidSubType", "ISO-8859-1"))
.setAutoRename(true)
.build();
post(getChildrenUrl(user1Home.getId()), user1, reqBody.getBody(), null, reqBody.getContentType(), 400);
}
/**
@@ -1548,8 +1570,8 @@ public class NodeApiTest extends AbstractBaseApiTest
// Update the empty node's content
String content = "The quick brown fox jumps over the lazy dog.";
ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes());
File file = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
BinaryPayload payload = new BinaryPayload(file, MimetypeMap.MIMETYPE_TEXT_PLAIN);
File txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
BinaryPayload payload = new BinaryPayload(txtFile, MimetypeMap.MIMETYPE_TEXT_PLAIN);
// Try to update a folder!
putBinary(URL_NODES + f1_nodeId + "/content", user1, payload, null, null, 400);
@@ -1576,6 +1598,8 @@ public class NodeApiTest extends AbstractBaseApiTest
contentInfo = docResp.getContent();
assertNotNull(contentInfo);
assertNotNull(contentInfo.getEncoding());
// Default encoding
assertEquals("UTF-8", contentInfo.getEncoding());
assertTrue(contentInfo.getSizeInBytes() > 0);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
assertNotNull(contentInfo.getMimeTypeName());
@@ -1587,13 +1611,14 @@ public class NodeApiTest extends AbstractBaseApiTest
assertEquals(content, response.getResponse());
// Update the node's content again. Also, change the mimeType and make the response to return path!
file = getResourceFile("quick.pdf");
payload = new BinaryPayload(file, MimetypeMap.MIMETYPE_PDF);
File pdfFile = getResourceFile("quick.pdf");
payload = new BinaryPayload(pdfFile, MimetypeMap.MIMETYPE_PDF, "ISO-8859-1");
response = putBinary(url + "?select=path", user1, payload, null, null, 200);
docResp = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
assertEquals(docName, docResp.getName());
assertNotNull(docResp.getContent());
assertNotNull("ISO-8859-1", docResp.getContent());
assertTrue(docResp.getContent().getSizeInBytes().intValue() > 0);
assertEquals(MimetypeMap.MIMETYPE_PDF, docResp.getContent().getMimeType());
PathInfo pathInfo = docResp.getPath();
@@ -1605,6 +1630,16 @@ public class NodeApiTest extends AbstractBaseApiTest
// check the last element is F1
assertEquals(f1.getName(), pathElements.get(pathElements.size() - 1).getName());
// update the original content with different encoding
payload = new BinaryPayload(txtFile, MimetypeMap.MIMETYPE_TEXT_PLAIN, "ISO-8859-15");
response = putBinary(url, user1, payload, null, null, 200);
docResp = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
assertEquals(docName, docResp.getName());
assertNotNull(docResp.getContent());
assertNotNull("ISO-8859-15", docResp.getContent());
assertTrue(docResp.getContent().getSizeInBytes().intValue() > 0);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, docResp.getContent().getMimeType());
// Download the file
response = getSingle(url, user1, null, 200);
assertNotNull(content, response.getResponse());

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2014 Alfresco Software Limited.
* Copyright (C) 2005-2016 Alfresco Software Limited.
*
* This file is part of Alfresco
*
@@ -880,8 +880,8 @@ public class PublicApiHttpClient
public BinaryRequestEntity(File file, String mimeType, String charset)
{
this.file = file;
this.mimeType = (mimeType == null) ? "application/octet-stream" : mimeType;
this.charset = (charset == null) ? "UTF-8" : charset;
this.mimeType = mimeType;
this.charset = charset;
}
@Override
@@ -918,7 +918,15 @@ public class PublicApiHttpClient
@Override
public String getContentType()
{
return mimeType + "; " + charset;
if (charset == null)
{
return mimeType;
}
if (mimeType == null)
{
return null;
}
return mimeType + "; charset=" + charset;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
* Copyright (C) 2005-2016 Alfresco Software Limited.
*
* This file is part of Alfresco
*
@@ -197,12 +197,19 @@ public class MultiPartBuilder
private final String fileName;
private final File file;
private final String mimetype;
private final String encoding;
public FileData(String fileName, File file, String mimetype)
{
this(fileName, file, mimetype, null);
}
public FileData(String fileName, File file, String mimetype, String encoding)
{
this.fileName = fileName;
this.file = file;
this.mimetype = mimetype;
this.encoding = encoding;
}
public String getFileName()
@@ -219,6 +226,11 @@ public class MultiPartBuilder
{
return mimetype;
}
public String getEncoding()
{
return encoding;
}
}
public static class MultiPartRequest
@@ -258,7 +270,7 @@ public class MultiPartBuilder
{
FilePart fp = new FilePart("filedata", fileData.getFileName(), fileData.getFile(), fileData.getMimetype(), null);
// Get rid of the default values added upon FilePart instantiation
fp.setCharSet(null);
fp.setCharSet(fileData.getEncoding());
fp.setContentType(fileData.getMimetype());
parts.add(fp);
addPartIfNotNull(parts, "filename", fileData.getFileName());