is-collector/src/org/gcube/informationsystem/collector/stubs/testsuite/wsdaix/AddDocumentsTester.java

153 lines
6.5 KiB
Java

package org.gcube.informationsystem.collector.stubs.testsuite.wsdaix;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.axis.message.MessageElement;
import org.gcube.common.core.contexts.GCUBERemotePortTypeContext;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.utils.logging.GCUBEClientLog;
import org.gcube.informationsystem.collector.stubs.wsdai.DataResourceUnavailableFaultType;
import org.gcube.informationsystem.collector.stubs.wsdai.InvalidResourceNameFaultType;
import org.gcube.informationsystem.collector.stubs.wsdai.NotAuthorizedFaultType;
import org.gcube.informationsystem.collector.stubs.wsdai.ServiceBusyFaultType;
import org.gcube.informationsystem.collector.stubs.wsdaix.AddDocumentRequestWrapper;
import org.gcube.informationsystem.collector.stubs.wsdaix.AddDocumentResponseWrapperResponse;
import org.gcube.informationsystem.collector.stubs.wsdaix.AddDocumentsRequest;
import org.gcube.informationsystem.collector.stubs.wsdaix.AddDocumentsResponse;
import org.gcube.informationsystem.collector.stubs.wsdaix.InvalidCollectionNameFaultType;
import org.gcube.informationsystem.collector.stubs.wsdaix.XMLCollectionAccessPT;
import org.gcube.informationsystem.collector.stubs.wsdaix.XMLWrapperType;
import org.gcube.informationsystem.collector.stubs.wsdaix.service.WsdaixServiceAddressingLocator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import com.sun.xml.bind.StringInputStream;
/**
*
* Tester for XMLCollectionAccess::addDocuments
*
* @author Manuele Simi (ISTI-CNR)
*
*/
public class AddDocumentsTester {
private static GCUBEClientLog logger = new GCUBEClientLog(AddDocumentsTester.class);
/**
* @param args
* 0 - host
* 1 - port
* 2 - scope
* 3 - resourcename
* 4 - collection
* 5 - document name
* 6 - filename
*/
public static void main(String[] args) {
final String portTypeURI = "http://" + args[0] + ":" + args[1] + "/wsrf/services/gcube/informationsystem/collector/wsdaix/XMLCollectionAccess";
try {
AddDocumentsResponse r = addDocuments(portTypeURI,GCUBEScope.getScope(args[2]),
new org.apache.axis.types.URI (args[3]),
new org.apache.axis.types.URI(args[4]),
new String[] { args[5] },
new Document[] {TestDocuments.loadDocument(args[6])} );
logger.info("Number of response wrappers "+ r.getAddDocumentResponseWrapper().length );
String response = null;
for(int i=0;i<r.getAddDocumentResponseWrapper().length;i++) {
if ( r.getAddDocumentResponseWrapper()[i].getDocumentName().equals(args[5]) ) {
response = r.getAddDocumentResponseWrapper()[i].getResponse().toString();
logger.info(response);
}
}
logger.info("Add response " + AddDocumentResponseWrapperResponse._value1.toString());
//logger.info("Add response " + AddDocumentResponseWrapperResponse._value4.toString()); //,sugdenResponse);
} catch (Exception e ) {
logger.error(e.toString());
}
}
/**
* Execute the AddDocuments operation
*
* @param serviceURL the URL of the data service
* @param scope the target scope
* @param resourceName the abstract name of the data resource
* @param collectionURI The URI of the collection to which the documents should be added
* @param documentNames An array of document names to be added
* @param documents The corresponding content of each document
*/
public static AddDocumentsResponse addDocuments(String serviceURL, GCUBEScope scope,
org.apache.axis.types.URI resourceName,
org.apache.axis.types.URI collectionURI, String[] documentNames, Document[] documents)
throws DataResourceUnavailableFaultType, MalformedURLException, RemoteException,
ServiceBusyFaultType, InvalidResourceNameFaultType, InvalidCollectionNameFaultType, NotAuthorizedFaultType {
AddDocumentsRequest request = new AddDocumentsRequest();
request.setDataResourceAbstractName(resourceName);
AddDocumentRequestWrapper[] wrappers = new AddDocumentRequestWrapper[documents.length];
for(int i=0;i<wrappers.length;i++) {
wrappers[i] = new AddDocumentRequestWrapper();
wrappers[i].setDocumentName(documentNames[i]); //document name
XMLWrapperType wrapper = new XMLWrapperType();
MessageElement msgElement = new MessageElement(documents[i].getDocumentElement());
MessageElement msgElement2;
try {
msgElement2 = new MessageElement(createMetadata("Profile", "http://source",
600, "MyGroupKey", "MyKey", "MyEntryKey").getDocumentElement());
} catch (Exception e) {
logger.error("Unable to add document " + documentNames[i], e);
continue;
}
wrapper.set_any(new MessageElement[] {msgElement, msgElement2} );
wrappers[i].setData(wrapper);
}
request.setAddDocumentRequestWrapper(wrappers);
request.setCollectionName(collectionURI);
XMLCollectionAccessPT stubs = null;
try {
stubs = new WsdaixServiceAddressingLocator().getXMLCollectionAccessPTPort(new URL(serviceURL));
stubs = GCUBERemotePortTypeContext.getProxy(stubs, scope);
} catch (Exception e) {
logger.error("Failed to add documentes", e);
}
return stubs.addDocuments(request);
}
private static Document createMetadata(String type, String source,
Integer tt, String groupkey, String key, String entrykey)
throws SAXException, IOException, ParserConfigurationException {
StringBuilder builder = new StringBuilder();
builder.append("<Metadata>");
builder.append("<Type>").append(type).append("</Type>");
builder.append("<Source>").append(source).append("</Source>");
builder.append("<TerminationTime>").append(String.valueOf(tt)).append("</TerminationTime>");
builder.append("<GroupKey>").append(groupkey).append("</GroupKey>");
builder.append("<EntryKey>").append(entrykey).append("</EntryKey>");
builder.append("<Key>").append(key).append("</Key>");
builder.append("</Metadata>");
builder.toString();
return DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(new StringInputStream(builder.toString()));
}
}