/*=============================================================================*
* 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.properties.set.impl;
import javax.xml.namespace.QName;
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.util.xml.XmlUtils;
import org.apache.muse.ws.resource.basefaults.BaseFault;
import org.apache.muse.ws.resource.ext.faults.InvalidMessageFormatFault;
import org.apache.muse.ws.resource.properties.ResourcePropertyCollection;
import org.apache.muse.ws.resource.properties.WsrpConstants;
/**
*
* UpdateRequest is a serializer/deserializer for the WS-ResourceProperties
* Update operation, which is part of SetResourceProperties.
*
* @author Dan Jemiolo (danj)
*
*/
public class UpdateRequest extends AbstractSetRequestComponent
{
//
// Used to lookup all exception messages
//
private static Messages _MESSAGES = MessagesFactory.get(UpdateRequest.class);
//
// The name of this SetResourceProperties operation
//
public static final String OPERATION = "Update";
public UpdateRequest(Element request)
throws BaseFault
{
if (request == null)
throw new NullPointerException(_MESSAGES.get("NullRequestElement"));
Element[] properties = XmlUtils.getAllElements(request);
if (properties.length == 0)
throw new InvalidMessageFormatFault(_MESSAGES.get("NoPropertiesFoundUpdate"));
setPropertyName(XmlUtils.getElementQName(properties[0]));
setValues(properties);
}
public UpdateRequest(QName qname, Object value)
{
this(qname, new Object[]{ value });
}
public UpdateRequest(QName qname, Object[] values)
{
if (qname == null)
throw new NullPointerException(_MESSAGES.get("NullQName"));
setPropertyName(qname);
setValues(values);
}
public void execute(ResourcePropertyCollection properties)
throws BaseFault
{
if (properties == null)
throw new NullPointerException(_MESSAGES.get("NullProperties"));
Object[] values = getValues();
properties.updateResourceProperty(getPropertyName(), values, getSecurityToken());
}
public String toString()
{
return XmlUtils.toString(toXML(), false);
}
public Element toXML()
{
return toXML(XmlUtils.EMPTY_DOC);
}
public Element toXML(Document doc)
{
if (doc == null)
throw new NullPointerException(_MESSAGES.get("NullDocument"));
Element root = XmlUtils.createElement(doc, WsrpConstants.UPDATE_QNAME);
Element[] values = getValues();
for (int n = 0; n < values.length; ++n)
{
Element next = (Element)doc.importNode(values[n], true);
root.appendChild(next);
}
return root;
}
}