KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > library > struts > UploadHTMLAction


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: UploadHTMLAction.java,v $
31  * Revision 1.3 2005/04/10 20:31:58 colinmacleod
32  * Added new themes.
33  * Changed id type to String.
34  * Changed i tag to em and b tag to strong.
35  * Improved PicoContainerFactory with NanoContainer scripts.
36  *
37  * Revision 1.2 2005/04/09 17:19:46 colinmacleod
38  * Changed copyright text to GPL v2 explicitly.
39  *
40  * Revision 1.1.1.1 2005/03/10 17:52:01 colinmacleod
41  * Restructured ivata op around Hibernate/PicoContainer.
42  * Renamed ivata groupware.
43  *
44  * Revision 1.10 2004/12/31 18:27:44 colinmacleod
45  * Added MaskFactory to constructor of MaskAction.
46  *
47  * Revision 1.9 2004/12/23 21:01:29 colinmacleod
48  * Updated Struts to v1.2.4.
49  * Changed base classes to use ivata masks.
50  *
51  * Revision 1.8 2004/11/12 18:19:15 colinmacleod
52  * Change action and form classes to extend MaskAction, MaskForm respectively.
53  *
54  * Revision 1.7 2004/11/03 15:31:51 colinmacleod
55  * Change method interfaces to remove log.
56  *
57  * Revision 1.6 2004/08/01 11:45:19 colinmacleod
58  * Restructured search engine into separate subproject.
59  *
60  * Revision 1.5 2004/07/13 19:47:29 colinmacleod
61  * Moved project to POJOs from EJBs.
62  * Applied PicoContainer to services layer (replacing session EJBs).
63  * Applied Hibernate to persistence layer (replacing entity EJBs).
64  *
65  * Revision 1.4 2004/03/21 21:16:29 colinmacleod
66  * Shortened name to ivata op.
67  *
68  * Revision 1.3 2004/02/10 19:57:24 colinmacleod
69  * Changed email address.
70  *
71  * Revision 1.2 2004/02/01 22:07:31 colinmacleod
72  * Added full names to author tags
73  *
74  * Revision 1.1.1.1 2004/01/27 20:58:42 colinmacleod
75  * Moved ivata openportal to SourceForge..
76  *
77  * Revision 1.3 2003/10/28 13:16:14 jano
78  * commiting library,
79  * still fixing compile and building openGroupware project
80  *
81  * Revision 1.2 2003/10/15 14:16:53 colin
82  * fixing for XDoclet
83  *
84  * Revision 1.2 2003/07/08 07:55:56 peter
85  * fixed logic for already uploaded images
86  * Revision 1.1 2003/07/08 06:31:33 peter
87  * added to cvs
88  * -----------------------------------------------------------------------------
89  */

90 package com.ivata.groupware.business.library.struts;
91
92 import java.io.BufferedReader JavaDoc;
93 import java.io.FileNotFoundException JavaDoc;
94 import java.io.IOException JavaDoc;
95 import java.io.InputStream JavaDoc;
96 import java.io.InputStreamReader JavaDoc;
97 import java.util.Iterator JavaDoc;
98 import java.util.List JavaDoc;
99 import java.util.Map JavaDoc;
100 import java.util.Set JavaDoc;
101 import java.util.Vector JavaDoc;
102
103 import javax.servlet.http.HttpServletRequest JavaDoc;
104 import javax.servlet.http.HttpServletResponse JavaDoc;
105 import javax.servlet.http.HttpSession JavaDoc;
106
107 import org.apache.struts.action.ActionErrors;
108 import org.apache.struts.action.ActionForm;
109 import org.apache.struts.action.ActionMapping;
110
111 import com.ivata.groupware.web.format.SanitizerFormat;
112 import com.ivata.mask.MaskFactory;
113 import com.ivata.mask.util.SystemException;
114 import com.ivata.mask.web.struts.MaskAction;
115 import com.ivata.mask.web.struts.MaskAuthenticator;
116
117
118 /**
119  * <p>Invoked when the user uploads a HTML file to a library
120  * document.</p>
121  *
122  * @since 2003-07-02
123  * @author Peter Illes
124  * @version $Revision: 1.3 $
125  *
126  */

127 public class UploadHTMLAction extends MaskAction {
128     /**
129      * <p>
130      * Constructor. Called by <strong>PicoContainer.</strong>.
131      * </p>
132      *
133      * @param maskFactory This factory is needed to access the masks and groups
134      * of masks.
135      * @param authenticator used to confirm whether or not the
136      * user should be allowed to continue, in the <code>execute</code> method.
137      */

138     public UploadHTMLAction(MaskFactory maskFactory, MaskAuthenticator authenticator) {
139         super(maskFactory, authenticator);
140     }
141     /**
142      * <p>Called from the other <code>execute</code> method, this can
143      * be overridden by each subclass to provide the <em>ivata</em>-specific
144      * processing required.</p>
145      *
146      * @param mapping The ActionMapping used to select this instance.
147      * @param log valid logging object to write messages to.
148      * @param errors valid errors object to append errors to. If there are
149      * any errors, the action will return to the input.
150      * @param form optional ActionForm bean for this request (if any)
151      * @param request non-HTTP request we are processing
152      * @param response The non-HTTP response we are creating
153      * @param session returned from the <code>request</code> parameter.
154      * @param userName valid, non-null user name from session.
155      * @param settings valid, non-null settings from session.
156      * @exception SystemException if there is any problem which
157      * prevents processing. It will result in the webapp being forwarded
158      * to
159      * the standard error page.
160      * @return this method returns the string used to identify the correct
161      * <em>Struts</em> <code>ActionForward</code> which should follow this
162      * page, or <code>null</code> if it should return to the input.
163      */

164     public String JavaDoc execute(ActionMapping mapping, ActionErrors errors,
165             ActionForm form, HttpServletRequest JavaDoc request,
166             HttpServletResponse JavaDoc response, HttpSession JavaDoc session)
167             throws SystemException {
168         return null;
169     }
170
171     /**
172      * <p>This method is called if the ok or apply buttons are
173      * pressed.</p>
174      *
175      * @param mapping The ActionMapping used to select this instance.
176      * @param log valid logging object to write messages to.
177      * @param errors valid errors object to append errors to. If there are
178      * any errors, the action will return to the input.
179      * @param form optional ActionForm bean for this request (if any)
180      * @param request non-HTTP request we are processing
181      * @param response The non-HTTP response we are creating
182      * @param session returned from the <code>request</code> parameter.
183      * @param userName valid, non-null user name from session.
184      * @param settings valid, non-null settings from session.
185      * @param ok <code>true</code> if the ok button was pressed, otherwise
186      * <code>false</code> if the apply button was pressed.
187      * @exception SystemException if there is any problem which
188      * prevents processing. It will result in the webapp being forwarded
189      * to
190      * the standard error page.
191      * @return this method returns the string used to identify the correct
192      * <em>Struts</em> <code>ActionForward</code> which should follow this
193      * page, or <code>null</code> if it should return to the input.
194      */

195     public String JavaDoc onConfirm(ActionMapping mapping,
196             ActionErrors errors, ActionForm form, HttpServletRequest JavaDoc request,
197             HttpServletResponse JavaDoc response, HttpSession JavaDoc session,
198             final String JavaDoc defaultForward)
199             throws SystemException {
200         UploadHTMLForm uploadForm = (UploadHTMLForm) form;
201         String JavaDoc returnForward = "utilClosePopUp";
202
203         try {
204             InputStream JavaDoc stream = uploadForm.getFile().getInputStream();
205             BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(stream));
206             StringBuffer JavaDoc messageBuffer = new StringBuffer JavaDoc();
207             char[] chbuf = new char[1024];
208             int count;
209
210             while ((count = in.read(chbuf)) != -1) {
211                 messageBuffer.append(chbuf, 0, count);
212             }
213
214             SanitizerFormat sanitizer = new SanitizerFormat();
215             sanitizer.setOnlyBodyContents(true);
216             sanitizer.setSourceName("user input");
217 // TODO Map parseResult = sanitizer.format(messageBuffer.toString());
218
Map JavaDoc parseResult = null;
219
220             List JavaDoc pages = (List JavaDoc) parseResult.get("pages");
221
222             if (!pages.isEmpty()) {
223                 ItemForm itemForm = (ItemForm) session.getAttribute(
224                         "libraryItemForm");
225 //TODO itemForm.getItem().setPages(new Vector(pages));
226
session.setAttribute("libraryItemForm", itemForm);
227
228                 Set JavaDoc images = (Set JavaDoc) parseResult.get("images");
229                 Vector JavaDoc itemAttachments = null;
230
231                 // the attachmnets are either in upload (new item) or in drive
232
/*TODO if (itemForm.getItem().getId() == null) {
233                     itemAttachments = itemForm.getUploadingFileList();
234                 } else {
235                     itemAttachments = itemForm.getFileList();
236                 }
237 */

238                 Vector JavaDoc newImages = new Vector JavaDoc();
239
240                 // look for images not attached yet
241
for (Iterator JavaDoc i = images.iterator(); i.hasNext();) {
242                     String JavaDoc currentImage = (String JavaDoc) i.next();
243                     boolean newImage = true;
244
245 /*TODO for (Iterator j = itemAttachments.iterator(); j.hasNext();) {
246                         if (currentImage.endsWith(
247                                     ((FileDO) j.next()).getFileName())) {
248                             newImage = false;
249
250                             break;
251                         }
252                     }
253 */

254                     if (newImage) {
255                         newImages.add(currentImage);
256                     }
257                 }
258
259                 // when there are ne images, go to image upload
260
if (!newImages.isEmpty()) {
261                     UploadImagesForm uploadImagesForm = new UploadImagesForm();
262                     uploadImagesForm.setImageFileName(newImages);
263                     request.setAttribute("libraryUploadImagesForm",
264                         uploadImagesForm);
265                     returnForward = "libraryImageUpload";
266                 } else {
267                     request.setAttribute("openerPage", "/library/submit.action");
268                 }
269             }
270         } catch (FileNotFoundException JavaDoc efnf) {
271             throw new SystemException(efnf);
272         } catch (IOException JavaDoc eio) {
273             throw new SystemException(eio);
274         }
275
276         return returnForward;
277     }
278 }
279
Popular Tags