Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jh-boot
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jan Hrabal
jh-boot
Commits
c4a30006
Commit
c4a30006
authored
Mar 25, 2020
by
Jan Hrabal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wip
parent
a654450a
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
17 additions
and
390 deletions
+17
-390
pom.xml
pom.xml
+0
-6
JhBootApplication.java
src/main/java/com/jh/boot/JhBootApplication.java
+12
-8
Email.java
src/main/java/com/jh/boot/email/Email.java
+1
-22
EmailAttachment.java
src/main/java/com/jh/boot/email/EmailAttachment.java
+1
-40
EmailConfig.java
src/main/java/com/jh/boot/email/EmailConfig.java
+0
-13
EmailRecipient.java
src/main/java/com/jh/boot/email/EmailRecipient.java
+1
-42
EmailRepository.java
src/main/java/com/jh/boot/email/EmailRepository.java
+0
-66
EmailService.java
src/main/java/com/jh/boot/email/EmailService.java
+0
-1
JhMailConfig.java
src/main/java/com/jh/boot/email/JhMailConfig.java
+0
-15
LocalEmailService.java
src/main/java/com/jh/boot/email/LocalEmailService.java
+0
-175
RestEmailService.java
src/main/java/com/jh/boot/email/RestEmailService.java
+2
-1
AbstractHibernateRepository.java
...ain/java/com/jh/boot/jpa/AbstractHibernateRepository.java
+0
-1
No files found.
pom.xml
View file @
c4a30006
...
@@ -90,12 +90,6 @@
...
@@ -90,12 +90,6 @@
<scope>
provided
</scope>
<scope>
provided
</scope>
</dependency>
</dependency>
<!-- email configuration -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-mail
</artifactId>
</dependency>
<!-- handlebars -->
<!-- handlebars -->
<dependency>
<dependency>
<groupId>
com.github.jknack
</groupId>
<groupId>
com.github.jknack
</groupId>
...
...
src/main/java/com/jh/boot/JhBootApplication.java
View file @
c4a30006
package
com
.
jh
.
boot
;
package
com
.
jh
.
boot
;
import
com.jh.boot.template.*
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
com.jh.boot.email.LocalEmailService
;
import
com.jh.boot.security.utils.TemplateEmailAuthServiceListener
;
import
com.jh.boot.security.utils.TemplateEmailAuthServiceListener
;
import
com.jh.boot.template.HibernateLoaderTemplateService
;
import
com.jh.boot.template.HibernateTemplateLoader
;
import
com.jh.boot.template.TemplateEngine
;
import
com.jh.boot.template.TemplateLoader
;
import
com.jh.boot.template.TemplateService
;
import
com.jh.boot.template.TemplateValuesConfig
;
import
com.jh.boot.template.handlebars.HandlebarsTemplateEngine
;
import
com.jh.boot.template.handlebars.HandlebarsTemplateEngine
;
public
class
JhBootApplication
{
public
class
JhBootApplication
{
@Bean
//
@Bean
@ConditionalOnProperty
(
value
=
"email.local.enabled"
,
havingValue
=
"true"
,
matchIfMissing
=
true
)
//
@ConditionalOnProperty(value = "email.local.enabled", havingValue = "true", matchIfMissing = true)
public
LocalEmailService
emailService
()
{
//
public LocalEmailService emailService() {
return
new
LocalEmailService
();
//
return new LocalEmailService();
}
//
}
@Bean
@Bean
@ConditionalOnProperty
(
value
=
"auth.listener.email.enabled"
,
havingValue
=
"true"
,
matchIfMissing
=
tru
e
)
@ConditionalOnProperty
(
value
=
"auth.listener.email.enabled"
,
havingValue
=
"true"
,
matchIfMissing
=
fals
e
)
public
TemplateEmailAuthServiceListener
authServiceListener
()
{
public
TemplateEmailAuthServiceListener
authServiceListener
()
{
return
new
TemplateEmailAuthServiceListener
();
return
new
TemplateEmailAuthServiceListener
();
}
}
...
...
src/main/java/com/jh/boot/email/Email.java
View file @
c4a30006
...
@@ -2,64 +2,43 @@ package com.jh.boot.email;
...
@@ -2,64 +2,43 @@ package com.jh.boot.email;
import
java.util.Set
;
import
java.util.Set
;
import
javax.persistence.CascadeType
;
import
javax.persistence.Column
;
import
javax.persistence.Entity
;
import
javax.persistence.OneToMany
;
import
javax.persistence.Table
;
import
com.jh.boot.jpa.AbstractIdEntity
;
/**
/**
* The Class Email.
* The Class Email.
*/
*/
@Entity
public
class
Email
{
@Table
(
name
=
"EMAIL"
)
public
class
Email
extends
AbstractIdEntity
{
/** The locked. */
/** The locked. */
@Column
(
name
=
"LOCKED"
)
private
Boolean
locked
;
private
Boolean
locked
;
/** The email from. */
/** The email from. */
@Column
(
name
=
"EMAIL_FROM"
)
private
String
emailFrom
;
private
String
emailFrom
;
/** The recipients. */
/** The recipients. */
@OneToMany
(
mappedBy
=
"email"
,
cascade
=
CascadeType
.
ALL
)
private
Set
<
EmailRecipient
>
recipients
;
private
Set
<
EmailRecipient
>
recipients
;
/** The subject. */
/** The subject. */
@Column
(
name
=
"SUBJECT"
)
private
String
subject
;
private
String
subject
;
/** The text. */
/** The text. */
@Column
(
name
=
"TEXT_CONTENT"
)
private
String
text
;
private
String
text
;
/** The html. */
/** The html. */
@Column
(
name
=
"HTML_CONTENT"
)
private
String
html
;
private
String
html
;
/** The to be sent. */
/** The to be sent. */
@Column
(
name
=
"TO_BE_SENT"
)
private
Boolean
toBeSent
;
private
Boolean
toBeSent
;
/** The status. */
/** The status. */
@Column
(
name
=
"STATUS"
)
private
String
status
;
private
String
status
;
/** The error. */
/** The error. */
@Column
(
name
=
"ERROR"
)
private
String
error
;
private
String
error
;
/** The receipt. */
/** The receipt. */
@Column
(
name
=
"RECEIPT"
)
private
Boolean
receipt
;
private
Boolean
receipt
;
/** The attachments. */
/** The attachments. */
@OneToMany
(
mappedBy
=
"email"
,
cascade
=
CascadeType
.
ALL
)
private
Set
<
EmailAttachment
>
attachments
;
private
Set
<
EmailAttachment
>
attachments
;
/**
/**
...
...
src/main/java/com/jh/boot/email/EmailAttachment.java
View file @
c4a30006
...
@@ -7,45 +7,24 @@ package com.jh.boot.email;
...
@@ -7,45 +7,24 @@ package com.jh.boot.email;
import
java.util.Base64
;
import
java.util.Base64
;
import
java.util.Base64.Encoder
;
import
java.util.Base64.Encoder
;
import
javax.persistence.Column
;
import
javax.persistence.Entity
;
import
javax.persistence.JoinColumn
;
import
javax.persistence.ManyToOne
;
import
javax.persistence.Table
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
import
com.jh.boot.jpa.AbstractIdEntity
;
/**
/**
* The Class EmailAttachment.
* The Class EmailAttachment.
*
*
* @author Jan Hrabal
* @author Jan Hrabal
*/
*/
@Entity
public
class
EmailAttachment
{
@Table
(
name
=
"EMAIL_ATTACHMENT"
)
public
class
EmailAttachment
extends
AbstractIdEntity
{
/** The name. */
/** The name. */
@Column
(
name
=
"NAME"
)
private
String
name
;
private
String
name
;
/** The mime type. */
/** The mime type. */
@Column
(
name
=
"MIME_TYPE"
)
private
String
mimeType
;
private
String
mimeType
;
/** The data. */
/** The data. */
//base 64
//base 64
@Column
(
name
=
"DATA"
)
private
String
data
;
private
String
data
;
/** The email. */
@ManyToOne
@org
.
hibernate
.
annotations
.
NotFound
(
action
=
org
.
hibernate
.
annotations
.
NotFoundAction
.
IGNORE
)
@JoinColumn
(
name
=
"EMAIL_ID"
)
@JsonIgnore
private
Email
email
;
/** The cid. */
/** The cid. */
@Column
(
name
=
"CID"
)
private
String
cid
;
private
String
cid
;
/**
/**
...
@@ -118,24 +97,6 @@ public class EmailAttachment extends AbstractIdEntity {
...
@@ -118,24 +97,6 @@ public class EmailAttachment extends AbstractIdEntity {
}
}
/**
/**
* Gets the email.
*
* @return the email
*/
public
Email
getEmail
()
{
return
email
;
}
/**
* Sets the email.
*
* @param email the new email
*/
public
void
setEmail
(
Email
email
)
{
this
.
email
=
email
;
}
/**
* Gets the cid.
* Gets the cid.
*
*
* @return the cid
* @return the cid
...
...
src/main/java/com/jh/boot/email/EmailConfig.java
deleted
100644 → 0
View file @
a654450a
package
com
.
jh
.
boot
.
email
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.scheduling.annotation.EnableScheduling
;
/**
* The Class EmailConfig.
*/
@Configuration
@EnableScheduling
public
class
EmailConfig
{
}
src/main/java/com/jh/boot/email/EmailRecipient.java
View file @
c4a30006
package
com
.
jh
.
boot
.
email
;
package
com
.
jh
.
boot
.
email
;
import
javax.persistence.Column
;
import
javax.persistence.Entity
;
import
javax.persistence.EnumType
;
import
javax.persistence.Enumerated
;
import
javax.persistence.JoinColumn
;
import
javax.persistence.ManyToOne
;
import
javax.persistence.Table
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
import
com.jh.boot.jpa.AbstractIdEntity
;
/**
/**
* The Class EmailRecipient.
* The Class EmailRecipient.
*/
*/
@Entity
public
class
EmailRecipient
{
@Table
(
name
=
"EMAIL_RECIPIENT"
)
public
class
EmailRecipient
extends
AbstractIdEntity
{
/** The email. */
@JsonIgnore
@ManyToOne
@org
.
hibernate
.
annotations
.
NotFound
(
action
=
org
.
hibernate
.
annotations
.
NotFoundAction
.
IGNORE
)
@JoinColumn
(
name
=
"EMAIL_ID"
)
private
Email
email
;
/** The address. */
/** The address. */
@Column
(
name
=
"ADDRESS"
)
private
String
address
;
private
String
address
;
/** The recipient type. */
/** The recipient type. */
@Column
(
name
=
"RECIPIENT_TYPE"
)
@Enumerated
(
EnumType
.
STRING
)
private
RecipientType
recipientType
;
private
RecipientType
recipientType
;
/**
/**
...
@@ -51,25 +29,6 @@ public class EmailRecipient extends AbstractIdEntity {
...
@@ -51,25 +29,6 @@ public class EmailRecipient extends AbstractIdEntity {
this
.
recipientType
=
recipientType
;
this
.
recipientType
=
recipientType
;
}
}
/**
* Gets the email.
*
* @return the email
*/
public
Email
getEmail
()
{
return
email
;
}
/**
* Sets the email.
*
* @param email the new email
*/
public
void
setEmail
(
Email
email
)
{
this
.
email
=
email
;
}
/**
/**
* Gets the address.
* Gets the address.
*
*
...
...
src/main/java/com/jh/boot/email/EmailRepository.java
deleted
100644 → 0
View file @
a654450a
package
com
.
jh
.
boot
.
email
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.stream.Collectors
;
import
javax.persistence.Query
;
import
org.hibernate.query.NativeQuery
;
import
org.springframework.stereotype.Repository
;
import
com.jh.boot.jpa.AbstractHibernateRepository
;
/**
* The Class EmailRepository.
*/
@Repository
public
class
EmailRepository
extends
AbstractHibernateRepository
{
/**
* Fetch emails to send.
*
* @return the list
*/
@SuppressWarnings
(
"unchecked"
)
public
List
<
Email
>
fetchEmailsToSend
()
{
String
sql
=
"select ID from EMAIL where LOCKED<>true and TO_BE_SENT=true for update limit 100"
;
NativeQuery
<
Number
>
nq
=
nativeQuery
(
sql
);
//fetch ids
List
<
Number
>
result
=
nq
.
getResultList
();
List
<
Long
>
ids
=
result
.
stream
().
map
((
n
)
->
n
.
longValue
()).
collect
(
Collectors
.
toList
());
if
(!
ids
.
isEmpty
())
{
lockEmails
(
ids
,
true
);
Query
q
=
entityManager
.
createQuery
(
"select distinct e from Email e left join fetch e.attachments left join fetch e.recipients where e.id in (:ids)"
);
q
.
setParameter
(
"ids"
,
ids
);
return
q
.
getResultList
();
}
return
Collections
.
emptyList
();
}
/**
* Lock emails.
*
* @param ids the ids
* @param locked the locked
*/
public
void
lockEmails
(
Collection
<
Long
>
ids
,
boolean
locked
)
{
if
(
ids
==
null
||
ids
.
isEmpty
())
{
return
;
}
String
hql
=
"update Email e set e.locked=:locked where e.id in (:ids)"
;
Query
q
=
entityManager
.
createQuery
(
hql
);
q
.
setParameter
(
"locked"
,
locked
);
q
.
setParameter
(
"ids"
,
ids
);
q
.
executeUpdate
();
}
}
src/main/java/com/jh/boot/email/EmailService.java
View file @
c4a30006
package
com
.
jh
.
boot
.
email
;
package
com
.
jh
.
boot
.
email
;
/**
/**
* The Interface EmailService.
* The Interface EmailService.
*/
*/
...
...
src/main/java/com/jh/boot/email/JhMailConfig.java
deleted
100644 → 0
View file @
a654450a
package
com
.
jh
.
boot
.
email
;
import
org.springframework.context.annotation.Bean
;
//TODO
public
class
JhMailConfig
{
@Bean
public
EmailService
emailService
()
{
return
new
LocalEmailService
();
}
}
src/main/java/com/jh/boot/email/LocalEmailService.java
deleted
100644 → 0
View file @
a654450a
package
com
.
jh
.
boot
.
email
;
import
java.util.ArrayList
;
import
java.util.Base64
;
import
java.util.List
;
import
java.util.Objects
;
import
javax.mail.internet.MimeMessage
;
import
javax.transaction.Transactional
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.core.io.ByteArrayResource
;
import
org.springframework.mail.javamail.JavaMailSender
;
import
org.springframework.mail.javamail.MimeMessageHelper
;
import
org.springframework.util.StringUtils
;
/**
* The Class LocalEmailService.
*/
public
class
LocalEmailService
implements
EmailService
{
/** The Constant LOG. */
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
LocalEmailService
.
class
);
/** The repo. */
@Autowired
private
EmailRepository
repo
;
/** The mail sender. */
@Autowired
private
JavaMailSender
mailSender
;
/** The email sender address. */
@Value
(
"${email.sender.from:}"
)
private
String
emailSenderAddress
;
/**
* @param email
* @return
* @see com.jh.module.email.EmailService#sendEmail(com.jh.module.email.Email)
*/
@Override
@Transactional
public
Email
sendEmail
(
Email
email
)
{
email
.
setToBeSent
(
true
);
email
.
setLocked
(
false
);
if
(!
StringUtils
.
hasText
(
email
.
getEmailFrom
()))
{
email
.
setEmailFrom
(
emailSenderAddress
);
}
if
(
email
.
getRecipients
()
!=
null
)
{
for
(
EmailRecipient
er
:
email
.
getRecipients
())
{
er
.
setEmail
(
email
);
}
}
if
(
email
.
getAttachments
()
!=
null
)
{
for
(
EmailAttachment
ea
:
email
.
getAttachments
())
{
ea
.
setEmail
(
email
);
}
}
repo
.
save
(
email
);
//TODO
doSendEmail
(
email
);
return
email
;
}
// @Scheduled(fixedDelayString = "${localEmail.scheduled.interval.ms:120000}")
// @Transactional
// public void scheduled() {
// List<Email> emails = repo.fetchEmailsToSend();
// if (emails == null || emails.isEmpty()) {
// return;
// }
// LOG.info("Going to send {} emails", emails.size());
// for (Email email : emails) {
// doSendEmail(email);
// }
// }
/**
* TODO.
*
* @param email the email
*/
private
void
doSendEmail
(
Email
email
)
{
MimeMessage
msg
=
mailSender
.
createMimeMessage
();
try
{
MimeMessageHelper
helper
=
new
MimeMessageHelper
(
msg
,
true
);
//send receipt?
if
(
Objects
.
equals
(
email
.
getReceipt
(),
true
))
{
msg
.
addHeader
(
"Disposition-Notification-To"
,
email
.
getEmailFrom
());
}
//TODO add tracking-number
msg
.
addHeader
(
"X-Email-RefId"
,
String
.
valueOf
(
email
.
getId
()));
//use properly configured (reverse DNS) sender address?
if
(
StringUtils
.
hasText
(
emailSenderAddress
))
{
helper
.
setFrom
(
emailSenderAddress
);
if
(
StringUtils
.
hasText
(
email
.
getFrom
()))
{
helper
.
setReplyTo
(
email
.
getFrom
());
}
}
else
{
helper
.
setFrom
(
email
.
getFrom
());
}
List
<
String
>
to
=
new
ArrayList
<>();
List
<
String
>
cc
=
new
ArrayList
<>();
List
<
String
>
bcc
=
new
ArrayList
<>();
if
(
email
.
getRecipients
()
!=
null
)
{
for
(
EmailRecipient
r
:
email
.
getRecipients
())
{
RecipientType
rt
=
r
.
getRecipientType
();
switch
(
rt
)
{
case
BCC:
bcc
.
add
(
r
.
getAddress
());
break
;
case
CC:
cc
.
add
(
r
.
getAddress
());
break
;
case
TO:
default
:
to
.
add
(
r
.
getAddress
());
}
}
}
String
[]
s
=
new
String
[
0
];
helper
.
setTo
(
to
.
toArray
(
s
));
helper
.
setCc
(
cc
.
toArray
(
s
));
helper
.
setBcc
(
bcc
.
toArray
(
s
));
helper
.
setSubject
(
email
.
getSubject
());
helper
.
setText
(
email
.
getText
());
if
(
StringUtils
.
hasText
(
email
.
getHtml
()))
{
helper
.
setText
(
email
.
getHtml
(),
true
);
}
if
(
email
.
getAttachments
()
!=
null
)
{
for
(
EmailAttachment
a
:
email
.
getAttachments
())
{
if
(
a
.
getData
()
!=
null
)
{
byte
[]
bytes
=
Base64
.
getDecoder
().
decode
(
a
.
getData
().
getBytes
());
if
(
StringUtils
.
hasText
(
a
.
getCid
()))
{
helper
.
addInline
(
a
.
getCid
(),
new
ByteArrayResource
(
bytes
),
a
.
getMimeType
());
}
else
{
helper
.
addAttachment
(
a
.
getName
(),
new
ByteArrayResource
(
bytes
),
a
.
getMimeType
());
}
}
}
}
mailSender
.
send
(
msg
);
email
.
setStatus
(
"SENT"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
email
.
setStatus
(
"FAILED"
);
email
.
setError
(
e
.
getMessage
());
}
email
.
setToBeSent
(
false
);
email
.
setLocked
(
false
);
}
}
src/main/java/com/jh/boot/email/RestEmailService.java
View file @
c4a30006
...
@@ -11,6 +11,7 @@ import org.springframework.http.RequestEntity;
...
@@ -11,6 +11,7 @@ import org.springframework.http.RequestEntity;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.client.RestTemplate
;
/**
/**
* The Class RestEmailService.
* The Class RestEmailService.
*/
*/
...
@@ -49,7 +50,7 @@ public class RestEmailService implements EmailService {
...
@@ -49,7 +50,7 @@ public class RestEmailService implements EmailService {
/**
/**
* {@inheritDoc}
* {@inheritDoc}
* @see com.jh.module.email.EmailService#sendEmail(com.jh.
module
.email.Email)
* @see com.jh.module.email.EmailService#sendEmail(com.jh.
emailer.model
.email.Email)
*/
*/
@Override
@Override
public
Email
sendEmail
(
Email
email
)
{
public
Email
sendEmail
(
Email
email
)
{
...
...
src/main/java/com/jh/boot/jpa/AbstractHibernateRepository.java
View file @
c4a30006
...
@@ -16,7 +16,6 @@ import javax.persistence.PersistenceContext;
...
@@ -16,7 +16,6 @@ import javax.persistence.PersistenceContext;
import
javax.persistence.Query
;
import
javax.persistence.Query
;
import
javax.persistence.criteria.CriteriaBuilder
;
import
javax.persistence.criteria.CriteriaBuilder
;
import
javax.persistence.criteria.CriteriaQuery
;
import
javax.persistence.criteria.CriteriaQuery
;
import
javax.persistence.criteria.Predicate
;
import
javax.persistence.criteria.Root
;
import
javax.persistence.criteria.Root
;
import
org.hibernate.query.NativeQuery
;
import
org.hibernate.query.NativeQuery
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment