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