1 16 17 package org.springframework.mock.web.portlet; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.io.OutputStreamWriter ; 23 import java.io.PrintWriter ; 24 import java.io.UnsupportedEncodingException ; 25 import java.io.Writer ; 26 import java.util.Locale ; 27 28 import javax.portlet.PortalContext; 29 import javax.portlet.PortletURL; 30 import javax.portlet.RenderResponse; 31 32 import org.springframework.web.util.WebUtils; 33 34 41 public class MockRenderResponse extends MockPortletResponse implements RenderResponse { 42 43 private String contentType; 44 45 private String namespace = "MockPortlet"; 46 47 private String title; 48 49 private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; 50 51 private PrintWriter writer; 52 53 private Locale locale = Locale.getDefault(); 54 55 private int bufferSize = 4096; 56 57 private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 58 59 private boolean committed; 60 61 private String includedUrl; 62 63 64 68 public MockRenderResponse() { 69 super(); 70 } 71 72 77 public MockRenderResponse(PortalContext portalContext) { 78 super(portalContext); 79 } 80 81 82 86 public String getContentType() { 87 return this.contentType; 88 } 89 90 public PortletURL createRenderURL() { 91 PortletURL url = new MockPortletURL(getPortalContext(), MockPortletURL.URL_TYPE_RENDER); 92 return url; 93 } 94 95 public PortletURL createActionURL() { 96 PortletURL url = new MockPortletURL(getPortalContext(), MockPortletURL.URL_TYPE_ACTION); 97 return url; 98 } 99 100 public String getNamespace() { 101 return this.namespace; 102 } 103 104 public void setTitle(String title) { 105 this.title = title; 106 } 107 108 public String getTitle() { 109 return title; 110 } 111 112 public void setContentType(String contentType) { 113 this.contentType = contentType; 114 } 115 116 public void setCharacterEncoding(String characterEncoding) { 117 this.characterEncoding = characterEncoding; 118 } 119 120 public String getCharacterEncoding() { 121 return this.characterEncoding; 122 } 123 124 public PrintWriter getWriter() throws UnsupportedEncodingException { 125 if (this.writer == null) { 126 Writer targetWriter = (this.characterEncoding != null 127 ? new OutputStreamWriter (this.outputStream, this.characterEncoding) 128 : new OutputStreamWriter (this.outputStream)); 129 this.writer = new PrintWriter (targetWriter); 130 } 131 return this.writer; 132 } 133 134 public byte[] getContentAsByteArray() { 135 flushBuffer(); 136 return this.outputStream.toByteArray(); 137 } 138 139 public String getContentAsString() throws UnsupportedEncodingException { 140 flushBuffer(); 141 return (this.characterEncoding != null) 142 ? this.outputStream.toString(this.characterEncoding) 143 : this.outputStream.toString(); 144 } 145 146 public void setLocale(Locale locale) { 147 this.locale = locale; 148 } 149 150 public Locale getLocale() { 151 return this.locale; 152 } 153 154 public void setBufferSize(int bufferSize) { 155 this.bufferSize = bufferSize; 156 } 157 158 public int getBufferSize() { 159 return this.bufferSize; 160 } 161 162 public void flushBuffer() { 163 if (this.writer != null) { 164 this.writer.flush(); 165 } 166 if (this.outputStream != null) { 167 try { 168 this.outputStream.flush(); 169 } 170 catch (IOException ex) { 171 throw new IllegalStateException ("Could not flush OutputStream: " + ex.getMessage()); 172 } 173 } 174 this.committed = true; 175 } 176 177 public void resetBuffer() { 178 if (this.committed) { 179 throw new IllegalStateException ("Cannot reset buffer - response is already committed"); 180 } 181 this.outputStream.reset(); 182 } 183 184 public void setCommitted(boolean committed) { 185 this.committed = committed; 186 } 187 188 public boolean isCommitted() { 189 return this.committed; 190 } 191 192 public void reset() { 193 resetBuffer(); 194 this.characterEncoding = null; 195 this.contentType = null; 196 this.locale = null; 197 } 198 199 public OutputStream getPortletOutputStream() throws IOException { 200 return this.outputStream; 201 } 202 203 204 208 public void setIncludedUrl(String includedUrl) { 209 this.includedUrl = includedUrl; 210 } 211 212 public String getIncludedUrl() { 213 return includedUrl; 214 } 215 216 } 217 | Popular Tags |