Friday, April 24, 2020

Fetching Unique XML Tags based on Values

Listing down the unique values of a tag in entire XML
Below is the raw information in the form of an XML

<?xml version="1.0" encoding="UTF-8"?>
<STATES>
<STATE>
<NAME>TELANGANA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>HYDERABAD</CAPITAL>
</STATE>
<STATE>
<NAME>TAMILNADU</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>CHENNAI</CAPITAL>
</STATE>
<STATE>
<NAME>KARNATAKA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>BENGALURU</CAPITAL>
</STATE>
<STATE>
<NAME>KERALA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>TRIVANDRUM</CAPITAL>
</STATE>
<STATE>
<NAME>MAHARASHTRA</NAME>
<REGION>WEST</REGION>
<CAPITAL>MUMBAI</CAPITAL>
</STATE>
<STATE>
<NAME>TELANGANA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>HYDERABAD</CAPITAL>
</STATE>
<STATE>
<NAME>TAMILNADU</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>CHENNAI</CAPITAL>
</STATE>
</STATES>

I want to get only the unique state names from the above information. (TELANGANA,TAMILNADU,KARNATAKA,KERALA,MAHARASHTRA)


//STATE/NAME[not(. = ../following-sibling::STATE/NAME)]

Credits : 
Padmapriya Iyengar (padmapriya.iyengar@gmail.com)

Saturday, April 28, 2018

Customize maven to fetch and push to other repositories

Maven Customization

Settings.xml (Understanding)


When we install maven either there will not be any settings.xml (in .m2 folder) or the settings present in conf folder will have default settings. Hence we must first understand the importance of settings.xml file.

The first task for us is to identify where is the settings.xml in the file system for you to go and change. Either you would use maven using Sts/eclipse in which case the settings file will not be present by default. In this case you must create settings.xml file in the folder <<UserHomeDirectory>>/.m2 

If you have downloaded the maven installation then the settings.xml file would be present with default settings under for folder <<MavenHome>>/conf

Settings.xml (Explained)



<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <servers> 
        <server>
            <username>admin</username> 
            <password>Darkhorse@123</password>
            <id>remote</id>
        </server>
    </servers>
    <distributionManagement> 
        <repository>
            <id>remote</id>
            <name>Darkhorses-MacBook-Pro-4.local-releases</name>
            <url>http://192.168.50.9:8081/artifactory/dh-repo1</url>
        </repository>
    </distributionManagement>
    <profiles> 
        <profile>
            <id>dh-repo1</id>
            <repositories>
                <repository>
                    <id>remote</id>
                    <name>Darkhorses-MacBook-Pro-4.local-releases</name>
                    <url>http://192.168.50.9:8081/artifactory/dh-repo1</url>
                </repository>
            </repositories>
        </profile>
    </profiles>
    <activeProfiles> 
        <activeProfile>dh-repo1</activeProfile>
    </activeProfiles>
</settings>

Further explanation :

Servers : It stores the credentials to connect to your distributionManagement repositories.

ID : This is the identifier with which it identifies the repository of distributionManagement. You can have multiple repositories in distributionManagement tag and each repository's credentials are identified by this tag

Distribution Management : This is used basically to push/deploy the application in some other repository apart from your local one. The server where you would deploy is provided in below url tag.

Profiles :  This stores the information of multiple profiles. Each profile can have multiple repositories defined which connects to different servers.

ActiveProfiles : Settings which you can change anytime to tell maven that fetch from which repository. This is used especially in situations where you want do refer dependencies from various different servers depending on the application.

Deployment to Custom repo :

mvn deploy -DaltDeploymentRepository=remote::default::http://192.168.50.9:8081/artifactory/dh-repo1
If you want to do it from sts, below image shows how to achieve that


It requires two things,

  • Goals : Entire command without mvn.
  • User Settings : settings.xml path which will be used to fetch distributionManagement details and deploy to that repository.
I hope I am clear in explaining how to fetch and how to publish to custom repositories.

Tuesday, June 21, 2016

How to check the validity of a WSDL

Java JDK has provided a way to validate the WSDL and is done in the below way,

Command to use to validate the wsdl : 

wsimport <<WSDL Url>>

I have a WSDL at the below given url :

http://localhost/wsdls/Sample.wsdl


Now the browser will not be able to validate the wsdl hence needing a validating step. There can be online validator's or even eclipse plug in can also validate the wsdl. If any of the both steps are possible please go ahead with it, this is the third way of validating where in the guy does not have any access to internet nor does he has an eclipse installed in the machine and in need of validating the wsdl.

Java jdk has provided a way to go ahead with this and the command is shown below,


Note : The above command must be executed from bin directory of a java jdk. Yes, you must be having java jdk for it.

The result of the above command is shown here,


Now if you check the above result, the "parsing WSDL..." is completed without any errors giving us the information that the wsdl is perfectly valid.

Saturday, April 30, 2016

Avoiding EventService problem

Problem Statement :

Avoiding the problem, "Event channel is unable to connect to web server. Do you want to reconnect?".

Kiran : Hey Champ, I am trying to build an application in cordys and I am facing a problem.

Champ : What is the problem that you are facing?

Kiran : There is no error while accessing cordys application, but this confirmation box keeps popping up every now and then only to increase irritation.

Champ : I did not get you :( 

Kiran : That error which is shown below,


Champ : That is not an error right? You can simple click "Yes" whenever it pops up. Wouldn't that solve the problem?

Kiran : Yes, that would solve the problem temporarily. Can I fix it permanently?

Champ : Ohh Yes, You can!!!! First tell me, Can you see any error in the debugging tools of the browser? You must be telling me something so that I can solve your problem.

Kiran : Yes, It is trying to download some of the resource and it is failing because of some ERR_HOST_NAME_ERROR or something. I don't remember exact error but what I do remember is that there is definitely a problem with the hostname. I am using the application using the IP Address.

Champ : Thank you!! That information really helps. Now can you try from the command prompt to ping the hostname and as well as the ip address that you are trying to connect. Then let me know the results.

Kiran : I am able to get a response from ping to IP Address but not to the hostname.

Champ : Look!!!! This is the problem. Your system is not able to look up the hostname for the IPAddress that you are connecting.

Kiran : What should I do for solving this problem?

Champ : Okay!!

  • Go to the mentioned folder and locate "hosts" file, shown below,


  • Then edit hosts file with an entry similar to below, 



  • Remember, 192.168.50.19 is my local intranet IP address and the host names are also mine. Don't prove yourself dumb by just copy pasting the same whatever I have shown. Use your brain please.
Kiran : You must be kidding!!!! Anyways thanks for your help champ. I understood the problem and it solved it :) See you again!!!!!! 



Thursday, April 28, 2016

Importance of variable names and column names in Entity of a Spring application

This is to have an understanding of how de-serialization works when JdbcTemplate converts the query result into object form.

My Entity Structure looks as show below,















If you observe, there is a variable name "month" whose column name is "monitor_month". This means it refers to "month" at Object level and "monitor_month" as database column name (as we know @Entity refers to a table).

Now a piece of module was developed in order to retrieve data from database. This can either be done by using JPARepository or by using JdbcTemplate. I am using the later approach,

Below is the piece of code which I have used to retrieve database information and de-serialize the information in the entity shown above,


The targetClassName here refers to the entity shown above. Considering no exception case, this piece of code should retrieve information from database by executing the query which is present in "queryTemplate" and de-serialize it by using the className provided in the variable "targetClassName". 

But somehow all of the data are not being de-serialized. Interestingly only the fields whose column names and variables are same (Ignore underscores and case) are being serialized, i.e., monitor_year is being deserialized because monitor_year and monitorYear are one and the same variable.

Hence it is must and should that the variable name which is being used should be equivalent to the column name that is being defined. 

I have modified the entity as shown below 

Once I have modified the entity as shown above, I was successful in fetching the result.

Explanation :

JdbcTemplate when executes the query gets the information which has meta-data of database (i.e., column names as of table column names). But now we need to de-serialize into an object format. Hence jdbcTemplate would want a kind of understanding between the variable names and the column names and hence it will assume these both are equivalent variables. This means that the column name "monitor_year" is equivalent to "monitoryear" or "MonitorYear" or MoniTORYear" or simply "monitorYear". Hence when there is a database column monitor_year, it will push the value into any of the above mentioned variables.

The problem with my previous approach was, the query result was giving response in the column "monitor_day" where as my variable's name is "day". This means the jdbcTemplate would consider "monitor_day" and "day" as two different variables and hence it will not de-serialize this field. The same was the problem with other fields as well.

Installation and change context path of gitlab server in linux

Installation :

The installation process of gitlab-community edition is explained here,
https://about.gitlab.com/downloads/#centos6
Configure custom Context-Path :

Once you install, there might be a necessary to change the context path of the gitlab-server. 

For example, when you install gitlab in an environment 192.168.10.10 then the gitlab GUI would be accessible from the url http://192.168.10.10/ . But normally you would want to add a context path to it i.e., you would want to access gitlab from the below url


http://192.168.10.10/gitlab

The same would be achieved by modifying gitlab.rb file present in the installation directory.If you have access to the file, then open with default text editor and modify the property external_url to http://192.168.10.10/gitlab

Alternatively you can use below command to modify,
sudo vi gitlab.rb
Go to the configuration external_url in the file and modify the property to below,
http://192.168.10.10/gitlab
Once you are done with the changes, you should let linux know about the change and to configure gitlab again, this can be done by the below command
sudo gitlab-ctl reconfigure 
Once the command is executed, the context path is configured for gitlab.

Monday, April 11, 2016

Execute DDL scripts when a spring application is initialized

Normally everyone would encounter a situation where in they want to execute few of the database scripts while deploying a solution. These scripts might include anything like executing a procedure, running a batch, insert script for defaulting of few values, update to existing dataset etc., 

When such situation arises, we can follow the below mentioned approach as an initial approach to handle it.

Spring by default tries to load a script file "data.sql" located in the classpath i.e., where the application.properties, log4j.properties etc., are present. 

Hence try to consolidate all the scripts into one file and name it as "data.sql" and place it under "<<apacheTomcatFolder>>\webapps\<<app>>\WEB-INF\classes", 

Disadvantages of this approach :
  • Since it is an automated process, any failure in the execution of scripts will not be known unless we go through the log files. Hence error percentage should be minimum.
  • Using the above approach, we cannot divide all the scripts into different modules and execute with different names. By default the spring will look for a file named "data.sql" unless someone overrides this setting in application.properties which is not explained in this blogpost.
  • A restart is needed for every change in this file. Try to use only when the script is flawless and also can be executed during the deployment. If there is a provision to execute the scripts in database then try to use that as it will provide more control over the scripts that will be run.

Thursday, August 20, 2015

MongoDB as a Service

Command to create mongodb as a service is 

mongod --dbpath="C:\Program Files\MongoDB\data" --logpath="C:\Program Files\MongoDB\log\log.txt" --install

Execute above command from Mongodb's bin folder,




References :

http://www.mkyong.com/mongodb/how-to-run-mongodb-as-windows-service/

http://stackoverflow.com/questions/2438055/how-to-run-mongodb-as-windows-service

Wednesday, August 19, 2015

Command to publish jar into your local Maven Repository


D:\Kiran\Kiran\Projects\NCB\ETL>mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar -Dfile=ojdbc6.jar
 -DgeneratePom=true

Points to be noted :

  • Maven client to be installed. You can check if maven is installed by running the below command ,
    mvn -version which would result in the below result,



  • Executing above command  results in below results,


Once you see the BUILD SUCCESS in the above command result,

You can add dependency in your POM.xml as below,

                <dependency>
<groupId>com.oracle</groupId> <!-- Group ID from the command-->
<artifactId>ojdbc6</artifactId>  <!-- Artifact ID from the command-->
<version>11.2.0.3</version>       <!-- Version from the command-->
</dependency>

References :


Saturday, July 25, 2015

How to know what version of OS you are running

Command to show the os version of windows,

Open command prompt and type in the below command

wmic os get osarchitecture


Thursday, July 16, 2015

Serialize manually using faster jackson library in spring

I am trying to get a JSON Structure for my object Order which internally has OrderDetails as oneToMany mapping,

@Entity
public class Order {    
    /*
        All the fields associated with this class
    */      
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "orderId")
    private Set<OrderDetails> orderDetails;

    //Getters for all properties defined in this class as jackson would depend on this thing
}
In my case I am using a textWebSocket which is expecting a message only in String format so I need to serialize the objects and push to the client, I am depending on faster jackson to do it and here it goes,

public String getObjectAsString()
{

    //orderObjs : Considering this is the list of objects of class Order

    ObjectMapper objMapper = new ObjectMapper();
            returnValue = objMapper.writerWithType(
                    objMapper.getTypeFactory().constructCollectionType(
                            List.class, Order.class)).writeValueAsString(
                    orderObjs);
    return returnValue;
}

objMapper.writerWithType(objMapper.getTypeFactory().constructCollectionType(List.class,Order.class)).writeValueAsString(orderObjs);

Monday, July 6, 2015

Use a new keyword and still inject the object : Spring

Spring works on dependency injection. This means there is no need to create an object explicitly, spring will do it for you.

But there are situations where you have to create new objects (ex., Reflection). By going with this approach you are conveying string that you will have control over the current object. i.e., any Autowired fields inside the object class will be null and spring will not consider injecting it. 

But there is a way to autowire those fields as well,

appctx.getAutowireCapableBeanFactory().autowireBean(wsdlParserAPIObject);

where appctx is ApplicationContext which is autowired.

http://stackoverflow.com/questions/31237062/using-class-forname-but-want-to-autowire-members-of-the-target-class

Monday, June 29, 2015

Call a procedure from Spring

How to call a procedure using spring :

In this blog post I would take you through the approach of calling a procedure from spring using "StoredProcedure" approach.

StoredProcedure is an abstract class which has to be extended.

Define a class which extends StoredProcedure, below is a sample which is shown,

Then call a procedure which is shown as below,





Friday, June 26, 2015

Autowire datasource while using JdbcDAOSupport

public class EntityDAO extends JdbcDaoSupport{

Logger logger = LoggerFactory.getLogger(EntityDAO.class);

@Autowired
private DataSource datasource;


/*
* Setting data source as it extends jdbcDAOSupport
*/
@Autowired
private void initialize()
{
setDataSource(datasource);
}

Wednesday, May 20, 2015

Comparing arrays in Java Script

A sample on how to compare two arrays :


Array.prototype.diff = function(a) {
    return this.filter(function(i) {return a.indexOf(i) < 0;});
};
var a = ['b','c'];
var b = ['b','c','d','e','f'];
var temp = a.diff(b);
temp.length;

Credits : Nikhil

Friday, April 10, 2015

Queries for faster development

Concatenation of column values :

I want to concatenate one column values and get it as an aggregated result from database,

For Eg :

EMAIL_ADDRESS 

kiran.joshi@darkhorseboa.com
j.kiran809@gmail.com
j.kiran@alahli.com

Result should be something like this

<to><address><emailAddress>kiran.joshi@darkhorseboa.com</address><address><emailAddress>j.kiran809@gmail.com</emailAddress></address><address><emailAddress>j.kiran@alahli.com</emailAddress></address></to>

This can be achieved in SQL Server using STUFF function and the link can be found here

http://learnings.joshikiran.com/2014/10/how-to-use-stuff-keyword-in-microsoft.html

In Oracle it can be achieved by,

SELECT 
  '<to>'||LISTAGG('<address><emailAddress>'||EMAIL_ADDRESS||'</emailAddress></address>','')
    WITHIN GROUP (ORDER BY COLUMN3)||'</to>' AS ALIAS_ID
FROM TABLE1
WHERE COLUMN2 = 'sample' AND EMAIL_ADDRESS IS NOT NULL;

Note : This works for Oracle version 11g R2 and higher only.

Credits : PadmaPriya Iyengar
----------------------------------------------------------------------------------------------------------------------

Tuesday, April 7, 2015

Authenticator is not instantiated via the properties constructor

I am triggering a SOAP request from SOAP UI to a custom gateway of cordys environment which accepts user credentials in SOAP header.

For Ex :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns="http://corp.alahli.com/middlewareservices/header/1.0/" xmlns:ns1="http://corp.alahli.com/middlewareservices/test/1.0/">
<soapenv:Header>
<oas:Security>
<oas:UsernameToken>
<oas:Username>${#Project#username}</oas:Username>
<oas:Password>${#Project#password}</oas:Password>
</oas:UsernameToken>
</oas:Security>
</soapenv:Header>
<soapenv:Body>
<!-- Main SOAP Request which is connected to service group SG1-->
</soapenv:Body>
</soapenv:Envelope>

Content which is shown in green color is the security information which I am passing from SOAP UI. I hope people are well aware what ${#Project#username} stands for (i.e., I am reading credentials from properties of SOAP Project).

Let's say the request is targeted to service group SG1 in this case. 

As we know that any cordys service is served using SAMLArt. In this case the SAMLArt will be provided by the custom gateway to the application container. Now when the custom gateway tries to get the SAMLArt it uses the property of the application container. If it does not find the property it might throw you an error which was my case.

I encountered an exception which says something like this,

com.eibus.security.authentication.InvalidAuthenticatorException: Authenticator is not instantiated through the properties constructor.
at com.eibus.security.authentication.AuthenticatorFactory.getAuthenticator(AuthenticatorFactory.java:78)
at com.eibus.security.identity.WSSecurityUsernameTokenIdentityType.validate(WSSecurityUsernameTokenIdentityType.java:64)
at com.eibus.soap.SOAPTransaction.<init>(SOAPTransaction.java:349)
at com.eibus.soap.SOAPTransaction.<init>(SOAPTransaction.java:175)
at com.eibus.soap.Processor.onReceive(Processor.java:970)
at com.eibus.soap.Processor.onReceive(Processor.java:943)
at com.eibus.connector.nom.Connector.onReceive(Connector.java:417)
at com.eibus.transport.Middleware$NonTransactionalWorkerThreadBody.run(Middleware.java:1758)
at com.eibus.util.threadpool.WorkerThread.run(WorkerThread.java:64)

Now I found out the property that was missing i.e., the authenticator class. We need to add the authenticator property to the service group that was implementing my service. This should be done by using LDAP explorer,

The property which is to be added is below,

<authenticator implementation="com.eibus.security.authentication.CARSAuthenticator" /> 

Since I am using CARSAuthenticator in my case. This should be added to the service group SG1 (in the above explained scenario). An example of such updation can be found below,



 

Saturday, April 4, 2015

Usage of elementFormDefault attribute of Schema tag

Learnings from Gora (amitagl27@gmail.com)

I was creating a WSDL in my project to generate contract first webservices. 

When I imported the WSDL in cordys, it gave me empty namespaces just after the attributes, i.e. it looked something like this :

<!--Cordys Input Structure-->

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP:Body>
    <CreateForeclosureProcess xmlns="http://schemas.bas.com/ext/IntakeProcessServices/">
      <CaseId xmlns="">PARAMETER</CaseId>
    </CreateForeclosureProcess>
  </SOAP:Body>
</SOAP:Envelope>

<!-- External Tool input Structure, Wizdler (https://chrome.google.com/webstore/detail/wizdler/oebpmncolmhiapingjaagmapififiakb?hl=en)-->

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <CreateForeclosureProcess xmlns="http://schemas.bas.com/ext/IntakeProcessServices/">
            <CaseId xmlns="">[string]</CaseId>
        </CreateForeclosureProcess>
    </Body>
</Envelope>

I did two things to remove these namespaces.

1. To remove from Cordys : Instead to having a direct element in my Request schema, I created a reference to another element.

Old :

<xsd:element name="CreateForeclosureProcess">
         <xsd:complexType>
          <xsd:sequence>
 <xsd:element  type="xsd:string" name="CaseId" />           
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>

Modified :

<xsd:element name="CreateForeclosureProcess">
         <xsd:complexType>
          <xsd:sequence>
 <xsd:element ref="tns:CaseId" />            
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
  
  <xsd:element  type="xsd:string" name="CaseId" />

 2. Even after doing this, the Wizdler plugin was still showing the empty namespace, then a little research on web gave me following link :

http://docs.intersystems.com/ens20082/csp/docbook/DocBook.UI.Page.cls?KEY=GSOP_service_fine_tuning

which suggested to add 

elementFormDefault='qualified'
to my schema tag as an attribute.

Example :
<xsd:schema targetNamespace="http://www.darkhorseboa.org/ThirdPartyUtilities/" elementFormDefault="qualified">