Search in sources :

Example 61 with Header

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

the class UserServlet method getRemoteResourceAsUpload.

public static FileUploadServlet.Upload getRemoteResourceAsUpload(AuthToken at, ItemId iid, Map<String, String> params) throws ServiceException, IOException {
    Map<String, String> pcopy = new HashMap<String, String>(params);
    pcopy.put(QP_ID, iid.toString());
    // fetch from remote store
    Provisioning prov = Provisioning.getInstance();
    Account target = prov.get(AccountBy.id, iid.getAccountId(), at);
    String url = getRemoteUrl(target, null, pcopy);
    Pair<Header[], HttpInputStream> response = getRemoteResourceAsStream(at.toZAuthToken(), url);
    // and save the result as an upload
    String ctype = "text/plain", filename = null;
    for (Header hdr : response.getFirst()) {
        String hname = hdr.getName().toLowerCase();
        if (hname.equals("content-type"))
            ctype = hdr.getValue();
        else if (hname.equals("content-disposition"))
            filename = new ContentDisposition(hdr.getValue()).getParameter("filename");
    }
    if (filename == null || filename.equals(""))
        filename = new ContentType(ctype).getParameter("name");
    if (filename == null || filename.equals(""))
        filename = "unknown";
    return FileUploadServlet.saveUpload(response.getSecond(), filename, ctype, at.getAccountId());
}
Also used : Account(com.zimbra.cs.account.Account) Header(org.apache.commons.httpclient.Header) ContentDisposition(com.zimbra.common.mime.ContentDisposition) ContentType(com.zimbra.common.mime.ContentType) HashMap(java.util.HashMap) Provisioning(com.zimbra.cs.account.Provisioning)

Example 62 with Header

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

the class SoapDebugListener method receiveSoapMessage.

@Override
public void receiveSoapMessage(PostMethod postMethod, Element envelope) {
    if (level == Level.OFF) {
        return;
    }
    printer.println();
    printer.println("=== Response ===");
    if (Level.needsHeader(level)) {
        Header[] headers = postMethod.getResponseHeaders();
        for (Header header : headers) {
            // trim the ending crlf
            printer.println(header.toString().trim());
        }
        printer.println();
    }
    if (Level.needsBody(level)) {
        printer.println(envelope.prettyPrint());
    }
}
Also used : Header(org.apache.commons.httpclient.Header)

Example 63 with Header

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

the class SoapDebugListener method sendSoapMessage.

@Override
public void sendSoapMessage(PostMethod postMethod, Element envelope, HttpState httpState) {
    if (level == Level.OFF) {
        return;
    }
    printer.println();
    printer.println("=== Request ===");
    if (Level.needsHeader(level)) {
        try {
            URI uri = postMethod.getURI();
            printer.println(uri.toString());
        } catch (URIException e) {
            e.printStackTrace();
        }
        // headers
        Header[] headers = postMethod.getRequestHeaders();
        for (Header header : headers) {
            // trim the ending crlf
            printer.println(header.toString().trim());
        }
        printer.println();
        //cookies
        if (httpState != null) {
            Cookie[] cookies = httpState.getCookies();
            for (Cookie cookie : cookies) {
                printer.println("Cookie: " + cookie.toString());
            }
        }
        printer.println();
    }
    if (Level.needsBody(level)) {
        printer.println(envelope.prettyPrint());
    }
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) URIException(org.apache.commons.httpclient.URIException) Header(org.apache.commons.httpclient.Header) URI(org.apache.commons.httpclient.URI)

Example 64 with Header

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

the class TestCalDav method testXBusyMacAttach.

@Test
public void testXBusyMacAttach() 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(smallBusyMacAttach.getBytes(), MimeConstants.CT_TEXT_VCARD));
    HttpMethodExecutor exe = HttpMethodExecutor.execute(client, postMethod, HttpStatus.SC_CREATED);
    String location = null;
    for (Header hdr : exe.respHeaders) {
        if ("Location".equals(hdr.getName())) {
            location = hdr.getValue();
        }
    }
    assertNotNull("Location Header returned when creating", location);
    String url = String.format("%s%s", contactsFolderUrl, location.substring(location.lastIndexOf('/') + 1));
    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 = { "\r\nX-BUSYMAC-ATTACH;X-FILENAME=favicon.ico;ENCODING=B:AAABAAEAEBAAAAEAIABoBA\r\n", "\r\n AAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAQAQAABMLAAATCwAAAAAAAAAAAAAAAAAAw4cAY8\r\n", "\r\nX-BUSYMAC-MODIFIED-BY:Gren Elliot\r\n", "\r\nX-CUSTOM:one two three four five six seven eight nine ten eleven twelve t\r\n hirteen fourteen fifteen", "\r\nX-CUSTOM:Here are my simple\\Nmultiline\\Nnotes\r\n", "\r\nX-CUSTOM;TYPE=pref:semi-colon\\;seperated\\;\"stuff\"\\;here\r\n", "\r\nX-CUSTOM:comma\\,\"stuff\"\\,'there'\\,too\r\n", "\r\nX-HOBBY:my backslash\\\\ hobbies\r\n", "\r\nX-CREATED:2015-04-05T09:50:44Z\r\n" };
    for (String expected : expecteds) {
        assertTrue(String.format("GET should contain '%s'\nBODY=%s", expected, respBody), respBody.contains(expected));
    }
    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", 1, hits.size());
}
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) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity) Test(org.junit.Test)

Example 65 with Header

use of org.apache.commons.httpclient.Header 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

Header (org.apache.commons.httpclient.Header)93 GetMethod (org.apache.commons.httpclient.methods.GetMethod)23 Test (org.junit.Test)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 IOException (java.io.IOException)20 HttpClient (org.apache.commons.httpclient.HttpClient)20 NameValuePair (org.apache.commons.httpclient.NameValuePair)18 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 ArrayList (java.util.ArrayList)16 InputStream (java.io.InputStream)14 HttpException (org.apache.commons.httpclient.HttpException)13 PutMethod (org.apache.commons.httpclient.methods.PutMethod)10 Account (com.zimbra.cs.account.Account)9 ServiceException (com.zimbra.common.service.ServiceException)6 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)6 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)6 Pair (com.zimbra.common.util.Pair)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 HashMap (java.util.HashMap)5 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)5