How to use Http,Socksproxies in java? - java

my java app needs to connect to the internet , I am behind a proxy server. i use the fallow
functions to set proxy but it doesn't work?
System.getProperties().put("http.proxySet", "true");
System.getProperties().put("http.socksProxyHost", value);
System.getProperties().put("http.socksProxyPort", value);
thanks

Check out this question, especially the answer which uses Proxy class.

Check the system parameter names. There are two ways you can do this:
Set the system parameter from the command line itself
java -Dhttp.proxyHost=proxyhostURL
-Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=userName
-Dhttp.proxyPassword=password YourProgram
OR Set these parameter in the code as follows
System.getProperties().put("http.proxyHost", "proxyURL");
System.getProperties().put("http.proxyPort", "proxyPort");
System.getProperties().put("http.proxyUser", "userName");
System.getProperties().put("http.proxyPassword", "password");

Related

Wrong host sending SMTP

I'm trying to send a mail using javax.mail. This is my code:
Properties props = new Properties();
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.port", port);
props.setProperty("mail.user", user);
props.setProperty("mail.password", password);
Session session = Session.getDefaultInstance(props);
But I get this error:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.wrong.server.com, port: 25;
The funny thing is that "smtp.wrong.server.com" isn't the value that I'm passing as host.
It is like Session.getDefaultInstance(props) is returning an already created session with the wrong host name.
There isn't any other place inside my EAR where javax.mail is used (at least not in my code, maybe inside a third party dependecy?).
This behaviour only happens, of course, in PRO environment. The same EAR deployed in DEV and TEST env works fine.
Any help would be appreciated
The problem was with Session.getDefaultInstance. I should use Session.getInstance
From javadoc:
getDefaultInstance
(...)the default session is potentially available to all code executing in the same Java virtual machine(...)Subsequent calls return the Session object that was created by the first call, and ignore the passed Properties object. Use the getInstance method to get a new Session object every time the method is called.
It seams your are not using the correct key for your proerties. see Javadoc for javax.mail.Session
It is expected that the client supplies values for the properties
listed in Appendix A of the JavaMail spec (particularly
mail.store.protocol, mail.transport.protocol, mail.host, mail.user,
and mail.from) as the defaults are unlikely to work in all cases.

JavaMail IMAP not using specified properties

I'm seeing an issue where when connected to a mailbox using IMAP the infinite timeout default is causing an issue. I am having an issue getting Java Mail to recgonise IMAP properties. I verified IMAP did not seem to be using the properties by setting things like port number to the value 1, which should not work.
This is the code snippit:
Properties props = new Properties()
props.put("mail.imap.port", "1");
props.put("mail.imap.timeout", "1");
props.put("mail.imaps.connectiontimeout", "1");
Session session = Session.getInstance(props, null);
Store store = session.getStore("imaps");
store.connect(***,***,***);
If anyone knows where the problem is arising from that would great, all help is appreciated.
I believe you should be using props.setProperty(key, value) instead of using props.put(key, value). The documentation here: http://docs.oracle.com/javase/tutorial/essential/environment/properties.html warns you not to use hashTable methods
You're using the "imaps" protocol but setting properties for the "imap" protocol. Change your property names to "mail.imaps.*".

How to implement a proxy server?

I'm looking to have my application connect to the internet through a proxy server (in order to avoid captcha). The code I am currently using is this:
Properties props = System.getProperties();
props.put("http.proxyPort", proxyPort); //proxy port
props.put("http.proxyHost", proxyHost); //proxy host
props.put("http.proxySet", "true");
This code has been unsuccesful, however. Any suggestions?
You can try the following:
SocketAddress sa = new InetSocketAddress(proxy_host_name, proxy_port_address);
Proxy proxy = new Proxy(Proxy.Type.xxx, sa);
URLConnection con = new URL(url).openConnection(proxy);
You are probably using a kind of "User Friendly Website Proxy", like http://newipnow.com or www.proxyultra.com. But you need to use a real SOCKS proxy server.
A free server, that I found, working, in a list of public Proxies:
System.setProperty("http.proxyHost", "187.115.172.82");
System.setProperty("http.proxyPort", "8181");
There is no need to set the http.proxySet property.
Pick a server from the nice list here: Hide My Ass: Proxy List
Put the parameters on the command line or use setProperty.
java -Dhttp.proxyHost=proxy.host -Dhttp.proxyPort=3128 MainClass

How to enable connection pooling over LDAP SSL?

Okay, so I'm moving my application over from non-SSL to SSL connections to my LDAP server. When running the application in non-SSL, connection pooling is working fine. However when I switch to SSL connection pools no longer work.
While researching here I realized that I never set the "com.sun.jndi.ldap.connect.pool.protocol" property to "plain ssl" since defaultly it is set to plain. I thought this was the problem.
When I implemented the change to include "plain ssl", it did not fix the problem and connection pools were still not being used.
Is there some other setting that I am missing?
Relevant code:
Hashtable LDAPEnvironment = new Hashtable();
LDAPEnvironment.put(Context.SECURITY_AUTHENTICATION, SECURITY_AUTHENTICATION);
LDAPEnvironment.put(Context.SECURITY_PRINCIPAL, SECURITY_PRINCIPAL);
LDAPEnvironment.put(Context.SECURITY_CREDENTIALS, SECURITY_CREDENTIALS);
LDAPEnvironment.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
LDAPEnvironment.put(Context.PROVIDER_URL, PROVIDER_URL );
LDAPEnvironment.put(Context.SECURITY_AUTHENTICATION, "simple");
LDAPEnvironment.put("java.naming.ldap.version", versionOfLDAP );
if (ldapProtocol != null && ldapProtocol.equalsIgnoreCase("SSL")){
LDAPEnvironment.put(Context.SECURITY_PROTOCOL,"ssl");
LDAPEnvironment.put("com.sun.jndi.ldap.connect.pool.protocol","plain ssl");
}
LDAPEnvironment.put("com.sun.jndi.ldap.connect.pool", "true");
I have found the problem. The documentation specifically states that the those properties are system properties and not environment properties. I was setting these as environment properties. :-)
If you scroll down a little, at the link you provided (scroll to "How Connections are Pooled"), you'll see the explanation to how the pooling works.
When you request a pooled connection, you will get one only if ALL the specified properties are identical. And that's a long list of properties...
I your case this is:
connection controls
host name, port number as specified in the "java.naming.provider.url" property, referral, or URL supplied to the initial context
java.naming.security.protocol property
java.naming.ldap.version property
java.naming.security.principal property
java.naming.security.credentials property
If you always use the same constants when request a connection from the connection pool, I think you should get the same pooled connection. That is, if you set the com.sun.jndi.ldap.connect.pool.* properties properly - but I didn't see that in the code you provided.
If you did set the com.sun.jndi.ldap.connect.pool.* properties to sensible values, try setting com.sun.jndi.ldap.connect.pool.debug to fine. This will help you debug.
Another option is to use a framework, or a provider that supports connection pooling. Note that the pooling provided to you by Java is rather limited. I used Spring-Ldap in the past, and it has very good support.
Hope this helps.

How do I make my Java application identify itself to Oracle on connection?

When my application connects to an Oracle database I want to be able to see by looking at the active sessions in the database that it is connected. Currently it identifies itself as "JDBC Thin Client" because that's the driver that I'm using, but other Java based applications that I have are somehow able to set this value to something more meaningful, like "SQL Developer". I thought it was a property of the Connection or the OracleDataSource, but I've not managed to find one that does the trick. Is this possible? In case it matters, I'm using Java 1.5, with Oracle 10g and the 10g thin driver.
java.util.Properties props = new java.util.Properties();
props.setProperty("password","mypassword");
props.setProperty("user","myusername");
props.put("v$session.osuser", System.getProperty("user.name").toString());
props.put("v$session.machine", InetAddress.getLocalHost().getCanonicalHostName());
props.put("v$session.program", "My Program Name");
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
Connection conn=
DriverManager.getConnection("jdbc:oracle:thin:#myhostname:1521:mysid", props);
SQL>select username,osuser,program,machine
from v$session
where username = 'ROB';
USERNAME OSUSER PROGRAM MACHINE
--------- ----------- ------------------ -----------
ROB rmerkw My Program Name machine
At application level you can use the following methods to set client_info, module and action in v$session:
dbms_application_info.set_client_info
dbms_application_info.set_module
dbms_application_info.set_action
There is also an Oracle function:
dbms_application_info.set_client_info('Client Info');
which sets the ClientInfo column in v$session.
This might be useful if you only have access to the Connection rather than the underlying DataSource or DriverManager.
Since oracle jdbc 12.1 you can set some client-info values via jdbc api, i.e. you can do
connection.setClientInfo("OCSID.CLIENTID", "MyClientId");
for properties OCSID...
ACTION, CLIENTID, ECID, MODULE, SEQUENCE_NUMBER and DBOP
See https://docs.oracle.com/database/121/JJDBC/jdbcvers.htm#JJDBC29006
Setting PROGRAM doesn't work this way, you can do that as described in the accepted answer or somewhat easier by setting the System property "oracle.jdbc.v$session.program".
You need to define the connection property v$session.program in your data source, in such a way that that property will be added to each connection. How you do that depends on your data source implementation. The value you set the property to will appear in oracle's active session table.
Starting with 12.1 the setEndToEndMetrics is deprecated, you may use setClientInfo
see the documentation for 12.2 here
Here a snippet of the usage
// "conn" is an instance of java.sql.Connection:
conn.setClientInfo("OCSID.CLIENTID", "clientID");
conn.setClientInfo("OCSID.MODULE", "myModule");
conn.setClientInfo("OCSID.ACTION", "myAction");
You may see the setting in V$SESSION with this query of the relevant session
select MODULE, ACTION, CLIENT_IDENTIFIER from v$session where ...
but only after a next statement is executed with this connection. The call of setClientInfo triggers no extra roundtrip this information is passed whit the next call.
Note also that you must use the Oracle (unwrapped) conenction - Check this for reference.

Categories

Resources