com.playtech.portal.platform.portlet.spring.InternalResourceViewResolver.java Source code

Java tutorial

Introduction

Here is the source code for com.playtech.portal.platform.portlet.spring.InternalResourceViewResolver.java

Source

package com.playtech.portal.platform.portlet.spring;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.servlet.view.InternalResourceView;

import javax.portlet.PortletRequest;
import java.io.File;
import java.util.Deque;
import java.util.LinkedList;

import static java.util.Arrays.asList;
import static org.apache.commons.lang.StringUtils.join;
import static org.apache.commons.lang.StringUtils.split;

public class InternalResourceViewResolver
        extends org.springframework.web.servlet.view.InternalResourceViewResolver {

    private String licenseePackageName = "licensee";

    protected String viewNameForLicensee(String viewName) {
        String[] pathParts = split(viewName, "/");
        if (pathParts == null)
            return null;

        Deque<String> deque = new LinkedList<>(asList(pathParts));
        String view = deque.pollLast();
        deque.offer(licenseePackageName);
        deque.offer(view);
        return join(deque, "/");
    }

    @Override
    protected AbstractUrlBasedView buildView(String viewName) throws Exception {
        String resultingViewName = viewNameForLicensee(viewName);

        File reqFile = new File(getPortletRequest().getPortletSession().getPortletContext()
                .getRealPath(this.getPrefix() + resultingViewName + this.getSuffix()));
        if (!reqFile.exists()) {
            resultingViewName = viewName;
        }

        InternalResourceView view = (InternalResourceView) super.buildView(resultingViewName);
        view.setAlwaysInclude(false);
        return view;
    }

    public void setLicenseePackageName(String licenseePackageName) {
        this.licenseePackageName = licenseePackageName;
    }

    private PortletRequest getPortletRequest() {
        return ((org.springframework.web.portlet.context.PortletRequestAttributes) RequestContextHolder
                .currentRequestAttributes()).getRequest();
    }
}