Java tutorial
/* * This file is part of the Synyx Greetingcard module for OpenCms. * * Copyright (c) 2007 Synyx GmbH & Co.KG (http://www.synyx.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package com.mylab; import com.mylab.cms.CmsContext; import com.mylab.cms.OpenCmsContext; import com.mylab.cms.OpenCmsVFSService; import com.mylab.cms.VFSService; import com.mylab.dao.Greetingcard; import com.mylab.dao.GreetingcardConfig; import com.mylab.dao.GreetingcardConfigDao; import com.mylab.dao.GreetingcardTemplate; import com.mylab.dao.GreetingcardTemplateDao; import com.mylab.dao.OpenCmsGreetingcardConfigDao; import com.mylab.dao.OpenCmsGreetingcardTemplateDao; import com.mylab.mail.MailTransmission; import com.mylab.mail.OpenCmsMailService; import com.mylab.mail.OpenCmsMailTransmission; import com.alkacon.opencms.v8.counter.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import javax.mail.MessagingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.opencms.file.CmsObject; import org.opencms.jsp.CmsJspActionElement; import org.opencms.jsp.CmsJspXmlContentBean; import org.opencms.main.CmsException; import org.opencms.main.CmsShell; import org.opencms.main.OpenCms; import org.opencms.module.CmsModule; /** * Encapsulates logic used for the transmit mail jsp. * @author Florian Hopf, Synyx GmbH & Co. KG */ public class TransmitMailBean { private Log log = LogFactory.getLog(TransmitMailBean.class); private boolean transmitted = false; private CmsJspActionElement jsp = null; private String authorName = null; private String authorMail = null; private String receiverName = null; private String receiverAddress = null; private String subject = null; private String imagePath = null; private String greetingcardTemplatePath = null; private String transmitTime = null; private String audiomessage = null; private Date transmitDate = null; private File file = null; private FileInputStream fileInputStream = null; private CmsContext cmsContext; private VFSService vfsService; private String remoteaddress; private static final String counterKey = "Wenskaarten"; private int count; public CmsCounterManager getCounterManager() { CmsCounterManager result = null; // Get the module CmsModule module = OpenCms.getModuleManager().getModule(CmsCounterManager.MODULE_NAME); // Get the action class result = (CmsCounterManager) module.getActionInstance(); if (result == null) { result = new CmsCounterManager(); } return result; } /** * Initializes the bean and sends or queues the mail. * @param context * @param request * @param response * @throws javax.servlet.jsp.JspException * @throws IOException */ public void init(PageContext context, HttpServletRequest request, HttpServletResponse response) { try { remoteaddress = request.getRemoteAddr(); jsp = new CmsJspXmlContentBean(context, request, response); GreetingcardConfigDao configDao = new OpenCmsGreetingcardConfigDao(jsp.getCmsObject()); GreetingcardConfig config = null; try { config = configDao.readConfig(); } catch (DataAccessException ex) { } cmsContext = new OpenCmsContext(jsp); CmsObject cms = jsp.getCmsObject(); vfsService = new OpenCmsVFSService(jsp); //get the parameters from the request authorName = request.getParameter("author_name"); authorMail = request.getParameter("author_address"); receiverName = request.getParameter("receiver_name"); receiverAddress = request.getParameter("receiver_address"); subject = request.getParameter("subject"); imagePath = request.getParameter("image_url"); greetingcardTemplatePath = request.getParameter("greetingcard_fileName"); transmitTime = request.getParameter("transmit_date"); audiomessage = request.getParameter("audio_message"); if ((audiomessage != null) && (audiomessage.length() > 0)) { if (audiomessage.contains("Letter-To-Hermione.mp3")) audiomessage = ""; else { CmsShell cmdShell = new CmsShell("/usr/local/apache-tomcat-7.0.57/webapps/opencms/WEB-INF", null, "opencms", "/bin/bash", null); file = new File("/home/jan/bin/opencmsshell.sh"); try { fileInputStream = new FileInputStream(file); cmdShell.start(fileInputStream); fileInputStream.close(); audiomessage = System.getProperty("audio_message"); System.clearProperty("audio_message"); } catch (IOException ex) { } } } transmitDate = null; if (!"0".equals(transmitTime)) { transmitDate = new Date(Long.parseLong(transmitTime)); } MailTransmission transmission = new OpenCmsMailTransmission(cms, config, new OpenCmsMailService()); GreetingcardTemplateDao greetingcardTemplateDao = new OpenCmsGreetingcardTemplateDao(cms); // get url of the server for sending the url of the generated image String serverUrl = cmsContext.getServerPath(cmsContext.link(cmsContext.removeSiteRoot(imagePath))); //String serverUrl = GreetingCardHelper.getServerContextPath(request, jsp.link(cms.getRequestContext().removeSiteRoot(imagePath))); // construct the card Greetingcard card = new Greetingcard(); card.setAuthorMail(authorMail); card.setAuthorName(authorName); card.setImagePath(imagePath); card.setReceiverMail(receiverAddress); card.setReceiverName(receiverName); card.setSubject(subject); card.setTransmitDate(transmitDate); card.setUrl(serverUrl); if ((audiomessage != null) && (audiomessage.length() > 0)) card.setAudioPath("/greetingcards/.content/audiomessages/" + audiomessage); else card.setAudioPath(""); // read the greetingcard template GreetingcardTemplate cardTemplate = greetingcardTemplateDao.readCard(greetingcardTemplatePath); // transform the content using MailTemplate // TODO this should be transformed using method parameters not constructor MailTemplate mailTemplate = new StringMailTemplate(cardTemplate.getMailText()); card.setContent(mailTemplate.getContent(card)); loginUser(); // send the mail with the TransmitMail-class transmission.sendOrQueue(config, vfsService, card); logout(); transmitted = true; count = getCounterManager().incrementCounter(counterKey + "_" + remoteaddress); } catch (MessagingException | DataAccessException e) { e.printStackTrace(); } catch (CmsException e) { e.printStackTrace(); } finally { } } public void logout() { HttpSession session = jsp.getRequest().getSession(false); if (session != null) { session.invalidate(); } } protected int loginUser() { try { // attempt to login the user String OU = "/cardadmin"; jsp.getCmsObject().loginUser(OU, "xxx17yyy"); // make sure we have a new session after login for security reasons HttpSession session = jsp.getRequest().getSession(false); if (session != null) { session.invalidate(); } session = jsp.getRequest().getSession(true); } catch (CmsException e) { // return error code } finally { // switch back to original project } return 0; } public boolean isTransmitted() { return transmitted; } public String getAudioMessage() { return audiomessage; } public void setAudioMessage(String audiomessage) { this.audiomessage = audiomessage; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }