View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.portal.web.insert;
35  
36  import fr.paris.lutece.portal.service.html.EncodingService;
37  import fr.paris.lutece.portal.service.template.AppTemplateService;
38  import fr.paris.lutece.portal.service.util.AppPathService;
39  import fr.paris.lutece.portal.web.l10n.LocaleService;
40  import fr.paris.lutece.util.ReferenceList;
41  import fr.paris.lutece.util.html.HtmlTemplate;
42  import fr.paris.lutece.util.url.UrlItem;
43  
44  import org.apache.commons.text.StringEscapeUtils;
45  
46  import java.io.Serializable;
47  
48  import java.util.HashMap;
49  
50  import javax.servlet.http.HttpServletRequest;
51  
52  /**
53   * Base class for InsertServiceJspBean
54   */
55  public abstract class InsertServiceJspBean implements Serializable
56  {
57      private static final long serialVersionUID = -2870769178710689751L;
58      private static final String PARAMETER_MODE = "mode";
59      private static final String PARAMETER_INPUT = "input";
60      private static final String PARAMETER_INSERT = "insert";
61      private static final String JSP_DO_INSERT = "jsp/admin/insert/DoInsertIntoElement.jsp";
62      private static final String TEMPLATE_LINK = "/admin/insert/insert_link.html";
63      private static final String MARK_TEXT = "text";
64      private static final String MARK_URL = "url";
65      private static final String MARK_TITLE = "title";
66      private static final String MARK_TARGET = "target";
67  
68      /**
69       * Build the Url to insert HTML code into the current rich text editor
70       * 
71       * @param request
72       *            The HTTP request
73       * @param strInput
74       *            The rich text input field
75       * @param strInsert
76       *            The code to insert
77       * @return The Url that will provide the insertion
78       */
79      protected String insertUrl( HttpServletRequest request, String strInput, String strInsert )
80      {
81          // No CR is allowed in the insert string
82          String strCleanInsert = strInsert.replaceAll( "\n", "" );
83          strCleanInsert = strCleanInsert.replaceAll( "\r", "" );
84  
85          // Build the url to make the insert
86          UrlItemItem.html#UrlItem">UrlItem urlDoInsert = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_DO_INSERT );
87          urlDoInsert.addParameter( PARAMETER_INPUT, strInput );
88          request.getSession( ).setAttribute( InsertServiceSelectorJspBean.SESSION_INSERT, strCleanInsert );
89          urlDoInsert.addParameter( PARAMETER_MODE, 1 );
90  
91          return urlDoInsert.getUrl( );
92      }
93  
94      /**
95       * Build the Url to insert HTML code into the current rich text editor
96       * 
97       * @param request
98       *            The HTTP request
99       * @param strInput
100      *            The rich text input field
101      * @param strInsert
102      *            The code to insert
103      * @return The Url that will provide the insertion
104      */
105     protected String insertUrlWithoutEscape( HttpServletRequest request, String strInput, String strInsert )
106     {
107         String strInsertTmp = EncodingService.encodeUrl( strInsert );
108 
109         // Build the url to make the insert
110         UrlItemItem.html#UrlItem">UrlItem urlDoInsert = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_DO_INSERT );
111         urlDoInsert.addParameter( PARAMETER_INPUT, strInput );
112         urlDoInsert.addParameter( PARAMETER_INSERT, strInsertTmp );
113         urlDoInsert.addParameter( PARAMETER_MODE, 2 );
114 
115         return urlDoInsert.getUrl( );
116     }
117 
118     /**
119      * Build an HTML link
120      * 
121      * @param strText
122      *            The text of the link
123      * @param strUrl
124      *            The Url of the link
125      * @param strTitle
126      *            The title of the link
127      * @param strTarget
128      *            The target window
129      * @return The HTML link
130      */
131     protected String buildLink( String strText, String strUrl, String strTitle, String strTarget )
132     {
133         HashMap<String, Object> model = new HashMap<>( );
134         model.put( MARK_TEXT, StringEscapeUtils.escapeHtml4( strText ) );
135         model.put( MARK_URL, strUrl );
136         model.put( MARK_TITLE, StringEscapeUtils.escapeHtml4( strTitle ) );
137         model.put( MARK_TARGET, strTarget );
138 
139         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_LINK, LocaleService.getDefault( ), model );
140 
141         return template.getHtml( );
142     }
143 
144     /**
145      * List of supported sub categories that may be used to filter resources.
146      * 
147      * @return the list. Default is an empty list.
148      */
149     public ReferenceList getSubCategories( )
150     {
151         return new ReferenceList( );
152     }
153 }