KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > servlets > RequestInterceptor


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.servlets;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.core.ContentHandler;
18 import info.magnolia.cms.core.HierarchyManager;
19 import info.magnolia.cms.core.ItemType;
20 import info.magnolia.cms.core.MetaData;
21 import info.magnolia.cms.core.Path;
22 import info.magnolia.cms.security.AccessDeniedException;
23 import info.magnolia.cms.security.SessionAccessControl;
24 import info.magnolia.cms.util.Resource;
25
26 import java.util.Iterator JavaDoc;
27
28 import javax.jcr.PathNotFoundException;
29 import javax.jcr.RepositoryException;
30 import javax.servlet.http.HttpServlet JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.apache.commons.lang.BooleanUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.log4j.Logger;
37
38
39 /**
40  * @version 2.0
41  */

42 public class RequestInterceptor extends HttpServlet JavaDoc {
43
44     /**
45      * Action: sort a paragraph.
46      */

47     private static final String JavaDoc ACTION_NODE_SORT = "NODE_SORT"; //$NON-NLS-1$
48

49     /**
50      * Action: delete a paragraph.
51      */

52     private static final String JavaDoc ACTION_NODE_DELETE = "NODE_DELETE"; //$NON-NLS-1$
53

54     /**
55      * Action: preview a page.
56      */

57     private static final String JavaDoc ACTION_PREVIEW = "PREVIEW"; //$NON-NLS-1$
58

59     /**
60      * request parameter: repository name.
61      */

62     private static final String JavaDoc PARAM_REPOSITORY = "mgnlRepository"; //$NON-NLS-1$
63

64     /**
65      * request parameter: node path, used for paragraph deletion.
66      */

67     private static final String JavaDoc PARAM_PATH = "mgnlPath"; //$NON-NLS-1$
68

69     /**
70      * request parameter: sort-above paragraph.
71      */

72     private static final String JavaDoc PARAM_PATH_SORT_ABOVE = "mgnlPathSortAbove"; //$NON-NLS-1$
73

74     /**
75      * request parameter: selected paragraph.
76      */

77     private static final String JavaDoc PARAM_PATH_SELECTED = "mgnlPathSelected"; //$NON-NLS-1$
78

79     /**
80      * Stable serialVersionUID.
81      */

82     private static final long serialVersionUID = 222L;
83
84     /**
85      * Logger.
86      */

87     private static Logger log = Logger.getLogger(RequestInterceptor.class);
88
89     /**
90      * Request and Response here is same as receivced by the original page so it includes all post/get data. Sub action
91      * could be called from here once this action finishes, it will continue loading the requested page.
92      */

93     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
94
95         String JavaDoc action = request.getParameter(EntryServlet.INTERCEPT);
96         String JavaDoc repository = request.getParameter(PARAM_REPOSITORY);
97         if (repository == null) {
98             repository = ContentRepository.WEBSITE;
99         }
100         HierarchyManager hm = SessionAccessControl.getHierarchyManager(request, repository);
101         if (action.equals(ACTION_PREVIEW)) {
102             // preview mode (button in main bar)
103
String JavaDoc preview = request.getParameter(Resource.MGNL_PREVIEW_ATTRIBUTE);
104             if (preview != null) {
105                 if (BooleanUtils.toBoolean(preview)) {
106                     request.getSession().setAttribute(Resource.MGNL_PREVIEW_ATTRIBUTE, Boolean.TRUE);
107                 }
108                 else {
109                     request.getSession().removeAttribute(Resource.MGNL_PREVIEW_ATTRIBUTE);
110                 }
111             }
112         }
113         else if (action.equals(ACTION_NODE_DELETE)) {
114             // delete paragraph
115
try {
116                 String JavaDoc path = request.getParameter(PARAM_PATH);
117                 // deactivate
118
updatePageMetaData(request, hm);
119                 hm.delete(path);
120                 hm.save();
121             }
122             catch (RepositoryException e) {
123                 log.error("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
124
}
125         }
126         else if (action.equals(ACTION_NODE_SORT)) {
127             // sort paragrpahs
128
try {
129                 String JavaDoc pathSelected = request.getParameter(PARAM_PATH_SELECTED);
130                 String JavaDoc pathSortAbove = request.getParameter(PARAM_PATH_SORT_ABOVE);
131                 String JavaDoc pathParent = StringUtils.substringBeforeLast(pathSelected, "/"); //$NON-NLS-1$
132
Iterator JavaDoc it = hm.getContent(pathParent).getChildren(
133                     ItemType.CONTENTNODE.getSystemName(),
134                     ContentHandler.SORT_BY_SEQUENCE).iterator();
135                 long seqPos0 = 0;
136                 long seqPos1 = 0;
137                 while (it.hasNext()) {
138                     Content c = (Content) it.next();
139                     if (c.getHandle().equals(pathSortAbove)) {
140                         seqPos1 = c.getMetaData().getSequencePosition();
141                         break;
142                     }
143                     seqPos0 = c.getMetaData().getSequencePosition();
144                 }
145                 Content nodeSelected = hm.getContent(pathSelected);
146                 if (seqPos0 == 0) {
147                     // move to first position -> 1000*coefficient above seqPos1 (old first)
148
nodeSelected
149                         .getMetaData()
150                         .setSequencePosition(seqPos1 - (MetaData.SEQUENCE_POS_COEFFICIENT * 1000));
151                 }
152                 else if (seqPos1 == 0) {
153                     // move to last position (pathSortAbove not found)
154
nodeSelected.getMetaData().setSequencePosition();
155                 }
156                 else {
157                     // move between two paragraphs
158
nodeSelected.getMetaData().setSequencePosition((seqPos0 + seqPos1) / 2);
159                 }
160
161                 this.updatePageMetaData(request, hm);
162                 hm.save();
163             }
164             catch (RepositoryException e) {
165                 log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
166
}
167         }
168     }
169
170     /**
171      * @param request
172      * @param hm
173      * @throws PathNotFoundException
174      * @throws RepositoryException
175      * @throws AccessDeniedException
176      */

177     private void updatePageMetaData(HttpServletRequest JavaDoc request, HierarchyManager hm) throws PathNotFoundException,
178         RepositoryException, AccessDeniedException {
179         String JavaDoc pagePath = StringUtils.substringBeforeLast(Path.getURI(request), "."); //$NON-NLS-1$
180
Content page = hm.getContent(pagePath);
181         page.updateMetaData(request);
182     }
183 }
184
Popular Tags