Author: julien(a)jboss.com
Date: 2008-01-28 07:25:21 -0500 (Mon, 28 Jan 2008)
New Revision: 9619
Modified:
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/impl/jsr168/api/MimeResponseImpl.java
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/invocation/response/FragmentResponse.java
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/jsr286/ext/dispatcher/ContentServlet.java
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/jsr286/ext/dispatcher/ContentTestCase.java
modules/portlet/trunk/portlet/src/test/resources/local-jboss-unit.xml
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletController.java
modules/portlet/trunk/test/src/test/resources/jsr286/ext/dispatcher-war/WEB-INF/web.xml
Log:
- implemented content commit/flush
- extends testcase for forward/include dispatch
Modified:
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/impl/jsr168/api/MimeResponseImpl.java
===================================================================
---
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/impl/jsr168/api/MimeResponseImpl.java 2008-01-28
10:22:35 UTC (rev 9618)
+++
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/impl/jsr168/api/MimeResponseImpl.java 2008-01-28
12:25:21 UTC (rev 9619)
@@ -183,6 +183,7 @@
public void flushBuffer() throws IOException
{
+ fragment.getBuffer().commit();
}
public void resetBuffer()
@@ -202,8 +203,7 @@
public boolean isCommitted()
{
- // Never afterCommit
- return false;
+ return fragment.getBuffer().isCommited();
}
public ResourceURL createResourceURL()
Modified:
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/invocation/response/FragmentResponse.java
===================================================================
---
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/invocation/response/FragmentResponse.java 2008-01-28
10:22:35 UTC (rev 9618)
+++
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/invocation/response/FragmentResponse.java 2008-01-28
12:25:21 UTC (rev 9619)
@@ -23,13 +23,12 @@
package org.jboss.portal.portlet.invocation.response;
import org.jboss.portal.portlet.cache.CacheControl;
+import org.jboss.portal.portlet.impl.jsr168.ContentBuffer;
import java.io.ByteArrayOutputStream;
-import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
-import java.io.Writer;
/**
* Data produced.
@@ -41,29 +40,20 @@
{
/** . */
- public static final int TYPE_EMPTY = 0;
+ public static final int TYPE_EMPTY = ContentBuffer.TYPE_EMPTY;
/** . */
- public static final int TYPE_CHARS = 1;
+ public static final int TYPE_CHARS = ContentBuffer.TYPE_CHARS;
/** . */
- public static final int TYPE_BYTES = 2;
+ public static final int TYPE_BYTES = ContentBuffer.TYPE_BYTES;
/** . */
private ResponseProperties properties;
- /** The output as a bytes if any. */
- private ClosableOutputStream bytes;
+ /** . */
+ private ContentBuffer buffer;
- /** The output as chars if any. */
- private ClosableWriter chars;
-
- /** The writer that will produce the chars output if any. */
- private PrintWriter writer;
-
- /** The result content type if any. */
- private String contentType;
-
/** The title if any. */
private String title;
@@ -72,10 +62,7 @@
public FragmentResponse()
{
- this.bytes = null;
- this.chars = null;
- this.writer = null;
- this.contentType = null;
+ this.buffer = new ContentBuffer();
this.title = null;
this.cacheControl = null;
this.properties = null;
@@ -103,21 +90,7 @@
public int getType()
{
- if (bytes == null)
- {
- if (chars == null)
- {
- return TYPE_EMPTY;
- }
- else
- {
- return TYPE_CHARS;
- }
- }
- else
- {
- return TYPE_BYTES;
- }
+ return buffer.getType();
}
/**
@@ -128,11 +101,7 @@
*/
public ByteArrayOutputStream getBytes() throws IllegalArgumentException
{
- if (bytes == null)
- {
- throw new IllegalStateException("Bytes not used");
- }
- return (ByteArrayOutputStream)bytes.out;
+ return buffer.getBytes();
}
/**
@@ -143,11 +112,7 @@
*/
public StringWriter getChars() throws IllegalArgumentException
{
- if (chars == null)
- {
- throw new IllegalStateException("Chars not used");
- }
- return (StringWriter)chars.writer;
+ return buffer.getChars();
}
/**
@@ -177,7 +142,7 @@
*/
public String getContentType()
{
- return contentType;
+ return buffer.getContentType();
}
/**
@@ -187,7 +152,7 @@
*/
public void setContentType(String contentType)
{
- this.contentType = contentType;
+ buffer.setContentType(contentType);
}
/**
@@ -198,20 +163,7 @@
*/
public PrintWriter getWriter() throws IllegalStateException
{
- if (bytes != null)
- {
- throw new IllegalStateException("The window output stream is already
used");
- }
- if (contentType == null)
- {
- throw new IllegalStateException("No content type defined");
- }
- if (chars == null)
- {
- chars = new ClosableWriter(new StringWriter());
- writer = new PrintWriter(chars);
- }
- return writer;
+ return buffer.getWriter();
}
/**
@@ -220,197 +172,16 @@
*/
public OutputStream getOutputStream() throws IllegalStateException
{
- if (chars != null)
- {
- throw new IllegalStateException("The window writer is already used");
- }
- if (contentType == null)
- {
- throw new IllegalStateException("No content type defined");
- }
- if (bytes == null)
- {
- bytes = new ClosableOutputStream(new ByteArrayOutputStream());
- }
- return bytes;
+ return buffer.getOutputStream();
}
public void resetBuffer()
{
- if (bytes != null)
- {
- if (bytes.closed)
- {
- throw new IllegalStateException("Cannot reset a closed stream");
- }
- ((ByteArrayOutputStream)bytes.out).reset();
- }
- else if (chars != null)
- {
- if (chars.closed)
- {
- throw new IllegalStateException("Cannot reset a closed writer");
- }
- StringWriter sw = (StringWriter)chars.writer;
- sw.flush();
- sw.getBuffer().setLength(0);
- }
+ buffer.reset();
}
- private class ClosableOutputStream extends OutputStream
+ public ContentBuffer getBuffer()
{
-
- /** . */
- boolean closed = false;
-
- /** . */
- final OutputStream out;
-
- public ClosableOutputStream(OutputStream out)
- {
- this.out = out;
- }
-
-
- public void write(byte b[]) throws IOException
- {
- if (closed)
- {
- throw new IOException("Cannot write to a closed stream");
- }
-
- //
- out.write(b);
- }
-
- public void write(byte b[], int off, int len) throws IOException
- {
- if (closed)
- {
- throw new IOException("Cannot write to a closed stream");
- }
-
- //
- out.write(b, off, len);
- }
-
- public void write(int b) throws IOException
- {
- if (closed)
- {
- throw new IOException("Cannot write to a closed stream");
- }
-
- //
- out.write(b);
- }
-
- public void flush() throws IOException
- {
- if (closed)
- {
- throw new IOException("Cannot flush a closed stream");
- }
-
- //
- out.flush();
- }
-
- public void close() throws IOException
- {
- super.close();
-
- //
- closed = true;
- }
+ return buffer;
}
-
- /**
- * Adds behavior to a writer that complies with JSR-168 notion of writer.
- */
- private class ClosableWriter extends Writer
- {
-
- /** . */
- boolean closed = false;
-
- /** . */
- final Writer writer;
-
- public ClosableWriter(Writer writer)
- {
- this.writer = writer;
- }
-
- public void write(int c) throws IOException
- {
- if (closed)
- {
- throw new IOException("Cannot write to a closed writer");
- }
-
- //
- writer.write(c);
- }
-
-
- public void write(char cbuf[], int off, int len) throws IOException
- {
- if (closed)
- {
- throw new IOException("Cannot write to a closed writer");
- }
-
- //
- writer.write(cbuf, off, len);
- }
-
-
- public void write(String str) throws IOException
- {
- if (closed)
- {
- throw new IOException("Cannot write to a closed writer");
- }
-
- //
- writer.write(str);
- }
-
- public void flush() throws IOException
- {
- if (closed)
- {
- throw new IOException("Cannot flush closed writer");
- }
-
- //
- writer.flush();
- }
-
- public void close() throws IOException
- {
- writer.close();
-
- //
- closed = true;
- }
- }
-
- /**
- * Return the content as a string.
- *
- * @return the content
- */
- public String getContent()
- {
- switch (getType())
- {
- case FragmentResponse.TYPE_CHARS:
- return getChars().toString();
- case FragmentResponse.TYPE_BYTES:
- return getBytes().toString();
- }
- return "";
- }
}
Modified:
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/jsr286/ext/dispatcher/ContentServlet.java
===================================================================
---
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/jsr286/ext/dispatcher/ContentServlet.java 2008-01-28
10:22:35 UTC (rev 9618)
+++
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/jsr286/ext/dispatcher/ContentServlet.java 2008-01-28
12:25:21 UTC (rev 9619)
@@ -37,15 +37,19 @@
{
/** . */
- public static final ThreadLocal<String> value = new
ThreadLocal<String>();
+ private String content;
+ public void init() throws ServletException
+ {
+ content = getServletConfig().getInitParameter("content");
+ }
+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException
{
PrintWriter writer = resp.getWriter();
- String s = value.get();
- if (s != null)
+ if (content != null)
{
- writer.print(value.get());
+ writer.print(content);
}
}
}
Modified:
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/jsr286/ext/dispatcher/ContentTestCase.java
===================================================================
---
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/jsr286/ext/dispatcher/ContentTestCase.java 2008-01-28
10:22:35 UTC (rev 9618)
+++
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/jsr286/ext/dispatcher/ContentTestCase.java 2008-01-28
12:25:21 UTC (rev 9619)
@@ -60,7 +60,6 @@
//
PortletRequestDispatcher prd =
request.getPortletSession().getPortletContext().getRequestDispatcher("/ForwardedRequestContentServlet");
assertNotNull(prd);
- ContentServlet.value.set("@ForwardedRequestContent@");
prd.forward(request, response);
//
@@ -85,25 +84,81 @@
writer.print("@ShouldNotBePresent@");
//
- PortletRequestDispatcher prd =
request.getPortletSession().getPortletContext().getRequestDispatcher("/NamedRequestContentServlet");
+ PortletRequestDispatcher prd =
request.getPortletSession().getPortletContext().getNamedDispatcher("ForwardedNamedContentServlet");
assertNotNull(prd);
- ContentServlet.value.set("@NamedRequestContent@");
prd.forward(request, response);
//
- return new EndTestResponse();
+ assertTrue(response.isCommitted());
+
+ //
+ return new InvokeGetResponse(response.createRenderURL().toString());
}
});
- seq.bindAction(1, UTP4.RENDER_JOIN_POINT, new PortletRenderTestAction()
+ seq.bindAction(2, UTP4.RENDER_JOIN_POINT, new PortletRenderTestAction()
{
protected DriverResponse run(Portlet portlet, RenderRequest request,
RenderResponse response, PortletTestContext context) throws PortletException, IOException
{
byte[] bytes = context.getResponseBody();
String s = new String(bytes);
assertFalse("Page " + s + " should not contain the string
@ShouldNotBePresent@", s.contains("@ShouldNotBePresent@"));
- assertTrue("Page " + s + " should contain the string
@NamedRequestContent@", s.contains("@NamedRequestContent@"));
+ assertTrue("Page " + s + " should contain the string
@NamedRequestContent@", s.contains("@ForwardedNamedContent@"));
//
+ response.setContentType("text/html");
+ PrintWriter writer = response.getWriter();
+ writer.print("@BeforeIncludeRequestContent@");
+
+ //
+ PortletRequestDispatcher prd =
request.getPortletSession().getPortletContext().getRequestDispatcher("/IncludedRequestContentServlet");
+ assertNotNull(prd);
+ prd.include(request, response);
+
+ //
+ assertFalse(response.isCommitted());
+ writer.print("@AfterIncludeRequestContent@");
+
+ //
+ return new InvokeGetResponse(response.createRenderURL().toString());
+ }
+ });
+ seq.bindAction(3, UTP4.RENDER_JOIN_POINT, new PortletRenderTestAction()
+ {
+ protected DriverResponse run(Portlet portlet, RenderRequest request,
RenderResponse response, PortletTestContext context) throws PortletException, IOException
+ {
+ byte[] bytes = context.getResponseBody();
+ String s = new String(bytes);
+ String t =
"@BeforeIncludeRequestContent@@IncludedRequestContent@@AfterIncludeRequestContent@";
+ assertTrue("Page " + s + " should contain the string " +
t, s.contains(t));
+
+ //
+ response.setContentType("text/html");
+ PrintWriter writer = response.getWriter();
+ writer.print("@BeforeIncludeNamedContent@");
+
+ //
+ PortletRequestDispatcher prd =
request.getPortletSession().getPortletContext().getNamedDispatcher("IncludedNamedContentServlet");
+ assertNotNull(prd);
+ prd.include(request, response);
+
+ //
+ assertFalse(response.isCommitted());
+ writer.print("@AfterIncludeNamedContent@");
+
+ //
+ return new InvokeGetResponse(response.createRenderURL().toString());
+ }
+ });
+ seq.bindAction(4, UTP4.RENDER_JOIN_POINT, new PortletRenderTestAction()
+ {
+ protected DriverResponse run(Portlet portlet, RenderRequest request,
RenderResponse response, PortletTestContext context) throws PortletException, IOException
+ {
+ byte[] bytes = context.getResponseBody();
+ String s = new String(bytes);
+ String t =
"@BeforeIncludeNamedContent@@IncludedNamedContent@@AfterIncludeNamedContent@";
+ assertTrue("Page " + s + " should contain the string " +
t, s.contains(t));
+
+ //
return new EndTestResponse();
}
});
Modified: modules/portlet/trunk/portlet/src/test/resources/local-jboss-unit.xml
===================================================================
--- modules/portlet/trunk/portlet/src/test/resources/local-jboss-unit.xml 2008-01-28
10:22:35 UTC (rev 9618)
+++ modules/portlet/trunk/portlet/src/test/resources/local-jboss-unit.xml 2008-01-28
12:25:21 UTC (rev 9619)
@@ -31,5 +31,8 @@
<test>
<class
name="org.jboss.portal.test.portlet.PropertiesTestCase"/>
</test>
+ <test>
+ <class
name="org.jboss.portal.test.portlet.ContentBufferTestCase"/>
+ </test>
</pojo>
</jboss-unit>
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletController.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletController.java 2008-01-28
10:22:35 UTC (rev 9618)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletController.java 2008-01-28
12:25:21 UTC (rev 9619)
@@ -742,7 +742,7 @@
if (fragment.getType() != FragmentResponse.TYPE_EMPTY)
{
writer.print("<div id=\"portlet\">");
- writer.print(fragment.getContent());
+ writer.print(fragment.getBuffer().getContent());
writer.print("</div>");
}
else
Modified:
modules/portlet/trunk/test/src/test/resources/jsr286/ext/dispatcher-war/WEB-INF/web.xml
===================================================================
---
modules/portlet/trunk/test/src/test/resources/jsr286/ext/dispatcher-war/WEB-INF/web.xml 2008-01-28
10:22:35 UTC (rev 9618)
+++
modules/portlet/trunk/test/src/test/resources/jsr286/ext/dispatcher-war/WEB-INF/web.xml 2008-01-28
12:25:21 UTC (rev 9619)
@@ -175,13 +175,39 @@
<servlet>
<servlet-name>ForwardedRequestContentServlet</servlet-name>
<servlet-class>org.jboss.portal.test.portlet.jsr286.ext.dispatcher.ContentServlet</servlet-class>
+ <init-param>
+ <param-name>content</param-name>
+ <param-value>@ForwardedRequestContent@</param-value>
+ </init-param>
</servlet>
<servlet>
<servlet-name>ForwardedNamedContentServlet</servlet-name>
<servlet-class>org.jboss.portal.test.portlet.jsr286.ext.dispatcher.ContentServlet</servlet-class>
+ <init-param>
+ <param-name>content</param-name>
+ <param-value>@ForwardedNamedContent@</param-value>
+ </init-param>
</servlet>
+ <servlet>
+ <servlet-name>IncludedRequestContentServlet</servlet-name>
+
<servlet-class>org.jboss.portal.test.portlet.jsr286.ext.dispatcher.ContentServlet</servlet-class>
+ <init-param>
+ <param-name>content</param-name>
+ <param-value>@IncludedRequestContent@</param-value>
+ </init-param>
+ </servlet>
+
+ <servlet>
+ <servlet-name>IncludedNamedContentServlet</servlet-name>
+
<servlet-class>org.jboss.portal.test.portlet.jsr286.ext.dispatcher.ContentServlet</servlet-class>
+ <init-param>
+ <param-name>content</param-name>
+ <param-value>@IncludedNamedContent@</param-value>
+ </init-param>
+ </servlet>
+
<servlet-mapping>
<servlet-name>UniversalServletA</servlet-name>
<url-pattern>/universalServletA</url-pattern>
@@ -203,8 +229,8 @@
</servlet-mapping>
<servlet-mapping>
- <servlet-name>ForwardedNamedContentServlet</servlet-name>
- <url-pattern>/ForwardedNamedContentServlet</url-pattern>
+ <servlet-name>IncludedRequestContentServlet</servlet-name>
+ <url-pattern>/IncludedRequestContentServlet</url-pattern>
</servlet-mapping>
</web-app>