if (queryHandler == null) {
throw DBWSException.couldNotLocateQueryForSession(name,
xrService.getORSession().getName());
}
queryHandler.initialize(xrService, this);
Session oxSession = xrService.getOXSession();
QName resultType = result == null ? null : result.getType();
addSimpleXMLFormatModelDescriptor(xrService);
addValueObjectDescriptor(xrService);
if (resultType == null) {
if (isAttachment()) {
Attachment attachment = result.getAttachment();
XMLDescriptor descriptor =
(XMLDescriptor)oxSession.getProject().getClassDescriptor(DataHandler.class);
if (descriptor == null) {
descriptor = new XMLDescriptor();
descriptor.setAlias("DataHandler");
descriptor.setJavaClass(DataHandler.class);
descriptor.setInstantiationPolicy(
this.new DataHandlerInstantiationPolicy(attachment.getMimeType()));
XMLBinaryDataMapping mapping = new XMLBinaryDataMapping();
mapping.setAttributeName("results");
mapping.setAttributeAccessor(new AttributeAccessor() {
@Override
public Object getAttributeValueFromObject(Object object)
throws DescriptorException {
Object result = null;
DataHandler dataHandler = (DataHandler)object;
try {
result = dataHandler.getContent();
if (result instanceof InputStream) {
InputStream is = (InputStream)result;
byte[] buf = new byte[2048];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead = is.read(buf);
while (bytesRead >= 0) {
baos.write(buf, 0, bytesRead);
bytesRead = is.read(buf);
}
result = baos.toByteArray();
}
}
catch (IOException e) {
// e.printStackTrace(); ignore
}
return result;
}
@Override
public void setAttributeValueInObject(Object object, Object value)
throws DescriptorException {
// TODO - figure out if inbound-path needs to be handled
}
});
mapping.setXPath(DEFAULT_SIMPLE_XML_FORMAT_TAG + "/" +
DEFAULT_SIMPLE_XML_TAG + "/attachment");
mapping.setSwaRef(true);
mapping.setShouldInlineBinaryData(false);
mapping.setMimeType(attachment.getMimeType());
descriptor.addMapping(mapping);
NamespaceResolver nr = new NamespaceResolver();
descriptor.setNamespaceResolver(nr);
oxSession.getProject().addDescriptor(descriptor);
((DatabaseSessionImpl)oxSession)
.initializeDescriptorIfSessionAlive(descriptor);
xrService.getXMLContext().storeXMLDescriptorByQName(descriptor);
}
}