package tech.greenfield.vertx.irked;

import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.impl.RoutingContextDecorator;
import tech.greenfield.vertx.irked.status.OK;

/**
 * Request handling wrapper which adds some useful routines for
 * API writers.
 * 
 * Can serve as a basis for local context parsers that API writers
 * can use to expose path arguments from parent prefixes
 * 
 * @author odeda
 */
public class Request extends RoutingContextDecorator {

	private RoutingContext outerContext;

	public Request(RoutingContext outerContext) {
		super(outerContext.currentRoute(), outerContext);
		this.outerContext = outerContext;
	}

	@Override
	public void fail(int statusCode) { 
		// we're overriding the fail handlers, which for some reason the decorator 
		// feels should be moved to another thread. Instead, use the outer implementation
		// and let it do what's right
		this.outerContext.fail(statusCode);
	}

	@Override
	public void fail(Throwable throwable) {
		// we're overriding the fail handlers, which for some reason the decorator 
		// feels should be moved to another thread. Instead, use the outer implementation
		// and let it do what's right
		this.outerContext.fail(throwable);
	}
	
	/**
	 * Helper method to terminate request processing with a success (200 OK) response containing a JSON body.
	 * @param json {@link JsonObject} containing the output to encode
	 */
	public void sendJSON(JsonObject json) {
		sendJSON(json, new OK());
	}
	
	/**
	 * Helper method to terminate request processing with a custom response containing a JSON body and the specified
	 * status line.
	 * @param json {@link JsonObject} containing the output to encode
	 * @param status HTTP status to send
	 */
	public void sendJSON(JsonObject json, HttpError status) {
		String content = json.encode();
		response(status)
				.putHeader("Content-Type", "application/json")
				.putHeader("Content-Length", String.valueOf(content.length()))
				.end(content);
	}
	
	/**
	 * Helper method to terminate request processing with an HTTP error (non-200 OK) response.
	 * The resulting HTTP response will have the correct status line and an application/json content
	 * with a JSON encoded object containing the fields "status" set to "false" and "message" set
	 * to the {@link HttpError}'s message.
	 * @param err
	 */
	public void sendError(HttpError err) {
		sendJSON(new JsonObject().put("status", false).put("message", err.getMessage()), err);
	}

	/**
	 * Helper method to generate response with the specified HTTP status
	 * @param status HTTP status code and text to set on the response
	 * @return HTTP response created using {@link RoutingContext#response()}
	 */
	public HttpServerResponse response(HttpError status) {
		return response().setStatusCode(status.getStatusCode()).setStatusMessage(status.getStatusText());
	}

}

@@ -77,16 +77,53 @@
 sponse;%0A
+import io.vertx.core.json.JsonArray;%0A
 import i
@@ -1416,32 +1416,37 @@
 00 OK) response 
+%0A%09 * 
 containing a JSO
@@ -1677,16 +1677,21 @@
 esponse 
+%0A%09 * 
 containi
@@ -1722,20 +1722,16 @@
 pecified
-%0A%09 *
  status 
@@ -1911,61 +1911,652 @@
 %7B%0A%09%09
-String content = json.encode();%0A%09%09response(
+sendContent(json.encode(), status);%0A%09%7D%0A%09%0A%09/**%0A%09 * Helper method to terminate request processing with a custom response %0A%09 * containing a JSON body and the specified status line.%0A%09 * @param json %7B@link JsonArray%7D containing the output to encode%0A%09 * @param status HTTP status to send%0A%09 */%0A%09public void sendJSON(JsonArray json, HttpError status) %7B%0A%09%09sendContent(json.encode(), status);%0A%09%7D%0A%09%0A%09/**%0A%09 * Helper method to terminate request processing with a custom response%0A%09 * containing some text and the specifeid status line.%0A%09 * @param content%0A%09 * @param status%0A%09 */%0A%09public void sendContent(String content, HttpError 
 status)
+ %7B
 %0A%09%09
+response(status)%0A
 %09%09.p
@@ -2600,18 +2600,16 @@
 /json%22)%0A
-%09%09
 %09%09.putHe
@@ -2665,18 +2665,16 @@
 gth()))%0A
-%09%09
 %09%09.end(c

add sendJson to send arrays