FileDocCategorySizeDatePackage
FilterChain.javaAPI DocGlassfish v2 API10633Sun Jul 29 11:07:50 BST 2007com.sun.enterprise.admin.wsmgmt.transform

FilterChain

public class FilterChain extends Object
Ordered XSLT filter chain.

Fields Summary
XMLReader
_reader
SAXTransformerFactory
_stf
Transformer
_transformer
TemplatesData[]
templatesData
static final int
STRING_WRITER_INIT_SIZE
private static final Logger
_logger
private static final com.sun.enterprise.util.i18n.StringManager
_stringMgr
Constructors Summary
public FilterChain(com.sun.enterprise.admin.wsmgmt.config.spi.TransformationRule[] rules, boolean reverse)
Public Constructor.

param
appId name of the application
param
endpoint end point name for which stats are collected

        try {
            // Set up to the Transformer
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser parser = spf.newSAXParser();
            _reader = parser.getXMLReader();
   
            _stf = (SAXTransformerFactory) TransformerFactory.newInstance();
            _transformer = _stf.newTransformer();
            setupTemplates(rules, reverse);
        } catch(Exception e) {
            throw new TransformException(e);
        }
    
Methods Summary
private synchronized voidaddTemplate(int index, com.sun.enterprise.admin.wsmgmt.config.spi.TransformationRule tRule)

        try {
            if (tRule == null) {
                return;
            }
            String name = tRule.getName();
            // check if the Rule File Location is relative, then prepend the 
            // instance root to the path
            File stylesheet = new File(tRule.getRuleFileLocation());
            if ( !stylesheet.isAbsolute()) {
                // construct the absolute path
                String instanceRoot = System.getProperty(
                    SystemPropertyConstants.INSTANCE_ROOT_PROPERTY);

                stylesheet = new File( instanceRoot 
                 + File.separator+tRule.getRuleFileLocation());
            } 
//            XMLFilter filter = _stf.newXMLFilter(new StreamSource(stylesheet));
            templatesData[index] = 
                new TemplatesData(name, _stf.newTemplates(new StreamSource(stylesheet)));            
            // addToList(index,name,templates);
        } catch(Exception e) {
            String msg = _stringMgr.getString("rule_not_compiled",
            tRule.getRuleFileLocation(), tRule.getName());
            _logger.log(Level.WARNING, msg); 
            throw new TransformException(e);
        }
    
private voidaddToList(java.util.LinkedList filterList, int index, org.xml.sax.XMLFilter filter)

        XMLFilter parent = null;
        if ( index == 0) {
            filter.setParent(_reader);
        } else {
            parent = (XMLFilter) filterList.get(index-1);
            filter.setParent(parent);
            // check if child the exists
            if ( filterList.size() -1 > index) {
                XMLFilter child = (XMLFilter) filterList.get(index);
                if ( child != null) {
                    child.setParent(filter);
                }
            }
        }
        filterList.add(index, filter);        
    
private java.util.LinkedListgetFilterList()

        LinkedList filterList = new LinkedList();
        if (templatesData == null)
            return filterList;
        for (int i = 0; i < templatesData.length; i++) {
            TemplatesData t = templatesData[i];
            XMLFilter f = _stf.newXMLFilter(t.getTemplates());
            addToList(filterList, i, f);
        }
        return filterList;
    
public synchronized voidprocess(com.sun.enterprise.admin.wsmgmt.filter.spi.FilterContext context)
Invoke the filter.

param
context filter context

            
        LinkedList filterList = getFilterList();
        if (( filterList.size() == 0 ) || (context == null)) {
            return;
        }

        try {

             SOAPMessageContext smc = context.getMessageContext();
             SOAPMessage sm = smc.getMessage();
             SOAPPart sp = sm.getSOAPPart();
             // gets the SOAP envelope as a JAXP Source object.
             Source s = sp.getContent();

            ByteArrayInputStream bis = null;
            InputSource inputSrc;
            if (s instanceof SAXSource) {
                inputSrc = SAXSource.sourceToInputSource(s);
            } else {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                TransformerFactory tf = TransformerFactory.newInstance();
                Transformer xtransformer = tf.newTransformer();
                xtransformer.transform(s, new StreamResult(bos));
                byte[] buf = bos.toByteArray();
                bos.flush();
                bos.close();
                
                bis = new ByteArrayInputStream(buf);
                inputSrc = new InputSource(bis);
            }

            // Set up the transformer to process the SAX events generated
            // by the last filter in the chain
            //inputSrc = SAXSource.sourceToInputSource(s);
            SAXSource transformSource = new SAXSource( 
                ((XMLFilter)filterList.getLast()), inputSrc);

             StringWriter sw = new StringWriter(STRING_WRITER_INIT_SIZE);
             Result r = new StreamResult(sw);
             _transformer.transform(transformSource, r);
             // get the string from the result.
             sw.flush();
             sw.close();
             if (bis != null)
                bis.close();
             StringBufferInputStream in = new StringBufferInputStream(
                            sw.toString());
             MessageFactory mFactory = MessageFactory.newInstance();
             SOAPMessage resMsg = mFactory.createMessage(null, in);
                 smc.setMessage(resMsg);


        } catch(Exception e) {
            _logger.fine("XSLT transformation failed for " 
                + context.getFullyQualifiedName());
            throw new TransformException(e);
        }
    
private synchronized voidsetupTemplates(com.sun.enterprise.admin.wsmgmt.config.spi.TransformationRule[] tRules, boolean reverse)

        if ((tRules == null) || (tRules.length == 0)) {
            return;
        }
        templatesData = new TemplatesData[tRules.length];
        
        if (!reverse) {    
            for(int idx =0; idx < tRules.length; idx++) {
                addTemplate(idx, tRules[idx]);
            }
        } else {
            int index =0;
            for(int idx = tRules.length -1; idx > -1; idx--) {
                addTemplate(index, tRules[idx]);
                index++;
            }
        }