Skip to content

Commit

Permalink
Implemented raw response body control.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Dec 25, 2015
1 parent 99d1942 commit a7986c3
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 9 deletions.
Expand Up @@ -42,6 +42,8 @@ public class HttpUtils implements HttpMetadata {

private static final String PAGE_RELOAD = "<h2>&nbsp;Reloading...</h2><script>location.reload();</script>";

private static final byte[] EMPTY_RESPONSE = {};

public static byte[] serializeLocals(Map<String, Serializable> locals) {
return locals != null ? UTILS.serialize(locals) : null;
}
Expand Down Expand Up @@ -237,6 +239,9 @@ private static void postProcessRedirect(Resp resp) {
if (redirect != null) {
resp.code(303);
resp.headers().put(HttpHeaders.LOCATION.name(), redirect);
if (resp.content() == null && resp.body() == null) {
resp.body(EMPTY_RESPONSE);
}
}
}

Expand Down
Expand Up @@ -33,11 +33,13 @@
import org.rapidoid.buffer.Buf;
import org.rapidoid.cls.Cls;
import org.rapidoid.commons.MediaType;
import org.rapidoid.data.JSON;
import org.rapidoid.http.Req;
import org.rapidoid.http.Resp;
import org.rapidoid.net.abstracts.Channel;
import org.rapidoid.u.U;
import org.rapidoid.util.Constants;
import org.rapidoid.util.UTILS;

@Authors("Nikolche Mihajlovski")
@Since("5.0.2")
Expand Down Expand Up @@ -384,10 +386,10 @@ private void startResponse() {
contentType = U.or(response.contentType(), MediaType.HTML_UTF_8);
}

startResponseRendering(code, contentType);
renderResponseHeaders(code, contentType);
}

private void startResponseRendering(int code, MediaType contentType) {
private void renderResponseHeaders(int code, MediaType contentType) {
http.startResponse(channel, code, isKeepAlive, contentType);

if (response != null || !U.isEmpty(cookies)) {
Expand Down Expand Up @@ -434,24 +436,50 @@ private void onDone() {
startRendering();

if (!wasRendering) {
String err = validateResponse();
if (err != null) {
http.renderBody(channel, 500, MediaType.HTML_UTF_8, err.getBytes());
} else {
http.renderBody(channel, response.code(), response.contentType(), response.content());
}
renderResponseBody();
}

completeResponse();
finish();
}

private void renderResponseBody() {
String err = validateResponse();

if (err != null) {
http.renderBody(channel, 500, MediaType.HTML_UTF_8, err.getBytes());

} else {
byte[] bytes;

if (response.content() != null) {
bytes = serializeResponse();
} else if (response.body() != null) {
bytes = UTILS.toBytes(response.body());
} else {
throw U.notExpected();
}

http.renderBody(channel, response.code(), response.contentType(), bytes);
}
}

private byte[] serializeResponse() {
Object content = response.content();

if (U.eq(response.contentType(), MediaType.JSON_UTF_8)) {
return JSON.stringifyToBytes(content);
} else {
return UTILS.toBytes(content);
}
}

private String validateResponse() {
if (response == null) {
return "Response wasn't provided!";
}

if (response.content() == null && response.redirect() == null) {
if (response.content() == null && response.body() == null && response.redirect() == null) {
return "Response content wasn't provided!";
}

Expand Down
@@ -0,0 +1,58 @@
package org.rapidoid.http;

/*
* #%L
* rapidoid-integration-tests
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import org.junit.Test;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.http.fast.On;
import org.rapidoid.http.fast.ReqHandler;
import org.rapidoid.util.Bufs;

@Authors("Nikolche Mihajlovski")
@Since("5.0.10")
public class HttpRawResponseTest extends HttpTestCommons {

@Test
public void testBytesResponse() {
On.get("/bytes").html(new ReqHandler() {
@Override
public Object handle(Req req) throws Exception {
return req.response().body("ABC".getBytes());
}
});

testGet("/bytes");
}

@Test
public void testByteBufferResponse() {
On.get("/buf").json(new ReqHandler() {
@Override
public Object handle(Req req) throws Exception {
return req.response().body(Bufs.buf("{\"byte-buffer\": true}"));
}
});

testGet("/buf");
}

}
@@ -0,0 +1,8 @@
HTTP/1.1 200 OK
Connection: keep-alive
Server: Rapidoid
Date: XXXXX GMT
Content-Type: application/json; charset=utf-8
Content-Length: 21

{"byte-buffer": true}
@@ -0,0 +1,8 @@
HTTP/1.1 200 OK
Connection: keep-alive
Server: Rapidoid
Date: XXXXX GMT
Content-Type: text/html; charset=utf-8
Content-Length: 3

ABC

0 comments on commit a7986c3

Please sign in to comment.