br.com.d4n.ui4entity.mvc.ControllerReturnValueHandler.java Source code

Java tutorial

Introduction

Here is the source code for br.com.d4n.ui4entity.mvc.ControllerReturnValueHandler.java

Source

package br.com.d4n.ui4entity.mvc;

/*
 * #%L
 * ui4entity
 * %%
 * Copyright (C) 2012 - 2015 DevFor Ninjas
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;

import br.com.d4n.ui4entity.UI4Entity;
import br.com.d4n.ui4entity.core.ParserEngine;

public class ControllerReturnValueHandler implements HandlerMethodReturnValueHandler {

    private final HandlerMethodReturnValueHandler delegate;
    private final ParserEngine engine;

    public ControllerReturnValueHandler(HandlerMethodReturnValueHandler delegate, ParserEngine engine) {
        this.delegate = delegate;
        this.engine = engine;
    }

    @Override
    public boolean supportsReturnType(MethodParameter returnType) {
        return delegate.supportsReturnType(returnType);
    }

    @Override
    public void handleReturnValue(Object returnValue, MethodParameter returnType,
            ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

        if (returnType.getMethodAnnotation(UI4Entity.class) != null
                || returnValue.getClass().getAnnotation(UI4Entity.class) != null) {

            returnValue = engine.parse(returnValue);
            returnType = new UI4EntityMethodParameter(returnType, returnValue.getClass());
        }

        delegate.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
    }
}