View Javadoc

1   /*
2    * $Id: SimpleImageQuery.java,v 1.1.1.1 2005/02/17 18:37:35 mch Exp $
3    */
4   
5   package org.astrogrid.dataservice.service.siap;
6   
7   import java.io.IOException;
8   import java.util.StringTokenizer;
9   import javax.servlet.http.HttpServletRequest;
10  import javax.servlet.http.HttpServletResponse;
11  import org.astrogrid.account.LoginAccount;
12  import org.astrogrid.dataservice.service.DataServer;
13  import org.astrogrid.dataservice.service.ServletHelper;
14  import org.astrogrid.query.Query;
15  import org.astrogrid.query.condition.CircleCondition;
16  import org.astrogrid.query.returns.ReturnImage;
17  import org.astrogrid.query.returns.ReturnSpec;
18  import org.astrogrid.query.returns.ReturnTable;
19  import org.astrogrid.slinger.targets.TargetMaker;
20  import org.astrogrid.webapp.DefaultServlet;
21  
22  /***
23   * Simple image access protocol. Consists of a query that returns either an image
24   * or a table that
25   * has URLs to the images themselves.  This class handles the initial
26   * query
27   * @see http://www.ivoa.net/Documents/latest/SIA.html
28   *
29   * @author mch
30   */
31  public class SimpleImageQuery extends DefaultServlet {
32     
33     DataServer server = new DataServer();
34   
35     public void doGet(HttpServletRequest request, HttpServletResponse response)  throws IOException {
36  
37        try {
38           CircleCondition circleCon = ServletHelper.makeCircleCondition(request);
39           String formatParam = request.getParameter("FORMAT");
40           if (formatParam == null) formatParam = request.getParameter("format");
41     
42           //split formats from CSV to an array
43           StringTokenizer tokenizer = new StringTokenizer(formatParam, ",");
44           String[] formats = new String[tokenizer.countTokens()];
45           for (int i = 0; i < formats.length; i++) {
46              formats[i] = tokenizer.nextToken();
47           }
48           
49           //now we ignore the list and only take the first one.  Rather than give
50           //failbacks, teh caller should check the metadata...
51           String format = formats[0];
52           
53           
54           ReturnSpec returnSpec = null;
55           boolean isTable = (ReturnTable.isTableFormat(formats));
56           boolean isImage = (ReturnImage.isImageFormat(formats));
57           
58           //check haven't specificed both
59           if ((isTable) && (isImage)) {
60              throw new IllegalArgumentException("Specify either image or table format, not both.  Given="+formatParam);
61           }
62           if (isImage) {
63              returnSpec = new ReturnImage(TargetMaker.makeTarget(response.getWriter()), format);
64           }
65           else {
66              //default as well as if format given
67              returnSpec = new ReturnTable(TargetMaker.makeTarget(response.getWriter()), format);
68           }
69           
70           try {
71              server.askQuery(LoginAccount.ANONYMOUS, new Query(circleCon, returnSpec), this);
72           } catch (Throwable e) {
73              doError(response, "SIAP error (RA="+circleCon.getRa()+", DEC="+circleCon.getDec()+", SIZE="+circleCon.getRadius()+", FORMAT="+formatParam+")", e);
74           }
75           
76        } catch (NumberFormatException e) {
77           doError(response, "Input parameters not correct",e);
78        }
79     }
80  }
81  
82