We have a syslog client in our application and it is implemented using Cloudbees- TcpSyslogMessageSender. We are creating the context and connHow to reuse the session and port number in TCP-TLS communication using Cloudbees- TcpSyslogMessageSender.
Will it be handled by Cloudbees or we have to configure any settings explicitly. Here is our code.
With this code, it is using a new port everytime.
TcpSyslogMessageSender messageSendertcp = new TcpSyslogMessageSender();
messageSendertcp.setSyslogServerHostname("localhost");
messageSendertcp.setSyslogServerPort("6514");
messageSendertcp.setMessageFormat(MessageFormat.RFC_5425);
messageSendertcp.setDefaultMessageHostname(this.getHostName());
messageSendertcp.setDefaultAppName("test");
messageSendertcp.setDefaultFacility("local0"));
messageSendertcp.setDefaultSeverity("notice");
logger.info("entering getsslcontext");
SSLContext context = getSSLContext(); //SSLContext is formed using client keystore and trustores
logger.info("context object");
messageSendertcp.setSSLContext(context);
messageSendertcp.setSsl(true);
}
try {
logger.info("sending message tcp");
messageSendertcp.sendMessage(syslogMessage);
} catch (IOException e) {
return false;
} finally {
try {
if (messageSendertcp != null)
messageSendertcp.close();
} catch (IOException e) {
return false;
}
}
Here Every Time your code is closing TCP object and and whenever new message comes it is again creating and using new socket. So in order to send the message on same port do not close the socket(TCP object) and use the Server details cache. For example this cache implemented using map that contains Server Details as the map and TCP object as key. And do not close the TCP object.
I am finding difficulty while setting up Riak Kv Cluster setup using java client.
Can anybody tell how we do that??
Achually i tried with below code to use riak security in cluster but i am getting error of SSLEngine Problem, can anybody tell how we resolve this issues.
Below is java Code for Refference
InputStream inputStream = null;
KeyStore ks = null;
try {
// inputStream = new FileInputStream("/ssl_dir/cacertfile.pem");
inputStream = new FileInputStream("/home/shahzad/Desktop/amit/rootCA.pem");
// Generate an X509Certificate from the InputStream
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
X509Certificate caCert = (X509Certificate) certFactory.generateCertificate(inputStream);
inputStream.close();
// Generate a KeyStore object
ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, "password".toCharArray());
ks.setCertificateEntry("cacert", caCert);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (RiakConfig.class) {
List<RiakNode> riakNodeList = new ArrayList<RiakNode>();
for (final String riakServer : riakServerArray) {
RiakNode node = new RiakNode.Builder()
.withMinConnections(10)
.withMinConnections(50)
.withRemoteAddress(riakServer.split(":")[0])
.withRemotePort(
Integer.parseInt(riakServer.split(":")[1]))
.withAuth("shahzad", "shahzad", ks)
.build();
riakNodeList.add(node);
}
// This cluster object takes our one node as an argument
cluster = new RiakCluster.Builder(riakNodeList).build();
// The cluster must be started to work, otherwise you will see
// errors
cluster.start();
}
Tell one thing..... how we access security enable Riak KV remotely.
Please check, if you enable security at Riak KV cluster and added client source
riak-admin security add-source all <<127.0.0.1/32>> trust
The IP address belongs to the client machine from where the connection will originate
I have MSSQL 2008 installed on my local PC, and my Java application needs to connect to a MSSQL database. I am a new to MSSQL and I would like get some help on creating user login for my Java application and getting connection via JDBC. So far I tried to create a user login for my app and used following connection string, but I doesn't work at all. Any help and hint will be appreciated.
jdbc:jtds:sqlserver://127.0.0.1:1433/dotcms
username="shuxer" password="itarator"
There are mainly two ways to use JDBC - using Windows authentication and SQL authentication. SQL authentication is probably the easiest. What you can do is something like:
String userName = "username";
String password = "password";
String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url, userName, password);
after adding sqljdbc4.jar to the build path.
For Window authentication you can do something like:
String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);
and then add the path to sqljdbc_auth.dll as a VM argument (still need sqljdbc4.jar in the build path).
Please take a look here for a short step-by-step guide showing how to connect to SQL Server from Java using jTDS and JDBC should you need more details. Hope it helps!
You can use this :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectMSSQLServer
{
public void dbConnect(String db_connect_string,
String db_userid,
String db_password)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(db_connect_string,
db_userid, db_password);
System.out.println("connected");
Statement statement = conn.createStatement();
String queryString = "select * from sysobjects where type='u'";
ResultSet rs = statement.executeQuery(queryString);
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
ConnectMSSQLServer connServer = new ConnectMSSQLServer();
connServer.dbConnect("jdbc:sqlserver://<hostname>", "<user>",
"<password>");
}
}
I am also using mssql server 2008 and jtds.In my case I am using the following connect string and it works.
Class.forName( "net.sourceforge.jtds.jdbc.Driver" );
Connection con = DriverManager.getConnection( "jdbc:jtds:sqlserver://<your server ip
address>:1433/zacmpf", userName, password );
Statement stmt = con.createStatement();
If your having trouble connecting, most likely the problem is that you haven't yet enabled the TCP/IP listener on port 1433. A quick "netstat -an" command will tell you if its listening. By default, SQL server doesn't enable this after installation.
Also, you need to set a password on the "sa" account and also ENABLE the "sa" account (if you plan to use that account to connect with).
Obviously, this also means you need to enable "mixed mode authentication" on your MSSQL node.
Try to use like this: jdbc:jtds:sqlserver://127.0.0.1/dotcms; instance=instanceName
I don't know which version of mssql you are using, if it is express edition, default instance is sqlexpress
Do not forget check if SQL Server Browser service is running.
You can try configure SQL server:
Step 1: Open SQL server 20xx Configuration Manager
Step 2: Click Protocols for SQL.. in SQL server configuration. Then, right click TCP/IP, choose Properties
Step 3: Click tab IP Address, Edit All TCP. Port is 1433
NOTE: ALL TCP port is 1433
Finally, restart the server.
Simple Java Program which connects to the SQL Server.
NOTE: You need to add sqljdbc.jar into the build path
// localhost : local computer acts as a server
// 1433 : SQL default port number
// username : sa
// password: use password, which is used at the time of installing SQL server management studio, In my case, it is 'root'
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conn {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Connection conn=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=company", "sa", "root");
if(conn!=null)
System.out.println("Database Successfully connected");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Try this.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SQLUtil {
public void dbConnect(String db_connect_string,String db_userid,
String db_password) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(db_connect_string,
db_userid, db_password);
System.out.println("connected");
Statement statement = conn.createStatement();
String queryString = "select * from cpl";
ResultSet rs = statement.executeQuery(queryString);
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
} }
public static void main(String[] args) {
SQLUtil connServer = new SQLUtil();
connServer.dbConnect("jdbc:sqlserver://192.168.10.97:1433;databaseName=myDB",
"sa",
"0123");
}
}
Try this
Class.forName( "net.sourceforge.jtds.jdbc.Driver" );
String url ="Jdbc:jtds:sqlsever://ip/instanceName;instance=instanceName;databseName=dbName;user=yourUser;password=yourpass;";
I need to transfer file (xml) to AS2 server. I am not so good at this communication channel, but I need to do it programmatically. For example sending to SFTP I am using this code:
import com.jcraft.jsch.*;
.......
public void uploadViaSFTP (String fileToUpload, String sftp_host, String sftp_user, String sftp_psw)
{
int SFTPPORT = 22;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try{
JSch jsch = new JSch();
session = jsch.getSession(sftp_user,sftp_host,SFTPPORT);
session.setPassword(sftp_psw);
java.util.Properties config = new java.util.Properties();
//this line should be used only for testing, not for production
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd("/");
File f = new File(fileToUpload);
channelSftp.put(new FileInputStream(f), f.getName());
}catch(Exception ex){
ex.printStackTrace();
}
}
But now I need to do same for AS2. What library I could use (openAS2)? Is there a simple method to transfer like it does for SFTP?
You should be able to use standard HTTPS components and S/MIME attachment, since AS2 is a security layer and usage specification on top of HTTP or HTTPS.
I'd start with (https://www.mkyong.com/java/java-https-client-httpsurlconnection-example/), the AS2 specification (http://www.ietf.org/rfc/rfc4130.txt) , and this example from github: (https://github.com/protocol7/smime-java-example)
I am new to the oracle database.
I installed OracleXE 11g on my machine. I created a separate test database[SID:testDB] from oracle default database(XE) referring this video. i created below things:
created windows service- OracleServicetestDB using below cmd:
oradim -new -sid testDB -startmode auto -pfile initTestDB.ora
created database
executed sql scripts
SQL> #?\rdbms\admin\catalog.sql
SQL> #?\rdbms\admin\catproc.sql
created user
After creating user/schema for this new database i am able to make connect it from sql developer and java/jdbc programme from other machine on the network.
I am surprised that i have not created any TNS listener or TNS entry for this database in tnsnames.ora but still i am able to connect with this database locally and remotely.
i am expecting answers of below questions:
how my testDB is connected without tns entry?
if testDB is depend on XE service/listener, how i configure OracleServicetestDB to seperate from XE services ?
List item
java/jdbc code:
import java.sql.Connection;
import java.sql.DriverManager;
public class FirstExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
static final String DB_URL = "jdbc:oracle:thin:#localhost:1521:testDB";
// Database credentials
static final String USER = "testDBUser";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
try {
// STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
// STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected.");
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
tnsnames.ora is used for the openers (apps/tools) depending on driver used and somtimes connection settings; the Java driver can handle most cases without tnsnames, see http://docs.oracle.com/cd/E11882_01/java.112/e16548/urls.htm#JJDBC08200 and note that "TNSnames alias" is only one of several options.
Listener is configured in ORAHOME/network/admin/listener.ora and usually the defaults don't require any change. On Windows listener runs as a service (a Windows service, not to be confused with an Oracle service-name!) and starts automatically. On both Unix and Windows you have one listener even if you have multiple database instances/SIDs.