1 /* 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.messaging.saaj.util; 27 28 import java.util.logging.Logger; 29 30 import javax.xml.parsers.SAXParser; 31 import javax.xml.soap.SOAPException; 32 33 import org.xml.sax.*; 34 import org.xml.sax.ext.LexicalHandler; 35 import org.xml.sax.helpers.XMLFilterImpl; 36 37 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; 38 import org.xml.sax.helpers.AttributesImpl; 39 40 /** 41 * Users of this class see a SAX2 XMLReader (via XMLFilterImpl). This 42 * class creates a parent XMLReader via JAXP and installs itself as a SAX2 43 * extension LexicalHandler which rejects document type declarations 44 * because they are not legal in SOAP. If the user of this class sets a 45 * LexicalHandler, then it forwards events to that handler. 46 * 47 * 48 * @author Edwin Goei 49 */ 50 51 public class RejectDoctypeSaxFilter extends XMLFilterImpl implements XMLReader, LexicalHandler{ 52 protected static final Logger log = 53 Logger.getLogger(LogDomainConstants.UTIL_DOMAIN, 54 "com.sun.xml.internal.messaging.saaj.util.LocalStrings"); 55 56 /** Standard SAX 2.0 ext property */ 57 static final String LEXICAL_HANDLER_PROP = 58 "http://xml.org/sax/properties/lexical-handler"; 59 60 static final String WSU_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd".intern(); 61 static final String SIGNATURE_LNAME = "Signature".intern(); 62 static final String ENCRYPTED_DATA_LNAME = "EncryptedData".intern(); 63 static final String DSIG_NS = "http://www.w3.org/2000/09/xmldsig#".intern(); 64 static final String XENC_NS = "http://www.w3.org/2001/04/xmlenc#".intern(); 65 static final String ID_NAME = "ID".intern(); 66 67 /** LexicalHandler to forward events to, if any */ 68 private LexicalHandler lexicalHandler; 69 70 public RejectDoctypeSaxFilter(SAXParser saxParser) throws SOAPException { 71 XMLReader xmlReader; 72 try { 73 xmlReader = saxParser.getXMLReader(); 74 } catch (Exception e) { 75 log.severe("SAAJ0602.util.getXMLReader.exception"); 76 throw new SOAPExceptionImpl( 77 "Couldn't get an XMLReader while constructing a RejectDoctypeSaxFilter", 78 e); 79 } 80 81 // Set ourselves up to be the SAX LexicalHandler 82 try { 83 xmlReader.setProperty(LEXICAL_HANDLER_PROP, this); 84 } catch (Exception e) { 85 log.severe("SAAJ0603.util.setProperty.exception"); 86 throw new SOAPExceptionImpl( 87 "Couldn't set the lexical handler property while constructing a RejectDoctypeSaxFilter", 88 e); 89 } 90 91 // Set the parent XMLReader of this SAX filter 92 setParent(xmlReader); 93 } 94 95 /* 96 * Override setProperty() to capture any LexicalHandler that is set for 97 * forwarding of events. 98 */ 99 @Override 100 public void setProperty(String name, Object value) 101 throws SAXNotRecognizedException, SAXNotSupportedException { 102 if (LEXICAL_HANDLER_PROP.equals(name)) { 103 lexicalHandler = (LexicalHandler) value; 104 } else { 105 super.setProperty(name, value); 106 } 107 } 108 109 // 110 // Beginning of SAX LexicalHandler callbacks... 111 // 112 113 @Override 114 public void startDTD(String name, String publicId, String systemId) 115 throws SAXException { 116 throw new SAXException("Document Type Declaration is not allowed"); 117 } 118 119 @Override 120 public void endDTD() throws SAXException { 121 } 122 123 @Override 124 public void startEntity(String name) throws SAXException { 125 if (lexicalHandler != null) { 126 lexicalHandler.startEntity(name); 127 } 128 } 129 130 @Override 131 public void endEntity(String name) throws SAXException { 132 if (lexicalHandler != null) { 133 lexicalHandler.endEntity(name); 134 } 135 } 136 137 @Override 138 public void startCDATA() throws SAXException { 139 if (lexicalHandler != null) { 140 lexicalHandler.startCDATA(); 141 } 142 } 143 144 @Override 145 public void endCDATA() throws SAXException { 146 if (lexicalHandler != null) { 147 lexicalHandler.endCDATA(); 148 } 149 } 150 151 @Override 152 public void comment(char[] ch, int start, int length) throws SAXException { 153 if (lexicalHandler != null) { 154 lexicalHandler.comment(ch, start, length); 155 } 156 } 157 158 // 159 // End of SAX LexicalHandler callbacks 160 // 161 162 @Override 163 public void startElement(String namespaceURI, String localName, 164 String qName, Attributes atts) throws SAXException{ 165 if(atts != null ){ 166 boolean eos = false; 167 if(namespaceURI == DSIG_NS || XENC_NS == namespaceURI){ 168 eos = true; 169 } 170 int length = atts.getLength(); 171 AttributesImpl attrImpl = new AttributesImpl(); 172 for(int i=0; i< length;i++){ 173 String name = atts.getLocalName(i); 174 if(name!=null && (name.equals("Id"))){ 175 if(eos || atts.getURI(i) == WSU_NS ){ 176 attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), 177 atts.getQName(i), ID_NAME, atts.getValue(i)); 178 }else{ 179 attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i)); 180 } 181 }else{ 182 attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), 183 atts.getQName(i), atts.getType(i), atts.getValue(i)); 184 } 185 } 186 super.startElement(namespaceURI,localName, qName,attrImpl); 187 }else{ 188 super.startElement(namespaceURI,localName, qName, null); 189 } 190 } 191 }