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.mapper.UncaughtJerseyExceptionMapper;
37  import fr.paris.lutece.plugins.rest.service.mapper.UncaughtThrowableMapper;
38  import fr.paris.lutece.plugins.rest.service.mediatype.MediaTypeMapping;
39  import fr.paris.lutece.plugins.rest.service.mediatype.RestMediaTypes;
40  import fr.paris.lutece.portal.service.spring.SpringContextService;
41  import fr.paris.lutece.portal.service.util.AppPropertiesService;
42  import org.apache.commons.collections4.CollectionUtils;
43  import org.apache.commons.lang3.StringUtils;
44  import org.apache.log4j.Level;
45  import org.glassfish.jersey.jackson.JacksonFeature;
46  import org.glassfish.jersey.server.ResourceConfig;
47  import org.glassfish.jersey.server.ServerProperties;
48  
49  import javax.ws.rs.Path;
50  import javax.ws.rs.core.MediaType;
51  import javax.ws.rs.ext.Provider;
52  import java.util.HashMap;
53  import java.util.List;
54  import java.util.Map;
55  
56  import static fr.paris.lutece.plugins.rest.service.LuteceJerseySpringServlet.LOGGER;
57  
58  public class LuteceApplicationResourceConfig extends ResourceConfig
59  {
60      // PROPERTIES
61      private static final String GENERIC_EXCEPTION_MAPPER = "rest.generic.exception.mapper";
62  
63      public LuteceApplicationResourceConfig( )
64      {
65          if ( AppPropertiesService.getPropertyBoolean( GENERIC_EXCEPTION_MAPPER, true ) )
66          {
67              // Registering JacksonFeature without its built-in ExceptionMapper so that we can register our own generic one
68              register( JacksonFeature.withoutExceptionMappers( ) );
69              register( new UncaughtThrowableMapper( ) );
70              register( new UncaughtJerseyExceptionMapper( ) );
71          }
72  
73          // Automatically register all beans with @Path annotation because
74          // this is was the previous versions of plugin-rest did
75          Map<String, Object> providers = SpringContextService.getContext( ).getBeansWithAnnotation( Provider.class );
76          for ( Object o : providers.values( ) )
77          {
78              register( o.getClass( ) );
79          }
80  
81          Map<String, Object> pathes = SpringContextService.getContext( ).getBeansWithAnnotation( Path.class );
82          for ( Object o : pathes.values( ) )
83          {
84              register( o.getClass( ) );
85          }
86  
87          try
88          {
89              try
90              {
91                  Map<String, MediaType> mapExtensionToMediaType = new HashMap<>( );
92  
93                  // map default ".extension" to MediaType
94                  mapExtensionToMediaType.put( "atom", MediaType.APPLICATION_ATOM_XML_TYPE );
95                  mapExtensionToMediaType.put( "xml", MediaType.APPLICATION_XML_TYPE );
96                  mapExtensionToMediaType.put( "json", MediaType.APPLICATION_JSON_TYPE );
97                  mapExtensionToMediaType.put( "kml", RestMediaTypes.APPLICATION_KML_TYPE );
98  
99                  // add specific-plugin-provided extensions
100                 List<MediaTypeMapping> listMappings = SpringContextService.getBeansOfType( MediaTypeMapping.class );
101 
102                 if ( CollectionUtils.isNotEmpty( listMappings ) )
103                 {
104                     for ( MediaTypeMapping mapping : listMappings )
105                     {
106                         String strExtension = mapping.getExtension( );
107                         MediaType mediaType = mapping.getMediaType( );
108 
109                         if ( StringUtils.isNotBlank( strExtension ) && ( mediaType != null ) )
110                         {
111                             mapExtensionToMediaType.put( strExtension, mediaType );
112                         }
113                         else
114                         {
115                             LOGGER.error( "Can't add media type mapping for extension : " + strExtension + ", mediatype : " + mediaType
116                                     + ". Please check your context configuration." );
117                         }
118                     }
119                 }
120 
121                 property( ServerProperties.MEDIA_TYPE_MAPPINGS, mapExtensionToMediaType );
122 
123             }
124 
125             catch( UnsupportedOperationException uoe )
126             {
127                 // In jersey 1.x, this might have been an immutable map.
128                 // In jersey 2.x, I don't know if this is useful
129                 LOGGER.error( uoe.getMessage( ) + ". Won't support extension mapping (.json, .xml, .atom)", uoe );
130             }
131 
132             // log services
133             if ( LOGGER.isDebugEnabled( ) )
134             {
135                 LOGGER.debug( "Listing registered services and providers" );
136 
137                 for ( Class<?> clazz : getClasses( ) )
138                 {
139                     LOGGER.debug( clazz );
140                 }
141 
142                 LOGGER.debug( "End of listing" );
143             }
144         }
145         catch( RuntimeException e )
146         {
147             LOGGER.log( Level.ERROR, "REST services won't be available. Please check your configuration or enable at least on rest module." );
148             LOGGER.log( Level.ERROR, "LuteceJerseySpringServlet : Exception occurred when intialization", e );
149             // throw e;
150         }
151     }
152 }