Search in sources :

Example 26 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project zeppelin by apache.

the class CredentialsRestApiTest method testInvalidRequest.

@Test
public void testInvalidRequest() throws IOException {
    String jsonInvalidRequestEntityNull = "{\"entity\" : null, \"username\" : \"test\", \"password\" : \"testpass\"}";
    String jsonInvalidRequestNameNull = "{\"entity\" : \"test\", \"username\" : null, \"password\" : \"testpass\"}";
    String jsonInvalidRequestPasswordNull = "{\"entity\" : \"test\", \"username\" : \"test\", \"password\" : null}";
    String jsonInvalidRequestAllNull = "{\"entity\" : null, \"username\" : null, \"password\" : null}";
    PutMethod entityNullPut = httpPut("/credential", jsonInvalidRequestEntityNull);
    entityNullPut.addRequestHeader("Origin", "http://localhost");
    assertThat(entityNullPut, isBadRequest());
    entityNullPut.releaseConnection();
    PutMethod nameNullPut = httpPut("/credential", jsonInvalidRequestNameNull);
    nameNullPut.addRequestHeader("Origin", "http://localhost");
    assertThat(nameNullPut, isBadRequest());
    nameNullPut.releaseConnection();
    PutMethod passwordNullPut = httpPut("/credential", jsonInvalidRequestPasswordNull);
    passwordNullPut.addRequestHeader("Origin", "http://localhost");
    assertThat(passwordNullPut, isBadRequest());
    passwordNullPut.releaseConnection();
    PutMethod allNullPut = httpPut("/credential", jsonInvalidRequestAllNull);
    allNullPut.addRequestHeader("Origin", "http://localhost");
    assertThat(allNullPut, isBadRequest());
    allNullPut.releaseConnection();
}
Also used : PutMethod(org.apache.commons.httpclient.methods.PutMethod) Test(org.junit.Test)

Example 27 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project pinot by linkedin.

the class WebHdfsV1Client method uploadSegment.

// This method is based on:
// https://hadoop.apache.org/docs/r1.0.4/webhdfs.html#CREATE
public synchronized boolean uploadSegment(String webHdfsPath, String localFilePath) {
    // Step 1: Submit a HTTP PUT request without automatically following
    // redirects and without sending the file data.
    String firstPutReqString = String.format(WEB_HDFS_UPLOAD_PATH_TEMPLATE, _protocol, _host, _port, webHdfsPath, _overwrite, _permission);
    HttpMethod firstPutReq = new PutMethod(firstPutReqString);
    try {
        LOGGER.info("Trying to send request: {}.", firstPutReqString);
        int firstResponseCode = _httpClient.executeMethod(firstPutReq);
        if (firstResponseCode != 307) {
            LOGGER.error(String.format("Failed to execute the first PUT request to upload segment to webhdfs: %s. " + "Expected response code 307, but get %s. Response body: %s", firstPutReqString, firstResponseCode, firstPutReq.getResponseBodyAsString()));
            return false;
        }
    } catch (Exception e) {
        LOGGER.error(String.format("Failed to execute the first request to upload segment to webhdfs: %s.", firstPutReqString), e);
        return false;
    } finally {
        firstPutReq.releaseConnection();
    }
    // Step 2: Submit another HTTP PUT request using the URL in the Location
    // header with the file data to be written.
    String redirectedReqString = firstPutReq.getResponseHeader(LOCATION).getValue();
    PutMethod redirectedReq = new PutMethod(redirectedReqString);
    File localFile = new File(localFilePath);
    RequestEntity requestEntity = new FileRequestEntity(localFile, "application/binary");
    redirectedReq.setRequestEntity(requestEntity);
    try {
        LOGGER.info("Trying to send request: {}.", redirectedReqString);
        int redirectedResponseCode = _httpClient.executeMethod(redirectedReq);
        if (redirectedResponseCode != 201) {
            LOGGER.error(String.format("Failed to execute the redirected PUT request to upload segment to webhdfs: %s. " + "Expected response code 201, but get %s. Response: %s", redirectedReqString, redirectedResponseCode, redirectedReq.getResponseBodyAsString()));
        }
        return true;
    } catch (IOException e) {
        LOGGER.error(String.format("Failed to execute the redirected request to upload segment to webhdfs: %s.", redirectedReqString), e);
        return false;
    } finally {
        redirectedReq.releaseConnection();
    }
}
Also used : FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) PutMethod(org.apache.commons.httpclient.methods.PutMethod) IOException(java.io.IOException) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) File(java.io.File) HttpMethod(org.apache.commons.httpclient.HttpMethod) IOException(java.io.IOException)

Example 28 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project intellij-community by JetBrains.

the class YouTrackIntegrationTest method createIssue.

@NotNull
private String createIssue(@NotNull HttpClient client) throws IOException {
    // http PUT "http://trackers-tests.labs.intellij.net:8067/rest/issue" project==BTYT4TT "summary==First issue created via REST API" 
    final PutMethod method = new PutMethod(myRepository.getUrl() + "/rest/issue");
    method.setQueryString(new NameValuePair[] { new NameValuePair("project", "BTYT4TT"), new NameValuePair("summary", "Test issue for time tracking updates (" + SHORT_TIMESTAMP_FORMAT.format(new Date()) + ")") });
    final int statusCode = client.executeMethod(method);
    assertEquals(HttpStatus.SC_CREATED, statusCode);
    final Header locationHeader = method.getResponseHeader("Location");
    assertNotNull(locationHeader);
    // Otherwise there will be timeout on connection acquiring
    method.releaseConnection();
    return PathUtil.getFileName(locationHeader.getValue());
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) Header(org.apache.commons.httpclient.Header) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Date(java.util.Date) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project intellij-community by JetBrains.

the class PivotalTrackerRepository method doREST.

private HttpMethod doREST(final String request, final HTTPMethod type) throws Exception {
    final HttpClient client = getHttpClient();
    client.getParams().setContentCharset("UTF-8");
    final String uri = getUrl() + request;
    final HttpMethod method = type == HTTPMethod.POST ? new PostMethod(uri) : type == HTTPMethod.PUT ? new PutMethod(uri) : new GetMethod(uri);
    configureHttpMethod(method);
    client.executeMethod(method);
    return method;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 30 with PutMethod

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

the class DavRequest method getHttpMethod.

public HttpMethod getHttpMethod(String baseUrl) {
    String url = mRedirectUrl;
    if (url == null) {
        // schedule-outbox-URL in the same report.
        try {
            // This will throw an exception when the URL is a relative one.
            new URL(mUri);
            url = mUri;
        } catch (MalformedURLException e) {
            url = baseUrl + mUri;
        }
    }
    if (mDoc == null)
        return new GetMethod(url) {

            @Override
            public String getName() {
                return mMethod;
            }
        };
    PutMethod m = new PutMethod(url) {

        RequestEntity re;

        @Override
        public String getName() {
            return mMethod;
        }

        @Override
        protected RequestEntity generateRequestEntity() {
            return re;
        }

        @Override
        public void setRequestEntity(RequestEntity requestEntity) {
            re = requestEntity;
            super.setRequestEntity(requestEntity);
        }
    };
    DocumentRequestEntity re = new DocumentRequestEntity(mDoc);
    m.setRequestEntity(re);
    return m;
}
Also used : MalformedURLException(java.net.MalformedURLException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) URL(java.net.URL)

Aggregations

PutMethod (org.apache.commons.httpclient.methods.PutMethod)37 Test (org.junit.Test)13 HttpClient (org.apache.commons.httpclient.HttpClient)12 Header (org.apache.commons.httpclient.Header)8 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)8 GetMethod (org.apache.commons.httpclient.methods.GetMethod)8 IOException (java.io.IOException)7 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 Map (java.util.Map)5 Note (org.apache.zeppelin.notebook.Note)5 Pair (com.zimbra.common.util.Pair)4 Account (com.zimbra.cs.account.Account)4 HttpException (org.apache.commons.httpclient.HttpException)4 DeleteMethod (org.apache.commons.httpclient.methods.DeleteMethod)4 PostMethod (org.apache.commons.httpclient.methods.PostMethod)4 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)4 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)3 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)3 Paragraph (org.apache.zeppelin.notebook.Paragraph)3 TypeToken (com.google.gson.reflect.TypeToken)2