MainServlet.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.service.servlet;

  35. import fr.paris.lutece.portal.service.util.AppLogService;

  36. import java.io.IOException;

  37. import javax.servlet.Servlet;
  38. import javax.servlet.ServletConfig;
  39. import javax.servlet.ServletException;
  40. import javax.servlet.ServletRequest;
  41. import javax.servlet.ServletResponse;
  42. import javax.servlet.http.HttpServletRequest;
  43. import javax.servlet.http.HttpServletResponse;

  44. /**
  45.  * MainServlet
  46.  */
  47. public class MainServlet implements Servlet
  48. {
  49.     private ServletConfig _config;

  50.     /**
  51.      * {@inheritDoc}
  52.      */
  53.     @Override
  54.     public void init( ServletConfig config ) throws ServletException
  55.     {
  56.         _config = config;
  57.     }

  58.     /**
  59.      * {@inheritDoc}
  60.      */
  61.     @Override
  62.     public void service( ServletRequest requestServlet, ServletResponse responseServlet ) throws ServletException, IOException
  63.     {
  64.         AppLogService.debug( "MainServlet : service()" );

  65.         HttpServletRequest request = (HttpServletRequest) requestServlet;
  66.         HttpServletResponse response = (HttpServletResponse) responseServlet;

  67.         for ( LuteceServlet servlet : ServletService.getInstance( ).getServlets( ) )
  68.         {
  69.             AppLogService.debug( "PluginServlet : {} - url pattern : {}", servlet.getName( ), servlet.getMappingUrlPattern( ) );

  70.             try
  71.             {
  72.                 // Checks mapping and plugin status
  73.                 if ( matchMapping( servlet, request ) && servlet.getPlugin( ).isInstalled( ) )
  74.                 {
  75.                     servlet.getServlet( ).service( request, response );
  76.                 }
  77.             }
  78.             catch( Exception e )
  79.             {
  80.                 AppLogService.error( "Error execution 'service' method - Servlet {}", servlet.getName( ), e );
  81.             }
  82.         }
  83.     }

  84.     /**
  85.      * {@inheritDoc}
  86.      */
  87.     @Override
  88.     public void destroy( )
  89.     {
  90.         for ( LuteceServlet servlet : ServletService.getInstance( ).getServlets( ) )
  91.         {
  92.             // Catch exception for each servlet to execute all chain
  93.             try
  94.             {
  95.                 // Checks mapping and plugin status
  96.                 if ( servlet.getPlugin( ).isInstalled( ) )
  97.                 {
  98.                     servlet.getServlet( ).destroy( );
  99.                 }
  100.             }
  101.             catch( Exception e )
  102.             {
  103.                 AppLogService.error( "Error execution destroy() method - Servlet {}", servlet.getName( ), e );
  104.             }
  105.         }
  106.     }

  107.     /**
  108.      * Check the mapping of the request with an url pattern
  109.      *
  110.      * @param servlet
  111.      *            The servlet
  112.      * @param request
  113.      *            The request
  114.      * @return True if the request match the url pattern
  115.      */
  116.     private boolean matchMapping( LuteceServlet servlet, HttpServletRequest request )
  117.     {
  118.         return matchUrl( servlet.getMappingUrlPattern( ), request.getServletPath( ) + request.getPathInfo( ) );
  119.     }

  120.     /**
  121.      * Check the mapping of the request with an url pattern according servlet specifications 2.3 rules
  122.      *
  123.      * @param strUrlPattern
  124.      *            The servlet url pattern
  125.      * @param strRequestUrl
  126.      *            The request Url
  127.      * @return True if the request match the url pattern
  128.      *
  129.      *         Algorithm comming from tomcat6
  130.      */
  131.     private boolean matchUrl( String strUrlPattern, String strRequestUrl )
  132.     {
  133.         return strRequestUrl.startsWith( strUrlPattern );
  134.     }

  135.     /**
  136.      * {@inheritDoc}
  137.      */
  138.     @Override
  139.     public ServletConfig getServletConfig( )
  140.     {
  141.         return _config;
  142.     }

  143.     /**
  144.      * {@inheritDoc}
  145.      */
  146.     @Override
  147.     public String getServletInfo( )
  148.     {
  149.         throw new UnsupportedOperationException( "Not supported yet." );
  150.     }
  151. }