1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.neo4j.server.webadmin.rest.representations;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.neo4j.server.rest.repr.MappingRepresentation;
26 import org.neo4j.server.rest.repr.MappingSerializer;
27
28 public class ServiceDefinitionRepresentation extends MappingRepresentation
29 {
30 private final HashMap<String, String> uris;
31 private final HashMap<String, String> templates;
32 private final String basePath;
33
34 public ServiceDefinitionRepresentation( String basePath )
35 {
36 super( "service-definition" );
37 this.basePath = basePath;
38 uris = new HashMap<String, String>();
39 templates = new HashMap<String, String>();
40 }
41
42 public void resourceUri( String name, String subPath )
43 {
44 uris.put( name, relative( subPath ) );
45 }
46
47 public void resourceTemplate( String name, String subPath )
48 {
49 templates.put( name, relative( subPath ) );
50 }
51
52 private String relative( String subPath )
53 {
54 if ( basePath.endsWith( "/" ) )
55 {
56 if ( subPath.startsWith( "/" ) ) return basePath + subPath.substring( 1 );
57 }
58 else if ( !subPath.startsWith( "/" ) ) return basePath + "/" + subPath;
59 return basePath + subPath;
60 }
61
62 @Override
63 public void serialize( MappingSerializer serializer )
64 {
65 serializer.putMapping( "resources", new MappingRepresentation( "resources" )
66 {
67 @Override
68 protected void serialize( MappingSerializer resourceSerializer )
69 {
70 for ( Map.Entry<String, String> entry : uris.entrySet() )
71 {
72 resourceSerializer.putUri( entry.getKey(), entry.getValue() );
73 }
74
75 for ( Map.Entry<String, String> entry : templates.entrySet() )
76 {
77 resourceSerializer.putUriTemplate( entry.getKey(), entry.getValue() );
78 }
79 }
80 } );
81 }
82 }