Fix hashcode/equals issue in HeadersRequestCondition

This commit is contained in:
Rossen Stoyanchev
2014-08-19 15:16:41 -04:00
parent 5cd1e6aff4
commit 0e49040707
3 changed files with 17 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -64,6 +64,8 @@ abstract class AbstractNameValueExpression<T> implements NameValueExpression<T>
return this.isNegated;
}
protected abstract boolean isCaseSensitiveName();
protected abstract T parseValue(String valueExpression);
public final boolean match(HttpServletRequest request) {
@@ -88,7 +90,9 @@ abstract class AbstractNameValueExpression<T> implements NameValueExpression<T>
}
if (obj != null && obj instanceof AbstractNameValueExpression) {
AbstractNameValueExpression<?> other = (AbstractNameValueExpression<?>) obj;
return ((this.name.equalsIgnoreCase(other.name)) &&
String thisName = isCaseSensitiveName() ? this.name : this.name.toLowerCase();
String otherName = isCaseSensitiveName() ? other.name : other.name.toLowerCase();
return ((thisName.equalsIgnoreCase(otherName)) &&
(this.value != null ? this.value.equals(other.value) : other.value == null) &&
this.isNegated == other.isNegated);
}
@@ -97,7 +101,7 @@ abstract class AbstractNameValueExpression<T> implements NameValueExpression<T>
@Override
public int hashCode() {
int result = name.hashCode();
int result = isCaseSensitiveName() ? name.hashCode() : name.toLowerCase().hashCode();
result = 31 * result + (value != null ? value.hashCode() : 0);
result = 31 * result + (isNegated ? 1 : 0);
return result;

View File

@@ -139,6 +139,11 @@ public final class HeadersRequestCondition extends AbstractRequestCondition<Head
super(expression);
}
@Override
protected boolean isCaseSensitiveName() {
return false;
}
@Override
protected String parseValue(String valueExpression) {
return valueExpression;
@@ -153,14 +158,6 @@ public final class HeadersRequestCondition extends AbstractRequestCondition<Head
protected boolean matchValue(HttpServletRequest request) {
return value.equals(request.getHeader(name));
}
@Override
public int hashCode() {
int result = name.toLowerCase().hashCode();
result = 31 * result + (value != null ? value.hashCode() : 0);
result = 31 * result + (isNegated ? 1 : 0);
return result;
}
}
}

View File

@@ -131,6 +131,11 @@ public final class ParamsRequestCondition extends AbstractRequestCondition<Param
super(expression);
}
@Override
protected boolean isCaseSensitiveName() {
return true;
}
@Override
protected String parseValue(String valueExpression) {
return valueExpression;