|
|
|
|
@ -9,19 +9,25 @@ 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 class RequestServlet extends MailerServlet
|
|
|
|
|
{
|
|
|
|
|
private final static String REQUEST_NAME = "request_name";
|
|
|
|
|
private final static String REQUEST_PHONE = "request_phone";
|
|
|
|
|
private final static String REQUEST_EMAIL = "request_email";
|
|
|
|
|
private final static String REQUEST_DETAILS = "request_details";
|
|
|
|
|
|
|
|
|
|
private final static String[] FORM_FIELDS = new String[]{
|
|
|
|
|
REQUEST_NAME, REQUEST_PHONE,
|
|
|
|
|
REQUEST_EMAIL, REQUEST_DETAILS
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public void doPost( HttpServletRequest req, HttpServletResponse res )
|
|
|
|
|
throws IOException
|
|
|
|
|
{
|
|
|
|
|
@ -38,122 +44,15 @@ public class RequestServlet extends HttpServlet
|
|
|
|
|
// 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 content = createContent( parameters, FORM_FIELDS, "mail/pedido_informacao.txt" );
|
|
|
|
|
|
|
|
|
|
String from = ( String ) parameters.get( REQUEST_EMAIL );
|
|
|
|
|
String destination = "psantos@evolute.pt";
|
|
|
|
|
String subject = "Pedido de informação através do site www.siprp.pt";
|
|
|
|
|
// manda mail
|
|
|
|
|
boolean success = sendMail( from, destination, subject, content );
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
// mostra pagina correspondente
|
|
|
|
|
out.println( showResultPage( success, "mail/pedido_enviado.html", "mail/pedido_nao_enviado.html" ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|