-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_dumpUrls.groovy
188 lines (165 loc) · 6.53 KB
/
server_dumpUrls.groovy
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
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
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.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.io.IOUtils;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
// TODO: Use javax.json.* for immutability
public class YurlDumpUrls {
public static final String YOUTUBE_DOWNLOAD = "/home/sarnobat/bin/youtube_download";
public static final Integer ROOT_ID = 45;
private static final String CYPHER_URI = "http://netgear.rohidekar.com:7474/db/data/cypher";
@Path("yurl")
public static class YurlResource { // Must be public
// ------------------------------------------------------------------------------------
// Backup
// ------------------------------------------------------------------------------------
@Deprecated
@GET
@Path("dumpurls.disabled")
@Produces("application/text")
public Response dumpUrls(@QueryParam("rootId") Integer iRootId)
throws IOException, JSONException {
Set<String> visitedInternalNodes = new HashSet<String>();
Integer startId = iRootId;
if (startId == null) {
startId = 0;
}
StringBuffer sb = new StringBuffer();
StringBuffer plainText = new StringBuffer();
sb.append("foo\n");
printNode(startId, sb, plainText, visitedInternalNodes, "dumpUrls() - ");
System.out.println("dumpUrls");
return Response.ok().header("Access-Control-Allow-Origin", "*")
.entity(plainText.toString()).type("text/plain").build();
}
private static void printNode(Integer iRootId, StringBuffer json,
StringBuffer plainText, Set<String> visitedInternalNodes, String sourceMeth)
throws IOException, JSONException {
json.append("bar");
ImmutableMap.Builder<String, Object> theParams = ImmutableMap.<String, Object>builder();
theParams.put("nodeId", iRootId);
JSONObject theResponse = execute(
"start root=node({nodeId}) MATCH root--n RETURN distinct n, id(n)",
theParams.build(), sourceMeth + ": " + "printNode()");
JSONArray jsonArray = (JSONArray) theResponse.get("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray aNode = (JSONArray) jsonArray.get(i);
JSONObject object = (JSONObject) ((JSONObject) aNode.get(0))
.get("data");
String id = (String) checkNotNull(aNode.get(1));
if (visitedInternalNodes.contains(id)) {
continue;
}
System.out.println(sourceMeth + "printNode() - 1.5");
if (object.has("type") && object.get("type") != null
&& object.get("type").equals("categoryNode")) {
plainText.append("=== ");
plainText.append(object.get("name"));
plainText.append(" ===\n");
}
if (object.has("title")) {
plainText.append("\"");
plainText.append(object.get("title"));
plainText.append("\",\"");
if (object.has("url")) {
plainText.append(object.get("url"));
}
plainText.append("\"");
plainText.append("\n");
}
if (object.has("url")) {
plainText.append(object.get("url"));
plainText.append("\n");
plainText.append("\n");
}
json.append(object);
System.out.println(sourceMeth + "printNode() - 2");
visitedInternalNodes.add(id);
printNode(Integer.parseInt(id), json, plainText,
visitedInternalNodes, sourceMeth);
}
}
static JSONObject execute(String iCypherQuery, Map<String, Object> iParams, String... iCommentPrefix) {
return execute(iCypherQuery, iParams, true, iCommentPrefix);
}
// TODO: make this map immutable
static JSONObject execute(String iCypherQuery,
Map<String, Object> iParams, boolean doLogging, String... iCommentPrefix) {
String commentPrefix = iCommentPrefix.length > 0 ? iCommentPrefix[0] + " " : "";
if (doLogging) {
System.out.println(commentPrefix + " - \t" + iCypherQuery);
System.out.println(commentPrefix + "- \tparams - " + iParams);
}
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
Boolean.TRUE);
// POST {} to the node entry point URI
ClientResponse theResponse = Client.create(clientConfig).resource(
CYPHER_URI)
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON).entity("{ }")
.post(ClientResponse.class, ImmutableMap
.<String, Object> of("query", iCypherQuery, "params",
Preconditions.checkNotNull(iParams)));
if (theResponse.getStatus() != 200) {
System.out.println(commentPrefix + "FAILED:\n\t" + iCypherQuery + "\n\tparams: " + iParams);
try {
throw new RuntimeException(IOUtils.toString(theResponse.getEntityInputStream(), "UTF-8"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
String theNeo4jResponse ;
try {
// Do not inline this. We need to close the stream after
// copying
theNeo4jResponse = IOUtils.toString(theResponse.getEntityInputStream(), "UTF-8");
theResponse.getEntityInputStream().close();
theResponse.close();
if (doLogging) {
System.out.println(commentPrefix + "end");
}
return new JSONObject(theNeo4jResponse);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) throws URISyntaxException, JSONException, IOException {
// 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:4442/"), new ResourceConfig(
YurlResource.class));
// Do not allow this in multiple processes otherwise your hard disk will fill up
// or overload the database
// Problem - this won't get executed until the server ends
//YurlWorldResource.downloadUndownloadedVideosInSeparateThread() ;
} catch (Exception e) {
System.out.println("Not creating server instance");
}
}
}