Search in sources :

Example 11 with ReaderInputStream

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);
    }
}
Also used : ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 12 with ReaderInputStream

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());
}
Also used : InputStreamReader(java.io.InputStreamReader) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ReportDto(com.evolveum.midpoint.web.page.admin.reports.dto.ReportDto) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) FileInputStream(java.io.FileInputStream) FileUploadField(org.apache.wicket.markup.html.form.upload.FileUploadField) MidPointApplication(com.evolveum.midpoint.web.security.MidPointApplication) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) WebApplicationConfiguration(com.evolveum.midpoint.web.security.WebApplicationConfiguration) File(org.apache.wicket.util.file.File) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload)

Example 13 with ReaderInputStream

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PrismContext(com.evolveum.midpoint.prism.PrismContext) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) EventHandler(com.evolveum.midpoint.common.validator.EventHandler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) FileInputStream(java.io.FileInputStream) PrismObject(com.evolveum.midpoint.prism.PrismObject) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Objectable(com.evolveum.midpoint.prism.Objectable) File(java.io.File) Validator(com.evolveum.midpoint.common.validator.Validator) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService)

Example 14 with ReaderInputStream

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));
                }
            }
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) PreparedStatement(java.sql.PreparedStatement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataFileStream(org.apache.avro.file.DataFileStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) CharArrayReader(java.io.CharArrayReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 15 with ReaderInputStream

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()));
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) User(org.eclipse.sw360.datahandler.thrift.users.User) StringReader(java.io.StringReader) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Test(org.junit.Test)

Aggregations

ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)25 StringReader (java.io.StringReader)16 Test (org.junit.Test)15 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)8 User (org.eclipse.sw360.datahandler.thrift.users.User)8 InputStream (java.io.InputStream)7 FileInputStream (java.io.FileInputStream)4 InputStreamReader (java.io.InputStreamReader)4 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 HttpEntity (org.apache.http.HttpEntity)3 StatusLine (org.apache.http.StatusLine)3 HttpGet (org.apache.http.client.methods.HttpGet)3 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)3 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)3 TestHelper.assertLicenseInfoParsingResult (org.eclipse.sw360.licenseinfo.TestHelper.assertLicenseInfoParsingResult)3 EventHandler (com.evolveum.midpoint.common.validator.EventHandler)2 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 MidPointApplication (com.evolveum.midpoint.web.security.MidPointApplication)2 WebApplicationConfiguration (com.evolveum.midpoint.web.security.WebApplicationConfiguration)2