<commit_before>/*
 * Copyright 2010-2012 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.server.network.protocol.http.command.post;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.config.OStorageEntryConfiguration;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.engine.local.OEngineLocal;
import com.orientechnologies.orient.core.engine.memory.OEngineMemory;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.exception.ODatabaseException;
import com.orientechnologies.orient.core.exception.OSecurityAccessException;
import com.orientechnologies.orient.core.index.OIndex;
import com.orientechnologies.orient.core.index.OIndexDefinition;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.security.ORole;
import com.orientechnologies.orient.core.metadata.security.OUser;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.serialization.serializer.OJSONWriter;
import com.orientechnologies.orient.core.storage.OCluster;
import com.orientechnologies.orient.core.storage.OStorage;
import com.orientechnologies.orient.core.storage.impl.local.OClusterLocal;
import com.orientechnologies.orient.core.storage.impl.local.ODataLocal;
import com.orientechnologies.orient.core.storage.impl.local.OStorageLocal;
import com.orientechnologies.orient.core.storage.impl.local.OTxSegment;
import com.orientechnologies.orient.server.network.protocol.http.OHttpRequest;
import com.orientechnologies.orient.server.network.protocol.http.OHttpResponse;
import com.orientechnologies.orient.server.network.protocol.http.OHttpUtils;
import com.orientechnologies.orient.server.network.protocol.http.command.OServerCommandAuthenticatedDbAbstract;
import com.orientechnologies.orient.server.network.protocol.http.command.OServerCommandAuthenticatedServerAbstract;

public class OServerCommandPostDatabase extends OServerCommandAuthenticatedServerAbstract {
  private static final String[] NAMES = { "POST|database/*" };

  public OServerCommandPostDatabase() {
    super("database.create");
  }

  @Override
  public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: database/<db>/<type>");

    iRequest.data.commandInfo = "Create database";

    try {
      final String databaseName = urlParts[1];
      final String storageMode = urlParts[2];
      String url = getStoragePath(databaseName, storageMode);
      final String type = urlParts.length > 3 ? urlParts[3] : "document";
      if (url != null) {
        url = url.replace(OServerCommandAuthenticatedDbAbstract.DBNAME_DIR_SEPARATOR, '/');

        final ODatabaseDocumentTx database = Orient.instance().getDatabaseFactory().createDatabase(type, url);
        if (database.exists())
          throw new ODatabaseException("Database '" + database.getURL() + "' already exists");

        for (OStorage stg : Orient.instance().getStorages()) {
          if (stg.getName().equalsIgnoreCase(database.getName()) && stg.exists())
            throw new ODatabaseException("Database named '" + database.getName() + "' already exists: " + stg);
        }
        OLogManager.instance().info(this, "Creating database " + url);
        database.create();
        sendDatabaseInfo(iRequest, iResponse, database);
      } else {
        throw new OCommandExecutionException("The '" + storageMode + "' storage mode does not exists.");<commit_msg>Fixed issue 1176: {ORIENTDB_HOME} treated as constant<commit_after>/*
 * Copyright 2010-2012 <NAME> (l.garulli--at--orient<EMAIL>.<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.server.network.protocol.http.command.post;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.config.OStorageEntryConfiguration;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.engine.local.OEngineLocal;
import com.orientechnologies.orient.core.engine.memory.OEngineMemory;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.exception.ODatabaseException;
import com.orientechnologies.orient.core.exception.OSecurityAccessException;
import com.orientechnologies.orient.core.index.OIndex;
import com.orientechnologies.orient.core.index.OIndexDefinition;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.security.ORole;
import com.orientechnologies.orient.core.metadata.security.OUser;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.serialization.serializer.OJSONWriter;
import com.orientechnologies.orient.core.storage.OCluster;
import com.orientechnologies.orient.core.storage.OStorage;
import com.orientechnologies.orient.core.storage.impl.local.OClusterLocal;
import com.orientechnologies.orient.core.storage.impl.local.ODataLocal;
import com.orientechnologies.orient.core.storage.impl.local.OStorageLocal;
import com.orientechnologies.orient.core.storage.impl.local.OTxSegment;
import com.orientechnologies.orient.server.network.protocol.http.OHttpRequest;
import com.orientechnologies.orient.server.network.protocol.http.OHttpResponse;
import com.orientechnologies.orient.server.network.protocol.http.OHttpUtils;
import com.orientechnologies.orient.server.network.protocol.http.command.OServerCommandAuthenticatedDbAbstract;
import com.orientechnologies.orient.server.network.protocol.http.command.OServerCommandAuthenticatedServerAbstract;

public class OServerCommandPostDatabase extends OServerCommandAuthenticatedServerAbstract {
  private static final String[] NAMES = { "POST|database/*" };

  public OServerCommandPostDatabase() {
    super("database.create");
  }

  @Override
  public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: database/<db>/<type>");

    iRequest.data.commandInfo = "Create database";

    try {
      final String databaseName = urlParts[1].replace(OServerCommandAuthenticatedDbAbstract.DBNAME_DIR_SEPARATOR, '/');
      final String storageMode = urlParts[2];
      String url = getStoragePath(databaseName, storageMode);
      final String type = urlParts.length > 3 ? urlParts[3] : "document";
      if (url != null) {

        final ODatabaseDocumentTx database = Orient.instance().getDatabaseFactory().createDatabase(type, url);
        if (database.exists())
          throw new ODatabaseException("Database '" + database.getURL() + "' already exists");

        for (OStorage stg : Orient.instance().getStorages()) {
          if (stg.getName().equalsIgnoreCase(database.getName()) && stg.exists())
            throw new ODatabaseException("Database named '" + database.getName() + "' already exists: " + stg);
        }
        OLogManager.instance().info(this, "Creating database " + url);
        database.create();
        sendDatabaseInfo(iRequest, iResponse, database);
      } else {
        throw new OCommandExecutionException("The '" + storageMode + "' storage mode does not exists.");
{'fraction_non_alphanumeric': 0.10503786631544287, 'fraction_numerical': 0.004170782570519152, 'mean_word_length': 5.140161725067386, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2504110', 'n_tokens_mistral': 2572, 'n_tokens_neox': 2532, 'n_words': 519}