Skip to content

Commit fd3b13e

Browse files
committed
Add text and image endpoints
Tested with go-httpbin.
1 parent 6116cea commit fd3b13e

File tree

6 files changed

+89
-8
lines changed

6 files changed

+89
-8
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ httpbin.org.
1111

1212
Java httpbin supports a subset of httpbin endpoints:
1313

14-
| Endpoint | Description |
15-
| -------------------------------------- | -------------------------------------------------------------------- |
16-
| `/get` | Returns GET data. |
17-
| `/post` | Returns POST data. |
18-
| `/patch` | Returns PATCH data. |
19-
| `/put` | Returns PUT data. |
20-
| `/status/:code` | Returns given HTTP Status code or random if more than one are given. |
14+
- `/get` Returns GET data.
15+
- `/post` Returns POST data.
16+
- `/patch` Returns PATCH data.
17+
- `/put` Returns PUT data.
18+
- `/status/:code` Returns given HTTP Status code.
19+
- `/redirect/:n` 302 Redirects _n_ times.
20+
- `/redirect-to?url=foo` 302 Redirects to the _foo_ URL.
21+
- `/cookies` Returns the cookies.
22+
- `/cookies/set?name=value` Sets one or more simple cookies.
23+
- `/basic-auth/:user/:passwd` Challenges HTTP Basic Auth.
24+
- `/html` Returns some HTML.
25+
- `/xml` Returns some XML.
26+
- `/image/png` Returns page containing a PNG image.
27+
- `/image/jpeg` Returns page containing a JPEG image.
2128

2229
## Usage
2330

src/main/java/org/gaul/httpbin/HttpBinHandler.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ public void handle(String target, Request baseRequest,
155155
return;
156156
} else if (uri.equals("/anything")) {
157157
servletResponse.setStatus(HttpServletResponse.SC_OK);
158-
baseRequest.setHandled(true);
159158

160159
final JSONObject response = new JSONObject();
161160

@@ -180,6 +179,32 @@ public void handle(String target, Request baseRequest,
180179
respondJSON(servletResponse, os, response);
181180
baseRequest.setHandled(true);
182181
return;
182+
} else if (method.equals("GET") && uri.equals("/image/jpeg")) {
183+
Utils.copy(is, Utils.NULL_OUTPUT_STREAM);
184+
servletResponse.setStatus(HttpServletResponse.SC_OK);
185+
servletResponse.addHeader("Content-Type", "image/jpeg");
186+
copyResource(servletResponse, "/image.jpg");
187+
baseRequest.setHandled(true);
188+
return;
189+
} else if (method.equals("GET") && uri.equals("/image/png")) {
190+
servletResponse.setStatus(HttpServletResponse.SC_OK);
191+
servletResponse.addHeader("Content-Type", "image/png");
192+
copyResource(servletResponse, "/image.png");
193+
baseRequest.setHandled(true);
194+
return;
195+
} else if (method.equals("GET") && uri.equals("/html")) {
196+
servletResponse.setStatus(HttpServletResponse.SC_OK);
197+
servletResponse.addHeader("Content-Type",
198+
"text/html; charset=utf-8");
199+
copyResource(servletResponse, "/text.html");
200+
baseRequest.setHandled(true);
201+
return;
202+
} else if (method.equals("GET") && uri.equals("/xml")) {
203+
servletResponse.setStatus(HttpServletResponse.SC_OK);
204+
servletResponse.addHeader("Content-Type", "application/xml");
205+
copyResource(servletResponse, "/text.xml");
206+
baseRequest.setHandled(true);
207+
return;
183208
}
184209
servletResponse.setStatus(501);
185210
baseRequest.setHandled(true);
@@ -205,4 +230,15 @@ private static void redirectTo(HttpServletResponse response,
205230
response.setHeader("Location", location);
206231
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
207232
}
233+
234+
private void copyResource(HttpServletResponse response, String resource)
235+
throws IOException {
236+
try (InputStream is = getClass().getResourceAsStream(resource)) {
237+
long length = Utils.copy(is, Utils.NULL_OUTPUT_STREAM);
238+
response.setContentLength((int) length);
239+
}
240+
try (InputStream is = getClass().getResourceAsStream(resource)) {
241+
Utils.copy(is, response.getOutputStream());
242+
}
243+
}
208244
}

src/main/resources/image.jpg

34.8 KB
Loading

src/main/resources/image.png

7.9 KB
Loading

src/main/resources/text.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
</head>
5+
<body>
6+
<h1>Herman Melville - Moby-Dick</h1>
7+
8+
<div>
9+
<p>
10+
Availing himself of the mild, summer-cool weather that now reigned in these latitudes, and in preparation for the peculiarly active pursuits shortly to be anticipated, Perth, the begrimed, blistered old blacksmith, had not removed his portable forge to the hold again, after concluding his contributory work for Ahab's leg, but still retained it on deck, fast lashed to ringbolts by the foremast; being now almost incessantly invoked by the headsmen, and harpooneers, and bowsmen to do some little job for them; altering, or repairing, or new shaping their various weapons and boat furniture. Often he would be surrounded by an eager circle, all waiting to be served; holding boat-spades, pike-heads, harpoons, and lances, and jealously watching his every sooty movement, as he toiled. Nevertheless, this old man's was a patient hammer wielded by a patient arm. No murmur, no impatience, no petulance did come from him. Silent, slow, and solemn; bowing over still further his chronically broken back, he toiled away, as if toil were life itself, and the heavy beating of his hammer the heavy beating of his heart. And so it was.—Most miserable! A peculiar walk in this old man, a certain slight but painful appearing yawing in his gait, had at an early period of the voyage excited the curiosity of the mariners. And to the importunity of their persisted questionings he had finally given in; and so it came to pass that every one now knew the shameful story of his wretched fate. Belated, and not innocently, one bitter winter's midnight, on the road running between two country towns, the blacksmith half-stupidly felt the deadly numbness stealing over him, and sought refuge in a leaning, dilapidated barn. The issue was, the loss of the extremities of both feet. Out of this revelation, part by part, at last came out the four acts of the gladness, and the one long, and as yet uncatastrophied fifth act of the grief of his life's drama. He was an old man, who, at the age of nearly sixty, had postponedly encountered that thing in sorrow's technicals called ruin. He had been an artisan of famed excellence, and with plenty to do; owned a house and garden; embraced a youthful, daughter-like, loving wife, and three blithe, ruddy children; every Sunday went to a cheerful-looking church, planted in a grove. But one night, under cover of darkness, and further concealed in a most cunning disguisement, a desperate burglar slid into his happy home, and robbed them all of everything. And darker yet to tell, the blacksmith himself did ignorantly conduct this burglar into his family's heart. It was the Bottle Conjuror! Upon the opening of that fatal cork, forth flew the fiend, and shrivelled up his home. Now, for prudent, most wise, and economic reasons, the blacksmith's shop was in the basement of his dwelling, but with a separate entrance to it; so that always had the young and loving healthy wife listened with no unhappy nervousness, but with vigorous pleasure, to the stout ringing of her young-armed old husband's hammer; whose reverberations, muffled by passing through the floors and walls, came up to her, not unsweetly, in her nursery; and so, to stout Labor's iron lullaby, the blacksmith's infants were rocked to slumber. Oh, woe on woe! Oh, Death, why canst thou not sometimes be timely? Hadst thou taken this old blacksmith to thyself ere his full ruin came upon him, then had the young widow had a delicious grief, and her orphans a truly venerable, legendary sire to dream of in their after years; and all of them a care-killing competency.
11+
</p>
12+
</div>
13+
</body>
14+
</html>

src/main/resources/text.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version='1.0' encoding='us-ascii'?>
2+
3+
<!-- A SAMPLE set of slides -->
4+
5+
<slideshow
6+
title="Sample Slide Show"
7+
date="Date of publication"
8+
author="Yours Truly"
9+
>
10+
11+
<!-- TITLE SLIDE -->
12+
<slide type="all">
13+
<title>Wake up to WonderWidgets!</title>
14+
</slide>
15+
16+
<!-- OVERVIEW -->
17+
<slide type="all">
18+
<title>Overview</title>
19+
<item>Why <em>WonderWidgets</em> are great</item>
20+
<item/>
21+
<item>Who <em>buys</em> WonderWidgets</item>
22+
</slide>
23+
24+
</slideshow>

0 commit comments

Comments
 (0)