/*=============================================================================*
* 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.remote;
import java.text.ParseException;
import java.util.Date;
import javax.xml.namespace.QName;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.apache.muse.core.AbstractResourceClient;
import org.apache.muse.core.Environment;
import org.apache.muse.ws.addressing.EndpointReference;
import org.apache.muse.ws.resource.lifetime.WsrlConstants;
import org.apache.muse.ws.resource.lifetime.impl.SetTerminationTime;
import org.apache.muse.ws.resource.lifetime.impl.SetTerminationTimeResponse;
import org.apache.muse.ws.resource.properties.WsrpConstants;
import org.apache.muse.ws.resource.properties.get.impl.GetMultipleRequest;
import org.apache.muse.ws.resource.properties.get.impl.GetRequest;
import org.apache.muse.ws.resource.properties.impl.WsrpUtils;
import org.apache.muse.ws.resource.properties.query.impl.QueryRequest;
import org.apache.muse.ws.resource.properties.set.SetOperationFactory;
import org.apache.muse.ws.resource.properties.set.SetRequest;
import org.apache.muse.ws.resource.properties.set.impl.SimpleSetOperationFactory;
import org.apache.muse.ws.addressing.soap.SoapClient;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.util.xml.XmlUtils;
/**
*
* WsResourceClient is a web service client that allows users to invoke standard
* WSRF operations without writing any XML plumbing code. This class includes
* all of the operations from WSRF, but this does <b>not</b> mean that all Muse
* resource types implement all WSRF operations; rather, it is more convenient
* to have one client class since all WSRF operations are defined as separate
* capabilities (which would result in one client per operation); if users need
* more detailed information about what operations are supported by a resource,
* they can use the WS-MetadataExchange client in combination with the Muse
* ResourceInspector tool.
*
* @author Dan Jemiolo (danj)
*
*/
public class WsResourceClient extends AbstractResourceClient
{
//
// used to parse WSRP SetResourceProperties requests
//
private static final SetOperationFactory _SET_FACTORY = new SimpleSetOperationFactory();
public WsResourceClient(EndpointReference destination)
{
super(destination);
}
public WsResourceClient(EndpointReference destination,
EndpointReference source)
{
super(destination, source);
}
public WsResourceClient(EndpointReference destination,
EndpointReference source,
Environment environment)
{
super(destination, source, environment);
}
public WsResourceClient(EndpointReference destination,
EndpointReference source,
SoapClient soapClient)
{
super(destination, source, soapClient);
}
public void deleteResourceProperty(QName qname)
throws SoapFault
{
SetRequest set = _SET_FACTORY.createDelete(qname);
setResourceProperties(set);
}
public void destroy()
throws SoapFault
{
Element body = XmlUtils.createElement(WsrlConstants.DESTROY_QNAME);
invoke(WsrlConstants.DESTROY_URI, body);
}
public Element[] getMultipleResourceProperties(QName[] qnames)
throws SoapFault
{
GetMultipleRequest get = new GetMultipleRequest(qnames);
Element response = invoke(WsrpConstants.GET_MULTIPLE_PROPERTIES_URI, get.toXML());
return XmlUtils.getAllElements(response);
}
/**
*
* This is a convenience method that calls getResourceProperty() and then
* parses the XML results into POJOs of the given type using the Muse
* serializer framework.
*
*/
public Object getPropertyAsObject(QName qname, Class type)
throws SoapFault
{
Element[] properties = getResourceProperty(qname);
return WsrpUtils.convertToObjects(properties, type);
}
public Element[] getResourceProperty(QName qname)
throws SoapFault
{
GetRequest get = new GetRequest(qname);
Element response = invoke(WsrpConstants.GET_RESOURCE_PROPERTY_URI, get.toXML());
return XmlUtils.getAllElements(response);
}
public Element getResourcePropertyDocument()
throws SoapFault
{
Element request = XmlUtils.createElement(WsrpConstants.GET_DOCUMENT_QNAME);
Element response = invoke(WsrpConstants.GET_RP_DOCUMENT_URI, request);
return XmlUtils.getFirstElement(response);
}
public void insertResourceProperty(QName qname, Object[] values)
throws SoapFault
{
SetRequest set = _SET_FACTORY.createInsert(qname, values);
setResourceProperties(set);
}
public Element putResourcePropertyDocument(Element wsrpDoc)
throws SoapFault
{
Element request = XmlUtils.createElement(XmlUtils.EMPTY_DOC, WsrpConstants.PUT_DOCUMENT_QNAME, wsrpDoc, false);
Element response = invoke(WsrpConstants.PUT_RP_DOCUMENT_URI, request);
return XmlUtils.getFirstElement(response);
}
public Node[] queryResourceProperties(String query, String dialect)
throws SoapFault
{
QueryRequest request = new QueryRequest(query, dialect);
Element response = invoke(WsrpConstants.QUERY_RESOURCE_PROPERTIES_URI, request.toXML());
NodeList children = response.getChildNodes();
return XmlUtils.convertToArray(children);
}
public void setResourceProperties(SetRequest request)
throws SoapFault
{
invoke(WsrpConstants.SET_RESOURCE_PROPERTIES_URI, request.toXML());
}
public Date setTerminationTime(Date time)
throws SoapFault
{
SetTerminationTime set = new SetTerminationTime(time);
Element response = invoke(WsrlConstants.SET_TERMINATION_URI, set.toXML());
try
{
SetTerminationTimeResponse setResponse =
new SetTerminationTimeResponse(response);
return setResponse.getTerminationTime();
}
catch (ParseException error)
{
throw new SoapFault(error.getMessage(), error);
}
}
public void updateResourceProperty(QName qname, Object[] values)
throws SoapFault
{
SetRequest set = _SET_FACTORY.createUpdate(qname, values);
setResourceProperties(set);
}
}