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.configuration.validation;
    21
    22  import java.net.URI;
    23  import java.net.URISyntaxException;
    24
    25  import org.apache.commons.configuration.Configuration;
    26  import org.neo4j.server.configuration.Configurator;
    27
    28  public class WebadminConfigurationRule implements ValidationRule {
    29      @Override
    30      public void validate(Configuration configuration) throws RuleFailedException {
    31          String managementApi = validateConfigurationContainsKey(configuration, Configurator.MANAGEMENT_PATH_PROPERTY_KEY);
    32          String restApi = validateConfigurationContainsKey(configuration, Configurator.DATA_API_PATH_PROPERTY_KEY);
    33
    34          // Check URIs are ok
    35          URI managementUri =  validateAndNormalizeUri(managementApi, Configurator.MANAGEMENT_PATH_PROPERTY_KEY);
    36          URI restUri = validateAndNormalizeUri(restApi, Configurator.DATA_API_PATH_PROPERTY_KEY);
    37
    38          // Overwrite the properties with the new normalised URIs
    39          configuration.clearProperty(Configurator.MANAGEMENT_PATH_PROPERTY_KEY);
    40          configuration.addProperty(Configurator.MANAGEMENT_PATH_PROPERTY_KEY, managementUri.toString());
    41
    42          configuration.clearProperty(Configurator.DATA_API_PATH_PROPERTY_KEY);
    43          configuration.addProperty(Configurator.DATA_API_PATH_PROPERTY_KEY, restUri.toString());
    44      }
    45
    46      private String trimTrailingSlash(String uri) {
    47          if(!uri.endsWith("/")) return uri;
    48
    49          return uri.substring(0, uri.length() -1);
    50      }
    51
    52      private URI validateAndNormalizeUri(String uri, String property) {
    53          URI result = null;
    54          try {
    55              result = new URI(uri).normalize();
    56              String resultStr = result.toString();
    57              if(resultStr.endsWith("/")) {
    58                  result = new URI(trimTrailingSlash(resultStr));
    59              }
    60          } catch (URISyntaxException e) {
    61              new RuleFailedException("The specified URI [%s] for the property [%s] is invalid. Please correct the neo4j-server.properties file.", uri ,property);
    62          }
    63          return result;
    64      }
    65
    66      private String validateConfigurationContainsKey(Configuration configuration, String key) throws RuleFailedException {
    67          if(!configuration.containsKey(key)) {
    68              throw new RuleFailedException("Webadmin configuration not found. Check that the neo4j-server.properties file contains the [%s] property.", key);
    69          }
    70
    71          return (String)configuration.getProperty(key);
    72      }
    73  }