You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openmeetings.apache.org by so...@apache.org on 2018/04/01 05:15:05 UTC

[openmeetings] branch 4.0.x updated: [OPENMEETINGS-1862] rss loading seems to be fixed

This is an automated email from the ASF dual-hosted git repository.

solomax pushed a commit to branch 4.0.x
in repository https://gitbox.apache.org/repos/asf/openmeetings.git


The following commit(s) were added to refs/heads/4.0.x by this push:
     new 1c2c0bd  [OPENMEETINGS-1862] rss loading seems to be fixed
1c2c0bd is described below

commit 1c2c0bd94060444ac1f06cd33663feb77cb7aecb
Author: Maxim Solodovnik <so...@gmail.com>
AuthorDate: Sun Apr 1 12:07:58 2018 +0700

    [OPENMEETINGS-1862] rss loading seems to be fixed
---
 .../service/quartz/scheduler/AtomReader.java       | 210 +++++++++++++++++++++
 .../service/quartz/scheduler/ReminderJob.java      |  62 +-----
 2 files changed, 212 insertions(+), 60 deletions(-)

diff --git a/openmeetings-service/src/main/java/org/apache/openmeetings/service/quartz/scheduler/AtomReader.java b/openmeetings-service/src/main/java/org/apache/openmeetings/service/quartz/scheduler/AtomReader.java
new file mode 100644
index 0000000..811b9d7
--- /dev/null
+++ b/openmeetings-service/src/main/java/org/apache/openmeetings/service/quartz/scheduler/AtomReader.java
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License") +  you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openmeetings.service.quartz.scheduler;
+
+import static javax.xml.stream.XMLInputFactory.IS_NAMESPACE_AWARE;
+import static javax.xml.stream.XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES;
+import static javax.xml.stream.XMLInputFactory.IS_VALIDATING;
+import static org.apache.openmeetings.core.rss.LoadAtomRssFeed.getFeedConnection;
+import static org.apache.openmeetings.util.OpenmeetingsVariables.getWebAppRootKey;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.EndElement;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+
+import org.red5.logging.Red5LoggerFactory;
+import org.slf4j.Logger;
+
+import com.github.openjson.JSONArray;
+import com.github.openjson.JSONObject;
+
+public class AtomReader {
+	private static Logger log = Red5LoggerFactory.getLogger(AtomReader.class, getWebAppRootKey());
+	private static final int MAX_ITEM_COUNT = 5;
+	private static final Map<String, Spec> specs = new HashMap<>();
+	private static final XMLInputFactory inputFactory;
+	static {
+		inputFactory = XMLInputFactory.newInstance();
+		inputFactory.setProperty(IS_REPLACING_ENTITY_REFERENCES, false);
+		inputFactory.setProperty(IS_VALIDATING, false);
+		inputFactory.setProperty(IS_NAMESPACE_AWARE, false);
+
+		add("item")
+			.add(new Field("title"))
+			.add(new Field("link"))
+			.add(new Field("description", "content", true))
+			.add(new Field("pubDate", "published"))
+			.add(new Field("author"));
+		add("entry")
+			.add(new Field("title"))
+			.add(new Field("link", "link", "href", false))
+			.add(new Field("content", "content", true))
+			.add(new Field("updated", "published"))
+			.add(new Field("published"))
+			.add(new Field("author"));
+	}
+
+	private static Spec add(String name) {
+		Spec s = new Spec(name);
+		specs.put(name, s);
+		return s;
+	}
+
+	public static void load(String url, JSONArray feed) {
+		HttpURLConnection con = null;
+		try {
+			con = getFeedConnection(url);
+			try (InputStream is = con.getInputStream()) {
+				XMLEventReader reader = inputFactory.createXMLEventReader(is);
+				int i = 0;
+				JSONObject obj = null;
+				StringBuilder val = null;
+				Spec spec = null;
+				Field f = null;
+				while (reader.hasNext()) {
+					XMLEvent evt = reader.nextEvent();
+					if (obj == null && evt.isStartElement()) {
+						StartElement start = (StartElement)evt;
+						String name = start.getName().getLocalPart();
+						if (specs.containsKey(name)) {
+							spec = specs.get(name);
+							obj = new JSONObject();
+							i++;
+						}
+					} else if (obj != null) {
+						if (evt.isStartElement()) {
+							StartElement start = (StartElement)evt;
+							String name = start.getName().getLocalPart();
+							if (spec.contains(name)) {
+								f = spec.get(name);
+								val = new StringBuilder();
+								if (f.getAttr() != null) {
+									Attribute a = start.getAttributeByName(new QName(f.getAttr()));
+									if (a != null) {
+										val.append(a.getValue());
+									}
+								}
+							}
+						} else if (f != null && evt.isCharacters()) {
+							val.append(((Characters)evt).getData());
+						} else if (f != null && evt.isEndElement() && f.getName().equals(((EndElement)evt).getName().getLocalPart())) {
+							if (!obj.has(f.getAlias())) {
+								obj.put(f.getAlias(), val.toString());
+							}
+							f = null;
+						} else if (evt.isEndElement() && spec.getName().equals(((EndElement)evt).getName().getLocalPart())) {
+							feed.put(obj);
+							obj = null;
+						}
+					}
+					if (i > MAX_ITEM_COUNT) {
+						break;
+					}
+				}
+			}
+		} catch (IOException|XMLStreamException e) {
+			log.error("Unexpected error while getting RSS", e);
+		} finally {
+			if (con != null) {
+				con.disconnect();
+			}
+		}
+	}
+
+	public static class Spec {
+		private final String name;
+		private final Map<String, Field> fields = new LinkedHashMap<>();
+
+		public Spec(final String name) {
+			this.name = name;
+		}
+
+		public Spec add(Field f) {
+			fields.put(f.getName(), f);
+			return this;
+		}
+
+		public String getName() {
+			return name;
+		}
+
+		public boolean contains(String f) {
+			return fields.containsKey(f);
+		}
+
+		public Field get(String f) {
+			return fields.get(f);
+		}
+	}
+
+	public static class Field {
+		private final String name;
+		private final String alias;
+		private final String attr;
+		private final boolean xml;
+
+		public Field(String name) {
+			this(name, name);
+		}
+
+		public Field(String name, String alias) {
+			this(name, alias, false);
+		}
+
+		public Field(String name, String alias, boolean xml) {
+			this(name, alias, null, xml);
+		}
+
+		public Field(String name, String alias, String attr, boolean xml) {
+			this.name= name;
+			this.alias = alias;
+			this.attr = attr;
+			this.xml = xml;
+		}
+
+		public String getName() {
+			return name;
+		}
+
+		public String getAlias() {
+			return alias;
+		}
+
+		public String getAttr() {
+			return attr;
+		}
+
+		public boolean isXml() {
+			return xml;
+		}
+	}
+}
diff --git a/openmeetings-service/src/main/java/org/apache/openmeetings/service/quartz/scheduler/ReminderJob.java b/openmeetings-service/src/main/java/org/apache/openmeetings/service/quartz/scheduler/ReminderJob.java
index bb9f38c..d134965 100644
--- a/openmeetings-service/src/main/java/org/apache/openmeetings/service/quartz/scheduler/ReminderJob.java
+++ b/openmeetings-service/src/main/java/org/apache/openmeetings/service/quartz/scheduler/ReminderJob.java
@@ -18,8 +18,6 @@
  */
 package org.apache.openmeetings.service.quartz.scheduler;
 
-import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.apache.openmeetings.core.rss.LoadAtomRssFeed.getFeedConnection;
 import static org.apache.openmeetings.core.rss.LoadAtomRssFeed.setRss;
 import static org.apache.openmeetings.util.OpenmeetingsVariables.CONFIG_DASHBOARD_RSS_FEED1;
 import static org.apache.openmeetings.util.OpenmeetingsVariables.CONFIG_DASHBOARD_RSS_FEED2;
@@ -27,10 +25,6 @@ import static org.apache.openmeetings.util.OpenmeetingsVariables.CONFIG_DASHBOAR
 import static org.apache.openmeetings.util.OpenmeetingsVariables.getWebAppRootKey;
 import static org.apache.openmeetings.util.OpenmeetingsVariables.isInitComplete;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.HttpURLConnection;
-
 import org.apache.openmeetings.core.mail.MailHandler;
 import org.apache.openmeetings.db.dao.basic.ConfigurationDao;
 import org.apache.openmeetings.db.dao.user.UserDao;
@@ -39,22 +33,16 @@ import org.apache.openmeetings.service.calendar.AppointmentLogic;
 import org.apache.openmeetings.service.mail.template.subject.RecordingExpiringTemplate;
 import org.apache.openmeetings.service.mail.template.subject.SubjectEmailTemplate;
 import org.apache.wicket.util.string.Strings;
-import org.dom4j.Document;
-import org.dom4j.DocumentException;
-import org.dom4j.Element;
-import org.dom4j.io.SAXReader;
 import org.red5.logging.Red5LoggerFactory;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import com.github.openjson.JSONArray;
-import com.github.openjson.JSONObject;
 
 @Component("reminderJob")
 public class ReminderJob extends AbstractJob {
 	private static Logger log = Red5LoggerFactory.getLogger(ReminderJob.class, getWebAppRootKey());
-	private static final int MAX_ITEM_COUNT = 5;
 	@Autowired
 	private AppointmentLogic appointmentLogic;
 	@Autowired
@@ -62,7 +50,7 @@ public class ReminderJob extends AbstractJob {
 	@Autowired
 	private MailHandler mailHandler;
 	@Autowired
-	private ConfigurationDao cfgDao;;
+	private ConfigurationDao cfgDao;
 
 	public void remindMeetings() {
 		log.trace("ReminderJob.remindMeetings");
@@ -104,56 +92,10 @@ public class ReminderJob extends AbstractJob {
 			log.debug("Rss disabled by Admin");
 			return;
 		}
-		SAXReader reader = new SAXReader(false);
-		reader.setEncoding(UTF_8.name());
 		JSONArray feed = new JSONArray();
 		for (String url : new String[] {cfgDao.getString(CONFIG_DASHBOARD_RSS_FEED1, ""), cfgDao.getString(CONFIG_DASHBOARD_RSS_FEED2, "")}) {
 			if (!Strings.isEmpty(url)) {
-				HttpURLConnection con = null;
-				try {
-					con = getFeedConnection(url);
-					try (InputStream is = con.getInputStream()) {
-						Document doc = reader.read(is);
-						int i = 0;
-						for (Element entry : doc.getRootElement().elements("item")) {
-							i++;
-							feed.put(new JSONObject()
-									.put("title", entry.element("title").getStringValue())
-									.put("link", entry.element("link").getStringValue())
-									.put("content", entry.element("description").getStringValue())
-									.put("published", entry.element("pubDate").getStringValue())
-									.put("author", entry.element("author").getStringValue())
-									);
-							if (i > MAX_ITEM_COUNT) {
-								break;
-							}
-						}
-						i = 0;
-						for (Element entry : doc.getRootElement().elements("entry")) {
-							i++;
-							Element date = entry.element("published");
-							if (date == null) {
-								date = entry.element("updated");
-							}
-							feed.put(new JSONObject()
-									.put("title", entry.element("title").getStringValue())
-									.put("link", entry.element("link").getStringValue())
-									.put("content", entry.element("content").getStringValue())
-									.put("published", date.getStringValue())
-									.put("author", entry.element("author").getStringValue())
-									);
-							if (i > MAX_ITEM_COUNT) {
-								break;
-							}
-						}
-					}
-				} catch (IOException|DocumentException e) {
-					log.error("Unexpected error while getting RSS", e);
-				} finally {
-					if (con != null) {
-						con.disconnect();
-					}
-				}
+				AtomReader.load(url, feed);
 			}
 		}
 		if (feed.length() > 0) {

-- 
To stop receiving notification emails like this one, please contact
solomax@apache.org.