package com.tubeonfire.controller;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tubeonfire.entity.TubesFeed;
import com.tubeonfire.service.TubeService;
import com.tubeonfire.util.JavaCacheHandle;
public class TubeSearchController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "public, max-age=86400");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
response.setHeader("Expires", cal.getTime().toString());
// search by input key.
String[] splitted = request.getRequestURI().split("/");
String key = null;
int currentPage = 1;
if (splitted.length == 3) {
key = URLDecoder.decode(splitted[2], "UTF-8");
} else if (splitted.length == 4) {
key = URLDecoder.decode(splitted[2], "UTF-8");
try {
currentPage = Integer.parseInt(splitted[3]);
} catch (Exception e) {
currentPage = 1;
}
} else {
response.sendRedirect("/home");
}
try {
System.out.println("Keyword to search : " + key);
TubesFeed result = TubeService.searchByKey(key, currentPage, 1);
request.setAttribute("url", request.getRequestURL());
request.setAttribute("listCategory", JavaCacheHandle.getCategory());
request.setAttribute("listChannel", JavaCacheHandle.getChannels());
request.setAttribute("result", result);
request.setAttribute("key", key);
request.getRequestDispatcher("/tube_show.jsp")
.forward(request, response);
} catch (Exception e) {
response.sendRedirect("/home");
}
}
}