001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle. 005 * 006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019/* 020 * To change this license header, choose License Headers in Project Properties. 021 * To change this template file, choose Tools | Templates 022 * and open the template in the editor. 023 */ 024 025package pt.ua.dicoogle.server.web.rest.elements; 026 027import java.io.IOException; 028import java.io.OutputStream; 029import java.net.URI; 030import java.util.HashMap; 031import java.util.concurrent.ExecutionException; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036import org.apache.commons.io.FilenameUtils; 037import org.apache.commons.io.IOUtils; 038import org.restlet.data.Disposition; 039import org.restlet.data.MediaType; 040import org.restlet.representation.OutputRepresentation; 041import pt.ua.dicoogle.plugins.PluginController; 042import pt.ua.dicoogle.sdk.StorageInputStream; 043import pt.ua.dicoogle.sdk.datastructs.SearchResult; 044import pt.ua.dicoogle.sdk.task.JointQueryTask; 045import pt.ua.dicoogle.sdk.task.Task; 046import pt.ua.dicoogle.server.web.rest.RestDimResource; 047import pt.ua.dicoogle.server.web.rest.RestFileResource; 048 049/** 050 * 051 * @author tiago 052 */ 053public class FileDownloadUtils { 054 055 public static OutputRepresentation gerFileRepresentation(String SOPInstanceUID) { 056 String query = "SOPInstanceUID:" + SOPInstanceUID; 057 058 HashMap<String, String> extraFields = new HashMap<>(); 059 extraFields.put("SOPInstanceUID", "SOPInstanceUID"); 060 061 PluginController pc = PluginController.getInstance(); 062 JointQueryTask task = new MyHolder(); 063 pc.queryAll(task, query, extraFields); 064 Iterable<SearchResult> queryResults = null; 065 try { 066 queryResults = task.get(); 067 } catch (InterruptedException | ExecutionException ex) { 068 LoggerFactory.getLogger(RestFileResource.class).error(ex.getMessage(), ex); 069 return null; 070 } 071 072 if(!queryResults.iterator().hasNext()) 073 return null; 074 075 URI fileURI = null; 076 for (SearchResult r : queryResults) { 077 LoggerFactory.getLogger(RestDimResource.class).error(r.getURI().toString()); 078 fileURI = r.getURI(); 079 } 080 081 Iterable<StorageInputStream> str = pc.resolveURI(fileURI); 082 if(!str.iterator().hasNext()) 083 return null; 084 085 for(StorageInputStream s : str){ 086 MyOutput out = new MyOutput(s, MediaType.register("application/dicom", 087 "dicom medical data file")); 088 089 System.out.println("Setting File Type"); 090 Disposition d = new Disposition(Disposition.TYPE_ATTACHMENT); 091 String name = FilenameUtils.getName(s.getURI().getPath()); 092 if(name.isEmpty()) 093 name = "Downloaded.dcm"; 094 d.setFilename(name); 095 096 097 out.setDisposition(d); 098 099 return out; 100 } 101 return null; 102 } 103 104 private static class MyHolder extends JointQueryTask { 105 106 @Override 107 public void onCompletion() { 108 } 109 110 @Override 111 public void onReceive(Task<Iterable<SearchResult>> e) { 112 } 113 } 114 115 private static class MyOutput extends OutputRepresentation{ 116 117 private final StorageInputStream stream; 118 119 public MyOutput(StorageInputStream stream, MediaType mediaType) { 120 super(mediaType); 121 this.stream = stream; 122 } 123 @Override 124 public void write(OutputStream out) throws IOException { 125 try { 126 IOUtils.copy(stream.getInputStream(), out); 127 } catch (IOException ex) { 128 LoggerFactory.getLogger(RestFileResource.class).error(ex.getMessage(), ex); 129 throw ex; 130 } 131 132 } 133 } 134 135}