Search in sources :

Example 6 with BwResourceContent

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

the class Notifications method update.

@Override
public boolean update(final NotificationType val) throws CalFacadeException {
    if ((val == null) || (val.getNotification() == null) || (val.getNotification().getElementName() == null)) {
        return false;
    }
    try {
        final String xml = val.toXml(true);
        if (xml == null) {
            return false;
        }
        final BwCalendar ncol = getCols().getSpecial(BwCalendar.calTypeNotifications, true);
        if (ncol == null) {
            return false;
        }
        final BwResource noteRsrc = getSvc().getResourcesHandler().get(Util.buildPath(false, ncol.getPath(), "/", val.getName()));
        if (noteRsrc == null) {
            return false;
        }
        BwResourceContent rc = noteRsrc.getContent();
        if (rc == null) {
            rc = new BwResourceContent();
            noteRsrc.setContent(rc);
        }
        final byte[] xmlData = xml.getBytes();
        rc.setValue(getSvc().getBlob(xmlData));
        noteRsrc.setContentLength(xmlData.length);
        noteRsrc.setContentType(val.getContentType());
        getSvc().getResourcesHandler().update(noteRsrc, true);
        getNoteClient().informNotifier(getPrincipalHref(), noteRsrc.getName());
        return true;
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        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 7 with BwResourceContent

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

the class EntityDAO method getResourceContent.

public void getResourceContent(final BwResource val) throws CalFacadeException {
    final HibSession sess = getSess();
    sess.createQuery(getResourceContentQuery);
    sess.setString("path", val.getColPath());
    sess.setString("name", val.getName());
    sess.cacheableQuery();
    final BwResourceContent rc = (BwResourceContent) sess.getUnique();
    if (rc == null) {
        throw new CalFacadeException(CalFacadeException.missingResourceContent);
    }
    val.setContent(rc);
}
Also used : HibSession(org.bedework.calcorei.HibSession) BwResourceContent(org.bedework.calfacade.BwResourceContent) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 8 with BwResourceContent

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

the class BwCalDAVResource method getBinaryStream.

private InputStream getBinaryStream() throws WebdavException {
    if (rsrc == null) {
        return null;
    }
    if (rsrc.getContent() == null) {
        intf.getFileContent(this);
    }
    BwResourceContent bwrc = rsrc.getContent();
    if (bwrc == null) {
        return null;
    }
    Blob b = bwrc.getValue();
    if (b == null) {
        return null;
    }
    try {
        return b.getBinaryStream();
    } catch (final Throwable t) {
        throw new WebdavException(t);
    }
}
Also used : Blob(java.sql.Blob) WebdavException(org.bedework.webdav.servlet.shared.WebdavException) BwResourceContent(org.bedework.calfacade.BwResourceContent)

Example 9 with BwResourceContent

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

the class BwCalDAVResource method setBinaryContent.

@Override
public void setBinaryContent(final InputStream val) throws WebdavException {
    BwResource r = getRsrc();
    BwResourceContent rc = r.getContent();
    if (rc == null) {
        if (!isNew()) {
            intf.getFileContent(this);
            rc = r.getContent();
        }
        if (rc == null) {
            rc = new BwResourceContent();
            r.setContent(rc);
        }
    }
    try {
        /* If this is a notification we need to unprefix the data */
        final InputStream str;
        if (!isNotification()) {
            str = val;
        } else {
            final NotificationType note = Parser.fromXml(val);
            note.getNotification().unprefixHrefs(intf.getUrlHandler());
            str = new ByteArrayInputStream(note.toXml(true).getBytes());
        }
        final ByteArrayOutputStream outBuff = new ByteArrayOutputStream();
        byte[] inBuffer = new byte[1000];
        long clen = 0;
        int chunkSize;
        int maxSize = intf.getAuthProperties().getMaxUserEntitySize();
        long oldSize = r.getContentLength();
        while (clen <= maxSize) {
            chunkSize = str.read(inBuffer);
            if (chunkSize < 0) {
                break;
            }
            outBuff.write(inBuffer, 0, chunkSize);
            clen += chunkSize;
        }
        if (clen > maxSize) {
            throw new WebdavForbidden(CaldavTags.maxResourceSize);
        }
        rc.setValue(intf.getBlob(outBuff.toByteArray()));
        r.setContentLength(clen);
        if (!intf.updateQuota(getOwner(), clen - oldSize)) {
            throw new WebdavForbidden(WebdavTags.quotaNotExceeded);
        }
    } catch (WebdavException wde) {
        throw wde;
    } catch (Throwable t) {
        throw new WebdavException(t);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WebdavForbidden(org.bedework.webdav.servlet.shared.WebdavForbidden) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BwResource(org.bedework.calfacade.BwResource) NotificationType(org.bedework.caldav.util.notifications.NotificationType) WebdavException(org.bedework.webdav.servlet.shared.WebdavException) BwResourceContent(org.bedework.calfacade.BwResourceContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 10 with BwResourceContent

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

the class Notifications method makeNotification.

private NotificationType makeNotification(final BwResource rsrc) throws CalFacadeException {
    getSvc().getResourcesHandler().getContent(rsrc);
    final BwResourceContent bwrc = rsrc.getContent();
    if (bwrc == null) {
        return null;
    }
    final Blob b = bwrc.getValue();
    if (b == null) {
        return null;
    }
    try {
        final InputStream is = b.getBinaryStream();
        final NotificationType note = Parser.fromXml(is);
        if (note != null) {
            note.setName(rsrc.getName());
            note.getNotification().setEncoding(rsrc.getEncoding());
        }
        return note;
    } catch (final Throwable t) {
        if (debug) {
            error(t);
        }
        error("Unable to parse notification " + rsrc.getColPath() + " " + rsrc.getName());
        return null;
    }
}
Also used : Blob(java.sql.Blob) InputStream(java.io.InputStream) NotificationType(org.bedework.caldav.util.notifications.NotificationType) 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