]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/servlet/ResourceBrowserServlet.java b/bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/servlet/ResourceBrowserServlet.java
new file mode 100644 (file)
index 0000000..ac35acf
--- /dev/null
@@ -0,0 +1,196 @@
+/*******************************************************************************\r
+ * Copyright (c) 2016 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     THTH ry - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.debug.browser.servlet;\r
+\r
+import java.io.IOException;\r
+import java.io.PrintWriter;\r
+import java.net.URI;\r
+import java.net.URISyntaxException;\r
+\r
+import javax.servlet.ServletException;\r
+import javax.servlet.http.HttpServlet;\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.request.Queries;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.UnaryRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ResourceNotFoundException;\r
+import org.simantics.db.service.ClusteringSupport;\r
+import org.simantics.db.service.SerialisationSupport;\r
+import org.simantics.debug.browser.content.ResourceBrowserContent;\r
+\r
+public class ResourceBrowserServlet extends HttpServlet {\r
+\r
+       private static final long serialVersionUID = -8253560202827865253L;\r
+\r
+       @Override\r
+       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r
+               response.setContentType("text/html; charset=utf-8");\r
+               response.setStatus(HttpServletResponse.SC_OK);\r
+\r
+               final PrintWriter writer = response.getWriter();\r
+               try {\r
+                       Session session = Simantics.peekSession();\r
+                       if (session == null) {\r
+                               writer.write("<html><body>No database session.</body></html>");\r
+                               return;\r
+                       }\r
+\r
+                       String requestedResource = request.getPathInfo();\r
+                       if (requestedResource == null || requestedResource.equals("/")) {\r
+                               long rootId = session.getRootLibrary().getResourceId();\r
+                               //writer.write("<html><body>No resource requested. Try <a href=\"" + rootId + "\">database root</a>.</body></html>");\r
+                               response.sendRedirect("" + rootId);\r
+                               return;\r
+                       }\r
+\r
+                       // Skip '/' suffix\r
+                       requestedResource = requestedResource.substring(1);\r
+                       final long resource = parseLong(requestedResource);\r
+\r
+                       if (resource != 0L) {\r
+                               session.syncRequest(new ReadRequest() {\r
+                                       @Override\r
+                                       public void run(ReadGraph graph) throws DatabaseException {\r
+                                               ResourceBrowserContent content = graph.syncRequest(new GetContent(resource));\r
+                                               content.toHtml(graph, writer);\r
+                                       }\r
+                               });\r
+                       } else {\r
+                               writer.write("<html><body>Resource not found: <em>");\r
+                               writer.write(requestedResource);\r
+                               writer.write("</em></body></html>");\r
+                       }\r
+               } catch (Throwable e) {\r
+                       writer.write("<html><body>Problem occurred while reading <em>");\r
+                       writer.write(request.getPathInfo());\r
+                       writer.write("</em>:<br/><pre>");\r
+                       e.printStackTrace(writer);\r
+                       writer.write("</pre></body></html>");\r
+               }\r
+       }\r
+       \r
+       @Override\r
+       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r
+           \r
+           Resource foundResource = null;\r
+           Session session = Simantics.getSession();\r
+           \r
+           String input = request.getParameter("resource-input");\r
+           if (input != null && !input.isEmpty()) {\r
+            // There's no harm in trimming out spaces from both ends of the input.\r
+            input = input.trim();\r
+            SerialisationSupport support = session.getService(SerialisationSupport.class);\r
+            if (input.startsWith("$")) {\r
+                try {\r
+                    foundResource = support.getResource(Long.parseLong(input.substring(1)));\r
+                } catch (NumberFormatException e) {\r
+                    // Let's silently fail here for now and navigate to project resource in the end\r
+                    //e.printStackTrace();\r
+                } catch (Exception e) {\r
+                    // Let's silently fail here for now and navigate to project resource in the end\r
+                    //e.printStackTrace();\r
+                }\r
+            }\r
+    \r
+            if (foundResource == null) {\r
+                String[] parts = input.split("-");\r
+                if (parts.length == 1) {\r
+                    try {\r
+                        int resourceKey = Integer.parseInt(parts[0].trim());\r
+                        foundResource = support.getResource(resourceKey);\r
+                        // Some validation, not enough though\r
+                        ClusteringSupport cs = session.getService(ClusteringSupport.class);\r
+                        long cluster = cs.getCluster(foundResource);\r
+                        if(cluster > 0) {\r
+        //                    changeLocation(r);\r
+                        }\r
+                    } catch (NumberFormatException e1) {\r
+                        // Ignore, may happen for crap input\r
+                        // e1.printStackTrace();\r
+                    } catch (Exception e1) {\r
+                        // Let's silently fail here for now and navigate to project resource in the end\r
+                        //e1.printStackTrace();\r
+                    }\r
+                } else if (parts.length == 2) {\r
+                    try {\r
+                        int resourceIndex = Integer.parseInt(parts[1]);\r
+                        long clusterId = Long.parseLong(parts[0]);\r
+                        ClusteringSupport cs = session.getService(ClusteringSupport.class);\r
+                        foundResource = cs.getResourceByIndexAndCluster(resourceIndex, clusterId);\r
+                    } catch (NumberFormatException e1) {\r
+                        // Let's silently fail here for now and navigate to project resource in the end\r
+                        //e1.printStackTrace();\r
+                    } catch (Exception e1) {\r
+                        // Let's silently fail here for now and navigate to project resource in the end\r
+                        //e1.printStackTrace();\r
+                    }\r
+                }\r
+            }\r
+            if (foundResource == null) {\r
+                // Try to see if the input data is an URI reference\r
+                try {\r
+                    // First check that the input really is a proper URI.\r
+                    String uri = input;\r
+                    if (!input.equals("http:/") && input.endsWith("/"))\r
+                        uri = input.substring(0, input.length() - 1);\r
+                    new URI(uri);\r
+                    foundResource = session.syncRequest( Queries.resource( uri ) );\r
+                } catch (URISyntaxException e) {\r
+                    // Ignore, this is not a proper URI at all.\r
+                } catch (ResourceNotFoundException e1) {\r
+                    // Ok, this was an URI, but no resource was found.\r
+                    // Let's silently fail here for now and navigate to project resource in the end\r
+                    //e1.printStackTrace();\r
+                } catch (DatabaseException e1) {\r
+                    // Let's silently fail here for now and navigate to project resource in the end\r
+                    //e1.printStackTrace();\r
+                }\r
+            }\r
+           }\r
+        \r
+        if (foundResource != null) {\r
+            response.sendRedirect("" + foundResource.getResourceId());\r
+        } else {\r
+            long rootId = session.getRootLibrary().getResourceId();\r
+            response.sendRedirect("" + rootId);\r
+        }\r
+        return;\r
+       }\r
+\r
+       private static long parseLong(String s) {\r
+               try {\r
+                       return Long.parseLong(s);\r
+               } catch (NumberFormatException e) {\r
+                       return 0;\r
+               }\r
+       }\r
+\r
+       static class GetContent extends UnaryRead<Long, ResourceBrowserContent> {\r
+               public GetContent(long parameter) {\r
+                       super(parameter);\r
+               }\r
+\r
+               @Override\r
+               public ResourceBrowserContent perform(ReadGraph graph) throws DatabaseException {\r
+                       Resource r = graph.getService(SerialisationSupport.class).getResource(parameter);\r
+                       return ResourceBrowserContent.createContentFor(graph, r);\r
+               }\r
+       }\r
+\r
+}\r