/** * Delete paragraph REST API * * @param * @return JSON with status.OK * @throws IOException */ @DELETE @Path("{notebookId}/paragraph/{paragraphId}") @ZeppelinApi public Response deleteParagraph( @PathParam("notebookId") String notebookId, @PathParam("paragraphId") String paragraphId) throws IOException { LOG.info("delete paragraph {} {}", notebookId, paragraphId); Note note = notebook.getNote(notebookId); if (note == null) { return new JsonResponse(Status.NOT_FOUND, "note not found.").build(); } Paragraph p = note.getParagraph(paragraphId); if (p == null) { return new JsonResponse(Status.NOT_FOUND, "paragraph not found.").build(); } AuthenticationInfo subject = new AuthenticationInfo(SecurityUtils.getPrincipal()); note.removeParagraph(paragraphId); note.persist(subject); notebookServer.broadcastNote(note); return new JsonResponse(Status.OK, "").build(); }
/** * Run 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/{notebookId}/{paragraphId}") @ZeppelinApi public Response runParagraph( @PathParam("notebookId") String notebookId, @PathParam("paragraphId") String paragraphId, String message) throws IOException, IllegalArgumentException { LOG.info("run paragraph job {} {} {}", notebookId, paragraphId, message); Note note = notebook.getNote(notebookId); if (note == null) { return new JsonResponse<>(Status.NOT_FOUND, "note not found.").build(); } Paragraph paragraph = note.getParagraph(paragraphId); if (paragraph == null) { return new JsonResponse<>(Status.NOT_FOUND, "paragraph not found.").build(); } // handle params if presented if (!StringUtils.isEmpty(message)) { RunParagraphWithParametersRequest request = gson.fromJson(message, RunParagraphWithParametersRequest.class); Map<String, Object> paramsForUpdating = request.getParams(); if (paramsForUpdating != null) { paragraph.settings.getParams().putAll(paramsForUpdating); AuthenticationInfo subject = new AuthenticationInfo(SecurityUtils.getPrincipal()); note.persist(subject); } } note.run(paragraph.getId()); return new JsonResponse<>(Status.OK).build(); }
/** * Insert paragraph REST API * * @param message - JSON containing paragraph's information * @return JSON with status.OK * @throws IOException */ @POST @Path("{notebookId}/paragraph") @ZeppelinApi public Response insertParagraph(@PathParam("notebookId") String notebookId, String message) throws IOException { LOG.info("insert paragraph {} {}", notebookId, message); Note note = notebook.getNote(notebookId); if (note == null) { return new JsonResponse(Status.NOT_FOUND, "note not found.").build(); } NewParagraphRequest request = gson.fromJson(message, NewParagraphRequest.class); Paragraph p; Double indexDouble = request.getIndex(); if (indexDouble == null) { p = note.addParagraph(); } else { p = note.insertParagraph(indexDouble.intValue()); } p.setTitle(request.getTitle()); p.setText(request.getText()); AuthenticationInfo subject = new AuthenticationInfo(SecurityUtils.getPrincipal()); note.persist(subject); notebookServer.broadcastNote(note); return new JsonResponse(Status.CREATED, "", p.getId()).build(); }
/** * Move paragraph REST API * * @param newIndex - new index to move * @return JSON with status.OK * @throws IOException */ @POST @Path("{notebookId}/paragraph/{paragraphId}/move/{newIndex}") @ZeppelinApi public Response moveParagraph( @PathParam("notebookId") String notebookId, @PathParam("paragraphId") String paragraphId, @PathParam("newIndex") String newIndex) throws IOException { LOG.info("move paragraph {} {} {}", notebookId, paragraphId, newIndex); Note note = notebook.getNote(notebookId); if (note == null) { return new JsonResponse(Status.NOT_FOUND, "note not found.").build(); } Paragraph p = note.getParagraph(paragraphId); if (p == null) { return new JsonResponse(Status.NOT_FOUND, "paragraph not found.").build(); } 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(); } }
/** * 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 notebook by JSON {}", message); NewNotebookRequest request = gson.fromJson(message, NewNotebookRequest.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(); p.setTitle(paragraphRequest.getTitle()); p.setText(paragraphRequest.getText()); } } note.addParagraph(); // add one paragraph to the last String noteName = request.getName(); if (noteName.isEmpty()) { noteName = "Note " + note.getId(); } note.setName(noteName); note.persist(subject); notebookServer.broadcastNote(note); notebookServer.broadcastNoteList(subject); return new JsonResponse<>(Status.CREATED, "", note.getId()).build(); }
/** set note authorization information */ @PUT @Path("{noteId}/permissions") @ZeppelinApi public Response putNotePermissions(@PathParam("noteId") String noteId, String req) throws IOException { HashMap<String, HashSet> permMap = gson.fromJson(req, new TypeToken<HashMap<String, HashSet>>() {}.getType()); Note note = notebook.getNote(noteId); String principal = SecurityUtils.getPrincipal(); HashSet<String> roles = SecurityUtils.getRoles(); LOG.info( "Set permissions {} {} {} {} {}", noteId, principal, permMap.get("owners"), permMap.get("readers"), permMap.get("writers")); HashSet<String> userAndRoles = new HashSet<String>(); userAndRoles.add(principal); userAndRoles.addAll(roles); if (!notebookAuthorization.isOwner(noteId, userAndRoles)) { return new JsonResponse<>( Status.FORBIDDEN, ownerPermissionError(userAndRoles, notebookAuthorization.getOwners(noteId))) .build(); } HashSet readers = permMap.get("readers"); HashSet owners = permMap.get("owners"); HashSet writers = permMap.get("writers"); // Set readers, if writers and owners is empty -> set to user requesting the change if (readers != null && !readers.isEmpty()) { if (writers.isEmpty()) { writers = Sets.newHashSet(SecurityUtils.getPrincipal()); } if (owners.isEmpty()) { owners = Sets.newHashSet(SecurityUtils.getPrincipal()); } } // Set writers, if owners is empty -> set to user requesting the change if (writers != null && !writers.isEmpty()) { if (owners.isEmpty()) { owners = Sets.newHashSet(SecurityUtils.getPrincipal()); } } notebookAuthorization.setReaders(noteId, readers); notebookAuthorization.setWriters(noteId, writers); notebookAuthorization.setOwners(noteId, owners); LOG.debug( "After set permissions {} {} {}", notebookAuthorization.getOwners(noteId), notebookAuthorization.getReaders(noteId), notebookAuthorization.getWriters(noteId)); AuthenticationInfo subject = new AuthenticationInfo(SecurityUtils.getPrincipal()); note.persist(subject); notebookServer.broadcastNote(note); return new JsonResponse<>(Status.OK).build(); }
/** * Clone existing note. * * @param sourceNoteID - the note ID to clone * @param newNoteName - the name of the new note * @return noteId * @throws IOException, CloneNotSupportedException, IllegalArgumentException */ public Note cloneNote(String sourceNoteID, String newNoteName, AuthenticationInfo subject) throws IOException, CloneNotSupportedException, IllegalArgumentException { Note sourceNote = getNote(sourceNoteID); if (sourceNote == null) { throw new IllegalArgumentException(sourceNoteID + "not found"); } Note newNote = createNote(subject); if (newNoteName != null) { newNote.setName(newNoteName); } else { newNote.setName("Note " + newNote.getId()); } // Copy the interpreter bindings List<String> boundInterpreterSettingsIds = getBindedInterpreterSettingsIds(sourceNote.getId()); bindInterpretersToNote(newNote.getId(), boundInterpreterSettingsIds); List<Paragraph> paragraphs = sourceNote.getParagraphs(); for (Paragraph p : paragraphs) { newNote.addCloneParagraph(p); } notebookIndex.addIndexDoc(newNote); newNote.persist(subject); return newNote; }
/** * import JSON as a new note. * * @param sourceJson - the note JSON to import * @param noteName - the name of the new note * @return notebook ID * @throws IOException */ public Note importNote(String sourceJson, String noteName, AuthenticationInfo subject) throws IOException { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setPrettyPrinting(); Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create(); JsonReader reader = new JsonReader(new StringReader(sourceJson)); reader.setLenient(true); Note newNote; try { Note oldNote = gson.fromJson(reader, Note.class); newNote = createNote(subject); if (noteName != null) newNote.setName(noteName); else newNote.setName(oldNote.getName()); List<Paragraph> paragraphs = oldNote.getParagraphs(); for (Paragraph p : paragraphs) { newNote.addCloneParagraph(p); } newNote.persist(subject); } catch (IOException e) { logger.error(e.toString(), e); throw e; } return newNote; }
/** * Create new note. * * @return * @throws IOException */ public Note createNote(List<String> interpreterIds) throws IOException { NoteInterpreterLoader intpLoader = new NoteInterpreterLoader(replFactory); Note note = new Note(notebookRepo, intpLoader, jobListenerFactory); intpLoader.setNoteId(note.id()); synchronized (notes) { notes.put(note.id(), note); } if (interpreterIds != null) { bindInterpretersToNote(note.id(), interpreterIds); } note.persist(); return note; }
/** * Create new note. * * @throws IOException */ public Note createNote(List<String> interpreterIds, AuthenticationInfo subject) throws IOException { Note note = new Note(notebookRepo, replFactory, jobListenerFactory, notebookIndex, credentials, this); synchronized (notes) { notes.put(note.getId(), note); } if (interpreterIds != null) { bindInterpretersToNote(note.getId(), interpreterIds); } notebookIndex.addIndexDoc(note); note.persist(subject); fireNoteCreateEvent(note); return note; }