JAL-3982 highlight any 3D structure mappings for positions mapped via CDS/transcript...
[jalview.git] / src / jalview / rest / InputAlignmentEndpoint.java
1 package jalview.rest;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
6 import java.net.URISyntaxException;
7 import java.net.URL;
8 import java.security.MessageDigest;
9 import java.security.NoSuchAlgorithmException;
10 import java.util.Map;
11
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.xml.bind.DatatypeConverter;
15
16 import jalview.bin.Console;
17 import jalview.gui.CutAndPasteTransfer;
18 import jalview.gui.Desktop;
19
20 public class InputAlignmentEndpoint extends AbstractEndpointAsync
21 {
22   public InputAlignmentEndpoint(API api)
23   {
24     super(api, path, name, parameters, description);
25   }
26
27   protected static final String path = "inputalignment";
28
29   private static final String name = "Input Alignment";
30
31   private static final String parameters = "POST <body> | GET ?[data=<data>|file=<fileURI>|url=<URL>]";
32
33   private static final String description = "Input an alignment from POST request body, GET request 'data' parameter, local file in 'file' parameter, url in 'url' parameter";
34
35   private String fileString;
36
37   private String urlString;
38
39   private String method;
40
41   private String data;
42
43   private String body;
44
45   private boolean isPost;
46
47   private boolean hasData;
48
49   private String access = null;
50
51   private String ref = null;
52
53   @Override
54   protected void initialise(HttpServletRequest request,
55           HttpServletResponse response)
56   {
57     fileString = request.getParameter("file");
58     urlString = request.getParameter("url");
59     method = request.getMethod().toLowerCase();
60     data = request.getParameter("data");
61     body = null;
62     isPost = method.equalsIgnoreCase("post");
63     hasData = data != null;
64
65     if (isPost)
66     {
67       access = "post";
68       try
69       {
70         body = RestHandler.getInstance().getRequestBody(request, response);
71         Console.debug("BODY='" + body + "'");
72       } catch (IOException e)
73       {
74         returnError(request, response, "could not read POST body");
75         Console.debug("Could not read POST body", e);
76         return;
77       }
78       // for ref see md5 later
79     }
80     else if (hasData)
81     {
82       access = "data";
83       // for ref see md5 later
84     }
85     else if (fileString != null)
86     {
87       access = "file";
88       ref = fileString;
89     }
90     else if (urlString != null)
91     {
92       access = "url";
93       ref = urlString;
94     }
95
96     if (access == null)
97     {
98       returnError(request, response,
99               "requires POST body or one of parameters 'data', 'file' or 'url'");
100       return;
101     }
102
103     // final content used in Future
104     String content;
105     if (isPost || hasData)
106     {
107       content = isPost ? body : data;
108       objectsPassedToProcessAsync.put("content", content); // needed as "final
109                                                            // String" in process
110       try
111       {
112         MessageDigest md5 = MessageDigest.getInstance("MD5");
113         md5.update(content.getBytes());
114         byte[] digest = md5.digest();
115         ref = DatatypeConverter.printBase64Binary(digest).toLowerCase();
116       } catch (NoSuchAlgorithmException e)
117       {
118         Console.debug("Could not find MD5 algorithm", e);
119       }
120     }
121     else
122     {
123       content = null;
124     }
125     objectsPassedToProcessAsync.put("content", content);
126
127     setId(request, access + "::" + ref);
128   }
129
130   protected void process(HttpServletRequest request,
131           HttpServletResponse response)
132   {
133     processAsync(request, response, null);
134   }
135
136   protected void processAsync(HttpServletRequest request,
137           HttpServletResponse response, final Map<String, Object> finalMap)
138   {
139     String content = (String) finalMap.get("content");
140     if (isPost || hasData)
141     {
142       // Sequence file contents being posted
143       // use File -> Input Alignment -> from Textbox
144       CutAndPasteTransfer cap = new CutAndPasteTransfer();
145       cap.setText(content);
146       cap.ok_actionPerformed(null);
147       cap.cancel_actionPerformed(null);
148     }
149     else if (fileString != null)
150     {
151       // Sequence file on filesystem
152       // use File -> Input Alignment -> From File
153       URL url = null;
154       File file = null;
155       try
156       {
157         url = new URL(fileString);
158         file = new File(url.toURI());
159       } catch (MalformedURLException | URISyntaxException e)
160       {
161         returnError(request, response,
162                 "could not resolve file='" + fileString + "'");
163         Console.debug("Could not resolve file '" + fileString + "'", e);
164         return;
165       }
166       if (!file.exists())
167       {
168         returnError(request, response,
169                 "file='" + fileString + "' does not exist");
170         return;
171       }
172       Desktop.instance.openFile(file, null, null);
173     }
174     else if (urlString != null)
175     {
176       boolean success = Desktop.instance.loadUrl(urlString, null);
177       if (!success)
178       {
179         returnError(request, response,
180                 "url='" + urlString + "' could not be opened");
181         return;
182       }
183     }
184   }
185 }