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

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

Introduction

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

Prototype

int SC_NO_CONTENT

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

Click Source Link

Document

<tt>204 No Content</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

/**
 * Verifies that a 204 status code is returned when the operation returns null.
 *//*  w w  w.  j ava2s  .  c  o  m*/
@Test
public void testPostOperationNull3() throws Exception {
    String url = URL_RESOURCE2 + "/pathPostOperationNull3";
    PostMethod method = new PostMethod(url);

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_NO_CONTENT);
    byte[] responseBody = method.getResponseBody();
    assertNull(responseBody);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation1() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation1";
    PutMethod method = new PutMethod(url);

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_NO_CONTENT);
    byte[] responseBody = method.getResponseBody();
    assertNull(responseBody);//from  w w w . j  a v  a  2 s.  co  m
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation2() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation2";
    PutMethod method = new PutMethod(url);
    String param = "this is string parameter!";
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_NO_CONTENT);
    byte[] responseBody = method.getResponseBody();
    assertNull(responseBody);/*from   www . j a v  a 2  s .  co m*/
}

From source file:com.zimbra.qa.unittest.TestCalDav.java

/** Mostly checking that if attendees cease to exist (even via DLs) then modification and cancel iTip
 * messages still work to the remaining attendees.
 *//*from  w  w w.jav  a  2s  .  c  o  m*/
@Test
public void testCreateModifyDeleteAttendeeModifyAndCancel() throws ServiceException, IOException {
    Account dav1 = users[1].create();
    Account dav2 = users[2].create();
    Account dav3 = users[3].create();
    Account dav4 = users[4].create();
    DistributionList dl = TestUtil.createDistributionList(DL1);
    String[] members = { dav4.getName() };
    prov.addMembers(dl, members);
    List<MailTarget> attendees = Lists.newArrayList();
    attendees.add(dav1);
    attendees.add(dav2);
    attendees.add(dav3);
    attendees.add(dl);
    ZVCalendar vCal = simpleMeeting(dav1, attendees, "1", 8);
    ZProperty uidProp = vCal.getComponent(ICalTok.VEVENT).getProperty(ICalTok.UID);
    String uid = uidProp.getValue();
    String davBaseName = uid + ".ics";
    String url = String.format("%s%s", getFolderUrl(dav1, "Calendar"), davBaseName);
    doIcalPut(url, dav1, zvcalendarToBytes(vCal), HttpStatus.SC_CREATED);
    String inboxhref = TestCalDav.waitForNewSchedulingRequestByUID(dav2, uid);
    assertTrue("Found meeting request for newly created item", inboxhref.contains(uid));
    doDeleteMethod(getLocalServerRoot().append(inboxhref).toString(), dav2, HttpStatus.SC_NO_CONTENT);

    // attendee via DL
    inboxhref = TestCalDav.waitForNewSchedulingRequestByUID(dav4, uid);
    assertTrue("Found meeting request for newly created item", inboxhref.contains(uid));
    doDeleteMethod(getLocalServerRoot().append(inboxhref).toString(), dav4, HttpStatus.SC_NO_CONTENT);

    vCal = simpleMeeting(dav1, attendees, uid, "2", 9);
    doIcalPut(url, dav1, zvcalendarToBytes(vCal), HttpStatus.SC_CREATED);
    inboxhref = TestCalDav.waitForNewSchedulingRequestByUID(dav2, uid);
    assertTrue("Found meeting request for newly created item", inboxhref.contains(uid));
    doDeleteMethod(getLocalServerRoot().append(inboxhref).toString(), dav2, HttpStatus.SC_NO_CONTENT);

    // attendee via DL
    inboxhref = TestCalDav.waitForNewSchedulingRequestByUID(dav4, uid);
    assertTrue("Found meeting request for newly created item", inboxhref.contains(uid));
    doDeleteMethod(getLocalServerRoot().append(inboxhref).toString(), dav4, HttpStatus.SC_NO_CONTENT);

    // Test that iTip handling still happens when some of the attendees no longer exist.
    users[3].cleanup();
    users[4].cleanup(); // attendee via DL
    vCal = simpleMeeting(dav1, attendees, uid, "3", 10);
    doIcalPut(url, dav1, zvcalendarToBytes(vCal), HttpStatus.SC_CREATED);
    inboxhref = TestCalDav.waitForNewSchedulingRequestByUID(dav2, uid);
    assertTrue("Found meeting request for newly created item", inboxhref.contains(uid));
    doDeleteMethod(getLocalServerRoot().append(inboxhref).toString(), dav2, HttpStatus.SC_NO_CONTENT);
    String dav2Url = String.format("%s%s", getFolderUrl(dav2, "Calendar"), davBaseName);
    doGetMethod(dav2Url, dav2, HttpStatus.SC_OK);

    // Cancel meeting by deleting it
    doDeleteMethod(url, dav1, HttpStatus.SC_NO_CONTENT);

    inboxhref = TestCalDav.waitForNewSchedulingRequestByUID(dav2, uid);
    assertTrue("Found meeting request for newly created item", inboxhref.contains(uid));
    doDeleteMethod(getLocalServerRoot().append(inboxhref).toString(), dav2, HttpStatus.SC_NO_CONTENT);
    // The associated calendar item should have been deleted as a result of the Cancel
    doGetMethod(dav2Url, dav2, HttpStatus.SC_NOT_FOUND);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation5Xml() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation5Xml";
    PutMethod method = new PutMethod(url);
    String param = this.getXmlCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/xml", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_NO_CONTENT);
    byte[] responseBody = method.getResponseBody();
    assertNull(responseBody);//w  w w . jav a2  s.  c o  m
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

@Test
public void testPutOperation5Json() throws Exception {
    String url = URL_RESOURCE1 + "/pathPutOperation5Json";
    PutMethod method = new PutMethod(url);
    String param = this.getJsonCd();
    method.setRequestEntity(new StringRequestEntity(param, "application/json", null));

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_NO_CONTENT);
    byte[] responseBody = method.getResponseBody();
    assertNull(responseBody);/*from  w  w w .j  ava2  s  .c o  m*/
}

From source file:com.zimbra.cs.service.UserServlet.java

private static Pair<Header[], HttpMethod> doHttpOp(ZAuthToken authToken, HttpMethod method)
        throws ServiceException {
    // create an HTTP client with the same cookies
    String url = "";
    String hostname = "";
    try {//from  w  w w.ja v a 2s.  c o m
        url = method.getURI().toString();
        hostname = method.getURI().getHost();
    } catch (IOException e) {
        log.warn("can't parse target URI", e);
    }

    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    Map<String, String> cookieMap = authToken.cookieMap(false);
    if (cookieMap != null) {
        HttpState state = new HttpState();
        for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
            state.addCookie(new org.apache.commons.httpclient.Cookie(hostname, ck.getKey(), ck.getValue(), "/",
                    null, false));
        }
        client.setState(state);
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    }

    if (method instanceof PutMethod) {
        long contentLength = ((PutMethod) method).getRequestEntity().getContentLength();
        if (contentLength > 0) {
            int timeEstimate = Math.max(10000, (int) (contentLength / 100)); // 100kbps in millis
            // cannot set connection time using our ZimbrahttpConnectionManager,
            // see comments in ZimbrahttpConnectionManager.
            // actually, length of the content to Put should not be a factor for
            // establishing a connection, only read time out matter, which we set
            // client.getHttpConnectionManager().getParams().setConnectionTimeout(timeEstimate);

            method.getParams().setSoTimeout(timeEstimate);
        }
    }

    try {
        int statusCode = HttpClientUtil.executeMethod(client, method);
        if (statusCode == HttpStatus.SC_NOT_FOUND || statusCode == HttpStatus.SC_FORBIDDEN)
            throw MailServiceException.NO_SUCH_ITEM(-1);
        else if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED
                && statusCode != HttpStatus.SC_NO_CONTENT)
            throw ServiceException.RESOURCE_UNREACHABLE(method.getStatusText(), null,
                    new ServiceException.InternalArgument(HTTP_URL, url, ServiceException.Argument.Type.STR),
                    new ServiceException.InternalArgument(HTTP_STATUS_CODE, statusCode,
                            ServiceException.Argument.Type.NUM));

        List<Header> headers = new ArrayList<Header>(Arrays.asList(method.getResponseHeaders()));
        headers.add(new Header("X-Zimbra-Http-Status", "" + statusCode));
        return new Pair<Header[], HttpMethod>(headers.toArray(new Header[0]), method);
    } catch (HttpException e) {
        throw ServiceException.RESOURCE_UNREACHABLE("HttpException while fetching " + url, e);
    } catch (IOException e) {
        throw ServiceException.RESOURCE_UNREACHABLE("IOException while fetching " + url, e);
    }
}

From source file:com.zimbra.qa.unittest.TestCalDav.java

@Test
public void testAndroidMeetingSeries() throws Exception {
    Account dav1 = users[1].create();/* w  ww  . j  a  v  a 2  s . c  o  m*/
    Account dav2 = users[2].create();
    users[2].getZMailbox(); // Force creation of mailbox - shouldn't be needed
    String calFolderUrl = getFolderUrl(dav1, "Calendar").replaceAll("@", "%40");
    String url = String.format("%s%s.ics", calFolderUrl, androidSeriesMeetingUid);
    HttpClient client = new HttpClient();
    PutMethod putMethod = new PutMethod(url);
    addBasicAuthHeaderForUser(putMethod, dav1);
    putMethod.addRequestHeader("Content-Type", "text/calendar");

    String body = androidSeriesMeetingTemplate.replace("%%ORG%%", dav1.getName())
            .replace("%%ATT%%", dav2.getName()).replace("%%UID%%", androidSeriesMeetingUid);
    putMethod.setRequestEntity(new ByteArrayRequestEntity(body.getBytes(), MimeConstants.CT_TEXT_CALENDAR));
    HttpMethodExecutor.execute(client, putMethod, HttpStatus.SC_CREATED);

    String inboxhref = TestCalDav.waitForNewSchedulingRequestByUID(dav2, androidSeriesMeetingUid);
    assertTrue("Found meeting request for newly created item", inboxhref.contains(androidSeriesMeetingUid));

    GetMethod getMethod = new GetMethod(url);
    addBasicAuthHeaderForUser(getMethod, dav1);
    HttpMethodExecutor exe = HttpMethodExecutor.execute(client, getMethod, HttpStatus.SC_OK);
    String etag = null;
    for (Header hdr : exe.respHeaders) {
        if (DavProtocol.HEADER_ETAG.equals(hdr.getName())) {
            etag = hdr.getValue();
        }
    }
    assertNotNull("ETag from get", etag);

    // Check that we fail if the etag is wrong
    putMethod = new PutMethod(url);
    addBasicAuthHeaderForUser(putMethod, dav1);
    putMethod.addRequestHeader("Content-Type", "text/calendar");
    putMethod.addRequestHeader(DavProtocol.HEADER_IF_MATCH, "willNotMatch");
    putMethod.setRequestEntity(new ByteArrayRequestEntity(body.getBytes(), MimeConstants.CT_TEXT_CALENDAR));
    HttpMethodExecutor.execute(client, putMethod, HttpStatus.SC_PRECONDITION_FAILED);

    PropFindMethod propFindMethod = new PropFindMethod(getFolderUrl(dav1, "Calendar"));
    addBasicAuthHeaderForUser(propFindMethod, dav1);
    TestCalDav.HttpMethodExecutor executor;
    String respBody;
    Element respElem;
    propFindMethod.addRequestHeader("Content-Type", MimeConstants.CT_TEXT_XML);
    propFindMethod.addRequestHeader("Depth", "1");
    propFindMethod.setRequestEntity(
            new ByteArrayRequestEntity(propFindEtagResType.getBytes(), MimeConstants.CT_TEXT_XML));
    executor = new TestCalDav.HttpMethodExecutor(client, propFindMethod, HttpStatus.SC_MULTI_STATUS);
    respBody = new String(executor.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
    respElem = Element.XMLElement.parseXML(respBody);
    assertEquals("name of top element in propfind response", DavElements.P_MULTISTATUS, respElem.getName());
    assertTrue("propfind response should have child elements", respElem.hasChildren());
    Iterator<Element> iter = respElem.elementIterator();
    boolean hasCalendarHref = false;
    boolean hasCalItemHref = false;
    while (iter.hasNext()) {
        Element child = iter.next();
        if (DavElements.P_RESPONSE.equals(child.getName())) {
            Iterator<Element> hrefIter = child.elementIterator(DavElements.P_HREF);
            while (hrefIter.hasNext()) {
                Element href = hrefIter.next();
                calFolderUrl.endsWith(href.getText());
                hasCalendarHref = hasCalendarHref || calFolderUrl.endsWith(href.getText());
                hasCalItemHref = hasCalItemHref || url.endsWith(href.getText());
            }
        }
    }
    assertTrue("propfind response contained entry for calendar", hasCalendarHref);
    assertTrue("propfind response contained entry for calendar entry ", hasCalItemHref);

    DeleteMethod deleteMethod = new DeleteMethod(url);
    addBasicAuthHeaderForUser(deleteMethod, dav1);
    HttpMethodExecutor.execute(client, deleteMethod, HttpStatus.SC_NO_CONTENT);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

/**
 * Verifies that a 204 status code is returned when the operation returns null.
 */// w w w . j  a v  a2 s  . c om
@Test
public void testPutOperationNull1() throws Exception {
    String url = URL_RESOURCE2 + "/pathPutOperationNull1";
    PutMethod method = new PutMethod(url);

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_NO_CONTENT);
    byte[] responseBody = method.getResponseBody();
    assertNull(responseBody);
}

From source file:muki.tool.JavaCompilationDeploymentTestCase.java

/**
 * Verifies that a 204 status code is returned when the operation returns null.
 *///from   ww w.  j  av  a2s  . c o  m
@Test
public void testPutOperationNull2() throws Exception {
    String url = URL_RESOURCE2 + "/pathPutOperationNull2";
    PutMethod method = new PutMethod(url);

    int statusCode = this.getHttpClient().executeMethod(method);
    assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_NO_CONTENT);
    byte[] responseBody = method.getResponseBody();
    assertNull(responseBody);
}