diff --git a/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/ThisApplication.java b/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/ThisApplication.java index d90cf0b38f4cba7930e2ab631cc7ca6091d2b8d6..94744246865cdecbf8785f15a680df713c6736a1 100644 --- a/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/ThisApplication.java +++ b/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/ThisApplication.java @@ -4,6 +4,7 @@ import com.x.base.core.project.Context; import com.x.base.core.project.cache.CacheManager; import com.x.base.core.project.config.Config; import com.x.base.core.project.logger.LoggerFactory; +import com.x.organization.assemble.personal.jaxrs.exmail.QueueUpdateExmail; import com.x.organization.assemble.personal.schedule.DisableExpiredEmpower; public class ThisApplication { @@ -14,6 +15,8 @@ public class ThisApplication { protected static Context context; + public static final QueueUpdateExmail queueUpdateExmail = new QueueUpdateExmail(); + public static Context context() { return context; } @@ -23,6 +26,7 @@ public class ThisApplication { CacheManager.init(context.clazz().getSimpleName()); LoggerFactory.setLevel(Config.logLevel().x_organization_assemble_personal()); context.schedule(DisableExpiredEmpower.class, "0 0/20 * * * ?"); + context.startQueue(queueUpdateExmail); } catch (Exception e) { e.printStackTrace(); } diff --git a/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/jaxrs/exmail/ActionPost.java b/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/jaxrs/exmail/ActionPost.java index 1cd8ec22c36284f314ae9f51ded5765660006c2d..427b8e486fe67ab6f4db4d45d9e71821e377a234 100644 --- a/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/jaxrs/exmail/ActionPost.java +++ b/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/jaxrs/exmail/ActionPost.java @@ -1,140 +1,56 @@ package com.x.organization.assemble.personal.jaxrs.exmail; import java.io.StringReader; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.util.Objects; -import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.BooleanUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.w3c.dom.NodeList; import org.xml.sax.InputSource; -import com.x.base.core.container.EntityManagerContainer; -import com.x.base.core.container.factory.EntityManagerContainerFactory; -import com.x.base.core.entity.JpaObject; -import com.x.base.core.entity.annotation.CheckPersistType; import com.x.base.core.project.config.Config; -import com.x.base.core.project.exception.ExceptionEntityNotExist; import com.x.base.core.project.http.ActionResult; import com.x.base.core.project.http.EffectivePerson; import com.x.base.core.project.jaxrs.WrapBoolean; import com.x.base.core.project.logger.Logger; import com.x.base.core.project.logger.LoggerFactory; -import com.x.base.core.project.tools.DateTools; -import com.x.base.core.project.tools.StringTools; -import com.x.organization.assemble.personal.Business; -import com.x.organization.core.entity.Person; -import com.x.organization.core.entity.PersonAttribute; +import com.x.organization.assemble.personal.ThisApplication; class ActionPost extends BaseAction { private static Logger logger = LoggerFactory.getLogger(ActionPost.class); - ActionResult execute(EffectivePerson effectivePerson, String msg_signature, String timestamp, String nonce, + ActionResult execute(EffectivePerson effectivePerson, String msgSignature, String timestamp, String nonce, String body) throws Exception { - - logger.debug("腾讯企业邮收到,msg_signature:{}, timestamp:{}, nonce:{}, body:{}.", msg_signature, timestamp, nonce, - body); - if (!Config.exmail().getEnable()) { + ActionResult result = new ActionResult<>(); + if (BooleanUtils.isNotTrue(Config.exmail().getEnable())) { throw new ExceptionExmailDisable(); } - try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { - ActionResult result = new ActionResult<>(); - Business business = new Business(emc); - Wo wo = new Wo(); - wo.setValue(false); - - WXBizMsgCrypt crypt = new WXBizMsgCrypt(Config.exmail().getToken(), Config.exmail().getEncodingAesKey(), - Config.exmail().getCorpId()); - String msg = crypt.DecryptMsg(msg_signature, timestamp, nonce, body); - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - StringReader sr = new StringReader(msg); - InputSource is = new InputSource(sr); - Document document = db.parse(is); - Element root = document.getDocumentElement(); - - NodeList userIDNodeList = root.getElementsByTagName("UserID"); - NodeList toUserIDNodeList = root.getElementsByTagName("ToUserID"); + String msg = decrypt(msgSignature, timestamp, nonce, body); - if (userIDNodeList.getLength() != 0) { - /* 未读邮件提醒 */ - String mail = userIDNodeList.item(0).getTextContent(); - Person person = emc.firstEqual(Person.class, Person.mail_FIELDNAME, mail); - if (null == person) { - throw new ExceptionEntityNotExist(mail, Person.class); - } - emc.beginTransaction(PersonAttribute.class); - String unReadCount = root.getElementsByTagName("UnReadCount").item(0).getTextContent(); - this.updateNewCount(business, person, unReadCount); - emc.commit(); - } else if (toUserIDNodeList.getLength() != 0) { - /* 新邮件提醒 */ - String mail = toUserIDNodeList.item(0).getTextContent(); - Person person = emc.firstEqual(Person.class, Person.mail_FIELDNAME, mail); - if (null == person) { - throw new ExceptionEntityNotExist(mail, Person.class); - } - emc.beginTransaction(PersonAttribute.class); - String fromUser = root.getElementsByTagName("FromUser").item(0).getTextContent(); - String title = root.getElementsByTagName("Title").item(0).getTextContent(); - String time = root.getElementsByTagName("Time").item(0).getTextContent(); - String newCount = root.getElementsByTagName("NewCount").item(0).getTextContent(); - this.updateNewCount(business, person, newCount); - this.updateTitle(business, person, time, title, fromUser); - emc.commit(); - wo.setValue(true); - } - result.setData(wo); - return result; - } - } + ThisApplication.queueUpdateExmail.send(msg); - private void updateNewCount(Business business, Person person, String newCount) throws Exception { - PersonAttribute attribute = business.entityManagerContainer().firstEqualAndEqual(PersonAttribute.class, - PersonAttribute.name_FIELDNAME, Config.exmail().getPersonAttributeNewCountName(), - PersonAttribute.person_FIELDNAME, person.getId()); - if (null == attribute) { - attribute = new PersonAttribute(); - attribute.setPerson(person.getId()); - attribute.setName(Config.exmail().getPersonAttributeNewCountName()); - attribute.setAttributeList(new ArrayList()); - business.entityManagerContainer().persist(attribute, CheckPersistType.all); - } - attribute.getAttributeList().clear(); - attribute.getAttributeList().add(newCount); + Wo wo = new Wo(); + wo.setValue(true); + result.setData(wo); + return result; } - private void updateTitle(Business business, Person person, String time, String title, String fromUser) - throws Exception { - Date date = new Date(); - date.setTime(Long.parseLong(time) * 1000); - String text = DateTools.format(date, "MM-dd HH:mm") + " " + (StringUtils.isBlank(title) ? "..." : title) + " " - + fromUser; - text = StringTools.utf8SubString(text, JpaObject.length_255B); - PersonAttribute attribute = business.entityManagerContainer().firstEqualAndEqual(PersonAttribute.class, - PersonAttribute.name_FIELDNAME, Config.exmail().getPersonAttributeTitleName(), - PersonAttribute.person_FIELDNAME, person.getId()); - if (null == attribute) { - attribute = new PersonAttribute(); - attribute.setPerson(person.getId()); - attribute.setName(Config.exmail().getPersonAttributeTitleName()); - attribute.setAttributeList(new ArrayList()); - business.entityManagerContainer().persist(attribute, CheckPersistType.all); - } - List list = attribute.getAttributeList(); - list.add(text); - for (int i = list.size() - 1; i >= 20; i--) { - list.remove(i); - } + private String decrypt(String msgSignature, String timestamp, String nonce, String body) throws Exception { + WXBizMsgCrypt crypt = new WXBizMsgCrypt(Config.exmail().getToken(), Config.exmail().getEncodingAesKey(), + Config.exmail().getCorpId()); + String msg = crypt.DecryptMsg(msgSignature, timestamp, nonce, body); + logger.debug("腾讯企业邮收到,msg_signature:{}, timestamp:{}, nonce:{}, body:{}, msg:{}.", msgSignature, timestamp, + nonce, body, msg); + return msg; } public static class Wo extends WrapBoolean { + + private static final long serialVersionUID = 7769885660412690442L; + } } diff --git a/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/jaxrs/exmail/QueueUpdateExmail.java b/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/jaxrs/exmail/QueueUpdateExmail.java new file mode 100644 index 0000000000000000000000000000000000000000..a4b67acadd98968f3c91c6d17591fc733af07a15 --- /dev/null +++ b/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/jaxrs/exmail/QueueUpdateExmail.java @@ -0,0 +1,200 @@ +package com.x.organization.assemble.personal.jaxrs.exmail; + +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import com.x.base.core.container.EntityManagerContainer; +import com.x.base.core.container.factory.EntityManagerContainerFactory; +import com.x.base.core.entity.JpaObject; +import com.x.base.core.entity.annotation.CheckPersistType; +import com.x.base.core.project.config.Config; +import com.x.base.core.project.exception.ExceptionEntityNotExist; +import com.x.base.core.project.logger.Logger; +import com.x.base.core.project.logger.LoggerFactory; +import com.x.base.core.project.queue.AbstractQueue; +import com.x.base.core.project.tools.DateTools; +import com.x.base.core.project.tools.StringTools; +import com.x.organization.core.entity.Person; +import com.x.organization.core.entity.PersonAttribute; + +public class QueueUpdateExmail extends AbstractQueue { + + private static final String MSGTYPE_UNREAD = "UnRead"; + + private static final String MSGTYPE_MAIL = "Mail"; + + private static Logger logger = LoggerFactory.getLogger(QueueUpdateExmail.class); + + protected void execute(String xmlBody) throws Exception { + Element element = this.xml(xmlBody); + String msgType = this.msgType(element); + if (StringUtils.equalsIgnoreCase(msgType, MSGTYPE_UNREAD)) { + unread(element); + } else if (StringUtils.equalsIgnoreCase(msgType, MSGTYPE_MAIL)) { + mail(element); + } else { + logger.warn("unknown msgType:{}, body:{}.", msgType, element.toString()); + } + } + + private Element xml(String msg) throws SAXException, IOException, ParserConfigurationException { + try (StringReader sr = new StringReader(msg)) { + InputSource is = new InputSource(sr); + Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is); + return document.getDocumentElement(); + } + } + + private String msgType(Element root) { + NodeList nodes = root.getElementsByTagName("MsgType"); + return nodes.item(0).getTextContent(); + } + + private void unread(Element element) throws Exception { + String userId = this.userId(element); + String unreadCount = this.unreadCount(element); + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + Person person = emc.firstEqual(Person.class, Person.mail_FIELDNAME, userId); + if (null == person) { + throw new ExceptionEntityNotExist(userId, Person.class); + } + // 更新未读邮件数量 + PersonAttribute unreadAttribute = emc.firstEqualAndEqual(PersonAttribute.class, + PersonAttribute.name_FIELDNAME, Config.exmail().getPersonAttributeNewCountName(), + PersonAttribute.person_FIELDNAME, person.getId()); + emc.beginTransaction(PersonAttribute.class); + if (null == unreadAttribute) { + unreadAttribute = new PersonAttribute(); + unreadAttribute.setPerson(person.getId()); + unreadAttribute.setName(Config.exmail().getPersonAttributeNewCountName()); + unreadAttribute.setAttributeList(new ArrayList<>()); + emc.persist(unreadAttribute, CheckPersistType.all); + } + unreadAttribute.getAttributeList().clear(); + unreadAttribute.getAttributeList().add(unreadCount); + emc.commit(); + } + } + + private void mail(Element element) throws Exception { + String toUserId = this.toUserId(element); + String fromUser = this.fromUser(element); + String title = this.title(element); + Date time = this.time(element); + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + Person person = emc.firstEqual(Person.class, Person.mail_FIELDNAME, toUserId); + if (null == person) { + throw new ExceptionEntityNotExist(toUserId, Person.class); + } + String text = fromUser + "(" + DateTools.format(time, "MM-dd HH:mm") + ")"; + text = StringTools.utf8SubString(title, JpaObject.length_255B - StringTools.utf8Length(text)) + text; + emc.beginTransaction(PersonAttribute.class); + // 更新新邮件标题 + PersonAttribute titleAttribute = emc.firstEqualAndEqual(PersonAttribute.class, + PersonAttribute.name_FIELDNAME, Config.exmail().getPersonAttributeTitleName(), + PersonAttribute.person_FIELDNAME, person.getId()); + if (null == titleAttribute) { + titleAttribute = new PersonAttribute(); + titleAttribute.setPerson(person.getId()); + titleAttribute.setName(Config.exmail().getPersonAttributeTitleName()); + titleAttribute.setAttributeList(new ArrayList<>()); + emc.persist(titleAttribute, CheckPersistType.all); + } + List list = titleAttribute.getAttributeList(); + + LinkedList queue = new LinkedList<>(list); + queue.push(text); + + titleAttribute.getAttributeList().clear(); + + for (int i = 0; i < Math.min(30, queue.size()); i++) { + titleAttribute.getAttributeList().add(queue.get(i)); + } + // 更新未读邮件数量 + PersonAttribute unreadAttribute = emc.firstEqualAndEqual(PersonAttribute.class, + PersonAttribute.name_FIELDNAME, Config.exmail().getPersonAttributeNewCountName(), + PersonAttribute.person_FIELDNAME, person.getId()); + if (null == unreadAttribute) { + unreadAttribute = new PersonAttribute(); + unreadAttribute.setPerson(person.getId()); + unreadAttribute.setName(Config.exmail().getPersonAttributeNewCountName()); + unreadAttribute.setAttributeList(new ArrayList<>()); + emc.persist(unreadAttribute, CheckPersistType.all); + } + int count = 1; + if (!unreadAttribute.getAttributeList().isEmpty()) { + count += NumberUtils.toInt(unreadAttribute.getAttributeList().get(0), 0); + unreadAttribute.getAttributeList().clear(); + } + unreadAttribute.getAttributeList().add(count + ""); + emc.commit(); + } + } + + private String toUserId(Element root) { + NodeList nodes = root.getElementsByTagName("ToUserID"); + return nodes.item(0).getTextContent(); + } + + private String fromUser(Element root) { + NodeList nodes = root.getElementsByTagName("FromUser"); + return nodes.item(0).getTextContent(); + } + + private String title(Element root) { + NodeList nodes = root.getElementsByTagName("Title"); + return nodes.item(0).getTextContent(); + } + + private String newCount(Element root) { + NodeList nodes = root.getElementsByTagName("NewCount"); + return nodes.item(0).getTextContent(); + } + + private String userId(Element root) { + NodeList nodes = root.getElementsByTagName("UserID"); + return nodes.item(0).getTextContent(); + } + + private String unreadCount(Element root) { + NodeList nodes = root.getElementsByTagName("UnReadCount"); + return nodes.item(0).getTextContent(); + } + + private Date time(Element root) { + NodeList nodes = root.getElementsByTagName("Time"); + String text = nodes.item(0).getTextContent(); + return new Date(Long.parseLong(text) * 1000); + } + + /** + * + * + * + * + * ]]> + * <![CDATA[邮件到达了]]> + * 11
+ *
+ * + * + * + * 10
+ */ + +} diff --git a/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/jaxrs/reset/ActionReset.java b/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/jaxrs/reset/ActionReset.java index 2a2161954158523efd0e193be37321fb19636b9e..cf26afabab5e98da3d25ca0c14036875f51dbd63 100644 --- a/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/jaxrs/reset/ActionReset.java +++ b/o2server/x_organization_assemble_personal/src/main/java/com/x/organization/assemble/personal/jaxrs/reset/ActionReset.java @@ -9,6 +9,7 @@ import com.google.gson.JsonElement; import com.x.base.core.container.EntityManagerContainer; import com.x.base.core.container.factory.EntityManagerContainerFactory; import com.x.base.core.entity.annotation.CheckPersistType; +import com.x.base.core.project.annotation.FieldDescribe; import com.x.base.core.project.config.Config; import com.x.base.core.project.exception.ExceptionWhen; import com.x.base.core.project.gson.GsonPropertyObject; @@ -54,7 +55,7 @@ class ActionReset extends BaseAction { if (!password.matches(Config.person().getPasswordRegex())) { throw new ExceptionInvalidPassword(Config.person().getPasswordRegexHint()); } - if (!business.instrument().code().validate(person.getMobile(), codeAnswer)) { + if (BooleanUtils.isFalse(business.instrument().code().validate(person.getMobile(), codeAnswer))) { throw new ExceptionInvalidCode(); } } @@ -72,8 +73,12 @@ class ActionReset extends BaseAction { public static class Wi extends GsonPropertyObject { + private static final long serialVersionUID = -575178712890850120L; + @FieldDescribe("用户标识") private String credential; + @FieldDescribe("密码") private String password; + @FieldDescribe("认证码") private String codeAnswer; public String getCredential() { @@ -104,6 +109,8 @@ class ActionReset extends BaseAction { public static class Wo extends WrapBoolean { + private static final long serialVersionUID = 7576056375014210316L; + } } \ No newline at end of file