Search in sources :

Example 31 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project ecf by eclipse.

the class TestBasicAuth method testPostBasicAuthentication.

public void testPostBasicAuthentication() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
    HttpState state = new HttpState();
    AuthScope authscope = new AuthScope(this.server.getLocalAddress(), this.server.getLocalPort(), "test");
    state.setCredentials(authscope, creds);
    this.client.setState(state);
    this.server.setRequestHandler(handlerchain);
    PostMethod post = new PostMethod("/test/");
    post.setRequestEntity(new StringRequestEntity("Test body"));
    try {
        this.client.executeMethod(post);
        assertEquals("Test body", post.getResponseBodyAsString());
    } finally {
        post.releaseConnection();
    }
    assertNotNull(post.getStatusLine());
    assertEquals(HttpStatus.SC_OK, post.getStatusLine().getStatusCode());
    Header auth = post.getRequestHeader("Authorization");
    assertNotNull(auth);
    String expected = "Basic " + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
    assertEquals(expected, auth.getValue());
    AuthState authstate = post.getHostAuthState();
    assertNotNull(authstate.getAuthScheme());
    assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
    assertEquals("test", authstate.getRealm());
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) EchoService(org.apache.commons.httpclient.EchoService) HttpState(org.apache.commons.httpclient.HttpState) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Header(org.apache.commons.httpclient.Header) HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler)

Example 32 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project lobcder by skoulouzis.

the class TestWebWAVFS method testUploadFileOnRootWithoutAdminRole.

@Test
public void testUploadFileOnRootWithoutAdminRole() throws IOException, DavException {
    System.err.println("testUploadFileOnRootWithoutAdminRole");
    String uname = prop.getProperty(("webdav.test.non.admin.username1"), "nonAdmin");
    assertNotNull(uname);
    String pass = prop.getProperty(("webdav.test.non.admin.password1"), "secret");
    assertNotNull(pass);
    HttpClient client = new HttpClient();
    assertNotNull(uri.getHost());
    assertNotNull(uri.getPort());
    assertNotNull(client);
    client.getState().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(uname, pass));
    String testFileURI1 = this.uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
    PutMethod put = new PutMethod(testFileURI1);
    put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
    int status = client.executeMethod(put);
    assertEquals(HttpStatus.SC_UNAUTHORIZED, status);
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.commons.httpclient.HttpClient) AuthScope(org.apache.commons.httpclient.auth.AuthScope) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Test(org.junit.Test)

Example 33 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project lobcder by skoulouzis.

the class Utils method createFile.

public void createFile(String resource, boolean mustSucceed) throws UnsupportedEncodingException, IOException {
    PutMethod put = new PutMethod(resource);
    put.setRequestEntity(new StringRequestEntity("foo", "text/plain", "UTF-8"));
    int status = this.client.executeMethod(put);
    if (mustSucceed) {
        assertEquals(HttpStatus.SC_CREATED, status);
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Example 34 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project lobcder by skoulouzis.

the class WebDAVSecurityTest method testAclMethod.

// @Test
// public void testGetCurrentUserPrivilegeSet() throws UnsupportedEncodingException, IOException, DavException {
// String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
// try {
// 
// DeleteMethod delete = new DeleteMethod(testFileURI1);
// int status = client1.executeMethod(delete);
// 
// 
// PutMethod put = new PutMethod(testFileURI1);
// put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
// status = client1.executeMethod(put);
// assertEquals(HttpStatus.SC_CREATED, status);
// 
// DavPropertyNameSet d = new DavPropertyNameSet();
// DavPropertyName userPriv = DavPropertyName.create("current-user-privilege-set");
// d.add(userPriv);
// 
// PropFindMethod propFind = new PropFindMethod(testFileURI1, d, DavConstants.DEPTH_INFINITY);
// status = client1.executeMethod(propFind);
// assertEquals(HttpStatus.SC_MULTI_STATUS, status);
// 
// 
// MultiStatus multiStatus = propFind.getResponseBodyAsMultiStatus();
// MultiStatusResponse[] responses = multiStatus.getResponses();
// 
// for (MultiStatusResponse r : responses) {
// System.out.println("Responce: " + r.getHref());
// DavPropertySet allProp = getProperties(r);
// 
// DavPropertyIterator iter = allProp.iterator();
// while (iter.hasNext()) {
// DavProperty<?> p = iter.nextProperty();
// System.out.println("\tName: " + p.getName() + " Values " + p.getValue());
// }
// }
// 
// assertEquals(HttpStatus.SC_OK, responses[0].getStatus()[0].getStatusCode());
// DavPropertySet allProp = getProperties(responses[0]);
// DavProperty<?> prop = allProp.get(userPriv);
// assertEquals(userPriv, prop.getName());
// 
// //             ArrayList<org.apache.xerces.dom.DeferredElementNSImpl> value =  (ArrayList<org.apache.xerces.dom.DeferredElementNSImpl>) prop.getValue();
// //             for(org.apache.xerces.dom.DeferredElementNSImpl el : value){
// //                 System.out.println("DeferredElementNSImpl: "+el.getBaseURI());
// //                 System.out.println("getLocalName: "+el.getLocalName());
// //                 System.out.println("getNodeValue: "+el.getNodeValue());
// //             }
// //            String value = (String) prop.getValue();
// //            assertTrue(value.contains("READ") || value.contains("ALL"));
// System.out.println("Name: " + prop.getName() + " Values " + prop.getValue() + " class: " + prop.getValue().getClass().getName() + " " + prop.getValue().getClass().getName());
// } finally {
// DeleteMethod delete = new DeleteMethod(testFileURI1);
// int status = client1.executeMethod(delete);
// assertTrue("DeleteMethod status: " + status, status == HttpStatus.SC_OK || status == HttpStatus.SC_NO_CONTENT);
// }
// }
// 
// @Test
// public void testUnothorizedCreateFile() {
// try {
// String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
// PutMethod put = new PutMethod(testFileURI1);
// put.setRequestEntity(new StringRequestEntity("foo", "text/plain", "UTF-8"));
// 
// client2.getState().setCredentials(
// new AuthScope(uri.getHost(), uri.getPort()),
// new UsernamePasswordCredentials(username2, "WRONG_PASSWORD"));
// 
// int status = client2.executeMethod(put);
// assertEquals(HttpStatus.SC_UNAUTHORIZED, status);
// 
// } catch (IOException ex) {
// Logger.getLogger(WebDAVSecurityTest.class.getName()).log(Level.SEVERE, null, ex);
// }
// }
// 
// @Test
// public void testGetSetACL() throws IOException, DavException {
// String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1;
// DeleteMethod del = new DeleteMethod(testFileURI1);
// client1.executeMethod(del);
// 
// PutMethod put = new PutMethod(testFileURI1);
// put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
// int status = client1.executeMethod(put);
// assertEquals(HttpStatus.SC_CREATED, status);
// 
// DavPropertyNameSet d = new DavPropertyNameSet();
// d.add(SecurityConstants.ACL);
// PropFindMethod propFind = new PropFindMethod(testFileURI1, d, DavConstants.DEPTH_INFINITY);
// 
// status = client1.executeMethod(propFind);
// assertEquals(HttpStatus.SC_MULTI_STATUS, status);
// 
// 
// MultiStatus multiStatus = propFind.getResponseBodyAsMultiStatus();
// MultiStatusResponse[] responses = multiStatus.getResponses();
// 
// for (MultiStatusResponse r : responses) {
// System.out.println("Responce: " + r.getHref());
// DavPropertySet allProp = getProperties(r);
// 
// DavPropertyIterator iter = allProp.iterator();
// while (iter.hasNext()) {
// DavProperty<?> p = iter.nextProperty();
// //                assertNotNull(p.getValue());
// System.out.println("\tName: " + p.getName() + " Values " + p.getValue());
// }
// 
// }
// }
@Test
public void testAclMethod() throws IOException, DavException {
    String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1;
    DeleteMethod del = new DeleteMethod(testFileURI1);
    client1.executeMethod(del);
    PutMethod put = new PutMethod(testFileURI1);
    put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
    int status = client1.executeMethod(put);
    assertEquals(HttpStatus.SC_CREATED, status);
    Privilege[] privs = new Privilege[] { Privilege.PRIVILEGE_WRITE, // /Privilege.getPrivilege("schedule-query-freebusy", SecurityConstants.NAMESPACE)
    Privilege.PRIVILEGE_READ };
    Principal principal = Principal.getHrefPrincipal(username1);
    Ace ace1 = AclProperty.createGrantAce(principal, privs, false, false, null);
    Principal principal2 = Principal.getHrefPrincipal("someUser");
    Ace ace2 = AclProperty.createDenyAce(principal2, privs, true, true, null);
    AclMethod acl = new AclMethod(testFileURI1, new AclProperty(new Ace[] { ace1, ace2 }));
    Header[] headers = acl.getRequestHeaders();
    for (Header h : headers) {
        System.out.println("getRequestHeaders: " + h.getName() + " : " + h.getValue());
    }
    status = client1.executeMethod(acl);
// assertEquals(HttpStatus.SC_OK, status);
// DavPropertyNameSet d = new DavPropertyNameSet();
// d.add(SecurityConstants.ACL);
// //        d.add(SecurityConstants.ACL_RESTRICTIONS);
// //        d.add(SecurityConstants.ALTERNATE_URI_SET);
// //        d.add(SecurityConstants.CURRENT_USER_PRIVILEGE_SET);
// //        d.add(SecurityConstants.GROUP);
// //        d.add(SecurityConstants.GROUP_MEMBERSHIP);
// //        d.add(SecurityConstants.GROUP_MEMBER_SET);
// //        d.add(SecurityConstants.INHERITED_ACL_SET);
// //        d.add(SecurityConstants.OWNER);
// //        d.add(SecurityConstants.PRINCIPAL_COLLECTION_SET);
// //        d.add(SecurityConstants.PRINCIPAL_URL);
// //        d.add(SecurityConstants.SUPPORTED_PRIVILEGE_SET);
// PropFindMethod propFind = new PropFindMethod(testFileURI1, d, DavConstants.DEPTH_INFINITY);
// 
// status = client1.executeMethod(propFind);
// assertEquals(HttpStatus.SC_MULTI_STATUS, status);
// 
// 
// MultiStatus multiStatus = propFind.getResponseBodyAsMultiStatus();
// MultiStatusResponse[] responses = multiStatus.getResponses();
// 
// for (MultiStatusResponse r : responses) {
// System.out.println("Responce: " + r.getHref());
// DavPropertySet allProp = getProperties(r);
// 
// DavPropertyIterator iter = allProp.iterator();
// while (iter.hasNext()) {
// DavProperty<?> p = iter.nextProperty();
// assertNotNull(p.getValue());
// System.out.println("\tName: " + p.getName() + " Values " + p.getValue());
// }
// }
}
Also used : DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) Ace(org.apache.jackrabbit.webdav.security.AclProperty.Ace) AclMethod(org.apache.jackrabbit.webdav.client.methods.AclMethod) Header(org.apache.commons.httpclient.Header) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) Test(org.junit.Test)

Example 35 with StringRequestEntity

use of org.apache.commons.httpclient.methods.StringRequestEntity in project rhsm-qe by RedHatQE.

the class CandlepinTasks method putResourceUsingRESTfulAPI.

public static String putResourceUsingRESTfulAPI(String authenticator, String password, String url, String path, JSONArray jsonData) throws Exception {
    PutMethod put = new PutMethod(url + path);
    if (jsonData != null) {
        put.setRequestEntity(new StringRequestEntity(jsonData.toString(), "application/json", null));
        put.addRequestHeader("accept", "application/json");
        put.addRequestHeader("content-type", "application/json");
    }
    // log the curl alternative to HTTP request
    // Example: curl --insecure --user testuser1:password --request PUT --data '{"autoheal":"false"}' --header 'accept: application/json' --header 'content-type: application/json' https://jsefler-onprem-62candlepin.usersys.redhat.com:8443/candlepin/consumers/e60d7786-1f61-4dec-ad19-bde068dd3c19
    String user = (authenticator.equals("")) ? "" : "--user " + authenticator + ":" + password + " ";
    String request = "--request " + put.getName() + " ";
    String data = (jsonData == null) ? "" : "--data '" + jsonData + "' ";
    String headers = "";
    if (jsonData != null)
        for (org.apache.commons.httpclient.Header header : put.getRequestHeaders()) headers += "--header '" + header.toString().trim() + "' ";
    log.info("SSH alternative to HTTP request: curl --stderr /dev/null --insecure " + user + request + data + headers + put.getURI());
    String response = getHTTPResponseAsString(client, put, authenticator, password);
    if (response != null && response.startsWith("{")) {
        JSONObject responseAsJSONObect = new JSONObject(response);
        if (responseAsJSONObect.has("displayMessage")) {
            log.warning("Attempt to PUT resource '" + path + "' failed: " + responseAsJSONObect.getString("displayMessage"));
        }
    }
    return response;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) JSONObject(org.json.JSONObject) PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Aggregations

StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)102 PostMethod (org.apache.commons.httpclient.methods.PostMethod)63 HttpClient (org.apache.commons.httpclient.HttpClient)33 Test (org.junit.Test)23 PutMethod (org.apache.commons.httpclient.methods.PutMethod)19 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)18 IOException (java.io.IOException)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 AuthRequestHandler (org.apache.commons.httpclient.server.AuthRequestHandler)12 HttpRequestHandlerChain (org.apache.commons.httpclient.server.HttpRequestHandlerChain)12 HttpServiceHandler (org.apache.commons.httpclient.server.HttpServiceHandler)12 Header (org.apache.commons.httpclient.Header)9 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)9 GetMethod (org.apache.commons.httpclient.methods.GetMethod)9 InputStream (java.io.InputStream)8 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)7 StringWriter (java.io.StringWriter)6 Map (java.util.Map)6 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)6 PutMethod (org.apache.jackrabbit.webdav.client.methods.PutMethod)6