/** * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ /******************************************************************************\ * * This product was developed by * * SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), * * a government company established under Brazilian law (5.615/70), * at Department of Development of Porto Alegre. * \******************************************************************************/ package serpro.mailarchiver.util; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.rewrite.handler.RegexRule; import org.eclipse.jetty.rewrite.handler.Rule; import org.eclipse.jetty.server.Request; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; import serpro.mailarchiver.session.Session; import serpro.mailarchiver.session.SessionMap; @Configurable public class MailAccessRule extends RegexRule implements Rule.ApplyURI { private static final Logger log = Logger.getLocalLogger(); public MailAccessRule() { _handling = false; _terminating = false; setRegex("^/(.*)$"); } @Autowired private UserAppConfig userAppConfig; private static final String messagePathRegex = "^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/" + "(([^/]+/)*([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\\.eml)$"; private static final Pattern messagePathPattern = Pattern.compile(messagePathRegex, Pattern.CASE_INSENSITIVE); @Override public String apply( String oldTarget, HttpServletRequest request, HttpServletResponse response, Matcher matcher) { Matcher m = messagePathPattern.matcher(matcher.group(1)); if(m.matches()) { String sessionId = m.group(1); Session session = SessionMap.get(sessionId); if(session != null) { String relativeStrPath = session.getUserId() + "/" + m.group(2); Path absolutePath = userAppConfig.SERVER.getArchiveDir() .resolve("mail") .resolve(relativeStrPath); if(Files.exists(absolutePath)) { return "/" + relativeStrPath; } } try { response.sendError(HttpServletResponse.SC_NOT_FOUND); } catch (IOException ex) { } } else { try { response.sendError(HttpServletResponse.SC_BAD_REQUEST); } catch (IOException ex) { } } return oldTarget; } @Override public void applyURI( Request request, String oldTarget, String newTarget) throws IOException { if( ! newTarget.equalsIgnoreCase(oldTarget)) { request.setRequestURI(request.getContextPath() + newTarget); } } }