forked from Coded/SIPRP
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							160 lines
						
					
					
						
							3.6 KiB
						
					
					
				
			
		
		
	
	
							160 lines
						
					
					
						
							3.6 KiB
						
					
					
				/*
 | 
						|
 * RequestServlet.java
 | 
						|
 *
 | 
						|
 * Created on 4 de Março de 2005, 18:19
 | 
						|
 */
 | 
						|
 | 
						|
package siprp.pagina;
 | 
						|
 | 
						|
import java.io.*;
 | 
						|
import java.util.*;
 | 
						|
 | 
						|
import javax.mail.*;
 | 
						|
import javax.mail.internet.*;
 | 
						|
import javax.servlet.*;
 | 
						|
import javax.servlet.http.*;
 | 
						|
 | 
						|
import org.apache.velocity.*;
 | 
						|
import org.apache.velocity.app.*;
 | 
						|
/**
 | 
						|
 *
 | 
						|
 * @author  psantos
 | 
						|
 */
 | 
						|
public class RequestServlet extends HttpServlet
 | 
						|
{
 | 
						|
	public void doPost( HttpServletRequest req, HttpServletResponse res )
 | 
						|
	throws IOException
 | 
						|
	{
 | 
						|
		ServletOutputStream out = res.getOutputStream();
 | 
						|
		res.setContentType( "text/html" );
 | 
						|
 | 
						|
		/*
 | 
						|
		 parametros:
 | 
						|
		 request_name
 | 
						|
		 request_phone
 | 
						|
		 request_email
 | 
						|
		 request_details
 | 
						|
		 */
 | 
						|
		// recebe info de contacto
 | 
						|
		Hashtable parameters = parseParameters( new Hashtable( req.getParameterMap() ) );
 | 
						|
 | 
						|
		// manda mail
 | 
						|
		boolean success = sendMail( parameters );
 | 
						|
 | 
						|
		// mostra pagina correspondente
 | 
						|
		out.println( showResultPage( success ) );
 | 
						|
	}
 | 
						|
 | 
						|
	private String showResultPage( boolean success )
 | 
						|
	{
 | 
						|
		VelocityContext context = new VelocityContext();
 | 
						|
		StringWriter output = new StringWriter();
 | 
						|
		String template;
 | 
						|
		if( success )
 | 
						|
		{
 | 
						|
			template = "pedido_enviado.html";
 | 
						|
		}
 | 
						|
		else
 | 
						|
		{
 | 
						|
			template = "pedido_nao_enviado.html";
 | 
						|
		}
 | 
						|
		try
 | 
						|
		{
 | 
						|
			Velocity.mergeTemplate( "mail/" + template, Velocity.ENCODING_DEFAULT, context, output );
 | 
						|
			return output.toString();
 | 
						|
		}
 | 
						|
		catch( Exception e )
 | 
						|
		{
 | 
						|
			e.printStackTrace();
 | 
						|
		}
 | 
						|
 | 
						|
		return null;
 | 
						|
	}
 | 
						|
	
 | 
						|
	private boolean sendMail( Hashtable parameters )
 | 
						|
	{
 | 
						|
		String smtp_server = "localhost";
 | 
						|
		//String from = "psantos@evolute.pt";
 | 
						|
		String subject = "Pedido de informação através do site www.siprp.pt";
 | 
						|
		String mailer = "Evolute Mailer";
 | 
						|
 | 
						|
		// Dados da form de pedido de informacao
 | 
						|
		String name = ( String ) parameters.get( "request_name" );
 | 
						|
		String phone = ( String ) parameters.get( "request_phone" );
 | 
						|
		String email = ( String ) parameters.get( "request_email" );
 | 
						|
		String details = ( String ) parameters.get( "request_details" );
 | 
						|
 | 
						|
		String destination = "psantos@evolute.pt";
 | 
						|
		
 | 
						|
		Properties props = System.getProperties();
 | 
						|
		props.put( "mail.smtp.host", smtp_server );
 | 
						|
 | 
						|
		Session session = Session.getInstance( props, null );
 | 
						|
		
 | 
						|
		Message msg = new MimeMessage( session );
 | 
						|
		try
 | 
						|
		{
 | 
						|
			VelocityContext context = new VelocityContext();
 | 
						|
			StringWriter content = new StringWriter();
 | 
						|
 | 
						|
			msg.setFrom( new InternetAddress( email ) );
 | 
						|
			msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( destination, true ) );
 | 
						|
			msg.setSubject( subject );
 | 
						|
 | 
						|
			context.put( "name", name );
 | 
						|
			context.put( "phone", phone );
 | 
						|
			context.put( "email", email );
 | 
						|
			context.put( "details", details );
 | 
						|
 | 
						|
			Velocity.mergeTemplate( "mail/pedido_informacao.txt", Velocity.ENCODING_DEFAULT, context, content );
 | 
						|
 | 
						|
			msg.setContent( content.toString(), "text/plain" );
 | 
						|
 | 
						|
			msg.setHeader( "X-Mailer", mailer );
 | 
						|
			msg.setSentDate( new Date() );
 | 
						|
			Transport.send( msg );
 | 
						|
 | 
						|
			return true;
 | 
						|
		}
 | 
						|
		catch( Exception e )
 | 
						|
		{
 | 
						|
			e.printStackTrace();
 | 
						|
		}
 | 
						|
 | 
						|
		return false;
 | 
						|
	}
 | 
						|
 | 
						|
	private String checkParameter( String parameter )
 | 
						|
	{
 | 
						|
		if( parameter != null && parameter.trim().length() > 0 )
 | 
						|
		{
 | 
						|
			return parameter.trim();
 | 
						|
		}
 | 
						|
 | 
						|
		return null;
 | 
						|
	}
 | 
						|
 | 
						|
	public Hashtable parseParameters( Hashtable parameters )
 | 
						|
	{
 | 
						|
		Hashtable tmp = new Hashtable();
 | 
						|
		String[] element;
 | 
						|
		String key;
 | 
						|
		String new_element;
 | 
						|
		for( Enumeration e = parameters.keys(); e.hasMoreElements(); )
 | 
						|
		{
 | 
						|
			key = ( String ) e.nextElement();
 | 
						|
			element = ( String[] ) parameters.get( key );
 | 
						|
			if( element != null )
 | 
						|
			{
 | 
						|
				new_element = checkParameter( element[ 0 ] );
 | 
						|
				if( new_element != null )
 | 
						|
				{
 | 
						|
					tmp.put( key, new_element );
 | 
						|
				}
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		return tmp;
 | 
						|
	}
 | 
						|
}
 |