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

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

Introduction

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

Prototype

public StringRequestEntity(String paramString) 

Source Link

Usage

From source file:org.apache.servicemix.http.ConsumerEndpointTest.java

public void testHttpSoap11UnkownOp() throws Exception {
    initSoapEndpoints(true);//w ww.  j av a  2  s. c  om

    PostMethod post = new PostMethod("http://localhost:8192/ep1/");
    post.setRequestEntity(
            new StringRequestEntity("<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>"
                    + "<s:Body><hello>world</hello></s:Body>" + "</s:Envelope>"));
    new HttpClient().executeMethod(post);
    String res = post.getResponseBodyAsString();
    log.info(res);
    Element elem = transformer.toDOMElement(new StringSource(res));
    assertEquals(Soap11.getInstance().getEnvelope(), DomUtil.getQName(elem));
    elem = DomUtil.getFirstChildElement(elem);
    assertEquals(Soap11.getInstance().getBody(), DomUtil.getQName(elem));
    elem = DomUtil.getFirstChildElement(elem);
    assertEquals(Soap11.getInstance().getFault(), DomUtil.getQName(elem));
    elem = DomUtil.getFirstChildElement(elem);
    assertEquals(SoapConstants.SOAP_11_FAULTCODE, DomUtil.getQName(elem));
    assertEquals(SoapConstants.SOAP_11_CODE_CLIENT, DomUtil.createQName(elem, elem.getTextContent()));
    assertEquals(500, post.getStatusCode());
}

From source file:org.apache.servicemix.http.ConsumerEndpointTest.java

public void testHttpSoap11() throws Exception {
    initSoapEndpoints(true);//from w  ww.  j a  va  2 s  .  c o m

    MockServiceComponent echo = new MockServiceComponent();
    echo.setService(new QName("urn:test", "echo"));
    echo.setEndpoint("endpoint");
    echo.setResponseXml("<jbi:message xmlns:jbi='http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper'>"
            + "<jbi:part><HelloResponse xmlns='uri:HelloWorld' /></jbi:part>" + "</jbi:message>");
    container.activateComponent(echo, "echo");

    PostMethod post = new PostMethod("http://localhost:8192/ep1/");
    post.setRequestEntity(
            new StringRequestEntity("<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>"
                    + "<s:Header><HelloHeader xmlns='uri:HelloWorld'/></s:Header>"
                    + "<s:Body><HelloRequest xmlns='uri:HelloWorld'>world</HelloRequest></s:Body>"
                    + "</s:Envelope>"));
    new HttpClient().executeMethod(post);
    String res = post.getResponseBodyAsString();
    log.info(res);
    Element elem = transformer.toDOMElement(new StringSource(res));
    assertEquals(Soap11.getInstance().getEnvelope(), DomUtil.getQName(elem));
    elem = DomUtil.getFirstChildElement(elem);
    assertEquals(Soap11.getInstance().getBody(), DomUtil.getQName(elem));
    elem = DomUtil.getFirstChildElement(elem);
    assertEquals(new QName("uri:HelloWorld", "HelloResponse"), DomUtil.getQName(elem));
    assertEquals(200, post.getStatusCode());
}

From source file:org.apache.servicemix.http.ConsumerEndpointTest.java

public void testHttpSoap12() throws Exception {
    initSoapEndpoints(true);//from  w  w  w .  jav a 2  s  .c o m

    TransformComponentSupport mock = new TransformComponentSupport() {
        protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out)
                throws MessagingException {
            Element elem;
            try {
                elem = transformer.toDOMElement(in.getContent());
                log.info(transformer.toString(elem));
            } catch (Exception e) {
                throw new MessagingException(e);
            }
            assertEquals(JbiConstants.WSDL11_WRAPPER_MESSAGE, DomUtil.getQName(elem));
            out.setContent(
                    new StringSource("<jbi:message xmlns:jbi='http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper'>"
                            + "<jbi:part><HelloResponse xmlns='uri:HelloWorld'>world</HelloResponse></jbi:part>"
                            + "</jbi:message> "));
            return true;
        }
    };
    mock.setService(new QName("urn:test", "echo"));
    mock.setEndpoint("endpoint");
    container.activateComponent(mock, "mock");

    PostMethod post = new PostMethod("http://localhost:8192/ep2/");
    post.setRequestEntity(
            new StringRequestEntity("<s:Envelope xmlns:s='http://www.w3.org/2003/05/soap-envelope'>"
                    + "<s:Header><HelloHeader xmlns='uri:HelloWorld'/></s:Header>"
                    + "<s:Body><HelloRequest xmlns='uri:HelloWorld'>world</HelloRequest></s:Body>"
                    + "</s:Envelope>"));
    new HttpClient().executeMethod(post);
    String res = post.getResponseBodyAsString();
    log.info(res);
    Element elem = transformer.toDOMElement(new StringSource(res));
    assertEquals(Soap12.getInstance().getEnvelope(), DomUtil.getQName(elem));
    elem = DomUtil.getFirstChildElement(elem);
    assertEquals(Soap12.getInstance().getBody(), DomUtil.getQName(elem));
    elem = DomUtil.getFirstChildElement(elem);
    assertEquals(new QName("uri:HelloWorld", "HelloResponse"), DomUtil.getQName(elem));
    assertEquals(200, post.getStatusCode());
}

From source file:org.apache.servicemix.http.ConsumerEndpointTest.java

public void testHttpSoap12WithoutJbiWrapper() throws Exception {
    initSoapEndpoints(false);//  w  w  w .  j  a v a  2 s. co m

    TransformComponentSupport mock = new TransformComponentSupport() {
        protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out)
                throws MessagingException {
            Element elem;
            try {
                elem = transformer.toDOMElement(in.getContent());
                log.info(transformer.toString(elem));
            } catch (Exception e) {
                throw new MessagingException(e);
            }
            assertEquals(new QName("uri:HelloWorld", "HelloRequest"), DomUtil.getQName(elem));
            out.setContent(new StringSource("<HelloResponse xmlns='uri:HelloWorld'>world</HelloResponse>"));
            return true;
        }
    };
    mock.setCopyProperties(false);
    mock.setService(new QName("urn:test", "echo"));
    mock.setEndpoint("endpoint");
    container.activateComponent(mock, "mock");

    PostMethod post = new PostMethod("http://localhost:8192/ep2/");
    post.setRequestEntity(
            new StringRequestEntity("<s:Envelope xmlns:s='http://www.w3.org/2003/05/soap-envelope'>"
                    + "<s:Header><HelloHeader xmlns='uri:HelloWorld'/></s:Header>"
                    + "<s:Body><HelloRequest xmlns='uri:HelloWorld'>world</HelloRequest></s:Body>"
                    + "</s:Envelope>"));
    new HttpClient().executeMethod(post);
    String res = post.getResponseBodyAsString();
    log.info(res);
    Element elem = transformer.toDOMElement(new StringSource(res));
    assertEquals(Soap12.getInstance().getEnvelope(), DomUtil.getQName(elem));
    elem = DomUtil.getFirstChildElement(elem);
    assertEquals(Soap12.getInstance().getBody(), DomUtil.getQName(elem));
    elem = DomUtil.getFirstChildElement(elem);
    assertEquals(new QName("uri:HelloWorld", "HelloResponse"), DomUtil.getQName(elem));
    assertEquals(200, post.getStatusCode());
}

From source file:org.apache.servicemix.http.ConsumerEndpointTest.java

public void testHttpInOutUnderLoad() throws Exception {
    final int nbThreads = 16;
    final int nbRequests = 8;
    final int endpointTimeout = 100;
    final int echoSleepTime = 90;
    final int soTimeout = 60 * 1000 * 1000;
    final int listenerTimeout = 5000;

    ExchangeCompletedListener listener = new ExchangeCompletedListener(listenerTimeout);
    container.addListener(listener);//from w  w  w  . ja  v  a2  s  .  c  o m

    HttpComponent http = new HttpComponent();
    //http.getConfiguration().setJettyConnectorClassName(SocketConnector.class.getName());
    HttpConsumerEndpoint ep = new HttpConsumerEndpoint();
    ep.setService(new QName("urn:test", "svc"));
    ep.setEndpoint("ep");
    ep.setTargetService(new QName("urn:test", "echo"));
    ep.setLocationURI("http://localhost:8192/ep1/");
    ep.setTimeout(endpointTimeout);
    http.setEndpoints(new HttpEndpointType[] { ep });
    container.activateComponent(http, "http");

    final CountDownLatch latchRecv = new CountDownLatch(nbThreads * nbRequests);
    EchoComponent echo = new EchoComponent() {
        protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out)
                throws MessagingException {
            latchRecv.countDown();
            try {
                Thread.sleep(echoSleepTime);
            } catch (InterruptedException e) {
                e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }
            out.setContent(in.getContent());
            return true;
        }
    };
    echo.setService(new QName("urn:test", "echo"));
    echo.setEndpoint("endpoint");
    container.activateComponent(echo, "echo");

    ((ExecutorFactoryImpl) container.getExecutorFactory()).getDefaultConfig().setMaximumPoolSize(16);

    container.start();

    final List<Throwable> throwables = new CopyOnWriteArrayList<Throwable>();
    final CountDownLatch latchSent = new CountDownLatch(nbThreads * nbRequests);
    for (int t = 0; t < nbThreads; t++) {
        new Thread() {
            public void run() {
                final SourceTransformer transformer = new SourceTransformer();
                final HttpClient client = new HttpClient();
                client.getParams().setSoTimeout(soTimeout);
                for (int i = 0; i < nbRequests; i++) {
                    try {
                        PostMethod post = new PostMethod("http://localhost:8192/ep1/");
                        post.setRequestEntity(new StringRequestEntity("<hello>world</hello>"));
                        client.executeMethod(post);
                        if (post.getStatusCode() != 200) {
                            throw new InvalidStatusResponseException(post.getStatusCode());
                        }
                        Node node = transformer.toDOMNode(new StreamSource(post.getResponseBodyAsStream()));
                        log.info(transformer.toString(node));
                        assertEquals("world", textValueOfXPath(node, "/hello/text()"));
                    } catch (Throwable t) {
                        throwables.add(t);
                    } finally {
                        latchSent.countDown();
                        //System.out.println("[" + System.currentTimeMillis() + "] Request " + latch.getCount() + " processed");
                    }
                }
            }
        }.start();
    }
    latchSent.await();
    latchRecv.await();
    listener.assertExchangeCompleted();
    for (Throwable t : throwables) {
        t.printStackTrace();
    }
}

From source file:org.apache.servicemix.http.HttpManagedTest.java

public void test() throws Exception {
    ContextHandler context = new ContextHandler();
    context.setContextPath("/test");
    context.setEventListeners(new EventListener[] { new ContextLoaderListener() });
    Map initParams = new HashMap();
    initParams.put("contextConfigLocation", "classpath:org/apache/servicemix/http/spring-web.xml");
    initParams.put("contextClass", XmlWebApplicationContext.class.getName());
    context.setInitParams(initParams);//ww w . j a  v  a 2  s  .c o  m
    ServletHolder holder = new ServletHolder();
    holder.setName("jbiServlet");
    holder.setClassName(HttpManagedServlet.class.getName());
    ServletHandler handler = new ServletHandler();
    handler.setServlets(new ServletHolder[] { holder });
    ServletMapping mapping = new ServletMapping();
    mapping.setServletName("jbiServlet");
    mapping.setPathSpec("/*");
    handler.setServletMappings(new ServletMapping[] { mapping });
    context.setHandler(handler);

    ContextHandlerCollection contexts = new ContextHandlerCollection();
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] { contexts });
    contexts.addHandler(context);

    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setHost("localhost");
    connector.setPort(8190);

    server = new Server();
    server.setConnectors(new Connector[] { connector });
    server.setHandler(handlers);
    server.start();

    logger.info("Started");

    PostMethod post = new PostMethod("http://localhost:8190/test/jbi/Service/");
    post.setRequestEntity(
            new StringRequestEntity("<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'>"
                    + "<soap:Body><hello>world</hello></soap:Body>" + "</soap:Envelope>"));
    new HttpClient().executeMethod(post);
    if (post.getStatusCode() != 200) {
        throw new InvalidStatusResponseException(post.getStatusCode());
    }
    logger.info(post.getResponseBodyAsString());

}

From source file:org.apache.servicemix.http.HttpSoapTest.java

public void testFaultOnParse() throws Exception {
    HttpComponent http = new HttpComponent();
    HttpEndpoint ep = new HttpEndpoint();
    ep.setService(new QName("urn:test", "echo"));
    ep.setEndpoint("echo");
    ep.setLocationURI("http://localhost:8192/ep1/");
    ep.setRoleAsString("consumer");
    ep.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out"));
    ep.setSoap(true);//from w ww .  j a  v a2 s  . co  m
    http.setEndpoints(new HttpEndpoint[] { ep });
    container.activateComponent(http, "http");
    container.start();

    PostMethod method = new PostMethod("http://localhost:8192/ep1/");
    method.setRequestEntity(new StringRequestEntity("<hello>world</hello>"));
    int state = new HttpClient().executeMethod(method);
    assertEquals(HttpServletResponse.SC_BAD_REQUEST, state);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FileUtil.copyInputStream(method.getResponseBodyAsStream(), baos);
    logger.info(baos.toString());
}

From source file:org.apache.servicemix.http.HttpSoapTest.java

public void testSoapRoundtripProviderConsumerProvider() throws Exception {
    EchoComponent echo = new EchoComponent();
    echo.setService(new QName("urn:test", "echo"));
    echo.setEndpoint("echo");
    container.activateComponent(echo, "echo");

    HttpComponent http = new HttpComponent();

    HttpEndpoint ep1 = new HttpEndpoint();
    ep1.setService(new QName("urn:test", "s1"));
    ep1.setEndpoint("ep1");
    ep1.setTargetService(new QName("urn:test", "s2"));
    ep1.setLocationURI("http://localhost:8192/ep1/");
    ep1.setRoleAsString("consumer");
    ep1.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out"));
    ep1.setSoap(true);//from w w w  .  j  a va  2 s .c  om

    HttpEndpoint ep2 = new HttpEndpoint();
    ep2.setService(new QName("urn:test", "s2"));
    ep2.setEndpoint("ep2");
    ep2.setLocationURI("http://localhost:8192/ep3/");
    ep2.setRoleAsString("provider");
    ep2.setSoap(true);

    HttpEndpoint ep3 = new HttpEndpoint();
    ep3.setService(new QName("urn:test", "s3"));
    ep3.setEndpoint("ep3");
    ep3.setTargetService(new QName("urn:test", "echo"));
    ep3.setLocationURI("http://localhost:8192/ep3/");
    ep3.setRoleAsString("consumer");
    ep3.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out"));
    ep3.setSoap(true);

    http.setEndpoints(new HttpEndpoint[] { ep1, ep2, ep3 });

    container.activateComponent(http, "http");

    container.start();

    PostMethod method = new PostMethod("http://localhost:8192/ep1/");
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {
        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            return false;
        }
    });
    method.setRequestEntity(
            new StringRequestEntity("<env:Envelope xmlns:env='http://www.w3.org/2003/05/soap-envelope'>"
                    + "<env:Body><hello>world</hello></env:body>" + "</env:Envelope>"));
    int state = new HttpClient().executeMethod(method);
    assertEquals(HttpServletResponse.SC_OK, state);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FileUtil.copyInputStream(method.getResponseBodyAsStream(), baos);
    logger.info(baos.toString());
}

From source file:org.apache.servicemix.http.security.HttpSecurityTest.java

protected void testAuthenticate(final String username, final String password) throws Exception {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(new AuthScope(AuthScope.ANY),
            new UsernamePasswordCredentials(username, password));

    PostMethod method = new PostMethod("http://localhost:8192/Service/");
    try {/*www .j a va2 s . c  o m*/
        method.setDoAuthentication(true);
        method.setRequestEntity(new StringRequestEntity("<hello>world</hello>"));
        int state = client.executeMethod(method);
        FileUtil.copyInputStream(method.getResponseBodyAsStream(), System.err);
        if (state != HttpServletResponse.SC_OK && state != HttpServletResponse.SC_ACCEPTED) {
            throw new IllegalStateException("Http status: " + state);
        }
    } finally {
        method.releaseConnection();
    }
}

From source file:org.apache.servicemix.http.security.HttpSecurityTest.java

public void testWSSec() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    FileUtil.copyInputStream(getClass().getResourceAsStream("request.xml"), out);
    String request = out.toString();
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod("http://localhost:8192/WSSec/");
    try {/* w  w  w  .ja v a 2 s  . c om*/
        method.setDoAuthentication(true);
        method.setRequestEntity(new StringRequestEntity(request));
        int state = client.executeMethod(method);

        String str = method.getResponseBodyAsString();
        log.info(str);
        if (state != HttpServletResponse.SC_OK && state != HttpServletResponse.SC_ACCEPTED) {
            throw new IllegalStateException("Http status: " + state);
        }
    } finally {
        method.releaseConnection();
    }
}