extensions/net.sf.basedb.genepattern/trunk/src/net/sf/basedb/genepattern/servlet/Ajax.java

Code
Comments
Other
Rev Date Author Line
1117 10 Jun 09 nicklas 1 /*
1117 10 Jun 09 nicklas 2   $Id: TestServer.java 1106 2009-06-02 07:34:10Z nicklas $
1117 10 Jun 09 nicklas 3
1117 10 Jun 09 nicklas 4   Copyright (C) 2007 Nicklas Nordborg
1117 10 Jun 09 nicklas 5
1117 10 Jun 09 nicklas 6   This file is part of BASE - BioArray Software Environment.
1117 10 Jun 09 nicklas 7   Available at http://base.thep.lu.se/
1117 10 Jun 09 nicklas 8
1117 10 Jun 09 nicklas 9   BASE is free software; you can redistribute it and/or
1117 10 Jun 09 nicklas 10   modify it under the terms of the GNU General Public License
1117 10 Jun 09 nicklas 11   as published by the Free Software Foundation; either version 2
1117 10 Jun 09 nicklas 12   of the License, or (at your option) any later version.
1117 10 Jun 09 nicklas 13
1117 10 Jun 09 nicklas 14   BASE is distributed in the hope that it will be useful,
1117 10 Jun 09 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
1117 10 Jun 09 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1117 10 Jun 09 nicklas 17   GNU General Public License for more details.
1117 10 Jun 09 nicklas 18
1117 10 Jun 09 nicklas 19   You should have received a copy of the GNU General Public License
1117 10 Jun 09 nicklas 20   along with this program; if not, write to the Free Software
1117 10 Jun 09 nicklas 21   Foundation, Inc., 59 Temple Place - Suite 330,
1117 10 Jun 09 nicklas 22   Boston, MA  02111-1307, USA.
1117 10 Jun 09 nicklas 23 */
1117 10 Jun 09 nicklas 24 package net.sf.basedb.genepattern.servlet;
1117 10 Jun 09 nicklas 25
1117 10 Jun 09 nicklas 26 import javax.servlet.ServletException;
1117 10 Jun 09 nicklas 27 import javax.servlet.http.HttpServlet;
1117 10 Jun 09 nicklas 28 import javax.servlet.http.HttpServletRequest;
1117 10 Jun 09 nicklas 29 import javax.servlet.http.HttpServletResponse;
1117 10 Jun 09 nicklas 30
1117 10 Jun 09 nicklas 31 import net.sf.basedb.clients.web.Base;
1117 10 Jun 09 nicklas 32 import net.sf.basedb.clients.web.util.AjaxWriter;
1117 10 Jun 09 nicklas 33 import net.sf.basedb.core.SessionControl;
1117 10 Jun 09 nicklas 34 import net.sf.basedb.genepattern.GPServer;
1117 10 Jun 09 nicklas 35 import net.sf.basedb.genepattern.wrapper.GPClient;
1117 10 Jun 09 nicklas 36 import net.sf.basedb.genepattern.wrapper.TaskInfo;
1117 10 Jun 09 nicklas 37 import net.sf.basedb.util.Values;
1117 10 Jun 09 nicklas 38 import net.sf.basedb.util.error.ThrowableUtil;
1117 10 Jun 09 nicklas 39
1117 10 Jun 09 nicklas 40 import org.genepattern.webservice.WebServiceException;
1117 10 Jun 09 nicklas 41
1117 10 Jun 09 nicklas 42 import java.io.IOException;
1117 10 Jun 09 nicklas 43
1117 10 Jun 09 nicklas 44 /**
1117 10 Jun 09 nicklas 45   A servlet designed to return information to AJAX requests. The parameter
1117 10 Jun 09 nicklas 46   <code>cmd</code> is required.
1117 10 Jun 09 nicklas 47   
1117 10 Jun 09 nicklas 48   <table>
1117 10 Jun 09 nicklas 49   <tr>
1117 10 Jun 09 nicklas 50     <th>cmd</th>
1117 10 Jun 09 nicklas 51     <th>Description</td>
1117 10 Jun 09 nicklas 52   </tr>
1117 10 Jun 09 nicklas 53   <tr>
1117 10 Jun 09 nicklas 54     <td>TestLogin</td>
1117 10 Jun 09 nicklas 55     <td>Checks if a GenePattern server url/login/password is correct. Expects the following
1117 10 Jun 09 nicklas 56       parameters:
1117 10 Jun 09 nicklas 57       <ul>
1117 10 Jun 09 nicklas 58       <li>server: The GenePattern server URL
1117 10 Jun 09 nicklas 59       <li>login: The login
1117 10 Jun 09 nicklas 60       <li>password: The password
1117 10 Jun 09 nicklas 61       </ul>
1117 10 Jun 09 nicklas 62       Responds with <code>status:ok</code> if everything is ok or with
1117 10 Jun 09 nicklas 63       <code>status:error</code> + additional error information if the
1117 10 Jun 09 nicklas 64       check failed
1117 10 Jun 09 nicklas 65     </td>
1117 10 Jun 09 nicklas 66   </tr>
1117 10 Jun 09 nicklas 67   <tr>
1117 10 Jun 09 nicklas 68     <td>ListModules</td>
1117 10 Jun 09 nicklas 69     <td>Get a list of all installed modules on a GenePattern server. Expectes the
1117 10 Jun 09 nicklas 70     following parameters:
1117 10 Jun 09 nicklas 71     <ul>
1117 10 Jun 09 nicklas 72     <li>server: The GenePattern server URL
1117 10 Jun 09 nicklas 73     </ul>
1117 10 Jun 09 nicklas 74     Login and password is taken from the stored registation information. Responds with a
1117 10 Jun 09 nicklas 75     list of modules information records.
1117 10 Jun 09 nicklas 76     </td>
1117 10 Jun 09 nicklas 77   </tr>
1117 10 Jun 09 nicklas 78   </table>
1117 10 Jun 09 nicklas 79
1117 10 Jun 09 nicklas 80   @author Nicklas
1117 10 Jun 09 nicklas 81   @version 1.0
1117 10 Jun 09 nicklas 82   @base.modified $Date: 2009-06-02 09:34:10 +0200 (Tue, 02 Jun 2009) $
1117 10 Jun 09 nicklas 83 */
1117 10 Jun 09 nicklas 84 public final class Ajax
1117 10 Jun 09 nicklas 85   extends HttpServlet
1117 10 Jun 09 nicklas 86 {
1117 10 Jun 09 nicklas 87
1117 10 Jun 09 nicklas 88   private static final long serialVersionUID = -5178889855933184593L;
1117 10 Jun 09 nicklas 89
1117 10 Jun 09 nicklas 90   public Ajax()
1117 10 Jun 09 nicklas 91   {}
1117 10 Jun 09 nicklas 92   
1117 10 Jun 09 nicklas 93   @Override
1117 10 Jun 09 nicklas 94   public void doGet(HttpServletRequest request, HttpServletResponse response)
1117 10 Jun 09 nicklas 95     throws IOException, ServletException
1117 10 Jun 09 nicklas 96   {
1117 10 Jun 09 nicklas 97     String cmd = request.getParameter("cmd");
1117 10 Jun 09 nicklas 98     AjaxWriter aw = new AjaxWriter(response.getWriter());
1117 10 Jun 09 nicklas 99     try
1117 10 Jun 09 nicklas 100     {
1117 10 Jun 09 nicklas 101       if ("TestLogin".equals(cmd))
1117 10 Jun 09 nicklas 102       {
1117 10 Jun 09 nicklas 103         testLogin(request, response, aw);
1117 10 Jun 09 nicklas 104       }
1117 10 Jun 09 nicklas 105       else if ("ListModules".equals(cmd))
1117 10 Jun 09 nicklas 106       {
1117 10 Jun 09 nicklas 107         listModules(request, response, aw);
1117 10 Jun 09 nicklas 108       }
1117 10 Jun 09 nicklas 109     }
1117 10 Jun 09 nicklas 110     catch (WebServiceException ex)
1117 10 Jun 09 nicklas 111     {
1117 10 Jun 09 nicklas 112       aw.ajaxPrintEntry("status", "error");
1117 10 Jun 09 nicklas 113       aw.ajaxPrintEntry("message", ex.getMessage());
1117 10 Jun 09 nicklas 114       aw.ajaxPrintEntry("stacktrace", ThrowableUtil.stackTraceToString(ex));
1117 10 Jun 09 nicklas 115     }
1117 10 Jun 09 nicklas 116     aw.flush();
1117 10 Jun 09 nicklas 117   }
1117 10 Jun 09 nicklas 118
1117 10 Jun 09 nicklas 119   @Override
1117 10 Jun 09 nicklas 120   public void doPost(HttpServletRequest request, HttpServletResponse response)
1117 10 Jun 09 nicklas 121     throws IOException, ServletException
1117 10 Jun 09 nicklas 122   {
1117 10 Jun 09 nicklas 123     doGet(request, response);
1117 10 Jun 09 nicklas 124   }
1117 10 Jun 09 nicklas 125   
1117 10 Jun 09 nicklas 126   
1117 10 Jun 09 nicklas 127   private void testLogin(HttpServletRequest request, HttpServletResponse response, AjaxWriter aw)
1117 10 Jun 09 nicklas 128     throws WebServiceException
1117 10 Jun 09 nicklas 129   {
1117 10 Jun 09 nicklas 130     String server = request.getParameter("server");
1117 10 Jun 09 nicklas 131     String login = request.getParameter("login");
1117 10 Jun 09 nicklas 132     String password = Values.getStringOrNull(request.getParameter("password"));
1117 10 Jun 09 nicklas 133     GPClient gp = new GPClient(server, login, password);
1117 10 Jun 09 nicklas 134     gp.checkLogin();
1117 10 Jun 09 nicklas 135     aw.ajaxPrintEntry("status", "ok");
1117 10 Jun 09 nicklas 136   }
1117 10 Jun 09 nicklas 137   
1117 10 Jun 09 nicklas 138   private void listModules(HttpServletRequest request, HttpServletResponse response, AjaxWriter aw)
1117 10 Jun 09 nicklas 139     throws WebServiceException
1117 10 Jun 09 nicklas 140   {
1117 10 Jun 09 nicklas 141     String server = request.getParameter("server");
1117 10 Jun 09 nicklas 142     SessionControl sc = Base.getSessionControl(request, false);
1117 10 Jun 09 nicklas 143     GPServer gpServer = GPServer.get(sc, server);
1117 10 Jun 09 nicklas 144     
1117 10 Jun 09 nicklas 145     GPClient gp = new GPClient(gpServer);
1117 10 Jun 09 nicklas 146     TaskInfo[] modules = gp.getModules(null);
1117 10 Jun 09 nicklas 147     
1117 10 Jun 09 nicklas 148     aw.ajaxPrintEntry("status", "ok");
1117 10 Jun 09 nicklas 149     for (TaskInfo ti : modules)
1117 10 Jun 09 nicklas 150     {
1117 10 Jun 09 nicklas 151       aw.ajaxPrintEntry("name", ti.getName());
1117 10 Jun 09 nicklas 152       aw.ajaxPrintEntry("lsid", ti.getLsid());
1117 10 Jun 09 nicklas 153       aw.ajaxPrintEntry("taskType", ti.getTaskType());
1117 10 Jun 09 nicklas 154       aw.ajaxPrintEntry("description", ti.getDescription());
1117 10 Jun 09 nicklas 155       aw.ajaxEndRecord();
1117 10 Jun 09 nicklas 156     }
1117 10 Jun 09 nicklas 157   }
1117 10 Jun 09 nicklas 158   
1117 10 Jun 09 nicklas 159 }