001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.tags;
020
021import org.apache.log4j.Logger;
022import org.apache.wiki.api.core.Attachment;
023import org.apache.wiki.api.core.Context;
024import org.apache.wiki.api.core.Engine;
025import org.apache.wiki.api.core.Page;
026import org.apache.wiki.api.exceptions.ProviderException;
027import org.apache.wiki.attachment.AttachmentManager;
028import org.apache.wiki.pages.PageManager;
029
030import javax.servlet.jsp.JspWriter;
031import javax.servlet.jsp.PageContext;
032import java.io.IOException;
033import java.util.List;
034
035
036/**
037 *  Iterates through the list of attachments one has.
038 *
039 *  <P><B>Attributes</B></P>
040 *  <UL>
041 *    <LI>page - Page name to refer to.  Default is the current page.
042 *  </UL>
043 *
044 *  @since 2.0
045 */
046// FIXME: Too much in common with IteratorTag - REFACTOR
047public class AttachmentsIteratorTag extends IteratorTag {
048    private static final long serialVersionUID = 0L;
049    
050    private static final Logger log = Logger.getLogger( AttachmentsIteratorTag.class );
051
052    /**
053     *  {@inheritDoc}
054     */
055    @Override
056    public final int doStartTag()  {
057        m_wikiContext = (Context) pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
058        final Engine engine = m_wikiContext.getEngine();
059        final AttachmentManager mgr = engine.getManager( AttachmentManager.class );
060        final Page page;
061
062        page = m_wikiContext.getPage();
063
064        if( !mgr.attachmentsEnabled() )
065        {
066            return SKIP_BODY;
067        }
068
069        try {
070            if( page != null && engine.getManager( PageManager.class ).wikiPageExists(page) ) {
071                final List< Attachment > atts = mgr.listAttachments( page );
072
073                if( atts == null ) {
074                    log.debug("No attachments to display.");
075                    // There are no attachments included
076                    return SKIP_BODY;
077                }
078
079                m_iterator = atts.iterator();
080
081                if( m_iterator.hasNext() ) {
082                    final Attachment  att = (Attachment) m_iterator.next();
083                    final Context context = m_wikiContext.clone();
084                    context.setPage( att );
085                    pageContext.setAttribute( Context.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE );
086                    pageContext.setAttribute( getId(), att );
087                } else {
088                    return SKIP_BODY;
089                }
090            } else {
091                return SKIP_BODY;
092            }
093
094            return EVAL_BODY_BUFFERED;
095        } catch( final ProviderException e ) {
096            log.fatal("Provider failed while trying to iterator through history",e);
097            // FIXME: THrow something.
098        }
099
100        return SKIP_BODY;
101    }
102
103    /**
104     *  {@inheritDoc}
105     */
106    @Override
107    public final int doAfterBody() {
108        if( bodyContent != null ) {
109            try {
110                final JspWriter out = getPreviousOut();
111                out.print(bodyContent.getString());
112                bodyContent.clearBody();
113            } catch( final IOException e ) {
114                log.error("Unable to get inner tag text", e);
115                // FIXME: throw something?
116            }
117        }
118
119        if( m_iterator != null && m_iterator.hasNext() ) {
120            final Attachment att = ( Attachment )m_iterator.next();
121            final Context context = m_wikiContext.clone();
122            context.setPage( att );
123            pageContext.setAttribute( Context.ATTR_CONTEXT,  context, PageContext.REQUEST_SCOPE );
124            pageContext.setAttribute( getId(), att );
125
126            return EVAL_BODY_BUFFERED;
127        }
128
129        return SKIP_BODY;
130    }
131
132}