use of com.zimbra.common.service.ServiceException.InternalArgument in project zm-mailbox by Zimbra.
the class FileUploadServlet method fetchRemoteUpload.
private static Upload fetchRemoteUpload(String accountId, String uploadId, AuthToken authtoken) throws ServiceException {
// check if we have fetched the Upload from the remote server previously
String localUploadId = null;
synchronized (mProxiedUploadIds) {
localUploadId = mProxiedUploadIds.get(uploadId);
}
if (localUploadId != null) {
synchronized (mPending) {
Upload up = mPending.get(localUploadId);
if (up != null)
return up;
}
}
// the first half of the upload id is the server id where it lives
Server server = Provisioning.getInstance().get(Key.ServerBy.id, getUploadServerId(uploadId));
String url = AccountUtil.getBaseUri(server);
if (url == null)
return null;
String hostname = server.getServiceHostname();
url += ContentServlet.SERVLET_PATH + ContentServlet.PREFIX_PROXY + '?' + ContentServlet.PARAM_UPLOAD_ID + '=' + uploadId + '&' + ContentServlet.PARAM_EXPUNGE + "=true";
// create an HTTP client with auth cookie to fetch the file from the remote ContentServlet
HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
HttpGet get = new HttpGet(url);
authtoken.encode(clientBuilder, get, false, hostname);
HttpClient client = clientBuilder.build();
try {
// fetch the remote item
HttpResponse httpResp = HttpClientUtil.executeMethod(client, get);
int statusCode = httpResp.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK)
return null;
// metadata is encoded in the response's HTTP headers
Header ctHeader = httpResp.getFirstHeader("Content-Type");
String contentType = ctHeader == null ? "text/plain" : ctHeader.getValue();
Header cdispHeader = httpResp.getFirstHeader("Content-Disposition");
String filename = cdispHeader == null ? "unknown" : new ContentDisposition(cdispHeader.getValue()).getParameter("filename");
// store the fetched upload along with original uploadId
Upload up = saveUpload(httpResp.getEntity().getContent(), filename, contentType, accountId);
synchronized (mProxiedUploadIds) {
mProxiedUploadIds.put(uploadId, up.uuid);
}
return up;
} catch (HttpException e) {
throw ServiceException.PROXY_ERROR(e, url);
} catch (IOException e) {
throw ServiceException.RESOURCE_UNREACHABLE("can't fetch remote upload", e, new InternalArgument(ServiceException.URL, url, Argument.Type.STR));
} finally {
get.releaseConnection();
}
}
use of com.zimbra.common.service.ServiceException.InternalArgument in project zm-mailbox by Zimbra.
the class SaveDocument method fetchMimePart.
private Doc fetchMimePart(OperationContext octxt, AuthToken authtoken, ItemId itemId, String partId, String name, String ct, String description) throws ServiceException {
String accountId = itemId.getAccountId();
Account acct = Provisioning.getInstance().get(AccountBy.id, accountId);
if (Provisioning.onLocalServer(acct)) {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
Message msg = mbox.getMessageById(octxt, itemId.getId());
try {
return new Doc(Mime.getMimePart(msg.getMimeMessage(), partId), name, ct);
} catch (MessagingException e) {
throw ServiceException.RESOURCE_UNREACHABLE("can't fetch mime part msgId=" + itemId + ", partId=" + partId, e);
} catch (IOException e) {
throw ServiceException.RESOURCE_UNREACHABLE("can't fetch mime part msgId=" + itemId + ", partId=" + partId, e);
}
}
String url = UserServlet.getRestUrl(acct) + "?auth=co&id=" + itemId + "&part=" + partId;
HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
HttpGet get = new HttpGet(url);
authtoken.encode(clientBuilder, get, false, acct.getAttr(ZAttrProvisioning.A_zimbraMailHost));
HttpClient client = clientBuilder.build();
try {
HttpResponse httpResp = HttpClientUtil.executeMethod(client, get);
int statusCode = httpResp.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw ServiceException.RESOURCE_UNREACHABLE("can't fetch remote mime part", null, new InternalArgument(ServiceException.URL, url, Argument.Type.STR));
}
Header ctHeader = httpResp.getFirstHeader("Content-Type");
ContentType contentType = new ContentType(ctHeader.getValue());
return new Doc(httpResp.getEntity().getContent(), contentType, name, ct, description);
} catch (HttpException e) {
throw ServiceException.PROXY_ERROR(e, url);
} catch (IOException e) {
throw ServiceException.RESOURCE_UNREACHABLE("can't fetch remote mime part", e, new InternalArgument(ServiceException.URL, url, Argument.Type.STR));
}
}
Aggregations