Package ch.entwine.weblounge.common.repository

Examples of ch.entwine.weblounge.common.repository.WritableContentRepository


      throw new WebApplicationException(Status.SERVICE_UNAVAILABLE);

    // Make sure this is a writable repository
    if (!(repository instanceof WritableContentRepository))
      throw new WebApplicationException(Status.PRECONDITION_FAILED);
    final WritableContentRepository writableRepository = (WritableContentRepository) repository;

    // Is the repository already being indexed?
    if (repository.isIndexing())
      throw new WebApplicationException(Status.CONFLICT);

    // Start indexing
    new Thread(new Runnable() {
      public void run() {
        try {
          writableRepository.index();
        } catch (ContentRepositoryException e) {
          logger.error("Index operation failed: " + e.getMessage());
        }
      }
    }).start();
View Full Code Here


      } finally {
        IOUtils.closeQuietly(is);
      }

      URI uri = null;
      WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);
      try {
        is = new FileInputStream(uploadedFile);
        resource = contentRepository.putContent(resource.getURI(), content, is);
        uri = new URI(resource.getURI().getIdentifier());
      } catch (IOException e) {
        logger.warn("Error writing content to resource {}: {}", resource.getURI(), e.getMessage());
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      } catch (IllegalStateException e) {
View Full Code Here

    // Make sure the user has editing rights
    if (!SecurityUtils.userHasRole(user, SystemRole.EDITOR))
      throw new WebApplicationException(Status.UNAUTHORIZED);

    WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);

    // Delete the resource
    try {
      resource = contentRepository.deleteContent(uri, content);
      resource.setModified(user, new Date());
      // TODO: Remove existing preview images
      contentRepository.put(resource);
    } catch (IllegalStateException e) {
      logger.warn("Tried to remove content from missing resource " + uri);
      throw new WebApplicationException(Status.NOT_FOUND);
    } catch (ContentRepositoryException e) {
      logger.warn("Error while accessing resource " + uri);
View Full Code Here

    if (site.getContentRepository().isReadOnly()) {
      logger.warn("Attempt to write to read-only content repository {}", site);
      throw new WebApplicationException(Status.PRECONDITION_FAILED);
    }

    WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);
    ResourceURI resourceURI = null;

    // Does the resource exist?
    try {
      resourceURI = contentRepository.getResourceURI(resourceId);
      if (resourceURI == null) {
        throw new WebApplicationException(Status.NOT_FOUND);
      }
    } catch (ContentRepositoryException e) {
      logger.warn("Error lookup up resource {} from repository: {}", resourceURI, e.getMessage());
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    }

    Resource<?> currentResource;
    try {
      currentResource = contentRepository.get(resourceURI);
    } catch (ContentRepositoryException e) {
      logger.warn("Error reading current resource {} from repository: {}", resourceURI, e.getMessage());
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    }

    // Check the value of the If-Match header against the etag
    if (ifMatchHeader != null) {
      String etag = Long.toString(ResourceUtils.getModificationDate(currentResource).getTime());
      if (!etag.equals(ifMatchHeader)) {
        throw new WebApplicationException(Status.PRECONDITION_FAILED);
      }
    }

    // Get the current user
    User user = securityService.getUser();
    if (user == null)
      throw new WebApplicationException(Status.UNAUTHORIZED);

    // Make sure the user has editing rights
    if (!SecurityUtils.userHasRole(user, SystemRole.EDITOR))
      throw new WebApplicationException(Status.UNAUTHORIZED);

    // Parse the resource and update it in the repository
    Resource<?> resource = null;
    // TOOD: Extract resource type
    String resourceType = resourceURI.getType();
    try {
      ResourceSerializer<?, ?> serializer = serializerService.getSerializerByType(resourceType);
      ResourceReader<?, ?> resourceReader = serializer.getReader();
      resource = resourceReader.read(IOUtils.toInputStream(resourceXml, "utf-8"), site);
      resource.setModified(user, new Date());
      contentRepository.put(resource, false);

      // Check if the resource has been moved
      String currentPath = currentResource.getURI().getPath();
      String newPath = StringUtils.trimToNull(resource.getURI().getPath());
      if (currentPath != null && newPath != null && !currentPath.equals(newPath)) {
        contentRepository.move(currentResource.getURI(), newPath, true);
      }
    } catch (IOException e) {
      logger.warn("Error reading udpated resource {} from request", resourceURI);
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    } catch (ParserConfigurationException e) {
View Full Code Here

  @Path("/")
  public Response createFile(@Context HttpServletRequest request,
      @FormParam("path") String path) {

    Site site = getSite(request);
    WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);

    // Get the current user
    User user = securityService.getUser();
    if (user == null)
      throw new WebApplicationException(Status.UNAUTHORIZED);

    // Make sure the user has editing rights
    if (!SecurityUtils.userHasRole(user, SystemRole.EDITOR))
      throw new WebApplicationException(Status.UNAUTHORIZED);

    // Create the resource uri
    ResourceURIImpl resourceURI = null;
    String uuid = UUID.randomUUID().toString();
    if (!StringUtils.isBlank(path)) {
      try {
        if (!path.startsWith("/"))
          path = "/" + path;
        WebUrl url = new WebUrlImpl(site, path);
        resourceURI = new GeneralResourceURIImpl(site, url.getPath(), uuid);

        // Make sure the resource doesn't exist
        if (contentRepository.exists(new GeneralResourceURIImpl(site, url.getPath()))) {
          logger.warn("Tried to create already existing resource {} in site '{}'", resourceURI, site);
          throw new WebApplicationException(Status.CONFLICT);
        }
      } catch (IllegalArgumentException e) {
        logger.warn("Tried to create a resource with an invalid path '{}': {}", path, e.getMessage());
        throw new WebApplicationException(Status.BAD_REQUEST);
      } catch (ContentRepositoryException e) {
        logger.warn("Resource lookup {} failed for site '{}'", resourceURI, site);
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      }
    } else {
      resourceURI = new GeneralResourceURIImpl(site, "/" + uuid.replaceAll("-", ""), uuid);
    }

    URI uri = null;
    Resource<?> resource = null;
    try {
      // Parse the resource and store it
      logger.debug("Creating new resource at {}", resourceURI);
      resource = new FileResourceImpl(resourceURI);
      resource.setCreated(user, new Date());
      contentRepository.put(resource, true);
      uri = new URI(UrlUtils.concat(request.getRequestURL().toString(), resourceURI.getIdentifier()));
    } catch (URISyntaxException e) {
      logger.warn("Error creating a uri for resource {}: {}", resourceURI, e.getMessage());
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    } catch (IOException e) {
View Full Code Here

    // Check the parameters
    if (resourceId == null)
      return Response.status(Status.BAD_REQUEST).build();

    Site site = getSite(request);
    WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);

    // Get the current user
    User user = securityService.getUser();
    if (user == null)
      throw new WebApplicationException(Status.UNAUTHORIZED);

    // Make sure the user has editing rights
    if (!SecurityUtils.userHasRole(user, SystemRole.EDITOR))
      throw new WebApplicationException(Status.UNAUTHORIZED);

    ResourceURI resourceURI = null;

    // Make sure the resource exists
    try {
      resourceURI = contentRepository.getResourceURI(resourceId);
      if (resourceURI == null) {
        logger.warn("Tried to delete non existing resource {} in site '{}'", resourceURI, site);
        throw new WebApplicationException(Status.NOT_FOUND);
      }
    } catch (ContentRepositoryException e) {
      logger.warn("File lookup {} failed for site '{}'", resourceURI, site);
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    }

    // Delete the resource
    try {
      // TODO: Versions?
      resourceURI = contentRepository.get(resourceURI).getURI();
      contentRepository.delete(resourceURI);
    } catch (SecurityException e) {
      logger.warn("Tried to delete file {} of site '{}' without permission", resourceURI, site);
      throw new WebApplicationException(Status.FORBIDDEN);
    } catch (ReferentialIntegrityException e) {
      logger.warn("Tried to delete referenced file {} of site '{}'", resourceURI, site);
View Full Code Here

      // Make sure the user has editing rights
      if (!SecurityUtils.userHasRole(user, SystemRole.EDITOR))
        throw new WebApplicationException(Status.UNAUTHORIZED);

      WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);

      // Create the resource uri
      URI uri = null;
      InputStream is = null;
      Resource<?> resource = null;
      ResourceURI resourceURI = null;
      logger.debug("Adding resource to {}", resourceURI);
      ResourceSerializer<?, ?> serializer = serializerService.getSerializerByMimeType(mimeType);
      if (serializer == null) {
        logger.debug("No specialized resource serializer found, using regular file serializer");
        serializer = serializerService.getSerializerByType(FileResource.TYPE);
      }

      // Create the resource
      try {
        is = new FileInputStream(uploadedFile);
        resource = serializer.newResource(site, is, user, language);
        resourceURI = resource.getURI();
      } catch (FileNotFoundException e) {
        logger.warn("Error creating resource at {} from image: {}", uri, e.getMessage());
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      } finally {
        IOUtils.closeQuietly(is);
      }

      // If a path has been specified, set it
      if (path != null && StringUtils.isNotBlank(path)) {
        try {
          if (!path.startsWith("/"))
            path = "/" + path;
          WebUrl url = new WebUrlImpl(site, path);
          resourceURI.setPath(url.getPath());

          // Make sure the resource doesn't exist
          if (contentRepository.exists(new GeneralResourceURIImpl(site, url.getPath()))) {
            logger.warn("Tried to create already existing resource {} in site '{}'", resourceURI, site);
            throw new WebApplicationException(Status.CONFLICT);
          }
        } catch (IllegalArgumentException e) {
          logger.warn("Tried to create a resource with an invalid path '{}': {}", path, e.getMessage());
          throw new WebApplicationException(Status.BAD_REQUEST);
        } catch (ContentRepositoryException e) {
          logger.warn("Resource lookup {} failed for site '{}'", resourceURI, site);
          throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
        }
      }

      // Store the new resource
      try {
        uri = new URI(resourceURI.getIdentifier());
        contentRepository.put(resource, true);
      } catch (URISyntaxException e) {
        logger.warn("Error creating a uri for resource {}: {}", resourceURI, e.getMessage());
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      } catch (IOException e) {
        logger.warn("Error writing new resource {}: {}", resourceURI, e.getMessage());
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      } catch (IllegalStateException e) {
        logger.warn("Illegal state while adding new resource {}: {}", resourceURI, e.getMessage());
        throw new WebApplicationException(Status.PRECONDITION_FAILED);
      } catch (ContentRepositoryException e) {
        logger.warn("Error adding new resource {}: {}", resourceURI, e.getMessage());
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      }

      ResourceContent content = null;
      ResourceContentReader<?> reader = null;
      try {
        reader = serializer.getContentReader();
        is = new FileInputStream(uploadedFile);
        content = reader.createFromContent(is, user, language, uploadedFile.length(), fileName, mimeType);
      } catch (IOException e) {
        logger.warn("Error reading resource content {} from request", uri);
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      } catch (ParserConfigurationException e) {
        logger.warn("Error configuring parser to read resource content {}: {}", uri, e.getMessage());
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      } catch (SAXException e) {
        logger.warn("Error parsing udpated resource {}: {}", uri, e.getMessage());
        throw new WebApplicationException(Status.BAD_REQUEST);
      } catch (Throwable t) {
        logger.warn("Unknown error while trying to read resource content {}: {}", uri, t.getMessage());
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      } finally {
        IOUtils.closeQuietly(is);
        if (content == null) {
          try {
            contentRepository.delete(resourceURI);
          } catch (Throwable t) {
            logger.error("Error deleting orphan resource {}", resourceURI, t);
          }
        }
      }

      try {
        is = new FileInputStream(uploadedFile);
        resource = contentRepository.putContent(resource.getURI(), content, is);
      } catch (IOException e) {
        logger.warn("Error writing content to resource {}: {}", uri, e.getMessage());
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      } catch (IllegalStateException e) {
        logger.warn("Illegal state while adding content to resource {}: {}", uri, e.getMessage());
View Full Code Here

TOP

Related Classes of ch.entwine.weblounge.common.repository.WritableContentRepository

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.