Search in sources :

Example 1 with BwResourceContent

use of org.bedework.calfacade.BwResourceContent in project bw-calendar-engine by Bedework.

the class ResourceContentRule method end.

@Override
public void end(final String ns, final String name) throws Exception {
    BwResourceContent entity = (BwResourceContent) pop();
    if (!(top() instanceof BwResource)) {
        warn("Expected a resource object: found " + top());
        return;
    }
    BwResource r = (BwResource) top();
    r.setContent(entity);
}
Also used : BwResource(org.bedework.calfacade.BwResource) BwResourceContent(org.bedework.calfacade.BwResourceContent)

Example 2 with BwResourceContent

use of org.bedework.calfacade.BwResourceContent in project bw-calendar-engine by Bedework.

the class Notifications method add.

@Override
public boolean add(final NotificationType val) throws CalFacadeException {
    if ((val == null) || (val.getNotification() == null) || (val.getNotification().getElementName() == null)) {
        return false;
    }
    final BwCalendar ncol = getCols().getSpecial(BwCalendar.calTypeNotifications, true);
    if (ncol == null) {
        return false;
    }
    final BwResource noteRsrc = new BwResource();
    noteRsrc.setName(val.getName());
    noteRsrc.setEncoding(val.getNotification().getEncoding());
    final BwResourceContent rc = new BwResourceContent();
    noteRsrc.setContent(rc);
    try {
        final String xml = val.toXml(true);
        if (xml == null) {
            return false;
        }
        final byte[] xmlData = xml.getBytes();
        rc.setValue(getSvc().getBlob(xmlData));
        noteRsrc.setContentLength(xmlData.length);
        noteRsrc.setContentType(val.getContentType());
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
    for (int i = 0; i <= 100; i++) {
        if (getRess().saveNotification(ncol.getPath(), noteRsrc)) {
            getNoteClient().informNotifier(getPrincipalHref(), noteRsrc.getName());
            return true;
        }
        noteRsrc.setName(val.getName() + "-" + i);
    }
    throw new CalFacadeException(CalFacadeException.duplicateResource, val.getName());
}
Also used : BwResource(org.bedework.calfacade.BwResource) BwResourceContent(org.bedework.calfacade.BwResourceContent) BwCalendar(org.bedework.calfacade.BwCalendar) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 3 with BwResourceContent

use of org.bedework.calfacade.BwResourceContent in project bw-calendar-engine by Bedework.

the class ResourcesImpl method copyMove.

@Override
public boolean copyMove(final BwResource val, final String to, final String name, final boolean copy, final boolean overwrite) throws CalFacadeException {
    try {
        setupSharableEntity(val, getPrincipal().getPrincipalRef());
        final BwCalendar collTo = getCols().get(to);
        if (collTo == null) {
            throw new CalFacadeException(CalFacadeException.collectionNotFound, to);
        }
        if (collTo.getCalType() != BwCalendar.calTypeFolder) {
            // Only allowed into a folder.
            throw new CalFacadeException(CalFacadeException.badRequest, to);
        }
        final int access;
        if (copy) {
            access = PrivilegeDefs.privWrite;
        } else {
            access = PrivilegeDefs.privBind;
        }
        checkAccess(collTo, access, false);
        BwResource r = getCal().getResource(val.getName(), collTo, access);
        boolean createdNew = false;
        getContent(val);
        if (r != null) {
            /* Update of the target from the source */
            if (!overwrite) {
                throw new CalFacadeException(CalFacadeException.targetExists, val.getName());
            }
            getContent(r);
            r.setContentType(val.getContentType());
            final BwResourceContent rc = r.getContent();
            final BwResourceContent toRc = val.getContent();
            r.setContentLength(toRc.getValue().length());
            r.updateLastmod(getCurrentTimestamp());
            rc.setValue(val.getContent().getValue());
            getCal().saveOrUpdate(r);
            getCal().saveOrUpdate(rc);
        } else {
            /* Create a new resource */
            r = new BwResource();
            setupSharableEntity(r, getPrincipal().getPrincipalRef());
            r.setName(name);
            r.setColPath(collTo.getPath());
            r.setContentType(val.getContentType());
            r.setContentLength(val.getContentLength());
            r.updateLastmod(getCurrentTimestamp());
            getCal().saveOrUpdate(r);
            final BwResourceContent fromRc = val.getContent();
            final BwResourceContent rc = new BwResourceContent();
            rc.setColPath(collTo.getPath());
            rc.setName(val.getName());
            rc.setValue(fromRc.getValue());
            getCal().saveOrUpdate(rc);
            createdNew = true;
        }
        if (!copy) {
            // Delete (tombstone) the old one
            final BwCalendar collFrom = getCols().get(val.getColPath());
            checkAccess(collFrom, PrivilegeDefs.privUnbind, false);
            final BwResourceContent rc = val.getContent();
            getCal().delete(rc);
            /* Remove any previous tombstoned version */
            final BwResource tr = getCal().getResource(val.getName() + BwResource.tombstonedSuffix, collFrom, PrivilegeDefs.privUnbind);
            if (tr != null) {
                getCal().delete(tr);
            }
            val.setContent(null);
            val.tombstone();
            val.updateLastmod(getCurrentTimestamp());
            getCal().saveOrUpdate(val);
            touchCalendar(collFrom);
        }
        touchCalendar(collTo);
        return createdNew;
    } catch (final CalFacadeException cfe) {
        getSvc().rollbackTransaction();
        throw cfe;
    } catch (final Throwable t) {
        getSvc().rollbackTransaction();
        throw new CalFacadeException(t);
    }
}
Also used : BwResource(org.bedework.calfacade.BwResource) BwResourceContent(org.bedework.calfacade.BwResourceContent) BwCalendar(org.bedework.calfacade.BwCalendar) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 4 with BwResourceContent

use of org.bedework.calfacade.BwResourceContent in project bw-calendar-engine by Bedework.

the class ResourcesImpl method save.

private boolean save(final String path, final BwResource val, final boolean forNotification, final boolean returnIfExists) throws CalFacadeException {
    try {
        final BwCalendar coll = getCols().get(path);
        if (coll == null) {
            throw new CalFacadeException(CalFacadeException.collectionNotFound, path);
        }
        if (forNotification) {
            // We allow this for subscription only
            if (coll.getCalType() != BwCalendar.calTypeNotifications) {
                throw new CalFacadeException(CalFacadeException.badRequest, path);
            }
        } else if (getPrincipalInfo().getSubscriptionsOnly()) {
            throw new CalFacadeForbidden("User has read only access");
        }
        final BwResource r = getCal().getResource(val.getName(), coll, PrivilegeDefs.privAny);
        if (r != null) {
            if (returnIfExists) {
                return false;
            }
            throw new CalFacadeException(CalFacadeException.duplicateResource, val.getName());
        }
        final BwResourceContent rc = val.getContent();
        if (rc == null) {
            throw new CalFacadeException(CalFacadeException.missingResourceContent);
        }
        setupSharableEntity(val, getPrincipal().getPrincipalRef());
        val.setColPath(path);
        if ((coll.getCalType() == BwCalendar.calTypeCalendarCollection) || (coll.getCalType() == BwCalendar.calTypeExtSub)) {
            throw new CalFacadeException(CalFacadeException.badRequest, path);
        }
        checkAccess(coll, PrivilegeDefs.privBind, false);
        val.updateLastmod(getCurrentTimestamp());
        getCal().add(val);
        rc.setColPath(val.getColPath());
        rc.setName(val.getName());
        getCal().add(rc);
        touchCalendar(coll);
        return true;
    } catch (final CalFacadeException cfe) {
        getSvc().rollbackTransaction();
        throw cfe;
    }
}
Also used : BwResource(org.bedework.calfacade.BwResource) CalFacadeForbidden(org.bedework.calfacade.exc.CalFacadeForbidden) BwResourceContent(org.bedework.calfacade.BwResourceContent) BwCalendar(org.bedework.calfacade.BwCalendar) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 5 with BwResourceContent

use of org.bedework.calfacade.BwResourceContent in project bw-calendar-engine by Bedework.

the class RestoreImpl method restoreResource.

@Override
public void restoreResource(final BwResource o) throws Throwable {
    try {
        startTransaction();
        o.markUnsaved();
        getCal().saveOrUpdate(o);
        final BwResourceContent rc = o.getContent();
        rc.markUnsaved();
        getCal().saveOrUpdate(rc);
    } finally {
        endTransaction();
    }
}
Also used : BwResourceContent(org.bedework.calfacade.BwResourceContent)

Aggregations

BwResourceContent (org.bedework.calfacade.BwResourceContent)12 BwResource (org.bedework.calfacade.BwResource)7 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)7 BwCalendar (org.bedework.calfacade.BwCalendar)4 InputStream (java.io.InputStream)2 Blob (java.sql.Blob)2 NotificationType (org.bedework.caldav.util.notifications.NotificationType)2 WebdavException (org.bedework.webdav.servlet.shared.WebdavException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HibSession (org.bedework.calcorei.HibSession)1 CalFacadeForbidden (org.bedework.calfacade.exc.CalFacadeForbidden)1 WebdavForbidden (org.bedework.webdav.servlet.shared.WebdavForbidden)1