abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r443047 - in /incubator/abdera/java/trunk: client/src/main/java/org/apache/abdera/protocol/client/ protocol/src/main/java/org/apache/abdera/protocol/ protocol/src/main/java/org/apache/abdera/protocol/util/ server/src/main/java/org/apache/ab...
Date Wed, 13 Sep 2006 18:02:49 GMT
Author: jmsnell
Date: Wed Sep 13 11:02:48 2006
New Revision: 443047

URL: http://svn.apache.org/viewvc?view=rev&rev=443047
Log:
Per APP Draft -10,

   Clients MAY send non-ASCII characters in the Slug entity-header,
   which they MUST encode using "encoded-words", as defined in
   [RFC2047].  Servers SHOULD treat the slug as [RFC2047] encoded if it
   matches the "encoded-words" production.

Add a method that allows the charset to be specified when setting the Slug header.

  request.setSlug("foo", "utf-8");

Modified request.getSlug() so that it always attempts to decode, just in case the value
is RFC2047-encoded.

Add methods for allowing any header to be encoded/decoded


Added:
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/EncodingUtil.java
Modified:
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Request.java
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractResponseContext.java

Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java?view=diff&rev=443047&r1=443046&r2=443047
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java
(original)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java
Wed Sep 13 11:02:48 2006
@@ -29,6 +29,7 @@
 import org.apache.abdera.protocol.Request;
 import org.apache.abdera.protocol.util.AbstractRequest;
 import org.apache.abdera.protocol.util.CacheControlUtil;
+import org.apache.abdera.protocol.util.EncodingUtil;
 import org.apache.commons.httpclient.util.DateParseException;
 import org.apache.commons.httpclient.util.DateUtil;
 
@@ -116,6 +117,22 @@
     setHeader("Authorization", auth);
   }
   
+  public void setEncodedHeader(String header, String charset, String value) {
+    setHeader(header, EncodingUtil.encode(value,charset));
+  }
+  
+  public void setEncodedHeader(String header, String charset, String... values) {
+    if (values != null && values.length > 0) {
+      for (int n = 0; n < values.length; n++) {
+        values[n] = EncodingUtil.encode(values[n], charset);
+      }
+      List<String> list = Arrays.asList(new String[] {combine(values)});
+      getHeaders().put(header, list);
+    } else {
+      removeHeaders(header);
+    }
+  }
+  
   public void setHeader(String header, String value) {
     if (value != null)
       setHeader(header, new String[] {value});
@@ -138,6 +155,25 @@
     removeHeaders(header);
   }
   
+  public void addEncodedHeader(String header, String charset, String value) {
+    addHeader(header, EncodingUtil.encode(value, charset));
+  }
+  
+  public void addEncodedHeader(String header, String charset, String... values) {
+    if (values == null || values.length == 0) return;
+    for (int n = 0; n < values.length; n++) {
+      values[n] = EncodingUtil.encode(values[n], charset);
+    }
+    List<String> list = getHeaders().get(header);
+    String value = combine(values);
+    if (list != null) {
+      if (!list.contains(value)) 
+        list.add(value);
+    } else {
+      setHeader(header, new String[] {value});
+    }
+  }
+  
   public void addHeader(String header, String value) {
     if (value != null)
       addHeader(header, new String[] {value});
@@ -241,6 +277,10 @@
   
   public void setSlug(String slug) {
     setHeader("Slug", slug);
+  }
+  
+  public void setSlug(String slug, String charset) {
+    setEncodedHeader("Slug", charset, slug);
   }
   
   public void setCacheControl(String cc) {

Modified: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Request.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Request.java?view=diff&rev=443047&r1=443046&r2=443047
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Request.java
(original)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Request.java
Wed Sep 13 11:02:48 2006
@@ -29,7 +29,11 @@
 
   String getHeader(String name);
   
+  String getDecodedHeader(String name);
+  
   List<String> getHeaders(String name);
+  
+  List<String> getDecodedHeaders(String name);
   
   String[] getHeaderNames();
   

Modified: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java?view=diff&rev=443047&r1=443046&r2=443047
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java
(original)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java
Wed Sep 13 11:02:48 2006
@@ -18,6 +18,7 @@
 package org.apache.abdera.protocol.util;
 
 import java.util.Date;
+import java.util.List;
 
 import javax.activation.MimeType;
 import javax.activation.MimeTypeParseException;
@@ -56,7 +57,7 @@
   }
   
   public String getSlug() {
-    return getHeader("Slug");
+    return EncodingUtil.decode(getHeader("Slug"));
   }
 
   public MimeType getContentType() throws MimeTypeParseException {
@@ -143,5 +144,18 @@
   
   public void setOnlyIfCached(boolean val) {
     toggle(val, ONLYIFCACHED);
+  }
+
+  public String getDecodedHeader(String header) {
+    return EncodingUtil.decode(getHeader(header));
+  }
+  
+  public List<String> getDecodedHeaders(String header) {
+    List<String> headers = getHeaders(header);
+    String[] vals = new String[headers.size()];
+    for (int n = 0; n < headers.size(); n++) {
+      vals[n] = EncodingUtil.decode(headers.get(n));
+    }
+    return java.util.Arrays.asList(vals);
   }
 }

Added: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/EncodingUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/EncodingUtil.java?view=auto&rev=443047
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/EncodingUtil.java
(added)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/EncodingUtil.java
Wed Sep 13 11:02:48 2006
@@ -0,0 +1,39 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.util;
+
+import org.apache.commons.codec.net.QCodec;
+
+public class EncodingUtil {
+
+  public static String encode(String value, String charset) {
+    try {
+      return (new QCodec(charset)).encode(value);
+    } catch (Exception e) {
+      return value;
+    }
+  }
+  
+  public static String decode(String value) {
+    try {
+      return (new QCodec()).decode(value);
+    } catch (Exception e) {
+      return value;
+    }
+  }
+}

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractResponseContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractResponseContext.java?view=diff&rev=443047&r1=443046&r2=443047
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractResponseContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractResponseContext.java
Wed Sep 13 11:02:48 2006
@@ -26,6 +26,7 @@
 import org.apache.abdera.protocol.EntityTag;
 import org.apache.abdera.protocol.server.provider.ResponseContext;
 import org.apache.abdera.protocol.util.AbstractResponse;
+import org.apache.abdera.protocol.util.EncodingUtil;
 
 public abstract class AbstractResponseContext
   extends AbstractResponse
@@ -43,6 +44,19 @@
     headers.remove(name);
   }
   
+  public void setEncodedHeader(String name, String charset, String value) {
+    setHeader(name, EncodingUtil.encode(value, charset));
+  }
+  
+  public void setEncodedHeader(String name, String charset, String... vals) {
+    Map<String,List<Object>> headers = getHeaders();
+    List<Object> values = new ArrayList<Object>();
+    for (String value : vals) {
+      values.add(EncodingUtil.encode(value, charset));
+    }
+    headers.put(name, values);
+  }
+  
   public void setHeader(String name, Object value) {
     Map<String,List<Object>> headers = getHeaders();
     List<Object> values = new ArrayList<Object>();
@@ -59,6 +73,22 @@
     headers.put(name, values);
   }
   
+  public void addEncodedHeader(String name, String charset, String value) {
+    addHeader(name, EncodingUtil.encode(value, charset));
+  }
+  
+  public void addEncodedHeaders(String name, String charset, String... vals) {
+    Map<String,List<Object>> headers = getHeaders();
+    List<Object> values = new ArrayList<Object>();
+    if (values == null) {
+      values = new ArrayList<Object>();
+      headers.put(name,values);
+    }
+    for (String value : vals) {
+      values.add(EncodingUtil.encode(value, charset));
+    }
+  }
+  
   public void addHeader(String name, Object value) {
     Map<String,List<Object>> headers = getHeaders();
     List<Object> values = new ArrayList<Object>();
@@ -179,6 +209,10 @@
       return;
     }
     setHeader("Content-Location", uri);
+  }
+  
+  public void setSlug(String slug, String charset) {
+    setSlug(EncodingUtil.encode(slug, charset));
   }
   
   public void setSlug(String slug) {



Mime
View raw message