InsertServiceJspBean.java

  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. import fr.paris.lutece.portal.service.html.EncodingService;
  36. import fr.paris.lutece.portal.service.template.AppTemplateService;
  37. import fr.paris.lutece.portal.service.util.AppPathService;
  38. import fr.paris.lutece.portal.web.l10n.LocaleService;
  39. import fr.paris.lutece.util.ReferenceList;
  40. import fr.paris.lutece.util.html.HtmlTemplate;
  41. import fr.paris.lutece.util.url.UrlItem;

  42. import org.apache.commons.text.StringEscapeUtils;

  43. import java.io.Serializable;

  44. import java.util.HashMap;

  45. import javax.servlet.http.HttpServletRequest;

  46. /**
  47.  * Base class for InsertServiceJspBean
  48.  */
  49. public abstract class InsertServiceJspBean implements Serializable
  50. {
  51.     private static final long serialVersionUID = -2870769178710689751L;
  52.     private static final String PARAMETER_MODE = "mode";
  53.     private static final String PARAMETER_INPUT = "input";
  54.     private static final String PARAMETER_INSERT = "insert";
  55.     private static final String JSP_DO_INSERT = "jsp/admin/insert/DoInsertIntoElement.jsp";
  56.     private static final String TEMPLATE_LINK = "/admin/insert/insert_link.html";
  57.     private static final String MARK_TEXT = "text";
  58.     private static final String MARK_URL = "url";
  59.     private static final String MARK_TITLE = "title";
  60.     private static final String MARK_TARGET = "target";

  61.     /**
  62.      * Build the Url to insert HTML code into the current rich text editor
  63.      *
  64.      * @param request
  65.      *            The HTTP request
  66.      * @param strInput
  67.      *            The rich text input field
  68.      * @param strInsert
  69.      *            The code to insert
  70.      * @return The Url that will provide the insertion
  71.      */
  72.     protected String insertUrl( HttpServletRequest request, String strInput, String strInsert )
  73.     {
  74.         // No CR is allowed in the insert string
  75.         String strCleanInsert = strInsert.replaceAll( "\n", "" );
  76.         strCleanInsert = strCleanInsert.replaceAll( "\r", "" );

  77.         // Build the url to make the insert
  78.         UrlItem urlDoInsert = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_DO_INSERT );
  79.         urlDoInsert.addParameter( PARAMETER_INPUT, strInput );
  80.         request.getSession( ).setAttribute( InsertServiceSelectorJspBean.SESSION_INSERT, strCleanInsert );
  81.         urlDoInsert.addParameter( PARAMETER_MODE, 1 );

  82.         return urlDoInsert.getUrl( );
  83.     }

  84.     /**
  85.      * Build the Url to insert HTML code into the current rich text editor
  86.      *
  87.      * @param request
  88.      *            The HTTP request
  89.      * @param strInput
  90.      *            The rich text input field
  91.      * @param strInsert
  92.      *            The code to insert
  93.      * @return The Url that will provide the insertion
  94.      */
  95.     protected String insertUrlWithoutEscape( HttpServletRequest request, String strInput, String strInsert )
  96.     {
  97.         String strInsertTmp = EncodingService.encodeUrl( strInsert );

  98.         // Build the url to make the insert
  99.         UrlItem urlDoInsert = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_DO_INSERT );
  100.         urlDoInsert.addParameter( PARAMETER_INPUT, strInput );
  101.         urlDoInsert.addParameter( PARAMETER_INSERT, strInsertTmp );
  102.         urlDoInsert.addParameter( PARAMETER_MODE, 2 );

  103.         return urlDoInsert.getUrl( );
  104.     }

  105.     /**
  106.      * Build an HTML link
  107.      *
  108.      * @param strText
  109.      *            The text of the link
  110.      * @param strUrl
  111.      *            The Url of the link
  112.      * @param strTitle
  113.      *            The title of the link
  114.      * @param strTarget
  115.      *            The target window
  116.      * @return The HTML link
  117.      */
  118.     protected String buildLink( String strText, String strUrl, String strTitle, String strTarget )
  119.     {
  120.         HashMap<String, Object> model = new HashMap<>( );
  121.         model.put( MARK_TEXT, StringEscapeUtils.escapeHtml4( strText ) );
  122.         model.put( MARK_URL, strUrl );
  123.         model.put( MARK_TITLE, StringEscapeUtils.escapeHtml4( strTitle ) );
  124.         model.put( MARK_TARGET, strTarget );

  125.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_LINK, LocaleService.getDefault( ), model );

  126.         return template.getHtml( );
  127.     }

  128.     /**
  129.      * List of supported sub categories that may be used to filter resources.
  130.      *
  131.      * @return the list. Default is an empty list.
  132.      */
  133.     public ReferenceList getSubCategories( )
  134.     {
  135.         return new ReferenceList( );
  136.     }
  137. }