github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

AlistairIsrael / junit-rules

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 2
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (7)
  • Wiki (2)
  • Graphs
  • Tree: e9377a9

click here to add a description

click here to add a homepage

  • Branches (2)
    • junit-classrules
    • master
  • Tags (7)
    • junit-rules-0.4.2
    • junit-rules-0.4.1
    • junit-rules-0.4
    • junit-interceptors-0.1.1
    • junit-interceptors-0.1
    • 0.3
    • 0.2
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Provides a set of rules that showcase JUnit's @Rule functionality — Read more

  cancel

http://wiki.github.com/AlistairIsrael/junit-rules

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

HttpServerInterceptor now extends ExternalResource, simplified quite a few 
things all around 
AlistairIsrael (author)
Wed Sep 02 19:19:57 -0700 2009
commit  e9377a9d19eb2fc1d6dad35404555407baacb947
tree    e74c4e422e14179d497370a142c7aff3a2f1b18e
parent  3f982aa35ee7ef0bef5f9ad8cf651a25d3e233e4
junit-rules / src / main / java / junit / interceptors / httpserver / SimpleHttpHandler.java src/main/java/junit/interceptors/httpserver/SimpleHttpHandler.java
100644 164 lines (145 sloc) 5.14 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* junit-interceptors: JUnit Interceptors Collection
*
* Copyright (c) 2009 by Alistair A. Israel.
* This software is made available under the terms of the MIT License.
*
* Created Sep 1, 2009
*/
package junit.interceptors.httpserver;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URI;
 
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
 
/**
* Simplifies the effort to write an {@link HttpHandler}.
*
* @author Alistair A. Israel
*/
public abstract class SimpleHttpHandler implements HttpHandler {
 
    /**
* HTTP OK ({@value #HTTP_OK})
* @see HttpURLConnection#HTTP_OK
*/
    public static final int HTTP_OK = HttpURLConnection.HTTP_OK;
 
    private HttpExchange httpExchange;
 
    private ByteArrayOutputStream out;
 
    private PrintWriter pw;
 
    private int responseCodeSent;
 
    /**
* {@inheritDoc}
*
* @see com.sun.net.httpserver.HttpHandler#handle(com.sun.net.httpserver.HttpExchange)
*/
    @Override
    public final void handle(final HttpExchange exchange) throws IOException {
        this.httpExchange = exchange;
        this.out = new ByteArrayOutputStream();
        this.pw = new PrintWriter(out);
        responseCodeSent = -1;
        onGet();
        if (responseCodeSent == -1) {
            sendResponse(HTTP_OK);
        }
    }
 
    /**
* @return the httpExchange
*/
    public final HttpExchange getHttpExchange() {
        return httpExchange;
    }
 
    /**
* Get the request URI
*
* @return the request URI
* @see com.sun.net.httpserver.HttpExchange#getRequestURI()
*/
    protected final URI getRequestURI() {
        return httpExchange.getRequestURI();
    }
 
    /**
* Returns a stream from which the request body can be read. Multiple calls
* to this method will return the same stream. It is recommended that
* applications should consume (read) all of the data from this stream
* before closing it. If a stream is closed before all data has been read,
* then the close() call will read and discard remaining data (up to an
* implementation specific number of bytes).
*
* @return the stream from which the request body can be read.
* @see com.sun.net.httpserver.HttpExchange#getRequestBody()
*/
    protected final InputStream getRequestBody() {
        return httpExchange.getRequestBody();
    }
 
    /**
* Returns an immutable Map containing the HTTP headers that were included
* with this request. The keys in this Map will be the header names, while
* the values will be a List of Strings containing each value that was
* included (either for a header that was listed several times, or one that
* accepts a comma-delimited list of values on a single line). In either of
* these cases, the values for the header name will be presented in the
* order that they were included in the request.
*
* @return a read-only Map which can be used to access request headers
* @see com.sun.net.httpserver.HttpExchange#getRequestHeaders()
*/
    protected final Headers getRequestHeaders() {
        return httpExchange.getRequestHeaders();
    }
 
    /**
* Get the request method
*
* @return the request method
*
* @see com.sun.net.httpserver.HttpExchange#getRequestMethod()
*/
    protected final String getRequestMethod() {
        return httpExchange.getRequestMethod();
    }
 
    /**
* Returns a {@link PrintWriter} to the response buffer used to calculate
* the byte length of the actual HTTP response to be sent later.
*
* @return a {@link PrintWriter}
*/
    protected final PrintWriter getResponse() {
        return this.pw;
    }
 
    /**
* Returns a mutable Map into which the HTTP response headers can be stored
* and which will be transmitted as part of this response. The keys in the
* Map will be the header names, while the values must be a List of Strings
* containing each value that should be included multiple times (in the
* order that they should be included).
*
* @return a writable Map which can be used to set response headers.
*
* @see com.sun.net.httpserver.HttpExchange#getResponseHeaders()
*/
    protected final Headers getResponseHeaders() {
        return httpExchange.getResponseHeaders();
    }
 
    /**
* @throws IOException
* on exception
*/
    protected abstract void onGet() throws IOException;
 
    /**
* @param responseCode
* the HTTP response code to send
* @throws IOException
* on exception
*/
    protected final void sendResponse(final int responseCode) throws IOException {
        pw.flush();
        httpExchange.sendResponseHeaders(responseCode, out.size());
        out.writeTo(httpExchange.getResponseBody());
        httpExchange.close();
        responseCodeSent = responseCode;
    }
}
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server