Example usage for org.apache.commons.httpclient.methods PutMethod setRequestEntity

List of usage examples for org.apache.commons.httpclient.methods PutMethod setRequestEntity

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PutMethod setRequestEntity.

Prototype

public void setRequestEntity(RequestEntity paramRequestEntity) 

Source Link

Usage

From source file:org.apache.wink.itest.request.RequestMethodsTest.java

/**
 * Tests the/*  w  ww .  ja  v a2s.  c om*/
 * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
 * uses the <code>If-Match</code> header.
 * 
 * @throws HttpException
 * @throws IOException
 */
private void checkETagIfMatch(String etag, boolean isEntityTagWeak) throws HttpException, IOException {
    HttpClient client = new HttpClient();

    final String justTheTag = etag;
    final String setETag = isEntityTagWeak ? "W/" + justTheTag : justTheTag;
    String isWeak = isEntityTagWeak ? "true" : "false";

    PutMethod putMethod = new PutMethod(getBaseURI() + "/context/request/etag");
    putMethod.setRequestEntity(new StringRequestEntity(setETag, "text/string", "UTF-8"));
    try {
        /*
         * sets an entity tag
         */
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", setETag);
    try {
        /*
         * verifies that if the exact etag is sent in, then the request is
         * allowed to proceed
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    try {
        /*
         * verifies that a request without an If-Match header will still
         * proceed
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", setETag.substring(1, setETag.length() - 1));
    try {
        /*
         * verifies that an unquoted entity tag is not a valid entity tag
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", setETag.substring(0, setETag.length() - 1));
    try {
        /*
         * verifies that a misquoted entity tag is not a valid entity tag
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", setETag.substring(1, setETag.length()));
    try {
        /*
         * verifies that a misquoted entity tag is not a valid entity tag
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", "\"someothervalue\"");
    try {
        /*
         * verifies that if an etag is sent that does not match the server
         * etag, that a 412 is returned
         */
        client.executeMethod(getMethod);
        assertEquals(412, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", "\"austin\", \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do not match the
         * server etag, that a 412 is returned
         */
        client.executeMethod(getMethod);
        assertEquals(412, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.addRequestHeader("If-Match", "\"austin\", \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do not match the
         * server etag, that a 412 is returned
         */
        client.executeMethod(getMethod);
        assertEquals(412, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.addRequestHeader("If-Match", "\"austin\", " + setETag + " , \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do match the server
         * etag, that a 200 and entity body is returned
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.request.RequestMethodsTest.java

/**
 * Tests the//ww w .  j a  v  a  2  s.co m
 * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
 * uses the <code>If-None-Match</code> header.
 * 
 * @throws HttpException
 * @throws IOException
 */
private void checkETagIfNoneMatch(String etag, boolean isEntityTagWeak) throws HttpException, IOException {
    HttpClient client = new HttpClient();
    final String justTheTag = etag;
    final String setETag = isEntityTagWeak ? "W/" + justTheTag : justTheTag;
    String isWeak = isEntityTagWeak ? "true" : "false";

    PutMethod putMethod = new PutMethod(getBaseURI() + "/context/request/etag");
    putMethod.setRequestEntity(new StringRequestEntity(setETag, "text/string", "UTF-8"));
    try {
        /*
         * sets an entity tag
         */
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", setETag);
    try {
        /*
         * verifies that if the exact etag is sent in, then the response is
         * a 304
         */
        client.executeMethod(getMethod);
        assertEquals(304, getMethod.getStatusCode());
        assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", "\"*\"");
    try {
        /*
         * verifies that if a "*" etag is sent in, then the response returns
         * a 304
         */
        client.executeMethod(getMethod);
        assertEquals(304, getMethod.getStatusCode());
        assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
    } finally {
        getMethod.releaseConnection();
    }

    PostMethod postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
    postMethod.setRequestHeader("If-None-Match", setETag);
    try {
        /*
         * verifies that if a matching etag is sent in, then the response
         * returns a 412
         */
        client.executeMethod(postMethod);
        assertEquals(412, postMethod.getStatusCode());
        assertEquals(setETag, postMethod.getResponseHeader("ETag").getValue());
    } finally {
        postMethod.releaseConnection();
    }

    postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
    postMethod.setRequestHeader("If-None-Match", "\"*\"");
    try {
        /*
         * verifies that if a "*" etag is sent in, then the response returns
         * a 412
         */
        client.executeMethod(postMethod);
        assertEquals(412, postMethod.getStatusCode());
        assertEquals(setETag, postMethod.getResponseHeader("ETag").getValue());
    } finally {
        postMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    try {
        /*
         * verifies that a request without an If-None-Match header will
         * still proceed
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
    try {
        /*
         * verifies that a request without an If-None-Match header will
         * still proceed
         */
        client.executeMethod(postMethod);
        assertEquals(200, postMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, postMethod.getResponseBodyAsString());
    } finally {
        postMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", setETag.substring(1, setETag.length() - 1));
    try {
        /*
         * verifies that an unquoted entity tag is invalid
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", setETag.substring(0, setETag.length() - 1));
    try {
        /*
         * verifies that a misquoted entity tag is invalid
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", setETag.substring(1, setETag.length()));
    try {
        /*
         * verifies that a misquoted entity tag is invalid
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", "\"someothervalue\"");
    try {
        /*
         * verifies that if an etag is sent that does not match the server
         * etag, that request is allowed to proceed
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", "\"austin\", \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do not match the
         * server etag, that the request is allowed to proceed
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.addRequestHeader("If-None-Match", "\"austin\", \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do not match the
         * server etag, then a 200 and the request entity is returned
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
    postMethod.addRequestHeader("If-None-Match", "\"austin\", \"powers\"");
    try {
        /*
         * verifies that a request without an If-None-Match header will
         * still proceed
         */
        client.executeMethod(postMethod);
        assertEquals(200, postMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, postMethod.getResponseBodyAsString());
    } finally {
        postMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.addRequestHeader("If-None-Match", "\"austin\", " + setETag + " , \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do match the server
         * etag, that a 304 is returned
         */
        client.executeMethod(getMethod);
        assertEquals(304, getMethod.getStatusCode());
        assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
    } finally {
        getMethod.releaseConnection();
    }

    postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
    postMethod.addRequestHeader("If-None-Match", "\"austin\", " + setETag + " , \"powers\"");
    try {
        /*
         * verifies that a request with an If-None-Match header will fail
         */
        client.executeMethod(postMethod);
        assertEquals(412, postMethod.getStatusCode());
        assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSBytesArrayTest.java

/**
 * Tests putting and then getting a byte array.
 * //from   w ww.  ja  va 2  s  .com
 * @throws HttpException
 * @throws IOException
 */
public void testPutByteArray() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/bytesarray");
    byte[] barr = new byte[1000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "bytes/array"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/bytesarray");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[1000];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        String contentType = (getMethod.getResponseHeader("Content-Type") == null) ? null
                : getMethod.getResponseHeader("Content-Type").getValue();
        assertNotNull(contentType, contentType);
        assertEquals(barr.length,
                Integer.valueOf(getMethod.getResponseHeader("Content-Length").getValue()).intValue());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSBytesArrayTest.java

/**
 * Tests receiving an empty byte array.//from  w  ww  .  j  a  v a2  s .co m
 * 
 * @throws HttpException
 * @throws IOException
 */
public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/bytesarray");
    byte[] barr = new byte[1000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "any/type"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/bytesarray");
    getMethod.addRequestHeader("Accept", "mytype/subtype");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[1000];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
        assertEquals(barr.length,
                Integer.valueOf(getMethod.getResponseHeader("Content-Length").getValue()).intValue());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSDataSourceTest.java

/**
 * Tests putting and then getting a DataSource entity.
 * //  ww w.j av a  2  s  . c  o  m
 * @throws HttpException
 * @throws IOException
 */
public void testPutDataSource() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/datasource");
    byte[] barr = new byte[1000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "bytes/array"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/datasource");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[1000];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }

        String contentType = (getMethod.getResponseHeader("Content-Type") == null) ? null
                : getMethod.getResponseHeader("Content-Type").getValue();
        assertNotNull(contentType, contentType);
        assertNull(
                (getMethod.getResponseHeader("Content-Length") == null) ? ""
                        : getMethod.getResponseHeader("Content-Length").getValue(),
                getMethod.getResponseHeader("Content-Length"));
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSDataSourceTest.java

/**
 * Tests receiving a DataSource with any media type.
 * /*from  w  w w  . ja v  a  2 s .  c o  m*/
 * @throws HttpException
 * @throws IOException
 */
public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/datasource");
    byte[] barr = new byte[1000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "any/type"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/datasource");
    getMethod.addRequestHeader("Accept", "mytype/subtype");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[1000];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
        assertNull(
                (getMethod.getResponseHeader("Content-Length") == null) ? ""
                        : getMethod.getResponseHeader("Content-Length").getValue(),
                getMethod.getResponseHeader("Content-Length"));
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSFileTest.java

/**
 * Tests receiving an empty byte array.//  w  w  w .j ava  2  s.c  o  m
 * 
 * @throws HttpException
 * @throws IOException
 */
public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/file");
    byte[] barr = new byte[1000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "any/type"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/file");
    getMethod.addRequestHeader("Accept", "mytype/subtype");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[1000];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
        assertEquals(barr.length,
                Integer.valueOf(getMethod.getResponseHeader("Content-Length").getValue()).intValue());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSInputStreamTest.java

/**
 * Tests putting and then getting a byte array.
 * //w  w w. j a  v a2 s .  c  om
 * @throws HttpException
 * @throws IOException
 */
public void testPutInputStream() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/inputstream");
    byte[] barr = new byte[100000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "bytes/array"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/inputstream");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[barr.length];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }

        String contentType = (getMethod.getResponseHeader("Content-Type") == null) ? null
                : getMethod.getResponseHeader("Content-Type").getValue();
        assertNotNull(contentType, contentType);
        Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
        assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader);
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSInputStreamTest.java

/**
 * Tests receiving an empty byte array.//from   w  w  w.j  ava2  s . co m
 * 
 * @throws HttpException
 * @throws IOException
 */
public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/inputstream");
    byte[] barr = new byte[100000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "any/type"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/inputstream");
    getMethod.addRequestHeader("Accept", "mytype/subtype");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[barr.length];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
        Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
        assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader);
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSMultivaluedMapTest.java

/**
 * Tests putting and then getting a /multivaluedmap.
 * /*ww  w.  j  a  v a  2 s  . co m*/
 * @throws HttpException
 * @throws IOException
 */
public void testPutReader() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/multivaluedmap");
    putMethod.setRequestEntity(new StringRequestEntity("username=user1&password=user1password",
            "application/x-www-form-urlencoded", "UTF-8"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/multivaluedmap");
    getMethod.addRequestHeader("Accept", "application/x-www-form-urlencoded");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        InputStreamReader isr = new InputStreamReader(is);
        char[] buffer = new char[1];
        int read = 0;
        int offset = 0;
        while ((read = isr.read(buffer, offset, buffer.length - offset)) != -1) {
            offset += read;
            if (offset >= buffer.length) {
                buffer = ArrayUtils.copyOf(buffer, buffer.length * 2);
            }
        }
        char[] carr = ArrayUtils.copyOf(buffer, offset);

        int checkEOF = is.read();
        assertEquals(-1, checkEOF);
        String str = new String(carr);

        assertTrue(str, "username=user1&password=user1password".equals(str)
                || "password=user1password&username=user1".equals(str));
        assertEquals("application/x-www-form-urlencoded",
                getMethod.getResponseHeader("Content-Type").getValue());
        Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
        if (contentLengthHeader != null) {
            // some of the containers can be "smarter" and set the
            // content-length for us if the payload is small
            assertEquals("37", contentLengthHeader.getValue());
        }
    } finally {
        getMethod.releaseConnection();
    }
}