diff --git a/pom.xml b/pom.xml index 731ccfc951c5fc345dee53d5a75e9c164836f067..1dce8a8ddae448a08eb9d04d75847638ad966530 100644 --- a/pom.xml +++ b/pom.xml @@ -177,6 +177,12 @@ <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.1</version> +</dependency> +<dependency> + <groupId>org.postgresql</groupId> + <artifactId>postgresql</artifactId> + <version>9.4-1206-jdbc42</version> + <scope>provided</scope> </dependency> </dependencies> diff --git a/src/main/java/no/nibio/vips/logic/messaging/MessageRecipient.java b/src/main/java/no/nibio/vips/logic/messaging/MessageRecipient.java new file mode 100644 index 0000000000000000000000000000000000000000..9d229880eca1ef256110d5f8105b219cbe3753d3 --- /dev/null +++ b/src/main/java/no/nibio/vips/logic/messaging/MessageRecipient.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSLogic. + * VIPSLogic is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSLogic 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 + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.logic.messaging; + +/** + * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +public class MessageRecipient { + private String type; + private String msgDeliveryAddress; + private String name; + private String recipientId; + + public MessageRecipient( + String recipientId, + String name, + String type, + String msgDeliveryAddress + ) + { + this.recipientId = recipientId; + this.name = name; + this.type=type; + this.msgDeliveryAddress = msgDeliveryAddress; + } + + /** + * @return the type + */ + public String getType() { + return type; + } + + /** + * @param type the type to set + */ + public void setType(String type) { + this.type = type; + } + + /** + * @return the msgDeliveryAddress + */ + public String getMsgDeliveryAddress() { + return msgDeliveryAddress; + } + + /** + * @param msgDeliveryAddress the msgDeliveryAddress to set + */ + public void setMsgDeliveryAddress(String msgDeliveryAddress) { + this.msgDeliveryAddress = msgDeliveryAddress; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the recipientId + */ + public String getRecipientId() { + return recipientId; + } + + /** + * @param recipientId the recipientId to set + */ + public void setRecipientId(String recipientId) { + this.recipientId = recipientId; + } +} diff --git a/src/main/java/no/nibio/vips/logic/messaging/MessagingBean.java b/src/main/java/no/nibio/vips/logic/messaging/MessagingBean.java new file mode 100644 index 0000000000000000000000000000000000000000..f4e55ca3f42266b23445cf65c0331a0de7d21b7e --- /dev/null +++ b/src/main/java/no/nibio/vips/logic/messaging/MessagingBean.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSLogic. + * VIPSLogic is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSLogic 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 + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.logic.messaging; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import no.nibio.vips.logic.entity.Message; +import no.nibio.vips.logic.entity.MessageLocale; +import no.nibio.vips.logic.entity.VipsLogicUser; +import no.nibio.vips.logic.util.SessionControllerGetter; + +/** + * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +@Stateless +public class MessagingBean { + @PersistenceContext(unitName="VIPSLogic-PU") + EntityManager em; + + public void testStoreUniversalMessage() + { + List<VipsLogicUser> allUsers=SessionControllerGetter.getUserBean().getAllUsers(); + Message m = SessionControllerGetter.getMessageBean().getMessage(5); + MessageLocale ml = m.getLocalMessage("nn"); + UniversalMessage um = new UniversalMessage( + ml.getHeading(), ml.getLeadParagraph(), ml.getBody(), "http://localhost:8000/messages/" + m.getMessageId() + ); + um.setExpiresAt(new Date()); + List<MessageRecipient> distributionList = new ArrayList<>(); + for(VipsLogicUser user : allUsers) + { + MessageRecipient r = new MessageRecipient( + String.valueOf(user.getUserId()), + user.getFirstName() + " " + user.getLastName(), + "Mail", + user.getEmail() + ); + distributionList.add(r); + } + um.setDistributionList(distributionList); + //em.persist(um); + + List<UniversalMessage> ums = em.createNamedQuery("UniversalMessage.findAll").getResultList(); + for(UniversalMessage uMe : ums) + { + System.out.println(uMe.getMsgSubject()); + } + } +} diff --git a/src/main/java/no/nibio/vips/logic/messaging/UniversalMessage.java b/src/main/java/no/nibio/vips/logic/messaging/UniversalMessage.java new file mode 100644 index 0000000000000000000000000000000000000000..f93819584030cfad72cf3b0d45b36d817130f015 --- /dev/null +++ b/src/main/java/no/nibio/vips/logic/messaging/UniversalMessage.java @@ -0,0 +1,210 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSLogic. + * VIPSLogic is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSLogic 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 + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.logic.messaging; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonRawValue; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.Serializable; +import java.util.Date; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Convert; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlRootElement; +import no.nibio.vips.logic.util.PostgresJSONStringConverter; +import no.nibio.vips.logic.util.StringJsonUserType; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.TypeDefs; + +/** + * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +@Entity +@Table(name = "universal_message", schema = "messaging") +@XmlRootElement +@TypeDefs( {@TypeDef( name= "StringJsonObject", typeClass = StringJsonUserType.class)}) +@NamedQueries({ + @NamedQuery(name = "UniversalMessage.findAll", query = "SELECT u FROM UniversalMessage u"), + @NamedQuery(name = "UniversalMessage.findByUniversalMessageId", query = "SELECT u FROM UniversalMessage u WHERE u.universalMessageId = :universalMessageId"), + @NamedQuery(name = "UniversalMessage.findByMsgSubject", query = "SELECT u FROM UniversalMessage u WHERE u.msgSubject = :msgSubject"), + @NamedQuery(name = "UniversalMessage.findByMsgLeadParagraph", query = "SELECT u FROM UniversalMessage u WHERE u.msgLeadParagraph = :msgLeadParagraph"), + @NamedQuery(name = "UniversalMessage.findByMsgBody", query = "SELECT u FROM UniversalMessage u WHERE u.msgBody = :msgBody"), + @NamedQuery(name = "UniversalMessage.findByMsgDownloadUrl", query = "SELECT u FROM UniversalMessage u WHERE u.msgDownloadUrl = :msgDownloadUrl"), + @NamedQuery(name = "UniversalMessage.findByExpiresAt", query = "SELECT u FROM UniversalMessage u WHERE u.expiresAt = :expiresAt")}) +public class UniversalMessage implements Serializable { + private static final long serialVersionUID = 1L; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Basic(optional = false) + @Column(name = "universal_message_id") + private Integer universalMessageId; + @Size(max = 2047) + @Column(name = "msg_subject") + private String msgSubject; + @Size(max = 2147483647) + @Column(name = "msg_lead_paragraph") + private String msgLeadParagraph; + @Size(max = 2147483647) + @Column(name = "msg_body") + private String msgBody; + @Size(max = 1023) + @Column(name = "msg_download_url") + private String msgDownloadUrl; + @Column(name = "expires_at") + @Temporal(TemporalType.TIMESTAMP) + private Date expiresAt; + //@Column(name = "distribution_list", columnDefinition = "json") + //@Convert(converter = PostgresJSONStringConverter.class) + // Documentation on StackOverflow: http://stackoverflow.com/questions/15974474/mapping-postgresql-json-column-to-hibernate-value-type + @Type(type = "StringJsonObject") + @Column(name = "distribution_list") + private String distributionList; + + + public UniversalMessage() { + } + + public UniversalMessage( + String msgSubject, + String msgLeadParagraph, + String msgBody, + String msgDownloadUrl + ) + { + this.msgSubject = msgSubject; + this.msgLeadParagraph = msgLeadParagraph; + this.msgBody = msgBody; + this.msgDownloadUrl = msgDownloadUrl; + } + + public UniversalMessage(Integer universalMessageId) { + this.universalMessageId = universalMessageId; + } + + @JsonIgnore + public Integer getUniversalMessageId() { + return universalMessageId; + } + + public void setUniversalMessageId(Integer universalMessageId) { + this.universalMessageId = universalMessageId; + } + + public String getMsgSubject() { + return msgSubject; + } + + public void setMsgSubject(String msgSubject) { + this.msgSubject = msgSubject; + } + + public String getMsgLeadParagraph() { + return msgLeadParagraph; + } + + public void setMsgLeadParagraph(String msgLeadParagraph) { + this.msgLeadParagraph = msgLeadParagraph; + } + + public String getMsgBody() { + return msgBody; + } + + public void setMsgBody(String msgBody) { + this.msgBody = msgBody; + } + + public String getMsgDownloadUrl() { + return msgDownloadUrl; + } + + public void setMsgDownloadUrl(String msgDownloadUrl) { + this.msgDownloadUrl = msgDownloadUrl; + } + + @JsonIgnore + public Date getExpiresAt() { + return expiresAt; + } + + public void setExpiresAt(Date expiresAt) { + this.expiresAt = expiresAt; + } + + @JsonRawValue + public String getDistributionList() { + return distributionList; + } + + public void setDistributionList(String distributionList) { + this.distributionList = distributionList; + } + + public void setDistributionList(List<MessageRecipient> distributionList) { + try { + this.distributionList = new ObjectMapper().writeValueAsString(distributionList); + } catch (JsonProcessingException ex) { + Logger.getLogger(UniversalMessage.class.getName()).log(Level.SEVERE, null, ex); + } + } + + @Override + public int hashCode() { + int hash = 0; + hash += (universalMessageId != null ? universalMessageId.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof UniversalMessage)) { + return false; + } + UniversalMessage other = (UniversalMessage) object; + if ((this.universalMessageId == null && other.universalMessageId != null) || (this.universalMessageId != null && !this.universalMessageId.equals(other.universalMessageId))) { + return false; + } + return true; + } + + @Override + public String toString() { + return "no.nibio.vips.logic.messaging.UniversalMessage[ universalMessageId=" + universalMessageId + " ]"; + } + +} diff --git a/src/main/java/no/nibio/vips/logic/messaging/UniversalMessagingServiceClient.java b/src/main/java/no/nibio/vips/logic/messaging/UniversalMessagingServiceClient.java new file mode 100644 index 0000000000000000000000000000000000000000..91eb2526d2c22fac7249420ddd889bc0f03ab9c0 --- /dev/null +++ b/src/main/java/no/nibio/vips/logic/messaging/UniversalMessagingServiceClient.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSLogic. + * VIPSLogic is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSLogic 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 + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.logic.messaging; + +import javax.ws.rs.Consumes; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Response; + +/** + * @copyright 2015 <a href="http://www.bioforsk.no/">Bioforsk</a> + * @author Tor-Einar Skog <tor-einar.skog@bioforsk.no> + */ +@Path("/") +public interface UniversalMessagingServiceClient { + + @POST + @Path("vips_msg_ws_01/rest/send_vips_msg") + @Consumes("application/json;charset=UTF-8") + @Produces("application/json;charset=UTF-8") + public Response sendMessage(UniversalMessage universalMessage); + + @POST + @Path("vips_msg_ws_01/rest/send_vips_msg") + @Consumes("application/json;charset=UTF-8") + @Produces("application/json;charset=UTF-8") + public Response sendMessage(String universalMessage); +} diff --git a/src/main/java/no/nibio/vips/logic/util/JsonPostgreSQLDialect.java b/src/main/java/no/nibio/vips/logic/util/JsonPostgreSQLDialect.java new file mode 100644 index 0000000000000000000000000000000000000000..ec41d5de02b55b86c9b203fbf48c96c598c86b30 --- /dev/null +++ b/src/main/java/no/nibio/vips/logic/util/JsonPostgreSQLDialect.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSLogic. + * VIPSLogic is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSLogic 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 + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.logic.util; + +import java.sql.Types; +import org.hibernate.dialect.PostgreSQL9Dialect; + +/** + * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +public class JsonPostgreSQLDialect extends PostgreSQL9Dialect{ + public JsonPostgreSQLDialect() { + + super(); + + this.registerColumnType(Types.JAVA_OBJECT, "json"); + } +} diff --git a/src/main/java/no/nibio/vips/logic/util/PostgresJSONStringConverter.java b/src/main/java/no/nibio/vips/logic/util/PostgresJSONStringConverter.java new file mode 100644 index 0000000000000000000000000000000000000000..9d8e51ca5f330ff9285f5b0cc443e7d120a1fb48 --- /dev/null +++ b/src/main/java/no/nibio/vips/logic/util/PostgresJSONStringConverter.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSLogic. + * VIPSLogic is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSLogic 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 + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.logic.util; + +import java.sql.SQLException; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; +import org.postgresql.util.PGobject; + +/** + * NOT IN USE. Does not work with current configuration. Use StringJsonUserType instead + * Based on this example: http://stackoverflow.com/questions/20339580/are-jpa-eclipselink-custom-types-possible/26126168#26126168 + * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +@Converter +public class PostgresJSONStringConverter implements AttributeConverter<String, PGobject> { + + @Override + public PGobject convertToDatabaseColumn(String x) { + System.out.println("I'm being used, actually!!!! JSON = " + x); + + try { + PGobject po = new PGobject(); + // here we tell Postgres to use JSON as type to treat our json + po.setType("json"); + // this is Jackson already added as dependency to project, it could be any JSON marshaller + po.setValue(x); + return po; + } catch (SQLException e) { + e.printStackTrace(); + System.out.println("convertToDatabaseColumn failed"); + return null; + } + } + + @Override + public String convertToEntityAttribute(PGobject y) { + return y.getValue(); + } + +} diff --git a/src/main/java/no/nibio/vips/logic/util/RESTAuthenticator.java b/src/main/java/no/nibio/vips/logic/util/RESTAuthenticator.java new file mode 100644 index 0000000000000000000000000000000000000000..0c2f05c28379136d4c4c0761b1e3df92ea43fd3a --- /dev/null +++ b/src/main/java/no/nibio/vips/logic/util/RESTAuthenticator.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSCommon. + * VIPSCommon is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSCommon 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 + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSCommon. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.logic.util; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.core.MultivaluedMap; +import javax.xml.bind.DatatypeConverter; + + +/** + * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +public class RESTAuthenticator implements ClientRequestFilter { + + private final String user; + private final String password; + + public RESTAuthenticator(String user, String password) { + this.user = user; + this.password = password; + } + + public void filter(ClientRequestContext requestContext) throws IOException { + MultivaluedMap<String, Object> headers = requestContext.getHeaders(); + final String basicAuthentication = getBasicAuthentication(); + headers.add("Authorization", basicAuthentication); + + } + + private String getBasicAuthentication() { + String token = this.user + ":" + this.password; + try { + return "Basic " + + DatatypeConverter.printBase64Binary(token.getBytes("UTF-8")); + } catch (UnsupportedEncodingException ex) { + throw new IllegalStateException("Cannot encode with UTF-8", ex); + } + } +} diff --git a/src/main/java/no/nibio/vips/logic/util/SessionControllerGetter.java b/src/main/java/no/nibio/vips/logic/util/SessionControllerGetter.java index 570654b903916ba6e151b7d87027d0b6f7bc0f9e..8b99f3d65ba45833845fbb6d8965e3adc6050a88 100644 --- a/src/main/java/no/nibio/vips/logic/util/SessionControllerGetter.java +++ b/src/main/java/no/nibio/vips/logic/util/SessionControllerGetter.java @@ -28,6 +28,7 @@ import javax.naming.NamingException; import no.nibio.vips.logic.controller.session.MessageBean; import no.nibio.vips.logic.controller.session.ObservationBean; import no.nibio.vips.logic.controller.session.OrganismBean; +import no.nibio.vips.logic.messaging.MessagingBean; /** * @copyright 2013-2014 <a href="http://www.nibio.no/">NIBIO</a> @@ -124,6 +125,20 @@ public class SessionControllerGetter { } } + public static MessagingBean getMessagingBean(){ + try + { + InitialContext ic = new InitialContext(); + MessagingBean retVal = (MessagingBean) ic.lookup(SessionControllerGetter.getJndiPath(MessagingBean.class)); + return retVal; + } + catch(NamingException ne) + { + System.out.println("Could not find " + MessagingBean.class.getSimpleName()); + return null; + } + } + public static ObservationBean getObservationBean() { try diff --git a/src/main/java/no/nibio/vips/logic/util/StringJsonUserType.java b/src/main/java/no/nibio/vips/logic/util/StringJsonUserType.java new file mode 100644 index 0000000000000000000000000000000000000000..92286a7aa42f40ebe0c3dafa68207456272b8281 --- /dev/null +++ b/src/main/java/no/nibio/vips/logic/util/StringJsonUserType.java @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSLogic. + * VIPSLogic is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSLogic 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 + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.logic.util; + +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; +import org.hibernate.HibernateException; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.usertype.UserType; + +/** + * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ + +public class StringJsonUserType implements UserType { + + /** + * Return the SQL type codes for the columns mapped by this type. The + * codes are defined on <tt>java.sql.Types</tt>. + * + * @return int[] the typecodes + * @see java.sql.Types + */ + @Override + public int[] sqlTypes() { + return new int[] { Types.JAVA_OBJECT}; + } + + /** + * The class returned by <tt>nullSafeGet()</tt>. + * + * @return Class + */ + @Override + public Class returnedClass() { + return String.class; + } + + /** + * Compare two instances of the class mapped by this type for persistence "equality". + * Equality of the persistent state. + * + * @param x + * @param y + * @return boolean + */ + @Override + public boolean equals(Object x, Object y) throws HibernateException { + + if( x== null){ + + return y== null; + } + + return x.equals( y); + } + + /** + * Get a hashcode for the instance, consistent with persistence "equality" + */ + @Override + public int hashCode(Object x) throws HibernateException { + + return x.hashCode(); + } + + /** + * Retrieve an instance of the mapped class from a JDBC resultset. Implementors + * should handle possibility of null values. + * + * @param rs a JDBC result set + * @param names the column names + * @param session + * @param owner the containing entity @return Object + * @throws org.hibernate.HibernateException + * + * @throws java.sql.SQLException + */ + @Override + public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { + if(rs.getString(names[0]) == null){ + return null; + } + return rs.getString(names[0]); + } + + /** + * Write an instance of the mapped class to a prepared statement. Implementors + * should handle possibility of null values. A multi-column type should be written + * to parameters starting from <tt>index</tt>. + * + * @param st a JDBC prepared statement + * @param value the object to write + * @param index statement parameter index + * @param session + * @throws org.hibernate.HibernateException + * + * @throws java.sql.SQLException + */ + @Override + public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { + if (value == null) { + st.setNull(index, Types.OTHER); + return; + } + + st.setObject(index, value, Types.OTHER); + } + + /** + * Return a deep copy of the persistent state, stopping at entities and at + * collections. It is not necessary to copy immutable objects, or null + * values, in which case it is safe to simply return the argument. + * + * @param value the object to be cloned, which may be null + * @return Object a copy + */ + @Override + public Object deepCopy(Object value) throws HibernateException { + + return value; + } + + /** + * Are objects of this type mutable? + * + * @return boolean + */ + @Override + public boolean isMutable() { + return true; + } + + /** + * Transform the object into its cacheable representation. At the very least this + * method should perform a deep copy if the type is mutable. That may not be enough + * for some implementations, however; for example, associations must be cached as + * identifier values. (optional operation) + * + * @param value the object to be cached + * @return a cachable representation of the object + * @throws org.hibernate.HibernateException + * + */ + @Override + public Serializable disassemble(Object value) throws HibernateException { + return (String)this.deepCopy( value); + } + + /** + * Reconstruct an object from the cacheable representation. At the very least this + * method should perform a deep copy if the type is mutable. (optional operation) + * + * @param cached the object to be cached + * @param owner the owner of the cached object + * @return a reconstructed object from the cachable representation + * @throws org.hibernate.HibernateException + * + */ + @Override + public Object assemble(Serializable cached, Object owner) throws HibernateException { + return this.deepCopy( cached); + } + + /** + * During merge, replace the existing (target) value in the entity we are merging to + * with a new (original) value from the detached entity we are merging. For immutable + * objects, or null values, it is safe to simply return the first parameter. For + * mutable objects, it is safe to return a copy of the first parameter. For objects + * with component values, it might make sense to recursively replace component values. + * + * @param original the value from the detached entity being merged + * @param target the value in the managed entity + * @return the value to be merged + */ + @Override + public Object replace(Object original, Object target, Object owner) throws HibernateException { + return original; + } + + +} diff --git a/src/main/setup/jboss-ds.xml b/src/main/setup/jboss-ds.xml index d3623fe9d8db480415c5223daa4f8772833091e2..7efd3dba56f87b794258307a8ed27c7042ae6042 100644 --- a/src/main/setup/jboss-ds.xml +++ b/src/main/setup/jboss-ds.xml @@ -20,4 +20,14 @@ <max-pool-size>20</max-pool-size> <idle-timeout-minutes>5</idle-timeout-minutes> </local-tx-datasource> + <local-tx-datasource> + <jndi-name>MessagingSchema</jndi-name> + <connection-url>jdbc:postgresql://localhost/vipslogic</connection-url> + <driver-class>org.postgresql.Driver</driver-class> + <user-name>vipslogic</user-name> + <password>VIPS123</password> + <min-pool-size>5</min-pool-size> + <max-pool-size>20</max-pool-size> + <idle-timeout-minutes>5</idle-timeout-minutes> + </local-tx-datasource> </datasources> diff --git a/src/test/java/no/nibio/vips/logic/messaging/UniversalMessagingTest.java b/src/test/java/no/nibio/vips/logic/messaging/UniversalMessagingTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a3f41fce093d964c51d3972fc91d40c9140bb469 --- /dev/null +++ b/src/test/java/no/nibio/vips/logic/messaging/UniversalMessagingTest.java @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSLogic. + * VIPSLogic is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSLogic 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 + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. + * + */ +package no.nibio.vips.logic.messaging; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import javax.ws.rs.core.Response; +import no.nibio.vips.logic.util.RESTAuthenticator; +import org.jboss.resteasy.client.jaxrs.ResteasyClient; +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author treinar + */ +public class UniversalMessagingTest { + + public UniversalMessagingTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + // TODO add test methods here. + // The methods must be annotated with annotation @Test. For example: + // + // @Test + // public void hello() {} + + @Test + public void serviceTest() + { + System.out.println("serviceTest"); + String result = null; + try + { + UniversalMessage um = new UniversalMessage( + "Bladveps funnet på Kyrksæterøra", + "Se opp for svermende veps med brodder", + "Lorem ipsum Dolores Sanctum Nobliatarus Factorum des nomine in sanctus spiritu benedictus qui venit dies irae", + "http://www.nibio.no/" + ); + + + String[][] recipients = { + {"1","Tor-Einar Skog","Mail","tor-einar.skog@nibio.no"}, + {"1","Tor-Einar Skog","Sms","+47 91303819"}, + {"2","Lars Aksel Opsahl", "Mail","lars.opsahl@nibio.no"} + }; + + List<MessageRecipient> rList = new ArrayList<>(); + for(String[] recipient:recipients) + { + rList.add(new MessageRecipient( + recipient[0], + recipient[1], + recipient[2], + recipient[3] + ) + ); + } + um.setDistributionList(rList); + + ResteasyClient client = new ResteasyClientBuilder().build(); + client.register(new RESTAuthenticator("user", "userPass")); + ResteasyWebTarget target = client.target("http://kart13utv.ad.skogoglandskap.no:8080"); + UniversalMessagingServiceClient umClient = target.proxy(UniversalMessagingServiceClient.class); + + + System.out.println(new ObjectMapper().writeValueAsString(um)); + Response r = umClient.sendMessage(new ObjectMapper().writeValueAsString(um)); + + result = r.readEntity(String.class); + System.out.println(result); +//Response r = umClient.sendMessage(um); + } + catch(IOException | IllegalArgumentException | NullPointerException e) + { + e.printStackTrace(); + fail(e.getMessage()); + } + assertNotNull(result); + } +}