package br.com.caelum.rest.client.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpClient;
import org.json.JSONException;
import org.json.JSONWriter;

import br.com.caelum.rest.client.http.HttpMethod;
import br.com.caelum.rest.client.http.HttpMethodWrapper;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;

@Resource
public class IndexController {

	private final HttpServletResponse response;
	private final Result result;

	@Path("/")
	public void index() {
	}

	public IndexController(HttpServletResponse response, Result result) {
		this.response = response;
		this.result = result;
	}

	@Path("/createSomething")
	@Get
	public void createSomething(HttpMethod method, String uri, String contentName, String contentValue) throws JSONException, IOException {
		HttpClient client = new HttpClient();


		HttpMethodWrapper httpMethod = method.getHttpMethod(uri);

		httpMethod.addParameter(contentName, contentValue);
		int resultCode = httpMethod.executeMethod(client);

		String response = httpMethod.getResponseBodyAsString();

		JSONWriter json = new JSONWriter(this.response.getWriter()).object();

		json.key("response").value(response);
		json.key("responseCode").value(resultCode);
		json.key("method").value(method);
		json.key("uri").value(uri);
		if (resultCode == 201) {
			String location = httpMethod.getResponseHeader("location").getValue();
			json.key("location").value(location);
		}
		json.endObject();
		result.use(Results.nothing());
	}

	@Path("/grab")
	@Get
	public void grab(String uri) throws JSONException, IOException {
		HttpClient client = new HttpClient();


		HttpMethodWrapper httpMethod = HttpMethod.GET.getHttpMethod(uri);

		int resultCode = httpMethod.executeMethod(client);

		String response = httpMethod.getResponseBodyAsString();

		JSONWriter json = new JSONWriter(this.response.getWriter()).object();

		json.key("response").value(response);
		json.key("responseCode").value(resultCode);
		json.key("uri").value(uri);
		json.endObject();
		result.use(Results.nothing());
	}
}

@@ -1009,33 +1009,32 @@
  HttpClient();%0A%0A
-%0A
 %09%09HttpMethodWrap
@@ -1177,33 +1177,32 @@
 Method(client);%0A
-%0A
 %09%09String respons
@@ -1268,56 +1268,17 @@
 n = 
-new JSONWriter(this.response.getWriter()).object
+getWriter
 ();%0A
@@ -1600,41 +1600,8 @@
 ();%0A
-%09%09result.use(Results.nothing());%0A
 %09%7D%0A%0A
@@ -1729,17 +1729,16 @@
 ent();%0A%0A
-%0A
 %09%09HttpMe
@@ -1851,17 +1851,16 @@
 lient);%0A
-%0A
 %09%09String
@@ -1934,36 +1934,26 @@
 n = 
-new JSON
+get
 Writer(
-this.
 response
 .get
@@ -1952,29 +1952,20 @@
 onse
-.getWriter()).object(
+, resultCode
 );%0A%0A
@@ -2105,41 +2105,143 @@
 );%0A%09
-%09result.use(Results.nothing());%0A%09%7D
+%7D%0A%09%0A%09private JSONWriter getWriter() %7B%0A%09%09result.use(Results.nothing());%0A%09%09return new JSONWriter(this.response.getWriter()).object();%0A%09%7D%0A%09
 %0A%7D%0A

refactor method out