1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
68
69
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
85 context.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, getContext( ) );
86 }
87
88 super.init( filterConfig );
89 }
90
91
92
93
94
95
96 private ConfigurableApplicationContext getContext( )
97 {
98 return (ConfigurableApplicationContext) SpringContextService.getContext( );
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
136
137
138
139
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 }