Search in sources :

Example 46 with PutMethod

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

the class CredentialsRestApiTest method testPutUserCredentials.

public void testPutUserCredentials(String requestData) throws IOException {
    PutMethod putMethod = httpPut("/credential", requestData);
    putMethod.addRequestHeader("Origin", "http://localhost");
    assertThat(putMethod, isAllowed());
    putMethod.releaseConnection();
}
Also used : PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Example 47 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 48 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 49 with PutMethod

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

the class UserServlet method doHttpOp.

private static Pair<Header[], HttpMethod> doHttpOp(ZAuthToken authToken, HttpMethod method) throws ServiceException {
    // create an HTTP client with the same cookies
    String url = "";
    String hostname = "";
    try {
        url = method.getURI().toString();
        hostname = method.getURI().getHost();
    } catch (IOException e) {
        log.warn("can't parse target URI", e);
    }
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    Map<String, String> cookieMap = authToken.cookieMap(false);
    if (cookieMap != null) {
        HttpState state = new HttpState();
        for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
            state.addCookie(new org.apache.commons.httpclient.Cookie(hostname, ck.getKey(), ck.getValue(), "/", null, false));
        }
        client.setState(state);
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    }
    if (method instanceof PutMethod) {
        long contentLength = ((PutMethod) method).getRequestEntity().getContentLength();
        if (contentLength > 0) {
            // 100kbps in millis
            int timeEstimate = Math.max(10000, (int) (contentLength / 100));
            // cannot set connection time using our ZimbrahttpConnectionManager,
            // see comments in ZimbrahttpConnectionManager.
            // actually, length of the content to Put should not be a factor for
            // establishing a connection, only read time out matter, which we set
            // client.getHttpConnectionManager().getParams().setConnectionTimeout(timeEstimate);
            method.getParams().setSoTimeout(timeEstimate);
        }
    }
    try {
        int statusCode = HttpClientUtil.executeMethod(client, method);
        if (statusCode == HttpStatus.SC_NOT_FOUND || statusCode == HttpStatus.SC_FORBIDDEN)
            throw MailServiceException.NO_SUCH_ITEM(-1);
        else if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED && statusCode != HttpStatus.SC_NO_CONTENT)
            throw ServiceException.RESOURCE_UNREACHABLE(method.getStatusText(), null, new ServiceException.InternalArgument(HTTP_URL, url, ServiceException.Argument.Type.STR), new ServiceException.InternalArgument(HTTP_STATUS_CODE, statusCode, ServiceException.Argument.Type.NUM));
        List<Header> headers = new ArrayList<Header>(Arrays.asList(method.getResponseHeaders()));
        headers.add(new Header("X-Zimbra-Http-Status", "" + statusCode));
        return new Pair<Header[], HttpMethod>(headers.toArray(new Header[0]), method);
    } catch (HttpException e) {
        throw ServiceException.RESOURCE_UNREACHABLE("HttpException while fetching " + url, e);
    } catch (IOException e) {
        throw ServiceException.RESOURCE_UNREACHABLE("IOException while fetching " + url, e);
    }
}
Also used : HttpState(org.apache.commons.httpclient.HttpState) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Header(org.apache.commons.httpclient.Header) HttpClient(org.apache.commons.httpclient.HttpClient) PutMethod(org.apache.commons.httpclient.methods.PutMethod) HttpException(org.apache.commons.httpclient.HttpException) Map(java.util.Map) HashMap(java.util.HashMap) Pair(com.zimbra.common.util.Pair)

Example 50 with PutMethod

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

the class TestCalDav method testAppleStyleGroup.

@Test
public void testAppleStyleGroup() throws ServiceException, IOException {
    Account dav1 = users[1].create();
    String contactsFolderUrl = getFolderUrl(dav1, "Contacts");
    HttpClient client = new HttpClient();
    PostMethod postMethod = new PostMethod(contactsFolderUrl);
    addBasicAuthHeaderForUser(postMethod, dav1);
    postMethod.addRequestHeader("Content-Type", "text/vcard");
    postMethod.setRequestEntity(new ByteArrayRequestEntity(rachelVcard.getBytes(), MimeConstants.CT_TEXT_VCARD));
    HttpMethodExecutor.execute(client, postMethod, HttpStatus.SC_CREATED);
    postMethod = new PostMethod(contactsFolderUrl);
    addBasicAuthHeaderForUser(postMethod, dav1);
    postMethod.addRequestHeader("Content-Type", "text/vcard");
    postMethod.setRequestEntity(new ByteArrayRequestEntity(blueGroupCreate.getBytes(), MimeConstants.CT_TEXT_VCARD));
    HttpMethodExecutor exe = HttpMethodExecutor.execute(client, postMethod, HttpStatus.SC_CREATED);
    String groupLocation = null;
    for (Header hdr : exe.respHeaders) {
        if ("Location".equals(hdr.getName())) {
            groupLocation = hdr.getValue();
        }
    }
    assertNotNull("Location Header returned when creating Group", groupLocation);
    postMethod = new PostMethod(contactsFolderUrl);
    addBasicAuthHeaderForUser(postMethod, dav1);
    postMethod.addRequestHeader("Content-Type", "text/vcard");
    postMethod.setRequestEntity(new ByteArrayRequestEntity(parisVcard.getBytes(), MimeConstants.CT_TEXT_VCARD));
    HttpMethodExecutor.execute(client, postMethod, HttpStatus.SC_CREATED);
    String url = String.format("%s%s", contactsFolderUrl, "F53A6F96-566F-46CC-8D48-A5263FAB5E38.vcf");
    PutMethod putMethod = new PutMethod(url);
    addBasicAuthHeaderForUser(putMethod, dav1);
    putMethod.addRequestHeader("Content-Type", "text/vcard");
    putMethod.setRequestEntity(new ByteArrayRequestEntity(blueGroupModify.getBytes(), MimeConstants.CT_TEXT_VCARD));
    HttpMethodExecutor.execute(client, putMethod, HttpStatus.SC_NO_CONTENT);
    GetMethod getMethod = new GetMethod(url);
    addBasicAuthHeaderForUser(getMethod, dav1);
    getMethod.addRequestHeader("Content-Type", "text/vcard");
    exe = HttpMethodExecutor.execute(client, getMethod, HttpStatus.SC_OK);
    String respBody = new String(exe.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
    String[] expecteds = { "X-ADDRESSBOOKSERVER-KIND:group", "X-ADDRESSBOOKSERVER-MEMBER:urn:uuid:BE43F16D-336E-4C3E-BAE6-22B8F245A986", "X-ADDRESSBOOKSERVER-MEMBER:urn:uuid:07139DE2-EA7B-46CB-A970-C4DF7F72D9AE" };
    for (String expected : expecteds) {
        assertTrue(String.format("GET should contain '%s'\nBODY=%s", expected, respBody), respBody.contains(expected));
    }
    // members are actually stored in a different way.  Make sure it isn't a fluke
    // that the GET response contained the correct members by checking that the members
    // appear where expected in a search hit.
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.setSortBy("dateDesc");
    searchRequest.setLimit(8);
    searchRequest.setSearchTypes("contact");
    searchRequest.setQuery("in:Contacts");
    ZMailbox mbox = users[1].getZMailbox();
    SearchResponse searchResp = mbox.invokeJaxb(searchRequest);
    assertNotNull("JAXB SearchResponse object", searchResp);
    List<SearchHit> hits = searchResp.getSearchHits();
    assertNotNull("JAXB SearchResponse hits", hits);
    assertEquals("JAXB SearchResponse hits", 3, hits.size());
    boolean seenGroup = false;
    for (SearchHit hit : hits) {
        ContactInfo contactInfo = (ContactInfo) hit;
        if ("BlueGroup".equals(contactInfo.getFileAs())) {
            seenGroup = true;
            assertEquals("Number of members of group in search hit", 2, contactInfo.getContactGroupMembers().size());
        }
        ZimbraLog.test.info("Hit %s class=%s", hit, hit.getClass().getName());
    }
    assertTrue("Seen group", seenGroup);
}
Also used : Account(com.zimbra.cs.account.Account) SearchRequest(com.zimbra.soap.mail.message.SearchRequest) SearchHit(com.zimbra.soap.type.SearchHit) PostMethod(org.apache.commons.httpclient.methods.PostMethod) SearchResponse(com.zimbra.soap.mail.message.SearchResponse) ZMailbox(com.zimbra.client.ZMailbox) Header(org.apache.commons.httpclient.Header) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) ContactInfo(com.zimbra.soap.mail.type.ContactInfo) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity) Test(org.junit.Test)

Aggregations

PutMethod (org.apache.commons.httpclient.methods.PutMethod)94 Test (org.junit.Test)49 GetMethod (org.apache.commons.httpclient.methods.GetMethod)29 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)19 Page (org.xwiki.rest.model.jaxb.Page)15 HttpClient (org.apache.commons.httpclient.HttpClient)14 IOException (java.io.IOException)13 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)13 DeleteMethod (org.apache.commons.httpclient.methods.DeleteMethod)12 PostMethod (org.apache.commons.httpclient.methods.PostMethod)10 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)7 DeleteMethod (org.apache.jackrabbit.webdav.client.methods.DeleteMethod)7 Link (org.xwiki.rest.model.jaxb.Link)7 Header (org.apache.commons.httpclient.Header)6 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 FileRequestEntity (org.apache.commons.httpclient.methods.FileRequestEntity)6 File (java.io.File)5 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)5 Object (org.xwiki.rest.model.jaxb.Object)5