Skip to content

Commit

Permalink
Merge pull request #23 from support-project/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
koda-masaru committed Mar 26, 2015
2 parents 53137af + 5ea6494 commit ef675f2
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 83 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.support-project</groupId>
<artifactId>knowledge</artifactId>
<packaging>war</packaging>
<version>0.4.5</version>
<version>0.4.6</version>
<name>webapp for knowledge</name>
<url>https://support-project.org/</url>

Expand All @@ -17,7 +17,7 @@
<dependency>
<groupId>org.support-project</groupId>
<artifactId>web</artifactId>
<version>0.4.5</version>
<version>0.4.6</version>
</dependency>

<dependency>
Expand Down
183 changes: 105 additions & 78 deletions src/main/java/org/support/project/knowledge/bat/MailSendBat.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@ public class MailSendBat {
public static final int MAIL_STATUS_UNSENT = 0;
/** メールの状態:送信済 */
public static final int MAIL_STATUS_SENDED = 10;

/** メールの状態:なんらかのエラーが発生した */
public static final int MAIL_STATUS_ERROR = -1;
/** メールの状態:アドレスのフォーマットエラー */
public static final int MAIL_STATUS_FORMAT_ERROR = -2;

public static final String MAIL_FORMAT = "^[a-zA-Z0-9!#$%&'_`/=~\\*\\+\\-\\?\\^\\{\\|\\}]+(\\.[a-zA-Z0-9!#$%&'_`/=~\\*\\+\\-\\?\\^\\{\\|\\}]+)*"
+ "@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]*(\\.[a-zA-Z0-9\\-]+)*$";

public static void main(String[] args) throws Exception {
MailSendBat bat = new MailSendBat();
bat.start();

}

/**
* メール送信処理の実行
*
* @throws UnsupportedEncodingException
* @throws MessagingException
* @throws InvalidKeyException
Expand All @@ -58,99 +66,118 @@ public static void main(String[] args) throws Exception {
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public void start() throws UnsupportedEncodingException, MessagingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
public void start() throws UnsupportedEncodingException, MessagingException, InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
MailConfigsDao mailConfigsDao = MailConfigsDao.get();
MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(org.support.project.knowledge.config.AppConfig.SYSTEM_NAME);
if (mailConfigsEntity == null) {
// メールの設定が登録されていなければ、送信処理は終了
return;
}

MailsDao dao = MailsDao.get();
List<MailsEntity> entities = dao.selectOnStatus(MAIL_STATUS_UNSENT);
int count = 0;
for (MailsEntity mailsEntity : entities) {
mailSend(mailConfigsEntity, mailsEntity);
if (mailsEntity.getToAddress().matches(MAIL_FORMAT)) {
mailSend(mailConfigsEntity, mailsEntity);
} else {
mailsEntity.setStatus(MAIL_STATUS_FORMAT_ERROR);
dao.save(mailsEntity);
}
count++;
}
LOG.info("MAIL sended. count: " + count);
}

/**
* メールを送信
* @param config
*
* @param config
* @param entity
* @throws MessagingException
* @throws UnsupportedEncodingException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws MessagingException
* @throws UnsupportedEncodingException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
*/
private void mailSend(MailConfigsEntity config, MailsEntity entity) throws MessagingException, UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
String host = config.getHost();
String port = String.valueOf(config.getPort());

String to = entity.getToAddress();
String toName = entity.getToName();

String from = entity.getFromAddress();
String fromName = entity.getFromName();
if (StringUtils.isEmpty(from)) {
from = config.getFromAddress();
}
if (StringUtils.isEmpty(fromName)) {
fromName = config.getFromName();
}

String title = entity.getTitle();
String message = entity.getContent();

Properties property = new Properties();
property.put("mail.smtp.host", host);
property.put("mail.smtp.port", port);
property.put("mail.smtp.socketFactory.port", port);
property.put("mail.smtp.debug", "true");

Session session;
if (1 == config.getAuthType()) {
// 認証あり
final String smtpid = config.getSmtpId();
final String smtppass = PasswordUtil.decrypt(config.getSmtpPassword(), config.getSalt());

property.put("mail.smtp.auth", "true");
property.put("mail.smtp.starttls.enable", "true");
property.put("mail.smtp.ssl.trust", host);

session = Session.getInstance(property, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpid, smtppass);
}
});
} else {
// 認証無し
session = Session.getDefaultInstance(property);
}
private void mailSend(MailConfigsEntity config, MailsEntity entity) throws MessagingException, UnsupportedEncodingException, InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
try {
String host = config.getHost();
String port = String.valueOf(config.getPort());

MimeMessage mimeMessage = new MimeMessage(session);
InternetAddress toAddress = new InternetAddress(to, toName, "ISO-2022-JP");
mimeMessage.setRecipient(Message.RecipientType.TO, toAddress);
InternetAddress fromAddress = new InternetAddress(from, fromName, "ISO-2022-JP");
mimeMessage.setFrom(fromAddress);
mimeMessage.setSubject(title, "ISO-2022-JP");
mimeMessage.setText(message, "ISO-2022-JP");

// メールの形式を指定
// mimeMessage.setHeader( "Content-Type", "text/html" );
// 送信日付を指定
mimeMessage.setSentDate( new Date() );
Transport.send(mimeMessage);
LOG.debug("Mail sended.");

// ステータス更新
MailsDao dao = MailsDao.get();
entity.setStatus(MAIL_STATUS_SENDED);
dao.save(entity);
String to = entity.getToAddress();
String toName = entity.getToName();

String from = entity.getFromAddress();
String fromName = entity.getFromName();
if (StringUtils.isEmpty(from)) {
from = config.getFromAddress();
}
if (StringUtils.isEmpty(fromName)) {
fromName = config.getFromName();
}

String title = entity.getTitle();
String message = entity.getContent();

Properties property = new Properties();
property.put("mail.smtp.host", host);
property.put("mail.smtp.port", port);
property.put("mail.smtp.socketFactory.port", port);
property.put("mail.smtp.debug", "true");

Session session;
if (1 == config.getAuthType()) {
// 認証あり
final String smtpid = config.getSmtpId();
final String smtppass = PasswordUtil.decrypt(config.getSmtpPassword(), config.getSalt());

property.put("mail.smtp.auth", "true");
property.put("mail.smtp.starttls.enable", "true");
property.put("mail.smtp.ssl.trust", host);

session = Session.getInstance(property, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpid, smtppass);
}
});
} else {
// 認証無し
session = Session.getDefaultInstance(property);
}

MimeMessage mimeMessage = new MimeMessage(session);
InternetAddress toAddress = new InternetAddress(to, toName, "ISO-2022-JP");
mimeMessage.setRecipient(Message.RecipientType.TO, toAddress);
InternetAddress fromAddress = new InternetAddress(from, fromName, "ISO-2022-JP");
mimeMessage.setFrom(fromAddress);
mimeMessage.setSubject(title, "ISO-2022-JP");
mimeMessage.setText(message, "ISO-2022-JP");

// メールの形式を指定
// mimeMessage.setHeader( "Content-Type", "text/html" );
// 送信日付を指定
mimeMessage.setSentDate(new Date());

Transport.send(mimeMessage);
LOG.debug("Mail sended.");

// ステータス更新
MailsDao dao = MailsDao.get();
entity.setStatus(MAIL_STATUS_SENDED);
dao.save(entity);
} catch (Exception e) {
//TODO メール送信失敗、二度と送らないようにする(リトライする?)
// 未送信にしておけば、再送できるが永遠に再送してしまう
// カウント制御するべきか?
LOG.error("Mail send error", e);
MailsDao dao = MailsDao.get();
entity.setStatus(MAIL_STATUS_ERROR);
dao.save(entity);
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/appresource.properties
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ message.success.save.target={1} was Saved.
message.allready.updated=Allready updated.

# Common Label
label.version=0.4.5
label.version=0.4.6
label.login=Sign in
label.previous = Previous
label.next=Next
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/appresource_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ message.success.save.target={1} \u4fdd\u5b58\u3057\u307e\u3057\u305f\u3002
message.allready.updated=\u3059\u3067\u306b\u66f4\u65b0\u3055\u308c\u3066\u3044\u307e\u3059

# Common Label
label.version=0.4.5
label.version=0.4.6
label.login=\u30b5\u30a4\u30f3\u30a4\u30f3
label.previous = \u524d\u3078
label.next = \u6b21\u3078
Expand Down
8 changes: 7 additions & 1 deletion src/main/webapp/js/knowledge-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ $(document).ready(function() {
});
*/

$('.thumbnail').hover(function() {
$(this).css('border', '1px solid gray');
}, function() {
$(this).css('border', '0px solid #ccc');
});

$('#input_tags').on('beforeItemRemove', function(event) {
event.cancel = true;
});
echo.init();
});

var showKnowledge = function(id, offset, keyword, tag, user) {
$('#discription_' + id).slideDown(20);
//$('#discription_' + id).slideDown(20);
var url = _CONTEXT + '/open.knowledge/view/' + id;

var param = '';
Expand Down

0 comments on commit ef675f2

Please sign in to comment.