Search in sources :

Example 1 with Paragraph

use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.

the class NotebookRestApi method runParagraphSynchronously.

/**
   * Run synchronously a paragraph REST API
   *
   * @param noteId - noteId
   * @param paragraphId - paragraphId
   * @param message - JSON with params if user wants to update dynamic form's value
   *                null, empty string, empty json if user doesn't want to update
   *
   * @return JSON with status.OK
   * @throws IOException, IllegalArgumentException
   */
@POST
@Path("run/{noteId}/{paragraphId}")
@ZeppelinApi
public Response runParagraphSynchronously(@PathParam("noteId") String noteId, @PathParam("paragraphId") String paragraphId, String message) throws IOException, IllegalArgumentException {
    LOG.info("run paragraph synchronously {} {} {}", noteId, paragraphId, message);
    Note note = notebook.getNote(noteId);
    checkIfNoteIsNotNull(note);
    checkIfUserCanWrite(noteId, "Insufficient privileges you cannot run paragraph");
    Paragraph paragraph = note.getParagraph(paragraphId);
    checkIfParagraphIsNotNull(paragraph);
    // handle params if presented
    handleParagraphParams(message, note, paragraph);
    if (paragraph.getListener() == null) {
        note.initializeJobListenerForParagraph(paragraph);
    }
    paragraph.run();
    final InterpreterResult result = paragraph.getResult();
    if (result.code() == InterpreterResult.Code.SUCCESS) {
        return new JsonResponse<>(Status.OK, result).build();
    } else {
        return new JsonResponse<>(Status.INTERNAL_SERVER_ERROR, result).build();
    }
}
Also used : Note(org.apache.zeppelin.notebook.Note) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Paragraph(org.apache.zeppelin.notebook.Paragraph) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

Example 2 with Paragraph

use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.

the class NotebookRestApi method runParagraph.

/**
   * Run asynchronously paragraph job REST API
   *
   * @param message - JSON with params if user wants to update dynamic form's value
   *                null, empty string, empty json if user doesn't want to update
   * @return JSON with status.OK
   * @throws IOException, IllegalArgumentException
   */
@POST
@Path("job/{noteId}/{paragraphId}")
@ZeppelinApi
public Response runParagraph(@PathParam("noteId") String noteId, @PathParam("paragraphId") String paragraphId, String message) throws IOException, IllegalArgumentException {
    LOG.info("run paragraph job asynchronously {} {} {}", noteId, paragraphId, message);
    Note note = notebook.getNote(noteId);
    checkIfNoteIsNotNull(note);
    checkIfUserCanWrite(noteId, "Insufficient privileges you cannot run job for this note");
    Paragraph paragraph = note.getParagraph(paragraphId);
    checkIfParagraphIsNotNull(paragraph);
    // handle params if presented
    handleParagraphParams(message, note, paragraph);
    AuthenticationInfo subject = new AuthenticationInfo(SecurityUtils.getPrincipal());
    paragraph.setAuthenticationInfo(subject);
    note.persist(subject);
    note.run(paragraph.getId());
    return new JsonResponse<>(Status.OK).build();
}
Also used : Note(org.apache.zeppelin.notebook.Note) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) Paragraph(org.apache.zeppelin.notebook.Paragraph) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

Example 3 with Paragraph

use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.

the class NotebookRestApi method createNote.

/**
   * Create new note REST API
   *
   * @param message - JSON with new note name
   * @return JSON with new note ID
   * @throws IOException
   */
@POST
@Path("/")
@ZeppelinApi
public Response createNote(String message) throws IOException {
    LOG.info("Create new note by JSON {}", message);
    NewNoteRequest request = gson.fromJson(message, NewNoteRequest.class);
    AuthenticationInfo subject = new AuthenticationInfo(SecurityUtils.getPrincipal());
    Note note = notebook.createNote(subject);
    List<NewParagraphRequest> initialParagraphs = request.getParagraphs();
    if (initialParagraphs != null) {
        for (NewParagraphRequest paragraphRequest : initialParagraphs) {
            Paragraph p = note.addParagraph(subject);
            p.setTitle(paragraphRequest.getTitle());
            p.setText(paragraphRequest.getText());
        }
    }
    // add one paragraph to the last
    note.addParagraph(subject);
    String noteName = request.getName();
    if (noteName.isEmpty()) {
        noteName = "Note " + note.getId();
    }
    note.setName(noteName);
    note.persist(subject);
    notebookServer.broadcastNote(note);
    notebookServer.broadcastNoteList(subject, SecurityUtils.getRoles());
    return new JsonResponse<>(Status.OK, "", note.getId()).build();
}
Also used : NewNoteRequest(org.apache.zeppelin.rest.message.NewNoteRequest) NewParagraphRequest(org.apache.zeppelin.rest.message.NewParagraphRequest) Note(org.apache.zeppelin.notebook.Note) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) Paragraph(org.apache.zeppelin.notebook.Paragraph) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

Example 4 with Paragraph

use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.

the class NotebookRestApi method moveParagraph.

/**
   * Move paragraph REST API
   *
   * @param newIndex - new index to move
   * @return JSON with status.OK
   * @throws IOException
   */
@POST
@Path("{noteId}/paragraph/{paragraphId}/move/{newIndex}")
@ZeppelinApi
public Response moveParagraph(@PathParam("noteId") String noteId, @PathParam("paragraphId") String paragraphId, @PathParam("newIndex") String newIndex) throws IOException {
    LOG.info("move paragraph {} {} {}", noteId, paragraphId, newIndex);
    Note note = notebook.getNote(noteId);
    checkIfNoteIsNotNull(note);
    checkIfUserCanWrite(noteId, "Insufficient privileges you cannot move paragraph");
    Paragraph p = note.getParagraph(paragraphId);
    checkIfParagraphIsNotNull(p);
    try {
        note.moveParagraph(paragraphId, Integer.parseInt(newIndex), true);
        AuthenticationInfo subject = new AuthenticationInfo(SecurityUtils.getPrincipal());
        note.persist(subject);
        notebookServer.broadcastNote(note);
        return new JsonResponse(Status.OK, "").build();
    } catch (IndexOutOfBoundsException e) {
        LOG.error("Exception in NotebookRestApi while moveParagraph ", e);
        return new JsonResponse(Status.BAD_REQUEST, "paragraph's new index is out of bound").build();
    }
}
Also used : Note(org.apache.zeppelin.notebook.Note) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) JsonResponse(org.apache.zeppelin.server.JsonResponse) Paragraph(org.apache.zeppelin.notebook.Paragraph) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

Example 5 with Paragraph

use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.

the class NotebookRestApi method deleteParagraph.

/**
   * Delete paragraph REST API
   *
   * @param noteId ID of Note
   * @return JSON with status.OK
   * @throws IOException
   */
@DELETE
@Path("{noteId}/paragraph/{paragraphId}")
@ZeppelinApi
public Response deleteParagraph(@PathParam("noteId") String noteId, @PathParam("paragraphId") String paragraphId) throws IOException {
    LOG.info("delete paragraph {} {}", noteId, paragraphId);
    Note note = notebook.getNote(noteId);
    checkIfNoteIsNotNull(note);
    checkIfUserCanRead(noteId, "Insufficient privileges you cannot remove paragraph from this note");
    Paragraph p = note.getParagraph(paragraphId);
    checkIfParagraphIsNotNull(p);
    AuthenticationInfo subject = new AuthenticationInfo(SecurityUtils.getPrincipal());
    note.removeParagraph(SecurityUtils.getPrincipal(), paragraphId);
    note.persist(subject);
    notebookServer.broadcastNote(note);
    return new JsonResponse(Status.OK, "").build();
}
Also used : Note(org.apache.zeppelin.notebook.Note) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) JsonResponse(org.apache.zeppelin.server.JsonResponse) Paragraph(org.apache.zeppelin.notebook.Paragraph) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi)

Aggregations

Paragraph (org.apache.zeppelin.notebook.Paragraph)75 Note (org.apache.zeppelin.notebook.Note)69 Test (org.junit.Test)40 Map (java.util.Map)30 TypeToken (com.google.gson.reflect.TypeToken)12 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)12 Path (javax.ws.rs.Path)11 ZeppelinApi (org.apache.zeppelin.annotation.ZeppelinApi)11 PostMethod (org.apache.commons.httpclient.methods.PostMethod)9 Message (org.apache.zeppelin.notebook.socket.Message)9 Notebook (org.apache.zeppelin.notebook.Notebook)7 GetMethod (org.apache.commons.httpclient.methods.GetMethod)6 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)6 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)5 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 JsonObject (com.google.gson.JsonObject)4 IOException (java.io.IOException)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4