Return-Path: Delivered-To: apmail-incubator-abdera-commits-archive@locus.apache.org Received: (qmail 72231 invoked from network); 8 Sep 2006 01:12:36 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 8 Sep 2006 01:12:36 -0000 Received: (qmail 74722 invoked by uid 500); 8 Sep 2006 01:12:36 -0000 Delivered-To: apmail-incubator-abdera-commits-archive@incubator.apache.org Received: (qmail 74713 invoked by uid 500); 8 Sep 2006 01:12:36 -0000 Mailing-List: contact abdera-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: abdera-dev@incubator.apache.org Delivered-To: mailing list abdera-commits@incubator.apache.org Received: (qmail 74704 invoked by uid 99); 8 Sep 2006 01:12:36 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Sep 2006 18:12:36 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Sep 2006 18:12:35 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 73DFD1A981A; Thu, 7 Sep 2006 18:12:15 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Fri, 08 Sep 2006 01:12:15 -0000 To: abdera-commits@incubator.apache.org From: jmsnell@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20060908011215.73DFD1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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; + } +}