Salvatore La Manna
4 anni fa
14 ha cambiato i file con 325 aggiunte e 132 eliminazioni
File binario non mostrato.
@ -1,27 +1,224 @@
|
||||
package it.mwg.sismica.fe.servlet; |
||||
|
||||
import it.tref.liferay.portos.bo.shared.util.Constants; |
||||
import it.tref.liferay.portos.mailmanager.shared.messaging.util.DestinationNames; |
||||
import it.tref.liferay.portos.mailmanager.shared.util.MailManagerConstants; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileWriter; |
||||
import java.io.IOException; |
||||
import java.nio.file.Files; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Arrays; |
||||
import java.util.Calendar; |
||||
import java.util.Comparator; |
||||
import java.util.Date; |
||||
|
||||
import javax.mail.internet.AddressException; |
||||
import javax.mail.internet.InternetAddress; |
||||
import javax.servlet.ServletException; |
||||
import javax.servlet.http.HttpServlet; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
import org.apache.commons.io.FileUtils; |
||||
import org.apache.commons.io.FilenameUtils; |
||||
|
||||
import com.liferay.portal.kernel.exception.PortalException; |
||||
import com.liferay.portal.kernel.exception.SystemException; |
||||
import com.liferay.portal.kernel.log.Log; |
||||
import com.liferay.portal.kernel.log.LogFactoryUtil; |
||||
import com.liferay.portal.kernel.mail.MailMessage; |
||||
import com.liferay.portal.kernel.messaging.Message; |
||||
import com.liferay.portal.kernel.messaging.MessageBusUtil; |
||||
import com.liferay.portal.kernel.util.DigesterUtil; |
||||
import com.liferay.portal.kernel.util.ParamUtil; |
||||
import com.liferay.portal.kernel.util.PropsUtil; |
||||
import com.liferay.portal.kernel.util.StringUtil; |
||||
import com.liferay.portal.kernel.util.Validator; |
||||
import com.liferay.portal.model.Company; |
||||
import com.liferay.portal.model.Role; |
||||
import com.liferay.portal.model.RoleConstants; |
||||
import com.liferay.portal.model.User; |
||||
import com.liferay.portal.service.RoleLocalServiceUtil; |
||||
import com.liferay.portal.service.ServiceContext; |
||||
import com.liferay.portal.service.ServiceContextFactory; |
||||
import com.liferay.portal.service.UserLocalServiceUtil; |
||||
import com.liferay.portal.util.PortalUtil; |
||||
|
||||
public class SorgentiServlet extends HttpServlet { |
||||
private static final long serialVersionUID = 5549374841236285035L; |
||||
private static final Log _log = LogFactoryUtil.getLog(SorgentiServlet.class); |
||||
|
||||
private static final int validita = 3; |
||||
private static final String folder = "condivisi"; |
||||
private static final String uri = "/portos-fe-portlet/sorgenti-sismica/"; |
||||
|
||||
@Override |
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, |
||||
IOException { |
||||
String pathInfo = request.getPathInfo(); |
||||
_log.info(pathInfo); |
||||
|
||||
if (Validator.isNull(pathInfo) || pathInfo.equals("/")) { |
||||
request.getRequestDispatcher("/html/sorgenti/form.jsp").forward(request, response); |
||||
} else { |
||||
String[] parti = StringUtil.split(pathInfo, '/'); |
||||
if (parti.length == 4) { |
||||
registra(request, "Scaricamento - " + parti[1] + "/" + parti[2] + "/" + parti[3]); |
||||
String path = parti[1]; |
||||
File file = new File(PropsUtil.get("liferay.home") + "/" + folder + "/" + parti[3]); |
||||
if (file.exists() && !file.isDirectory() && pathValido(request, path)) { |
||||
String contentType = probeContentType(file); |
||||
response.setContentType(contentType); |
||||
response.setContentLength((int) file.length()); |
||||
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName()); |
||||
FileUtils.copyFile(file, response.getOutputStream()); |
||||
} else { |
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI()); |
||||
} |
||||
} else { |
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, |
||||
IOException { |
||||
String pathInfo = request.getPathInfo(); |
||||
|
||||
if (Validator.isNotNull(pathInfo) && pathInfo.equals("/richiedi")) { |
||||
try { |
||||
if (inviaMail(request, response)) { |
||||
request.getRequestDispatcher("/html/sorgenti/invio_ok.jsp").forward(request, response); |
||||
} else { |
||||
request.getRequestDispatcher("/html/sorgenti/invio_ko.jsp").forward(request, response); |
||||
} |
||||
} catch (AddressException | PortalException | SystemException e) { |
||||
request.getRequestDispatcher("/html/sorgenti/invio_ko.jsp").forward(request, response); |
||||
e.printStackTrace(); |
||||
} |
||||
} else { |
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI()); |
||||
} |
||||
} |
||||
|
||||
private static boolean pathValido(HttpServletRequest request, String path) { |
||||
Calendar calendar = Calendar.getInstance(); |
||||
calendar.setTime(new Date()); |
||||
for (int i = 0; i <= validita; i++) { |
||||
if (path.equals(getLink(request, calendar.getTime()))) { |
||||
return true; |
||||
} |
||||
calendar.add(Calendar.DAY_OF_MONTH, -1); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
private static String getSeed(HttpServletRequest request) { |
||||
String seed = null; |
||||
try { |
||||
Company company = PortalUtil.getCompany(request); |
||||
seed = (String) company.getExpandoBridge().getAttribute(Constants.COMPANY_CUSTOM_FIELD_LINK_SEED, |
||||
false); |
||||
} catch (PortalException | SystemException e) { |
||||
} |
||||
return seed; |
||||
} |
||||
|
||||
private static boolean inviaMail(HttpServletRequest request, HttpServletResponse response) |
||||
throws AddressException, PortalException, SystemException, IOException { |
||||
Date date = new Date(); |
||||
Calendar calendar = Calendar.getInstance(); |
||||
calendar.setTime(date); |
||||
calendar.add(Calendar.DAY_OF_MONTH, validita); |
||||
|
||||
String nome = ParamUtil.getString(request, "nome"); |
||||
String cognome = ParamUtil.getString(request, "cognome"); |
||||
String email = ParamUtil.getString(request, "email"); |
||||
|
||||
String link = getLink(request, date); |
||||
String digest = digest(request, email); |
||||
String scadenza = new SimpleDateFormat("dd/MM/yyyy").format(calendar.getTime()); |
||||
|
||||
String testo = "Gentile " + nome + " " + cognome |
||||
+ ".<br/><br/>Puoi scaricare i sorgenti di Sismica Sicilia da qui:<br>"; |
||||
|
||||
File[] lista = new File(PropsUtil.get("liferay.home") + "/" + folder).listFiles(); |
||||
Arrays.sort(lista, new Comparator<File>() { |
||||
@Override |
||||
public int compare(File f1, File f2) { |
||||
return f1.getName().compareTo(f2.getName()); |
||||
} |
||||
}); |
||||
for (File file : lista) { |
||||
testo += "<a href=\"" + PortalUtil.getPortalURL(request) + uri + link + "/" + digest + "/" |
||||
+ file.getName() + "\">" + file.getName() + "</a>.<br>"; |
||||
} |
||||
if (lista.length > 1) { |
||||
testo += "<br>I link saranno validi"; |
||||
} else { |
||||
testo += "<br>Il link sarà valido"; |
||||
} |
||||
testo += " fino a tutto il giorno " + scadenza |
||||
+ ".<br><br>È possibile in ogni momento richiedere un nuovo link da questa <a href=\"" |
||||
+ PortalUtil.getPortalURL(request) + uri + "\">pagina</a>."; |
||||
|
||||
InternetAddress from = new InternetAddress("noreply-Sismica@lavoripubblici.sicilia.it", true); |
||||
InternetAddress[] to = { new InternetAddress(email, true) }; |
||||
|
||||
MailMessage mail = new MailMessage(from, "Sorgenti del portale Sismica Sicilia", testo, true); |
||||
mail.setTo(to); |
||||
|
||||
ServiceContext serviceContext = ServiceContextFactory.getInstance(request); |
||||
Role role = RoleLocalServiceUtil.getRole(serviceContext.getCompanyId(), RoleConstants.ADMINISTRATOR); |
||||
User user = UserLocalServiceUtil.getRoleUsers(role.getRoleId(), 0, 1).get(0); |
||||
serviceContext.setUserId(user.getUserId()); |
||||
|
||||
Message message = new Message(); |
||||
message.put("serviceContext", serviceContext); |
||||
message.put("mode", MailManagerConstants.MESSAGING_SEND_MODE_ASYNC); |
||||
|
||||
message.setPayload(mail); |
||||
|
||||
MessageBusUtil.sendMessage(DestinationNames.SEND_MAIL, message); |
||||
|
||||
registra(request, "Richiesta - nome: " + nome + ", cognome: " + cognome + ", email: " + email |
||||
+ ", cartella: " + link + "/" + digest); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
private static String getLink(HttpServletRequest request, Date date) { |
||||
return digest(request, new SimpleDateFormat("dd/MM/yyyy").format(date)); |
||||
} |
||||
|
||||
private static String digest(HttpServletRequest request, String text) { |
||||
return DigesterUtil.digestHex("md5", getSeed(request) + text); |
||||
} |
||||
|
||||
private static void registra(HttpServletRequest request, String text) throws IOException { |
||||
FileWriter writer = new FileWriter(PropsUtil.get("liferay.home") + "/logs/sorgenti.log", true); |
||||
writer.write(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + " [" |
||||
+ request.getRemoteAddr() + "] " + text + "\n"); |
||||
writer.close(); |
||||
} |
||||
|
||||
// La macchina di produzione non ha il supporto per mime, aggiriamo per i
|
||||
// casi che ci interessano
|
||||
private static String probeContentType(File file) { |
||||
try { |
||||
String extension = FilenameUtils.getExtension(file.getName()).toLowerCase(); |
||||
switch (extension) { |
||||
case "pdf": |
||||
return "application/pdf"; |
||||
case "zip": |
||||
return "application/zip"; |
||||
default: |
||||
return Files.probeContentType(file.toPath()); |
||||
} |
||||
} catch (IOException e) { |
||||
return "application/octet-stream"; |
||||
} |
||||
} |
||||
} |
||||
|
@ -1,6 +1,6 @@
|
||||
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> |
||||
</div> |
||||
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> |
||||
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> |
||||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script> |
||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> |
||||
</body> |
||||
</html> |
@ -1,18 +1,22 @@
|
||||
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> |
||||
<%@page import="com.liferay.portal.kernel.util.ParamUtil" %> |
||||
<!DOCTPYE html> |
||||
<html> |
||||
<head> |
||||
<title>Sorgenti del portale Sismica Sicilia</title> |
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> |
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" |
||||
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"> |
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" |
||||
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> |
||||
</head> |
||||
<body> |
||||
<div class="container"> |
||||
<div class="row"> |
||||
<div class="col-xs-6"> |
||||
<img src="/portos-public-theme/images/logo-rs-testo.png" alt="Regione Siciliana" /> |
||||
<img style="height: 60px" src="/portos-public-theme/images/logo-rs-testo.png" alt="Regione Siciliana" /> |
||||
</div> |
||||
<div class="col-xs-6 text-right"></div> |
||||
<div class="col-xs-6 text-right"> |
||||
<img style="height: 60px" src="/portos-public-theme/images/etoscana-dark-text.png" /> |
||||
</div> |
||||
<hr /> |
||||
</div> |
@ -0,0 +1,10 @@
|
||||
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> |
||||
<%@ include file="/html/sorgenti/header.jsp"%> |
||||
<div class="row"> |
||||
<div class="col-xs-12"> |
||||
Si è verificato un errore durante l'inzio di una mail all'indirizzo |
||||
<%=ParamUtil.getString(request, "email")%><br><br> |
||||
Ti invitiamo a riprovare tra qualche minuto. |
||||
</div> |
||||
</div> |
||||
<%@ include file="/html/sorgenti/footer.jsp"%> |
@ -0,0 +1,10 @@
|
||||
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> |
||||
<%@ include file="/html/sorgenti/header.jsp"%> |
||||
<div class="row"> |
||||
<div class="col-xs-12"> |
||||
È stata inviata una mail all'indirizzo |
||||
<%=ParamUtil.getString(request, "email")%> |
||||
contente l'indirizzo per scaricare i sorgenti del portale Sismica di Regione Siciliana |
||||
</div> |
||||
</div> |
||||
<%@ include file="/html/sorgenti/footer.jsp"%> |
Caricamento…
Reference in new issue