use of org.apache.commons.io.input.ReaderInputStream in project pentaho-platform by pentaho.
the class PentahoVersionCheckReflectHelperTest method validateResult.
private void validateResult(String result) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
StringReader stringReader = new StringReader(result);
documentBuilder.parse(new ReaderInputStream(stringReader));
} catch (ParserConfigurationException | SAXException | IOException e) {
assertTrue("unexpected exception", false);
}
}
use of org.apache.commons.io.input.ReaderInputStream in project midpoint by Evolveum.
the class PageNewReport method importReportFromFilePerformed.
private void importReportFromFilePerformed(AjaxRequestTarget target) {
OperationResult result = new OperationResult(OPERATION_IMPORT_REPORT);
FileUploadField file = (FileUploadField) get(createComponentPath(ID_MAIN_FORM, ID_INPUT, ID_INPUT_FILE, ID_FILE_INPUT));
final FileUpload uploadedFile = file.getFileUpload();
if (uploadedFile == null) {
error(getString("PageNewReport.message.nullFile"));
target.add(getFeedbackPanel());
return;
}
InputStream stream = null;
File newFile = null;
try {
// Create new file
MidPointApplication application = getMidpointApplication();
WebApplicationConfiguration config = application.getWebApplicationConfiguration();
File folder = new File(config.getImportFolder());
if (!folder.exists() || !folder.isDirectory()) {
folder.mkdir();
}
newFile = new File(folder, uploadedFile.getClientFileName());
// Check new file, delete if it already exists
if (newFile.exists()) {
newFile.delete();
}
// Save file
// Task task = createSimpleTask(OPERATION_IMPORT_FILE);
newFile.createNewFile();
uploadedFile.writeTo(newFile);
InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8");
// reader.
stream = new ReaderInputStream(reader, reader.getEncoding());
byte[] reportIn = IOUtils.toByteArray(stream);
setResponsePage(new PageReport(new ReportDto(Base64.encodeBase64(reportIn))));
} catch (Exception ex) {
result.recordFatalError("Couldn't import file.", ex);
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't import file", ex);
} finally {
if (stream != null) {
IOUtils.closeQuietly(stream);
}
if (newFile != null) {
FileUtils.deleteQuietly(newFile);
}
}
showResult(result);
target.add(getFeedbackPanel());
}
use of org.apache.commons.io.input.ReaderInputStream in project midpoint by Evolveum.
the class ImportObjects method execute.
public boolean execute() {
System.out.println("Starting objects import.");
File objects = new File(filePath);
if (!objects.exists() || !objects.canRead()) {
System.out.println("XML file with objects '" + objects.getAbsolutePath() + "' doesn't exist or can't be read.");
return false;
}
InputStream input = null;
ClassPathXmlApplicationContext context = null;
try {
System.out.println("Loading spring contexts.");
context = new ClassPathXmlApplicationContext(CONTEXTS);
InputStreamReader reader = new InputStreamReader(new FileInputStream(objects), "utf-8");
input = new ReaderInputStream(reader, reader.getEncoding());
final RepositoryService repository = context.getBean("repositoryService", RepositoryService.class);
PrismContext prismContext = context.getBean(PrismContext.class);
EventHandler handler = new EventHandler() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
return EventResult.cont();
}
@Override
public <T extends Objectable> EventResult postMarshall(PrismObject<T> object, Element objectElement, OperationResult objectResult) {
try {
String displayName = getDisplayName(object);
System.out.println("Importing object " + displayName);
repository.addObject((PrismObject<ObjectType>) object, null, objectResult);
} catch (Exception ex) {
objectResult.recordFatalError("Unexpected problem: " + ex.getMessage(), ex);
System.out.println("Exception occurred during import, reason: " + ex.getMessage());
ex.printStackTrace();
}
objectResult.recordSuccessIfUnknown();
if (objectResult.isAcceptable()) {
// Continue import
return EventResult.cont();
} else {
return EventResult.skipObject(objectResult.getMessage());
}
}
@Override
public void handleGlobalError(OperationResult currentResult) {
}
};
Validator validator = new Validator(prismContext, handler);
validator.setVerbose(true);
validator.setValidateSchema(validateSchema);
OperationResult result = new OperationResult("Import objeccts");
validator.validate(input, result, OperationConstants.IMPORT_OBJECT);
result.recomputeStatus();
if (!result.isSuccess()) {
System.out.println("Operation result was not success, dumping result.\n" + result.debugDump(3));
}
} catch (Exception ex) {
System.out.println("Exception occurred during import task, reason: " + ex.getMessage());
ex.printStackTrace();
} finally {
IOUtils.closeQuietly(input);
destroyContext(context);
}
System.out.println("Objects import finished.");
return true;
}
use of org.apache.commons.io.input.ReaderInputStream in project nifi by apache.
the class TestJdbcCommon method testClob.
@Test
public void testClob() throws Exception {
try (final Statement stmt = con.createStatement()) {
stmt.executeUpdate("CREATE TABLE clobtest (id INT, text CLOB(64 K))");
stmt.execute("INSERT INTO clobtest VALUES (41, NULL)");
PreparedStatement ps = con.prepareStatement("INSERT INTO clobtest VALUES (?, ?)");
ps.setInt(1, 42);
final char[] buffer = new char[4002];
IntStream.range(0, 4002).forEach((i) -> buffer[i] = String.valueOf(i % 10).charAt(0));
// Put a zero-byte in to test the buffer building logic
buffer[1] = 0;
ReaderInputStream isr = new ReaderInputStream(new CharArrayReader(buffer), Charset.defaultCharset());
// - set the value of the input parameter to the input stream
ps.setAsciiStream(2, isr, 4002);
ps.execute();
isr.close();
final ResultSet resultSet = stmt.executeQuery("select * from clobtest");
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
JdbcCommon.convertToAvroStream(resultSet, outStream, false);
final byte[] serializedBytes = outStream.toByteArray();
assertNotNull(serializedBytes);
// Deserialize bytes to records
final InputStream instream = new ByteArrayInputStream(serializedBytes);
final DatumReader<GenericRecord> datumReader = new GenericDatumReader<>();
try (final DataFileStream<GenericRecord> dataFileReader = new DataFileStream<>(instream, datumReader)) {
GenericRecord record = null;
while (dataFileReader.hasNext()) {
// Reuse record object by passing it to next(). This saves us from
// allocating and garbage collecting many objects for files with
// many items.
record = dataFileReader.next(record);
Integer id = (Integer) record.get("ID");
Object o = record.get("TEXT");
if (id == 41) {
assertNull(o);
} else {
assertNotNull(o);
final String text = o.toString();
assertEquals(4002, text.length());
// Third character should be '2'
assertEquals('2', text.charAt(2));
}
}
}
}
}
use of org.apache.commons.io.input.ReaderInputStream in project sw360portal by sw360.
the class CombinedCLIParserTest method testIsApplicableToFailsOnIncorrectRootElement.
@Test
public void testIsApplicableToFailsOnIncorrectRootElement() throws Exception {
AttachmentContent content = new AttachmentContent().setId("A1").setFilename("a.xml").setContentType("application/xml");
when(connector.getAttachmentStream(content, new User(), new Project())).thenReturn(new ReaderInputStream(new StringReader("<wrong-root/>")));
assertFalse(parser.isApplicableTo(attachment, new User(), new Project()));
}
Aggregations