Search in sources :

Example 16 with ServiceContext

use of org.apache.zeppelin.service.ServiceContext in project zeppelin by apache.

the class NotebookServer method patchParagraph.

private void patchParagraph(NotebookSocket conn, ServiceContext context, Message fromMessage) throws IOException {
    if (!collaborativeModeEnable) {
        return;
    }
    String paragraphId = fromMessage.getType("id", LOG);
    if (paragraphId == null) {
        return;
    }
    String noteId = connectionManager.getAssociatedNoteId(conn);
    if (noteId == null) {
        noteId = fromMessage.getType("noteId", LOG);
        if (noteId == null) {
            return;
        }
    }
    final String noteId2 = noteId;
    String patchText = fromMessage.getType("patch", LOG);
    if (patchText == null) {
        return;
    }
    getNotebookService().patchParagraph(noteId, paragraphId, patchText, context, new WebSocketServiceCallback<String>(conn) {

        @Override
        public void onSuccess(String result, ServiceContext context) throws IOException {
            super.onSuccess(result, context);
            Message message = new Message(OP.PATCH_PARAGRAPH).put("patch", result).put("paragraphId", paragraphId);
            connectionManager.broadcastExcept(noteId2, message, conn);
        }
    });
}
Also used : OnMessage(javax.websocket.OnMessage) ClusterMessage(org.apache.zeppelin.cluster.event.ClusterMessage) Message(org.apache.zeppelin.common.Message) ServiceContext(org.apache.zeppelin.service.ServiceContext) IOException(java.io.IOException)

Example 17 with ServiceContext

use of org.apache.zeppelin.service.ServiceContext in project zeppelin by apache.

the class NotebookServer method getServiceContext.

private ServiceContext getServiceContext(TicketContainer.Entry ticketEntry) {
    AuthenticationInfo authInfo = new AuthenticationInfo(ticketEntry.getPrincipal(), ticketEntry.getRoles(), ticketEntry.getTicket());
    Set<String> userAndRoles = new HashSet<>();
    userAndRoles.add(authInfo.getUser());
    userAndRoles.addAll(authInfo.getRoles());
    return new ServiceContext(authInfo, userAndRoles);
}
Also used : ServiceContext(org.apache.zeppelin.service.ServiceContext) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) HashSet(java.util.HashSet)

Example 18 with ServiceContext

use of org.apache.zeppelin.service.ServiceContext in project zeppelin by apache.

the class NotebookServer method onMessage.

public void onMessage(NotebookSocket conn, String msg) {
    try {
        Message receivedMessage = deserializeMessage(msg);
        if (receivedMessage.op != OP.PING) {
            LOG.debug("RECEIVE: " + receivedMessage.op + ", RECEIVE PRINCIPAL: " + receivedMessage.principal + ", RECEIVE TICKET: " + receivedMessage.ticket + ", RECEIVE ROLES: " + receivedMessage.roles + ", RECEIVE DATA: " + receivedMessage.data);
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("RECEIVE MSG = " + receivedMessage);
        }
        TicketContainer.Entry ticketEntry = TicketContainer.instance.getTicketEntry(receivedMessage.principal);
        if (ticketEntry == null || StringUtils.isEmpty(ticketEntry.getTicket())) {
            LOG.debug("{} message: invalid ticket {}", receivedMessage.op, receivedMessage.ticket);
            return;
        } else if (!ticketEntry.getTicket().equals(receivedMessage.ticket)) {
            /* not to pollute logs, log instead of exception */
            LOG.debug("{} message: invalid ticket {} != {}", receivedMessage.op, receivedMessage.ticket, ticketEntry.getTicket());
            if (!receivedMessage.op.equals(OP.PING)) {
                conn.send(serializeMessage(new Message(OP.SESSION_LOGOUT).put("info", "Your ticket is invalid possibly due to server restart. Please login again.")));
            }
            return;
        }
        ZeppelinConfiguration conf = ZeppelinConfiguration.create();
        boolean allowAnonymous = conf.isAnonymousAllowed();
        if (!allowAnonymous && receivedMessage.principal.equals("anonymous")) {
            LOG.warn("Anonymous access not allowed.");
            return;
        }
        if (Message.isDisabledForRunningNotes(receivedMessage.op)) {
            boolean noteRunning = getNotebook().processNote((String) receivedMessage.get("noteId"), note -> note != null && note.isRunning());
            if (noteRunning) {
                throw new Exception("Note is now running sequentially. Can not be performed: " + receivedMessage.op);
            }
        }
        if (StringUtils.isEmpty(conn.getUser())) {
            connectionManager.addUserConnection(receivedMessage.principal, conn);
        }
        ServiceContext context = getServiceContext(ticketEntry);
        // Lets be elegant here
        switch(receivedMessage.op) {
            case LIST_NOTES:
                listNotesInfo(conn, context);
                break;
            case RELOAD_NOTES_FROM_REPO:
                broadcastReloadedNoteList(context);
                break;
            case GET_HOME_NOTE:
                getHomeNote(conn, context);
                break;
            case GET_NOTE:
                getNote(conn, context, receivedMessage);
                break;
            case RELOAD_NOTE:
                reloadNote(conn, context, receivedMessage);
                break;
            case NEW_NOTE:
                createNote(conn, context, receivedMessage);
                break;
            case DEL_NOTE:
                deleteNote(conn, context, receivedMessage);
                break;
            case REMOVE_FOLDER:
                removeFolder(conn, context, receivedMessage);
                break;
            case MOVE_NOTE_TO_TRASH:
                moveNoteToTrash(conn, context, receivedMessage);
                break;
            case MOVE_FOLDER_TO_TRASH:
                moveFolderToTrash(conn, context, receivedMessage);
                break;
            case EMPTY_TRASH:
                emptyTrash(conn, context);
                break;
            case RESTORE_FOLDER:
                restoreFolder(conn, context, receivedMessage);
                break;
            case RESTORE_NOTE:
                restoreNote(conn, context, receivedMessage);
                break;
            case RESTORE_ALL:
                restoreAll(conn, context, receivedMessage);
                break;
            case CLONE_NOTE:
                cloneNote(conn, context, receivedMessage);
                break;
            case IMPORT_NOTE:
                importNote(conn, context, receivedMessage);
                break;
            case CONVERT_NOTE_NBFORMAT:
                convertNote(conn, receivedMessage);
                break;
            case COMMIT_PARAGRAPH:
                updateParagraph(conn, context, receivedMessage);
                break;
            case RUN_PARAGRAPH:
                runParagraph(conn, context, receivedMessage);
                break;
            case PARAGRAPH_EXECUTED_BY_SPELL:
                broadcastSpellExecution(conn, context, receivedMessage);
                break;
            case RUN_ALL_PARAGRAPHS:
                runAllParagraphs(conn, context, receivedMessage);
                break;
            case CANCEL_PARAGRAPH:
                cancelParagraph(conn, context, receivedMessage);
                break;
            case MOVE_PARAGRAPH:
                moveParagraph(conn, context, receivedMessage);
                break;
            case INSERT_PARAGRAPH:
                insertParagraph(conn, context, receivedMessage);
                break;
            case COPY_PARAGRAPH:
                copyParagraph(conn, context, receivedMessage);
                break;
            case PARAGRAPH_REMOVE:
                removeParagraph(conn, context, receivedMessage);
                break;
            case PARAGRAPH_CLEAR_OUTPUT:
                clearParagraphOutput(conn, context, receivedMessage);
                break;
            case PARAGRAPH_CLEAR_ALL_OUTPUT:
                clearAllParagraphOutput(conn, context, receivedMessage);
                break;
            case NOTE_UPDATE:
                updateNote(conn, context, receivedMessage);
                break;
            case NOTE_RENAME:
                renameNote(conn, context, receivedMessage);
                break;
            case FOLDER_RENAME:
                renameFolder(conn, context, receivedMessage);
                break;
            case UPDATE_PERSONALIZED_MODE:
                updatePersonalizedMode(conn, context, receivedMessage);
                break;
            case COMPLETION:
                completion(conn, context, receivedMessage);
                break;
            case PING:
                // do nothing
                break;
            case ANGULAR_OBJECT_UPDATED:
                angularObjectUpdated(conn, context, receivedMessage);
                break;
            case ANGULAR_OBJECT_CLIENT_BIND:
                angularObjectClientBind(conn, receivedMessage);
                break;
            case ANGULAR_OBJECT_CLIENT_UNBIND:
                angularObjectClientUnbind(conn, receivedMessage);
                break;
            case LIST_CONFIGURATIONS:
                sendAllConfigurations(conn, context, receivedMessage);
                break;
            case CHECKPOINT_NOTE:
                checkpointNote(conn, context, receivedMessage);
                break;
            case LIST_REVISION_HISTORY:
                listRevisionHistory(conn, context, receivedMessage);
                break;
            case SET_NOTE_REVISION:
                setNoteRevision(conn, context, receivedMessage);
                break;
            case NOTE_REVISION:
                getNoteByRevision(conn, context, receivedMessage);
                break;
            case NOTE_REVISION_FOR_COMPARE:
                getNoteByRevisionForCompare(conn, context, receivedMessage);
                break;
            case LIST_NOTE_JOBS:
                unicastNoteJobInfo(conn, context, receivedMessage);
                break;
            case UNSUBSCRIBE_UPDATE_NOTE_JOBS:
                unsubscribeNoteJobInfo(conn);
                break;
            case GET_INTERPRETER_BINDINGS:
                getInterpreterBindings(conn, context, receivedMessage);
                break;
            case SAVE_INTERPRETER_BINDINGS:
                saveInterpreterBindings(conn, context, receivedMessage);
                break;
            case EDITOR_SETTING:
                getEditorSetting(conn, context, receivedMessage);
                break;
            case GET_INTERPRETER_SETTINGS:
                getInterpreterSettings(conn, context, receivedMessage);
                break;
            case WATCHER:
                connectionManager.switchConnectionToWatcher(conn);
                break;
            case SAVE_NOTE_FORMS:
                saveNoteForms(conn, context, receivedMessage);
                break;
            case REMOVE_NOTE_FORMS:
                removeNoteForms(conn, context, receivedMessage);
                break;
            case PATCH_PARAGRAPH:
                patchParagraph(conn, context, receivedMessage);
                break;
            default:
                break;
        }
    } catch (Exception e) {
        LOG.error("Can't handle message: " + msg, e);
        try {
            conn.send(serializeMessage(new Message(OP.ERROR_INFO).put("info", e.getMessage())));
        } catch (IOException iox) {
            LOG.error("Fail to send error info", iox);
        }
    }
}
Also used : TicketContainer(org.apache.zeppelin.ticket.TicketContainer) OnMessage(javax.websocket.OnMessage) ClusterMessage(org.apache.zeppelin.cluster.event.ClusterMessage) Message(org.apache.zeppelin.common.Message) ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) ServiceContext(org.apache.zeppelin.service.ServiceContext) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ForbiddenException(org.apache.zeppelin.rest.exception.ForbiddenException) TException(org.apache.thrift.TException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ServiceException(org.apache.zeppelin.interpreter.thrift.ServiceException)

Example 19 with ServiceContext

use of org.apache.zeppelin.service.ServiceContext in project zeppelin by apache.

the class NotebookServerTest method testImportNotebook.

@Test
public void testImportNotebook() throws IOException {
    String msg = "{\"op\":\"IMPORT_NOTE\",\"data\":" + "{\"note\":{\"paragraphs\": [{\"text\": \"Test " + "paragraphs import\"," + "\"progressUpdateIntervalMs\":500," + "\"config\":{},\"settings\":{}}]," + "\"name\": \"Test Zeppelin notebook import\",\"config\": " + "{}}}}";
    Message messageReceived = notebookServer.deserializeMessage(msg);
    String noteId = null;
    ServiceContext context = new ServiceContext(AuthenticationInfo.ANONYMOUS, new HashSet<>());
    try {
        try {
            noteId = notebookServer.importNote(null, context, messageReceived);
        } catch (NullPointerException e) {
            // broadcastNoteList(); failed nothing to worry.
            LOG.error("Exception in NotebookServerTest while testImportNotebook, failed nothing to " + "worry ", e);
        }
        notebook.processNote(noteId, note -> {
            assertNotNull(note);
            assertEquals("Test Zeppelin notebook import", note.getName());
            assertEquals("Test paragraphs import", note.getParagraphs().get(0).getText());
            return null;
        });
    } finally {
        if (noteId != null) {
            notebook.removeNote(noteId, anonymous);
        }
    }
}
Also used : Message(org.apache.zeppelin.common.Message) ServiceContext(org.apache.zeppelin.service.ServiceContext) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.Test)

Example 20 with ServiceContext

use of org.apache.zeppelin.service.ServiceContext in project zeppelin by apache.

the class NotebookServer method angularObjectUpdated.

/**
 * 1. When angular object updated from client.
 * 2. Save AngularObject to note.
 *
 * @param conn        the web socket.
 * @param fromMessage the message.
 */
private void angularObjectUpdated(NotebookSocket conn, ServiceContext context, Message fromMessage) throws IOException {
    String noteId = (String) fromMessage.get("noteId");
    String paragraphId = (String) fromMessage.get("paragraphId");
    String interpreterGroupId = (String) fromMessage.get("interpreterGroupId");
    String varName = (String) fromMessage.get("name");
    Object varValue = fromMessage.get("value");
    String user = fromMessage.principal;
    getNotebookService().updateAngularObject(noteId, paragraphId, interpreterGroupId, varName, varValue, context, new WebSocketServiceCallback<AngularObject>(conn) {

        @Override
        public void onSuccess(AngularObject ao, ServiceContext context) throws IOException {
            super.onSuccess(ao, context);
            connectionManager.broadcastExcept(noteId, new Message(OP.ANGULAR_OBJECT_UPDATE).put("angularObject", ao).put("interpreterGroupId", interpreterGroupId).put("noteId", noteId).put("paragraphId", ao.getParagraphId()), conn);
            getNotebook().processNote(noteId, note -> {
                note.addOrUpdateAngularObject(interpreterGroupId, ao);
                return null;
            });
        }
    });
}
Also used : ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) TestUtils(org.apache.zeppelin.utils.TestUtils) Arrays(java.util.Arrays) TypeToken(com.google.gson.reflect.TypeToken) Provider(javax.inject.Provider) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) OnMessage(javax.websocket.OnMessage) GUI(org.apache.zeppelin.display.GUI) LoggerFactory(org.slf4j.LoggerFactory) ManagedOperation(org.eclipse.jetty.util.annotation.ManagedOperation) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) StringUtils(org.apache.commons.lang3.StringUtils) TicketContainer(org.apache.zeppelin.ticket.TicketContainer) GsonBuilder(com.google.gson.GsonBuilder) ManagedAttribute(org.eclipse.jetty.util.annotation.ManagedAttribute) ServiceContext(org.apache.zeppelin.service.ServiceContext) HeliumPackage(org.apache.zeppelin.helium.HeliumPackage) ParagraphInfo(org.apache.zeppelin.interpreter.thrift.ParagraphInfo) Gson(com.google.gson.Gson) Map(java.util.Map) ClusterEvent(org.apache.zeppelin.cluster.event.ClusterEvent) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) AuthorizationService(org.apache.zeppelin.notebook.AuthorizationService) ParagraphJobListener(org.apache.zeppelin.notebook.ParagraphJobListener) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) AngularObject(org.apache.zeppelin.display.AngularObject) Revision(org.apache.zeppelin.notebook.repo.NotebookRepoWithVersionControl.Revision) Session(javax.websocket.Session) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) NoteInfo(org.apache.zeppelin.notebook.NoteInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) JupyterUtil(org.apache.zeppelin.jupyter.JupyterUtil) ForbiddenException(org.apache.zeppelin.rest.exception.ForbiddenException) ServerEndpoint(javax.websocket.server.ServerEndpoint) Executors(java.util.concurrent.Executors) ClusterManagerServer(org.apache.zeppelin.cluster.ClusterManagerServer) JobManagerService(org.apache.zeppelin.service.JobManagerService) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry) List(java.util.List) Type(java.lang.reflect.Type) Status(org.apache.zeppelin.scheduler.Job.Status) NotebookImportDeserializer(org.apache.zeppelin.notebook.NotebookImportDeserializer) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) MSG_ID_NOT_DEFINED(org.apache.zeppelin.common.Message.MSG_ID_NOT_DEFINED) RemoteInterpreterProcessListener(org.apache.zeppelin.interpreter.remote.RemoteInterpreterProcessListener) CloseReason(javax.websocket.CloseReason) ClusterEventListener(org.apache.zeppelin.cluster.event.ClusterEventListener) Input(org.apache.zeppelin.display.Input) InterpreterSettingsList(org.apache.zeppelin.types.InterpreterSettingsList) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ClusterMessage(org.apache.zeppelin.cluster.event.ClusterMessage) OnOpen(javax.websocket.OnOpen) AngularObjectRegistryListener(org.apache.zeppelin.display.AngularObjectRegistryListener) IdHashes(org.apache.zeppelin.util.IdHashes) OnClose(javax.websocket.OnClose) Message(org.apache.zeppelin.common.Message) ExecutorService(java.util.concurrent.ExecutorService) OP(org.apache.zeppelin.common.Message.OP) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) NoteEventListener(org.apache.zeppelin.notebook.NoteEventListener) Paragraph(org.apache.zeppelin.notebook.Paragraph) CorsUtils(org.apache.zeppelin.utils.CorsUtils) NotebookService(org.apache.zeppelin.service.NotebookService) Logger(org.slf4j.Logger) ApplicationEventListener(org.apache.zeppelin.helium.ApplicationEventListener) SimpleServiceCallback(org.apache.zeppelin.service.SimpleServiceCallback) EndpointConfig(javax.websocket.EndpointConfig) Note(org.apache.zeppelin.notebook.Note) TException(org.apache.thrift.TException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Notebook(org.apache.zeppelin.notebook.Notebook) ServiceException(org.apache.zeppelin.interpreter.thrift.ServiceException) InterpreterCompletion(org.apache.zeppelin.interpreter.thrift.InterpreterCompletion) ZEPPELIN_ALLOWED_ORIGINS(org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars.ZEPPELIN_ALLOWED_ORIGINS) ConfigurationService(org.apache.zeppelin.service.ConfigurationService) OnMessage(javax.websocket.OnMessage) ClusterMessage(org.apache.zeppelin.cluster.event.ClusterMessage) Message(org.apache.zeppelin.common.Message) ServiceContext(org.apache.zeppelin.service.ServiceContext) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) AngularObject(org.apache.zeppelin.display.AngularObject) AngularObject(org.apache.zeppelin.display.AngularObject) IOException(java.io.IOException)

Aggregations

ServiceContext (org.apache.zeppelin.service.ServiceContext)21 IOException (java.io.IOException)16 Message (org.apache.zeppelin.common.Message)15 OnMessage (javax.websocket.OnMessage)11 ClusterMessage (org.apache.zeppelin.cluster.event.ClusterMessage)11 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)7 Map (java.util.Map)6 URISyntaxException (java.net.URISyntaxException)5 UnknownHostException (java.net.UnknownHostException)5 HashMap (java.util.HashMap)5 TException (org.apache.thrift.TException)5 ServiceException (org.apache.zeppelin.interpreter.thrift.ServiceException)5 Paragraph (org.apache.zeppelin.notebook.Paragraph)5 ForbiddenException (org.apache.zeppelin.rest.exception.ForbiddenException)5 HashSet (java.util.HashSet)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 AngularObject (org.apache.zeppelin.display.AngularObject)4 Note (org.apache.zeppelin.notebook.Note)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3