<commit_before>/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.controller.freemarker;


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

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.SimpleRequestedAction;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAction;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequiresActions;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.DataGetter;
import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.DataGetterUtils;
/**
 * Controller for getting data for pages defined in the display model. 
 * 
 * This controller passes these variables to the template: 
 * page: a map with information about the page from the display model.
 * pageUri: the URI of the page that identifies the page in the model 
 *  (note that this is not the URL address of the page).
 *    
 * See implementations of PageDataGetter for more variables. 
 */
public class PageController extends FreemarkerHttpServlet{
    private static final Log log = LogFactory.getLog(PageController.class);
    
    protected final static String DEFAULT_TITLE = "Page";        
    protected final static String DEFAULT_BODY_TEMPLATE = "emptyPage.ftl";     

    protected static final String DATA_GETTER_MAP = "pageTypeToDataGetterMap";
 
    /**
     * Get the required actions for all the data getters then
     * AND them together.
     */
    @Override
    protected Actions requiredActions(VitroRequest vreq) {
        try {
            Actions pageActs = getActionsForPage( vreq );
            Actions dgActs = getActionsForDataGetters( vreq );

            if( pageActs == null && dgActs == null){
                return Actions.AUTHORIZED;
            }else if( pageActs == null && dgActs != null ){
                return dgActs;
            }else{
                return pageActs;
            }                
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            log.debug(e);
            return Actions.UNAUTHORIZED;
        }                
    }

    /**
     * Get all the required actions directly required for the page.
     */
    private Actions getActionsForPage( VitroRequest vreq ) throws Exception{
        List<String> simplePremUris = vreq.getWebappDaoFactory().getPageDao()
            .getRequiredActions( getPageUri(vreq) );
        
        List<RequestedAction> actions = new ArrayList<RequestedAction>();
        
        for( String uri : simplePremUris ){
            actions.add( new SimpleRequestedAction(uri) );
        }
        
        return new Actions( actions );
    }
    /**
     * Get Actions object for the data getters for the page.
     */
    private Actions getActionsForDataGetters(VitroRequest vreq ){
        try {<commit_msg>VIVO-530 Fix logic bug in authorization<commit_after>/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.controller.freemarker;


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

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.SimpleRequestedAction;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestedAction;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequiresActions;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.DataGetter;
import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.DataGetterUtils;
/**
 * Controller for getting data for pages defined in the display model. 
 * 
 * This controller passes these variables to the template: 
 * page: a map with information about the page from the display model.
 * pageUri: the URI of the page that identifies the page in the model 
 *  (note that this is not the URL address of the page).
 *    
 * See implementations of PageDataGetter for more variables. 
 */
public class PageController extends FreemarkerHttpServlet{
    private static final Log log = LogFactory.getLog(PageController.class);
    
    protected final static String DEFAULT_TITLE = "Page";        
    protected final static String DEFAULT_BODY_TEMPLATE = "emptyPage.ftl";     

    protected static final String DATA_GETTER_MAP = "pageTypeToDataGetterMap";
 
    /**
     * Get the required actions for all the data getters then
     * AND them together.
     */
    @Override
    protected Actions requiredActions(VitroRequest vreq) {
        try {
            Actions pageActs = getActionsForPage( vreq );
            Actions dgActs = getActionsForDataGetters( vreq );

            if( pageActs == null && dgActs == null){
                return Actions.AUTHORIZED;
            }else if( pageActs == null ){
                return dgActs;
            }else if( dgActs == null ){
            	return pageActs;
            }else{
                return pageActs.and(dgActs);
            }                
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            log.debug(e);
            return Actions.UNAUTHORIZED;
        }                
    }

    /**
     * Get all the required actions directly required for the page.
     */
    private Actions getActionsForPage( VitroRequest vreq ) throws Exception{
        List<String> simplePremUris = vreq.getWebappDaoFactory().getPageDao()
            .getRequiredActions( getPageUri(vreq) );
        
        List<RequestedAction> actions = new ArrayList<RequestedAction>();
        
        for( String uri : simplePremUris ){
            actions.add( new SimpleRequestedAction(uri) );
        }
        
        return new Actions( actions );
    }
    /**
     * Get Actions object for the data getters for the page.
     */
    private Actions getActionsForDataGetters(VitroRequest vreq ){
        try {
{'fraction_non_alphanumeric': 0.08516556291390728, 'fraction_numerical': 0.00039735099337748344, 'mean_word_length': 3.0058355437665782, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7491685', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2145, 'n_words': 523}