001    /**
002     * Copyright (c) 2000-present 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.portal.model.impl;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.WebKeys;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.model.LayoutTypeController;
023    
024    import java.util.Collection;
025    import java.util.Locale;
026    import java.util.Map;
027    
028    import javax.servlet.RequestDispatcher;
029    import javax.servlet.ServletContext;
030    import javax.servlet.ServletResponse;
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    
034    /**
035     * @author Eudaldo Alonso
036     */
037    public abstract class BaseLayoutTypeControllerImpl
038            implements LayoutTypeController {
039    
040            @Override
041            public String[] getConfigurationActionDelete() {
042                    return StringPool.EMPTY_ARRAY;
043            }
044    
045            @Override
046            public String[] getConfigurationActionUpdate() {
047                    return StringPool.EMPTY_ARRAY;
048            }
049    
050            @Override
051            public String getType() {
052                    return StringPool.BLANK;
053            }
054    
055            @Override
056            public String includeEditContent(
057                            HttpServletRequest request, HttpServletResponse response,
058                            Layout layout)
059                    throws Exception {
060    
061                    RequestDispatcher requestDispatcher =
062                            servletContext.getRequestDispatcher(getEditPage());
063    
064                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
065    
066                    ServletResponse servletResponse = createServletResponse(
067                            response, unsyncStringWriter);
068    
069                    try {
070                            addAttributes(request);
071    
072                            requestDispatcher.include(request, servletResponse);
073                    }
074                    finally {
075                            removeAttributes(request);
076                    }
077    
078                    return unsyncStringWriter.toString();
079            }
080    
081            @Override
082            public boolean includeLayoutContent(
083                            HttpServletRequest request, HttpServletResponse response,
084                            Layout layout)
085                    throws Exception {
086    
087                    RequestDispatcher requestDispatcher =
088                            servletContext.getRequestDispatcher(getViewPage());
089    
090                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
091    
092                    ServletResponse servletResponse = createServletResponse(
093                            response, unsyncStringWriter);
094    
095                    String contentType = servletResponse.getContentType();
096    
097                    try {
098                            addAttributes(request);
099    
100                            requestDispatcher.include(request, servletResponse);
101                    }
102                    finally {
103                            removeAttributes(request);
104                    }
105    
106                    if (contentType != null) {
107                            response.setContentType(contentType);
108                    }
109    
110                    request.setAttribute(
111                            WebKeys.LAYOUT_CONTENT, unsyncStringWriter.getStringBundler());
112    
113                    return false;
114            }
115    
116            @Override
117            public boolean isBrowsable() {
118                    return true;
119            }
120    
121            @Override
122            public boolean isCheckLayoutViewPermission() {
123                    return true;
124            }
125    
126            @Override
127            public boolean isFullPageDisplayable() {
128                    return false;
129            }
130    
131            @Override
132            public boolean isInstanceable() {
133                    return true;
134            }
135    
136            @Override
137            public boolean matches(
138                    HttpServletRequest request, String friendlyURL, Layout layout) {
139    
140                    try {
141                            Map<Locale, String> friendlyURLMap = layout.getFriendlyURLMap();
142    
143                            Collection<String> values = friendlyURLMap.values();
144    
145                            return values.contains(friendlyURL);
146                    }
147                    catch (SystemException se) {
148                            throw new RuntimeException(se);
149                    }
150            }
151    
152            protected void addAttributes(HttpServletRequest request) {
153            }
154    
155            protected abstract ServletResponse createServletResponse(
156                    HttpServletResponse response, UnsyncStringWriter unsyncStringWriter);
157    
158            protected abstract String getEditPage();
159    
160            protected abstract String getViewPage();
161    
162            protected void removeAttributes(HttpServletRequest request) {
163            }
164    
165            protected ServletContext servletContext;
166    
167    }