]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/servlet/ResourceBrowserServlet.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / servlet / ResourceBrowserServlet.java
1 /*******************************************************************************
2  * Copyright (c) 2016 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     THTH ry - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.debug.browser.servlet;
13
14 import java.io.IOException;
15 import java.io.PrintWriter;
16 import java.net.URI;
17 import java.net.URISyntaxException;
18
19 import javax.servlet.ServletException;
20 import javax.servlet.http.HttpServlet;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
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;
36
37 public class ResourceBrowserServlet extends HttpServlet {
38
39         private static final long serialVersionUID = -8253560202827865253L;
40
41         @Override
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);
45
46                 final PrintWriter writer = response.getWriter();
47                 try {
48                         Session session = Simantics.peekSession();
49                         if (session == null) {
50                                 writer.write("<html><body>No database session.</body></html>");
51                                 return;
52                         }
53
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);
59                                 return;
60                         }
61
62                         // Skip '/' suffix
63                         requestedResource = requestedResource.substring(1);
64                         final long resource = parseLong(requestedResource);
65
66                         if (resource != 0L) {
67                                 session.syncRequest(new ReadRequest() {
68                                         @Override
69                                         public void run(ReadGraph graph) throws DatabaseException {
70                                                 ResourceBrowserContent content = graph.syncRequest(new GetContent(resource));
71                                                 content.toHtml(graph, writer);
72                                         }
73                                 });
74                         } else {
75                                 writer.write("<html><body>Resource not found: <em>");
76                                 writer.write(requestedResource);
77                                 writer.write("</em></body></html>");
78                         }
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>");
85                 }
86         }
87         
88         @Override
89         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
90             
91             Resource foundResource = null;
92             Session session = Simantics.getSession();
93             
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.
97             input = input.trim();
98             SerialisationSupport support = session.getService(SerialisationSupport.class);
99             if (input.startsWith("$")) {
100                 try {
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();
108                 }
109             }
110     
111             if (foundResource == null) {
112                 String[] parts = input.split("-");
113                 if (parts.length == 1) {
114                     try {
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);
120                         if(cluster > 0) {
121         //                    changeLocation(r);
122                         }
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();
129                     }
130                 } else if (parts.length == 2) {
131                     try {
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();
142                     }
143                 }
144             }
145             if (foundResource == null) {
146                 // Try to see if the input data is an URI reference
147                 try {
148                     // First check that the input really is a proper URI.
149                     String uri = input;
150                     if (!input.equals("http:/") && input.endsWith("/"))
151                         uri = input.substring(0, input.length() - 1);
152                     new URI(uri);
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();
163                 }
164             }
165             }
166         
167         if (foundResource != null) {
168             response.sendRedirect("" + foundResource.getResourceId());
169         } else {
170             long rootId = session.getRootLibrary().getResourceId();
171             response.sendRedirect("" + rootId);
172         }
173         return;
174         }
175
176         private static long parseLong(String s) {
177                 try {
178                         return Long.parseLong(s);
179                 } catch (NumberFormatException e) {
180                         return 0;
181                 }
182         }
183
184         static class GetContent extends UnaryRead<Long, ResourceBrowserContent> {
185                 public GetContent(long parameter) {
186                         super(parameter);
187                 }
188
189                 @Override
190                 public ResourceBrowserContent perform(ReadGraph graph) throws DatabaseException {
191                         Resource r = graph.getService(SerialisationSupport.class).getResource(parameter);
192                         return ResourceBrowserContent.createContentFor(graph, r);
193                 }
194         }
195
196 }