package be.peopleware.theopenmoviedb.servlet;

import java.io.IOException;
import java.util.List;

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

import be.peopleware.theopenmoviedb.Movie;
import be.peopleware.theopenmoviedb.Util;

public class MovieServlet extends HttpServlet {
	
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json; charset=UTF-8");
		ServletOutputStream out = response.getOutputStream();
		
		String queryId = request.getParameter("id");
		String queryTitle = request.getParameter("title");
		
		if (queryId != null && queryId.length() > 0) {
			handleIdRequest(out, queryId);
		} else if (queryTitle != null && queryTitle.length() >= 3) {
			handleTitleRequest(out, queryTitle);
		} else {
			handleEmptyRequest(out);
		}
		
		out.close();
		
	}
	
	private void handleIdRequest(ServletOutputStream out, String queryId) throws IOException {
		Movie movie = Util.searchForMovie(queryId);
		if (movie != null) {
			out.println("{ identifier: 'movieId', items:[");
			out.print("{title: \"" + movie.getTitle() + "\", movieId: \"" + movie.getId() + "\" }");
			out.println("]}");
		} else {
			handleEmptyRequest(out);
		}
		
	}
	
	private void handleTitleRequest(ServletOutputStream out, String queryTitle) throws IOException {
		
		List<Movie> movies = Util.searchForMovies(queryTitle);
		
		out.println("{ identifier: 'movieId', items:[");
	
		for (int i = 0; i < movies.size(); i++) {
			out.print("{title: \"" + movies.get(i).getTitle() + "\", movieId: \"" + movies.get(i).getId() + "\" }");
			if (i < movies.size() -1)
				out.println(",");
		}
	
		out.println("]}");
		
	}
	
	private void handleEmptyRequest(ServletOutputStream out) throws IOException {
		out.println("{ identifier: 'movieId', items:[ ]}");
	}

}

@@ -60,32 +60,100 @@
 io.IOException;%0A
+import java.io.OutputStreamWriter;%0Aimport java.nio.charset.Charset;%0A
 import java.util
@@ -742,24 +742,109 @@
 utStream();%0A
+%09%09OutputStreamWriter writer = new OutputStreamWriter(out, Charset.forName(%22UTF-8%22));%0A
 %09%09%0A%09%09String 
@@ -1001,27 +1001,30 @@
 leIdRequest(
-out
+writer
 , queryId);%0A
@@ -1108,19 +1108,22 @@
 Request(
-out
+writer
 , queryT
@@ -1154,35 +1154,38 @@
 dleEmptyRequest(
-out
+writer
 );%0A%09%09%7D%0A%09%09%0A%09%09out.
@@ -1180,19 +1180,22 @@
 %09%7D%0A%09%09%0A%09%09
-out
+writer
 .close()
@@ -1226,39 +1226,32 @@
 handleIdRequest(
-Servlet
 OutputStream out
@@ -1238,36 +1238,45 @@
 est(OutputStream
- out
+Writer writer
 , String queryId
@@ -1298,16 +1298,17 @@
 ption %7B%0A
+%0A
 %09%09Movie 
@@ -1371,27 +1371,28 @@
 l) %7B%0A%09%09%09
-out.println
+writer.write
 (%22%7B iden
@@ -1420,33 +1420,36 @@
 ems:%5B%22);%0A%09%09%09
-out.print
+writer.write
 (%22%7Btitle: %5C%22
@@ -1519,27 +1519,28 @@
 %7D%22);%0A%09%09%09
-out.println
+writer.write
 (%22%5D%7D%22);%0A
@@ -1572,19 +1572,22 @@
 Request(
-out
+writer
 );%0A%09%09%7D%0A%09
@@ -1618,39 +1618,32 @@
 dleTitleRequest(
-Servlet
 OutputStream out
@@ -1638,20 +1638,29 @@
 utStream
- out
+Writer writer
 , String
@@ -1758,27 +1758,28 @@
 );%0A%09%09%0A%09%09
-out.println
+writer.write
 (%22%7B iden
@@ -1857,25 +1857,28 @@
 +) %7B%0A%09%09%09
-out.print
+writer.write
 (%22%7Btitle
@@ -1998,27 +1998,28 @@
 -1)%0A%09%09%09%09
-out.println
+writer.write
 (%22,%22);%0A%09
@@ -2025,27 +2025,28 @@
 %09%09%7D%0A%09%0A%09%09
-out.println
+writer.write
 (%22%5D%7D%22);%0A
@@ -2086,23 +2086,16 @@
 Request(
-Servlet
 OutputSt
@@ -2098,20 +2098,29 @@
 utStream
- out
+Writer writer
 ) throws
@@ -2140,19 +2140,20 @@
 %7B%0A%09%09
-out.println
+writer.write
 (%22%7B 

use outputstreamwriter with utf-8 charset instead