Search in sources :

Example 1 with WebProtegeSession

use of edu.stanford.bmir.protege.web.server.session.WebProtegeSession in project webprotege by protegeproject.

the class LogOutUserActionHandler method execute.

@Nonnull
@Override
public LogOutUserResult execute(@Nonnull LogOutUserAction action, @Nonnull ExecutionContext executionContext) {
    WebProtegeSession session = executionContext.getSession();
    UserId userId = session.getUserInSession();
    session.clearUserInSession();
    if (!userId.isGuest()) {
        userActivityManager.setLastLogout(userId, System.currentTimeMillis());
    }
    return new LogOutUserResult(userInSessionFactory.getUserInSession(UserId.getGuest()));
}
Also used : UserId(edu.stanford.bmir.protege.web.shared.user.UserId) WebProtegeSession(edu.stanford.bmir.protege.web.server.session.WebProtegeSession) LogOutUserResult(edu.stanford.bmir.protege.web.shared.user.LogOutUserResult) Nonnull(javax.annotation.Nonnull)

Example 2 with WebProtegeSession

use of edu.stanford.bmir.protege.web.server.session.WebProtegeSession in project webprotege by protegeproject.

the class PerformLoginActionHandler method executeAuthenticatedAction.

@Override
protected PerformLoginResult executeAuthenticatedAction(PerformLoginAction action, ExecutionContext executionContext) {
    WebProtegeSession session = executionContext.getSession();
    UserId userId = action.getUserId();
    session.setUserInSession(userId);
    activityManager.setLastLogin(userId, System.currentTimeMillis());
    logger.info("{} logged in", userId);
    return new PerformLoginResult(AuthenticationResponse.SUCCESS, userInSessionFactory.getUserInSession(userId));
}
Also used : PerformLoginResult(edu.stanford.bmir.protege.web.shared.auth.PerformLoginResult) UserId(edu.stanford.bmir.protege.web.shared.user.UserId) WebProtegeSession(edu.stanford.bmir.protege.web.server.session.WebProtegeSession)

Example 3 with WebProtegeSession

use of edu.stanford.bmir.protege.web.server.session.WebProtegeSession in project webprotege by protegeproject.

the class WebProtegeSessionListener method sessionDestroyed.

@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    WebProtegeSession session = new WebProtegeSessionImpl(httpSessionEvent.getSession());
    UserId userId = session.getUserInSession();
    if (!userId.isGuest()) {
        logger.info("{} Session expired", userId);
        userActivityManager.setLastLogout(userId, System.currentTimeMillis());
    }
}
Also used : UserId(edu.stanford.bmir.protege.web.shared.user.UserId) WebProtegeSession(edu.stanford.bmir.protege.web.server.session.WebProtegeSession) WebProtegeSessionImpl(edu.stanford.bmir.protege.web.server.session.WebProtegeSessionImpl)

Example 4 with WebProtegeSession

use of edu.stanford.bmir.protege.web.server.session.WebProtegeSession in project webprotege by protegeproject.

the class ProjectDownloadServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final WebProtegeSession webProtegeSession = new WebProtegeSessionImpl(req.getSession());
    UserId userId = webProtegeSession.getUserInSession();
    FileDownloadParameters downloadParameters = new FileDownloadParameters(req);
    if (!downloadParameters.isProjectDownload()) {
        logger.info("Bad project download request from {} at {}.  Request URI: {}  Query String: {}", webProtegeSession.getUserInSession(), formatAddr(req), req.getRequestURI(), req.getQueryString());
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    logger.info("Received download request from {} at {} for project {}", userId, formatAddr(req), downloadParameters.getProjectId());
    if (!accessManager.hasPermission(Subject.forUser(userId), new ProjectResource(downloadParameters.getProjectId()), BuiltInAction.DOWNLOAD_PROJECT)) {
        logger.info("Denied download request as user does not have permission to download this project.");
        resp.sendError(HttpServletResponse.SC_FORBIDDEN);
    } else if (downloadParameters.isProjectDownload()) {
        startProjectDownload(resp, userId, downloadParameters);
    }
}
Also used : UserId(edu.stanford.bmir.protege.web.shared.user.UserId) WebProtegeSession(edu.stanford.bmir.protege.web.server.session.WebProtegeSession) ProjectResource(edu.stanford.bmir.protege.web.server.access.ProjectResource) WebProtegeSessionImpl(edu.stanford.bmir.protege.web.server.session.WebProtegeSessionImpl)

Example 5 with WebProtegeSession

use of edu.stanford.bmir.protege.web.server.session.WebProtegeSession in project webprotege by protegeproject.

the class FileUploadServlet method doPost.

@Override
@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    WebProtegeSession webProtegeSession = new WebProtegeSessionImpl(req.getSession());
    UserId userId = webProtegeSession.getUserInSession();
    if (!accessManager.hasPermission(Subject.forUser(userId), ApplicationResource.get(), BuiltInAction.UPLOAD_PROJECT)) {
        sendErrorMessage(resp, "You do not have permission to upload files to " + applicationNameSupplier.get());
    }
    logger.info("Received upload request from {} at {}", webProtegeSession.getUserInSession(), formatAddr(req));
    resp.setHeader("Content-Type", RESPONSE_MIME_TYPE);
    try {
        if (ServletFileUpload.isMultipartContent(req)) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setFileSizeMax(maxUploadSizeSupplier.get());
            List<FileItem> items = upload.parseRequest(req);
            for (FileItem item : items) {
                if (!item.isFormField()) {
                    File uploadedFile = createServerSideFile();
                    item.write(uploadedFile);
                    long sizeInBytes = uploadedFile.length();
                    long computedFileSizeInBytes = computeFileSize(uploadedFile);
                    logger.info("File size is {} bytes.  Computed file size is {} bytes.", sizeInBytes, computedFileSizeInBytes);
                    if (computedFileSizeInBytes > maxUploadSizeSupplier.get()) {
                        sendFileSizeTooLargeResponse(resp);
                    } else {
                        logger.info("Stored uploaded file with name {}", uploadedFile.getName());
                        resp.setStatus(HttpServletResponse.SC_CREATED);
                        sendSuccessMessage(resp, uploadedFile.getName());
                    }
                    return;
                }
            }
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not find form file item");
        } else {
            logger.info("Bad upload request: POST must be multipart encoding.");
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "POST must be multipart encoding.");
        }
    } catch (FileUploadBase.FileSizeLimitExceededException | FileUploadBase.SizeLimitExceededException e) {
        sendFileSizeTooLargeResponse(resp);
    } catch (FileUploadBase.FileUploadIOException | FileUploadBase.IOFileUploadException e) {
        logger.info("File upload failed because an IOException occurred: {}", e.getMessage(), e);
        sendErrorMessage(resp, "File upload failed because of an IOException");
    } catch (FileUploadBase.InvalidContentTypeException e) {
        logger.info("File upload failed because the content type was invalid: {}", e.getMessage());
        sendErrorMessage(resp, "File upload failed because the content type is invalid");
    } catch (FileUploadException e) {
        logger.info("File upload failed: {}", e.getMessage());
        sendErrorMessage(resp, "File upload failed");
    } catch (Exception e) {
        logger.info("File upload failed because of an error when trying to write the file item: {}", e.getMessage(), e);
        sendErrorMessage(resp, "File upload failed");
    }
}
Also used : DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FileUploadException(org.apache.commons.fileupload.FileUploadException) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileUploadBase(org.apache.commons.fileupload.FileUploadBase) UserId(edu.stanford.bmir.protege.web.shared.user.UserId) WebProtegeSession(edu.stanford.bmir.protege.web.server.session.WebProtegeSession) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException) WebProtegeSessionImpl(edu.stanford.bmir.protege.web.server.session.WebProtegeSessionImpl)

Aggregations

WebProtegeSession (edu.stanford.bmir.protege.web.server.session.WebProtegeSession)5 UserId (edu.stanford.bmir.protege.web.shared.user.UserId)5 WebProtegeSessionImpl (edu.stanford.bmir.protege.web.server.session.WebProtegeSessionImpl)3 ProjectResource (edu.stanford.bmir.protege.web.server.access.ProjectResource)1 PerformLoginResult (edu.stanford.bmir.protege.web.shared.auth.PerformLoginResult)1 LogOutUserResult (edu.stanford.bmir.protege.web.shared.user.LogOutUserResult)1 File (java.io.File)1 IOException (java.io.IOException)1 Nonnull (javax.annotation.Nonnull)1 ServletException (javax.servlet.ServletException)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)1 FileUploadBase (org.apache.commons.fileupload.FileUploadBase)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1