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.blog.web;
35
36 import fr.paris.lutece.plugins.blog.business.DocContent;
37 import fr.paris.lutece.plugins.blog.business.DocContentHome;
38 import fr.paris.lutece.portal.service.util.AppLogService;
39 import fr.paris.lutece.portal.service.util.AppPropertiesService;
40
41 import org.apache.commons.lang3.StringUtils;
42
43 import java.io.IOException;
44 import java.io.OutputStream;
45
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 public class BlogFileServlet extends HttpServlet
55 {
56
57
58
59 private static final long serialVersionUID = -3589685443968252550L;
60
61
62 public static final String PARAMETER_ID_FILE = "id_file";
63
64
65 public static final String URL_SERVLET = "servlet/plugins/blog/file";
66 private static final String LOG_UNKNOWN_ID_RESPONSE = "Calling Blogd file servlet with unknown id file : ";
67 private static final String LOG_WRONG_ID_RESPONSE = "Calling Blogd file servlet with wrong format for parameter " + PARAMETER_ID_FILE + " : ";
68 private static final String PROPERTY_MAX_AGE = "blog.fileServlet.maxAge";
69 private static final long DEFAULT_MAX_AGE = 60L * 60 * 24 * 7;
70 private static final long MAX_AGE = AppPropertiesService.getPropertyLong( PROPERTY_MAX_AGE, DEFAULT_MAX_AGE );
71
72
73
74
75
76
77
78
79
80
81
82
83
84 protected void processRequest( HttpServletRequest request, HttpServletResponse httpResponse ) throws ServletException, IOException
85 {
86 String strIdFile = request.getParameter( PARAMETER_ID_FILE );
87 if ( !StringUtils.isEmpty( strIdFile ) && StringUtils.isNumeric( strIdFile ) )
88 {
89 int nIdResponse = Integer.parseInt( strIdFile );
90
91 DocContent docContent = DocContentHome.getDocsContent( nIdResponse );
92
93 if ( docContent == null )
94 {
95 AppLogService.error( LOG_UNKNOWN_ID_RESPONSE + strIdFile );
96 throw new ServletException( LOG_UNKNOWN_ID_RESPONSE + strIdFile );
97 }
98
99 httpResponse.setHeader( "Content-Disposition", "attachment; filename=\"" + docContent.getTextValue( ) + "\";" );
100 httpResponse.setHeader( "Content-type", docContent.getValueContentType( ) );
101 httpResponse.addHeader( "Content-Encoding", "UTF-8" );
102 httpResponse.addHeader( "Cache-Control", "public,max-age=" + MAX_AGE );
103
104 try ( OutputStream os = httpResponse.getOutputStream( ) )
105 {
106 os.write( docContent.getBinaryValue( ) );
107
108
109
110 }
111 catch( IOException e )
112 {
113 AppLogService.error( e.getStackTrace( ), e );
114 }
115 }
116 else
117 {
118 AppLogService.error( LOG_WRONG_ID_RESPONSE + strIdFile );
119 throw new ServletException( );
120 }
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135 @Override
136 protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
137 {
138 processRequest( request, response );
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153 @Override
154 protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
155 {
156 processRequest( request, response );
157 }
158
159
160
161
162
163
164 @Override
165 public String getServletInfo( )
166 {
167 return "Servlet serving file content";
168 }
169
170 }