KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentResource > document > ViewDocumentAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.io.ByteArrayInputStream JavaDoc;
36
37 /**
38  * <p>View document
39  * </p>
40  * <p><a HREF="ViewDocumentAction.java.htm"><i>View Source</i></a></p>
41  * <p/>
42  *
43  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
44  * @version $Revision: 1.5 $ $Date: 2006/03/10 17:10:22 $
45  * @struts.action path="/core/contentResource/document/view"
46  * name="contentDocumentForm"
47  * scope="request"
48  * input="listContentResources"
49  * validate="false"
50  * roles="core-contentResource-view"
51  * @struts.action-forward name="viewDocument"
52  * path=".core.contentResource.document.view"
53  * @struts.action-forward name="listContentResources"
54  * path="/core/contentResource/list.do"
55  * redirect="true"
56  */

57 public final class ViewDocumentAction extends BaseAction {
58     /**
59      * @param mapping The ActionMapping used to select this instance
60      * @param form The optional ActionForm bean for this request (if any)
61      * @param request The HTTP request we are proceeding
62      * @param response The HTTP response we are creating
63      * @return an ActionForward instance describing where and how
64      * control should be forwarded, or null if response
65      * has already been completed
66      */

67     public ActionForward execute(ActionMapping mapping, ActionForm form,
68                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
69         resetToken(request);
70         ContentDocumentForm documentForm = (ContentDocumentForm) form;
71         Long JavaDoc 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 JavaDoc) 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             // document not found. it might be deleted by someone else
88
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 JavaDoc plainText = null;
95         try {
96             plainText = new PlainTextExtractor().extract(new ByteArrayInputStream JavaDoc(document.getResourceData().getData()), document.getMimeType(), document.getCharset());
97         } catch ( Exception JavaDoc 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 JavaDoc summarySize = GlobalProperties.getInstance(servlet.getServletContext()).getInteger(WebappConstants.DOCUMENT_SUMMARY_SIZE_KEY, new Integer JavaDoc(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