Package ch.entwine.weblounge.common.repository

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


      throws JobException {

    Site site = (Site) ctx.get(Site.class.getName());

    // Get hold of the content repository
    WritableContentRepository contentRepository = null;
    if (site.getContentRepository().isReadOnly())
      throw new JobException(this, "Content repository of site '" + site + "' is read only");
    contentRepository = (WritableContentRepository) site.getContentRepository();

    // Read the configuration value for the repository url
    String repositoryUrl = (String) ctx.get(OPT_REPOSITORY_URL);
    if (StringUtils.isBlank(repositoryUrl))
      throw new JobException(this, "Configuration option '" + OPT_REPOSITORY_URL + "' is missing from the job configuration");

    // Make sure the url is well formed
    URL url = null;
    try {
      url = new URL(repositoryUrl);
    } catch (MalformedURLException e) {
      throw new JobException(this, "Repository url '" + repositoryUrl + "' is malformed: " + e.getMessage());
    }

    // Read the configuration value for the flavors
    String presentationTrackFlavor = (String) ctx.get(OPT_PRSENTATION_TRACK_FLAVORS);
    if (StringUtils.isBlank(presentationTrackFlavor))
      throw new JobException(this, "Configuration option '" + OPT_PRSENTATION_TRACK_FLAVORS + "' is missing from the job configuration");

    String presenterTrackFlavor = (String) ctx.get(OPT_PRESENTER_TRACK_FLAVORS);
    if (StringUtils.isBlank(presenterTrackFlavor))
      throw new JobException(this, "Configuration option '" + OPT_PRESENTER_TRACK_FLAVORS + "' is missing from the job configuration");

    String dcEpisodeFlavor = (String) ctx.get(OPT_EPISODE_DC_FLAVORS);
    if (StringUtils.isBlank(dcEpisodeFlavor))
      throw new JobException(this, "Configuration option '" + OPT_EPISODE_DC_FLAVORS + "' is missing from the job configuration");

    String dcSeriesFlavor = (String) ctx.get(OPT_SERIES_DC_FLAVORS);
    if (StringUtils.isBlank(dcSeriesFlavor))
      throw new JobException(this, "Configuration option '" + OPT_SERIES_DC_FLAVORS + "' is missing from the job configuration");
   
    String mimesTypes = (String) ctx.get(OPT_MIMETYPES);
    if (StringUtils.isBlank(mimesTypes))
      throw new JobException(this, "Configuration option '" + OPT_MIMETYPES + "' is missing from the job configuration");
   
   
    // Read the configuration value for the handler class
    String handlerClass = (String) ctx.get(OPT_HANDLER_CLASS);
    if (StringUtils.isBlank(handlerClass))
      throw new JobException(this, "Configuration option '" + OPT_HANDLER_CLASS + "' is missing from the job configuration");

    UserImpl harvesterUser = new UserImpl(name, site.getIdentifier(), "Harvester");

    RecordHandler handler;
    try {
      Class<? extends AbstractWebloungeRecordHandler> c = (Class<? extends AbstractWebloungeRecordHandler>) Thread.currentThread().getContextClassLoader().loadClass(handlerClass);
      Class<?> paramTypes[] = new Class[8];
      paramTypes[0] = Site.class;
      paramTypes[1] = WritableContentRepository.class;
      paramTypes[2] = User.class;
      paramTypes[3] = String.class;
      paramTypes[4] = String.class;
      paramTypes[5] = String.class;
      paramTypes[6] = String.class;
      paramTypes[7] = String.class;
      Constructor<? extends AbstractWebloungeRecordHandler> constructor = c.getConstructor(paramTypes);
      Object arglist[] = new Object[8];
      arglist[0] = site;
      arglist[1] = contentRepository;
      arglist[2] = harvesterUser;
      arglist[3] = presentationTrackFlavor;
      arglist[4] = presenterTrackFlavor;
      arglist[5] = dcEpisodeFlavor;
      arglist[6] = dcSeriesFlavor;
      arglist[7] = mimesTypes;
      handler = constructor.newInstance(arglist);
    } catch (Throwable t) {
      throw new IllegalStateException("Unable to instantiate class " + handlerClass + ": " + t.getMessage(), t);
    }

    SearchResult searchResult;
    SearchQuery q = new SearchQueryImpl(site);
    q.withTypes(MovieResource.TYPE);
    q.sortByPublishingDate(Order.Descending);
    q.withPublisher(harvesterUser);
    try {
      searchResult = contentRepository.find(q);
    } catch (ContentRepositoryException e) {
      logger.error("Error searching for resources with harvester publisher.");
      throw new RuntimeException(e);
    }
View Full Code Here


    if (site.getContentRepository().isReadOnly()) {
      logger.warn("Unable to publish e-mail messages to site '{}': repository is read only", site);
      return;
    }

    WritableContentRepository repository = (WritableContentRepository)site.getContentRepository();

    // Extract the configuration from the job properties
    String provider = (String) ctx.get(OPT_PROVIDER);
    Account account = null;
    try {
      if (StringUtils.isBlank(provider)) {
        provider = DEFAULT_PROVIDER;
      }
      account = new Account(ctx);
    } catch (IllegalArgumentException e) {
      throw new JobException(this, e);
    }

    // Connect to the server
    Properties sessionProperties = new Properties();
    Session session = Session.getDefaultInstance(sessionProperties, null);
    Store store = null;
    Folder inbox = null;

    try {

      // Connect to the server
      try {
        store = session.getStore(provider);
        store.connect(account.getHost(), account.getLogin(), account.getPassword());
      } catch (NoSuchProviderException e) {
        throw new JobException(this, "Unable to connect using unknown e-mail provider '" + provider + "'", e);
      } catch (MessagingException e) {
        throw new JobException(this, "Error connecting to " + provider + " account " + account, e);
      }

      // Open the account's inbox
      try {
        inbox = store.getFolder(INBOX);
        if (inbox == null)
          throw new JobException(this, "No inbox found at " + account);
        inbox.open(Folder.READ_WRITE);
      } catch (MessagingException e) {
        throw new JobException(this, "Error connecting to inbox at " + account, e);
      }

      // Get the messages from the server
      try {
        for (Message message : inbox.getMessages()) {
          if (!message.isSet(Flag.SEEN)) {
            try {
              Page page = aggregate(message, site);
              message.setFlag(Flag.DELETED, true);
              repository.put(page, true);
              logger.info("E-Mail message published at {}", page.getURI());
            } catch (Exception e) {
              logger.info("E-Mail message discarded: {}", e.getMessage());
              message.setFlag(Flag.SEEN, true);
              // TODO: Reply to sender if the "from" field exists
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 workURI = new PageURIImpl(site, null, pageId, Resource.WORK);

    // Does the page exist?
    Page currentPage = null;
    try {
      currentPage = (Page) contentRepository.get(workURI);
      if (currentPage == null) {
        logger.warn("Attempt to update a page without creating a work version first");
        throw new WebApplicationException(Status.PRECONDITION_FAILED);
      }
      workURI.setPath(currentPage.getURI().getPath());
    } catch (ContentRepositoryException e) {
      logger.warn("Error lookup up page {} from repository: {}", workURI, 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 = ResourceUtils.getETagValue(currentPage);
      if (!etag.equals(ifMatchHeader)) {
        throw new WebApplicationException(Status.PRECONDITION_FAILED);
      }
    }

    // Get the 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);

    boolean isAdmin = SecurityUtils.userHasRole(user, SystemRole.SITEADMIN);

    // If the page is locked by a different user, refuse
    if (currentPage.isLocked() && (!currentPage.getLockOwner().equals(user) && !isAdmin)) {
      return Response.status(Status.FORBIDDEN).build();
    }

    // Parse the page and update it in the repository
    Page page = null;
    try {
      PageReader pageReader = new PageReader();
      page = pageReader.read(IOUtils.toInputStream(pageXml, "utf-8"), site);
      if (StringUtils.isBlank(page.getURI().getPath()))
        throw new WebApplicationException(Status.PRECONDITION_FAILED);
      page.setModified(user, new Date());
      page.setVersion(Resource.WORK);

      // TODO: Preview generation disabled due to performance problems
      contentRepository.putAsynchronously(page, false);

      // Check if the page has been moved
      String currentPath = currentPage.getURI().getPath();
      String newPath = page.getURI().getPath();
      if ((currentPath != null && !currentPath.equals(newPath) || (currentPath == null && newPath != null))) {
        contentRepository.moveAsynchronously(currentPage.getURI(), newPath, true);
      }
    } catch (SecurityException e) {
      logger.warn("Tried to update page {} of site '{}' without permission", workURI, site);
      throw new WebApplicationException(Status.FORBIDDEN);
    } catch (IOException e) {
View Full Code Here

  public Response createPage(@Context HttpServletRequest request,
      @FormParam("content") String pageXml, @FormParam("path") String path,
      @FormParam("asynchronous") boolean asynchronous) {

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

    // Create the page uri
    ResourceURIImpl pageURI = null;
    String uuid = UUID.randomUUID().toString();
    if (!StringUtils.isBlank(path)) {
      try {
        if (!path.startsWith("/"))
          path = "/" + path;
        WebUrl url = new WebUrlImpl(site, path);
        path = url.getPath();
      } catch (IllegalArgumentException e) {
        logger.warn("Tried to create a page with an invalid path '{}': {}", path, e.getMessage());
        throw new WebApplicationException(Status.BAD_REQUEST);
      }
    } else {
      path = "/" + uuid.replaceAll("-", "");
    }

    pageURI = new PageURIImpl(site, path, uuid, Resource.WORK);

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

    // Get the 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 page and store it
    PageImpl page = null;
    URI uri = null;
    if (!StringUtils.isBlank(pageXml)) {
      logger.debug("Adding page to {}", pageURI);
      try {
        PageReader pageReader = new PageReader();
        page = pageReader.read(IOUtils.toInputStream(pageXml, "utf-8"), site);
        page.setIdentifier(pageURI.getIdentifier());
        page.setPath(pageURI.getPath());
        page.setVersion(pageURI.getVersion());
      } catch (IOException e) {
        logger.warn("Error reading page {} from request", pageURI);
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      } catch (ParserConfigurationException e) {
        logger.warn("Error configuring parser to read updated page {}: {}", pageURI, e.getMessage());
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
      } catch (SAXException e) {
        logger.warn("Error parsing updated page {}: {}", pageURI, e.getMessage());
        throw new WebApplicationException(Status.BAD_REQUEST);
      }
    } else {
      logger.debug("Creating new page at {}", pageURI);
      page = new PageImpl(pageURI);
      page.setTemplate(site.getDefaultTemplate().getIdentifier());
      page.setCreated(user, new Date());
    }

    // Store the new page
    try {
      contentRepository.put(page, true);
      uri = new URI(UrlUtils.concat(request.getRequestURL().toString(), pageURI.getIdentifier()));
    } catch (URISyntaxException e) {
      logger.warn("Error creating a uri for page {}: {}", pageURI, e.getMessage());
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    } catch (SecurityException e) {
View Full Code Here

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

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

    ResourceURI livePageURI = new PageURIImpl(site, null, pageId, Resource.LIVE);
    ResourceURI workPageURI = new PageURIImpl(site, null, pageId, Resource.WORK);

    try {
      if (!contentRepository.existsInAnyVersion(livePageURI)) {
        logger.warn("Tried to delete non existing page {} in site '{}'", livePageURI, site);
        throw new WebApplicationException(Status.NOT_FOUND);
      }
    } catch (ContentRepositoryException e) {
      logger.warn("Page lookup {} failed for site '{}'", livePageURI, site);
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    }

    Page page = null;
    try {
      page = (Page) contentRepository.get(livePageURI);
      if (page != null) {
        livePageURI.setPath(page.getURI().getPath());
      } else {
        page = (Page) contentRepository.get(workPageURI);
        workPageURI.setPath(page.getURI().getPath());
      }
    } catch (ContentRepositoryException e) {
      logger.warn("Error lookup up page {} from repository: {}", livePageURI, e.getMessage());
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    }

    // Get the 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);

    // If the page is published, the user needs publishing rights
    if (page.isPublished() && !SecurityUtils.userHasRole(user, SystemRole.PUBLISHER))
      throw new WebApplicationException(Status.UNAUTHORIZED);

    boolean isAdmin = SecurityUtils.userHasRole(user, SystemRole.SITEADMIN);

    // If the page is locked by a different user, refuse
    if (page.isLocked() && (!page.getLockOwner().equals(user) && !isAdmin)) {
      return Response.status(Status.FORBIDDEN).build();
    }

    // Delete the page
    try {
      contentRepository.delete(page.getURI(), true);
    } catch (SecurityException e) {
      logger.warn("Tried to delete page {} of site '{}' without permission", livePageURI, site);
      throw new WebApplicationException(Status.FORBIDDEN);
    } catch (ReferentialIntegrityException e) {
      logger.warn("Tried to delete referenced page {} of site '{}'", livePageURI, site);
View Full Code Here

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

    WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);
    ResourceURI workURI = new PageURIImpl(site, null, pageId, Resource.WORK);

    // Does the page exist at all?
    Page workPage = null;
    try {
      if (!contentRepository.existsInAnyVersion(workURI))
        throw new WebApplicationException(Status.NOT_FOUND);
    } catch (ContentRepositoryException e) {
      logger.warn("Error looking up page {} from repository: {}", workURI, e.getMessage());
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    }

    // Make sure we have a work page. If it doesn't exist yet, it needs
    // to be created as a result of the lock operation
    ResourceURI liveURI = new PageURIImpl(site, null, pageId, Resource.LIVE);
    try {
      workPage = (Page) contentRepository.get(workURI);
      if (workPage == null) {
        logger.debug("Creating work version of {}", liveURI);
        PageReader reader = new PageReader();
        Page livePage = (Page) contentRepository.get(liveURI);
        workPage = reader.read(IOUtils.toInputStream(livePage.toXml(), "utf-8"), site);
        workPage.setVersion(Resource.WORK);
        contentRepository.putAsynchronously(workPage, false);
      } else {
        workURI.setPath(workPage.getURI().getPath());
      }
    } catch (ContentRepositoryException e) {
      logger.warn("Error lookup up page {} from repository: {}", workURI, e.getMessage());
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    } catch (IllegalStateException e) {
      logger.warn("Error putting a page work copy {} to repository: {}", workURI, e.getMessage());
      throw new WebApplicationException(Status.PRECONDITION_FAILED);
    } catch (IOException e) {
      logger.warn("Error putting a page work copy {} to repository: {}", workURI, e.getMessage());
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    } catch (ParserConfigurationException e) {
      logger.warn("Error reading live page {} from repository: {}", liveURI, e.getMessage());
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    } catch (SAXException e) {
      logger.warn("Error reading live page {} from repository: {}", liveURI, 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 = ResourceUtils.getETagValue(workPage);
      if (!etag.equals(ifMatchHeader)) {
        throw new WebApplicationException(Status.PRECONDITION_FAILED);
      }
    }

    // Get the 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);

    boolean isAdmin = SecurityUtils.userHasRole(user, SystemRole.SITEADMIN);

    // If the page is locked by a different user, refuse
    if (workPage.isLocked() && (!workPage.getLockOwner().equals(user) && !isAdmin)) {
      return Response.status(Status.FORBIDDEN).build();
    }

    // Finally, perform the lock operation (on all resource versions)
    try {
      contentRepository.lockAsynchronously(workURI, user);
      logger.info("Page {} has been locked by {}", workURI, user);
    } catch (SecurityException e) {
      logger.warn("Tried to lock page {} of site '{}' without permission", workURI, site);
      throw new WebApplicationException(Status.FORBIDDEN);
    } catch (IOException e) {
View Full Code Here

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

    WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);
    ResourceURI pageURI = new PageURIImpl(site, null, pageId, Resource.WORK);

    // Does the page exist?
    Page page = null;
    try {
      ResourceURI[] versions = contentRepository.getVersions(pageURI);
      if (versions.length == 0)
        throw new WebApplicationException(Status.NOT_FOUND);
      page = (Page) contentRepository.get(versions[0]);
      if (page == null)
        throw new WebApplicationException(Status.NOT_FOUND);
      pageURI.setPath(page.getURI().getPath());
    } catch (ContentRepositoryException e) {
      logger.warn("Error lookup up page {} from repository: {}", pageURI, 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 = ResourceUtils.getETagValue(page);
      if (!etag.equals(ifMatchHeader)) {
        throw new WebApplicationException(Status.PRECONDITION_FAILED);
      }
    }

    // Get the 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);

    boolean isAdmin = SecurityUtils.userHasRole(user, SystemRole.SITEADMIN);

    // If the page is locked by a different user, refuse
    if (page.isLocked() && (!page.getLockOwner().equals(user) && !isAdmin)) {
      return Response.status(Status.FORBIDDEN).build();
    }

    // Finally, perform the lock operation (on all resource versions)
    try {
      contentRepository.unlockAsynchronously(pageURI, user);
      logger.info("Page {} has been unlocked by {}", pageURI, user);
    } catch (SecurityException e) {
      logger.warn("Tried to unlock page {} of site '{}' without permission", pageURI, site);
      throw new WebApplicationException(Status.FORBIDDEN);
    } catch (IOException e) {
View Full Code Here

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

    WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);
    ResourceURI workURI = new PageURIImpl(site, null, pageId, Resource.WORK);

    // Does the work page exist?
    Page workPage = null;
    try {
      if (!contentRepository.existsInAnyVersion(workURI))
        throw new WebApplicationException(Status.NOT_FOUND);
      workPage = (Page) contentRepository.get(workURI);
      if (workPage == null)
        throw new WebApplicationException(Status.PRECONDITION_FAILED);
      workURI.setPath(workPage.getURI().getPath());
    } catch (ContentRepositoryException e) {
      logger.warn("Error looking up page {} from repository: {}", workURI, 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 = ResourceUtils.getETagValue(workPage);
      if (!etag.equals(ifMatchHeader)) {
        throw new WebApplicationException(Status.PRECONDITION_FAILED);
      }
    }

    // Make sure the page does not contain references to resources that don't
    // exist anymore.
    logger.debug("Checking referenced resources on {}", workPage);
    try {
      for (Pagelet pagelet : workPage.getPagelets()) {
        String resourceId = pagelet.getProperty("resourceid");
        if (StringUtils.isEmpty(resourceId))
          continue;
        ResourceURI resourceURI = contentRepository.getResourceURI(resourceId);
        if (resourceURI == null) {
          logger.warn("Page {} references non existing resource '{}'", workPage, resourceId);
          throw new WebApplicationException(Status.PRECONDITION_FAILED);
        }
        resourceURI.setVersion(Resource.LIVE);
        if (!contentRepository.exists(resourceURI)) {
          logger.warn("Page {} references unpublished resource '{}'", workPage, resourceURI);
          throw new WebApplicationException(Status.PRECONDITION_FAILED);
        }
      }
    } catch (ContentRepositoryException e) {
      logger.warn("Error looking up referenced resources", e);
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    }

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

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

    boolean isAdmin = SecurityUtils.userHasRole(user, SystemRole.SITEADMIN);

    // If the page is locked by a different user, refuse
    if (workPage.isLocked() && (!workPage.getLockOwner().equals(user) && !isAdmin)) {
      return Response.status(Status.FORBIDDEN).build();
    }

    // Fix the dates
    Date startDate = null;
    Date endDate = null;
    DateFormat df = new SimpleDateFormat();

    // Parse the start date
    if (StringUtils.isNotBlank(startDateText)) {
      try {
        startDate = df.parse(startDateText);
      } catch (ParseException e) {
        try {
          startDate = WebloungeDateFormat.parseStatic(startDateText);
        } catch (ParseException e2) {
          throw new WebApplicationException(Status.BAD_REQUEST);
        }
      }
    } else {
      startDate = new Date();
    }

    // Parse the end date
    if (StringUtils.isNotBlank(endDateText)) {
      try {
        endDate = df.parse(endDateText);
      } catch (ParseException e) {
        try {
          endDate = WebloungeDateFormat.parseStatic(endDateText);
        } catch (ParseException e2) {
          throw new WebApplicationException(Status.BAD_REQUEST);
        }
      }
    }

    // Finally, perform the publish operation
    try {
      PageReader reader = new PageReader();
      Page livePage = reader.read(IOUtils.toInputStream(workPage.toXml(), "utf-8"), site);
      livePage.setVersion(Resource.LIVE);
      if (setModified)
        livePage.setModified(user, new Date());
      if (!livePage.isPublished())
        livePage.setPublished(user, startDate, endDate);
      contentRepository.putAsynchronously(livePage);
      contentRepository.deleteAsynchronously(workURI);
      logger.info("Page {} has been published by {}", workURI, user);
    } catch (SecurityException e) {
      logger.warn("Tried to publish page {} of site '{}' without permission", workURI, site);
      throw new WebApplicationException(Status.FORBIDDEN);
    } catch (IOException e) {
View Full Code Here

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

    WritableContentRepository contentRepository = (WritableContentRepository) getContentRepository(site, true);
    ResourceURI liveURI = new PageURIImpl(site, null, pageId, Resource.LIVE);

    // Does the page exist?
    Page livePage = null;
    try {
      if (!contentRepository.existsInAnyVersion(liveURI))
        throw new WebApplicationException(Status.NOT_FOUND);
      livePage = (Page) contentRepository.get(liveURI);
      if (livePage == null)
        throw new WebApplicationException(Status.PRECONDITION_FAILED);
      liveURI.setPath(livePage.getURI().getPath());
    } catch (ContentRepositoryException e) {
      logger.warn("Error lookup up page {} from repository: {}", liveURI, e.getMessage());
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    } catch (IllegalStateException e) {
      logger.warn("Error unpublishing page {}: {}", liveURI, e.getMessage());
      throw new WebApplicationException(Status.PRECONDITION_FAILED);
    }

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

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

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

    boolean isAdmin = SecurityUtils.userHasRole(user, SystemRole.SITEADMIN);

    // If the page is locked by a different user, refuse
    if (livePage.isLocked() && (!livePage.getLockOwner().equals(user) && !isAdmin)) {
      return Response.status(Status.FORBIDDEN).build();
    }

    // Finally, perform the unpublish operation, including saving the current
    // live version of the page as the new work version.
    try {
      contentRepository.delete(liveURI);
      ResourceURI workURI = new ResourceURIImpl(liveURI, Resource.WORK);
      if (!contentRepository.exists(workURI)) {
        logger.debug("Creating work version of {}", workURI);
        PageReader reader = new PageReader();
        Page workPage = reader.read(IOUtils.toInputStream(livePage.toXml(), "utf-8"), site);
        workPage.setVersion(Resource.WORK);
        workPage.setPublished(null, null, null);
        contentRepository.putAsynchronously(workPage);
      }
      logger.info("Page {} has been unpublished by {}", liveURI, user);
    } catch (SecurityException e) {
      logger.warn("Tried to unpublish page {} of site '{}' without permission", liveURI, site);
      throw new WebApplicationException(Status.FORBIDDEN);
View Full Code Here

      if (writable) {
        if (contentRepository.isReadOnly()) {
          logger.warn("Content repository '{}' is not writable", site);
          throw new WebApplicationException(Status.PRECONDITION_FAILED);
        }
        WritableContentRepository wcr = (WritableContentRepository) contentRepository;
        return wcr;
      } else {
        return contentRepository;
      }
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.