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 */
019package pt.ua.dicoogle.server.web.rest;
020
021import java.io.*;
022import org.slf4j.LoggerFactory;
023import org.apache.commons.io.IOUtils;
024import org.restlet.data.MediaType;
025import org.restlet.representation.OutputRepresentation;
026import org.restlet.representation.StreamRepresentation;
027import org.restlet.resource.Get;
028import org.restlet.resource.ServerResource;
029import pt.ua.dicoogle.plugins.PluginController;
030import pt.ua.dicoogle.sdk.StorageInputStream;
031import pt.ua.dicoogle.sdk.datastructs.SearchResult;
032import pt.ua.dicoogle.sdk.task.JointQueryTask;
033import pt.ua.dicoogle.sdk.task.Task;
034import pt.ua.dicoogle.server.web.rest.elements.FileDownloadUtils;
035
036/**
037 *
038 * @author psytek
039 *
040 * WARNING: This will return *any* file on the host! The best way to correct
041 * this will be to change the generated XML in order to produce SOPInstanceUID,
042 * and let them act as a tag for the file. This will imply a new query every
043 * time we wanto to download a new file, but that's probably fine...
044 *
045 */
046public class RestFileResource extends ServerResource {
047
048    private PluginController core;
049
050    public RestFileResource() {
051        this.core = PluginController.getInstance();
052    }
053    
054    @Get
055    public StreamRepresentation represent() {
056        String SOPInstanceUID = getRequest().getResourceRef().getQueryAsForm().getValues("uid");
057        if (SOPInstanceUID == null) {
058            return null;
059        }
060        return FileDownloadUtils.gerFileRepresentation(SOPInstanceUID);
061        /*File file = new File(queryResultList.get(0).getOrigin());
062        
063        File temp = file;
064        if (file.getAbsolutePath().endsWith(".gz"))
065        {
066            try {
067                temp = File.createTempFile(SOPInstanceUID, Platform.homePath() + ".dcm");
068            } catch (IOException ex) {
069                LoggerFactory.getLogger(RestWADOResource.class).error(ex.getMessage(), ex);
070            }
071            temp.deleteOnExit();
072            FileOutputStream fos;
073            try {
074                fos = new FileOutputStream(temp);
075                BufferedOutputStream bos = new BufferedOutputStream(fos);
076                GZIPInputStream gz = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
077                int byte_;
078                while ((byte_ = gz.read()) != -1)
079                    bos.write(byte_);
080                bos.close();
081                gz.close();
082            } catch (Exception ex) {
083                LoggerFactory.getLogger(RestWADOResource.class).error(ex.getMessage(), ex);
084            }
085
086        }*/
087    }
088    
089     private static class MyHolder extends JointQueryTask {
090         
091        @Override
092        public void onCompletion() {
093        }
094
095        @Override
096        public void onReceive(Task<Iterable<SearchResult>> e) {
097        }
098    }    
099     
100     private static class MyOutput extends OutputRepresentation{
101
102         private StorageInputStream stream;
103
104        public MyOutput(StorageInputStream stream, MediaType mediaType) {
105            super(mediaType);
106            this.stream = stream;
107        }
108        @Override
109        public void write(OutputStream out) throws IOException  {
110            try {
111                IOUtils.copy(stream.getInputStream(), out);
112            } catch (IOException ex) {
113                LoggerFactory.getLogger(RestFileResource.class).error(ex.getMessage(), ex);
114                ex.printStackTrace();
115                throw ex;
116            }
117        }
118     }
119}