jaxws/src/java.xml.soap/share/classes/com/sun/xml/internal/messaging/saaj/util/RejectDoctypeSaxFilter.java
changeset 28644 a70f5680dbab
parent 28643 a665e19ca007
parent 28642 a42fefc69922
child 28647 f44908f03772
equal deleted inserted replaced
28643:a665e19ca007 28644:a70f5680dbab
     1 /*
       
     2  * Copyright (c) 1997, 2013, 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     public void setProperty(String name, Object value)
       
   100     throws SAXNotRecognizedException, SAXNotSupportedException {
       
   101         if (LEXICAL_HANDLER_PROP.equals(name)) {
       
   102             lexicalHandler = (LexicalHandler) value;
       
   103         } else {
       
   104             super.setProperty(name, value);
       
   105         }
       
   106     }
       
   107 
       
   108     //
       
   109     // Beginning of SAX LexicalHandler callbacks...
       
   110     //
       
   111 
       
   112     public void startDTD(String name, String publicId, String systemId)
       
   113     throws SAXException {
       
   114         throw new SAXException("Document Type Declaration is not allowed");
       
   115     }
       
   116 
       
   117     public void endDTD() throws SAXException {
       
   118     }
       
   119 
       
   120     public void startEntity(String name) throws SAXException {
       
   121         if (lexicalHandler != null) {
       
   122             lexicalHandler.startEntity(name);
       
   123         }
       
   124     }
       
   125 
       
   126     public void endEntity(String name) throws SAXException {
       
   127         if (lexicalHandler != null) {
       
   128             lexicalHandler.endEntity(name);
       
   129         }
       
   130     }
       
   131 
       
   132     public void startCDATA() throws SAXException {
       
   133         if (lexicalHandler != null) {
       
   134             lexicalHandler.startCDATA();
       
   135         }
       
   136     }
       
   137 
       
   138     public void endCDATA() throws SAXException {
       
   139         if (lexicalHandler != null) {
       
   140             lexicalHandler.endCDATA();
       
   141         }
       
   142     }
       
   143 
       
   144     public void comment(char[] ch, int start, int length) throws SAXException {
       
   145         if (lexicalHandler != null) {
       
   146             lexicalHandler.comment(ch, start, length);
       
   147         }
       
   148     }
       
   149 
       
   150     //
       
   151     // End of SAX LexicalHandler callbacks
       
   152     //
       
   153 
       
   154     public void startElement(String namespaceURI, String localName,
       
   155     String qName, Attributes atts)   throws SAXException{
       
   156         if(atts != null ){
       
   157             boolean eos = false;
       
   158             if(namespaceURI == DSIG_NS || XENC_NS == namespaceURI){
       
   159                 eos = true;
       
   160             }
       
   161             int length = atts.getLength();
       
   162             AttributesImpl attrImpl = new AttributesImpl();
       
   163             for(int i=0; i< length;i++){
       
   164                 String name = atts.getLocalName(i);
       
   165                 if(name!=null && (name.equals("Id"))){
       
   166                     if(eos || atts.getURI(i) == WSU_NS ){
       
   167                         attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i),
       
   168                         atts.getQName(i), ID_NAME, atts.getValue(i));
       
   169                     }else{
       
   170                          attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i));
       
   171                     }
       
   172                 }else{
       
   173                     attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i),
       
   174                     atts.getQName(i), atts.getType(i), atts.getValue(i));
       
   175                 }
       
   176             }
       
   177             super.startElement(namespaceURI,localName, qName,attrImpl);
       
   178         }else{
       
   179             super.startElement(namespaceURI,localName, qName, null);
       
   180         }
       
   181     }
       
   182 }