NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/*
* Copyright 2006-2021 HighRadius Corporation
*/
package com.highradius.g4.service.catalog.service.impl;

import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.highradius.g4.catalog.dto.AuthKeysDTO;
import com.highradius.g4.catalog.dto.ResponseDTO;
import com.highradius.g4.catalog.entity.dc.DcConnection;
import com.highradius.g4.catalog.entity.map.MapDataSourceProvider;
import com.highradius.g4.core.common.G4Log;
import com.highradius.g4.core.common.G4LogManager;
import com.highradius.g4.service.annotation.RestResource;
import com.highradius.g4.service.catalog.manager.AuthenticationKeysManager;
import com.highradius.g4.service.catalog.manager.ConnectionManager;
import com.highradius.g4.service.catalog.manager.SaveConnectionManager;
import com.highradius.g4.service.catalog.service.SFTPConnectionService;
import com.highradius.g4.service.catalog.service.utils.Constants;
import com.highradius.g4.service.catalog.service.utils.FSClient;
import com.highradius.g4.service.catalog.service.utils.SFTPUtility;
import com.jcraft.jsch.Session;

/**
* The Class SFTPConnectionServiceImpl.
*/
@RestResource
public class SFTPConnectionServiceImpl implements SFTPConnectionService {

/** The Constant LOGGER. */
private static final G4Log LOGGER = G4LogManager.getLog(SFTPConnectionServiceImpl.class);
/** The Constant accountId. */
private static final Long accountId = Long.valueOf(-1);

/** The Constant SFTP. */
private static final String SFTP_CLIENT = "SFTP Client";

/** The Constant SFTP_Highradius. */
private static final String SFTP_HIGHRADIUS = "SFTP Highradius";

/** The authentication keys manager. */
@Autowired
private AuthenticationKeysManager authenticationKeysManager;

/** The sftp utility. */
@Autowired
private SFTPUtility sftpUtility;

/** The save connection manager. */
@Autowired
private SaveConnectionManager saveConnectionManager;

/** The connection manager. */
@Autowired
private ConnectionManager connectionManager;

/** The fs client. */
FSClient fsClient = new FSClient();

/**
* Upload.
*
* @param jsonInputSFTPConnection the json input SFTP connection
* @return the SFTP connection DTO
*/
@Override
public ResponseDTO upload(String jsonInputSFTPConnection) {
ResponseDTO responseDTO = new ResponseDTO();
DcConnection dcConnection = new DcConnection();
responseDTO.setSuccess(Boolean.FALSE);
String messageFormatArgument[] = new String[100];
String connectionName = new String();
String authType = new String();
String username = null;
String password = null;
Long port = null;
String host = null;
byte[] sftpKey = null;
Session session = null;
// Saving the connection based on Dc AuthKeys
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonFactory jsonFactory = objectMapper.getFactory();
JsonParser jsonParser = jsonFactory.createParser(jsonInputSFTPConnection);
JsonNode jsonNode = objectMapper.readTree(jsonParser);
if (jsonNode.get("authType") != null) {
authType = (jsonNode.get("authType") != null)
? jsonNode.get("authType").toString().replaceAll(""", "").trim()
: "";
connectionName = (jsonNode.get("connectionname") != null)
? jsonNode.get("connectionname").toString().replaceAll(""", "").trim()
: "";
host = (jsonNode.get("host") != null) ? jsonNode.get("host").toString().replaceAll(""", "").trim()
: "";
if (authType.equalsIgnoreCase("Username and Password")) {
username = (jsonNode.get("username") != null)
? jsonNode.get("username").toString().replaceAll(""", "").trim()
: "";
password = (jsonNode.get("password") != null)
? jsonNode.get("password").toString().replaceAll(""", "").trim()
: "";
port = (jsonNode.get("port") != null)
? Long.parseLong(jsonNode.get("port").toString().replaceAll(""", "").trim())
: null;
} else {
ArrayNode arrayNode = (jsonNode.get("sftpKey") != null) ? (ArrayNode) jsonNode.get("sftpKey")
: null;
sftpKey = new byte[arrayNode.size()];
for (JsonNode js : arrayNode) {
sftpKey = ArrayUtils.add(sftpKey, Byte.parseByte(js.toString()));
}
}
}
Boolean isNameNotUnique = saveConnectionManager.checkExistingName(connectionName, accountId);
if (isNameNotUnique) {
responseDTO.setStatus(Constants.UNIQUE_NAME_MSG);
responseDTO.setSuccess(Boolean.FALSE);
return responseDTO;
}
List<AuthKeysDTO> authKeysList = authenticationKeysManager.getAuthenticationKeys(authType);
MapDataSourceProvider mapDataSourceProvider = saveConnectionManager.getDataSourceProvider(accountId,
SFTP_CLIENT);
String dataSourceName = mapDataSourceProvider.getDcDataSource().getName();

// Creating SFTP Connection
dcConnection = sftpUtility.saveDcConnection(connectionName, accountId, mapDataSourceProvider);

messageFormatArgument[0] = connectionName;
messageFormatArgument[1] = dataSourceName.toLowerCase();

// Mapping the connection created with the auth values using auth keys
messageFormatArgument = sftpUtility.insertIntoMapAuthValuesTable(responseDTO, authKeysList, jsonNode,
dcConnection, messageFormatArgument);

// Check which method to call from FSClient and check if connection is mapped to
// auth values or not
if (username != null && messageFormatArgument != null) {
session = fsClient.getSftpSession(host, port.toString(), username, password);
} else if (messageFormatArgument != null) {
session = fsClient.getSftpSession(host, String.valueOf(port), sftpKey);
}
if (session != null) {
responseDTO.setStatus(Constants.CONNECTION_SUCCESS);
responseDTO.setSuccess(Boolean.TRUE);
} else {
responseDTO.setStatus(Constants.CONNECTION_FAILED);
// Rollback
responseDTO.setSuccess(!connectionManager.changeConnectionStatus(dcConnection.getPkDcConnectionId(),
Constants.CONNECTION_FAILED));
}
responseDTO.setPkId(dcConnection.getPkDcConnectionId());
} catch (Exception e) {
LOGGER.error("Unable to connect,exception occured: ", e);
responseDTO.setStatus(Constants.CONNECTION_FAILED);
// Rollback
responseDTO.setSuccess(!connectionManager.changeConnectionStatus(dcConnection.getPkDcConnectionId(),
Constants.CONNECTION_FAILED));
}
return responseDTO;
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.