use of org.openmuc.j60870.Connection in project WeexErosFramework by bmfe.
the class WeexOkhttp3Interceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
String requestId = String.valueOf(mNextRequestId.getAndIncrement());
Request request = chain.request();
mEventReporter = NetworkEventReporterManager.get();
RequestBodyHelper requestBodyHelper = null;
if (mEventReporter != null) {
requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId);
OkHttpInspectorRequest inspectorRequest = new OkHttpInspectorRequest(requestId, request, requestBodyHelper);
mEventReporter.requestWillBeSent(inspectorRequest);
}
Response response;
try {
response = chain.proceed(request);
} catch (IOException e) {
if (mEventReporter != null) {
mEventReporter.httpExchangeFailed(requestId, e.toString());
}
throw e;
}
if (mEventReporter != null) {
if (requestBodyHelper.hasBody()) {
requestBodyHelper.reportDataSent();
}
Connection connection = chain.connection();
mEventReporter.responseHeadersReceived(new OkHttpInspectorResponse(requestId, request, response));
ResponseBody body = response.body();
MediaType contentType = null;
InputStream responseStream = null;
if (body != null) {
contentType = body.contentType();
responseStream = body.byteStream();
}
responseStream = mEventReporter.interpretResponseStream(requestId, contentType != null ? contentType.toString() : null, response.header("Content-Encoding"), responseStream, new DefaultResponseHandler(mEventReporter, requestId));
if (responseStream != null) {
response = response.newBuilder().body(new ForwardingResponseBody(body, responseStream)).build();
}
}
return response;
}
use of org.openmuc.j60870.Connection in project pentaho-kettle by pentaho.
the class SSHData method OpenConnection.
public static Connection OpenConnection(String serveur, int port, String username, String password, boolean useKey, String keyFilename, String passPhrase, int timeOut, VariableSpace space, String proxyhost, int proxyport, String proxyusername, String proxypassword) throws KettleException {
Connection conn = null;
char[] content = null;
boolean isAuthenticated = false;
try {
// perform some checks
if (useKey) {
if (Utils.isEmpty(keyFilename)) {
throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyFileMissing"));
}
FileObject keyFileObject = KettleVFS.getFileObject(keyFilename);
if (!keyFileObject.exists()) {
throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyNotExist", keyFilename));
}
FileContent keyFileContent = keyFileObject.getContent();
CharArrayWriter charArrayWriter = new CharArrayWriter((int) keyFileContent.getSize());
try (InputStream in = keyFileContent.getInputStream()) {
IOUtils.copy(in, charArrayWriter);
}
content = charArrayWriter.toCharArray();
}
// Create a new connection
conn = createConnection(serveur, port);
/* We want to connect through a HTTP proxy */
if (!Utils.isEmpty(proxyhost)) {
// if the proxy requires basic authentication:
if (!Utils.isEmpty(proxyusername)) {
conn.setProxyData(new HTTPProxyData(proxyhost, proxyport, proxyusername, proxypassword));
} else {
conn.setProxyData(new HTTPProxyData(proxyhost, proxyport));
}
}
// and connect
if (timeOut == 0) {
conn.connect();
} else {
conn.connect(null, 0, timeOut * 1000);
}
// authenticate
if (useKey) {
isAuthenticated = conn.authenticateWithPublicKey(username, content, space.environmentSubstitute(passPhrase));
} else {
isAuthenticated = conn.authenticateWithPassword(username, password);
}
if (isAuthenticated == false) {
throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.AuthenticationFailed", username));
}
} catch (Exception e) {
// do not forget to disconnect if connected
if (conn != null) {
conn.close();
}
throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.ErrorConnecting", serveur, username), e);
}
return conn;
}
use of org.openmuc.j60870.Connection in project pentaho-kettle by pentaho.
the class JobEntryFTPDeleteDialog method connectToSSH.
private boolean connectToSSH() {
boolean retval = false;
try {
if (conn == null) {
// Create a connection instance
conn = new Connection(jobMeta.environmentSubstitute(wServerName.getText()), Const.toInt(jobMeta.environmentSubstitute(wPort.getText()), 22));
/* We want to connect through a HTTP proxy */
if (wuseProxy.getSelection()) {
// if the proxy requires basic authentication:
if (!Utils.isEmpty(wProxyUsername.getText())) {
conn.setProxyData(new HTTPProxyData(jobMeta.environmentSubstitute(wProxyHost.getText()), Const.toInt(wProxyPort.getText(), 22), jobMeta.environmentSubstitute(wProxyUsername.getText()), Utils.resolvePassword(jobMeta, wProxyPassword.getText())));
} else {
conn.setProxyData(new HTTPProxyData(jobMeta.environmentSubstitute(wProxyHost.getText()), Const.toInt(wProxyPort.getText(), 22)));
}
}
conn.connect();
// Authenticate
if (wusePublicKey.getSelection()) {
retval = conn.authenticateWithPublicKey(jobMeta.environmentSubstitute(wUserName.getText()), new java.io.File(jobMeta.environmentSubstitute(wKeyFilename.getText())), jobMeta.environmentSubstitute(wkeyfilePass.getText()));
} else {
retval = conn.authenticateWithPassword(jobMeta.environmentSubstitute(wUserName.getText()), Utils.resolvePassword(jobMeta, wPassword.getText()));
}
}
retval = true;
} catch (Exception e) {
if (conn != null) {
try {
conn.close();
} catch (Exception ignored) {
// We've tried quitting the SSH Client exception
// nothing else to be done if the SSH Client was already disconnected
}
conn = null;
}
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(BaseMessages.getString(PKG, "JobFTPDelete.ErrorConnect.NOK", e.getMessage()) + Const.CR);
mb.setText(BaseMessages.getString(PKG, "JobFTPDelete.ErrorConnect.Title.Bad"));
mb.open();
}
return retval;
}
use of org.openmuc.j60870.Connection in project pentaho-kettle by pentaho.
the class SSHDialog method test.
private void test() {
boolean testOK = false;
String errMsg = null;
String servername = transMeta.environmentSubstitute(wServerName.getText());
int nrPort = Const.toInt(transMeta.environmentSubstitute(wPort.getText()), 22);
String username = transMeta.environmentSubstitute(wUserName.getText());
String password = Utils.resolvePassword(transMeta, wPassword.getText());
String keyFilename = transMeta.environmentSubstitute(wPrivateKey.getText());
String passphrase = transMeta.environmentSubstitute(wPassphrase.getText());
int timeOut = Const.toInt(transMeta.environmentSubstitute(wTimeOut.getText()), 0);
String proxyhost = transMeta.environmentSubstitute(wProxyHost.getText());
int proxyport = Const.toInt(transMeta.environmentSubstitute(wProxyPort.getText()), 0);
String proxyusername = transMeta.environmentSubstitute(wProxyUsername.getText());
String proxypassword = Utils.resolvePassword(transMeta, wProxyPassword.getText());
Connection conn = null;
try {
conn = SSHData.OpenConnection(servername, nrPort, username, password, wUseKey.getSelection(), keyFilename, passphrase, timeOut, transMeta, proxyhost, proxyport, proxyusername, proxypassword);
testOK = true;
} catch (Exception e) {
errMsg = e.getMessage();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
/* Ignore */
}
}
}
if (testOK) {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
mb.setMessage(BaseMessages.getString(PKG, "SSHDialog.Connected.OK", servername, username) + Const.CR);
mb.setText(BaseMessages.getString(PKG, "SSHDialog.Connected.Title.Ok"));
mb.open();
} else {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(BaseMessages.getString(PKG, "SSHDialog.Connected.NOK.ConnectionBad", servername, username) + Const.CR + errMsg + Const.CR);
mb.setText(BaseMessages.getString(PKG, "SSHDialog.Connected.Title.Bad"));
mb.open();
}
}
use of org.openmuc.j60870.Connection in project pentaho-kettle by pentaho.
the class JobEntrySSH2GET method execute.
@Override
public Result execute(Result previousResult, int nr) {
Result result = previousResult;
result.setResult(false);
if (log.isRowLevel()) {
logRowlevel(BaseMessages.getString(PKG, "JobSSH2GET.Log.GettingFieldsValue"));
}
// 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);
// target files
String realLocalDirectory = environmentSubstitute(localDirectory);
String realwildcard = environmentSubstitute(wildcard);
// Remote source
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, "JobSSH2GET.Log.CanNotNormalizePath", e.getMessage()));
result.setNrErrors(1);
return result;
}
// Check for mandatory fields
if (log.isRowLevel()) {
logRowlevel(BaseMessages.getString(PKG, "JobSSH2GET.Log.CheckingMandatoryFields"));
}
boolean mandatoryok = true;
if (Utils.isEmpty(realServerName)) {
mandatoryok = false;
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.ServernameMissing"));
}
if (usehttpproxy) {
if (Utils.isEmpty(realProxyHost)) {
mandatoryok = false;
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.HttpProxyhostMissing"));
}
}
if (publicpublickey) {
if (Utils.isEmpty(realKeyFilename)) {
mandatoryok = false;
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.KeyFileMissing"));
} else {
// Let's check if key file exists...
if (!new File(realKeyFilename).exists()) {
mandatoryok = false;
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.KeyFileNotExist"));
}
}
}
if (Utils.isEmpty(realLocalDirectory)) {
mandatoryok = false;
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.LocalFolderMissing"));
} else {
// Check if target folder exists...
if (!new File(realLocalDirectory).exists()) {
if (createtargetfolder) {
// Create Target folder
if (!CreateFolder(realLocalDirectory)) {
mandatoryok = false;
}
} else {
mandatoryok = false;
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.LocalFolderNotExists", realLocalDirectory));
}
} else {
if (!new File(realLocalDirectory).isDirectory()) {
mandatoryok = false;
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.LocalFolderNotFolder", realLocalDirectory));
}
}
}
if (afterFtpPut.equals("move_file")) {
if (Utils.isEmpty(realDestinationFolder)) {
mandatoryok = false;
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.DestinatFolderMissing"));
}
}
if (mandatoryok) {
Connection conn = null;
SFTPv3Client client = null;
boolean good = true;
try {
// Create a connection instance
conn = getConnection(realServerName, realServerPort, realProxyHost, realProxyPort, realproxyUserName, realProxyPassword);
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.ConnectionInstanceCreated"));
}
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) {
isAuthenticated = conn.authenticateWithPublicKey(realUserName, new File(realKeyFilename), relKeyFilepass);
} else {
isAuthenticated = conn.authenticateWithPassword(realUserName, realServerPassword);
}
// LET'S CHECK AUTHENTICATION ...
if (isAuthenticated == false) {
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.AuthenticationFailed"));
} else {
if (log.isBasic()) {
logBasic(BaseMessages.getString(PKG, "JobSSH2GET.Log.Connected", serverName, userName));
}
client = new SFTPv3Client(conn);
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.ProtocolVersion", "" + client.getProtocolVersion()));
}
// Check if ftp (source) directory exists
if (!Utils.isEmpty(realftpDirectory)) {
if (!sshDirectoryExists(client, realftpDirectory)) {
good = false;
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.RemoteDirectoryNotExist", realftpDirectory));
} else if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.RemoteDirectoryExist", realftpDirectory));
}
}
if (!Utils.isEmpty(realDestinationFolder)) {
// Check now destination folder
if (!sshDirectoryExists(client, realDestinationFolder)) {
if (createdestinationfolder) {
if (!CreateRemoteFolder(client, realDestinationFolder)) {
good = false;
}
} else {
good = false;
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.DestinatFolderNotExist", realDestinationFolder));
}
}
}
if (good) {
Pattern pattern = null;
if (!Utils.isEmpty(realwildcard)) {
pattern = Pattern.compile(realwildcard);
}
if (includeSubFolders) {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.RecursiveModeOn"));
}
copyRecursive(realftpDirectory, realLocalDirectory, client, pattern, parentJob);
} else {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.RecursiveModeOff"));
}
GetFiles(realftpDirectory, realLocalDirectory, client, pattern, parentJob);
}
/**
****************************** RESULT *******************
*/
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.JobEntryEnd1"));
logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.TotalFiles", "" + nbfilestoget));
logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.TotalFilesPut", "" + nbgot));
logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.TotalFilesError", "" + nbrerror));
logDetailed(BaseMessages.getString(PKG, "JobSSH2GET.Log.Result.JobEntryEnd2"));
}
if (nbrerror == 0) {
result.setResult(true);
/**
****************************** RESULT *******************
*/
}
}
}
} catch (Exception e) {
result.setNrErrors(nbrerror);
logError(BaseMessages.getString(PKG, "JobSSH2GET.Log.Error.ErrorFTP", e.getMessage()));
} finally {
if (conn != null) {
conn.close();
}
if (client != null) {
client.close();
}
}
}
return result;
}
Aggregations