[6785] | 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.nio.file.Files; |
---|
| 34 | import java.nio.file.Path; |
---|
| 35 | import java.util.regex.Matcher; |
---|
| 36 | import java.util.regex.Pattern; |
---|
| 37 | |
---|
| 38 | import javax.servlet.http.HttpServletRequest; |
---|
| 39 | import javax.servlet.http.HttpServletResponse; |
---|
| 40 | |
---|
| 41 | import org.eclipse.jetty.rewrite.handler.RegexRule; |
---|
| 42 | import org.eclipse.jetty.rewrite.handler.Rule; |
---|
| 43 | import org.eclipse.jetty.server.Request; |
---|
| 44 | |
---|
| 45 | import org.springframework.beans.factory.annotation.Autowired; |
---|
| 46 | import org.springframework.beans.factory.annotation.Configurable; |
---|
| 47 | |
---|
| 48 | import serpro.mailarchiver.session.Session; |
---|
| 49 | import serpro.mailarchiver.session.SessionMap; |
---|
| 50 | |
---|
| 51 | @Configurable |
---|
| 52 | public class MailAccessRule |
---|
| 53 | extends RegexRule |
---|
| 54 | implements Rule.ApplyURI |
---|
| 55 | { |
---|
| 56 | private static final Logger log = Logger.getLocalLogger(); |
---|
| 57 | |
---|
| 58 | public MailAccessRule() { |
---|
| 59 | _handling = false; |
---|
| 60 | _terminating = false; |
---|
| 61 | |
---|
| 62 | setRegex("^/(.*)$"); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | @Autowired |
---|
| 66 | private UserAppConfig userAppConfig; |
---|
| 67 | |
---|
| 68 | private static final String messagePathRegex = |
---|
| 69 | "^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/" + |
---|
| 70 | "(([^/]+/)*([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\\.eml)$"; |
---|
| 71 | |
---|
| 72 | private static final Pattern messagePathPattern = Pattern.compile(messagePathRegex, Pattern.CASE_INSENSITIVE); |
---|
| 73 | |
---|
| 74 | @Override |
---|
| 75 | public String apply( |
---|
| 76 | String oldTarget, |
---|
| 77 | HttpServletRequest request, |
---|
| 78 | HttpServletResponse response, |
---|
| 79 | Matcher matcher) { |
---|
| 80 | |
---|
| 81 | Matcher m = messagePathPattern.matcher(matcher.group(1)); |
---|
| 82 | |
---|
| 83 | if(m.matches()) { |
---|
| 84 | String sessionId = m.group(1); |
---|
| 85 | Session session = SessionMap.get(sessionId); |
---|
| 86 | if(session != null) { |
---|
| 87 | String relativeStrPath = session.getUserId() + "/" + m.group(2); |
---|
| 88 | Path absolutePath = userAppConfig.SERVER.getArchiveDir() |
---|
| 89 | .resolve("mail") |
---|
| 90 | .resolve(relativeStrPath); |
---|
| 91 | if(Files.exists(absolutePath)) { |
---|
| 92 | return "/" + relativeStrPath; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | try { |
---|
| 96 | response.sendError(HttpServletResponse.SC_NOT_FOUND); |
---|
| 97 | } |
---|
| 98 | catch (IOException ex) { |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | else { |
---|
| 102 | try { |
---|
| 103 | response.sendError(HttpServletResponse.SC_BAD_REQUEST); |
---|
| 104 | } |
---|
| 105 | catch (IOException ex) { |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | return oldTarget; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | @Override |
---|
| 113 | public void applyURI( |
---|
| 114 | Request request, |
---|
| 115 | String oldTarget, |
---|
| 116 | String newTarget) throws IOException { |
---|
| 117 | |
---|
| 118 | if( ! newTarget.equalsIgnoreCase(oldTarget)) { |
---|
| 119 | request.setRequestURI(request.getContextPath() + newTarget); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | } |
---|