src/share/jaxws_classes/com/sun/xml/internal/ws/policy/sourcemodel/attach/ExternalAttachmentsUnmarshaller.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 515
6cd506508147
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, 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.ws.policy.sourcemodel.attach;
aoqi@0 27
aoqi@0 28 import com.sun.xml.internal.ws.policy.Policy;
aoqi@0 29 import com.sun.xml.internal.ws.policy.PolicyConstants;
aoqi@0 30 import com.sun.xml.internal.ws.policy.PolicyException;
aoqi@0 31 import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages;
aoqi@0 32 import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger;
aoqi@0 33 import com.sun.xml.internal.ws.policy.sourcemodel.PolicyModelTranslator;
aoqi@0 34 import com.sun.xml.internal.ws.policy.sourcemodel.PolicyModelUnmarshaller;
aoqi@0 35 import com.sun.xml.internal.ws.policy.sourcemodel.PolicySourceModel;
aoqi@0 36
aoqi@0 37 import java.io.Reader;
aoqi@0 38 import java.net.URI;
aoqi@0 39 import java.net.URISyntaxException;
aoqi@0 40 import java.util.Collections;
aoqi@0 41 import java.util.HashMap;
aoqi@0 42 import java.util.Map;
aoqi@0 43 import javax.xml.namespace.QName;
aoqi@0 44 import javax.xml.stream.Location;
aoqi@0 45 import javax.xml.stream.XMLEventReader;
aoqi@0 46 import javax.xml.stream.XMLInputFactory;
aoqi@0 47 import javax.xml.stream.XMLStreamConstants;
aoqi@0 48 import javax.xml.stream.XMLStreamException;
aoqi@0 49 import javax.xml.stream.events.Characters;
aoqi@0 50 import javax.xml.stream.events.EndElement;
aoqi@0 51 import javax.xml.stream.events.StartElement;
aoqi@0 52 import javax.xml.stream.events.XMLEvent;
aoqi@0 53
aoqi@0 54 /**
aoqi@0 55 * Unmarshal external policy attachments.
aoqi@0 56 *
aoqi@0 57 * @author Fabian Ritzmann
aoqi@0 58 */
aoqi@0 59 public class ExternalAttachmentsUnmarshaller {
aoqi@0 60
aoqi@0 61 private static final PolicyLogger LOGGER = PolicyLogger.getLogger(ExternalAttachmentsUnmarshaller.class);
aoqi@0 62
aoqi@0 63 public static final URI BINDING_ID;
aoqi@0 64 public static final URI BINDING_OPERATION_ID;
aoqi@0 65 public static final URI BINDING_OPERATION_INPUT_ID;
aoqi@0 66 public static final URI BINDING_OPERATION_OUTPUT_ID;
aoqi@0 67 public static final URI BINDING_OPERATION_FAULT_ID;
aoqi@0 68
aoqi@0 69 static {
aoqi@0 70 try {
aoqi@0 71 BINDING_ID = new URI("urn:uuid:c9bef600-0d7a-11de-abc1-0002a5d5c51b");
aoqi@0 72 BINDING_OPERATION_ID = new URI("urn:uuid:62e66b60-0d7b-11de-a1a2-0002a5d5c51b");
aoqi@0 73 BINDING_OPERATION_INPUT_ID = new URI("urn:uuid:730d8d20-0d7b-11de-84e9-0002a5d5c51b");
aoqi@0 74 BINDING_OPERATION_OUTPUT_ID = new URI("urn:uuid:85b0f980-0d7b-11de-8e9d-0002a5d5c51b");
aoqi@0 75 BINDING_OPERATION_FAULT_ID = new URI("urn:uuid:917cb060-0d7b-11de-9e80-0002a5d5c51b");
aoqi@0 76 } catch (URISyntaxException e) {
aoqi@0 77 throw LOGGER.logSevereException(new IllegalArgumentException(LocalizationMessages.WSP_0094_INVALID_URN()), e);
aoqi@0 78 }
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 private static final QName POLICY_ATTACHMENT = new QName("http://www.w3.org/ns/ws-policy", "PolicyAttachment");
aoqi@0 82 private static final QName APPLIES_TO = new QName("http://www.w3.org/ns/ws-policy", "AppliesTo");
aoqi@0 83 private static final QName POLICY = new QName("http://www.w3.org/ns/ws-policy", "Policy");
aoqi@0 84 private static final QName URI = new QName("http://www.w3.org/ns/ws-policy", "URI");
aoqi@0 85 private static final QName POLICIES = new QName(PolicyConstants.SUN_MANAGEMENT_NAMESPACE, "Policies");
aoqi@0 86 private static final ContextClassloaderLocal<XMLInputFactory> XML_INPUT_FACTORY = new ContextClassloaderLocal<XMLInputFactory>() {
aoqi@0 87 @Override
aoqi@0 88 protected XMLInputFactory initialValue() throws Exception {
aoqi@0 89 return XMLInputFactory.newInstance();
aoqi@0 90 }
aoqi@0 91 };
aoqi@0 92
aoqi@0 93 private static final PolicyModelUnmarshaller POLICY_UNMARSHALLER = PolicyModelUnmarshaller.getXmlUnmarshaller();
aoqi@0 94
aoqi@0 95 private final Map<URI, Policy> map = new HashMap<URI, Policy>();
aoqi@0 96 private URI currentUri = null;
aoqi@0 97 private Policy currentPolicy = null;
aoqi@0 98
aoqi@0 99 public static Map<URI, Policy> unmarshal(final Reader source) throws PolicyException {
aoqi@0 100 LOGGER.entering(source);
aoqi@0 101 try {
aoqi@0 102 XMLEventReader reader = XML_INPUT_FACTORY.get().createXMLEventReader(source);
aoqi@0 103 ExternalAttachmentsUnmarshaller instance = new ExternalAttachmentsUnmarshaller();
aoqi@0 104 final Map<URI, Policy> map = instance.unmarshal(reader, null);
aoqi@0 105 LOGGER.exiting(map);
aoqi@0 106 return Collections.unmodifiableMap(map);
aoqi@0 107 } catch (XMLStreamException ex) {
aoqi@0 108 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0086_FAILED_CREATE_READER(source)), ex);
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 private Map<URI, Policy> unmarshal(final XMLEventReader reader, final StartElement parentElement) throws PolicyException {
aoqi@0 113 XMLEvent event = null;
aoqi@0 114 while (reader.hasNext()) {
aoqi@0 115 try {
aoqi@0 116 event = reader.peek();
aoqi@0 117 switch (event.getEventType()) {
aoqi@0 118 case XMLStreamConstants.START_DOCUMENT:
aoqi@0 119 case XMLStreamConstants.COMMENT:
aoqi@0 120 reader.nextEvent();
aoqi@0 121 break;
aoqi@0 122
aoqi@0 123 case XMLStreamConstants.CHARACTERS:
aoqi@0 124 processCharacters(event.asCharacters(), parentElement, map);
aoqi@0 125 reader.nextEvent();
aoqi@0 126 break;
aoqi@0 127
aoqi@0 128 case XMLStreamConstants.END_ELEMENT:
aoqi@0 129 processEndTag(event.asEndElement(), parentElement);
aoqi@0 130 reader.nextEvent();
aoqi@0 131 return map;
aoqi@0 132
aoqi@0 133 case XMLStreamConstants.START_ELEMENT:
aoqi@0 134 final StartElement element = event.asStartElement();
aoqi@0 135 processStartTag(element, parentElement, reader, map);
aoqi@0 136 break;
aoqi@0 137
aoqi@0 138 case XMLStreamConstants.END_DOCUMENT:
aoqi@0 139 return map;
aoqi@0 140
aoqi@0 141 default:
aoqi@0 142 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0087_UNKNOWN_EVENT(event)));
aoqi@0 143 }
aoqi@0 144 } catch (XMLStreamException e) {
aoqi@0 145 final Location location = event == null ? null : event.getLocation();
aoqi@0 146 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0088_FAILED_PARSE(location)), e);
aoqi@0 147 }
aoqi@0 148 }
aoqi@0 149 return map;
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 private void processStartTag(final StartElement element, final StartElement parent,
aoqi@0 153 final XMLEventReader reader, final Map<URI, Policy> map)
aoqi@0 154 throws PolicyException {
aoqi@0 155 try {
aoqi@0 156 final QName name = element.getName();
aoqi@0 157 if (parent == null) {
aoqi@0 158 if (!name.equals(POLICIES)) {
aoqi@0 159 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<Policies>", name, element.getLocation())));
aoqi@0 160 }
aoqi@0 161 } else {
aoqi@0 162 final QName parentName = parent.getName();
aoqi@0 163 if (parentName.equals(POLICIES)) {
aoqi@0 164 if (!name.equals(POLICY_ATTACHMENT)) {
aoqi@0 165 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<PolicyAttachment>", name, element.getLocation())));
aoqi@0 166 }
aoqi@0 167 } else if (parentName.equals(POLICY_ATTACHMENT)) {
aoqi@0 168 if (name.equals(POLICY)) {
aoqi@0 169 readPolicy(reader);
aoqi@0 170 return;
aoqi@0 171 } else if (!name.equals(APPLIES_TO)) {
aoqi@0 172 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<AppliesTo> or <Policy>", name, element.getLocation())));
aoqi@0 173 }
aoqi@0 174 } else if (parentName.equals(APPLIES_TO)) {
aoqi@0 175 if (!name.equals(URI)) {
aoqi@0 176 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0089_EXPECTED_ELEMENT("<URI>", name, element.getLocation())));
aoqi@0 177 }
aoqi@0 178 } else {
aoqi@0 179 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0090_UNEXPECTED_ELEMENT(name, element.getLocation())));
aoqi@0 180 }
aoqi@0 181 }
aoqi@0 182 reader.nextEvent();
aoqi@0 183 this.unmarshal(reader, element);
aoqi@0 184 } catch (XMLStreamException e) {
aoqi@0 185 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0088_FAILED_PARSE(element.getLocation()), e));
aoqi@0 186 }
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 private void readPolicy(final XMLEventReader reader) throws PolicyException {
aoqi@0 190 final PolicySourceModel policyModel = POLICY_UNMARSHALLER.unmarshalModel(reader);
aoqi@0 191 final PolicyModelTranslator translator = PolicyModelTranslator.getTranslator();
aoqi@0 192 final Policy policy = translator.translate(policyModel);
aoqi@0 193 if (this.currentUri != null) {
aoqi@0 194 map.put(this.currentUri, policy);
aoqi@0 195 this.currentUri = null;
aoqi@0 196 this.currentPolicy = null;
aoqi@0 197 }
aoqi@0 198 else {
aoqi@0 199 this.currentPolicy = policy;
aoqi@0 200 }
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 private void processEndTag(EndElement element, StartElement startElement) throws PolicyException {
aoqi@0 204 checkEndTagName(startElement.getName(), element);
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 private void checkEndTagName(final QName expectedName, final EndElement element) throws PolicyException {
aoqi@0 208 final QName actualName = element.getName();
aoqi@0 209 if (!expectedName.equals(actualName)) {
aoqi@0 210 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0091_END_ELEMENT_NO_MATCH(expectedName, element, element.getLocation())));
aoqi@0 211 }
aoqi@0 212
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 private void processCharacters(final Characters chars, final StartElement currentElement, final Map<URI, Policy> map)
aoqi@0 216 throws PolicyException {
aoqi@0 217 if (chars.isWhiteSpace()) {
aoqi@0 218 return;
aoqi@0 219 }
aoqi@0 220 else {
aoqi@0 221 final String data = chars.getData();
aoqi@0 222 if ((currentElement != null) && URI.equals(currentElement.getName())) {
aoqi@0 223 processUri(chars, map);
aoqi@0 224 return;
aoqi@0 225 } else {
aoqi@0 226 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0092_CHARACTER_DATA_UNEXPECTED(currentElement, data, chars.getLocation())));
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 }
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 private void processUri(final Characters chars, final Map<URI, Policy> map) throws PolicyException {
aoqi@0 233 final String data = chars.getData().trim();
aoqi@0 234 try {
aoqi@0 235 final URI uri = new URI(data);
aoqi@0 236 if (this.currentPolicy != null) {
aoqi@0 237 map.put(uri, this.currentPolicy);
aoqi@0 238 this.currentUri = null;
aoqi@0 239 this.currentPolicy = null;
aoqi@0 240 } else {
aoqi@0 241 this.currentUri = uri;
aoqi@0 242 }
aoqi@0 243 } catch (URISyntaxException e) {
aoqi@0 244 throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0093_INVALID_URI(data, chars.getLocation())), e);
aoqi@0 245 }
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 }

mercurial