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.writers;
35
36 import fr.paris.lutece.plugins.rest.service.formatters.IFormatter;
37 import fr.paris.lutece.portal.service.util.AppPropertiesService;
38
39 import org.apache.commons.lang3.StringUtils;
40
41 import java.io.IOException;
42 import java.io.OutputStream;
43
44 import java.lang.annotation.Annotation;
45 import java.lang.reflect.Type;
46
47 import java.util.List;
48 import java.util.Map;
49
50 import javax.ws.rs.WebApplicationException;
51 import javax.ws.rs.core.MediaType;
52 import javax.ws.rs.core.MultivaluedMap;
53 import javax.ws.rs.core.Response.Status;
54 import javax.ws.rs.ext.MessageBodyWriter;
55
56
57
58
59
60
61
62
63
64 public abstract class AbstractWriter<E> implements MessageBodyWriter<List<E>>
65 {
66
67 private static final String ERROR_CODE = "1";
68
69
70 private static final String MESSAGE_NO_RESOURCE = "No resource";
71
72
73 private static final String PROPERTY_WRITER_ENCODING = "rest.writer.encoding";
74
75
76 private Map<String, IFormatter<E>> _mapFormatters;
77
78
79
80
81
82
83
84 public void setFormatters( Map<String, IFormatter<E>> mapFormatters )
85 {
86 _mapFormatters = mapFormatters;
87 }
88
89
90
91
92 public long getSize( List<E> resource, Class<?> type, Type genericType, Annotation [ ] annotations, MediaType mediaType )
93 {
94 return -1;
95 }
96
97
98
99
100 public void writeTo( List<E> listResources, Class<?> type, Type genericType, Annotation [ ] annotations, MediaType mediaType,
101 MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream ) throws IOException, WebApplicationException
102 {
103 if ( _mapFormatters != null )
104 {
105 String strContent = StringUtils.EMPTY;
106 IFormatter<E> formatter = _mapFormatters.get( mediaType.toString( ) );
107
108 if ( formatter == null )
109 {
110
111 throw new WebApplicationException( Status.UNSUPPORTED_MEDIA_TYPE );
112 }
113
114 if ( listResources != null )
115 {
116 if ( listResources.size( ) == 1 )
117 {
118 E resource = listResources.get( 0 );
119
120 if ( resource != null )
121 {
122 strContent = formatter.format( resource );
123 }
124 else
125 {
126 strContent = formatter.formatError( ERROR_CODE, MESSAGE_NO_RESOURCE );
127 }
128 }
129 else
130 if ( listResources.size( ) > 1 )
131 {
132 strContent = formatter.format( listResources );
133 }
134 else
135 {
136 strContent = formatter.formatError( ERROR_CODE, MESSAGE_NO_RESOURCE );
137 }
138 }
139 else
140 {
141 strContent = formatter.formatError( ERROR_CODE, MESSAGE_NO_RESOURCE );
142 }
143
144 if ( StringUtils.isBlank( strContent ) )
145 {
146
147 throw new WebApplicationException( Status.UNSUPPORTED_MEDIA_TYPE );
148 }
149
150 String strEncoding = AppPropertiesService.getProperty( PROPERTY_WRITER_ENCODING );
151 entityStream.write( strContent.getBytes( strEncoding ) );
152 }
153 else
154 {
155
156 throw new WebApplicationException( Status.INTERNAL_SERVER_ERROR );
157 }
158 }
159 }