246f51110a9a12624c1fc2208caf338c2b9e37f1
[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     this.saveParameters(request);
66
67     if (isPost)
68     {
69       access = "post";
70       try
71       {
72         body = RestHandler.getInstance().getRequestBody(request, response);
73       } catch (IOException e)
74       {
75         returnError(request, response, "could not read POST body");
76         Console.debug("Could not read POST body", e);
77         return;
78       }
79       // for ref see md5 later
80     }
81     else if (hasData)
82     {
83       access = "data";
84       // for ref see md5 later
85     }
86     else if (fileString != null)
87     {
88       access = "file";
89       ref = fileString;
90     }
91     else if (urlString != null)
92     {
93       access = "url";
94       ref = urlString;
95     }
96
97     if (access == null)
98     {
99       returnError(request, response,
100               "requires POST body or one of parameters 'data', 'file' or 'url'");
101       return;
102     }
103
104     // final content used in Future
105     String content;
106     if (isPost || hasData)
107     {
108       content = isPost ? body : data;
109       objectsPassedToProcessAsync.put("content", content); // needed as "final
110                                                            // String" in process
111       try
112       {
113         MessageDigest md5 = MessageDigest.getInstance("MD5");
114         md5.update(content.getBytes());
115         byte[] digest = md5.digest();
116         ref = DatatypeConverter.printBase64Binary(digest).toLowerCase();
117       } catch (NoSuchAlgorithmException e)
118       {
119         Console.debug("Could not find MD5 algorithm", e);
120       }
121     }
122     else
123     {
124       content = null;
125     }
126     objectsPassedToProcessAsync.put("content", content);
127
128     setId(request, access + "::" + ref);
129   }
130
131   protected void process(HttpServletRequest request,
132           HttpServletResponse response)
133   {
134     processAsync(request, response, null);
135   }
136
137   protected void processAsync(HttpServletRequest request,
138           HttpServletResponse response, final Map<String, Object> finalMap)
139   {
140     String content = (String) finalMap.get("content");
141     if (isPost || hasData)
142     {
143       // Sequence file contents being posted
144       // use File -> Input Alignment -> from Textbox
145       CutAndPasteTransfer cap = new CutAndPasteTransfer();
146       cap.setText(content);
147
148       Map<String, String> options = getOptions(request);
149
150       cap.ok_actionPerformed(null, options);
151       cap.cancel_actionPerformed(null);
152     }
153     else if (fileString != null)
154     {
155       // Sequence file on filesystem
156       // use File -> Input Alignment -> From File
157       URL url = null;
158       File file = null;
159       try
160       {
161         url = new URL(fileString);
162         file = new File(url.toURI());
163       } catch (MalformedURLException | URISyntaxException e)
164       {
165         returnError(request, response,
166                 "could not resolve file='" + fileString + "'");
167         Console.debug("Could not resolve file '" + fileString + "'", e);
168         return;
169       }
170       if (!file.exists())
171       {
172         returnError(request, response,
173                 "file='" + fileString + "' does not exist");
174         return;
175       }
176       Desktop.instance.openFile(file, null, null);
177     }
178     else if (urlString != null)
179     {
180       boolean success = Desktop.instance.loadUrl(urlString, null);
181       if (!success)
182       {
183         returnError(request, response,
184                 "url='" + urlString + "' could not be opened");
185         return;
186       }
187     }
188   }
189 }