KickJava   Java API By Example, From Geeks To Geeks.

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


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;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.parsers.PlainTextExtractor;
20 import com.blandware.atleap.common.util.StringUtil;
21 import com.blandware.atleap.webapp.action.core.BaseAction;
22 import com.blandware.atleap.webapp.form.core.UploadContentResourceForm;
23 import com.blandware.atleap.webapp.util.core.GlobalProperties;
24 import com.blandware.atleap.webapp.util.core.ImageUtil;
25 import com.blandware.atleap.webapp.util.core.ResourceTypesManager;
26 import com.blandware.atleap.webapp.util.core.WebappConstants;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.action.ActionMessage;
31 import org.apache.struts.action.ActionMessages;
32 import org.apache.struts.upload.FormFile;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import java.io.ByteArrayInputStream JavaDoc;
37 import java.util.List JavaDoc;
38
39 /**
40  * <p>Creates new content resource
41  * </p>
42  * <p><a HREF="CreateContentResourceAction.java.htm"><i>View Source</i></a></p>
43  * <p/>
44  *
45  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
46  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
47  * @version $Revision: 1.28 $ $Date: 2006/03/26 11:52:07 $
48  * @struts.action path="/core/contentResource/create"
49  * name="uploadContentResourceForm"
50  * scope="request"
51  * input="inputForward"
52  * validate="true"
53  * roles="core-contentResource-create"
54  * @struts.action-forward name="inputForward"
55  * path="/core/contentResource/callCreate.do"
56  * redirect="true"
57  * @struts.action-forward name="callCreateImage"
58  * path="/core/contentResource/image/callCreate.do"
59  * @struts.action-forward name="callCreateFile"
60  * path="/core/contentResource/file/callCreate.do"
61  * @struts.action-forward name="callCreateDocument"
62  * path="/core/contentResource/document/callCreate.do"
63  * @struts.action-forward name="listContentResources"
64  * path="/core/contentResource/list.do"
65  * redirect="true"
66  */

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

78     public ActionForward execute(ActionMapping mapping, ActionForm form,
79                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
80         if ( isCancelled(request) ) {
81             resetToken(request);
82             return mapping.findForward("listContentResources");
83         }
84
85         UploadContentResourceForm uploadContentResourceForm = (UploadContentResourceForm) form;
86
87         ResourceTypesManager resourceTypesManager = ResourceTypesManager.getInstance(request.getSession().getServletContext());
88
89         FormFile file = uploadContentResourceForm.getFile();
90         if ( file == null || file.getFileName() == null || file.getFileName().trim().length() == 0 || file.getFileSize() == 0 ) {
91             List JavaDoc supportedExtensions = resourceTypesManager.getSupportedExtensions();
92             ActionMessages errors = new ActionMessages();
93             errors.add("contentResourceUnsupportedExtension", new ActionMessage("core.contentResource.errors.unsupportedExtension", supportedExtensions.toString()));
94             saveErrors(request, errors);
95             saveToken(request);
96             return mapping.getInputForward();
97         }
98
99         String JavaDoc fileName = file.getFileName();
100         String JavaDoc resourceType = resourceTypesManager.getResourceTypeByFileName(fileName);
101         String JavaDoc mimeType = resourceTypesManager.getMimeTypeByFileName(fileName);
102
103         if ( log.isDebugEnabled() ) {
104             log.debug("Uploaded file '" + fileName + "' (" + file.getFileSize() + " bytes) type=" + resourceType);
105         }
106
107         if (resourceType == null) {
108             resourceType = Constants.RESOURCE_TYPE_FILE;
109         }
110
111         request.getSession().setAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY, uploadContentResourceForm);
112         if ( resourceType.equalsIgnoreCase(Constants.RESOURCE_TYPE_IMAGE) ) {
113
114             //check for support
115
try {
116                 ImageUtil.decodeImage(file.getFileData(), mimeType);
117             } catch ( Exception JavaDoc ex ) {
118                 ActionMessages errors = new ActionMessages();
119                 errors.add("contentResourceUnsupportedOrCorrupted", new ActionMessage("core.contentResource.errors.unsupportedOrCorrupted"));
120                 saveErrors(request, errors);
121                 saveToken(request);
122                 return mapping.findForward("callCreateFile");
123             }
124
125             return mapping.findForward("callCreateImage");
126         } else if ( resourceType.equalsIgnoreCase(Constants.RESOURCE_TYPE_FILE) ) {
127             return mapping.findForward("callCreateFile");
128         } else if ( resourceType.equalsIgnoreCase(Constants.RESOURCE_TYPE_DOCUMENT) ) {
129
130             //check for support
131
String JavaDoc plainText = null;
132             String JavaDoc determinedEncoding = null;
133             try {
134                 PlainTextExtractor extractor = new PlainTextExtractor();
135                 plainText = extractor.extract(new ByteArrayInputStream JavaDoc(file.getFileData()), mimeType);
136                 determinedEncoding = extractor.getUsedEncoding();
137             } catch ( Exception JavaDoc ex ) {
138                 ActionMessages errors = new ActionMessages();
139                 errors.add("contentResourceUnsupportedOrCorrupted", new ActionMessage("core.contentResource.errors.unsupportedOrCorrupted"));
140                 saveErrors(request, errors);
141                 saveToken(request);
142                 return mapping.findForward("callCreateFile");
143             }
144             Integer JavaDoc summarySize = GlobalProperties.getInstance(servlet.getServletContext()).getInteger(WebappConstants.DOCUMENT_SUMMARY_SIZE_KEY, new Integer JavaDoc(400));
145             plainText = StringUtil.htmlEncode(StringUtil.shortenString(plainText, Math.min(plainText.length(), summarySize.intValue())));
146             request.getSession().setAttribute(WebappConstants.CONTENT_DOCUMENT_NEW_SUMMARY_KEY, plainText);
147
148             request.getSession().setAttribute(WebappConstants.CONTENT_DOCUMENT_ENCODING_KEY, determinedEncoding);
149
150             return mapping.findForward("callCreateDocument");
151         } else {
152             throw new Exception JavaDoc("There are not action found for resource type=" + resourceType);
153         }
154
155     }
156 }
Popular Tags