src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/RejectDoctypeSaxFilter.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/RejectDoctypeSaxFilter.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,182 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.messaging.saaj.util;
    1.30 +
    1.31 +import java.util.logging.Logger;
    1.32 +
    1.33 +import javax.xml.parsers.SAXParser;
    1.34 +import javax.xml.soap.SOAPException;
    1.35 +
    1.36 +import org.xml.sax.*;
    1.37 +import org.xml.sax.ext.LexicalHandler;
    1.38 +import org.xml.sax.helpers.XMLFilterImpl;
    1.39 +
    1.40 +import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
    1.41 +import org.xml.sax.helpers.AttributesImpl;
    1.42 +
    1.43 +/**
    1.44 + * Users of this class see a SAX2 XMLReader (via XMLFilterImpl).  This
    1.45 + * class creates a parent XMLReader via JAXP and installs itself as a SAX2
    1.46 + * extension LexicalHandler which rejects document type declarations
    1.47 + * because they are not legal in SOAP.  If the user of this class sets a
    1.48 + * LexicalHandler, then it forwards events to that handler.
    1.49 + *
    1.50 + *
    1.51 + * @author Edwin Goei
    1.52 + */
    1.53 +
    1.54 +public class RejectDoctypeSaxFilter extends XMLFilterImpl implements XMLReader, LexicalHandler{
    1.55 +    protected static final Logger log =
    1.56 +    Logger.getLogger(LogDomainConstants.UTIL_DOMAIN,
    1.57 +    "com.sun.xml.internal.messaging.saaj.util.LocalStrings");
    1.58 +
    1.59 +    /** Standard SAX 2.0 ext property */
    1.60 +    static final String LEXICAL_HANDLER_PROP =
    1.61 +    "http://xml.org/sax/properties/lexical-handler";
    1.62 +
    1.63 +    static final String WSU_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd".intern();
    1.64 +    static final String SIGNATURE_LNAME = "Signature".intern();
    1.65 +    static final String ENCRYPTED_DATA_LNAME = "EncryptedData".intern();
    1.66 +    static final String DSIG_NS = "http://www.w3.org/2000/09/xmldsig#".intern();
    1.67 +    static final String XENC_NS = "http://www.w3.org/2001/04/xmlenc#".intern();
    1.68 +    static final String ID_NAME = "ID".intern();
    1.69 +
    1.70 +    /** LexicalHandler to forward events to, if any */
    1.71 +    private LexicalHandler lexicalHandler;
    1.72 +
    1.73 +    public RejectDoctypeSaxFilter(SAXParser saxParser) throws SOAPException {
    1.74 +        XMLReader xmlReader;
    1.75 +        try {
    1.76 +            xmlReader = saxParser.getXMLReader();
    1.77 +        } catch (Exception e) {
    1.78 +            log.severe("SAAJ0602.util.getXMLReader.exception");
    1.79 +            throw new SOAPExceptionImpl(
    1.80 +            "Couldn't get an XMLReader while constructing a RejectDoctypeSaxFilter",
    1.81 +            e);
    1.82 +        }
    1.83 +
    1.84 +        // Set ourselves up to be the SAX LexicalHandler
    1.85 +        try {
    1.86 +            xmlReader.setProperty(LEXICAL_HANDLER_PROP, this);
    1.87 +        } catch (Exception e) {
    1.88 +            log.severe("SAAJ0603.util.setProperty.exception");
    1.89 +            throw new SOAPExceptionImpl(
    1.90 +            "Couldn't set the lexical handler property while constructing a RejectDoctypeSaxFilter",
    1.91 +            e);
    1.92 +        }
    1.93 +
    1.94 +        // Set the parent XMLReader of this SAX filter
    1.95 +        setParent(xmlReader);
    1.96 +    }
    1.97 +
    1.98 +    /*
    1.99 +     * Override setProperty() to capture any LexicalHandler that is set for
   1.100 +     * forwarding of events.
   1.101 +     */
   1.102 +    public void setProperty(String name, Object value)
   1.103 +    throws SAXNotRecognizedException, SAXNotSupportedException {
   1.104 +        if (LEXICAL_HANDLER_PROP.equals(name)) {
   1.105 +            lexicalHandler = (LexicalHandler) value;
   1.106 +        } else {
   1.107 +            super.setProperty(name, value);
   1.108 +        }
   1.109 +    }
   1.110 +
   1.111 +    //
   1.112 +    // Beginning of SAX LexicalHandler callbacks...
   1.113 +    //
   1.114 +
   1.115 +    public void startDTD(String name, String publicId, String systemId)
   1.116 +    throws SAXException {
   1.117 +        throw new SAXException("Document Type Declaration is not allowed");
   1.118 +    }
   1.119 +
   1.120 +    public void endDTD() throws SAXException {
   1.121 +    }
   1.122 +
   1.123 +    public void startEntity(String name) throws SAXException {
   1.124 +        if (lexicalHandler != null) {
   1.125 +            lexicalHandler.startEntity(name);
   1.126 +        }
   1.127 +    }
   1.128 +
   1.129 +    public void endEntity(String name) throws SAXException {
   1.130 +        if (lexicalHandler != null) {
   1.131 +            lexicalHandler.endEntity(name);
   1.132 +        }
   1.133 +    }
   1.134 +
   1.135 +    public void startCDATA() throws SAXException {
   1.136 +        if (lexicalHandler != null) {
   1.137 +            lexicalHandler.startCDATA();
   1.138 +        }
   1.139 +    }
   1.140 +
   1.141 +    public void endCDATA() throws SAXException {
   1.142 +        if (lexicalHandler != null) {
   1.143 +            lexicalHandler.endCDATA();
   1.144 +        }
   1.145 +    }
   1.146 +
   1.147 +    public void comment(char[] ch, int start, int length) throws SAXException {
   1.148 +        if (lexicalHandler != null) {
   1.149 +            lexicalHandler.comment(ch, start, length);
   1.150 +        }
   1.151 +    }
   1.152 +
   1.153 +    //
   1.154 +    // End of SAX LexicalHandler callbacks
   1.155 +    //
   1.156 +
   1.157 +    public void startElement(String namespaceURI, String localName,
   1.158 +    String qName, Attributes atts)   throws SAXException{
   1.159 +        if(atts != null ){
   1.160 +            boolean eos = false;
   1.161 +            if(namespaceURI == DSIG_NS || XENC_NS == namespaceURI){
   1.162 +                eos = true;
   1.163 +            }
   1.164 +            int length = atts.getLength();
   1.165 +            AttributesImpl attrImpl = new AttributesImpl();
   1.166 +            for(int i=0; i< length;i++){
   1.167 +                String name = atts.getLocalName(i);
   1.168 +                if(name!=null && (name.equals("Id"))){
   1.169 +                    if(eos || atts.getURI(i) == WSU_NS ){
   1.170 +                        attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i),
   1.171 +                        atts.getQName(i), ID_NAME, atts.getValue(i));
   1.172 +                    }else{
   1.173 +                         attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i));
   1.174 +                    }
   1.175 +                }else{
   1.176 +                    attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i),
   1.177 +                    atts.getQName(i), atts.getType(i), atts.getValue(i));
   1.178 +                }
   1.179 +            }
   1.180 +            super.startElement(namespaceURI,localName, qName,attrImpl);
   1.181 +        }else{
   1.182 +            super.startElement(namespaceURI,localName, qName, null);
   1.183 +        }
   1.184 +    }
   1.185 +}

mercurial