wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r935437 - in /incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common: internal/providers/header/AcceptCharsetHeaderDelegate.java internal/utils/MediaTypeUtils.java utils/ProviderUtils.java
Date Mon, 19 Apr 2010 02:47:37 GMT
Author: bluk
Date: Mon Apr 19 02:47:37 2010
New Revision: 935437

URL: http://svn.apache.org/viewvc?rev=935437&view=rev
Log:
Add caching for charsets

Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegate.java
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/MediaTypeUtils.java
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/utils/ProviderUtils.java

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegate.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegate.java?rev=935437&r1=935436&r2=935437&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegate.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/AcceptCharsetHeaderDelegate.java
Mon Apr 19 02:47:37 2010
@@ -26,10 +26,18 @@ import java.util.List;
 import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
 
 import org.apache.wink.common.internal.http.AcceptCharset;
+import org.apache.wink.common.internal.utils.SoftConcurrentMap;
 
 public class AcceptCharsetHeaderDelegate implements HeaderDelegate<AcceptCharset> {
+    private static final SoftConcurrentMap<String, AcceptCharset> cache =
+                                                                            new SoftConcurrentMap<String,
AcceptCharset>();
 
     public AcceptCharset fromString(String value) throws IllegalArgumentException {
+        AcceptCharset cached = cache.get(value);
+        if (cached != null) {
+            return cached;
+        }
+
         List<String> acceptable = new LinkedList<String>();
         List<String> banned = new LinkedList<String>();
         boolean anyAllowed = (value == null);
@@ -53,7 +61,8 @@ public class AcceptCharsetHeaderDelegate
                 }
             }
         }
-        return new AcceptCharset(value, acceptable, banned, anyAllowed, vCharsets);
+        return cache
+            .put(value, new AcceptCharset(value, acceptable, banned, anyAllowed, vCharsets));
     }
 
     private List<AcceptCharset.ValuedCharset> parseAcceptCharset(String acceptableCharsetValue)
{

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/MediaTypeUtils.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/MediaTypeUtils.java?rev=935437&r1=935436&r2=935437&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/MediaTypeUtils.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/MediaTypeUtils.java
Mon Apr 19 02:47:37 2010
@@ -21,9 +21,7 @@ package org.apache.wink.common.internal.
 
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.HashMap;
 import java.util.LinkedHashSet;
-import java.util.Map;
 import java.util.Set;
 
 import javax.ws.rs.core.HttpHeaders;
@@ -267,26 +265,24 @@ public class MediaTypeUtils {
                 .debug("Media Type not explicitly set on Response so going to correct charset
parameter if necessary"); //$NON-NLS-1$
             if (ProviderUtils.getCharsetOrNull(mediaType) == null) { //$NON-NLS-1$
                 try {
-                    Map<String, String> params =
-                        new HashMap<String, String>(mediaType.getParameters());
-
                     RuntimeContext context = RuntimeContextTLS.getRuntimeContext();
                     HttpHeaders requestHeaders = null;
                     if (context != null) {
                         requestHeaders = context.getHttpHeaders();
                     }
-                    params.put("charset", ProviderUtils.getCharset(mediaType, requestHeaders));
//$NON-NLS-1$ $NON-NLS-2$
-                    mediaType = new MediaType(mediaType.getType(), mediaType.getSubtype(),
params);
-                    httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType); //$NON-NLS-1$
+                    String charsetValue = ProviderUtils.getCharset(mediaType, requestHeaders);
+                    String newMediaTypeStr = mediaType.toString() + ";charset=" + charsetValue;
+                    mediaType = MediaType.valueOf(newMediaTypeStr);
+                    httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, newMediaTypeStr); //$NON-NLS-1$
                     logger
-                        .debug("Changed media type to be {} in Content-Type HttpHeader",
mediaType); //$NON-NLS-1$
+                        .debug("Changed media type to be {} in Content-Type HttpHeader",
newMediaTypeStr); //$NON-NLS-1$
                 } catch (Exception e) {
                     logger.debug("Caught exception while trying to set the charset", e);
//$NON-NLS-1$
                 }
             }
         }
 
-        logger.debug("setDefaultCharsetOnMediaTypeHeader() exit"); //$NON-NLS-1$
+        logger.debug("setDefaultCharsetOnMediaTypeHeader() exit returning {}", mediaType);
//$NON-NLS-1$
         return mediaType;
     }
 

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/utils/ProviderUtils.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/utils/ProviderUtils.java?rev=935437&r1=935436&r2=935437&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/utils/ProviderUtils.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/utils/ProviderUtils.java
Mon Apr 19 02:47:37 2010
@@ -32,6 +32,8 @@ import java.io.Writer;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
 import java.util.List;
 
 import javax.ws.rs.core.HttpHeaders;
@@ -43,13 +45,21 @@ import javax.ws.rs.ext.Providers;
 
 import org.apache.wink.common.internal.MultivaluedMapImpl;
 import org.apache.wink.common.internal.http.AcceptCharset;
+import org.apache.wink.common.internal.utils.SoftConcurrentMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class ProviderUtils {
-    private static final Logger logger          = LoggerFactory.getLogger(ProviderUtils.class);
-
-    private static final String DEFAULT_CHARSET = "UTF-8";
+    private static final Logger                       logger            =
+                                                                            LoggerFactory
+                                                                                .getLogger(ProviderUtils.class);
+
+    private static final String                       DEFAULT_CHARSET   = "UTF-8";
+
+    private static SoftConcurrentMap<String, Boolean> validCharsets     =
+                                                                            new SoftConcurrentMap<String,
Boolean>();
+    private static SoftConcurrentMap<String, String>  preferredCharsets =
+                                                                            new SoftConcurrentMap<String,
String>();
 
     public static String getCharsetOrNull(MediaType m) {
         String name = (m == null) ? null : m.getParameters().get("charset"); //$NON-NLS-1$
@@ -79,9 +89,9 @@ public class ProviderUtils {
         if (requestHeaders == null) {
             logger
                 .debug("getCharset() returning {} since requestHeaders was null", DEFAULT_CHARSET);
-            return DEFAULT_CHARSET; //$NON-NLS-1$
+            return DEFAULT_CHARSET;
         }
-        AcceptCharset charsets = null;
+
         List<String> acceptableCharsets =
             requestHeaders.getRequestHeader(HttpHeaders.ACCEPT_CHARSET);
         if (acceptableCharsets == null || acceptableCharsets.isEmpty()) {
@@ -89,7 +99,7 @@ public class ProviderUtils {
             // charset is acceptable so we'll stick with UTF-8 by default.
             logger.debug("getCharset() returning {} since no Accept-Charset header",
                          DEFAULT_CHARSET);
-            return DEFAULT_CHARSET; //$NON-NLS-1$
+            return DEFAULT_CHARSET;
         }
 
         StringBuilder acceptCharsetsTemp = new StringBuilder();
@@ -100,14 +110,48 @@ public class ProviderUtils {
         }
         String acceptCharsets = acceptCharsetsTemp.toString();
         logger.debug("acceptCharsets combined value is {}", acceptCharsets);
-        charsets = AcceptCharset.valueOf(acceptCharsets);
+        String cached = preferredCharsets.get(acceptCharsets);
+        if (cached != null) {
+            return cached;
+        }
+        AcceptCharset charsets = AcceptCharset.valueOf(acceptCharsets);
+
+        if (charsets.isAnyCharsetAllowed()) {
+            preferredCharsets.put(acceptCharsets, DEFAULT_CHARSET);
+            return DEFAULT_CHARSET;
+        }
 
         List<String> orderedCharsets = charsets.getAcceptableCharsets();
         logger.debug("orderedCharsets is {}", orderedCharsets);
         if (!orderedCharsets.isEmpty()) {
-            String charset = orderedCharsets.get(0);
-            logger.debug("getCharset() returning {} since highest Accept-Charset value",
charset);
-            return charset;
+            for (int c = 0; c < orderedCharsets.size(); ++c) {
+                String charset = orderedCharsets.get(c);
+                try {
+                    Boolean b = validCharsets.get(charset);
+                    if (b != null && b.booleanValue()) {
+                        logger
+                            .debug("getCharset() returning {} since highest Accept-Charset
value",
+                                   charset);
+                        preferredCharsets.put(acceptCharsets, charset);
+                        return charset;
+                    }
+                    Charset.forName(charset);
+                    validCharsets.put(charset, Boolean.TRUE);
+                    logger.debug("getCharset() returning {} since highest Accept-Charset
value",
+                                 charset);
+                    preferredCharsets.put(acceptCharsets, charset);
+                    return charset;
+                } catch (IllegalCharsetNameException e) {
+                    logger.debug("IllegalCharsetNameException for {}", charset, e);
+                    validCharsets.put(charset, Boolean.FALSE);
+                } catch (UnsupportedCharsetException e) {
+                    logger.debug("UnsupportedCharsetException for {}", charset, e);
+                    validCharsets.put(charset, Boolean.FALSE);
+                } catch (IllegalArgumentException e) {
+                    logger.debug("IllegalArgumentException for {}", charset, e);
+                    validCharsets.put(charset, Boolean.FALSE);
+                }
+            }
         }
         // At this point, it's either any charset is allowed (i.e. wildcard "*"
         // has a higher quality value than any other charset sent in the
@@ -116,7 +160,8 @@ public class ProviderUtils {
         // charset.
         logger.debug("getCharset() returning {} since no explicit charset required",
                      DEFAULT_CHARSET);
-        return DEFAULT_CHARSET; //$NON-NLS-1$
+        preferredCharsets.put(acceptCharsets, DEFAULT_CHARSET);
+        return DEFAULT_CHARSET;
     }
 
     public static Reader createReader(InputStream stream, MediaType mediaType) {



Mime
View raw message