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/08/04 19:36:35 UTC
svn commit: r428793 [2/2] - in /incubator/abdera/java/trunk/client: ./ src/
src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/abdera/ src/main/java/org/apache/abdera/protocol/
src/main/java/org/apache/abdera...
Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/CacheControlUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/CacheControlUtil.java?rev=428793&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/CacheControlUtil.java (added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/CacheControlUtil.java Fri Aug 4 10:36:34 2006
@@ -0,0 +1,111 @@
+/*
+* 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.abdera.protocol.client.RequestOptions;
+import org.apache.abdera.protocol.client.ResponseBase;
+
+public class CacheControlUtil {
+
+ private static long value(String val) {
+ return (val != null) ? Long.parseLong(val) : -1;
+ }
+
+ public static void parseCacheControl(
+ String cc,
+ RequestOptions options) {
+ CacheControlParser parser = new CacheControlParser(cc);
+ options.setNoCache(false);
+ options.setNoStore(false);
+ options.setNoTransform(false);
+ options.setOnlyIfCached(false);
+ options.setMaxAge(-1);
+ options.setMaxStale(-1);
+ options.setMinFresh(-1);
+ for (String directive : parser) {
+ if (directive.equalsIgnoreCase("no-cache")) {
+ options.setNoCache(true);
+ } else if (directive.equalsIgnoreCase("no-store"))
+ options.setNoStore(true);
+ else if (directive.equalsIgnoreCase("no-transform"))
+ options.setNoTransform(true);
+ else if (directive.equalsIgnoreCase("only-if-cached"))
+ options.setOnlyIfCached(true);
+ else if (directive.equalsIgnoreCase("max-age"))
+ options.setMaxAge(value(parser.getValue(directive)));
+ else if (directive.equalsIgnoreCase("max-stale"))
+ options.setMaxStale(value(parser.getValue(directive)));
+ else if (directive.equalsIgnoreCase("min-fresh"))
+ options.setMinFresh(value(parser.getValue(directive)));
+ }
+ }
+
+ public static void parseCacheControl(
+ String cc,
+ ResponseBase response) {
+ CacheControlParser parser = new CacheControlParser(cc);
+ response.setNoCache(false);
+ response.setNoStore(false);
+ response.setNoTransform(false);
+ response.setMustRevalidate(false);
+ response.setPrivate(false);
+ response.setPublic(false);
+ response.setMaxAge(-1);
+ for (String directive : parser) {
+ if (directive.equalsIgnoreCase("no-cache")) {
+ response.setNoCache(true);
+ String value = parser.getValue(directive);
+ if (value != null) {
+ String[] headers = splitAndTrim(value, ",", true);
+ response.setNoCacheHeaders(headers);
+ }
+ } else if (directive.equalsIgnoreCase("no-store"))
+ response.setNoStore(true);
+ else if (directive.equalsIgnoreCase("no-transform"))
+ response.setNoTransform(true);
+ else if (directive.equalsIgnoreCase("must-revalidate"))
+ response.setMustRevalidate(true);
+ else if (directive.equalsIgnoreCase("public"))
+ response.setPublic(true);
+ else if (directive.equalsIgnoreCase("private")) {
+ response.setPrivate(true);
+ String value = parser.getValue(directive);
+ if (value != null) {
+ String[] headers = splitAndTrim(value, ",", true);
+ response.setPrivateHeaders(headers);
+ }
+ } else if (directive.equalsIgnoreCase("max-age"))
+ response.setMaxAge(value(parser.getValue(directive)));
+ }
+ }
+
+ private static String unquote(String s) {
+ if (s == null || s.length() == 0) return s;
+ if (s.startsWith("\"")) s = s.substring(1);
+ if (s.endsWith("\"")) s = s.substring(0, s.length() - 1);
+ return s;
+ }
+
+ public static String[] splitAndTrim(String value, String delim, boolean unquote) {
+ String[] headers = (unquote) ? unquote(value).split(delim) : value.split(delim);
+ for (int n = 0; n < headers.length; n++) {
+ headers[n] = headers[n].trim();
+ }
+ return headers;
+ }
+}
Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/CachingInputStream.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/CachingInputStream.java?rev=428793&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/CachingInputStream.java (added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/util/CachingInputStream.java Fri Aug 4 10:36:34 2006
@@ -0,0 +1,89 @@
+/*
+* 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.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * The CachingInputStream will automatically write
+ * read bytes out to an OutputStream.
+ */
+public class CachingInputStream
+ extends FilterInputStream {
+
+ private OutputStream out;
+ private boolean consumeOnClose = true;
+
+ public CachingInputStream(
+ InputStream in,
+ OutputStream out,
+ boolean consumeOnClose) {
+ super(in);
+ this.out = out;
+ this.consumeOnClose = consumeOnClose;
+ }
+
+ protected int write(int n) throws IOException {
+ if (out != null) out.write(n);
+ return n;
+ }
+
+ protected void write(byte[] b, int off, int len) throws IOException {
+ if (out != null) out.write(b, off, len);
+ }
+
+ protected void close_out() throws IOException {
+ if (out != null) out.close();
+ }
+
+ public void flush() throws IOException {
+ if (out != null) out.flush();
+ }
+
+ @Override
+ public int read() throws IOException {
+ int n = write(super.read());
+ if (n == -1) close();
+ return n;
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ int n = super.read(b, off, len);
+ if (n != -1) write(b,0,n);
+ if (n == -1) close();
+ return n;
+ }
+
+ @Override
+ public void close() throws IOException {
+ if (consumeOnClose) {
+ byte[] buf = new byte[1024];
+ int n = -1;
+ while ((n = read(buf)) != -1) {
+ write(buf,0,n);
+ }
+ }
+ flush();
+ close_out();
+ super.close();
+ }
+}