001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.bridges.freemarker;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.template.Template;
021    import com.liferay.portal.kernel.template.TemplateConstants;
022    import com.liferay.portal.kernel.template.TemplateException;
023    import com.liferay.portal.kernel.template.TemplateManagerUtil;
024    import com.liferay.portal.kernel.template.TemplateResource;
025    import com.liferay.portal.kernel.template.TemplateResourceLoaderUtil;
026    import com.liferay.portal.kernel.template.TemplateTaglibSupportProvider;
027    import com.liferay.portal.kernel.util.ServiceLoader;
028    import com.liferay.portal.kernel.util.UnsyncPrintWriterPool;
029    import com.liferay.util.bridges.mvc.MVCPortlet;
030    
031    import java.io.IOException;
032    import java.io.Writer;
033    
034    import java.util.List;
035    
036    import javax.portlet.MimeResponse;
037    import javax.portlet.PortletContext;
038    import javax.portlet.PortletException;
039    import javax.portlet.PortletRequest;
040    import javax.portlet.PortletResponse;
041    
042    /**
043     * @author Raymond Aug??
044     */
045    public class FreeMarkerPortlet extends MVCPortlet {
046    
047            @Override
048            public void destroy() {
049                    super.destroy();
050    
051                    Class<?> clazz = getClass();
052    
053                    TemplateManagerUtil.destroy(clazz.getClassLoader());
054            }
055    
056            protected TemplateTaglibSupportProvider getTaglibSupportProvider()
057                    throws Exception {
058    
059                    if (_templateTaglibSupportProvider != null) {
060                            return _templateTaglibSupportProvider;
061                    }
062    
063                    List<TemplateTaglibSupportProvider> templateTaglibSupportProviders =
064                            ServiceLoader.load(TemplateTaglibSupportProvider.class);
065    
066                    if (!templateTaglibSupportProviders.isEmpty()) {
067                            _templateTaglibSupportProvider = templateTaglibSupportProviders.get(
068                                    0);
069                    }
070    
071                    return _templateTaglibSupportProvider;
072            }
073    
074            @Override
075            protected void include(
076                            String path, PortletRequest portletRequest,
077                            PortletResponse portletResponse, String lifecycle)
078                    throws IOException, PortletException {
079    
080                    PortletContext portletContext = getPortletContext();
081    
082                    String servletContextName = portletContext.getPortletContextName();
083    
084                    String resourcePath = servletContextName.concat(
085                            TemplateConstants.SERVLET_SEPARATOR).concat(path);
086    
087                    boolean resourceExists = false;
088    
089                    try {
090                            resourceExists = TemplateResourceLoaderUtil.hasTemplateResource(
091                                    TemplateConstants.LANG_TYPE_FTL, resourcePath);
092                    }
093                    catch (TemplateException te) {
094                            throw new IOException(te.getMessage());
095                    }
096    
097                    if (!resourceExists) {
098                            _log.error(path + " is not a valid include");
099                    }
100                    else {
101                            try {
102                                    TemplateResource templateResource =
103                                            TemplateResourceLoaderUtil.getTemplateResource(
104                                                    TemplateConstants.LANG_TYPE_FTL, resourcePath);
105    
106                                    Template template = TemplateManagerUtil.getTemplate(
107                                            TemplateConstants.LANG_TYPE_FTL, templateResource, false);
108    
109                                    TemplateTaglibSupportProvider templateTaglibSupportProvider =
110                                            getTaglibSupportProvider();
111    
112                                    if (templateTaglibSupportProvider != null) {
113                                            templateTaglibSupportProvider.addTaglibSupport(
114                                                    template, servletContextName, portletRequest,
115                                                    portletResponse);
116                                    }
117    
118                                    template.put("portletContext", getPortletContext());
119                                    template.put(
120                                            "userInfo",
121                                            portletRequest.getAttribute(PortletRequest.USER_INFO));
122    
123                                    Writer writer = null;
124    
125                                    if (portletResponse instanceof MimeResponse) {
126                                            MimeResponse mimeResponse = (MimeResponse)portletResponse;
127    
128                                            writer = UnsyncPrintWriterPool.borrow(
129                                                    mimeResponse.getWriter());
130                                    }
131                                    else {
132                                            writer = new UnsyncStringWriter();
133                                    }
134    
135                                    template.processTemplate(writer);
136                            }
137                            catch (Exception e) {
138                                    throw new PortletException(e);
139                            }
140                    }
141    
142                    if (clearRequestParameters) {
143                            if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
144                                    portletResponse.setProperty("clear-request-parameters", "true");
145                            }
146                    }
147            }
148    
149            private static Log _log = LogFactoryUtil.getLog(FreeMarkerPortlet.class);
150    
151            private TemplateTaglibSupportProvider _templateTaglibSupportProvider;
152    
153    }