Search in sources :

Example 1 with Upload

use of com.zimbra.cs.service.FileUploadServlet.Upload in project zm-mailbox by Zimbra.

the class FileUploadServletTest method testFilenames.

@Test
public void testFilenames() throws Exception {
    ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
    addFormField(bb, "_charset_", "");
    addFormField(bb, "filename1", filename1);
    addFormFile(bb, filename1, "text/plain", content1);
    addFormField(bb, "filename2", filename2);
    addFormFile(bb, filename2, "text/plain", content2);
    addFormField(bb, "filename3", "");
    addFormFile(bb, "", null, null);
    endForm(bb);
    List<Upload> uploads = uploadForm(bb.toByteArray());
    Assert.assertEquals(2, uploads == null ? 0 : uploads.size());
    compareUploads(uploads.get(0), filename1, content1.getBytes(CharsetUtil.UTF_8));
    compareUploads(uploads.get(1), filename2, content2.getBytes(CharsetUtil.UTF_8));
}
Also used : ByteBuilder(com.zimbra.common.mime.HeaderUtils.ByteBuilder) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) Test(org.junit.Test)

Example 2 with Upload

use of com.zimbra.cs.service.FileUploadServlet.Upload in project zm-mailbox by Zimbra.

the class FileUploadServletTest method testExtraFilenames.

@Test
public void testExtraFilenames() throws Exception {
    ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
    addFormField(bb, "_charset_", "");
    addFormField(bb, "filename1", filename1 + "\r\nextra\r\ndata.txt");
    addFormFile(bb, filename1, "text/plain", content1);
    addFormField(bb, "filename2", filename2);
    addFormFile(bb, filename2, "text/plain", content2);
    addFormField(bb, "filename3", "bar.gif");
    addFormFile(bb, "", null, null);
    endForm(bb);
    List<Upload> uploads = uploadForm(bb.toByteArray());
    Assert.assertEquals(2, uploads == null ? 0 : uploads.size());
    compareUploads(uploads.get(0), filename1, content1.getBytes(CharsetUtil.UTF_8));
    compareUploads(uploads.get(1), filename2, content2.getBytes(CharsetUtil.UTF_8));
}
Also used : ByteBuilder(com.zimbra.common.mime.HeaderUtils.ByteBuilder) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) Test(org.junit.Test)

Example 3 with Upload

use of com.zimbra.cs.service.FileUploadServlet.Upload in project zm-mailbox by Zimbra.

the class CalendarCollection method uploadToInvites.

private List<Invite> uploadToInvites(DavContext ctxt, Account account) throws DavException, IOException {
    Upload upload = ctxt.getUpload();
    String contentType = upload.getContentType();
    if (!contentType.startsWith(MimeConstants.CT_TEXT_CALENDAR)) {
        throw new DavException.InvalidData(DavElements.E_SUPPORTED_CALENDAR_DATA, String.format("Incorrect Content-Type '%s', expected '%s'", contentType, MimeConstants.CT_TEXT_CALENDAR));
    }
    if (upload.getSize() <= 0) {
        throw new DavException.InvalidData(DavElements.E_VALID_CALENDAR_DATA, "empty request");
    }
    List<Invite> invites;
    try (InputStream is = ctxt.getUpload().getInputStream()) {
        ZCalendar.ZVCalendar vcalendar = ZCalendar.ZCalendarBuilder.build(is, MimeConstants.P_CHARSET_UTF8);
        // Apple iCal fixup
        CalDavUtils.removeAttendeeForOrganizer(vcalendar);
        if (ctxt.isIcalClient()) {
            // Apple iCal fixup for todos
            CalDavUtils.adjustPercentCompleteForToDos(vcalendar);
        }
        invites = Invite.createFromCalendar(account, findSummary(vcalendar), vcalendar, true);
    } catch (ServiceException se) {
        throw new DavException.InvalidData(DavElements.E_VALID_CALENDAR_DATA, String.format("Problem parsing %s data - %s", MimeConstants.CT_TEXT_CALENDAR, se.getMessage()));
    }
    return invites;
}
Also used : ZCalendar(com.zimbra.common.calendar.ZCalendar) ServiceException(com.zimbra.common.service.ServiceException) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) DavException(com.zimbra.cs.dav.DavException) InputStream(java.io.InputStream) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 4 with Upload

use of com.zimbra.cs.service.FileUploadServlet.Upload in project zm-mailbox by Zimbra.

the class ImportAppointments method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    String folder = request.getAttribute(MailConstants.A_FOLDER, DEFAULT_FOLDER_ID);
    ItemId iidFolder = new ItemId(folder, zsc);
    String ct = request.getAttribute(MailConstants.A_CONTENT_TYPE);
    if (!ct.equalsIgnoreCase("ics") && !ct.equalsIgnoreCase(MimeConstants.CT_TEXT_CALENDAR))
        throw ServiceException.INVALID_REQUEST("unsupported content type: " + ct, null);
    Element content = request.getElement(MailConstants.E_CONTENT);
    List<Upload> uploads = null;
    InputStream is = null;
    String partStr = null;
    String attachment = content.getAttribute(MailConstants.A_ATTACHMENT_ID, null);
    String messageId = content.getAttribute(MailConstants.A_MESSAGE_ID, null);
    if (attachment != null && messageId != null) {
        throw ServiceException.INVALID_REQUEST("use either aid or mid but not both", null);
    }
    SourceSpecMethod sourceSpecMethod;
    if (messageId != null) {
        sourceSpecMethod = SourceSpecMethod.MSG_PART;
    } else if (attachment != null) {
        sourceSpecMethod = SourceSpecMethod.ATTACH_ID;
    } else {
        sourceSpecMethod = SourceSpecMethod.INLINE_TEXT;
    }
    try {
        if (SourceSpecMethod.MSG_PART.equals(sourceSpecMethod)) {
            // Get content from part of existing message.
            ItemId iid = new ItemId(messageId, zsc);
            String part = content.getAttribute(MailConstants.A_PART);
            partStr = CreateContact.fetchItemPart(zsc, octxt, mbox, iid, part, null, MimeConstants.P_CHARSET_UTF8);
            is = new ByteArrayInputStream(partStr.getBytes(MimeConstants.P_CHARSET_UTF8));
        }
        // if the "target item" is remote, we will need to proxy the request
        ItemId iidTarget = getProxyTarget(zsc, octxt, iidFolder, true);
        if (iidTarget != null) {
            ZimbraLog.misc.info("Proxying ImportAppointments - folder=" + folder + ", acct=" + iidTarget.getAccountId());
            if (SourceSpecMethod.MSG_PART.equals(sourceSpecMethod)) {
                /* Bug 77131 change specification method to something that will work in the proxied context */
                content.addAttribute(MailConstants.A_MESSAGE_ID, (String) null);
                content.addAttribute(MailConstants.A_PART, (String) null);
                Upload up = FileUploadServlet.saveUpload(is, "upload.ics", MimeConstants.CT_TEXT_CALENDAR, zsc.getAuthtokenAccountId());
                content.addAttribute(MailConstants.A_ATTACHMENT_ID, up.getId());
            }
            return proxyRequest(request, context, iidFolder, iidTarget);
        }
        if (SourceSpecMethod.ATTACH_ID.equals(sourceSpecMethod)) {
            is = parseUploadedContent(zsc, attachment, uploads = new ArrayList<Upload>());
        } else if (SourceSpecMethod.INLINE_TEXT.equals(sourceSpecMethod)) {
            // Convert LF to CRLF because the XML parser normalizes element text to LF.
            String text = StringUtil.lfToCrlf(content.getText());
            is = new ByteArrayInputStream(text.getBytes(MimeConstants.P_CHARSET_UTF8));
        }
        List<ZVCalendar> icals = ZCalendarBuilder.buildMulti(is, MimeConstants.P_CHARSET_UTF8);
        is.close();
        is = null;
        List<Invite> invites = Invite.createFromCalendar(mbox.getAccount(), null, icals, true, true, null);
        Set<String> uidsSeen = new HashSet<String>();
        StringBuilder ids = new StringBuilder();
        for (Invite inv : invites) {
            // handle missing UIDs on remote calendars by generating them as needed
            String uid = inv.getUid();
            if (uid == null) {
                uid = LdapUtil.generateUUID();
                inv.setUid(uid);
            }
            boolean addRevision;
            if (!uidsSeen.contains(uid)) {
                addRevision = true;
                uidsSeen.add(uid);
            } else {
                addRevision = false;
            }
            // and add the invite to the calendar!
            try {
                AddInviteData aid = mbox.addInvite(octxt, inv, iidFolder.getId(), false, addRevision);
                if (aid != null) {
                    if (ids.length() > 0)
                        ids.append(",");
                    ids.append(aid.calItemId).append("-").append(aid.invId);
                }
            } catch (ServiceException e) {
                ZimbraLog.calendar.warn("Skipping bad iCalendar object during import: uid=" + inv.getUid(), e);
            }
        }
        Element response = zsc.createElement(MailConstants.IMPORT_APPOINTMENTS_RESPONSE);
        Element cn = response.addElement(MailConstants.E_APPOINTMENT);
        cn.addAttribute(MailConstants.A_IDS, ids.toString());
        cn.addAttribute(MailConstants.A_NUM, invites.size());
        return response;
    } catch (IOException e) {
        throw MailServiceException.UNABLE_TO_IMPORT_APPOINTMENTS(e.getMessage(), e);
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
            }
        if (attachment != null)
            FileUploadServlet.deleteUploads(uploads);
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AddInviteData(com.zimbra.cs.mailbox.Mailbox.AddInviteData) Element(com.zimbra.common.soap.Element) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) IOException(java.io.IOException) ItemId(com.zimbra.cs.service.util.ItemId) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Invite(com.zimbra.cs.mailbox.calendar.Invite) HashSet(java.util.HashSet)

Example 5 with Upload

use of com.zimbra.cs.service.FileUploadServlet.Upload in project zm-mailbox by Zimbra.

the class ImportAppointments method parseUploadedContent.

private static InputStream parseUploadedContent(ZimbraSoapContext lc, String attachId, List<Upload> uploads) throws ServiceException {
    Upload up = FileUploadServlet.fetchUpload(lc.getAuthtokenAccountId(), attachId, lc.getAuthToken());
    if (up == null)
        throw MailServiceException.NO_SUCH_UPLOAD(attachId);
    uploads.add(up);
    try {
        return up.getInputStream();
    } catch (IOException e) {
        throw ServiceException.FAILURE(e.getMessage(), e);
    }
}
Also used : Upload(com.zimbra.cs.service.FileUploadServlet.Upload) IOException(java.io.IOException)

Aggregations

Upload (com.zimbra.cs.service.FileUploadServlet.Upload)24 IOException (java.io.IOException)12 ServiceException (com.zimbra.common.service.ServiceException)6 Element (com.zimbra.common.soap.Element)5 ItemId (com.zimbra.cs.service.util.ItemId)5 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)5 InputStream (java.io.InputStream)5 ByteBuilder (com.zimbra.common.mime.HeaderUtils.ByteBuilder)4 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)4 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 Test (org.junit.Test)4 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)3 Account (com.zimbra.cs.account.Account)3 AuthToken (com.zimbra.cs.account.AuthToken)3 OperationContext (com.zimbra.cs.mailbox.OperationContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)2 ContentDisposition (com.zimbra.common.mime.ContentDisposition)2 ContentType (com.zimbra.common.mime.ContentType)2 AuthTokenException (com.zimbra.cs.account.AuthTokenException)2