View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.plugins.jsr168.pluto.core;
35  
36  import fr.paris.lutece.plugins.jsr168.pluto.LutecePlutoConstant;
37  import fr.paris.lutece.portal.web.constants.Parameters;
38  
39  import org.apache.pluto.om.common.ObjectID;
40  import org.apache.pluto.om.window.PortletWindow;
41  import org.apache.pluto.portalImpl.om.window.impl.PortletWindowImpl;
42  import org.apache.pluto.portalImpl.services.config.Config;
43  import org.apache.pluto.services.information.PortletURLProvider;
44  
45  import java.util.Collections;
46  import java.util.Map;
47  
48  import javax.portlet.PortletMode;
49  import javax.portlet.WindowState;
50  
51  import javax.servlet.http.HttpServletRequest;
52  import javax.servlet.http.HttpServletResponse;
53  
54  
55  /**
56   * Build an URL for a portlet action (action or render)<br>
57   *
58   * Portlet right's about portlet mode and window state isn't controled.
59   */
60  public class PortletURLProviderImpl implements PortletURLProvider
61  {
62      private static final String _hostNameHTTP;
63      private static final String _hostNameHTTPS;
64  
65      static
66      {
67          final String hostName = Config.getParameters(  )
68                                        .getString( LutecePlutoConstant.CONFIG_SERVICES_PARAM_HOST_NAME,
69                  LutecePlutoConstant.CONFIG_SERVICES_PARAM_HOST_NAME_DEFAULT );
70          final String hostPortHTTP = Config.getParameters(  )
71                                            .getString( LutecePlutoConstant.CONFIG_SERVICES_PARAM_HOST_PORT_HTTP,
72                  LutecePlutoConstant.CONFIG_SERVICES_PARAM_HOST_PORT_HTTP_DEFAULT );
73          final String hostPortHTTPS = Config.getParameters(  )
74                                             .getString( LutecePlutoConstant.CONFIG_SERVICES_PARAM_HOST_PORT_HTTPS,
75                  LutecePlutoConstant.CONFIG_SERVICES_PARAM_HOST_PORT_HTTPS_DEFAULT );
76  
77          final StringBuffer hostHTTP = new StringBuffer( "http://" );
78          hostHTTP.append( hostName );
79  
80          if ( !"80".equals( hostPortHTTP ) )
81          {
82              hostHTTP.append( ':' );
83              hostHTTP.append( hostPortHTTP );
84          }
85  
86          _hostNameHTTP = hostHTTP.toString(  );
87  
88          final StringBuffer hostHTTPS = new StringBuffer( "https://" );
89          hostHTTPS.append( hostName );
90  
91          if ( !"443".equals( hostPortHTTPS ) )
92          {
93              hostHTTPS.append( ':' );
94              hostHTTPS.append( hostPortHTTPS );
95          }
96  
97          _hostNameHTTPS = hostHTTPS.toString(  );
98      }
99  
100     private final HttpServletRequest _request;
101     private final DynamicInformationProviderImpl _provider;
102     private final PortletWindowImpl _portletWindow;
103 
104     /**
105      * Mode qui doit apparaître dans l'URL de la portlet.
106      */
107     private PortletMode _mode;
108 
109     /**
110      * Etat qui doit apparaître dans l'URL de la portlet.
111      */
112     private WindowState _state;
113 
114     /**
115      * Indique que l'URL est de type action (sinon elle est de type
116      * render).
117      */
118     private boolean _bAction;
119 
120     /**
121      * Indique que l'URL doit être de type sécurisé (HTTPS par exemple).
122      */
123     private boolean _secure;
124     private boolean _clearParameters;
125     private Map _parameters;
126 
127     /**
128      * Initialize a portlet URL provider instance
129      *
130      * @param request Current HTTP request
131      * @param provider Dynamic information provider (for current request)
132      * @param portletWindow Current portlet window
133      */
134     public PortletURLProviderImpl( HttpServletRequest request, DynamicInformationProviderImpl provider,
135         PortletWindow portletWindow )
136     {
137         _request = request;
138         _provider = provider;
139         _portletWindow = (PortletWindowImpl) portletWindow;
140         _bAction = false;
141         _secure = false;
142     }
143 
144     /**
145      * @see org.apache.pluto.services.information.PortletURLProvider#setPortletMode(javax.portlet.PortletMode)
146      */
147     public void setPortletMode( PortletMode mode )
148     {
149         _mode = mode;
150     }
151 
152     /**
153      * @see org.apache.pluto.services.information.PortletURLProvider#setWindowState(javax.portlet.WindowState)
154      */
155     public void setWindowState( WindowState state )
156     {
157         _state = state;
158     }
159 
160     /**
161      * @see org.apache.pluto.services.information.PortletURLProvider#setAction()
162      */
163     public void setAction(  )
164     {
165         _bAction = true;
166     }
167 
168     /**
169      * @see org.apache.pluto.services.information.PortletURLProvider#setSecure()
170      */
171     public void setSecure(  )
172     {
173         _secure = true;
174     }
175 
176     /**
177      * @see org.apache.pluto.services.information.PortletURLProvider#clearParameters()
178      */
179     public void clearParameters(  )
180     {
181         _clearParameters = true;
182     }
183 
184     /**
185          * @see org.apache.pluto.services.information.PortletURLProvider#setParameters(java.util.Map)
186          */
187     public void setParameters( Map parameters )
188     {
189         _parameters = parameters;
190     }
191 
192     /**
193          * @see java.lang.Object#toString()
194          */
195     public String toString(  )
196     {
197         StringBuffer urlBuf = new StringBuffer(  );
198 
199         urlBuf.append( _secure ? _hostNameHTTPS : _hostNameHTTP );
200         urlBuf.append( _request.getContextPath(  ) );
201         urlBuf.append( LutecePlutoConstant.URL_JSR168_ACTION );
202 
203         // Vestige du portail de Pluto: les parametres sont
204         // conservés dans la session (et plus dans le PathInfo),
205         // l'url est entièrement reconstruite.
206         /*
207         if ( _clearParameters )
208         {
209         }
210         */
211         String params = PortalURL.buildParams( _portletWindow.getId(  ), _bAction, _mode, _state, _parameters );
212 
213         if ( ( params != null ) && ( params.length(  ) > 0 ) )
214         {
215             urlBuf.append( params );
216         }
217 
218         // /////////////////////////////////////////////////////
219         // 
220         //    Lutece parameter's
221         // 
222         // /////////////////////////////////////////////////////        
223         if ( _request.getParameter( Parameters.PAGE_ID ) != null )
224         {
225             urlBuf.append( '&' );
226             urlBuf.append( Parameters.PAGE_ID );
227             urlBuf.append( '=' );
228             urlBuf.append( _request.getParameter( Parameters.PAGE_ID ) );
229         }
230 
231         PortalEnvironment environment = PortalEnvironment.getPortalEnvironment( _request );
232 
233         return environment.getResponse(  ).encodeURL( urlBuf.toString(  ) );
234     }
235 
236     /**
237      * Return the URL base for all URL construction (protocol and server name,
238      * for example <code>&quot;http://localhost/lutece&quot;</code>)
239      *
240      * @return the URL base for all URL construction
241      */
242     static String getBaseURLexcludeContext(  )
243     {
244         return _hostNameHTTP;
245     }
246 
247     /**
248      * Return an URL string for redirect user to Lutece portal (used after action request for
249      * process render)
250      *
251      * @param request Current HTTP request
252      * @param response Current HTTP response
253      * @return an URL string for redirect user to Lutece portal
254      */
255     public static String getRedirectPortalURL( HttpServletRequest request, final HttpServletResponse response )
256     {
257         StringBuffer urlBuf = new StringBuffer(  );
258 
259         urlBuf.append( _hostNameHTTP );
260         urlBuf.append( request.getContextPath(  ) );
261         urlBuf.append( LutecePlutoConstant.URL_LUTECE_PORTAL );
262 
263         // /////////////////////////////////////////////////////
264         // 
265         //    Lutece parameter's
266         // 
267         // /////////////////////////////////////////////////////        
268         String pageIdParam = request.getParameter( Parameters.PAGE_ID );
269 
270         if ( pageIdParam != null )
271         {
272             urlBuf.append( '&' );
273             urlBuf.append( Parameters.PAGE_ID );
274             urlBuf.append( '=' );
275             urlBuf.append( pageIdParam );
276         }
277 
278         return response.encodeURL( urlBuf.toString(  ) );
279     }
280 
281     /**
282      * Return an URL string making a render request
283      *
284      * @param request Current HTTP request
285      * @param response Current HTTP response
286      * @param mode Portlet mode for URL
287      * @param state Window state for URL
288      * @param portletID Portlet ID
289      * @return an URL string making a render request
290      */
291     public static String getRenderURL( HttpServletRequest request, HttpServletResponse response, PortletMode mode,
292         WindowState state, ObjectID portletID )
293     {
294         StringBuffer urlBuf = new StringBuffer(  );
295 
296         urlBuf.append( _hostNameHTTP );
297         urlBuf.append( request.getContextPath(  ) );
298         urlBuf.append( LutecePlutoConstant.URL_JSR168_ACTION );
299 
300         String params = PortalURL.buildParams( portletID, false, mode, state, Collections.EMPTY_MAP );
301 
302         if ( ( params != null ) && ( params.length(  ) > 0 ) )
303         {
304             urlBuf.append( params );
305         }
306 
307         // /////////////////////////////////////////////////////
308         // 
309         //    Parametre propre à Lutece
310         // 
311         // /////////////////////////////////////////////////////
312         String pageIdParam = request.getParameter( Parameters.PAGE_ID );
313 
314         if ( pageIdParam != null )
315         {
316             urlBuf.append( '&' );
317             urlBuf.append( Parameters.PAGE_ID );
318             urlBuf.append( '=' );
319             urlBuf.append( pageIdParam );
320         }
321 
322         return response.encodeURL( urlBuf.toString(  ) );
323     }
324 }