use of org.apache.commons.vfs2.UserAuthenticationData.Type in project pentaho-kettle by pentaho.
the class RepositoryVfsFileObject method createFolder.
@Override
public void createFolder() throws FileSystemException {
try {
provider.getRepo().createRepositoryDirectory(provider.getRepo().loadRepositoryDirectoryTree(), path);
type = FileType.FOLDER;
} catch (KettleException ex) {
throw new FileSystemException(ex);
}
}
use of org.apache.commons.vfs2.UserAuthenticationData.Type in project pentaho-kettle by pentaho.
the class XsdValidator method processRow.
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
meta = (XsdValidatorMeta) smi;
data = (XsdValidatorData) 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 XML stream is given
if (meta.getXMLStream() != null) {
// Try to get XML Field index
data.xmlindex = getInputRowMeta().indexOfValue(meta.getXMLStream());
// Let's check the Field
if (data.xmlindex < 0) {
// The field is unreachable !
logError(BaseMessages.getString(PKG, "XsdValidator.Log.ErrorFindingField") + "[" + meta.getXMLStream() + "]");
throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.Exception.CouldnotFindField", meta.getXMLStream()));
}
// Let's check that Result Field is given
if (meta.getResultfieldname() == null) {
// Result field is missing !
logError(BaseMessages.getString(PKG, "XsdValidator.Log.ErrorResultFieldMissing"));
throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.Exception.ErrorResultFieldMissing"));
}
// Is XSD file is provided?
if (meta.getXSDSource().equals(meta.SPECIFY_FILENAME)) {
if (meta.getXSDFilename() == null) {
logError(BaseMessages.getString(PKG, "XsdValidator.Log.ErrorXSDFileMissing"));
throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.Exception.ErrorXSDFileMissing"));
} else {
// Is XSD file exists ?
FileObject xsdfile = null;
try {
xsdfile = KettleVFS.getFileObject(environmentSubstitute(meta.getXSDFilename()), getTransMeta());
if (!xsdfile.exists()) {
logError(BaseMessages.getString(PKG, "XsdValidator.Log.Error.XSDFileNotExists"));
throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.Exception.XSDFileNotExists"));
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "XsdValidator.Log.Error.GettingXSDFile"));
throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.Exception.GettingXSDFile"));
} finally {
try {
if (xsdfile != null) {
xsdfile.close();
}
} catch (IOException e) {
// Ignore errors
}
}
}
}
// Is XSD field is provided?
if (meta.getXSDSource().equals(meta.SPECIFY_FIELDNAME)) {
if (meta.getXSDDefinedField() == null) {
logError(BaseMessages.getString(PKG, "XsdValidator.Log.Error.XSDFieldMissing"));
throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.Exception.XSDFieldMissing"));
} else {
// Let's check if the XSD field exist
// Try to get XML Field index
data.xsdindex = getInputRowMeta().indexOfValue(meta.getXSDDefinedField());
if (data.xsdindex < 0) {
// The field is unreachable !
logError(BaseMessages.getString(PKG, "XsdValidator.Log.ErrorFindingXSDField", meta.getXSDDefinedField()));
throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.Exception.ErrorFindingXSDField", meta.getXSDDefinedField()));
}
}
}
} else {
// XML stream field is missing !
logError(BaseMessages.getString(PKG, "XsdValidator.Log.Error.XmlStreamFieldMissing"));
throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.Exception.XmlStreamFieldMissing"));
}
}
try {
// Get the XML field value
String XMLFieldvalue = getInputRowMeta().getString(row, data.xmlindex);
boolean isvalid = false;
// XSD filename
String xsdfilename = null;
if (meta.getXSDSource().equals(meta.SPECIFY_FILENAME)) {
xsdfilename = environmentSubstitute(meta.getXSDFilename());
} else if (meta.getXSDSource().equals(meta.SPECIFY_FIELDNAME)) {
// Get the XSD field value
xsdfilename = getInputRowMeta().getString(row, data.xsdindex);
}
// Get XSD filename
FileObject xsdfile = null;
String validationmsg = null;
try {
SchemaFactory factoryXSDValidator = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
xsdfile = KettleVFS.getFileObject(xsdfilename, getTransMeta());
// Get XML stream
Source sourceXML = new StreamSource(new StringReader(XMLFieldvalue));
if (meta.getXMLSourceFile()) {
// We deal with XML file
// Get XML File
FileObject xmlfileValidator = KettleVFS.getFileObject(XMLFieldvalue);
if (xmlfileValidator == null || !xmlfileValidator.exists()) {
logError(BaseMessages.getString(PKG, "XsdValidator.Log.Error.XMLfileMissing", XMLFieldvalue));
throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.Exception.XMLfileMissing", XMLFieldvalue));
}
sourceXML = new StreamSource(xmlfileValidator.getContent().getInputStream());
}
// create the schema
Schema SchematXSD = null;
if (xsdfile instanceof AbstractFileObject) {
if (xsdfile.getName().getURI().contains("ram:///")) {
SchematXSD = factoryXSDValidator.newSchema(new StreamSource(xsdfile.getContent().getInputStream()));
} else {
SchematXSD = factoryXSDValidator.newSchema(new File(KettleVFS.getFilename(xsdfile)));
}
} else {
// a url should be made a FileObject.
throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.Exception.CannotCreateSchema", xsdfile.getClass().getName()));
}
if (meta.getXSDSource().equals(meta.NO_NEED)) {
// ---Some documents specify the schema they expect to be validated against,
// ---typically using xsi:noNamespaceSchemaLocation and/or xsi:schemaLocation attributes
// ---Schema SchematXSD = factoryXSDValidator.newSchema();
SchematXSD = factoryXSDValidator.newSchema();
}
// Create XSDValidator
Validator xsdValidator = SchematXSD.newValidator();
// https://www.owasp.org/index.php/XML_Security_Cheat_Sheet#XML_Entity_Expansion
if (!meta.isAllowExternalEntities()) {
xsdValidator.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
xsdValidator.setFeature("http://xml.org/sax/features/external-general-entities", false);
xsdValidator.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
xsdValidator.setProperty("http://apache.org/xml/properties/internal/entity-resolver", (XMLEntityResolver) xmlResourceIdentifier -> {
String message = BaseMessages.getString(PKG, "XsdValidator.Exception.DisallowedDocType");
throw new IOException(message);
});
}
// Validate XML / XSD
xsdValidator.validate(sourceXML);
isvalid = true;
} catch (SAXException ex) {
validationmsg = ex.getMessage();
} catch (IOException ex) {
validationmsg = ex.getMessage();
} finally {
try {
if (xsdfile != null) {
xsdfile.close();
}
} catch (IOException e) {
// Ignore errors
}
}
Object[] outputRowData = null;
Object[] outputRowData2 = null;
if (meta.getOutputStringField()) {
// Output type=String
if (isvalid) {
outputRowData = RowDataUtil.addValueData(row, getInputRowMeta().size(), environmentSubstitute(meta.getIfXmlValid()));
} else {
outputRowData = RowDataUtil.addValueData(row, getInputRowMeta().size(), environmentSubstitute(meta.getIfXmlInvalid()));
}
} else {
outputRowData = RowDataUtil.addValueData(row, getInputRowMeta().size(), isvalid);
}
if (meta.useAddValidationMessage()) {
outputRowData2 = RowDataUtil.addValueData(outputRowData, getInputRowMeta().size() + 1, validationmsg);
} else {
outputRowData2 = outputRowData;
}
if (log.isRowLevel()) {
logRowlevel(BaseMessages.getString(PKG, "XsdValidator.Log.ReadRow") + " " + getInputRowMeta().getString(row));
}
// add new values to the row.
// copy row to output rowset(s);
putRow(data.outputRowMeta, outputRowData2);
} catch (KettleException e) {
boolean sendToErrorRow = false;
String errorMessage = null;
if (getStepMeta().isDoingErrorHandling()) {
sendToErrorRow = true;
errorMessage = e.toString();
}
if (sendToErrorRow) {
// Simply add this row to the error row
putError(getInputRowMeta(), row, 1, errorMessage, null, "XSD001");
} else {
logError(BaseMessages.getString(PKG, "XsdValidator.ErrorProcesing" + " : " + e.getMessage()));
throw new KettleStepException(BaseMessages.getString(PKG, "XsdValidator.ErrorProcesing"), e);
}
}
return true;
}
use of org.apache.commons.vfs2.UserAuthenticationData.Type in project pentaho-kettle by pentaho.
the class TextFileInputDialog method getCSV.
// Get the data layout
private void getCSV() {
TextFileInputMeta meta = new TextFileInputMeta();
getInfo(meta);
TextFileInputMeta previousMeta = (TextFileInputMeta) meta.clone();
FileInputList textFileList = meta.getTextFileList(transMeta);
InputStream fileInputStream;
CompressionInputStream inputStream = null;
StringBuilder lineStringBuilder = new StringBuilder(256);
int fileFormatType = meta.getFileFormatTypeNr();
String delimiter = transMeta.environmentSubstitute(meta.getSeparator());
String enclosure = transMeta.environmentSubstitute(meta.getEnclosure());
String escapeCharacter = transMeta.environmentSubstitute(meta.getEscapeCharacter());
if (textFileList.nrOfFiles() > 0) {
int clearFields = meta.hasHeader() ? SWT.YES : SWT.NO;
int nrInputFields = meta.getInputFields().length;
if (meta.hasHeader() && nrInputFields > 0) {
MessageBox mb = new MessageBox(shell, SWT.YES | SWT.NO | SWT.CANCEL | SWT.ICON_QUESTION);
mb.setMessage(BaseMessages.getString(PKG, "TextFileInputDialog.ClearFieldList.DialogMessage"));
mb.setText(BaseMessages.getString(PKG, "TextFileInputDialog.ClearFieldList.DialogTitle"));
clearFields = mb.open();
if (clearFields == SWT.CANCEL) {
return;
}
}
try {
wFields.table.removeAll();
FileObject fileObject = textFileList.getFile(0);
fileInputStream = KettleVFS.getInputStream(fileObject);
Table table = wFields.table;
CompressionProvider provider = CompressionProviderFactory.getInstance().createCompressionProviderInstance(meta.getFileCompression());
inputStream = provider.createInputStream(fileInputStream);
InputStreamReader reader;
if (meta.getEncoding() != null && meta.getEncoding().length() > 0) {
reader = new InputStreamReader(inputStream, meta.getEncoding());
} else {
reader = new InputStreamReader(inputStream);
}
EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());
if (clearFields == SWT.YES || !meta.hasHeader() || nrInputFields > 0) {
// Scan the header-line, determine fields...
String line;
if (meta.hasHeader() || meta.getInputFields().length == 0) {
line = TextFileInput.getLine(log, reader, encodingType, fileFormatType, lineStringBuilder);
if (line != null) {
// Estimate the number of input fields...
// Chop up the line using the delimiter
String[] fields = TextFileInput.guessStringsFromLine(transMeta, log, line, meta, delimiter, enclosure, escapeCharacter);
for (int i = 0; i < fields.length; i++) {
String field = fields[i];
if (field == null || field.length() == 0 || (nrInputFields == 0 && !meta.hasHeader())) {
field = "Field" + (i + 1);
} else {
// Trim the field
field = Const.trim(field);
// Replace all spaces & - with underscore _
field = Const.replace(field, " ", "_");
field = Const.replace(field, "-", "_");
}
TableItem item = new TableItem(table, SWT.NONE);
item.setText(1, field);
// The default type is String...
item.setText(2, "String");
}
wFields.setRowNums();
wFields.optWidth(true);
// Copy it...
getInfo(meta);
}
}
// Sample a few lines to determine the correct type of the fields...
String shellText = BaseMessages.getString(PKG, "TextFileInputDialog.LinesToSample.DialogTitle");
String lineText = BaseMessages.getString(PKG, "TextFileInputDialog.LinesToSample.DialogMessage");
EnterNumberDialog end = new EnterNumberDialog(shell, 100, shellText, lineText);
int samples = end.open();
if (samples >= 0) {
getInfo(meta);
TextFileCSVImportProgressDialog pd = new TextFileCSVImportProgressDialog(shell, meta, transMeta, reader, samples, clearFields == SWT.YES);
String message = pd.open();
if (message != null) {
wFields.removeAll();
// OK, what's the result of our search?
getData(meta);
//
if (clearFields == SWT.NO) {
getFieldsData(previousMeta, true);
wFields.table.setSelection(previousMeta.getInputFields().length, wFields.table.getItemCount() - 1);
}
wFields.removeEmptyRows();
wFields.setRowNums();
wFields.optWidth(true);
EnterTextDialog etd = new EnterTextDialog(shell, BaseMessages.getString(PKG, "TextFileInputDialog.ScanResults.DialogTitle"), BaseMessages.getString(PKG, "TextFileInputDialog.ScanResults.DialogMessage"), message, true);
etd.setReadOnly();
etd.open();
}
}
} else {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(BaseMessages.getString(PKG, "TextFileInputDialog.UnableToReadHeaderLine.DialogMessage"));
mb.setText(BaseMessages.getString(PKG, "System.Dialog.Error.Title"));
mb.open();
}
} catch (IOException e) {
new ErrorDialog(shell, BaseMessages.getString(PKG, "TextFileInputDialog.IOError.DialogTitle"), BaseMessages.getString(PKG, "TextFileInputDialog.IOError.DialogMessage"), e);
} catch (KettleException e) {
new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"), BaseMessages.getString(PKG, "TextFileInputDialog.ErrorGettingFileDesc.DialogMessage"), e);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
// Ignore errors
}
}
} else {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(BaseMessages.getString(PKG, "TextFileInputDialog.NoValidFileFound.DialogMessage"));
mb.setText(BaseMessages.getString(PKG, "System.Dialog.Error.Title"));
mb.open();
}
}
use of org.apache.commons.vfs2.UserAuthenticationData.Type in project scheduling by ow2-proactive.
the class SchedulerStateRest method pullFile.
@Override
public InputStream pullFile(String sessionId, String spaceName, String filePath) throws IOException, NotConnectedRestException, PermissionRestException {
checkAccess(sessionId, "pullFile");
Session session = dataspaceRestApi.checkSessionValidity(sessionId);
filePath = normalizeFilePath(filePath, null);
FileObject sourcefo = dataspaceRestApi.resolveFile(session, spaceName, filePath);
if (!sourcefo.exists() || !sourcefo.isReadable()) {
RuntimeException ex = new IllegalArgumentException("File " + filePath + " does not exist or is not readable in space " + spaceName);
logger.error(ex);
throw ex;
}
if (sourcefo.getType().equals(FileType.FOLDER)) {
logger.info("[pullFile] reading directory content from " + sourcefo.getURL());
// if it's a folder we return an InputStream listing its content
StringBuilder sb = new StringBuilder();
String nl = System.lineSeparator();
for (FileObject fo : sourcefo.getChildren()) {
sb.append(fo.getName().getBaseName() + nl);
}
return IOUtils.toInputStream(sb.toString(), Charset.forName(FILE_ENCODING));
} else if (sourcefo.getType().equals(FileType.FILE)) {
logger.info("[pullFile] reading file content from " + sourcefo.getURL());
return sourcefo.getContent().getInputStream();
} else {
RuntimeException ex = new IllegalArgumentException("File " + filePath + " has an unsupported type " + sourcefo.getType());
logger.error(ex);
throw ex;
}
}
use of org.apache.commons.vfs2.UserAuthenticationData.Type in project ant-ivy by apache.
the class VfsRepository method createVFSManager.
private StandardFileSystemManager createVFSManager() throws IOException {
StandardFileSystemManager result = null;
try {
/*
* The DefaultFileSystemManager gets its configuration from the jakarta-vfs-common
* implementation which includes the res and tmp schemes which are of no use to use
* here. Using StandardFileSystemManager lets us specify which schemes to support as
* well as providing a mechanism to change this support without recompilation.
*/
result = new StandardFileSystemManager() {
protected void configurePlugins() throws FileSystemException {
// disable automatic loading potential unsupported extensions
}
};
result.setConfiguration(getClass().getResource(IVY_VFS_CONFIG));
result.init();
// Generate and print a list of available schemes
Message.verbose("Available VFS schemes...");
String[] schemes = result.getSchemes();
Arrays.sort(schemes);
for (String scheme : schemes) {
Message.verbose("VFS Supported Scheme: " + scheme);
}
} catch (FileSystemException e) {
/*
* If our attempt to initialize a VFS Repository fails we log the failure but continue
* on. Given that an Ivy instance may involve numerous different repository types, it
* seems overly cautious to throw a runtime exception on the initialization failure of
* just one repository type.
*/
Message.error("Unable to initialize VFS repository manager!");
Message.error(e.getLocalizedMessage());
throw new IOException(e.getLocalizedMessage(), e);
}
return result;
}
Aggregations