Package org.damour.base.client.objects

Examples of org.damour.base.client.objects.File


    Transaction tx = session.beginTransaction();

    try {
      for (final FileItem item : fileItems) {

        File fileObject = null;
        if (item.getContentType().contains("image")) {
          fileObject = new Photo();
        } else {
          fileObject = new File();
        }
        fileObject.setOwner(owner);

        String parentFolderId = request.getParameter("parentFolder");
        Folder parentFolder = null;
        if (parentFolderId != null && !"".equals(parentFolderId)) {
          parentFolder = (Folder) session.load(Folder.class, new Long(parentFolderId));
          if (!SecurityHelper.doesUserHavePermission(session, owner, parentFolder, PERM.WRITE)) {
            Cookie cookie = new Cookie(item.getFieldName(), "");
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            response.flushBuffer();
            Logger.log("Unauthorized upload requested: " + request.getRemoteAddr());
            return;
          }
        }
        fileObject.setParent(parentFolder);

        fileObject.setContentType(item.getContentType());
        String name = item.getName();
        if (name.lastIndexOf("/") >= 0) {
          name = name.substring(name.lastIndexOf("/") + 1);
        }
        if (name.lastIndexOf("\\") >= 0) {
          name = name.substring(name.lastIndexOf("\\") + 1);
        }

        fileObject.setName(name);
        fileObject.setDescription(name);
        session.save(fileObject);

        status.setStatus(FileUploadStatus.WRITING_FILE);
        java.io.File outputPath = new java.io.File(java.io.File.separatorChar + "tmp" + java.io.File.separatorChar + BaseSystem.getDomainName(request));
        java.io.File outputFile = new java.io.File(outputPath, fileObject.getId() + "_" + fileObject.getName());
        outputPath.mkdirs();
        item.write(outputFile);
        tx = session.beginTransaction();
        fileObject.setNameOnDisk(fileObject.getId() + "_" + fileObject.getName());
        fileObject.setSize(outputFile.length());
        session.save(fileObject);
        tx.commit();
        Logger.log("Wrote to file: " + outputFile.getCanonicalPath());

        status.setStatus(FileUploadStatus.WRITING_DATABASE);
        saveFileFromStream(fileObject, new FileInputStream(outputFile));

        try {

          // if we are uploading a photo, create slideshow (sane size) image and thumbnail
          if (fileObject instanceof Photo) {
            // convert file to png for faster inclusion in thumbnails
            BufferedImage bi = javax.imageio.ImageIO.read(outputFile);
            ByteArrayOutputStream pngOut = new ByteArrayOutputStream();
            ImageIO.write(bi, "png", pngOut);

            tx = session.beginTransaction();
            Photo photo = (Photo) fileObject;
            photo.setWidth(bi.getWidth());
            photo.setHeight(bi.getHeight());

            PhotoThumbnail thumbFile = new PhotoThumbnail();
            thumbFile.setHidden(true);
            thumbFile.setOwner(owner);
            thumbFile.setParent(parentFolder);
            thumbFile.setName(createFileName("", name, "_thumb"));
            thumbFile.setDescription("Thumbnail for " + name);
            session.save(thumbFile);

            PhotoThumbnail previewFile = new PhotoThumbnail();
            previewFile.setHidden(true);
            previewFile.setOwner(owner);
            previewFile.setParent(parentFolder);
            previewFile.setName(createFileName("", name, "_preview"));
            previewFile.setDescription("Preview image for " + name);
            session.save(previewFile);
           
            PhotoThumbnail slideFile = new PhotoThumbnail();
            slideFile.setHidden(true);
            slideFile.setOwner(owner);
            slideFile.setParent(parentFolder);
            slideFile.setName(createFileName("", name, "_slideshow"));
            slideFile.setDescription("Slideshow image for " + name);
            session.save(slideFile);

           
            photo.setThumbnailImage(thumbFile);
            photo.setSlideshowImage(slideFile);
            photo.setPreviewImage(previewFile);

           
            ByteArrayInputStream pngIn = new ByteArrayInputStream(pngOut.toByteArray());

            ByteArrayOutputStream thumbOutStream = new ByteArrayOutputStream();
            BufferedImage thumbbi = ImageIO.read(pngIn);
            BufferedImage convertedThumbImage = createThumbnail(thumbbi, 128, 128, true);
            thumbFile.setWidth(convertedThumbImage.getWidth());
            thumbFile.setHeight(convertedThumbImage.getHeight());

            ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
            // instantiate an ImageWriteParam object with default compression options
            ImageWriteParam iwp = writer.getDefaultWriteParam();

            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            iwp.setCompressionQuality(0.75f);
            writer.setOutput(new MemoryCacheImageOutputStream(thumbOutStream));
            IIOImage thumbimage = new IIOImage(convertedThumbImage, null, null);
            writer.write(null, thumbimage, iwp);

            pngIn.reset();
            ByteArrayOutputStream slideOutStream = new ByteArrayOutputStream();
            BufferedImage slidebi = ImageIO.read(pngIn);
            BufferedImage convertedSlideImage = createThumbnail(slidebi, 640, 400, true);
            slideFile.setWidth(convertedSlideImage.getWidth());
            slideFile.setHeight(convertedSlideImage.getHeight());
            writer.setOutput(new MemoryCacheImageOutputStream(slideOutStream));
            IIOImage slideimage = new IIOImage(convertedSlideImage, null, null);
            writer.write(null, slideimage, iwp);

            pngIn.reset();
            ByteArrayOutputStream previewOutStream = new ByteArrayOutputStream();
            BufferedImage previewbi = ImageIO.read(pngIn);
            BufferedImage convertedPreviewImage = createThumbnail(previewbi, 320, 200, true);
            previewFile.setWidth(convertedPreviewImage.getWidth());
            previewFile.setHeight(convertedPreviewImage.getHeight());
            writer.setOutput(new MemoryCacheImageOutputStream(previewOutStream));
            IIOImage previewimage = new IIOImage(convertedPreviewImage, null, null);
            writer.write(null, previewimage, iwp);           
           
            thumbFile.setContentType("image/jpeg");
            slideFile.setContentType("image/jpeg");
            previewFile.setContentType("image/jpeg");
            thumbFile.setSize(thumbOutStream.size());
            slideFile.setSize(slideOutStream.size());
            previewFile.setSize(previewOutStream.size());
            session.save(thumbFile);
            session.save(slideFile);
            session.save(previewFile);
            tx.commit();

            tx = session.beginTransaction();
            thumbFile.setNameOnDisk(createFileName(thumbFile.getId().toString() + "_", name, "_thumb"));
            slideFile.setNameOnDisk(createFileName(slideFile.getId().toString() + "_", name, "_slide"));
            previewFile.setNameOnDisk(createFileName(previewFile.getId().toString() + "_", name, "_preview"));
            tx.commit();

            saveFileFromStream(thumbFile, new ByteArrayInputStream(thumbOutStream.toByteArray()));
            saveFileFromStream(slideFile, new ByteArrayInputStream(slideOutStream.toByteArray()));
            saveFileFromStream(previewFile, new ByteArrayInputStream(previewOutStream.toByteArray()));
          }

          status.setStatus(FileUploadStatus.FINISHED);
          Logger.log("Wrote to database: " + fileObject.getName());
        } catch (Throwable t) {
          Logger.log(t);
        }
        Cookie cookie = new Cookie(item.getFieldName(), fileObject.getId().toString());
        cookie.setPath("/");
        cookie.setMaxAge(BaseService.COOKIE_TIMEOUT);
        response.addCookie(cookie);
        response.setStatus(HttpServletResponse.SC_OK);
        response.getOutputStream().close();
View Full Code Here


      GroupMembership membership = new GroupMembership();
      membership.setUser(admin);
      membership.setUserGroup(group);
      session.save(membership);

      File f = new File();
      f.setName("Test File");
      f.setOwner(admin);
      session.save(f);
    }
    User anonymous = UserHelper.getUser(session, "anonymous");
    if (anonymous == null) {
      anonymous = new User();
View Full Code Here

  protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    Session session = HibernateUtil.getInstance().getSession();
    response.setBufferSize(65536);
    ServletOutputStream outStream = response.getOutputStream();
    File file = null;
    FileData fileData = null;
    try {
      // we should be able to pull items by path and by id
      // path is more tricky because of the HQL
      // (load each folder down the path until we hit the file)
      String fileParameter = request.getParameter("file");
      String disposition = request.getParameter("disposition");

      if (fileParameter == null || fileParameter.equals("")) {
        String pi = request.getPathInfo();
        int lastSlashIndex = pi.lastIndexOf("/") + 1;
        fileParameter = pi.substring(lastSlashIndex, pi.indexOf("_", pi.lastIndexOf("/")));
      }

      if (fileParameter == null || fileParameter.equals("")) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        response.flushBuffer();
        Logger.log("file parameter not specified");
        return;
      }

      if (disposition == null || disposition.equals("")) {
        String pi = request.getPathInfo();
        String filename = pi.substring(pi.lastIndexOf("/") + 1);
        int underscoreIndex = filename.indexOf("_") + 1;
        disposition = filename.substring(underscoreIndex, filename.indexOf("_", underscoreIndex));
      }

      file = (File) session.load(File.class, new Long(fileParameter));
      Logger.log("Content requested=" + file.getName() + ":" + fileParameter + " Referral: " + request.getParameter("referer"));

      long ifModifiedSince = request.getDateHeader("If-Modified-Since");
      long fileDate = file.getLastModifiedDate() - (file.getLastModifiedDate() % 1000);

      if (fileDate <= ifModifiedSince) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        if ("attachment".equals(disposition)) {
          response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
        } else {
          response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
        }
        response.setContentType(file.getContentType());
        response.setHeader("Content-Description", file.getName());
        response.setDateHeader("Last-Modified", file.getLastModifiedDate());
        // one year into the future
        response.setDateHeader("Expires", System.currentTimeMillis() + 31536000000L);
        response.setContentLength((int) file.getSize());
        response.flushBuffer();
        Logger.log("Conditional GET: " + file.getName());
        return;
      }

      User authUser = baseService.getAuthenticatedUser(session, request, response);
      if (!SecurityHelper.doesUserHavePermission(session, authUser, file, Permission.PERM.READ)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN, "Forbidden");
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        response.flushBuffer();
        Logger.log("Forbidden content requested: " + fileParameter);
        return;
      }

      String contentType = file.getContentType();
      response.setContentType(contentType);
      if ("attachment".equals(disposition)) {
        response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
      } else {
        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
      }
      String name = file.getName();
      response.setHeader("Content-Description", name);
      response.setDateHeader("Last-Modified", file.getLastModifiedDate());
      response.setDateHeader("Expires", System.currentTimeMillis() + 31536000000L);
      response.setContentLength((int) file.getSize());

      java.io.File possibleDataFile = new java.io.File(BaseSystem.getTempDir() + file.getNameOnDisk());
      if (possibleDataFile.exists()) {
        Logger.log("File exists in " + BaseSystem.getTempDir() + " pulling " + possibleDataFile.getName());
        FileInputStream fileInputStream = new FileInputStream(possibleDataFile);
        try {
          IOUtils.copy(fileInputStream, outStream);
        } finally {
          try {
            fileInputStream.close();
          } catch (Throwable t) {
          }
        }
      } else {
        List<FileData> fileDataList = HibernateUtil.getInstance().executeQuery(session,
            "from " + FileData.class.getSimpleName() + " where permissibleObject.id = " + file.getId());
        if (fileDataList.size() == 0) {
          response.sendError(HttpServletResponse.SC_NOT_FOUND);
          response.setStatus(HttpServletResponse.SC_NOT_FOUND);
          Logger.log("Requested content not found: " + fileParameter);
          response.flushBuffer();
View Full Code Here

    Transaction tx = session.beginTransaction();
    User user = new User();
    user.setUsername("mdamour1976");
    session.save(user);

    File file = new File();
    file.setName("comment file");
    file.setOwner(user);
    session.save(file);

    for (int i = 0; i < 100; i++) {
      Comment comment = new Comment();
      comment.setComment("comment " + i);
      comment.setOwner(file.getOwner());
      comment.setAuthor(user);
      comment.setParent(file);
      session.save(comment);
    }
    tx.commit();
View Full Code Here

            repositoryCallback.objectRenamed(permissibleObject);
          }
        };

        if (permissibleObject instanceof File) {
          File file = (File) permissibleObject;
          file.setName(nameTextBox.getText());
          BaseServiceCache.getService().renameFile(file, renameCallback);
        } else if (permissibleObject instanceof Folder) {
          Folder folder = (Folder) permissibleObject;
          folder.setName(nameTextBox.getText());
          BaseServiceCache.getService().renameFolder(folder, renameCallback);
View Full Code Here

    if (authUser == null) {
      throw new SimpleMessageException("User is not authenticated.");
    }
    Transaction tx = session.get().beginTransaction();
    try {
      File hibfile = (File) session.get().load(File.class, file.getId());
      if (!SecurityHelper.doesUserHavePermission(session.get(), authUser, hibfile, PERM.WRITE)) {
        throw new SimpleMessageException("User is not authorized to rename this file.");
      }
      hibfile.setName(file.getName());
      session.get().save(hibfile);
      tx.commit();
    } catch (Throwable t) {
      Logger.log(t);
      try {
View Full Code Here

    Image fileTypeIcon = new Image();
    fileTypeIcon.setResource(BaseImageBundle.images.file32());
    if (permissibleObject instanceof Folder) {
      fileTypeIcon.setResource(BaseImageBundle.images.folder32());
    } else if (permissibleObject instanceof File) {
      File file = (File) permissibleObject;
      if (StringUtils.isEmpty(file.getContentType())) {
        fileTypeIcon.setResource(BaseImageBundle.images.file32());
      } else if (file.getContentType().contains("image/x-png")) {
        fileTypeIcon.setResource(BaseImageBundle.images.png32());
      } else if (file.getContentType().contains("image/jpeg")) {
        fileTypeIcon.setResource(BaseImageBundle.images.jpg32());
      } else if (file.getContentType().contains("image/pjpeg")) {
        fileTypeIcon.setResource(BaseImageBundle.images.jpg32());
      } else if (file.getContentType().contains("image")) {
        fileTypeIcon.setResource(BaseImageBundle.images.png32());
      } else if (file.getContentType().contains("video")) {
        fileTypeIcon.setResource(BaseImageBundle.images.movie32());
      } else if (file.getContentType().contains("audio")) {
        fileTypeIcon.setResource(BaseImageBundle.images.audio32());
      } else if (file.getContentType().contains("text/plain")) {
        fileTypeIcon.setResource(BaseImageBundle.images.text32());
      } else if (file.getContentType().contains("text/html")) {
        fileTypeIcon.setResource(BaseImageBundle.images.html32());
      } else if (file.getContentType().contains("application/x-java-archive")) {
        fileTypeIcon.setResource(BaseImageBundle.images.jar32());
      } else if (file.getContentType().contains("application/x-zip-compressed")) {
        fileTypeIcon.setResource(BaseImageBundle.images.archive32());
      }
    }
    return fileTypeIcon;
  }
View Full Code Here

        folder.setGlobalRead(true);
        folder.setParent(parent);
        session.save(folder);

        for (int z = 0; z < 5; z++) {
          File file = new File();
          file.setName("globalRead:true " + z);
          file.setOwner(user);
          file.setGlobalRead(true);
          file.setParent(folder);
          session.save(file);
        }
      }
    }

    User user2 = new User();
    user2.setUsername("nobody");
    session.save(user2);

    for (int j = 0; j < 1; j++) {
      File file = new File();
      file.setName("HAS PERM " + j);
      file.setOwner(user);
      session.save(file);

      Permission perm = new Permission();
      perm.readPerm = true;
      perm.setSecurityPrincipal(user2);
      perm.setPermissibleObject(file);
      session.save(perm);
    }

    for (int j = 0; j < 1; j++) {
      PermissibleObject folder = new PermissibleObject();
      folder.setName("HAS PERM " + j);
      folder.setOwner(user);
      session.save(folder);

      Permission perm = new Permission();
      perm.readPerm = true;
      perm.setSecurityPrincipal(user2);
      perm.setPermissibleObject(folder);
      session.save(perm);
    }

    for (int j = 0; j < 1; j++) {
      PermissibleObject folder = new PermissibleObject();
      folder.setName("user owns, but does not have perm");
      folder.setOwner(user2);
      session.save(folder);

      Permission perm = new Permission();
      perm.setReadPerm(true);
      perm.setSecurityPrincipal(user);
      perm.setPermissibleObject(folder);
      session.save(perm);
    }   

    UserGroup group = new UserGroup();
    group.setName("blah");
    session.save(group);
    GroupMembership membership = new GroupMembership();
    membership.setUser(user2);
    membership.setUserGroup(group);
    session.save(membership);
   
    File groupPermFile = new File();
    groupPermFile.setName("not owner:  group perm file");
    groupPermFile.setOwner(user);
    session.save(groupPermFile);

    Permission perm = new Permission();
    perm.setReadPerm(true);
    perm.setSecurityPrincipal(group);
View Full Code Here

TOP

Related Classes of org.damour.base.client.objects.File

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.