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 2006/09/08 03:12:15 UTC

svn commit: r441315 - in /incubator/abdera/java/trunk: client/src/main/java/org/apache/abdera/protocol/client/CommonsResponse.java protocol/src/main/java/org/apache/abdera/protocol/util/ContentEncodingUtil.java

Author: jmsnell
Date: Thu Sep  7 18:12:14 2006
New Revision: 441315

URL: http://svn.apache.org/viewvc?view=rev&rev=441315
Log:
Move the code for supporting Content-Encodings (e.g. gzip, deflate) to a utility class
in the protocol module.

Added:
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/ContentEncodingUtil.java
Modified:
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsResponse.java

Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsResponse.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsResponse.java?view=diff&rev=441315&r1=441314&r2=441315
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsResponse.java (original)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/CommonsResponse.java Thu Sep  7 18:12:14 2006
@@ -24,11 +24,8 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.zip.GZIPInputStream;
-import java.util.zip.InflaterInputStream;
-import java.util.zip.ZipInputStream;
 
-import org.apache.abdera.protocol.util.CacheControlParser;
+import org.apache.abdera.protocol.util.ContentEncodingUtil;
 import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HttpMethod;
 import org.apache.commons.httpclient.URIException;
@@ -119,25 +116,10 @@
 
   public InputStream getInputStream() throws IOException {
     if (in == null) {
-      in = method.getResponseBodyAsStream();
       String ce = getHeader("Content-Encoding");
-      if (ce != null) {
-        // multiple encodings may be applied, they're listed in the order
-        // they were applied, so we need to walk the list backwards
-        String[] encodings = CacheControlParser.splitAndTrim(ce, ",", false);
-        for (int n = encodings.length -1; n >= 0; n--) {
-          if ("gzip".equalsIgnoreCase(encodings[n]) ||
-              "x-gzip".equalsIgnoreCase(encodings[n])) {
-            in = new GZIPInputStream(in);
-          } else if ("deflate".equalsIgnoreCase(encodings[n])) {
-            in = new InflaterInputStream(in);
-          } else if ("zip".equalsIgnoreCase(encodings[n])) {
-            in = new ZipInputStream(in);
-          } else {
-            throw new IOException("Unsupported Content-Encoding");
-          }
-        }
-      } 
+      in = method.getResponseBodyAsStream();
+      if (ce != null)
+        in = ContentEncodingUtil.getDecodingInputStream(in, ce);
     }
     return super.getInputStream();
   }

Added: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/ContentEncodingUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/ContentEncodingUtil.java?view=auto&rev=441315
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/ContentEncodingUtil.java (added)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/ContentEncodingUtil.java Thu Sep  7 18:12:14 2006
@@ -0,0 +1,61 @@
+/*
+* 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 java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.zip.DeflaterOutputStream;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+import java.util.zip.InflaterInputStream;
+
+public class ContentEncodingUtil {
+
+  public enum ContentEncoding { GZIP, XGZIP, DEFLATE }
+  
+  public static OutputStream getEncodedOutputStream(OutputStream out, ContentEncoding encoding) throws IOException {
+    return getEncodedOutputStream(out, new ContentEncoding[] {encoding});
+  }
+  
+  public static OutputStream getEncodedOutputStream(OutputStream out, ContentEncoding... encodings) throws IOException {
+    for (ContentEncoding encoding:encodings) {
+      switch(encoding) {
+        case GZIP:
+          out = new GZIPOutputStream(out); break;
+        case DEFLATE:
+          out = new DeflaterOutputStream(out); break;
+      }
+    }
+    return out;
+  }
+  
+  public static InputStream getDecodingInputStream(InputStream in, String ce) throws IOException {
+    String[] encodings = CacheControlParser.splitAndTrim(ce, ",", false);
+    for (int n = encodings.length -1; n >= 0; n--) {
+      switch(ContentEncoding.valueOf(encodings[n].toUpperCase().replaceAll("-", ""))) {
+        case GZIP:
+        case XGZIP:
+          in = new GZIPInputStream(in); break;
+        case DEFLATE:
+          in = new InflaterInputStream(in); break;
+      }
+    }
+    return in;
+  }
+}