1 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 ; 35 import javax.servlet.http.HttpServletResponse ; 36 import java.io.ByteArrayInputStream ; 37 import java.util.List ; 38 39 67 public final class CreateContentResourceAction extends BaseAction { 68 69 78 public ActionForward execute(ActionMapping mapping, ActionForm form, 79 HttpServletRequest request, HttpServletResponse response) throws Exception { 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 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 fileName = file.getFileName(); 100 String resourceType = resourceTypesManager.getResourceTypeByFileName(fileName); 101 String 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 try { 116 ImageUtil.decodeImage(file.getFileData(), mimeType); 117 } catch ( Exception 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 String plainText = null; 132 String determinedEncoding = null; 133 try { 134 PlainTextExtractor extractor = new PlainTextExtractor(); 135 plainText = extractor.extract(new ByteArrayInputStream (file.getFileData()), mimeType); 136 determinedEncoding = extractor.getUsedEncoding(); 137 } catch ( Exception 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 summarySize = GlobalProperties.getInstance(servlet.getServletContext()).getInteger(WebappConstants.DOCUMENT_SUMMARY_SIZE_KEY, new Integer (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 ("There are not action found for resource type=" + resourceType); 153 } 154 155 } 156 } | Popular Tags |