Example usage for org.apache.wicket.util.io IOUtils toByteArray

List of usage examples for org.apache.wicket.util.io IOUtils toByteArray

Introduction

In this page you can find the example usage for org.apache.wicket.util.io IOUtils toByteArray.

Prototype

public static byte[] toByteArray(final Reader input) throws IOException 

Source Link

Document

Get the contents of a Reader as a byte[] using the default character encoding of the platform.

Usage

From source file:au.org.theark.core.web.component.customfieldupload.CustomFieldUploadStep4.java

License:Open Source License

private void save(ICustomImporter iCustomImporter) {
    File temp = containerForm.getModelObject().getTempFile();
    InputStream inputStream = null;
    try {//from w  ww .ja va 2s .co m
        inputStream = new BufferedInputStream(new FileInputStream(temp));
        byte[] bytes = IOUtils.toByteArray(inputStream);
        Payload payload = iArkCommonService.createPayload(bytes);
        containerForm.getModelObject().getUpload().setPayload(payload);

        containerForm.getModelObject().getUpload().setFinishTime(new Date(System.currentTimeMillis()));
        containerForm.getModelObject().getUpload()
                .setUploadStatus(iArkCommonService.getUploadStatusFor(Constants.UPLOAD_STATUS_COMPLETED));
        //TODO investigate if only one created
        iArkCommonService.createUpload(containerForm.getModelObject().getUpload());

        if (iCustomImporter instanceof CustomFieldCategoryImporter) {
            Collection<CustomFieldCategoryUpload> cfcUploadLinks = containerForm.getModelObject()
                    .getCustomFieldUploadCategoryCollection();
            for (CustomFieldCategoryUpload cfcUpload : cfcUploadLinks) {
                cfcUpload.setUpload(containerForm.getModelObject().getUpload());
                iArkCommonService.createCustomFieldCategoryUpload(cfcUpload);
            }
        } else if (iCustomImporter instanceof CustomFieldImporter) {
            Collection<CustomFieldUpload> cfUploadLinks = containerForm.getModelObject()
                    .getCustomFieldUploadCollection();
            for (CustomFieldUpload cfUpload : cfUploadLinks) {
                cfUpload.setUpload(containerForm.getModelObject().getUpload());
                iArkCommonService.createCustomFieldUpload(cfUpload);
            }
        }
    } catch (Exception ioe) {
        log.error("Failed to save the uploaded file: " + ioe);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                log.error("Unable to close inputStream: " + e.getMessage());
            }
        }
        if (temp != null) {
            // Attempt manual a delete
            temp.delete();
            temp = null;
            containerForm.getModelObject().setTempFile(temp);
        }
    }
}

From source file:au.org.theark.phenotypic.web.component.phenofielduploader.PhenoDataSetCategoryFieldUploadStep4.java

License:Open Source License

private void save(IPhenoImporter iPhenoImporter) {
    File temp = containerForm.getModelObject().getTempFile();
    InputStream inputStream = null;
    try {//w  w w .  j  a  va  2s  .  c o m
        inputStream = new BufferedInputStream(new FileInputStream(temp));
        byte[] bytes = IOUtils.toByteArray(inputStream);
        Payload payload = iArkCommonService.createPayload(bytes);
        containerForm.getModelObject().getUpload().setPayload(payload);

        containerForm.getModelObject().getUpload().setFinishTime(new Date(System.currentTimeMillis()));
        containerForm.getModelObject().getUpload()
                .setUploadStatus(iArkCommonService.getUploadStatusFor(Constants.UPLOAD_STATUS_COMPLETED));
        //TODO investigate if only one created
        iArkCommonService.createUpload(containerForm.getModelObject().getUpload());

        if (iPhenoImporter instanceof PhenoDataSetFieldCategoryImporter) {
            Collection<PhenoDataSetFieldCategoryUpload> pfcUploadLinks = containerForm.getModelObject()
                    .getPhenoFieldUploadCategoryCollection();
            for (PhenoDataSetFieldCategoryUpload pfcUpload : pfcUploadLinks) {
                pfcUpload.setStudyUpload(containerForm.getModelObject().getUpload());
                iPhenotypicService.createPhenoDataSetFieldCategoryUpload(pfcUpload);
            }
        } else if (iPhenoImporter instanceof PhenoDataSetFieldImporter) {
            Collection<PhenoFieldUpload> pfUploadLinks = containerForm.getModelObject()
                    .getPhenoFieldUploadCollection();
            for (PhenoFieldUpload pfUpload : pfUploadLinks) {
                pfUpload.setUpload(containerForm.getModelObject().getUpload());
                iPhenotypicService.createPhenoDataSetFieldUpload(pfUpload);
            }
        }
    } catch (Exception ioe) {
        log.error("Failed to save the uploaded file: " + ioe);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                log.error("Unable to close inputStream: " + e.getMessage());
            }
        }
        if (temp != null) {
            // Attempt manual a delete
            temp.delete();
            temp = null;
            containerForm.getModelObject().setTempFile(temp);
        }
    }
}

From source file:au.org.theark.study.web.component.attachments.form.DetailForm.java

License:Open Source License

@Override
protected void onSave(Form<SubjectVO> containerForm, AjaxRequestTarget target) {
    LinkSubjectStudy linkSubjectStudy = null;
    Long studyId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
    Study study = iArkCommonService.getStudy(studyId);
    Long sessionPersonId = (Long) SecurityUtils.getSubject().getSession()
            .getAttribute(au.org.theark.core.Constants.PERSON_CONTEXT_ID);
    String userId = SecurityUtils.getSubject().getPrincipal().toString();

    try {//from w ww .jav a 2  s .c  o  m
        linkSubjectStudy = iArkCommonService.getSubject(sessionPersonId, study);
        containerForm.getModelObject().getSubjectFile().setLinkSubjectStudy(linkSubjectStudy);

        // Implement Save/Update
        if (containerForm.getModelObject().getSubjectFile().getId() == null) {
            // required for file uploads
            setMultiPart(true);

            // Retrieve file and store as Blob in database
            // TODO: AJAX-ified and asynchronous and hit database
            FileUpload fileSubjectFile = fileSubjectFileField.getFileUpload();

            try {
                containerForm.getModelObject().getSubjectFile()
                        .setPayload(IOUtils.toByteArray(fileSubjectFile.getInputStream()));
            } catch (IOException e) {
                log.error("IOException trying to convert inputstream" + e);
            }

            byte[] byteArray = fileSubjectFile.getMD5();
            String checksum = getHex(byteArray);

            // Set details of ConsentFile object
            containerForm.getModelObject().getSubjectFile().setChecksum(checksum);
            containerForm.getModelObject().getSubjectFile().setFilename(fileSubjectFile.getClientFileName());
            containerForm.getModelObject().getSubjectFile().setUserId(userId);

            // Save
            iStudyService.create(containerForm.getModelObject().getSubjectFile());
            this.info("Attachment " + containerForm.getModelObject().getSubjectFile().getFilename()
                    + " was created successfully");
            //            processErrors(target);
        } else {
            FileUpload fileSubjectFile = fileSubjectFileField.getFileUpload();

            try {
                containerForm.getModelObject().getSubjectFile()
                        .setPayload(IOUtils.toByteArray(fileSubjectFile.getInputStream()));
            } catch (IOException e) {
                log.error("IOException trying to convert inputstream" + e);
            }

            byte[] byteArray = fileSubjectFile.getMD5();
            String checksum = getHex(byteArray);

            // Set details of ConsentFile object
            //containerForm.getModelObject().getSubjectFile().setChecksum(checksum);
            containerForm.getModelObject().getSubjectFile().setFilename(fileSubjectFile.getClientFileName());
            containerForm.getModelObject().getSubjectFile().setUserId(userId);

            // Update
            iStudyService.update(containerForm.getModelObject().getSubjectFile(), checksum);
            this.info("Attachment " + containerForm.getModelObject().getSubjectFile().getFilename()
                    + " was updated successfully");
        }

        processErrors(target);
        onSavePostProcess(target);
        AjaxButton deleteButton = (AjaxButton) arkCrudContainerVO.getEditButtonContainer().get("delete");
        deleteButton.setEnabled(false);

    } catch (EntityNotFoundException e) {
        this.error("The record you tried to update is no longer available in the system");
    } catch (ArkSystemException e) {
        this.error("Saving the file attachment failed. Please contact The Ark administrator.");
    }
    processErrors(target);
}

From source file:au.org.theark.study.web.component.consent.form.DetailForm.java

License:Open Source License

private void createConsentFile() throws ArkSystemException {
    FileUpload fileSubjectFile = fileUploadField.getFileUpload();

    if (fileSubjectFile != null) {
        LinkSubjectStudy linkSubjectStudy = null;
        Long studyId = (Long) SecurityUtils.getSubject().getSession()
                .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID);
        Study study = iArkCommonService.getStudy(studyId);
        Long sessionPersonId = (Long) SecurityUtils.getSubject().getSession()
                .getAttribute(au.org.theark.core.Constants.PERSON_CONTEXT_ID);

        try {//ww w  .  j av a2  s.c  om
            linkSubjectStudy = iArkCommonService.getSubject(sessionPersonId, study);
            containerForm.getModelObject().getSubjectFile().setLinkSubjectStudy(linkSubjectStudy);

            containerForm.getModelObject().getSubjectFile()
                    .setPayload(IOUtils.toByteArray(fileSubjectFile.getInputStream()));

            byte[] byteArray = fileSubjectFile.getMD5();
            String checksum = getHex(byteArray);

            // Set details of Consent File object
            containerForm.getModelObject().getSubjectFile()
                    .setStudyComp(containerForm.getModelObject().getConsent().getStudyComp());
            containerForm.getModelObject().getSubjectFile()
                    .setComments(containerForm.getModelObject().getConsent().getComments());
            containerForm.getModelObject().getSubjectFile().setChecksum(checksum);
            containerForm.getModelObject().getSubjectFile().setFilename(fileSubjectFile.getClientFileName());
            containerForm.getModelObject().getSubjectFile()
                    .setUserId(SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal().toString());

            // Save
            iStudyService.create(containerForm.getModelObject().getSubjectFile());
            this.info("Consent file: " + containerForm.getModelObject().getSubjectFile().getFilename()
                    + " was created successfully");
        } catch (IOException ioe) {
            log.error("Failed to save the uploaded file: " + ioe);
        } catch (EntityNotFoundException e) {
            log.error(e.getMessage());
        }
    }
}

From source file:com.socialsite.image.DefaultImage.java

License:Open Source License

/**
 * gets the image as byte array/*from  www.  j  a  va  2s.com*/
 * 
 * @param name
 *            name of the image file
 * @return byte array of the image
 */
public byte[] getImageData(final String name) {
    final PackageResource imageRef = PackageResource.get(getClass(), name);
    try {
        return IOUtils.toByteArray(imageRef.getResourceStream().getInputStream());
    } catch (final IOException e) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
    } catch (final ResourceStreamNotFoundException e) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
    }
    return null;
}

From source file:cz.zcu.kiv.eegdatabase.wui.components.utils.FileUtils.java

License:Apache License

/**
 * Prepare byte array from file.//from w w  w .ja va 2  s .  co m
 * 
 * @param file
 * @return
 */
public static byte[] getFileContent(File file) {

    if (file == null) {
        return new byte[0];
    } else {
        try {

            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] byteArray = IOUtils.toByteArray(fileInputStream);
            fileInputStream.close();
            return byteArray;

        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            return new byte[0];
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return new byte[0];
        }
    }

}

From source file:fiftyfive.wicket.resource.SimpleCDNTest.java

License:Apache License

/**
 * Verify that the rewritten resource URLs still work and download the expected binary data
 * once the CDN host is stripped off (as a reverse-proxy CDN would do).
 *///from   w  w w . j a  va  2 s  . co  m
void assertResourcesDownload(WicketTester tester) throws Exception {
    String[] resources = new String[] { "test.css", "test.js", "test.gif" };
    for (String res : resources) {
        InputStream is = getClass().getResourceAsStream(res);
        try {
            String uri = "wicket/resource/fiftyfive.wicket.resource.SimpleCDNTestPage/" + res;
            byte[] expected = IOUtils.toByteArray(is);
            WicketTestUtils.assertDownloadEquals(tester, uri, expected);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}

From source file:org.apache.openmeetings.web.user.rooms.RoomsPanel.java

License:Apache License

public RoomsPanel(String id, List<Room> rooms) {
    super(id);/* ww w.j  a  v  a 2 s  .  com*/
    add(new RoomListPanel("list", rooms, Application.getString(131)) {
        private static final long serialVersionUID = 1L;

        @Override
        public void onContainerClick(AjaxRequestTarget target, Room r) {
            roomId = r.getId();
            updateRoomDetails(target);
        }

        @Override
        public void onRefreshClick(AjaxRequestTarget target, Room r) {
            super.onRefreshClick(target, r);
            roomId = r.getId();
            updateRoomDetails(target);
        }
    });

    // Users in this Room
    add(details.setOutputMarkupId(true).setVisible(rooms.size() > 0));
    details.add(new Label("roomId", roomID));
    details.add(new Label("roomName", roomName));
    details.add(new Label("roomComment", roomComment));
    clients = new ListView<Client>("clients", clientsInRoom) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(final ListItem<Client> item) {
            Client client = item.getModelObject();
            final Long userId = client.getUserId();
            item.add(new Image("clientImage", new ByteArrayResource("image/jpeg") {
                private static final long serialVersionUID = 1L;

                @Override
                protected ResourceResponse newResourceResponse(Attributes attributes) {
                    ResourceResponse rr = super.newResourceResponse(attributes);
                    rr.disableCaching();
                    return rr;
                }

                @Override
                protected byte[] getData(Attributes attributes) {
                    String uri = null;
                    if (userId != null) {
                        uri = Application.getBean(UserDao.class).get(userId > 0 ? userId : -userId)
                                .getPictureuri();
                    }
                    File img = OmFileHelper.getUserProfilePicture(userId, uri);
                    try (InputStream is = new FileInputStream(img)) {
                        return IOUtils.toByteArray(is);
                    } catch (Exception e) {
                        //no-op
                    }
                    return null;
                }
            }));
            item.add(new Label("clientLogin", client.getUsername()));
            item.add(new Label("from", client.getConnectedSince()));
        }
    };
    details.add(clientsContainer.add(clients.setOutputMarkupId(true)).setOutputMarkupId(true));
}

From source file:org.apache.openmeetings.web.util.ProfileImageResourceReference.java

License:Apache License

@Override
public IResource getResource() {
    return new ByteArrayResource("image/jpeg") {
        private static final long serialVersionUID = 1L;
        private Long userId = null;
        private String uri = null;

        @Override//from  w w w.j a  v  a2s  .c om
        protected ResourceResponse newResourceResponse(Attributes attributes) {
            PageParameters params = attributes.getParameters();
            userId = params.get("id").toOptionalLong();
            uri = getBean(UserDao.class).get(userId).getPictureuri();
            ResourceResponse rr = super.newResourceResponse(attributes);
            rr.disableCaching();
            return rr;
        }

        @Override
        protected byte[] getData(Attributes attributes) {
            if (!isAbsolute(uri)) {
                File img = OmFileHelper.getUserProfilePicture(userId, uri);
                try (InputStream is = new FileInputStream(img)) {
                    return IOUtils.toByteArray(is);
                } catch (Exception e) {
                    log.error("failed to get bytes from image", e);
                }
            }
            return null;
        }
    };
}

From source file:org.artifactory.webapp.wicket.resource.ImageFileResource.java

License:Open Source License

@Override
protected byte[] getImageData(Attributes attributes) {
    InputStream inputStream = null;
    try {/*w w w. ja va2  s.co m*/
        inputStream = file.inputStream();
        return IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        throw new RuntimeException("Can't read image file", e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}