Monday, July 21, 2014

Call a web service from html using JQuery

Adding to the earlier post of mine http://learnings.joshikiran.com/2014/05/call-web-service-from-html.html I wanted to show a way to call a web service using JQuery in this post.

So the pre-requisites remains same i.e., you must know which web service you want to call (Method + Namespace) and also a gateway URL (URL to which you want to POST/GET).

The example I am trying here is executing the below SOAP request

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<GetXMLObject xmlns="http://schemas.cordys.com/1.0/xmlstore">
<key version="">/OAuthIntegration/RedirectorContent.xml</key>
</GetXMLObject>
</SOAP:Body>
</SOAP:Envelope>

Gateway URL which I am using is : /cordys/com.eibus.web.soap.Gateway.wcp? 

(For Ppl who understand the architecture of cordys : This is a SOAP request intended to be executed under system organization context. If you want a service to be executed under any organization context you need to append the Organization dn also to the gateway)

By taking references from cordys API, I have created a wrapper on $.ajax call to call a webservice. The wrapper which i have created is something like $.joshi.ajax which is on similar lines with $.cordys.ajax

This works exactly similar way what I have explained in the similar post (request as string and posting to the gateway URL) but this using ajax calls of JQuery.

A utility js file should be as mentioned below, this utility file then be imported where ever you want to use,

<<NameOfJSFile : joshiJQuery which you can view it html page while importing>>

$.joshi = function(){
$.joshi={};
}
$.joshi.ajax = function(options){
var opts = $.extend({}, $.joshi.ajax.defaults);
opts = ajaxExtend(opts, options);
opts.url = configureGatewayUrl(opts.url, opts);
if (!opts.url) return null;
if (typeof (opts.data) === "undefined" && opts.method && opts.namespace) {
var dataStrings = [];
dataStrings.push("<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'><SOAP:Body><");
dataStrings.push(opts.method);
dataStrings.push(" xmlns='");
dataStrings.push(opts.namespace);
dataStrings.push("'>");
if (opts.parameters) {
dataStrings.push(getParameterString(opts.parameters, opts));
}
dataStrings.push("</" + opts.method + ">");
dataStrings.push("</SOAP:Body></SOAP:Envelope>");
opts.data = dataStrings.join("");
}
debugger;
return $.ajax(opts);
}
function getParameterString(parameters, settings) {
var pStrings = [];
if ($.isArray(parameters)) {
for (var i = 0, len = parameters.length; i < len; i++) {
var par = parameters[i];
pStrings.push("<" + par.name + ">");
pStrings.push((typeof (par.value) === "function" ? par.value() : par.value));
pStrings.push("</" + par.name + ">");
}
} else if (typeof (parameters) === "object") {
if ($.cordys.json) pStrings.push($.cordys.json.js2xmlstring(parameters));
else {
for (var par in parameters) {
pStrings.push("<" + par + ">");
pStrings.push((typeof (parameters[par]) === "function" ? parameters[par]() : parameters[par]));
pStrings.push("</" + par + ">");
}
}
} else if (typeof (parameters) === "function") {
if (typeof (settings.context) === "object") {
pStrings.push(parameters.call(settings.context, settings));
} else {
pStrings.push(parameters(settings));
}
} else if (typeof (parameters) === "string") {
pStrings.push(parameters);
}
return pStrings.join("");
}
function configureGatewayUrl(url, options) {
return url ? url.replace(/^http:\//, window.location.protocol + "/").replace(/\/localhost\//, "/" + window.location.host + "/") : "com.eibus.web.soap.Gateway.wcp";
}
function ajaxExtend(target, src) {
var key, deep, flatOptions = jQuery.ajaxSettings.flatOptions || {};
for (key in src) {
if (src[key] !== undefined) {
(flatOptions[key] ? target : (deep || (deep = {})))[key] = src[key];
}
}
if (deep) {
jQuery.extend(true, target, deep);
}
return target;
}
$.joshi.ajax.defaults = {
url: "",
async: true,
isMock: false,
type: "POST",
contentType: "text/xml; charset=\"utf-8\"",
dataType: "json"
} 

And finally the html which you have to write should be something similar to below

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="joshiJQuery.js"></script>
<script type="text/javascript">
function callWebService()
{
debugger;
var xmlKey="";
var xmlKey=$('#key').val();
$.joshi.ajax({
 url : "http://dhl-hyd1024/cordys/com.eibus.web.soap.Gateway.wcp",
 method :"GetXMLObject",
 namespace: "http://schemas.cordys.com/1.0/xmlstore",
 parameters : "<key version=''>"+xmlKey+"</key>",
 complete : function(data){
debugger;
$('#responseOfService').html(data.responseText.replace(/</g,"&lt;").replace(/>/g,'&gt;'));
 }
});
}
</script>
</head>
<body class='default'>
<input type="input" id="key" style="width:400px;" value="/bFOIntFrameWork/RedirectorContent.xml"/>
<input type="button" value="Fire Web Service" style="width:200px;" onclick="callWebService()"></input>
<br/><br/>
<div id="responseOfService" style="
font-family: Trebuchet ms;
font-size: 0.9em;
width: 700px;
border: 1px solid red;
">
</div>
</body>

</html>


How to execute a window command from Java

This java code will show how can we execute a windows command from Java

package commons.utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExecuteCommandFromJava {
public static void main(String[] args) throws IOException {
   String[] command = new String[3];
        command[0] = "cmd";
        command[1] = "/c";
        command[2] = "E: && dir && cd snap";
        Process p = Runtime.getRuntime().exec(command);
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = reader.readLine();
        while (line != null) {
            System.out.println(line);
            line = reader.readLine();
        }
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        String Error;
        while ((Error = stdError.readLine()) != null) {
            System.out.println(Error);
        }
        while ((Error = stdInput.readLine()) != null) {
            System.out.println(Error);
        }
}
}

Friday, July 18, 2014

Links to online utilities for faster development and testing

This post is just to have all useful links at one place

XPath:
To Test XPath 1.0 :             
           http://www.online-toolz.com/tools/xpath-editor.php
To Test Xpath 2.0
           http://videlibri.sourceforge.net/cgi-bin/xidelcgi

Javascript:
Check for errors in java script
           http://www.javascriptlint.com/online_lint.php
Javascript beautifier (beautify javascript or html)
           http://jsbeautifier.org/
JSON Beautify 
           http://jsonformat.com/#jsondataurllabel
              http://jsonformatter.curiousconcept.com/

Stylesheet:
CSS Beautify
           http://www.codebeautifier.com/#skip
Convert inline css to external css
           http://www.cssout.com/
Working with Javascript styling attributes
           http://www.comptechdoc.org/independent/web/cgi/javamanual/javastyle.html

JDBC Connection:
Working with JDBC Connections :
           http://www.herongyang.com/JDBC/Oracle-JDBC-Driver-Connection-URL.html

Extends Class provides several online tools (mentioned below):

Playgrounds:

API tools:
Random Data Generator:
Checkers:
XML tools:
JSON tools:
Converters / Encoders:
Minifiers:
Other:

Wednesday, July 16, 2014

Listing down unique tags in entire XML

I have a requirement here, I would try to keep it in very simple terms,

I have a raw information in the form of XML:

<MyFruits>
<Apple>23</Apple>
<Mango>12</Mango>
<Orange>10</Orange>
<Apple>19</Apple>
</MyFruits>

I want to get only unique fruits among them. (Apple,Mango and Orange)

Can anyone write an XPath to retrieve this?
Status : Archived

You can give your answers as comments

Hint : 
Had the XML be like below

<MyFruits>
<Apple>23</Apple>
<Apple>19</Apple>
<Mango>12</Mango>
<Orange>10</Orange>
</MyFruits>

This XPath would have worked 

//MyFruits/*[not(name(.)=name(following-sibling::*))]


Tuesday, July 15, 2014

Issue with groupbox setting of XForm with IE11

It's been a long time since my last post and I finally wanted to break the shackles. Running out of proper topics and falling short of time refrained myself to blog.

I thank my friends in sharing something,
---------------------------------------------------------------------------------------------------
Hi,

We faced one issue in our project while making it IE11 compatible. Below is the details :

In XForm if you are using Groupbox with Collapsibility - XForm does not render and gives following script error.




 
This error comes with the following config of the GroupBox.





   
 But, if we choose following config, the same xForm will work.

We can use the 2nd configuration and collapse the groupboxes in initDone event of xForm.
 --------------------------------------------------------------------------------------------------
Courtesy : Amit Kumar and Varsha Musku