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.ContextEnum; 024import org.apache.wiki.api.exceptions.ProviderException; 025import org.apache.wiki.ui.TemplateManager; 026 027import javax.servlet.ServletException; 028import javax.servlet.jsp.JspException; 029import java.io.IOException; 030import java.util.HashMap; 031import java.util.Map; 032 033 034/** 035 * Is used as a "super include" tag, which can include the proper context 036 * based on the wikicontext. 037 * 038 * @since 2.2 039 */ 040public class ContentTag extends WikiTagBase { 041 042 private static final long serialVersionUID = 0L; 043 private static final Logger LOG = LogManager.getLogger( ContentTag.class ); 044 045 private final Map<String, String> m_mappings = new HashMap<>(); 046 047 /** 048 * Set the template for the VIEW context. 049 * 050 * @param s The template name. 051 */ 052 public void setView( final String s ) 053 { 054 m_mappings.put( ContextEnum.PAGE_VIEW.getRequestContext(), s ); 055 } 056 057 /** 058 * Set the template for the DIFF context. 059 * 060 * @param s The template name. 061 */ 062 public void setDiff( final String s ) 063 { 064 m_mappings.put( ContextEnum.PAGE_DIFF.getRequestContext(), s ); 065 } 066 067 /** 068 * Set the template for the INFO context. 069 * 070 * @param s The template name. 071 */ 072 public void setInfo( final String s ) 073 { 074 m_mappings.put( ContextEnum.PAGE_INFO.getRequestContext(), s ); 075 } 076 077 /** 078 * Set the template for the PREVIEW context. 079 * 080 * @param s The template name. 081 */ 082 public void setPreview( final String s ) 083 { 084 m_mappings.put( ContextEnum.PAGE_PREVIEW.getRequestContext(), s ); 085 } 086 087 /** 088 * Set the template for the CONFLICT context. 089 * 090 * @param s The template name. 091 */ 092 public void setConflict( final String s ) 093 { 094 m_mappings.put( ContextEnum.PAGE_CONFLICT.getRequestContext(), s ); 095 } 096 097 /** 098 * Set the template for the FIND context. 099 * 100 * @param s The template name. 101 */ 102 public void setFind( final String s ) 103 { 104 m_mappings.put( ContextEnum.WIKI_FIND.getRequestContext(), s ); 105 } 106 107 /** 108 * Set the template for the PREFS context. 109 * 110 * @param s The template name. 111 */ 112 public void setPrefs( final String s ) 113 { 114 m_mappings.put( ContextEnum.WIKI_PREFS.getRequestContext(), s ); 115 } 116 117 /** 118 * Set the template for the ERROR context. 119 * 120 * @param s The template name. 121 */ 122 public void setError( final String s ) 123 { 124 m_mappings.put( ContextEnum.WIKI_ERROR.getRequestContext(), s ); 125 } 126 127 /** 128 * Set the template for the EDIT context. 129 * 130 * @param s The template name. 131 */ 132 public void setEdit( final String s ) 133 { 134 m_mappings.put( ContextEnum.PAGE_EDIT.getRequestContext(), s ); 135 } 136 137 /** 138 * Set the template for the COMMENT context. 139 * 140 * @param s The template name. 141 */ 142 public void setComment( final String s ) 143 { 144 m_mappings.put( ContextEnum.PAGE_COMMENT.getRequestContext(), s ); 145 } 146 147 /** 148 * {@inheritDoc} 149 */ 150 @Override public final int doWikiStartTag() 151 throws IOException, 152 ProviderException 153 { 154 return SKIP_BODY; 155 } 156 157 /** 158 * {@inheritDoc} 159 */ 160 @Override public final int doEndTag() 161 throws JspException 162 { 163 try 164 { 165 // Check the overridden templates first 166 final String requestContext = m_wikiContext.getRequestContext(); 167 String contentTemplate = m_mappings.get( requestContext ); 168 169 // If not found, use the defaults 170 if( contentTemplate == null ) { 171 contentTemplate = m_wikiContext.getContentTemplate(); 172 } 173 174 // If still no, something fishy is going on 175 if( contentTemplate == null ) { 176 throw new JspException( "This template uses <wiki:Content/> in an unsupported context: " + requestContext ); 177 } 178 179 final String page = m_wikiContext.getEngine().getManager( TemplateManager.class ).findJSP( pageContext, 180 m_wikiContext.getTemplate(), 181 contentTemplate ); 182 pageContext.include( page ); 183 } 184 catch( final ServletException e ) 185 { 186 LOG.warn( "Including failed, got a servlet exception from sub-page. "+ 187 "Rethrowing the exception to the JSP engine.", e ); 188 throw new JspException( e.getMessage() ); 189 } 190 catch( final IOException e ) 191 { 192 LOG.warn( "I/O exception - probably the connection was broken. "+ 193 "Rethrowing the exception to the JSP engine.", e ); 194 throw new JspException( e.getMessage() ); 195 } 196 197 return EVAL_PAGE; 198 } 199}