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