/**
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
* DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
* OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
* OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
* LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed, licensed or
* intended for use in the design, construction, operation or
* maintenance of any nuclear facility.
*/
package com.abccircuits.client;
import com.sun.enterprise.security.SecurityContext;
import javax.security.auth.Subject;
import javax.naming.InitialContext;
import javax.naming.Context;
import java.util.*;
import javax.xml.rpc.*;
/**
* This class WebservicesClientWithJSR196 is used by the ABC Circuits
* client portal and makes a web service request to the CMTChips web service.
* The configured JSR 196 Client provider uses the user's credential that are
* set in the Subject's private credentials and will the liberty protocols
* to retrieve the user's asserted security attributes by making a discovery
* service query. The result in security assertion will be included in the
* outbound SOAP message request to the CMTChips purchasing webservice.
*/
public class WebservicesClientWithJSR196 {
/**
* Default constructor
*/
public WebservicesClientWithJSR196() {}
/**
* Sends the purchase request to the CMTChips webservice for the
* requested data.
* @param user username that needs to be asserted.
* @param pwd user's password.
* @param requestData requested data by the user.
* @return PurchaseOrderStatus.
*/
public PurchaseOrderStatus sendPurchaseRequest(
String user, String pwd, Map requestData) {
try {
// Populate the subject's private credentials with the given
// user name and the password for the consumption of JSR196
// Client Provider.
SecurityContext iSC = SecurityContext.getCurrent();
final Subject subject = iSC.getSubject();
Map credentials = getCredentialMap(user, pwd);
subject.getPrivateCredentials().add(credentials);
//get the context of the CMTChips purchase web application
Context ic = new InitialContext();
CMTChipsPurchase ccp = (CMTChipsPurchase)ic.lookup(
"java:comp/env/service/CMTPO");
// get the cmt chips endpoint.
PurchaseOrderPortType port = ccp.getPurchaseOrderPortTypePort();
// Create a purchase order request using the data populated by
// the application GUI.
PurchaseOrder po = createPurchaseOrder(requestData);
// Make a request and return the result.
return port.requestPurchase(po);
} catch(Exception ex) {
System.out.println("Failure in sending the request.");
ex.printStackTrace();
}
return null;
}
/**
* Returns the credential map that will be set in the subject.
*/
private Map getCredentialMap(String user, String pwd) {
Map map = new HashMap();
map.put("username", user);
map.put("password", pwd);
return map;
}
/**
* Creates a purchase order by using the data supplied by the GUI.
*/
private PurchaseOrder createPurchaseOrder(Map data) {
String bAddrStreet = (String)data.get("bStreet");
String bAddrCity = (String)data.get("bCity");
String bAddrState = (String)data.get("bState");
String bAddrZip = (String)data.get("bZip");
POAddress bAddr = new POAddress();
bAddr.setStreet(bAddrStreet);
bAddr.setCity(bAddrCity);
bAddr.setState(bAddrState);
bAddr.setZip(bAddrZip);
String sAddrStreet = (String)data.get("sStreet");
String sAddrCity = (String)data.get("sCity");
String sAddrState = (String)data.get("sState");
String sAddrZip = (String)data.get("sZip");
POAddress sAddr = new POAddress();
sAddr.setStreet(sAddrStreet);
sAddr.setCity(sAddrCity);
sAddr.setState(sAddrState);
sAddr.setZip(sAddrZip);
POLineItem item1 = new POLineItem();
String itemName1 = (String)data.get("itemName1");
String itemQuantity1 = (String)data.get("itemQuantity1");
String itemPrice1 = (String)data.get("itemPrice1");
item1.setItemName(itemName1);
item1.setPrice(itemPrice1);
item1.setQuantity(itemQuantity1);
POLineItem item2 = new POLineItem();
String itemName2 = (String)data.get("itemName2");
String itemQuantity2 = (String)data.get("itemQuantity2");
String itemPrice2 = (String)data.get("itemPrice2");
item2.setItemName(itemName2);
item2.setPrice(itemPrice2);
item2.setQuantity(itemQuantity2);
POLineItem item3 = new POLineItem();
String itemName3 = (String)data.get("itemName3");
String itemQuantity3 = (String)data.get("itemQuantity3");
String itemPrice3 = (String)data.get("itemPrice3");
item3.setItemName(itemName3);
item3.setPrice(itemPrice3);
item3.setQuantity(itemQuantity3);
List items = new ArrayList();
items.add(item1);
items.add(item2);
items.add(item3);
PurchaseOrder po = new PurchaseOrder();
po.setBillAddress(bAddr);
po.setShipAddress(sAddr);
po.setLineItems(items);
return po;
}
}