summaryrefslogtreecommitdiff
path: root/src/com/p4square/fmfacade/FreeMarkerPageResource.java
blob: 46b0ec516a43a8129f33ffd087bea6a7b258cf2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * 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 <jesse@jesterpm.net>
 */
public class FreeMarkerPageResource extends ServerResource {
    private static Logger cLog = Logger.getLogger(FreeMarkerPageResource.class);

    public static Map<String, Object> baseRootObject(Context context) {
        Map<String, Object> root = new HashMap<String, Object>();

        root.put("get", new GetMethod(context.getClientDispatcher()));

        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<String, Object> getRootObject() {
        Map<String, Object> root = baseRootObject(getContext());

        root.put("attributes", getRequestAttributes());
        root.put("query", getQuery().getValuesMap());
        
        if (getClientInfo().isAuthenticated()) {
            final User user = getClientInfo().getUser();
            final Map<String, String> userMap = new HashMap<String, String>();
            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;
    }
}