Wednesday, January 11, 2012

JAXB

old pieces of code:

public static com.harrods.hrc.ws.client.mapping.getDetails.Member parseGetDetailsResult(String resultFromWS)
throws JAXBException
{
if ( resultFromWS == null ) return null;
resultFromWS = "" + resultFromWS + "";
JAXBContext jaxbContext = JAXBContext.newInstance( "com.harrods.hrc.ws.client.mapping.getDetails" );
ByteArrayInputStream input = new ByteArrayInputStream (resultFromWS.getBytes());
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
com.harrods.hrc.ws.client.mapping.getDetails.Member getDetailsResult =
(com.harrods.hrc.ws.client.mapping.getDetails.Member)
unmarshaller.unmarshal(input);
return getDetailsResult;
}

//another impl
if ( memberAsString == null ) throw new Exception("member is null");
memberAsString = "" + memberAsString + "";

JAXBContext jaxbContext = JAXBContext.newInstance( com.harrods.hrc.ws.client.mapping.enrol.response.Member.class );

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader stringReader = new StringReader(memberAsString);
return
(com.harrods.hrc.ws.client.mapping.enrol.response.Member)
unmarshaller.unmarshal(stringReader);


//
JAXBContext jaxbContext = JAXBContext.newInstance(
com.harrods.hrc.ws.client.mapping.enrol.request.RequestData.class
);

Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING , "UTF-8");
//setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

StringWriter stringWriter = new StringWriter();
marshaller.marshal(enrolRequestData, stringWriter);
return stringWriter.toString();


//
EndpointReference targetEPR = new EndpointReference(
wsEndpoint
);

Options options = new Options();
//GRIPS MX Web Service endpoint
options.setTo(targetEPR);
// Increase the time out when sending large attachments
options.setTimeOutInMilliSeconds(timeout);
options.setTo(targetEPR);
//GRIPS MX Web Service IssueCheque operation
options.setAction("http://jetsystems.com/GRIPSMX/IssueCheque");

ServiceClient sender = new ServiceClient(); //default context
sender.setOptions(options);
OperationClient mepClient = sender
.createClient(ServiceClient.ANON_OUT_IN_OP);

MessageContext messageContext = new MessageContext();

SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope();

SAXOMBuilder saxomBuilder = new SAXOMBuilder();
JAXBContext jaxbContext =
JAXBContext.newInstance("com.jetsystems.gripsmx.ws");
Marshaller marshaller = jaxbContext.createMarshaller();

marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
new NamespacePrefixMapperImpl());

com.jetsystems.gripsmx.ws.ChequeRequest chequeRequest =
generateChequeRequest(
country,
shop,
tillId,
operatorId,
posBasket,
dataEntry,
basketWsProperties
);
marshaller.marshal(chequeRequest, saxomBuilder);
OMElement issueChequeRequest = saxomBuilder.getRootElement();
issueChequeRequest.setLocalName("issueChequeRequest");

soapEnvelope.getBody().addChild(issueChequeRequest);

messageContext.setEnvelope(soapEnvelope);

mepClient.addMessageContext(messageContext);
mepClient.execute(true);
MessageContext response = mepClient
.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
SOAPBody soapBody = response.getEnvelope().getBody();

//try to unmarshal the response
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

JAXBElement jaxbElement =
(JAXBElement)unmarshaller.unmarshal(
soapBody.getFirstElement().getXMLStreamReader());
com.jetsystems.gripsmx.ws.Cheque cheque =
(com.jetsystems.gripsmx.ws.Cheque)
jaxbElement.getValue();

logger.debug("issued cheque = " + cheque);
return cheque;

//

No comments:

Post a Comment