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.exceptions.ProviderException;
024import org.apache.wiki.ui.TemplateManager;
025import org.apache.wiki.util.TextUtil;
026
027import javax.servlet.ServletException;
028import javax.servlet.jsp.JspException;
029import java.io.IOException;
030
031/**
032 *  Includes an another JSP page, making sure that we actually pass the WikiContext correctly.
033 *
034 *  @since 2.0
035 */
036// FIXME: Perhaps unnecessary?
037public class IncludeTag extends WikiTagBase {
038
039    private static final long serialVersionUID = 0L;
040    private static final Logger log = LogManager.getLogger( IncludeTag.class );
041    
042    protected String m_page;
043
044    @Override
045    public void initTag() {
046        super.initTag();
047        m_page = null;
048    }
049
050    public void setPage( final String page )
051    {
052        m_page = page;
053    }
054
055    public String getPage()
056    {
057        return m_page;
058    }
059
060    @Override
061    public final int doWikiStartTag() throws IOException, ProviderException {
062        return SKIP_BODY;
063    }
064
065    @Override
066    public final int doEndTag() throws JspException {
067        try {
068            final String page = m_wikiContext.getEngine().getManager( TemplateManager.class ).findJSP( pageContext,
069                                                                                                 m_wikiContext.getTemplate(),
070                                                                                                 m_page );
071
072            if( page == null ) {
073                pageContext.getOut().println( "No template file called '" + TextUtil.replaceEntities( m_page ) + "'" );
074            } else {
075                pageContext.include( page );
076            }
077        } catch( final ServletException e ) {
078            log.warn( "Including failed, got a servlet exception from sub-page. Rethrowing the exception to the JSP engine.", e );
079            throw new JspException( e.getMessage() );
080        } catch( final IOException e ) {
081            log.warn( "I/O exception - probably the connection was broken. Rethrowing the exception to the JSP engine.", e );
082            throw new JspException( e.getMessage() );
083        }
084
085        return EVAL_PAGE;
086    }
087
088}