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
10 * THTH ry - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.debug.browser.servlet;
\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
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
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
37 public class ResourceBrowserServlet extends HttpServlet {
\r
39 private static final long serialVersionUID = -8253560202827865253L;
\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
46 final PrintWriter writer = response.getWriter();
\r
48 Session session = Simantics.peekSession();
\r
49 if (session == null) {
\r
50 writer.write("<html><body>No database session.</body></html>");
\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
63 requestedResource = requestedResource.substring(1);
\r
64 final long resource = parseLong(requestedResource);
\r
66 if (resource != 0L) {
\r
67 session.syncRequest(new ReadRequest() {
\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
75 writer.write("<html><body>Resource not found: <em>");
\r
76 writer.write(requestedResource);
\r
77 writer.write("</em></body></html>");
\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
89 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
\r
91 Resource foundResource = null;
\r
92 Session session = Simantics.getSession();
\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
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
111 if (foundResource == null) {
\r
112 String[] parts = input.split("-");
\r
113 if (parts.length == 1) {
\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
121 // changeLocation(r);
\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
130 } else if (parts.length == 2) {
\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
145 if (foundResource == null) {
\r
146 // Try to see if the input data is an URI reference
\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
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
167 if (foundResource != null) {
\r
168 response.sendRedirect("" + foundResource.getResourceId());
\r
170 long rootId = session.getRootLibrary().getResourceId();
\r
171 response.sendRedirect("" + rootId);
\r
176 private static long parseLong(String s) {
\r
178 return Long.parseLong(s);
\r
179 } catch (NumberFormatException e) {
\r
184 static class GetContent extends UnaryRead<Long, ResourceBrowserContent> {
\r
185 public GetContent(long parameter) {
\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