1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.nntpserver.repository;
23
24 import org.apache.james.util.codec.Base64;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.util.Date;
31 import java.util.Enumeration;
32 import java.util.Properties;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class ArticleIDRepository {
47
48
49
50
51 private final File root;
52
53
54
55
56 private final String articleIDDomainSuffix;
57
58
59
60
61
62
63 private int counter = 0;
64
65 ArticleIDRepository(File root,String articleIDDomainSuffix) {
66 this.root = root;
67 this.articleIDDomainSuffix = articleIDDomainSuffix;
68 }
69
70
71
72
73 String generateArticleID() {
74 int idx = Math.abs(counter++);
75 StringBuffer idBuffer =
76 new StringBuffer(256)
77 .append("<")
78 .append(Thread.currentThread().hashCode())
79 .append(".")
80 .append(System.currentTimeMillis())
81 .append(".")
82 .append(idx)
83 .append("@")
84 .append(articleIDDomainSuffix)
85 .append(">");
86 return idBuffer.toString();
87 }
88
89
90
91
92
93
94 void addArticle(String articleID,Properties prop) throws IOException {
95 if ( articleID == null ) {
96 articleID = generateArticleID();
97 }
98 FileOutputStream fout = null;
99 try {
100 fout = new FileOutputStream(getFileFromID(articleID));
101 prop.store(fout,new Date().toString());
102 } finally {
103 if (fout != null) {
104 fout.close();
105 }
106 }
107 }
108
109
110
111
112
113
114
115
116
117 File getFileFromID(String articleID) {
118 String b64Id;
119 try {
120 b64Id = removeCRLF(Base64.encodeAsString(articleID));
121 } catch (Exception e) {
122 throw new RuntimeException("This shouldn't happen: " + e);
123 }
124 return new File(root, b64Id);
125 }
126
127
128
129
130
131
132 private static String removeCRLF(String str) {
133 StringBuffer buffer = new StringBuffer();
134 for (int i = 0; i < str.length(); i++) {
135 char c = str.charAt(i);
136 if (c != '\r' && c != '\n') {
137 buffer.append(c);
138 }
139 }
140 return buffer.toString();
141 }
142
143
144
145
146
147
148
149
150 boolean isExists(String articleID) {
151 return ( articleID == null ) ? false : getFileFromID(articleID).exists();
152 }
153
154
155
156
157
158
159
160
161
162
163
164 NNTPArticle getArticle(NNTPRepository repo,String id) throws IOException {
165 File f = getFileFromID(id);
166 if ( f.exists() == false ) {
167 return null;
168 }
169 FileInputStream fin = null;
170 Properties prop = new Properties();
171 try {
172 fin = new FileInputStream(f);
173 prop.load(fin);
174 } finally {
175 if (fin != null) {
176 fin.close();
177 }
178 }
179 Enumeration enumeration = prop.keys();
180 NNTPArticle article = null;
181 while ( article == null && enumeration.hasMoreElements() ) {
182 String groupName = (String)enumeration.nextElement();
183 int number = Integer.parseInt(prop.getProperty(groupName));
184 NNTPGroup group = repo.getGroup(groupName);
185 if ( group != null ) {
186 article = group.getArticle(number);
187 }
188 }
189 return article;
190 }
191 }