aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.messaging.saaj.util; aoqi@0: aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: import javax.xml.parsers.SAXParser; aoqi@0: import javax.xml.soap.SOAPException; aoqi@0: aoqi@0: import org.xml.sax.*; aoqi@0: import org.xml.sax.ext.LexicalHandler; aoqi@0: import org.xml.sax.helpers.XMLFilterImpl; aoqi@0: aoqi@0: import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; aoqi@0: import org.xml.sax.helpers.AttributesImpl; aoqi@0: aoqi@0: /** aoqi@0: * Users of this class see a SAX2 XMLReader (via XMLFilterImpl). This aoqi@0: * class creates a parent XMLReader via JAXP and installs itself as a SAX2 aoqi@0: * extension LexicalHandler which rejects document type declarations aoqi@0: * because they are not legal in SOAP. If the user of this class sets a aoqi@0: * LexicalHandler, then it forwards events to that handler. aoqi@0: * aoqi@0: * aoqi@0: * @author Edwin Goei aoqi@0: */ aoqi@0: aoqi@0: public class RejectDoctypeSaxFilter extends XMLFilterImpl implements XMLReader, LexicalHandler{ aoqi@0: protected static final Logger log = aoqi@0: Logger.getLogger(LogDomainConstants.UTIL_DOMAIN, aoqi@0: "com.sun.xml.internal.messaging.saaj.util.LocalStrings"); aoqi@0: aoqi@0: /** Standard SAX 2.0 ext property */ aoqi@0: static final String LEXICAL_HANDLER_PROP = aoqi@0: "http://xml.org/sax/properties/lexical-handler"; aoqi@0: aoqi@0: static final String WSU_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd".intern(); aoqi@0: static final String SIGNATURE_LNAME = "Signature".intern(); aoqi@0: static final String ENCRYPTED_DATA_LNAME = "EncryptedData".intern(); aoqi@0: static final String DSIG_NS = "http://www.w3.org/2000/09/xmldsig#".intern(); aoqi@0: static final String XENC_NS = "http://www.w3.org/2001/04/xmlenc#".intern(); aoqi@0: static final String ID_NAME = "ID".intern(); aoqi@0: aoqi@0: /** LexicalHandler to forward events to, if any */ aoqi@0: private LexicalHandler lexicalHandler; aoqi@0: aoqi@0: public RejectDoctypeSaxFilter(SAXParser saxParser) throws SOAPException { aoqi@0: XMLReader xmlReader; aoqi@0: try { aoqi@0: xmlReader = saxParser.getXMLReader(); aoqi@0: } catch (Exception e) { aoqi@0: log.severe("SAAJ0602.util.getXMLReader.exception"); aoqi@0: throw new SOAPExceptionImpl( aoqi@0: "Couldn't get an XMLReader while constructing a RejectDoctypeSaxFilter", aoqi@0: e); aoqi@0: } aoqi@0: aoqi@0: // Set ourselves up to be the SAX LexicalHandler aoqi@0: try { aoqi@0: xmlReader.setProperty(LEXICAL_HANDLER_PROP, this); aoqi@0: } catch (Exception e) { aoqi@0: log.severe("SAAJ0603.util.setProperty.exception"); aoqi@0: throw new SOAPExceptionImpl( aoqi@0: "Couldn't set the lexical handler property while constructing a RejectDoctypeSaxFilter", aoqi@0: e); aoqi@0: } aoqi@0: aoqi@0: // Set the parent XMLReader of this SAX filter aoqi@0: setParent(xmlReader); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Override setProperty() to capture any LexicalHandler that is set for aoqi@0: * forwarding of events. aoqi@0: */ aoqi@0: public void setProperty(String name, Object value) aoqi@0: throws SAXNotRecognizedException, SAXNotSupportedException { aoqi@0: if (LEXICAL_HANDLER_PROP.equals(name)) { aoqi@0: lexicalHandler = (LexicalHandler) value; aoqi@0: } else { aoqi@0: super.setProperty(name, value); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // Beginning of SAX LexicalHandler callbacks... aoqi@0: // aoqi@0: aoqi@0: public void startDTD(String name, String publicId, String systemId) aoqi@0: throws SAXException { aoqi@0: throw new SAXException("Document Type Declaration is not allowed"); aoqi@0: } aoqi@0: aoqi@0: public void endDTD() throws SAXException { aoqi@0: } aoqi@0: aoqi@0: public void startEntity(String name) throws SAXException { aoqi@0: if (lexicalHandler != null) { aoqi@0: lexicalHandler.startEntity(name); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void endEntity(String name) throws SAXException { aoqi@0: if (lexicalHandler != null) { aoqi@0: lexicalHandler.endEntity(name); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void startCDATA() throws SAXException { aoqi@0: if (lexicalHandler != null) { aoqi@0: lexicalHandler.startCDATA(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void endCDATA() throws SAXException { aoqi@0: if (lexicalHandler != null) { aoqi@0: lexicalHandler.endCDATA(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void comment(char[] ch, int start, int length) throws SAXException { aoqi@0: if (lexicalHandler != null) { aoqi@0: lexicalHandler.comment(ch, start, length); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // End of SAX LexicalHandler callbacks aoqi@0: // aoqi@0: aoqi@0: public void startElement(String namespaceURI, String localName, aoqi@0: String qName, Attributes atts) throws SAXException{ aoqi@0: if(atts != null ){ aoqi@0: boolean eos = false; aoqi@0: if(namespaceURI == DSIG_NS || XENC_NS == namespaceURI){ aoqi@0: eos = true; aoqi@0: } aoqi@0: int length = atts.getLength(); aoqi@0: AttributesImpl attrImpl = new AttributesImpl(); aoqi@0: for(int i=0; i< length;i++){ aoqi@0: String name = atts.getLocalName(i); aoqi@0: if(name!=null && (name.equals("Id"))){ aoqi@0: if(eos || atts.getURI(i) == WSU_NS ){ aoqi@0: attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), aoqi@0: atts.getQName(i), ID_NAME, atts.getValue(i)); aoqi@0: }else{ aoqi@0: attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i)); aoqi@0: } aoqi@0: }else{ aoqi@0: attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), aoqi@0: atts.getQName(i), atts.getType(i), atts.getValue(i)); aoqi@0: } aoqi@0: } aoqi@0: super.startElement(namespaceURI,localName, qName,attrImpl); aoqi@0: }else{ aoqi@0: super.startElement(namespaceURI,localName, qName, null); aoqi@0: } aoqi@0: } aoqi@0: }