This is an automated email from the ASF dual-hosted git repository. zjffdu pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/zeppelin.git
The following commit(s) were added to refs/heads/master by this push: new 631d6f1 [ZEPPELIN-5642] Provide REST API and SDK to get noteId by notePath 631d6f1 is described below commit 631d6f18108628c8014261843b1017ac8e6a72fd Author: huage1994 <guanhua...@foxmail.com> AuthorDate: Wed Jan 26 16:37:55 2022 +0800 [ZEPPELIN-5642] Provide REST API and SDK to get noteId by notePath ### What is this PR for? Provide REST API and SDK to get noteId by notePath. ### What type of PR is it? [Feature] ### Todos * [ ] - Task ### What is the Jira issue? * Open an issue on Jira https://issues.apache.org/jira/browse/ZEPPELIN-5642 ### How should this be tested? CI passed ### Screenshots (if appropriate) ### Questions: * Does the licenses files need update? No * Is there breaking changes for older versions? No * Does this needs documentation? No Author: huage1994 <guanhua...@foxmail.com> Closes #4292 from huage1994/ZEPPELIN-5642 and squashes the following commits: 6fbebfb0fd [huage1994] [ZEPPELIN-5642] Provide REST API and SDK to get noteId by notePath --- .../org/apache/zeppelin/client/ZeppelinClient.java | 25 +++++++++++++ .../org/apache/zeppelin/rest/NotebookRestApi.java | 34 +++++++++++++---- .../rest/message/GetNoteByPathRequest.java | 32 ++++++++++++++++ .../apache/zeppelin/service/NotebookService.java | 25 +++++++++++++ .../apache/zeppelin/rest/NotebookRestApiTest.java | 43 ++++++++++++++++++++++ .../org/apache/zeppelin/notebook/NoteManager.java | 5 +++ .../org/apache/zeppelin/notebook/Notebook.java | 4 ++ 7 files changed, 160 insertions(+), 8 deletions(-) diff --git a/zeppelin-client/src/main/java/org/apache/zeppelin/client/ZeppelinClient.java b/zeppelin-client/src/main/java/org/apache/zeppelin/client/ZeppelinClient.java index 9daec97..2f8f6bc 100644 --- a/zeppelin-client/src/main/java/org/apache/zeppelin/client/ZeppelinClient.java +++ b/zeppelin-client/src/main/java/org/apache/zeppelin/client/ZeppelinClient.java @@ -382,6 +382,27 @@ public class ZeppelinClient { .get("/notebook/{noteId}") .routeParam("noteId", noteId) .asJson(); + return extractNoteResultFromResponse(response); + } + + /** + * Query {@link NoteResult} with provided notePath. + * + * @param notePath + * @return + * @throws Exception + */ + public NoteResult queryNoteResultByPath(String notePath) throws Exception { + JSONObject bodyObject = new JSONObject(); + bodyObject.put("notePath", notePath); + HttpResponse<JsonNode> response = Unirest + .post("/notebook/getByPath") + .body(bodyObject) + .asJson(); + return extractNoteResultFromResponse(response); + } + + private NoteResult extractNoteResultFromResponse(HttpResponse<JsonNode> response) throws Exception { checkResponse(response); JsonNode jsonNode = response.getBody(); checkJsonNodeStatus(jsonNode); @@ -394,6 +415,10 @@ public class ZeppelinClient { isRunning = Boolean.parseBoolean(infoJsonObject.getString("isRunning")); } } + String noteId = null; + if (noteJsonObject.has("id")) { + noteId = noteJsonObject.getString("id"); + } String notePath = null; if (noteJsonObject.has("path")) { diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java index a4b2004..d68a68a 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java @@ -53,14 +53,7 @@ import org.apache.zeppelin.rest.exception.BadRequestException; import org.apache.zeppelin.rest.exception.ForbiddenException; import org.apache.zeppelin.rest.exception.NoteNotFoundException; import org.apache.zeppelin.rest.exception.ParagraphNotFoundException; -import org.apache.zeppelin.rest.message.CronRequest; -import org.apache.zeppelin.rest.message.NewNoteRequest; -import org.apache.zeppelin.rest.message.NewParagraphRequest; -import org.apache.zeppelin.rest.message.NoteJobStatus; -import org.apache.zeppelin.rest.message.ParagraphJobStatus; -import org.apache.zeppelin.rest.message.RenameNoteRequest; -import org.apache.zeppelin.rest.message.ParametersRequest; -import org.apache.zeppelin.rest.message.UpdateParagraphRequest; +import org.apache.zeppelin.rest.message.*; import org.apache.zeppelin.search.SearchService; import org.apache.zeppelin.server.JsonResponse; import org.apache.zeppelin.service.AuthenticationService; @@ -339,6 +332,31 @@ public class NotebookRestApi extends AbstractRestApi { note -> new JsonResponse<>(Status.OK, "", note).build()); } + + /** + * Get note of this specified notePath. + * + * @param message - JSON containing notePath + * @param reload + * @return + * @throws IOException + */ + + @POST + @Path("getByPath") + @ZeppelinApi + public Response getNoteByPath(String message, + @QueryParam("reload") boolean reload) throws IOException { + // notePath may contains special character like space. + // it should be in http body instead of in url + // to avoid problem of url conversion by external service like knox + GetNoteByPathRequest request = GSON.fromJson(message, GetNoteByPathRequest.class); + String notePath = request.getNotePath(); + return notebookService.getNoteByPath(notePath, reload, getServiceContext(), new RestServiceCallback<>(), + note -> new JsonResponse<>(Status.OK, "", note).build()); + } + + /** * Export note REST API. * diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/message/GetNoteByPathRequest.java b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/message/GetNoteByPathRequest.java new file mode 100644 index 0000000..937d787 --- /dev/null +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/message/GetNoteByPathRequest.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.zeppelin.rest.message; + +/** + * GetNoteByPathRequest rest api request message. + */ +public class GetNoteByPathRequest { + + private String notePath; + + public GetNoteByPathRequest() { + } + + public String getNotePath() { + return notePath; + } +} diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/service/NotebookService.java b/zeppelin-server/src/main/java/org/apache/zeppelin/service/NotebookService.java index f1e9904..fff9a68 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/service/NotebookService.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/service/NotebookService.java @@ -154,6 +154,31 @@ public class NotebookService { } /** + * @param notePath + * @param reload + * @param context + * @param callback + * @return + * @throws IOException + */ + public <T> T getNoteByPath(String notePath, + boolean reload, + ServiceContext context, + ServiceCallback<Note> callback, + NoteProcessor<T> noteProcessor) throws IOException { + String noteId = null; + try { + noteId = notebook.getNoteIdByPath(notePath); + } catch (IOException e) { + throw new NoteNotFoundException(notePath); + } + return getNote(noteId, reload, context, callback, noteProcessor); + } + + + + + /** * * @param notePath * @param defaultInterpreterGroup diff --git a/zeppelin-server/src/test/java/org/apache/zeppelin/rest/NotebookRestApiTest.java b/zeppelin-server/src/test/java/org/apache/zeppelin/rest/NotebookRestApiTest.java index 9d2401c..dc95df5 100644 --- a/zeppelin-server/src/test/java/org/apache/zeppelin/rest/NotebookRestApiTest.java +++ b/zeppelin-server/src/test/java/org/apache/zeppelin/rest/NotebookRestApiTest.java @@ -120,6 +120,49 @@ public class NotebookRestApiTest extends AbstractTestRestApi { } } } + + @Test + public void testGetNoteByPath() throws IOException { + LOG.info("Running testGetNoteByPath"); + String note1Id = null; + try { + String notePath = "dir1/note1"; + note1Id = TestUtils.getInstance(Notebook.class).createNote(notePath, anonymous); + TestUtils.getInstance(Notebook.class).processNote(note1Id, + note1 -> { + note1.addNewParagraph(AuthenticationInfo.ANONYMOUS); + TestUtils.getInstance(Notebook.class).saveNote(note1, anonymous); + return null; + }); + + CloseableHttpResponse post = httpPost("/notebook/getByPath" , "{\"notePath\":\""+ notePath + "\"}" ); + + assertThat(post, isAllowed()); + Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), + new TypeToken<Map<String, Object>>() {}.getType()); + Map<String, Object> noteObject = (Map<String, Object>) resp.get("body"); + assertEquals(notePath, ((String)noteObject.get("path")).substring(1)); + post.close(); + } finally { + // cleanup + if (null != note1Id) { + TestUtils.getInstance(Notebook.class).removeNote(note1Id, anonymous); + } + } + } + + @Test + public void testGetNoteByPathWithPathNotExist() throws IOException { + LOG.info("Running testGetNoteByPathWithPathNotExist"); + String notePath = "A note that doesn't exist"; + CloseableHttpResponse post = httpPost("/notebook/getByPath" , "{\"notePath\":\""+ notePath + "\"}" ); + assertThat(post, isNotFound()); + Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), + new TypeToken<Map<String, Object>>() {}.getType()); + String status = (String) resp.get("status"); + assertEquals(status, "NOT_FOUND"); + post.close(); + } @Test public void testGetNoteParagraphJobStatus() throws IOException { diff --git a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/NoteManager.java b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/NoteManager.java index be842c9..ba6e443 100644 --- a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/NoteManager.java +++ b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/NoteManager.java @@ -417,6 +417,11 @@ public class NoteManager { return true; } + public String getNoteIdByPath(String notePath) throws IOException { + NoteNode noteNode = getNoteNode(notePath); + return noteNode.getNoteId(); + } + /** * Represent one folder that could contains sub folders and note files. */ diff --git a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Notebook.java b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Notebook.java index 9147367..b985b1e 100644 --- a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Notebook.java +++ b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Notebook.java @@ -415,6 +415,10 @@ public class Notebook { } + public String getNoteIdByPath(String notePath) throws IOException { + return noteManager.getNoteIdByPath(notePath); + } + public void saveNote(Note note, AuthenticationInfo subject) throws IOException { noteManager.saveNote(note, subject); }