001    /*
002     * ----------------------------------------------------------------------
003     * Copyright (C) 2009 Enrique Lara (k957@68k.org)
004     *
005     * TinLizard is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public License
007     * as published by the Free Software Foundation; either version 3.0
008     * of the License, or (at your option) any later version.
009     *
010     * TinLizard is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013     * GNU Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public License
016     * along with TinLizard. If not, see http://www.gnu.org/licenses/.
017     * ----------------------------------------------------------------------
018     */
019    package tinlizard.web;
020    
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.http.HttpServletResponse;
023    import javax.servlet.jsp.JspException;
024    import javax.servlet.jsp.tagext.TagSupport;
025    
026    import org.codehaus.plexus.util.StringUtils;
027    
028    /**
029     * Tag Lib to insert custom content.
030     */
031    public final class MashInTag extends TagSupport {
032        private static final long serialVersionUID = 1L;
033        private Object it;
034        private String template = null;
035    
036        public void setIt(final Object it) {
037            this.it = it;
038        }
039    
040        public void setTemplate(final String template) {
041            this.template = template;
042        }
043    
044        public int doStartTag() {
045            HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
046            HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
047    
048            //XXX default to "it" defined by stapler if not set.
049            if (StringUtils.isNotBlank(template)) {
050                String username = null;
051    
052                if (request.getUserPrincipal() != null) {
053                    username = request.getUserPrincipal().getName();
054                }
055    
056                TinLizardWeb.getInstance().applyTemplate(it, template, pageContext.getOut(), response.getLocale(), username);
057            }
058    
059            return SKIP_BODY;
060        }
061    
062        public int doEndTag() throws JspException {
063            release();
064    
065            return EVAL_PAGE;
066        }
067    }