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