summaryrefslogtreecommitdiff
path: root/src/com/p4square/fmfacade/ftl/GetMethod.java
blob: a47c4b0f2e6fa48df108b537738b2b4797941ef3 (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
/*
 * Copyright 2013 Jesse Morgan
 */

package com.p4square.fmfacade.ftl;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import java.io.IOException;

import freemarker.core.Environment;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

import org.apache.log4j.Logger;

import org.restlet.data.Status;
import org.restlet.data.Method;
import org.restlet.representation.Representation;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;

import org.restlet.ext.jackson.JacksonRepresentation;

/**
 * This method allows templates to make GET requests.
 *
 * @author Jesse Morgan <jesse@jesterpm.net>
 */
public class GetMethod implements TemplateMethodModel {
    private static final Logger cLog = Logger.getLogger(GetMethod.class);

    private final Restlet mDispatcher;

    public GetMethod(Restlet dispatcher) {
        mDispatcher = dispatcher;
    }

    /**
     * @param args List with exactly two arguments:
     *              * The variable in which to put the result.
     *              * The URI to GET.
     */
    public TemplateModel exec(List args) throws TemplateModelException {
        final Environment env = Environment.getCurrentEnvironment();

        if (args.size() != 2) {
            throw new TemplateModelException(
                    "Expecting exactly one argument containing the URI");
        }

        Request request = new Request(Method.GET, (String) args.get(1));
        Response response = mDispatcher.handle(request);
        Status status = response.getStatus();
        Representation representation = response.getEntity();

        try {
            if (response.getStatus().isSuccess()) {
                JacksonRepresentation<Map> mapRepresentation;
                if (representation instanceof JacksonRepresentation) {
                    mapRepresentation = (JacksonRepresentation<Map>) representation;
                } else {
                    mapRepresentation = new JacksonRepresentation<Map>(
                            representation, Map.class);
                }
                try {
                    TemplateModel mapModel = env.getObjectWrapper().wrap(mapRepresentation.getObject());

                    env.setVariable((String) args.get(0), mapModel);

                } catch (IOException e) {
                    cLog.warn("Exception occurred when calling getObject(): " 
                            + e.getMessage(), e);
                    status = Status.SERVER_ERROR_INTERNAL;
                }
            }

            Map statusMap = new HashMap();
            statusMap.put("code", status.getCode());
            statusMap.put("reason", status.getReasonPhrase());
            statusMap.put("succeeded", status.isSuccess());
            return env.getObjectWrapper().wrap(statusMap);
        } finally {
            if (representation != null) {
                representation.release();
            }
        }
    }
}