View Javadoc
1   /*
2    * Copyright (c) 2002-2024, 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.plugins.rest.service;
35  
36  import fr.paris.lutece.plugins.rest.service.mediatype.MediaTypeMapping;
37  import fr.paris.lutece.plugins.rest.service.mediatype.RestMediaTypes;
38  import fr.paris.lutece.portal.service.spring.SpringContextService;
39  import fr.paris.lutece.util.signrequest.RequestAuthenticator;
40  
41  import org.apache.commons.lang3.StringUtils;
42  
43  import org.apache.log4j.Level;
44  import org.apache.log4j.Logger;
45  
46  import org.glassfish.jersey.server.ResourceConfig;
47  import org.glassfish.jersey.servlet.ServletContainer;
48  
49  import org.springframework.context.ConfigurableApplicationContext;
50  import org.springframework.web.context.support.WebApplicationContextUtils;
51  import org.springframework.web.context.WebApplicationContext;
52  
53  import java.io.IOException;
54  
55  import java.util.List;
56  import java.util.Map;
57  
58  import javax.servlet.FilterChain;
59  import javax.servlet.FilterConfig;
60  import javax.servlet.ServletContext;
61  import javax.servlet.ServletException;
62  import javax.servlet.http.HttpServletRequest;
63  import javax.servlet.http.HttpServletResponse;
64  
65  /**
66   *
67   * LuteceJerseySpringServlet : using {@link SpringContextService#getContext()} as context.
68   * 
69   * @see ServletContainer
70   */
71  public class LuteceJerseySpringServlet extends ServletContainer
72  {
73      private static final long serialVersionUID = 5686655395749077671L;
74      private static final String BEAN_REQUEST_AUTHENTICATOR = "rest.requestAuthenticator";
75  
76      static final Logger LOGGER = Logger.getLogger( RestConstants.REST_LOGGER );
77  
78      @Override
79      public void init( FilterConfig filterConfig ) throws ServletException
80      {
81          ServletContext context = filterConfig.getServletContext( );
82          if ( WebApplicationContextUtils.getWebApplicationContext( context ) == null )
83          {
84              // If we are not using spring's context loader, register this property needed by the jersey spring integration
85              context.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, getContext( ) );
86          }
87  
88          super.init( filterConfig );
89      }
90  
91      /**
92       * Gets the lutece spring context
93       * 
94       * @return the spring context
95       */
96      private ConfigurableApplicationContext getContext( )
97      {
98          return (ConfigurableApplicationContext) SpringContextService.getContext( );
99      }
100 
101     /**
102      * Checks if the request is authenticated. Sets {@link HttpServletResponse#SC_UNAUTHORIZED} if not, calls
103      * {@link ServletContainer#doFilter(HttpServletRequest, HttpServletResponse, FilterChain)} otherwise.
104      * 
105      * @param request
106      *            the HTTP request
107      * @param response
108      *            the response
109      * @param chain
110      *            the filter chain
111      * @throws IOException
112      *             exception if I/O error
113      * @throws ServletException
114      *             exception if servlet error
115      */
116     @Override
117     public void doFilter( HttpServletRequest request, HttpServletResponse response, FilterChain chain ) throws IOException, ServletException
118     {
119         if ( checkRequestAuthentification( request ) )
120         {
121             if ( LOGGER.isDebugEnabled( ) )
122             {
123                 LOGGER.debug( "LuteceJerseySpringServlet processing request : " + request.getMethod( ) + " " + request.getContextPath( )
124                         + request.getServletPath( ) );
125             }
126             super.doFilter( request, response, chain );
127         }
128         else
129         {
130             response.setStatus( HttpServletResponse.SC_UNAUTHORIZED );
131         }
132     }
133 
134     /**
135      * Checks if the request is authenticated.
136      * 
137      * @param request
138      *            the request
139      * @return <code>true</code> if the request is authenticated, <code>false</code> otherwise.
140      */
141     private boolean checkRequestAuthentification( HttpServletRequest request )
142     {
143         RequestAuthenticator ra = (RequestAuthenticator) SpringContextService.getBean( BEAN_REQUEST_AUTHENTICATOR );
144 
145         return ra.isRequestAuthenticated( request );
146     }
147 }