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