Merged DEV/JASONH to HEAD

12169: JCR - guess/set mime type and encoding ... means that doc added via JCR will display properly if clicked on in the web client
  12634: JCR - add test for JAWS-191 (r12169) improvement and wrap debug


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@12654 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jan Vonka
2009-01-09 13:07:34 +00:00
parent 0f51b06274
commit c246edcef3
3 changed files with 143 additions and 5 deletions

View File

@@ -24,6 +24,8 @@
*/
package org.alfresco.jcr.item;
import java.io.InputStream;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
@@ -52,6 +54,8 @@ public class ItemTest extends BaseJCRTest
{
protected Session session;
private static final String QUICK_TXT = "The quick brown fox jumps over the lazy dog";
@Override
protected void setUp() throws Exception
{
@@ -161,11 +165,10 @@ public class ItemTest extends BaseJCRTest
//
// write some content to new node
//
content.setProperty("cm:content", "The quick brown fox jumps over the lazy dog");
content.setProperty("cm:content", QUICK_TXT);
// use Alfresco native API to set mimetype
ServiceRegistry registry = (ServiceRegistry)applicationContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
setMimetype(registry, content, "cm:content", MimetypeMap.MIMETYPE_TEXT_PLAIN);
// enable versioning capability
@@ -228,6 +231,100 @@ public class ItemTest extends BaseJCRTest
}
}
public void test_JAWS_191() throws RepositoryException
{
SimpleCredentials user = new SimpleCredentials("admin", "admin".toCharArray());
session = repository.login(user, "SpacesStore");
String runid = ""+System.currentTimeMillis();
String pathname = "cm:JCR-"+runid+".jpg";
String name = "JCR Sample ("+runid+")";
try
{
Node rootNode = session.getRootNode();
Node companyHome = rootNode.getNode("app:company_home");
// create the content node
Node content = companyHome.addNode(pathname, "cm:content");
content.setProperty("cm:name", name);
// add titled aspect (for Web Client display)
content.addMixin("cm:titled");
content.setProperty("cm:title", name);
content.setProperty("cm:description", name);
InputStream is = getClass().getClassLoader().getResourceAsStream("org/alfresco/jcr/test/testQuick.jpg");
assertNotNull(is);
//
// write some content to new node
//
content.setProperty("cm:content", is);
// save changes
session.save();
content = companyHome.getNode(pathname);
// use Alfresco native API to get content data (for mimetype / encoding)
ServiceRegistry registry = (ServiceRegistry)applicationContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
ContentData contentData = getContentData(registry, content);
assertEquals(MimetypeMap.MIMETYPE_IMAGE_JPEG, contentData.getMimetype());
assertEquals("UTF-8", contentData.getEncoding());
}
finally
{
if (session != null) { session.logout(); }
}
session = repository.login(user, "SpacesStore");
runid = ""+System.currentTimeMillis();
pathname = "cm:JCR-"+runid+".txt";
name = "JCR Sample ("+runid+")";
try
{
Node rootNode = session.getRootNode();
Node companyHome = rootNode.getNode("app:company_home");
// create the content node
Node content = companyHome.addNode(pathname, "cm:content");
content.setProperty("cm:name", name);
// add titled aspect (for Web Client display)
content.addMixin("cm:titled");
content.setProperty("cm:title", name);
content.setProperty("cm:description", name);
//
// write some content to new node
//
content.setProperty("cm:content", QUICK_TXT);
// save changes
session.save();
content = companyHome.getNode(pathname);
// use Alfresco native API to get content data (for mimetype / encoding)
ServiceRegistry registry = (ServiceRegistry)applicationContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
ContentData contentData = getContentData(registry, content);
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentData.getMimetype());
assertEquals("UTF-8", contentData.getEncoding());
}
finally
{
if (session != null) { session.logout(); }
}
}
private static void setMimetype(ServiceRegistry registry, Node node, String propertyName, String mimeType) throws RepositoryException
{
// convert the JCR Node to an Alfresco Node Reference
@@ -241,4 +338,14 @@ public class ItemTest extends BaseJCRTest
content = ContentData.setMimetype(content, mimeType);
nodeService.setProperty(nodeRef, ContentModel.PROP_CONTENT, content);
}
private static ContentData getContentData(ServiceRegistry registry, Node node) throws RepositoryException
{
// convert the JCR Node to an Alfresco Node Reference
NodeRef nodeRef = JCRNodeRef.getNodeRef(node);
// retrieve the Content Property (represented as a ContentData object in Alfresco)
NodeService nodeService = registry.getNodeService();
return (ContentData)nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
}
}

View File

@@ -24,8 +24,10 @@
*/
package org.alfresco.jcr.item;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
@@ -49,18 +51,21 @@ import org.alfresco.jcr.api.JCRNodeRef;
import org.alfresco.jcr.dictionary.DataTypeMap;
import org.alfresco.jcr.dictionary.PropertyDefinitionImpl;
import org.alfresco.jcr.util.JCRProxyFactory;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.content.encoding.ContentCharsetFinder;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.dictionary.InvalidTypeException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.MimetypeService;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.Path;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.repository.datatype.TypeConversionException;
import org.alfresco.service.namespace.QName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
@@ -70,6 +75,7 @@ import org.alfresco.service.namespace.QName;
*/
public class PropertyImpl extends ItemImpl implements Property
{
private static Log logger = LogFactory.getLog(PropertyImpl.class);
private NodeImpl node;
private QName name;
@@ -603,8 +609,33 @@ public class PropertyImpl extends ItemImpl implements Property
{
ContentService contentService = session.getRepositoryImpl().getServiceRegistry().getContentService();
ContentWriter writer = contentService.getWriter(node.getNodeRef(), name, true);
writer.setMimetype(MimetypeMap.MIMETYPE_BINARY);
writer.putContent((InputStream)value);
MimetypeService mimetypeService = session.getRepositoryImpl().getServiceRegistry().getMimetypeService();
String guessedMimetype = mimetypeService.guessMimetype(node.getName());
writer.setMimetype(guessedMimetype);
if (logger.isDebugEnabled())
{
logger.debug("node pathname: " + node.getName());
logger.debug("guessed mime type: " + guessedMimetype);
}
BufferedInputStream bis = new BufferedInputStream((InputStream)value);
ContentCharsetFinder charsetFinder = mimetypeService.getContentCharsetFinder();
Charset encoding = charsetFinder.getCharset(bis, guessedMimetype);
writer.setEncoding(encoding.name());
if (logger.isDebugEnabled())
{
logger.debug("setEncoding: " + encoding.name());
}
writer.putContent(bis);
}
catch(RepositoryException e)
{
throw new ValueFormatException(e);
}
catch(InvalidTypeException e)
{

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB