/* * Copyright 2013 Jesse Morgan */ package com.p4square.fmfacade; import java.util.Map; import java.util.HashMap; import freemarker.template.Template; import org.restlet.Context; import org.restlet.data.MediaType; import org.restlet.data.Status; import org.restlet.ext.freemarker.TemplateRepresentation; import org.restlet.representation.Representation; import org.restlet.resource.ServerResource; import org.restlet.security.User; import org.apache.log4j.Logger; import com.p4square.fmfacade.ftl.GetMethod; import com.p4square.session.Session; import com.p4square.session.Sessions; /** * * @author Jesse Morgan */ public class FreeMarkerPageResource extends ServerResource { private static Logger cLog = Logger.getLogger(FreeMarkerPageResource.class); public static Map baseRootObject(final Context context, final FMFacade fmf) { Map root = new HashMap(); root.put("get", new GetMethod(context.getClientDispatcher())); root.put("config", fmf.getConfig()); return root; } private FMFacade mFMF; private String mCurrentPage; @Override public void doInit() { mFMF = (FMFacade) getApplication(); mCurrentPage = getReference().getRemainingPart(false, false); } protected Representation get() { try { Template t = mFMF.getTemplate("pages" + mCurrentPage + ".ftl"); if (t == null) { setStatus(Status.CLIENT_ERROR_NOT_FOUND); return null; } return new TemplateRepresentation(t, getRootObject(), MediaType.TEXT_HTML); } catch (Exception e) { cLog.fatal("Could not render page: " + e.getMessage(), e); setStatus(Status.SERVER_ERROR_INTERNAL); return null; } } /** * Build and return the root object to pass to the FTL Template. * @return A map of objects and methods for the template to access. */ protected Map getRootObject() { Map root = baseRootObject(getContext(), mFMF); root.put("attributes", getRequestAttributes()); root.put("query", getQuery().getValuesMap()); if (getClientInfo().isAuthenticated()) { final User user = getClientInfo().getUser(); final Map userMap = new HashMap(); userMap.put("id", user.getIdentifier()); userMap.put("firstName", user.getFirstName()); userMap.put("lastName", user.getLastName()); userMap.put("email", user.getEmail()); root.put("user", userMap); } Session s = Sessions.getInstance().get(getRequest()); if (s != null) { root.put("session", s.getMap()); } return root; } }