Example usage for javax.activation DataHandler getInputStream

List of usage examples for javax.activation DataHandler getInputStream

Introduction

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

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Get the InputStream for this object.

Usage

From source file:org.pentaho.platform.repository2.unified.webservices.jaxws.SimpleRepositoryFileDataDto.java

/**
 * Converts SimpleRepositoryFileDataDto to SimpleRepositoryFileData.
 *///from   w  w w  .  jav  a 2  s  .c  om
public static SimpleRepositoryFileData convert(final SimpleRepositoryFileDataDto simpleJaxWsData) {
    FileOutputStream fout = null;
    InputStream in = null;
    DataHandler dh = null;
    boolean foutClosed = false;
    try {
        File tmpFile = File.createTempFile("pentaho", null); //$NON-NLS-1$
        // TODO mlowery this might not delete files soon enough
        tmpFile.deleteOnExit();
        fout = FileUtils.openOutputStream(tmpFile);
        // used to cast to com.sun.xml.ws.developer.StreamingDataHandler here but that stopped working
        dh = simpleJaxWsData.dataHandler;
        // used to call dh.readOnce() (instead of dh.getInputStream()) here
        in = dh.getInputStream();
        IOUtils.copy(in, fout);
        fout.close();
        foutClosed = true;
        InputStream fin = new BufferedInputStream(FileUtils.openInputStream(tmpFile));
        return new SimpleRepositoryFileData(fin, simpleJaxWsData.encoding, simpleJaxWsData.mimeType);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            // close the streams
            if (in != null) {
                in.close();
            }
            // used to have to call dh.close() on the com.sun.xml.ws.developer.StreamingDataHandler here
            if (fout != null && !foutClosed) {
                fout.close();
            }
        } catch (Exception e) {
            // CHECKSTYLES IGNORE
        }
    }
}

From source file:org.apache.synapse.config.SynapseConfigUtils.java

public static InputStream getInputStream(Object o) {

    if (o == null) {
        handleException("Cannot convert null to a StreamSource");

    } else if (o instanceof OMElement) {
        OMElement omElement = (OMElement) o;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {/*w  ww  .jav a  2s  .co m*/
            omElement.serialize(baos);
            return new ByteArrayInputStream(baos.toByteArray());
        } catch (XMLStreamException e) {
            handleException("Error converting to a StreamSource", e);
        }

    } else if (o instanceof OMText) {
        DataHandler dataHandler = (DataHandler) ((OMText) o).getDataHandler();
        if (dataHandler != null) {
            try {
                return dataHandler.getInputStream();
            } catch (IOException e) {
                handleException("Error in reading content as a stream ");
            }
        }
    } else if (o instanceof URI) {
        try {
            return ((URI) (o)).toURL().openStream();
        } catch (IOException e) {
            handleException("Error opening stream form URI", e);
        }
    } else {
        handleException("Cannot convert object to a StreamSource");
    }
    return null;
}

From source file:org.apache.synapse.config.SynapseConfigUtils.java

/**
 * Return a StreamSource for the given Object
 *
 * @param o the object//  ww  w.  j  a va2  s. c  o  m
 * @return the StreamSource
 */
public static StreamSource getStreamSource(Object o) {

    if (o == null) {
        handleException("Cannot convert null to a StreamSource");

    } else if (o instanceof OMElement) {
        OMElement omElement = (OMElement) o;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            omElement.serialize(baos);
            return new StreamSource(new ByteArrayInputStream(baos.toByteArray()));
        } catch (XMLStreamException e) {
            handleException("Error converting to a StreamSource", e);
        }

    } else if (o instanceof OMText) {
        DataHandler dataHandler = (DataHandler) ((OMText) o).getDataHandler();
        if (dataHandler != null) {
            try {
                return new StreamSource(dataHandler.getInputStream());
            } catch (IOException e) {
                handleException("Error in reading content as a stream ");
            }
        }
    } else {

        handleException("Cannot convert object to a StreamSource");
    }
    return null;
}

From source file:org.openehealth.ipf.platform.camel.ihe.continua.hrn.converters.DataHandlerToByteArrayConverter.java

@Override
public byte[] convert(DataHandler source) {
    try {/*from  w w  w  .  j a va  2s.  co  m*/
        return IOUtils.toByteArray(source.getInputStream());
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:org.apache.axis.encoding.ser.ImageDataHandlerDeserializer.java

public void startElement(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {

    super.startElement(namespace, localName, prefix, attributes, context);

    if (getValue() instanceof DataHandler) {
        try {// ww  w  .  j ava 2s . c om
            DataHandler dh = (DataHandler) getValue();

            InputStream is = dh.getInputStream();
            Image image = ImageIOFactory.getImageIO().loadImage(is);
            setValue(image);
        } catch (Exception e) {
        }
    }
}

From source file:org.apache.axis.encoding.ser.OctetStreamDataHandlerDeserializer.java

public void startElement(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {

    super.startElement(namespace, localName, prefix, attributes, context);

    if (getValue() instanceof DataHandler) {
        try {//  ww w  .  j a  v a 2  s  . c  o m
            DataHandler dh = (DataHandler) getValue();
            InputStream in = dh.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int byte1 = -1;
            while ((byte1 = in.read()) != -1)
                baos.write(byte1);
            OctetStream os = new OctetStream(baos.toByteArray());
            setValue(os);
        } catch (IOException ioe) {
        }
    }
}

From source file:org.apache.axis.encoding.ser.SourceDataHandlerDeserializer.java

public void startElement(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {

    super.startElement(namespace, localName, prefix, attributes, context);

    if (getValue() instanceof DataHandler) {
        try {/*from   ww w. ja  va2 s . com*/
            DataHandler dh = (DataHandler) getValue();
            StreamSource ss = new StreamSource(dh.getInputStream());
            setValue(ss);
        } catch (IOException ioe) {
        }
    }
}

From source file:org.openehealth.ipf.platform.camel.lbs.cxf.process.GreeterImpl.java

private String toString(DataHandler onewayAttach) throws IOException {
    InputStream inputStream = onewayAttach.getInputStream();
    try {/*from   w  ww  . jav a2s.co m*/
        return IOUtils.toString(inputStream);
    } finally {
        inputStream.close();
    }
}

From source file:gov.nih.nci.caintegrator.application.study.deployment.GladSegmentationHandlerTest.java

@Before
public void setUp() throws Exception {
    genePatternClient = mock(GenePatternClient.class);
    when(genePatternClient.runAnalysis(anyString(), anyListOf(ParameterInfo.class))).thenReturn(new JobInfo());
    when(genePatternClient.getStatus(any(JobInfo.class))).thenAnswer(new Answer<JobInfo>() {
        @Override/*from w ww.  ja va2 s  .  c  om*/
        public JobInfo answer(InvocationOnMock invocation) throws Throwable {
            JobInfo jobInfo = (JobInfo) invocation.getArguments()[0];
            jobInfo.setStatus("Completed");
            return jobInfo;
        }
    });
    when(genePatternClient.getResultFile(any(JobInfo.class), anyString())).thenAnswer(new Answer<File>() {
        @Override
        public File answer(InvocationOnMock invocation) throws Throwable {
            File outputFile = File.createTempFile("output", ".glad");
            DataHandler dataHandler = mock(DataHandler.class);
            when(dataHandler.getInputStream()).thenReturn(new ByteArrayInputStream(TEST_OUTPUT.getBytes()));
            FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
            IOUtils.copy(dataHandler.getInputStream(), fileOutputStream);
            fileOutputStream.close();
            return outputFile;
        }
    });
    handler = new GladSegmentationHandler(genePatternClient);
}

From source file:de.kp.ames.web.function.transform.TransformDQM.java

public FileUtil getTransformator(String item) throws Exception {

    /*/* www .  j  av a2  s  .c om*/
     * Determine Xsl transformator from the repository
     * item of the respective registry object
     */
    ExtrinsicObjectImpl eo = (ExtrinsicObjectImpl) getRegistryObjectById(item);
    if (eo == null)
        throw new Exception("[TransformDQM] A transformator with id <" + item + "> does not exist.");

    String mimetype = eo.getMimeType();

    DataHandler handler = eo.getRepositoryItem();
    InputStream stream = handler.getInputStream();

    FileUtil file = new FileUtil(stream, mimetype);
    return file;

}