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.paybox.web;
35  
36  import java.io.IOException;
37  import java.util.List;
38  
39  import javax.servlet.ServletException;
40  import javax.servlet.http.HttpServlet;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  import fr.paris.lutece.plugins.paybox.PayboxUtil;
45  import fr.paris.lutece.plugins.paybox.service.PayboxLogService;
46  import fr.paris.lutece.plugins.paybox.service.PayboxPlugin;
47  import fr.paris.lutece.plugins.paybox.service.PayboxService;
48  import fr.paris.lutece.portal.service.plugin.PluginService;
49  import fr.paris.lutece.portal.service.spring.SpringContextService;
50  import fr.paris.lutece.portal.service.util.AppLogService;
51  import fr.paris.lutece.portal.service.util.AppPropertiesService;
52  
53  
54  /**
55   * Handles paybox server to server.
56   */
57  public class PayboxServlet extends HttpServlet
58  {
59      /** The Constant serialVersionUID. */
60      private static final long serialVersionUID = 1L;
61  
62      /** The _paybox log service. */
63      private final PayboxLogService _payboxLogService = SpringContextService.getBean( "paybox.payboxLogService" );
64  
65      /*
66       * (non-Javadoc)
67       *
68       * @see
69       * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
70       * , javax.servlet.http.HttpServletResponse)
71       */
72      @Override
73      protected void doGet( final HttpServletRequest req, final HttpServletResponse resp )
74          throws ServletException, IOException
75      {
76          this.doPost( req, resp );
77      }
78  
79      /*
80       * (non-Javadoc)
81       *
82       * @see
83       * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
84       * , javax.servlet.http.HttpServletResponse)
85       */
86      @Override
87      protected void doPost( final HttpServletRequest request, final HttpServletResponse response )
88          throws ServletException, IOException
89      {
90          if ( !AppPropertiesService.getPropertyBoolean( "paybox.check.sign.callback", true )
91                  || PayboxUtil.checkSignature( request.getQueryString( ) ) )
92          {
93              // ici maj du service de logs
94              this._payboxLogService.removeLog( request.getParameter( "reference" ),
95                  PluginService.getPlugin( PayboxPlugin.PLUGIN_NAME ) );
96  
97              this.delagate( request, response );
98          }
99          else
100         {
101             AppLogService.error( "Problème de vérification de la signature." );
102         }
103 
104         response.setStatus( 200 );
105     }
106 
107     /**
108      * Delagate.
109      *
110      * @param request the request
111      * @param response the response
112      */
113     private void delagate( final HttpServletRequest request, final HttpServletResponse response )
114     {
115         // no DI possible...
116         final List<PayboxService> listPayboxServices = SpringContextService.getBeansOfType( PayboxService.class );
117 
118         if ( ( listPayboxServices == null ) || listPayboxServices.isEmpty(  ) )
119         {
120             AppLogService.error( "No PayboxService bean defined. Please check your Spring configuration." );
121         }
122         else if ( listPayboxServices.size(  ) > 1 )
123         {
124             AppLogService.error( "Multiple PayboxService beans defined. Please check your Spring configuration." );
125         }
126         else
127         {
128             final PayboxService payboxService = listPayboxServices.get( 0 );
129             AppLogService.debug( "Using " + payboxService.getClass(  ) + " as PayboxService" );
130 
131             try
132             {
133                 payboxService.handlePayboxReturn( request, response );
134             }
135             catch ( final Exception e )
136             {
137                 AppLogService.error( e );
138             }
139         }
140     }
141 }