Example usage for javax.activation DataHandler getDataSource

List of usage examples for javax.activation DataHandler getDataSource

Introduction

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

Prototype

public DataSource getDataSource() 

Source Link

Document

Return the DataSource associated with this instance of DataHandler.

Usage

From source file:org.wso2.carbon.application.mgt.ui.ApplicationAdminClient.java

public void downloadCappArchive(String filename, HttpServletResponse response)
        throws IOException, ApplicationAdminExceptionException {

    ServletOutputStream out = response.getOutputStream();
    DataHandler dataHandler = stub.downloadCappArchive(filename);
    if (dataHandler != null) {
        response.setHeader("Content-Disposition", "fileName=" + filename + ".car");
        response.setContentType(dataHandler.getContentType());
        InputStream in = dataHandler.getDataSource().getInputStream();
        int nextChar;
        while ((nextChar = in.read()) != -1) {
            out.write((char) nextChar);
        }//from  w  ww  .  java  2s.  co m
        out.flush();
        in.close();
        out.close();
    } else {
        out.write("The requested capp archive was not found on the server".getBytes());
    }

}

From source file:org.wso2.carbon.humantask.ui.clients.HumanTaskPackageManagementServiceClient.java

/**
 * Download package archive.//from w w  w  . jav a2 s.co m
 *
 * @param humanTaskPackageName : The package name.
 * @param response             :
 * @throws PackageManagementException :
 * @throws java.io.IOException        :
 */
public void downloadHumanTaskPackage(String humanTaskPackageName, HttpServletResponse response)
        throws PackageManagementException, IOException {
    try {
        ServletOutputStream out = response.getOutputStream();

        HumanTaskPackageDownloadData downloadData = stub.downloadHumanTaskPackage(humanTaskPackageName);
        if (downloadData != null) {
            DataHandler handler = downloadData.getPackageFileData();
            response.setHeader("Content-Disposition", "fileName=" + downloadData.getPackageName());
            response.setContentType(handler.getContentType());
            InputStream in = handler.getDataSource().getInputStream();
            int nextChar;
            while ((nextChar = in.read()) != -1) {
                out.write((char) nextChar);
            }
            out.flush();
            in.close();
        } else {
            out.write("The requested package archive was not found on the server".getBytes());
        }
    } catch (RemoteException e) {
        log.error(e);
        throw e;
    } catch (IOException e) {
        log.error(e);
        throw e;
    }
}

From source file:org.wso2.carbon.mediation.library.ui.LibraryAdminClient.java

public void downloadCappArchive(String filename, HttpServletResponse response)
        throws IOException, MediationLibraryAdminServiceException {

    ServletOutputStream out = response.getOutputStream();
    DataHandler dataHandler = stub.downloadLibraryArchive(filename);
    if (dataHandler != null) {
        response.setHeader("Content-Disposition", "fileName=" + filename + ".zip");
        response.setContentType(dataHandler.getContentType());
        InputStream in = dataHandler.getDataSource().getInputStream();
        int nextChar;
        while ((nextChar = in.read()) != -1) {
            out.write((char) nextChar);
        }//from ww w.j  ava 2  s  .  com
        out.flush();
        in.close();
        out.close();
    } else {
        out.write("The requested library archive was not found on the server".getBytes());
    }

}

From source file:org.wso2.carbon.relay.ExpandingMessageFormatter.java

private void findAndWrite2OutputStream(MessageContext messageContext, OutputStream out, boolean preserve)
        throws AxisFault {
    try {/*from w ww.jav a 2 s  .  c o m*/
        SOAPEnvelope envelope = messageContext.getEnvelope();
        OMElement contentEle = envelope.getBody().getFirstElement();
        if (contentEle != null) {
            OMNode node = contentEle.getFirstOMChild();
            if (!(node instanceof OMText)) {
                String msg = "Wrong Input for the Validator, "
                        + "the content of the first child element of the Body " + "should have the zip file";
                log.error(msg);
                throw new AxisFault(msg);
            }
            OMText binaryDataNode = (OMText) node;
            DataHandler dh = (DataHandler) binaryDataNode.getDataHandler();

            DataSource dataSource = dh.getDataSource();
            if (dataSource instanceof StreamingOnRequestDataSource) {
                if (((StreamingOnRequestDataSource) dataSource).isConsumed()) {
                    Object httpMethodObj = messageContext.getProperty(Constants.Configuration.HTTP_METHOD);
                    if ((httpMethodObj instanceof String)
                            && Constants.Configuration.HTTP_METHOD_POST.equals(httpMethodObj)) {
                        log.warn("Attempting to send an already consumed request ["
                                + messageContext.getTo().getAddress() + " POST/Empty Message Body]");
                    }

                    //Ask the data source to stream, if it has not already cached the request
                    if (!preserve) {
                        ((StreamingOnRequestDataSource) dataSource).setLastUse(true);
                    }
                }
            }
            dh.writeTo(out);
        }
    } catch (OMException e) {
        log.error(e);
        throw AxisFault.makeFault(e);
    } catch (IOException e) {
        log.error(e);
        throw AxisFault.makeFault(e);
    }
}

From source file:org.wso2.carbon.relay.module.handler.SkipAdminServiceHandler.java

public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
    try {//from  w w  w . ja v a  2 s.  c  o  m
        Parameter relayParam = msgContext.getParameter(RelayConstants.RELAY_CONFIG_PARAM);

        if (relayParam == null) {
            handleException("Relay not initialized");
        }

        RelayConfiguration relConf = (RelayConfiguration) relayParam.getValue();

        if (relConf == null) {
            handleException("Relay not initialized");
        }

        if (isFilteredOutService(msgContext.getAxisService(), relConf)) {
            SOAPEnvelope envelope = msgContext.getEnvelope();

            OMElement contentEle = envelope.getBody()
                    .getFirstChildWithName(RelayConstants.BINARY_CONTENT_QNAME);

            if (contentEle != null) {
                OMNode node = contentEle.getFirstOMChild();
                if (node != null && (node instanceof OMText)) {
                    OMText binaryDataNode = (OMText) node;
                    DataHandler dh = (DataHandler) binaryDataNode.getDataHandler();

                    if (dh == null) {
                        if (log.isDebugEnabled()) {
                            log.warn("Message has the Binary content element. "
                                    + "But doesn't have binary content embedded within it");
                        }
                        return InvocationResponse.CONTINUE;
                    }

                    DataSource dataSource = dh.getDataSource();
                    //Ask the data source to stream, if it has not alredy cached the request
                    if (dataSource instanceof StreamingOnRequestDataSource) {
                        ((StreamingOnRequestDataSource) dataSource).setLastUse(true);
                    }
                    InputStream in = dh.getInputStream();
                    //extract the wrapped binary content

                    //Select the right builder, create the envelope and stick it in.
                    String contentType = (String) msgContext.getProperty(Constants.Configuration.CONTENT_TYPE);

                    OMElement element = relConf.getMessageBuilder().getDocument(contentType, msgContext, in);

                    if (element != null) {
                        msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(element));

                        msgContext.setProperty(MessageBuilder.RELAY_FORMATTERS_MAP,
                                relConf.getMessageBuilder().getFormatters());
                    } else {
                        log.warn("Error building the message, skipping message building");
                    }
                    //now we have undone thing done  by Relay
                    if (log.isDebugEnabled()) {
                        log.debug("Undo wrapping done by Relay");
                    }
                } else {
                    //if is not wrapped binary content, there is nothing to be done
                    if (log.isDebugEnabled()) {
                        log.debug("not wrapped binary content, there is nothing to be done");
                    }
                }
            } else {
                //there is no body content, we will let it go
                if (log.isDebugEnabled()) {
                    log.debug("Body of the Soap Envelope is empty, nothing to unwrap");
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Not a admin service, nothing to be done");
            }
        }
        return InvocationResponse.CONTINUE;
    } catch (OMException e) {
        throw AxisFault.makeFault(e);
    } catch (IOException e) {
        throw AxisFault.makeFault(e);
    } catch (XMLStreamException e) {
        throw AxisFault.makeFault(e);
    }
}

From source file:org.wso2.carbon.service.mgt.ui.ServiceAdminClient.java

public void downloadServiceArchive(String serviceGroupName, String serviceType, HttpServletResponse response)
        throws AxisFault {
    try {/*from   w ww  .  ja v a  2s  .  co m*/
        ServletOutputStream out = response.getOutputStream();
        ServiceDownloadData downloadData = stub.downloadServiceArchive(serviceGroupName);
        if (downloadData != null) {
            DataHandler handler = downloadData.getServiceFileData();
            response.setHeader("Content-Disposition", "fileName=" + downloadData.getFileName());
            response.setContentType(handler.getContentType());
            InputStream in = handler.getDataSource().getInputStream();
            int nextChar;
            while ((nextChar = in.read()) != -1) {
                out.write((char) nextChar);
            }
            out.flush();
            in.close();
        } else {
            out.write("The requested service archive was not found on the server".getBytes());
        }
    } catch (RemoteException e) {
        handleException("error.downloading.service", e);
    } catch (IOException e) {
        handleException("error.downloading.service", e);
    }
}

From source file:org.wso2.carbon.tools.wsdlvalidator.WsdlValidator.java

public Report validateFromFile(String type, DataHandler filedata) throws Exception {
    InputStream inputStream;// www.  j a va 2 s.  c o m
    try {
        inputStream = filedata.getDataSource().getInputStream();
    } catch (IOException e) {
        throw new WSDLValidatorException("Exception occurred when validating XML document", e);
    }
    WSDLValidationInfo info = validaWSDLFromURI(inputStream);
    return dataPacker(info);
}

From source file:org.wso2.carbon.ui.util.FileDownloadUtil.java

public synchronized boolean acquireResource(ConfigurationContextService configCtxService,
        HttpServletRequest request, HttpServletResponse response) throws CarbonException {

    OutputStream out;/* w w w  .  j  av a 2  s  .co m*/
    try {
        out = response.getOutputStream();
    } catch (IOException e) {
        String msg = "Unable to retrieve file ";
        log.error(msg, e);
        throw new CarbonException(msg, e);
    }
    String fileID = request.getParameter("id");
    String fileName = getFileName(configCtxService, request, fileID);

    if (fileName == null) {
        String serverURL = CarbonUIUtil.getServerURL(request.getSession().getServletContext(),
                request.getSession());

        String serviceEPR = serverURL + "FileDownloadService";
        try {

            FileDownloadServiceStub stub;
            if (CarbonUtils.isRunningOnLocalTransportMode()) {
                stub = new FileDownloadServiceStub(configCtxService.getServerConfigContext(), serviceEPR);
            } else {
                stub = new FileDownloadServiceStub(configCtxService.getClientConfigContext(), serviceEPR);
            }
            DataHandler dataHandler = stub.downloadFile(fileID);
            if (dataHandler != null) {
                response.setHeader("Content-Disposition", "filename=" + fileID);
                response.setContentType(dataHandler.getContentType());
                InputStream in = dataHandler.getDataSource().getInputStream();
                int nextChar;
                while ((nextChar = in.read()) != -1) {
                    out.write((char) nextChar);
                }
                out.flush();
                out.close();
                in.close();
                return true;
            }
            out.write("The requested file was not found on the server".getBytes());
            out.flush();
            out.close();
        } catch (IOException e) {
            String msg = "Unable to write output to HttpServletResponse OutputStream ";
            log.error(msg, e);
            throw new CarbonException(msg, e);
        }
        return false;
    }

    try {
        File file = new File(fileName);
        FileInputStream in = new FileInputStream(file);
        byte[] b = new byte[(int) file.length()];
        response.setContentType(mimeMap.getMIMEType(file));
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "filename=" + file.getName());
        int lengthRead = in.read(b);
        if (lengthRead != -1) {
            out.write(b);
        }
        out.flush();
        out.close();
        in.close();
        return true;
    } catch (IOException e) {
        String msg = "Unable to retrieve file ";
        log.error(msg, e);
        throw new CarbonException(msg, e);
    }
}

From source file:org.wso2.carbon.webapp.list.ui.WebappAdminClient.java

public void downloadWarFileHandler(String fileName, String hostName, String webappType,
        HttpServletResponse response) throws AxisFault {
    try {//  w  ww  . java 2  s .  co  m
        ServletOutputStream out = response.getOutputStream();
        DataHandler handler = stub.downloadWarFileHandler(fileName, hostName, webappType);
        if (handler != null) {
            if ("jaggeryWebapp".equals(webappType)) {
                if (!fileName.endsWith(".zip")) {
                    fileName = fileName.concat(".zip");
                }
            } else if (!fileName.endsWith(".war")) {
                fileName = fileName.concat(".war");
            }
            response.setHeader("Content-Disposition", "fileName=" + fileName);
            response.setContentType(handler.getContentType());
            InputStream in = handler.getDataSource().getInputStream();
            int nextChar;
            while ((nextChar = in.read()) != -1) {
                out.write((char) nextChar);
            }
            out.flush();
            in.close();
        } else {
            out.write("The requested webapp was not found on the server".getBytes());
        }
    } catch (RemoteException e) {
        handleException("error.downloading.war", e);
    } catch (IOException e) {
        handleException("error.downloading.war", e);
    }
}

From source file:org.wso2.carbon.webapp.list.ui.WebappAdminClient.java

public InputStream getWarFileInputStream(String fileName, String hostName, String webappType) throws AxisFault {
    InputStream inputStream = null;
    try {//from   w  w w  .j ava2  s. co m

        DataHandler handler = stub.downloadWarFileHandler(fileName, hostName, webappType);
        if (handler != null) {

            inputStream = handler.getDataSource().getInputStream();

        } else {

        }
    } catch (RemoteException e) {
        handleException("error.downloading.war", e);
    } catch (IOException e) {
        handleException("error.downloading.war", e);
    }
    return inputStream;
}