<commit_before>/*! ******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2019 by <NAME> : http://www.pentaho.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 org.pentaho.di.connections.ui.endpoints;

import org.pentaho.di.base.AbstractMeta;
import org.pentaho.di.connections.ConnectionDetails;
import org.pentaho.di.connections.ConnectionManager;
import org.pentaho.di.connections.ui.dialog.ConnectionDialog;
import org.pentaho.di.connections.ui.tree.ConnectionFolderProvider;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.EngineMetaInterface;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.osgi.metastore.locator.api.MetastoreLocator;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.util.function.Supplier;

import org.pentaho.di.ui.util.HelpUtils;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

public class ConnectionEndpoints {

  private static Class<?> PKG = ConnectionDialog.class;
  private Supplier<Spoon> spoonSupplier = Spoon::getInstance;

  private ConnectionManager connectionManager;

  public static final String HELP_URL =
    Const.getDocUrl( BaseMessages.getString( PKG, "ConnectionDialog.help.dialog.Help" ) );

  public ConnectionEndpoints( MetastoreLocator metastoreLocator ) {
    this.connectionManager = ConnectionManager.getInstance();
    this.connectionManager.setMetastoreSupplier( metastoreLocator::getMetastore );
  }

  @GET
  @Path( "/types" )
  @Produces( { APPLICATION_JSON } )
  public Response getTypes() {
    return Response.ok( connectionManager.getItems() ).build();
  }

  @GET
  @Path( "/connection/{scheme}" )
  @Produces( { APPLICATION_JSON } )
  public Response getFields( @PathParam( "scheme" ) String scheme ) {
    return Response.ok( connectionManager.createConnectionDetails( scheme ) ).build();
  }

  @GET
  @Path( "/connection" )
  @Consumes( { APPLICATION_JSON } )
  public Response getConnection( @QueryParam( "name" ) String name ) {
    return Response.ok( connectionManager.getConnectionDetails( name ) ).build();
  }

  @GET
  @Path( "/connection/exists" )
  @Consumes( { APPLICATION_JSON } )
  public Response getConnectionExists( @QueryParam( "name" ) String name ) {
    return Response.ok( String.valueOf( connectionManager.exists( name ) ) ).build();
  }

  @PUT
  @Path( "/connection" )
  @Consumes( { APPLICATION_JSON } )
  public Response createConnection( ConnectionDetails connectionDetails, @QueryParam( "name" ) String name ) {
    boolean saved = connectionManager.save( connectionDetails );
    if ( saved ) {
      connectionManager.delete( name );
      getSpoon().getShell().getDisplay().asyncExec( () -> getSpoon().refreshTree(
        ConnectionFolderProvider.STRING_VFS_CONNECTIONS ) );
      EngineMetaInterface engineMetaInterface = getSpoon().getActiveMeta();
      if ( engineMetaInterface instanceof AbstractMeta ) {
        ( (AbstractMeta) engineMetaInterface ).setChanged();
      }
      return Response.ok().build();
    } else {
      return Response.serverError().build();
    }
  }

  @POST
  @Path( "/test" )
  @Consumes( { APPLICATION_JSON } )
  public Response testConnection( ConnectionDetails connectionDetails ) {
    boolean valid = connectionManager.test( connectionDetails );
    if ( valid ) {
      return Response.ok().build();
    } else {
      return Response.status( Response.Status.BAD_REQUEST ).build();
    }
  }

  @GET
  @Path( "/help" )
  public Response help() {
    spoonSupplier.get().getShell().getDisplay().asyncExec( () -><commit_msg>[PDI-18683] VFS connection is not created if edited while being created<commit_after>/*! ******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2020 by <NAME> : http://www.pentaho.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 org.pentaho.di.connections.ui.endpoints;

import org.pentaho.di.base.AbstractMeta;
import org.pentaho.di.connections.ConnectionDetails;
import org.pentaho.di.connections.ConnectionManager;
import org.pentaho.di.connections.ui.dialog.ConnectionDialog;
import org.pentaho.di.connections.ui.tree.ConnectionFolderProvider;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.EngineMetaInterface;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.osgi.metastore.locator.api.MetastoreLocator;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.util.function.Supplier;

import org.pentaho.di.ui.util.HelpUtils;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

public class ConnectionEndpoints {

  private static Class<?> PKG = ConnectionDialog.class;
  private Supplier<Spoon> spoonSupplier = Spoon::getInstance;

  private ConnectionManager connectionManager;

  public static final String HELP_URL =
    Const.getDocUrl( BaseMessages.getString( PKG, "ConnectionDialog.help.dialog.Help" ) );

  public ConnectionEndpoints( MetastoreLocator metastoreLocator ) {
    this.connectionManager = ConnectionManager.getInstance();
    this.connectionManager.setMetastoreSupplier( metastoreLocator::getMetastore );
  }

  @GET
  @Path( "/types" )
  @Produces( { APPLICATION_JSON } )
  public Response getTypes() {
    return Response.ok( connectionManager.getItems() ).build();
  }

  @GET
  @Path( "/connection/{scheme}" )
  @Produces( { APPLICATION_JSON } )
  public Response getFields( @PathParam( "scheme" ) String scheme ) {
    return Response.ok( connectionManager.createConnectionDetails( scheme ) ).build();
  }

  @GET
  @Path( "/connection" )
  @Consumes( { APPLICATION_JSON } )
  public Response getConnection( @QueryParam( "name" ) String name ) {
    return Response.ok( connectionManager.getConnectionDetails( name ) ).build();
  }

  @GET
  @Path( "/connection/exists" )
  @Consumes( { APPLICATION_JSON } )
  public Response getConnectionExists( @QueryParam( "name" ) String name ) {
    return Response.ok( String.valueOf( connectionManager.exists( name ) ) ).build();
  }

  @PUT
  @Path( "/connection" )
  @Consumes( { APPLICATION_JSON } )
  public Response createConnection( ConnectionDetails connectionDetails, @QueryParam( "name" ) String name ) {
    boolean saved = connectionManager.save( connectionDetails );
    if ( saved ) {
      if ( !connectionDetails.getName().equals( name ) ) {
        connectionManager.delete( name );
      }
      getSpoon().getShell().getDisplay().asyncExec( () -> getSpoon().refreshTree(
        ConnectionFolderProvider.STRING_VFS_CONNECTIONS ) );
      EngineMetaInterface engineMetaInterface = getSpoon().getActiveMeta();
      if ( engineMetaInterface instanceof AbstractMeta ) {
        ( (AbstractMeta) engineMetaInterface ).setChanged();
      }
      return Response.ok().build();
    } else {
      return Response.serverError().build();
    }
  }

  @POST
  @Path( "/test" )
  @Consumes( { APPLICATION_JSON } )
  public Response testConnection( ConnectionDetails connectionDetails ) {
    boolean valid = connectionManager.test( connectionDetails );
    if ( valid ) {
      return Response.ok().build();
    } else {
      return Response.status( Response.Status.BAD_REQUEST ).build();
    }
  }

  @GET
  @Path( "/help" )
  public Response help() {
    spoonSupplier.get().getShell().getDisplay().asyncExec( () ->
{'fraction_non_alphanumeric': 0.16333660880008735, 'fraction_numerical': 0.0027295556283437055, 'mean_word_length': 4.375586854460094, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1095146', 'n_tokens_mistral': 2626, 'n_tokens_neox': 2543, 'n_words': 615}