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.portlet;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    
020    import java.util.Locale;
021    
022    import javax.portlet.PortletRequest;
023    import javax.portlet.PortletURL;
024    import javax.portlet.ResourceRequest;
025    import javax.portlet.ResourceResponse;
026    import javax.portlet.ResourceURL;
027    
028    import javax.servlet.http.Cookie;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class ResourceResponseImpl
035            extends MimeResponseImpl implements ResourceResponse {
036    
037            @Override
038            public void addDateHeader(String name, long date) {
039                    _response.addDateHeader(name, date);
040            }
041    
042            @Override
043            public void addHeader(String name, String value) {
044                    _response.addHeader(name, value);
045            }
046    
047            @Override
048            public void addIntHeader(String name, int value) {
049                    _response.addIntHeader(name, value);
050            }
051    
052            @Override
053            public void addProperty(Cookie cookie) {
054                    _response.addCookie(cookie);
055            }
056    
057            @Override
058            public PortletURL createActionURL() {
059                    return super.createActionURL();
060            }
061    
062            @Override
063            public LiferayPortletURL createLiferayPortletURL(
064                    String portletName, String lifecycle) {
065    
066                    ResourceRequest resourceRequest = (ResourceRequest)getPortletRequest();
067    
068                    String cacheability = resourceRequest.getCacheability();
069    
070                    if (cacheability.equals(ResourceURL.PAGE)) {
071                    }
072                    else if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
073                            throw new IllegalStateException(
074                                    "Unable to create an action URL from a resource response " +
075                                            "when the cacheability is not set to PAGE");
076                    }
077                    else if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
078                            throw new IllegalStateException(
079                                    "Unable to create a render URL from a resource response when " +
080                                            "the cacheability is not set to PAGE");
081                    }
082    
083                    return super.createLiferayPortletURL(portletName, lifecycle);
084            }
085    
086            @Override
087            public PortletURL createRenderURL() {
088                    return super.createRenderURL();
089            }
090    
091            @Override
092            public ResourceURL createResourceURL() {
093                    return super.createResourceURL();
094            }
095    
096            @Override
097            public String getLifecycle() {
098                    return PortletRequest.RESOURCE_PHASE;
099            }
100    
101            @Override
102            public void setCharacterEncoding(String charset) {
103                    _response.setCharacterEncoding(charset);
104            }
105    
106            @Override
107            public void setContentLength(int length) {
108                    _response.setContentLength(length);
109            }
110    
111            @Override
112            public void setDateHeader(String name, long date) {
113                    _response.setDateHeader(name, date);
114            }
115    
116            @Override
117            public void setHeader(String name, String value) {
118                    _response.setHeader(name, value);
119    
120                    if (name.equals(ResourceResponse.HTTP_STATUS_CODE)) {
121                            int status = GetterUtil.getInteger(
122                                    value, HttpServletResponse.SC_OK);
123    
124                            _response.setStatus(status);
125                    }
126            }
127    
128            @Override
129            public void setIntHeader(String name, int value) {
130                    _response.setIntHeader(name, value);
131            }
132    
133            @Override
134            public void setLocale(Locale locale) {
135                    _response.setLocale(locale);
136            }
137    
138            @Override
139            protected void init(
140                    PortletRequestImpl portletRequestImpl, HttpServletResponse response,
141                    String portletName, long companyId, long plid) {
142    
143                    super.init(portletRequestImpl, response, portletName, companyId, plid);
144    
145                    _response = response;
146            }
147    
148            private HttpServletResponse _response;
149    
150    }