View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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  }