/*=============================================================================*
* 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;
/**
*
* InsertRequest is a serializer/deserializer for the WS-ResourceProperties
* Insert operation, which is part of SetResourceProperties.
*
* @author Dan Jemiolo (danj)
*
*/
public class InsertRequest extends AbstractSetRequestComponent
{
//
// Used to lookup all exception messages
//
private static Messages _MESSAGES = MessagesFactory.get(InsertRequest.class);
//
// The name of this SetResourceProperties operation
//
public static final String OPERATION = "Insert";
public InsertRequest(Element request)
throws BaseFault
{
if (request == null)
throw new NullPointerException(_MESSAGES.get("NullRequestElement"));
Element[] children = XmlUtils.getAllElements(request);
if (children.length == 0)
throw new InvalidMessageFormatFault(_MESSAGES.get("NoPropertiesFoundInsert"));
//
// the WS-RP spec says that in an Insert request with multiple
// elements, all elements MUST have the same qualified name
//
QName firstQName = XmlUtils.getElementQName(children[0]);
Element[] matches = XmlUtils.getElements(request, firstQName);
if (matches.length != children.length)
throw new InvalidMessageFormatFault(_MESSAGES.get("NamesNotEqualInsert"));
setPropertyName(firstQName);
setValues(children);
}
public InsertRequest(QName qname)
{
this(qname, 1);
}
public InsertRequest(QName qname, int numberOfCopies)
{
this(qname, new Object[numberOfCopies]);
}
public InsertRequest(QName qname, Object value)
{
this(qname, new Object[]{ value });
}
public InsertRequest(QName qname, Object[] values)
{
if (qname == null)
throw new NullPointerException(_MESSAGES.get("NullQName"));
if (values == null)
throw new NullPointerException(_MESSAGES.get("NullValueArray"));
if (values.length == 0)
throw new IllegalArgumentException(_MESSAGES.get("EmptyValuesArray"));
setPropertyName(qname);
setValues(values);
}
public void execute(ResourcePropertyCollection properties)
throws BaseFault
{
if (properties == null)
throw new NullPointerException(_MESSAGES.get("NullProperties"));
properties.insertResourceProperty(getPropertyName(), getValues(), 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.INSERT_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;
}
}