1
2
3
4
5
6
7
8
9
10
11 package org.mule.example.geomail.components;
12
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
15
16
17
18
19 public class ReceivedHeader
20 {
21 private String id;
22 private String from;
23 private String by;
24 private String via;
25 private String with;
26 private String _for;
27 private String timestamp;
28
29 public static ReceivedHeader getInstance(String receivedHeader)
30 {
31 String fromPattern = "(?:from (.*?))?";
32 String byPattern = "(?:by (.*?))?";
33 String viaPattern = "(?:via (.*?))?";
34 String withPattern = "(?:with (.*?))?";
35 String idPattern = "(?:id (.*?))?";
36 String forPattern = "(?:for (.*?))?";
37 String timePattern = ";(.*)";
38
39 String pattern = fromPattern + byPattern + viaPattern + withPattern + idPattern + forPattern
40 + timePattern;
41
42 Matcher matcher = Pattern.compile(pattern, Pattern.DOTALL).matcher(receivedHeader);
43
44 ReceivedHeader result = null;
45 if (matcher.find())
46 {
47 result = new ReceivedHeader();
48 result.setFrom(matcher.group(1));
49 result.setBy(matcher.group(2));
50 result.setVia(matcher.group(3));
51 result.setWith(matcher.group(4));
52 result.setId(matcher.group(5));
53 result.setFor(matcher.group(6));
54 result.setTimestamp(matcher.group(7));
55 }
56
57 return result;
58 }
59
60 public String getId()
61 {
62 return id;
63 }
64
65 private void setId(String id)
66 {
67 this.id = (id != null ? id.trim() : null);
68 }
69
70 public String getFrom()
71 {
72 return from;
73 }
74
75 private void setFrom(String from)
76 {
77 this.from = (from != null ? from.trim() : null);
78 }
79
80 public String getBy()
81 {
82 return by;
83 }
84
85 private void setBy(String by)
86 {
87 this.by = (by != null ? by.trim() : null);
88 }
89
90 public String getVia()
91 {
92 return via;
93 }
94
95 private void setVia(String via)
96 {
97 this.via = (via != null ? via.trim() : null);
98 }
99
100 public String getWith()
101 {
102 return with;
103 }
104
105 private void setWith(String with)
106 {
107 this.with = (with != null ? with.trim() : null);
108 }
109
110 public String getFor()
111 {
112 return _for;
113 }
114
115 public void setFor(String _for)
116 {
117 this._for = (_for != null ? _for.trim() : null);
118 }
119
120 public String getTimestamp()
121 {
122 return timestamp;
123 }
124
125 private void setTimestamp(String timestamp)
126 {
127 this.timestamp = timestamp.trim();
128 }
129
130 @Override
131 public String toString()
132 {
133 return "Received {\n " + (getId() != null ? "id: " + getId() + "\n " : "")
134 + (getFrom() != null ? "from: " + getFrom() + "\n " : "")
135 + (getBy() != null ? "by: " + getBy() + "\n " : "")
136 + (getVia() != null ? "via: " + getVia() + "\n " : "")
137 + (getWith() != null ? "with: " + getWith() + "\n " : "")
138 + (getFor() != null ? "for: " + getFor() + "\n " : "") + "date-time: " + getTimestamp()
139 + "\n " + "}";
140 }
141 }