Search in sources :

Example 86 with Connection

use of org.openmuc.j60870.Connection in project open-smart-grid-platform by OSGP.

the class Iec60870ConnectionRegistryTest method getAllConnectionsShouldReturnAllConnectionsPresentInRegistry.

@Test
public void getAllConnectionsShouldReturnAllConnectionsPresentInRegistry() {
    // Arrange
    final Connection connection1 = mock(Connection.class);
    final Connection connection2 = mock(Connection.class);
    this.iec60870ConnectionRegistry.registerConnection(connection1);
    this.iec60870ConnectionRegistry.registerConnection(connection2);
    final Set<Connection> expected = new HashSet<>(Arrays.asList(connection1, connection2));
    // Act
    final Set<Connection> actual = this.iec60870ConnectionRegistry.getAllConnections();
    // Assert
    assertThat(actual).isEqualTo(expected);
}
Also used : Connection(org.openmuc.j60870.Connection) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 87 with Connection

use of org.openmuc.j60870.Connection in project pinpoint by pinpoint-apm.

the class MethodInvocationHandlerInterceptor method createTrace.

/**
 * Creates the trace.
 *
 * @param target the target
 * @param args the args
 * @return the trace
 */
private Trace createTrace(final Object target, final Object[] args) {
    final Trace trace = traceContext.newTraceObject();
    final Connection connection = RemotingContext.getConnection();
    final String remoteAddress = JbossUtility.fetchRemoteAddress(connection);
    if (trace.canSampled()) {
        final SpanRecorder recorder = trace.getSpanRecorder();
        final String methodName = getMethodName(args);
        recordRootSpan(recorder, methodName, remoteAddress);
        if (isDebug) {
            logger.debug("Trace sampling is true, Recording trace. methodInvoked:{}, remoteAddress:{}", methodName, remoteAddress);
        }
    } else {
        if (isDebug) {
            final String methodName = getMethodName(args);
            logger.debug("Trace sampling is false, Skip recording trace. methodInvoked:{}, remoteAddress:{}", methodName, remoteAddress);
        }
    }
    return trace;
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) SpanRecorder(com.navercorp.pinpoint.bootstrap.context.SpanRecorder) Connection(org.jboss.remoting3.Connection)

Example 88 with Connection

use of org.openmuc.j60870.Connection in project warn-report by saaavsaaa.

the class LinuxExecUtil method connect.

public Connection connect(String host, String user, String password, String keyPath) throws IOException {
    conn = new Connection(host);
    conn.connect();
    File KeyFile = new File(keyPath);
    boolean isAuthenticated = conn.authenticateWithPublicKey(user, KeyFile, password);
    if (isAuthenticated == false) {
        throw new IOException("Authentication failed.");
    }
    return conn;
}
Also used : Connection(ch.ethz.ssh2.Connection)

Example 89 with Connection

use of org.openmuc.j60870.Connection in project warn-report by saaavsaaa.

the class LinuxExecUtil method connect.

public Connection connect(final String host, final int port, final String user, final String password) throws IOException {
    conn = new Connection(host, port);
    conn.connect();
    boolean isAuthenticated = conn.authenticateWithPassword(user, password);
    if (isAuthenticated == false) {
        throw new IOException("Authentication failed.");
    }
    return conn;
}
Also used : Connection(ch.ethz.ssh2.Connection)

Example 90 with Connection

use of org.openmuc.j60870.Connection in project pentaho-kettle by pentaho.

the class JobEntrySSH2PUT method execute.

@Override
public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);
    try {
        // Get real variable value
        String realServerName = environmentSubstitute(serverName);
        int realServerPort = Const.toInt(environmentSubstitute(serverPort), 22);
        String realUserName = environmentSubstitute(userName);
        String realServerPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password));
        // Proxy Host
        String realProxyHost = environmentSubstitute(httpproxyhost);
        int realProxyPort = Const.toInt(environmentSubstitute(httpproxyport), 22);
        String realproxyUserName = environmentSubstitute(httpproxyusername);
        String realProxyPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(httpProxyPassword));
        // Key file
        String realKeyFilename = environmentSubstitute(keyFilename);
        String relKeyFilepass = environmentSubstitute(keyFilePass);
        // Source files
        String realLocalDirectory = environmentSubstitute(localDirectory);
        String realwildcard = environmentSubstitute(wildcard);
        // Remote destination
        String realftpDirectory = environmentSubstitute(ftpDirectory);
        // Destination folder (Move to)
        String realDestinationFolder = environmentSubstitute(destinationfolder);
        try {
            // Remote source
            realftpDirectory = FTPUtils.normalizePath(realftpDirectory);
            // Destination folder (Move to)
            realDestinationFolder = FTPUtils.normalizePath(realDestinationFolder);
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.CanNotNormalizePath", e.getMessage()));
            result.setNrErrors(1);
            return result;
        }
        // Check for mandatory fields
        boolean mandatoryok = true;
        if (Utils.isEmpty(realServerName)) {
            mandatoryok = false;
            logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.ServernameMissing"));
        }
        if (usehttpproxy) {
            if (Utils.isEmpty(realProxyHost)) {
                mandatoryok = false;
                logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.HttpProxyhostMissing"));
            }
        }
        if (publicpublickey) {
            if (Utils.isEmpty(realKeyFilename)) {
                mandatoryok = false;
                logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.KeyFileMissing"));
            } else {
                // Let's check if folder exists...
                if (!KettleVFS.fileExists(realKeyFilename, this)) {
                    mandatoryok = false;
                    logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.KeyFileNotExist"));
                }
            }
        }
        if (Utils.isEmpty(realLocalDirectory)) {
            mandatoryok = false;
            logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.LocalFolderMissing"));
        }
        if (afterFtpPut.equals("move_file")) {
            if (Utils.isEmpty(realDestinationFolder)) {
                mandatoryok = false;
                logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.DestinatFolderMissing"));
            } else {
                FileObject folder = null;
                try {
                    folder = KettleVFS.getFileObject(realDestinationFolder, this);
                    // Let's check if folder exists...
                    if (!folder.exists()) {
                        // Do we need to create it?
                        if (createDestinationFolder) {
                            folder.createFolder();
                        } else {
                            logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.DestinatFolderNotExist", realDestinationFolder));
                        }
                    }
                } catch (Exception e) {
                    throw new KettleException(e);
                } finally {
                    if (folder != null) {
                        try {
                            folder.close();
                            folder = null;
                        } catch (Exception e) {
                        /* Ignore */
                        }
                    }
                }
            }
        }
        if (mandatoryok) {
            Connection conn = null;
            SFTPv3Client client = null;
            boolean good = true;
            int nbfilestoput = 0;
            int nbput = 0;
            int nbrerror = 0;
            try {
                // Create a connection instance
                conn = getConnection(realServerName, realServerPort, realProxyHost, realProxyPort, realproxyUserName, realProxyPassword);
                if (timeout > 0) {
                    // Cache Host Key
                    if (cachehostkey) {
                        conn.connect(new SimpleVerifier(database), 0, timeout * 1000);
                    } else {
                        conn.connect(null, 0, timeout * 1000);
                    }
                } else {
                    // Cache Host Key
                    if (cachehostkey) {
                        conn.connect(new SimpleVerifier(database));
                    } else {
                        conn.connect();
                    }
                }
                // Authenticate
                boolean isAuthenticated = false;
                if (publicpublickey) {
                    String keyContent = KettleVFS.getTextFileContent(realKeyFilename, this, Const.XML_ENCODING);
                    isAuthenticated = conn.authenticateWithPublicKey(realUserName, keyContent.toCharArray(), relKeyFilepass);
                } else {
                    isAuthenticated = conn.authenticateWithPassword(realUserName, realServerPassword);
                }
                // LET'S CHECK AUTHENTICATION ...
                if (isAuthenticated == false) {
                    logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.AuthenticationFailed"));
                } else {
                    if (log.isBasic()) {
                        logBasic(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Connected", serverName, userName));
                    }
                    client = new SFTPv3Client(conn);
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.ProtocolVersion", "" + client.getProtocolVersion()));
                    }
                    // Check if remote directory exists
                    if (!Utils.isEmpty(realftpDirectory)) {
                        if (!sshDirectoryExists(client, realftpDirectory)) {
                            good = false;
                            if (createRemoteFolder) {
                                good = CreateRemoteFolder(client, realftpDirectory);
                                if (good) {
                                    logBasic(BaseMessages.getString(PKG, "JobSSH2PUT.Log.RemoteDirectoryCreated"));
                                }
                            } else {
                                logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.RemoteDirectoryNotExist", realftpDirectory));
                            }
                        } else if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.RemoteDirectoryExist", realftpDirectory));
                        }
                    }
                    if (good) {
                        // Get files list from local folder (source)
                        List<FileObject> myFileList = getFiles(realLocalDirectory);
                        // Prepare Pattern for wildcard
                        Pattern pattern = null;
                        if (!Utils.isEmpty(realwildcard)) {
                            pattern = Pattern.compile(realwildcard);
                        }
                        // Get the files in the list
                        for (int i = 0; i < myFileList.size() && !parentJob.isStopped(); i++) {
                            FileObject myFile = myFileList.get(i);
                            String localFilename = myFile.toString();
                            String remoteFilename = myFile.getName().getBaseName();
                            boolean getIt = true;
                            // First see if the file matches the regular expression!
                            if (pattern != null) {
                                Matcher matcher = pattern.matcher(remoteFilename);
                                getIt = matcher.matches();
                            }
                            // do we have a target directory?
                            if (!Utils.isEmpty(realftpDirectory)) {
                                remoteFilename = realftpDirectory + FTPUtils.FILE_SEPARATOR + remoteFilename;
                            }
                            if (onlyGettingNewFiles) {
                                // We get only new files
                                // ie not exist on the remote server
                                getIt = !sshFileExists(client, remoteFilename);
                            }
                            if (getIt) {
                                nbfilestoput++;
                                boolean putok = putFile(myFile, remoteFilename, client);
                                if (!putok) {
                                    nbrerror++;
                                    logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Error.CanNotPutFile", localFilename));
                                } else {
                                    nbput++;
                                }
                                if (putok && !afterFtpPut.equals("do_nothing")) {
                                    deleteOrMoveFiles(myFile, realDestinationFolder);
                                }
                            }
                        }
                        /**
                         ****************************** RESULT *******************
                         */
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.JobEntryEnd1"));
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.TotalFiles", "" + nbfilestoput));
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.TotalFilesPut", "" + nbput));
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.TotalFilesError", "" + nbrerror));
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.JobEntryEnd2"));
                        }
                        if (nbrerror == 0) {
                            result.setResult(true);
                        /**
                         ****************************** RESULT *******************
                         */
                        }
                    }
                }
            } catch (Exception e) {
                result.setNrErrors(nbrerror);
                logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Error.ErrorFTP", e.getMessage()));
            } finally {
                if (conn != null) {
                    conn.close();
                }
                if (client != null) {
                    client.close();
                }
            }
        }
    } catch (Exception e) {
        result.setResult(false);
        result.setNrErrors(1L);
        logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Error.UnexpectedError"), e);
    }
    return result;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Connection(com.trilead.ssh2.Connection) SFTPv3Client(com.trilead.ssh2.SFTPv3Client) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) IOException(java.io.IOException) Result(org.pentaho.di.core.Result) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

Connection (org.ovirt.engine.sdk4.Connection)64 Connection (com.trilead.ssh2.Connection)55 IOException (java.io.IOException)43 Session (com.trilead.ssh2.Session)32 VmsService (org.ovirt.engine.sdk4.services.VmsService)30 Vm (org.ovirt.engine.sdk4.types.Vm)30 InputStream (java.io.InputStream)25 VmService (org.ovirt.engine.sdk4.services.VmService)18 Connection (okhttp3.Connection)15 Connection (ch.ethz.ssh2.Connection)13 Request (okhttp3.Request)13 SystemService (org.ovirt.engine.sdk4.services.SystemService)13 Response (okhttp3.Response)12 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)12 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)12 MediaType (okhttp3.MediaType)11 ResponseBody (okhttp3.ResponseBody)11 RequestBody (okhttp3.RequestBody)10 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 Charset (java.nio.charset.Charset)9