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 2008/02/01 02:03:19 UTC
svn commit: r617319 - in
/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server:
filters/ servlet/
Author: jmsnell
Date: Thu Jan 31 17:03:12 2008
New Revision: 617319
URL: http://svn.apache.org/viewvc?rev=617319&view=rev
Log:
refactor the compressionservlet and methodoverrideservlet as instances of the new Abdera Filter interface. This simplifies their implementation and eliminates one additional dependency on the servlet api
Added:
incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/filters/
incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/filters/CompressionFilter.java
incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/filters/MethodOverrideFilter.java
Removed:
incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaFilter.java
incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbstractFilter.java
incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/CompressionFilter.java
incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/MethodOverrideFilter.java
Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/filters/CompressionFilter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/filters/CompressionFilter.java?rev=617319&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/filters/CompressionFilter.java (added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/filters/CompressionFilter.java Thu Jan 31 17:03:12 2008
@@ -0,0 +1,118 @@
+/*
+* 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.server.filters;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.zip.DeflaterOutputStream;
+
+import org.apache.abdera.protocol.server.Filter;
+import org.apache.abdera.protocol.server.FilterChain;
+import org.apache.abdera.protocol.server.ProviderHelper;
+import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.ResponseContext;
+import org.apache.abdera.protocol.server.context.ResponseContextWrapper;
+import org.apache.abdera.util.CompressionUtil;
+import org.apache.abdera.util.CompressionUtil.CompressionCodec;
+import org.apache.abdera.writer.Writer;
+
+public class CompressionFilter
+ implements Filter {
+
+ public ResponseContext filter(
+ RequestContext request,
+ FilterChain chain) {
+ String encoding = request.getHeader("Accept-Encoding");
+ String[] encodings =
+ encoding != null ?
+ ProviderHelper.orderByQ(encoding) :
+ new String[0];
+ for (String enc : encodings) {
+ try {
+ CompressionCodec codec = CompressionCodec.valueOf(enc.toUpperCase());
+ return new CompressingResponseContextWrapper(chain.next(request), codec);
+ } catch (Exception e) {}
+ }
+ return chain.next(request);
+ }
+
+ /**
+ * A HttpServletResponseWrapper implementation that applies GZip or Deflate
+ * compression to response output.
+ */
+ public static class CompressingResponseContextWrapper
+ extends ResponseContextWrapper {
+
+ private final CompressionCodec codec;
+
+ public CompressingResponseContextWrapper(
+ ResponseContext response,
+ CompressionCodec codec) {
+ super(response);
+ this.codec = codec;
+ }
+
+ private OutputStream wrap(OutputStream out) {
+ return new CompressingOutputStream(codec, out);
+ }
+
+ public void writeTo(
+ OutputStream out,
+ Writer writer)
+ throws IOException {
+ out = wrap(out);
+ super.writeTo(out, writer);
+ out.flush();
+ }
+
+ public void writeTo(
+ OutputStream out)
+ throws IOException {
+ out = wrap(out);
+ super.writeTo(out);
+ out.flush();
+ }
+ }
+
+ public static class CompressingOutputStream
+ extends FilterOutputStream {
+
+ public CompressingOutputStream(
+ CompressionCodec codec,
+ OutputStream out) {
+ super(initStream(codec,out));
+ }
+
+ public CompressingOutputStream(DeflaterOutputStream dout) {
+ super(dout);
+ }
+
+ private static OutputStream initStream(
+ CompressionCodec codec,
+ OutputStream out) {
+ try {
+ return CompressionUtil.getEncodedOutputStream(out, codec);
+ } catch (Exception e) {
+ return out;
+ }
+ }
+
+ }
+
+}
Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/filters/MethodOverrideFilter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/filters/MethodOverrideFilter.java?rev=617319&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/filters/MethodOverrideFilter.java (added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/filters/MethodOverrideFilter.java Thu Jan 31 17:03:12 2008
@@ -0,0 +1,82 @@
+/*
+* 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.server.filters;
+
+import java.util.Arrays;
+
+import org.apache.abdera.protocol.server.Filter;
+import org.apache.abdera.protocol.server.FilterChain;
+import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.ResponseContext;
+import org.apache.abdera.protocol.server.context.RequestContextWrapper;
+
+public class MethodOverrideFilter
+ implements Filter {
+
+ private String[] methods;
+
+ public MethodOverrideFilter() {
+ this("DELETE","PUT");
+ }
+
+ public MethodOverrideFilter(String... methods) {
+ setMethods(methods);
+ }
+
+ public String[] getMethods() {
+ return methods;
+ }
+
+ public void setMethods(String... methods) {
+ this.methods = methods;
+ Arrays.sort(methods);
+ }
+
+ public ResponseContext filter(
+ RequestContext request,
+ FilterChain chain) {
+ return chain.next(
+ new MethodOverrideRequestContext(
+ request));
+ }
+
+ private class MethodOverrideRequestContext
+ extends RequestContextWrapper {
+
+ private final String method;
+
+ public MethodOverrideRequestContext(
+ RequestContext request) {
+ super(request);
+ String method = super.getMethod();
+ String xheader = getHeader("X-HTTP-Method-Override");
+ if (xheader == null) xheader = getHeader("X-Method-Override");
+ if (xheader != null) xheader = xheader.toUpperCase().trim();
+ if (method.equals("POST") &&
+ xheader != null &&
+ Arrays.binarySearch(methods,xheader) > -1) {
+ method = xheader;
+ }
+ this.method = method;
+ }
+
+ public String getMethod() {
+ return method;
+ }
+ }
+}