http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java index 2178006..ef43625 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java @@ -16,12 +16,39 @@ */ package org.apache.zeppelin.socket; +import com.google.common.base.Strings; +import com.google.common.collect.Queues; +import com.google.common.collect.Sets; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + +import org.apache.commons.lang.StringUtils; +import org.eclipse.jetty.websocket.servlet.WebSocketServlet; +import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; +import org.joda.time.DateTime; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; +import org.quartz.SchedulerException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.IOException; import java.net.URISyntaxException; import java.net.UnknownHostException; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +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 java.util.regex.Matcher; @@ -29,13 +56,22 @@ import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; -import org.apache.commons.lang.StringUtils; import org.apache.zeppelin.conf.ZeppelinConfiguration; import org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars; -import org.apache.zeppelin.display.*; +import org.apache.zeppelin.display.AngularObject; +import org.apache.zeppelin.display.AngularObjectRegistry; +import org.apache.zeppelin.display.AngularObjectRegistryListener; +import org.apache.zeppelin.display.GUI; +import org.apache.zeppelin.display.Input; import org.apache.zeppelin.helium.ApplicationEventListener; import org.apache.zeppelin.helium.HeliumPackage; -import org.apache.zeppelin.interpreter.*; +import org.apache.zeppelin.interpreter.Interpreter; +import org.apache.zeppelin.interpreter.InterpreterContextRunner; +import org.apache.zeppelin.interpreter.InterpreterGroup; +import org.apache.zeppelin.interpreter.InterpreterNotFoundException; +import org.apache.zeppelin.interpreter.InterpreterResult; +import org.apache.zeppelin.interpreter.InterpreterResultMessage; +import org.apache.zeppelin.interpreter.InterpreterSetting; import org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry; import org.apache.zeppelin.interpreter.remote.RemoteInterpreterProcessListener; import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion; @@ -62,21 +98,6 @@ import org.apache.zeppelin.user.AuthenticationInfo; import org.apache.zeppelin.util.WatcherSecurityKey; import org.apache.zeppelin.utils.InterpreterBindingUtils; import org.apache.zeppelin.utils.SecurityUtils; -import org.eclipse.jetty.websocket.servlet.WebSocketServlet; -import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; -import org.joda.time.DateTime; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; -import org.quartz.SchedulerException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.base.Strings; -import com.google.common.collect.Queues; -import com.google.common.collect.Sets; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; /** * Zeppelin websocket service. @@ -86,13 +107,13 @@ public class NotebookServer extends WebSocketServlet RemoteInterpreterProcessListener, ApplicationEventListener { /** - * Job manager service type + * Job manager service type. */ - protected enum JOB_MANAGER_SERVICE { + protected enum JobManagerService { JOB_MANAGER_PAGE("JOB_MANAGER_PAGE"); private String serviceTypeKey; - JOB_MANAGER_SERVICE(String serviceType) { + JobManagerService(String serviceType) { this.serviceTypeKey = serviceType; } @@ -133,9 +154,7 @@ public class NotebookServer extends WebSocketServlet public boolean checkOrigin(HttpServletRequest request, String origin) { try { return SecurityUtils.isValidOrigin(origin, ZeppelinConfiguration.create()); - } catch (UnknownHostException e) { - LOG.error(e.toString(), e); - } catch (URISyntaxException e) { + } catch (UnknownHostException | URISyntaxException e) { LOG.error(e.toString(), e); } return false; @@ -207,7 +226,7 @@ public class NotebookServer extends WebSocketServlet new AuthenticationInfo(messagereceived.principal, messagereceived.roles, messagereceived.ticket); - /** Lets be elegant here */ + // Lets be elegant here switch (messagereceived.op) { case LIST_NOTES: unicastNoteList(conn, subject, userAndRoles); @@ -535,7 +554,7 @@ public class NotebookServer extends WebSocketServlet } public void unicastNoteJobInfo(NotebookSocket conn, Message fromMessage) throws IOException { - addConnectionToNote(JOB_MANAGER_SERVICE.JOB_MANAGER_PAGE.getKey(), conn); + addConnectionToNote(JobManagerService.JOB_MANAGER_PAGE.getKey(), conn); AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal); List<Map<String, Object>> noteJobs = notebook().getJobListByUnixTime(false, 0, subject); Map<String, Object> response = new HashMap<>(); @@ -549,7 +568,7 @@ public class NotebookServer extends WebSocketServlet public void broadcastUpdateNoteJobInfo(long lastUpdateUnixTime) throws IOException { List<Map<String, Object>> noteJobs = new LinkedList<>(); Notebook notebookObject = notebook(); - List<Map<String, Object>> jobNotes = null; + List<Map<String, Object>> jobNotes; if (notebookObject != null) { jobNotes = notebook().getJobListByUnixTime(false, lastUpdateUnixTime, null); noteJobs = jobNotes == null ? noteJobs : jobNotes; @@ -559,12 +578,12 @@ public class NotebookServer extends WebSocketServlet response.put("lastResponseUnixTime", System.currentTimeMillis()); response.put("jobs", noteJobs != null ? noteJobs : new LinkedList<>()); - broadcast(JOB_MANAGER_SERVICE.JOB_MANAGER_PAGE.getKey(), + broadcast(JobManagerService.JOB_MANAGER_PAGE.getKey(), new Message(OP.LIST_UPDATE_NOTE_JOBS).put("noteRunningJobs", response)); } public void unsubscribeNoteJobInfo(NotebookSocket conn) { - removeConnectionFromNote(JOB_MANAGER_SERVICE.JOB_MANAGER_PAGE.getKey(), conn); + removeConnectionFromNote(JobManagerService.JOB_MANAGER_PAGE.getKey(), conn); } public void saveInterpreterBindings(NotebookSocket conn, Message fromMessage) { @@ -572,8 +591,7 @@ public class NotebookServer extends WebSocketServlet try { List<String> settingIdList = gson.fromJson(String.valueOf(fromMessage.data.get("selectedSettingIds")), - new TypeToken<ArrayList<String>>() { - }.getType()); + new TypeToken<ArrayList<String>>() {}.getType()); AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal); notebook().bindInterpretersToNote(subject.getUser(), noteId, settingIdList); broadcastInterpreterBindings(noteId, @@ -592,8 +610,7 @@ public class NotebookServer extends WebSocketServlet } public List<Map<String, String>> generateNotesInfo(boolean needsReload, - AuthenticationInfo subject, Set<String> userAndRoles) { - + AuthenticationInfo subject, Set<String> userAndRoles) { Notebook notebook = notebook(); ZeppelinConfiguration conf = notebook.getConf(); @@ -661,7 +678,7 @@ public class NotebookServer extends WebSocketServlet } public void broadcastParagraphs(Map<String, Paragraph> userParagraphMap, - Paragraph defaultParagraph) { + Paragraph defaultParagraph) { if (null != userParagraphMap) { for (String user : userParagraphMap.keySet()) { multicastToUser(user, @@ -689,7 +706,7 @@ public class NotebookServer extends WebSocketServlet } public void unicastNoteList(NotebookSocket conn, AuthenticationInfo subject, - HashSet<String> userAndRoles) { + HashSet<String> userAndRoles) { List<Map<String, String>> notesInfo = generateNotesInfo(false, subject, userAndRoles); unicast(new Message(OP.NOTES_INFO).put("notes", notesInfo), conn); } @@ -707,7 +724,7 @@ public class NotebookServer extends WebSocketServlet } private void broadcastNoteListExcept(List<Map<String, String>> notesInfo, - AuthenticationInfo subject) { + AuthenticationInfo subject) { Set<String> userAndRoles; NotebookAuthorization authInfo = NotebookAuthorization.getInstance(); for (String user : userConnectedSockets.keySet()) { @@ -723,7 +740,7 @@ public class NotebookServer extends WebSocketServlet } void permissionError(NotebookSocket conn, String op, String userName, Set<String> userAndRoles, - Set<String> allowed) throws IOException { + Set<String> allowed) throws IOException { LOG.info("Cannot {}. Connection readers {}. Allowed readers {}", op, userAndRoles, allowed); conn.send(serializeMessage(new Message(OP.AUTH_INFO).put("info", @@ -735,12 +752,9 @@ public class NotebookServer extends WebSocketServlet /** * @return false if user doesn't have reader permission for this paragraph */ - private boolean hasParagraphReaderPermission(NotebookSocket conn, - Notebook notebook, String noteId, - HashSet<String> userAndRoles, - String principal, String op) - throws IOException { - + private boolean hasParagraphReaderPermission(NotebookSocket conn, Notebook notebook, + String noteId, HashSet<String> userAndRoles, String principal, String op) + throws IOException { NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization(); if (!notebookAuthorization.isReader(noteId, userAndRoles)) { permissionError(conn, op, principal, userAndRoles, @@ -754,12 +768,9 @@ public class NotebookServer extends WebSocketServlet /** * @return false if user doesn't have runner permission for this paragraph */ - private boolean hasParagraphRunnerPermission(NotebookSocket conn, - Notebook notebook, String noteId, - HashSet<String> userAndRoles, - String principal, String op) - throws IOException { - + private boolean hasParagraphRunnerPermission(NotebookSocket conn, Notebook notebook, + String noteId, HashSet<String> userAndRoles, String principal, String op) + throws IOException { NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization(); if (!notebookAuthorization.isRunner(noteId, userAndRoles)) { permissionError(conn, op, principal, userAndRoles, @@ -773,12 +784,9 @@ public class NotebookServer extends WebSocketServlet /** * @return false if user doesn't have writer permission for this paragraph */ - private boolean hasParagraphWriterPermission(NotebookSocket conn, - Notebook notebook, String noteId, - HashSet<String> userAndRoles, - String principal, String op) - throws IOException { - + private boolean hasParagraphWriterPermission(NotebookSocket conn, Notebook notebook, + String noteId, HashSet<String> userAndRoles, String principal, String op) + throws IOException { NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization(); if (!notebookAuthorization.isWriter(noteId, userAndRoles)) { permissionError(conn, op, principal, userAndRoles, @@ -792,12 +800,8 @@ public class NotebookServer extends WebSocketServlet /** * @return false if user doesn't have owner permission for this paragraph */ - private boolean hasParagraphOwnerPermission(NotebookSocket conn, - Notebook notebook, String noteId, - HashSet<String> userAndRoles, - String principal, String op) - throws IOException { - + private boolean hasParagraphOwnerPermission(NotebookSocket conn, Notebook notebook, String noteId, + HashSet<String> userAndRoles, String principal, String op) throws IOException { NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization(); if (!notebookAuthorization.isOwner(noteId, userAndRoles)) { permissionError(conn, op, principal, userAndRoles, @@ -809,8 +813,7 @@ public class NotebookServer extends WebSocketServlet } private void sendNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { - + Message fromMessage) throws IOException { LOG.info("New operation from {} : {} : {} : {} : {}", conn.getRequest().getRemoteAddr(), conn.getRequest().getRemotePort(), fromMessage.principal, fromMessage.op, fromMessage.get("id")); @@ -843,7 +846,7 @@ public class NotebookServer extends WebSocketServlet } private void sendHomeNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { String noteId = notebook.getConf().getString(ConfVars.ZEPPELIN_NOTEBOOK_HOMESCREEN); String user = fromMessage.principal; @@ -868,7 +871,7 @@ public class NotebookServer extends WebSocketServlet } private void updateNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { String noteId = (String) fromMessage.get("id"); String name = (String) fromMessage.get("name"); Map<String, Object> config = (Map<String, Object>) fromMessage.get("config"); @@ -907,7 +910,7 @@ public class NotebookServer extends WebSocketServlet } private void updatePersonalizedMode(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) throws SchedulerException, IOException { + Notebook notebook, Message fromMessage) throws IOException { String noteId = (String) fromMessage.get("id"); String personalized = (String) fromMessage.get("personalized"); boolean isPersonalized = personalized.equals("true") ? true : false; @@ -930,15 +933,13 @@ public class NotebookServer extends WebSocketServlet } } - private void renameNote(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) - throws SchedulerException, IOException { + private void renameNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, + Message fromMessage) throws IOException { renameNote(conn, userAndRoles, notebook, fromMessage, "rename"); } - private void renameNote(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage, String op) - throws SchedulerException, IOException { + private void renameNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, + Message fromMessage, String op) throws IOException { String noteId = (String) fromMessage.get("id"); String name = (String) fromMessage.get("name"); @@ -963,15 +964,13 @@ public class NotebookServer extends WebSocketServlet } } - private void renameFolder(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) - throws SchedulerException, IOException { + private void renameFolder(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, + Message fromMessage) throws IOException { renameFolder(conn, userAndRoles, notebook, fromMessage, "rename"); } - private void renameFolder(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage, String op) - throws SchedulerException, IOException { + private void renameFolder(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, + Message fromMessage, String op) throws IOException { String oldFolderId = (String) fromMessage.get("id"); String newFolderId = (String) fromMessage.get("name"); @@ -1017,11 +1016,11 @@ public class NotebookServer extends WebSocketServlet } private void createNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message message) throws IOException { + Message message) throws IOException { AuthenticationInfo subject = new AuthenticationInfo(message.principal); try { - Note note = null; + Note note; String defaultInterpreterId = (String) message.get("defaultInterpreterId"); if (!StringUtils.isEmpty(defaultInterpreterId)) { @@ -1049,7 +1048,7 @@ public class NotebookServer extends WebSocketServlet } note.persist(subject); - addConnectionToNote(note.getId(), (NotebookSocket) conn); + addConnectionToNote(note.getId(), conn); conn.send(serializeMessage(new Message(OP.NEW_NOTE).put("note", note))); } catch (IOException e) { LOG.error("Exception from createNote", e); @@ -1062,7 +1061,7 @@ public class NotebookServer extends WebSocketServlet } private void removeNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { String noteId = (String) fromMessage.get("id"); if (noteId == null) { return; @@ -1079,9 +1078,8 @@ public class NotebookServer extends WebSocketServlet broadcastNoteList(subject, userAndRoles); } - private void removeFolder(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) - throws SchedulerException, IOException { + private void removeFolder(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, + Message fromMessage) throws IOException { String folderId = (String) fromMessage.get("id"); if (folderId == null) { return; @@ -1105,9 +1103,8 @@ public class NotebookServer extends WebSocketServlet broadcastNoteList(subject, userAndRoles); } - private void moveNoteToTrash(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) - throws SchedulerException, IOException { + private void moveNoteToTrash(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, + Message fromMessage) throws SchedulerException, IOException { String noteId = (String) fromMessage.get("id"); if (noteId == null) { return; @@ -1129,8 +1126,7 @@ public class NotebookServer extends WebSocketServlet } private void moveFolderToTrash(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) - throws SchedulerException, IOException { + Notebook notebook, Message fromMessage) throws SchedulerException, IOException { String folderId = (String) fromMessage.get("id"); if (folderId == null) { return; @@ -1158,9 +1154,8 @@ public class NotebookServer extends WebSocketServlet } } - private void restoreNote(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) - throws SchedulerException, IOException { + private void restoreNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, + Message fromMessage) throws SchedulerException, IOException { String noteId = (String) fromMessage.get("id"); if (noteId == null) { @@ -1181,9 +1176,8 @@ public class NotebookServer extends WebSocketServlet } } - private void restoreFolder(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) - throws SchedulerException, IOException { + private void restoreFolder(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, + Message fromMessage) throws SchedulerException, IOException { String folderId = (String) fromMessage.get("id"); if (folderId == null) { @@ -1213,9 +1207,8 @@ public class NotebookServer extends WebSocketServlet } } - private void restoreAll(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) - throws SchedulerException, IOException { + private void restoreAll(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, + Message fromMessage) throws SchedulerException, IOException { Folder trashFolder = notebook.getFolder(Folder.TRASH_FOLDER_ID); if (trashFolder != null) { fromMessage.data = new HashMap<>(); @@ -1225,16 +1218,15 @@ public class NotebookServer extends WebSocketServlet } } - private void emptyTrash(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) - throws SchedulerException, IOException { + private void emptyTrash(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, + Message fromMessage) throws SchedulerException, IOException { fromMessage.data = new HashMap<>(); fromMessage.put("id", Folder.TRASH_FOLDER_ID); removeFolder(conn, userAndRoles, notebook, fromMessage); } private void updateParagraph(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) throws IOException { + Notebook notebook, Message fromMessage) throws IOException { String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { return; @@ -1281,7 +1273,7 @@ public class NotebookServer extends WebSocketServlet } private void cloneNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException, CloneNotSupportedException { + Message fromMessage) throws IOException { String noteId = getOpenNoteId(conn); String name = (String) fromMessage.get("name"); Note newNote = notebook.cloneNote(noteId, name, new AuthenticationInfo(fromMessage.principal)); @@ -1292,7 +1284,7 @@ public class NotebookServer extends WebSocketServlet } private void clearAllParagraphOutput(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) throws IOException { + Notebook notebook, Message fromMessage) throws IOException { final String noteId = (String) fromMessage.get("id"); if (StringUtils.isBlank(noteId)) { return; @@ -1309,12 +1301,12 @@ public class NotebookServer extends WebSocketServlet } protected Note importNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { Note note = null; if (fromMessage != null) { String noteName = (String) ((Map) fromMessage.get("note")).get("name"); String noteJson = gson.toJson(fromMessage.get("note")); - AuthenticationInfo subject = null; + AuthenticationInfo subject; if (fromMessage.principal != null) { subject = new AuthenticationInfo(fromMessage.principal); } else { @@ -1329,7 +1321,7 @@ public class NotebookServer extends WebSocketServlet } private void removeParagraph(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { final String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { return; @@ -1343,7 +1335,7 @@ public class NotebookServer extends WebSocketServlet final Note note = notebook.getNote(noteId); - /** Don't allow removing paragraph when there is only one paragraph in the Notebook */ + // Don't allow removing paragraph when there is only one paragraph in the Notebook if (note.getParagraphCount() > 1) { AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal); Paragraph para = note.removeParagraph(subject.getUser(), paragraphId); @@ -1356,7 +1348,7 @@ public class NotebookServer extends WebSocketServlet } private void clearParagraphOutput(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) throws IOException { + Notebook notebook, Message fromMessage) throws IOException { final String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { return; @@ -1381,7 +1373,7 @@ public class NotebookServer extends WebSocketServlet } private void completion(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { String paragraphId = (String) fromMessage.get("id"); String buffer = (String) fromMessage.get("buf"); int cursor = (int) Double.parseDouble(fromMessage.get("cursor").toString()); @@ -1404,14 +1396,14 @@ public class NotebookServer extends WebSocketServlet } /** - * When angular object updated from client + * When angular object updated from client. * * @param conn the web socket. * @param notebook the notebook. * @param fromMessage the message. */ private void angularObjectUpdated(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) { + Notebook notebook, Message fromMessage) { String noteId = (String) fromMessage.get("noteId"); String paragraphId = (String) fromMessage.get("paragraphId"); String interpreterGroupId = (String) fromMessage.get("interpreterGroupId"); @@ -1492,12 +1484,11 @@ public class NotebookServer extends WebSocketServlet } /** - * Push the given Angular variable to the target - * interpreter angular registry given a noteId - * and a paragraph id + * Push the given Angular variable to the target interpreter angular registry given a noteId + * and a paragraph id. */ protected void angularObjectClientBind(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) throws Exception { + Notebook notebook, Message fromMessage) throws Exception { String noteId = fromMessage.getType("noteId"); String varName = fromMessage.getType("name"); Object varValue = fromMessage.get("value"); @@ -1527,12 +1518,11 @@ public class NotebookServer extends WebSocketServlet } /** - * Remove the given Angular variable to the target - * interpreter(s) angular registry given a noteId - * and an optional list of paragraph id(s) + * Remove the given Angular variable to the target interpreter(s) angular registry given a noteId + * and an optional list of paragraph id(s). */ protected void angularObjectClientUnbind(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) throws Exception { + Notebook notebook, Message fromMessage) throws Exception { String noteId = fromMessage.getType("noteId"); String varName = fromMessage.getType("name"); String paragraphId = fromMessage.getType("paragraphId"); @@ -1560,7 +1550,7 @@ public class NotebookServer extends WebSocketServlet } private InterpreterGroup findInterpreterGroupForParagraph(Note note, String paragraphId) - throws Exception { + throws Exception { final Paragraph paragraph = note.getParagraph(paragraphId); if (paragraph == null) { throw new IllegalArgumentException("Unknown paragraph with id : " + paragraphId); @@ -1569,9 +1559,8 @@ public class NotebookServer extends WebSocketServlet } private void pushAngularObjectToRemoteRegistry(String noteId, String paragraphId, String varName, - Object varValue, RemoteAngularObjectRegistry remoteRegistry, String interpreterGroupId, - NotebookSocket conn) { - + Object varValue, RemoteAngularObjectRegistry remoteRegistry, String interpreterGroupId, + NotebookSocket conn) { final AngularObject ao = remoteRegistry.addAndNotifyRemoteProcess(varName, varValue, noteId, paragraphId); @@ -1581,7 +1570,8 @@ public class NotebookServer extends WebSocketServlet } private void removeAngularFromRemoteRegistry(String noteId, String paragraphId, String varName, - RemoteAngularObjectRegistry remoteRegistry, String interpreterGroupId, NotebookSocket conn) { + RemoteAngularObjectRegistry remoteRegistry, String interpreterGroupId, + NotebookSocket conn) { final AngularObject ao = remoteRegistry.removeAndNotifyRemoteProcess(varName, noteId, paragraphId); this.broadcastExcept(noteId, new Message(OP.ANGULAR_OBJECT_REMOVE).put("angularObject", ao) @@ -1590,8 +1580,8 @@ public class NotebookServer extends WebSocketServlet } private void pushAngularObjectToLocalRepo(String noteId, String paragraphId, String varName, - Object varValue, AngularObjectRegistry registry, String interpreterGroupId, - NotebookSocket conn) { + Object varValue, AngularObjectRegistry registry, String interpreterGroupId, + NotebookSocket conn) { AngularObject angularObject = registry.get(varName, noteId, paragraphId); if (angularObject == null) { angularObject = registry.add(varName, varValue, noteId, paragraphId); @@ -1606,7 +1596,7 @@ public class NotebookServer extends WebSocketServlet } private void removeAngularObjectFromLocalRepo(String noteId, String paragraphId, String varName, - AngularObjectRegistry registry, String interpreterGroupId, NotebookSocket conn) { + AngularObjectRegistry registry, String interpreterGroupId, NotebookSocket conn) { final AngularObject removed = registry.remove(varName, noteId, paragraphId); if (removed != null) { this.broadcastExcept(noteId, @@ -1617,7 +1607,7 @@ public class NotebookServer extends WebSocketServlet } private void moveParagraph(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { final String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { return; @@ -1640,7 +1630,7 @@ public class NotebookServer extends WebSocketServlet } private String insertParagraph(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) throws IOException { + Notebook notebook, Message fromMessage) throws IOException { final int index = (int) Double.parseDouble(fromMessage.get("index").toString()); String noteId = getOpenNoteId(conn); final Note note = notebook.getNote(noteId); @@ -1666,7 +1656,7 @@ public class NotebookServer extends WebSocketServlet } private void copyParagraph(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { String newParaId = insertParagraph(conn, userAndRoles, notebook, fromMessage); if (newParaId == null) { @@ -1678,7 +1668,7 @@ public class NotebookServer extends WebSocketServlet } private void cancelParagraph(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { final String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { return; @@ -1697,8 +1687,7 @@ public class NotebookServer extends WebSocketServlet } private void runAllParagraphs(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, - Message fromMessage) throws IOException { + Notebook notebook, Message fromMessage) throws IOException { final String noteId = (String) fromMessage.get("noteId"); if (StringUtils.isBlank(noteId)) { return; @@ -1736,9 +1725,7 @@ public class NotebookServer extends WebSocketServlet } private void broadcastSpellExecution(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook, Message fromMessage) - throws IOException { - + Notebook notebook, Message fromMessage) throws IOException { final String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { return; @@ -1792,7 +1779,7 @@ public class NotebookServer extends WebSocketServlet } private void runParagraph(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { final String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { return; @@ -1840,8 +1827,8 @@ public class NotebookServer extends WebSocketServlet /** * @return false if failed to save a note */ - private boolean persistNoteWithAuthInfo(NotebookSocket conn, - Note note, Paragraph p) throws IOException { + private boolean persistNoteWithAuthInfo(NotebookSocket conn, Note note, Paragraph p) + throws IOException { try { note.persist(p.getAuthenticationInfo()); return true; @@ -1855,9 +1842,8 @@ public class NotebookServer extends WebSocketServlet } } - private boolean persistAndExecuteSingleParagraph(NotebookSocket conn, - Note note, Paragraph p, - boolean blocking) throws IOException { + private boolean persistAndExecuteSingleParagraph(NotebookSocket conn, Note note, Paragraph p, + boolean blocking) throws IOException { addNewParagraphIfLastParagraphIsExecuted(note, p); if (!persistNoteWithAuthInfo(conn, note, p)) { return false; @@ -1877,8 +1863,7 @@ public class NotebookServer extends WebSocketServlet } private Paragraph setParagraphUsingMessage(Note note, Message fromMessage, String paragraphId, - String text, String title, Map<String, Object> params, - Map<String, Object> config) { + String text, String title, Map<String, Object> params, Map<String, Object> config) { Paragraph p = note.getParagraph(paragraphId); p.setText(text); p.setTitle(title); @@ -1901,7 +1886,7 @@ public class NotebookServer extends WebSocketServlet } private void sendAllConfigurations(NotebookSocket conn, HashSet<String> userAndRoles, - Notebook notebook) throws IOException { + Notebook notebook) throws IOException { ZeppelinConfiguration conf = notebook.getConf(); Map<String, String> configurations = @@ -1919,7 +1904,7 @@ public class NotebookServer extends WebSocketServlet } private void checkpointNote(NotebookSocket conn, Notebook notebook, Message fromMessage) - throws IOException { + throws IOException { String noteId = (String) fromMessage.get("noteId"); String commitMessage = (String) fromMessage.get("commitMessage"); AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal); @@ -1936,7 +1921,7 @@ public class NotebookServer extends WebSocketServlet } private void listRevisionHistory(NotebookSocket conn, Notebook notebook, Message fromMessage) - throws IOException { + throws IOException { String noteId = (String) fromMessage.get("noteId"); AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal); List<Revision> revisions = notebook.listRevisionHistory(noteId, subject); @@ -1946,8 +1931,7 @@ public class NotebookServer extends WebSocketServlet } private void setNoteRevision(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { - + Message fromMessage) throws IOException { String noteId = (String) fromMessage.get("noteId"); String revisionId = (String) fromMessage.get("revisionId"); AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal); @@ -2013,7 +1997,7 @@ public class NotebookServer extends WebSocketServlet } /** - * This callback is for the paragraph that runs on ZeppelinServer + * This callback is for the paragraph that runs on ZeppelinServer. * * @param output output to append */ @@ -2025,7 +2009,7 @@ public class NotebookServer extends WebSocketServlet } /** - * This callback is for the paragraph that runs on ZeppelinServer + * This callback is for the paragraph that runs on ZeppelinServer. * * @param output output to update (replace) */ @@ -2045,9 +2029,8 @@ public class NotebookServer extends WebSocketServlet } } - /** - * This callback is for the paragraph that runs on ZeppelinServer + * This callback is for the paragraph that runs on ZeppelinServer. */ @Override public void onOutputClear(String noteId, String paragraphId) { @@ -2059,7 +2042,7 @@ public class NotebookServer extends WebSocketServlet } /** - * When application append output + * When application append output. */ @Override public void onOutputAppend(String noteId, String paragraphId, int index, String appId, @@ -2071,7 +2054,7 @@ public class NotebookServer extends WebSocketServlet } /** - * When application update output + * When application update output. */ @Override public void onOutputUpdated(String noteId, String paragraphId, int index, String appId, @@ -2157,17 +2140,15 @@ public class NotebookServer extends WebSocketServlet paragraph.setAuthenticationInfo(subject); noteIns.run(paragraphId); - } catch (Exception e) { throw e; } } /** - * Notebook Information Change event + * Notebook Information Change event. */ public static class NotebookInformationListener implements NotebookEventListener { - private NotebookServer notebookServer; public NotebookInformationListener(NotebookServer notebookServer) { @@ -2208,9 +2189,8 @@ public class NotebookServer extends WebSocketServlet response.put("lastResponseUnixTime", System.currentTimeMillis()); response.put("jobs", notesInfo); - notebookServer.broadcast(JOB_MANAGER_SERVICE.JOB_MANAGER_PAGE.getKey(), + notebookServer.broadcast(JobManagerService.JOB_MANAGER_PAGE.getKey(), new Message(OP.LIST_UPDATE_NOTE_JOBS).put("noteRunningJobs", response)); - } @Override @@ -2221,7 +2201,7 @@ public class NotebookServer extends WebSocketServlet response.put("lastResponseUnixTime", System.currentTimeMillis()); response.put("jobs", notebookJobs); - notebookServer.broadcast(JOB_MANAGER_SERVICE.JOB_MANAGER_PAGE.getKey(), + notebookServer.broadcast(JobManagerService.JOB_MANAGER_PAGE.getKey(), new Message(OP.LIST_UPDATE_NOTE_JOBS).put("noteRunningJobs", response)); } @@ -2233,7 +2213,7 @@ public class NotebookServer extends WebSocketServlet response.put("lastResponseUnixTime", System.currentTimeMillis()); response.put("jobs", notebookJobs); - notebookServer.broadcast(JOB_MANAGER_SERVICE.JOB_MANAGER_PAGE.getKey(), + notebookServer.broadcast(JobManagerService.JOB_MANAGER_PAGE.getKey(), new Message(OP.LIST_UPDATE_NOTE_JOBS).put("noteRunningJobs", response)); } @@ -2246,7 +2226,7 @@ public class NotebookServer extends WebSocketServlet response.put("lastResponseUnixTime", System.currentTimeMillis()); response.put("jobs", notebookJobs); - notebookServer.broadcast(JOB_MANAGER_SERVICE.JOB_MANAGER_PAGE.getKey(), + notebookServer.broadcast(JobManagerService.JOB_MANAGER_PAGE.getKey(), new Message(OP.LIST_UPDATE_NOTE_JOBS).put("noteRunningJobs", response)); } @@ -2258,17 +2238,15 @@ public class NotebookServer extends WebSocketServlet response.put("lastResponseUnixTime", System.currentTimeMillis()); response.put("jobs", notebookJobs); - notebookServer.broadcast(JOB_MANAGER_SERVICE.JOB_MANAGER_PAGE.getKey(), + notebookServer.broadcast(JobManagerService.JOB_MANAGER_PAGE.getKey(), new Message(OP.LIST_UPDATE_NOTE_JOBS).put("noteRunningJobs", response)); } } - /** * Need description here. */ public static class ParagraphListenerImpl implements ParagraphJobListener { - private NotebookServer notebookServer; private Note note; @@ -2319,7 +2297,7 @@ public class NotebookServer extends WebSocketServlet } /** - * This callback is for paragraph that runs on RemoteInterpreterProcess + * This callback is for paragraph that runs on RemoteInterpreterProcess. */ @Override public void onOutputAppend(Paragraph paragraph, int idx, String output) { @@ -2331,7 +2309,7 @@ public class NotebookServer extends WebSocketServlet } /** - * This callback is for paragraph that runs on RemoteInterpreterProcess + * This callback is for paragraph that runs on RemoteInterpreterProcess. */ @Override public void onOutputUpdate(Paragraph paragraph, int idx, InterpreterResultMessage result) { @@ -2442,8 +2420,8 @@ public class NotebookServer extends WebSocketServlet String user = fromMessage.principal; Message resp = new Message(OP.EDITOR_SETTING); resp.put("paragraphId", paragraphId); - Interpreter interpreter = - null; + Interpreter interpreter; + try { interpreter = notebook().getInterpreterFactory().getInterpreter(user, noteId, replName); } catch (InterpreterNotFoundException e) { @@ -2468,8 +2446,7 @@ public class NotebookServer extends WebSocketServlet interpreterSetting.setInfos(metaInfos); } - private void switchConnectionToWatcher(NotebookSocket conn, Message messagereceived) - throws IOException { + private void switchConnectionToWatcher(NotebookSocket conn, Message messagereceived) { if (!isSessionAllowedToSwitchToWatcher(conn)) { LOG.error("Cannot switch this client to watcher, invalid security key"); return; @@ -2495,7 +2472,7 @@ public class NotebookServer extends WebSocketServlet } /** - * Send websocket message to all connections regardless of notebook id + * Send websocket message to all connections regardless of notebook id. */ private void broadcastToAllConnections(String serialized) { broadcastToAllConnectionsExcept(null, serialized); @@ -2533,7 +2510,7 @@ public class NotebookServer extends WebSocketServlet @Override public void onParaInfosReceived(String noteId, String paragraphId, - String interpreterSettingId, Map<String, String> metaInfos) { + String interpreterSettingId, Map<String, String> metaInfos) { Note note = notebook().getNote(noteId); if (note != null) { Paragraph paragraph = note.getParagraph(paragraphId); @@ -2588,7 +2565,7 @@ public class NotebookServer extends WebSocketServlet } private void saveNoteForms(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { String noteId = (String) fromMessage.get("noteId"); Map<String, Object> noteParams = (Map<String, Object>) fromMessage.get("noteParams"); @@ -2608,7 +2585,7 @@ public class NotebookServer extends WebSocketServlet } private void removeNoteForms(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, - Message fromMessage) throws IOException { + Message fromMessage) throws IOException { String noteId = (String) fromMessage.get("noteId"); String formName = (String) fromMessage.get("formName");
http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookSocket.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookSocket.java b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookSocket.java index baee746..66e4038 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookSocket.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookSocket.java @@ -16,19 +16,18 @@ */ package org.apache.zeppelin.socket; -import java.io.IOException; - -import javax.servlet.http.HttpServletRequest; - import org.apache.commons.lang.StringUtils; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.WebSocketAdapter; +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; + /** - * Notebook websocket + * Notebook websocket. */ public class NotebookSocket extends WebSocketAdapter { - private Session connection; private NotebookSocketListener listener; private HttpServletRequest request; @@ -59,7 +58,6 @@ public class NotebookSocket extends WebSocketAdapter { listener.onMessage(this, message); } - public HttpServletRequest getRequest() { return request; } http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookSocketListener.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookSocketListener.java b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookSocketListener.java index 6fc0734..3c2edd3 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookSocketListener.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookSocketListener.java @@ -17,7 +17,7 @@ package org.apache.zeppelin.socket; /** - * NoteboookSocket listener + * NoteboookSocket listener. */ public interface NotebookSocketListener { void onClose(NotebookSocket socket, int code, String message); http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/main/java/org/apache/zeppelin/types/InterpreterSettingsList.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/types/InterpreterSettingsList.java b/zeppelin-server/src/main/java/org/apache/zeppelin/types/InterpreterSettingsList.java index e016916..9f8380f 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/types/InterpreterSettingsList.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/types/InterpreterSettingsList.java @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.zeppelin.types; import java.util.List; @@ -22,7 +21,7 @@ import java.util.List; import org.apache.zeppelin.interpreter.InterpreterInfo; /** - * InterpreterSetting information for binding + * InterpreterSetting information for binding. */ public class InterpreterSettingsList { private String id; http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/main/java/org/apache/zeppelin/utils/AnyOfRolesAuthorizationFilter.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/utils/AnyOfRolesAuthorizationFilter.java b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/AnyOfRolesAuthorizationFilter.java index 37c9146..61fa0b5 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/utils/AnyOfRolesAuthorizationFilter.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/AnyOfRolesAuthorizationFilter.java @@ -19,9 +19,10 @@ package org.apache.zeppelin.utils; import org.apache.shiro.subject.Subject; import org.apache.shiro.web.filter.authz.RolesAuthorizationFilter; +import java.io.IOException; + import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; -import java.io.IOException; /** * Allows access if current user has at least one role of the specified list. @@ -30,11 +31,9 @@ import java.io.IOException; * of {@literal AND} on the specified roles. */ public class AnyOfRolesAuthorizationFilter extends RolesAuthorizationFilter { - @Override public boolean isAccessAllowed(ServletRequest request, ServletResponse response, - Object mappedValue) throws IOException { - + Object mappedValue) throws IOException { final Subject subject = getSubject(request, response); final String[] rolesArray = (String[]) mappedValue; http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/main/java/org/apache/zeppelin/utils/CommandLineUtils.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/utils/CommandLineUtils.java b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/CommandLineUtils.java index 9e33ef1..63f8580 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/utils/CommandLineUtils.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/CommandLineUtils.java @@ -16,12 +16,12 @@ */ package org.apache.zeppelin.utils; -import org.apache.zeppelin.util.Util; - import java.util.Locale; +import org.apache.zeppelin.util.Util; + /** - * CommandLine Support Class + * CommandLine Support Class. */ public class CommandLineUtils { public static void main(String[] args) { http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/main/java/org/apache/zeppelin/utils/InterpreterBindingUtils.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/utils/InterpreterBindingUtils.java b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/InterpreterBindingUtils.java index 94d97fd..a1b954e 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/utils/InterpreterBindingUtils.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/InterpreterBindingUtils.java @@ -16,19 +16,19 @@ */ package org.apache.zeppelin.utils; +import java.util.LinkedList; +import java.util.List; + import org.apache.zeppelin.interpreter.InterpreterSetting; import org.apache.zeppelin.notebook.Notebook; import org.apache.zeppelin.types.InterpreterSettingsList; -import java.util.LinkedList; -import java.util.List; - /** - * Utils for interpreter bindings + * Utils for interpreter bindings. */ public class InterpreterBindingUtils { public static List<InterpreterSettingsList> getInterpreterBindings(Notebook notebook, - String noteId) { + String noteId) { List<InterpreterSettingsList> settingList = new LinkedList<>(); List<InterpreterSetting> selectedSettings = notebook.getBindedInterpreterSettings(noteId); http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/main/java/org/apache/zeppelin/utils/SecurityUtils.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/utils/SecurityUtils.java b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/SecurityUtils.java index e24af20..7762384 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/utils/SecurityUtils.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/SecurityUtils.java @@ -16,6 +16,18 @@ */ package org.apache.zeppelin.utils; +import com.google.common.collect.Sets; + +import org.apache.shiro.authz.AuthorizationInfo; +import org.apache.shiro.realm.Realm; +import org.apache.shiro.realm.text.IniRealm; +import org.apache.shiro.subject.SimplePrincipalCollection; +import org.apache.shiro.subject.Subject; +import org.apache.shiro.util.ThreadContext; +import org.apache.shiro.web.mgt.DefaultWebSecurityManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.net.InetAddress; import java.net.URI; import java.net.URISyntaxException; @@ -29,28 +41,14 @@ import java.util.Map; import javax.naming.NamingException; -import org.apache.shiro.authz.AuthorizationInfo; -import org.apache.shiro.config.IniSecurityManagerFactory; -import org.apache.shiro.mgt.SecurityManager; -import org.apache.shiro.realm.Realm; -import org.apache.shiro.realm.text.IniRealm; -import org.apache.shiro.subject.SimplePrincipalCollection; -import org.apache.shiro.subject.Subject; -import org.apache.shiro.util.ThreadContext; -import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.apache.zeppelin.conf.ZeppelinConfiguration; import org.apache.zeppelin.realm.ActiveDirectoryGroupRealm; import org.apache.zeppelin.realm.LdapRealm; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.Sets; /** - * Tools for securing Zeppelin + * Tools for securing Zeppelin. */ public class SecurityUtils { - private static final String ANONYMOUS = "anonymous"; private static final HashSet<String> EMPTY_HASHSET = Sets.newHashSet(); private static boolean isEnabled = false; @@ -80,7 +78,7 @@ public class SecurityUtils { } /** - * Return the authenticated user if any otherwise returns "anonymous" + * Return the authenticated user if any otherwise returns "anonymous". * * @return shiro principal */ @@ -122,7 +120,7 @@ public class SecurityUtils { } /** - * Return the roles associated with the authenticated user if any otherwise returns empty set + * Return the roles associated with the authenticated user if any otherwise returns empty set. * TODO(prasadwagle) Find correct way to get user roles (see SHIRO-492) * * @return shiro roles @@ -146,8 +144,8 @@ public class SecurityUtils { } else if (name.equals("org.apache.zeppelin.realm.LdapRealm")) { try { AuthorizationInfo auth = ((LdapRealm) realm).queryForAuthorizationInfo( - new SimplePrincipalCollection(subject.getPrincipal(), realm.getName()), - ((LdapRealm) realm).getContextFactory() + new SimplePrincipalCollection(subject.getPrincipal(), realm.getName()), + ((LdapRealm) realm).getContextFactory() ); roles = new HashSet<>(auth.getRoles()); } catch (NamingException e) { @@ -173,7 +171,7 @@ public class SecurityUtils { } /** - * Checked if shiro enabled or not + * Checked if shiro enabled or not. */ public static boolean isAuthenticated() { if (!isEnabled) { http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/test/java/org/apache/zeppelin/ZeppelinITUtils.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/test/java/org/apache/zeppelin/ZeppelinITUtils.java b/zeppelin-server/src/test/java/org/apache/zeppelin/ZeppelinITUtils.java index 74ae495..cc545a3 100644 --- a/zeppelin-server/src/test/java/org/apache/zeppelin/ZeppelinITUtils.java +++ b/zeppelin-server/src/test/java/org/apache/zeppelin/ZeppelinITUtils.java @@ -14,18 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.zeppelin; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.openqa.selenium.WebDriver; -import java.util.concurrent.TimeUnit; public class ZeppelinITUtils { - - public final static Logger LOG = LoggerFactory.getLogger(ZeppelinITUtils.class); + public static final Logger LOG = LoggerFactory.getLogger(ZeppelinITUtils.class); public static void sleep(long millis, boolean logOutput) { if (logOutput) { http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/test/java/org/apache/zeppelin/configuration/RequestHeaderSizeTest.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/test/java/org/apache/zeppelin/configuration/RequestHeaderSizeTest.java b/zeppelin-server/src/test/java/org/apache/zeppelin/configuration/RequestHeaderSizeTest.java index 5cdfde6..307dacb 100644 --- a/zeppelin-server/src/test/java/org/apache/zeppelin/configuration/RequestHeaderSizeTest.java +++ b/zeppelin-server/src/test/java/org/apache/zeppelin/configuration/RequestHeaderSizeTest.java @@ -14,53 +14,52 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.zeppelin.configuration; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.lang.RandomStringUtils; -import org.apache.zeppelin.conf.ZeppelinConfiguration; -import org.apache.zeppelin.rest.AbstractTestRestApi; import org.junit.After; import org.junit.Before; import org.junit.Test; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -public class RequestHeaderSizeTest extends AbstractTestRestApi { - private static final int REQUEST_HEADER_MAX_SIZE = 20000; - - @Before - public void startZeppelin() throws Exception { - System.setProperty(ZeppelinConfiguration.ConfVars.ZEPPELIN_SERVER_JETTY_REQUEST_HEADER_SIZE.getVarName(), String.valueOf(REQUEST_HEADER_MAX_SIZE)); - startUp(RequestHeaderSizeTest.class.getSimpleName()); - } - - @After - public void stopZeppelin() throws Exception { - shutDown(); - } +import org.apache.zeppelin.conf.ZeppelinConfiguration; +import org.apache.zeppelin.rest.AbstractTestRestApi; - @Test - public void increased_request_header_size_do_not_cause_413_when_request_size_is_over_8K() throws Exception { - HttpClient httpClient = new HttpClient(); +public class RequestHeaderSizeTest extends AbstractTestRestApi { + private static final int REQUEST_HEADER_MAX_SIZE = 20000; - GetMethod getMethod = new GetMethod(getUrlToTest() + "/version"); - String headerValue = RandomStringUtils.randomAlphanumeric(REQUEST_HEADER_MAX_SIZE - 2000); - getMethod.setRequestHeader("not_too_large_header", headerValue); - int httpCode = httpClient.executeMethod(getMethod); - assertThat(httpCode, is(HttpStatus.SC_OK)); + @Before + public void startZeppelin() throws Exception { + System.setProperty(ZeppelinConfiguration.ConfVars.ZEPPELIN_SERVER_JETTY_REQUEST_HEADER_SIZE + .getVarName(), String.valueOf(REQUEST_HEADER_MAX_SIZE)); + startUp(RequestHeaderSizeTest.class.getSimpleName()); + } + @After + public void stopZeppelin() throws Exception { + shutDown(); + } - getMethod = new GetMethod(getUrlToTest() + "/version"); - headerValue = RandomStringUtils.randomAlphanumeric(REQUEST_HEADER_MAX_SIZE + 2000); - getMethod.setRequestHeader("too_large_header", headerValue); - httpCode = httpClient.executeMethod(getMethod); - assertThat(httpCode, is(HttpStatus.SC_REQUEST_TOO_LONG)); - } + @Test + public void increased_request_header_size_do_not_cause_413_when_request_size_is_over_8K() + throws Exception { + HttpClient httpClient = new HttpClient(); + GetMethod getMethod = new GetMethod(getUrlToTest() + "/version"); + String headerValue = RandomStringUtils.randomAlphanumeric(REQUEST_HEADER_MAX_SIZE - 2000); + getMethod.setRequestHeader("not_too_large_header", headerValue); + int httpCode = httpClient.executeMethod(getMethod); + assertThat(httpCode, is(HttpStatus.SC_OK)); + getMethod = new GetMethod(getUrlToTest() + "/version"); + headerValue = RandomStringUtils.randomAlphanumeric(REQUEST_HEADER_MAX_SIZE + 2000); + getMethod.setRequestHeader("too_large_header", headerValue); + httpCode = httpClient.executeMethod(getMethod); + assertThat(httpCode, is(HttpStatus.SC_REQUEST_TOO_LONG)); + } } http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/test/java/org/apache/zeppelin/display/AngularObjectBuilder.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/test/java/org/apache/zeppelin/display/AngularObjectBuilder.java b/zeppelin-server/src/test/java/org/apache/zeppelin/display/AngularObjectBuilder.java index a6aaae6..cfbdc31 100644 --- a/zeppelin-server/src/test/java/org/apache/zeppelin/display/AngularObjectBuilder.java +++ b/zeppelin-server/src/test/java/org/apache/zeppelin/display/AngularObjectBuilder.java @@ -14,13 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.zeppelin.display; public class AngularObjectBuilder { - public static <T> AngularObject<T> build(String varName, T value, String noteId, - String paragraphId) { + String paragraphId) { return new AngularObject<>(varName, value, noteId, paragraphId, null); } } http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/test/java/org/apache/zeppelin/realm/LdapRealmTest.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/test/java/org/apache/zeppelin/realm/LdapRealmTest.java b/zeppelin-server/src/test/java/org/apache/zeppelin/realm/LdapRealmTest.java index 9070c5f..5bff693 100644 --- a/zeppelin-server/src/test/java/org/apache/zeppelin/realm/LdapRealmTest.java +++ b/zeppelin-server/src/test/java/org/apache/zeppelin/realm/LdapRealmTest.java @@ -18,11 +18,23 @@ */ package org.apache.zeppelin.realm; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.shiro.realm.ldap.LdapContextFactory; +import org.apache.shiro.session.Session; +import org.apache.shiro.subject.SimplePrincipalCollection; +import org.junit.Test; + import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Set; + import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.directory.BasicAttributes; @@ -30,45 +42,24 @@ import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; import javax.naming.ldap.LdapContext; -import org.apache.shiro.realm.ldap.LdapContextFactory; -import org.apache.shiro.session.Session; -import org.apache.shiro.subject.SimplePrincipalCollection; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.verify; - - public class LdapRealmTest { - @Test public void testGetUserDn() { LdapRealm realm = new LdapRealm(); // without a user search filter realm.setUserSearchFilter(null); - assertEquals( - "foo ", - realm.getUserDn("foo ") - ); + assertEquals("foo ", realm.getUserDn("foo ")); // with a user search filter realm.setUserSearchFilter("memberUid={0}"); - assertEquals( - "foo", - realm.getUserDn("foo") - ); + assertEquals("foo", realm.getUserDn("foo")); } @Test public void testExpandTemplate() { - assertEquals( - "uid=foo,cn=users,dc=ods,dc=foo", - LdapRealm.expandTemplate("uid={0},cn=users,dc=ods,dc=foo", "foo") - ); + assertEquals("uid=foo,cn=users,dc=ods,dc=foo", + LdapRealm.expandTemplate("uid={0},cn=users,dc=ods,dc=foo", "foo")); } @Test @@ -76,18 +67,13 @@ public class LdapRealmTest { LdapRealm realm = new LdapRealm(); realm.setUserSearchAttributeName("uid"); - assertEquals( - "foo", - realm.getUserDnForSearch("foo") - ); + assertEquals("foo", realm.getUserDnForSearch("foo")); // using a template realm.setUserSearchAttributeName(null); realm.setMemberAttributeValueTemplate("cn={0},ou=people,dc=hadoop,dc=apache"); - assertEquals( - "cn=foo,ou=people,dc=hadoop,dc=apache", - realm.getUserDnForSearch("foo") - ); + assertEquals("cn=foo,ou=people,dc=hadoop,dc=apache", + realm.getUserDnForSearch("foo")); } @Test @@ -104,7 +90,6 @@ public class LdapRealmTest { LdapContext ldapCtx = mock(LdapContext.class); Session session = mock(Session.class); - // expected search results BasicAttributes group1 = new BasicAttributes(); group1.put(realm.getGroupIdAttribute(), "group-one"); @@ -121,27 +106,17 @@ public class LdapRealmTest { group3.put(realm.getMemberAttribute(), "principal"); NamingEnumeration<SearchResult> results = enumerationOf(group1, group2, group3); - when(ldapCtx.search(any(String.class), any(String.class), any(SearchControls.class))).thenReturn(results); - + when(ldapCtx.search(any(String.class), any(String.class), any(SearchControls.class))) + .thenReturn(results); Set<String> roles = realm.rolesFor( - new SimplePrincipalCollection("principal", "ldapRealm"), - "principal", - ldapCtx, - ldapContextFactory, - session - ); - - verify(ldapCtx).search( - "cn=groups,dc=apache", - "(objectclass=posixGroup)", - realm.getGroupSearchControls() - ); - - assertEquals( - new HashSet(Arrays.asList("group-one", "zeppelin-role")), - roles - ); + new SimplePrincipalCollection("principal", "ldapRealm"), + "principal", ldapCtx, ldapContextFactory, session); + + verify(ldapCtx).search("cn=groups,dc=apache", "(objectclass=posixGroup)", + realm.getGroupSearchControls()); + + assertEquals(new HashSet(Arrays.asList("group-one", "zeppelin-role")), roles); } private NamingEnumeration<SearchResult> enumerationOf(BasicAttributes... attrs) { http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/test/java/org/apache/zeppelin/realm/PamRealmTest.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/test/java/org/apache/zeppelin/realm/PamRealmTest.java b/zeppelin-server/src/test/java/org/apache/zeppelin/realm/PamRealmTest.java index 55448bd..f328407 100644 --- a/zeppelin-server/src/test/java/org/apache/zeppelin/realm/PamRealmTest.java +++ b/zeppelin-server/src/test/java/org/apache/zeppelin/realm/PamRealmTest.java @@ -16,19 +16,19 @@ */ package org.apache.zeppelin.realm; -import org.apache.shiro.authc.AuthenticationInfo; -import org.apache.shiro.authc.UsernamePasswordToken; -import org.junit.Test; - import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import org.apache.shiro.authc.AuthenticationInfo; +import org.apache.shiro.authc.UsernamePasswordToken; +import org.junit.Test; + /** - * The test will only be executed if the environment variables PAM_USER and PAM_PASS are present. They should - * contain username and password of an valid system user to make the test pass. The service needs to be configured - * under /etc/pam.d/sshd to resolve and authenticate the system user. + * The test will only be executed if the environment variables PAM_USER and PAM_PASS are present. + * They should contain username and password of an valid system user to make the test pass. The + * service needs to be configured under /etc/pam.d/sshd to resolve and authenticate the system user. * * Contains main() function so the test can be executed manually. * @@ -43,16 +43,16 @@ public class PamRealmTest { PamRealm realm = new PamRealm(); realm.setService("sshd"); - String pam_user = System.getenv("PAM_USER"); - String pam_pass = System.getenv("PAM_PASS"); - assumeTrue(pam_user != null); - assumeTrue(pam_pass != null); + String pamUser = System.getenv("PAM_USER"); + String pamPass = System.getenv("PAM_PASS"); + assumeTrue(pamUser != null); + assumeTrue(pamPass != null); // mock shiro auth token UsernamePasswordToken authToken = mock(UsernamePasswordToken.class); - when(authToken.getUsername()).thenReturn(pam_user); - when(authToken.getPassword()).thenReturn(pam_pass.toCharArray()); - when(authToken.getCredentials()).thenReturn(pam_pass); + when(authToken.getUsername()).thenReturn(pamUser); + when(authToken.getPassword()).thenReturn(pamPass.toCharArray()); + when(authToken.getCredentials()).thenReturn(pamPass); AuthenticationInfo authInfo = realm.doGetAuthenticationInfo(authToken); @@ -63,4 +63,4 @@ public class PamRealmTest { PamRealmTest test = new PamRealmTest(); test.testDoGetAuthenticationInfo(); } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/zeppelin/blob/1add74b4/zeppelin-server/src/test/java/org/apache/zeppelin/recovery/RecoveryTest.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/test/java/org/apache/zeppelin/recovery/RecoveryTest.java b/zeppelin-server/src/test/java/org/apache/zeppelin/recovery/RecoveryTest.java index 37277ee..0336402 100644 --- a/zeppelin-server/src/test/java/org/apache/zeppelin/recovery/RecoveryTest.java +++ b/zeppelin-server/src/test/java/org/apache/zeppelin/recovery/RecoveryTest.java @@ -14,14 +14,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.zeppelin.recovery; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + import com.google.common.io.Files; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; + import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.io.FileUtils; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.util.Map; + import org.apache.zeppelin.conf.ZeppelinConfiguration; import org.apache.zeppelin.interpreter.ManagedInterpreterGroup; import org.apache.zeppelin.interpreter.recovery.FileSystemRecoveryStorage; @@ -32,18 +42,8 @@ import org.apache.zeppelin.rest.AbstractTestRestApi; import org.apache.zeppelin.scheduler.Job; import org.apache.zeppelin.server.ZeppelinServer; import org.apache.zeppelin.user.AuthenticationInfo; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.io.File; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; public class RecoveryTest extends AbstractTestRestApi { - private Gson gson = new Gson(); private static File recoveryDir = null; @@ -52,7 +52,8 @@ public class RecoveryTest extends AbstractTestRestApi { System.setProperty(ZeppelinConfiguration.ConfVars.ZEPPELIN_RECOVERY_STORAGE_CLASS.getVarName(), FileSystemRecoveryStorage.class.getName()); recoveryDir = Files.createTempDir(); - System.setProperty(ZeppelinConfiguration.ConfVars.ZEPPELIN_RECOVERY_DIR.getVarName(), recoveryDir.getAbsolutePath()); + System.setProperty(ZeppelinConfiguration.ConfVars.ZEPPELIN_RECOVERY_DIR.getVarName(), + recoveryDir.getAbsolutePath()); startUp(RecoveryTest.class.getSimpleName()); } @@ -71,8 +72,8 @@ public class RecoveryTest extends AbstractTestRestApi { p1.setText("%python user='abc'"); PostMethod post = httpPost("/notebook/job/" + note1.getId(), ""); assertThat(post, isAllowed()); - Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() { - }.getType()); + Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), + new TypeToken<Map<String, Object>>() {}.getType()); assertEquals(resp.get("status"), "OK"); post.releaseConnection(); assertEquals(Job.Status.FINISHED, p1.getStatus()); @@ -101,8 +102,8 @@ public class RecoveryTest extends AbstractTestRestApi { p1.setText("%python user='abc'"); PostMethod post = httpPost("/notebook/job/" + note1.getId(), ""); assertThat(post, isAllowed()); - Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() { - }.getType()); + Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), + new TypeToken<Map<String, Object>>() {}.getType()); assertEquals(resp.get("status"), "OK"); post.releaseConnection(); assertEquals(Job.Status.FINISHED, p1.getStatus()); @@ -137,8 +138,8 @@ public class RecoveryTest extends AbstractTestRestApi { p1.setText("%python user='abc'"); PostMethod post = httpPost("/notebook/job/" + note1.getId(), ""); assertThat(post, isAllowed()); - Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() { - }.getType()); + Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), + new TypeToken<Map<String, Object>>() {}.getType()); assertEquals(resp.get("status"), "OK"); post.releaseConnection(); assertEquals(Job.Status.FINISHED, p1.getStatus());