Search in sources :

Example 56 with PostMethod

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

the class TestServiceServlet method testDeployAdminExtension.

@Test
public void testDeployAdminExtension() throws Exception {
    deployAdminVersionCheck();
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    HttpClient client = new HttpClient();
    String url = baseURL + "deployzimlet";
    PostMethod method = new PostMethod(url);
    addAuthTokenHeader(method, mbox.getAuthToken().getValue());
    method.addRequestHeader(ZimletUtil.PARAM_ZIMLET, "com_zimbra_phone");
    //standard zimlet, should always be there. No harm in redeploying it
    File zimletFile = new File("/opt/zimbra/zimlets/com_zimbra_adminversioncheck.zip");
    if (zimletFile.exists()) {
        InputStream targetStream = new FileInputStream(zimletFile);
        method.setRequestBody(targetStream);
    }
    int respCode = HttpClientUtil.executeMethod(client, method);
    Assert.assertEquals("Should be getting error code with user's auth token", HttpStatus.SC_UNAUTHORIZED, respCode);
    method = new PostMethod(url);
    addAuthTokenHeader(method, getAdminAuthToken(delegatedAdminWithRights.getName()));
    method.addRequestHeader(ZimletUtil.PARAM_ZIMLET, "com_zimbra_adminversioncheck");
    if (zimletFile.exists()) {
        InputStream targetStream = new FileInputStream(zimletFile);
        method.setRequestBody(targetStream);
    }
    respCode = HttpClientUtil.executeMethod(client, method);
    Assert.assertEquals("Should be getting code 401 with permitted delegated admin's auth token", HttpStatus.SC_UNAUTHORIZED, respCode);
    method = new PostMethod(url);
    addAuthTokenHeader(method, getAdminAuthToken(delegatedAdminWithoutRights.getName()));
    method.addRequestHeader(ZimletUtil.PARAM_ZIMLET, "com_zimbra_adminversioncheck");
    if (zimletFile.exists()) {
        InputStream targetStream = new FileInputStream(zimletFile);
        method.setRequestBody(targetStream);
    }
    respCode = HttpClientUtil.executeMethod(client, method);
    Assert.assertEquals("Should be getting code 401 with unpermitted delegated admin's auth token", HttpStatus.SC_UNAUTHORIZED, respCode);
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) PostMethod(org.apache.commons.httpclient.methods.PostMethod) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 57 with PostMethod

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

the class TestServiceServlet method testDeployZimlet.

@Test
public void testDeployZimlet() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    HttpClient client = new HttpClient();
    String url = baseURL + "deployzimlet";
    PostMethod method = new PostMethod(url);
    addAuthTokenHeader(method, mbox.getAuthToken().getValue());
    method.addRequestHeader(ZimletUtil.PARAM_ZIMLET, "com_zimbra_phone");
    //standard zimlet, should always be there. No harm in redeploying it
    File zimletFile = new File("/opt/zimbra/zimlets/com_zimbra_phone.zip");
    if (zimletFile.exists()) {
        InputStream targetStream = new FileInputStream(zimletFile);
        method.setRequestBody(targetStream);
    }
    int respCode = HttpClientUtil.executeMethod(client, method);
    Assert.assertEquals("Should be getting error code with user's auth token", HttpStatus.SC_UNAUTHORIZED, respCode);
    method = new PostMethod(url);
    addAuthTokenHeader(method, AuthProvider.getAdminAuthToken().getEncoded());
    method.addRequestHeader(ZimletUtil.PARAM_ZIMLET, "com_zimbra_phone");
    if (zimletFile.exists()) {
        InputStream targetStream = new FileInputStream(zimletFile);
        method.setRequestBody(targetStream);
    }
    respCode = HttpClientUtil.executeMethod(client, method);
    Assert.assertEquals("Should be getting code 200 with super admin's auth token", HttpStatus.SC_OK, respCode);
    method = new PostMethod(url);
    addAuthTokenHeader(method, getAdminAuthToken(delegatedAdminWithRights.getName()));
    method.addRequestHeader(ZimletUtil.PARAM_ZIMLET, "com_zimbra_phone");
    if (zimletFile.exists()) {
        InputStream targetStream = new FileInputStream(zimletFile);
        method.setRequestBody(targetStream);
    }
    respCode = HttpClientUtil.executeMethod(client, method);
    Assert.assertEquals("Should be getting code 200 with permitted delegated admin's auth token", HttpStatus.SC_OK, respCode);
    method = new PostMethod(url);
    addAuthTokenHeader(method, getAdminAuthToken(delegatedAdminWithoutRights.getName()));
    method.addRequestHeader(ZimletUtil.PARAM_ZIMLET, "com_zimbra_phone");
    if (zimletFile.exists()) {
        InputStream targetStream = new FileInputStream(zimletFile);
        method.setRequestBody(targetStream);
    }
    respCode = HttpClientUtil.executeMethod(client, method);
    Assert.assertEquals("Should be getting code 401 with unpermitted delegated admin's auth token", HttpStatus.SC_UNAUTHORIZED, respCode);
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) PostMethod(org.apache.commons.httpclient.methods.PostMethod) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 58 with PostMethod

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

the class ExchangeFreeBusyProvider method formAuth.

private boolean formAuth(HttpClient client, ServerInfo info) throws IOException {
    StringBuilder buf = new StringBuilder();
    buf.append("destination=");
    buf.append(URLEncoder.encode(info.url, "UTF-8"));
    buf.append("&username=");
    buf.append(info.authUsername);
    buf.append("&password=");
    buf.append(URLEncoder.encode(info.authPassword, "UTF-8"));
    buf.append("&flags=0");
    buf.append("&SubmitCreds=Log On");
    buf.append("&trusted=0");
    String url = info.url + LC.calendar_exchange_form_auth_url.value();
    PostMethod method = new PostMethod(url);
    ByteArrayRequestEntity re = new ByteArrayRequestEntity(buf.toString().getBytes(), "x-www-form-urlencoded");
    method.setRequestEntity(re);
    HttpState state = new HttpState();
    client.setState(state);
    try {
        int status = HttpClientUtil.executeMethod(client, method);
        if (status >= 400) {
            ZimbraLog.fb.error("form auth to Exchange returned an error: " + status);
            return false;
        }
    } finally {
        method.releaseConnection();
    }
    return true;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpState(org.apache.commons.httpclient.HttpState) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 59 with PostMethod

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

the class TestCalDav method doFreeBusyCheck.

public HttpMethodExecutor doFreeBusyCheck(Account organizer, List<Account> attendees, Date start, Date end) throws ServiceException, IOException {
    HttpClient client = new HttpClient();
    String outboxurl = getSchedulingOutboxUrl(organizer, organizer);
    PostMethod postMethod = new PostMethod(outboxurl);
    postMethod.addRequestHeader("Content-Type", "text/calendar");
    postMethod.addRequestHeader("Originator", "mailto:" + organizer.getName());
    for (Account attendee : attendees) {
        postMethod.addRequestHeader("Recipient", "mailto:" + attendee.getName());
    }
    addBasicAuthHeaderForUser(postMethod, organizer);
    String fbIcal = makeFreeBusyRequestIcal(organizer, attendees, start, end);
    postMethod.setRequestEntity(new ByteArrayRequestEntity(fbIcal.getBytes(), MimeConstants.CT_TEXT_CALENDAR));
    return HttpMethodExecutor.execute(client, postMethod, HttpStatus.SC_OK);
}
Also used : Account(com.zimbra.cs.account.Account) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 60 with PostMethod

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

the class TestCalDav method testPostToSchedulingOutbox.

@Test
public void testPostToSchedulingOutbox() throws Exception {
    Account dav1 = users[1].create();
    Account dav2 = users[2].create();
    Account dav3 = users[3].create();
    String url = getSchedulingOutboxUrl(dav1, dav1);
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(url);
    addBasicAuthHeaderForUser(method, dav1);
    method.addRequestHeader("Content-Type", "text/calendar");
    method.addRequestHeader("Originator", "mailto:" + dav1.getName());
    method.addRequestHeader("Recipient", "mailto:" + dav2.getName());
    method.addRequestHeader("Recipient", "mailto:" + dav3.getName());
    method.setRequestEntity(new ByteArrayRequestEntity(exampleCancelIcal(dav1, dav2, dav3).getBytes(), MimeConstants.CT_TEXT_CALENDAR));
    HttpMethodExecutor.execute(client, method, HttpStatus.SC_OK);
}
Also used : Account(com.zimbra.cs.account.Account) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity) Test(org.junit.Test)

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