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