Browse code

replace jersey with jsoup for simpler tribune backend manipulations

devnewton authored on 02/09/2014 16:56:18
Showing 5 changed files
... ...
@@ -1,50 +1,51 @@
1 1
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
-	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
-	<modelVersion>4.0.0</modelVersion>
2
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
+    <modelVersion>4.0.0</modelVersion>
4 4
 
5
-	<groupId>im.bci</groupId>
6
-	<artifactId>replopbot</artifactId>
7
-	<version>0.0.1-SNAPSHOT</version>
8
-	<packaging>jar</packaging>
5
+    <groupId>im.bci</groupId>
6
+    <artifactId>replopbot</artifactId>
7
+    <version>0.0.1-SNAPSHOT</version>
8
+    <packaging>jar</packaging>
9 9
 
10
-	<name>replop</name>
11
-	<url>http://maven.apache.org</url>
10
+    <name>replop</name>
11
+    <url>http://maven.apache.org</url>
12 12
 
13
-	<properties>
14
-		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15
-		<camel.version>2.13.2</camel.version>
16
-	</properties>
13
+    <properties>
14
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15
+        <camel.version>2.13.2</camel.version>
16
+    </properties>
17 17
 
18
-	<dependencies>
19
-		<dependency>
20
-			<groupId>org.apache.camel</groupId>
21
-			<artifactId>camel-irc</artifactId>
22
-			<version>${camel.version}</version>
23
-		</dependency>
24
-		<dependency>
25
-			<groupId>org.apache.camel</groupId>
26
-			<artifactId>camel-xmpp</artifactId>
27
-			<version>${camel.version}</version>
28
-		</dependency>
29
-		<dependency>
30
-			<groupId>org.glassfish.jersey.core</groupId>
31
-			<artifactId>jersey-client</artifactId>
32
-			<version>2.6</version>
33
-		</dependency>
34
-		<dependency>
35
-			<groupId>org.glassfish.jersey.media</groupId>
36
-			<artifactId>jersey-media-json-jackson</artifactId>
37
-			<version>2.6</version>
38
-		</dependency>
39
-		<dependency>
40
-			<groupId>commons-lang</groupId>
41
-			<artifactId>commons-lang</artifactId>
42
-			<version>2.6</version>
43
-		</dependency>
44
-		<dependency>
45
-			<groupId>org.slf4j</groupId>
46
-			<artifactId>slf4j-simple</artifactId>
47
-			<version>1.7.7</version>
48
-		</dependency>
49
-	</dependencies>
18
+    <dependencies>
19
+        <dependency>
20
+            <groupId>org.apache.camel</groupId>
21
+            <artifactId>camel-irc</artifactId>
22
+            <version>${camel.version}</version>
23
+        </dependency>
24
+        <dependency>
25
+            <groupId>org.apache.camel</groupId>
26
+            <artifactId>camel-xmpp</artifactId>
27
+            <version>${camel.version}</version>
28
+        </dependency>
29
+        <dependency>
30
+            <groupId>commons-lang</groupId>
31
+            <artifactId>commons-lang</artifactId>
32
+            <version>2.6</version>
33
+        </dependency>
34
+        <dependency>
35
+            <groupId>org.slf4j</groupId>
36
+            <artifactId>slf4j-simple</artifactId>
37
+            <version>1.7.7</version>
38
+        </dependency>
39
+        <dependency>
40
+            <groupId>org.jsoup</groupId>
41
+            <artifactId>jsoup</artifactId>
42
+            <version>1.7.3</version>
43
+        </dependency>
44
+        <dependency>
45
+            <artifactId>jackson-mapper-asl</artifactId>
46
+            <groupId>org.codehaus.jackson</groupId>
47
+            <type>jar</type>
48
+            <version>1.9.13</version>
49
+        </dependency>
50
+    </dependencies>
50 51
 </project>
... ...
@@ -2,53 +2,51 @@ package im.bci.camel.tribune;
2 2
 
3 3
 import java.util.Collections;
4 4
 import java.util.Comparator;
5
-
6
-import im.bci.camel.tribune.backend.Board;
7
-import im.bci.camel.tribune.backend.Post;
8
-
9
-import javax.ws.rs.client.Client;
10
-import javax.ws.rs.client.ClientBuilder;
11
-import javax.ws.rs.client.WebTarget;
12
-
13 5
 import org.apache.camel.Endpoint;
14 6
 import org.apache.camel.Exchange;
15 7
 import org.apache.camel.Processor;
16 8
 import org.apache.camel.impl.ScheduledPollConsumer;
9
+import org.jsoup.Jsoup;
10
+import org.jsoup.nodes.Document;
11
+import org.jsoup.nodes.Element;
12
+import org.jsoup.parser.Parser;
13
+import org.jsoup.select.Elements;
17 14
 
18 15
 public class TribuneConsumer extends ScheduledPollConsumer {
19 16
 
20 17
     private final TribuneConfiguration configuration;
21
-    private Client client;
22 18
     private long lastId = -1;
23 19
 
24 20
     public TribuneConsumer(Endpoint endpoint, Processor processor, TribuneConfiguration configuration) {
25 21
         super(endpoint, processor);
26 22
         this.configuration = configuration;
27 23
         this.setDelay(1000);
28
-        client = ClientBuilder.newBuilder().build();
29 24
     }
30 25
 
31 26
     @Override
32 27
     protected int poll() throws Exception {
33
-        WebTarget target = client.target(configuration.getGetUrl());
34
-        Board board = target.request().get(Board.class);
28
+        Document doc = Jsoup.connect(configuration.getGetUrl())
29
+                .parser(Parser.xmlParser())
30
+                .get();
31
+        Elements posts = doc.select("post");
35 32
         int nbPostPolled = 0;
36 33
         long maxId = -1;
37
-        Collections.sort(board.getPost(), new Comparator<Post>() {
34
+        Collections.sort(posts, new Comparator<Element>() {
38 35
 
39
-            public int compare(Post p1, Post p2) {
40
-                return Long.compare(p1.getId(), p2.getId());
36
+            public int compare(Element p1, Element p2) {
37
+                return p1.attr("id").compareTo(p2.attr("id"));
41 38
             }
42 39
         });
43
-        for (Post post : board.getPost()) {
44
-            if (post.getId() > lastId) {
40
+        for (Element post : posts) {
41
+            long postId = Long.parseLong(post.attr("id"));
42
+            if (postId > lastId) {
45 43
                 if (lastId > 0) {
46 44
                     Exchange ex = getEndpoint().createExchange();
47 45
                     ex.setIn(new TribuneMessage(post));
48 46
                     getProcessor().process(ex);
49 47
                     ++nbPostPolled;
50 48
                 }
51
-                maxId = Math.max(maxId, post.getId());
49
+                maxId = Math.max(maxId, postId);
52 50
             }
53 51
         }
54 52
         lastId = Math.max(maxId, lastId);
... ...
@@ -1,30 +1,33 @@
1 1
 package im.bci.camel.tribune;
2 2
 
3 3
 import java.util.Map;
4
-
5
-import im.bci.camel.tribune.backend.Post;
6
-
7 4
 import org.apache.camel.impl.DefaultMessage;
8 5
 import org.apache.commons.lang.StringUtils;
6
+import org.jsoup.nodes.Element;
7
+import org.jsoup.select.Elements;
9 8
 
10 9
 public class TribuneMessage extends DefaultMessage {
11 10
 
12
-    private Post post;
11
+    private final Element post;
13 12
 
14
-    public TribuneMessage(Post post) {
13
+    TribuneMessage(Element post) {
15 14
         this.post = post;
16 15
     }
17
-    
16
+
18 17
     @Override
19 18
     protected Object createBody() {
20
-        return post.getMessage();
19
+        Elements message = post.select("message");
20
+        for(Element a : message.select("a")) {
21
+            a.text(a.attr("href"));
22
+        }
23
+        return message.text();
21 24
     }
22
-    
25
+
23 26
     @Override
24 27
     protected void populateInitialHeaders(Map<String, Object> map) {
25
-        String sender = post.getLogin();
26
-        if(StringUtils.isEmpty(sender)) {
27
-            sender = post.getInfo();
28
+        String sender = post.select("login").text();
29
+        if (StringUtils.isEmpty(sender)) {
30
+            sender = post.select("info").text();
28 31
         }
29 32
         map.put(TribuneConstants.SENDER, sender);
30 33
     }
... ...
@@ -1,38 +1,26 @@
1 1
 package im.bci.camel.tribune;
2 2
 
3
-import javax.ws.rs.client.Client;
4
-import javax.ws.rs.client.ClientBuilder;
5
-import javax.ws.rs.client.Entity;
6
-import javax.ws.rs.client.WebTarget;
7
-import javax.ws.rs.core.Form;
8
-import javax.ws.rs.core.MediaType;
9
-
10 3
 import org.apache.camel.Endpoint;
11 4
 import org.apache.camel.Exchange;
12 5
 import org.apache.camel.impl.DefaultProducer;
13 6
 import org.apache.commons.lang.ObjectUtils;
7
+import org.jsoup.Jsoup;
14 8
 
15 9
 public class TribuneProducer extends DefaultProducer {
16 10
 
17
-    private TribuneConfiguration configuration;
18
-    private Client client;
11
+    private final TribuneConfiguration configuration;
19 12
 
20 13
     public TribuneProducer(Endpoint endpoint, TribuneConfiguration configuration) {
21 14
         super(endpoint);
22 15
         this.configuration = configuration;
23
-        client = ClientBuilder.newBuilder().build();
24 16
     }
25 17
 
26 18
     public void process(Exchange exchange) throws Exception {
27 19
         String message = exchange.getIn().getBody(String.class);
28
-        WebTarget target = client.target(configuration.getPostUrl());
29
-        Form form = new Form();
30
-        form.param("message", message);
31
-        String sender = ObjectUtils.toString(exchange.getIn().getHeader(TribuneConstants.SENDER));
32
-        if (!sender.isEmpty()) {
33
-            form.param("nickname", sender);
34
-        }
35
-        target.request().post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
20
+        Jsoup.connect(configuration.getPostUrl())
21
+                .data("message", message)
22
+                .data("nickname", ObjectUtils.toString(exchange.getIn().getHeader(TribuneConstants.SENDER)))
23
+                .post();
36 24
     }
37 25
 
38 26
 }
... ...
@@ -10,10 +10,12 @@ package im.bci.camel.tribune.backend;
10 10
 
11 11
 import javax.xml.bind.annotation.XmlAccessType;
12 12
 import javax.xml.bind.annotation.XmlAccessorType;
13
+import javax.xml.bind.annotation.XmlAnyElement;
13 14
 import javax.xml.bind.annotation.XmlAttribute;
14 15
 import javax.xml.bind.annotation.XmlElement;
15 16
 import javax.xml.bind.annotation.XmlRootElement;
16 17
 import javax.xml.bind.annotation.XmlType;
18
+import org.w3c.dom.Element;
17 19
 
18 20
 
19 21
 /**
... ...
@@ -50,8 +52,8 @@ public class Post {
50 52
 
51 53
     @XmlElement(required = true)
52 54
     protected String info;
53
-    @XmlElement(required = true)
54
-    protected String message;
55
+    @XmlAnyElement(lax = true)
56
+    protected Element message;
55 57
     @XmlElement(required = true)
56 58
     protected String login;
57 59
     @XmlAttribute(name = "time", required = true)
... ...
@@ -91,7 +93,7 @@ public class Post {
91 93
      *     {@link Message }
92 94
      *     
93 95
      */
94
-    public String getMessage() {
96
+    public Element getMessage() {
95 97
         return message;
96 98
     }
97 99
 
... ...
@@ -103,7 +105,7 @@ public class Post {
103 105
      *     {@link Message }
104 106
      *     
105 107
      */
106
-    public void setMessage(String value) {
108
+    public void setMessage(Element value) {
107 109
         this.message = value;
108 110
     }
109 111