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