1 | /** |
---|
2 | * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. |
---|
3 | * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior |
---|
4 | * |
---|
5 | * This program is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU Affero General Public License as |
---|
7 | * published by the Free Software Foundation, either version 3 of the |
---|
8 | * License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU Affero General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Affero General Public License |
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | |
---|
19 | /******************************************************************************\ |
---|
20 | * |
---|
21 | * This product was developed by |
---|
22 | * |
---|
23 | * SERVIÃO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), |
---|
24 | * |
---|
25 | * a government company established under Brazilian law (5.615/70), |
---|
26 | * at Department of Development of Porto Alegre. |
---|
27 | * |
---|
28 | \******************************************************************************/ |
---|
29 | |
---|
30 | package serpro.mailarchiver.util; |
---|
31 | |
---|
32 | import java.io.IOException; |
---|
33 | import java.net.URLEncoder; |
---|
34 | import java.util.regex.Matcher; |
---|
35 | |
---|
36 | import javax.servlet.http.HttpServletRequest; |
---|
37 | import javax.servlet.http.HttpServletResponse; |
---|
38 | |
---|
39 | import org.eclipse.jetty.rewrite.handler.RegexRule; |
---|
40 | import org.eclipse.jetty.rewrite.handler.Rule; |
---|
41 | import org.eclipse.jetty.server.Request; |
---|
42 | |
---|
43 | import org.springframework.beans.factory.annotation.Autowired; |
---|
44 | import org.springframework.beans.factory.annotation.Configurable; |
---|
45 | |
---|
46 | import serpro.mailarchiver.domain.metaarchive.BinaryBody; |
---|
47 | import serpro.mailarchiver.domain.metaarchive.Message; |
---|
48 | import serpro.mailarchiver.domain.metaarchive.SingleBody; |
---|
49 | import serpro.mailarchiver.service.find.FBinaryBody; |
---|
50 | import serpro.mailarchiver.service.find.FMessage; |
---|
51 | import serpro.mailarchiver.service.find.FTextBody; |
---|
52 | import serpro.mailarchiver.util.transaction.WithReadWriteTx; |
---|
53 | |
---|
54 | @Configurable |
---|
55 | public class TempPublishRule |
---|
56 | extends RegexRule |
---|
57 | implements Rule.ApplyURI |
---|
58 | { |
---|
59 | private static final Logger log = Logger.getLocalLogger(); |
---|
60 | |
---|
61 | @Autowired |
---|
62 | private FMessage findMessage; |
---|
63 | |
---|
64 | @Autowired |
---|
65 | private FBinaryBody findBinaryBody; |
---|
66 | |
---|
67 | @Autowired |
---|
68 | private FTextBody findTextBody; |
---|
69 | |
---|
70 | public TempPublishRule() { |
---|
71 | |
---|
72 | _handling = false; |
---|
73 | _terminating = false; |
---|
74 | |
---|
75 | setRegex("^/(download/((parts_)|(mails_))?)?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(\\.(zip|jar|tar(\\.(bz2|gz))?|tb2|tbz|tgz)|(/thumb/(\\d{1,3}))?(/(.*))?)$"); |
---|
76 | //group --> 1 23 4 5 6 7 8 9 1 1 1 1 |
---|
77 | // 0 1 2 3 |
---|
78 | } |
---|
79 | |
---|
80 | @WithReadWriteTx |
---|
81 | @Override |
---|
82 | public String apply( |
---|
83 | String oldTarget, |
---|
84 | HttpServletRequest request, |
---|
85 | HttpServletResponse response, |
---|
86 | Matcher matcher) { |
---|
87 | |
---|
88 | // for(int i = 0; i < 14; i++) { |
---|
89 | // if(matcher.group(i) != null) { |
---|
90 | // System.out.println("group(" + i + "): >>" + matcher.group(i) + "<<"); |
---|
91 | // } |
---|
92 | // } |
---|
93 | |
---|
94 | String newTarget = oldTarget; |
---|
95 | |
---|
96 | String id = matcher.group(5); |
---|
97 | String format = matcher.group(7); |
---|
98 | String thumbSize = matcher.group(11); |
---|
99 | String fileName = matcher.group(13); |
---|
100 | |
---|
101 | boolean download = (matcher.group(1) != null); |
---|
102 | boolean downloadParts = (matcher.group(3) != null) && (format != null); |
---|
103 | boolean downloadMails = (matcher.group(4) != null) && (format != null); |
---|
104 | boolean thumbnail = (matcher.group(10) != null); |
---|
105 | |
---|
106 | if(downloadMails) { |
---|
107 | |
---|
108 | response.setHeader("Content-Type", "application/download"); |
---|
109 | response.setHeader("Content-Disposition", "attachment; filename=mails_" + id + "." + format); |
---|
110 | |
---|
111 | newTarget = newTarget.substring(9); |
---|
112 | |
---|
113 | log.debug("Target rewrited to {%s}", newTarget); |
---|
114 | } |
---|
115 | else if(downloadParts) { |
---|
116 | |
---|
117 | Message message = findMessage.byId(id); |
---|
118 | |
---|
119 | if(message != null) { |
---|
120 | |
---|
121 | try { |
---|
122 | message.publishPartsZip(format); |
---|
123 | |
---|
124 | response.setHeader("Content-Type", "application/download"); |
---|
125 | response.setHeader("Content-Disposition", "attachment; filename=" + message.getPartsZipFileName(format)); |
---|
126 | |
---|
127 | newTarget = newTarget.substring(9); |
---|
128 | |
---|
129 | log.debug("Target rewrited to {%s}", newTarget); |
---|
130 | } |
---|
131 | catch(IOException ex) { |
---|
132 | log.error(ex, "Temp publish rule"); |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | else { |
---|
137 | |
---|
138 | BinaryBody binaryBody = findBinaryBody.byId(id); |
---|
139 | SingleBody singleBody = (binaryBody != null) ? binaryBody : findTextBody.byId(id); |
---|
140 | |
---|
141 | if(singleBody != null) { |
---|
142 | |
---|
143 | boolean redirect = (fileName == null) || ( ! fileName.equals(singleBody.getFileName())); |
---|
144 | |
---|
145 | try { |
---|
146 | |
---|
147 | if(thumbnail) { |
---|
148 | binaryBody.publishThumbnail(Integer.parseInt(thumbSize)); |
---|
149 | } |
---|
150 | else { |
---|
151 | singleBody.publish(); |
---|
152 | } |
---|
153 | |
---|
154 | if(redirect) { |
---|
155 | |
---|
156 | newTarget = "/" |
---|
157 | + (download ? "download/" : "") |
---|
158 | + id + "/" |
---|
159 | + (thumbnail ? "thumb/" + thumbSize + "/" : "") |
---|
160 | + URLEncoder.encode(singleBody.getFileName(), "UTF-8").replace("+", "%20"); |
---|
161 | |
---|
162 | String contextPath = request.getContextPath(); |
---|
163 | String location = response.encodeRedirectURL(contextPath + newTarget); |
---|
164 | |
---|
165 | response.sendRedirect(location); |
---|
166 | |
---|
167 | log.debug("Target redirected to {%s}", newTarget); |
---|
168 | } |
---|
169 | else if(download) { |
---|
170 | |
---|
171 | response.setHeader("Content-Type", "application/download"); |
---|
172 | response.setHeader("Content-Disposition", "attachment; filename=" + singleBody.getFileName()); |
---|
173 | response.setHeader("Content-Length", "" + singleBody.getSize()); |
---|
174 | |
---|
175 | newTarget = newTarget.substring(9); |
---|
176 | |
---|
177 | log.debug("Target rewrited to {%s}", newTarget); |
---|
178 | } |
---|
179 | } |
---|
180 | catch(IOException ex) { |
---|
181 | log.error(ex, "Temp publish rule"); |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | return newTarget; |
---|
187 | } |
---|
188 | |
---|
189 | @Override |
---|
190 | public void applyURI( |
---|
191 | Request request, |
---|
192 | String oldTarget, |
---|
193 | String newTarget) throws IOException { |
---|
194 | |
---|
195 | if( ! newTarget.equalsIgnoreCase(oldTarget)) { |
---|
196 | request.setRequestURI(request.getContextPath() + newTarget); |
---|
197 | } |
---|
198 | } |
---|
199 | } |
---|