Search in sources :

Example 1 with InternalArgument

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();
    }
}
Also used : Server(com.zimbra.cs.account.Server) HttpGet(org.apache.http.client.methods.HttpGet) InternalArgument(com.zimbra.common.service.ServiceException.InternalArgument) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) IOException(java.io.IOException) Header(org.apache.http.Header) ContentDisposition(com.zimbra.common.mime.ContentDisposition) HttpClient(org.apache.http.client.HttpClient) HttpException(org.apache.http.HttpException)

Example 2 with InternalArgument

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));
    }
}
Also used : Account(com.zimbra.cs.account.Account) Message(com.zimbra.cs.mailbox.Message) ContentType(com.zimbra.common.mime.ContentType) MessagingException(javax.mail.MessagingException) HttpGet(org.apache.http.client.methods.HttpGet) InternalArgument(com.zimbra.common.service.ServiceException.InternalArgument) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) Mailbox(com.zimbra.cs.mailbox.Mailbox) Header(org.apache.http.Header) HttpClient(org.apache.http.client.HttpClient) HttpException(org.apache.http.HttpException)

Aggregations

InternalArgument (com.zimbra.common.service.ServiceException.InternalArgument)2 IOException (java.io.IOException)2 Header (org.apache.http.Header)2 HttpException (org.apache.http.HttpException)2 HttpResponse (org.apache.http.HttpResponse)2 HttpClient (org.apache.http.client.HttpClient)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)2 ContentDisposition (com.zimbra.common.mime.ContentDisposition)1 ContentType (com.zimbra.common.mime.ContentType)1 Account (com.zimbra.cs.account.Account)1 Server (com.zimbra.cs.account.Server)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 Message (com.zimbra.cs.mailbox.Message)1 MessagingException (javax.mail.MessagingException)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1