View Javadoc
1   /*
2    * Copyright (c) 2002-2024, 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  
35  package fr.paris.lutece.plugins.quicklinks.web;
36  
37  import fr.paris.lutece.plugins.quicklinks.business.insertservice.InternalLinkInsertService;
38  import fr.paris.lutece.plugins.quicklinks.business.insertservice.InternalLinkInsertServiceHome;
39  import fr.paris.lutece.portal.business.page.Page;
40  import fr.paris.lutece.portal.business.page.PageHome;
41  import fr.paris.lutece.portal.business.user.AdminUser;
42  import fr.paris.lutece.portal.service.admin.AdminUserService;
43  import fr.paris.lutece.portal.service.message.AdminMessage;
44  import fr.paris.lutece.portal.service.message.AdminMessageService;
45  import fr.paris.lutece.portal.service.page.IPageService;
46  import fr.paris.lutece.portal.service.page.PageResourceIdService;
47  import fr.paris.lutece.portal.service.plugin.Plugin;
48  import fr.paris.lutece.portal.service.plugin.PluginService;
49  import fr.paris.lutece.portal.service.spring.SpringContextService;
50  import fr.paris.lutece.portal.service.template.AppTemplateService;
51  import fr.paris.lutece.portal.service.util.AppPathService;
52  import fr.paris.lutece.portal.service.util.AppPropertiesService;
53  import fr.paris.lutece.portal.web.constants.Messages;
54  import fr.paris.lutece.portal.web.insert.InsertServiceJspBean;
55  import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
56  import fr.paris.lutece.util.html.HtmlTemplate;
57  import fr.paris.lutece.util.url.UrlItem;
58  
59  import java.util.ArrayList;
60  import java.util.Collection;
61  import java.util.HashMap;
62  
63  import javax.servlet.http.HttpServletRequest;
64  
65  import static org.apache.commons.text.StringEscapeUtils.escapeEcmaScript;
66  
67  /**
68   * This class provides the user interface to manage InternalLink features
69   */
70  public class InternalLinkInsertServiceJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
71  {
72      // Constants
73      private static final long serialVersionUID = -5176913689822438398L;
74  
75      private static final String REGEX_PAGE_ID = "^[\\d]+$";
76  
77      //Parameters
78      private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
79      private static final String PARAMETER_PAGE_NAME = "page_name";
80      private static final String PARAMETER_PAGE_ID = "page";
81      private static final String PARAMETER_PAGE_ID_URL = "page_id";
82      private static final String PARAMETER_ALT = "alt";
83      private static final String PARAMETER_TARGET = "target";
84      private static final String PARAMETER_NAME = "name";
85      private static final String PARAMETER_INPUT = "input";
86  
87      //Properties
88      private static final String PROPERTY_SEARCH_MESSAGE = "message.warning.resulsearch.empty";
89  
90      //Markers
91      private static final String MARK_PLUGIN_NAME = "plugin_name";
92      private static final String MARK_SEARCH_MESSAGE = "search_message";
93      private static final String MARK_PAGES_LIST = "pages_list";
94      private static final String MARK_LIST_PAGE = "list_page";
95      private static final String MARK_URL = "url";
96      private static final String MARK_TARGET = "target";
97      private static final String MARK_ALT = "alt";
98      private static final String MARK_NAME = "name";
99      private static final String MARK_INPUT = "input";
100 
101     //Templates
102     private static final String TEMPLATE_SELECTOR_PAGE = "admin/plugins/quicklinks/internallinkinsertservice_selector.html";
103     private static final String TEMPLATE_LINK = "admin/plugins/quicklinks/internallinkinsertservice_link.html";
104     private AdminUser _user;
105     private transient Plugin _plugin;
106     private String _input;
107     private transient IPageService _pageService = (IPageService) SpringContextService.getBean( "pageService" );
108 
109     // Methods
110 
111     /**
112      * Return the html form for image selection.
113      *
114      * @param request
115      *         The Http Request
116      * @return The html form.
117      */
118     public String getInsertServiceSelectorUI( HttpServletRequest request )
119     {
120         init( request );
121 
122         AdminUser user = AdminUserService.getAdminUser( request );
123         String strSearch = request.getParameter( PARAMETER_PAGE_NAME );
124         String strResultSearch = ( strSearch == null ) ? "" : strSearch;
125         String strPageName = replaceString( strResultSearch, "'", "''" );
126 
127         HashMap model = getDefaultModel( );
128 
129         Collection<InternalLinkInsertService> listPages = InternalLinkInsertServiceHome.getPageListbyName( strPageName );
130         Collection<InternalLinkInsertService> listPagesAuthorized = new ArrayList<>( );
131 
132         for ( InternalLinkInsertService internalLinkInsertService : listPages )
133         {
134             if ( _pageService.isAuthorizedAdminPage( internalLinkInsertService.getIdPage( ), PageResourceIdService.PERMISSION_VIEW, user ) )
135             {
136                 listPagesAuthorized.add( internalLinkInsertService );
137             }
138         }
139 
140         model.put( MARK_PAGES_LIST, listPagesAuthorized );
141 
142         StringBuilder strListPage = new StringBuilder( );
143 
144         if ( listPages.isEmpty( ) )
145         {
146             model.put( MARK_SEARCH_MESSAGE, AppPropertiesService.getProperty( PROPERTY_SEARCH_MESSAGE ) );
147         }
148 
149         String strBaseUrl = AppPathService.getBaseUrl( request );
150 
151         // Search Message
152         model.put( MARK_URL, strBaseUrl );
153         model.put( MARK_LIST_PAGE, strListPage.toString( ) );
154 
155         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECTOR_PAGE, _user.getLocale( ), model );
156 
157         return template.getHtml( );
158     }
159 
160     /**
161      * Replaces a string in an initial string by another string
162      *
163      * @param str
164      *         the initial string
165      * @param oldStr
166      *         the string to replace
167      * @param newStr
168      *         the string which will replace the old string
169      * @return the cleaned string
170      */
171     private String replaceString( String str, String oldStr, String newStr )
172     {
173         // Temporary string to avoid assignment of parameter str
174         String cleanString = str;
175 
176         int index = 0;
177 
178         while ( true )
179         {
180             index = cleanString.lastIndexOf( oldStr );
181 
182             if ( ( index == 2 ) || ( index == -1 ) )
183             {
184                 break;
185             }
186 
187             cleanString = cleanString.substring( 0, index ) + newStr + cleanString.substring( index + oldStr.length( ) );
188         }
189 
190         return cleanString;
191     }
192 
193     /**
194      * Insert the specified url into HTML content
195      *
196      * @param request
197      *         The http request
198      * @return String The url
199      */
200     public String doInsertUrl( HttpServletRequest request )
201     {
202         init( request );
203 
204         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
205         String strTarget = request.getParameter( PARAMETER_TARGET );
206         String strAlt = request.getParameter( PARAMETER_ALT );
207         String strName = request.getParameter( PARAMETER_NAME );
208         HashMap<String, Object> model = new HashMap<>( );
209 
210         Page page = null;
211 
212         if ( ( strPageId == null ) || !strPageId.matches( REGEX_PAGE_ID ) )
213         {
214             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
215         }
216 
217         page = PageHome.findByPrimaryKey( Integer.parseInt( strPageId ) );
218 
219         UrlItem url = new UrlItem( AppPathService.getPortalUrl( ) );
220         url.addParameter( PARAMETER_PAGE_ID_URL, page.getId( ) );
221         model.put( MARK_URL, url.getUrl( ) );
222         model.put( MARK_TARGET, strTarget );
223         model.put( MARK_ALT, strAlt );
224         model.put( MARK_NAME, ( strName.length( ) == 0 ) ? page.getName( ) : strName );
225 
226         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_LINK, null, model );
227 
228         return insertUrl( request, _input, escapeEcmaScript( template.getHtml( ) ) );
229     }
230 
231     private void init( HttpServletRequest request )
232     {
233         String strPluginName = request.getParameter( PARAMETER_PLUGIN_NAME );
234         _user = AdminUserService.getAdminUser( request );
235         _plugin = PluginService.getPlugin( strPluginName );
236         _input = request.getParameter( PARAMETER_INPUT );
237     }
238 
239     private HashMap getDefaultModel( )
240     {
241         HashMap model = new HashMap( );
242         model.put( MARK_PLUGIN_NAME, _plugin.getName( ) );
243         model.put( MARK_INPUT, _input );
244 
245         return model;
246     }
247 }