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 2007/07/31 00:04:10 UTC
svn commit: r561145 - in /incubator/abdera/java/trunk:
client/src/main/java/org/apache/abdera/protocol/client/
core/src/main/java/org/apache/abdera/util/
protocol/src/main/java/org/apache/abdera/protocol/
protocol/src/main/java/org/apache/abdera/protoc...
Author: jmsnell
Date: Mon Jul 30 15:04:09 2007
New Revision: 561145
URL: http://svn.apache.org/viewvc?view=rev&rev=561145
Log:
Use the EntityTag class in the Request/Response objects
Modified:
incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java
incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java
incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Request.java
incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java
incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractResponseContext.java
Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java?view=diff&rev=561145&r1=561144&r2=561145
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java (original)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/RequestOptions.java Mon Jul 30 15:04:09 2007
@@ -32,6 +32,7 @@
import org.apache.abdera.protocol.util.AbstractRequest;
import org.apache.abdera.protocol.util.CacheControlUtil;
import org.apache.abdera.protocol.util.EncodingUtil;
+import org.apache.abdera.util.EntityTag;
import org.apache.abdera.util.Messages;
import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;
@@ -288,28 +289,56 @@
* Sets the value of the HTTP If-Match header
*/
public void setIfMatch(String entity_tag) {
- setHeader("If-Match", entity_tag);
+ setIfMatch(new EntityTag(entity_tag));
+ }
+
+ /**
+ * Sets the value of the HTTP If-Match header
+ */
+ public void setIfMatch(EntityTag entity_tag) {
+ setHeader("If-Match", entity_tag.toString());
+ }
+
+ /**
+ * Sets the value of the HTTP If-Match header
+ */
+ public void setIfMatch(EntityTag... entity_tags) {
+ setHeader("If-Match", EntityTag.toString(entity_tags));
}
/**
* Sets the value of the HTTP If-Match header
*/
public void setIfMatch(String... entity_tags) {
- setHeader("If-Match", entity_tags);
+ setHeader("If-Match", EntityTag.toString(entity_tags));
}
/**
* Sets the value of the HTTP If-None-Match header
*/
public void setIfNoneMatch(String entity_tag) {
- setHeader("If-None-Match", entity_tag);
+ setIfNoneMatch(new EntityTag(entity_tag));
+ }
+
+ /**
+ * Sets the value of the HTTP If-None-Match header
+ */
+ public void setIfNoneMatch(EntityTag entity_tag) {
+ setHeader("If-None-Match", entity_tag.toString());
+ }
+
+ /**
+ * Sets the value of the HTTP If-None-Match header
+ */
+ public void setIfNoneMatch(EntityTag... entity_tags) {
+ setHeader("If-None-Match", EntityTag.toString(entity_tags));
}
/**
* Sets the value of the HTTP If-None-Match header
*/
public void setIfNoneMatch(String... entity_tags) {
- setHeader("If-None-Match", entity_tags);
+ setHeader("If-None-Match", EntityTag.toString(entity_tags));
}
/**
Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java?view=diff&rev=561145&r1=561144&r2=561145
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java Mon Jul 30 15:04:09 2007
@@ -1,20 +1,16 @@
/*
-* 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.
-*/
+ * 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.util;
import java.io.Serializable;
@@ -29,20 +25,20 @@
/**
* Implements an EntityTag.
*/
-public class EntityTag
- implements Cloneable,
- Serializable {
+public class EntityTag implements Cloneable, Serializable {
private static final long serialVersionUID = 1559972888659121461L;
- private static final String INVALID_ENTITY_TAG = Messages.get("INVALID.ENTITY.TAG");
+
+ private static final String INVALID_ENTITY_TAG = Messages
+ .get("INVALID.ENTITY.TAG");
public static EntityTag parse(String entity_tag) {
- if (entity_tag == null || entity_tag.length() == 0)
- throw new IllegalArgumentException(INVALID_ENTITY_TAG);
+ if (entity_tag == null || entity_tag.length() == 0)
+ throw new IllegalArgumentException(INVALID_ENTITY_TAG);
boolean weak = entity_tag.startsWith("W/");
- if (!weak && !entity_tag.startsWith("\""))
+ if (!weak && !entity_tag.startsWith("\""))
throw new IllegalArgumentException(INVALID_ENTITY_TAG);
- String tag = entity_tag.substring((weak)?3:1, entity_tag.length() - 1);
+ String tag = entity_tag.substring((weak) ? 3 : 1, entity_tag.length() - 1);
return new EntityTag(tag, weak);
}
@@ -57,84 +53,89 @@
}
return etags.toArray(new EntityTag[etags.size()]);
}
-
+
public static boolean matchesAny(EntityTag tag1, String tags) {
return matchesAny(tag1, parseTags(tags), false);
}
-
+
public static boolean matchesAny(EntityTag tag1, String tags, boolean weak) {
return matchesAny(tag1, parseTags(tags), weak);
}
-
+
public static boolean matchesAny(String tag1, String tags) {
return matchesAny(parse(tag1), parseTags(tags), false);
}
-
+
public static boolean matchesAny(String tag1, String tags, boolean weak) {
return matchesAny(parse(tag1), parseTags(tags), weak);
}
-
+
public static boolean matchesAny(EntityTag tag1, EntityTag[] tags) {
- return matchesAny(tag1,tags,false);
+ return matchesAny(tag1, tags, false);
}
-
- public static boolean matchesAny(EntityTag tag1, EntityTag[] tags, boolean weak) {
- if (tags == null) return (tag1 == null) ? true : false;
+
+ public static boolean matchesAny(EntityTag tag1, EntityTag[] tags,
+ boolean weak) {
+ if (tags == null)
+ return (tag1 == null) ? true : false;
for (EntityTag tag : tags) {
- if (tag1.equals(tag)) return true;
+ if (tag1.equals(tag))
+ return true;
}
return false;
}
-
+
public static boolean matches(EntityTag tag1, EntityTag tag2) {
return tag1.equals(tag2);
}
-
+
public static boolean matches(String tag1, String tag2) {
EntityTag etag1 = parse(tag1);
EntityTag etag2 = parse(tag2);
return etag1.equals(etag2);
}
-
+
public static boolean matches(EntityTag tag1, String tag2) {
return tag1.equals(tag2);
}
-
+
private final String tag;
+
private final boolean weak;
-
+
public EntityTag(String tag) {
- this(tag,false);
+ this(tag, false);
}
-
+
public EntityTag(String tag, boolean weak) {
checkTag(tag);
this.tag = tag;
this.weak = weak;
}
-
+
private void checkTag(String tag) {
- if (tag.indexOf('"') != -1)
- throw new IllegalArgumentException(INVALID_ENTITY_TAG);
+ if (tag.startsWith("\"") ||
+ (tag.endsWith("\"") && !tag.endsWith("\\\"")) )
+ throw new IllegalArgumentException("Invalid Entity Tag");
}
-
+
public String getTag() {
return tag;
}
-
+
public boolean isWeak() {
return weak;
}
-
+
public String toString() {
StringBuffer buf = new StringBuffer();
- if (weak) buf.append("W/");
+ if (weak)
+ buf.append("W/");
buf.append('"');
buf.append(tag);
buf.append('"');
return buf.toString();
}
-
public int hashCode() {
final int PRIME = 31;
@@ -149,8 +150,8 @@
return true;
if (obj == null)
return false;
- if (obj instanceof String)
- obj = new EntityTag((String)obj);
+ if (obj instanceof String)
+ obj = new EntityTag((String) obj);
if (getClass() != obj.getClass())
return false;
final EntityTag other = (EntityTag) obj;
@@ -163,43 +164,72 @@
return false;
return true;
}
-
+
@Override
- protected Object clone()
- throws CloneNotSupportedException {
- return super.clone();
+ protected Object clone() throws CloneNotSupportedException {
+ return super.clone();
}
-
/**
- * Utility method for generating ETags. Works by concatenating the UTF-8
- * bytes of the provided strings then generating an MD5 hash of the result.
+ * Utility method for generating ETags. Works by concatenating the UTF-8 bytes
+ * of the provided strings then generating an MD5 hash of the result.
*/
public static EntityTag generate(String... material) {
String etag = null;
try {
MessageDigest md = MessageDigest.getInstance("md5");
for (String s : material) {
- if (s != null) md.update(s.getBytes("utf-8"));
+ if (s != null)
+ md.update(s.getBytes("utf-8"));
}
byte[] digest = md.digest();
etag = new String(Hex.encodeHex(digest));
} catch (NoSuchAlgorithmException e) {
- throw new UnsupportedOperationException(
- Messages.get("HASHING.NOT.AVAILABLE"));
+ throw new UnsupportedOperationException(Messages
+ .get("HASHING.NOT.AVAILABLE"));
} catch (UnsupportedEncodingException e) {
// should not happen
throw new RuntimeException(Messages.get("UTF8.NOT.SUPPORTED"), e);
}
return new EntityTag(etag);
}
-
+
/**
- * Checks that the passed in ETag matches the ETag generated by the
- * generate method
+ * Checks that the passed in ETag matches the ETag generated by the generate
+ * method
*/
public static boolean matches(EntityTag etag, String... material) {
EntityTag etag2 = generate(material);
- return EntityTag.matches(etag,etag2);
+ return EntityTag.matches(etag, etag2);
+ }
+
+ public static EntityTag[] parseList(String list) {
+ if (list == null || list.length() == 0)
+ return new EntityTag[0];
+ String[] tags = list.split("((?<=\")\\s*,\\s*(?=\"))");
+ List<EntityTag> etags = new ArrayList<EntityTag>();
+ for (String tag : tags) {
+ etags.add(EntityTag.parse(tag));
+ }
+ return etags.toArray(new EntityTag[etags.size()]);
+ }
+
+ public static String toString(EntityTag... tags) {
+ StringBuffer buf = new StringBuffer();
+ for (EntityTag tag : tags) {
+ if (buf.length() > 0) buf.append(", ");
+ buf.append(tag.toString());
+ }
+ return buf.toString();
+ }
+
+ public static String toString(String ... tags) {
+ StringBuffer buf = new StringBuffer();
+ for (String tag : tags) {
+ if (buf.length() > 0) buf.append(", ");
+ EntityTag etag = new EntityTag(tag);
+ buf.append(etag.toString());
+ }
+ return buf.toString();
}
}
Modified: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Request.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Request.java?view=diff&rev=561145&r1=561144&r2=561145
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Request.java (original)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Request.java Mon Jul 30 15:04:09 2007
@@ -24,6 +24,7 @@
import javax.activation.MimeTypeParseException;
import org.apache.abdera.protocol.util.ProtocolConstants;
+import org.apache.abdera.util.EntityTag;
public interface Request extends ProtocolConstants {
@@ -55,11 +56,11 @@
Date getDateHeader(String name);
- String getIfMatch();
+ EntityTag[] getIfMatch();
Date getIfModifiedSince();
- String getIfNoneMatch();
+ EntityTag[] getIfNoneMatch();
Date getIfUnmodifiedSince();
Modified: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java?view=diff&rev=561145&r1=561144&r2=561145
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java (original)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/util/AbstractRequest.java Mon Jul 30 15:04:09 2007
@@ -25,6 +25,7 @@
import org.apache.abdera.i18n.iri.Escaping;
import org.apache.abdera.protocol.Request;
+import org.apache.abdera.util.EntityTag;
public abstract class AbstractRequest implements Request {
@@ -66,16 +67,16 @@
return (value != null) ? new MimeType(value) : null;
}
- public String getIfMatch() {
- return getHeader("If-Match");
+ public EntityTag[] getIfMatch() {
+ return EntityTag.parseList(getHeader("If-Match"));
}
public Date getIfModifiedSince() {
return getDateHeader("If-Modified-Since");
}
- public String getIfNoneMatch() {
- return getHeader("If-None-Match");
+ public EntityTag[] getIfNoneMatch() {
+ return EntityTag.parseList(getHeader("If-None-Match"));
}
public Date getIfUnmodifiedSince() {
Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractResponseContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractResponseContext.java?view=diff&rev=561145&r1=561144&r2=561145
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractResponseContext.java (original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractResponseContext.java Mon Jul 30 15:04:09 2007
@@ -240,7 +240,11 @@
}
setHeader("Content-Type", type);
}
-
+
+ public void setEntityTag(String etag) {
+ setEntityTag(new EntityTag(etag));
+ }
+
public void setEntityTag(EntityTag etag) {
if (etag == null) {
removeHeader("ETag");