From 3acbe08eac83453aef5c4fce387cd6709fa22200 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 11 Feb 2009 09:24:28 +0000 Subject: [PATCH] Code clean up --- .../netty/handler/codec/http/HttpMethod.java | 51 ++++++++++++------- .../netty/handler/codec/http/HttpVersion.java | 45 ++++++++++------ 2 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/jboss/netty/handler/codec/http/HttpMethod.java b/src/main/java/org/jboss/netty/handler/codec/http/HttpMethod.java index 82930b7531..17c245051c 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/HttpMethod.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/HttpMethod.java @@ -94,39 +94,52 @@ public class HttpMethod implements Comparable { methodMap.put(CONNECT.toString(), CONNECT); } - public static HttpMethod valueOf(String method) { - if (method == null) { - throw new NullPointerException("method"); + public static HttpMethod valueOf(String name) { + if (name == null) { + throw new NullPointerException("name"); } - method = method.trim().toUpperCase(); - HttpMethod result = methodMap.get(method); + name = name.trim().toUpperCase(); + if (name.length() == 0) { + throw new IllegalArgumentException("empty name"); + } + + HttpMethod result = methodMap.get(name); if (result != null) { return result; } else { - return new HttpMethod(method); + return new HttpMethod(name); } } - private final String method; + private final String name; - public HttpMethod(String method) { - if (method == null) { - throw new NullPointerException("method"); + public HttpMethod(String name) { + if (name == null) { + throw new NullPointerException("name"); } - method = method.trim().toUpperCase(); - - if (method.length() == 0) { - throw new IllegalArgumentException("empty method"); + name = name.trim().toUpperCase(); + if (name.length() == 0) { + throw new IllegalArgumentException("empty name"); } - this.method = method; + for (int i = 0; i < name.length(); i ++) { + if (Character.isISOControl(name.charAt(i))) { + throw new IllegalArgumentException("control character in name"); + } + } + + this.name = name; + } + + public String getName() { + return name; } @Override public int hashCode() { - return method.hashCode(); + return getName().hashCode(); } @Override @@ -136,15 +149,15 @@ public class HttpMethod implements Comparable { } HttpMethod that = (HttpMethod) o; - return method.equals(that.method); + return getName().equals(that.getName()); } @Override public String toString() { - return method; + return getName(); } public int compareTo(HttpMethod o) { - return method.compareTo(o.method); + return getName().compareTo(o.getName()); } } diff --git a/src/main/java/org/jboss/netty/handler/codec/http/HttpVersion.java b/src/main/java/org/jboss/netty/handler/codec/http/HttpVersion.java index 97c7042e23..c79d99a0be 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/HttpVersion.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/HttpVersion.java @@ -42,36 +42,45 @@ public class HttpVersion implements Comparable { public static final HttpVersion HTTP_1_0 = new HttpVersion("HTTP", 1, 0); public static final HttpVersion HTTP_1_1 = new HttpVersion("HTTP", 1, 1); - public static HttpVersion valueOf(String value) { - value = value.toUpperCase(); - if (value.equals("HTTP/1.1")) { + public static HttpVersion valueOf(String text) { + if (text == null) { + throw new NullPointerException("text"); + } + + text = text.trim().toUpperCase(); + if (text.equals("HTTP/1.1")) { return HTTP_1_1; } - if (value.equals("HTTP/1.0")) { + if (text.equals("HTTP/1.0")) { return HTTP_1_0; } - return new HttpVersion(value); + return new HttpVersion(text); } private final String protocolName; private final int majorVersion; private final int minorVersion; - private final String string; + private final String text; - public HttpVersion(String value) { - if (value == null) { - throw new NullPointerException("value"); + public HttpVersion(String text) { + if (text == null) { + throw new NullPointerException("text"); } - Matcher m = VERSION_PATTERN.matcher(value); + text = text.trim().toUpperCase(); + if (text.length() == 0) { + throw new IllegalArgumentException("empty text"); + } + + Matcher m = VERSION_PATTERN.matcher(text); if (!m.matches()) { - throw new IllegalArgumentException("invalid version format: " + value); + throw new IllegalArgumentException("invalid version format: " + text); } protocolName = m.group(1); majorVersion = Integer.parseInt(m.group(2)); minorVersion = Integer.parseInt(m.group(3)); - string = protocolName + '/' + majorVersion + '.' + minorVersion; + this.text = protocolName + '/' + majorVersion + '.' + minorVersion; } public HttpVersion( @@ -86,8 +95,8 @@ public class HttpVersion implements Comparable { } for (int i = 0; i < protocolName.length(); i ++) { - if (Character.isWhitespace(protocolName.charAt(i))) { - throw new IllegalArgumentException("whitespace in protocolName"); + if (Character.isISOControl(protocolName.charAt(i))) { + throw new IllegalArgumentException("control character in protocolName"); } } @@ -101,7 +110,7 @@ public class HttpVersion implements Comparable { this.protocolName = protocolName; this.majorVersion = majorVersion; this.minorVersion = minorVersion; - string = protocolName + '/' + majorVersion + '.' + minorVersion; + text = protocolName + '/' + majorVersion + '.' + minorVersion; } public String getProtocolName() { @@ -116,9 +125,13 @@ public class HttpVersion implements Comparable { return minorVersion; } + public String getText() { + return text; + } + @Override public String toString() { - return string; + return getText(); } @Override