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.rest.discovery;
21  
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  
25  import javax.ws.rs.GET;
26  import javax.ws.rs.Path;
27  import javax.ws.rs.Produces;
28  import javax.ws.rs.core.Context;
29  import javax.ws.rs.core.MediaType;
30  import javax.ws.rs.core.Response;
31  
32  import org.apache.commons.configuration.Configuration;
33  import org.mortbay.log.Log;
34  import org.neo4j.server.configuration.Configurator;
35  import org.neo4j.server.rest.repr.DiscoveryRepresentation;
36  import org.neo4j.server.rest.repr.OutputFormat;
37  
38  /**
39   * Used to discover the rest of the server urls through a HTTP GET request to the server root (/).
40   */
41  @Path( "/" )
42  public class DiscoveryService {
43      
44      private final Configuration configuration;
45      private final OutputFormat outputFormat;
46  
47      public DiscoveryService(@Context Configuration configuration, @Context OutputFormat outputFormat) {
48          this.configuration = configuration;
49          this.outputFormat = outputFormat;
50      }
51  
52      @GET
53      @Produces(MediaType.APPLICATION_JSON)
54      public Response getDiscoveryDocument() throws URISyntaxException {
55          String webAdminManagementUri = configuration.getString(Configurator.MANAGEMENT_PATH_PROPERTY_KEY, Configurator.DEFAULT_MANAGEMENT_API_PATH);
56          String dataUri = configuration.getString( Configurator.DATA_API_PATH_PROPERTY_KEY, Configurator.DEFAULT_DATA_API_PATH);
57  
58          DiscoveryRepresentation dr = new DiscoveryRepresentation(webAdminManagementUri, dataUri);
59          return outputFormat.ok(dr);
60      }
61      
62      
63      @GET
64      @Produces(MediaType.TEXT_HTML)
65      public Response redirectToWebadmin() {
66          try {
67              return Response.seeOther(new URI(Configurator.DEFAULT_WEB_ADMIN_PATH)).build();
68          } catch (URISyntaxException e) {
69              Log.warn(e.getMessage());
70              return Response.serverError().build();
71          }
72      }
73  }