View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.document.modules.geoloc.service;
35  
36  import fr.paris.lutece.plugins.document.business.Document;
37  import fr.paris.lutece.plugins.document.business.DocumentHome;
38  import fr.paris.lutece.plugins.document.business.DocumentType;
39  import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
40  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
41  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttributeHome;
42  import fr.paris.lutece.plugins.document.business.portlet.DocumentListPortletHome;
43  import fr.paris.lutece.plugins.document.service.publishing.PublishingService;
44  import fr.paris.lutece.plugins.leaflet.rest.service.IPopupContentProvider;
45  import fr.paris.lutece.portal.business.XmlContent;
46  import fr.paris.lutece.portal.business.page.Page;
47  import fr.paris.lutece.portal.business.page.PageHome;
48  import fr.paris.lutece.portal.business.portlet.Portlet;
49  import fr.paris.lutece.portal.business.style.ModeHome;
50  import fr.paris.lutece.portal.business.style.StyleHome;
51  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
52  import fr.paris.lutece.portal.business.stylesheet.StyleSheetHome;
53  import fr.paris.lutece.portal.service.html.XmlTransformerService;
54  import fr.paris.lutece.portal.service.portal.PortalMenuService;
55  import fr.paris.lutece.portal.service.security.SecurityService;
56  import fr.paris.lutece.portal.service.util.AppLogService;
57  import fr.paris.lutece.portal.service.util.AppPathService;
58  import fr.paris.lutece.portal.web.constants.Parameters;
59  import fr.paris.lutece.portal.web.l10n.LocaleService;
60  import fr.paris.lutece.util.xml.XmlUtil;
61  
62  import java.util.Collection;
63  import java.util.HashMap;
64  import java.util.Iterator;
65  import java.util.Map;
66  import java.util.Map.Entry;
67  
68  import javax.servlet.http.HttpServletRequest;
69  
70  public class DocumentPopupContentProvider implements IPopupContentProvider
71  {
72      // Attribute parameter pointing to a style
73      private static final String ATTR_PARAMETER_STYLE = "style";
74  
75      // Xsl cache key
76      private static final String DOCUMENT_STYLE_PREFIX_ID = "document-popup-";
77  
78      // To mimic a DocumentPortlet
79      private static final String TAG_DOCUMENT_PORTLET = "document-portlet";
80      private static final String VALUE_TRUE = "1";
81      private static final String VALUE_FALSE = "0";
82  
83      // Parameters, copy from PageService.java
84      private static final String MARKER_IS_USER_AUTHENTICATED = "is-user-authenticated";
85      private static final String PARAMETER_SITE_PATH = "site-path";
86      private static final String PARAMETER_USER_SELECTED_LOCALE = "user-selected-language";
87      private static final String PARAMETER_PLUGIN_NAME = "plugin-name";
88      private static final String PARAMETER_PORTLET = "portlet";
89  
90      public String getPopup( HttpServletRequest request, String strIdDocument, String strCode )
91      {
92          int nDocId;
93  
94          try
95          {
96              nDocId = Integer.parseInt( strIdDocument );
97          }
98          catch( NumberFormatException nfe )
99          {
100             AppLogService.error( "Document popup rest API: invalid docId: " + strIdDocument + " exeception " + nfe );
101 
102             return null;
103         }
104 
105         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocId );
106 
107         if ( ( document == null ) || ( !document.isValid( ) ) )
108         {
109             AppLogService.error( "Document popup rest API: invalid document " + strIdDocument );
110 
111             return null;
112         }
113 
114         // Find first portlet of type DocumentList
115         Collection<Portlet> portlets = PublishingService.getInstance( ).getPortletsByDocumentId( strIdDocument );
116 
117         if ( portlets.size( ) == 0 )
118         {
119             AppLogService.error( "Document popup rest API: no portlets for doc " + strIdDocument );
120 
121             return null;
122         }
123 
124         Iterator<Portlet> iterator = portlets.iterator( );
125 
126         // Find first portlet with correct settings
127         Portlet portlet = null;
128 
129         do
130         {
131             Portlet p = iterator.next( );
132 
133             if ( p.getStatus( ) == Portlet.STATUS_UNPUBLISHED )
134             {
135                 AppLogService.debug( "Document popup rest API: refuse unpublished portlet for " + strIdDocument + ", portlet " + p.getId( ) );
136 
137                 continue;
138             }
139 
140             if ( SecurityService.isAuthenticationEnable( ) )
141             {
142                 String strRolePortlet = p.getRole( );
143 
144                 if ( !strRolePortlet.equals( Page.ROLE_NONE ) )
145                 {
146                     if ( !SecurityService.getInstance( ).isUserInRole( request, strRolePortlet ) )
147                     {
148                         AppLogService.debug(
149                                 "Document popup rest API: refuse portlet role for " + strIdDocument + ", portlet " + p.getId( ) + ", role " + strRolePortlet );
150 
151                         continue;
152                     }
153                 }
154 
155                 String strRolePage = PageHome.getPage( p.getPageId( ) ).getRole( );
156 
157                 if ( !strRolePage.equals( Page.ROLE_NONE ) )
158                 {
159                     if ( !SecurityService.getInstance( ).isUserInRole( request, strRolePage ) )
160                     {
161                         AppLogService.debug(
162                                 "Document popup rest API: refuse page role for " + strIdDocument + ", portlet " + p.getId( ) + ", role " + strRolePage );
163 
164                         continue;
165                     }
166                 }
167             }
168 
169             portlet = p;
170         }
171         while ( ( portlet == null ) && iterator.hasNext( ) );
172 
173         if ( portlet == null )
174         {
175             AppLogService.error( "Document popup rest API: no matching DocumentList portlets for doc " + strIdDocument );
176 
177             return null;
178         }
179 
180         DocumentType type = DocumentTypeHome.findByPrimaryKey( document.getCodeDocumentType( ) );
181         DocumentAttribute attribute = null;
182 
183         for ( DocumentAttribute _attribute : type.getAttributes( ) )
184         {
185             if ( _attribute.getCode( ).equals( strCode ) )
186             {
187                 attribute = _attribute;
188 
189                 break;
190             }
191         }
192 
193         if ( attribute == null )
194         {
195             AppLogService.error( "Document popup rest API: no attribute " + strCode + " for doc " + strIdDocument + " of type " + type.getCode( ) );
196 
197             return null;
198         }
199 
200         String strStyleId = DocumentAttributeHome.getAttributeParameterValues( attribute.getId( ), ATTR_PARAMETER_STYLE ).get( 0 );
201         Collection<StyleSheet> listStyleSheet = StyleHome.getStyleSheetList( Integer.parseInt( strStyleId ) );
202 
203         if ( listStyleSheet.isEmpty( ) )
204         {
205             AppLogService.error( "Document popup rest API: no stylesheet for style " + strStyleId );
206 
207             return null;
208         }
209 
210         StyleSheet stylesheet = StyleSheetHome.findByPrimaryKey( listStyleSheet.iterator( ).next( ).getId( ) );
211 
212         // Copy from Portlet addPortletTags
213         StringBuffer strXml = new StringBuffer( );
214         XmlUtil.beginElement( strXml, XmlContent.TAG_PORTLET );
215         XmlUtil.addElementHtml( strXml, XmlContent.TAG_PORTLET_NAME, portlet.getName( ) );
216         XmlUtil.addElement( strXml, XmlContent.TAG_PORTLET_ID, portlet.getId( ) );
217         XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, portlet.getPageId( ) );
218         XmlUtil.addElement( strXml, XmlContent.TAG_PLUGIN_NAME, portlet.getPluginName( ) );
219         XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_PORTLET_TITLE, portlet.getDisplayPortletTitle( ) );
220 
221         String strDisplayOnSmallDevice = ( ( portlet.getDeviceDisplayFlags( ) & Portlet.FLAG_DISPLAY_ON_SMALL_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
222         XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_SMALL_DEVICE, strDisplayOnSmallDevice );
223 
224         String strDisplayOnNormalDevice = ( ( portlet.getDeviceDisplayFlags( ) & Portlet.FLAG_DISPLAY_ON_NORMAL_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
225         XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_NORMAL_DEVICE, strDisplayOnNormalDevice );
226 
227         String strDisplayOnLargeDevice = ( ( portlet.getDeviceDisplayFlags( ) & Portlet.FLAG_DISPLAY_ON_LARGE_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
228         XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_LARGE_DEVICE, strDisplayOnLargeDevice );
229 
230         String strDisplayOnXLargeDevice = ( ( portlet.getDeviceDisplayFlags( ) & Portlet.FLAG_DISPLAY_ON_XLARGE_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
231         XmlUtil.addElement( strXml, XmlContent.TAG_DISPLAY_ON_XLARGE_DEVICE, strDisplayOnXLargeDevice );
232 
233         XmlUtil.beginElement( strXml, TAG_DOCUMENT_PORTLET );
234         strXml.append( document.getXml( request, portlet.getId( ) ) );
235         XmlUtil.endElement( strXml, TAG_DOCUMENT_PORTLET );
236         XmlUtil.endElement( strXml, XmlContent.TAG_PORTLET );
237 
238         XmlTransformerService xmlTransformerService = new XmlTransformerService( );
239         String strXslUniquePrefix = DOCUMENT_STYLE_PREFIX_ID + stylesheet.getId( );
240 
241         // Copy from PageService getParams
242         Map<String, String> mapModifyParam = new HashMap<String, String>( );
243         mapModifyParam.put( PARAMETER_USER_SELECTED_LOCALE, LocaleService.getUserSelectedLocale( request ).getLanguage( ) );
244 
245         mapModifyParam.put( PARAMETER_SITE_PATH, AppPathService.getPortalUrl( ) );
246 
247         if ( SecurityService.isAuthenticationEnable( ) )
248         {
249             mapModifyParam.put( MARKER_IS_USER_AUTHENTICATED,
250                     ( SecurityService.getInstance( ).getRegisteredUser( request ) != null ) ? VALUE_TRUE : VALUE_FALSE );
251         }
252 
253         mapModifyParam.put( Parameters.PAGE_ID, Integer.toString( portlet.getPageId( ) ) );
254 
255         Map<String, String> mapXslParams = portlet.getXslParams( );
256         Map<String, String> mapParams = mapModifyParam;
257 
258         if ( mapXslParams != null )
259         {
260             for ( Entry<String, String> entry : mapXslParams.entrySet( ) )
261             {
262                 mapParams.put( entry.getKey( ), entry.getValue( ) );
263             }
264         }
265 
266         String result = xmlTransformerService.transformBySourceWithXslCache( strXml.toString( ), stylesheet.getSource( ), strXslUniquePrefix, mapParams,
267                 ModeHome.getOuputXslProperties( PortalMenuService.MODE_NORMAL ) );
268 
269         return result;
270     }
271 }