Skip to content

Commit 3645354

Browse files
committed
Rest Apis to export/import
1 parent c54b882 commit 3645354

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252

5353
import com.google.gson.Gson;
5454
import com.google.gson.reflect.TypeToken;
55-
55+
import com.google.gson.GsonBuilder;
56+
import com.google.gson.stream.JsonReader;
57+
import java.io.StringReader;
5658
/**
5759
* Rest api endpoint for the noteBook.
5860
*/
@@ -146,6 +148,56 @@ public Response getNotebook(@PathParam("notebookId") String notebookId) throws I
146148
return new JsonResponse<>(Status.OK, "", note).build();
147149
}
148150

151+
/**
152+
* export note REST API
153+
* @param
154+
* @return note JSON with status.OK
155+
* @throws IOException
156+
*/
157+
@GET
158+
@Path("export/{id}")
159+
public Response exportNoteBook(@PathParam("id") String noteId) {
160+
GsonBuilder gsonBuilder = new GsonBuilder();
161+
gsonBuilder.setPrettyPrinting();
162+
Gson gson = gsonBuilder.create();
163+
Note note = notebook.getNote(noteId);
164+
String json = gson.toJson(note);
165+
return new JsonResponse(Status.OK, "", json).build();
166+
}
167+
168+
/**
169+
* import new note REST API
170+
* @param req - notebook Json
171+
* @return JSON with new note ID
172+
* @throws IOException
173+
*/
174+
@PUT
175+
@Path("import")
176+
public Response importNotebook(String req) {
177+
GsonBuilder gsonBuilder = new GsonBuilder();
178+
gsonBuilder.setPrettyPrinting();
179+
Gson gson = gsonBuilder.create();
180+
JsonReader reader = new JsonReader(new StringReader(req));
181+
reader.setLenient(true);
182+
Note newNote;
183+
try {
184+
Note oldNote = gson.fromJson(reader, Note.class);
185+
newNote = notebook.createNote();
186+
newNote.setName(oldNote.getName());
187+
List<Paragraph> paragraphs = oldNote.getParagraphs();
188+
for (Paragraph p : paragraphs) {
189+
newNote.addCloneParagraph(p);
190+
}
191+
192+
newNote.persist();
193+
} catch (IOException e) {
194+
return new JsonResponse(Status.INTERNAL_SERVER_ERROR).build();
195+
}
196+
notebookServer.broadcastNote(newNote);
197+
notebookServer.broadcastNoteList();
198+
return new JsonResponse<>(Status.CREATED, "", newNote.getId() ).build();
199+
}
200+
149201
/**
150202
* Create new note REST API
151203
* @param message - JSON with new note name

zeppelin-server/src/test/java/org/apache/zeppelin/rest/ZeppelinRestApiTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,83 @@ public void testDeleteNoteBadId() throws IOException {
319319
}
320320

321321

322+
@Test
323+
public void testExportNotebook() throws IOException {
324+
LOG.info("testExportNotebook");
325+
Note note = ZeppelinServer.notebook.createNote();
326+
assertNotNull("can't create new note", note);
327+
note.setName("source note for export");
328+
Paragraph paragraph = note.addParagraph();
329+
Map config = paragraph.getConfig();
330+
config.put("enabled", true);
331+
paragraph.setConfig(config);
332+
paragraph.setText("%md This is my new paragraph in my new note");
333+
note.persist();
334+
String sourceNoteID = note.getId();
335+
// Call export Notebook REST API
336+
GetMethod get = httpGet("/notebook/export/" + sourceNoteID);
337+
LOG.info("testNotebookExport \n" + get.getResponseBodyAsString());
338+
assertThat("test notebook export method:", get, isAllowed());
339+
340+
Map<String, Object> resp = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
341+
}.getType());
342+
343+
String exportJSON = (String) resp.get("body");
344+
assertNotNull("Can not find new notejson", exportJSON);
345+
LOG.info("export JSON:=" + exportJSON);
346+
ZeppelinServer.notebook.removeNote(sourceNoteID);
347+
get.releaseConnection();
348+
349+
}
350+
351+
@Test
352+
public void testImportNotebook() throws IOException {
353+
Map<String, Object> resp;
354+
String noteName="source note for import";
355+
LOG.info("testImortNotebook");
356+
//create test notebook
357+
Note note = ZeppelinServer.notebook.createNote();
358+
assertNotNull("can't create new note", note);
359+
note.setName(noteName);
360+
Paragraph paragraph = note.addParagraph();
361+
Map config = paragraph.getConfig();
362+
config.put("enabled", true);
363+
paragraph.setConfig(config);
364+
paragraph.setText("%md This is my new paragraph in my new note");
365+
note.persist();
366+
String sourceNoteID = note.getId();
367+
// get note content as JSON
368+
String oldJson = getNoteContent(sourceNoteID);
369+
//call notebook put
370+
PutMethod importPut = httpPut("/notebook/import/" , oldJson);
371+
assertThat(importPut, isCreated());
372+
resp = gson.fromJson(importPut.getResponseBodyAsString(), new TypeToken<Map<String, Object>>(){}.getType());
373+
String importId = (String) resp.get("body");
374+
375+
assertNotNull("Did not get back a notebook id in body", importId);
376+
Note newNote=ZeppelinServer.notebook.getNote(importId);
377+
assertEquals("Compare note names", noteName, newNote.getName());
378+
assertEquals("Compare paragraphs count", note.getParagraphs().size(), newNote.getParagraphs().size());
379+
//cleanup
380+
ZeppelinServer.notebook.removeNote(note.getId());
381+
ZeppelinServer.notebook.removeNote(newNote.getId());
382+
importPut.releaseConnection();
383+
}
384+
385+
private String getNoteContent(String id) throws IOException {
386+
GetMethod get = httpGet("/notebook/export/" + id);
387+
assertThat(get, isAllowed());
388+
get.addRequestHeader("Origin", "http://localhost");
389+
Map<String, Object> resp = gson.fromJson(get.getResponseBodyAsString(),
390+
new TypeToken<Map<String, Object>>() {
391+
}.getType());
392+
assertEquals(200, get.getStatusCode());
393+
String body = resp.get("body").toString();
394+
// System.out.println("Body is " + body);
395+
get.releaseConnection();
396+
return body;
397+
}
398+
322399
private void testDeleteNotebook(String notebookId) throws IOException {
323400

324401
DeleteMethod delete = httpDelete(("/notebook/" + notebookId));

0 commit comments

Comments
 (0)