Example usage for javax.activation DataSource getInputStream

List of usage examples for javax.activation DataSource getInputStream

Introduction

In this page you can find the example usage for javax.activation DataSource getInputStream.

Prototype

public InputStream getInputStream() throws IOException;

Source Link

Document

This method returns an InputStream representing the data and throws the appropriate exception if it can not do so.

Usage

From source file:org.mule.ibeans.flickr.FlickrSignatureFactory.java

public Object create(String paramName, boolean optional, InvocationContext invocationContext) {
    String secretKey = (String) invocationContext.getIBeanConfig().getPropertyParams().get("secret_key");
    if (secretKey == null) {
        throw new IllegalArgumentException(
                "A Flickr secret key must be set using one of the init methods on this iBeans");
    }//from   w  ww  . j  ava 2 s.com
    String sig;

    StringBuffer buf = new StringBuffer();
    buf.append(secretKey);

    try {
        //Need to find a cleaner way of doing this for users
        if ("GET".equals(invocationContext.getIBeanConfig().getPropertyParams().get("http.method"))
                || invocationContext.isTemplateMethod()) {
            Map<String, String> params;
            if (invocationContext.isTemplateMethod()) {
                params = invocationContext.getTemplateSpecificUriParams();
            } else {
                params = invocationContext.getCallSpecificUriParams();
            }
            for (Map.Entry<String, String> entry : params.entrySet()) {
                //Always delete the param for this factory. Also photo should be removed
                if (entry.getKey().equals(paramName) || entry.getKey().equals("photo")) {
                    continue;
                } else {
                    buf.append(entry.getKey()).append(entry.getValue());
                }
            }
        } else {
            for (DataSource ds : invocationContext.getIBeanConfig().getAttachments()) {
                if (ds.getName().equals("photo") || ds.getName().equals("api_sig")) {
                    continue;
                }
                buf.append(ds.getName()).append(IOUtils.toCharArray(ds.getInputStream()));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    sig = buf.toString();

    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] bytes = md5.digest(buf.toString().getBytes("UTF-8"));
        sig = StringUtils.toHexString(bytes);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return sig;
}

From source file:org.onesec.raven.ivr.actions.StartRecordingActionTest.java

@Test
public void test() throws Exception {
    MocksControl control = new MocksControl(MocksControl.MockType.NICE);
    IvrEndpointConversation conv = control.createMock(IvrEndpointConversation.class);
    ConversationScenarioState state = control.createMock(ConversationScenarioState.class);
    Bindings bindings = control.createMock(Bindings.class);
    IncomingRtpStream inRtp = control.createMock(IncomingRtpStream.class);

    expect(conv.getConversationScenarioState()).andReturn(state).atLeastOnce();
    expect(state.getBindings()).andReturn(bindings).atLeastOnce();
    expect(bindings.get(BindingNames.DATA_CONTEXT_BINDING)).andReturn(null);
    expect(bindings.get(StartRecordingAction.RECORDER_BINDING)).andReturn(null);
    expect(conv.getOwner()).andReturn(actionNode);
    expect(conv.getExecutorService()).andReturn(executor).atLeastOnce();
    expect(conv.getIncomingRtpStream()).andReturn(inRtp);
    expect(inRtp.addDataSourceListener(checkRtpListener(codecManager, executor, actionNode, bufferCache),
            isNull(AudioFormat.class))).andReturn(true);
    state.setBinding(eq(StartRecordingAction.RECORDER_BINDING), isA(Recorder.class),
            eq(BindingScope.CONVERSATION));
    expect(bindings.remove(StartRecordingAction.RECORDER_BINDING)).andReturn(null);

    control.replay();/*from   ww  w. j ava 2  s.  c  o m*/

    StartRecordingAction action = (StartRecordingAction) actionNode.createAction();
    assertNotNull(action);
    action.doExecute(conv);
    Thread.sleep(1000);
    assertNotNull(recorder);
    recorder.stopRecording(false);

    assertEquals(1, collector.getDataListSize());
    assertTrue(collector.getDataList().get(0) instanceof DataSource);
    DataSource ds = (DataSource) collector.getDataList().get(0);
    FileOutputStream out = new FileOutputStream("target/test.wav");
    IOUtils.copy(ds.getInputStream(), out);
    out.close();
    //        while (action.)
    control.verify();
}

From source file:org.openehealth.ipf.platform.camel.lbs.core.converter.LbsConverter.java

/**
 * Converts a data source to an input stream
 * @param dataSource//from  w  w w  .  j a va2s  . c om
 *          the data source
 * @return the input stream. The stream must be closed by the caller of the converter.
 *          This also applies when using the converter implicitly via 
 *          {@link org.apache.camel.Message#getBody(Class))}. Example:
 *          <blockquote><pre><code>
 *          InputStream input = exchange.getIn().getBody(InputStream.class);
 *          try {
 *              ...
 *          }
 *          finally {
 *              input.close();
 *          }
 *          </code></pre></blockquote>
 * @throws IOException
 *          if an {@code IOException} was thrown by the data source
 */
@Converter
public static InputStream toInputStream(DataSource dataSource) throws IOException {
    log.debug("converted data source to input stream: " + dataSource);
    return dataSource.getInputStream();
}

From source file:org.openehealth.ipf.platform.camel.lbs.core.converter.LbsConverter.java

/**
 * Converts a data source to a String/*from  w w  w.  j  a v  a  2  s. com*/
 * @param dataSource
 *          the data source
 * @return text that was contained in the data source
 * @throws IOException
 *          if an {@code IOException} was thrown by the data source
 */
@Converter
public static String toString(DataSource dataSource) throws IOException {
    InputStream inputStream = dataSource.getInputStream();
    try {
        String result = IOUtils.toString(inputStream);
        log.debug("converted data source to string: " + dataSource + " -> " + result);
        return result;
    } finally {
        inputStream.close();
    }
}

From source file:org.openehealth.ipf.platform.camel.lbs.core.converter.LbsConverter.java

/**
 * Converts a data source to a byte array
 * @param dataSource//from   w w w.  j  av a 2  s .  com
 *          the data source
 * @return byte array representing the content of the data source
 * @throws IOException
 *          if an {@code IOException} was thrown by the data source
 */
@Converter
public static byte[] toByteArray(DataSource dataSource) throws IOException {
    log.debug("converted data source to byte[]: " + dataSource);
    InputStream inputStream = dataSource.getInputStream();
    try {
        return IOUtils.toByteArray(inputStream);
    } finally {
        inputStream.close();
    }
}

From source file:org.openehealth.ipf.platform.camel.lbs.core.converter.LbsConverterTest.java

@Test
public void testConversionDataSource2InputStream() throws Exception {
    TextInputStream inputStream = new TextInputStream();
    DataSource dataSource = createMock(DataSource.class);
    expect(dataSource.getInputStream()).andReturn(inputStream);
    replay(dataSource);//from w  ww.j  a v  a2  s.c  om

    CamelContext camelContext = new DefaultCamelContext();
    DefaultExchange exchange = new DefaultExchange(camelContext);
    exchange.getIn().setBody(dataSource);
    InputStream resultInputStream = exchange.getIn().getMandatoryBody(InputStream.class);
    assertFalse(inputStream.isClosed());
    assertEquals("Hello World", IOUtils.toString(resultInputStream));

    verify(dataSource);
}

From source file:org.openehealth.ipf.platform.camel.lbs.core.converter.LbsConverterTest.java

@Test
public void testConversionDataSource2String() throws Exception {
    TextInputStream inputStream = new TextInputStream();
    DataSource dataSource = createMock(DataSource.class);
    expect(dataSource.getInputStream()).andReturn(inputStream);
    replay(dataSource);/*from  ww  w  . ja  va  2  s  .com*/

    CamelContext camelContext = new DefaultCamelContext();
    DefaultExchange exchange = new DefaultExchange(camelContext);
    exchange.getIn().setBody(dataSource);
    String result = exchange.getIn().getMandatoryBody(String.class);
    assertTrue(inputStream.isClosed());
    assertEquals("Hello World", result);

    verify(dataSource);
}

From source file:org.openehealth.ipf.platform.camel.lbs.core.converter.LbsConverterTest.java

@Test(expected = InvalidPayloadException.class)
public void testConversionDataSource2StringThrowsException() throws Exception {
    CorruptedInputStream inputStream = new CorruptedInputStream();
    DataSource dataSource = createMock(DataSource.class);
    expect(dataSource.getInputStream()).andReturn(inputStream);
    replay(dataSource);//from ww w  . ja  v a 2 s  .c  o m

    CamelContext camelContext = new DefaultCamelContext();
    DefaultExchange exchange = new DefaultExchange(camelContext);
    exchange.getIn().setBody(dataSource);
    try {
        exchange.getIn().getMandatoryBody(String.class);
    } finally {
        assertTrue(inputStream.isClosed());
        verify(dataSource);
    }
}

From source file:org.openehealth.ipf.platform.camel.lbs.core.converter.LbsConverterTest.java

@Test
public void testConversionDataSource2ByteArray() throws Exception {
    TextInputStream inputStream = new TextInputStream();
    DataSource dataSource = createMock(DataSource.class);
    expect(dataSource.getInputStream()).andReturn(inputStream);
    replay(dataSource);//from  w  w w.  ja v  a  2 s  .c o m

    CamelContext camelContext = new DefaultCamelContext();
    DefaultExchange exchange = new DefaultExchange(camelContext);
    exchange.getIn().setBody(dataSource);
    byte[] result = exchange.getIn().getMandatoryBody(byte[].class);
    assertTrue(inputStream.isClosed());
    assertTrue(Arrays.equals("Hello World".getBytes(), result));
    verify(dataSource);
}

From source file:org.openehealth.ipf.platform.camel.lbs.core.converter.LbsConverterTest.java

@Test(expected = InvalidPayloadException.class)
public void testConversionDataSource2ByteArrayThrowsException() throws Exception {
    CorruptedInputStream inputStream = new CorruptedInputStream();
    DataSource dataSource = createMock(DataSource.class);
    expect(dataSource.getInputStream()).andReturn(inputStream);
    replay(dataSource);//from  ww w  . ja v  a  2s.c om

    CamelContext camelContext = new DefaultCamelContext();
    DefaultExchange exchange = new DefaultExchange(camelContext);
    exchange.getIn().setBody(dataSource);
    try {
        exchange.getIn().getMandatoryBody(byte[].class);
    } finally {
        assertTrue(inputStream.isClosed());
        verify(dataSource);
    }
}