Search in sources :

Example 1 with WriterOutputStream

use of org.apache.commons.io.output.WriterOutputStream in project karaf by apache.

the class ShellTableTest method testNoFormat.

@Test
public void testNoFormat() {
    ShellTable table = new ShellTable();
    table.column(new Col("first"));
    table.column(new Col("second"));
    table.addRow().addContent("first column", "second column");
    StringWriter writer = new StringWriter();
    PrintStream out = new PrintStream(new WriterOutputStream(writer));
    table.print(out, false);
    out.flush();
    String expected = "first column\tsecond column\n";
    Assert.assertEquals(expected, getString(writer));
}
Also used : Col(org.apache.karaf.shell.table.Col) PrintStream(java.io.PrintStream) ShellTable(org.apache.karaf.shell.table.ShellTable) StringWriter(java.io.StringWriter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) Test(org.junit.Test)

Example 2 with WriterOutputStream

use of org.apache.commons.io.output.WriterOutputStream in project sling by apache.

the class JSONReporterTest method getJSON.

private static Map<String, Object> getJSON(MetricRegistry registry) throws IOException {
    StringWriter sw = new StringWriter();
    JSONReporter reporter = JSONReporter.forRegistry(registry).outputTo(new PrintStream(new WriterOutputStream(sw))).build();
    reporter.report();
    reporter.close();
    return new JSONParser(sw.toString()).getParsed();
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) JSONParser(org.apache.felix.utils.json.JSONParser) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream)

Example 3 with WriterOutputStream

use of org.apache.commons.io.output.WriterOutputStream in project wso2-axis2-transports by wso2.

the class MailUtils method setupLogging.

public static void setupLogging(Session session, Log log, ParameterInclude params) throws AxisFault {
    // take care to override it.
    if (log.isTraceEnabled()) {
        // This is the old behavior: just set debug to true
        session.setDebug(true);
    }
    if (ParamUtils.getOptionalParamBoolean(params, MailConstants.TRANSPORT_MAIL_DEBUG, false)) {
        // Redirect debug output to where it belongs, namely to the logs!
        session.setDebugOut(new PrintStream(new WriterOutputStream(new LogWriter(log)), true));
        // Only enable debug afterwards since the call to setDebug might already cause debug output
        session.setDebug(true);
    }
}
Also used : PrintStream(java.io.PrintStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream)

Example 4 with WriterOutputStream

use of org.apache.commons.io.output.WriterOutputStream in project wso2-axis2-transports by wso2.

the class MqttSender method createMqttMessage.

private MqttMessage createMqttMessage(MessageContext messageContext) {
    OMOutputFormat format = BaseUtils.getOMOutputFormat(messageContext);
    MessageFormatter messageFormatter;
    try {
        messageFormatter = MessageProcessorSelector.getMessageFormatter(messageContext);
    } catch (AxisFault axisFault) {
        throw new AxisMqttException("Unable to get the message formatter to use");
    }
    OutputStream out;
    StringWriter sw = new StringWriter();
    try {
        out = new WriterOutputStream(sw, format.getCharSetEncoding());
    } catch (UnsupportedCharsetException ex) {
        throw new AxisMqttException("Unsupported encoding " + format.getCharSetEncoding(), ex);
    }
    try {
        messageFormatter.writeTo(messageContext, format, out, true);
        out.close();
    } catch (IOException e) {
        throw new AxisMqttException("IO Error while creating BytesMessage", e);
    }
    MqttMessage mqttMessage = new MqttMessage();
    mqttMessage.setPayload(sw.toString().getBytes());
    return mqttMessage;
}
Also used : AxisFault(org.apache.axis2.AxisFault) StringWriter(java.io.StringWriter) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) OutputStream(java.io.OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream)

Example 5 with WriterOutputStream

use of org.apache.commons.io.output.WriterOutputStream in project wso2-axis2-transports by wso2.

the class MSMQSender method createMSMQMessage.

/**
 * Generating MSMQ message wrapper in order to communicate with JNI
 * @param msgCtx
 * @param contentTypeProperty
 * @param queuName
 * @return
 * @throws AxisFault
 */
private Message createMSMQMessage(MessageContext msgCtx, String messageContentType, String queuName) throws AxisFault {
    String msgBody = null;
    byte[] msgByteBody = null;
    String msgType = getProperty(msgCtx, MSMQConstants.MSMQ_MESSAGE_TYPE);
    // String msgLable = "L:" + queuName+"["+contentType+"]"; // TODO
    // check the first element of the SOAP body, do we have content wrapped
    // using the
    // default wrapper elements for binary
    // (BaseConstants.DEFAULT_BINARY_WRAPPER) or
    // text (BaseConstants.DEFAULT_TEXT_WRAPPER) ? If so, do not create SOAP
    // messages
    // for MSMQ but just get the payload in its native format
    String msmqMessageType = guessMessageType(msgCtx);
    if (msmqMessageType == null) {
        OMOutputFormat format = BaseUtils.getOMOutputFormat(msgCtx);
        MessageFormatter messageFormatter = null;
        try {
            messageFormatter = MessageProcessorSelector.getMessageFormatter(msgCtx);
        } catch (AxisFault axisFault) {
            handleException("Unable to get the message formatter to use", axisFault);
        }
        String contentType = messageFormatter != null ? messageFormatter.getContentType(msgCtx, format, msgCtx.getSoapAction()) : "";
        boolean useBytesMessage = msgType != null && MSMQConstants.MSMQ_BYTE_MESSAGE.equals(msgType) || contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) > -1;
        OutputStream out = null;
        StringWriter sw = null;
        if (useBytesMessage) {
        // TODO: handle MSMQ byte message here
        } else {
            sw = new StringWriter();
            try {
                out = new WriterOutputStream(sw, format.getCharSetEncoding());
            } catch (UnsupportedCharsetException ex) {
                handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
            }
        }
        try {
            if (out != null) {
                messageFormatter.writeTo(msgCtx, format, out, true);
                out.close();
            }
        } catch (IOException e) {
            handleException("IO Error while creating BytesMessage", e);
        }
        if (!useBytesMessage) {
            msgBody = sw.toString();
        }
    } else if (MSMQConstants.MSMQ_BYTE_MESSAGE.equals(msmqMessageType)) {
    // TODO. handle .net byte messages here.
    } else if (MSMQConstants.MSMQ_TEXT_MESSAGE.equals(msmqMessageType)) {
        msgBody = msgCtx.getEnvelope().getBody().getFirstChildWithName(BaseConstants.DEFAULT_TEXT_WRAPPER).getText();
    }
    try {
        // Keep message correlation empty will be deciding later on the process
        Message message = new Message(msgBody != null ? msgBody : "", "", "");
        return message;
    } catch (UnsupportedEncodingException e) {
        log.error("Unsported message has been received: ", e);
        handleEexception("Unsported message has been received: ", e);
    }
    return null;
}
Also used : AxisFault(org.apache.axis2.AxisFault) Message(org.apache.axis2.transport.msmq.util.Message) OutputStream(java.io.OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) StringWriter(java.io.StringWriter) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Aggregations

WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)149 StringWriter (java.io.StringWriter)142 PrintStream (java.io.PrintStream)137 Test (org.junit.Test)132 LogConfig (io.restassured.config.LogConfig)46 RequestLoggingFilter (io.restassured.filter.log.RequestLoggingFilter)44 ResponseBuilder (io.restassured.builder.ResponseBuilder)36 Filter (io.restassured.filter.Filter)35 FilterContext (io.restassured.filter.FilterContext)35 FilterableRequestSpecification (io.restassured.specification.FilterableRequestSpecification)35 FilterableResponseSpecification (io.restassured.specification.FilterableResponseSpecification)35 IOException (java.io.IOException)6 Col (org.apache.karaf.shell.table.Col)6 ShellTable (org.apache.karaf.shell.table.ShellTable)6 OutputStream (java.io.OutputStream)5 RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)4 ResponseLoggingFilter (io.restassured.filter.log.ResponseLoggingFilter)4 InputStream (java.io.InputStream)4 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)3 ScalatraObject (io.restassured.itest.java.objects.ScalatraObject)3