KickJava   Java API By Example, From Geeks To Geeks.

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


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.common.Constants;
19 import com.blandware.atleap.model.core.ContentResource;
20 import com.blandware.atleap.model.core.ResourceData;
21 import com.blandware.atleap.service.core.ContentResourceManager;
22 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
23 import com.blandware.atleap.webapp.action.core.BaseAction;
24 import com.blandware.atleap.webapp.form.ContentResourceForm;
25 import com.blandware.atleap.webapp.form.core.UploadContentResourceForm;
26 import com.blandware.atleap.webapp.util.core.CacheUtil;
27 import com.blandware.atleap.webapp.util.core.ResourceTypesManager;
28 import com.blandware.atleap.webapp.util.core.WebappConstants;
29 import com.blandware.atleap.webapp.util.core.WebappUtil;
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.upload.FormFile;
36
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39
40 /**
41  * <p>Creates new content resource with type 'file'
42  * </p>
43  * <p><a HREF="CreateFileAction.java.htm"><i>View Source</i></a></p>
44  * <p/>
45  *
46  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
47  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
48  * @version $Revision: 1.16 $ $Date: 2006/03/10 17:10:22 $
49  * @struts.action path="/core/contentResource/file/create"
50  * name="contentResourceForm"
51  * scope="request"
52  * input="inputForward"
53  * validate="true"
54  * roles="core-contentResource-create"
55  * @struts.action-forward name="inputForward"
56  * path=".core.contentResource.file.create"
57  * @struts.action-forward name="listContentResources"
58  * path="/core/contentResource/list.do"
59  * redirect="true"
60  * @struts.action-forward name="createContentResource"
61  * path=".core.contentResource.create"
62  * @struts.action-forward name="unsatisfiable"
63  * path="/core/contentResource/list.do"
64  */

65 public final class CreateFileAction extends BaseAction {
66     /**
67      * @param mapping The ActionMapping used to select this instance
68      * @param form The optional ActionForm bean for this request (if any)
69      * @param request The HTTP request we are proceeding
70      * @param response The HTTP response we are creating
71      * @return an ActionForward instance describing where and how
72      * control should be forwarded, or null if response
73      * has already been completed
74      */

75     public ActionForward execute(ActionMapping mapping, ActionForm form,
76                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
77
78         ContentResourceForm contentResourceForm = (ContentResourceForm) form;
79
80         UploadContentResourceForm uploadContentResourceForm = (UploadContentResourceForm) request.getSession().getAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
81         if ( uploadContentResourceForm == null || uploadContentResourceForm.getFile() == null ) {
82             // Maybe session was invalidated and after that user pressed 'back'
83
// or 'cancel'?
84
if (isBackPressed(request)) {
85                 request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
86                 saveToken(request);
87                 return mapping.findForward("createContentResource");
88             }
89             if ( isCancelled(request) ) {
90                 request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
91                 return mapping.findForward("listContentResources");
92             }
93             if ( log.isWarnEnabled() ) {
94                 log.warn("Missing required attribute in session '" + WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY + "'");
95             }
96             ActionMessages errors = new ActionMessages();
97             errors.add("missingResource", new ActionMessage("core.contentResource.errors.missingResource"));
98             saveErrors(request, errors);
99             saveToken(request);
100             return mapping.getInputForward();
101         }
102         FormFile formFile = uploadContentResourceForm.getFile();
103         String JavaDoc fileName = formFile.getFileName();
104
105         if (isBackPressed(request)) {
106             if ( formFile != null ) {
107                 formFile.destroy();
108             }
109             request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
110             saveToken(request);
111             return mapping.findForward("createContentResource");
112         }
113
114         if ( isCancelled(request) ) {
115             if ( formFile != null ) {
116                 formFile.destroy();
117             }
118             request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
119             return mapping.findForward("listContentResources");
120         }
121
122         ResourceTypesManager resourceTypesManager = ResourceTypesManager.getInstance(request.getSession().getServletContext());
123
124         if ( log.isDebugEnabled() ) {
125             log.debug("Storing formFile '" + fileName + "' into DB");
126         }
127
128         ContentResource file = new ContentResource();
129         WebappUtil.copyProperties(file, contentResourceForm, request);
130
131         //setup uri
132
String JavaDoc uri = Constants.RESOURCES_URI_PREFIX + contentResourceForm.getUri();
133         file.setUri(uri);
134
135         file.setType(Constants.RESOURCE_TYPE_FILE);
136         String JavaDoc mimeType = resourceTypesManager.getMimeTypeByFileName(fileName);
137         file.setMimeType(mimeType);
138
139         byte[] fileData = formFile.getFileData();
140
141         file.setSize(new Long JavaDoc(fileData.length));
142
143         ContentResourceManager contentResourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN);
144         try {
145             Long JavaDoc contentResourceId = contentResourceManager.createContentResource(file, new ResourceData(fileData));
146
147             //put into cache
148
file = contentResourceManager.retrieveContentResource(contentResourceId);
149             CacheUtil cacheUtil = CacheUtil.getInstance(request);
150             CacheUtil.ResourceData rd = new CacheUtil.ResourceData(fileData, mimeType, null, file.getLastUpdatedDatetime().getTime());
151             cacheUtil.putResourceInCache(rd, uri);
152
153         } catch ( BeanAlreadyExistsException e ) {
154             // content resource already exists
155
ActionMessages errors = new ActionMessages();
156             errors.add("contentResourceAlreadyExists", new ActionMessage("core.contentResource.errors.alreadyExists"));
157             saveErrors(request, errors);
158             saveToken(request);
159             return mapping.getInputForward();
160         }
161         if ( formFile != null ) {
162             formFile.destroy();
163         }
164         request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
165         return mapping.findForward("listContentResources");
166
167     }
168 }
Popular Tags