<commit_before>/**  
   Copyright 2008 University of Rochester

   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 edu.ur.ir.web.action.item;

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

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import edu.ur.file.db.FileInfo;
import edu.ur.ir.item.GenericItem;
import edu.ur.ir.item.ItemFile;
import edu.ur.ir.item.ItemFileSecurityService;
import edu.ur.ir.item.ItemService;
import edu.ur.ir.statistics.DownloadStatisticsService;
import edu.ur.ir.user.IrUser;
import edu.ur.ir.user.UserService;
import edu.ur.ir.web.action.UserIdAware;
import edu.ur.ir.web.util.WebIoUtils;

/**
 * Action to download item files.
 * 
 * @author <NAME>
 *
 */
public class GenericItemFileDownload extends ActionSupport implements ServletResponseAware, ServletRequestAware, UserIdAware
{

	/** Eclipse generated id. */
	private static final long serialVersionUID = 5430030320610916010L;

	/**  Logger for file upload */
	private static final Logger log = Logger.getLogger(GenericItemFileDownload.class);
	
	/**  File to download */
	private Long itemFileId;
	
	/** item that should contain the file */
	private Long itemId;

    /** Service for items */
    private ItemService itemService;
	
	/**  Servlet response to write to */
	private HttpServletResponse response;
	
	/**  Servlet request made */
	private HttpServletRequest request;
	
	/** Utility for streaming file */
	private WebIoUtils webIoUtils;

	/** Download statistics service */
	private DownloadStatisticsService downloadStatisticsService;
	
	/** Id of user logged in */
	private Long userId;
	
	/** User service */
	private UserService userService; 
	
	/** Item file security service */
	private ItemFileSecurityService itemFileSecurityService; 
	
    /**
     * Checks for user permission and then downloads the file 
     * 
     * @return
     * @throws Exception
     */
    public String fileDownloadWithPermissionCheck() throws Exception {

	    log.debug("Trying to download the file to user");
	    
        if (itemFileId == null) {
        	log.debug("file id is null");
            return INPUT;
        }
        
        if( itemId == null)
        {
        	log.debug("item id is null");
        	return INPUT;
        }
         
        GenericItem genericItem = itemService.getGenericItem(itemId, false);
 
        if( genericItem == null)
        {
        	log.debug("Item  is null");
        	return INPUT;
        }
        
        ItemFile itemFile = genericItem.getItemFile(itemFileId);
 
        if( itemFile == null)
        {
        	log.debug("Item file is null");
        	return INPUT;
        }
        
        IrUser user = null;
        
        if (userId != null) {
        	user = userService.getUser(userId, false);
        }
        
         // Check if file can be downloaded by user
        if (itemFile.isPublic() ) {
        	
        	if( user == null || !genericItem.getOwner().equals(user))
        	{
        	    downloadStatisticsService.addFileDownloadInfo(request.getRemoteAddr(),
            		itemFile.getIrFile());
        	}
        	downloadFile(itemFile);
        }
        else if ( user != null)
        {
        	if( genericItem.getOwner().equals(user) || 
            	(itemFileSecurityService.hasPermission(itemFile, user, ItemFileSecurityService.ITEM_FILE_READ_PERMISSION) > 0) )
        	{<commit_msg>Fixed to check to make sure the item is publicly viewable before checking that the file is publicly viewable<commit_after>/**  
   Copyright 2008 University of Rochester

   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 edu.ur.ir.web.action.item;

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

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import edu.ur.file.db.FileInfo;
import edu.ur.ir.item.GenericItem;
import edu.ur.ir.item.ItemFile;
import edu.ur.ir.item.ItemFileSecurityService;
import edu.ur.ir.item.ItemService;
import edu.ur.ir.statistics.DownloadStatisticsService;
import edu.ur.ir.user.IrUser;
import edu.ur.ir.user.UserService;
import edu.ur.ir.web.action.UserIdAware;
import edu.ur.ir.web.util.WebIoUtils;

/**
 * Action to download item files.
 * 
 * @author <NAME>
 *
 */
public class GenericItemFileDownload extends ActionSupport implements ServletResponseAware, ServletRequestAware, UserIdAware
{

	/** Eclipse generated id. */
	private static final long serialVersionUID = 5430030320610916010L;

	/**  Logger for file upload */
	private static final Logger log = Logger.getLogger(GenericItemFileDownload.class);
	
	/**  File to download */
	private Long itemFileId;
	
	/** item that should contain the file */
	private Long itemId;

    /** Service for items */
    private ItemService itemService;
	
	/**  Servlet response to write to */
	private HttpServletResponse response;
	
	/**  Servlet request made */
	private HttpServletRequest request;
	
	/** Utility for streaming file */
	private WebIoUtils webIoUtils;

	/** Download statistics service */
	private DownloadStatisticsService downloadStatisticsService;
	
	/** Id of user logged in */
	private Long userId;
	
	/** User service */
	private UserService userService; 
	
	/** Item file security service */
	private ItemFileSecurityService itemFileSecurityService; 
	
    /**
     * Checks for user permission and then downloads the file 
     * 
     * @return
     * @throws Exception
     */
    public String fileDownloadWithPermissionCheck() throws Exception {

	    log.debug("Trying to download the file to user");
	    
        if (itemFileId == null) {
        	log.debug("file id is null");
            return INPUT;
        }
        
        if( itemId == null)
        {
        	log.debug("item id is null");
        	return INPUT;
        }
         
        GenericItem genericItem = itemService.getGenericItem(itemId, false);
 
        if( genericItem == null)
        {
        	log.debug("Item  is null");
        	return INPUT;
        }
        
        ItemFile itemFile = genericItem.getItemFile(itemFileId);
 
        if( itemFile == null)
        {
        	log.debug("Item file is null");
        	return INPUT;
        }
        
        IrUser user = null;
        
        if (userId != null) {
        	user = userService.getUser(userId, false);
        }
        
         // Check if file can be downloaded by user
        if (genericItem.isPubliclyViewable() && itemFile.isPublic() ) {
        	
        	if( user == null || !genericItem.getOwner().equals(user))
        	{
        	    downloadStatisticsService.addFileDownloadInfo(request.getRemoteAddr(),
            		itemFile.getIrFile());
        	}
        	downloadFile(itemFile);
        }
        else if ( user != null)
        {
        	if( genericItem.getOwner().equals(user) || 
            	(itemFileSecurityService.hasPermission(itemFile, user, ItemFileSecurityService.ITEM_FILE_READ_PERMISSION) > 0) )
        	{
{'fraction_non_alphanumeric': 0.09022828844063292, 'fraction_numerical': 0.007730402222490639, 'mean_word_length': 2.706356311548791, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4153664', 'n_tokens_mistral': 2395, 'n_tokens_neox': 2288, 'n_words': 665}