Search in sources :

Example 51 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project sling by apache.

the class SlingIntegrationTestClient method upload.

/** Upload a file to the Sling repository
     *  @return the HTTP status code
     */
public int upload(String toUrl, InputStream is) throws IOException {
    final PutMethod put = new PutMethod(toUrl);
    put.setRequestEntity(new InputStreamRequestEntity(is));
    return httpClient.executeMethod(put);
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Example 52 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project sling by apache.

the class PutMethodServletTest method testPutMethodServletSpecificRT.

public void testPutMethodServletSpecificRT() throws Exception {
    final PutMethod put = new PutMethod(testNodeRT.nodeUrl);
    final int status = httpClient.executeMethod(put);
    assertEquals("PUT to testNodeRT should return 200", 200, status);
    final String content = put.getResponseBodyAsString();
    assertServlet(content, PUT_SERVLET_SUFFIX);
}
Also used : PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Example 53 with PutMethod

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

the class CandlepinTasks method putResourceUsingRESTfulAPI.

public static String putResourceUsingRESTfulAPI(String authenticator, String password, String url, String path, JSONObject 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) {
        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)

Example 54 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project dataverse by IQSS.

the class HttpSendReceiveClientStep method buildMethod.

HttpMethodBase buildMethod(boolean rollback, WorkflowContext ctxt) throws Exception {
    String methodName = params.getOrDefault("method" + (rollback ? "-rollback" : ""), "GET").trim().toUpperCase();
    HttpMethodBase m = null;
    switch(methodName) {
        case "GET":
            m = new GetMethod();
            m.setFollowRedirects(true);
            break;
        case "POST":
            m = new PostMethod();
            break;
        case "PUT":
            m = new PutMethod();
            break;
        case "DELETE":
            m = new DeleteMethod();
            m.setFollowRedirects(true);
            break;
        default:
            throw new IllegalStateException("Unsupported HTTP method: '" + methodName + "'");
    }
    Map<String, String> templateParams = new HashMap<>();
    templateParams.put("invocationId", ctxt.getInvocationId());
    templateParams.put("dataset.id", Long.toString(ctxt.getDataset().getId()));
    templateParams.put("dataset.identifier", ctxt.getDataset().getIdentifier());
    templateParams.put("dataset.globalId", ctxt.getDataset().getGlobalId());
    templateParams.put("dataset.displayName", ctxt.getDataset().getDisplayName());
    templateParams.put("dataset.citation", ctxt.getDataset().getCitation());
    templateParams.put("minorVersion", Long.toString(ctxt.getNextMinorVersionNumber()));
    templateParams.put("majorVersion", Long.toString(ctxt.getNextVersionNumber()));
    templateParams.put("releaseStatus", (ctxt.getType() == TriggerType.PostPublishDataset) ? "done" : "in-progress");
    m.addRequestHeader("Content-Type", params.getOrDefault("contentType", "text/plain"));
    String urlKey = rollback ? "rollbackUrl" : "url";
    String url = params.get(urlKey);
    try {
        m.setURI(new URI(process(url, templateParams), true));
    } catch (URIException ex) {
        throw new IllegalStateException("Illegal URL: '" + url + "'");
    }
    String bodyKey = (rollback ? "rollbackBody" : "body");
    if (params.containsKey(bodyKey) && m instanceof EntityEnclosingMethod) {
        String body = params.get(bodyKey);
        ((EntityEnclosingMethod) m).setRequestEntity(new StringRequestEntity(process(body, templateParams)));
    }
    return m;
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HashMap(java.util.HashMap) EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) URI(org.apache.commons.httpclient.URI) URIException(org.apache.commons.httpclient.URIException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Example 55 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project xwiki-platform by xwiki.

the class ObjectsResourceTest method testPUTObjectUnauthorized.

@Test
public void testPUTObjectUnauthorized() throws Exception {
    final String TAG_VALUE = UUID.randomUUID().toString();
    Object objectToBePut = createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
    GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Object object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    String originalTagValue = getProperty(object, "tags").getValue();
    getProperty(object, "tags").setValue(TAG_VALUE);
    PutMethod putMethod = executePutXml(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, objectToBePut.getClassName(), objectToBePut.getNumber()).toString(), object);
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_UNAUTHORIZED, putMethod.getStatusCode());
    getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(originalTagValue, getProperty(object, "tags").getValue());
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Object(org.xwiki.rest.model.jaxb.Object) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Aggregations

PutMethod (org.apache.commons.httpclient.methods.PutMethod)94 Test (org.junit.Test)49 GetMethod (org.apache.commons.httpclient.methods.GetMethod)29 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)19 Page (org.xwiki.rest.model.jaxb.Page)15 HttpClient (org.apache.commons.httpclient.HttpClient)14 IOException (java.io.IOException)13 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)13 DeleteMethod (org.apache.commons.httpclient.methods.DeleteMethod)12 PostMethod (org.apache.commons.httpclient.methods.PostMethod)10 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)7 DeleteMethod (org.apache.jackrabbit.webdav.client.methods.DeleteMethod)7 Link (org.xwiki.rest.model.jaxb.Link)7 Header (org.apache.commons.httpclient.Header)6 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 FileRequestEntity (org.apache.commons.httpclient.methods.FileRequestEntity)6 File (java.io.File)5 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)5 Object (org.xwiki.rest.model.jaxb.Object)5