Search in sources :

Example 11 with UserServletException

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

the class CsvFormatter method saveCallback.

@Override
public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename) throws UserServletException, ServiceException, IOException {
    // Disable the jetty timeout
    disableJettyTimeout(context);
    // Detect the charset of upload file.
    PushbackInputStream pis = new PushbackInputStream(context.getRequestInputStream(), READ_AHEAD_BUFFER_SIZE);
    byte[] buf = new byte[READ_AHEAD_BUFFER_SIZE];
    int bytesRead = pis.read(buf, 0, READ_AHEAD_BUFFER_SIZE);
    CharsetDetector detector = new CharsetDetector();
    detector.setText(buf);
    CharsetMatch match = detector.detect();
    String guess = match.getName();
    Charset charset;
    if (guess != null) {
        try {
            charset = Charset.forName(guess);
        } catch (IllegalArgumentException e) {
            charset = Charsets.UTF_8;
        }
    } else {
        charset = Charsets.UTF_8;
    }
    if (bytesRead > 0) {
        pis.unread(buf, 0, bytesRead);
    }
    InputStreamReader isr = new InputStreamReader(pis, charset);
    BufferedReader reader = new BufferedReader(isr);
    try {
        String format = context.params.get(UserServlet.QP_CSVFORMAT);
        String locale = context.req.getParameter(UserServlet.QP_CSVLOCALE);
        if (locale == null) {
            locale = context.getLocale().toString();
        }
        List<Map<String, String>> contacts = ContactCSV.getContacts(reader, format, locale);
        ItemId iidFolder = new ItemId(folder);
        ImportContacts.ImportCsvContacts(context.opContext, context.targetMailbox, iidFolder, contacts);
    } catch (ContactCSV.ParseException e) {
        ZimbraLog.misc.debug("ContactCSV - ParseException thrown", e);
        throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "Could not parse csv file - Reason : " + e.getMessage());
    } finally {
        reader.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) UserServletException(com.zimbra.cs.service.UserServletException) CharsetDetector(com.ibm.icu.text.CharsetDetector) Charset(java.nio.charset.Charset) ItemId(com.zimbra.cs.service.util.ItemId) CharsetMatch(com.ibm.icu.text.CharsetMatch) PushbackInputStream(java.io.PushbackInputStream) BufferedReader(java.io.BufferedReader) Map(java.util.Map)

Example 12 with UserServletException

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

the class Formatter method updateClient.

protected void updateClient(UserServletContext context, Exception e, List<ServiceException> w) throws UserServletException, IOException, ServletException, ServiceException {
    String callback = context.params.get(QP_CALLBACK);
    Throwable exception = null;
    PrintWriter out = null;
    if (e != null) {
        Throwable cause = e.getCause();
        exception = cause instanceof UserServletException || cause instanceof ServletException || cause instanceof IOException ? cause : e;
        context.logError(exception);
    }
    // This ensures seamless display of manual download link provided by error callback, or otherwise a null blank preview pane.
    if (exception instanceof ConversionUnsupportedException && (callback == null || !callback.startsWith("ZmPreviewView._errorCallback"))) {
        exception = null;
    }
    /* Make doubly sure that parameters are valid.  In the past, a path which failed to call this during
         * initial formatter setup was missed.  Ideally, should have caught this issue earlier to avoid
         * wasting effort.
         */
    validateParams(context);
    if (callback == null || callback.equals("")) {
        if (context.params.get(PROGRESS) == null) {
            if (exception == null) {
                return;
            } else if (exception instanceof UserServletException) {
                throw (UserServletException) exception;
            } else if (exception instanceof ServletException) {
                throw (ServletException) exception;
            } else if (exception instanceof IOException) {
                throw (IOException) exception;
            } else if (exception instanceof NoSuchItemException) {
                throw (ServiceException) exception;
            } else if (exception instanceof ServiceException) {
                ServiceException se = (ServiceException) exception;
                if (se.getCode() == ServiceException.INVALID_REQUEST) {
                    throw se;
                }
            }
            throw ServiceException.FAILURE(getType() + " formatter failure", exception);
        }
        try {
            out = updateClient(context, false);
        } catch (IllegalStateException ise) {
            ZimbraLog.misc.warn("format output has already been written.");
            return;
        }
        if (exception == null && (w == null || w.isEmpty())) {
            out.println("<body></body>\n</html>");
        } else {
            ZimbraLog.misc.warn(getType() + " formatter exception", exception);
            out.println("<body>\n<pre>");
            if (exception != null)
                out.print(exception.getLocalizedMessage());
            for (ServiceException warning : w) {
                out.println("<br>");
                out.println(warning.toString().replace("\n", "<br>"));
            }
            out.println("</pre>\n</body>\n</html>");
        }
    } else if (!"2".equals(context.params.get(PROGRESS))) {
        String result;
        if (exception != null) {
            if (exception instanceof ConversionUnsupportedException) {
                ZimbraLog.misc.warn(getType() + " formatter exception, " + exception.getMessage());
            } else {
                ZimbraLog.misc.warn(getType() + " formatter exception", exception);
            }
            result = "fail";
        } else if (w == null || w.size() == 0) {
            if (context.req.getMethod().equals("GET")) {
                return;
            }
            result = "success";
        } else {
            result = "warn";
        }
        try {
            out = updateClient(context, false);
        } catch (IllegalStateException ise) {
            ZimbraLog.misc.warn("format output has already been written.");
            return;
        }
        // mark done no matter what happens next
        context.params.put(PROGRESS, "2");
        out.println("<body onload='onLoad()'>");
        out.println("<script>");
        out.println("function onLoad() {");
        out.print("    window.parent." + callback + "('" + result + "'");
        if (exception != null) {
            ServiceException se = exception instanceof ServiceException ? (ServiceException) exception : FormatterServiceException.UNKNOWN_ERROR(exception);
            out.print(",\n\t");
            out.print(SoapProtocol.SoapJS.soapFault(se));
        }
        if (w != null) {
            for (ServiceException warning : w) {
                out.print(",\n\t");
                out.print(SoapProtocol.SoapJS.soapFault(warning));
            }
        }
        out.println(");");
        out.println("}");
        out.println("</script>");
        out.println("</body>");
        out.println("</html>");
    }
}
Also used : ServletException(javax.servlet.ServletException) UserServletException(com.zimbra.cs.service.UserServletException) ConversionUnsupportedException(com.zimbra.cs.convert.ConversionUnsupportedException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) UserServletException(com.zimbra.cs.service.UserServletException) IOException(java.io.IOException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) PrintWriter(java.io.PrintWriter)

Aggregations

UserServletException (com.zimbra.cs.service.UserServletException)12 ServiceException (com.zimbra.common.service.ServiceException)8 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)6 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)5 ServletException (javax.servlet.ServletException)5 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 IOException (java.io.IOException)4 ConversionUnsupportedException (com.zimbra.cs.convert.ConversionUnsupportedException)3 MailItem (com.zimbra.cs.mailbox.MailItem)3 PushbackInputStream (java.io.PushbackInputStream)3 GuestAccount (com.zimbra.cs.account.GuestAccount)2 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)2 Document (com.zimbra.cs.mailbox.Document)2 ParsedDocument (com.zimbra.cs.mime.ParsedDocument)2 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)2 UploadScanner (com.zimbra.cs.service.mail.UploadScanner)2 Blob (com.zimbra.cs.store.Blob)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Charset (java.nio.charset.Charset)2