1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.web.xpages;
35
36 import fr.paris.lutece.portal.business.XmlContent;
37 import fr.paris.lutece.portal.business.page.Page;
38 import fr.paris.lutece.portal.business.page.PageHome;
39 import fr.paris.lutece.portal.business.portalcomponent.PortalComponentHome;
40 import fr.paris.lutece.portal.business.style.ModeHome;
41 import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
42 import fr.paris.lutece.portal.service.html.XmlTransformerService;
43 import fr.paris.lutece.portal.service.i18n.I18nService;
44 import fr.paris.lutece.portal.service.plugin.Plugin;
45 import fr.paris.lutece.portal.service.portal.PortalService;
46 import fr.paris.lutece.portal.service.security.LuteceUser;
47 import fr.paris.lutece.portal.service.security.SecurityService;
48 import fr.paris.lutece.portal.service.util.AppPathService;
49 import fr.paris.lutece.portal.web.admin.AdminPageJspBean;
50 import fr.paris.lutece.util.xml.XmlUtil;
51
52 import org.apache.commons.lang.StringUtils;
53
54 import java.util.Arrays;
55 import java.util.HashMap;
56 import java.util.Locale;
57 import java.util.Map;
58 import java.util.Properties;
59
60 import javax.servlet.http.HttpServletRequest;
61
62
63
64
65
66 public class SiteMapApp implements XPageApplication
67 {
68
69
70
71 private static final long serialVersionUID = 1799476496018552101L;
72 private static final int PORTAL_COMPONENT_SITE_MAP_ID = 6;
73 private static final int MODE_NORMAL = 0;
74 private static final int MODE_ADMIN = 1;
75 private static final String PARAMETER_SITE_PATH = "site-path";
76 private static final String MARKER_TARGET = "target";
77 private static final String TARGET_TOP = "target='_top'";
78 private static final String PROPERTY_SERVICE_NAME = "portal.site.serviceName.siteMapService";
79 private static final String PROPERTY_PATH_LABEL = "portal.site.site_map.pathLabel";
80 private static final String PROPERTY_PAGE_TITLE = "portal.site.site_map.pageTitle";
81
82
83
84
85 public SiteMapApp( )
86 {
87 }
88
89
90
91
92
93
94 public String getName( Locale locale )
95 {
96 return I18nService.getLocalizedString( PROPERTY_SERVICE_NAME, locale );
97 }
98
99
100
101
102
103
104
105
106
107
108 @Override
109 public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
110 {
111 XPage page = new XPage( );
112 String strKey = getKey( nMode, request );
113
114 Locale locale = request.getLocale( );
115
116 SiteMapCacheService siteMapCacheService = SiteMapCacheService.getInstance( );
117
118
119 String strCachedPage = siteMapCacheService.isCacheEnable( )
120 ? (String) siteMapCacheService.getFromCache( strKey ) : null;
121
122 if ( strCachedPage == null )
123 {
124
125 String strPage = buildPageContent( nMode, request );
126
127
128 if ( siteMapCacheService.isCacheEnable( ) )
129 {
130 synchronized ( strKey )
131 {
132 siteMapCacheService.putInCache( strKey, strPage );
133 }
134 }
135
136 page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PATH_LABEL, locale ) );
137 page.setTitle( I18nService.getLocalizedString( PROPERTY_PAGE_TITLE, locale ) );
138 page.setContent( strPage );
139
140 return page;
141 }
142
143
144 page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PATH_LABEL, locale ) );
145 page.setTitle( I18nService.getLocalizedString( PROPERTY_PAGE_TITLE, locale ) );
146 page.setContent( strCachedPage );
147
148 return page;
149 }
150
151
152
153
154
155
156
157 private String getKey( int nMode, HttpServletRequest request )
158 {
159 String strRoles = "-";
160
161 if ( SecurityService.isAuthenticationEnable( ) )
162 {
163 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
164
165 if ( ( user != null ) && ( user.getRoles( ) != null ) )
166 {
167 String[] roles = user.getRoles( );
168 Arrays.sort( roles );
169 strRoles = StringUtils.join( roles, ',' );
170 }
171 }
172
173 return "[m:" + nMode + "][roles:" + strRoles + "]";
174 }
175
176
177
178
179
180
181
182
183
184 private String buildPageContent( int nMode, HttpServletRequest request )
185 {
186 StringBuffer strArborescenceXml = new StringBuffer( );
187 strArborescenceXml.append( XmlUtil.getXmlHeader( ) );
188
189 int nLevel = 0;
190 findPages( strArborescenceXml, PortalService.getRootPageId( ), nLevel, request );
191
192
193
194 StyleSheet xslSource;
195
196 switch ( nMode )
197 {
198 case MODE_NORMAL:
199 case MODE_ADMIN:
200 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_SITE_MAP_ID, MODE_NORMAL );
201
202 break;
203
204 default:
205 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_SITE_MAP_ID, nMode );
206
207 break;
208 }
209
210
211
212 Map<String, String> mapParamRequest = new HashMap<String, String>( );
213
214 if ( nMode != MODE_ADMIN )
215 {
216 mapParamRequest.put( PARAMETER_SITE_PATH, AppPathService.getPortalUrl( ) );
217 }
218 else
219 {
220 mapParamRequest.put( PARAMETER_SITE_PATH, AppPathService.getAdminPortalUrl( ) );
221 mapParamRequest.put( MARKER_TARGET, TARGET_TOP );
222 }
223
224 Properties outputProperties = ModeHome.getOuputXslProperties( nMode );
225
226 XmlTransformerService xmlTransformerService = new XmlTransformerService( );
227
228 return xmlTransformerService.transformBySourceWithXslCache( strArborescenceXml.toString( ), xslSource,
229 mapParamRequest, outputProperties );
230 }
231
232
233
234
235
236
237
238
239
240
241 private void findPages( StringBuffer strXmlArborescence, int nPageId, int nLevel, HttpServletRequest request )
242 {
243 Page page = PageHome.getPage( nPageId );
244
245 if ( page.isVisible( request ) )
246 {
247 XmlUtil.beginElement( strXmlArborescence, XmlContent.TAG_PAGE );
248 XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_ID, page.getId( ) );
249 XmlUtil.addElementHtml( strXmlArborescence, XmlContent.TAG_PAGE_NAME, page.getName( ) );
250 XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_DESCRIPTION, page.getDescription( ) );
251 XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_LEVEL, nLevel );
252
253 AdminPageJspBean adminPage = new AdminPageJspBean( );
254
255 if ( page.getImageContent( ) != null )
256 {
257 int nImageLength = page.getImageContent( ).length;
258
259 if ( nImageLength >= 1 )
260 {
261 String strPageId = Integer.toString( page.getId( ) );
262 XmlUtil.addElement( strXmlArborescence, XmlContent.TAG_PAGE_IMAGE,
263 adminPage.getResourceImagePage( page, strPageId ) );
264 }
265 }
266
267 XmlUtil.beginElement( strXmlArborescence, XmlContent.TAG_CHILD_PAGES_LIST );
268
269 for ( Page pageChild : PageHome.getChildPagesMinimalData( nPageId ) )
270 {
271 findPages( strXmlArborescence, pageChild.getId( ), nLevel + 1, request );
272 }
273
274 XmlUtil.endElement( strXmlArborescence, XmlContent.TAG_CHILD_PAGES_LIST );
275 XmlUtil.endElement( strXmlArborescence, XmlContent.TAG_PAGE );
276 }
277 }
278 }