View Javadoc

1   /*
2    * $Id: ImageResults.java,v 1.2 2005/05/27 16:21:02 clq2 Exp $
3    *
4    * (C) Copyright Astrogrid...
5    */
6   
7   package org.astrogrid.dataservice.queriers;
8   
9   import java.io.IOException;
10  import java.io.InputStream;
11  import java.io.OutputStream;
12  import java.io.Writer;
13  import java.net.URL;
14  import java.security.Principal;
15  import org.astrogrid.dataservice.queriers.status.QuerierProcessingResults;
16  import org.astrogrid.io.Piper;
17  import org.astrogrid.query.returns.ReturnImage;
18  import org.astrogrid.query.returns.ReturnSpec;
19  import org.astrogrid.slinger.targets.HttpResponseTarget;
20  
21  /*** A container interface that holds the results of a query that is an image.
22   * <p>
23   *
24   * @author M Hill
25   */
26  
27  public class ImageResults
28  {
29   
30     URL imageUrl = null;
31     Querier querier = null;
32     
33     /*** Construct with a link to the Querier that spawned these results, and a
34      * URL to the image*/
35     public ImageResults(Querier parentQuerier, URL aUrl) {
36        this.querier = parentQuerier;
37        this.imageUrl = aUrl;
38     }
39  
40     /*** returns the formats that this result implementation can produce (ie VOTABLE, HTML, CSV, etc) */
41     public static String[] getFormats() {
42        return new String[] { ReturnImage.FITS  }; //not quite true, depends on what's at the end of the URL
43     }
44     
45     /*** Looks at given format and decides which output method to use */
46     protected void write(Writer out, QuerierProcessingResults statusToUpdate, ReturnSpec returns) throws IOException {
47        
48        assert (out != null);
49  
50     }
51  
52     /*** This is a helper method for plugins; it is meant to be called
53      * from the askQuery method.  It transforms the results and sends them
54      * as required, updating the querier status appropriately.
55      */
56     public void send(ReturnSpec returns, Principal user) throws IOException {
57  
58        //we have no idea what form the original image is in, so let's ignore all
59        //requests for formatting and just pipe it straight from the url to the target
60        
61        if (returns instanceof ReturnImage) {
62  
63           if (returns.getTarget() instanceof HttpResponseTarget) {
64              //in fact, if it's an http response, forward it to the URL
65              ((HttpResponseTarget) returns.getTarget()).getResponse().sendRedirect(imageUrl.toString());
66           }
67           else {
68              InputStream in = imageUrl.openStream();
69              returns.getTarget().setMimeType("image");
70              OutputStream out = returns.getTarget().openOutputStream();
71              
72              Piper.bufferedPipe(in, out);
73           }
74        }
75        else {
76           throw new UnsupportedOperationException("Unknown return type "+returns.getClass().getName()+", specify Image");
77        }
78     }
79     
80  
81  
82  }
83  
84