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();
}
}
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>");
}
}
Aggregations