Search in sources :

Example 11 with NameValuePair

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

the class TestPreAuthServlet method testShouldNotAllowPreAuthGetCookieReuse.

public void testShouldNotAllowPreAuthGetCookieReuse() throws Exception {
    Account account = TestUtil.getAccount("user1");
    AuthToken authToken = new ZimbraAuthToken(account);
    System.out.println(authToken.isRegistered());
    HttpClient client = new HttpClient();
    Server localServer = Provisioning.getInstance().getLocalServer();
    String protoHostPort = "http://localhost:" + localServer.getIntAttr(Provisioning.A_zimbraMailPort, 0);
    String url = protoHostPort + PRE_AUTH_URL;
    //allow first request
    HttpMethod method = new GetMethod(url);
    NameValuePair[] queryStringPairArray = new NameValuePair[] { new NameValuePair("isredirect", "1"), new NameValuePair("authtoken", authToken.getEncoded()) };
    method.setQueryString(queryStringPairArray);
    int respCode = HttpClientUtil.executeMethod(client, method);
    //reject second request
    method = new GetMethod(url);
    method.setQueryString(queryStringPairArray);
    respCode = HttpClientUtil.executeMethod(client, method);
    Assert.assertEquals(400, respCode);
}
Also used : Account(com.zimbra.cs.account.Account) NameValuePair(org.apache.commons.httpclient.NameValuePair) Server(com.zimbra.cs.account.Server) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) AuthToken(com.zimbra.cs.account.AuthToken) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 12 with NameValuePair

use of org.apache.commons.httpclient.NameValuePair in project cloudstack by apache.

the class BigSwitchBcfApi method executeRetrieveObject.

@SuppressWarnings("unchecked")
protected <T> T executeRetrieveObject(final Type returnObjectType, final String uri, final Map<String, String> parameters) throws BigSwitchBcfApiException {
    checkInvariants();
    GetMethod gm = (GetMethod) createMethod("get", uri, _port);
    setHttpHeader(gm);
    if (parameters != null && !parameters.isEmpty()) {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(parameters.size());
        for (Entry<String, String> e : parameters.entrySet()) {
            nameValuePairs.add(new NameValuePair(e.getKey(), e.getValue()));
        }
        gm.setQueryString(nameValuePairs.toArray(new NameValuePair[0]));
    }
    executeMethod(gm);
    String hash = checkResponse(gm, "BigSwitch HTTP get failed: ");
    T returnValue;
    try {
        // CAUTIOUS: Safety margin of 2048 characters - extend if needed.
        returnValue = (T) gson.fromJson(gm.getResponseBodyAsString(2048), returnObjectType);
    } catch (IOException e) {
        S_LOGGER.error("IOException while retrieving response body", e);
        throw new BigSwitchBcfApiException(e);
    } finally {
        gm.releaseConnection();
    }
    if (returnValue instanceof ControlClusterStatus) {
        if (HASH_CONFLICT.equals(hash)) {
            isMaster = true;
            ((ControlClusterStatus) returnValue).setTopologySyncRequested(true);
        } else if (!HASH_IGNORE.equals(hash) && !isMaster) {
            isMaster = true;
            ((ControlClusterStatus) returnValue).setTopologySyncRequested(true);
        }
    }
    return returnValue;
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) GetMethod(org.apache.commons.httpclient.methods.GetMethod) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 13 with NameValuePair

use of org.apache.commons.httpclient.NameValuePair in project sling by apache.

the class HttpTestBase method getContent.

/** retrieve the contents of given URL and assert its content type
     * @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
     * @param httpMethod supports just GET and POST methods
     * @throws IOException
     * @throws HttpException */
public String getContent(String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode, String httpMethod) throws IOException {
    HttpMethodBase method = null;
    if (HTTP_METHOD_GET.equals(httpMethod)) {
        method = new GetMethod(url);
    } else if (HTTP_METHOD_POST.equals(httpMethod)) {
        method = new PostMethod(url);
    } else {
        fail("Http Method not supported in this test suite, method: " + httpMethod);
    }
    if (params != null) {
        final NameValuePair[] nvp = new NameValuePair[0];
        method.setQueryString(params.toArray(nvp));
    }
    final int status = httpClient.executeMethod(method);
    final String content = getResponseBodyAsStream(method, 0);
    assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status);
    final Header h = method.getResponseHeader("Content-Type");
    if (expectedContentType == null) {
        if (h != null) {
            fail("Expected null Content-Type, got " + h.getValue());
        }
    } else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
    // no check
    } else if (h == null) {
        fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url);
    } else {
        assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType));
    }
    return content.toString();
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 14 with NameValuePair

use of org.apache.commons.httpclient.NameValuePair in project sling by apache.

the class SelectorAuthenticationResponseCodeTest method assertPostStatus.

// TODO - move this method into commons.testing
protected HttpMethod assertPostStatus(String url, int expectedStatusCode, List<NameValuePair> postParams, List<Header> headers, String assertMessage) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    if (headers != null) {
        for (Header header : headers) {
            post.addRequestHeader(header);
        }
    }
    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }
    final int status = H.getHttpClient().executeMethod(post);
    if (assertMessage == null) {
        assertEquals(expectedStatusCode, status);
    } else {
        assertEquals(assertMessage, expectedStatusCode, status);
    }
    return post;
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod)

Example 15 with NameValuePair

use of org.apache.commons.httpclient.NameValuePair in project sling by apache.

the class AccessPrivilegesInfoTest method testSLING_1090.

/**
	 * Test the fix for SLING-1090
	 */
@Test
public void testSLING_1090() throws Exception {
    testUserId = H.createTestUser();
    //grant jcr: removeChildNodes to the root node
    ArrayList<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:removeChildNodes", "granted"));
    Credentials adminCreds = new UsernamePasswordCredentials("admin", "admin");
    H.assertAuthenticatedPostStatus(adminCreds, HttpTest.HTTP_BASE_URL + "/.modifyAce.html", HttpServletResponse.SC_OK, postParams, null);
    //create a node as a child of the root folder
    testFolderUrl = H.getTestClient().createNode(HttpTest.HTTP_BASE_URL + "/testFolder" + random.nextInt() + SlingPostConstants.DEFAULT_CREATE_SUFFIX, null);
    String postUrl = testFolderUrl + ".modifyAce.html";
    //grant jcr:removeNode to the test node
    postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:removeNode", "granted"));
    H.assertAuthenticatedPostStatus(adminCreds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    //fetch the JSON for the test page to verify the settings.
    String getUrl = testFolderUrl + ".privileges-info.json";
    Credentials testUserCreds = new UsernamePasswordCredentials(testUserId, "testPwd");
    String json = H.getAuthenticatedContent(testUserCreds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObj = JsonUtil.parseObject(json);
    assertEquals(true, jsonObj.getBoolean("canDelete"));
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Aggregations

NameValuePair (org.apache.commons.httpclient.NameValuePair)162 ArrayList (java.util.ArrayList)109 Credentials (org.apache.commons.httpclient.Credentials)64 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)64 Test (org.junit.Test)55 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)49 JsonObject (javax.json.JsonObject)43 HashMap (java.util.HashMap)24 PostMethod (org.apache.commons.httpclient.methods.PostMethod)24 JsonArray (javax.json.JsonArray)20 Header (org.apache.commons.httpclient.Header)16 HashSet (java.util.HashSet)14 HttpClient (org.apache.commons.httpclient.HttpClient)13 HttpMethod (org.apache.commons.httpclient.HttpMethod)13 GetMethod (org.apache.commons.httpclient.methods.GetMethod)12 IOException (java.io.IOException)8 LinkedList (java.util.LinkedList)8 InputStream (java.io.InputStream)7 URL (java.net.URL)6 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)5