-
Notifications
You must be signed in to change notification settings - Fork 15
/
RejectDoctypeSaxFilter.java
176 lines (152 loc) · 5.85 KB
/
RejectDoctypeSaxFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.messaging.saaj.util;
import java.util.logging.Logger;
import javax.xml.parsers.SAXParser;
import jakarta.xml.soap.SOAPException;
import org.xml.sax.*;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.XMLFilterImpl;
import com.sun.xml.messaging.saaj.SOAPExceptionImpl;
import org.xml.sax.helpers.AttributesImpl;
/**
* Users of this class see a SAX2 XMLReader (via XMLFilterImpl). This
* class creates a parent XMLReader via JAXP and installs itself as a SAX2
* extension LexicalHandler which rejects document type declarations
* because they are not legal in SOAP. If the user of this class sets a
* LexicalHandler, then it forwards events to that handler.
*
*
* @author Edwin Goei
*/
public class RejectDoctypeSaxFilter extends XMLFilterImpl implements XMLReader, LexicalHandler{
private static final Logger log =
Logger.getLogger(LogDomainConstants.UTIL_DOMAIN,
"com.sun.xml.messaging.saaj.util.LocalStrings");
/** Standard SAX 2.0 ext property */
static final String LEXICAL_HANDLER_PROP =
"http://xml.org/sax/properties/lexical-handler";
static final String WSU_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
static final String SIGNATURE_LNAME = "Signature";
static final String ENCRYPTED_DATA_LNAME = "EncryptedData";
static final String DSIG_NS = "http://www.w3.org/2000/09/xmldsig#";
static final String XENC_NS = "http://www.w3.org/2001/04/xmlenc#";
static final String ID_NAME = "ID";
/** LexicalHandler to forward events to, if any */
private LexicalHandler lexicalHandler;
public RejectDoctypeSaxFilter(SAXParser saxParser) throws SOAPException {
XMLReader xmlReader;
try {
xmlReader = saxParser.getXMLReader();
} catch (SAXException e) {
log.severe("SAAJ0602.util.getXMLReader.exception");
throw new SOAPExceptionImpl(
"Couldn't get an XMLReader while constructing a RejectDoctypeSaxFilter",
e);
}
// Set ourselves up to be the SAX LexicalHandler
try {
xmlReader.setProperty(LEXICAL_HANDLER_PROP, this);
} catch (Exception e) {
log.severe("SAAJ0603.util.setProperty.exception");
throw new SOAPExceptionImpl(
"Couldn't set the lexical handler property while constructing a RejectDoctypeSaxFilter",
e);
}
// Set the parent XMLReader of this SAX filter
setParent(xmlReader);
}
/*
* Override setProperty() to capture any LexicalHandler that is set for
* forwarding of events.
*/
@Override
public void setProperty(String name, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException {
if (LEXICAL_HANDLER_PROP.equals(name)) {
lexicalHandler = (LexicalHandler) value;
} else {
super.setProperty(name, value);
}
}
//
// Beginning of SAX LexicalHandler callbacks...
//
@Override
public void startDTD(String name, String publicId, String systemId)
throws SAXException {
throw new SAXException("Document Type Declaration is not allowed");
}
@Override
public void endDTD() throws SAXException {
}
@Override
public void startEntity(String name) throws SAXException {
if (lexicalHandler != null) {
lexicalHandler.startEntity(name);
}
}
@Override
public void endEntity(String name) throws SAXException {
if (lexicalHandler != null) {
lexicalHandler.endEntity(name);
}
}
@Override
public void startCDATA() throws SAXException {
if (lexicalHandler != null) {
lexicalHandler.startCDATA();
}
}
@Override
public void endCDATA() throws SAXException {
if (lexicalHandler != null) {
lexicalHandler.endCDATA();
}
}
@Override
public void comment(char[] ch, int start, int length) throws SAXException {
if (lexicalHandler != null) {
lexicalHandler.comment(ch, start, length);
}
}
//
// End of SAX LexicalHandler callbacks
//
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException{
if(atts != null ){
boolean eos = false;
if(namespaceURI == DSIG_NS || XENC_NS == namespaceURI){
eos = true;
}
int length = atts.getLength();
AttributesImpl attrImpl = new AttributesImpl();
for(int i=0; i< length;i++){
String name = atts.getLocalName(i);
if(name!=null && (name.equals("Id"))){
if(eos || atts.getURI(i) == WSU_NS ){
attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i),
atts.getQName(i), ID_NAME, atts.getValue(i));
}else{
attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i));
}
}else{
attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i),
atts.getQName(i), atts.getType(i), atts.getValue(i));
}
}
super.startElement(namespaceURI,localName, qName,attrImpl);
}else{
super.startElement(namespaceURI,localName, qName, null);
}
}
}