]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/servlet/ResourceBrowserServlet.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / servlet / ResourceBrowserServlet.java
1 /*******************************************************************************\r
2  * Copyright (c) 2016 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     THTH ry - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.debug.browser.servlet;\r
13 \r
14 import java.io.IOException;\r
15 import java.io.PrintWriter;\r
16 import java.net.URI;\r
17 import java.net.URISyntaxException;\r
18 \r
19 import javax.servlet.ServletException;\r
20 import javax.servlet.http.HttpServlet;\r
21 import javax.servlet.http.HttpServletRequest;\r
22 import javax.servlet.http.HttpServletResponse;\r
23 \r
24 import org.simantics.Simantics;\r
25 import org.simantics.db.ReadGraph;\r
26 import org.simantics.db.Resource;\r
27 import org.simantics.db.Session;\r
28 import org.simantics.db.common.request.Queries;\r
29 import org.simantics.db.common.request.ReadRequest;\r
30 import org.simantics.db.common.request.UnaryRead;\r
31 import org.simantics.db.exception.DatabaseException;\r
32 import org.simantics.db.exception.ResourceNotFoundException;\r
33 import org.simantics.db.service.ClusteringSupport;\r
34 import org.simantics.db.service.SerialisationSupport;\r
35 import org.simantics.debug.browser.content.ResourceBrowserContent;\r
36 \r
37 public class ResourceBrowserServlet extends HttpServlet {\r
38 \r
39         private static final long serialVersionUID = -8253560202827865253L;\r
40 \r
41         @Override\r
42         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r
43                 response.setContentType("text/html; charset=utf-8");\r
44                 response.setStatus(HttpServletResponse.SC_OK);\r
45 \r
46                 final PrintWriter writer = response.getWriter();\r
47                 try {\r
48                         Session session = Simantics.peekSession();\r
49                         if (session == null) {\r
50                                 writer.write("<html><body>No database session.</body></html>");\r
51                                 return;\r
52                         }\r
53 \r
54                         String requestedResource = request.getPathInfo();\r
55                         if (requestedResource == null || requestedResource.equals("/")) {\r
56                                 long rootId = session.getRootLibrary().getResourceId();\r
57                                 //writer.write("<html><body>No resource requested. Try <a href=\"" + rootId + "\">database root</a>.</body></html>");\r
58                                 response.sendRedirect("" + rootId);\r
59                                 return;\r
60                         }\r
61 \r
62                         // Skip '/' suffix\r
63                         requestedResource = requestedResource.substring(1);\r
64                         final long resource = parseLong(requestedResource);\r
65 \r
66                         if (resource != 0L) {\r
67                                 session.syncRequest(new ReadRequest() {\r
68                                         @Override\r
69                                         public void run(ReadGraph graph) throws DatabaseException {\r
70                                                 ResourceBrowserContent content = graph.syncRequest(new GetContent(resource));\r
71                                                 content.toHtml(graph, writer);\r
72                                         }\r
73                                 });\r
74                         } else {\r
75                                 writer.write("<html><body>Resource not found: <em>");\r
76                                 writer.write(requestedResource);\r
77                                 writer.write("</em></body></html>");\r
78                         }\r
79                 } catch (Throwable e) {\r
80                         writer.write("<html><body>Problem occurred while reading <em>");\r
81                         writer.write(request.getPathInfo());\r
82                         writer.write("</em>:<br/><pre>");\r
83                         e.printStackTrace(writer);\r
84                         writer.write("</pre></body></html>");\r
85                 }\r
86         }\r
87         \r
88         @Override\r
89         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r
90             \r
91             Resource foundResource = null;\r
92             Session session = Simantics.getSession();\r
93             \r
94             String input = request.getParameter("resource-input");\r
95             if (input != null && !input.isEmpty()) {\r
96             // There's no harm in trimming out spaces from both ends of the input.\r
97             input = input.trim();\r
98             SerialisationSupport support = session.getService(SerialisationSupport.class);\r
99             if (input.startsWith("$")) {\r
100                 try {\r
101                     foundResource = support.getResource(Long.parseLong(input.substring(1)));\r
102                 } catch (NumberFormatException e) {\r
103                     // Let's silently fail here for now and navigate to project resource in the end\r
104                     //e.printStackTrace();\r
105                 } catch (Exception e) {\r
106                     // Let's silently fail here for now and navigate to project resource in the end\r
107                     //e.printStackTrace();\r
108                 }\r
109             }\r
110     \r
111             if (foundResource == null) {\r
112                 String[] parts = input.split("-");\r
113                 if (parts.length == 1) {\r
114                     try {\r
115                         int resourceKey = Integer.parseInt(parts[0].trim());\r
116                         foundResource = support.getResource(resourceKey);\r
117                         // Some validation, not enough though\r
118                         ClusteringSupport cs = session.getService(ClusteringSupport.class);\r
119                         long cluster = cs.getCluster(foundResource);\r
120                         if(cluster > 0) {\r
121         //                    changeLocation(r);\r
122                         }\r
123                     } catch (NumberFormatException e1) {\r
124                         // Ignore, may happen for crap input\r
125                         // e1.printStackTrace();\r
126                     } catch (Exception e1) {\r
127                         // Let's silently fail here for now and navigate to project resource in the end\r
128                         //e1.printStackTrace();\r
129                     }\r
130                 } else if (parts.length == 2) {\r
131                     try {\r
132                         int resourceIndex = Integer.parseInt(parts[1]);\r
133                         long clusterId = Long.parseLong(parts[0]);\r
134                         ClusteringSupport cs = session.getService(ClusteringSupport.class);\r
135                         foundResource = cs.getResourceByIndexAndCluster(resourceIndex, clusterId);\r
136                     } catch (NumberFormatException e1) {\r
137                         // Let's silently fail here for now and navigate to project resource in the end\r
138                         //e1.printStackTrace();\r
139                     } catch (Exception e1) {\r
140                         // Let's silently fail here for now and navigate to project resource in the end\r
141                         //e1.printStackTrace();\r
142                     }\r
143                 }\r
144             }\r
145             if (foundResource == null) {\r
146                 // Try to see if the input data is an URI reference\r
147                 try {\r
148                     // First check that the input really is a proper URI.\r
149                     String uri = input;\r
150                     if (!input.equals("http:/") && input.endsWith("/"))\r
151                         uri = input.substring(0, input.length() - 1);\r
152                     new URI(uri);\r
153                     foundResource = session.syncRequest( Queries.resource( uri ) );\r
154                 } catch (URISyntaxException e) {\r
155                     // Ignore, this is not a proper URI at all.\r
156                 } catch (ResourceNotFoundException e1) {\r
157                     // Ok, this was an URI, but no resource was found.\r
158                     // Let's silently fail here for now and navigate to project resource in the end\r
159                     //e1.printStackTrace();\r
160                 } catch (DatabaseException e1) {\r
161                     // Let's silently fail here for now and navigate to project resource in the end\r
162                     //e1.printStackTrace();\r
163                 }\r
164             }\r
165             }\r
166         \r
167         if (foundResource != null) {\r
168             response.sendRedirect("" + foundResource.getResourceId());\r
169         } else {\r
170             long rootId = session.getRootLibrary().getResourceId();\r
171             response.sendRedirect("" + rootId);\r
172         }\r
173         return;\r
174         }\r
175 \r
176         private static long parseLong(String s) {\r
177                 try {\r
178                         return Long.parseLong(s);\r
179                 } catch (NumberFormatException e) {\r
180                         return 0;\r
181                 }\r
182         }\r
183 \r
184         static class GetContent extends UnaryRead<Long, ResourceBrowserContent> {\r
185                 public GetContent(long parameter) {\r
186                         super(parameter);\r
187                 }\r
188 \r
189                 @Override\r
190                 public ResourceBrowserContent perform(ReadGraph graph) throws DatabaseException {\r
191                         Resource r = graph.getService(SerialisationSupport.class).getResource(parameter);\r
192                         return ResourceBrowserContent.createContentFor(graph, r);\r
193                 }\r
194         }\r
195 \r
196 }\r