Commit 477e59cb authored by Sebastian's avatar Sebastian

mailversand

parent 95781d1f
......@@ -265,6 +265,15 @@
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
<build>
<finalName>geoportal</finalName>
......
......@@ -55,8 +55,8 @@ public class HibernateConfig {
String db = this.dev?"dev":"geoportal";
String user = this.dev?"dev":"geoportal";
String password = this.dev?"dev":"geoportal";
String host = this.dev?"postgres-dev-server":"postgres-server";
String host = this.dev?"postgres-dev-server":"postgres-server";
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://"+host+":5432/"+db);
dataSource.setUsername(user);
......
......@@ -174,18 +174,19 @@ public class GeoElements {
map.put("error", "key and value may not be empty or null");
return Response.status(Status.BAD_REQUEST).entity(map).build();
}
for(KeyValuePair p : element.getTags()) {
if(p.getKey() == kvpp.key) {
HashMap<String,String> map = new HashMap<String,String>();
map.put("error", "tag exists. use PATCH to update.");
return Response.status(Status.CONFLICT).entity(map).build();
List<KeyValuePair> removedTags = new ArrayList<KeyValuePair>();
if(element.hasTag(kvpp.key)) {
for(KeyValuePair p: element.getTags()) {
if(p.getKey().equals(kvpp.key) ) {
removedTags.add(p);
}
}
if(element.hasTag(kvpp.key)) {
HashMap<String,String> map = new HashMap<String,String>();
map.put("error", "tag exists. use PATCH to update.");
return Response.status(Status.CONFLICT).entity(map).build();
for(KeyValuePair p: removedTags) {
System.out.println("Removing tag "+p.getId()+ " from element "+element.getId());
element.removeTag(p);
}
}else {
System.out.println("Tag "+kvpp.key+ " does not yet exist on element "+element.getId());
}
KeyValuePair kvp = new KeyValuePair();
......@@ -197,9 +198,13 @@ public class GeoElements {
kvpService.create(kvp);
element.addTag(kvp);
osmRefService.merge(element);
for(KeyValuePair p:removedTags) {
kvpService.delete(p);
}
return Response.status(Status.OK).entity(element).build();
}
}
/**
* Gibt alle Elemente zurück, die im Rechteck (minX,maxX) x (minY,maxY) liegen.
*
......
package de.transformationsstadt.geoportal.api;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.net.SocketFactory;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.omg.PortableInterceptor.TRANSPORT_RETRY;
import de.transformationsstadt.geoportal.apiparameters.MailParameters;
@Path("/contact/")
public class Mail {
Properties properties;
public final static String propertiesFile = "geoportal.properties.xml";
public Mail() {
InputStream is = getClass().getClassLoader().getResourceAsStream(propertiesFile);
properties = new Properties();
try{
properties.loadFromXML(is);
}catch(IOException ioe) {
}
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response sendMail(MailParameters mail) {
if(mail == null) {
return Response.status(Status.BAD_REQUEST).entity("{\"error}\":\"message not delivered, parameters empty.\"}").build();
}
if(mail != null && mail.isValid()) {
String mailFrom = (String)properties.getProperty("mail.from");
String mailReceiver = (String)properties.getProperty("mail.receiver");
//Session session = Session.getDefaultInstance(properties);
Properties mailProperties = System.getProperties();
Integer port = Integer.parseInt(properties.getProperty("mail.smtp.port"));
mailProperties.put("mail.smtp.socketFactory.port", port);
mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
mailProperties.put("mail.smtp.socketFactory.fallback", "false");
mailProperties.put("mail.smtp.starttls.enable", "true");
mailProperties.put("mail.smtp.host", (String)properties.getProperty("mail.smtp.host"));
mailProperties.put("mail.smtp.auth", (String)properties.getProperty("mail.smtp.auth") );
mailProperties.put("mail.smtp.from", (String)properties.getProperty("mail.from"));
mailProperties.put("mail.smtp.port", port.toString());
String username = (String)properties.getProperty("mail.user");
String password = (String)properties.getProperty("mail.password");
mailProperties.put("mail.smtp.socketFactory.port", port.toString());
mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
mailProperties.put("mail.smtp.socketFactory.fallback", "true");
mailProperties.put("mail.smtp.starttls.enable", "false");
Session session = Session.getDefaultInstance(mailProperties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
MimeMessage msg = new MimeMessage(session);
msg.setSubject("Geoportal Kontaktformular: "+mail.betreff);
msg.setFrom(new InternetAddress(mailFrom));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailReceiver));
msg.setText(
"Nabend.\n\n"+
"Jemand hat über die Geoportal-API (z.B. das Kontaktformular) eine Nachricht gesendet.\n\n"+
"Absender ist "+mail.email+"\n"+
"Datenschutz: "+(mail.datenschutz?"":"nicht ")+" akzeptiert\n"+
"Newsletter:"+(mail.newsletter?"":"nicht ")+" erwünscht.\n"+
"\n\n"+
"Nachricht: \n\n"+
mail.nachricht
);
Transport.send(msg);
}catch(MessagingException me){
System.out.println("Error sending mail: "+me.getMessage());
return Response.status(Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"der mailversand ging schief.\"}").build();
//return Response.status(Status.INTERNAL_SERVER_ERROR).entity("{\"error\":\"your message seems alright but we could not deliver it.\"}").build();
}
return Response.status(Status.OK).entity("{\"status\":\"sent\"}").build();
}else {
return Response.status(Status.BAD_REQUEST).entity("{\"error\":\"message not delivered, parameters invalid.\"}").build();
}
}
}
package de.transformationsstadt.geoportal.apiparameters;
import java.io.Serializable;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
public class MailParameters implements Serializable{
public String betreff;
public Boolean datenschutz;
public String email;
public String nachricht;
public String name;
public Boolean newsletter;
public boolean isValid() {
boolean valid=true;
try {
InternetAddress emailAddr = new InternetAddress(email);
emailAddr.validate();
}catch(AddressException ae) {
valid = false;
}
if(newsletter == null) {
newsletter = false;
}
if(datenschutz == null) {
datenschutz = false;
}
valid &= (name != null && !name.isEmpty());
valid &= (nachricht != null && !nachricht.isEmpty());
valid &= (datenschutz != null && datenschutz.equals(new Boolean(true)));
valid &= (betreff != null && !betreff.isEmpty());
return valid;
}
}
......@@ -19,7 +19,7 @@ import io.jsonwebtoken.SignatureAlgorithm;
*
*/
public class JwtUtil{
public final static String propertiesFile = "geoportal.properties";
public final static String propertiesFile = "geoportal.properties.xml";
private SignatureAlgorithm sigAlg = SignatureAlgorithm.HS512;
private String keyString;
......@@ -32,7 +32,7 @@ public class JwtUtil{
InputStream is = getClass().getClassLoader().getResourceAsStream(propertiesFile);
Properties properties = new Properties();
try{
properties.load(is);
properties.loadFromXML(is);
keyString = (String)properties.getProperty("jwt.key");
issuerPrefix = (String)properties.getProperty("jwt.issuerPrefix");
keyBytes = DatatypeConverter.parseBase64Binary(keyString);
......
......@@ -17,4 +17,6 @@ public interface KeyValuePairService extends ServiceInterface<KeyValuePair>{
public KeyValuePair get(Long tagId);
public void delete(KeyValuePair kvp);
}
......@@ -34,5 +34,7 @@ public class KeyValuePairServiceImpl extends GenericService<KeyValuePair> implem
return false;
}
public void delete(KeyValuePair kvp) {
dao.delete(kvp.getId());
}
}
jwt.key = i am a secret key and should be changed for deployment
jwt.issuerPrefix = geoportal
userRegistration.enabled = false
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="jwt.key">i am a secret key and should be changed for deployment</entry>
<entry key="jwt.issuerPrefix">geoportal</entry>
<entry key="userRegistration.enabled">false</entry>
<entry key="mail.smtp.host">your_smtp_host</entry>
<entry key="mail.from">your_mail_sender</entry>
<entry key="mail.receiver">your_mail_receiver</entry>
<entry key="mail.smtp.auth">true</entry>
<entry key="mail.smtp.port">465</entry>
<entry key="mail.user">your_mail_user</entry>
<entry key="mail.password">your_password</entry>
</properties>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment