Search in sources :

Example 61 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project zm-mailbox by Zimbra.

the class TritonBlobStoreManager method finishUpload.

@Override
public String finishUpload(ExternalUploadedBlob blob) throws IOException, ServiceException {
    TritonBlob tb = (TritonBlob) blob;
    PostMethod post = new PostMethod(url + tb.getUploadId());
    ZimbraLog.store.info("posting to %s with locator %s", post.getURI(), tb.getLocator());
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    try {
        post.addRequestHeader(TritonHeaders.OBJECTID, tb.getLocator());
        post.addRequestHeader(TritonHeaders.HASH_TYPE, hashType.toString());
        post.addRequestHeader(TritonHeaders.SERVER_TOKEN, tb.getServerToken().getToken());
        int statusCode = HttpClientUtil.executeMethod(client, post);
        if (statusCode == HttpStatus.SC_CREATED) {
            return tb.getLocator();
        } else {
            ZimbraLog.store.error("failed with code %d response: %s", statusCode, post.getResponseBodyAsString());
            throw ServiceException.FAILURE("unable to store blob " + statusCode + ":" + post.getStatusText(), null);
        }
    } finally {
        post.releaseConnection();
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 62 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project zm-mailbox by Zimbra.

the class TritonIncomingOutputStream method sendHttpData.

private void sendHttpData() throws IOException {
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    PostMethod post;
    boolean started = false;
    if (uploadUrl.isInitialized()) {
        started = true;
        post = new PostMethod(baseUrl + uploadUrl);
    } else {
        post = new PostMethod(baseUrl + "/blob");
    }
    try {
        ZimbraLog.store.info("posting to %s", post.getURI());
        HttpClientUtil.addInputStreamToHttpMethod(post, new ByteArrayInputStream(baos.toByteArray()), baos.size(), "application/octet-stream");
        post.addRequestHeader(TritonHeaders.CONTENT_LENGTH, baos.size() + "");
        post.addRequestHeader(TritonHeaders.HASH_TYPE, hashType.toString());
        post.addRequestHeader("Content-Range", "bytes " + written.longValue() + "-" + (written.longValue() + baos.size() - 1) + "/*");
        if (serverToken.getToken() != null) {
            post.addRequestHeader(TritonHeaders.SERVER_TOKEN, serverToken.getToken());
        }
        int statusCode = HttpClientUtil.executeMethod(client, post);
        if (statusCode == HttpStatus.SC_OK) {
            handleResponse(post);
        } else if (!started && statusCode == HttpStatus.SC_SEE_OTHER) {
            started = true;
            uploadUrl.setUploadUrl(post.getResponseHeader(TritonHeaders.LOCATION).getValue());
            handleResponse(post);
        } else {
            throw new IOException("Unable to append, bad response code " + statusCode);
        }
    } finally {
        post.releaseConnection();
    }
    baos = new ByteArrayOutputStream(LC.triton_upload_buffer_size.intValue());
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.commons.httpclient.HttpClient) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 63 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project zm-mailbox by Zimbra.

the class TritonBlobStoreManager method sisCreate.

/**
     * Run SIS operation against remote server. If a blob already exists for the locator the remote ref count is incremented.
     * @param hash: The content hash of the blob
     * @return true if blob already exists, false if not
     * @throws IOException
     * @throws ServiceException
     */
private boolean sisCreate(byte[] hash) throws IOException {
    String locator = getLocator(hash);
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    PostMethod post = new PostMethod(blobApiUrl + locator);
    ZimbraLog.store.info("SIS create URL: %s", post.getURI());
    try {
        post.addRequestHeader(TritonHeaders.HASH_TYPE, hashType.toString());
        int statusCode = HttpClientUtil.executeMethod(client, post);
        if (statusCode == HttpStatus.SC_CREATED) {
            //exists, ref count incremented
            return true;
        } else if (statusCode == HttpStatus.SC_NOT_FOUND) {
            if (emptyLocator.equals(locator)) {
                //empty file
                return true;
            } else {
                //does not exist
                return false;
            }
        } else if (statusCode == HttpStatus.SC_BAD_REQUEST) {
            //does not exist, probably wrong hash algorithm
            ZimbraLog.store.warn("failed with code %d response: %s", statusCode, post.getResponseBodyAsString());
            return false;
        } else {
            //unexpected condition
            ZimbraLog.store.error("failed with code %d response: %s", statusCode, post.getResponseBodyAsString());
            throw new IOException("unable to SIS create " + statusCode + ":" + post.getStatusText(), null);
        }
    } finally {
        post.releaseConnection();
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) IOException(java.io.IOException)

Example 64 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project zm-mailbox by Zimbra.

the class WebClientServiceUtil method sendUndeployZimletRequestToUiNode.

public static void sendUndeployZimletRequestToUiNode(Server server, String zimlet, String authToken) throws ServiceException {
    if (isServerAtLeast8dot5(server)) {
        HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
        HttpProxyUtil.configureProxy(client);
        PostMethod method = new PostMethod(URLUtil.getServiceURL(server, "/fromservice/undeployzimlet", false));
        method.addRequestHeader(ZimletUtil.PARAM_ZIMLET, zimlet);
        postToUiNode(server, method, authToken);
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 65 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project zm-mailbox by Zimbra.

the class ZimbraServlet method proxyServletRequest.

public static void proxyServletRequest(HttpServletRequest req, HttpServletResponse resp, Server server, String uri, AuthToken authToken) throws IOException, ServiceException {
    if (server == null) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "cannot find remote server");
        return;
    }
    HttpMethod method;
    String url = getProxyUrl(req, server, uri);
    mLog.debug("Proxy URL = %s", url);
    if (req.getMethod().equalsIgnoreCase("GET")) {
        method = new GetMethod(url.toString());
    } else if (req.getMethod().equalsIgnoreCase("POST") || req.getMethod().equalsIgnoreCase("PUT")) {
        PostMethod post = new PostMethod(url.toString());
        post.setRequestEntity(new InputStreamRequestEntity(req.getInputStream()));
        method = post;
    } else {
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "cannot proxy method: " + req.getMethod());
        return;
    }
    HttpState state = new HttpState();
    String hostname = method.getURI().getHost();
    if (authToken != null) {
        authToken.encode(state, false, hostname);
    }
    try {
        proxyServletRequest(req, resp, method, state);
    } finally {
        method.releaseConnection();
    }
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpState(org.apache.commons.httpclient.HttpState) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

PostMethod (org.apache.commons.httpclient.methods.PostMethod)203 HttpClient (org.apache.commons.httpclient.HttpClient)114 Test (org.junit.Test)55 IOException (java.io.IOException)32 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)28 Part (org.apache.commons.httpclient.methods.multipart.Part)25 NameValuePair (org.apache.commons.httpclient.NameValuePair)24 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)23 Map (java.util.Map)21 GetMethod (org.apache.commons.httpclient.methods.GetMethod)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)21 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)21 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 Header (org.apache.commons.httpclient.Header)16 Note (org.apache.zeppelin.notebook.Note)13 HttpException (org.apache.commons.httpclient.HttpException)12 File (java.io.File)11 InputStream (java.io.InputStream)11 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)11 JSONParser (org.json.simple.parser.JSONParser)11