package com.breakersoft.plow.http;

import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.thrift.TException;
import org.apache.thrift.TSerializer;
import org.apache.thrift.protocol.TSimpleJSONProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.breakersoft.plow.thrift.JobFilterT;
import com.breakersoft.plow.thrift.JobT;
import com.breakersoft.plow.thrift.dao.ThriftJobDao;

/**
 * A controller for exposing the REST interface.
 *
 * Collection URI: /plow/jobs
 * Element URI: /plow/jobs/jobid
 *
 * @author chambers
 *
 */
@Controller
public class RestController {

	private static final Logger logger = LoggerFactory.getLogger(RestController.class);

    @Autowired
    private ThriftJobDao thriftJobDao;

	@RequestMapping(value = "/jobs", method = RequestMethod.GET)
	public void jobs(HttpServletRequest request, HttpServletResponse response) throws IOException, TException {
		final TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
		final ServletOutputStream stream = response.getOutputStream();
		response.setContentType("application/json");
		stream.print("[\n");
		for (JobT o: thriftJobDao.getJobs(new JobFilterT())) {
			stream.print(serializer.toString(o));
		}
		stream.print("]");
	}
}

@@ -56,16 +56,39 @@
 ception;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -1255,16 +1255,412 @@
 esponse)
+%0A%09%09%09throws IOException, TException %7B%0A%09%09// TODO: implement filtering via query string.%0A%09%09serializeOut(response, thriftJobDao.getJobs(new JobFilterT()));%0A%09%7D%0A%0A%09/**%0A%09 * Serialize a list TBase objects out through an HttpServletResponse.%0A%09 *%0A%09 * @param response%0A%09 * @param items%0A%09 * @throws IOException%0A%09 * @throws TException%0A%09 */%0A%09private void serializeOut(HttpServletResponse response, List%3C?%3E items)
  throws 
@@ -1685,16 +1685,17 @@
 ption %7B%0A
+%0A
 %09%09final 
@@ -2008,16 +2008,16 @@
 ));%0A%09%09%7D%0A
-
 %09%09stream
@@ -2029,13 +2029,31 @@
 t(%22%5D%22);%0A
+%09%09stream.flush();%0A
 %09%7D%0A%7D%0A

Simplify the rest controller.