Example usage for org.apache.commons.vfs FileObject exists

List of usage examples for org.apache.commons.vfs FileObject exists

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileObject exists.

Prototype

public boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

From source file:org.pentaho.di.trans.steps.dom.xslt.DOMXslt.java

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (DOMXsltMeta) smi;//w w  w. j  a  v a2 s .  c  o m
    data = (DOMXsltData) sdi;

    Object[] row = getRow();

    if (row == null) { // no more input to be expected...
        setOutputDone();
        return false;
    }
    if (first) {
        first = false;
        data.outputRowMeta = getInputRowMeta().clone();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);

        // Check if The result field is given
        if (Const.isEmpty(meta.getResultfieldname())) {
            // Result Field is missing !
            logError(BaseMessages.getString(PKG, "Xslt.Log.ErrorResultFieldMissing"));
            throw new KettleStepException(
                    BaseMessages.getString(PKG, "Xslt.Exception.ErrorResultFieldMissing"));
        }

        // Check if The XML field is given
        if (Const.isEmpty(meta.getFieldname())) {
            // Result Field is missing !
            logError(BaseMessages.getString(PKG, "Xslt.Exception.ErrorXMLFieldMissing"));
            throw new KettleStepException(BaseMessages.getString(PKG, "Xslt.Exception.ErrorXMLFieldMissing"));
        }

        // Try to get XML Field index
        data.fieldposition = getInputRowMeta().indexOfValue(meta.getFieldname());
        // Let's check the Field
        if (data.fieldposition < 0) {
            // The field is unreachable !
            logError(BaseMessages.getString(PKG, "Xslt.Log.ErrorFindingField") + "[" + meta.getFieldname()
                    + "]");
            throw new KettleStepException(
                    BaseMessages.getString(PKG, "Xslt.Exception.CouldnotFindField", meta.getFieldname()));
        }

        // Check if the XSL Filename is contained in a column
        if (meta.useXSLField()) {
            if (Const.isEmpty(meta.getXSLFileField())) {
                // The field is missing
                // Result field is missing !
                logError(BaseMessages.getString(PKG, "Xslt.Log.ErrorXSLFileFieldMissing"));
                throw new KettleStepException(
                        BaseMessages.getString(PKG, "Xslt.Exception.ErrorXSLFileFieldMissing"));
            }

            // Try to get Field index
            data.fielxslfiledposition = getInputRowMeta().indexOfValue(meta.getXSLFileField());

            // Let's check the Field
            if (data.fielxslfiledposition < 0) {
                // The field is unreachable !
                logError(BaseMessages.getString(PKG, "Xslt.Log.ErrorXSLFileFieldFinding") + "["
                        + meta.getXSLFileField() + "]");
                throw new KettleStepException(BaseMessages.getString(PKG,
                        "Xslt.Exception.ErrorXSLFileFieldFinding", meta.getXSLFileField()));
            }

        } else {
            if (Const.isEmpty(meta.getXslFilename())) {
                logError(BaseMessages.getString(PKG, "Xslt.Log.ErrorXSLFile"));
                throw new KettleStepException(BaseMessages.getString(PKG, "Xslt.Exception.ErrorXSLFile"));
            }

            // Check if XSL File exists!
            data.xslfilename = environmentSubstitute(meta.getXslFilename());
            FileObject file = null;
            try {
                file = KettleVFS.getFileObject(data.xslfilename);
                if (!file.exists()) {
                    logError(BaseMessages.getString(PKG, "Xslt.Log.ErrorXSLFileNotExists", data.xslfilename));
                    throw new KettleStepException(BaseMessages.getString(PKG,
                            "Xslt.Exception.ErrorXSLFileNotExists", data.xslfilename));
                }
                if (file.getType() != FileType.FILE) {
                    logError(BaseMessages.getString(PKG, "Xslt.Log.ErrorXSLNotAFile", data.xslfilename));
                    throw new KettleStepException(
                            BaseMessages.getString(PKG, "Xslt.Exception.ErrorXSLNotAFile", data.xslfilename));
                }
            } catch (Exception e) {
                throw new KettleStepException(e);
            } finally {
                try {
                    if (file != null) {
                        file.close();
                    }
                } catch (Exception e) { /* Ignore */
                }
            }
        }

        // Check output parameters
        int nrOutputProps = meta.getOutputPropertyName() == null ? 0 : meta.getOutputPropertyName().length;
        if (nrOutputProps > 0) {
            data.outputProperties = new Properties();
            for (int i = 0; i < nrOutputProps; i++) {
                data.outputProperties.put(meta.getOutputPropertyName()[i],
                        environmentSubstitute(meta.getOutputPropertyValue()[i]));
            }
            data.setOutputProperties = true;
        }

        // Check parameters
        data.nrParams = meta.getParameterField() == null ? 0 : meta.getParameterField().length;
        if (data.nrParams > 0) {
            data.indexOfParams = new int[data.nrParams];
            data.nameOfParams = new String[data.nrParams];
            for (int i = 0; i < data.nrParams; i++) {
                String name = environmentSubstitute(meta.getParameterName()[i]);
                String field = environmentSubstitute(meta.getParameterField()[i]);
                if (Const.isEmpty(field)) {
                    throw new KettleStepException(
                            BaseMessages.getString(PKG, "Xslt.Exception.ParameterFieldMissing", name, i));
                }
                data.indexOfParams[i] = getInputRowMeta().indexOfValue(field);
                if (data.indexOfParams[i] < 0) {
                    throw new KettleStepException(
                            BaseMessages.getString(PKG, "Xslt.Exception.ParameterFieldNotFound", name));
                }
                data.nameOfParams[i] = name;
            }
            data.useParameters = true;
        }

        data.factory = TransformerFactory.newInstance();

        if (meta.getXSLFactory().equals("SAXON")) {
            // Set the TransformerFactory to the SAXON implementation.
            data.factory = new net.sf.saxon.TransformerFactoryImpl();
        }

        try {
            data.builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            throw new KettleStepException(e);
        }

    } // end if first

    // Get the field value
    Document xmlValue = (Document) row[data.fieldposition];
    if (meta.useXSLField()) {
        // Get the value
        data.xslfilename = getInputRowMeta().getString(row, data.fielxslfiledposition);
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "Xslt.Log.XslfileNameFromFied", data.xslfilename,
                    meta.getXSLFileField()));
        }
    }

    try {

        if (log.isDetailed()) {
            if (meta.isXSLFieldIsAFile()) {
                logDetailed(BaseMessages.getString(PKG, "Xslt.Log.Filexsl") + data.xslfilename);
            } else {
                logDetailed(BaseMessages.getString(PKG, "Xslt.Log.XslStream", data.xslfilename));
            }
        }

        // Get the template from the cache
        Transformer transformer = data.getTemplate(data.xslfilename, data.xslIsAfile);

        // Do we need to set output properties?
        if (data.setOutputProperties) {
            transformer.setOutputProperties(data.outputProperties);
        }

        // Do we need to pass parameters?
        if (data.useParameters) {
            for (int i = 0; i < data.nrParams; i++) {
                transformer.setParameter(data.nameOfParams[i], row[data.indexOfParams[i]]);
            }
        }

        Source source = new DOMSource(xmlValue);
        // Prepare output stream

        Document doc = data.builder.newDocument();

        // transform xml source
        DOMResult result = new DOMResult(doc);

        transformer.transform(source, result);

        //By default the DOMResult object creates a Document node to hold the output:
        Document outputDocument = (Document) result.getNode();

        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "Xslt.Log.FileResult"));
            //logDetailed( xmlString );
        }
        Object[] outputRowData = RowDataUtil.addValueData(row, getInputRowMeta().size(), outputDocument);

        if (log.isRowLevel()) {
            logRowlevel(
                    BaseMessages.getString(PKG, "Xslt.Log.ReadRow") + " " + getInputRowMeta().getString(row));
        }

        // add new values to the row.
        putRow(data.outputRowMeta, outputRowData); // copy row to output rowset(s);

    } catch (Exception e) {
        String errorMessage = e.getMessage();
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        DefaultErrorHandler.printLocation(pw, e);
        pw.close();
        errorMessage = sw.toString() + "\n" + errorMessage;

        if (getStepMeta().isDoingErrorHandling()) {
            // Simply add this row to the error row
            putError(getInputRowMeta(), row, 1, errorMessage, meta.getResultfieldname(), "XSLT01");
        } else {
            logError(BaseMessages.getString(PKG, "Xslt.ErrorProcesing" + " : " + errorMessage));
            throw new KettleStepException(BaseMessages.getString(PKG, "Xslt.ErrorProcesing"), e);
        }
    }

    return true;
}

From source file:org.pentaho.di.trans.steps.exceloutput.ExcelOutput.java

private boolean createParentFolder(FileObject file) {
    boolean retval = true;
    // Check for parent folder
    FileObject parentfolder = null;
    try {/*  ww w. j  av  a2 s.c  o  m*/
        // Get parent folder
        parentfolder = file.getParent();
        if (parentfolder.exists()) {
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "ExcelOutput.Log.ParentFolderExist",
                        parentfolder.getName().toString()));
            }
        } else {
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "ExcelOutput.Log.ParentFolderNotExist",
                        parentfolder.getName().toString()));
            }
            if (meta.isCreateParentFolder()) {
                parentfolder.createFolder();
                if (isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "ExcelOutput.Log.ParentFolderCreated",
                            parentfolder.getName().toString()));
                }
            } else {
                retval = false;
                logError(BaseMessages.getString(PKG, "ExcelOutput.Error.CanNotFoundParentFolder",
                        parentfolder.getName().toString(), file.getName().toString()));
            }
        }
    } catch (Exception e) {
        retval = false;
        logError(BaseMessages.getString(PKG, "ExcelOutput.Log.CouldNotCreateParentFolder",
                parentfolder.getName().toString()));
    } finally {
        if (parentfolder != null) {
            try {
                parentfolder.close();
            } catch (Exception ex) {
                // Ignore
            }
        }
    }
    return retval;
}

From source file:org.pentaho.di.trans.steps.exceloutput.ExcelOutput.java

private void setFonts() throws Exception {
    // --- Set Header font
    int headerFontSize = Const.toInt(environmentSubstitute(meta.getHeaderFontSize()),
            ExcelOutputMeta.DEFAULT_FONT_SIZE);
    // Set font name
    FontName headerFontName = ExcelFontMap.getFontName(meta.getHeaderFontName());
    // Set UnderlineStyle
    UnderlineStyle underline = ExcelFontMap.getUnderlineStyle(meta.getHeaderFontUnderline());

    WritableFont writableHeaderFont = null;
    if (meta.isHeaderFontBold()) {
        writableHeaderFont = new WritableFont(headerFontName, headerFontSize, WritableFont.BOLD,
                meta.isHeaderFontItalic(), underline);
    } else {//  ww w  . j a  v a2  s  .  c  o  m
        writableHeaderFont = new WritableFont(headerFontName, headerFontSize, WritableFont.NO_BOLD,
                meta.isHeaderFontItalic(), underline);
    }

    // Header font color
    Colour fontHeaderColour = ExcelFontMap.getColour(meta.getHeaderFontColor(), Colour.BLACK);
    if (!fontHeaderColour.equals(Colour.BLACK)) {
        writableHeaderFont.setColour(fontHeaderColour);
    }
    data.headerCellFormat = new WritableCellFormat(writableHeaderFont);

    // Header background color
    if (meta.getHeaderBackGroundColor() != ExcelOutputMeta.FONT_COLOR_NONE) {
        data.headerCellFormat.setBackground(ExcelFontMap.getColour(meta.getHeaderBackGroundColor(), null));
    }

    // Set alignment
    data.headerCellFormat = ExcelFontMap.getAlignment(meta.getHeaderAlignment(), data.headerCellFormat);
    data.headerCellFormat = ExcelFontMap.getOrientation(meta.getHeaderFontOrientation(), data.headerCellFormat);

    // Do we need to put a image on the header
    if (!Const.isEmpty(data.realHeaderImage)) {
        FileObject imageFile = null;
        try {
            imageFile = KettleVFS.getFileObject(data.realHeaderImage);
            if (!imageFile.exists()) {
                throw new KettleException(
                        BaseMessages.getString(PKG, "ExcelInputLog.ImageFileNotExists", data.realHeaderImage));
            }
            data.realHeaderImage = KettleVFS.getFilename(imageFile);
            // Put an image
            Dimension m = ExcelFontMap.getImageDimension(data.realHeaderImage);
            data.headerImageWidth = m.getWidth() * 0.016;
            data.headerImageHeight = m.getHeight() * 0.0625;

            byte[] imageData = new byte[(int) imageFile.getContent().getSize()];
            KettleVFS.getInputStream(imageFile).read(imageData);

            data.headerImage = new WritableImage(0, 0, data.headerImageWidth, data.headerImageHeight,
                    imageData);
        } catch (Exception e) {
            throw new KettleException(e);
        } finally {
            if (imageFile != null) {
                try {
                    imageFile.close();
                } catch (Exception e) {
                    // Ignore;
                }
            }
        }
    }

    // --- Set rows font
    // Set font size
    int rowFontSize = Const.toInt(environmentSubstitute(meta.getRowFontSize()),
            ExcelOutputMeta.DEFAULT_FONT_SIZE);
    // Set font name
    FontName rowFontName = ExcelFontMap.getFontName(meta.getRowFontName());

    data.writableFont = new WritableFont(rowFontName, rowFontSize, WritableFont.NO_BOLD, false,
            UnderlineStyle.NO_UNDERLINE);

    // Row font color
    Colour rowFontColour = ExcelFontMap.getColour(meta.getRowFontColor(), Colour.BLACK);
    if (!fontHeaderColour.equals(Colour.BLACK)) {
        data.writableFont.setColour(rowFontColour);
    }

    // Set rows background color if needed
    if (meta.getRowBackGroundColor() != ExcelOutputMeta.FONT_COLOR_NONE) {
        data.rowFontBackgoundColour = ExcelFontMap.getColour(meta.getRowBackGroundColor(), null);
    }
}

From source file:org.pentaho.di.trans.steps.fixedinput.FixedInputMeta.java

/**
 * @param space/*from   w  w w. ja  va 2s .  c om*/
 *          the variable space to use
 * @param definitions
 * @param resourceNamingInterface
 * @param repository
 *          The repository to optionally load other resources from (to be converted to XML)
 * @param metaStore
 *          the metaStore in which non-kettle metadata could reside.
 *
 * @return the filename of the exported resource
 */
public String exportResources(VariableSpace space, Map<String, ResourceDefinition> definitions,
        ResourceNamingInterface resourceNamingInterface, Repository repository, IMetaStore metaStore)
        throws KettleException {
    try {
        // The object that we're modifying here is a copy of the original!
        // So let's change the filename from relative to absolute by grabbing the file object...
        //
        // From : ${Internal.Transformation.Filename.Directory}/../foo/bar.txt
        // To : /home/matt/test/files/foo/bar.txt
        //
        FileObject fileObject = KettleVFS.getFileObject(space.environmentSubstitute(filename), space);

        // If the file doesn't exist, forget about this effort too!
        //
        if (fileObject.exists()) {
            // Convert to an absolute path...
            //
            filename = resourceNamingInterface.nameResource(fileObject, space, true);

            return filename;
        }
        return null;
    } catch (Exception e) {
        throw new KettleException(e);
    }
}

From source file:org.pentaho.di.trans.steps.getxmldata.GetXMLDataMeta.java

/**
 * Since the exported transformation that runs this will reside in a ZIP file, we can't reference files relatively. So
 * what this does is turn the name of files into absolute paths OR it simply includes the resource in the ZIP file.
 * For now, we'll simply turn it into an absolute path and pray that the file is on a shared drive or something like
 * that.// w  ww  . j av  a2  s. com
 *
 * @param space
 *          the variable space to use
 * @param definitions
 * @param resourceNamingInterface
 * @param repository
 *          The repository to optionally load other resources from (to be converted to XML)
 * @param metaStore
 *          the metaStore in which non-kettle metadata could reside.
 *
 * @return the filename of the exported resource
 */
public String exportResources(VariableSpace space, Map<String, ResourceDefinition> definitions,
        ResourceNamingInterface resourceNamingInterface, Repository repository, IMetaStore metaStore)
        throws KettleException {
    try {
        // The object that we're modifying here is a copy of the original!
        // So let's change the filename from relative to absolute by grabbing the file object...
        // In case the name of the file comes from previous steps, forget about this!
        //
        List<String> newFilenames = new ArrayList<String>();

        if (!isInFields()) {
            FileInputList fileList = getFiles(space);
            if (fileList.getFiles().size() > 0) {
                for (FileObject fileObject : fileList.getFiles()) {
                    // From : ${Internal.Transformation.Filename.Directory}/../foo/bar.xml
                    // To : /home/matt/test/files/foo/bar.xml
                    //
                    // If the file doesn't exist, forget about this effort too!
                    //
                    if (fileObject.exists()) {
                        // Convert to an absolute path and add it to the list.
                        //
                        newFilenames.add(fileObject.getName().getPath());
                    }
                }

                // Still here: set a new list of absolute filenames!
                //
                fileName = newFilenames.toArray(new String[newFilenames.size()]);
                fileMask = new String[newFilenames.size()]; // all null since converted to absolute path.
                fileRequired = new String[newFilenames.size()]; // all null, turn to "Y" :
                for (int i = 0; i < newFilenames.size(); i++) {
                    fileRequired[i] = "Y";
                }
            }
        }
        return null;
    } catch (Exception e) {
        throw new KettleException(e);
    }
}

From source file:org.pentaho.di.trans.steps.gpload.GPLoad.java

/**
 * Returns the path to the pathToFile. It should be the same as what was passed but this method will check the file
 * system to see if the path is valid./* ww w. j  a v a  2 s  .co  m*/
 * 
 * @param pathToFile
 *          Path to the file to verify.
 * @param exceptionMessage
 *          The message to use when the path is not provided.
 * @param checkExistence
 *          When true the path's existence will be verified.
 * @return
 * @throws KettleException
 */
private String getPath(String pathToFile, String exceptionMessage, boolean checkExistenceOfFile)
        throws KettleException {

    // Make sure the path is not empty
    if (Const.isEmpty(pathToFile)) {
        throw new KettleException(exceptionMessage);
    }

    // make sure the variable substitution is not empty
    pathToFile = environmentSubstitute(pathToFile).trim();
    if (Const.isEmpty(pathToFile)) {
        throw new KettleException(exceptionMessage);
    }

    FileObject fileObject = KettleVFS.getFileObject(pathToFile, getTransMeta());
    try {
        // we either check the existence of the file
        if (checkExistenceOfFile) {
            if (!fileObject.exists()) {
                throw new KettleException(
                        BaseMessages.getString(PKG, "GPLoad.Execption.FileDoesNotExist", pathToFile));
            }
        } else { // if the file does not have to exist, the parent, or source folder, does.
            FileObject parentFolder = fileObject.getParent();
            if (parentFolder.exists()) {
                return KettleVFS.getFilename(fileObject);
            } else {
                throw new KettleException(BaseMessages.getString(PKG, "GPLoad.Exception.DirectoryDoesNotExist",
                        parentFolder.getURL().getPath()));
            }

        }

        // if Windows is the OS
        if (Const.getOS().startsWith("Windows")) {
            return addQuotes(pathToFile);
        } else {
            return KettleVFS.getFilename(fileObject);
        }
    } catch (FileSystemException fsex) {
        throw new KettleException(
                BaseMessages.getString(PKG, "GPLoad.Exception.GPLoadCommandBuild", fsex.getMessage()));
    }
}

From source file:org.pentaho.di.trans.steps.jsonoutput.JsonOutput.java

private void createParentFolder(String filename) throws KettleStepException {
    if (!meta.isCreateParentFolder()) {
        return;/*  www.  j a v a 2 s . c om*/
    }
    // Check for parent folder
    FileObject parentfolder = null;
    try {
        // Get parent folder
        parentfolder = KettleVFS.getFileObject(filename, getTransMeta()).getParent();
        if (!parentfolder.exists()) {
            if (log.isDebug()) {
                logDebug(BaseMessages.getString(PKG, "JsonOutput.Error.ParentFolderNotExist",
                        parentfolder.getName()));
            }
            parentfolder.createFolder();
            if (log.isDebug()) {
                logDebug(BaseMessages.getString(PKG, "JsonOutput.Log.ParentFolderCreated"));
            }
        }
    } catch (Exception e) {
        throw new KettleStepException(BaseMessages.getString(PKG, "JsonOutput.Error.ErrorCreatingParentFolder",
                parentfolder.getName()));
    } finally {
        if (parentfolder != null) {
            try {
                parentfolder.close();
            } catch (Exception ex) { /* Ignore */
            }
        }
    }
}

From source file:org.pentaho.di.trans.steps.mail.Mail.java

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (MailMeta) smi;//from   w ww  . j a va 2 s. co  m
    data = (MailData) sdi;

    Object[] r = getRow(); // get row, set busy!
    if (r == null) { // no more input to be expected...

        setOutputDone();
        return false;
    }

    if (first) {
        first = false;

        // get the RowMeta
        data.previousRowMeta = getInputRowMeta().clone();

        // Check is filename field is provided
        if (Const.isEmpty(meta.getDestination())) {
            throw new KettleException(BaseMessages.getString(PKG, "Mail.Log.DestinationFieldEmpty"));
        }

        // Check is replyname field is provided
        if (Const.isEmpty(meta.getReplyAddress())) {
            throw new KettleException(BaseMessages.getString(PKG, "Mail.Log.ReplyFieldEmpty"));
        }

        // Check is SMTP server is provided
        if (Const.isEmpty(meta.getServer())) {
            throw new KettleException(BaseMessages.getString(PKG, "Mail.Log.ServerFieldEmpty"));
        }

        // Check Attached filenames when dynamic
        if (meta.isDynamicFilename() && Const.isEmpty(meta.getDynamicFieldname())) {
            throw new KettleException(BaseMessages.getString(PKG, "Mail.Log.DynamicFilenameFielddEmpty"));
        }

        // Check Attached zipfilename when dynamic
        if (meta.isZipFilenameDynamic() && Const.isEmpty(meta.getDynamicZipFilenameField())) {
            throw new KettleException(BaseMessages.getString(PKG, "Mail.Log.DynamicZipFilenameFieldEmpty"));
        }

        if (meta.isZipFiles() && Const.isEmpty(meta.getZipFilename())) {
            throw new KettleException(BaseMessages.getString(PKG, "Mail.Log.ZipFilenameEmpty"));
        }

        // check authentication
        if (meta.isUsingAuthentication()) {
            // check authentication user
            if (Const.isEmpty(meta.getAuthenticationUser())) {
                throw new KettleException(BaseMessages.getString(PKG, "Mail.Log.AuthenticationUserFieldEmpty"));
            }

            // check authentication pass
            if (Const.isEmpty(meta.getAuthenticationPassword())) {
                throw new KettleException(
                        BaseMessages.getString(PKG, "Mail.Log.AuthenticationPasswordFieldEmpty"));
            }
        }

        // cache the position of the destination field
        if (data.indexOfDestination < 0) {
            String realDestinationFieldname = meta.getDestination();
            data.indexOfDestination = data.previousRowMeta.indexOfValue(realDestinationFieldname);
            if (data.indexOfDestination < 0) {
                throw new KettleException(BaseMessages.getString(PKG,
                        "Mail.Exception.CouldnotFindDestinationField", realDestinationFieldname));
            }
        }

        // Cc
        if (!Const.isEmpty(meta.getDestinationCc())) {
            // cache the position of the Cc field
            if (data.indexOfDestinationCc < 0) {
                String realDestinationCcFieldname = meta.getDestinationCc();
                data.indexOfDestinationCc = data.previousRowMeta.indexOfValue(realDestinationCcFieldname);
                if (data.indexOfDestinationCc < 0) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "Mail.Exception.CouldnotFindDestinationCcField", realDestinationCcFieldname));
                }
            }
        }
        // BCc
        if (!Const.isEmpty(meta.getDestinationBCc())) {
            // cache the position of the BCc field
            if (data.indexOfDestinationBCc < 0) {
                String realDestinationBCcFieldname = meta.getDestinationBCc();
                data.indexOfDestinationBCc = data.previousRowMeta.indexOfValue(realDestinationBCcFieldname);
                if (data.indexOfDestinationBCc < 0) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "Mail.Exception.CouldnotFindDestinationBCcField", realDestinationBCcFieldname));
                }
            }
        }
        // Sender Name
        if (!Const.isEmpty(meta.getReplyName())) {
            // cache the position of the sender field
            if (data.indexOfSenderName < 0) {
                String realSenderName = meta.getReplyName();
                data.indexOfSenderName = data.previousRowMeta.indexOfValue(realSenderName);
                if (data.indexOfSenderName < 0) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "Mail.Exception.CouldnotFindReplyNameField", realSenderName));
                }
            }
        }
        // Sender address
        // cache the position of the sender field
        if (data.indexOfSenderAddress < 0) {
            String realSenderAddress = meta.getReplyAddress();
            data.indexOfSenderAddress = data.previousRowMeta.indexOfValue(realSenderAddress);
            if (data.indexOfSenderAddress < 0) {
                throw new KettleException(BaseMessages.getString(PKG,
                        "Mail.Exception.CouldnotFindReplyAddressField", realSenderAddress));
            }
        }

        // Reply to
        if (!Const.isEmpty(meta.getReplyToAddresses())) {
            // cache the position of the reply to field
            if (data.indexOfReplyToAddresses < 0) {
                String realReplyToAddresses = meta.getReplyToAddresses();
                data.indexOfReplyToAddresses = data.previousRowMeta.indexOfValue(realReplyToAddresses);
                if (data.indexOfReplyToAddresses < 0) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "Mail.Exception.CouldnotFindReplyToAddressesField", realReplyToAddresses));
                }
            }
        }

        // Contact Person
        if (!Const.isEmpty(meta.getContactPerson())) {
            // cache the position of the destination field
            if (data.indexOfContactPerson < 0) {
                String realContactPerson = meta.getContactPerson();
                data.indexOfContactPerson = data.previousRowMeta.indexOfValue(realContactPerson);
                if (data.indexOfContactPerson < 0) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "Mail.Exception.CouldnotFindContactPersonField", realContactPerson));
                }
            }
        }
        // Contact Phone
        if (!Const.isEmpty(meta.getContactPhone())) {
            // cache the position of the destination field
            if (data.indexOfContactPhone < 0) {
                String realContactPhone = meta.getContactPhone();
                data.indexOfContactPhone = data.previousRowMeta.indexOfValue(realContactPhone);
                if (data.indexOfContactPhone < 0) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "Mail.Exception.CouldnotFindContactPhoneField", realContactPhone));
                }
            }
        }
        // cache the position of the Server field
        if (data.indexOfServer < 0) {
            String realServer = meta.getServer();
            data.indexOfServer = data.previousRowMeta.indexOfValue(realServer);
            if (data.indexOfServer < 0) {
                throw new KettleException(
                        BaseMessages.getString(PKG, "Mail.Exception.CouldnotFindServerField", realServer));
            }
        }
        // Port
        if (!Const.isEmpty(meta.getPort())) {
            // cache the position of the port field
            if (data.indexOfPort < 0) {
                String realPort = meta.getPort();
                data.indexOfPort = data.previousRowMeta.indexOfValue(realPort);
                if (data.indexOfPort < 0) {
                    throw new KettleException(
                            BaseMessages.getString(PKG, "Mail.Exception.CouldnotFindPortField", realPort));
                }
            }
        }
        // Authentication
        if (meta.isUsingAuthentication()) {
            // cache the position of the Authentication user field
            if (data.indexOfAuthenticationUser < 0) {
                String realAuthenticationUser = meta.getAuthenticationUser();
                data.indexOfAuthenticationUser = data.previousRowMeta.indexOfValue(realAuthenticationUser);
                if (data.indexOfAuthenticationUser < 0) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "Mail.Exception.CouldnotFindAuthenticationUserField", realAuthenticationUser));
                }
            }

            // cache the position of the Authentication password field
            if (data.indexOfAuthenticationPass < 0) {
                String realAuthenticationPassword = meta.getAuthenticationPassword();
                data.indexOfAuthenticationPass = data.previousRowMeta.indexOfValue(realAuthenticationPassword);
                if (data.indexOfAuthenticationPass < 0) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "Mail.Exception.CouldnotFindAuthenticationPassField", realAuthenticationPassword));
                }
            }
        }
        // Mail Subject
        if (!Const.isEmpty(meta.getSubject())) {
            // cache the position of the subject field
            if (data.indexOfSubject < 0) {
                String realSubject = meta.getSubject();
                data.indexOfSubject = data.previousRowMeta.indexOfValue(realSubject);
                if (data.indexOfSubject < 0) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "Mail.Exception.CouldnotFindSubjectField", realSubject));
                }
            }
        }
        // Mail Comment
        if (!Const.isEmpty(meta.getComment())) {
            // cache the position of the comment field
            if (data.indexOfComment < 0) {
                String realComment = meta.getComment();
                data.indexOfComment = data.previousRowMeta.indexOfValue(realComment);
                if (data.indexOfComment < 0) {
                    throw new KettleException(BaseMessages.getString(PKG,
                            "Mail.Exception.CouldnotFindCommentField", realComment));
                }
            }
        }

        if (meta.isAttachContentFromField()) {
            // We are dealing with file content directly loaded from file
            // and not physical file
            String attachedContentField = meta.getAttachContentField();
            if (Const.isEmpty(attachedContentField)) {
                // Empty Field
                throw new KettleException(
                        BaseMessages.getString(PKG, "Mail.Exception.AttachedContentFieldEmpty"));
            }
            data.indexOfAttachedContent = data.previousRowMeta.indexOfValue(attachedContentField);
            if (data.indexOfComment < 0) {
                throw new KettleException(BaseMessages.getString(PKG,
                        "Mail.Exception.CouldnotFindAttachedContentField", attachedContentField));
            }
            // Attached content filename
            String attachedContentFileNameField = meta.getAttachContentFileNameField();
            if (Const.isEmpty(attachedContentFileNameField)) {
                // Empty Field
                throw new KettleException(
                        BaseMessages.getString(PKG, "Mail.Exception.AttachedContentFileNameFieldEmpty"));
            }
            data.IndexOfAttachedFilename = data.previousRowMeta.indexOfValue(attachedContentFileNameField);
            if (data.indexOfComment < 0) {
                throw new KettleException(
                        BaseMessages.getString(PKG, "Mail.Exception.CouldnotFindAttachedContentFileNameField",
                                attachedContentFileNameField));
            }

        } else {

            // Dynamic Zipfilename
            if (meta.isZipFilenameDynamic()) {
                // cache the position of the attached source filename field
                if (data.indexOfDynamicZipFilename < 0) {
                    String realZipFilename = meta.getDynamicZipFilenameField();
                    data.indexOfDynamicZipFilename = data.previousRowMeta.indexOfValue(realZipFilename);
                    if (data.indexOfDynamicZipFilename < 0) {
                        throw new KettleException(BaseMessages.getString(PKG,
                                "Mail.Exception.CouldnotSourceAttachedZipFilenameField", realZipFilename));
                    }
                }
            }
            data.zipFileLimit = Const.toLong(environmentSubstitute(meta.getZipLimitSize()), 0);
            if (data.zipFileLimit > 0) {
                data.zipFileLimit = data.zipFileLimit * 1048576; // Mo
            }

            if (!meta.isZipFilenameDynamic()) {
                data.ZipFilename = environmentSubstitute(meta.getZipFilename());
            }

            // Attached files
            if (meta.isDynamicFilename()) {
                // cache the position of the attached source filename field
                if (data.indexOfSourceFilename < 0) {
                    String realSourceattachedFilename = meta.getDynamicFieldname();
                    data.indexOfSourceFilename = data.previousRowMeta.indexOfValue(realSourceattachedFilename);
                    if (data.indexOfSourceFilename < 0) {
                        throw new KettleException(BaseMessages.getString(PKG,
                                "Mail.Exception.CouldnotSourceAttachedFilenameField",
                                realSourceattachedFilename));
                    }
                }

                // cache the position of the attached wildcard field
                if (!Const.isEmpty(meta.getSourceWildcard())) {
                    if (data.indexOfSourceWildcard < 0) {
                        String realSourceattachedWildcard = meta.getDynamicWildcard();
                        data.indexOfSourceWildcard = data.previousRowMeta
                                .indexOfValue(realSourceattachedWildcard);
                        if (data.indexOfSourceWildcard < 0) {
                            throw new KettleException(
                                    BaseMessages.getString(PKG, "Mail.Exception.CouldnotSourceAttachedWildcard",
                                            realSourceattachedWildcard));
                        }
                    }
                }
            } else {
                // static attached filenames
                data.realSourceFileFoldername = environmentSubstitute(meta.getSourceFileFoldername());
                data.realSourceWildcard = environmentSubstitute(meta.getSourceWildcard());
            }
        }

        // check embedded images
        if (meta.getEmbeddedImages() != null && meta.getEmbeddedImages().length > 0) {
            FileObject image = null;
            data.embeddedMimePart = new HashSet<MimeBodyPart>();
            try {
                for (int i = 0; i < meta.getEmbeddedImages().length; i++) {
                    String imageFile = environmentSubstitute(meta.getEmbeddedImages()[i]);
                    String contentID = environmentSubstitute(meta.getContentIds()[i]);
                    image = KettleVFS.getFileObject(imageFile);

                    if (image.exists() && image.getType() == FileType.FILE) {
                        // Create part for the image
                        MimeBodyPart imagePart = new MimeBodyPart();
                        // Load the image
                        URLDataSource fds = new URLDataSource(image.getURL());
                        imagePart.setDataHandler(new DataHandler(fds));
                        // Setting the header
                        imagePart.setHeader("Content-ID", "<" + contentID + ">");
                        // keep this part for further user
                        data.embeddedMimePart.add(imagePart);
                        logBasic(BaseMessages.getString(PKG, "Mail.Log.ImageAdded", imageFile));

                    } else {
                        logError(BaseMessages.getString(PKG, "Mail.Log.WrongImage", imageFile));
                    }
                }
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "Mail.Error.AddingImage", e.getMessage()));
            } finally {
                if (image != null) {
                    try {
                        image.close();
                    } catch (Exception e) { /* Ignore */
                    }
                }
            }
        }

    } // end if first

    boolean sendToErrorRow = false;
    String errorMessage = null;

    try {
        // get values
        String maildestination = data.previousRowMeta.getString(r, data.indexOfDestination);
        if (Const.isEmpty(maildestination)) {
            throw new KettleException("Mail.Error.MailDestinationEmpty");
        }
        String maildestinationCc = null;
        if (data.indexOfDestinationCc > -1) {
            maildestinationCc = data.previousRowMeta.getString(r, data.indexOfDestinationCc);
        }
        String maildestinationBCc = null;
        if (data.indexOfDestinationBCc > -1) {
            maildestinationBCc = data.previousRowMeta.getString(r, data.indexOfDestinationBCc);
        }

        String mailsendername = null;
        if (data.indexOfSenderName > -1) {
            mailsendername = data.previousRowMeta.getString(r, data.indexOfSenderName);
        }
        String mailsenderaddress = data.previousRowMeta.getString(r, data.indexOfSenderAddress);

        // reply addresses
        String mailreplyToAddresses = null;
        if (data.indexOfReplyToAddresses > -1) {
            mailreplyToAddresses = data.previousRowMeta.getString(r, data.indexOfReplyToAddresses);
        }

        String contactperson = null;
        if (data.indexOfContactPerson > -1) {
            contactperson = data.previousRowMeta.getString(r, data.indexOfContactPerson);
        }
        String contactphone = null;
        if (data.indexOfContactPhone > -1) {
            contactphone = data.previousRowMeta.getString(r, data.indexOfContactPhone);
        }

        String servername = data.previousRowMeta.getString(r, data.indexOfServer);
        if (Const.isEmpty(servername)) {
            throw new KettleException("Mail.Error.MailServerEmpty");
        }
        int port = -1;
        if (data.indexOfPort > -1) {
            port = Const.toInt("" + data.previousRowMeta.getInteger(r, data.indexOfPort), -1);
        }

        String authuser = null;
        if (data.indexOfAuthenticationUser > -1) {
            authuser = data.previousRowMeta.getString(r, data.indexOfAuthenticationUser);
        }
        String authpass = null;
        if (data.indexOfAuthenticationPass > -1) {
            authpass = data.previousRowMeta.getString(r, data.indexOfAuthenticationPass);
        }

        String subject = null;
        if (data.indexOfSubject > -1) {
            subject = data.previousRowMeta.getString(r, data.indexOfSubject);
        }

        String comment = null;
        if (data.indexOfComment > -1) {
            comment = data.previousRowMeta.getString(r, data.indexOfComment);
        }

        // send email...
        sendMail(r, servername, port, mailsenderaddress, mailsendername, maildestination, maildestinationCc,
                maildestinationBCc, contactperson, contactphone, authuser, authpass, subject, comment,
                mailreplyToAddresses);

        putRow(getInputRowMeta(), r); // copy row to possible alternate rowset(s).); // copy row to output rowset(s);

        if (log.isRowLevel()) {
            logRowlevel(BaseMessages.getString(PKG, "Mail.Log.LineNumber",
                    getLinesRead() + " : " + getInputRowMeta().getString(r)));
        }

    } catch (Exception e) {
        if (getStepMeta().isDoingErrorHandling()) {
            sendToErrorRow = true;
            errorMessage = e.toString();
        } else {
            throw new KettleException(BaseMessages.getString(PKG, "Mail.Error.General"), e);
        }
        if (sendToErrorRow) {
            // Simply add this row to the error row
            putError(getInputRowMeta(), r, 1, errorMessage, null, "MAIL001");
        }
    }

    return true;
}

From source file:org.pentaho.di.trans.steps.mail.Mail.java

private void setAttachedFilesList(Object[] r, LogChannelInterface log) throws Exception {
    String realSourceFileFoldername = null;
    String realSourceWildcard = null;
    FileObject sourcefile = null;
    FileObject file = null;/* w w  w. j  a  va 2s .  com*/

    ZipOutputStream zipOutputStream = null;
    File masterZipfile = null;

    if (meta.isZipFilenameDynamic()) {
        data.ZipFilename = data.previousRowMeta.getString(r, data.indexOfDynamicZipFilename);
    }

    try {

        if (meta.isDynamicFilename()) {
            // dynamic attached filenames
            if (data.indexOfSourceFilename > -1) {
                realSourceFileFoldername = data.previousRowMeta.getString(r, data.indexOfSourceFilename);
            }

            if (data.indexOfSourceWildcard > -1) {
                realSourceWildcard = data.previousRowMeta.getString(r, data.indexOfSourceWildcard);
            }

        } else {
            // static attached filenames
            realSourceFileFoldername = data.realSourceFileFoldername;
            realSourceWildcard = data.realSourceWildcard;
        }

        if (!Const.isEmpty(realSourceFileFoldername)) {
            sourcefile = KettleVFS.getFileObject(realSourceFileFoldername, getTransMeta());
            if (sourcefile.exists()) {
                long FileSize = 0;
                FileObject[] list = null;
                if (sourcefile.getType() == FileType.FILE) {
                    list = new FileObject[1];
                    list[0] = sourcefile;
                } else {
                    list = sourcefile
                            .findFiles(new TextFileSelector(sourcefile.toString(), realSourceWildcard));
                }
                if (list.length > 0) {

                    boolean zipFiles = meta.isZipFiles();
                    if (zipFiles && data.zipFileLimit == 0) {
                        masterZipfile = new File(
                                System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR + data.ZipFilename);

                        zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));
                    }

                    for (int i = 0; i < list.length; i++) {

                        file = KettleVFS.getFileObject(KettleVFS.getFilename(list[i]), getTransMeta());

                        if (zipFiles) {

                            if (data.zipFileLimit == 0) {
                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        file.getContent().getInputStream());
                                int c;
                                while ((c = inputStream.read()) >= 0) {
                                    zipOutputStream.write(c);
                                }
                                inputStream.close();
                                zipOutputStream.closeEntry();
                            } else {
                                FileSize += file.getContent().getSize();
                            }
                        } else {
                            addAttachedFilePart(file);
                        }
                    } // end for
                    if (zipFiles) {
                        if (isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "Mail.Log.FileSize", "" + FileSize));
                        }
                        if (isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "Mail.Log.LimitSize", "" + data.zipFileLimit));
                        }

                        if (data.zipFileLimit > 0 && FileSize > data.zipFileLimit) {

                            masterZipfile = new File(System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR
                                    + data.ZipFilename);

                            zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));

                            for (int i = 0; i < list.length; i++) {

                                file = KettleVFS.getFileObject(KettleVFS.getFilename(list[i]), getTransMeta());

                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        file.getContent().getInputStream());
                                int c;
                                while ((c = inputStream.read()) >= 0) {
                                    zipOutputStream.write(c);
                                }
                                inputStream.close();
                                zipOutputStream.closeEntry();

                            }

                        }
                        if (data.zipFileLimit > 0 && FileSize > data.zipFileLimit || data.zipFileLimit == 0) {
                            file = KettleVFS.getFileObject(masterZipfile.getAbsolutePath(), getTransMeta());
                            addAttachedFilePart(file);
                        }
                    }
                }
            } else {
                logError(BaseMessages.getString(PKG, "Mail.Error.SourceFileFolderNotExists",
                        realSourceFileFoldername));
            }
        }
    } catch (Exception e) {
        logError(e.getMessage());
    } finally {
        if (sourcefile != null) {
            try {
                sourcefile.close();
            } catch (Exception e) {
                // Ignore errors
            }
        }
        if (file != null) {
            try {
                file.close();
            } catch (Exception e) {
                // Ignore errors
            }
        }

        if (zipOutputStream != null) {
            try {
                zipOutputStream.finish();
                zipOutputStream.close();
            } catch (IOException e) {
                logError("Unable to close attachement zip file archive : " + e.toString());
            }
        }
    }

}

From source file:org.pentaho.di.trans.steps.mondrianinput.MondrianInputMeta.java

/**
 * Since the exported transformation that runs this will reside in a ZIP file, we can't reference files relatively. So
 * what this does is turn the name of files into absolute paths OR it simply includes the resource in the ZIP file.
 * For now, we'll simply turn it into an absolute path and pray that the file is on a shared drive or something like
 * that.//from   w w w.ja  v  a2s  . c om
 *
 * @param space
 *          the variable space to use
 * @param definitions
 * @param resourceNamingInterface
 * @param repository
 *          The repository to optionally load other resources from (to be converted to XML)
 * @param metaStore
 *          the metaStore in which non-kettle metadata could reside.
 *
 * @return the filename of the exported resource
 */
public String exportResources(VariableSpace space, Map<String, ResourceDefinition> definitions,
        ResourceNamingInterface resourceNamingInterface, Repository repository, IMetaStore metaStore)
        throws KettleException {
    try {
        // The object that we're modifying here is a copy of the original!
        // So let's change the filename from relative to absolute by grabbing the file object...
        // In case the name of the file comes from previous steps, forget about this!
        //
        if (Const.isEmpty(catalog)) {
            // From : ${Internal.Transformation.Filename.Directory}/../foo/bar.csv
            // To : /home/matt/test/files/foo/bar.csv
            //
            FileObject fileObject = KettleVFS.getFileObject(space.environmentSubstitute(catalog), space);

            // If the file doesn't exist, forget about this effort too!
            //
            if (fileObject.exists()) {
                // Convert to an absolute path...
                //
                catalog = resourceNamingInterface.nameResource(fileObject, space, true);

                return catalog;
            }
        }
        return null;
    } catch (Exception e) {
        throw new KettleException(e);
    }
}