Author: julien(a)jboss.com
Date: 2008-02-28 06:57:23 -0500 (Thu, 28 Feb 2008)
New Revision: 10151
Modified:
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/impl/jsr168/ContentBuffer.java
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/ContentBufferTestCase.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/ResourceRenderer.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/SimpleFragmentRenderer.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/jsp/ControllerFilter.java
Log:
refactor to remove the dependency of FragmentResponse over ContentBuffer
Modified:
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/impl/jsr168/ContentBuffer.java
===================================================================
---
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/impl/jsr168/ContentBuffer.java 2008-02-28
10:15:38 UTC (rev 10150)
+++
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/impl/jsr168/ContentBuffer.java 2008-02-28
11:57:23 UTC (rev 10151)
@@ -38,15 +38,6 @@
public class ContentBuffer
{
- /** . */
- public static final int TYPE_EMPTY = 0;
-
- /** . */
- public static final int TYPE_CHARS = 1;
-
- /** . */
- public static final int TYPE_BYTES = 2;
-
/** The output as a bytes if any. */
private ClosableOutputStream bytes;
@@ -71,70 +62,38 @@
this.commited = false;
}
- public int getType()
- {
- if (bytes == null)
- {
- if (chars == null)
- {
- return TYPE_EMPTY;
- }
- else
- {
- return TYPE_CHARS;
- }
- }
- else
- {
- return TYPE_BYTES;
- }
- }
-
/**
- * Return the content as a string.
+ * Return the bytes of the content held by the fragment or null.
*
- * @return the content
- */
- public String getContent()
- {
- switch (getType())
- {
- case TYPE_CHARS:
- return getChars().toString();
- case TYPE_BYTES:
- return getBytes().toString();
- }
- return "";
- }
-
- /**
- * Return the bytes of the content held by the fragment.
- *
* @return the bytes
- * @throws IllegalArgumentException if the type is not bytes
*/
- public ByteArrayOutputStream getBytes() throws IllegalArgumentException
+ public byte[] getBytes()
{
if (bytes == null)
{
- throw new IllegalStateException("Bytes not used");
+ return null;
}
- return (ByteArrayOutputStream)bytes.out;
+ else
+ {
+ return ((ByteArrayOutputStream)bytes.out).toByteArray();
+ }
}
/**
- * Return the chars of the content held by the fragment.
+ * Return the chars of the content held by the fragment or null.
*
* @return the chars
- * @throws IllegalArgumentException if the type is not chars
*/
- public StringWriter getChars() throws IllegalArgumentException
+ public String getChars()
{
if (chars == null)
{
- throw new IllegalStateException("Chars not used");
+ return null;
}
- return (StringWriter)chars.writer;
+ else
+ {
+ return chars.writer.toString();
+ }
}
/**
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-02-28
10:15:38 UTC (rev 10150)
+++
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/impl/jsr168/api/MimeResponseImpl.java 2008-02-28
11:57:23 UTC (rev 10151)
@@ -115,7 +115,9 @@
return new FragmentResponse(
getProperties(false),
preq.attributes.getAttributeMap(),
- responseContent,
+ responseContent.getContentType(),
+ responseContent.getBytes(),
+ responseContent.getChars(),
responseTitle,
cc,
responseNextModes != null ? responseNextModes : preq.supportedModes);
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-02-28
10:15:38 UTC (rev 10150)
+++
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/invocation/response/FragmentResponse.java 2008-02-28
11:57:23 UTC (rev 10151)
@@ -23,13 +23,8 @@
package org.jboss.portal.portlet.invocation.response;
import org.jboss.portal.portlet.cache.CacheControl;
-import org.jboss.portal.portlet.impl.jsr168.ContentBuffer;
import org.jboss.portal.Mode;
-import java.io.ByteArrayOutputStream;
-import java.io.OutputStream;
-import java.io.PrintWriter;
-import java.io.StringWriter;
import java.util.Set;
import java.util.Map;
@@ -43,13 +38,13 @@
{
/** . */
- public static final int TYPE_EMPTY = ContentBuffer.TYPE_EMPTY;
+ public static final int TYPE_EMPTY = 0;
/** . */
- public static final int TYPE_CHARS = ContentBuffer.TYPE_CHARS;
+ public static final int TYPE_CHARS = 1;
/** . */
- public static final int TYPE_BYTES = ContentBuffer.TYPE_BYTES;
+ public static final int TYPE_BYTES = 2;
/** . */
private final ResponseProperties properties;
@@ -57,9 +52,15 @@
/** . */
private final Map<String, Object> attributes;
+ /** The result content type if any. */
+ private String contentType;
+
/** . */
- private final ContentBuffer buffer;
+ private final byte[] bytes;
+ /** . */
+ private final String chars;
+
/** The title if any. */
private final String title;
@@ -69,11 +70,21 @@
/** . */
private final Set<Mode> nextModes;
- public FragmentResponse(ResponseProperties properties, Map<String, Object>
attributes, ContentBuffer buffer, String title, CacheControl cacheControl, Set<Mode>
nextModes)
+ public FragmentResponse(
+ ResponseProperties properties,
+ Map<String, Object> attributes,
+ String contentType,
+ byte[] bytes,
+ String chars,
+ String title,
+ CacheControl cacheControl,
+ Set<Mode> nextModes)
{
this.properties = properties;
this.attributes = attributes;
- this.buffer = buffer;
+ this.contentType = contentType;
+ this.bytes = bytes;
+ this.chars = chars;
this.title = title;
this.cacheControl = cacheControl;
this.nextModes = nextModes;
@@ -96,18 +107,52 @@
public int getType()
{
- return buffer.getType();
+ if (bytes == null)
+ {
+ if (chars == null)
+ {
+ return TYPE_EMPTY;
+ }
+ else
+ {
+ return TYPE_CHARS;
+ }
+ }
+ else
+ {
+ return TYPE_BYTES;
+ }
}
/**
+ * Return the content as a string.
+ *
+ * @return the content
+ */
+ public String getContent()
+ {
+ switch (getType())
+ {
+ case TYPE_CHARS:
+ return getChars();
+ case TYPE_BYTES:
+ return new String(bytes);
+ case TYPE_EMPTY:
+ return "";
+ default:
+ throw new AssertionError();
+ }
+ }
+
+ /**
* Return the bytes of the content held by the fragment.
*
* @return the bytes
* @throws IllegalArgumentException if the type is not bytes
*/
- public ByteArrayOutputStream getBytes() throws IllegalArgumentException
+ public byte[] getBytes() throws IllegalArgumentException
{
- return buffer.getBytes();
+ return bytes;
}
/**
@@ -116,9 +161,9 @@
* @return the chars
* @throws IllegalArgumentException if the type is not chars
*/
- public StringWriter getChars() throws IllegalArgumentException
+ public String getChars() throws IllegalArgumentException
{
- return buffer.getChars();
+ return chars;
}
/**
@@ -138,34 +183,9 @@
*/
public String getContentType()
{
- return buffer.getContentType();
+ return contentType;
}
- /**
- * Returns the writer.
- *
- * @return the writer
- * @throws IllegalStateException if the output stream is already used or if no content
type is defined
- */
- public PrintWriter getWriter() throws IllegalStateException
- {
- return buffer.getWriter();
- }
-
- /**
- * @return the output stream
- * @throws IllegalStateException if the window writer is already used or if no content
type is defined
- */
- public OutputStream getOutputStream() throws IllegalStateException
- {
- return buffer.getOutputStream();
- }
-
- public ContentBuffer getBuffer()
- {
- return buffer;
- }
-
public Set<Mode> getNextModes()
{
return nextModes;
Modified:
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/ContentBufferTestCase.java
===================================================================
---
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/ContentBufferTestCase.java 2008-02-28
10:15:38 UTC (rev 10150)
+++
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/ContentBufferTestCase.java 2008-02-28
11:57:23 UTC (rev 10151)
@@ -25,6 +25,7 @@
import org.jboss.unit.api.pojo.annotations.Test;
import org.jboss.portal.portlet.impl.jsr168.ContentBuffer;
import static org.jboss.unit.api.Assert.*;
+import static org.jboss.unit.api.Assert.assertEquals;
import java.io.PrintWriter;
import java.io.OutputStream;
@@ -46,7 +47,7 @@
PrintWriter writer = buffer.getWriter();
writer.print("foo");
buffer.reset();
- assertChars("", buffer.getChars().getBuffer());
+ assertEquals("", buffer.getChars());
}
@Test
@@ -57,7 +58,7 @@
OutputStream out = buffer.getOutputStream();
out.write("foo".getBytes("UTF8"));
buffer.reset();
- assertEquals(new byte[0], buffer.getBytes().toByteArray());
+ assertEquals(new byte[0], buffer.getBytes());
}
@Test
@@ -96,7 +97,7 @@
assertNotNull(writer);
writer.print("foo");
writer.close();
- assertChars("foo", buffer.getChars().getBuffer());
+ assertEquals("foo", buffer.getChars());
}
@Test
@@ -110,7 +111,7 @@
buffer.commit();
writer.print("bar");
writer.close();
- assertChars("foobar", buffer.getChars().getBuffer());
+ assertEquals("foobar", buffer.getChars());
}
@Test
@@ -123,7 +124,7 @@
assertNotNull(out);
out.write("foo".getBytes("UTF8"));
out.close();
- assertEquals("foo".getBytes("UTF8"),
buffer.getBytes().toByteArray());
+ assertEquals("foo".getBytes("UTF8"), buffer.getBytes());
}
@Test
@@ -137,7 +138,7 @@
assertNotNull(out);
out.write("bar".getBytes("UTF8"));
out.close();
- assertEquals("foobar".getBytes("UTF8"),
buffer.getBytes().toByteArray());
+ assertEquals("foobar".getBytes("UTF8"), buffer.getBytes());
}
@Test
@@ -183,14 +184,4 @@
out.close();
assertTrue(buffer.isCommited());
}
-
- private void assertChars(String s, StringBuffer sb)
- {
- int length = s.length();
- assertEquals(length, sb.length());
- char[] chars = new char[length];
- sb.getChars(0, length, chars, 0);
- assertEquals(s, new String(chars));
- }
-
}
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/ResourceRenderer.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/ResourceRenderer.java 2008-02-28
10:15:38 UTC (rev 10150)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/ResourceRenderer.java 2008-02-28
11:57:23 UTC (rev 10151)
@@ -96,7 +96,7 @@
try
{
out = resp.getOutputStream();
- out.write(fragment.getBytes().toByteArray());
+ out.write(fragment.getBytes());
}
finally
{
@@ -109,7 +109,7 @@
try
{
writer = resp.getWriter();
- writer.write(fragment.getChars().toString());
+ writer.write(fragment.getChars());
}
finally
{
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/SimpleFragmentRenderer.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/SimpleFragmentRenderer.java 2008-02-28
10:15:38 UTC (rev 10150)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/SimpleFragmentRenderer.java 2008-02-28
11:57:23 UTC (rev 10151)
@@ -26,6 +26,8 @@
import org.jboss.portal.portlet.invocation.response.ErrorResponse;
import org.jboss.portal.portlet.invocation.response.FragmentResponse;
+import java.io.UnsupportedEncodingException;
+
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision: 9748 $
@@ -42,11 +44,18 @@
//
if (fragment.getType() == FragmentResponse.TYPE_BYTES)
{
- frag = fragment.getBytes().toString();
+ try
+ {
+ frag = new String(fragment.getBytes(), "UTF-8");
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ throw new Error(e);
+ }
}
else
{
- frag = fragment.getChars().toString();
+ frag = fragment.getChars();
}
StringBuilder builder = new StringBuilder(frag.length() + 50);
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/jsp/ControllerFilter.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/jsp/ControllerFilter.java 2008-02-28
10:15:38 UTC (rev 10150)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/jsp/ControllerFilter.java 2008-02-28
11:57:23 UTC (rev 10151)
@@ -185,7 +185,7 @@
try
{
out = resp.getOutputStream();
- out.write(fragment.getBytes().toByteArray());
+ out.write(fragment.getBytes());
}
finally
{
@@ -198,7 +198,7 @@
try
{
writer = resp.getWriter();
- writer.write(fragment.getChars().toString());
+ writer.write(fragment.getChars());
}
finally
{