All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.randombits.supplier.confluence.content.AbstractContentSupplier Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
/*
 * Copyright (c) 2007, CustomWare Asia Pacific
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of "CustomWare Asia Pacific" nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package org.randombits.supplier.confluence.content;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.randombits.supplier.confluence.AbstractConfluenceSupplier;
import org.randombits.supplier.core.annotate.KeyValue;
import org.randombits.supplier.core.annotate.KeyWeight;
import org.randombits.supplier.core.annotate.SupplierKey;
import org.randombits.utils.lang.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.atlassian.confluence.core.BodyContent;
import com.atlassian.confluence.core.BodyType;
import com.atlassian.confluence.core.ContentEntityManager;
import com.atlassian.confluence.core.ContentEntityObject;
import com.atlassian.confluence.core.ContentPropertyManager;
import com.atlassian.confluence.core.VersionHistorySummary;
import com.atlassian.confluence.labels.Label;
import com.atlassian.confluence.links.OutgoingLink;
import com.atlassian.confluence.pages.Attachment;
import com.atlassian.confluence.pages.AttachmentManager;
import com.atlassian.confluence.search.v2.SearchResult;
import com.atlassian.confluence.security.Permission;
import com.atlassian.confluence.util.ExcerptHelper;
import com.atlassian.renderer.links.Link;
import com.atlassian.renderer.links.UnpermittedLink;
import com.atlassian.renderer.links.UnresolvedLink;
import com.atlassian.spring.container.ContainerManager;
import com.atlassian.user.User;

/**
 * Looks up values against standard Confluence properties.
 *
 * @author David Peterson
 */
public abstract class AbstractContentSupplier extends AbstractEntitySupplier {

    private static final String MAILTO_PREFIX = "mailto:";
 
    private static final Logger LOG = Logger.getLogger( AbstractContentSupplier.class );

    private ContentPropertyManager contentPropertyManager;

    private AttachmentManager attachmentManager;

    private ContentEntityManager contentEntityManager;
    
    private static final ExcerptHelper excerptHelper =  new ExcerptHelper();
    
    static {  
    	ContainerManager.autowireComponent(excerptHelper); 
    }
  
    /**
     * Constructs an annotated supplier.
     */
    public AbstractContentSupplier() {
    }

    @SupplierKey("version comment")
    @KeyWeight(5)
    @API("1.0.0")
    public String getVersionComment( @KeyValue SearchResult result ) {
        return result.getLastUpdateDescription();
    }

    @SupplierKey("version comment")
    @API("1.0.0")
    public String getVersionComment( @KeyValue T content ) {
        return content.getVersionComment();
    }

    @SupplierKey("attachments")
    @API("1.0.0")
    public List getAttachments( @KeyValue T content ) {
        return getAttachmentManager().getLatestVersionsOfAttachments( content );
    }

    @SupplierKey("body content")
    @API("1.0.0")
    public BodyContent getBodyContent( @KeyValue T content ) {
        return content.getBodyContent();
    }

    @SupplierKey("body")
    @API("1.0.0")
    public BodyContent getBody( @KeyValue T content ) {
        return content.getBodyContent();
    }

    @SupplierKey("images")
    @API("1.0.0")
    public List getImages( @KeyValue T content ) {
        List attachments = getAttachmentManager().getLatestVersionsOfAttachments( content );
        List images = new java.util.ArrayList();

        for ( Attachment attachment : attachments ) {
            if ( attachment.getContentType().startsWith( "image/" ) )
                images.add( attachment );
        }

        return images;
    }

    @SupplierKey("version history")
    @API("1.0.0")
    public Iterator getVersionHistory( @KeyValue T content ) {
        List history = getContentEntityManager().getVersionHistorySummaries( content );

        if ( history != null ) {
            //noinspection unchecked
            return (Iterator) IteratorUtils.transformedIterator( history.iterator(), new Transformer() {

                public Object transform( Object object ) {
                    return getContentEntityManager().getById( ( (VersionHistorySummary) object ).getId() );
                }
            } );
        }
        return null;
    }

    @Autowired
    public void setContentPropertyManager( ContentPropertyManager contentPropertyManager ) {
        this.contentPropertyManager = contentPropertyManager;
    }

    private ContentPropertyManager getContentPropertyManager() {
        return contentPropertyManager;
    }

    @Autowired
    public void setContentEntityManager( @Qualifier("contentEntityManager") ContentEntityManager contentEntityManager ) {
        this.contentEntityManager = contentEntityManager;
    }

    private ContentEntityManager getContentEntityManager() {
        return contentEntityManager;
    }

    @SupplierKey("summary")
    @API("1.0.0")
    public String getSummary( @KeyValue T content ) {
        String excerpt = getExcerpt( content );
        if ( StringUtils.isBlank( excerpt ) ) {
            excerpt = createSummary( content.getBodyContent(), 200 );
        }

        return excerpt;
    }

    private String createSummary( BodyContent bodyContent, int maxLength ) {
        if ( bodyContent != null ) {
            BodyType type = bodyContent.getBodyType();
            if ( type == BodyType.WIKI )
                return createWikiSummary( bodyContent.getBody(), maxLength );
            // TODO: Add support for 'XHTML' format summaries.
        }
        return null;
    }

    // Matches {macros...}, {noformat} and {code} content from wiki markup.
    private static final Pattern STRIP_WIKI_PATTERN = Pattern.compile(
            "(? maxLength ) {
                // Grab the last complete word the maximum length.
                value = value.substring( 0, maxLength );
                Matcher matcher = SUMMARY_PATTERN.matcher( value.substring( 0, maxLength ) );
                if ( matcher.matches() ) {
                    value = matcher.group( 1 );
                } else {
                    value = value.substring( 0, maxLength );
                }
            }
            // Add ellipses if the summary does not match the original content.
            if ( value.length() < content.length() )
                value = value.trim() + "...";
            return value;
        }
        return null;
    }

    @Autowired
    public void setAttachmentManager( AttachmentManager attachmentManager ) {
        this.attachmentManager = attachmentManager;
    }

    private AttachmentManager getAttachmentManager() {
        return attachmentManager;
    }
      
    @SupplierKey("outgoing links")
    @API("1.0.0")
    public Set getOutgoingLinks( @KeyValue T content ) {
        if ( content != null ) {
            @SuppressWarnings("unchecked")
            List outgoingLinks = content.getOutgoingLinks();

            Set uniqueLinks = new HashSet();
            Link link;
            String linkText;

            for ( OutgoingLink outLink : outgoingLinks ) {

                String destPageTitle = outLink.getDestinationPageTitle();
                if ( destPageTitle != null && destPageTitle.startsWith( MAILTO_PREFIX ) )
                    linkText = outLink.getDestinationPageTitle();
                else
                    linkText = outLink.getDestinationSpaceKey() + ":"
                            + ( destPageTitle == null ? "" : destPageTitle );

                if ( outLink.getLinkTitle() != null )
                    linkText = outLink.getLinkTitle() + "|" + linkText;

                LOG.debug( "resolving outgoing link: '" + linkText + "'" );
                link = getLinkResolver().createLink( content.toPageContext(), linkText );

                if ( link != null && !( link instanceof UnpermittedLink ) && !( link instanceof UnresolvedLink ) ) {
                    LOG.debug( "link found: " + link );

                    uniqueLinks.add( link );
                }
            }

            return uniqueLinks;
        } else {
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    @SupplierKey("labels")
    @API("1.0.0")
    public Collection




© 2015 - 2024 Weber Informatics LLC | Privacy Policy