Search in sources :

Example 46 with UsernamePasswordCredentials

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

the class GetNodeCommand method execute.

@Override
public Result<byte[]> execute() {
    GetMethod get = new GetMethod(getPath());
    try {
        httpClient.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword());
        //TODO
        httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(), repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds);
        int responseStatus = httpClient.executeMethod(get);
        if (isSuccessStatus(responseStatus))
            return AbstractResult.success(get.getResponseBody());
        return failureResultForStatusCode(responseStatus);
    } catch (Exception e) {
        return AbstractResult.failure(new RepositoryException(e));
    } finally {
        get.releaseConnection();
    }
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) RepositoryException(org.apache.sling.ide.transport.RepositoryException) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 47 with UsernamePasswordCredentials

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

the class ListChildrenCommand method execute.

@Override
public Result<ResourceProxy> execute() {
    GetMethod get = new GetMethod(getPath());
    try {
        httpClient.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword());
        httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(), repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds);
        int responseStatus = httpClient.executeMethod(get);
        //return EncodingUtil.getString(rawdata, m.getResponseCharSet());
        if (!isSuccessStatus(responseStatus))
            return failureResultForStatusCode(responseStatus);
        ResourceProxy resource = new ResourceProxy(path);
        Gson gson = new Gson();
        try (JsonReader jsonReader = new JsonReader(new InputStreamReader(get.getResponseBodyAsStream(), get.getResponseCharSet()))) {
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                String name = jsonReader.nextName();
                JsonToken token = jsonReader.peek();
                if (token == JsonToken.BEGIN_OBJECT) {
                    ResourceProxy child = new ResourceProxy(PathUtil.join(path, name));
                    ResourceWithPrimaryType resourceWithPrimaryType = gson.fromJson(jsonReader, ResourceWithPrimaryType.class);
                    // evaluate its jcr:primaryType as well!
                    child.addProperty(Repository.JCR_PRIMARY_TYPE, resourceWithPrimaryType.getPrimaryType());
                    resource.addChild(child);
                } else if (token == JsonToken.STRING) {
                    if (Repository.JCR_PRIMARY_TYPE.equals(name)) {
                        String primaryType = jsonReader.nextString();
                        if (primaryType != null) {
                            // TODO - needed?
                            resource.addProperty(Repository.JCR_PRIMARY_TYPE, primaryType);
                        }
                    }
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
        }
        return AbstractResult.success(resource);
    } catch (Exception e) {
        return AbstractResult.failure(new RepositoryException(e));
    } finally {
        get.releaseConnection();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) RepositoryException(org.apache.sling.ide.transport.RepositoryException) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

Example 48 with UsernamePasswordCredentials

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

the class ModifyAceTest method testAddAceOrderByNumeric.

/**
	 * Test to verify adding an ACE at a specific index inside 
	 * the ACL
	 */
@Test
public void testAddAceOrderByNumeric() throws IOException, JsonException {
    createAceOrderTestFolderWithOneAce();
    testGroupId = H.createTestGroup();
    addOrUpdateAce(testFolderUrl, testGroupId, true, "0");
    //fetch the JSON for the acl to verify the settings.
    String getUrl = testFolderUrl + ".acl.json";
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    assertEquals(2, jsonObject.size());
    JsonObject group = jsonObject.getJsonObject(testGroupId);
    assertNotNull(group);
    assertEquals(testGroupId, group.getString("principal"));
    assertEquals(0, group.getInt("order"));
    JsonObject user = jsonObject.getJsonObject(testUserId);
    assertNotNull(user);
    assertEquals(testUserId, user.getString("principal"));
    assertEquals(1, user.getInt("order"));
    //add another principal between the testGroupId and testUserId
    testUserId2 = H.createTestUser();
    addOrUpdateAce(testFolderUrl, testUserId2, true, "1");
    String json2 = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json2);
    JsonObject jsonObject2 = JsonUtil.parseObject(json2);
    assertEquals(3, jsonObject2.size());
    JsonObject group2 = jsonObject2.getJsonObject(testGroupId);
    assertNotNull(group2);
    assertEquals(testGroupId, group2.getString("principal"));
    assertEquals(0, group2.getInt("order"));
    JsonObject user3 = jsonObject2.getJsonObject(testUserId2);
    assertNotNull(user3);
    assertEquals(testUserId2, user3.getString("principal"));
    assertEquals(1, user3.getInt("order"));
    JsonObject user2 = jsonObject2.getJsonObject(testUserId);
    assertNotNull(user2);
    assertEquals(testUserId, user2.getString("principal"));
    assertEquals(2, user2.getInt("order"));
}
Also used : 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)

Example 49 with UsernamePasswordCredentials

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

the class ModifyAceTest method testUpdateAcePreservePosition.

/**
	 * Test to make sure modifying an existing ace without changing the order 
	 * leaves the ACE in the same position in the ACL
	 */
@Test
public void testUpdateAcePreservePosition() throws IOException, JsonException {
    createAceOrderTestFolderWithOneAce();
    testGroupId = H.createTestGroup();
    addOrUpdateAce(testFolderUrl, testGroupId, true, "first");
    //update the ace to make sure the update does not change the ACE order
    addOrUpdateAce(testFolderUrl, testGroupId, false, null);
    //fetch the JSON for the acl to verify the settings.
    String getUrl = testFolderUrl + ".acl.json";
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    assertEquals(2, jsonObject.size());
    JsonObject group = jsonObject.getJsonObject(testGroupId);
    assertNotNull(group);
    assertEquals(testGroupId, group.getString("principal"));
    assertEquals(0, group.getInt("order"));
    JsonObject user = jsonObject.getJsonObject(testUserId);
    assertNotNull(user);
    assertEquals(testUserId, user.getString("principal"));
    assertEquals(1, user.getInt("order"));
}
Also used : 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)

Example 50 with UsernamePasswordCredentials

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

the class ModifyAceTest method addOrUpdateAce.

/**
	 * Helper to add or update an ace for testing
	 */
private void addOrUpdateAce(String folderUrl, String principalId, boolean readGranted, String order) throws IOException, JsonException {
    String postUrl = folderUrl + ".modifyAce.html";
    //1. create an initial set of privileges
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", principalId));
    postParams.add(new NameValuePair("privilege@jcr:read", readGranted ? "granted" : "denied"));
    postParams.add(new NameValuePair("privilege@jcr:write", "denied"));
    if (order != null) {
        postParams.add(new NameValuePair("order", order));
    }
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Aggregations

UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)113 Credentials (org.apache.commons.httpclient.Credentials)97 ArrayList (java.util.ArrayList)65 NameValuePair (org.apache.commons.httpclient.NameValuePair)61 JsonObject (javax.json.JsonObject)52 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)51 Test (org.junit.Test)51 JsonArray (javax.json.JsonArray)19 HttpClient (org.apache.commons.httpclient.HttpClient)19 AuthScope (org.apache.commons.httpclient.auth.AuthScope)17 HashSet (java.util.HashSet)14 GetMethod (org.apache.commons.httpclient.methods.GetMethod)14 HttpException (org.apache.commons.httpclient.HttpException)9 IOException (java.io.IOException)8 HttpMethod (org.apache.commons.httpclient.HttpMethod)8 HttpState (org.apache.commons.httpclient.HttpState)6 PostMethod (org.apache.commons.httpclient.methods.PostMethod)6 URL (java.net.URL)5 RepositoryException (org.apache.sling.ide.transport.RepositoryException)5 Header (org.apache.commons.httpclient.Header)4