<%@ page import="org.primeframework.mvc.action.ActionInvocation" %>
<%@ page import="org.primeframework.mvc.action.ActionInvocationStore" %>
<%@ page import="org.primeframework.mvc.servlet.PrimeServletContextListener" %>
<%@ page import="org.primeframework.mvc.servlet.ServletObjectsHolder" %>
<%@ page import="com.google.inject.Injector" %>
<%@ page import="org.primeframework.mvc.action.result.ForwardResult" %>
<%@ page import="org.primeframework.mvc.action.result.ForwardResult.ForwardImpl" %>
<%@ page session="false" %>
<%
  Injector injector = (Injector) pageContext.getServletContext().getAttribute(PrimeServletContextListener.GUICE_INJECTOR_KEY);
  if (ServletObjectsHolder.getServletContext() == null) {
    ServletObjectsHolder.setServletContext(config.getServletContext());
  }
  if (ServletObjectsHolder.getServletRequest() == null) {
    ServletObjectsHolder.setServletRequest(new HttpServletRequestWrapper(request));
  }
  if (ServletObjectsHolder.getServletResponse() == null) {
    ServletObjectsHolder.setServletResponse(response);
  }

  try {
    ActionInvocation action = new ActionInvocation(null, null, "/errors/404", null, null);
    ActionInvocationStore store = injector.getInstance(ActionInvocationStore.class);
    store.setCurrent(action);

    ForwardResult forward = injector.getInstance(ForwardResult.class);
    forward.execute(new ForwardImpl("/templates/errors/404.ftl", "error", "text/html; charset=UTF-8", 404));
  } finally {
    ServletObjectsHolder.clearServletRequest();
    ServletObjectsHolder.clearServletResponse();
  }

// Note, we can delete this code if we want. It would be cool to ge this working so we can theme these pages.
// Once we are at master, if we delete this, then it will at least be in Git History if we want to revisit this.
// ------------------------------------------------------------------------------------------------------------
//  try {
//    // See if we can find a client_id or tenantId in the request.
//    String clientId = request.getParameter("client_id");
//    String tenantIdString = request.getParameter("tenantId");
//    UUID tenantId = null;
//    if (tenantIdString != null) {
//      try {
//        tenantId = UUID.fromString(tenantIdString);
//      } catch (Exception ignore) {
//      }
//    }
//
//    // Resolve the tenant and theme
//    FrontEndThemeResolver resolver = injector.getInstance(FrontEndThemeResolver.class);
//    Result result = resolver.resolve(request, clientId, tenantId, false);
//
//    ActionInvocationStore store = injector.getInstance(ActionInvocationStore.class);
//    store.setCurrent(new ActionInvocation(new ThemedErrorAction(result.theme.id), null, "/errors/404", null, null));
//
//    ThemedForwardResult forward = injector.getInstance(ThemedForwardResult.class);
//    forward.execute(new ThemedForwardImpl("/templates/errors/404.ftl", "error", "text/html; charset=UTF-8", 404));
//
//  } finally {
//    ServletObjectsHolder.clearServletRequest();
//    ServletObjectsHolder.clearServletResponse();
//  }
%>
