/*=============================================================================*
* Copyright 2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*=============================================================================*/
package org.apache.muse.ws.resource.sg.impl;
import java.text.ParseException;
import java.util.Date;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.muse.util.messages.Messages;
import org.apache.muse.util.messages.MessagesFactory;
import org.apache.muse.ws.addressing.EndpointReference;
import org.apache.muse.ws.resource.ext.faults.InvalidMessageFormatFault;
import org.apache.muse.ws.resource.remote.WsResourceClient;
import org.apache.muse.ws.resource.sg.WssgConstants;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.util.xml.XmlSerializable;
import org.apache.muse.util.xml.XmlUtils;
/**
*
* AddRequest is a serializer/deserializer for the WS-ServiceGroup Add
* operation's request content.
*
* @author Dan Jemiolo (danj)
*
*/
public class AddRequest implements XmlSerializable
{
//
// Used to look up all exception messages
//
private static Messages _MESSAGES = MessagesFactory.get(AddRequest.class);
private Element _content = null;
private EndpointReference _memberEPR = null;
private Date _termination = null;
public AddRequest(Element xml)
throws InvalidMessageFormatFault
{
if (xml == null)
throw new NullPointerException(_MESSAGES.get("NullElement"));
Element eprXML = XmlUtils.getElement(xml, WssgConstants.MEMBER_EPR_QNAME);
if (eprXML == null)
throw new InvalidMessageFormatFault(_MESSAGES.get("NoMemberEPR"));
try
{
_memberEPR = new EndpointReference(eprXML);
}
catch (SoapFault error)
{
throw new InvalidMessageFormatFault(error);
}
_content = XmlUtils.getElement(xml, WssgConstants.CONTENT_QNAME);
Element timeXML = XmlUtils.getElement(xml, WssgConstants.INIT_TERMINATION_QNAME);
try
{
if (timeXML != null)
_termination = XmlUtils.getDate(timeXML);
}
catch (ParseException error)
{
throw new InvalidMessageFormatFault(error);
}
}
public AddRequest(EndpointReference memberEPR, Date termination)
{
if (memberEPR == null)
throw new NullPointerException(_MESSAGES.get("NullMemberEPR"));
_memberEPR = memberEPR;
_termination = termination;
}
public Element getContent()
{
return _content;
}
public EndpointReference getMemberEPR()
{
return _memberEPR;
}
public Date getTerminationTime()
{
return _termination;
}
public Element toXML()
{
return toXML(XmlUtils.EMPTY_DOC);
}
public Element toXML(Document factory)
{
if (factory == null)
throw new NullPointerException(_MESSAGES.get("NullDocument"));
Element root = XmlUtils.createElement(factory, WssgConstants.ADD_QNAME);
EndpointReference epr = getMemberEPR();
XmlUtils.setElement(root, WssgConstants.MEMBER_EPR_QNAME, epr.toXML());
WsResourceClient member = new WsResourceClient(epr);
Element content = getContent();
//
// if no content recorded, retrieve content values at runtime using
// remote client
//
try
{
if (content == null)
{
Element[] results = member.getResourceProperty(WssgConstants.CONTENT_QNAME);
content = results[0];
}
}
catch (SoapFault error)
{
throw new RuntimeException(error.getMessage(), error);
}
content = (Element)factory.importNode(content, true);
root.appendChild(content);
Date time = getTerminationTime();
if (time != null)
XmlUtils.setElement(root, WssgConstants.INIT_TERMINATION_QNAME, time);
return root;
}
}