Search in sources :

Example 26 with JsonResponse

use of org.apache.zeppelin.server.JsonResponse in project zeppelin by apache.

the class CredentialRestApi method putCredentials.

/**
 * Put User Credentials REST API.
 *
 * @param message - JSON with entity, username, password.
 * @return JSON with status.OK
 */
@PUT
public Response putCredentials(String message) {
    Map<String, String> messageMap = gson.fromJson(message, new TypeToken<Map<String, String>>() {
    }.getType());
    String entity = messageMap.get("entity");
    String username = messageMap.get("username");
    String password = messageMap.get("password");
    if (StringUtils.isEmpty(entity) || StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
        return new JsonResponse<>(Status.BAD_REQUEST).build();
    }
    String user = authenticationService.getPrincipal();
    LOGGER.info("Update credentials for user {} entity {}", user, entity);
    UserCredentials uc;
    try {
        uc = credentials.getUserCredentials(user);
        uc.putUsernamePassword(entity, new UsernamePassword(username, password));
        credentials.putUserCredentials(user, uc);
        return new JsonResponse<>(Status.OK).build();
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        return new JsonResponse<>(Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) UserCredentials(org.apache.zeppelin.user.UserCredentials) IOException(java.io.IOException) JsonResponse(org.apache.zeppelin.server.JsonResponse) UsernamePassword(org.apache.zeppelin.user.UsernamePassword) PUT(javax.ws.rs.PUT)

Example 27 with JsonResponse

use of org.apache.zeppelin.server.JsonResponse in project zeppelin by apache.

the class CredentialRestApi method getCredentials.

/**
 * Get User Credentials list REST API.
 *
 * @return JSON with status.OK
 */
@GET
public Response getCredentials() {
    String user = authenticationService.getPrincipal();
    LOGGER.info("getCredentials for user {} ", user);
    UserCredentials uc;
    try {
        uc = credentials.getUserCredentials(user);
        return new JsonResponse<>(Status.OK, uc).build();
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        return new JsonResponse<>(Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : UserCredentials(org.apache.zeppelin.user.UserCredentials) IOException(java.io.IOException) JsonResponse(org.apache.zeppelin.server.JsonResponse) GET(javax.ws.rs.GET)

Example 28 with JsonResponse

use of org.apache.zeppelin.server.JsonResponse in project zeppelin by apache.

the class InterpreterRestApi method installInterpreter.

/**
 * Install interpreter
 */
@POST
@Path("install")
@ZeppelinApi
public Response installInterpreter(@NotNull String message) {
    LOGGER.info("Install interpreter: {}", message);
    InterpreterInstallationRequest request = InterpreterInstallationRequest.fromJson(message);
    try {
        interpreterService.installInterpreter(request, new SimpleServiceCallback<String>() {

            @Override
            public void onStart(String message, ServiceContext context) {
                Message m = new Message(OP.INTERPRETER_INSTALL_STARTED);
                Map<String, Object> data = new HashMap<>();
                data.put("result", "Starting");
                data.put("message", message);
                m.data = data;
                notebookServer.broadcast(m);
            }

            @Override
            public void onSuccess(String message, ServiceContext context) {
                Message m = new Message(OP.INTERPRETER_INSTALL_RESULT);
                Map<String, Object> data = new HashMap<>();
                data.put("result", "Succeed");
                data.put("message", message);
                m.data = data;
                notebookServer.broadcast(m);
            }

            @Override
            public void onFailure(Exception ex, ServiceContext context) {
                Message m = new Message(OP.INTERPRETER_INSTALL_RESULT);
                Map<String, Object> data = new HashMap<>();
                data.put("result", "Failed");
                data.put("message", ex.getMessage());
                m.data = data;
                notebookServer.broadcast(m);
            }
        });
    } catch (Throwable t) {
        return new JsonResponse<>(Status.INTERNAL_SERVER_ERROR, t.getMessage()).build();
    }
    return new JsonResponse<>(Status.OK).build();
}
Also used : InterpreterInstallationRequest(org.apache.zeppelin.rest.message.InterpreterInstallationRequest) Message(org.apache.zeppelin.common.Message) ServiceContext(org.apache.zeppelin.service.ServiceContext) HashMap(java.util.HashMap) Map(java.util.Map) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) IOException(java.io.IOException) JsonResponse(org.apache.zeppelin.server.JsonResponse) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

Example 29 with JsonResponse

use of org.apache.zeppelin.server.JsonResponse in project zeppelin by apache.

the class InterpreterRestApi method newSettings.

/**
 * Add new interpreter setting.
 *
 * @param message NewInterpreterSettingRequest
 */
@POST
@Path("setting")
@ZeppelinApi
public Response newSettings(String message) {
    try {
        NewInterpreterSettingRequest request = NewInterpreterSettingRequest.fromJson(message);
        if (request == null) {
            return new JsonResponse<>(Status.BAD_REQUEST).build();
        }
        InterpreterSetting interpreterSetting = interpreterSettingManager.createNewSetting(request.getName(), request.getGroup(), request.getDependencies(), request.getOption(), request.getProperties());
        LOGGER.info("new setting created with {}", interpreterSetting.getId());
        return new JsonResponse<>(Status.OK, "", interpreterSetting).build();
    } catch (IOException e) {
        LOGGER.error("Exception in InterpreterRestApi while creating ", e);
        return new JsonResponse<>(Status.NOT_FOUND, e.getMessage(), ExceptionUtils.getStackTrace(e)).build();
    }
}
Also used : InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) NewInterpreterSettingRequest(org.apache.zeppelin.rest.message.NewInterpreterSettingRequest) IOException(java.io.IOException) JsonResponse(org.apache.zeppelin.server.JsonResponse) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

Example 30 with JsonResponse

use of org.apache.zeppelin.server.JsonResponse in project zeppelin by apache.

the class HeliumRestApi method load.

@POST
@Path("load/{noteId}/{paragraphId}")
public Response load(@PathParam("noteId") String noteId, @PathParam("paragraphId") String paragraphId, String heliumPackage) {
    try {
        return notebook.processNote(noteId, note -> {
            if (note == null) {
                return new JsonResponse<>(Response.Status.NOT_FOUND, "Note " + noteId + " not found").build();
            }
            Paragraph paragraph = note.getParagraph(paragraphId);
            if (paragraph == null) {
                return new JsonResponse<>(Response.Status.NOT_FOUND, "Paragraph " + paragraphId + " not found").build();
            }
            HeliumPackage pkg = HeliumPackage.fromJson(heliumPackage);
            try {
                return new JsonResponse<>(Response.Status.OK, "", helium.getApplicationFactory().loadAndRun(pkg, paragraph)).build();
            } catch (RuntimeException e) {
                logger.error(e.getMessage(), e);
                return new JsonResponse<>(Response.Status.INTERNAL_SERVER_ERROR, e.getMessage()).build();
            }
        });
    } catch (IOException e) {
        return new JsonResponse<>(Response.Status.NOT_FOUND, "Fail to get note: " + noteId + "\n" + ExceptionUtils.getStackTrace(e)).build();
    }
}
Also used : HeliumPackage(org.apache.zeppelin.helium.HeliumPackage) IOException(java.io.IOException) JsonResponse(org.apache.zeppelin.server.JsonResponse) Paragraph(org.apache.zeppelin.notebook.Paragraph) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

JsonResponse (org.apache.zeppelin.server.JsonResponse)37 ZeppelinApi (org.apache.zeppelin.annotation.ZeppelinApi)22 Path (javax.ws.rs.Path)20 IOException (java.io.IOException)12 POST (javax.ws.rs.POST)12 UserCredentials (org.apache.zeppelin.user.UserCredentials)8 PUT (javax.ws.rs.PUT)6 Note (org.apache.zeppelin.notebook.Note)6 GET (javax.ws.rs.GET)5 Paragraph (org.apache.zeppelin.notebook.Paragraph)5 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)5 HashMap (java.util.HashMap)4 Subject (org.apache.shiro.subject.Subject)4 ZeppelinConfiguration (org.apache.zeppelin.conf.ZeppelinConfiguration)4 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)4 Map (java.util.Map)3 DELETE (javax.ws.rs.DELETE)3 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)3 MetaStoreException (org.smartdata.metastore.MetaStoreException)3 UserInfo (org.smartdata.model.UserInfo)3