/** * 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.net.URLEncoder; import java.util.regex.Matcher; 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.domain.metaarchive.BinaryBody; import serpro.mailarchiver.domain.metaarchive.Message; import serpro.mailarchiver.domain.metaarchive.SingleBody; import serpro.mailarchiver.service.find.FBinaryBody; import serpro.mailarchiver.service.find.FMessage; import serpro.mailarchiver.service.find.FTextBody; import serpro.mailarchiver.util.transaction.WithReadWriteTx; @Configurable public class TempPublishRule extends RegexRule implements Rule.ApplyURI { private static final Logger log = Logger.getLocalLogger(); @Autowired private FMessage findMessage; @Autowired private FBinaryBody findBinaryBody; @Autowired private FTextBody findTextBody; public TempPublishRule() { _handling = false; _terminating = false; 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}))?(/(.*))?)$"); //group --> 1 23 4 5 6 7 8 9 1 1 1 1 // 0 1 2 3 } @WithReadWriteTx @Override public String apply( String oldTarget, HttpServletRequest request, HttpServletResponse response, Matcher matcher) { // for(int i = 0; i < 14; i++) { // if(matcher.group(i) != null) { // System.out.println("group(" + i + "): >>" + matcher.group(i) + "<<"); // } // } String newTarget = oldTarget; String id = matcher.group(5); String format = matcher.group(7); String thumbSize = matcher.group(11); String fileName = matcher.group(13); boolean download = (matcher.group(1) != null); boolean downloadParts = (matcher.group(3) != null) && (format != null); boolean downloadMails = (matcher.group(4) != null) && (format != null); boolean thumbnail = (matcher.group(10) != null); if(downloadMails) { response.setHeader("Content-Type", "application/download"); response.setHeader("Content-Disposition", "attachment; filename=mails_" + id + "." + format); newTarget = newTarget.substring(9); log.debug("Target rewrited to {%s}", newTarget); } else if(downloadParts) { Message message = findMessage.byId(id); if(message != null) { try { message.publishPartsZip(format); response.setHeader("Content-Type", "application/download"); response.setHeader("Content-Disposition", "attachment; filename=" + message.getPartsZipFileName(format)); newTarget = newTarget.substring(9); log.debug("Target rewrited to {%s}", newTarget); } catch(IOException ex) { log.error(ex, "Temp publish rule"); } } } else { BinaryBody binaryBody = findBinaryBody.byId(id); SingleBody singleBody = (binaryBody != null) ? binaryBody : findTextBody.byId(id); if(singleBody != null) { boolean redirect = (fileName == null) || ( ! fileName.equals(singleBody.getFileName())); try { if(thumbnail) { binaryBody.publishThumbnail(Integer.parseInt(thumbSize)); } else { singleBody.publish(); } if(redirect) { newTarget = "/" + (download ? "download/" : "") + id + "/" + (thumbnail ? "thumb/" + thumbSize + "/" : "") + URLEncoder.encode(singleBody.getFileName(), "UTF-8").replace("+", "%20"); String contextPath = request.getContextPath(); String location = response.encodeRedirectURL(contextPath + newTarget); response.sendRedirect(location); log.debug("Target redirected to {%s}", newTarget); } else if(download) { response.setHeader("Content-Type", "application/download"); response.setHeader("Content-Disposition", "attachment; filename=" + singleBody.getFileName()); response.setHeader("Content-Length", "" + singleBody.getSize()); newTarget = newTarget.substring(9); log.debug("Target rewrited to {%s}", newTarget); } } catch(IOException ex) { log.error(ex, "Temp publish rule"); } } } return newTarget; } @Override public void applyURI( Request request, String oldTarget, String newTarget) throws IOException { if( ! newTarget.equalsIgnoreCase(oldTarget)) { request.setRequestURI(request.getContextPath() + newTarget); } } }