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.plugins.extend.modules.feedback.web;
35  
36  import fr.paris.lutece.plugins.extend.modules.feedback.business.config.FeedbackExtenderConfig;
37  import fr.paris.lutece.plugins.extend.modules.feedback.service.ExtendFeedbackService;
38  import fr.paris.lutece.plugins.extend.modules.feedback.service.FeedbackCaptchaService;
39  import fr.paris.lutece.plugins.extend.modules.feedback.service.FeedbackPlugin;
40  import fr.paris.lutece.plugins.extend.modules.feedback.service.IExtendFeedbackService;
41  import fr.paris.lutece.plugins.extend.modules.feedback.service.IFeedbackCaptchaService;
42  import fr.paris.lutece.plugins.extend.modules.feedback.service.extender.FeedbackResourceExtender;
43  import fr.paris.lutece.plugins.extend.modules.feedback.util.constants.FeedbackConstants;
44  import fr.paris.lutece.plugins.extend.service.extender.config.IResourceExtenderConfigService;
45  import fr.paris.lutece.portal.service.message.SiteMessage;
46  import fr.paris.lutece.portal.service.message.SiteMessageException;
47  import fr.paris.lutece.portal.service.message.SiteMessageService;
48  import fr.paris.lutece.portal.service.plugin.Plugin;
49  import fr.paris.lutece.portal.service.security.UserNotSignedException;
50  import fr.paris.lutece.portal.service.spring.SpringContextService;
51  import fr.paris.lutece.portal.web.constants.Messages;
52  import fr.paris.lutece.portal.web.xpages.XPage;
53  import fr.paris.lutece.portal.web.xpages.XPageApplication;
54  
55  
56  import javax.servlet.http.HttpServletRequest;
57  
58  import org.apache.commons.lang3.StringUtils;
59  
60  
61  
62  
63  /**
64   *
65   * FeedbackApp
66   *
67   */
68  public class FeedbackApp implements XPageApplication
69  {
70      /**
71  	 * 
72  	 */
73  	private static final long serialVersionUID = 7290027035637324504L;
74  	// TEMPLATES
75      private IFeedbackCaptchaService _feedbackCaptchaService = SpringContextService.getBean( FeedbackCaptchaService.BEAN_SERVICE );
76      private IExtendFeedbackService _extendFeedbackService = SpringContextService.getBean( ExtendFeedbackService.BEAN_SERVICE );
77      private IResourceExtenderConfigService _configService = SpringContextService.getBean( FeedbackConstants.BEAN_CONFIG_SERVICE );
78      
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
84          throws UserNotSignedException, SiteMessageException
85      {
86          // Test the captcha
87          _feedbackCaptchaService.validateCaptcha( request );
88          
89          String strIdExtendableResource = request.getParameter( FeedbackConstants.PARAMETER_ID_EXTENDABLE_RESOURCE );
90          String strExtendableResourceType = request.getParameter( FeedbackConstants.PARAMETER_EXTENDABLE_RESOURCE_TYPE );
91          
92          FeedbackExtenderConfig config = _configService.find( FeedbackResourceExtender.RESOURCE_EXTENDER, strIdExtendableResource, strExtendableResourceType );
93          String strBackUrl = getBackUrl( request );
94          
95          if ( !_extendFeedbackService.isAuthorized( request, config ) )
96          {
97          	throw new UserNotSignedException( );
98          }
99  
100         if ( _extendFeedbackService.doSubmitFeedback( request ) )
101         {            
102             SiteMessageService.setMessage( request, FeedbackConstants.MESSAGE_MESSAGE_SENT,
103             		null, null, null, null, SiteMessage.TYPE_CONFIRMATION, null, strBackUrl );          
104         }
105 
106         SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_STOP );
107 
108         return null;
109     }
110     
111     /**
112      * Get backUrl
113      * @param request
114      * @return
115      */
116     private String getBackUrl ( HttpServletRequest request )
117     {   	
118         String strIdExtendableResource = request.getParameter( FeedbackConstants.PARAMETER_ID_EXTENDABLE_RESOURCE );
119     	String strBackUrl = ( String ) request.getSession( ).getAttribute( FeedbackPlugin.PLUGIN_NAME + FeedbackConstants.PARAMETER_SESSION_BACK_URL + strIdExtendableResource );
120     	
121     	if( StringUtils.isEmpty( strBackUrl ) )
122     	{
123             String strbackUrl = request.getHeader( FeedbackConstants.PARAMETER_REFERER );
124 
125             request.getSession( ).setAttribute( FeedbackPlugin.PLUGIN_NAME + FeedbackConstants.PARAMETER_SESSION_BACK_URL  + strIdExtendableResource , strbackUrl );
126             
127             return strbackUrl;
128     	}
129     	
130     	return strBackUrl;
131     }
132 }