Java Code Examples for org.alfresco.service.cmr.repository.ContentData#getContentUrl()

The following examples show how to use org.alfresco.service.cmr.repository.ContentData#getContentUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContentServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public DirectAccessUrl getDirectAccessUrl(NodeRef nodeRef, Date expiresAt)
{
    ContentData contentData = getContentData(nodeRef, ContentModel.PROP_CONTENT);

    // check that the URL is available
    if (contentData == null || contentData.getContentUrl() == null)
    {
        throw new IllegalArgumentException("The supplied nodeRef " + nodeRef + " has no content.");
    }

    if (store.isDirectAccessSupported())
    {
        return store.getDirectAccessUrl(contentData.getContentUrl(), expiresAt);
    }

    return null;
}
 
Example 2
Source File: RenditionService2Impl.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns the hash code of the source node's content url. As transformations may be returned in a different
 * sequences to which they were requested, this is used work out if a rendition should be replaced.
 */
private int getSourceContentHashCode(NodeRef sourceNodeRef)
{
    int hashCode = SOURCE_HAS_NO_CONTENT;
    ContentData contentData = DefaultTypeConverter.INSTANCE.convert(ContentData.class, nodeService.getProperty(sourceNodeRef, PROP_CONTENT));
    if (contentData != null)
    {
        // Originally we used the contentData URL, but that is not enough if the mimetype changes.
        String contentString = contentData.getContentUrl()+contentData.getMimetype();
        if (contentString != null)
        {
            hashCode = contentString.hashCode();
        }
    }
    return hashCode;
}
 
Example 3
Source File: ContentDataDAOImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected int deleteContentDataEntity(Long id)
{
    // Get the content urls
    try
    {
        ContentData contentData = getContentData(id).getSecond();
        String contentUrl = contentData.getContentUrl();
        if (contentUrl != null)
        {
            // It has been dereferenced and may be orphaned - we'll check later
            registerDereferencedContentUrl(contentUrl);
        }
    }
    catch (DataIntegrityViolationException e)
    {
        // Doesn't exist.  The node doesn't enforce a FK constraint, so we protect against this.
    }
    // Issue the delete statement
    Map<String, Object> params = new HashMap<String, Object>(11);
    params.put("id", id);
    return template.delete(DELETE_CONTENT_DATA, params);
}
 
Example 4
Source File: NodeContentData.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Construct
 */
public NodeContentData(NodeRef nodeRef, ContentData contentData)
{
    super(contentData.getContentUrl(), contentData.getMimetype(), contentData.getSize(),
            contentData.getEncoding(), contentData.getLocale());
    this.nodeRef = nodeRef;
}
 
Example 5
Source File: RepoPrimaryManifestProcessorImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param nodeToUpdate NodeRef
 * @param contentProps Map<QName, Serializable>
 * @return true if any content property has been updated for the needToUpdate node
 */
private boolean writeContent(NodeRef nodeToUpdate, Map<QName, Serializable> contentProps)
{
    boolean contentUpdated = false;
    File stagingDir = getStagingFolder();
    for (Map.Entry<QName, Serializable> contentEntry : contentProps.entrySet())
    {
        ContentData contentData = (ContentData) contentEntry.getValue();
        String contentUrl = contentData.getContentUrl();
        if(contentUrl == null || contentUrl.isEmpty())
        {
            log.debug("content data is null or empty:" + nodeToUpdate);
            ContentData cd = new ContentData(null, null, 0, null);
            nodeService.setProperty(nodeToUpdate, contentEntry.getKey(), cd);
            contentUpdated = true;
        }
        else
        {
            String fileName = TransferCommons.URLToPartName(contentUrl);
            File stagedFile = new File(stagingDir, fileName);
            if (!stagedFile.exists())
            {
                error(MSG_REFERENCED_CONTENT_FILE_MISSING);
            }
            ContentWriter writer = contentService.getWriter(nodeToUpdate, contentEntry.getKey(), true);
            writer.setEncoding(contentData.getEncoding());
            writer.setMimetype(contentData.getMimetype());
            writer.setLocale(contentData.getLocale());
            writer.putContent(stagedFile);
            contentUpdated = true;
        }
    }
    return contentUpdated;
}
 
Example 6
Source File: RepoPrimaryManifestProcessorImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * inject transferred
 */
private void injectTransferred(Map<QName, Serializable> props)
{       
    if(!props.containsKey(TransferModel.PROP_REPOSITORY_ID))
    {
        log.debug("injecting repositoryId property");
        props.put(TransferModel.PROP_REPOSITORY_ID, header.getRepositoryId());
    }
    props.put(TransferModel.PROP_FROM_REPOSITORY_ID, header.getRepositoryId());
    
    /**
     * For each property
     */
    List<String> contentProps = new ArrayList<String>();
    for (Serializable value : props.values())
    {
        if ((value != null) && ContentData.class.isAssignableFrom(value.getClass()))
        {
            ContentData srcContent = (ContentData)value;

            if(srcContent.getContentUrl() != null && !srcContent.getContentUrl().isEmpty())
            {
                log.debug("adding part name to from content field");
                contentProps.add(TransferCommons.URLToPartName(srcContent.getContentUrl()));
            }  
        }
    }
    
    props.put(TransferModel.PROP_FROM_CONTENT, (Serializable)contentProps);
}
 
Example 7
Source File: ContentServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private ContentReader getReader(NodeRef nodeRef, QName propertyQName, boolean fireContentReadPolicy)
{
    ContentData contentData = getContentData(nodeRef, propertyQName);

    // check that the URL is available
    if (contentData == null || contentData.getContentUrl() == null)
    {
        // there is no URL - the interface specifies that this is not an error condition
        return null;                                
    }
    String contentUrl = contentData.getContentUrl();
    
    // The context of the read is entirely described by the URL
    ContentReader reader = store.getReader(contentUrl);
    if (reader == null)
    {
        throw new AlfrescoRuntimeException("ContentStore implementations may not return null ContentReaders");
    }
    
    // set extra data on the reader
    reader.setMimetype(contentData.getMimetype());
    reader.setEncoding(contentData.getEncoding());
    reader.setLocale(contentData.getLocale());
    
    // Fire the content read policy
    if (reader != null && fireContentReadPolicy == true)
    {
        // Fire the content update policy
        Set<QName> types = new HashSet<QName>(this.nodeService.getAspects(nodeRef));
        types.add(this.nodeService.getType(nodeRef));
        OnContentReadPolicy policy = this.onContentReadDelegate.get(nodeRef, types);
        policy.onContentRead(nodeRef);
    }
    
    // we don't listen for anything
    // result may be null - but interface contract says we may return null
    return reader;
}
 
Example 8
Source File: ContentStreamIdProperty.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Serializable getValueInternal(CMISNodeInfo nodeInfo)
{
    ContentData contentData = getContentData(nodeInfo);

    if (contentData != null)
    {
        return contentData.getContentUrl();
    }
    return null;
}
 
Example 9
Source File: ContentDataDAOTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testContentUrl_FetchingOrphansNoLimit() throws Exception
{
    ContentData contentData = getContentData();
    Pair<Long, ContentData> resultPair = create(contentData);
    getAndCheck(resultPair.getFirst(), contentData);
    delete(resultPair.getFirst());
    // The content URL is orphaned
    final String contentUrlOrphaned = contentData.getContentUrl();
    final boolean[] found = new boolean[] {false}; 
    
    // Iterate over all orphaned content URLs and ensure that we hit the one we just orphaned
    ContentUrlHandler handler = new ContentUrlHandler()
    {
        public void handle(Long id, String contentUrl, Long orphanTime)
        {
            // Check
            if (id == null || contentUrl == null || orphanTime == null)
            {
                fail("Invalid orphan data returned to handler: " + id + "-" + contentUrl + "-" + orphanTime);
            }
            // Did we get the one we wanted?
            if (contentUrl.equals(contentUrlOrphaned))
            {
                found[0] = true;
            }
        }
    };
    contentDataDAO.getContentUrlsOrphaned(handler, Long.MAX_VALUE, Integer.MAX_VALUE);
    assertTrue("Newly-orphaned content URL not found", found[0]);
}
 
Example 10
Source File: DecryptingContentReaderFacade.java    From alfresco-simple-content-stores with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public ContentData getContentData()
{
    final ContentData contentData = super.getContentData();
    // correct size
    final ContentData updatedData = new ContentData(contentData.getContentUrl(), contentData.getMimetype(), this.unencryptedSize,
            contentData.getEncoding(), contentData.getLocale());
    return updatedData;
}
 
Example 11
Source File: RepoPrimaryManifestProcessorImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * This method takes all the received properties and separates them into two parts. The content properties are
 * removed from the non-content properties such that the non-content properties remain in the "props" map and the
 * content properties are returned from this method Subsequently, any properties that are to be retained from the
 * local repository are copied over into the "props" map. The result of all this is that, upon return, "props"
 * contains all the non-content properties that are to be written to the local repo, and "contentProps" contains all
 * the content properties that are to be written to the local repo.
 * 
 * @param nodeToUpdate
 *            The noderef of the existing node in the local repo that is to be updated with these properties. May be
 *            null, indicating that these properties are destined for a brand new local node.
 * @param props the new properties
 * @param existingProps the existing properties, null if this is a create
 * @return A map containing the content properties which are going to be replaced from the supplied "props" map
 */
private Map<QName, Serializable> processProperties(NodeRef nodeToUpdate, Map<QName, Serializable> props,
        Map<QName, Serializable> existingProps)
{
    Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>();
    // ...and copy any supplied content properties into this new map...
    for (Map.Entry<QName, Serializable> propEntry : props.entrySet())
    {
        Serializable value = propEntry.getValue();
        QName key = propEntry.getKey();
        if (log.isDebugEnabled())
        {
            if (value == null)
            {
                log.debug("Received a null value for property " + propEntry.getKey());
            }
        }
        if ((value != null) && ContentData.class.isAssignableFrom(value.getClass()))
        {
            if(existingProps != null)
            {
                // This is an update and we have content data 
                File stagingDir = getStagingFolder();
                ContentData contentData = (ContentData) propEntry.getValue();
                String contentUrl = contentData.getContentUrl();
                String fileName = TransferCommons.URLToPartName(contentUrl);
                File stagedFile = new File(stagingDir, fileName);
                if (stagedFile.exists())
                {
                    if(log.isDebugEnabled())
                    {
                        log.debug("replace content for node:" + nodeToUpdate + ", " + key);
                    }
                    // Yes we are going to replace the content item
                    contentProps.put(propEntry.getKey(), propEntry.getValue());
                }
                else
                {    
                    // Staging file does not exist
                    if(props.containsKey(key))
                    {
                        if(log.isDebugEnabled())
                        {
                            log.debug("keep existing content for node:" + nodeToUpdate + ", " + key);
                        }
                        // keep the existing content value
                        props.put(propEntry.getKey(), existingProps.get(key));
                    } 
                }
            }
            else
            {
                // This is a create so all content items are new
                contentProps.put(propEntry.getKey(), propEntry.getValue());
            }                  
        }
    }

    // Now we can remove the content properties from amongst the other kinds
    // of properties
    // (no removeAll on a Map...)
    for (QName contentPropertyName : contentProps.keySet())
    {
        props.remove(contentPropertyName);
    }

    if (existingProps != null)
    {
        // Finally, overlay the repo-specific properties from the existing
        // node (if there is one)
        for (QName localProperty : getLocalProperties())
        {
            Serializable existingValue = existingProps.get(localProperty);
            if (existingValue != null)
            {
                props.put(localProperty, existingValue);
            }
            else
            {
                props.remove(localProperty);
            }
        }
    }
    return contentProps;
}
 
Example 12
Source File: ImporterComponent.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Import Node Content.
 * <p>
 * The content URL, if present, will be a local URL.  This import copies the content
 * from the local URL to a server-assigned location.
 *
 * @param nodeRef containing node
 * @param propertyName the name of the content-type property
 * @param importContentData the identifier of the content to import
 */
private void importContent(NodeRef nodeRef, QName propertyName, String importContentData)
{
    ImporterContentCache contentCache = (binding == null) ? null : binding.getImportConentCache();
    
    // bind import content data description
    importContentData = bindPlaceHolder(importContentData, binding);
    if (importContentData != null && importContentData.length() > 0)
    {
        DataTypeDefinition dataTypeDef = dictionaryService.getDataType(DataTypeDefinition.CONTENT);
        ContentData contentData = (ContentData)DefaultTypeConverter.INSTANCE.convert(dataTypeDef, importContentData);
        String contentUrl = contentData.getContentUrl();
        if (contentUrl != null && contentUrl.length() > 0)
        {
            Map<QName, Serializable> propsBefore = null;
            if (contentUsageImpl != null && contentUsageImpl.getEnabled())
            {
                propsBefore = nodeService.getProperties(nodeRef);
            }

            if (contentCache != null)
            {
                // import content from source
                ContentData cachedContentData = contentCache.getContent(streamHandler, contentData);
                nodeService.setProperty(nodeRef, propertyName, cachedContentData);
            }
            else
            {
                // import the content from the import source file
                InputStream contentStream = streamHandler.importStream(contentUrl);
                ContentWriter writer = contentService.getWriter(nodeRef, propertyName, true);
                writer.setEncoding(contentData.getEncoding());
                writer.setMimetype(contentData.getMimetype());
                writer.putContent(contentStream);
            }
                                
            if (contentUsageImpl != null && contentUsageImpl.getEnabled())
            {
                // Since behaviours for content nodes have all been disabled,
                // it is necessary to update the user's usage stats.
                Map<QName, Serializable> propsAfter = nodeService.getProperties(nodeRef);
                contentUsageImpl.onUpdateProperties(nodeRef, propsBefore, propsAfter);
            }
            
            reportContentCreated(nodeRef, contentUrl);
        }
    }
}
 
Example 13
Source File: DefaultImporterContentCache.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ContentData getContent(final ImportPackageHandler handler, final ContentData sourceContentData)
{
    ContentData cachedContentData = null;
    final String sourceContentUrl = sourceContentData.getContentUrl();

    contentUrlsLock.readLock().lock();
    
    try
    {
        cachedContentData = contentUrls.get(sourceContentUrl);
        if (cachedContentData == null)
        {
            contentUrlsLock.readLock().unlock();
            contentUrlsLock.writeLock().lock();
            
            try
            {
                cachedContentData = contentUrls.get(sourceContentUrl);
                if (cachedContentData == null)
                {
                    cachedContentData = TenantUtil.runAsTenant(new TenantRunAsWork<ContentData>()
                    {
                        @Override
                        public ContentData doWork() throws Exception
                        {
                            InputStream contentStream = handler.importStream(sourceContentUrl);
                            ContentWriter writer = contentService.getWriter(null, null, false);
                            writer.setEncoding(sourceContentData.getEncoding());
                            writer.setMimetype(sourceContentData.getMimetype());
                            writer.putContent(contentStream);
                            return writer.getContentData();
                        }
                    }, TenantService.DEFAULT_DOMAIN);
                    
                    contentUrls.put(sourceContentUrl, cachedContentData);
                }
            }
            finally
            {
                contentUrlsLock.readLock().lock();
                contentUrlsLock.writeLock().unlock();
            }
        }
    }
    finally
    {
        contentUrlsLock.readLock().unlock();
    }
    
    if (logger.isDebugEnabled())
        logger.debug("Mapped contentUrl " + sourceContentUrl + " to " + cachedContentData);
        
    return cachedContentData;
}
 
Example 14
Source File: AbstractContentDataDAOImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Translates the {@link ContentData} into persistable values using the helper DAOs
 */
protected ContentDataEntity createContentDataEntity(ContentData contentData)
{
    // Resolve the content URL
    Long contentUrlId = null;
    String contentUrl = contentData.getContentUrl();
    long size = contentData.getSize();
    if (contentUrl != null)
    {
        ContentUrlEntity contentUrlEntity = new ContentUrlEntity();
        contentUrlEntity.setContentUrl(contentUrl);
        contentUrlEntity.setSize(size);
        Pair<Long, ContentUrlEntity> pair = contentUrlCache.createOrGetByValue(contentUrlEntity, controlDAO);
        contentUrlId = pair.getFirst();
    }

    // Resolve the mimetype
    Long mimetypeId = null;
    String mimetype = contentData.getMimetype();
    if (mimetype != null)
    {
        mimetypeId = mimetypeDAO.getOrCreateMimetype(mimetype).getFirst();
    }
    // Resolve the encoding
    Long encodingId = null;
    String encoding = contentData.getEncoding();
    if (encoding != null)
    {
        encodingId = encodingDAO.getOrCreateEncoding(encoding).getFirst();
    }
    // Resolve the locale
    Long localeId = null;
    Locale locale = contentData.getLocale();
    if (locale != null)
    {
        localeId = localeDAO.getOrCreateLocalePair(locale).getFirst();
    }
    
    // Create ContentDataEntity
    ContentDataEntity contentDataEntity = createContentDataEntity(contentUrlId, mimetypeId, encodingId, localeId);
    // Done
    return contentDataEntity;
}
 
Example 15
Source File: AbstractContentDataDAOImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Translates the {@link ContentData} into persistable values using the helper DAOs
 */
protected int updateContentDataEntity(ContentDataEntity contentDataEntity, ContentData contentData)
{
    // Resolve the content URL
    Long oldContentUrlId = contentDataEntity.getContentUrlId();
    ContentUrlEntity contentUrlEntity = null;
    if(oldContentUrlId != null)
    {
        Pair<Long, ContentUrlEntity> entityPair = contentUrlCache.getByKey(oldContentUrlId);
        if (entityPair == null)
        {
            throw new DataIntegrityViolationException("No ContentUrl value exists for ID " + oldContentUrlId);
        }
        contentUrlEntity = entityPair.getSecond();
    }

    String oldContentUrl = (contentUrlEntity != null ? contentUrlEntity.getContentUrl() : null);
    String newContentUrl = contentData.getContentUrl();
    if (!EqualsHelper.nullSafeEquals(oldContentUrl, newContentUrl))
    {
        if (oldContentUrl != null)
        {
            // We have a changed value.  The old content URL has been dereferenced.
            registerDereferencedContentUrl(oldContentUrl);
        }
        if (newContentUrl != null)
        {
            if(contentUrlEntity == null)
            {
                contentUrlEntity = new ContentUrlEntity();
                contentUrlEntity.setContentUrl(newContentUrl);
            }
            Pair<Long, ContentUrlEntity> pair = contentUrlCache.getOrCreateByValue(contentUrlEntity);
            Long newContentUrlId = pair.getFirst();
            contentUrlEntity.setId(newContentUrlId);
            contentDataEntity.setContentUrlId(newContentUrlId);
        }
        else
        {
            contentDataEntity.setId(null);
            contentDataEntity.setContentUrlId(null);
        }
    }

    // Resolve the mimetype
    Long mimetypeId = null;
    String mimetype = contentData.getMimetype();
    if (mimetype != null)
    {
        mimetypeId = mimetypeDAO.getOrCreateMimetype(mimetype).getFirst();
    }
    // Resolve the encoding
    Long encodingId = null;
    String encoding = contentData.getEncoding();
    if (encoding != null)
    {
        encodingId = encodingDAO.getOrCreateEncoding(encoding).getFirst();
    }
    // Resolve the locale
    Long localeId = null;
    Locale locale = contentData.getLocale();
    if (locale != null)
    {
        localeId = localeDAO.getOrCreateLocalePair(locale).getFirst();
    }

    contentDataEntity.setMimetypeId(mimetypeId);
    contentDataEntity.setEncodingId(encodingId);
    contentDataEntity.setLocaleId(localeId);

    return updateContentDataEntity(contentDataEntity);
}