Permalink
Browse files

Cleaned up some code.

  • Loading branch information...
1 parent c8bf760 commit 3e1bfafbd83a8f66dac934c4f6c700632cc5a6f2 @amolenaar amolenaar committed Jan 25, 2012
@@ -33,7 +33,9 @@ public void waitForReadyToSend() {
public boolean isReadyToSend() { return isReadyToSend.isTrue(); }
- protected void addSpecificHeaders() {
+ @Override
+ protected void addStandardHeaders() {
+ super.addStandardHeaders();
if (!dontChunk)
addHeader("Transfer-Encoding", "chunked");
}
@@ -30,7 +30,9 @@ public void readyToSend(ResponseSender sender) throws IOException {
}
}
- protected void addSpecificHeaders() {
+ @Override
+ protected void addStandardHeaders() {
+ super.addStandardHeaders();
addHeader("Content-Length", getContentSize() + "");
}
@@ -8,6 +8,8 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
import java.util.TimeZone;
public abstract class Response {
@@ -85,8 +87,6 @@ public boolean isJavaFormat(){
public abstract void readyToSend(ResponseSender sender) throws IOException;
- protected abstract void addSpecificHeaders();
-
public abstract int getContentSize();
public int getStatus() {
@@ -155,17 +155,13 @@ public String getHeader(String key) {
}
void makeHeaders(StringBuffer text) {
- for (Iterator<String> iterator = headers.keySet().iterator(); iterator
- .hasNext();) {
- String key = iterator.next();
- String value = headers.get(key);
- text.append(key).append(": ").append(value).append(CRLF);
+ for (Entry<String, String> entry: headers.entrySet()) {
+ text.append(entry.getKey()).append(": ").append(entry.getValue()).append(CRLF);
}
}
protected void addStandardHeaders() {
addHeader("Content-Type", getContentType());
- addSpecificHeaders();
}
protected String getReasonPhrase() {
@@ -63,9 +63,6 @@ public MockResponse(String formatString) {
public void readyToSend(ResponseSender sender) {
}
- protected void addSpecificHeaders() {
- }
-
public int getContentSize() {
return 0;
}
@@ -5,6 +5,8 @@
import java.io.IOException;
import java.nio.ByteBuffer;
+import util.StringUtil;
+
public class SimpleResponse extends Response {
private byte[] content = new byte[0];
@@ -18,9 +20,10 @@ public SimpleResponse(int status) {
@Override
public void readyToSend(ResponseSender sender) {
- byte[] bytes = getBytes();
+ addStandardHeaders();
try {
- sender.send(bytes);
+ sender.send(makeHttpHeaders().getBytes());
+ sender.send(content);
} finally {
sender.close();
}
@@ -48,26 +51,14 @@ public String getContent() {
return content;
}
- public String getText() {
- return new String(getBytes());
- }
-
- public byte[] getBytes() {
- addStandardHeaders();
- byte[] headerBytes = makeHttpHeaders().getBytes();
- ByteBuffer bytes = ByteBuffer.allocate(headerBytes.length
- + getContentSize());
- bytes.put(headerBytes).put(content);
- return bytes.array();
- }
-
@Override
public int getContentSize() {
return content.length;
}
@Override
- protected void addSpecificHeaders() {
+ protected void addStandardHeaders() {
+ super.addStandardHeaders();
addHeader("Content-Length", String.valueOf(getContentSize()));
}
}
@@ -9,15 +9,17 @@
import util.RegexTestCase;
public class SimpleResponseTest extends RegexTestCase implements ResponseSender {
+ private StringBuffer buffer;
private String text;
private boolean closed = false;
public void send(byte[] bytes) {
try {
- text = new String(bytes, "UTF-8");
+ buffer.append(new String(bytes, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
+ text = buffer.toString();
}
public void close() {
@@ -29,6 +31,8 @@ public Socket getSocket() {
}
public void setUp() throws Exception {
+ buffer = new StringBuffer();
+ text = null;
}
public void tearDown() throws Exception {

0 comments on commit 3e1bfaf

Please sign in to comment.