Search in sources :

Example 41 with HttpMethod

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

the class UserServlet method putRemoteResource.

public static Pair<Header[], HttpInputStream> putRemoteResource(ZAuthToken authToken, String url, InputStream req, Header[] headers) throws ServiceException, IOException {
    StringBuilder u = new StringBuilder(url);
    u.append("?").append(QP_AUTH).append('=').append(AUTH_COOKIE);
    PutMethod method = new PutMethod(u.toString());
    String contentType = "application/octet-stream";
    if (headers != null) {
        for (Header hdr : headers) {
            String name = hdr.getName();
            method.addRequestHeader(hdr);
            if (name.equals("Content-Type"))
                contentType = hdr.getValue();
        }
    }
    method.setRequestEntity(new InputStreamRequestEntity(req, contentType));
    Pair<Header[], HttpMethod> pair = doHttpOp(authToken, method);
    return new Pair<Header[], HttpInputStream>(pair.getFirst(), new HttpInputStream(pair.getSecond()));
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) Header(org.apache.commons.httpclient.Header) PutMethod(org.apache.commons.httpclient.methods.PutMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod) Pair(com.zimbra.common.util.Pair)

Example 42 with HttpMethod

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

the class UserServlet method getRemoteResource.

public static Pair<Header[], byte[]> getRemoteResource(ZAuthToken authToken, String url) throws ServiceException {
    HttpMethod get = null;
    try {
        Pair<Header[], HttpMethod> pair = doHttpOp(authToken, new GetMethod(url));
        get = pair.getSecond();
        return new Pair<Header[], byte[]>(pair.getFirst(), get.getResponseBody());
    } catch (IOException x) {
        throw ServiceException.FAILURE("Can't read response body " + url, x);
    } finally {
        if (get != null) {
            get.releaseConnection();
        }
    }
}
Also used : Header(org.apache.commons.httpclient.Header) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod) Pair(com.zimbra.common.util.Pair)

Example 43 with HttpMethod

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

Example 44 with HttpMethod

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

the class TestAccessKeyGrant method testCalendarGet_guest.

/*
     * use zmmailbox to grant guest access:
     * zmmailbox -z -m user1@phoebe.mac mfg Calendar guest g1@guest.com zzz r
     */
public void testCalendarGet_guest() throws Exception {
    HttpState initialState = new HttpState();
    /*
        Cookie authCookie = new Cookie(restURL.getURL().getHost(), "ZM_AUTH_TOKEN", mAuthToken, "/", null, false);
        Cookie sessionCookie = new Cookie(restURL.getURL().getHost(), "JSESSIONID", mSessionId, "/zimbra", null, false);
        initialState.addCookie(authCookie);
        initialState.addCookie(sessionCookie);
        */
    String guestName = "g1@guest.com";
    String guestPassword = "zzz";
    Credentials loginCredentials = new UsernamePasswordCredentials(guestName, guestPassword);
    initialState.setCredentials(AuthScope.ANY, loginCredentials);
    HttpClient client = new HttpClient();
    client.setState(initialState);
    String url = getRestCalendarUrl(OWNER_NAME);
    System.out.println("REST URL: " + url);
    HttpMethod method = new GetMethod(url);
    executeHttpMethod(client, method);
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) HttpMethod(org.apache.commons.httpclient.HttpMethod) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 45 with HttpMethod

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

the class TestAccessKeyGrant method disable_testCalendarGet_Yahoo_accesskey.

/*
     * setup:
     *
     * 1. use zmmailbox to grant key access: zmmailbox -z -d -r soap12
     *        -d: debug, so it shows soap trace
     *        -z: default admin auth
     *        -r soap12: use soap12 protocol, it's easier to read than json
     *
     *    mbox> sm user1
     *    mbox user1@phoebe.mac> mfg Calendar key k1@key.com r
     *
     *    grab the access key from the FolderActionResponse:
     *        <FolderActionResponse xmlns="urn:zimbraMail">
     *            <action d="k1@key.com" key="3c4877ed3948511cee39379debbf968d" op="grant" zid="k1@key.com" id="10"/>
     *        </FolderActionResponse>
     *
     * 2. paste the access key to the test (TODO, automate it)
     *
     * 3. In com.zimbra.cs.service.AuthProvider, uncomment // register(new com.zimbra.qa.unittest.TestAccessKeyGrant.DummyAuthProvider());
     *
     * 4. ant deploy-war
     *
     * 5. modify localconfig.xml, add:
     *    <key name="zimbra_auth_provider">
     *        <value>DUMMY_AUTH_PROVIDER</value>
     *    </key>
     *
     * 6. retstart server
     *
     * ready to run the test
     */
public void disable_testCalendarGet_Yahoo_accesskey() throws Exception {
    HttpClient client = new HttpClient();
    String accessKey = "3c4877ed3948511cee39379debbf968d-bogus";
    String url = getRestCalendarUrl(OWNER_NAME);
    /*
         * the Yahoo accesskey URL is:
         * /home/yid/Calendar/Folder.ics?k=accesskey&h=yid
         */
    url = url + "?k=" + accessKey + "&h=" + getAccountId(OWNER_NAME);
    System.out.println("REST URL: " + url);
    HttpMethod method = new GetMethod(url);
    executeHttpMethod(client, method);
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

HttpMethod (org.apache.commons.httpclient.HttpMethod)151 HttpClient (org.apache.commons.httpclient.HttpClient)99 GetMethod (org.apache.commons.httpclient.methods.GetMethod)95 InputStream (java.io.InputStream)61 IOException (java.io.IOException)43 ArrayList (java.util.ArrayList)30 HttpException (org.apache.commons.httpclient.HttpException)28 Map (java.util.Map)24 Test (org.junit.Test)23 Element (org.w3c.dom.Element)22 HashMap (java.util.HashMap)20 PostMethod (org.apache.commons.httpclient.methods.PostMethod)19 Header (org.apache.commons.httpclient.Header)17 List (java.util.List)14 NameValuePair (org.apache.commons.httpclient.NameValuePair)13 NodeList (org.w3c.dom.NodeList)12 FileInputStream (java.io.FileInputStream)10 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)10 SAXBuilder (org.jdom.input.SAXBuilder)10 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)9