filemultiactioncontroller.java

来自「Java的框架」· Java 代码 · 共 397 行

JAVA
397
字号
package mcaps.core.docman.webapp.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

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

import mcaps.core.docman.util.DocManUtil;
import mcaps.core.docman.util.NameConstants;
import mcap.core.base.webapp.controller.BaseMultiActionController;
import mcap.core.docman.model.FileObject;
import mcap.core.docman.model.FolderObject;
import mcap.core.docman.service.DocAccessException;
import mcap.core.docman.service.DocExistsException;
import mcap.core.docman.service.DocPathNotFoundException;
import mcap.core.docman.service.DocManManager;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

/**
 * This class handle multiple request pertaining to
 * a file object.
 *
 * @author Chan Chin Wei
 * @date Apr 19, 2006
 * @version 1.0.1.0
 */
public class FileMultiActionController extends BaseMultiActionController implements InitializingBean {

	private static final String FILE_VIEW = "core/docMan/file";
	private static final String FILE_VERSION_VIEW = "core/docMan/fileVersion";
	private static final String FILE_VERSIONS_VIEW = "core/docMan/fileVersions";
	private static final String DEST_VIEW = "core/docMan/destSelection";
	
	private DocManManager docManager;

	public DocManManager getDocManager() {
		return docManager;
	}

	public void setDocManager(DocManManager docManager) {
		this.docManager = docManager;
	}

//	===========================================================================================================
//	 INITIALIZING BEAN IMPLEMENTATION
//	===========================================================================================================


	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 * Invoked by a BeanFactory after it has set all bean properties supplied. This allows 
	 * the bean instance to perform initialization only possible when all bean properties 
	 * have been set and to throw an exception in the event of misconfiguration. 
	 */
	public void afterPropertiesSet() throws Exception {
		if (docManager == null)
			throw new ApplicationContextException ("Must set docManager bean property on " + getClass());
		
	}

//	===========================================================================================================
//	 HANDLERS
//	===========================================================================================================

	/**
	 * Custom handler for file display
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView fileHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		String path = request.getParameter ("path");
		FileObject file = null;
		InputStream ips = null;
		try {
			file = docManager.getFile(path);
			ips = file.getInputStream();
		} catch (DocAccessException e) {
			e.printStackTrace();
			throw new ServletException (e.getMessage());
		} finally {
			try {
				ips.close();
			} catch (Exception ex) {
				//catch io exception for closing inputstream
			}
		}

		return new ModelAndView(FILE_VIEW, NameConstants.FILE, file);
	}
	
	/**
	 * Custom handler for file deletion
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView deleteFileHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		Locale locale = request.getLocale();
		String path = request.getParameter ("path");
		try {
			docManager.removeFile(path);
			saveMessage (request, getText ("fileObject.deleted", request.getParameter ("name"), locale));
		} catch (DocAccessException e) {
			e.printStackTrace();
			if (e instanceof DocPathNotFoundException)
				saveError (request, getText ("errors.fileObject.deleteNotFound", request.getParameter ("name"), locale));
			else
				saveError (request, getText ("errors.fileObject.deleteFail", request.getParameter ("name"), locale));
		}
		return new ModelAndView (new RedirectView ("docMan.action?path=" + DocManUtil.urlEncodePath (request.getParameter ("parentId"))));
	}
	
	/**
	 * Custom handler for file move destination folder selection display
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView getFileDestHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		String path = request.getParameter ("path");
		FileObject file = null;
		FolderObject folderstruct = null;
		InputStream ips = null;
		try {
			file = docManager.getFile(path);
			folderstruct = docManager.getFolder("", true);
			ips = file.getInputStream();
		} catch (DocAccessException e) {
			e.printStackTrace();
			throw new ServletException (e.getMessage());
		} finally {
			try {
				ips.close();
			} catch (Exception ex) {
				//catch io exception for closing inputstream
			}
		}
		
		ModelAndView mv = new ModelAndView(DEST_VIEW, NameConstants.FOLDERSTRUCT, folderstruct);
		mv.addObject(NameConstants.FILE, file);
		return mv;
	}
	
	/**
	 * Custom handler for file move
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView moveFileHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		Locale locale = request.getLocale();
		String path = request.getParameter ("path");
		String destpath = request.getParameter ("destpath");
		FileObject file = null;
		InputStream ips = null;
		try {
			file = docManager.getFile(path);
			docManager.moveFile(destpath, file);
			ips = file.getInputStream();
		} catch (DocAccessException e) {
			e.printStackTrace();
			String name = "";
			//since error occur, redirect to original parent folder
			if (file != null) {
				destpath = file.getParentId();
				name = file.getName();
			}
			else {
				int index = path.lastIndexOf("/");
				if (index != -1) {
					name = path.substring(index+1);
					destpath = path.substring(0, index);
				}
				else {
					name = path;
					destpath = "";
				}
			}
			if (e instanceof DocExistsException)
				saveError (request, getText ("errors.fileObject.moveFailExist", name, locale));
			else
				saveError (request, getText ("errors.fileObject.moveFail", name, locale));
		} finally {
			try {
				ips.close();
			} catch (Exception ex) {
				//catch io exception for closing inputstream
			}
		}
		return new ModelAndView (new RedirectView ("docMan.action?path=" + DocManUtil.urlEncodePath (destpath)));
	}
	
	/**
	 * Custom handler for file download
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView downloadFileHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		String path = request.getParameter ("path");
		FileObject file = null;
		InputStream ips = null;
		BufferedOutputStream bops = null;
		try {
			file = docManager.getFile(path);
			ips = file.getInputStream();
			response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
			bops = new BufferedOutputStream (response.getOutputStream());
			byte[] buf = new byte [1024];
			
			int size = ips.read (buf, 0, 1024);
			while (size != -1) {
				bops.write(buf, 0, size);
				size = ips.read (buf, 0, 1024);
			}
			bops.flush();
			
		} catch (DocAccessException e) {
			e.printStackTrace();
			throw new ServletException (e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServletException (e.getMessage());
		} finally {
			try {
				ips.close();
			} catch (Exception ex) {
				//catch io exception for closing inputstream
			}
			try {
				bops.close ();
			} catch (Exception ex) {
				//catch io exception for closing outputstream
			}
		}
		return null;
//		return new ModelAndView(FILE_VIEW, NameConstants.FILE, file);
	}
	
	/**
	 * Custom handler for a specify version of file download
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView downloadFileVersionHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		String path = request.getParameter ("path");
		int version = Integer.parseInt(request.getParameter ("version"));
		FileObject file = null;
		InputStream ips = null;
		BufferedOutputStream bops = null;
		try {
			file = docManager.getFile(path, version);
			ips = file.getInputStream();
			response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
			bops = new BufferedOutputStream (response.getOutputStream());
			byte[] buf = new byte [1024];
			
			int size = ips.read (buf, 0, 1024);
			while (size != -1) {
				bops.write(buf, 0, size);
				size = ips.read (buf, 0, 1024);
			}
			bops.flush();
			
		} catch (DocAccessException e) {
			e.printStackTrace();
			throw new ServletException (e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServletException (e.getMessage());
		} finally {
			try {
				ips.close();
			} catch (Exception ex) {
				//catch io exception for closing inputstream
			}
			try {
				bops.close ();
			} catch (Exception ex) {
				//catch io exception for closing outputstream
			}
		}
		return null;
//		return new ModelAndView(FILE_VIEW, NameConstants.FILE, file);
	}
	
	/**
	 * Custom handler for a certain version of file display
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView fileVersionHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		String path = request.getParameter ("path");
		int version = Integer.parseInt(request.getParameter ("version"));
		FileObject file = null;
		InputStream ips = null;
		try {
			file = docManager.getFile(path, version);
			ips = file.getInputStream();
		} catch (DocAccessException e) {
			e.printStackTrace();
			throw new ServletException (e.getMessage());
		} finally {
			try {
				ips.close();
			} catch (Exception ex) {
				//catch io exception for closing inputstream
			}
		}

		return new ModelAndView(FILE_VERSION_VIEW, NameConstants.FILE, file);
	}
	
	/**
	 * Custom handler for a certain version of file display
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView fileVersionsHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		String path = request.getParameter ("path");
		List files = null;
		try {
			files = docManager.getFileVersions(path);
		} catch (DocAccessException e) {
			e.printStackTrace();
			throw new ServletException (e.getMessage());
		}
		return new ModelAndView(FILE_VERSIONS_VIEW, NameConstants.FILES, files);
	}
	
	/**
	 * Custom handler for publish a certain version of file
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView publishFileHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		String path = request.getParameter ("path");
		String version = request.getParameter ("version");
		String mainPage = request.getParameter ("mainPage");
		String indexPage = request.getParameter ("indexPage");
System.out.println("************ path=" + path);
System.out.println("************ version=" + version);
System.out.println("************ mainPage=" + mainPage);
System.out.println("************ indexPage=" + indexPage);
		FileObject file = new FileObject ();
		file.setFilePath(path);
		file.setVersionPublished(new Integer (version));
		file.setPublishedDate(java.util.Calendar.getInstance());
		if (mainPage != null && mainPage.equalsIgnoreCase("true"))
			file.setMainPage(true);
		if (indexPage != null && indexPage.equalsIgnoreCase("true"))
			file.setIndexPage(true);
		
		try {
			docManager.publishFile(file);
		} catch (DocAccessException e) {
			e.printStackTrace();
			throw new ServletException (e.getMessage());
		}

		return new ModelAndView (new RedirectView ("file.action?path="+DocManUtil.urlEncodePath (path)));
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?