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.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
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
68 register( JacksonFeature.withoutExceptionMappers( ) );
69 register( new UncaughtThrowableMapper( ) );
70 register( new UncaughtJerseyExceptionMapper( ) );
71 }
72
73
74
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
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
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
128
129 LOGGER.error( uoe.getMessage( ) + ". Won't support extension mapping (.json, .xml, .atom)", uoe );
130 }
131
132
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
150 }
151 }
152 }