1 16 package com.blandware.atleap.webapp.action.core.contentResource.document; 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.model.core.ContentDocument; 22 import com.blandware.atleap.service.core.ContentResourceManager; 23 import com.blandware.atleap.webapp.action.core.BaseAction; 24 import com.blandware.atleap.webapp.form.ContentDocumentForm; 25 import com.blandware.atleap.webapp.util.core.GlobalProperties; 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 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import java.io.ByteArrayInputStream ; 36 37 57 public final class ViewDocumentAction extends BaseAction { 58 67 public ActionForward execute(ActionMapping mapping, ActionForm form, 68 HttpServletRequest request, HttpServletResponse response) throws Exception { 69 resetToken(request); 70 ContentDocumentForm documentForm = (ContentDocumentForm) form; 71 Long contentResourceId = null; 72 if ( documentForm.getId() != null ) { 73 contentResourceId = Long.valueOf(documentForm.getId()); 74 } else if ( request.getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY) != null ) { 75 contentResourceId = (Long ) request.getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY); 76 } else { 77 if ( log.isWarnEnabled() ) { 78 log.warn("Missing content resource ID"); 79 } 80 return mapping.findForward("listContentResources"); 81 } 82 83 ContentResourceManager contentResourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN); 84 ContentDocument document = contentResourceManager.retrieveContentDocument(contentResourceId); 85 86 if ( document == null ) { 87 ActionMessages errors = new ActionMessages(); 89 errors.add("documentNotFound", new ActionMessage("core.contentResource.document.errors.notFound")); 90 saveErrors(request, errors); 91 return mapping.findForward("listContentResources"); 92 } 93 94 String plainText = null; 95 try { 96 plainText = new PlainTextExtractor().extract(new ByteArrayInputStream (document.getResourceData().getData()), document.getMimeType(), document.getCharset()); 97 } catch ( Exception ex ) { 98 ActionMessages errors = new ActionMessages(); 99 errors.add("contentResourceCorruptedData", new ActionMessage("core.contentResource.errors.corruptedData")); 100 saveErrors(request, errors); 101 } 102 if ( plainText != null ) { 103 Integer summarySize = GlobalProperties.getInstance(servlet.getServletContext()).getInteger(WebappConstants.DOCUMENT_SUMMARY_SIZE_KEY, new Integer (400)); 104 plainText = StringUtil.htmlEncode(StringUtil.shortenString(plainText, Math.min(plainText.length(), summarySize.intValue()))); 105 request.setAttribute(WebappConstants.CONTENT_DOCUMENT_NEW_SUMMARY_KEY, plainText); 106 } 107 108 request.setAttribute("document", document); 109 return mapping.findForward("viewDocument"); 110 } 111 } | Popular Tags |