From 4e12d1df5daab1f7600fa3a58e6fc535375224ff Mon Sep 17 00:00:00 2001 From: pdanner Date: Mon, 10 Sep 2007 15:16:34 +0000 Subject: moved test classes, cashing of resolved entities git-svn-id: https://joinup.ec.europa.eu/svn/moa-idspss/trunk@1002 d688527b-c9ab-4aba-bd8d-4036d912da1d --- .../moa/spss/server/invoke/DataObjectFactory.java | 119 +++++++++++++------ .../spss/server/invoke/ExternalURIResolver.java | 15 --- .../invoke/XMLSignatureVerificationInvoker.java | 6 +- .../moa/spss/server/service/AxisHandler.java | 43 ++++++- .../server/service/SignatureCreationService.java | 13 +- .../server/transaction/TransactionContext.java | 131 +++++++++++++++++---- 6 files changed, 243 insertions(+), 84 deletions(-) (limited to 'spss/server/serverlib/src/main/java/at') diff --git a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/DataObjectFactory.java b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/DataObjectFactory.java index 1386d5c2d..ad0da28f4 100644 --- a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/DataObjectFactory.java +++ b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/DataObjectFactory.java @@ -1,5 +1,10 @@ package at.gv.egovernment.moa.spss.server.invoke; +import iaik.ixsil.util.URI; +import iaik.ixsil.util.XPointerReferenceResolver; +import iaik.server.modules.xml.DataObject; +import iaik.server.modules.xml.XMLDataObject; + import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -7,6 +12,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Vector; import javax.xml.parsers.ParserConfigurationException; @@ -14,25 +20,11 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; - import org.xml.sax.EntityResolver; import org.xml.sax.SAXException; -import iaik.ixsil.util.URI; -import iaik.ixsil.util.XPointerReferenceResolver; -import iaik.server.modules.xml.DataObject; -import iaik.server.modules.xml.XMLDataObject; - import at.gv.egovernment.moa.logging.LogMsg; import at.gv.egovernment.moa.logging.Logger; -import at.gv.egovernment.moa.util.Constants; -import at.gv.egovernment.moa.util.DOMUtils; -import at.gv.egovernment.moa.util.EntityResolverChain; -import at.gv.egovernment.moa.util.MOAEntityResolver; -import at.gv.egovernment.moa.util.MOAErrorHandler; -import at.gv.egovernment.moa.util.StreamEntityResolver; -import at.gv.egovernment.moa.util.StreamUtils; - import at.gv.egovernment.moa.spss.MOAApplicationException; import at.gv.egovernment.moa.spss.MOASystemException; import at.gv.egovernment.moa.spss.api.common.Content; @@ -48,7 +40,16 @@ import at.gv.egovernment.moa.spss.server.iaik.xml.ByteStreamDataObjectImpl; import at.gv.egovernment.moa.spss.server.iaik.xml.DataObjectImpl; import at.gv.egovernment.moa.spss.server.iaik.xml.XMLDataObjectImpl; import at.gv.egovernment.moa.spss.server.iaik.xml.XMLNodeListDataObjectImpl; +import at.gv.egovernment.moa.spss.server.transaction.TransactionContext; +import at.gv.egovernment.moa.spss.server.transaction.TransactionContextManager; import at.gv.egovernment.moa.spss.util.MessageProvider; +import at.gv.egovernment.moa.util.Constants; +import at.gv.egovernment.moa.util.DOMUtils; +import at.gv.egovernment.moa.util.EntityResolverChain; +import at.gv.egovernment.moa.util.MOAEntityResolver; +import at.gv.egovernment.moa.util.MOAErrorHandler; +import at.gv.egovernment.moa.util.StreamEntityResolver; +import at.gv.egovernment.moa.util.StreamUtils; /** * A class to create DataObjects contained in different @@ -132,12 +133,16 @@ public class DataObjectFactory { } case Content.LOCREF_CONTENT: { - ExternalURIResolver uriResolver = new ExternalURIResolver(); String locRefURI = ((ContentLocRef) content).getLocationReferenceURI(); InputStream is = null; try { - is = uriResolver.resolve(locRefURI); + TransactionContext context = TransactionContextManager.getInstance().getTransactionContext(); + is = context.ResolveURI(locRefURI); + if (is == null) { + ExternalURIResolver uriResolver = new ExternalURIResolver(); + is = uriResolver.resolve(locRefURI); + } contentBytes = StreamUtils.readStream(is); } catch (MOAApplicationException e) @@ -299,25 +304,39 @@ public class DataObjectFactory { } case Content.LOCREF_CONTENT: { - ExternalURIResolver uriResolver = new ExternalURIResolver(); - String locRefURI = ((ContentLocRef) content).getLocationReferenceURI(); InputStream contentIS = null; - InputStream uriStream = null; - try - { - uriStream = uriResolver.resolve(locRefURI); - byte[] contentBytes = StreamUtils.readStream(uriStream); - contentIS = new ByteArrayInputStream(contentBytes); - } - catch (Exception e) - { - throw new MOAApplicationException("3202", new Object[]{reference, locRefURI}, e); - } - finally - { - closeInputStream(uriStream); - } - entities.put(reference, contentIS); + String locRefURI = ((ContentLocRef) content).getLocationReferenceURI(); + + + TransactionContext context = TransactionContextManager.getInstance().getTransactionContext(); + Vector entity = context.FindResolvedEntity(locRefURI); + if (entity!=null) { + contentIS = (InputStream) entity.get(0); + } else { + + ExternalURIResolver uriResolver = new ExternalURIResolver(); + + InputStream uriStream = null; + byte[] contentBytes; + String contentType = null; + try + { + uriStream = uriResolver.resolve(locRefURI); + contentBytes = StreamUtils.readStream(uriStream); + contentIS = new ByteArrayInputStream(contentBytes); + contentType = uriResolver.getContentType(); + } + catch (Exception e) + { + throw new MOAApplicationException("3202", new Object[]{reference, locRefURI}, e); + } + finally + { + closeInputStream(uriStream); + } + entities.put(locRefURI, contentIS); + context.PutResolvedEntity(locRefURI, contentBytes, contentType); + } break; } case Content.XML_CONTENT : @@ -648,9 +667,23 @@ public class DataObjectFactory { private DataObjectImpl createFromURIImpl(String uri, boolean asXml) throws MOASystemException, MOAApplicationException { + Logger.trace(">>> resolving uri \"" + uri + "\""); + ExternalURIResolver resolver = new ExternalURIResolver(); - InputStream is = resolver.resolve(uri); - String contentType = resolver.getContentType(); + + TransactionContext context = TransactionContextManager.getInstance().getTransactionContext(); + InputStream is = context.ResolveURI(uri); + String contentType = null; + boolean foundURI = false; + if (is == null) { + is = resolver.resolve(uri); + contentType = resolver.getContentType(); + } else { + foundURI = true; + contentType = (String) context.FindResolvedEntity(uri).get(1); + Logger.trace("found \"" + uri + "\" InputStream in preread Supplements!, do not read any more. Content=" + contentType); + } + DataObjectImpl dataObject; // read the content @@ -684,6 +717,7 @@ public class DataObjectFactory { dataObject = new XMLDataObjectImpl(doc.getDocumentElement()); } catch (Exception e) { // this is the last chance: return the data as a byte stream + Logger.trace(">>> reading stream for \"" + uri + "\""); is = resolver.resolve(uri); ByteArrayInputStream bis; try @@ -699,9 +733,11 @@ public class DataObjectFactory { { closeInputStream(is); } + Logger.trace(">>> read stream for \"" + uri + "\""); } } } + else if (asXml) { // if we need XML data, we're in the wrong place here @@ -711,10 +747,13 @@ public class DataObjectFactory { else { // content is binary: make it available as a binary input stream + Logger.trace(">>> getting binary input for \"" + uri + "\""); + byte[] contentBytes; ByteArrayInputStream bis; try { - bis = new ByteArrayInputStream(StreamUtils.readStream(is)); + contentBytes = StreamUtils.readStream(is); + bis = new ByteArrayInputStream(contentBytes); } catch (IOException e) { @@ -724,12 +763,18 @@ public class DataObjectFactory { { closeInputStream(is); } + if (!foundURI) { + context.PutResolvedEntity(uri, contentBytes, contentType); + } dataObject = new ByteStreamDataObjectImpl(bis); + Logger.trace("<<< got binary input for \"" + uri + "\""); } dataObject.setMimeType(contentType); dataObject.setURI(uri); + Logger.trace("<<< resolved uri \"" + uri + "\""); + return dataObject; } diff --git a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/ExternalURIResolver.java b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/ExternalURIResolver.java index 106742067..cbd88f7f3 100644 --- a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/ExternalURIResolver.java +++ b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/ExternalURIResolver.java @@ -61,7 +61,6 @@ public class ExternalURIResolver { //no transaction throw new MOAApplicationException("2282", new Object[] { uri }); } else { - InputStream attachmentIs = context.getAttachmentInputStream(uri); if (attachmentIs != null) { setContentType(context.getAttachmentContentType(uri.getPath())); @@ -70,20 +69,6 @@ public class ExternalURIResolver { //maybe attachments provided but no suiting attachment found throw new MOAApplicationException("2282", new Object[] { uri }); } -/* - try { - InputStream attachmentIs = context.getAttachment(uri).getInputStream(); - if (attachmentIs != null) { - setContentType(context.getAttachmentContentType(uri.getPath())); - return attachmentIs; - } else { - //maybe attachments provided but no suiting attachment found - throw new MOAApplicationException("2282", new Object[] { uri }); - } - } catch (IOException e) { - throw new MOAApplicationException("2208", new Object[] { uri }, e); - } -*/ } } diff --git a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/XMLSignatureVerificationInvoker.java b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/XMLSignatureVerificationInvoker.java index 4642593eb..3dd7ecf11 100644 --- a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/XMLSignatureVerificationInvoker.java +++ b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/XMLSignatureVerificationInvoker.java @@ -89,10 +89,8 @@ public class XMLSignatureVerificationInvoker { FILTERED_REF_TYPES = new HashSet(); FILTERED_REF_TYPES.add(DsigManifest.XML_DSIG_MANIFEST_TYPE); FILTERED_REF_TYPES.add(SecurityLayerManifest.SECURITY_LAYER_MANIFEST_TYPE); - FILTERED_REF_TYPES.add( - SecurityLayerManifest.SECURITY_LAYER_MANIFEST_TYPE_OLD); - FILTERED_REF_TYPES.add( - XMLConstants.NAMESPACE_ETSI_STRING + "SignedProperties"); + FILTERED_REF_TYPES.add(SecurityLayerManifest.SECURITY_LAYER_MANIFEST_TYPE_OLD); + FILTERED_REF_TYPES.add(XMLConstants.NAMESPACE_ETSI_STRING + "SignedProperties"); } /** diff --git a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/service/AxisHandler.java b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/service/AxisHandler.java index befbd58dd..f5f77ff50 100644 --- a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/service/AxisHandler.java +++ b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/service/AxisHandler.java @@ -27,6 +27,7 @@ import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.logging.LoggingContext; import at.gv.egovernment.moa.logging.LoggingContextManager; import at.gv.egovernment.moa.spss.MOASystemException; +import at.gv.egovernment.moa.spss.api.common.Content; import at.gv.egovernment.moa.spss.server.config.ConfigurationProvider; import at.gv.egovernment.moa.spss.server.transaction.TransactionContext; import at.gv.egovernment.moa.spss.server.transaction.TransactionContextManager; @@ -94,6 +95,7 @@ public class AxisHandler extends BasicHandler { */ private void handleRequest(MessageContext msgContext) throws AxisFault { try { + Logger.trace("---- Entering Axishandler"); // generate a unique transaction id and build the TransactionContext // for this request HttpServletRequest request = @@ -143,7 +145,9 @@ public class AxisHandler extends BasicHandler { } context.setRequestName(soapAction); + Logger.trace(">>> Get AttachmentCount"); int attachmentCount = soapMessage.getAttachmentsImpl().getAttachmentCount(); + Logger.trace("<<< Finished Get AttachmentCount"); if (attachmentCount>0) { // add SOAP attachments to transaction context @@ -156,12 +160,23 @@ public class AxisHandler extends BasicHandler { //Now get the InputStream (note: we could also get the content with Object content = attachment.getContent();) InputStream is = null; javax.activation.DataHandler datahandler = attachment.getDataHandler(); - org.apache.axis.attachments.ManagedMemoryDataSource mmds = (org.apache.axis.attachments.ManagedMemoryDataSource)datahandler.getDataSource(); - if (mmds!=null){ - is = mmds.getInputStream(); + + int TYPE = 2; + switch (TYPE) { + case 1: + { + org.apache.axis.attachments.ManagedMemoryDataSource mmds = (org.apache.axis.attachments.ManagedMemoryDataSource)datahandler.getDataSource(); + context.addAttachment(id, type, mmds); + break; + } + case 2: + { + is = datahandler.getDataSource().getInputStream(); + context.addAttachment(id, type, is, datahandler.getDataSource().getName()); + break; + } } debug("handler.06", new Object[] {id, type}); - context.addAttachment(id, type, mmds); } } @@ -202,6 +217,7 @@ public class AxisHandler extends BasicHandler { fault.setFaultDetail(new Element[] { e.toErrorResponse()}); throw fault; } + Logger.trace("---- Leaving Axishandler"); } /** @@ -379,4 +395,21 @@ public class AxisHandler extends BasicHandler { Logger.debug(new LogMsg(msg.getMessage(messageId, parameters))); } -} + + +// private byte[] toByteArray(AttachmentPart attachment) throws SOAPException, IOException +// { +// ByteArrayOutputStream outputStream = new ByteArrayOutputStream(attachment.getSize()); +// InputStream inputStream = (InputStream) attachment.getContent(); +// int currentByte = -1; +// while ((currentByte = inputStream.read()) != -1) +// outputStream.write(currentByte); +// +// inputStream.close(); +// outputStream.close(); +// +// return outputStream.toByteArray(); +// +//} + +} \ No newline at end of file diff --git a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/service/SignatureCreationService.java b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/service/SignatureCreationService.java index 2d548ea3a..c173625f8 100644 --- a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/service/SignatureCreationService.java +++ b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/service/SignatureCreationService.java @@ -40,6 +40,7 @@ public class SignatureCreationService { */ public Element[] CreateXMLSignatureRequest(Element[] request) throws AxisFault { + Logger.trace("---- Entering SignatureCreationService"); XMLSignatureCreationInvoker invoker = XMLSignatureCreationInvoker.getInstance(); Element[] response = new Element[1]; @@ -72,17 +73,23 @@ public class SignatureCreationService { reparsedReq = ServiceUtils.reparseRequest(context.getRequest()); // convert to API objects + Logger.trace(">>> preparsing Request"); requestObj = requestParser.parse(reparsedReq); - + Logger.trace("<<< preparsed Request"); + + Logger.trace(">>> creating Signature"); // invoke the core logic - responseObj = - invoker.createXMLSignature(requestObj, Collections.EMPTY_SET); + responseObj = invoker.createXMLSignature(requestObj, Collections.EMPTY_SET); + Logger.trace("<<< created Signature"); + Logger.trace(">>> building Response"); // map back to XML response[0] = responseBuilder.build(responseObj).getDocumentElement(); + Logger.trace("<<< built Response"); // save response in transaction context.setResponse(response[0]); + Logger.trace("---- Leaving SignatureCreationService"); } catch (MOAException e) { AxisFault fault = AxisFault.makeFault(e); diff --git a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/transaction/TransactionContext.java b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/transaction/TransactionContext.java index 774880d26..5c1e35a95 100644 --- a/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/transaction/TransactionContext.java +++ b/spss/server/serverlib/src/main/java/at/gv/egovernment/moa/spss/server/transaction/TransactionContext.java @@ -2,6 +2,7 @@ package at.gv.egovernment.moa.spss.server.transaction; import iaik.ixsil.util.URI; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -14,6 +15,7 @@ import java.util.Map.Entry; import org.apache.axis.attachments.ManagedMemoryDataSource; import org.w3c.dom.Element; +import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.spss.MOAApplicationException; import at.gv.egovernment.moa.spss.server.config.ConfigurationProvider; @@ -37,9 +39,11 @@ public class TransactionContext { private Element response; /** The map pointing to SOAP attachments needed by the request. */ private HashMap attachments = null; + /** The map containing cashed entities used in DataObjectFactory. */ + private HashMap resolvedEntities = null; /** The configuration to use throughout the request. */ private ConfigurationProvider configuration = null; - + /** * Create a TransactionContext object. * @@ -169,6 +173,23 @@ public class TransactionContext { this.attachments.put(referenceId, entry); } + /** + * Adds an attachment to the transactions list of SOAP attachments. + * + * @param referenceId Identification value for the SOAP attachment. + * @param contentType MIME type of the SOAP attachment. + * @param is Handle to the InputStream of the SOAP attachment. + * @param filename Filename of the temporary file the InputStream belongs to + */ + public void addAttachment(String referenceId, String contentType, InputStream is, String filename) { + if (this.attachments == null) this.attachments = new HashMap(); + Vector entry = new Vector(3); + entry.add(contentType); + entry.add(is); + entry.add(filename); + this.attachments.put(referenceId, entry); + } + /** * Returns the ManagedMemoryDataSource to a specific SOAP attachment identified by referenceId. * @@ -182,10 +203,14 @@ public class TransactionContext { if (entry==null) { return null; } - //return (InputStream) ( ((ManagedMemoryDataSource)entry.get(1)).getInputStream()); - return (ManagedMemoryDataSource) entry.get(1); + Object object = entry.get(1); + if (object instanceof ManagedMemoryDataSource) { + return (ManagedMemoryDataSource) object; + } else { + return null; + } } - + /** * Returns the InputStream to a specific SOAP attachment identified by uri. * @@ -202,12 +227,17 @@ public class TransactionContext { } InputStream attachmentIs = null; - try { - attachmentIs = (InputStream) ( ((ManagedMemoryDataSource)entry.get(1)).getInputStream()); - } catch (IOException e) { - throw new MOAApplicationException("2208", new Object[] { uri }, e); + Object object = entry.get(1); + + if (object instanceof ManagedMemoryDataSource) { + try { + attachmentIs = (InputStream) ( ((ManagedMemoryDataSource)object).getInputStream()); + } catch (IOException e) { + throw new MOAApplicationException("2208", new Object[] { uri }, e); + } + } else { + attachmentIs = (InputStream) object; } - return attachmentIs; //If we would return the whole mmds: return (ManagedMemoryDataSource) entry.get(1); } @@ -236,17 +266,32 @@ public void cleanAttachmentCache() { while (iterator.hasNext()) { Entry hmEntry = (Entry) iterator.next(); Vector entry = (Vector)hmEntry.getValue(); - ManagedMemoryDataSource mmds = (ManagedMemoryDataSource)entry.get(1); - try { - if (mmds!=null) { - InputStream is = mmds.getInputStream(); - if (is!=null) is.close(); - File f = mmds.getDiskCacheFile(); - if (f!=null) f.delete(); - mmds.delete(); - } - } catch (IOException e) { - // ok to do nothing here + Object object = entry.get(1); + if (object instanceof ManagedMemoryDataSource) { + ManagedMemoryDataSource mmds = (ManagedMemoryDataSource)object; + try { + if (mmds!=null) { + InputStream is = mmds.getInputStream(); + if (is!=null) is.close(); + File f = mmds.getDiskCacheFile(); + if (f!=null) f.delete(); + mmds.delete(); + } + } catch (IOException e) { + // ok to do nothing here + } + } else if (object instanceof InputStream) { + InputStream is = (InputStream)object; + try { + if (is!=null) is.close(); + String tempFile = (String) entry.get(2); + if (tempFile!=null){ + File f = new File(tempFile); + f.delete(); + } + } catch (IOException e) { + // ok to do nothing here + } } } } @@ -261,4 +306,50 @@ public void cleanAttachmentCache() { return configuration; } + /** + * Search an uri content in cashed map. + * + * @param uri The value to look for. + * @return If found the cached entity, null otherwise. + */ + public Vector FindResolvedEntity(String uri) { + if (resolvedEntities==null) return null; + return (Vector) resolvedEntities.get(uri); + } + + /** + * Get a new InputStream of a cached entity. + * + * @param uri The value to look for. + * @return A new InputStream of the cached entity. + */ + public InputStream ResolveURI(String uri) { + InputStream is = null; + Vector entity = FindResolvedEntity(uri); + if (entity!=null) { + byte[] contentBytes = (byte[]) entity.get(0); + if (contentBytes!=null) { + is = new ByteArrayInputStream(contentBytes); + } + } + return is; + } + + /** + * Put a read entity (supplement, detached content, data object) on + * transactions entity cash, to prevent repeated reading on slower channels. + * + * @param uri A transaction-wide unique URI used as key of the entity cash + * table. + * @param contentBytes The cached content belonging to the uri. + * @param contentType If known, the MIME-type of the cashed content. + */ + public void PutResolvedEntity(String uri, byte[] contentBytes, String contentType) { + Logger.trace(" storing uri content of uri \"" + uri + "\" for future references"); + if (resolvedEntities==null) resolvedEntities = new HashMap(); + Vector entity = new Vector(); + entity.add(contentBytes); + entity.add(contentType); + resolvedEntities.put(uri, entity); + } } -- cgit v1.2.3