You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2007/09/06 18:30:48 UTC

svn commit: r573318 - in /incubator/abdera/java/branches/0.3.0-incubating: client/src/main/java/org/apache/abdera/protocol/client/ parser/src/main/java/org/apache/abdera/parser/stax/ server/src/main/java/org/apache/abdera/protocol/server/impl/

Author: jmsnell
Date: Thu Sep  6 09:30:45 2007
New Revision: 573318

URL: http://svn.apache.org/viewvc?rev=573318&view=rev
Log:
Fixing charset issues as reported by Chris Berry and Herbert Welker
https://issues.apache.org/jira/browse/ABDERA-60
https://issues.apache.org/jira/browse/ABDERA-61

This forces the use of UTF-8 when the charset is not otherwise specificied explicitly.

Modified:
    incubator/abdera/java/branches/0.3.0-incubating/client/src/main/java/org/apache/abdera/protocol/client/AbstractClientResponse.java
    incubator/abdera/java/branches/0.3.0-incubating/client/src/main/java/org/apache/abdera/protocol/client/ClientResponse.java
    incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java
    incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
    incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriter.java
    incubator/abdera/java/branches/0.3.0-incubating/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractResponseContext.java
    incubator/abdera/java/branches/0.3.0-incubating/server/src/main/java/org/apache/abdera/protocol/server/impl/BaseResponseContext.java

Modified: incubator/abdera/java/branches/0.3.0-incubating/client/src/main/java/org/apache/abdera/protocol/client/AbstractClientResponse.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/0.3.0-incubating/client/src/main/java/org/apache/abdera/protocol/client/AbstractClientResponse.java?rev=573318&r1=573317&r2=573318&view=diff
==============================================================================
--- incubator/abdera/java/branches/0.3.0-incubating/client/src/main/java/org/apache/abdera/protocol/client/AbstractClientResponse.java (original)
+++ incubator/abdera/java/branches/0.3.0-incubating/client/src/main/java/org/apache/abdera/protocol/client/AbstractClientResponse.java Thu Sep  6 09:30:45 2007
@@ -19,6 +19,8 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
 import java.util.Date;
 
 import javax.activation.MimeType;
@@ -84,7 +86,7 @@
         cl = r.resolve(cl);
       }
       String base = (cl != null) ? cl.toASCIIString() : getUri();
-      Document<T> doc = parser.parse(getInputStream(), base, options);
+      Document<T> doc = parser.parse(getReader(), base, options);
       EntityTag etag = getEntityTag();
       if (etag != null) doc.setEntityTag(etag);
       Date lm = getLastModified();
@@ -107,6 +109,16 @@
 
   public void setInputStream(InputStream in) {
     this.in = in;
+  }
+  
+  public Reader getReader() throws IOException {
+    String charset = getCharacterEncoding();
+    return getReader(charset != null ? charset : "UTF-8");
+  }
+  
+  public Reader getReader(String charset) throws IOException {
+    if (charset == null) charset = "UTF-8";
+    return new InputStreamReader(getInputStream(),charset);
   }
 
   public Date getServerDate() {

Modified: incubator/abdera/java/branches/0.3.0-incubating/client/src/main/java/org/apache/abdera/protocol/client/ClientResponse.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/0.3.0-incubating/client/src/main/java/org/apache/abdera/protocol/client/ClientResponse.java?rev=573318&r1=573317&r2=573318&view=diff
==============================================================================
--- incubator/abdera/java/branches/0.3.0-incubating/client/src/main/java/org/apache/abdera/protocol/client/ClientResponse.java (original)
+++ incubator/abdera/java/branches/0.3.0-incubating/client/src/main/java/org/apache/abdera/protocol/client/ClientResponse.java Thu Sep  6 09:30:45 2007
@@ -19,6 +19,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.Reader;
 import java.util.Date;
 
 import org.apache.abdera.model.Document;
@@ -49,6 +50,19 @@
    * Returns the inputstream used to read data from this response
    */
   InputStream getInputStream() throws IOException;
+  
+  /**
+   * Returns a reader used to read data from this response.  Will
+   * use the character set declared in the Content-Type to create
+   * the reader
+   */
+  Reader getReader() throws IOException;
+  
+  /**
+   * Returns a reader used to read data from this response. Will
+   * use the character set specified to create the reader
+   */
+  Reader getReader(String charset) throws IOException;
   
   void setInputStream(InputStream in);
 

Modified: incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java?rev=573318&r1=573317&r2=573318&view=diff
==============================================================================
--- incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java (original)
+++ incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMDocument.java Thu Sep  6 09:30:45 2007
@@ -167,7 +167,9 @@
   }
   
   public void writeTo(OutputStream out) throws IOException {
-    writeTo(new OutputStreamWriter(out));
+    String charset = getCharset();
+    if (charset == null) charset = "UTF-8";
+    writeTo(new OutputStreamWriter(out, charset));
   }
 
   public void writeTo(java.io.Writer writer) throws IOException {

Modified: incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java?rev=573318&r1=573317&r2=573318&view=diff
==============================================================================
--- incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java (original)
+++ incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java Thu Sep  6 09:30:45 2007
@@ -388,7 +388,9 @@
   }
   
   public void writeTo(OutputStream out) throws IOException {
-    writeTo(new OutputStreamWriter(out));
+    Document doc = getDocument();
+    String charset = doc != null ? doc.getCharset() : "UTF-8";
+    writeTo(new OutputStreamWriter(out,charset));
   }
 
   public void writeTo(java.io.Writer writer) throws IOException {

Modified: incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriter.java?rev=573318&r1=573317&r2=573318&view=diff
==============================================================================
--- incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriter.java (original)
+++ incubator/abdera/java/branches/0.3.0-incubating/parser/src/main/java/org/apache/abdera/parser/stax/FOMWriter.java Thu Sep  6 09:30:45 2007
@@ -26,6 +26,7 @@
 import org.apache.abdera.Abdera;
 import org.apache.abdera.model.Base;
 import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Element;
 import org.apache.abdera.util.AbstractWriter;
 import org.apache.abdera.util.Constants;
 import org.apache.abdera.util.MimeTypeHelper;
@@ -39,6 +40,7 @@
   
   public FOMWriter(Abdera abdera) {}
   
+  @SuppressWarnings("unchecked") 
   public void writeTo(
     Base base, 
     OutputStream out, 
@@ -46,13 +48,23 @@
       throws IOException {
     out = getCompressedOutputStream(out, options);
     String charset = options.getCharset();
-    if (charset != null) {
-      if (base instanceof Document)
-        ((Document)base).setCharset(charset);
-      base.writeTo(new OutputStreamWriter(out,charset));
+    if (charset == null) {
+      if (base instanceof Document) 
+        charset = ((Document)base).getCharset();
+      else if (base instanceof Element) {
+        Document doc = ((Element)base).getDocument();
+        if (doc != null) charset = doc.getCharset();
+      }
+      if (charset == null) charset = "UTF-8";
     } else {
-      base.writeTo(out);
+      Document doc = null;
+      if (base instanceof Document)
+        doc = (Document)base;
+      else if (base instanceof Element)
+        doc = ((Element)base).getDocument();
+      if (doc != null) doc.setCharset(charset);
     }
+    base.writeTo(new OutputStreamWriter(out,charset));  
     finishCompressedOutputStream(out, options);
     if (options.getAutoClose()) out.close();
   }

Modified: incubator/abdera/java/branches/0.3.0-incubating/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractResponseContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/0.3.0-incubating/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractResponseContext.java?rev=573318&r1=573317&r2=573318&view=diff
==============================================================================
--- incubator/abdera/java/branches/0.3.0-incubating/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractResponseContext.java (original)
+++ incubator/abdera/java/branches/0.3.0-incubating/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractResponseContext.java Thu Sep  6 09:30:45 2007
@@ -24,6 +24,8 @@
 import java.util.List;
 import java.util.Map;
 
+import javax.activation.MimeType;
+
 import org.apache.abdera.i18n.iri.Constants;
 import org.apache.abdera.i18n.iri.Escaping;
 import org.apache.abdera.protocol.server.ResponseContext;
@@ -234,11 +236,19 @@
   }
   
   public void setContentType(String type) {
+    setContentType(type,null);
+  }
+  
+  public void setContentType(String type, String charset) {
     if (type == null) {
       removeHeader("Content-Type");
       return;
     }
-    setHeader("Content-Type", type);
+    try {
+      MimeType mimeType = new MimeType(type);
+      if (charset != null) mimeType.setParameter("charset", charset);
+      setHeader("Content-Type", mimeType.toString());
+    } catch (Exception e) {}
   }
   
   public void setEntityTag(String etag) {

Modified: incubator/abdera/java/branches/0.3.0-incubating/server/src/main/java/org/apache/abdera/protocol/server/impl/BaseResponseContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/branches/0.3.0-incubating/server/src/main/java/org/apache/abdera/protocol/server/impl/BaseResponseContext.java?rev=573318&r1=573317&r2=573318&view=diff
==============================================================================
--- incubator/abdera/java/branches/0.3.0-incubating/server/src/main/java/org/apache/abdera/protocol/server/impl/BaseResponseContext.java (original)
+++ incubator/abdera/java/branches/0.3.0-incubating/server/src/main/java/org/apache/abdera/protocol/server/impl/BaseResponseContext.java Thu Sep  6 09:30:45 2007
@@ -24,6 +24,8 @@
 import javax.activation.MimeType;
 
 import org.apache.abdera.model.Base;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Element;
 import org.apache.abdera.util.MimeTypeHelper;
 import org.apache.abdera.writer.Writer;
 
@@ -43,8 +45,23 @@
     setStatusText("OK");
     this.chunked = chunked;
     try {
-      setContentType(getContentType().toString());
+      MimeType type = getContentType();
+      String charset = type.getParameter("charset");
+      if (charset == null) charset = getCharsetFromBase(base);
+      if (charset == null) charset = "UTF-8";
+      type.setParameter("charset", charset);
+      setContentType(type.toString());
     } catch (Exception e) {}
+  }
+  
+  @SuppressWarnings("unchecked") private String getCharsetFromBase(Base base) {
+    if (base == null) return null;
+    if (base instanceof Document) {
+      return ((Document)base).getCharset();
+    } else if (base instanceof Element) {
+      return getCharsetFromBase(((Element)base).getDocument());
+    }
+    return null;
   }
   
   public T getBase() {