Example usage for org.apache.commons.httpclient.methods ByteArrayRequestEntity ByteArrayRequestEntity

List of usage examples for org.apache.commons.httpclient.methods ByteArrayRequestEntity ByteArrayRequestEntity

Introduction

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

Prototype

public ByteArrayRequestEntity(byte[] paramArrayOfByte, String paramString) 

Source Link

Usage

From source file:org.apache.wink.itest.contentencoding.ContentEncodingTest.java

/**
 * Tests sending in small bits of gzip encoded content.
 * /*w w  w . j  av a2 s .c om*/
 * @throws HttpException
 * @throws IOException
 */
public void testSendLargeGzipContentEncodedAndReceiveContentEncoded() throws HttpException, IOException {
    PostMethod postMethod = new PostMethod(BASE_URI + "/bigbook/mirror");
    postMethod.setContentChunked(true);
    postMethod.setRequestHeader("Accept", "text/plain");
    postMethod.setRequestHeader("Accept-Encoding", "gzip");
    postMethod.setRequestHeader("Content-Encoding", "gzip");
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    for (int c = 0; c < 5000000; ++c) {
        originalContent.write(c);
    }

    /*
     * gzip the contents
     */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
    originalContent.writeTo(gzipOut);
    gzipOut.finish();
    byte[] content = baos.toByteArray();

    postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8"));
    try {
        int result = client.executeMethod(postMethod);
        assertEquals(200, result);
        InputStream responseStream = new GZIPInputStream(postMethod.getResponseBodyAsStream());
        for (int c = 0; c < 5000000; ++c) {
            assertEquals(c % 256, responseStream.read());
        }
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.contextresolver.ContextTest.java

public void testUserContextProvider() throws Exception {
    HttpClient httpClient = new HttpClient();

    User user = new User();
    user.setUserName("joedoe@example.com");
    JAXBElement<User> element = new JAXBElement<User>(new QName("http://jaxb.context.tests", "user"),
            User.class, user);
    JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
    StringWriter sw = new StringWriter();
    Marshaller m = context.createMarshaller();
    m.marshal(element, sw);// ww  w . j a  v a 2  s .c o m
    PostMethod postMethod = new PostMethod(getBaseURI());
    try {
        postMethod.setRequestEntity(new ByteArrayRequestEntity(sw.toString().getBytes(), "text/xml"));
        httpClient.executeMethod(postMethod);
        assertEquals(204, postMethod.getStatusCode());
    } finally {
        postMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/joedoe@example.com");
    try {
        httpClient.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        Unmarshaller u = context.createUnmarshaller();
        element = u.unmarshal(new StreamSource(getMethod.getResponseBodyAsStream()), User.class);
        assertNotNull(element);
        user = element.getValue();
        assertNotNull(user);
        assertEquals("joedoe@example.com", user.getUserName());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.contextresolver.DepartmentTest.java

/**
 * This will drive several different requests that interact with the
 * Departments resource class./*from ww  w . j av  a 2s.c  o m*/
 */
public void testDepartmentsResourceJAXB() throws Exception {
    PostMethod postMethod = null;
    GetMethod getAllMethod = null;
    GetMethod getOneMethod = null;
    HeadMethod headMethod = null;
    DeleteMethod deleteMethod = null;
    try {

        // make sure everything is clear before testing
        DepartmentDatabase.clearEntries();

        // create a new Department
        Department newDepartment = new Department();
        newDepartment.setDepartmentId("1");
        newDepartment.setDepartmentName("Marketing");
        JAXBContext context = JAXBContext
                .newInstance(new Class<?>[] { Department.class, DepartmentListWrapper.class });
        Marshaller marshaller = context.createMarshaller();
        StringWriter sw = new StringWriter();
        marshaller.marshal(newDepartment, sw);
        HttpClient client = new HttpClient();
        postMethod = new PostMethod(getBaseURI());
        RequestEntity reqEntity = new ByteArrayRequestEntity(sw.toString().getBytes(), "text/xml");
        postMethod.setRequestEntity(reqEntity);
        client.executeMethod(postMethod);

        newDepartment = new Department();
        newDepartment.setDepartmentId("2");
        newDepartment.setDepartmentName("Sales");
        sw = new StringWriter();
        marshaller.marshal(newDepartment, sw);
        client = new HttpClient();
        postMethod = new PostMethod(getBaseURI());
        reqEntity = new ByteArrayRequestEntity(sw.toString().getBytes(), "text/xml");
        postMethod.setRequestEntity(reqEntity);
        client.executeMethod(postMethod);

        // now let's get the list of Departments that we just created
        // (should be 2)
        client = new HttpClient();
        getAllMethod = new GetMethod(getBaseURI());
        client.executeMethod(getAllMethod);
        byte[] bytes = getAllMethod.getResponseBody();
        assertNotNull(bytes);
        InputStream bais = new ByteArrayInputStream(bytes);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Object obj = unmarshaller.unmarshal(bais);
        assertTrue(obj instanceof DepartmentListWrapper);
        DepartmentListWrapper wrapper = (DepartmentListWrapper) obj;
        List<Department> dptList = wrapper.getDepartmentList();
        assertNotNull(dptList);
        assertEquals(2, dptList.size());

        // now get a specific Department that was created
        client = new HttpClient();
        getOneMethod = new GetMethod(getBaseURI() + "/1");
        client.executeMethod(getOneMethod);
        bytes = getOneMethod.getResponseBody();
        assertNotNull(bytes);
        bais = new ByteArrayInputStream(bytes);
        obj = unmarshaller.unmarshal(bais);
        assertTrue(obj instanceof Department);
        Department dept = (Department) obj;
        assertEquals("1", dept.getDepartmentId());
        assertEquals("Marketing", dept.getDepartmentName());

        // let's send a Head request for both an existent and non-existent
        // resource
        // we are testing to see if header values being set in the resource
        // implementation
        // are sent back appropriately
        client = new HttpClient();
        headMethod = new HeadMethod(getBaseURI() + "/3");
        client.executeMethod(headMethod);
        assertNotNull(headMethod.getResponseHeaders());
        Header header = headMethod.getResponseHeader("unresolved-id");
        assertNotNull(header);
        assertEquals("3", header.getValue());
        headMethod.releaseConnection();

        // now the resource that should exist
        headMethod = new HeadMethod(getBaseURI() + "/1");
        client.executeMethod(headMethod);
        assertNotNull(headMethod.getResponseHeaders());
        header = headMethod.getResponseHeader("resolved-id");
        assertNotNull(header);
        assertEquals("1", header.getValue());

        deleteMethod = new DeleteMethod(getBaseURI() + "/1");
        client.executeMethod(deleteMethod);
        assertEquals(204, deleteMethod.getStatusCode());

        deleteMethod = new DeleteMethod(getBaseURI() + "/2");
        client.executeMethod(deleteMethod);
        assertEquals(204, deleteMethod.getStatusCode());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (postMethod != null) {
            postMethod.releaseConnection();
        }
        if (getAllMethod != null) {
            getAllMethod.releaseConnection();
        }
        if (getOneMethod != null) {
            getOneMethod.releaseConnection();
        }
        if (headMethod != null) {
            headMethod.releaseConnection();
        }
        if (deleteMethod != null) {
            deleteMethod.releaseConnection();
        }
    }
}

From source file:org.apache.wink.itest.exceptions.NullValuesDuringTargettingTest.java

/**
 * Tests that a request to a method with a content type, a request entity,
 * but without a {@link Consumes} method results in 200 successful method
 * invocation./*ww w.j  a v  a 2s .co  m*/
 * 
 * @throws IOException
 */
public void testContentTypeWithRequestEntityIncomingRequestWithNoConsumesMethod() throws IOException {
    PostMethod postMethod = new PostMethod(getBaseURI() + "/targeting/nullresource/withoutconsumes");
    postMethod.setRequestEntity(new ByteArrayRequestEntity("myString".getBytes(), "custom/type"));
    try {
        client.executeMethod(postMethod);
        assertEquals(200, postMethod.getStatusCode());
        assertEquals("myString", postMethod.getResponseBodyAsString());
        String contentType = (postMethod.getResponseHeader("Content-Type") == null) ? null
                : postMethod.getResponseHeader("Content-Type").getValue();
        assertNotNull(contentType, contentType);
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.exceptions.NullValuesDuringTargettingTest.java

/**
 * Tests that a request to a method with a content type, a request entity,
 * but with a {@link Consumes} method results in 200 successful method
 * invocation.//from  w w w. java 2 s. c  om
 * 
 * @throws IOException
 */
public void testContentTypeWithRequestEntityIncomingRequestWithConsumesMethod() throws IOException {
    PostMethod postMethod = new PostMethod(getBaseURI() + "/targeting/nullresource/withconsumes");
    postMethod.setRequestEntity(new ByteArrayRequestEntity("mystring".getBytes(), "text/plain"));
    try {
        client.executeMethod(postMethod);
        assertEquals(200, postMethod.getStatusCode());
        assertEquals("mystring", postMethod.getResponseBodyAsString());
        String contentType = (postMethod.getResponseHeader("Content-Type") == null) ? null
                : postMethod.getResponseHeader("Content-Type").getValue();
        assertNotNull(contentType, contentType);
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.InheritanceTest.java

public void testOverrideInterfaceAnnotations() throws Exception {
    PostMethod postMethod = new PostMethod(PARKING_LOT_URI + "/cars");
    GetMethod getMethod = new GetMethod(PARKING_LOT_URI + "/cars");
    try {//from  ww  w.  j  a  v a2  s  . c om
        String licenseNum = "103DIY";
        postMethod.setRequestEntity(new ByteArrayRequestEntity(licenseNum.getBytes(), "text/xml"));
        httpClient.executeMethod(postMethod);
        Header header = postMethod.getResponseHeader("Invoked");
        assertNotNull(header);
        assertEquals("ParkingLot.addCar", header.getValue());
        httpClient.executeMethod(getMethod);
        String resp = getMethod.getResponseBodyAsString();
        assertTrue(resp.contains(licenseNum));
        header = getMethod.getResponseHeader("Invoked");
        assertNotNull(header);
        assertEquals("ParkingLot.getCars", header.getValue());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.toString());
    } finally {
        postMethod.releaseConnection();
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.InheritanceTest.java

public void testOverrideSuperClassAnnotations() throws Exception {
    PostMethod postMethod = new PostMethod(PARKING_GARAGE_URI + "/cars");
    GetMethod getMethod = new GetMethod(PARKING_GARAGE_URI + "/cars/1");
    try {//ww w  .  ja v  a2 s  .  c o  m
        String licenseNum = "103DIY";
        postMethod.setRequestEntity(new ByteArrayRequestEntity(licenseNum.getBytes(), "text/xml"));
        httpClient.executeMethod(postMethod);
        Header header = postMethod.getResponseHeader("Invoked");
        assertNotNull(header);
        assertEquals("ParkingGarage.addCar", header.getValue());
        httpClient.executeMethod(getMethod);
        String resp = getMethod.getResponseBodyAsString();
        assertTrue(resp.contains(licenseNum));
        header = getMethod.getResponseHeader("Invoked");
        assertNotNull(header);
        assertEquals("ParkingGarage.getCars", header.getValue());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.toString());
    } finally {
        postMethod.releaseConnection();
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.InheritanceTest.java

public void testInheritAnnotationsFromInterface() throws Exception {
    PostMethod postMethod = new PostMethod(CARPORT_URI + "/carstorage");
    GetMethod getMethod = new GetMethod(CARPORT_URI + "/carstorage");
    try {//  w ww  .  j  a  v  a  2s  .  c  om
        String licenseNum = "103DIY";
        postMethod.setRequestEntity(new ByteArrayRequestEntity(licenseNum.getBytes(), "text/xml"));
        httpClient.executeMethod(postMethod);
        Header header = postMethod.getResponseHeader("Invoked");
        assertNotNull(header);
        assertEquals("Carport.addCar", header.getValue());
        httpClient.executeMethod(getMethod);
        String resp = getMethod.getResponseBodyAsString();
        assertTrue(resp.contains(licenseNum));
        header = getMethod.getResponseHeader("Invoked");
        assertNotNull(header);
        assertEquals("Carport.getCars", header.getValue());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.toString());
    } finally {
        postMethod.releaseConnection();
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.InheritanceTest.java

public void testInheritAnnotationsFromSuperClass() throws Exception {
    PostMethod postMethod = new PostMethod(CARFERRY_URI + "/cars");
    GetMethod getMethod = new GetMethod(CARFERRY_URI + "/cars/1");
    try {/*w  w  w. java2 s. c o m*/
        String licenseNum = "103DIY";
        postMethod.setRequestEntity(new ByteArrayRequestEntity(licenseNum.getBytes(), "text/xml"));
        httpClient.executeMethod(postMethod);
        Header header = postMethod.getResponseHeader("Invoked");
        assertNotNull(header);
        assertEquals("CarFerry.addCar", header.getValue());
        httpClient.executeMethod(getMethod);
        String resp = getMethod.getResponseBodyAsString();
        assertTrue(resp.contains(licenseNum));
        header = getMethod.getResponseHeader("Invoked");
        assertNotNull(header);
        assertEquals("CarFerry.getCars", header.getValue());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.toString());
    } finally {
        postMethod.releaseConnection();
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.largeentity.LargeEntityTest.java

/**
 * Tests sending a large string. Possible failures including the servlet
 * request buffer being too small, so the status headers do not get set
 * correctly since the message body will have to be flushed out of the
 * servlet response buffer./*from w  ww  .  j a  v a2 s  . c  om*/
 * 
 * @throws Exception
 */
public void testSendLargeString() throws Exception {
    PostMethod postMethod = new PostMethod(getBaseURI() + "/large");
    try {

        ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
        for (int c = 0; c < 5000000; ++c) {
            originalContent.write(c);
        }
        byte[] entity = originalContent.toByteArray();
        postMethod.setRequestEntity(new ByteArrayRequestEntity(entity, "text/xml"));
        client.executeMethod(postMethod);
        assertEquals(277, postMethod.getStatusCode());

        InputStream respStream = postMethod.getResponseBodyAsStream();
        for (int c = 0; c < entity.length; ++c) {
            int respByte = respStream.read();
            assertEquals(entity[c] % 256, (byte) respByte);
        }

        // final int maxHeaderLength = 100;
        // int headerLength = (entity.length < maxHeaderLength) ?
        // entity.length : maxHeaderLength;
        // byte[] headerBytes = new byte[headerLength];
        // for (int c = 0; c < headerLength; ++c) {
        // headerBytes[c] = entity[c];
        // }
        //

        StringBuffer sb = new StringBuffer();
        for (int c = 0; c < 50; ++c) {
            sb.append("abcdefghijklmnopqrstuvwxyz");
        }
        // String expectedHeaderValue = new String(headerBytes, "UTF-8");
        Header header = postMethod.getResponseHeader("appendStringsHeader");
        assertNotNull(header);
        assertEquals(sb.toString(), header.getValue());
    } finally {
        postMethod.releaseConnection();
    }
}