Search in sources :

Example 1 with WebProtegeSessionImpl

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

the class WebProtegeRemoteServiceServlet method getUserInSession.

/**
 * Gets the userId for the client associated with the current thread local request.
 *
 * @return The UserId.  Not null (if no user is logged in then the value specified by {@link edu.stanford.bmir.protege.web.shared.user.UserId#getGuest()} ()}
 *         will be returned.
 */
public UserId getUserInSession() {
    HttpServletRequest request = getThreadLocalRequest();
    final HttpSession session = request.getSession();
    return new WebProtegeSessionImpl(session).getUserInSession();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) WebProtegeSessionImpl(edu.stanford.bmir.protege.web.server.session.WebProtegeSessionImpl)

Example 2 with WebProtegeSessionImpl

use of edu.stanford.bmir.protege.web.server.session.WebProtegeSessionImpl 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 3 with WebProtegeSessionImpl

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

the class DispatchServlet method executeAction.

@Override
public DispatchServiceResultContainer executeAction(Action action) throws ActionExecutionException, PermissionDeniedException {
    UserId userId = getUserInSession();
    HttpServletRequest request = getThreadLocalRequest();
    HttpSession session = request.getSession();
    final RequestContext requestContext = new RequestContext(userId);
    final ExecutionContext executionContext = new ExecutionContext(new WebProtegeSessionImpl(session));
    return executor.execute(action, requestContext, executionContext);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UserId(edu.stanford.bmir.protege.web.shared.user.UserId) HttpSession(javax.servlet.http.HttpSession) WebProtegeSessionImpl(edu.stanford.bmir.protege.web.server.session.WebProtegeSessionImpl)

Example 4 with WebProtegeSessionImpl

use of edu.stanford.bmir.protege.web.server.session.WebProtegeSessionImpl 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 WebProtegeSessionImpl

use of edu.stanford.bmir.protege.web.server.session.WebProtegeSessionImpl 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

WebProtegeSessionImpl (edu.stanford.bmir.protege.web.server.session.WebProtegeSessionImpl)5 UserId (edu.stanford.bmir.protege.web.shared.user.UserId)4 WebProtegeSession (edu.stanford.bmir.protege.web.server.session.WebProtegeSession)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpSession (javax.servlet.http.HttpSession)2 ProjectResource (edu.stanford.bmir.protege.web.server.access.ProjectResource)1 File (java.io.File)1 IOException (java.io.IOException)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