Search in sources :

Example 11 with ServiceContext

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

the class NotebookServerTest method testRuntimeInfos.

@Test
public void testRuntimeInfos() throws IOException {
    // mock note
    String msg = "{\"op\":\"IMPORT_NOTE\",\"data\":" + "{\"note\":{\"paragraphs\": [{\"text\": \"Test " + "paragraphs import\"," + "\"progressUpdateIntervalMs\":500," + "\"config\":{},\"settings\":{}}]," + "\"name\": \"Test RuntimeInfos\",\"config\": " + "{}}}}";
    Message messageReceived = notebookServer.deserializeMessage(msg);
    String noteId = null;
    ServiceContext context = new ServiceContext(AuthenticationInfo.ANONYMOUS, new HashSet<>());
    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);
    } catch (IOException e) {
        e.printStackTrace();
    }
    // update RuntimeInfos
    Map<String, String> infos = new java.util.HashMap<>();
    String paragraphId = notebook.processNote(noteId, note -> {
        assertNotNull(note);
        assertNotNull(note.getParagraph(0));
        infos.put("jobUrl", "jobUrl_value");
        infos.put("jobLabel", "jobLabel_value");
        infos.put("label", "SPARK JOB");
        infos.put("tooltip", "View in Spark web UI");
        infos.put("noteId", note.getId());
        infos.put("paraId", note.getParagraph(0).getId());
        return note.getParagraph(0).getId();
    });
    notebookServer.onParaInfosReceived(noteId, paragraphId, "spark", infos);
    notebook.processNote(noteId, note -> {
        Paragraph paragraph = note.getParagraph(paragraphId);
        // check RuntimeInfos
        assertTrue(paragraph.getRuntimeInfos().containsKey("jobUrl"));
        List<Object> list = paragraph.getRuntimeInfos().get("jobUrl").getValue();
        assertEquals(1, list.size());
        Map<String, String> map = (Map<String, String>) list.get(0);
        assertEquals(2, map.size());
        assertEquals(map.get("jobUrl"), "jobUrl_value");
        assertEquals(map.get("jobLabel"), "jobLabel_value");
        return null;
    });
}
Also used : Message(org.apache.zeppelin.common.Message) ServiceContext(org.apache.zeppelin.service.ServiceContext) Mockito.anyString(org.mockito.Mockito.anyString) IOException(java.io.IOException) Paragraph(org.apache.zeppelin.notebook.Paragraph) AngularObject(org.apache.zeppelin.display.AngularObject) Map(java.util.Map) Test(org.junit.Test)

Example 12 with ServiceContext

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

the class NotebookServerTest method testImportJupyterNote.

@Test
public void testImportJupyterNote() throws IOException {
    String jupyterNoteJson = IOUtils.toString(getClass().getResourceAsStream("/Lecture-4.ipynb"), StandardCharsets.UTF_8);
    String msg = "{\"op\":\"IMPORT_NOTE\",\"data\":" + "{\"note\": " + jupyterNoteJson + "}}";
    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 testImportJupyterNote, failed nothing to " + "worry ", e);
        }
        notebook.processNote(noteId, note -> {
            assertNotNull(note);
            assertTrue(note.getName(), note.getName().startsWith("Note converted from Jupyter_"));
            assertEquals("md", note.getParagraphs().get(0).getIntpText());
            assertEquals("\n# matplotlib - 2D and 3D plotting in Python", note.getParagraphs().get(0).getScriptText());
            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 13 with ServiceContext

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

the class AbstractRestApi method getServiceContext.

protected ServiceContext getServiceContext() {
    AuthenticationInfo authInfo = new AuthenticationInfo(authenticationService.getPrincipal());
    Set<String> userAndRoles = new HashSet<>();
    userAndRoles.add(authenticationService.getPrincipal());
    userAndRoles.addAll(authenticationService.getAssociatedRoles());
    return new ServiceContext(authInfo, userAndRoles);
}
Also used : ServiceContext(org.apache.zeppelin.service.ServiceContext) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) HashSet(java.util.HashSet)

Example 14 with ServiceContext

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

the class NotebookRepoRestApi method getServiceContext.

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

Example 15 with ServiceContext

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

the class NotebookRestApi method cloneNote.

/**
 * Clone note REST API.
 *
 * @param noteId ID of Note
 * @return JSON with status.OK
 * @throws IOException
 * @throws CloneNotSupportedException
 * @throws IllegalArgumentException
 */
@POST
@Path("{noteId}")
@ZeppelinApi
public Response cloneNote(@PathParam("noteId") String noteId, String message) throws IOException, IllegalArgumentException {
    LOGGER.info("Clone note by JSON {}", message);
    checkIfUserCanWrite(noteId, "Insufficient privileges you cannot clone this note");
    NewNoteRequest request = NewNoteRequest.fromJson(message);
    String newNoteName = null;
    if (request != null) {
        newNoteName = request.getName();
    }
    AuthenticationInfo subject = new AuthenticationInfo(authenticationService.getPrincipal());
    String newNoteId = notebookService.cloneNote(noteId, newNoteName, getServiceContext(), new RestServiceCallback<Note>() {

        @Override
        public void onSuccess(Note newNote, ServiceContext context) throws IOException {
            notebookServer.broadcastNote(newNote);
            notebookServer.broadcastNoteList(subject, context.getUserAndRoles());
        }
    });
    return new JsonResponse<>(Status.OK, "", newNoteId).build();
}
Also used : ServiceContext(org.apache.zeppelin.service.ServiceContext) Note(org.apache.zeppelin.notebook.Note) IOException(java.io.IOException) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

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