Example usage for java.io OutputStream toString

List of usage examples for java.io OutputStream toString

Introduction

In this page you can find the example usage for java.io OutputStream toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:com.comcast.cmb.test.tools.CNSTestingUtils.java

public static void deleteallSubscriptions(CNSControllerServlet cns, User user, OutputStream out) {
    //Get all topics

    try {/*  ww w.  ja v  a2 s  .  c  o  m*/
        listSubscriptions(cns, user, out, null);
    } catch (Exception e) {
        logger.error("Exception", e);
        assertTrue(false);
    }
    String res = out.toString();
    Vector<CNSSubscriptionTest> subs = CNSTestingUtils.getSubscriptionsFromString(res);

    //Delete all topics
    //System.out.println("Deleting arns size:" + arns.size());

    try {
        for (CNSSubscriptionTest sub : subs) {
            //System.out.println("Deleting arn:" + arn);
            String subscriptionArn = sub.getSubscriptionArn();
            if (!subscriptionArn.equals("PendingConfirmation")) {
                CNSTestingUtils.unSubscribe(cns, user, out, subscriptionArn);
            }

        }
    } catch (Exception e) {
        logger.error("Exception", e);
        assertFalse(true);
    }
}

From source file:nl.surfnet.coin.mock.MockSoapServerTest.java

/**
 * Test the MockHttpServer./*from   w  ww.  ja v  a2  s  . c o  m*/
 * 
 * @throws Exception
 */
@Test
public void testMockHappyFlow() throws Exception {
    ClassPathResource responseResource = new ClassPathResource("test.json");
    super.setResponseResource(responseResource);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(new HttpGet("http://localhost:8088/testUrl"));
    String contentType = response.getHeaders("Content-Type")[0].getValue();
    Assert.assertEquals("application/json", contentType);
    InputStream is = response.getEntity().getContent();
    OutputStream output = new ByteArrayOutputStream();
    IOUtils.copy(is, output);
    Assert.assertEquals(IOUtils.toString(responseResource.getInputStream()), output.toString());
}

From source file:org.apache.synapse.commons.json.JsonValueTest.java

public void runTest(String value) {
    try {// w w w. j a  v  a2s. co m
        InputStream inputStream = Util.newInputStream(value.getBytes());
        MessageContext messageContext = Util.newMessageContext();
        OMElement element = JsonUtil.getNewJsonPayload(messageContext, inputStream, true, true);
        OutputStream out = Util.newOutputStream();
        JsonUtil.writeAsJson(element, out);
        assertTrue(value.equals(out.toString()));
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
        assertTrue(false);
    }
}

From source file:org.paolomoz.zehnkampf.utils.TestGenHTTPResponse.java

public void testWriteResponse() throws IOException {
    HttpEntity entity = new StringEntity(entityString);

    HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_OK, "OK");
    response.setHeader("Content-length", new Integer(entityString.length()).toString());
    response.setEntity(entity);//  w w  w  . jav  a  2s.c om

    assertEquals(1, response.getAllHeaders().length);
    assertEquals("HTTP/1.1 200 OK", response.getStatusLine().toString());

    OutputStream out = new ByteArrayOutputStream();
    genHTTPResp.writeResponse(response, out);

    assertEquals("HTTP/1.1 200 OK\r\n" + "Content-length: 4\r\n" + "\r\n" + "test", out.toString());
}

From source file:io.swagger.inflector.processors.JacksonProcessor.java

@Override
public Object process(MediaType mediaType, InputStream entityStream, Class<?> cls) throws ConversionException {
    try {/*from w  w  w.  j av a2 s .  c o m*/
        if (String.class.equals(cls)) {
            OutputStream outputStream = new ByteArrayOutputStream();
            IOUtils.copy(entityStream, outputStream);
            return outputStream.toString();
        }
        if (MediaType.APPLICATION_JSON_TYPE.isCompatible(mediaType)) {
            return Json.mapper().readValue(entityStream, cls);
        }
        if (MediaType.APPLICATION_XML_TYPE.isCompatible(mediaType)) {
            return XML.readValue(entityStream, cls);
        }
        if (APPLICATION_YAML_TYPE.isCompatible(mediaType)) {
            return Yaml.mapper().readValue(entityStream, cls);
        }
    } catch (Exception e) {
        LOGGER.trace(
                "unable to extract entity from content-type `" + mediaType + "` to " + cls.getCanonicalName(),
                e);
        throw new ConversionException().message(new ValidationMessage().code(ValidationError.UNACCEPTABLE_VALUE)
                .message("unable to convert input to " + cls.getCanonicalName()));
    }

    return null;
}

From source file:org.pentaho.platform.engine.services.SoapHelperTest.java

/**
 * Tests for SoapHelper.createSoapResponseDocument(IRuntimeContext, IOutputHandler, OutputStream, List)
 *//*from   ww w.  j a v  a  2  s  .c o m*/
public void testCreateSoapResponseDocumentFromContext() {
    //Set up test data
    Set<Object> outputNames = new HashSet<>();

    IActionParameter actionParameter = mock(IActionParameter.class);
    when(actionParameter.getValue()).thenReturn("testValue");

    IRuntimeContext context = mock(IRuntimeContext.class);
    when(context.getOutputNames()).thenReturn(outputNames);
    when(context.getStatus()).thenReturn(IRuntimeContext.RUNTIME_STATUS_SUCCESS);
    when(context.getOutputParameter(anyString())).thenReturn(actionParameter);
    List messages = new ArrayList();

    IOutputHandler outputHandler = mock(IOutputHandler.class);
    IContentItem contentItem = mock(IContentItem.class);
    when(outputHandler.getOutputContentItem(anyString(), anyString(), anyString(), anyString()))
            .thenReturn(contentItem);
    when(contentItem.getMimeType()).thenReturn("text/xml");

    OutputStream contentStream = mock(OutputStream.class);
    when(contentStream.toString()).thenReturn("contentStreamTestString");

    //Tests for document with two output names
    outputNames.add("outputName1");
    outputNames.add("outputName2");
    Document d1 = SoapHelper.createSoapResponseDocument(context, outputHandler, contentStream, messages);
    Element activityResponse1 = d1.getRootElement().element("SOAP-ENV:Body").element("ExecuteActivityResponse");
    assertEquals(activityResponse1.elements().size(), 2);
    assertNotNull(activityResponse1.element("outputName1"));
    assertNotNull(activityResponse1.element("outputName2"));

    //Test for document with one output name (different branch in createSoapResponseDocument())
    outputNames.clear();
    outputNames.add("outputName1");
    Document d2 = SoapHelper.createSoapResponseDocument(context, outputHandler, contentStream, messages);
    Element activityResponse2 = d2.getRootElement().element("SOAP-ENV:Body").element("ExecuteActivityResponse");
    assertEquals(activityResponse2.elements().size(), 1);
    assertNotNull(activityResponse2.element("outputName1"));

    //Tests for document with no output names
    outputNames.clear();
    Document d3 = SoapHelper.createSoapResponseDocument(context, outputHandler, contentStream, messages);
    Element activityResponse3 = d3.getRootElement().element("SOAP-ENV:Body").element("ExecuteActivityResponse");
    assertTrue(activityResponse3.elements().isEmpty());
}

From source file:org.kie.camel.component.cxf.CxfSoapTest.java

@Test
public void test1() throws Exception {
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody();
    QName payloadName = new QName("http://soap.jax.drools.org", "execute", "ns1");

    body.addBodyElement(payloadName);//from   w w w .  j  a  va  2  s  .  c  o  m

    String cmd = "";
    cmd += "<batch-execution lookup=\"ksession1\">\n";
    cmd += "  <insert out-identifier=\"salaboy\" disconnected=\"true\">\n";
    cmd += "      <org.kie.pipeline.camel.Person>\n";
    cmd += "         <name>salaboy</name>\n";
    cmd += "         <age>27</age>\n";
    cmd += "      </org.kie.pipeline.camel.Person>\n";
    cmd += "   </insert>\n";
    cmd += "   <fire-all-rules/>\n";
    cmd += "</batch-execution>\n";

    body.addTextNode(cmd);

    Object object = this.context.createProducerTemplate().requestBody("direct://http", soapMessage);
    OutputStream out = new ByteArrayOutputStream();
    out = new ByteArrayOutputStream();
    soapMessage = (SOAPMessage) object;
    soapMessage.writeTo(out);
    String response = out.toString();
    assertTrue(response.contains("fact-handle identifier=\"salaboy\""));
}

From source file:com.nortal.jroad.client.util.WSConsumptionLoggingInterceptor.java

private boolean logMessage(MessageContext mc, MessageType messageType) {
    if (log.isDebugEnabled()) {
        WebServiceMessage message = MessageType.REQUEST.equals(messageType) ? mc.getRequest()
                : mc.getResponse();//  w  ww  .ja v  a2 s. c  o m

        if (message instanceof SaajSoapMessage) {
            OutputStream out = new ByteArrayOutputStream();
            try {
                ((SaajSoapMessage) message).writeTo(out);
                log.debug(messageType + " message follows:\n" + out.toString());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    return true;
}

From source file:nl.surfnet.coin.util.CustomMappingJacksonHttpMessageConverterTest.java

/**
 * Test method for// w  w  w .ja v a2 s. co  m
 * {@link nl.surfnet.coin.util.CustomMappingJacksonHttpMessageConverter#CustomMappingJacksonHttpMessageConverter()}
 * .
 */
@Test
public void testCustomMappingJacksonHttpMessageConverter()
        throws JsonGenerationException, JsonMappingException, IOException {
    CustomMappingJacksonHttpMessageConverter convert = new CustomMappingJacksonHttpMessageConverter();
    OutputStream out = new ByteArrayOutputStream();
    Person value = new Person();
    value.setError("error");
    convert.getObjectMapper().writeValue(out, value);
    assertEquals("{\"error\":\"error\"}", out.toString());
}

From source file:org.drools.camel.component.cxf.CxfSoapTest.java

@Test
public void test1() throws Exception {

    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody();
    QName payloadName = new QName("http://soap.jax.drools.org", "execute", "ns1");

    body.addBodyElement(payloadName);//from  ww w .j a v a  2  s .com

    String cmd = "";
    cmd += "<batch-execution lookup=\"ksession1\">\n";
    cmd += "  <insert out-identifier=\"salaboy\" disconnected=\"true\">\n";
    cmd += "      <org.drools.pipeline.camel.Person>\n";
    cmd += "         <name>salaboy</name>\n";
    cmd += "         <age>27</age>\n";
    cmd += "      </org.drools.pipeline.camel.Person>\n";
    cmd += "   </insert>\n";
    cmd += "   <fire-all-rules/>\n";
    cmd += "</batch-execution>\n";

    body.addTextNode(cmd);

    Object object = this.context.createProducerTemplate().requestBody("direct://http", soapMessage);

    OutputStream out = new ByteArrayOutputStream();
    out = new ByteArrayOutputStream();
    soapMessage = (SOAPMessage) object;
    soapMessage.writeTo(out);
    String response = out.toString();
    assertTrue(response.contains("fact-handle identifier=\"salaboy\""));
}