-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYurlStash.java.beforeJdk11
376 lines (344 loc) · 13.7 KB
/
YurlStash.java.beforeJdk11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// I think the only remaining method in use here is:
// Request URL: http://netgear.rohidekar.com:44447/yurl/parent?nodeId=641476
// Everything else doesn't use port 4447
// Actually also image change is
// And move()
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.Paths;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import org.apache.commons.io.FileUtils;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import com.google.common.collect.ImmutableSet;
/**
* Deprecation doesn't mean the method can be removed. Only when index.html
* stops referring to it can it be removed.
*
* Neo4j dependencies removed.
*
* Only writing to the persistent store (and the associated async tasks) should
* remain in this thread.
*/
public class YurlStash {
// TODO: use java properties
private static final String QUEUE_DIR = System
.getProperty("user.home") + "/db.git/yurl_flatfile_db/";
private static final String QUEUE_FILE_TXT = "yurl_queue.txt";
private static final String TITLE_FILE_TXT = "yurl_titles_2017.txt";
private static final String QUEUE_FILE_TXT_MASTER = "yurl_master.txt";
private static final String QUEUE_FILE_TXT_2017 = "yurl_queue_2017.txt";// Started using this in Aug 2017. Older data is not in this file.
private static final String QUEUE_FILE_TXT_DELETE = "yurl_deleted.txt";
@Path("yurl")
public static class YurlResource { // Must be public
// --------------------------------------------------------------------------------------
// Write operations
// --------------------------------------------------------------------------------------
@GET
@Path("stash")
@Produces("application/json")
public Response stash(@QueryParam("param1") String iUrl,
@QueryParam("rootId") Integer iCategoryId)
throws JSONException, IOException {
System.err.println("stash() begin");
String theHttpUrl = URLDecoder.decode(iUrl, "UTF-8");
System.err.println("stash() theHttpUrl = " + theHttpUrl);
try {
String dir1 = YurlStash.QUEUE_DIR;
String queueFile1 = dir1 + "/"
+ YurlStash.QUEUE_FILE_TXT_MASTER;
String command1 = "echo '" + iCategoryId.toString() + "::"
+ iUrl + "::'`date +%s` | tee -a '" + queueFile1 + "'";
appendToTextFileSync(iUrl, dir1, command1);
String queueFile2 = QUEUE_DIR + "/"
+ YurlStash.QUEUE_FILE_TXT_2017;
String command2 = "echo '" + iCategoryId.toString() + "::"
+ theHttpUrl + "::'`date +%s` | tee -a '" + queueFile2
+ "'";
appendToTextFileSync(theHttpUrl, QUEUE_DIR, command2);
// Delete the url cache file for this category. It will get
// regenrated next time we load that category page.
removeCategoryCache(iCategoryId);
_getTitle: {
System.err.println(
"YurlStash.YurlResource.getTitle() - we are still using this. Ideally we shouldn't.");
String title = "";
try {
title = Executors.newFixedThreadPool(2)
.invokeAll(ImmutableSet
.<Callable<String>>of(new Callable<String>() {
public String call() throws Exception {
try {
return Jsoup
.connect(new URL(theHttpUrl).toString())
.get().title();
} catch (org.jsoup.UnsupportedMimeTypeException e) {
System.err
.println("YurlResource.getTitle() - "
+ e.getMessage());
return "";
}
}
}), 3000L, TimeUnit.SECONDS)
.get(0).get();
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (ExecutionException e2) {
e2.printStackTrace();
}
String theTitle = title;
if (theTitle != null && theTitle.length() > 0) {
new Thread(new Runnable() {
@Override
public void run() {
String titleFileStr = YurlStash.QUEUE_DIR + "/"
+ YurlStash.TITLE_FILE_TXT;
File file2 = Paths.get(YurlStash.QUEUE_DIR).toFile();
if (!file2.exists()) {
throw new RuntimeException(
"Non-existent: " + file2.getAbsolutePath());
}
String command = "echo '" + theHttpUrl + "::"
+ theTitle + "' | tee -a '" + titleFileStr + "'";
System.err.println(
"appendToTextFile() - command = " + command);
Process p;
try {
p = new ProcessBuilder().directory(file2)
.command("echo", "hello world")
.command("/bin/sh", "-c", command).inheritIO()
.start();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (p.exitValue() == 0) {
System.err.println(
"appendToTextFile() - successfully appended 2 "
+ theHttpUrl);
} else {
System.err.println(
"launchAsynchronousTasksHttpcat() - 4 error appending "
+ theHttpUrl);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
String dir = YurlStash.QUEUE_DIR;
// This is not (yet) the master file. The master file is written to
// synchronously.
String queueFile = dir + "/" + YurlStash.QUEUE_FILE_TXT;
String command = "echo '" + iCategoryId.toString() + "::"
+ theHttpUrl + "::'`date +%s` | tee -a '" + queueFile
+ "'";
appendToTextFile(theHttpUrl, dir, queueFile, command);
System.err.println(
"YurlStash.YurlResource.stash() sending empty json response. This should work.");
return Response.ok()
.header("Access-Control-Allow-Origin", "*")
.entity(new JSONObject().toString())
.type("application/json").build();
} catch (Exception e) {
e.printStackTrace();
throw new JSONException(e);
}
}
private static void removeCategoryCache(Integer iCategoryId) {
java.nio.file.Path path1 = Paths
.get(System.getProperty("user.home")
+ "/github/yurl/tmp/urls/" + iCategoryId + ".json");
path1.toFile().delete();
System.err.println(
"Yurl.YurlResource.launchAsynchronousTasksHttpcat() deleted cache file 1: "
+ path1);
java.nio.file.Path path = Paths
.get(System.getProperty("user.home")
+ "/github/yurl/tmp/categories/topology/" + iCategoryId
+ ".txt");
path.toFile().delete();
System.err.println(
"Yurl.YurlResource.launchAsynchronousTasksHttpcat() deleted cache file 2: "
+ path);
}
private static void appendToTextFileSync(String iUrl,
final String id, String dir, String file2, long created)
throws IOException, InterruptedException {
String queueFile = dir + "/" + file2;
File file = Paths.get(dir).toFile();
if (!file.exists()) {
throw new RuntimeException(
"Non-existent: " + file.getAbsolutePath());
}
String command = "echo '" + id + "::" + iUrl + "::" + created
+ "' | tee -a '" + queueFile + "'";
System.err.println("appendToTextFileSync() - " + command);
Process p = new ProcessBuilder().directory(file)
.command("echo", "hello world")
.command("/bin/sh", "-c", command).inheritIO().start();
p.waitFor();
if (p.exitValue() == 0) {
System.err.println(
"appendToTextFileSync() - successfully appended 3 "
+ iUrl);
} else {
System.err.println(
"appendToTextFileSync() - 1 error appending " + iUrl);
}
}
private static void appendToTextFileSync(String iUrl, String dir,
String command) throws IOException, InterruptedException {
File file = Paths.get(dir).toFile();
if (!file.exists()) {
throw new RuntimeException(
"Non-existent: " + file.getAbsolutePath());
}
System.err.println("appendToTextFileSync() - " + command);
Process p = new ProcessBuilder().directory(file)
.command("echo", "hello world")
.command("/bin/sh", "-c", command).inheritIO().start();
p.waitFor();
if (p.exitValue() == 0) {
System.err.println(
"appendToTextFileSync() - successfully appended 4 "
+ iUrl);
} else {
System.err.println(
"appendToTextFileSync() - 2 error appending " + iUrl);
}
}
private static void appendToTextFile(String iUrl, String dir,
String queueFile, String command) {
new Thread(new Runnable() {
@Override
public void run() {
File file = Paths.get(dir).toFile();
if (!file.exists()) {
throw new RuntimeException(
"Non-existent: " + file.getAbsolutePath());
}
System.err.println("appendToTextFile() - " + command);
try {
Process p = new ProcessBuilder().directory(file)
.command("echo", "hello world")
.command("/bin/sh", "-c", command).inheritIO()
.start();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (p.exitValue() == 0) {
System.err.println(
"appendToTextFile() - successfully appended 5 "
+ iUrl + " to " + queueFile);
} else {
System.err.println(
"appendToTextFile() - 3 error appending " + iUrl);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
@GET
@Path("updateImage")
@Produces("application/json")
public Response changeImage(@QueryParam("url") String imageUrl,
@QueryParam("linkUrl") String iUrl,
@QueryParam("categoryId") String iCategoryId)
throws IOException, JSONException {
System.err.println(
"YurlStash.java::YurlResource.changeImage() begin");
FileUtils.write(
Paths.get(QUEUE_DIR + "/yurl_master_images.txt").toFile(),
iUrl + "::" + imageUrl + "\n", "UTF-8", true);
System.err.println(
"YurlStash.java::YurlResource.changeImage() - success: "
+ iUrl + " :: " + imageUrl);
new Thread() {
@Override
public void run() {
removeCategoryCache(Integer.parseInt(iCategoryId));
}
}.start();
return Response.ok().header("Access-Control-Allow-Origin", "*")
.entity(new JSONObject().toString())
.type("application/json").build();
}
/**
* This MOVES a node to a new subcategory. It deletes the relationship with the
* existing parent
*
* @throws InterruptedException
*/
@GET
@Path("relate")
@Produces("application/json")
public Response move(@QueryParam("parentId") Integer iNewParentId,
@QueryParam("url") String iUrl,
@QueryParam("currentParentId") Integer iCurrentParentId,
@QueryParam("created") Long created)
throws JSONException, IOException, InterruptedException {
System.err.println("Yurl.YurlResource.move() begin");
appendToTextFileSync(iUrl, iNewParentId.toString(),
YurlStash.QUEUE_DIR, YurlStash.QUEUE_FILE_TXT_MASTER,
created);
System.err.println("Yurl.YurlResource.move() 2");
appendToTextFileSync(iUrl, "-" + iCurrentParentId.toString(),
QUEUE_DIR, YurlStash.QUEUE_FILE_TXT_DELETE, created);
System.err.println("Yurl.YurlResource.move() 4");
new Thread() {
@Override
public void run() {
removeCategoryCache(iNewParentId);
}
}.start();
new Thread() {
@Override
public void run() {
removeCategoryCache(iCurrentParentId);
}
}.start();
return Response.ok().header("Access-Control-Allow-Origin", "*")
.entity(new JSONObject().toString())
.type("application/json").build();
}
}
public static void main(String[] args)
throws URISyntaxException, JSONException, IOException {
System.err.println("main() - begin");
String port = "4447";
// Turn off that stupid Jersey logger.
// This works in Java but not in Groovy.
// java.util.Logger.getLogger("org.glassfish.jersey").setLevel(java.util.Level.SEVERE);
try {
JdkHttpServerFactory.createHttpServer(
new URI("http://localhost:" + port + "/"),
new ResourceConfig(YurlStash.YurlResource.class));
} catch (Exception e) {
// e.printStackTrace();
System.err.println("Not creating server instance");
}
}
}