1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.neo4j.server.rest.web;
21
22 import javax.ws.rs.GET;
23 import javax.ws.rs.Path;
24 import javax.ws.rs.core.Context;
25 import javax.ws.rs.core.UriInfo;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.io.Reader;
31
32 @Path("/resource")
33 public class ResourcesService {
34 final static String JAVASCRIPT_BODY;
35 static {
36
37
38 String body = null;
39 try {
40 body = readResourceAsString("htmlbrowse.js");
41 } catch (Exception e) {
42 body = readFileAsString("src/main/resources/htmlbrowse.js");
43 }
44 JAVASCRIPT_BODY = body;
45 }
46
47 public ResourcesService(@Context UriInfo uriInfo) {
48 }
49
50 @GET
51 @Path("htmlbrowse.js")
52 public String getHtmlBrowseJavascript() {
53 return JAVASCRIPT_BODY;
54 }
55
56 private static String readFileAsString(String file) {
57 try {
58 return readAsString(new FileInputStream(file));
59 } catch (IOException e) {
60 throw new RuntimeException(e);
61 }
62 }
63
64 private static String readResourceAsString(String resource) {
65 return readAsString(ClassLoader.getSystemResourceAsStream(resource));
66 }
67
68 private static String readAsString(InputStream input) {
69 final char[] buffer = new char[0x10000];
70 StringBuilder out = new StringBuilder();
71 Reader reader = null;
72 try {
73 reader = new InputStreamReader(input, "UTF-8" );
74 int read;
75 do {
76 read = reader.read(buffer, 0, buffer.length);
77 if (read > 0) {
78 out.append(buffer, 0, read);
79 }
80 } while (read >= 0);
81 } catch (IOException e) {
82 throw new RuntimeException(e);
83 } finally {
84 if (reader != null) {
85 try {
86 reader.close();
87 } catch (IOException e) {
88
89 }
90 }
91 }
92 return out.toString();
93 }
94 }