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 ==================

Thursday, December 7, 2017

WSO2 not getting carbon login page with host name or IP address but works well for localhost

I have started exploring the WSO2 Enterprise Integrator.  I am happy that the installation is as simple as tomcat installation (just copy the the root folder on to your system and start running it.) with prior JDK on your system.  After starting the server I am able to login to the Management console using the https://localhost:9443/carbon  or  https://127.0.0.1:9443/carbon URL.  However if I try to access the same with system name or the system dynamic IP when I am inside the intranet then say https://lpt-systemesb:9443/carbon   or  https://175.23.4.56:9443/carbon  then I am not able to access the server.  I didn't see any request going to the server as well.

After trying several blogs and documentations we have to set the  HostName  or the MgtHostName  values.  But still the issue is not resolved.  because the issue is not with this setting.  We don't have to do any changes in the WSO2 related any config files.

The resolution for this issue is DISABLE the firewall or allow the access on the ports. Uncheck the Enable Firewall checkbox. (Means disable the firewall). Now you should be able to access the WSO2 Management console without any issues on the same system during the development.



In the above image the Enable Firewall is checked, to work properly it needs to be unchecked.

Monday, October 30, 2017

How to consume NTLM (MSDynamics) authentication based SOAP services in JAVA using httpclient

I have a need to consume the Webservices published in MSDynamics applications.  This uses the Microsoft specific NTLM based authentication and authorization mechanism.  However to consume the SOAP services I did find any direct solution.  Below is the code that is working.  You have to change the credentials and the input SOAP message formation strings remaining all you can use as it is.

Note: Use the latest httpclient-4.2.3.jar or above ,  commons-codec-1.11  or above.  Few more other jars which you can take from Axis application.

CODE  ( TestCustomerRead.java )
============================

import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

public class TestCustomerRead {

static String HOST  = "120.142.157.189";
static int    PORT  = 81;
static String USER  = "yourusername";
static String PWD   = "slf#pwd";
static String DOMAIN    = "mcpdl";
static String PROTOCOL  = "http";
static String WSDL_PATH = "/MicrosoftDynamicsAXAif60/AXCustomerService/xppservice.svc?wsdl";
static String SERVICE_ENDPOINT = "/MicrosoftDynamicsAXAif60/AXCustomerService/xppservice.svc";

// soap action you can find in the WSDL at the action attribute of the operation you want to consume //
static String SOAP_ACTION    = "http://schemas.microsoft.com/dynamics/2008/01/services/CustomerService/read";

public static void main(String[] args) {

try {
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(USER, PWD, HOST, DOMAIN));
HttpHost target = new HttpHost(HOST, PORT , PROTOCOL);

HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);

System.out.println("authenticated started");

// Execute a cheap method first. This will trigger NTLM
// authentication  (optional )
HttpGet httpget = new HttpGet( WSDL_PATH );
CloseableHttpResponse response1 = httpclient.execute(target,
httpget, context);
try {
HttpEntity entity1 = response1.getEntity();
System.out.println("=====GET=======>>>>" + response1.toString());
} finally {
response1.close();
}

// Execute an expensive method next reusing the same context (and
// connection)
HttpPost httppost = new HttpPost( SERVICE_ENDPOINT );
httppost.setHeader("SOAPAction",SOAP_ACTION );
httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
StringEntity stringEntity = new StringEntity(getReadRequestSOAP().toString(), "UTF-8");
stringEntity.setChunked(true);
httppost.setEntity(stringEntity);

CloseableHttpResponse response2 = httpclient.execute(target, httppost, context);
try {
HttpEntity entity2 = response2.getEntity();

if(response2.getStatusLine().getStatusCode()== 200){
System.out.println("=====POST=======>>>>HTTP Response Status :"
+ response2.getStatusLine().toString());

System.out.println("RESPONSE MESSAGE IS \n ===================="  );
InputStream in = entity2.getContent();
String body = IOUtils.toString(in, "UTF-8");
System.out.println(body);
System.out.println("\n ===================="  );

}else{
System.out.println("AUTHENTICATION ERROR WITH CODE " + response2.getStatusLine().toString());
}

} catch (Exception ex) {
ex.printStackTrace();
} finally {
response2.close();
System.out
.println("COMPLETED THE REQUEST/RESONSE.");
}

} catch (Exception ex) {
ex.printStackTrace();
}
}

  // replace the SOAP message with your request messages.
// change the the input values as per your request data

private static String getReadRequestSOAP() {
// Below just form the complete SOAP message as a string
String statusRequest = " "
+ "    "
+ "         "
+ "               mcpl"
+ "       
" + "   
" + "    "
+ "          "
+ "              "
+ "              "
+ "                "
+ "                      "
+ "                           AccountNum "
+ "                           C-1008 "
+ "                     
" + "               
" + "             
" + "             
" + "               
" + "             
" + "         
"; return statusRequest;
}

}

=====================END =============
References:

http://hc.apache.org/httpcomponents-client-ga/

https://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html

http://hc.apache.org/httpcomponents-client-4.5.x/ntlm.html


https://www.mkyong.com/java/apache-httpclient-examples/

https://rathinasaba.wordpress.com/2013/02/01/call-webservice-wsdl-based-using-apache-httpconnection/

https://stackoverflow.com/questions/12561503/how-to-call-a-soap-webservice-with-a-simple-string-xml-in-string-format 

Tuesday, August 29, 2017

SOA 11g Transaction timing configuration

Reference taken from other blog. at ( http://soadiscovery.blogspot.in/2011/04/soa-11g-configuring-transaction-timeout.html )

We have set our environment with below values and worked fine.

The gist of the blog the following value should be set (SyncMaxWaitTime < BPEL EJB's transaction timeout < Global Transaction)

SyncMaxWaitTime à300

All the EJB Beans values à 1200

JTA (Global Transaction Timeout)à1800


================================================================

SOA 11g : Configuring Transaction timeout in BPEL
Transaction timeout can be configured for BPEL in 11g using the below properties.
1. SyncMaxWaitTime
Maximum time BPEL process wait before returning result to client(or another Sync process)
To set this property
a. Login to EM console
b. Expand SOA and right click on “soa-infra”
c. From context menu, select SOA Administration –> BPEL properties
d. Click on “More BPEL Configuration properties…”
 SyncMaxWaitTime
 2.Transaction Time-out for BPEL EJB’s
The following EJB’s need to be configured for transaction time outs.
  • BPELActivityManagerBean
  • BPELDeliveryBean
  • BPELDispatcherBean
  • BPELEngineBean
  • BPELFinderBean
  • BPELInstanceManagerBean
  • BPELProcessManagerBean
  • BPELSensorValuesBean
  • BPELServerManagerBean
To change time out for these beans ,
a. Login to Administration Console
b. Click on Deployments
c. Expand soa-infra –> EJB’s
Transaction-EJB1of3 Transaction-EJB2of3
d. Click on EJB for which you want to change the timeout
e. Click on Configuration
f. Change value for field “Transacion Timeout”
g. Click Save.
Transaction-EJB3of3
 3. Global Transaction Timeout
This property is timeout in seconds for active transactions. After this time, if the transaction is still “Active”, then it gets rolled back.
To change this value
a. Login to Administration Console
b. Expand “Services” –> Click on “JTA”
c. Click on “JTA” tab if it’s on a different tab
d. Change value for field “Timeout Seconds”
GlobalTransactionTimeout
Very Important :
SyncMaxWaitTime < BPEL EJB's transaction timeout <  Global Transaction Timeout

Monday, May 22, 2017

SOA 11g BPEL process or the Sub Process goes in recovery mode resolution

In the SOA if a BPEL process is always going in recovery mode then the main problem would be one of the end points inside the BPEL process consuming services is not reachable.

Check if your deployment configuration file has the right targets when moving from one environment to another environment.

Check if the external service that is pointing is up and running.

DB Adapter stopped polling the record from the database table however the process health looks good

If the BPEL process health looks good, there is a chance that you could find error message in the log files about primary key value null.

If the DB Adapter is polling using the logical primary key (ie. database table doesn't have primary key on the table)  and the primary key value is null in at least one field of a record then that record will be picked and after that stops proceeding further.  This record will be in stuck positon.  And all the subsequent records won’t be polled.    So, delete all the bad primary key based records.  Then the remaining process will continue picking up by the Db adapter as usual.