KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > administration > A_CmsImportFromHttp


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/administration/A_CmsImportFromHttp.java,v $
3  * Date : $Date: 2005/06/23 11:11:23 $
4  * Version: $Revision: 1.7 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.administration;
33
34 import org.opencms.file.CmsResource;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.main.CmsException;
37 import org.opencms.main.CmsIllegalArgumentException;
38 import org.opencms.main.OpenCms;
39 import org.opencms.util.CmsRfsException;
40 import org.opencms.util.CmsStringUtil;
41 import org.opencms.workplace.CmsDialog;
42 import org.opencms.workplace.CmsWorkplaceSettings;
43
44 import java.io.File JavaDoc;
45 import java.io.FileNotFoundException JavaDoc;
46 import java.io.FileOutputStream JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.OutputStream JavaDoc;
49 import java.util.Iterator JavaDoc;
50
51 import javax.servlet.ServletException JavaDoc;
52 import javax.servlet.http.HttpServletRequest JavaDoc;
53 import javax.servlet.http.HttpServletResponse JavaDoc;
54 import javax.servlet.jsp.JspException JavaDoc;
55 import javax.servlet.jsp.JspWriter JavaDoc;
56 import javax.servlet.jsp.PageContext JavaDoc;
57
58 import org.apache.commons.fileupload.FileItem;
59
60 /**
61  * Abstract class to upload a zip file containing VFS resources with HTTP upload.<p>
62  *
63  * @author Andreas Zahner
64  * @author Michael Emmerich
65  *
66  * @version $Revision: 1.7 $
67  *
68  * @since 6.0.0
69  */

70 public abstract class A_CmsImportFromHttp extends CmsDialog {
71
72     /** The dialog type. */
73     public static final String JavaDoc DIALOG_TYPE = "ImportHttp";
74
75     /** Import file request parameter. */
76     public static final String JavaDoc PARAM_IMPORTFILE = "importfile";
77
78     /** The exception thrown if an error occurs. */
79     private CmsException m_exception;
80
81     /** The import file name that is uploaded. */
82     private String JavaDoc m_paramImportfile;
83
84     /**
85      * Public constructor with JSP action element.<p>
86      *
87      * @param jsp an initialized JSP action element
88      */

89     public A_CmsImportFromHttp(CmsJspActionElement jsp) {
90
91         super(jsp);
92     }
93
94     /**
95      * Public constructor with JSP variables.<p>
96      *
97      * @param context the JSP page context
98      * @param req the JSP request
99      * @param res the JSP response
100      */

101     public A_CmsImportFromHttp(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
102
103         this(new CmsJspActionElement(context, req, res));
104     }
105
106     /**
107      * Performs the import operation after "OK" has been pressed.<p>
108      *
109      * @throws IOException in case of errros forwarding to the required result page
110      * @throws ServletException in case of errros forwarding to the required result page
111      */

112     public abstract void actionCommit() throws IOException JavaDoc, ServletException JavaDoc;
113
114     /**
115      * Performs the dialog actions depending on the initialized action and displays the dialog form.<p>
116      *
117      * @throws JspException if dialog actions fail
118      * @throws IOException if writing to the JSP out fails, or in case of errros forwarding to the required result page
119      * @throws ServletException in case of errros forwarding to the required result page
120      */

121     public void displayDialog() throws IOException JavaDoc, JspException JavaDoc, ServletException JavaDoc {
122
123         switch (getAction()) {
124
125             case ACTION_CANCEL:
126                 // ACTION: cancel button pressed
127
actionCloseDialog();
128                 break;
129
130             case ACTION_OK:
131                 // ACTION: ok button pressed
132
setParamAction(DIALOG_OK);
133                 actionCommit();
134                 if (getException() == null) {
135                     // file successfully copied to server
136
break;
137                 }
138
139             case ACTION_DEFAULT:
140             default:
141                 // ACTION: show dialog (default)
142
setParamAction(DIALOG_OK);
143                 JspWriter JavaDoc out = getJsp().getJspContext().getOut();
144                 out.print(defaultActionHtml());
145         }
146     }
147
148     /**
149      * Gets the return uri for this dialog.<p>
150      * @return return uri for this dialog
151      */

152     public abstract String JavaDoc getDialogReturnUri();
153
154     /**
155      * Gets the localized import message text for the imput form.<p>
156      * @return localized import message text for the imput form
157      */

158     public abstract String JavaDoc getImportMessage();
159
160     /**
161      * Returns the import file name that is uploaded.<p>
162      *
163      * @return the import file name that is uploaded
164      */

165     public String JavaDoc getParamImportfile() {
166
167         return m_paramImportfile;
168     }
169
170     /**
171      * Gets the localized starttext for the imput form.<p>
172      * @return localized starttext for the imput form
173      */

174     public abstract String JavaDoc getStarttext();
175
176     /**
177      * Sets the import file name that is uploaded.<p>
178      *
179      * @param importfile the import file name that is uploaded
180      */

181     public void setParamImportfile(String JavaDoc importfile) {
182
183         m_paramImportfile = importfile;
184     }
185
186     /**
187      * Gets a database importfile from the client and copys it to the server.<p>
188      *
189      * @param destination the destination of the file on the server
190      * @return the name of the file or null if something went wrong when importing the file.
191      *
192      * @throws CmsIllegalArgumentException if the specified file name is invalid
193      * @throws CmsRfsException if generating folders or files on the server fails
194      */

195     protected String JavaDoc copyFileToServer(String JavaDoc destination) throws CmsIllegalArgumentException, CmsRfsException {
196
197         // get the file item from the multipart request
198
Iterator JavaDoc i = getMultiPartFileItems().iterator();
199         FileItem fi = null;
200         while (i.hasNext()) {
201             fi = (FileItem)i.next();
202             if (fi.getName() != null) {
203                 // found the file object, leave iteration
204
break;
205             } else {
206                 // this is no file object, check next item
207
continue;
208             }
209         }
210
211         String JavaDoc fileName = null;
212
213         if (fi != null && CmsStringUtil.isNotEmptyOrWhitespaceOnly(fi.getName())) {
214             // file name has been specified, upload the file
215
fileName = fi.getName();
216             byte[] content = fi.get();
217             fi.delete();
218             // get the file name without folder information
219
fileName = CmsResource.getName(fileName.replace('\\', '/'));
220             // first create the folder if it does not exist
221
File JavaDoc discFolder = new File JavaDoc(OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
222                 OpenCms.getSystemInfo().getPackagesRfsPath() + File.separator));
223             if (!discFolder.exists()) {
224                 if (!discFolder.mkdir()) {
225                     throw new CmsRfsException(Messages.get().container(Messages.ERR_FOLDER_NOT_CREATED_0));
226                 }
227             }
228             // write the file into the packages folder of the OpenCms server
229
File JavaDoc discFile = new File JavaDoc(OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
230                 destination + File.separator + fileName));
231             try {
232                 // write the new file to disk
233
OutputStream JavaDoc s = new FileOutputStream JavaDoc(discFile);
234                 s.write(content);
235                 s.close();
236             } catch (FileNotFoundException JavaDoc e) {
237                 throw new CmsRfsException(Messages.get().container(Messages.ERR_FILE_NOT_FOUND_1, fileName, e));
238             } catch (IOException JavaDoc e) {
239                 throw new CmsRfsException(Messages.get().container(Messages.ERR_FILE_NOT_WRITTEN_0, e));
240             }
241         } else {
242             // no file name has been specified, throw exception
243
throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_FILE_NOT_SPECIFIED_0));
244         }
245         // set the request parameter to the name of the import file
246
setParamImportfile(fileName);
247         return fileName;
248     }
249
250     /**
251      * Creates the HTML for the error message if validation errors were found.<p>
252      *
253      * @return the HTML for the error message if validation errors were found
254      */

255     protected String JavaDoc createDialogErrorMessage() {
256
257         if (getException() != null) {
258             StringBuffer JavaDoc result = new StringBuffer JavaDoc(8);
259             result.append(dialogBlockStart(""));
260             result.append("<table border=\"0\">\n");
261             result.append("<tr><td><img SRC=\"");
262             result.append(getSkinUri()).append("commons/");
263             result.append("error.png");
264             result.append("\" border=\"0\" alt=\"\"></td><td class=\"xmlTdError maxwidth\">");
265             Throwable JavaDoc t = getException();
266             while (t != null) {
267                 result.append(t.getLocalizedMessage());
268                 t = t.getCause();
269                 if (t != null) {
270                     result.append("<br>");
271                 }
272             }
273             result.append("</table>\n");
274             result.append(dialogBlockEnd());
275             return result.toString();
276         }
277         return "";
278     }
279
280     /**
281      * Returns the HTML to build the input form of the upload dialog.<p>
282      *
283      * @return the HTML to build the input form of the upload dialog
284      */

285     protected String JavaDoc defaultActionHtml() {
286
287         StringBuffer JavaDoc result = new StringBuffer JavaDoc(32);
288
289         result.append(htmlStart());
290         result.append(bodyStart(null));
291         result.append(dialogStart());
292         result.append(dialogContentStart(""));
293         result.append("<form name=\"main\" class=\"nomargin\" action=\"");
294         result.append(getJsp().link(getDialogReturnUri()));
295         result.append("\" method=\"post\" onsubmit=\"submitAction('");
296         result.append(DIALOG_OK);
297         result.append("', null, 'main');\" enctype=\"multipart/form-data\">\n");
298         result.append(paramsAsHidden());
299         if (getParamFramename() == null) {
300             result.append("<input type=\"hidden\" name=\"");
301             result.append(PARAM_FRAMENAME);
302             result.append("\" value=\"\">");
303         }
304         result.append(createDialogErrorMessage());
305         result.append(dialogBlockStart(getStarttext()));
306         result.append("<table border=\"0\" width=\"100%\">\n");
307         result.append("<tr>\n\t<td style=\"white-space: nowrap;\" unselectable=\"on\">");
308         result.append(getImportMessage());
309         result.append(":</td>\n");
310         result.append("\t<td class=\"maxwidth\">");
311         result.append("<input type=\"file\" name=\"");
312         result.append(PARAM_IMPORTFILE);
313         result.append("\" class=\"maxwidth\" accept=\"application/zip\">");
314         result.append("</td>\n</tr>");
315         result.append("</table>\n");
316         result.append(dialogBlockEnd());
317
318         result.append(dialogContentEnd());
319         result.append(dialogButtonsOkCancel());
320         result.append("</form>\n");
321         result.append(dialogEnd());
322         result.append(bodyEnd());
323         result.append(htmlEnd());
324         return result.toString();
325     }
326
327     /**
328      * Returns the dialog exception.<p>
329      *
330      * @return the dialog exception
331      */

332     protected CmsException getException() {
333
334         return m_exception;
335     }
336
337     /**
338      * @see org.opencms.workplace.CmsWorkplace#initMessages()
339      */

340     protected abstract void initMessages();
341
342     /**
343      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
344      */

345     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
346
347         // set the dialog type
348
setParamDialogtype(getClass().getName());
349
350         // fill the parameter values in the get/set methods
351
fillParamValues(request);
352
353         // set the action for the JSP switch
354
if (DIALOG_OK.equals(getParamAction())) {
355             // ok button pressed
356
setAction(ACTION_OK);
357         } else if (DIALOG_CANCEL.equals(getParamAction())) {
358             // cancel button pressed
359
setAction(ACTION_CANCEL);
360         } else {
361             // first dialog call, set the default action
362
setAction(ACTION_DEFAULT);
363         }
364     }
365
366     /**
367      * Sets the dialog exception.<p>
368      *
369      * @param exception the dialog exception
370      */

371     protected void setException(CmsException exception) {
372
373         m_exception = exception;
374     }
375 }
Popular Tags