You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2005/11/23 13:50:51 UTC

svn commit: r348424 - in /cocoon/blocks/portal/trunk: java/org/apache/cocoon/portal/pluto/servlet/ServletResponseImpl.java java/org/apache/cocoon/portal/serialization/IncludingHTMLSerializer.java status.xml

Author: cziegeler
Date: Wed Nov 23 04:50:43 2005
New Revision: 348424

URL: http://svn.apache.org/viewcvs?rev=348424&view=rev
Log:
 Portal block: Fix possible encoding problems with portlets.

Modified:
    cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/pluto/servlet/ServletResponseImpl.java
    cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/serialization/IncludingHTMLSerializer.java
    cocoon/blocks/portal/trunk/status.xml

Modified: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/pluto/servlet/ServletResponseImpl.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/pluto/servlet/ServletResponseImpl.java?rev=348424&r1=348423&r2=348424&view=diff
==============================================================================
--- cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/pluto/servlet/ServletResponseImpl.java (original)
+++ cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/pluto/servlet/ServletResponseImpl.java Wed Nov 23 04:50:43 2005
@@ -17,7 +17,9 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
 import java.util.Locale;
 
 import javax.servlet.ServletOutputStream;
@@ -39,25 +41,33 @@
 
     protected String redirectURL;
 
+    protected String encoding = "ISO-8859-1";
+
     public ServletResponseImpl(HttpServletResponse response) {
         super(response);
         this.stream = new MyOutputStream();
-        this.writer = new PrintWriter(this.stream);
     }
 
     /**
      * Return the content of the portlet
      */
     public String getContent() {
-        this.writer.flush();
+        if ( this.writer != null ) {
+            this.writer.flush();
+        }
         try {
             this.stream.flush();
         } catch (IOException ignore) {
             // just ignore it
         }
-        final String value = new String(this.stream.stream.toByteArray());
+        String value;
+        try {
+            value = this.stream.stream.toString(this.encoding);
+        } catch (UnsupportedEncodingException uee) {
+            value = new String(this.stream.stream.toByteArray());
+        }
         this.stream = new MyOutputStream();
-        this.writer = new PrintWriter(this.stream);
+        this.writer = null;
         return value;
     }
 
@@ -68,108 +78,87 @@
         return this.redirectURL;
     }
 
-    /* (non-Javadoc)
+    /**
      * @see java.lang.Object#toString()
      */
     public String toString() {
         return this.getContent();
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see javax.servlet.http.HttpServletResponse#sendError(int,
-     *      java.lang.String)
+    /**
+     * @see javax.servlet.http.HttpServletResponse#sendError(int, String)
      */
     public void sendError(int arg0, String arg1) throws IOException {
         //this.response.sendError(arg0, arg1);
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.http.HttpServletResponse#sendError(int)
      */
     public void sendError(int arg0) throws IOException {
         //this.response.sendError(arg0);
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.http.HttpServletResponse#redirect(java.lang.String)
      */
     public void sendRedirect(String arg0) throws IOException {
         this.redirectURL = arg0;
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see javax.servlet.http.HttpServletResponse#setStatus(int,
-     *      java.lang.String)
+    /**
+     * @see javax.servlet.http.HttpServletResponse#setStatus(int, String)
      */
     public void setStatus(int arg0, String arg1) {
         //this.response.setStatus(arg0, arg1);
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.http.HttpServletResponse#setStatus(int)
      */
     public void setStatus(int arg0) {
         //this.response.setStatus(arg0);
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.ServletResponse#flushBuffer()
      */
     public void flushBuffer() throws IOException {
         this.committed = true;
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.ServletResponse#getBufferSize()
      */
     public int getBufferSize() {
         return this.bufferSize = 1024;
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.ServletResponse#getOutputStream()
      */
     public ServletOutputStream getOutputStream() throws IOException {
         return this.stream;
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.ServletResponse#getWriter()
      */
     public PrintWriter getWriter() throws IOException {
+        if ( this.writer == null ) {
+            this.writer = new PrintWriter(new OutputStreamWriter(this.stream, this.encoding));
+        }
         return this.writer;
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.ServletResponse#isCommitted()
      */
     public boolean isCommitted() {
         return this.committed;
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.ServletResponse#reset()
      */
     public void reset() {
@@ -178,78 +167,71 @@
         }
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.ServletResponse#setBufferSize(int)
      */
     public void setBufferSize(int arg0) {
         this.bufferSize = arg0;
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.ServletResponse#setContentLength(int)
      */
     public void setContentLength(int arg0) {
         // nothing to do 
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.ServletResponse#setContentType(java.lang.String)
      */
-    public void setContentType(String arg0) {
-        // nothing to do 
+    public void setContentType(String typeInfo) {
+        if ( typeInfo != null ) {
+            int pos = typeInfo.indexOf("charset=");
+            if ( pos != -1 ) {
+                this.encoding = typeInfo.substring(pos + 8);
+            }
+        }
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    public void setCharacterEncoding(String enc) {
+        this.encoding = enc;
+    }
+
+    /**
      * @see javax.servlet.ServletResponse#setLocale(java.util.Locale)
      */
-    public void setLocale(Locale arg0) {
+    public void setLocale(Locale locale) {
         // nothing to do 
     }
 
-    /*
-     * (non-Javadoc)
-     *
+    /**
      * @see javax.servlet.ServletResponse#resetBuffer()
      */
     public void resetBuffer() {
         // nothing to do 
     }
 
-}
-
-class MyOutputStream extends ServletOutputStream {
+    protected final static class MyOutputStream extends ServletOutputStream {
 
-    ByteArrayOutputStream stream = new ByteArrayOutputStream();
+        ByteArrayOutputStream stream = new ByteArrayOutputStream();
 
-    public MyOutputStream() {
-        // nothing to do 
-    }
+        public MyOutputStream() {
+            // nothing to do 
+        }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see java.io.OutputStream#write(int)
-     */
-    public void write(int b) throws IOException {
-        this.stream.write(b);
-    }
+        /**
+         * @see java.io.OutputStream#write(int)
+         */
+        public void write(int b) throws IOException {
+            this.stream.write(b);
+        }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see java.io.OutputStream#flush()
-     */
-    public void flush() throws IOException {
-        super.flush();
-        this.stream.flush();
+        /**
+         * @see java.io.OutputStream#flush()
+         */
+        public void flush() throws IOException {
+            super.flush();
+            this.stream.flush();
+        }
     }
-
-}
\ No newline at end of file
+}

Modified: cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/serialization/IncludingHTMLSerializer.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/serialization/IncludingHTMLSerializer.java?rev=348424&r1=348423&r2=348424&view=diff
==============================================================================
--- cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/serialization/IncludingHTMLSerializer.java (original)
+++ cocoon/blocks/portal/trunk/java/org/apache/cocoon/portal/serialization/IncludingHTMLSerializer.java Wed Nov 23 04:50:43 2005
@@ -21,6 +21,8 @@
 import java.util.LinkedList;
 import java.util.Map;
 
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.cocoon.serialization.HTMLSerializer;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
@@ -49,6 +51,18 @@
 
     protected static final char[] tokens = new char[] {token, token};
 
+    /** The encoding. */
+    protected String encoding = "utf-8";
+ 
+    /**
+     * @see org.apache.cocoon.serialization.HTMLSerializer#configure(org.apache.avalon.framework.configuration.Configuration)
+     */
+    public void configure(Configuration conf)
+    throws ConfigurationException {
+        super.configure(conf);
+        this.encoding = conf.getChild("encoding").getValue(this.encoding);
+    }
+
     /**
      * @see org.apache.avalon.excalibur.pool.Recyclable#recycle()
      */
@@ -109,26 +123,34 @@
      * @see org.apache.cocoon.sitemap.SitemapOutputComponent#setOutputStream(java.io.OutputStream)
      */
     public void setOutputStream(OutputStream stream) throws IOException {
-        super.setOutputStream(new ReplacingOutputStream(stream, this.orderedPortletList));
+        super.setOutputStream(new ReplacingOutputStream(stream,
+                                                        this.orderedPortletList,
+                                                        this.encoding));
     }
 
 }
-class ReplacingOutputStream extends OutputStream {
-
-    /** Stream */
+final class ReplacingOutputStream extends OutputStream {
+    
+    /** Stream. */
     protected final OutputStream stream;
 
     protected boolean inKey;
 
     protected final LinkedList orderedValues;
 
+    /** Encoding. */
+    protected final String encoding;
+
     /**
-     * Constructor
+     * Constructor.
      */
-    public ReplacingOutputStream(OutputStream stream, LinkedList values) {
+    public ReplacingOutputStream(OutputStream stream,
+                                 LinkedList   values,
+                                 String       enc) {
         this.stream = stream;    
         this.orderedValues = values;
         this.inKey = false;
+        this.encoding = enc;
     }
 
     /**
@@ -145,7 +167,7 @@
         this.stream.flush();
     }
 
-    /* *
+    /**
      * @see java.io.OutputStream#write(byte[], int, int)
      */
     public void write(byte[] b, int off, int len) throws IOException {
@@ -227,7 +249,8 @@
     protected void writeNextValue() throws IOException {
         final String value = (String)this.orderedValues.removeLast();
         if ( value != null ) {
-            this.stream.write(value.getBytes(), 0, value.length());
+            this.stream.write(value.getBytes(this.encoding), 0, value.length());
         }        
     }
 }
+

Modified: cocoon/blocks/portal/trunk/status.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/portal/trunk/status.xml?rev=348424&r1=348423&r2=348424&view=diff
==============================================================================
--- cocoon/blocks/portal/trunk/status.xml (original)
+++ cocoon/blocks/portal/trunk/status.xml Wed Nov 23 04:50:43 2005
@@ -69,9 +69,6 @@
       Implement (hot)deployment for JSR 168 portlets.
     </action>
     <action dev="CZ" type="add">
-      Support external form actions in HTML transformation.
-    </action>
-    <action dev="CZ" type="add">
       Add deployment infrastructure.
     </action>
     <action dev="CZ" type="add" fixes-bug="COCOON-1674">