View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.controller;
17  
18  import java.io.InputStream;
19  import java.io.OutputStream;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.apache.commons.beanutils.DynaBean;
25  import org.apache.commons.beanutils.PropertyUtils;
26  import org.apache.commons.io.CopyUtils;
27  import org.apache.commons.io.IOUtils;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionForward;
32  import org.apache.struts.action.ActionMapping;
33  import org.apache.struts.action.ActionMessage;
34  import org.apache.struts.action.ActionMessages;
35  import org.apache.struts.actions.MappingDispatchAction;
36  import org.apache.struts.upload.FormFile;
37  import org.seasar.tuigwaa.cms.ContentsStoreService;
38  import org.seasar.tuigwaa.cms.core.Resource;
39  import org.seasar.tuigwaa.system.Constants;
40  import org.seasar.tuigwaa.util.TgwContext;
41  import org.seasar.tuigwaa.util.TgwPathResolver;
42  
43  import com.isenshi.util.HttpUtils;
44  import com.isenshi.util.ResourceUtils;
45  import com.isenshi.util.extlib.StrutsUtil;
46  
47  /***
48   * @author nishioka
49   */
50  public class FileAction extends MappingDispatchAction {
51  
52  	//private static final String CONTENT_DISPOSITION = "Content-Disposition";
53  
54  	private Log log = LogFactory.getLog(getClass());
55  
56  	private ContentsStoreService contentsStore;
57  
58  	public FileAction(ContentsStoreService slideService) {
59  		this.contentsStore = slideService;
60  	}
61  
62  	public ActionForward doPrepare(ActionMapping mapping, ActionForm form,
63  			HttpServletRequest request, HttpServletResponse response)
64  			throws Exception {
65  		setMaxFileSize(mapping, request);
66  		if ("true".equals(request.getParameter(Constants.PARAM_IFRAME_MODE))) {
67  			return mapping.findForward("iframe");
68  		} else {
69  			return mapping.findForward("success");
70  		}
71  	}
72  
73  	public ActionForward download(ActionMapping mapping, ActionForm form,
74  			HttpServletRequest request, HttpServletResponse response)
75  			throws Exception {
76  
77  		String siteName = TgwContext.getSiteName();
78  		String pagePath = TgwContext.getPageName();
79  		String fileName = StrutsUtil.getURLDecodedParameter(request, Constants.PARAM_FILE_NAME);
80  		
81  		Resource resource = contentsStore.getAttachmentResource(siteName, pagePath, fileName);
82  		
83  		if (resource != null) {
84  			HttpUtils.setResponseHeader(request, response, resource.getContentType());
85  			OutputStream os = response.getOutputStream();
86  			contentsStore.writeAttachment(siteName, pagePath, fileName, os);
87  			os.close();
88  		} else {
89  			response.sendError(HttpServletResponse.SC_NOT_FOUND);
90  		}
91  		return null;
92  	}
93  
94  	public ActionForward downloadThumbnail(ActionMapping mapping,
95  			ActionForm form, HttpServletRequest request,
96  			HttpServletResponse response) throws Exception {
97  
98  		return null;
99  	}
100 
101 	public ActionForward upload(ActionMapping mapping, ActionForm form,
102 			HttpServletRequest request, HttpServletResponse response)
103 			throws Exception {
104 		DynaBean bean = (DynaBean) form;
105 
106 		setMaxFileSize(mapping, request);
107 
108 		String pagePath = (String) bean.get(Constants.PARAM_PAGENAME);
109 		TgwContext.bindPageName(pagePath);
110 
111 		FormFile file = (FormFile) PropertyUtils.getProperty(form, "file");
112 		// FormFile file = (FormFile) dform.get("file");
113 
114 		if (file == null) {
115 			ActionMessages msgs = new ActionMessages();
116 			msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
117 					"file.sizeexceed"));
118 			saveMessages(request, msgs);
119 			log.error("size exceeded.");
120 			return mapping.findForward("sizeExceeded");
121 		}
122 
123 		String fileName = file.getFileName();
124 		String contentType = file.getContentType();
125 		int contentLength = file.getFileSize();
126 		InputStream iStream = file.getInputStream();
127 
128 		String siteName = TgwContext.getSiteName();
129 
130 		String tmpDir = (String) bean.get(Constants.PARAM_TMP_DIR);
131 		if (tmpDir != null && tmpDir.length() > 0) {
132 			String path = TgwPathResolver.getTmpUploadFilePath(siteName,
133 					tmpDir, fileName);
134 			OutputStream os = ResourceUtils.getOutputStream(path);
135 			CopyUtils.copy(iStream, os);
136 			IOUtils.closeQuietly(os);
137 		} else {
138 			contentsStore.createAttachment(siteName, pagePath, fileName,
139 					contentType, contentLength, iStream);
140 		}
141 
142 		return mapping.findForward("success");
143 	}
144 
145 	private void setMaxFileSize(ActionMapping mapping,
146 			HttpServletRequest request) {
147 		String maxFileSize = StrutsUtil.getMaxFileSize(mapping);
148 		request.setAttribute(Constants.RATTR_MAX_FILE_SIZE, maxFileSize);
149 	}
150 
151 }