057c523dad70bc7c6c20ade421e5edba45a6e7ac
[jalview.git] / src / jalview / gbtest / TestListener.java
1 package jalview.gbtest;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.net.BindException;
6 import java.net.URI;
7 import java.util.HashMap;
8
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import jalview.analysis.SeqsetUtils;
13 import jalview.bin.Cache;
14 import jalview.datamodel.AlignmentI;
15 import jalview.datamodel.SearchResults;
16 import jalview.datamodel.SequenceI;
17 import jalview.gui.AlignFrame;
18 import jalview.gui.Desktop;
19 import jalview.gui.SeqPanel;
20 import jalview.gui.SequenceFetcher;
21 import jalview.httpserver.AbstractRequestHandler;
22 import jalview.structure.SelectionSource;
23 import jalview.structure.VamsasSource;
24 import jalview.util.DBRefUtils;
25
26 public class TestListener extends AbstractRequestHandler
27         implements SelectionSource, VamsasSource
28 {
29
30   private String MY_PATH = "TEST";
31
32   private static String viewgene = "viewgene";
33
34   private static String highlight = "highlight";
35
36   private HashMap<String, Thread> threadMap = new HashMap<>();
37
38   private HashMap<String, AlignFrame> alignFrames = new HashMap<>();
39
40   /**
41    * Constructor that registers this as an Http request handler on path
42    * /chimeraN, where N is incremented for each instance. Call getUri to get the
43    * resulting URI for this handler.
44    * 
45    * @param chimeraBinding
46    * @throws BindException
47    *           if no free port can be assigned
48    */
49   public TestListener() throws BindException
50   {
51     setPath(MY_PATH);
52     registerHandler();
53   }
54
55   @Override
56   protected void processRequest(HttpServletRequest request,
57           HttpServletResponse response) throws IOException
58   {
59     dumpRequest(request);
60
61     try // debugging try -- remove later!
62     {
63       URI reqUri = URI.create(request.getRequestURI());
64
65       Cache.warn("*** request.getRequestURI()=" + request.getRequestURI());
66       Cache.warn("*** reqUri=" + reqUri.toASCIIString());
67       Cache.warn(
68               "*** request.getContextPath()=" + request.getContextPath());
69       Cache.warn("*** request.getPathInfo()=" + request.getPathInfo());
70
71       String pathInfo = request.getPathInfo();
72       Cache.warn("*** pathInfo=" + pathInfo);
73       if (pathInfo.startsWith("/" + viewgene + "/"))
74       {
75
76         String temp = pathInfo.substring(viewgene.length() + 2);
77         String ensemblId = temp.indexOf("/") > -1
78                 ? temp.substring(0, temp.indexOf("/"))
79                 : temp;
80         Cache.warn("*** temp=" + temp);
81         Cache.warn("*** ensemblId=" + ensemblId);
82
83         // Check whether this ensembl search has already been started
84         // and hasn't finished.
85         StringBuilder idSB = new StringBuilder();
86         // Commenting out HttpSession -- there's no SessionManager (causes
87         // Exception)
88         // HttpSession session = request.getSession(true);
89         // idSB.append(session == null ? "NOSESSION" : session.getId());
90         idSB.append(MY_PATH);
91         idSB.append("::");
92         // idSB.append(viewgene);
93         // idSB.append("::");
94         idSB.append(ensemblId);
95
96         String id = request.getParameter("id") == null ? idSB.toString()
97                 : request.getParameter("id");
98
99         Thread worker = threadMap.get(id);
100         if (worker == null)
101         {
102           String db = DBRefUtils.getCanonicalName("ensembl");
103           Desktop desktop = Desktop.instance;
104           SequenceFetcher sf = new SequenceFetcher(desktop, db, ensemblId);
105           sf.closeDialog(true);
106           sf.saveAlignFrame(id, alignFrames);
107           worker = sf.ok_actionPerformed(true);
108           threadMap.put(id, worker);
109         }
110         AlignFrame af = alignFrames.get(id);
111         PrintWriter pw = response.getWriter();
112         pw.println("id=" + id);
113         pw.println("state=" + worker.getState().toString());
114         pw.println("alignframe=" + (af == null ? null : af.getTitle()));
115         if (af != null)
116         {
117
118         }
119       }
120       else if (pathInfo.startsWith("/" + highlight + "/"))
121       {
122         String temp = pathInfo.substring(highlight.length() + 2);
123         String id = temp.indexOf("/") > -1
124                 ? temp.substring(0, temp.indexOf("/"))
125                 : temp;
126         AlignFrame af = alignFrames.get(id);
127         Cache.warn("*** temp=" + temp);
128         Cache.warn("*** id=" + id);
129         PrintWriter pw = response.getWriter();
130         pw.println("id=" + id);
131         pw.println("state=" + threadMap.get(id).getState().toString());
132         pw.println("alignframe=" + (af == null ? null : af.getTitle()));
133         if (af == null)
134         {
135           pw.println("error=alignframe not ready");
136           return;
137         }
138         String startS = request.getParameter("start");
139         String endS = request.getParameter("end");
140         int start = 0;
141         int end = -1;
142         if (startS != null || endS != null)
143         {
144           try
145           {
146             start = Integer.parseInt(startS);
147             end = Integer.parseInt(endS);
148           } catch (NumberFormatException e)
149           {
150             Cache.warn(Cache.getStackTraceString(e));
151             pw.println("error=couldn't parse start and end");
152             return;
153           }
154         }
155         pw.println("start=" + start);
156         pw.println("end=" + end);
157
158         SeqPanel seqPanel = af.alignPanel.getSeqPanel();
159         SequenceI[] seqs = af.getViewport().getSequenceSelection();
160         SequenceI seq = seqs.length > 0 ? seqs[0] : null;
161         SearchResults sr = new SearchResults();
162         sr.addResult(seq, start, end);
163         seqPanel.highlightSequence(sr);
164       }
165       else if (pathInfo.startsWith("/ensembl/"))
166       {
167         String temp = pathInfo.substring("/ensembl/".length());
168         String id = temp.indexOf("/") > -1
169                 ? temp.substring(0, temp.indexOf("/"))
170                 : temp;
171         // id is an ensembl gene ID - we look through our alignFrames to find a sequence
172         SequenceI ensDatasetSeq = null;
173         AlignFrame af=null;
174         // ideally Desktop.getDataSets() to find all datasets with this one..
175         for (AlignFrame _af: alignFrames.values())
176         {
177           AlignmentI ds = _af.getCurrentView().getAlignment().getDataset();
178           if ((ensDatasetSeq = ds.findName(id)) !=null) {
179             af = _af;
180             break;
181           }
182         }
183         if (ensDatasetSeq!=null)
184         {
185         Cache.warn("*** temp=" + temp);
186         Cache.warn("*** id=" + id);
187         PrintWriter pw = response.getWriter();
188         pw.println("id=" + id);
189         pw.println("alignframe=" + (af == null ? null : af.getTitle()));
190         if (af == null)
191         {
192           pw.println("error=alignframe not ready");
193           return;
194         }
195         String startS = request.getParameter("start");
196         String endS = request.getParameter("end");
197         int start = 0;
198         int end = -1;
199         if (startS != null || endS != null)
200         {
201           try
202           {
203             if (startS!=null) {
204               start = Integer.parseInt(startS);
205             }
206             if (endS!=null)
207             {
208               end = Integer.parseInt(endS);
209             }
210           } catch (NumberFormatException e)
211           {
212             Cache.warn(Cache.getStackTraceString(e));
213             pw.println("error=couldn't parse start and end");
214             return;
215           }
216         }
217         pw.println("start=" + start);
218         pw.println("end=" + end);
219         // highlight start for now
220         af.getCurrentView().getStructureSelectionManager().mouseOverSequence(ensDatasetSeq, start, -1, this);
221         }
222       }
223     } catch (Exception e)
224     {
225       Cache.warn(Cache.getStackTraceString(e));
226     }
227
228   }
229
230   /**
231    * Returns a display name for this service
232    */
233   @Override
234   public String getName()
235   {
236     return "TestListener";
237   }
238
239 }