Search in sources :

Example 1 with IORuntimeException

use of com.odysseusinc.arachne.portal.exception.IORuntimeException in project ArachneCentralAPI by OHDSI.

the class BaseStudyServiceImpl method saveFile.

@Override
@PreAuthorize("hasPermission(#studyId, 'Study', " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).UPLOAD_FILES)")
public String saveFile(String link, Long studyId, String label, IUser user) throws IOException {
    Study study = studyRepository.findOne(studyId);
    String fileNameLowerCase = UUID.randomUUID().toString();
    try {
        if (link == null) {
            throw new IORuntimeException("wrong url");
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(singletonList(MediaType.APPLICATION_OCTET_STREAM));
        HttpEntity<String> entity = new HttpEntity<>(headers);
        URL url = new URL(link);
        String fileName = FilenameUtils.getName(url.getPath());
        ResponseEntity<byte[]> response = restTemplate.exchange(link.toString(), HttpMethod.GET, entity, byte[].class);
        if (response.getStatusCode() == HttpStatus.OK) {
            String contentType = CommonFileUtils.TYPE_LINK;
            StudyFile studyFile = new StudyFile();
            studyFile.setUuid(fileNameLowerCase);
            studyFile.setContentType(contentType);
            studyFile.setStudy(study);
            studyFile.setLabel(label);
            studyFile.setRealName(fileName);
            studyFile.setLink(link);
            Date created = new Date();
            studyFile.setCreated(created);
            studyFile.setUpdated(created);
            studyFile.setAuthor(user);
            studyFile.setAntivirusStatus(AntivirusStatus.WILL_NOT_SCAN);
            studyFile.setAntivirusDescription("External links are not scanned");
            studyFileRepository.save(studyFile);
            return fileNameLowerCase;
        }
        return null;
    } catch (IOException | RuntimeException ex) {
        String message = "error save file to disk, filename=" + fileNameLowerCase + " ex=" + ex.toString();
        LOGGER.debug(message, ex);
        throw new IOException(message);
    }
}
Also used : UserStudy(com.odysseusinc.arachne.portal.model.UserStudy) Study(com.odysseusinc.arachne.portal.model.Study) FavouriteStudy(com.odysseusinc.arachne.portal.model.FavouriteStudy) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) StudyFile(com.odysseusinc.arachne.portal.model.StudyFile) IOException(java.io.IOException) URL(java.net.URL) Date(java.util.Date) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with IORuntimeException

use of com.odysseusinc.arachne.portal.exception.IORuntimeException in project ArachneCentralAPI by OHDSI.

the class BaseAnalysisServiceImpl method saveFile.

@Override
public AnalysisFile saveFile(String link, IUser user, A analysis, String label, Boolean isExecutable) throws IOException {
    throwAccessDeniedExceptionIfLocked(analysis);
    Study study = analysis.getStudy();
    String fileNameLowerCase = UUID.randomUUID().toString();
    try {
        if (link == null) {
            throw new IORuntimeException("wrong url");
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_OCTET_STREAM));
        HttpEntity<String> entity = new HttpEntity<>(headers);
        URL url = new URL(link);
        String fileName = FilenameUtils.getName(url.getPath());
        ResponseEntity<byte[]> response = restTemplate.exchange(link, HttpMethod.GET, entity, byte[].class);
        if (response.getStatusCode() == HttpStatus.OK) {
            final String contentType = response.getHeaders().getContentType().toString();
            Path pathToAnalysis = getAnalysisPath(analysis);
            Files.copy(new ByteArrayInputStream(response.getBody()), pathToAnalysis, REPLACE_EXISTING);
            AnalysisFile analysisFile = new AnalysisFile();
            analysisFile.setUuid(fileNameLowerCase);
            analysisFile.setAnalysis(analysis);
            analysisFile.setContentType(contentType);
            analysisFile.setLabel(label);
            analysisFile.setAuthor(user);
            analysisFile.setExecutable(Boolean.TRUE.equals(isExecutable));
            analysisFile.setRealName(fileName);
            analysisFile.setEntryPoint(fileName);
            Date created = new Date();
            analysisFile.setCreated(created);
            analysisFile.setUpdated(created);
            analysisFile.setVersion(1);
            return analysisFileRepository.save(analysisFile);
        }
    } catch (IOException | RuntimeException ex) {
        String message = "error save file to disk, filename=" + fileNameLowerCase + " ex=" + ex.toString();
        LOGGER.error(message, ex);
        throw new IOException(message);
    }
    return null;
}
Also used : Path(java.nio.file.Path) Study(com.odysseusinc.arachne.portal.model.Study) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) AnalysisFile(com.odysseusinc.arachne.portal.model.AnalysisFile) IOException(java.io.IOException) URL(java.net.URL) Date(java.util.Date) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 3 with IORuntimeException

use of com.odysseusinc.arachne.portal.exception.IORuntimeException in project ArachneCentralAPI by OHDSI.

the class AnalysisHelper method getAnalysisFolder.

public Path getAnalysisFolder(Analysis analysis) {
    String studyFilesPath = getStoreFilesPath();
    Path filesStoreDirPath = Paths.get(studyFilesPath);
    File fileStoreDir = filesStoreDirPath.toFile();
    if (!fileStoreDir.exists()) {
        if (!fileStoreDir.mkdirs()) {
            throw new IORuntimeException("cann't create directory:" + filesStoreDirPath);
        }
    }
    Path studyStoreDirPath = Paths.get(studyFilesPath, analysis.getStudy().getId().toString());
    File studyStoreDir = studyStoreDirPath.toFile();
    if (!studyStoreDir.exists()) {
        if (!studyStoreDir.mkdirs()) {
            throw new IORuntimeException("cann't create directory:" + studyStoreDirPath);
        }
    }
    Path storeDirPath = studyStoreDirPath.resolve(analysis.getId().toString());
    File storeDir = storeDirPath.toFile();
    if (!storeDir.exists()) {
        if (!storeDir.mkdirs()) {
            throw new IORuntimeException("cann't create directory:" + storeDirPath);
        }
    }
    return storeDirPath;
}
Also used : Path(java.nio.file.Path) IORuntimeException(com.odysseusinc.arachne.portal.exception.IORuntimeException) ResultFile(com.odysseusinc.arachne.portal.model.ResultFile) SubmissionFile(com.odysseusinc.arachne.portal.model.SubmissionFile) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File)

Aggregations

IORuntimeException (com.odysseusinc.arachne.portal.exception.IORuntimeException)3 Study (com.odysseusinc.arachne.portal.model.Study)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Path (java.nio.file.Path)2 Date (java.util.Date)2 HttpEntity (org.springframework.http.HttpEntity)2 HttpHeaders (org.springframework.http.HttpHeaders)2 AnalysisFile (com.odysseusinc.arachne.portal.model.AnalysisFile)1 FavouriteStudy (com.odysseusinc.arachne.portal.model.FavouriteStudy)1 ResultFile (com.odysseusinc.arachne.portal.model.ResultFile)1 StudyFile (com.odysseusinc.arachne.portal.model.StudyFile)1 SubmissionFile (com.odysseusinc.arachne.portal.model.SubmissionFile)1 UserStudy (com.odysseusinc.arachne.portal.model.UserStudy)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 ZipFile (net.lingala.zip4j.core.ZipFile)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1