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.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 * This class handle the action of formatting an object E to String with the appropriate MediaType.
59 *
60 * @param <E>
61 * the <i>real</i> object class
62 *
63 */
64 public abstract class AbstractWriter<E> implements MessageBodyWriter<List<E>>
65 {
66 // ERROR CODE
67 private static final String ERROR_CODE = "1";
68
69 // MESSAGES
70 private static final String MESSAGE_NO_RESOURCE = "No resource";
71
72 // PROPERTIES
73 private static final String PROPERTY_WRITER_ENCODING = "rest.writer.encoding";
74
75 // VARIABLES
76 private Map<String, IFormatter<E>> _mapFormatters;
77
78 /**
79 * Set the formatters
80 *
81 * @param mapFormatters
82 * the formatters
83 */
84 public void setFormatters( Map<String, IFormatter<E>> mapFormatters )
85 {
86 _mapFormatters = mapFormatters;
87 }
88
89 /**
90 * {@inheritDoc}
91 */
92 public long getSize( List<E> resource, Class<?> type, Type genericType, Annotation [ ] annotations, MediaType mediaType )
93 {
94 return -1;
95 }
96
97 /**
98 * {@inheritDoc}
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 // Should not happen
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 // Should not happen
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 // Not well configured
156 throw new WebApplicationException( Status.INTERNAL_SERVER_ERROR );
157 }
158 }
159 }