alfresco-community-repo/source/java/org/alfresco/opencmis/AlfrescoCmisStreamInterceptor.java
Dave Ward 2c920d57cc Merged V4.1-BUG-FIX to HEAD
47423: Merged V4.1-BUG-FIX-2013_02_26 to V4.1-BUG-FIX
      47381: ALF-15903 : form.getFieldLabel(field.id) sometimes returns the wrong label
         Added a check for the overridden id with the ending "-cntrl"
   47424: Fixes: ALF-17950: Content I'm Editing dashlet runs relativeDate parsing twice. Removes duplicate code and also prevents relativeTime parsing from breaking if called multiple times.
   47425: Merged V4.1-BUG-FIX-2013_02_26 to V4.1-BUG-FIX
      47386: ALF-15873: Form field validators not executed for NON mandatory date fields
         Add to context all form constraints defined in custom config.
   47426: Merged V4.1-BUG-FIX-2013_02_26 to V4.1-BUG-FIX
      47418: ALF-16385 : When the 'My activites' dashlet is narrow enough, vertical sizing of the content box is wrong.
         Recalculate a height of dashlet, when it was resized
   47427: ALF-18092: fixed issue with hidden-transitions field JSON
   47428: Merge DEV to V4.1-BUG-FIX
     46336 : ALF-16747 changing type of the root node of replicated set of nodes is not propagated to target.
   47437: Fixes ALF-17145: Pagination did not play nicely with back button.
   47439: Implements suggested fix for: ALF-16603
   47443: Fixed ALF-17255: AUDIT_PATH_REGEX regex pattern recompiled at runtime 
    - Switch to pre-compiled Pattern
   47473: Merged BRANCHES/DEV/BELARUS/V4.1-BUG-FIX-2013_02_26 to BRANCHES/DEV/V4.1-BUG-FIX:
      47313: ALF-18006 : Sending a PUT request without a Content-Type header resets the contents mimetype to application/octet-stream
          fix unit test
   47475: ALF-18092: Fixed unit test fallout from hidden transitions property serialization changes


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@47476 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2013-03-03 12:36:29 +00:00

159 lines
5.7 KiB
Java

/*
* Copyright (C) 2005-2013 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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/>.
*/
package org.alfresco.opencmis;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.content.filestore.FileContentReader;
import org.alfresco.service.cmr.repository.MimetypeService;
import org.alfresco.util.TempFileProvider;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
/**
* Interceptor to replace ContentStream parameter values with ReusableContentStream parameters
* which unlike the original may be closed and then opened again. This is important as retrying
* transaction advice is also added. A reopen of the original results is zero bytes being read.
*
* @author Alan Davis
* @since 4.0.2.26 / 4.1.4
*/
public class AlfrescoCmisStreamInterceptor implements MethodInterceptor
{
private MimetypeService mimetypeService;
/**
* @param mimetypeService service for helping with mimetypes
*/
public void setMimetypeService(MimetypeService mimetypeService)
{
this.mimetypeService = mimetypeService;
}
public Object invoke(MethodInvocation mi) throws Throwable
{
List<ReusableContentStream> reusableContentStreams = null;
try
{
Class<?>[] parameterTypes = mi.getMethod().getParameterTypes();
Object[] arguments = mi.getArguments();
for (int i=0; i<parameterTypes.length; i++)
{
if (ContentStream.class.isAssignableFrom(parameterTypes[i]))
{
ContentStream contentStream = (ContentStream) arguments[i];
if (contentStream != null)
{
if (reusableContentStreams == null)
{
reusableContentStreams = new ArrayList<ReusableContentStream>();
}
ReusableContentStream reuableContentStream = new ReusableContentStream(contentStream);
// ALF-18006
if (contentStream.getMimeType() == null)
{
String mimeType = mimetypeService.guessMimetype(reuableContentStream.getFileName(), new FileContentReader(reuableContentStream.file));
reuableContentStream.setMimeType(mimeType);
}
reusableContentStreams.add(reuableContentStream);
// It is possible to just change the arguments. No need to call a setter.
// Wow, did not expect that.
arguments[i] = reuableContentStream;
}
}
}
return mi.proceed();
}
finally
{
if (reusableContentStreams != null)
{
for (ReusableContentStream contentStream: reusableContentStreams)
{
contentStream.close();
}
}
}
}
private static class ReusableContentStream extends ContentStreamImpl
{
private static final long serialVersionUID = 8992465629472248502L;
private File file;
public ReusableContentStream(ContentStream contentStream) throws Exception
{
setLength(contentStream.getBigLength());
setMimeType(contentStream.getMimeType());
setFileName(contentStream.getFileName());
file = TempFileProvider.createTempFile(contentStream.getStream(), "cmis", "contentStream");
}
@Override
public InputStream getStream() {
InputStream stream = super.getStream();
if (stream == null && file != null)
{
try
{
stream = new FileInputStream(file)
{
@Override
public void close() throws IOException
{
setStream(null);
super.close();
}
};
}
catch (Exception e)
{
throw new AlfrescoRuntimeException("Expected to be able to reopen temporary file", e);
}
setStream(stream);
}
return stream;
}
public void close()
{
try
{
file.delete();
}
finally
{
file = null;
}
}
}
}