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.logging.log4j.LogManager; 022import org.apache.logging.log4j.Logger; 023import org.apache.wiki.api.core.Attachment; 024import org.apache.wiki.api.core.Context; 025import org.apache.wiki.api.core.Engine; 026import org.apache.wiki.api.core.Page; 027import org.apache.wiki.api.exceptions.ProviderException; 028import org.apache.wiki.attachment.AttachmentManager; 029import org.apache.wiki.pages.PageManager; 030 031import javax.servlet.jsp.JspWriter; 032import javax.servlet.jsp.PageContext; 033import java.io.IOException; 034import java.util.List; 035 036 037/** 038 * Iterates through the list of attachments one has. 039 * 040 * <P><B>Attributes</B></P> 041 * <UL> 042 * <LI>page - Page name to refer to. Default is the current page. 043 * </UL> 044 * 045 * @since 2.0 046 */ 047// FIXME: Too much in common with IteratorTag - REFACTOR 048public class AttachmentsIteratorTag extends IteratorTag { 049 private static final long serialVersionUID = 0L; 050 051 private static final Logger log = LogManager.getLogger( AttachmentsIteratorTag.class ); 052 053 /** 054 * {@inheritDoc} 055 */ 056 @Override 057 public final int doStartTag() { 058 m_wikiContext = (Context) pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE ); 059 final Engine engine = m_wikiContext.getEngine(); 060 final AttachmentManager mgr = engine.getManager( AttachmentManager.class ); 061 final Page page; 062 063 page = m_wikiContext.getPage(); 064 065 if( !mgr.attachmentsEnabled() ) 066 { 067 return SKIP_BODY; 068 } 069 070 try { 071 if( page != null && engine.getManager( PageManager.class ).wikiPageExists(page) ) { 072 final List< Attachment > atts = mgr.listAttachments( page ); 073 074 if( atts == null ) { 075 log.debug("No attachments to display."); 076 // There are no attachments included 077 return SKIP_BODY; 078 } 079 080 m_iterator = atts.iterator(); 081 082 if( m_iterator.hasNext() ) { 083 final Attachment att = (Attachment) m_iterator.next(); 084 final Context context = m_wikiContext.clone(); 085 context.setPage( att ); 086 pageContext.setAttribute( Context.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE ); 087 pageContext.setAttribute( getId(), att ); 088 } else { 089 return SKIP_BODY; 090 } 091 } else { 092 return SKIP_BODY; 093 } 094 095 return EVAL_BODY_BUFFERED; 096 } catch( final ProviderException e ) { 097 log.fatal("Provider failed while trying to iterator through history",e); 098 // FIXME: THrow something. 099 } 100 101 return SKIP_BODY; 102 } 103 104 /** 105 * {@inheritDoc} 106 */ 107 @Override 108 public final int doAfterBody() { 109 if( bodyContent != null ) { 110 try { 111 final JspWriter out = getPreviousOut(); 112 out.print(bodyContent.getString()); 113 bodyContent.clearBody(); 114 } catch( final IOException e ) { 115 log.error("Unable to get inner tag text", e); 116 // FIXME: throw something? 117 } 118 } 119 120 if( m_iterator != null && m_iterator.hasNext() ) { 121 final Attachment att = ( Attachment )m_iterator.next(); 122 final Context context = m_wikiContext.clone(); 123 context.setPage( att ); 124 pageContext.setAttribute( Context.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE ); 125 pageContext.setAttribute( getId(), att ); 126 127 return EVAL_BODY_BUFFERED; 128 } 129 130 return SKIP_BODY; 131 } 132 133}