package it.acxent.pg.servlet; import it.acxent.db.ApplParmFull; import it.acxent.pg.Foto; import it.acxent.pg.Users; import java.io.File; import java.io.FileInputStream; import javax.servlet.RequestDispatcher; import javax.servlet.ServletOutputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GetFileTnSvlt extends _PgPageSvlt { private static final long serialVersionUID = -3037980260742696647L; protected boolean checkLoginProfile(HttpServletRequest req) { try { return true; } catch (Exception e) { handleDebug(e); return false; } } protected String getLoginPage(HttpServletRequest req, HttpServletResponse res) { return "/documentoCR.jsp"; } protected void otherCommands(HttpServletRequest req, HttpServletResponse res) { _fileTn(req, res); } protected void sendFile(String fileName, HttpServletRequest req, HttpServletResponse res) { File theFile = null; FileInputStream fis = null; try { theFile = new File(fileName); String ext = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length()); res.setHeader("Content-Type", "image/jpg"); res.setContentType("application/" + ext.toLowerCase()); if (theFile.exists()) { fis = new FileInputStream(theFile); byte[] temp = new byte[1024]; int nByte = 0; ServletOutputStream sos = res.getOutputStream(); while ((nByte = fis.read(temp)) != -1) sos.write(temp, 0, nByte); sos.flush(); fis.close(); sos.close(); } } catch (Exception e) { e.printStackTrace(); } finally { theFile = null; if (fis != null) fis = null; } } public void _fileTn(HttpServletRequest req, HttpServletResponse res) { inviaFile(true, req, res); } protected void search(HttpServletRequest req, HttpServletResponse res) { _fileTn(req, res); } public void _fileOriginale(HttpServletRequest req, HttpServletResponse res) { ApplParmFull apFull = getApFull(req); boolean inviaFile = true; boolean inviaTn = false; Users bean = new Users(apFull); bean.findByPrimaryKey(getLoginUserId(req)); long l_id_foto = getId_foto(req); Foto foto = new Foto(apFull); foto.findByPrimaryKey(l_id_foto); if (bean.getFlgMiniature() == 1L) inviaTn = true; if (foto.getPuntoFoto().getGara().getFlgFree() == 1L) { inviaFile = true; } else if (bean.getFlgValido().equals("N")) { inviaFile = false; } else if (bean.isScaduto()) { forceMessage(req, "Utente non valido. Utente scaduto."); req.getSession().setAttribute("msgS", "ATTENZIONE!! Account scaduto"); bean.sendNoMorePictureMessageScad(); inviaFile = false; } else if (bean.isFotoMaxRaggiunto()) { forceMessage(req, "Utente non valido. Numero massimo di foto visualizzate raggiunto."); req.getSession().setAttribute("msgS", "ATTENZIONE!! Numero massimo di foto visualizzate raggiunto."); bean.sendNoMorePictureMessageNumber(); inviaFile = false; } if (inviaFile) { inviaFile(inviaTn, req, res); } else { sendHtmlMsgResponse(req, res, "Attenzione!. Account scaduto o raggiunto n. foto massimo"); } } private long getId_foto(HttpServletRequest req) { long l_id_foto = getRequestLongParameter(req, "id_foto"); if (l_id_foto == 0L) { String requestURI = req.getRequestURI(); int idx_ = requestURI.lastIndexOf("-"); if (idx_ > 0) { int idxDot = requestURI.lastIndexOf("."); try { l_id_foto = Long.valueOf(requestURI.substring(idx_ + 1, idxDot)); } catch (Exception e) { l_id_foto = 0L; } } } return l_id_foto; } private void inviaFile(boolean inviaTn, HttpServletRequest req, HttpServletResponse res) { ApplParmFull apFull = getApFull(req); String fileName = ""; long l_id_foto = getId_foto(req); Foto foto = new Foto(apFull); if (l_id_foto == 0L) { fileName = getDocBase() + "_img/_imgNotFound.png"; } else { foto.findByPrimaryKey(l_id_foto); if (foto.getDBState() == 1) { if (inviaTn) { fileName = foto.getPathCompletoFileTn(); } else { fileName = foto.getPathCompletoFile(); } if (!inviaTn) { if (fileName.indexOf("_X") > 0 && getLoginUser(req).getId_userProfile() != 1L) fileName = null; if (fileName != null && fileName.indexOf("_Y") > 0 && getLoginUser(req).getId_userProfile() != 1L) fileName = null; if (fileName != null && fileName.indexOf("_Z") > 0 && getLoginUser(req).getId_userProfile() != 111L) fileName = null; } } else { fileName = getDocBase() + "_img/_imgNotFound.png"; } } if (fileName != null) { if (!inviaTn && foto.getId_foto() > 0L) { Users user = new Users(apFull); user.findByPrimaryKey(getLoginUserId(req)); Users.addLogFoto(user, foto, apFull); } sendFile(fileName, req, res); } else { String context = req.getRequestURL().toString().substring(0, req.getRequestURL().toString().lastIndexOf("/fotoOriginali/")); String absPage = context + "/fotoNonPrelevabile.jsp"; try { res.sendRedirect(absPage); } catch (Exception e) { StringBuilder msg = new StringBuilder(); if (e.getCause() != null) { msg.append("Causa:\n"); msg.append(e.getCause().getMessage()); msg.append("\n"); } msg.append(e.getMessage()); handleDebug(e, 2); forceMessage(req, getJspPage(req)); req.setAttribute("errorMsg", msg.toString()); RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/config/error.jsp"); try { rd.forward((ServletRequest)req, (ServletResponse)res); } catch (Exception exception) {} } } } public void _fileTnAdmin(HttpServletRequest req, HttpServletResponse res) { ApplParmFull apFull = getApFull(req); String fileName = ""; long l_id_foto = getRequestLongParameter(req, "id_foto"); Foto foto = new Foto(apFull); if (l_id_foto == 0L) { String requestURI = req.getRequestURI(); System.out.println("GetFileTnSvlt " + requestURI); int idx_ = requestURI.lastIndexOf("-"); if (idx_ > 0) { int idxDot = requestURI.lastIndexOf("."); try { l_id_foto = Long.valueOf(requestURI.substring(idx_ + 1, idxDot)); } catch (Exception e) { l_id_foto = 0L; } } } if (l_id_foto == 0L) { fileName = getDocBase() + "_img/_imgNotFound.png"; } else { foto.findByPrimaryKey(l_id_foto); if (foto.getDBState() == 1) { fileName = foto.getPathCompletoRidottaFile(); if (new File(fileName).exists()); } else { fileName = getDocBase() + "_img/_imgNotFound.png"; } } if (fileName != null && !fileName.isEmpty()) sendFile(fileName, req, res); } }