ZEPPELIN-1897 REST API - Update paragraph

This commit is contained in:
chilang 2017-09-27 11:40:21 +01:00
parent 12d1bb9a0d
commit 9cc86dee63
4 changed files with 163 additions and 5 deletions

View file

@ -752,6 +752,59 @@ Notebooks REST API supports the following operations: List, Create, Get, Delete,
</tr>
</table>
<br/>
### Update paragraph
<table class="table-configuration">
<col width="200">
<tr>
<td>Description</td>
<td>This ```PUT``` method update paragraph contents using given id, e.g. <code>{"text": "hello"}</code>
</td>
</tr>
<tr>
<td>URL</td>
<td>```http://[zeppelin-server]:[zeppelin-port]/api/notebook/[noteId]/paragraph/[paragraphId]```</td>
</tr>
<tr>
<td>Success code</td>
<td>200</td>
</tr>
<tr>
<td>Bad Request code</td>
<td>400</td>
</tr>
<tr>
<td>Forbidden code</td>
<td>403</td>
</tr>
<tr>
<td>Not Found code</td>
<td>404</td>
</tr>
<tr>
<td>Fail code</td>
<td>500</td>
</tr>
<tr>
<td>sample JSON input</td>
<td><pre>
{
"title": "Hello world",
"text": "println(\"hello world\")"
}</pre></td>
</tr>
<tr>
<td>sample JSON response</td>
<td><pre>
{
"status": "OK",
"message": ""
}
}</pre></td>
</tr>
</table>
<br/>
### Update paragraph configuration
<table class="table-configuration">

View file

@ -41,10 +41,7 @@ import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.rest.exception.BadRequestException;
import org.apache.zeppelin.rest.exception.NotFoundException;
import org.apache.zeppelin.rest.exception.ForbiddenException;
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.RunParagraphWithParametersRequest;
import org.apache.zeppelin.rest.message.*;
import org.apache.zeppelin.search.SearchService;
import org.apache.zeppelin.server.JsonResponse;
import org.apache.zeppelin.socket.NotebookServer;
@ -496,6 +493,37 @@ public class NotebookRestApi {
return new JsonResponse<>(Status.OK, "", p).build();
}
/**
* Update paragraph
*
* @param message json containing the "text" and optionally the "title" of the paragraph, e.g.
* {"text" : "updated text", "title" : "Updated title" }
*
*/
@PUT
@Path("{noteId}/paragraph/{paragraphId}")
@ZeppelinApi
public Response updateParagraph(@PathParam("noteId") String noteId,
@PathParam("paragraphId") String paragraphId,
String message) throws IOException {
String user = SecurityUtils.getPrincipal();
LOG.info("{} will update paragraph {} {}", user, noteId, paragraphId);
Note note = notebook.getNote(noteId);
checkIfNoteIsNotNull(note);
checkIfUserCanWrite(noteId, "Insufficient privileges you cannot update this paragraph");
Paragraph p = note.getParagraph(paragraphId);
checkIfParagraphIsNotNull(p);
UpdateParagraphRequest updatedParagraph = gson.fromJson(message, UpdateParagraphRequest.class);
p.setText(updatedParagraph.getText());
if (updatedParagraph.getTitle() != null) { p.setTitle(updatedParagraph.getTitle()); }
AuthenticationInfo subject = new AuthenticationInfo(user);
note.persist(subject);
notebookServer.broadcastParagraph(note, p);
return new JsonResponse<>(Status.OK, "").build();
}
@PUT
@Path("{noteId}/paragraph/{paragraphId}/config")
@ZeppelinApi
@ -514,7 +542,6 @@ public class NotebookRestApi {
configureParagraph(p, newConfig, user);
AuthenticationInfo subject = new AuthenticationInfo(user);
note.persist(subject);
return new JsonResponse<>(Status.OK, "", p).build();
}

View file

@ -0,0 +1,40 @@
/*
* 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;
/**
* UpdateParagraphRequest
*/
public class UpdateParagraphRequest {
String title;
String text;
public UpdateParagraphRequest() {
}
public String getTitle() {
return title;
}
public String getText() {
return text;
}
}

View file

@ -27,6 +27,7 @@ import com.google.common.collect.Sets;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.zeppelin.notebook.Note;
import org.apache.zeppelin.notebook.Paragraph;
@ -651,6 +652,43 @@ public class ZeppelinRestApiTest extends AbstractTestRestApi {
ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
@Test
public void testUpdateParagraph() throws IOException {
Note note = ZeppelinServer.notebook.createNote(anonymous);
String jsonRequest = "{\"title\": \"title1\", \"text\": \"text1\"}";
PostMethod post = httpPost("/notebook/" + note.getId() + "/paragraph", jsonRequest);
Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {}.getType());
post.releaseConnection();
String newParagraphId = (String) resp.get("body");
Paragraph newParagraph = ZeppelinServer.notebook.getNote(note.getId()).getParagraph(newParagraphId);
assertEquals("title1", newParagraph.getTitle());
assertEquals("text1", newParagraph.getText());
String updateRequest = "{\"text\": \"updated text\"}";
PutMethod put = httpPut("/notebook/" + note.getId() + "/paragraph/" + newParagraphId, updateRequest);
assertThat("Test update method:", put, isAllowed());
put.releaseConnection();
Paragraph updatedParagraph = ZeppelinServer.notebook.getNote(note.getId()).getParagraph(newParagraphId);
assertEquals("title1", updatedParagraph.getTitle());
assertEquals("updated text", updatedParagraph.getText());
String updateBothRequest = "{\"title\": \"updated title\", \"text\" : \"updated text 2\" }";
PutMethod updatePut = httpPut("/notebook/" + note.getId() + "/paragraph/" + newParagraphId, updateBothRequest);
updatePut.releaseConnection();
Paragraph updatedBothParagraph = ZeppelinServer.notebook.getNote(note.getId()).getParagraph(newParagraphId);
assertEquals("updated title", updatedBothParagraph.getTitle());
assertEquals("updated text 2", updatedBothParagraph.getText());
ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
@Test
public void testGetParagraph() throws IOException {
Note note = ZeppelinServer.notebook.createNote(anonymous);