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_STR);
descriptor.setJavaClass(DataHandler.class);
descriptor.setInstantiationPolicy(
this.new DataHandlerInstantiationPolicy(attachment.getMimeType()));
XMLBinaryDataMapping mapping = new XMLBinaryDataMapping();
mapping.setAttributeName(RESULTS_STR);
mapping.setAttributeAccessor(new AttributeAccessor() {
@Override
public Object getAttributeValueFromObject(Object object)
throws DescriptorException {
Object result = null;
InputStream is = null;
DataHandler dataHandler = (DataHandler)object;
try {
result = dataHandler.getContent();
if (result instanceof 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) {
// ignore
} finally {
try {
is.close();
} catch (IOException e) {
}
}
return result;
}
@Override
public void setAttributeValueInObject(Object object, Object value)
throws DescriptorException {
}
});
mapping.setXPath(DEFAULT_SIMPLE_XML_FORMAT_TAG + SLASH_CHAR +
DEFAULT_SIMPLE_XML_TAG + ATTACHMENT_STR);
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);
}
}