Merged RETURN-OF-THE-API (5.2.0) to HEAD (5.2)

128054: REPO-495: When uploading/updating binary content, guess mimetype and encoding
   - follow similar pattern to v0 Upload API (ignore Content-Type header)
   - in the future, we might wish to loosen this restriction [pending MNT-16381] and/or allow admin's option to update mimeType/encoding directly via node metadata update


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@128104 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jan Vonka
2016-06-09 16:13:50 +00:00
parent 41ae7f2d10
commit 939ed76c22
4 changed files with 137 additions and 121 deletions

View File

@@ -1,28 +1,28 @@
/*
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
/*
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.impl;
import org.alfresco.model.ApplicationModel;
@@ -140,9 +140,13 @@ import org.springframework.extensions.webscripts.servlet.FormData;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
@@ -1625,10 +1629,8 @@ public class NodesImpl implements Nodes
if (isContent)
{
// add empty file
ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
setWriterContentType(writer, new ContentInfoWrapper(nodeInfo.getContent()), nodeRef, false);
writer.putContent("");
// add empty file - note: currently will be set to default encoding only (UTF-8)
writeContent(nodeRef, nodeName, new ByteArrayInputStream("".getBytes()), false);
}
// eg. to create mandatory assoc(s)
@@ -2256,7 +2258,7 @@ public class NodesImpl implements Nodes
behaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_VERSIONABLE);
try
{
writeContent(nodeRef, contentInfo, stream);
writeContent(nodeRef, fileName, stream, true);
if ((isVersioned) || (versionMajor != null) || (versionComment != null) )
{
@@ -2281,11 +2283,69 @@ public class NodesImpl implements Nodes
return getFolderOrDocumentFullInfo(nodeRef, null, null, parameters);
}
private void writeContent(NodeRef nodeRef, BasicContentInfo contentInfo, InputStream stream)
private void writeContent(NodeRef nodeRef, String fileName, InputStream stream, boolean guessEncoding)
{
ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
setWriterContentType(writer, new ContentInfoWrapper(contentInfo), nodeRef, true);
writer.putContent(stream);
String mimeType = mimetypeService.guessMimetype(fileName);
writer.setMimetype(mimeType);
InputStream is = null;
if (guessEncoding)
{
is = new BufferedInputStream(stream);
is.mark(1024);
writer.setEncoding(guessEncoding(is, mimeType, false));
try
{
is.reset();
}
catch (IOException ioe)
{
if (logger.isWarnEnabled())
{
logger.warn("Failed to reset stream after trying to guess encoding: " + ioe.getMessage());
}
}
}
else
{
is = stream;
}
writer.putContent(is);
}
private String guessEncoding(InputStream in, String mimeType, boolean close)
{
String encoding = "UTF-8";
try
{
if (in != null)
{
Charset charset = mimetypeService.getContentCharsetFinder().getCharset(in, mimeType);
encoding = charset.name();
}
}
finally
{
try
{
if (close && (in != null))
{
in.close();
}
}
catch (IOException ioe)
{
if (logger.isWarnEnabled())
{
logger.warn("Failed to close stream after trying to guess encoding: " + ioe.getMessage());
}
}
}
return encoding;
}
protected void createVersion(NodeRef nodeRef, boolean isVersioned, VersionType versionType, String reason)
@@ -2308,34 +2368,6 @@ public class NodesImpl implements Nodes
}
}
private void setWriterContentType(ContentWriter writer, ContentInfoWrapper contentInfo, NodeRef nodeRef, boolean guessEncodingIfNull)
{
String mimeType = contentInfo.mimeType;
// Manage MimeType
if ((mimeType == null) || mimeType.equals(DEFAULT_MIMETYPE))
{
// the mimeType was not provided (or was the default binary mimeType) via the contentInfo, so try to guess
final String fileName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
mimeType = mimetypeService.guessMimetype(fileName);
}
writer.setMimetype(mimeType);
// Manage Encoding
if (contentInfo.encoding == null)
{
if (guessEncodingIfNull)
{
// the encoding was not provided, so try to guess
writer.guessEncoding();
}
}
else
{
writer.setEncoding(contentInfo.encoding);
}
}
@Override
public Node upload(String parentFolderNodeId, FormData formData, Parameters parameters)
{
@@ -2543,19 +2575,20 @@ public class NodesImpl implements Nodes
{
nodeType = ContentModel.TYPE_CONTENT;
}
NodeRef newFile = createNodeImpl(parentNodeRef, fileName, nodeType, props, assocTypeQName);
NodeRef nodeRef = createNodeImpl(parentNodeRef, fileName, nodeType, props, assocTypeQName);
// Write content
write(newFile, content);
writeContent(nodeRef, fileName, content.getInputStream(), true);
// Ensure the file is versionable (autoVersion = true, autoVersionProps = false)
ensureVersioningEnabled(newFile, true, false);
ensureVersioningEnabled(nodeRef, true, false);
// Extract the metadata
extractMetadata(newFile);
extractMetadata(nodeRef);
// Create the response
return getFolderOrDocumentFullInfo(newFile, parentNodeRef, nodeType, params);
return getFolderOrDocumentFullInfo(nodeRef, parentNodeRef, nodeType, params);
}
private String getStringOrNull(String value)
@@ -2652,23 +2685,6 @@ public class NodesImpl implements Nodes
}
}
/**
* Writes the content to the repository.
*
* @param nodeRef the reference to the node having a content property
* @param content the content
*/
protected void write(NodeRef nodeRef, Content content)
{
ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
// Per RA-637 & RA-885 requirement the mimeType provided by the client takes precedence, however,
// if the mimeType is null (or default binary mimeType) then it will be guessed.
setWriterContentType(writer, new ContentInfoWrapper(content), nodeRef, true);
writer.putContent(content.getInputStream());
}
/**
* Ensures the given node has the {@code cm:versionable} aspect applied to it, and
* that it has the initial version in the version store.
@@ -2806,11 +2822,22 @@ public class NodesImpl implements Nodes
/**
* @author Jamal Kaabi-Mofrad
*/
private static class ContentInfoWrapper
/*
private static class ContentInfoWrapper implements BasicContentInfo
{
private String mimeType;
private String encoding;
public String getEncoding()
{
return encoding;
}
public String getMimeType()
{
return mimeType;
}
ContentInfoWrapper(BasicContentInfo basicContentInfo)
{
if (basicContentInfo != null)
@@ -2851,5 +2878,6 @@ public class NodesImpl implements Nodes
}
}
}
*/
}