Monday, December 3, 2018

Loading TrustStore and KeyStore from an external directory Sample Code

package com.sample.khavara;

import java.security.KeyStore;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.Resource;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
@Profile({"dev", "test", "prod"})
public class RestTemplateSecureConfiguration {

@Autowired
private ApplicationContext context;

@Autowired
private ObjectMapper objectMapper;

@Value("${spring.resttemplate.ssl.client.keystore}")
private String keyStoreFile;
@Value("${spring.resttemplate.ssl.client.key-store-password}")
private String keyStorePassword;
@Value("${server.ssl.trust-store}")
private String trustStoreFile;
@Value("${server.ssl.trust-store-password}")
private String trustStorePassword;

private static final String KEYSTORE_TYPE = "JKS";

@Bean
public RestTemplate restTemplate() throws Exception {
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
List> messageConverters = new ArrayList<>();
MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
jsonMessageConverter.setObjectMapper(objectMapper);
messageConverters.add(jsonMessageConverter);
restTemplate.setMessageConverters(messageConverters);
return restTemplate;
}

private ClientHttpRequestFactory clientHttpRequestFactory() throws Exception {
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient());
return requestFactory;
}

private HttpClient httpClient() throws Exception {
KeyStore keyStore = loadKeyStore();
KeyStore trustStore = loadTrustStore();
SSLConnectionSocketFactory socketFactory = createSSLConnectionSocketFactory(keyStore, trustStore);
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
return httpClient;
}

private KeyStore loadTrustStore() throws Exception {
Resource resource = context.getResource(trustStoreFile);
KeyStore trustStore = KeyStore.getInstance(KEYSTORE_TYPE);
trustStore.load(resource.getInputStream(), trustStorePassword.toCharArray());
return trustStore;
}

private KeyStore loadKeyStore() throws Exception {
Resource resource = context.getResource(keyStoreFile);
KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE);
keyStore.load(resource.getInputStream(), keyStorePassword.toCharArray());
return keyStore;
}

private SSLConnectionSocketFactory createSSLConnectionSocketFactory(KeyStore keyStore, KeyStore trustStore) throws Exception {
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder()
.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, keyStorePassword.toCharArray()).build());
return socketFactory;
}
}

Monday, February 5, 2018

Reading exclamation mark (!) from command line on windows for scala script

Hi,
I was wondering while I was able to send the ! character as part command line argument to scala script on Linux shell but not on Windows.

Here is my Scala Script in Hello.scala file

object Hello extends App{
args.foreach( println(_))
}


Execution command from the command prompt on linux is

$scala  Hello.scala  This is my input!

Expected out is coming fine with ! mark, however the same thing on Windows machine the ! mark is not coming.  Here is the reason the windows shall take the input characters with escaped ^ and the command needs to be with in the string.  So the below command will come fine on the windows.

C:\>scala  Hello.scala  This is my "input^!"


====================== ENJOY LEARNING ==================