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