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.newsletter.util;
35
36 import fr.paris.lutece.plugins.newsletter.business.NewsLetterTemplate;
37 import fr.paris.lutece.plugins.newsletter.business.NewsLetterTemplateHome;
38 import fr.paris.lutece.portal.service.html.EncodingService;
39 import fr.paris.lutece.portal.service.i18n.I18nService;
40 import fr.paris.lutece.portal.service.plugin.Plugin;
41 import fr.paris.lutece.portal.service.util.AppPropertiesService;
42 import fr.paris.lutece.util.string.StringUtil;
43 import fr.paris.lutece.util.url.UrlItem;
44
45 import javax.servlet.ServletRequest;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.apache.commons.lang3.StringUtils;
50
51 import java.util.Locale;
52 import java.util.Optional;
53
54
55
56
57 public final class NewsletterUtils
58 {
59
60
61
62 private NewsletterUtils( )
63 {
64 }
65
66
67
68
69
70
71
72
73
74
75 public static String getHtmlTemplatePath( int nTemplateId, Plugin plugin )
76 {
77 NewsLetterTemplate newsletterTemplate = NewsLetterTemplateHome.findByPrimaryKey( nTemplateId, plugin );
78
79 if ( ( newsletterTemplate == null ) || StringUtils.isEmpty( newsletterTemplate.getFileName( ) ) )
80 {
81 return null;
82 }
83
84 String strTemplatePathName = AppPropertiesService.getProperty( NewsLetterConstants.PROPERTY_PATH_FILE_NEWSLETTER_TEMPLATE );
85 strTemplatePathName += NewsLetterConstants.CONSTANT_SLASH;
86 strTemplatePathName += newsletterTemplate.getFileName( );
87
88 return strTemplatePathName;
89 }
90
91
92
93
94
95
96
97
98 public static String convertForJavascript( String strIn )
99 {
100
101 if ( strIn == null )
102 {
103 return StringUtils.EMPTY;
104 }
105
106 String strOut = strIn;
107
108 strOut = StringUtil.substitute( strOut, "\\\\", "\\" );
109
110 strOut = StringUtil.substitute( strOut, "\\\'", "'" );
111
112 strOut = StringUtil.substitute( strOut, "\\\"", "\"" );
113
114 strOut = StringUtil.substitute( strOut, "\\r", "\r\n" );
115
116 strOut = StringUtil.substitute( strOut, "\\n", "\n" );
117
118 return strOut;
119 }
120
121
122
123
124
125
126
127
128 public static String encodeForURL( String strEntry )
129 {
130 return EncodingService.encodeUrl( strEntry );
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public static void addHeaderResponse( HttpServletRequest request, HttpServletResponse response, String strFileName, String strFileExtension )
146 {
147 response.setHeader( "Content-Disposition", "attachment ;filename=\"" + strFileName + "\"" );
148
149 if ( strFileExtension.equals( ".csv" ) )
150 {
151 response.setContentType( "application/csv" );
152 }
153 else
154 {
155 String strMimeType = request.getSession( ).getServletContext( ).getMimeType( strFileName );
156
157 if ( strMimeType != null )
158 {
159 response.setContentType( strMimeType );
160 }
161 else
162 {
163 response.setContentType( "application/octet-stream" );
164 }
165 }
166
167 response.setHeader( "Pragma", "public" );
168 response.setHeader( "Expires", "0" );
169 response.setHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
170 }
171
172
173
174
175
176
177
178
179
180
181
182 public static void addParameters( UrlItem urlItem, String strParameterName, String [ ] values )
183 {
184 for ( String strParameterValue : values )
185 {
186 urlItem.addParameter( strParameterName, strParameterValue );
187 }
188 }
189
190
191
192
193
194
195
196
197 public static String getStringFromStringArray( String [ ] strArrayValues )
198 {
199 if ( strArrayValues != null )
200 {
201 return strArrayValues.length == 0 ? null : strArrayValues [0];
202 }
203 return null;
204 }
205
206
207
208
209
210
211
212
213
214
215 public static String rewriteUrls( String strContent, String strBaseUrl )
216 {
217 HtmlDomDocNewsletterr/util/HtmlDomDocNewsletter.html#HtmlDomDocNewsletter">HtmlDomDocNewsletter doc = new HtmlDomDocNewsletter( strContent, strBaseUrl );
218 doc.convertAllRelativesUrls( HtmlDomDocNewsletter.ELEMENT_IMG );
219 doc.convertAllRelativesUrls( HtmlDomDocNewsletter.ELEMENT_A );
220 doc.convertAllRelativesUrls( HtmlDomDocNewsletter.ELEMENT_FORM );
221 doc.convertAllRelativesUrls( HtmlDomDocNewsletter.ELEMENT_CSS );
222 doc.convertAllRelativesUrls( HtmlDomDocNewsletter.ELEMENT_JAVASCRIPT );
223
224 return doc.getContent( );
225 }
226
227 public static Locale getLocale( ServletRequest request )
228 {
229 return Optional.ofNullable( request ).map( ServletRequest::getLocale ).orElse( I18nService.getDefaultLocale( ) );
230 }
231
232 }