Tuesday 4 March 2014

WeakHashMap Demo

package com.core.java;

import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

public class WeakHashMapDemo {

    public static void main(String[] args) {
/*        Hash table based implementation of the Map interface, with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use.
        The garbage collector may discard keys at any time, a WeakHashMap may behave as though an unknown thread is silently removing entries.       
        Collections.synchronizedMap(map) doesnt' not allow thread to remove entries from WeakHashMap.*/
       
        Map map =  new WeakHashMap();
        //Map map =  Collections.synchronizedMap(new WeakHashMap());
        Data someDataObject = new Data("foo");
        Data1 someDataObject1 = new Data1("foo");
        map.put(someDataObject, "");
        map.put(someDataObject1, "");
        System.out.println("map contains someDataObject ? " + map.containsKey(someDataObject));

        // make someDataObject elligible for garbage collection...
        someDataObject = null;

        for (int i = 0; i < 1000000; i++) {
            if (map.size() != 1) {
                System.out.println("At iteration " + i + " the map still holds the reference on someDataObject ::"+map.size());
            } else {
                System.out.println("somDataObject has finally been garbage collected at iteration " + i + ", hence the map is now empty ::"+map.size());
                break;
            }
        }
    }
}
class Data {
    String value;
    Data(String value) {
        this.value = value;
    }
}
class Data1 {
    String value;
    Data1(String value) {
        this.value = value;
    }
}


output:

At iteration 869 the map still holds the reference on someDataObject ::2
At iteration 870 the map still holds the reference on someDataObject ::2
At iteration 871 the map still holds the reference on someDataObject ::2
At iteration 872 the map still holds the reference on someDataObject ::2
somDataObject has finally been garbage collected at iteration 873, hence the map is now empty ::1

Thursday 20 February 2014

Reading XML

package com.rest;

import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class ReadRPCResponseXML {
    public static void main(String... str) {
        readXML("C:\\IBM\\WCDE_ENT70\\workspace\\RestTest\\req-response\\rpc-request.xml");
    }

    static Map readXML(String xml) {
        Map<String,List> specs = new HashMap<String,List>();
        try {
            File fXmlFile = new File(xml);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            Document doc = dBuilder.parse(is);

            doc.getDocumentElement().normalize();
            System.out.println("Root element :"+ doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName("Specifications");
            System.out.println("Total ::: "+nList.getLength());
            List<String> spec = null;
            for (int temp = 0; temp < nList.getLength(); temp++) {
                spec = new ArrayList<String>();
                Node nNode = nList.item(temp);
                NodeList list = nNode.getChildNodes();
              
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    String publicname = eElement.getAttribute("publicname");
                    if("TECHSPEC".equals(publicname) || "FEATURES".equals(publicname) || "OVERVIEW".equals(publicname)){
                        System.out.println("Specifications : "+ eElement.getAttribute("publicname"));
                        System.out.println("----------------------------"+list.getLength());
                        for (int temp1 = 0; temp1 < list.getLength(); temp1++) {
                            Node nNode1 = list.item(temp1);
                            if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
                                Element eElement1 = (Element) nNode1;
                                System.out.println(eElement1.getAttribute("label")+"-->"+eElement1.getElementsByTagName("Value").item(0).getChildNodes().item(0).getNodeValue().trim());
                                spec.add(eElement1.getAttribute("label")+" : "+eElement1.getElementsByTagName("Value").item(0).getChildNodes().item(0).getNodeValue().trim());
                            }
                        }
                        specs.put(eElement.getAttribute("publicname"), spec);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
      
        return specs;
      
    }
}

/*
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
    <queryResponse xmlns="http://service.asl.wsrpc.hp.com">
        <RichContent>
            <Specifications groupdisplayorder="0" publicname="ENLARGEDIMAGE"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="1"
                    label="Photo (product photo, jpg, 400x400)" languagecode="en"
                    specgroupdisplayname="" specgroupdisplayorder="1"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">http://h10010.www1.hp.com/wwpc/images/emea/hp-z400-workstation_400x400.jpg
                    </Value>
                </Specification>
            </Specifications>
            <Specifications groupdisplayorder="0" publicname="FEATURES"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="51"
                    label="AP operation modes" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">&lt;p /></Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Features" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="GH" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Features</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="52"
                    label="Footnote [01]" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">&lt;span class="small">&lt;i>&lt;sup>1&lt;/sup>
                        Quad-Core is designed to improve performance of multithreaded
                        software products and hardware-aware multitasking operating
                        systems and may require appropriate operating system software for
                        full benefits; check with software provider to determine
                        suitability; not all customers or software applications will
                        necessarily benefit from use of these technologies. 64-bit
                        computing on Intel architecture requires a computer system with a
                        processor, chipset, BIOS, operating system, device drivers and
                        applications enabled for Intel® 64 architecture. Processors will
                        not operate (including 32-bit operation) without an Intel 64
                        architecture-enabled BIOS. Performance will vary depending on
                        your hardware and software configurations. See
                        www.intel.com/info/em64t for more information&lt;/i>&lt;/span>
                    </Value>
                </Specification>
                <Specification countrycode="uk" displayorder="4"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Work smarter: run more tasks, processes and
                        applications simultaneously with a workstation that brings
                        together and optimizes the latest processors, memory, graphics
                        and storage technologies.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="11"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Updated design: with a smart new chassis, quiet
                        acoustics and optional liquid cooling that makes the system even
                        quieter at heavy loads, the HP Z400 showcases HP
                        innovation—inside and out.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="18"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Designed with the environment in mind: minimize
                        power and cooling costs with ENERGY STAR® qualified systems
                        featuring 85% efficient power supplies and an HP-developed
                        energy-saving feature that helps lower energy consumption in off
                        mode.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="25"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Do more every minute: take on bigger challenges
                        with the new Intel® QuickPath Interconnect architecture and your
                        choice of the latest dual- or quad-core Intel® Xeon® processors
                        &lt;sup>1&lt;/sup>.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="32"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Latest graphics: drive four 3D or six 2D displays
                        with the newest professional graphics options from ATI and
                        NVIDIA.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="39"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Simple to service: the tool-less chassis and
                        uncluttered internal design simplify access and service for
                        adding or changing components quickly.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Key selling point MDA" languagecode="en"
                    specgroupdisplayname="" specgroupdisplayorder="1"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SH" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Windows 8 Pro or other operating systems
                        available&lt;p /></Value>
                </Specification>
            </Specifications>
            <Specifications groupdisplayorder="0" publicname="LARGEIMAGE"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="1"
                    label="Photo (product photo, jpg, 190x170)" languagecode="en"
                    specgroupdisplayname="" specgroupdisplayorder="1"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">http://h10010.www1.hp.com/wwpc/images/emea/hp-z400-workstation_190x170.jpg
                    </Value>
                </Specification>
            </Specifications>
            <Specifications groupdisplayorder="0" publicname="OVERVIEW"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="1"
                    label="Overview" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="GH" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Overview</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Overview" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">With its revolutionary new architecture and bold
                        new industrial design, the HP Z400 Workstation helps you
                        accomplish more with every minute of your time and every dollar
                        of your investment.</Value>
                </Specification>
            </Specifications>
            <Specifications groupdisplayorder="0" publicname="TAGLINE"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="1"
                    label="Legal tagline" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">HP recommends Windows.</Value>
                </Specification>
            </Specifications>
            <Specifications groupdisplayorder="0" publicname="TECHSPEC"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="2"
                    label="Audio" languagecode="en" specgroupdisplayname="Communications"
                    specgroupdisplayorder="6" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Integrated High Definition Realtek ALC262 Audio,
                        optional Creative X-Fi Titanium PCIe Audio Card, optional HP Thin
                        USB Powered Speakers</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="4"
                    label="Chipset" languagecode="en" specgroupdisplayname="System features"
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Intel® X58 Express</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Display" languagecode="en" specgroupdisplayname="Graphics"
                    specgroupdisplayorder="4" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">HP LP1965 19-inch LCD Monitor, HP LP2065 20-inch
                        LCD Monitor, HP LP2275w 22-inch Widescreen LCD Monitor, HP
                        LP2475w 24-inch Widescreen LCD Monitor, HP DreamColor LP2480zx
                        Professional Display (24-inch widescreen), HP LP3065 30-inch
                        Widescreen LCD Monitor (all sold separately)</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Energy efficiency" languagecode="en" specgroupdisplayname="Power"
                    specgroupdisplayorder="7" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">ENERGY STAR®, EPEAT Goldâ„¢, 80PLUS Bronze Power
                        Supply</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="External drive bays" languagecode="en"
                    specgroupdisplayname="Storage" specgroupdisplayorder="3"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">3 external 5.25" bays</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="6"
                    label="Form factor" languagecode="en" specgroupdisplayname="System features"
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Convertible minitower</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Graphics" languagecode="en" specgroupdisplayname="Graphics"
                    specgroupdisplayorder="4" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">No integrated Graphic Card</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="5"
                    label="Hard drive" languagecode="en" specgroupdisplayname="Storage"
                    specgroupdisplayorder="3" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">500 GB 7200 rpm SATA NCQ</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Internal drive bays" languagecode="en"
                    specgroupdisplayname="Storage" specgroupdisplayorder="3"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">2 internal 3.5" bays (up to 4 with converter)
                    </Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Memory slots" languagecode="en" specgroupdisplayname="Memory"
                    specgroupdisplayorder="2" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">6 DIMM slots</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Memory, standard" languagecode="en" specgroupdisplayname="Memory"
                    specgroupdisplayorder="2" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">4 GB 1333 MHz DDR3 ECC Unbuffered RAM </Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Minimum dimensions (W x D x H)" languagecode="en"
                    specgroupdisplayname="Dimensions and weight"
                    specgroupdisplayorder="8" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">45.53 x 45.02 x 16.79 cm</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Network interface" languagecode="en" specgroupdisplayname="Communications"
                    specgroupdisplayorder="6" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Integrated Broadcom 5764 10/100/1000 PCIe LAN,
                        optional Broadcom NIC, optional Intel NIC</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Operating system" languagecode="en" specgroupdisplayname="System features"
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Windows XP Professional 32-bit (available through
                        downgrade rights from Genuine Windows® 7 Professional)</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="8"
                    label="Optical drive" languagecode="en" specgroupdisplayname="Storage"
                    specgroupdisplayorder="3" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">SATA 16X DVD+/-RW SuperMulti LightScribe</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Ports" languagecode="en" specgroupdisplayname="Expansion features"
                    specgroupdisplayorder="5" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">12 USB 2.0&lt;br> 1 IEEE 1394a&lt;br> 1 audio
                        in&lt;br> 2 audio out&lt;br> 2 microphone in&lt;br> 2 PS/2&lt;br>
                        1 RJ-45&lt;br/> 2 USB 3.0 (via optional PCIe card); 1 serial
                        (optional), 2 IEEE 1394a or 1394b (via optional PCI card); 4
                        internal USB 2.0 available by 2 separate 2x5 headers</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Power" languagecode="en" specgroupdisplayname="Power"
                    specgroupdisplayorder="7" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">475 watts wide-ranging, active Power Factor
                        Correction, 85% efficient Power Supply</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="3"
                    label="Processor" languagecode="en" specgroupdisplayname="System features"
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">1 Intel® Xeon® Quad-Core Processor W3520 (2.66
                        GHz, 8 MB cache, 1066 MHz memory)</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Security management" languagecode="en"
                    specgroupdisplayname="What's included" specgroupdisplayorder="9"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">HP Solenoid Hood Lock/Sensor Kit, Kensington
                        Security Cable &amp; Lock</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Slots" languagecode="en" specgroupdisplayname="Expansion features"
                    specgroupdisplayorder="5" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">2 PCI Express Gen2 x16&lt;br> 1 PCI Express Gen2
                        (x8 mechanically, x4 electrically)&lt;br> 1 PCI Express Gen1 (x8
                        mechanically, x4 electrically)&lt;br> 2 PCI&lt;br> 1 22-in-1
                        Media Card Reader (optional)&lt;br/> The PCIe x8 connectors are
                        open ended, allowing a PCIe x16 card to be seated in the slot.
                    </Value>
                </Specification>
                <Specification countrycode="uk" displayorder="7"
                    label="Storage controller" languagecode="en" specgroupdisplayname="Storage"
                    specgroupdisplayorder="3" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Integrated SATA 3.0 Gb/s with RAID 0, 1, 10, 5
                        support. LSI 8888ELP 8-port SAS RAID (optional); LSI MegaRAID
                        9260-8i SAS 6Gb/s ROC RAID and iBBU08 Battery Backup Unit;
                    </Value>
                </Specification>
                <Specification countrycode="uk" displayorder="3"
                    label="Warranty" languagecode="en" specgroupdisplayname="What's included"
                    specgroupdisplayorder="9" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">3-year worldwide parts, labour and next business
                        day onsite service&lt;br> Terms and conditions may vary,
                        restrictions apply</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Weight" languagecode="en" specgroupdisplayname="Dimensions and weight"
                    specgroupdisplayorder="8" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Starting at 13.5 kg</Value>
                </Specification>
            </Specifications>
            <ImageUrls xmlns="http://common.wsrpc.hp.com">
                <ImageUrl oid="4080932" sku="KK581ET" xmlns="http://common.wsrpc.hp.com">http://h10010.www1.hp.com/wwpc/images/emea/hp-z400-workstation_70x100.jpg
                </ImageUrl>
            </ImageUrls>
        </RichContent>
    </queryResponse>
</soap:Body>
</soap:Envelope>
*/

UserNameToken - SOAP request

package com.rest;

import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import org.apache.axis.encoding.Base64;

public class WsrpcClientCall {
    //http://h10077.www1.hp.com/wsrpc/services/WsrpcServiceXfireImpl?wsdl
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
       
        String endPoint = "http://h10077.www1.hp.com/wsrpc/services/WsrpcServiceXfireImpl";
        String xmlns = "http://common.wsrpc.hp.com";
       
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage();
        message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
        message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");

        SOAPPart soapPart = message.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();

       
        //=======================================USERNAMETOKEN - START====================================================
       
       
        String pass = "password";
        String user = "username";
        WsrpcClientCall call = new WsrpcClientCall();
        String nonceString = call.getNonce();


        String dig=calculatePasswordDigest(nonceString, new Date().toGMTString(), pass);


        //SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
        SOAPHeader header = envelope.getHeader();

        SOAPElement security =
        header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");


        SOAPElement usernameToken =
        security.addChildElement("UsernameToken", "wsse");


        SOAPElement username =
        usernameToken.addChildElement("Username", "wsse");
        username.addTextNode(user);

        SOAPElement password =
        usernameToken.addChildElement("Password", "wsse");
        password
                .setAttribute(
                        "Type",
                        "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
        password.addTextNode(dig);

        SOAPElement nonce =
        usernameToken.addChildElement("Nonce", "wsse");
        nonce.addTextNode(Base64.encode(hexEncode(nonceString).getBytes()));

        SOAPElement created = usernameToken.addChildElement("Created", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
        created.addTextNode(new Date().toString());
       
       
        //==============================================USERNAMETOKEN - END=================================================
       
       
        SOAPBody mbody = message.getSOAPBody();
       
      
       
        SOAPElement query =  mbody.addChildElement("query", "ns0", xmlns);
        SOAPElement checkAccEl =  query.addChildElement("QueryRequest", "ns0", xmlns);
       
        checkAccEl.addChildElement("countryCode", "ns0", xmlns).addTextNode("UK");
        checkAccEl.addChildElement("languageCode", "ns0", xmlns).addTextNode("en");
        checkAccEl.addChildElement("languageFallback", "ns0", xmlns).addTextNode("false");
        checkAccEl.addChildElement("layoutCode", "ns0", xmlns).addTextNode("overview");
        checkAccEl.addChildElement("skuList", "ns0", xmlns).addTextNode("KK581ET");
       
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        message.writeTo(out);
       
        String strMsg = new String(out.toByteArray());
        System.out.println(strMsg);
       
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();
       
        URL url = new URL(endPoint);
        SOAPMessage soapResponse = soapConnection.call(message, url);
       
        System.out.println(soapResponse);

        ByteArrayOutputStream resultOut = new ByteArrayOutputStream();
        soapResponse.writeTo(resultOut);
       
        String responsexml = new String(resultOut.toByteArray());
        Map<String,List> specs = ReadRPCResponseXML.readXML(responsexml);
        System.out.println("=======================================================");
        for (Map.Entry<String, List> entry : specs.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        System.out.println("=======================================================");
       
        System.out.println(specs.get("TECHSPEC"));
        //readXML(finalReslt);
        //System.out.println("===="+responsexml);
    }
    private String getNonce() {
        // TODO Auto-generated method stub
        return Integer.toString(this.hashCode());
    }
    private static String calculatePasswordDigest(String nonce, String created, String password) {
        String encoded = null;
        try {
            String pass = hexEncode(nonce) + created + password;
            MessageDigest md = MessageDigest.getInstance( "SHA1" );
            md.update( pass.getBytes() );
            byte[] encodedPassword = md.digest();
            encoded = Base64.encode(encodedPassword);
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }

        return encoded;
    }

    private static String hexEncode(String in) {
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < (in.length() - 2) + 1; i = i + 2) {
            int c = Integer.parseInt(in.substring(i, i + 2), 16);
            char chr = (char) c;
            sb.append(chr);
        }
        return sb.toString();
    }
}

/*
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header>
        <wsse:Security
            xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken>
                <wsse:Username>username</wsse:Username>
                <wsse:Password
                    Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">LgXEwKzGfYeZayoLW2H1s6AXY+c=</wsse:Password>
                <wsse:Nonce>MAI/cA==</wsse:Nonce>
                <wsu:Created
                    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">Thu Feb 20 16:00:51 IST 2014</wsu:Created>
            </wsse:UsernameToken>
        </wsse:Security>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns0:query xmlns:ns0="http://common.wsrpc.hp.com">
            <ns0:QueryRequest>
                <ns0:countryCode>UK</ns0:countryCode>
                <ns0:languageCode>en</ns0:languageCode>
                <ns0:languageFallback>false</ns0:languageFallback>
                <ns0:layoutCode>overview</ns0:layoutCode>
                <ns0:skuList>KK581ET</ns0:skuList>
            </ns0:QueryRequest>
        </ns0:query>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/