KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

65     public ActionForward execute(ActionMapping mapping, ActionForm form,
66                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
67
68         UploadContentResourceForm uploadContentResourceForm = (UploadContentResourceForm) request.getSession().getAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
69         if ( uploadContentResourceForm == null || uploadContentResourceForm.getFile() == null ) {
70             if ( log.isWarnEnabled() ) {
71                 log.warn("Missing required attribute in session '" + WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY + "'");
72             }
73             ActionMessages errors = new ActionMessages();
74             errors.add("missingResource", new ActionMessage("core.contentResource.errors.missingResource"));
75             saveErrors(request, errors);
76             saveToken(request);
77             return mapping.findForward("callCreateContentResource");
78         }
79
80         FormFile formFile = uploadContentResourceForm.getFile();
81         String JavaDoc fileName = formFile.getFileName();
82
83         ImageForm imageForm = (ImageForm) form;
84         String JavaDoc uri = "/";
85         if ( GenericValidator.isBlankOrNull(imageForm.getUri()) && request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCES_CURRENT_PATH_KEY) != null ) {
86             uri = (String JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCES_CURRENT_PATH_KEY) + "/";
87             if ( !uri.endsWith("/") ) {
88                 uri += "/";
89             }
90             // TODO: this is a patch!! Fix the reason!
91
if (!"/".equals(uri) && !"".equals(uri)) {
92                 if (!uri.startsWith("/")) {
93                     uri = "/" + uri;
94                 }
95             }
96         }
97         imageForm.setUri(uri + fileName);
98
99         ResourceTypesManager resourceTypesManager = ResourceTypesManager.getInstance(request.getSession().getServletContext());
100         String JavaDoc mimeType = resourceTypesManager.getMimeTypeByFileName(fileName);
101
102         imageForm.setMimeType(mimeType);
103         imageForm.setSize(String.valueOf(formFile.getFileData().length));
104         request.getSession().setAttribute(WebappConstants.CONTENT_IMAGE_NEW_FILENAME_KEY, formFile.getFileName());
105
106
107         try {
108             IIOImage JavaDoc source = ImageUtil.decodeImage(formFile.getFileData(), mimeType);
109             imageForm.setWidth(String.valueOf(source.getRenderedImage().getWidth()));
110             imageForm.setHeight(String.valueOf(source.getRenderedImage().getHeight()));
111         } catch ( Exception JavaDoc ex ) {
112             if ( formFile != null ) {
113                 formFile.destroy();
114             }
115             request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
116
117             ActionMessages errors = new ActionMessages();
118             errors.add("contentResourceCorruptedData", new ActionMessage("core.contentResource.errors.corruptedData"));
119             saveErrors(request, errors);
120             return mapping.findForward("callCreateContentResource");
121         }
122
123         saveToken(request);
124         return mapping.findForward("createImage");
125     }
126 }
Popular Tags