KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentResource > file > CallCreateFileAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.action.core.contentResource.file;
17
18 import com.blandware.atleap.webapp.action.core.BaseAction;
19 import com.blandware.atleap.webapp.form.ContentResourceForm;
20 import com.blandware.atleap.webapp.form.core.UploadContentResourceForm;
21 import com.blandware.atleap.webapp.util.core.ResourceTypesManager;
22 import com.blandware.atleap.webapp.util.core.WebappConstants;
23 import org.apache.struts.action.ActionForm;
24 import org.apache.struts.action.ActionForward;
25 import org.apache.struts.action.ActionMapping;
26 import org.apache.struts.action.ActionMessage;
27 import org.apache.struts.action.ActionMessages;
28 import org.apache.struts.upload.FormFile;
29 import org.apache.commons.validator.GenericValidator;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 /**
35  * <p>Returns page with form to create new file
36  * </p>
37  * <p><a HREF="CallCreateFileAction.java.htm"><i>View Source</i></a></p>
38  * <p/>
39  *
40  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
41  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
42  * @version $Revision: 1.9 $ $Date: 2006/03/10 17:10:22 $
43  * @struts.action name="contentResourceForm"
44  * path="/core/contentResource/file/callCreate"
45  * validate="false"
46  * roles="core-contentResource-create"
47  * input="callCreateContentResource"
48  * @struts.action-forward name="createFile"
49  * path=".core.contentResource.file.create"
50  * @struts.action-forward name="callCreateContentResource"
51  * path="/core/contentResource/callCreate.do"
52  */

53 public final class CallCreateFileAction extends BaseAction {
54     /**
55      * @param mapping The ActionMapping used to select this instance
56      * @param form The optional ActionForm bean for this request (if any)
57      * @param request The HTTP request we are proceeding
58      * @param response The HTTP response we are creating
59      * @return an ActionForward instance describing where and how
60      * control should be forwarded, or null if response
61      * has already been completed
62      */

63     public ActionForward execute(ActionMapping mapping, ActionForm form,
64                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
65
66         UploadContentResourceForm uploadContentResourceForm = (UploadContentResourceForm) request.getSession().getAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
67         if ( uploadContentResourceForm == null || uploadContentResourceForm.getFile() == null ) {
68             if ( log.isWarnEnabled() ) {
69                 log.warn("Missing required attribute in session '" + WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY + "'");
70             }
71             ActionMessages errors = new ActionMessages();
72             errors.add("missingResource", new ActionMessage("core.contentResource.errors.missingResource"));
73             saveErrors(request, errors);
74             saveToken(request);
75             return mapping.findForward("callCreateContentResource");
76         }
77         FormFile formFile = uploadContentResourceForm.getFile();
78         String JavaDoc fileName = formFile.getFileName();
79
80         ContentResourceForm contentResourceForm = (ContentResourceForm) form;
81
82         String JavaDoc uri = "/";
83         if ( GenericValidator.isBlankOrNull(contentResourceForm.getUri()) && request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCES_CURRENT_PATH_KEY) != null ) {
84             uri = (String JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCES_CURRENT_PATH_KEY) + "/";
85             if ( !uri.endsWith("/") ) {
86                 uri += "/";
87             }
88             // TODO: this is a patch!! Fix the reason!
89
if (!"/".equals(uri) && !"".equals(uri)) {
90                 if (!uri.startsWith("/")) {
91                     uri = "/" + uri;
92                 }
93             }
94         }
95         contentResourceForm.setUri(uri + fileName);
96
97         ResourceTypesManager resourceTypesManager = ResourceTypesManager.getInstance(request.getSession().getServletContext());
98         String JavaDoc mimeType = resourceTypesManager.getMimeTypeByFileName(fileName);
99
100         if ( log.isDebugEnabled() ) {
101             log.debug("Uploaded formFile resource with mimeType=" + mimeType);
102         }
103         contentResourceForm.setMimeType(mimeType);
104         contentResourceForm.setSize(String.valueOf(formFile.getFileData().length));
105         request.getSession().setAttribute(WebappConstants.CONTENT_FILE_NEW_FILENAME_KEY, formFile.getFileName());
106
107         saveToken(request);
108         return mapping.findForward("createFile");
109     }
110 }
Popular Tags