Example usage for org.apache.commons.httpclient HttpStatus SC_ACCEPTED

List of usage examples for org.apache.commons.httpclient HttpStatus SC_ACCEPTED

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_ACCEPTED.

Prototype

int SC_ACCEPTED

To view the source code for org.apache.commons.httpclient HttpStatus SC_ACCEPTED.

Click Source Link

Document

<tt>202 Accepted</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:org.xwiki.test.rest.AttachmentsResourceTest.java

@Test
public void testGETAttachmentVersions() throws Exception {
    final int NUMBER_OF_VERSIONS = 4;
    String attachmentName = String.format("%s.txt", UUID.randomUUID().toString());

    Map<String, String> versionToContentMap = new HashMap<String, String>();

    /* Create NUMBER_OF_ATTACHMENTS attachments */
    for (int i = 0; i < NUMBER_OF_VERSIONS; i++) {
        String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName);
        String content = String.format("CONTENT %d", i);
        PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN,
                TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
        if (i == 0) {
            Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());
        } else {/*from   w  w w. java  2  s .c  o  m*/
            Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED,
                    putMethod.getStatusCode());
        }

        Attachment attachment = (Attachment) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());

        versionToContentMap.put(attachment.getVersion(), content);
    }

    String attachmentsUri = buildURIForThisPage(AttachmentHistoryResource.class, attachmentName);
    GetMethod getMethod = executeGet(attachmentsUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Attachments attachments = (Attachments) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(NUMBER_OF_VERSIONS, attachments.getAttachments().size());

    for (Attachment attachment : attachments.getAttachments()) {
        getMethod = executeGet(getFirstLinkByRelation(attachment, Relations.ATTACHMENT_DATA).getHref());
        Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

        Assert.assertEquals(versionToContentMap.get(attachment.getVersion()),
                getMethod.getResponseBodyAsString());
    }
}

From source file:org.xwiki.test.rest.framework.AbstractHttpTest.java

protected int setPageContent(String wikiName, List<String> spaceName, String pageName, String content)
        throws Exception {
    String uri = buildURI(PageResource.class, wikiName, spaceName, pageName).toString();

    PutMethod putMethod = executePut(uri, content, javax.ws.rs.core.MediaType.TEXT_PLAIN,
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());

    int code = putMethod.getStatusCode();
    Assert.assertTrue(String.format("Failed to set page content, %s", getHttpMethodInfo(putMethod)),
            code == HttpStatus.SC_ACCEPTED || code == HttpStatus.SC_CREATED);

    return code;//from   w  w w. j  a va 2s  .c o  m
}

From source file:org.xwiki.test.rest.ObjectsResourceTest.java

@Test
public void testPUTObject() throws Exception {
    final String TAG_VALUE = UUID.randomUUID().toString();

    Object objectToBePut = createObjectIfDoesNotExists("XWiki.TagClass", MAIN_SPACE, "WebHome");

    GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), MAIN_SPACE, "WebHome",
            objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Object objectSummary = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    getProperty(objectSummary, "tags").setValue(TAG_VALUE);

    PutMethod putMethod = executePutXml(
            buildURI(ObjectResource.class, getWiki(), MAIN_SPACE, "WebHome", objectToBePut.getClassName(),
                    objectToBePut.getNumber()).toString(),
            objectSummary, TestUtils.ADMIN_CREDENTIALS.getUserName(),
            TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());

    Object updatedObjectSummary = (Object) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());

    Assert.assertEquals(TAG_VALUE, getProperty(updatedObjectSummary, "tags").getValue());
    Assert.assertEquals(objectSummary.getClassName(), updatedObjectSummary.getClassName());
    Assert.assertEquals(objectSummary.getNumber(), updatedObjectSummary.getNumber());
}

From source file:org.xwiki.test.rest.ObjectsResourceTest.java

@Test
public void testPUTProperty() throws Exception {
    final String TAG_VALUE = UUID.randomUUID().toString();

    /* Make sure that an Object with the TagClass exists. */
    createObjectIfDoesNotExists("XWiki.TagClass", MAIN_SPACE, "WebHome");

    GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), MAIN_SPACE, "WebHome").toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
    Assert.assertNotNull(link);/*  w  ww  .j  ava2  s  . com*/

    getMethod = executeGet(link.getHref());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertFalse(objects.getObjectSummaries().isEmpty());

    Object currentObject = null;

    for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
        if (objectSummary.getClassName().equals("XWiki.TagClass")) {
            link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
            Assert.assertNotNull(link);
            getMethod = executeGet(link.getHref());
            Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

            currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
            break;
        }
    }

    Assert.assertNotNull(currentObject);

    Property tagsProperty = getProperty(currentObject, "tags");

    Assert.assertNotNull(tagsProperty);

    Link tagsPropertyLink = getFirstLinkByRelation(tagsProperty, Relations.SELF);

    Assert.assertNotNull(tagsPropertyLink);

    Property newTags = objectFactory.createProperty();
    newTags.setValue(TAG_VALUE);

    PutMethod putMethod = executePutXml(tagsPropertyLink.getHref(), newTags,
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());

    getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), MAIN_SPACE, "WebHome",
            currentObject.getClassName(), currentObject.getNumber()).toString());
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());

    currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    tagsProperty = getProperty(currentObject, "tags");

    Assert.assertEquals(TAG_VALUE, tagsProperty.getValue());
}

From source file:org.xwiki.test.rest.ObjectsResourceTest.java

@Test
public void testPUTPropertyWithTextPlain() throws Exception {
    final String TAG_VALUE = UUID.randomUUID().toString();

    /* Make sure that an Object with the TagClass exists. */
    createObjectIfDoesNotExists("XWiki.TagClass", MAIN_SPACE, "WebHome");

    GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), MAIN_SPACE, "WebHome").toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
    Assert.assertNotNull(link);//from ww w. j a  v  a2  s. c om

    getMethod = executeGet(link.getHref());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertFalse(objects.getObjectSummaries().isEmpty());

    Object currentObject = null;

    for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
        if (objectSummary.getClassName().equals("XWiki.TagClass")) {
            link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
            Assert.assertNotNull(link);
            getMethod = executeGet(link.getHref());
            Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

            currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
            break;
        }
    }

    Assert.assertNotNull(currentObject);

    Property tagsProperty = getProperty(currentObject, "tags");

    Assert.assertNotNull(tagsProperty);

    Link tagsPropertyLink = getFirstLinkByRelation(tagsProperty, Relations.SELF);

    Assert.assertNotNull(tagsPropertyLink);

    PutMethod putMethod = executePut(tagsPropertyLink.getHref(), TAG_VALUE, MediaType.TEXT_PLAIN,
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());

    getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), MAIN_SPACE, "WebHome",
            currentObject.getClassName(), currentObject.getNumber()).toString());
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());

    currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    tagsProperty = getProperty(currentObject, "tags");

    Assert.assertEquals(TAG_VALUE, tagsProperty.getValue());
}

From source file:org.xwiki.test.rest.ObjectsResourceTest.java

@Test
public void testPUTObjectFormUrlEncoded() throws Exception {
    final String TAG_VALUE = UUID.randomUUID().toString();

    Object objectToBePut = createObjectIfDoesNotExists("XWiki.TagClass", MAIN_SPACE, "WebHome");

    GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), MAIN_SPACE, "WebHome",
            objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Object object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    NameValuePair[] nameValuePairs = new NameValuePair[1];
    nameValuePairs[0] = new NameValuePair("property#tags", TAG_VALUE);

    PostMethod postMethod = executePostForm(
            String.format("%s?method=PUT",
                    buildURI(ObjectResource.class, getWiki(), MAIN_SPACE, "WebHome",
                            objectToBePut.getClassName(), objectToBePut.getNumber()).toString()),
            nameValuePairs, TestUtils.ADMIN_CREDENTIALS.getUserName(),
            TestUtils.ADMIN_CREDENTIALS.getPassword());

    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_ACCEPTED, postMethod.getStatusCode());

    Object updatedObjectSummary = (Object) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());

    Assert.assertEquals(TAG_VALUE, getProperty(updatedObjectSummary, "tags").getValue());
    Assert.assertEquals(object.getClassName(), updatedObjectSummary.getClassName());
    Assert.assertEquals(object.getNumber(), updatedObjectSummary.getNumber());
}

From source file:org.xwiki.test.rest.ObjectsResourceTest.java

@Test
public void testPUTPropertyFormUrlEncoded() throws Exception {
    final String TAG_VALUE = UUID.randomUUID().toString();

    /* Make sure that an Object with the TagClass exists. */
    createObjectIfDoesNotExists("XWiki.TagClass", MAIN_SPACE, "WebHome");

    GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), MAIN_SPACE, "WebHome").toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
    Assert.assertNotNull(link);/*  w w w . j  a va2 s  . c  o  m*/

    getMethod = executeGet(link.getHref());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertFalse(objects.getObjectSummaries().isEmpty());

    Object currentObject = null;

    for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
        if (objectSummary.getClassName().equals("XWiki.TagClass")) {
            link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
            Assert.assertNotNull(link);
            getMethod = executeGet(link.getHref());
            Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

            currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
            break;
        }
    }

    Assert.assertNotNull(currentObject);

    Property tagsProperty = getProperty(currentObject, "tags");

    Assert.assertNotNull(tagsProperty);

    Link tagsPropertyLink = getFirstLinkByRelation(tagsProperty, Relations.SELF);

    Assert.assertNotNull(tagsPropertyLink);

    NameValuePair[] nameValuePairs = new NameValuePair[1];
    nameValuePairs[0] = new NameValuePair("property#tags", TAG_VALUE);

    PostMethod postMethod = executePostForm(String.format("%s?method=PUT", tagsPropertyLink.getHref()),
            nameValuePairs, TestUtils.ADMIN_CREDENTIALS.getUserName(),
            TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_ACCEPTED, postMethod.getStatusCode());

    getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), MAIN_SPACE, "WebHome",
            currentObject.getClassName(), currentObject.getNumber()).toString());
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());

    currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    tagsProperty = getProperty(currentObject, "tags");

    Assert.assertEquals(TAG_VALUE, tagsProperty.getValue());
}

From source file:org.xwiki.test.rest.ObjectsResourceTest.java

@Test
public void testGETObjectAtPageVersion() throws Exception {
    Object objectToBePut = createObjectIfDoesNotExists("XWiki.TagClass", MAIN_SPACE, "WebHome");

    Map<String, String> versionToValueMap = new HashMap<String, String>();
    for (int i = 0; i < 5; i++) {
        String value = String.format("Value%d", i);

        Property property = getProperty(objectToBePut, "tags");
        property.setValue(value);/*from   ww  w . j  a v  a 2  s .  co m*/

        PutMethod putMethod = executePutXml(
                buildURI(ObjectResource.class, getWiki(), MAIN_SPACE, "WebHome", objectToBePut.getClassName(),
                        objectToBePut.getNumber()).toString(),
                objectToBePut, TestUtils.ADMIN_CREDENTIALS.getUserName(),
                TestUtils.ADMIN_CREDENTIALS.getPassword());
        Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());

        GetMethod getMethod = executeGet(
                buildURI(PageResource.class, getWiki(), MAIN_SPACE, "WebHome").toString());
        Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

        Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

        versionToValueMap.put(page.getVersion(), value);
    }

    for (String version : versionToValueMap.keySet()) {
        GetMethod getMethod = executeGet(buildURI(ObjectAtPageVersionResource.class, getWiki(), MAIN_SPACE,
                "WebHome", version, objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
        Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

        Object currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

        Property property = getProperty(currentObject, "tags");

        Assert.assertEquals(versionToValueMap.get(version), property.getValue());

        checkLinks(currentObject);
        for (Property p : currentObject.getProperties()) {
            checkLinks(p);
        }
    }
}

From source file:org.xwiki.test.rest.PageResourceTest.java

@Test
public void testPUTGETPage() throws Exception {
    final String title = String.format("Title (%s)", UUID.randomUUID().toString());
    final String content = String.format("This is a content (%d)", System.currentTimeMillis());
    final String comment = String.format("Updated title and content (%d)", System.currentTimeMillis());

    Page originalPage = getFirstPage();//from  w w w  .  j  a  v a 2s  .c  om

    Page newPage = objectFactory.createPage();
    newPage.setTitle(title);
    newPage.setContent(content);
    newPage.setComment(comment);

    Link link = getFirstLinkByRelation(originalPage, Relations.SELF);
    Assert.assertNotNull(link);

    // PUT
    PutMethod putMethod = executePutXml(link.getHref(), newPage, TestUtils.ADMIN_CREDENTIALS.getUserName(),
            TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
    Page modifiedPage = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());

    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());

    // GET
    GetMethod getMethod = executeGet(link.getHref());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    modifiedPage = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());
}

From source file:org.xwiki.test.rest.PageResourceTest.java

@Test
public void testPUTGETWithObject() throws Exception {
    String pageURI = buildURI(PageResource.class, getWiki(), Arrays.asList("RESTTest"), "PageWithObject");

    final String title = String.format("Title (%s)", UUID.randomUUID().toString());
    final String content = String.format("This is a content (%d)", System.currentTimeMillis());
    final String comment = String.format("Updated title and content (%d)", System.currentTimeMillis());

    Page newPage = objectFactory.createPage();
    newPage.setTitle(title);/*  w w  w  .ja  v  a 2 s.  c o m*/
    newPage.setContent(content);
    newPage.setComment(comment);

    // Add object
    final String TAG_VALUE = "TAG";
    Property property = new Property();
    property.setName("tags");
    property.setValue(TAG_VALUE);
    Object object = objectFactory.createObject();
    object.setClassName("XWiki.TagClass");
    object.getProperties().add(property);
    newPage.setObjects(objectFactory.createObjects());
    newPage.getObjects().getObjectSummaries().add(object);

    // PUT
    PutMethod putMethod = executePutXml(pageURI, newPage, TestUtils.ADMIN_CREDENTIALS.getUserName(),
            TestUtils.ADMIN_CREDENTIALS.getPassword());
    assertThat(getHttpMethodInfo(putMethod), putMethod.getStatusCode(),
            isIn(Arrays.asList(HttpStatus.SC_ACCEPTED, HttpStatus.SC_CREATED)));

    Page modifiedPage = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());

    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());

    // GET
    GetMethod getMethod = executeGet(pageURI + "?objects=true");
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    modifiedPage = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());

    Assert.assertEquals(TAG_VALUE,
            getProperty((Object) modifiedPage.getObjects().getObjectSummaries().get(0), "tags").getValue());

    // Send again but with empty object list

    modifiedPage.getObjects().getObjectSummaries().clear();

    // PUT
    putMethod = executePutXml(pageURI, modifiedPage, TestUtils.ADMIN_CREDENTIALS.getUserName(),
            TestUtils.ADMIN_CREDENTIALS.getPassword());
    assertThat(getHttpMethodInfo(putMethod), putMethod.getStatusCode(),
            isIn(Arrays.asList(HttpStatus.SC_ACCEPTED)));

    modifiedPage = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());

    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());

    // GET
    getMethod = executeGet(pageURI + "?objects=true");
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    modifiedPage = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());

    Assert.assertTrue(modifiedPage.getObjects().getObjectSummaries().isEmpty());
}