http://git-wip-us.apache.org/repos/asf/zeppelin/blob/0d746fa2/zeppelin-server/src/main/java/org/apache/zeppelin/service/InterpreterService.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/service/InterpreterService.java b/zeppelin-server/src/main/java/org/apache/zeppelin/service/InterpreterService.java index e97cc09..331f838 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/service/InterpreterService.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/service/InterpreterService.java @@ -154,9 +154,8 @@ public class InterpreterService { if (null != serviceCallback) { try { serviceCallback.onFailure( - new Exception( - "Error while downloading " + request.getName() + " as " + e.getMessage()), - null); + new Exception("Error while downloading " + request.getName() + " as " + + e.getMessage()), null); } catch (IOException e1) { logger.error("ServiceCallback failure", e1); }
http://git-wip-us.apache.org/repos/asf/zeppelin/blob/0d746fa2/zeppelin-server/src/main/java/org/apache/zeppelin/service/JobManagerService.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/service/JobManagerService.java b/zeppelin-server/src/main/java/org/apache/zeppelin/service/JobManagerService.java index 95628c2..374d8ff 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/service/JobManagerService.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/service/JobManagerService.java @@ -17,10 +17,6 @@ package org.apache.zeppelin.service; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; import org.apache.commons.lang3.StringUtils; import org.apache.zeppelin.notebook.Note; import org.apache.zeppelin.notebook.Notebook; @@ -29,7 +25,14 @@ import org.apache.zeppelin.scheduler.Job; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** Service class for JobManager Page */ +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * Service class for JobManager Page + */ public class JobManagerService { private static final Logger LOGGER = LoggerFactory.getLogger(JobManagerService.class); @@ -40,8 +43,9 @@ public class JobManagerService { this.notebook = notebook; } - public List<NoteJobInfo> getNoteJobInfo( - String noteId, ServiceContext context, ServiceCallback<List<NoteJobInfo>> callback) + public List<NoteJobInfo> getNoteJobInfo(String noteId, + ServiceContext context, + ServiceCallback<List<NoteJobInfo>> callback) throws IOException { List<NoteJobInfo> notesJobInfo = new ArrayList<>(); Note jobNote = notebook.getNote(noteId); @@ -50,11 +54,12 @@ public class JobManagerService { return notesJobInfo; } - /** Get all NoteJobInfo after lastUpdateServerUnixTime */ - public List<NoteJobInfo> getNoteJobInfoByUnixTime( - long lastUpdateServerUnixTime, - ServiceContext context, - ServiceCallback<List<NoteJobInfo>> callback) + /** + * Get all NoteJobInfo after lastUpdateServerUnixTime + */ + public List<NoteJobInfo> getNoteJobInfoByUnixTime(long lastUpdateServerUnixTime, + ServiceContext context, + ServiceCallback<List<NoteJobInfo>> callback) throws IOException { List<Note> notes = notebook.getAllNotes(); List<NoteJobInfo> notesJobInfo = new ArrayList<>(); @@ -68,9 +73,9 @@ public class JobManagerService { return notesJobInfo; } - public void removeNoteJobInfo( - String noteId, ServiceContext context, ServiceCallback<List<NoteJobInfo>> callback) - throws IOException { + public void removeNoteJobInfo(String noteId, + ServiceContext context, + ServiceCallback<List<NoteJobInfo>> callback) throws IOException { List<NoteJobInfo> notesJobInfo = new ArrayList<>(); notesJobInfo.add(new NoteJobInfo(noteId, true)); callback.onSuccess(notesJobInfo, context); @@ -86,6 +91,7 @@ public class JobManagerService { } } + public static class ParagraphJobInfo { private String id; private String name; @@ -143,8 +149,8 @@ public class JobManagerService { } private boolean isCron(Note note) { - return note.getConfig().containsKey("cron") - && !StringUtils.isBlank(note.getConfig().get("cron").toString()); + return note.getConfig().containsKey("cron") && + !StringUtils.isBlank(note.getConfig().get("cron").toString()); } public NoteJobInfo(String noteId, boolean isRemoved) { http://git-wip-us.apache.org/repos/asf/zeppelin/blob/0d746fa2/zeppelin-server/src/main/java/org/apache/zeppelin/service/NotebookService.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/service/NotebookService.java b/zeppelin-server/src/main/java/org/apache/zeppelin/service/NotebookService.java index f59fe8b..e7a5f03 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/service/NotebookService.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/service/NotebookService.java @@ -17,14 +17,6 @@ package org.apache.zeppelin.service; -import static org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars.ZEPPELIN_NOTEBOOK_HOMESCREEN; - -import java.io.IOException; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; import org.apache.commons.lang.StringUtils; import org.apache.zeppelin.conf.ZeppelinConfiguration; import org.apache.zeppelin.interpreter.Interpreter; @@ -46,7 +38,18 @@ import org.apache.zeppelin.scheduler.Job; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** Service class for Notebook related operations. */ +import java.io.IOException; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars.ZEPPELIN_NOTEBOOK_HOMESCREEN; + +/** + * Service class for Notebook related operations. + */ public class NotebookService { private static final Logger LOGGER = LoggerFactory.getLogger(NotebookService.class); @@ -61,15 +64,15 @@ public class NotebookService { this.zConf = notebook.getConf(); } - public Note getHomeNote(ServiceContext context, ServiceCallback<Note> callback) - throws IOException { + public Note getHomeNote(ServiceContext context, + ServiceCallback<Note> callback) throws IOException { String noteId = notebook.getConf().getString(ZEPPELIN_NOTEBOOK_HOMESCREEN); Note note = null; if (noteId != null) { note = notebook.getNote(noteId); if (note != null) { - if (!checkPermission( - noteId, Permission.READER, Message.OP.GET_HOME_NOTE, context, callback)) { + if (!checkPermission(noteId, Permission.READER, Message.OP.GET_HOME_NOTE, context, + callback)) { return null; } } @@ -78,15 +81,17 @@ public class NotebookService { return note; } - public Note getNote(String noteId, ServiceContext context, ServiceCallback<Note> callback) - throws IOException { + public Note getNote(String noteId, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { Note note = notebook.getNote(noteId); if (note == null) { callback.onFailure(new NoteNotFoundException(noteId), context); return null; } - if (!checkPermission(noteId, Permission.READER, Message.OP.GET_NOTE, context, callback)) { + if (!checkPermission(noteId, Permission.READER, Message.OP.GET_NOTE, context, + callback)) { return null; } if (note.isPersonalizedMode()) { @@ -96,15 +101,14 @@ public class NotebookService { return note; } - public Note createNote( - String noteName, - String defaultInterpreterGroup, - ServiceContext context, - ServiceCallback<Note> callback) - throws IOException { + + public Note createNote(String noteName, + String defaultInterpreterGroup, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { if (defaultInterpreterGroup == null) { - defaultInterpreterGroup = - zConf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_INTERPRETER_GROUP_DEFAULT); + defaultInterpreterGroup = zConf.getString( + ZeppelinConfiguration.ConfVars.ZEPPELIN_INTERPRETER_GROUP_DEFAULT); } if (StringUtils.isBlank(noteName)) { noteName = "Untitled Note"; @@ -123,8 +127,10 @@ public class NotebookService { } } - public void removeNote(String noteId, ServiceContext context, ServiceCallback<String> callback) - throws IOException { + + public void removeNote(String noteId, + ServiceContext context, + ServiceCallback<String> callback) throws IOException { if (!checkPermission(noteId, Permission.OWNER, Message.OP.DEL_NOTE, context, callback)) { return; } @@ -136,10 +142,9 @@ public class NotebookService { } } - public List<Map<String, String>> listNotes( - boolean needsReload, - ServiceContext context, - ServiceCallback<List<Map<String, String>>> callback) + public List<Map<String, String>> listNotes(boolean needsReload, + ServiceContext context, + ServiceCallback<List<Map<String, String>>> callback) throws IOException { ZeppelinConfiguration conf = notebook.getConf(); @@ -170,9 +175,10 @@ public class NotebookService { return notesInfo; } - public void renameNote( - String noteId, String newNoteName, ServiceContext context, ServiceCallback<Note> callback) - throws IOException { + public void renameNote(String noteId, + String newNoteName, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { if (!checkPermission(noteId, Permission.WRITER, Message.OP.NOTE_RENAME, context, callback)) { return; } @@ -186,37 +192,38 @@ public class NotebookService { } else { callback.onFailure(new NoteNotFoundException(noteId), context); } + } - public Note cloneNote( - String noteId, String newNoteName, ServiceContext context, ServiceCallback<Note> callback) - throws IOException { + public Note cloneNote(String noteId, + String newNoteName, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { Note newNote = notebook.cloneNote(noteId, newNoteName, context.getAutheInfo()); callback.onSuccess(newNote, context); return newNote; } - public Note importNote( - String noteName, String noteJson, ServiceContext context, ServiceCallback<Note> callback) - throws IOException { + public Note importNote(String noteName, + String noteJson, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { Note note = notebook.importNote(noteJson, noteName, context.getAutheInfo()); note.persist(context.getAutheInfo()); callback.onSuccess(note, context); return note; } - public boolean runParagraph( - String noteId, - String paragraphId, - String title, - String text, - Map<String, Object> params, - Map<String, Object> config, - boolean failIfDisabled, - boolean blocking, - ServiceContext context, - ServiceCallback<Paragraph> callback) - throws IOException { + public boolean runParagraph(String noteId, + String paragraphId, + String title, + String text, + Map<String, Object> params, + Map<String, Object> config, + boolean failIfDisabled, + boolean blocking, + ServiceContext context, + ServiceCallback<Paragraph> callback) throws IOException { if (!checkPermission(noteId, Permission.RUNNER, Message.OP.RUN_PARAGRAPH, context, callback)) { return false; @@ -265,14 +272,12 @@ public class NotebookService { } } - public void runAllParagraphs( - String noteId, - List<Map<String, Object>> paragraphs, - ServiceContext context, - ServiceCallback<Paragraph> callback) - throws IOException { - if (!checkPermission( - noteId, Permission.RUNNER, Message.OP.RUN_ALL_PARAGRAPHS, context, callback)) { + public void runAllParagraphs(String noteId, + List<Map<String, Object>> paragraphs, + ServiceContext context, + ServiceCallback<Paragraph> callback) throws IOException { + if (!checkPermission(noteId, Permission.RUNNER, Message.OP.RUN_ALL_PARAGRAPHS, context, + callback)) { return; } @@ -292,22 +297,20 @@ public class NotebookService { Map<String, Object> params = (Map<String, Object>) raw.get("params"); Map<String, Object> config = (Map<String, Object>) raw.get("config"); - if (runParagraph( - noteId, paragraphId, title, text, params, config, false, true, context, callback)) { + if (runParagraph(noteId, paragraphId, title, text, params, config, false, true, + context, callback)) { // stop execution when one paragraph fails. break; } } } - public void cancelParagraph( - String noteId, - String paragraphId, - ServiceContext context, - ServiceCallback<Paragraph> callback) - throws IOException { - if (!checkPermission( - noteId, Permission.RUNNER, Message.OP.CANCEL_PARAGRAPH, context, callback)) { + public void cancelParagraph(String noteId, + String paragraphId, + ServiceContext context, + ServiceCallback<Paragraph> callback) throws IOException { + if (!checkPermission(noteId, Permission.RUNNER, Message.OP.CANCEL_PARAGRAPH, context, + callback)) { return; } Note note = notebook.getNote(noteId); @@ -322,14 +325,13 @@ public class NotebookService { callback.onSuccess(p, context); } - public void moveParagraph( - String noteId, - String paragraphId, - int newIndex, - ServiceContext context, - ServiceCallback<Paragraph> callback) - throws IOException { - if (!checkPermission(noteId, Permission.WRITER, Message.OP.MOVE_PARAGRAPH, context, callback)) { + public void moveParagraph(String noteId, + String paragraphId, + int newIndex, + ServiceContext context, + ServiceCallback<Paragraph> callback) throws IOException { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.MOVE_PARAGRAPH, context, + callback)) { return; } Note note = notebook.getNote(noteId); @@ -340,8 +342,8 @@ public class NotebookService { throw new ParagraphNotFoundException(paragraphId); } if (newIndex >= note.getParagraphCount()) { - callback.onFailure( - new BadRequestException("newIndex " + newIndex + " is out of bounds"), context); + callback.onFailure(new BadRequestException("newIndex " + newIndex + " is out of bounds"), + context); return; } note.moveParagraph(paragraphId, newIndex); @@ -349,14 +351,12 @@ public class NotebookService { callback.onSuccess(note.getParagraph(newIndex), context); } - public void removeParagraph( - String noteId, - String paragraphId, - ServiceContext context, - ServiceCallback<Paragraph> callback) - throws IOException { - if (!checkPermission( - noteId, Permission.WRITER, Message.OP.PARAGRAPH_REMOVE, context, callback)) { + public void removeParagraph(String noteId, + String paragraphId, + ServiceContext context, + ServiceCallback<Paragraph> callback) throws IOException { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.PARAGRAPH_REMOVE, context, + callback)) { return; } Note note = notebook.getNote(noteId); @@ -371,15 +371,13 @@ public class NotebookService { callback.onSuccess(p, context); } - public Paragraph insertParagraph( - String noteId, - int index, - Map<String, Object> config, - ServiceContext context, - ServiceCallback<Paragraph> callback) - throws IOException { - if (!checkPermission( - noteId, Permission.WRITER, Message.OP.INSERT_PARAGRAPH, context, callback)) { + public Paragraph insertParagraph(String noteId, + int index, + Map<String, Object> config, + ServiceContext context, + ServiceCallback<Paragraph> callback) throws IOException { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.INSERT_PARAGRAPH, context, + callback)) { return null; } Note note = notebook.getNote(noteId); @@ -393,9 +391,11 @@ public class NotebookService { return newPara; } - public void restoreNote(String noteId, ServiceContext context, ServiceCallback<Note> callback) - throws IOException { - if (!checkPermission(noteId, Permission.WRITER, Message.OP.RESTORE_NOTE, context, callback)) { + public void restoreNote(String noteId, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.RESTORE_NOTE, context, + callback)) { return; } Note note = notebook.getNote(noteId); @@ -403,7 +403,7 @@ public class NotebookService { callback.onFailure(new NoteNotFoundException(noteId), context); return; } - // restore cron + //restore cron Map<String, Object> config = note.getConfig(); if (config.get("cron") != null) { notebook.refreshCron(note.getId()); @@ -413,25 +413,21 @@ public class NotebookService { String newName = note.getName().replaceFirst(Folder.TRASH_FOLDER_ID + "/", ""); renameNote(noteId, newName, context, callback); } else { - callback.onFailure( - new IOException( - String.format("Trying to restore a note {} " + "which is not in Trash", noteId)), - context); + callback.onFailure(new IOException(String.format("Trying to restore a note {} " + + "which is not in Trash", noteId)), context); } } - public void updateParagraph( - String noteId, - String paragraphId, - String title, - String text, - Map<String, Object> params, - Map<String, Object> config, - ServiceContext context, - ServiceCallback<Paragraph> callback) - throws IOException { - if (!checkPermission( - noteId, Permission.WRITER, Message.OP.COMMIT_PARAGRAPH, context, callback)) { + public void updateParagraph(String noteId, + String paragraphId, + String title, + String text, + Map<String, Object> params, + Map<String, Object> config, + ServiceContext context, + ServiceCallback<Paragraph> callback) throws IOException { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.COMMIT_PARAGRAPH, context, + callback)) { return; } Note note = notebook.getNote(noteId); @@ -460,14 +456,12 @@ public class NotebookService { callback.onSuccess(p, context); } - public void clearParagraphOutput( - String noteId, - String paragraphId, - ServiceContext context, - ServiceCallback<Paragraph> callback) - throws IOException { - if (!checkPermission( - noteId, Permission.WRITER, Message.OP.PARAGRAPH_CLEAR_OUTPUT, context, callback)) { + public void clearParagraphOutput(String noteId, + String paragraphId, + ServiceContext context, + ServiceCallback<Paragraph> callback) throws IOException { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.PARAGRAPH_CLEAR_OUTPUT, context, + callback)) { return; } Note note = notebook.getNote(noteId); @@ -482,8 +476,8 @@ public class NotebookService { } Paragraph returnedParagraph = null; if (note.isPersonalizedMode()) { - returnedParagraph = - note.clearPersonalizedParagraphOutput(paragraphId, context.getAutheInfo().getUser()); + returnedParagraph = note.clearPersonalizedParagraphOutput(paragraphId, + context.getAutheInfo().getUser()); } else { note.clearParagraphOutput(paragraphId); returnedParagraph = note.getParagraph(paragraphId); @@ -491,10 +485,11 @@ public class NotebookService { callback.onSuccess(returnedParagraph, context); } - public void clearAllParagraphOutput( - String noteId, ServiceContext context, ServiceCallback<Note> callback) throws IOException { - if (!checkPermission( - noteId, Permission.WRITER, Message.OP.PARAGRAPH_CLEAR_ALL_OUTPUT, context, callback)) { + public void clearAllParagraphOutput(String noteId, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.PARAGRAPH_CLEAR_ALL_OUTPUT, context, + callback)) { return; } Note note = notebook.getNote(noteId); @@ -507,14 +502,15 @@ public class NotebookService { callback.onSuccess(note, context); } - public void updateNote( - String noteId, - String name, - Map<String, Object> config, - ServiceContext context, - ServiceCallback<Note> callback) - throws IOException { - if (!checkPermission(noteId, Permission.WRITER, Message.OP.NOTE_UPDATE, context, callback)) { + + + public void updateNote(String noteId, + String name, + Map<String, Object> config, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.NOTE_UPDATE, context, + callback)) { return; } @@ -540,11 +536,11 @@ public class NotebookService { callback.onSuccess(note, context); } + private boolean isCronUpdated(Map<String, Object> configA, Map<String, Object> configB) { boolean cronUpdated = false; - if (configA.get("cron") != null - && configB.get("cron") != null - && configA.get("cron").equals(configB.get("cron"))) { + if (configA.get("cron") != null && configB.get("cron") != null && configA.get("cron") + .equals(configB.get("cron"))) { cronUpdated = true; } else if (configA.get("cron") == null && configB.get("cron") == null) { cronUpdated = false; @@ -555,14 +551,12 @@ public class NotebookService { return cronUpdated; } - public void saveNoteForms( - String noteId, - Map<String, Object> noteParams, - ServiceContext context, - ServiceCallback<Note> callback) - throws IOException { - if (!checkPermission( - noteId, Permission.WRITER, Message.OP.SAVE_NOTE_FORMS, context, callback)) { + public void saveNoteForms(String noteId, + Map<String, Object> noteParams, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.SAVE_NOTE_FORMS, context, + callback)) { return; } @@ -577,17 +571,18 @@ public class NotebookService { callback.onSuccess(note, context); } - public void removeNoteForms( - String noteId, String formName, ServiceContext context, ServiceCallback<Note> callback) - throws IOException { + public void removeNoteForms(String noteId, + String formName, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { Note note = notebook.getNote(noteId); if (note == null) { callback.onFailure(new NoteNotFoundException(noteId), context); return; } - if (!checkPermission( - noteId, Permission.WRITER, Message.OP.REMOVE_NOTE_FORMS, context, callback)) { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.REMOVE_NOTE_FORMS, context, + callback)) { return; } @@ -601,8 +596,7 @@ public class NotebookService { String noteId, String commitMessage, ServiceContext context, - ServiceCallback<NotebookRepoWithVersionControl.Revision> callback) - throws IOException { + ServiceCallback<NotebookRepoWithVersionControl.Revision> callback) throws IOException { Note note = notebook.getNote(noteId); if (note == null) { @@ -610,8 +604,8 @@ public class NotebookService { return null; } - if (!checkPermission( - noteId, Permission.WRITER, Message.OP.REMOVE_NOTE_FORMS, context, callback)) { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.REMOVE_NOTE_FORMS, context, + callback)) { return null; } @@ -624,8 +618,7 @@ public class NotebookService { public List<NotebookRepoWithVersionControl.Revision> listRevisionHistory( String noteId, ServiceContext context, - ServiceCallback<List<NotebookRepoWithVersionControl.Revision>> callback) - throws IOException { + ServiceCallback<List<NotebookRepoWithVersionControl.Revision>> callback) throws IOException { Note note = notebook.getNote(noteId); if (note == null) { @@ -645,17 +638,19 @@ public class NotebookService { return revisions; } - public Note setNoteRevision( - String noteId, String revisionId, ServiceContext context, ServiceCallback<Note> callback) - throws IOException { + + public Note setNoteRevision(String noteId, + String revisionId, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { Note note = notebook.getNote(noteId); if (note == null) { callback.onFailure(new NoteNotFoundException(noteId), context); return null; } - if (!checkPermission( - noteId, Permission.WRITER, Message.OP.SET_NOTE_REVISION, context, callback)) { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.SET_NOTE_REVISION, context, + callback)) { return null; } @@ -669,9 +664,10 @@ public class NotebookService { } } - public void getNotebyRevision( - String noteId, String revisionId, ServiceContext context, ServiceCallback<Note> callback) - throws IOException { + public void getNotebyRevision(String noteId, + String revisionId, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { Note note = notebook.getNote(noteId); if (note == null) { @@ -679,16 +675,18 @@ public class NotebookService { return; } - if (!checkPermission(noteId, Permission.READER, Message.OP.NOTE_REVISION, context, callback)) { + if (!checkPermission(noteId, Permission.READER, Message.OP.NOTE_REVISION, context, + callback)) { return; } Note revisionNote = notebook.getNoteByRevision(noteId, revisionId, context.getAutheInfo()); callback.onSuccess(revisionNote, context); } - public void getNoteByRevisionForCompare( - String noteId, String revisionId, ServiceContext context, ServiceCallback<Note> callback) - throws IOException { + public void getNoteByRevisionForCompare(String noteId, + String revisionId, + ServiceContext context, + ServiceCallback<Note> callback) throws IOException { Note note = notebook.getNote(noteId); if (note == null) { @@ -696,8 +694,8 @@ public class NotebookService { return; } - if (!checkPermission( - noteId, Permission.READER, Message.OP.NOTE_REVISION_FOR_COMPARE, context, callback)) { + if (!checkPermission(noteId, Permission.READER, Message.OP.NOTE_REVISION_FOR_COMPARE, context, + callback)) { return; } Note revisionNote = null; @@ -715,8 +713,7 @@ public class NotebookService { String buffer, int cursor, ServiceContext context, - ServiceCallback<List<InterpreterCompletion>> callback) - throws IOException { + ServiceCallback<List<InterpreterCompletion>> callback) throws IOException { Note note = notebook.getNote(noteId); if (note == null) { @@ -724,7 +721,8 @@ public class NotebookService { return null; } - if (!checkPermission(noteId, Permission.WRITER, Message.OP.COMPLETION, context, callback)) { + if (!checkPermission(noteId, Permission.WRITER, Message.OP.COMPLETION, context, + callback)) { return null; } @@ -738,12 +736,10 @@ public class NotebookService { } } - public void getEditorSetting( - String noteId, - String replName, - ServiceContext context, - ServiceCallback<Map<String, Object>> callback) - throws IOException { + public void getEditorSetting(String noteId, + String replName, + ServiceContext context, + ServiceCallback<Map<String, Object>> callback) throws IOException { Note note = notebook.getNote(noteId); if (note == null) { @@ -751,18 +747,11 @@ public class NotebookService { return; } try { - Interpreter intp = - notebook - .getInterpreterFactory() - .getInterpreter( - context.getAutheInfo().getUser(), - noteId, - replName, - notebook.getNote(noteId).getDefaultInterpreterGroup()); - Map<String, Object> settings = - notebook - .getInterpreterSettingManager() - .getEditorSetting(intp, context.getAutheInfo().getUser(), noteId, replName); + Interpreter intp = notebook.getInterpreterFactory().getInterpreter( + context.getAutheInfo().getUser(), noteId, replName, + notebook.getNote(noteId).getDefaultInterpreterGroup()); + Map<String, Object> settings = notebook.getInterpreterSettingManager(). + getEditorSetting(intp, context.getAutheInfo().getUser(), noteId, replName); callback.onSuccess(settings, context); } catch (InterpreterNotFoundException e) { callback.onFailure(new IOException("Fail to find interpreter", e), context); @@ -770,6 +759,7 @@ public class NotebookService { } } + enum Permission { READER, WRITER, @@ -778,8 +768,8 @@ public class NotebookService { } /** - * Return null when it is allowed, otherwise return the error message which could be propagated to - * frontend + * Return null when it is allowed, otherwise return the error message which could be + * propagated to frontend * * @param noteId * @param context @@ -787,13 +777,11 @@ public class NotebookService { * @param op * @return */ - private <T> boolean checkPermission( - String noteId, - Permission permission, - Message.OP op, - ServiceContext context, - ServiceCallback<T> callback) - throws IOException { + private <T> boolean checkPermission(String noteId, + Permission permission, + Message.OP op, + ServiceContext context, + ServiceCallback<T> callback) throws IOException { boolean isAllowed = false; Set<String> allowed = null; switch (permission) { @@ -817,17 +805,9 @@ public class NotebookService { if (isAllowed) { return true; } else { - String errorMsg = - "Insufficient privileges to " - + permission - + " note.\n" - + "Allowed users or roles: " - + allowed - + "\n" - + "But the user " - + context.getAutheInfo().getUser() - + " belongs to: " - + context.getUserAndRoles(); + String errorMsg = "Insufficient privileges to " + permission + " note.\n" + + "Allowed users or roles: " + allowed + "\n" + "But the user " + + context.getAutheInfo().getUser() + " belongs to: " + context.getUserAndRoles(); callback.onFailure(new ForbiddenException(errorMsg), context); return false; } http://git-wip-us.apache.org/repos/asf/zeppelin/blob/0d746fa2/zeppelin-server/src/main/java/org/apache/zeppelin/service/ServiceCallback.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/service/ServiceCallback.java b/zeppelin-server/src/main/java/org/apache/zeppelin/service/ServiceCallback.java index a1ab090..fd5af9e 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/service/ServiceCallback.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/service/ServiceCallback.java @@ -19,12 +19,13 @@ package org.apache.zeppelin.service; import java.io.IOException; -/** This will be used by service classes as callback mechanism. */ +/** + * This will be used by service classes as callback mechanism. + */ public interface ServiceCallback<T> { /** * Called when this service call is starting - * * @param message * @param context * @throws IOException @@ -33,7 +34,6 @@ public interface ServiceCallback<T> { /** * Called when this service call is succeed - * * @param result * @param context * @throws IOException @@ -41,11 +41,11 @@ public interface ServiceCallback<T> { void onSuccess(T result, ServiceContext context) throws IOException; /** - * Called when this service call is failed - * + * Called when this service call is failed * @param ex * @param context * @throws IOException */ void onFailure(Exception ex, ServiceContext context) throws IOException; + } http://git-wip-us.apache.org/repos/asf/zeppelin/blob/0d746fa2/zeppelin-server/src/main/java/org/apache/zeppelin/service/ServiceContext.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/service/ServiceContext.java b/zeppelin-server/src/main/java/org/apache/zeppelin/service/ServiceContext.java index 390801c..3db8bf8 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/service/ServiceContext.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/service/ServiceContext.java @@ -15,12 +15,16 @@ * limitations under the License. */ + package org.apache.zeppelin.service; -import java.util.Set; import org.apache.zeppelin.user.AuthenticationInfo; -/** Context info for Service call */ +import java.util.Set; + +/** + * Context info for Service call + */ public class ServiceContext { private AuthenticationInfo autheInfo; http://git-wip-us.apache.org/repos/asf/zeppelin/blob/0d746fa2/zeppelin-server/src/main/java/org/apache/zeppelin/service/SimpleServiceCallback.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/service/SimpleServiceCallback.java b/zeppelin-server/src/main/java/org/apache/zeppelin/service/SimpleServiceCallback.java index 699f3e2..6957707 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/service/SimpleServiceCallback.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/service/SimpleServiceCallback.java @@ -15,13 +15,18 @@ * limitations under the License. */ + package org.apache.zeppelin.service; -import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** @param <T> */ +import java.io.IOException; + +/** + * + * @param <T> + */ public class SimpleServiceCallback<T> implements ServiceCallback<T> { private static Logger LOGGER = LoggerFactory.getLogger(SimpleServiceCallback.class); @@ -40,4 +45,5 @@ public class SimpleServiceCallback<T> implements ServiceCallback<T> { public void onFailure(Exception ex, ServiceContext context) throws IOException { LOGGER.warn(ex.getMessage()); } + } http://git-wip-us.apache.org/repos/asf/zeppelin/blob/0d746fa2/zeppelin-server/src/main/java/org/apache/zeppelin/socket/ConnectionManager.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/ConnectionManager.java b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/ConnectionManager.java index 34351b5..5d02d9f 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/ConnectionManager.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/ConnectionManager.java @@ -17,22 +17,11 @@ package org.apache.zeppelin.socket; + import com.google.common.collect.Queues; import com.google.common.collect.Sets; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Queue; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; import org.apache.commons.lang.StringUtils; import org.apache.zeppelin.conf.ZeppelinConfiguration; import org.apache.zeppelin.display.GUI; @@ -48,17 +37,30 @@ import org.apache.zeppelin.util.WatcherSecurityKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** Manager class for managing websocket connections */ +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; + +/** + * Manager class for managing websocket connections + */ public class ConnectionManager { private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionManager.class); - private static Gson gson = - new GsonBuilder() - .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") - .registerTypeAdapter(Date.class, new NotebookImportDeserializer()) - .setPrettyPrinting() - .registerTypeAdapterFactory(Input.TypeAdapterFactory) - .create(); + private static Gson gson = new GsonBuilder() + .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") + .registerTypeAdapter(Date.class, new NotebookImportDeserializer()) + .setPrettyPrinting() + .registerTypeAdapterFactory(Input.TypeAdapterFactory).create(); final Queue<NotebookSocket> connectedSockets = new ConcurrentLinkedQueue<>(); // noteId -> connection @@ -67,15 +69,18 @@ public class ConnectionManager { final Map<String, Queue<NotebookSocket>> userSocketMap = new ConcurrentHashMap<>(); /** - * This is a special endpoint in the notebook websoket, Every connection in this Queue will be - * able to watch every websocket event, it doesnt need to be listed into the map of noteSocketMap. - * This can be used to get information about websocket traffic and watch what is going on. + * This is a special endpoint in the notebook websoket, Every connection in this Queue + * will be able to watch every websocket event, it doesnt need to be listed into the map of + * noteSocketMap. This can be used to get information about websocket traffic and watch what + * is going on. */ final Queue<NotebookSocket> watcherSockets = Queues.newConcurrentLinkedQueue(); private HashSet<String> collaborativeModeList = new HashSet<>(); - private Boolean collaborativeModeEnable = - ZeppelinConfiguration.create().isZeppelinNotebookCollaborativeModeEnable(); + private Boolean collaborativeModeEnable = ZeppelinConfiguration + .create() + .isZeppelinNotebookCollaborativeModeEnable(); + public void addConnection(NotebookSocket conn) { connectedSockets.add(conn); @@ -198,6 +203,7 @@ public class ConnectionManager { broadcast(noteId, message); } + protected String serializeMessage(Message m) { return gson.toJson(m); } @@ -239,11 +245,8 @@ public class ConnectionManager { for (NotebookSocket watcher : watcherSockets) { try { watcher.send( - WatcherMessage.builder(noteId) - .subject(subject) - .message(serializeMessage(message)) - .build() - .toJson()); + WatcherMessage.builder(noteId).subject(subject).message(serializeMessage(message)) + .build().toJson()); } catch (IOException e) { LOGGER.error("Cannot broadcast message to watcher", e); } @@ -275,7 +278,9 @@ public class ConnectionManager { } } - /** Send websocket message to all connections regardless of notebook id. */ + /** + * Send websocket message to all connections regardless of notebook id. + */ public void broadcastToAllConnections(String serialized) { broadcastToAllConnectionsExcept(null, serialized); } @@ -304,6 +309,7 @@ public class ConnectionManager { return connectedUsers; } + public void multicastToUser(String user, Message m) { if (!userSocketMap.containsKey(user)) { LOGGER.warn("Multicasting to user {} that is not in connections map", user); @@ -340,15 +346,15 @@ public class ConnectionManager { } } - public void broadcastNoteListExcept( - List<Map<String, String>> notesInfo, AuthenticationInfo subject) { + public void broadcastNoteListExcept(List<Map<String, String>> notesInfo, + AuthenticationInfo subject) { Set<String> userAndRoles; NotebookAuthorization authInfo = NotebookAuthorization.getInstance(); for (String user : userSocketMap.keySet()) { if (subject.getUser().equals(user)) { continue; } - // reloaded already above; parameter - false + //reloaded already above; parameter - false userAndRoles = authInfo.getRoles(user); userAndRoles.add(user); // TODO(zjffdu) is it ok for comment the following line ? @@ -371,12 +377,12 @@ public class ConnectionManager { } } - public void broadcastParagraphs( - Map<String, Paragraph> userParagraphMap, Paragraph defaultParagraph) { + public void broadcastParagraphs(Map<String, Paragraph> userParagraphMap, + Paragraph defaultParagraph) { if (null != userParagraphMap) { for (String user : userParagraphMap.keySet()) { - multicastToUser( - user, new Message(Message.OP.PARAGRAPH).put("paragraph", userParagraphMap.get(user))); + multicastToUser(user, + new Message(Message.OP.PARAGRAPH).put("paragraph", userParagraphMap.get(user))); } } } @@ -384,8 +390,7 @@ public class ConnectionManager { private void broadcastNewParagraph(Note note, Paragraph para) { LOGGER.info("Broadcasting paragraph on run call instead of note."); int paraIndex = note.getParagraphs().indexOf(para); - broadcast( - note.getId(), + broadcast(note.getId(), new Message(Message.OP.PARAGRAPH_ADDED).put("paragraph", para).put("index", paraIndex)); } @@ -401,12 +406,13 @@ public class ConnectionManager { // broadcastNoteListExcept(notesInfo, subject); // } + private void broadcastNoteForms(Note note) { GUI formsSettings = new GUI(); formsSettings.setForms(note.getNoteForms()); formsSettings.setParams(note.getNoteParams()); - broadcast( - note.getId(), new Message(Message.OP.SAVE_NOTE_FORMS).put("formsData", formsSettings)); + broadcast(note.getId(), new Message(Message.OP.SAVE_NOTE_FORMS) + .put("formsData", formsSettings)); } public void switchConnectionToWatcher(NotebookSocket conn) { @@ -430,7 +436,7 @@ public class ConnectionManager { private boolean isSessionAllowedToSwitchToWatcher(NotebookSocket session) { String watcherSecurityKey = session.getRequest().getHeader(WatcherSecurityKey.HTTP_HEADER); - return !(StringUtils.isBlank(watcherSecurityKey) - || !watcherSecurityKey.equals(WatcherSecurityKey.getKey())); + return !(StringUtils.isBlank(watcherSecurityKey) || !watcherSecurityKey + .equals(WatcherSecurityKey.getKey())); } }
