-
Notifications
You must be signed in to change notification settings - Fork 5
/
ConfluenceJsonParseToPipelineDocs.js
230 lines (185 loc) · 7.02 KB
/
ConfluenceJsonParseToPipelineDocs.js
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
var confluenceJsonReader = function (doc) {
var PipelineDocument = com.lucidworks.apollo.common.pipeline.PipelineDocument;
var URL = java.net.URL;
var ArrayList = java.util.ArrayList;
var JSONObject = org.json.JSONObject;
var Jsoup = org.jsoup.Jsoup;
var Document = org.jsoup.nodes.Document;
var Element = org.jsoup.nodes.Element;
var String = java.lang.String;
var System = java.lang.System;
var e = java.lang.Exception;
var docs = Java.type("java.util.ArrayList");
var user = new String(USERNAME);
var pwd = new String(PWD);
var url = new String("https://cprassoc.atlassian.net/wiki/rest/api/search?cql=siteSearch~%22*%22%20AND%20type=page&label=docs&expand=content.space,space.homepage");
var baseUrl = new String("https://cprassoc.atlassian.net/wiki");
var pipelineDocs = new ArrayList();
var pdoc;
try {
var json = new JSONObject(getContent(url, user, pwd));
var results = json.getJSONArray("results");
var obj;
// var contentObj;
var link;
for (var i = 0; i < results.length(); i++) {
obj = results.get(i);
System.out.println(obj.getString("title"));
doc = new PipelineDocument();
doc.addField("title", obj.getString("title"));
// contentObj = obj.getJSONObject("content");
link = baseUrl + obj.getString("url");
System.out.println(link);
doc.setId(link);
pipelineDocs.add(doc);
}
} catch (e) {
logger.error(e);
}
try {
var jdoc;
var mainEle;
var content = new String("");
for (var i = 0; i < pipelineDocs.size(); i++) {
content = getContent(new URL(pipelineDocs.get(i).getId()), user, pwd);
jdoc = Jsoup.parse(content);
mainEle = jdoc.select("#main").first();
if (mainEle === null) {
mainEle = jdoc.body();
}
if (mainEle !== null) {
pipelineDocs.get(i).addField("_raw_content_", content);
pipelineDocs.get(i).addField("body", mainEle.text());
System.out.println(mainEle.text());
}
}
} catch (e) {
logger.error(e);
}
return pipelineDocs;
}
function getContent(durl, user, pwd) {
var Base64 = org.apache.commons.codec.binary.Base64;
var BufferedReader = java.io.BufferedReader;
var InputStreamReader = java.io.InputStreamReader;
var HttpURLConnection = java.net.HttpURLConnection;
var StringBuffer = java.lang.StringBuffer;
var url = new URL(durl);
var buffer = new StringBuffer();
try {
var connection = url.openConnection();
var userCredentials = user + ":" + pwd;
var basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
connection.setRequestProperty("Authorization", basicAuth);
var inb = new BufferedReader(new InputStreamReader(connection.getInputStream()));
var inputLine = new String();
while ((inputLine = inb.readLine()) !== null) {
buffer.append(inputLine);
}
inb.close();
} catch (e) {
}
return buffer.toString();
}
/****
* Conflunce from Fusion
*/
var confluence2 = function (doc) {
logger.info("Loading Java Classes...");
var PipelineDocument = com.lucidworks.apollo.common.pipeline.PipelineDocument;
var URL = java.net.URL;
var ArrayList = java.util.ArrayList;
var Base64 = org.apache.commons.codec.binary.Base64;
var JSONObject = org.json.JSONObject;
var Jsoup = org.jsoup.Jsoup;
var Document = org.jsoup.nodes.Document;
var Element = org.jsoup.nodes.Element;
var String = java.lang.String;
var System = java.lang.System;
var e = java.lang.Exception;
logger.info("spin up variables");
var docs = Java.type("java.util.ArrayList");
var user = new String("kev.m.cowan");
var pwd = new String("s3RvI@Pwd0X");
var url = new String("https://cprassoc.atlassian.net/wiki/rest/api/search?cql=siteSearch~%22*%22%20AND%20type=page&label=docs&expand=content.space,space.homepage");
var baseUrl = new String("https://cprassoc.atlassian.net/wiki");
var pipelineDocs = new ArrayList();
var pdoc;
try {
logger.info("Get initial content");
var json = new JSONObject(getContent(url, user, pwd));
var results = json.getJSONArray("results");
var obj;
// var contentObj;
var link;
logger.info("iterate over results");
for (var i = 0; i < results.length(); i++) {
obj = results.get(i);
logger.info(obj.getString("title"));
doc = new PipelineDocument();
doc.addField("title", obj.getString("title"));
// contentObj = obj.getJSONObject("content");
link = baseUrl + obj.getString("url");
logger.info(link);
doc.setId(link);
pipelineDocs.add(doc);
}
} catch (e) {
logger.error(e);
}
logger.info("Now get the content for the page for each document");
try {
var jdoc;
var mainEle;
var content = new String("");
for (var i = 0; i < pipelineDocs.size(); i++) {
content = getContent(new URL(pipelineDocs.get(i).getId()), user, pwd);
jdoc = Jsoup.parse(content);
mainEle = jdoc.select("#main").first();
if (mainEle === null) {
mainEle = jdoc.body();
}
if (mainEle !== null) {
if(content !== null){
pipelineDocs.get(i).addField("_raw_content_", new Base64().encode(content));
}
pipelineDocs.get(i).addField("body", mainEle.text());
logger.info(mainEle.text());
}
}
} catch (e) {
logger.error(e);
}
return pipelineDocs;
}
function getContent(durl, user, pwd) {
var Base64 = org.apache.commons.codec.binary.Base64;
var BufferedReader = java.io.BufferedReader;
var InputStreamReader = java.io.InputStreamReader;
var HttpURLConnection = java.net.HttpURLConnection;
var String = java.lang.String;
var URL = java.net.URL;
var url = new URL(durl);
var buffer = new String("");
var e = java.lang.Exception;
try {
var connection = url.openConnection();
var userCredentials = user + ":" + pwd;
var basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
connection.setRequestProperty("Authorization", basicAuth);
var inb = new BufferedReader(new InputStreamReader(connection.getInputStream()));
var inputLine = new String();
while ((inputLine = inb.readLine()) !== null) {
buffer += inputLine;
}
inb.close();
} catch (e) {
logger.error(e);
}
return buffer;
}