camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pankaj Kumar <pankaj.bsr2...@gmail.com>
Subject Camel with SSL and HostNameVerifier
Date Fri, 12 May 2017 04:10:58 GMT
Hi All,
 I'm new to camel. Plese suggest me how Ican change this class into camel
route xml. Plese give me suggestions


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.inject.Named;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

public class RequestResponseIml implements RequestResponse {
	@Value(value = "${proxy.host}")
	private String proxyHost;
	@Value(value = "${proxy.port}")
	private String proxyPort;
	@Value(value = "${SSL_CONTEXT}")
	private String ssl_context_version;

	public String invokeAction(String url, String token) throws Exception {

		SSLContext sslcontext = null;
		String response = "";
		try {
			sslcontext = SSLContext.getInstance(ssl_context_version);
			sslcontext.init(new KeyManager[0], new TrustManager[] { new
TrustManager() }, new SecureRandom());

		} catch (NoSuchAlgorithmException e) {
			logger.info("Error NoSuchAlgorithmException " + e.getLocalizedMessage());
		} catch (KeyManagementException e) {
			logger.info("Error KeyManagementException " + e.getLocalizedMessage());
		} catch (Exception e) {
			logger.error("Exception in getting Instance of SSLContext: " +
e.toString());
			throw e;
		}

		try {
			
			SSLSocketFactory factory = sslcontext.getSocketFactory();
						
			logger.info("Creating HttpsURLConnection");
			HttpsURLConnection connection = null;
			InputStream is = null;
			
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost,
Integer.parseInt(proxyPort)));
				         
			connection = (HttpsURLConnection) new URL(url).openConnection(proxy);
			logger.info("HttpsURLConnection has created "+connection);	         	
	         connection.setRequestMethod("POST");
	        
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");	 
	         connection.setRequestProperty("Content-Length", "" +
Integer.toString(token.getBytes().length));
	         connection.setRequestProperty("Content-Language", "en-US");  	   	
	         connection.setUseCaches (false);
	         connection.setDoInput(true);
	         connection.setDoOutput(true);	         
	        
			 connection.setSSLSocketFactory(factory);
			 logger.info("connection setSSLSocketFactory");
	         connection.setHostnameVerifier(new HostnameVerifier());
	         logger.info("connection setHostnameVerifier");
	         
	         OutputStream os = connection.getOutputStream();
	         OutputStreamWriter osw = new OutputStreamWriter(os);
	         osw.write(token);
	         osw.flush();
	         osw.close();

	         is =connection.getInputStream();
	       
	         BufferedReader in = new BufferedReader(new InputStreamReader(is));             
	         response = in.readLine();
	 		
			logger.info("Output from NSDL "+response);
	 		is.close();
	 		in.close();
			
		} catch (UnsupportedEncodingException e) {
			String encodeException = "Unsupported Encoding Exception - " +
e.getMessage();
			logger.error(encodeException);
		} catch(Exception e) {
			   logger.info("Error in invokeAction - " + e.getLocalizedMessage());
			   logger.info("Error in invokeAction StactTrace - " +
e.getStackTrace());
			   //e.printStackTrace();
		   }

		return response;
	}
	
	public static class TrustManager implements X509TrustManager {

		public boolean isClientTrusted(X509Certificate cert[]) {
			return true;
		}

		public boolean isServerTrusted(X509Certificate cert[]) {
			return true;
		}

		@Override
		public X509Certificate[] getAcceptedIssuers() {
			return new X509Certificate[0];
		}

		@Override
		public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws
CertificateException {

		}

		@Override
		public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws
CertificateException {

		}
	}

	public static class HostnameVerifier implements HostnameVerifier {

		@Override
		public boolean verify(String arg0, SSLSession arg1) {
			return true;
		}

		public boolean verify(String urlHostname, String certHostname) {
			return true;
		}

	}

}




--
View this message in context: http://camel.465427.n5.nabble.com/Camel-with-SSL-and-HostNameVerifier-tp5799349.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Mime
View raw message