001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.mock.web.portlet;
018
019import java.util.Collections;
020import java.util.LinkedHashMap;
021import java.util.LinkedHashSet;
022import java.util.Map;
023import java.util.Set;
024import javax.portlet.PortalContext;
025import javax.portlet.PortletResponse;
026import javax.servlet.http.Cookie;
027import javax.xml.parsers.DocumentBuilderFactory;
028import javax.xml.parsers.ParserConfigurationException;
029
030import org.w3c.dom.DOMException;
031import org.w3c.dom.Document;
032import org.w3c.dom.Element;
033
034import org.springframework.util.Assert;
035
036/**
037 * Mock implementation of the {@link javax.portlet.PortletResponse} interface.
038 *
039 * @author John A. Lewis
040 * @author Juergen Hoeller
041 * @since 2.0
042 */
043public class MockPortletResponse implements PortletResponse {
044
045        private final PortalContext portalContext;
046
047        private final Map<String, String[]> properties = new LinkedHashMap<String, String[]>();
048
049        private String namespace = "";
050
051        private final Set<Cookie> cookies = new LinkedHashSet<Cookie>();
052
053        private final Map<String, Element[]> xmlProperties = new LinkedHashMap<String, Element[]>();
054
055        private Document xmlDocument;
056
057
058        /**
059         * Create a new MockPortletResponse with a default {@link MockPortalContext}.
060         * @see MockPortalContext
061         */
062        public MockPortletResponse() {
063                this(null);
064        }
065
066        /**
067         * Create a new MockPortletResponse.
068         * @param portalContext the PortalContext defining the supported
069         * PortletModes and WindowStates
070         */
071        public MockPortletResponse(PortalContext portalContext) {
072                this.portalContext = (portalContext != null ? portalContext : new MockPortalContext());
073        }
074
075        /**
076         * Return the PortalContext that this MockPortletResponse runs in,
077         * defining the supported PortletModes and WindowStates.
078         */
079        public PortalContext getPortalContext() {
080                return this.portalContext;
081        }
082
083
084        //---------------------------------------------------------------------
085        // PortletResponse methods
086        //---------------------------------------------------------------------
087
088        @Override
089        public void addProperty(String key, String value) {
090                Assert.notNull(key, "Property key must not be null");
091                String[] oldArr = this.properties.get(key);
092                if (oldArr != null) {
093                        String[] newArr = new String[oldArr.length + 1];
094                        System.arraycopy(oldArr, 0, newArr, 0, oldArr.length);
095                        newArr[oldArr.length] = value;
096                        this.properties.put(key, newArr);
097                }
098                else {
099                        this.properties.put(key, new String[] {value});
100                }
101        }
102
103        @Override
104        public void setProperty(String key, String value) {
105                Assert.notNull(key, "Property key must not be null");
106                this.properties.put(key, new String[] {value});
107        }
108
109        public Set<String> getPropertyNames() {
110                return Collections.unmodifiableSet(this.properties.keySet());
111        }
112
113        public String getProperty(String key) {
114                Assert.notNull(key, "Property key must not be null");
115                String[] arr = this.properties.get(key);
116                return (arr != null && arr.length > 0 ? arr[0] : null);
117        }
118
119        public String[] getProperties(String key) {
120                Assert.notNull(key, "Property key must not be null");
121                return this.properties.get(key);
122        }
123
124        @Override
125        public String encodeURL(String path) {
126                return path;
127        }
128
129        public void setNamespace(String namespace) {
130                this.namespace = namespace;
131        }
132
133        @Override
134        public String getNamespace() {
135                return this.namespace;
136        }
137
138        @Override
139        public void addProperty(Cookie cookie) {
140                Assert.notNull(cookie, "Cookie must not be null");
141                this.cookies.add(cookie);
142        }
143
144        public Cookie[] getCookies() {
145                return this.cookies.toArray(new Cookie[this.cookies.size()]);
146        }
147
148        public Cookie getCookie(String name) {
149                Assert.notNull(name, "Cookie name must not be null");
150                for (Cookie cookie : this.cookies) {
151                        if (name.equals(cookie.getName())) {
152                                return cookie;
153                        }
154                }
155                return null;
156        }
157
158        @Override
159        public void addProperty(String key, Element value) {
160                Assert.notNull(key, "Property key must not be null");
161                Element[] oldArr = this.xmlProperties.get(key);
162                if (oldArr != null) {
163                        Element[] newArr = new Element[oldArr.length + 1];
164                        System.arraycopy(oldArr, 0, newArr, 0, oldArr.length);
165                        newArr[oldArr.length] = value;
166                        this.xmlProperties.put(key, newArr);
167                }
168                else {
169                        this.xmlProperties.put(key, new Element[] {value});
170                }
171        }
172
173
174        public Set<String> getXmlPropertyNames() {
175                return Collections.unmodifiableSet(this.xmlProperties.keySet());
176        }
177
178        public Element getXmlProperty(String key) {
179                Assert.notNull(key, "Property key must not be null");
180                Element[] arr = this.xmlProperties.get(key);
181                return (arr != null && arr.length > 0 ? arr[0] : null);
182        }
183
184        public Element[] getXmlProperties(String key) {
185                Assert.notNull(key, "Property key must not be null");
186                return this.xmlProperties.get(key);
187        }
188
189        @Override
190        public Element createElement(String tagName) throws DOMException {
191                if (this.xmlDocument == null) {
192                        try {
193                                this.xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
194                        }
195                        catch (ParserConfigurationException ex) {
196                                throw new DOMException(DOMException.INVALID_STATE_ERR, ex.toString());
197                        }
198                }
199                return this.xmlDocument.createElement(tagName);
200        }
201
202}