mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
Rest Apis to export/import
This commit is contained in:
parent
c54b8821fc
commit
364535481e
2 changed files with 130 additions and 1 deletions
|
|
@ -52,7 +52,9 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import java.io.StringReader;
|
||||
/**
|
||||
* Rest api endpoint for the noteBook.
|
||||
*/
|
||||
|
|
@ -146,6 +148,56 @@ public class NotebookRestApi {
|
|||
return new JsonResponse<>(Status.OK, "", note).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* export note REST API
|
||||
* @param
|
||||
* @return note JSON with status.OK
|
||||
* @throws IOException
|
||||
*/
|
||||
@GET
|
||||
@Path("export/{id}")
|
||||
public Response exportNoteBook(@PathParam("id") String noteId) {
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.setPrettyPrinting();
|
||||
Gson gson = gsonBuilder.create();
|
||||
Note note = notebook.getNote(noteId);
|
||||
String json = gson.toJson(note);
|
||||
return new JsonResponse(Status.OK, "", json).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* import new note REST API
|
||||
* @param req - notebook Json
|
||||
* @return JSON with new note ID
|
||||
* @throws IOException
|
||||
*/
|
||||
@PUT
|
||||
@Path("import")
|
||||
public Response importNotebook(String req) {
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.setPrettyPrinting();
|
||||
Gson gson = gsonBuilder.create();
|
||||
JsonReader reader = new JsonReader(new StringReader(req));
|
||||
reader.setLenient(true);
|
||||
Note newNote;
|
||||
try {
|
||||
Note oldNote = gson.fromJson(reader, Note.class);
|
||||
newNote = notebook.createNote();
|
||||
newNote.setName(oldNote.getName());
|
||||
List<Paragraph> paragraphs = oldNote.getParagraphs();
|
||||
for (Paragraph p : paragraphs) {
|
||||
newNote.addCloneParagraph(p);
|
||||
}
|
||||
|
||||
newNote.persist();
|
||||
} catch (IOException e) {
|
||||
return new JsonResponse(Status.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
notebookServer.broadcastNote(newNote);
|
||||
notebookServer.broadcastNoteList();
|
||||
return new JsonResponse<>(Status.CREATED, "", newNote.getId() ).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new note REST API
|
||||
* @param message - JSON with new note name
|
||||
|
|
|
|||
|
|
@ -319,6 +319,83 @@ public class ZeppelinRestApiTest extends AbstractTestRestApi {
|
|||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testExportNotebook() throws IOException {
|
||||
LOG.info("testExportNotebook");
|
||||
Note note = ZeppelinServer.notebook.createNote();
|
||||
assertNotNull("can't create new note", note);
|
||||
note.setName("source note for export");
|
||||
Paragraph paragraph = note.addParagraph();
|
||||
Map config = paragraph.getConfig();
|
||||
config.put("enabled", true);
|
||||
paragraph.setConfig(config);
|
||||
paragraph.setText("%md This is my new paragraph in my new note");
|
||||
note.persist();
|
||||
String sourceNoteID = note.getId();
|
||||
// Call export Notebook REST API
|
||||
GetMethod get = httpGet("/notebook/export/" + sourceNoteID);
|
||||
LOG.info("testNotebookExport \n" + get.getResponseBodyAsString());
|
||||
assertThat("test notebook export method:", get, isAllowed());
|
||||
|
||||
Map<String, Object> resp = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
|
||||
}.getType());
|
||||
|
||||
String exportJSON = (String) resp.get("body");
|
||||
assertNotNull("Can not find new notejson", exportJSON);
|
||||
LOG.info("export JSON:=" + exportJSON);
|
||||
ZeppelinServer.notebook.removeNote(sourceNoteID);
|
||||
get.releaseConnection();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImportNotebook() throws IOException {
|
||||
Map<String, Object> resp;
|
||||
String noteName="source note for import";
|
||||
LOG.info("testImortNotebook");
|
||||
//create test notebook
|
||||
Note note = ZeppelinServer.notebook.createNote();
|
||||
assertNotNull("can't create new note", note);
|
||||
note.setName(noteName);
|
||||
Paragraph paragraph = note.addParagraph();
|
||||
Map config = paragraph.getConfig();
|
||||
config.put("enabled", true);
|
||||
paragraph.setConfig(config);
|
||||
paragraph.setText("%md This is my new paragraph in my new note");
|
||||
note.persist();
|
||||
String sourceNoteID = note.getId();
|
||||
// get note content as JSON
|
||||
String oldJson = getNoteContent(sourceNoteID);
|
||||
//call notebook put
|
||||
PutMethod importPut = httpPut("/notebook/import/" , oldJson);
|
||||
assertThat(importPut, isCreated());
|
||||
resp = gson.fromJson(importPut.getResponseBodyAsString(), new TypeToken<Map<String, Object>>(){}.getType());
|
||||
String importId = (String) resp.get("body");
|
||||
|
||||
assertNotNull("Did not get back a notebook id in body", importId);
|
||||
Note newNote=ZeppelinServer.notebook.getNote(importId);
|
||||
assertEquals("Compare note names", noteName, newNote.getName());
|
||||
assertEquals("Compare paragraphs count", note.getParagraphs().size(), newNote.getParagraphs().size());
|
||||
//cleanup
|
||||
ZeppelinServer.notebook.removeNote(note.getId());
|
||||
ZeppelinServer.notebook.removeNote(newNote.getId());
|
||||
importPut.releaseConnection();
|
||||
}
|
||||
|
||||
private String getNoteContent(String id) throws IOException {
|
||||
GetMethod get = httpGet("/notebook/export/" + id);
|
||||
assertThat(get, isAllowed());
|
||||
get.addRequestHeader("Origin", "http://localhost");
|
||||
Map<String, Object> resp = gson.fromJson(get.getResponseBodyAsString(),
|
||||
new TypeToken<Map<String, Object>>() {
|
||||
}.getType());
|
||||
assertEquals(200, get.getStatusCode());
|
||||
String body = resp.get("body").toString();
|
||||
// System.out.println("Body is " + body);
|
||||
get.releaseConnection();
|
||||
return body;
|
||||
}
|
||||
|
||||
private void testDeleteNotebook(String notebookId) throws IOException {
|
||||
|
||||
DeleteMethod delete = httpDelete(("/notebook/" + notebookId));
|
||||
|
|
|
|||
Loading…
Reference in a new issue