Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion docs/rest-api/rest-notebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ If you work with Apache Zeppelin and find a need for an additional REST API, ple
<col width="200">
<tr>
<td>Description</td>
<td> This ```POST``` method runs the paragraph synchronously by given note and paragraph id. This API can return SUCCESS or ERROR depending on the outcome of the paragraph execution
<td>This ```POST``` method runs the paragraph synchronously by given note and paragraph id. This API can return SUCCESS or ERROR depending on the outcome of the paragraph execution
</td>
</tr>
<tr>
Expand Down Expand Up @@ -972,3 +972,39 @@ If you work with Apache Zeppelin and find a need for an additional REST API, ple
</tr>
</tr>
</table>

<br />
### Clear all paragraph result
<table class="table-configuration">
<col width="200">
<tr>
<td>Description</td>
<td>This ```PUT``` method clear all paragraph results from note of given id.
</td>
</tr>
<tr>
<td>URL</td>
<td>```http://[zeppelin-server]:[zeppelin-port]/api/notebook/[noteId]/clear```</td>
</tr>
<tr>
<td>Success code</td>
<td>200</td>
</tr>
<tr>
<td>Forbidden code</td>
<td>401</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 response</td>
<td><pre>{"status": "OK"}</pre></td>
</tr>
</tr>
</table>
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import org.apache.zeppelin.notebook.NotebookAuthorization;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.rest.exception.NotFoundException;
import org.apache.zeppelin.rest.exception.UnauthorizedException;
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;
Expand Down Expand Up @@ -124,7 +124,7 @@ private void checkIfUserIsOwner(String noteId, String errorMsg) {
userAndRoles.add(SecurityUtils.getPrincipal());
userAndRoles.addAll(SecurityUtils.getRoles());
if (!notebookAuthorization.isOwner(userAndRoles, noteId)) {
throw new UnauthorizedException(errorMsg);
throw new ForbiddenException(errorMsg);
}
}

Expand All @@ -136,7 +136,7 @@ private void checkIfUserCanWrite(String noteId, String errorMsg) {
userAndRoles.add(SecurityUtils.getPrincipal());
userAndRoles.addAll(SecurityUtils.getRoles());
if (!notebookAuthorization.hasWriteAuthorization(userAndRoles, noteId)) {
throw new UnauthorizedException(errorMsg);
throw new ForbiddenException(errorMsg);
}
}

Expand All @@ -148,7 +148,7 @@ private void checkIfUserCanRead(String noteId, String errorMsg) {
userAndRoles.add(SecurityUtils.getPrincipal());
userAndRoles.addAll(SecurityUtils.getRoles());
if (!notebookAuthorization.hasReadAuthorization(userAndRoles, noteId)) {
throw new UnauthorizedException(errorMsg);
throw new ForbiddenException(errorMsg);
}
}

Expand Down Expand Up @@ -516,6 +516,27 @@ public Response deleteParagraph(@PathParam("noteId") String noteId,
return new JsonResponse(Status.OK, "").build();
}

/**
* Clear result of all paragraphs REST API
*
* @param noteId ID of Note
* @return JSON with status.ok
*/
@PUT
@Path("{noteId}/clear")
@ZeppelinApi
public Response clearAllParagraphOutput(@PathParam("noteId") String noteId)
throws IOException {
LOG.info("clear all paragraph output of note {}", noteId);
checkIfUserCanWrite(noteId, "Insufficient privileges you cannot clear this note");

Note note = notebook.getNote(noteId);
checkIfNoteIsNotNull(note);
note.clearAllParagraphOutput();

return new JsonResponse(Status.OK, "").build();
}

/**
* Run note jobs REST API
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.zeppelin.rest.exception;

import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
Expand All @@ -27,24 +28,24 @@
* UnauthorizedException handler for WebApplicationException.
*
*/
public class UnauthorizedException extends WebApplicationException {
public class ForbiddenException extends WebApplicationException {
private static final long serialVersionUID = 4394749068760407567L;
private static final String UNAUTHORIZED_MSG = "Authorization required";
private static final String FORBIDDEN_MSG = "Not allowed to access";

public UnauthorizedException() {
super(unauthorizedJson(UNAUTHORIZED_MSG));
public ForbiddenException() {
super(forbiddenJson(FORBIDDEN_MSG));
}

private static Response unauthorizedJson(String message) {
private static Response forbiddenJson(String message) {
return ExceptionUtils.jsonResponseContent(FORBIDDEN, message);
}

public UnauthorizedException(Throwable cause, String message) {
super(cause, unauthorizedJson(message));
public ForbiddenException(Throwable cause, String message) {
super(cause, forbiddenJson(message));
}

public UnauthorizedException(String message) {
super(unauthorizedJson(message));
public ForbiddenException(String message) {
super(forbiddenJson(message));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ public void onMessage(NotebookSocket conn, String msg) {
case PARAGRAPH_CLEAR_OUTPUT:
clearParagraphOutput(conn, userAndRoles, notebook, messagereceived);
break;
case PARAGRAPH_CLEAR_ALL_OUTPUT:
clearAllParagraphOutput(conn, userAndRoles, notebook, messagereceived);
break;
case NOTE_UPDATE:
updateNote(conn, userAndRoles, notebook, messagereceived);
break;
Expand Down Expand Up @@ -822,6 +825,25 @@ private void cloneNote(NotebookSocket conn, HashSet<String> userAndRoles,
broadcastNoteList(subject, userAndRoles);
}

private void clearAllParagraphOutput(NotebookSocket conn, HashSet<String> userAndRoles,
Notebook notebook, Message fromMessage)
throws IOException {
final String noteId = (String) fromMessage.get("id");
if (StringUtils.isBlank(noteId)) {
return;
}
Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "clear output", fromMessage.principal,
userAndRoles, notebookAuthorization.getOwners(noteId));
return;
}

note.clearAllParagraphOutput();
broadcastNote(note);
}

protected Note importNote(NotebookSocket conn, HashSet<String> userAndRoles,
Notebook notebook, Message fromMessage)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ public static void ps() {


/** Status code matcher */
protected Matcher<? super HttpMethodBase> isForbiden() { return responsesWith(403); }
protected Matcher<? super HttpMethodBase> isForbidden() { return responsesWith(403); }

protected Matcher<? super HttpMethodBase> isAllowed() {
return responsesWith(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
package org.apache.zeppelin.rest;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.notebook.Note;
import org.apache.zeppelin.notebook.NotebookAuthorization;
import org.apache.zeppelin.notebook.NotebookAuthorizationInfoSaving;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.server.ZeppelinServer;
import org.apache.zeppelin.user.AuthenticationInfo;
import org.junit.AfterClass;
Expand All @@ -37,12 +36,11 @@
import org.junit.runners.MethodSorters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

/**
Expand Down Expand Up @@ -179,6 +177,43 @@ public void testCloneNote() throws IOException {
ZeppelinServer.notebook.removeNote(clonedNoteId, anonymous);

}

@Test
public void testClearAllParagraphOutput() throws IOException {
// Create note and set result explicitly
Note note = ZeppelinServer.notebook.createNote(anonymous);
Paragraph p1 = note.addParagraph();
InterpreterResult result = new InterpreterResult(InterpreterResult.Code.SUCCESS, InterpreterResult.Type.TEXT, "result");
p1.setResult(result);

Paragraph p2 = note.addParagraph();
p2.setReturn(result, new Throwable());

// clear paragraph result
PutMethod put = httpPut("/notebook/" + note.getId() + "/clear", "");
LOG.info("test clear paragraph output response\n" + put.getResponseBodyAsString());
assertThat(put, isAllowed());
put.releaseConnection();

// check if paragraph results are cleared
GetMethod get = httpGet("/notebook/" + note.getId() + "/paragraph/" + p1.getId());
assertThat(get, isAllowed());
Map<String, Object> resp1 = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
}.getType());
Map<String, Object> resp1Body = (Map<String, Object>) resp1.get("body");
assertNull(resp1Body.get("result"));

get = httpGet("/notebook/" + note.getId() + "/paragraph/" + p2.getId());
assertThat(get, isAllowed());
Map<String, Object> resp2 = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
}.getType());
Map<String, Object> resp2Body = (Map<String, Object>) resp2.get("body");
assertNull(resp2Body.get("result"));
get.releaseConnection();

//cleanup
ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public void testThatOtherUserCannotAccessNoteIfPermissionSet() throws IOExceptio
//set permission
String payload = "{ \"owners\": [\"admin\"], \"readers\": [\"user2\"], \"writers\": [\"user2\"] }";
PutMethod put = httpPut("/notebook/" + noteId + "/permissions", payload , "admin", "password1");
assertThat("test set note premission method:", put, isAllowed());
assertThat("test set note permission method:", put, isAllowed());
put.releaseConnection();

userTryGetNote(noteId, "user1", "password2", isForbiden());
userTryGetNote(noteId, "user1", "password2", isForbidden());

userTryGetNote(noteId, "user2", "password3", isAllowed());

Expand All @@ -99,10 +99,10 @@ public void testThatWriterCannotRemoveNote() throws IOException {
//set permission
String payload = "{ \"owners\": [\"admin\", \"user1\"], \"readers\": [\"user2\"], \"writers\": [\"user2\"] }";
PutMethod put = httpPut("/notebook/" + noteId + "/permissions", payload , "admin", "password1");
assertThat("test set note premission method:", put, isAllowed());
assertThat("test set note permission method:", put, isAllowed());
put.releaseConnection();

userTryRemoveNote(noteId, "user2", "password3", isForbiden());
userTryRemoveNote(noteId, "user2", "password3", isForbidden());
userTryRemoveNote(noteId, "user1", "password2", isAllowed());

Note deletedNote = ZeppelinServer.notebook.getNote(noteId);
Expand Down
15 changes: 12 additions & 3 deletions zeppelin-web/src/app/home/home.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
'websocketMsgSrv',
'$rootScope',
'arrayOrderingSrv',
'ngToast'
'ngToast',
'noteActionSrv'
];

function HomeCtrl($scope, noteListDataFactory, websocketMsgSrv, $rootScope, arrayOrderingSrv, ngToast) {
function HomeCtrl($scope, noteListDataFactory, websocketMsgSrv, $rootScope, arrayOrderingSrv,
ngToast, noteActionSrv) {
ngToast.dismiss();
var vm = this;
vm.notes = noteListDataFactory;
Expand Down Expand Up @@ -85,6 +87,13 @@
vm.notebookHome = false;
}
});
}

$scope.removeNote = function(noteId) {
noteActionSrv.removeNote(noteId, false);
};

$scope.clearAllParagraphOutput = function(noteId) {
noteActionSrv.clearAllParagraphOutput(noteId);
};
}
})();
16 changes: 15 additions & 1 deletion zeppelin-web/src/app/home/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@
-->

<script type="text/ng-template" id="notebook_folder_renderer.html">
<div ng-if="node.children == null">
<div ng-if="node.children == null"
ng-mouseenter="showButton=true"
ng-mouseleave="showButton=false">
<a style="text-decoration: none;" href="#/notebook/{{node.id}}">
<i style="font-size: 10px;" class="icon-doc"/> {{noteName(node)}}
</a>
<a style="text-decoration: none;">
<i style="font-size: 13px; margin-left: 10px; cursor: pointer; text-decoration: none;"
class="fa fa-eraser" ng-show="showButton" ng-click="clearAllParagraphOutput(node.id)"
tooltip-placement="bottom" tooltip="Clear output">
</i>
</a>
<a style="text-decoration: none;">
<i style="font-size: 13px; margin-left: 2px; cursor: pointer; text-decoration: none;"
class="fa fa-trash-o" ng-show="showButton" ng-click="removeNote(node.id)"
tooltip-placement="bottom" tooltip="Remove note">
</i>
</a>
</div>
<div ng-if="node.children != null">
<a style="text-decoration: none; cursor: pointer;" ng-click="toggleFolderNode(node)">
Expand Down
2 changes: 1 addition & 1 deletion zeppelin-web/src/app/notebook/notebook-actionBar.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ <h3>
</button>
<button type="button"
class="btn btn-default btn-xs"
ng-click="clearAllParagraphOutput()"
ng-click="clearAllParagraphOutput(note.id)"
ng-hide="viewOnly"
ng-class="{'disabled':isNoteRunning()}"
tooltip-placement="bottom" tooltip="Clear output">
Expand Down
Loading