package foodtruck.server;

import java.io.IOException;

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

import com.google.common.base.Strings;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import foodtruck.model.Location;
import foodtruck.util.Clock;

/**
 * Servlet that serves up the main food truck page.
 * @author aviolette
 * @since Jul 12, 2011
 */
@Singleton
public class FoodTruckServlet extends HttpServlet {
  private final Location mapCenter;
  private final DateTimeFormatter timeFormatter;
  private final Clock clock;
  private final DateTimeFormatter dateFormatter;

  @Inject
  public FoodTruckServlet( DateTimeZone zone, @Named("center") Location centerLocation,
      Clock clock) {
    this.clock = clock;
    this.mapCenter = centerLocation;
    this.timeFormatter = DateTimeFormat.forPattern("YYYYMMdd-HHmm").withZone(zone);
    this.dateFormatter = DateTimeFormat.forPattern("EEE MMM dd, YYYY");
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    final String path = req.getRequestURI();
    if (!Strings.isNullOrEmpty(path)) {
      req.setAttribute("showScheduleFor", path.substring(1));
    }
    final String timeRequest = req.getParameter("time");
    DateTime dateTime = null;
    if (!Strings.isNullOrEmpty(timeRequest)) {
      try {
        dateTime = timeFormatter.parseDateTime(timeRequest);
      } catch (IllegalArgumentException ignored) {
      }
    }
    if (dateTime == null) {
      dateTime = clock.now();
    }
    req.setAttribute("center", mapCenter);
    String googleAnalytics = System.getProperty("foodtruck.google.analytics", null);
    if (googleAnalytics != null) {
      req.setAttribute("google_analytics_ua", googleAnalytics);
    }
    final String mode = req.getParameter("mode");
    req.setAttribute("mobile", "mobile".equals(mode));
    req.setAttribute("requestDate", dateFormatter.print(dateTime));
    req.setAttribute("requestTime", timeFormatter.print(dateTime));
    req.setAttribute("requestTimeInMillis", dateTime.getMillis());
    req.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(req, resp);
  }
}

@@ -47,16 +47,49 @@
 ception;
+%0Aimport java.util.logging.Logger;
 %0A%0Aimport
@@ -950,16 +950,104 @@
 rmatter;
+%0A  private static final Logger log = Logger.getLogger(FoodTruckServlet.class.getName());
 %0A%0A  @Inj
@@ -1076,17 +1076,16 @@
 Servlet(
- 
 DateTime
@@ -1551,24 +1551,214 @@
 questURI();%0A
+    if (req.getServerName().contains(%22chifoodtruckz.com%22)) %7B%0A      resp.setStatus(301);%0A      resp.setHeader(%22Location%22, %22http://www.chicagofoodtruckfinder.com%22 + path);%0A      return;%0A    %7D%0A
     if (!Str

Redirect chifoodtruckz.com to chicagofoodtruckfinder.com