com.threewks.thundr.view.velocity.VelocityViewResolver.java Source code

Java tutorial

Introduction

Here is the source code for com.threewks.thundr.view.velocity.VelocityViewResolver.java

Source

/*
 * This file is a component of thundr, a software library from 3wks.
 * Read more: http://3wks.github.io/thundr/
 * Copyright (C) 2014 3wks, <thundr@3wks.com.au>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.threewks.thundr.view.velocity;

import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import com.threewks.thundr.request.Request;
import com.threewks.thundr.request.Response;
import com.threewks.thundr.view.BaseView;
import com.threewks.thundr.view.GlobalModel;
import com.threewks.thundr.view.Model;
import com.threewks.thundr.view.ViewResolutionException;
import com.threewks.thundr.view.ViewResolver;

import jodd.util.StringPool;

public class VelocityViewResolver implements ViewResolver<VelocityView> {

    private GlobalModel globalModel;
    private VelocityEngine velocityEngine;

    public VelocityViewResolver(VelocityEngine velocityEngine, GlobalModel globalModel) {
        this.globalModel = globalModel;
        this.velocityEngine = velocityEngine;
    }

    public VelocityEngine getVelocityEngine() {
        return velocityEngine;
    }

    public GlobalModel getGlobalModel() {
        return globalModel;
    }

    @Override
    public void resolve(Request req, Response resp, VelocityView viewResult) {
        String view = viewResult.getView();
        String characterEncoding = StringUtils.defaultString(viewResult.getCharacterEncoding(), "UTF-8");

        try {
            Map<String, Object> model = mergeModels(req, viewResult);
            VelocityContext context = new VelocityContext(model);
            Writer stringWriter = new StringWriter();
            velocityEngine.mergeTemplate(view, StringPool.UTF_8, context, stringWriter);
            String content = stringWriter.toString();
            BaseView.applyToResponse(viewResult, resp);
            byte[] data = content.getBytes(characterEncoding);
            resp.withContentLength(data.length);
            resp.getOutputStream().write(data);
        } catch (Exception e) {
            throw new ViewResolutionException(e, "Failed to render velocity template '%s': %s", view,
                    e.getMessage());
        }
    }

    protected Map<String, Object> mergeModels(Request req, VelocityView viewResult) {
        return Model.combine(globalModel, req.getAllData(), viewResult.getModel());
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}