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.portal.service.image;
35
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.io.OutputStream;
40
41 import javax.servlet.ServletContext;
42 import javax.servlet.ServletException;
43 import javax.servlet.http.HttpServlet;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46
47 import fr.paris.lutece.portal.service.util.AppLogService;
48 import fr.paris.lutece.portal.service.util.AppPathService;
49 import fr.paris.lutece.portal.service.util.AppPropertiesService;
50 import fr.paris.lutece.portal.web.LocalVariables;
51
52
53
54
55 public class ImageServlet extends HttpServlet
56 {
57 private static final long serialVersionUID = -5713203328367191908L;
58 private static final String ERROR_MSG = "ImageServlet error : {}";
59 public static final String PARAMETER_RESOURCE_TYPE = "resource_type";
60 public static final String PARAMETER_ID = "id";
61 private static final String PROPERTY_PATH_IMAGES = "path.images.root";
62 private static final String PROPERTY_IMAGE_PAGE_DEFAULT = "image.page.default";
63
64
65
66
67
68
69
70
71
72 protected void processRequest( HttpServletRequest request, HttpServletResponse response )
73 {
74 String strResourceId = request.getParameter( PARAMETER_ID );
75 String strResourceTypeId = request.getParameter( PARAMETER_RESOURCE_TYPE );
76
77
78
79
80
81 LocalVariables.setLocal( getServletConfig( ), request, response );
82
83 try
84 {
85 ImageResource image;
86
87 if ( strResourceId != null )
88 {
89 int nResourceId = Integer.parseInt( strResourceId );
90 image = ImageResourceManager.getImageResource( strResourceTypeId, nResourceId );
91
92
93 if ( getImageExist( image ) )
94 {
95 response.setContentType( image.getMimeType( ) );
96 try ( OutputStream out = response.getOutputStream( ) )
97 {
98 out.write( image.getImage( ) );
99 }
100 catch( IOException ex )
101 {
102 AppLogService.error( ERROR_MSG, ex.getMessage( ), ex );
103 }
104 }
105 else
106 {
107 ServletContext sc = getServletContext( );
108 String strImageUrl = AppPathService.getAbsolutePathFromRelativePath(
109 AppPropertiesService.getProperty( PROPERTY_PATH_IMAGES ) + "/" + AppPropertiesService.getProperty( PROPERTY_IMAGE_PAGE_DEFAULT ) );
110 response.setContentType( sc.getMimeType( strImageUrl ) );
111
112 File file = new File( strImageUrl );
113 response.setContentLength( (int) file.length( ) );
114
115 try ( FileInputStream in = new FileInputStream( file ) ; OutputStream out = response.getOutputStream( ) )
116 {
117
118 byte [ ] buf = new byte [ 1024];
119 int count;
120 while ( ( count = in.read( buf ) ) >= 0 )
121 {
122 out.write( buf, 0, count );
123 }
124
125 }
126 catch( IOException ex )
127 {
128 AppLogService.error( ERROR_MSG, ex.getMessage( ), ex );
129 }
130 }
131 }
132 }
133 finally
134 {
135 LocalVariables.setLocal( null, null, null );
136 }
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151 @Override
152 protected void doGet( HttpServletRequest request, HttpServletResponse response )
153 {
154 processRequest( request, response );
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169 @Override
170 protected void doPost( HttpServletRequest request, HttpServletResponse response )
171 {
172 processRequest( request, response );
173 }
174
175
176
177
178
179
180 @Override
181 public String getServletInfo( )
182 {
183 return "Servlet serving images content";
184 }
185
186
187
188
189
190
191
192
193
194 private boolean getImageExist( ImageResource image )
195 {
196 if ( image != null && image.getImage( ) != null )
197 {
198 int nImageLength = image.getImage( ).length;
199
200 if ( nImageLength >= 1 )
201 {
202 return true;
203 }
204 }
205
206 return false;
207 }
208 }