1 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 ; 45 import java.io.FileNotFoundException ; 46 import java.io.FileOutputStream ; 47 import java.io.IOException ; 48 import java.io.OutputStream ; 49 import java.util.Iterator ; 50 51 import javax.servlet.ServletException ; 52 import javax.servlet.http.HttpServletRequest ; 53 import javax.servlet.http.HttpServletResponse ; 54 import javax.servlet.jsp.JspException ; 55 import javax.servlet.jsp.JspWriter ; 56 import javax.servlet.jsp.PageContext ; 57 58 import org.apache.commons.fileupload.FileItem; 59 60 70 public abstract class A_CmsImportFromHttp extends CmsDialog { 71 72 73 public static final String DIALOG_TYPE = "ImportHttp"; 74 75 76 public static final String PARAM_IMPORTFILE = "importfile"; 77 78 79 private CmsException m_exception; 80 81 82 private String m_paramImportfile; 83 84 89 public A_CmsImportFromHttp(CmsJspActionElement jsp) { 90 91 super(jsp); 92 } 93 94 101 public A_CmsImportFromHttp(PageContext context, HttpServletRequest req, HttpServletResponse res) { 102 103 this(new CmsJspActionElement(context, req, res)); 104 } 105 106 112 public abstract void actionCommit() throws IOException , ServletException ; 113 114 121 public void displayDialog() throws IOException , JspException , ServletException { 122 123 switch (getAction()) { 124 125 case ACTION_CANCEL: 126 actionCloseDialog(); 128 break; 129 130 case ACTION_OK: 131 setParamAction(DIALOG_OK); 133 actionCommit(); 134 if (getException() == null) { 135 break; 137 } 138 139 case ACTION_DEFAULT: 140 default: 141 setParamAction(DIALOG_OK); 143 JspWriter out = getJsp().getJspContext().getOut(); 144 out.print(defaultActionHtml()); 145 } 146 } 147 148 152 public abstract String getDialogReturnUri(); 153 154 158 public abstract String getImportMessage(); 159 160 165 public String getParamImportfile() { 166 167 return m_paramImportfile; 168 } 169 170 174 public abstract String getStarttext(); 175 176 181 public void setParamImportfile(String importfile) { 182 183 m_paramImportfile = importfile; 184 } 185 186 195 protected String copyFileToServer(String destination) throws CmsIllegalArgumentException, CmsRfsException { 196 197 Iterator i = getMultiPartFileItems().iterator(); 199 FileItem fi = null; 200 while (i.hasNext()) { 201 fi = (FileItem)i.next(); 202 if (fi.getName() != null) { 203 break; 205 } else { 206 continue; 208 } 209 } 210 211 String fileName = null; 212 213 if (fi != null && CmsStringUtil.isNotEmptyOrWhitespaceOnly(fi.getName())) { 214 fileName = fi.getName(); 216 byte[] content = fi.get(); 217 fi.delete(); 218 fileName = CmsResource.getName(fileName.replace('\\', '/')); 220 File discFolder = new File (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 File discFile = new File (OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf( 230 destination + File.separator + fileName)); 231 try { 232 OutputStream s = new FileOutputStream (discFile); 234 s.write(content); 235 s.close(); 236 } catch (FileNotFoundException e) { 237 throw new CmsRfsException(Messages.get().container(Messages.ERR_FILE_NOT_FOUND_1, fileName, e)); 238 } catch (IOException e) { 239 throw new CmsRfsException(Messages.get().container(Messages.ERR_FILE_NOT_WRITTEN_0, e)); 240 } 241 } else { 242 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_FILE_NOT_SPECIFIED_0)); 244 } 245 setParamImportfile(fileName); 247 return fileName; 248 } 249 250 255 protected String createDialogErrorMessage() { 256 257 if (getException() != null) { 258 StringBuffer result = new StringBuffer (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 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 285 protected String defaultActionHtml() { 286 287 StringBuffer result = new StringBuffer (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 332 protected CmsException getException() { 333 334 return m_exception; 335 } 336 337 340 protected abstract void initMessages(); 341 342 345 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 346 347 setParamDialogtype(getClass().getName()); 349 350 fillParamValues(request); 352 353 if (DIALOG_OK.equals(getParamAction())) { 355 setAction(ACTION_OK); 357 } else if (DIALOG_CANCEL.equals(getParamAction())) { 358 setAction(ACTION_CANCEL); 360 } else { 361 setAction(ACTION_DEFAULT); 363 } 364 } 365 366 371 protected void setException(CmsException exception) { 372 373 m_exception = exception; 374 } 375 } | Popular Tags |