Thu, 31 Aug 2017 15:18:52 +0800
merge
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 */
26 package com.sun.xml.internal.ws.policy.jaxws;
28 import com.sun.xml.internal.ws.api.model.wsdl.WSDLObject;
29 import com.sun.xml.internal.ws.api.model.wsdl.editable.*;
30 import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension;
31 import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtensionContext;
32 import com.sun.xml.internal.ws.api.policy.PolicyResolver;
33 import com.sun.xml.internal.ws.resources.PolicyMessages;
34 import com.sun.xml.internal.ws.policy.jaxws.SafePolicyReader.PolicyRecord;
35 import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger;
36 import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
37 import com.sun.xml.internal.ws.policy.sourcemodel.PolicySourceModel;
38 import com.sun.xml.internal.ws.policy.sourcemodel.PolicySourceModelContext;
39 import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.NamespaceVersion;
40 import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.XmlToken;
41 import com.sun.xml.internal.ws.policy.PolicyException;
42 import com.sun.xml.internal.ws.policy.PolicyMap;
43 import com.sun.xml.internal.ws.util.xml.XmlUtil;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.net.URI;
48 import java.net.URISyntaxException;
49 import java.net.URL;
50 import java.util.List;
51 import java.util.HashSet;
52 import java.util.ArrayList;
53 import java.util.Collection;
54 import java.util.HashMap;
55 import java.util.LinkedList;
56 import java.util.Map;
58 import javax.xml.namespace.QName;
59 import javax.xml.stream.XMLStreamException;
60 import javax.xml.stream.XMLStreamReader;
61 import javax.xml.ws.WebServiceException;
63 /**
64 * This class parses the Policy Attachments in the WSDL and creates a PolicyMap thaty captures the policies configured on
65 * different PolicySubjects in the wsdl.
66 *
67 * After, it is finished it sets the PolicyMap on the WSDLModel.
68 *
69 * @author Jakub Podlesak (jakub.podlesak at sun.com)
70 * @author Fabian Ritzmann
71 * @author Rama Pulavarthi
72 */
73 final public class PolicyWSDLParserExtension extends WSDLParserExtension {
75 enum HandlerType {
76 PolicyUri, AnonymousPolicyId
77 }
79 final static class PolicyRecordHandler {
80 String handler;
81 HandlerType type;
83 PolicyRecordHandler(HandlerType type, String handler) {
84 this.type = type;
85 this.handler = handler;
86 }
88 HandlerType getType() {
89 return type;
90 }
92 String getHandler() {
93 return handler;
94 }
95 }
97 private static final PolicyLogger LOGGER = PolicyLogger.getLogger(PolicyWSDLParserExtension.class);
99 //anonymous policy id prefix
100 private static final StringBuffer AnonymnousPolicyIdPrefix = new StringBuffer("#__anonymousPolicy__ID");
102 // anonymous policies count
103 private int anonymousPoliciesCount;
105 private final SafePolicyReader policyReader = new SafePolicyReader();
107 // policy queue -- needed for evaluating the right order policy of policy models expansion
108 private PolicyRecord expandQueueHead = null;
110 // storage for policy models with an id passed by
111 private Map<String,PolicyRecord> policyRecordsPassedBy = null;
112 // storage for anonymous policies defined within given WSDL
113 private Map<String,PolicySourceModel> anonymousPolicyModels = null;
115 // container for URIs of policies referenced
116 private List<String> unresolvedUris = null;
118 // structures for policies really needed to build a map
119 private final LinkedList<String> urisNeeded = new LinkedList<String>();
120 private final Map<String, PolicySourceModel> modelsNeeded = new HashMap<String, PolicySourceModel>();
122 // lookup tables for Policy attachments found
123 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4ServiceMap = null;
124 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4PortMap = null;
125 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4PortTypeMap = null;
126 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4BindingMap = null;
127 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4BoundOperationMap = null;
128 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4OperationMap = null;
129 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4MessageMap = null;
130 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4InputMap = null;
131 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4OutputMap = null;
132 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4FaultMap = null;
133 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4BindingInputOpMap = null;
134 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4BindingOutputOpMap = null;
135 private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4BindingFaultOpMap = null;
137 private PolicyMapBuilder policyBuilder = new PolicyMapBuilder();
139 private boolean isPolicyProcessed(final String policyUri) {
140 return modelsNeeded.containsKey(policyUri);
141 }
143 private void addNewPolicyNeeded(final String policyUri, final PolicySourceModel policyModel) {
144 if (!modelsNeeded.containsKey(policyUri)) {
145 modelsNeeded.put(policyUri, policyModel);
146 urisNeeded.addFirst(policyUri);
147 }
148 }
150 private Map<String, PolicySourceModel> getPolicyModels() {
151 return modelsNeeded;
152 }
154 private Map<String,PolicyRecord> getPolicyRecordsPassedBy() {
155 if (null==policyRecordsPassedBy) {
156 policyRecordsPassedBy = new HashMap<String,PolicyRecord>();
157 }
158 return policyRecordsPassedBy;
159 }
161 private Map<String,PolicySourceModel> getAnonymousPolicyModels() {
162 if (null==anonymousPolicyModels) {
163 anonymousPolicyModels = new HashMap<String,PolicySourceModel>();
164 }
165 return anonymousPolicyModels;
166 }
168 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4ServiceMap() {
169 if (null==handlers4ServiceMap) {
170 handlers4ServiceMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
171 }
172 return handlers4ServiceMap;
173 }
175 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4PortMap() {
176 if (null==handlers4PortMap) {
177 handlers4PortMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
178 }
179 return handlers4PortMap;
180 }
182 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4PortTypeMap() {
183 if (null==handlers4PortTypeMap) {
184 handlers4PortTypeMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
185 }
186 return handlers4PortTypeMap;
187 }
189 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4BindingMap() {
190 if (null==handlers4BindingMap) {
191 handlers4BindingMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
192 }
193 return handlers4BindingMap;
194 }
196 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4OperationMap() {
197 if (null==handlers4OperationMap) {
198 handlers4OperationMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
199 }
200 return handlers4OperationMap;
201 }
203 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4BoundOperationMap() {
204 if (null==handlers4BoundOperationMap) {
205 handlers4BoundOperationMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
206 }
207 return handlers4BoundOperationMap;
208 }
210 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4MessageMap() {
211 if (null==handlers4MessageMap) {
212 handlers4MessageMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
213 }
214 return handlers4MessageMap;
215 }
217 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4InputMap() {
218 if (null==handlers4InputMap) {
219 handlers4InputMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
220 }
221 return handlers4InputMap;
222 }
224 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4OutputMap() {
225 if (null==handlers4OutputMap) {
226 handlers4OutputMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
227 }
228 return handlers4OutputMap;
229 }
231 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4FaultMap() {
232 if (null==handlers4FaultMap) {
233 handlers4FaultMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
234 }
235 return handlers4FaultMap;
236 }
238 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4BindingInputOpMap() {
239 if (null==handlers4BindingInputOpMap) {
240 handlers4BindingInputOpMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
241 }
242 return handlers4BindingInputOpMap;
243 }
245 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4BindingOutputOpMap() {
246 if (null==handlers4BindingOutputOpMap) {
247 handlers4BindingOutputOpMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
248 }
249 return handlers4BindingOutputOpMap;
250 }
252 private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4BindingFaultOpMap() {
253 if (null==handlers4BindingFaultOpMap) {
254 handlers4BindingFaultOpMap = new HashMap<WSDLObject,Collection<PolicyRecordHandler>>();
255 }
256 return handlers4BindingFaultOpMap;
257 }
259 private List<String> getUnresolvedUris(final boolean emptyListNeeded) {
260 if ((null == unresolvedUris) || emptyListNeeded) {
261 unresolvedUris = new LinkedList<String>();
262 }
263 return unresolvedUris;
264 }
268 private void policyRecToExpandQueue(final PolicyRecord policyRec) {
269 if (null==expandQueueHead) {
270 expandQueueHead = policyRec;
271 } else {
272 expandQueueHead = expandQueueHead.insert(policyRec);
273 }
274 }
276 /**
277 * Creates a new instance of PolicyWSDLParserExtension
278 */
279 public PolicyWSDLParserExtension() {
281 }
284 private PolicyRecordHandler readSinglePolicy(final PolicyRecord policyRec, final boolean inner) {
285 PolicyRecordHandler handler = null;
286 String policyId = policyRec.policyModel.getPolicyId();
287 if (policyId == null) {
288 policyId = policyRec.policyModel.getPolicyName();
289 }
290 if (policyId != null) { // policy id defined, keep the policy
291 handler = new PolicyRecordHandler(HandlerType.PolicyUri, policyRec.getUri());
292 getPolicyRecordsPassedBy().put(policyRec.getUri(), policyRec);
293 policyRecToExpandQueue(policyRec);
294 } else if (inner) { // no id given to the policy --> keep as an annonymous policy model
295 final String anonymousId = AnonymnousPolicyIdPrefix.append(anonymousPoliciesCount++).toString();
296 handler = new PolicyRecordHandler(HandlerType.AnonymousPolicyId,anonymousId);
297 getAnonymousPolicyModels().put(anonymousId, policyRec.policyModel);
298 if (null != policyRec.unresolvedURIs) {
299 getUnresolvedUris(false).addAll(policyRec.unresolvedURIs);
300 }
301 }
302 return handler;
303 }
306 private void addHandlerToMap(
307 final Map<WSDLObject, Collection<PolicyRecordHandler>> map, final WSDLObject key, final PolicyRecordHandler handler) {
308 if (map.containsKey(key)) {
309 map.get(key).add(handler);
310 } else {
311 final Collection<PolicyRecordHandler> newSet = new LinkedList<PolicyRecordHandler>();
312 newSet.add(handler);
313 map.put(key,newSet);
314 }
315 }
317 private String getBaseUrl(final String policyUri) {
318 if (null == policyUri) {
319 return null;
320 }
321 // TODO: encoded urls (escaped characters) might be a problem ?
322 final int fragmentIdx = policyUri.indexOf('#');
323 return (fragmentIdx == -1) ? policyUri : policyUri.substring(0, fragmentIdx);
324 }
326 // adding current url even to locally referenced policies
327 // in order to distinguish imported policies
328 private void processReferenceUri(
329 final String policyUri,
330 final WSDLObject element,
331 final XMLStreamReader reader,
332 final Map<WSDLObject, Collection<PolicyRecordHandler>> map) {
334 if (null == policyUri || policyUri.length() == 0) {
335 return;
336 }
337 if ('#' != policyUri.charAt(0)) { // external uri (already)
338 getUnresolvedUris(false).add(policyUri);
339 }
341 addHandlerToMap(map, element,
342 new PolicyRecordHandler(
343 HandlerType.PolicyUri,
344 SafePolicyReader.relativeToAbsoluteUrl(policyUri, reader.getLocation().getSystemId())));
345 }
347 private boolean processSubelement(
348 final WSDLObject element, final XMLStreamReader reader, final Map<WSDLObject, Collection<PolicyRecordHandler>> map) {
349 if (NamespaceVersion.resolveAsToken(reader.getName()) == XmlToken.PolicyReference) { // "PolicyReference" element interests us
350 processReferenceUri(policyReader.readPolicyReferenceElement(reader), element, reader, map);
351 return true;
352 } else if (NamespaceVersion.resolveAsToken(reader.getName()) == XmlToken.Policy) { // policy could be defined here
353 final PolicyRecordHandler handler =
354 readSinglePolicy(
355 policyReader.readPolicyElement(
356 reader,
357 (null == reader.getLocation().getSystemId()) ? // baseUrl
358 "" : reader.getLocation().getSystemId()),
359 true);
360 if (null != handler) { // only policies with an Id can work for us
361 addHandlerToMap(map, element, handler);
362 } // endif null != handler
363 return true; // element consumed
364 }//end if Policy element found
365 return false;
366 }
368 private void processAttributes(final WSDLObject element, final XMLStreamReader reader, final Map<WSDLObject, Collection<PolicyRecordHandler>> map) {
369 final String[] uriArray = getPolicyURIsFromAttr(reader);
370 if (null != uriArray) {
371 for (String policyUri : uriArray) {
372 processReferenceUri(policyUri, element, reader, map);
373 }
374 }
375 }
377 @Override
378 public boolean portElements(final EditableWSDLPort port, final XMLStreamReader reader) {
379 LOGGER.entering();
380 final boolean result = processSubelement(port, reader, getHandlers4PortMap());
381 LOGGER.exiting();
382 return result;
383 }
385 @Override
386 public void portAttributes(final EditableWSDLPort port, final XMLStreamReader reader) {
387 LOGGER.entering();
388 processAttributes(port, reader, getHandlers4PortMap());
389 LOGGER.exiting();
390 }
392 @Override
393 public boolean serviceElements(final EditableWSDLService service, final XMLStreamReader reader) {
394 LOGGER.entering();
395 final boolean result = processSubelement(service, reader, getHandlers4ServiceMap());
396 LOGGER.exiting();
397 return result;
398 }
400 @Override
401 public void serviceAttributes(final EditableWSDLService service, final XMLStreamReader reader) {
402 LOGGER.entering();
403 processAttributes(service, reader, getHandlers4ServiceMap());
404 LOGGER.exiting();
405 }
408 @Override
409 public boolean definitionsElements(final XMLStreamReader reader){
410 LOGGER.entering();
411 if (NamespaceVersion.resolveAsToken(reader.getName()) == XmlToken.Policy) { // Only "Policy" element interests me
412 readSinglePolicy(
413 policyReader.readPolicyElement(
414 reader,
415 (null == reader.getLocation().getSystemId()) ? // baseUrl
416 "" : reader.getLocation().getSystemId()),
417 false);
418 LOGGER.exiting();
419 return true;
420 }
421 LOGGER.exiting();
422 return false;
423 }
425 @Override
426 public boolean bindingElements(final EditableWSDLBoundPortType binding, final XMLStreamReader reader) {
427 LOGGER.entering();
428 final boolean result = processSubelement(binding, reader, getHandlers4BindingMap());
429 LOGGER.exiting();
430 return result;
431 }
433 @Override
434 public void bindingAttributes(final EditableWSDLBoundPortType binding, final XMLStreamReader reader) {
435 LOGGER.entering();
436 processAttributes(binding, reader, getHandlers4BindingMap());
437 LOGGER.exiting();
438 }
440 @Override
441 public boolean portTypeElements(final EditableWSDLPortType portType, final XMLStreamReader reader) {
442 LOGGER.entering();
443 final boolean result = processSubelement(portType, reader, getHandlers4PortTypeMap());
444 LOGGER.exiting();
445 return result;
446 }
448 @Override
449 public void portTypeAttributes(final EditableWSDLPortType portType, final XMLStreamReader reader) {
450 LOGGER.entering();
451 processAttributes(portType, reader, getHandlers4PortTypeMap());
452 LOGGER.exiting();
453 }
455 @Override
456 public boolean portTypeOperationElements(final EditableWSDLOperation operation, final XMLStreamReader reader) {
457 LOGGER.entering();
458 final boolean result = processSubelement(operation, reader, getHandlers4OperationMap());
459 LOGGER.exiting();
460 return result;
461 }
463 @Override
464 public void portTypeOperationAttributes(final EditableWSDLOperation operation, final XMLStreamReader reader) {
465 LOGGER.entering();
466 processAttributes(operation, reader, getHandlers4OperationMap());
467 LOGGER.exiting();
468 }
470 @Override
471 public boolean bindingOperationElements(final EditableWSDLBoundOperation boundOperation, final XMLStreamReader reader) {
472 LOGGER.entering();
473 final boolean result = processSubelement(boundOperation, reader, getHandlers4BoundOperationMap());
474 LOGGER.exiting();
475 return result;
476 }
478 @Override
479 public void bindingOperationAttributes(final EditableWSDLBoundOperation boundOperation, final XMLStreamReader reader) {
480 LOGGER.entering();
481 processAttributes(boundOperation, reader, getHandlers4BoundOperationMap());
482 LOGGER.exiting();
483 }
485 @Override
486 public boolean messageElements(final EditableWSDLMessage msg, final XMLStreamReader reader) {
487 LOGGER.entering();
488 final boolean result = processSubelement(msg, reader, getHandlers4MessageMap());
489 LOGGER.exiting();
490 return result;
491 }
493 @Override
494 public void messageAttributes(final EditableWSDLMessage msg, final XMLStreamReader reader) {
495 LOGGER.entering();
496 processAttributes(msg, reader, getHandlers4MessageMap());
497 LOGGER.exiting();
498 }
500 @Override
501 public boolean portTypeOperationInputElements(final EditableWSDLInput input, final XMLStreamReader reader) {
502 LOGGER.entering();
503 final boolean result = processSubelement(input, reader, getHandlers4InputMap());
504 LOGGER.exiting();
505 return result;
506 }
508 @Override
509 public void portTypeOperationInputAttributes(final EditableWSDLInput input, final XMLStreamReader reader) {
510 LOGGER.entering();
511 processAttributes(input, reader, getHandlers4InputMap());
512 LOGGER.exiting();
513 }
516 @Override
517 public boolean portTypeOperationOutputElements(final EditableWSDLOutput output, final XMLStreamReader reader) {
518 LOGGER.entering();
519 final boolean result = processSubelement(output, reader, getHandlers4OutputMap());
520 LOGGER.exiting();
521 return result;
522 }
524 @Override
525 public void portTypeOperationOutputAttributes(final EditableWSDLOutput output, final XMLStreamReader reader) {
526 LOGGER.entering();
527 processAttributes(output, reader, getHandlers4OutputMap());
528 LOGGER.exiting();
529 }
532 @Override
533 public boolean portTypeOperationFaultElements(final EditableWSDLFault fault, final XMLStreamReader reader) {
534 LOGGER.entering();
535 final boolean result = processSubelement(fault, reader, getHandlers4FaultMap());
536 LOGGER.exiting();
537 return result;
538 }
540 @Override
541 public void portTypeOperationFaultAttributes(final EditableWSDLFault fault, final XMLStreamReader reader) {
542 LOGGER.entering();
543 processAttributes(fault, reader, getHandlers4FaultMap());
544 LOGGER.exiting();
545 }
547 @Override
548 public boolean bindingOperationInputElements(final EditableWSDLBoundOperation operation, final XMLStreamReader reader) {
549 LOGGER.entering();
550 final boolean result = processSubelement(operation, reader, getHandlers4BindingInputOpMap());
551 LOGGER.exiting();
552 return result;
553 }
555 @Override
556 public void bindingOperationInputAttributes(final EditableWSDLBoundOperation operation, final XMLStreamReader reader) {
557 LOGGER.entering();
558 processAttributes(operation, reader, getHandlers4BindingInputOpMap());
559 LOGGER.exiting();
560 }
563 @Override
564 public boolean bindingOperationOutputElements(final EditableWSDLBoundOperation operation, final XMLStreamReader reader) {
565 LOGGER.entering();
566 final boolean result = processSubelement(operation, reader, getHandlers4BindingOutputOpMap());
567 LOGGER.exiting();
568 return result;
569 }
571 @Override
572 public void bindingOperationOutputAttributes(final EditableWSDLBoundOperation operation, final XMLStreamReader reader) {
573 LOGGER.entering();
574 processAttributes(operation, reader, getHandlers4BindingOutputOpMap());
575 LOGGER.exiting();
576 }
578 @Override
579 public boolean bindingOperationFaultElements(final EditableWSDLBoundFault fault, final XMLStreamReader reader) {
580 LOGGER.entering();
581 final boolean result = processSubelement(fault, reader, getHandlers4BindingFaultOpMap());
582 LOGGER.exiting(result);
583 return result;
584 }
586 @Override
587 public void bindingOperationFaultAttributes(final EditableWSDLBoundFault fault, final XMLStreamReader reader) {
588 LOGGER.entering();
589 processAttributes(fault, reader, getHandlers4BindingFaultOpMap());
590 LOGGER.exiting();
591 }
594 private PolicyMapBuilder getPolicyMapBuilder() {
595 if (null == policyBuilder) {
596 policyBuilder = new PolicyMapBuilder();
597 }
598 return policyBuilder;
599 }
601 private Collection<String> getPolicyURIs(
602 final Collection<PolicyRecordHandler> handlers, final PolicySourceModelContext modelContext) throws PolicyException{
603 final Collection<String> result = new ArrayList<String>(handlers.size());
604 String policyUri;
605 for (PolicyRecordHandler handler : handlers) {
606 policyUri = handler.handler;
607 if (HandlerType.AnonymousPolicyId == handler.type) {
608 final PolicySourceModel policyModel = getAnonymousPolicyModels().get(policyUri);
609 policyModel.expand(modelContext);
610 while (getPolicyModels().containsKey(policyUri)) {
611 policyUri = AnonymnousPolicyIdPrefix.append(anonymousPoliciesCount++).toString();
612 }
613 getPolicyModels().put(policyUri,policyModel);
614 }
615 result.add(policyUri);
616 }
617 return result;
618 }
620 private boolean readExternalFile(final String fileUrl) {
621 InputStream ios = null;
622 XMLStreamReader reader = null;
623 try {
624 final URL xmlURL = new URL(fileUrl);
625 ios = xmlURL.openStream();
626 reader = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(ios);
627 while (reader.hasNext()) {
628 if (reader.isStartElement() && NamespaceVersion.resolveAsToken(reader.getName()) == XmlToken.Policy) {
629 readSinglePolicy(policyReader.readPolicyElement(reader, fileUrl), false);
630 }
631 reader.next();
632 }
633 return true;
634 } catch (IOException ioe) {
635 return false;
636 } catch (XMLStreamException xmlse) {
637 return false;
638 } finally {
639 PolicyUtils.IO.closeResource(reader);
640 PolicyUtils.IO.closeResource(ios);
641 }
642 }
644 @Override
645 public void finished(final WSDLParserExtensionContext context) {
646 LOGGER.entering(context);
647 // need to make sure proper beginning order of internal policies within unresolvedUris list
648 if (null != expandQueueHead) { // any policies found
649 final List<String> externalUris = getUnresolvedUris(false); // protect list of possible external policies
650 getUnresolvedUris(true); // cleaning up the list only
651 final LinkedList<String> baseUnresolvedUris = new LinkedList<String>();
652 for (PolicyRecord currentRec = expandQueueHead ; null != currentRec ; currentRec = currentRec.next) {
653 baseUnresolvedUris.addFirst(currentRec.getUri());
654 }
655 getUnresolvedUris(false).addAll(baseUnresolvedUris);
656 expandQueueHead = null; // cut the queue off
657 getUnresolvedUris(false).addAll(externalUris);
658 }
660 while (!getUnresolvedUris(false).isEmpty()) {
661 final List<String> urisToBeSolvedList = getUnresolvedUris(false);
662 getUnresolvedUris(true); // just cleaning up the list
663 for (String currentUri : urisToBeSolvedList) {
664 if (!isPolicyProcessed(currentUri)) {
665 final PolicyRecord prefetchedRecord = getPolicyRecordsPassedBy().get(currentUri);
666 if (null == prefetchedRecord) {
667 if (policyReader.getUrlsRead().contains(getBaseUrl(currentUri))) { // --> unresolvable policy
668 LOGGER.logSevereException(new PolicyException(PolicyMessages.WSP_1014_CAN_NOT_FIND_POLICY(currentUri)));
669 } else {
670 if (readExternalFile(getBaseUrl(currentUri))) {
671 getUnresolvedUris(false).add(currentUri);
672 }
673 }
674 } else { // policy has not been yet passed by
675 if (null != prefetchedRecord.unresolvedURIs) {
676 getUnresolvedUris(false).addAll(prefetchedRecord.unresolvedURIs);
677 } // end-if null != prefetchedRecord.unresolvedURIs
678 addNewPolicyNeeded(currentUri, prefetchedRecord.policyModel);
679 }
680 } // end-if policy already processed
681 } // end-foreach unresolved uris
682 }
683 final PolicySourceModelContext modelContext = PolicySourceModelContext.createContext();
684 for (String policyUri : urisNeeded) {
685 final PolicySourceModel sourceModel = modelsNeeded.get(policyUri);
686 try {
687 sourceModel.expand(modelContext);
688 modelContext.addModel(new URI(policyUri), sourceModel);
689 } catch (URISyntaxException e) {
690 LOGGER.logSevereException(e);
691 } catch (PolicyException e) {
692 LOGGER.logSevereException(e);
693 }
694 }
696 // Start-preparation of policy map builder
697 // iterating over all services and binding all the policies read before
698 try {
699 // messageSet holds the handlers for all wsdl:message elements. There
700 // may otherwise be multiple entries for policies that are contained
701 // by fault messages.
702 HashSet<BuilderHandlerMessageScope> messageSet = new HashSet<BuilderHandlerMessageScope>();
703 for (EditableWSDLService service : context.getWSDLModel().getServices().values()) {
704 if (getHandlers4ServiceMap().containsKey(service)) {
705 getPolicyMapBuilder().registerHandler(new BuilderHandlerServiceScope(
706 getPolicyURIs(getHandlers4ServiceMap().get(service),modelContext)
707 ,getPolicyModels()
708 ,service
709 ,service.getName()));
710 }
711 // end service scope
713 for (EditableWSDLPort port : service.getPorts()) {
714 if (getHandlers4PortMap().containsKey(port)) {
715 getPolicyMapBuilder().registerHandler(
716 new BuilderHandlerEndpointScope(
717 getPolicyURIs(getHandlers4PortMap().get(port),modelContext)
718 ,getPolicyModels()
719 ,port
720 ,port.getOwner().getName()
721 ,port.getName()));
722 }
723 if ( // port.getBinding may not be null, but in case ...
724 null != port.getBinding()) {
725 if ( // handler for binding
726 getHandlers4BindingMap().containsKey(port.getBinding())) {
727 getPolicyMapBuilder()
728 .registerHandler(
729 new BuilderHandlerEndpointScope(
730 getPolicyURIs(getHandlers4BindingMap().get(port.getBinding()),modelContext)
731 ,getPolicyModels()
732 ,port.getBinding()
733 ,service.getName()
734 ,port.getName()));
735 } // endif handler for binding
736 if ( // handler for port type
737 getHandlers4PortTypeMap().containsKey(port.getBinding().getPortType())) {
738 getPolicyMapBuilder()
739 .registerHandler(
740 new BuilderHandlerEndpointScope(
741 getPolicyURIs(getHandlers4PortTypeMap().get(port.getBinding().getPortType()),modelContext)
742 ,getPolicyModels()
743 ,port.getBinding().getPortType()
744 ,service.getName()
745 ,port.getName()));
746 } // endif handler for port type
747 // end endpoint scope
749 for (EditableWSDLBoundOperation boundOperation : port.getBinding().getBindingOperations()) {
751 final EditableWSDLOperation operation = boundOperation.getOperation();
752 final QName operationName = new QName(boundOperation.getBoundPortType().getName().getNamespaceURI(), boundOperation.getName().getLocalPart());
753 // We store the message and portType/operation under the same namespace as the binding/operation so that we can match them up later
754 if ( // handler for operation scope -- by boundOperation
755 getHandlers4BoundOperationMap().containsKey(boundOperation)) {
756 getPolicyMapBuilder()
757 .registerHandler(
758 new BuilderHandlerOperationScope(
759 getPolicyURIs(getHandlers4BoundOperationMap().get(boundOperation),modelContext)
760 ,getPolicyModels()
761 ,boundOperation
762 ,service.getName()
763 ,port.getName()
764 ,operationName));
765 } // endif handler for binding:operation scope
766 if ( // handler for operation scope -- by operation map
767 getHandlers4OperationMap().containsKey(operation)) {
768 getPolicyMapBuilder()
769 .registerHandler(
770 new BuilderHandlerOperationScope(
771 getPolicyURIs(getHandlers4OperationMap().get(operation),modelContext)
772 ,getPolicyModels()
773 ,operation
774 ,service.getName()
775 ,port.getName()
776 ,operationName));
777 } // endif for portType:operation scope
778 // end operation scope
780 final EditableWSDLInput input = operation.getInput();
781 if (null!=input) {
782 EditableWSDLMessage inputMsg = input.getMessage();
783 if (inputMsg != null && getHandlers4MessageMap().containsKey(inputMsg)) {
784 messageSet.add(new BuilderHandlerMessageScope(
785 getPolicyURIs(
786 getHandlers4MessageMap().get(inputMsg), modelContext)
787 ,getPolicyModels()
788 ,inputMsg
789 ,BuilderHandlerMessageScope.Scope.InputMessageScope
790 ,service.getName()
791 ,port.getName()
792 ,operationName
793 ,null)
794 );
795 }
796 }
797 if ( // binding op input msg
798 getHandlers4BindingInputOpMap().containsKey(boundOperation)) {
799 getPolicyMapBuilder()
800 .registerHandler(
801 new BuilderHandlerMessageScope(
802 getPolicyURIs(getHandlers4BindingInputOpMap().get(boundOperation),modelContext)
803 ,getPolicyModels()
804 ,boundOperation
805 ,BuilderHandlerMessageScope.Scope.InputMessageScope
806 ,service.getName()
807 ,port.getName()
808 ,operationName
809 ,null));
810 } // endif binding op input msg
811 if ( null != input // portType op input msg
812 && getHandlers4InputMap().containsKey(input)) {
813 getPolicyMapBuilder()
814 .registerHandler(
815 new BuilderHandlerMessageScope(
816 getPolicyURIs(getHandlers4InputMap().get(input),modelContext)
817 ,getPolicyModels()
818 ,input
819 ,BuilderHandlerMessageScope.Scope.InputMessageScope
820 ,service.getName()
821 ,port.getName()
822 ,operationName
823 ,null));
824 } // endif portType op input msg
825 // end input message scope
827 final EditableWSDLOutput output = operation.getOutput();
828 if (null!=output) {
829 EditableWSDLMessage outputMsg = output.getMessage();
830 if (outputMsg != null && getHandlers4MessageMap().containsKey(outputMsg)) {
831 messageSet.add(new BuilderHandlerMessageScope(
832 getPolicyURIs(
833 getHandlers4MessageMap().get(outputMsg),modelContext)
834 ,getPolicyModels()
835 ,outputMsg
836 ,BuilderHandlerMessageScope.Scope.OutputMessageScope
837 ,service.getName()
838 ,port.getName()
839 ,operationName
840 ,null)
841 );
842 }
843 }
844 if ( // binding op output msg
845 getHandlers4BindingOutputOpMap().containsKey(boundOperation)) {
846 getPolicyMapBuilder()
847 .registerHandler(
848 new BuilderHandlerMessageScope(
849 getPolicyURIs(getHandlers4BindingOutputOpMap().get(boundOperation),modelContext)
850 ,getPolicyModels()
851 ,boundOperation
852 ,BuilderHandlerMessageScope.Scope.OutputMessageScope
853 ,service.getName()
854 ,port.getName()
855 ,operationName
856 ,null));
857 } // endif binding op output msg
858 if ( null != output // portType op output msg
859 && getHandlers4OutputMap().containsKey(output)) {
860 getPolicyMapBuilder()
861 .registerHandler(
862 new BuilderHandlerMessageScope(
863 getPolicyURIs(getHandlers4OutputMap().get(output),modelContext)
864 ,getPolicyModels()
865 ,output
866 ,BuilderHandlerMessageScope.Scope.OutputMessageScope
867 ,service.getName()
868 ,port.getName()
869 ,operationName
870 ,null));
871 } // endif portType op output msg
872 // end output message scope
874 for (EditableWSDLBoundFault boundFault : boundOperation.getFaults()) {
875 final EditableWSDLFault fault = boundFault.getFault();
877 // this shouldn't happen ususally,
878 // but since this scenario tested in lagacy tests, dont' fail here
879 if (fault == null) {
880 LOGGER.warning(PolicyMessages.WSP_1021_FAULT_NOT_BOUND(boundFault.getName()));
881 continue;
882 }
884 final EditableWSDLMessage faultMessage = fault.getMessage();
885 final QName faultName = new QName(boundOperation.getBoundPortType().getName().getNamespaceURI(), boundFault.getName());
886 // We store the message and portType/fault under the same namespace as the binding/fault so that we can match them up later
887 if (faultMessage != null && getHandlers4MessageMap().containsKey(faultMessage)) {
888 messageSet.add(
889 new BuilderHandlerMessageScope(
890 getPolicyURIs(getHandlers4MessageMap().get(faultMessage), modelContext)
891 ,getPolicyModels()
892 ,new WSDLBoundFaultContainer(boundFault, boundOperation)
893 ,BuilderHandlerMessageScope.Scope.FaultMessageScope
894 ,service.getName()
895 ,port.getName()
896 ,operationName
897 ,faultName)
898 );
899 }
900 if (getHandlers4FaultMap().containsKey(fault)) {
901 messageSet.add(
902 new BuilderHandlerMessageScope(
903 getPolicyURIs(getHandlers4FaultMap().get(fault), modelContext)
904 ,getPolicyModels()
905 ,new WSDLBoundFaultContainer(boundFault, boundOperation)
906 ,BuilderHandlerMessageScope.Scope.FaultMessageScope
907 ,service.getName()
908 ,port.getName()
909 ,operationName
910 ,faultName)
911 );
912 }
913 if (getHandlers4BindingFaultOpMap().containsKey(boundFault)) {
914 messageSet.add(
915 new BuilderHandlerMessageScope(
916 getPolicyURIs(getHandlers4BindingFaultOpMap().get(boundFault), modelContext)
917 ,getPolicyModels()
918 ,new WSDLBoundFaultContainer(boundFault, boundOperation)
919 ,BuilderHandlerMessageScope.Scope.FaultMessageScope
920 ,service.getName()
921 ,port.getName()
922 ,operationName
923 ,faultName)
924 );
925 }
926 } // end foreach binding operation fault msg
927 // end fault message scope
929 } // end foreach boundOperation in port
930 } // endif port.getBinding() != null
931 } // end foreach port in service
932 } // end foreach service in wsdl
933 // Add handlers for wsdl:message elements
934 for (BuilderHandlerMessageScope scopeHandler : messageSet) {
935 getPolicyMapBuilder().registerHandler(scopeHandler);
936 }
937 } catch(PolicyException e) {
938 LOGGER.logSevereException(e);
939 }
940 // End-preparation of policy map builder
942 LOGGER.exiting();
943 }
946 // time to read possible config file and do alternative selection (on client side)
947 @Override
948 public void postFinished(final WSDLParserExtensionContext context) {
949 // finally register the PolicyMap on the WSDLModel
950 EditableWSDLModel wsdlModel = context.getWSDLModel();
951 PolicyMap effectiveMap;
952 try {
953 if(context.isClientSide())
954 effectiveMap = context.getPolicyResolver().resolve(new PolicyResolver.ClientContext(policyBuilder.getPolicyMap(),context.getContainer()));
955 else
956 effectiveMap = context.getPolicyResolver().resolve(new PolicyResolver.ServerContext(policyBuilder.getPolicyMap(), context.getContainer(),null));
957 wsdlModel.setPolicyMap(effectiveMap);
958 } catch (PolicyException e) {
959 LOGGER.logSevereException(e);
960 throw LOGGER.logSevereException(new WebServiceException(PolicyMessages.WSP_1007_POLICY_EXCEPTION_WHILE_FINISHING_PARSING_WSDL(), e));
961 }
962 try {
963 PolicyUtil.configureModel(wsdlModel,effectiveMap);
964 } catch (PolicyException e) {
965 LOGGER.logSevereException(e);
966 throw LOGGER.logSevereException(new WebServiceException(PolicyMessages.WSP_1012_FAILED_CONFIGURE_WSDL_MODEL(), e));
967 }
968 LOGGER.exiting();
969 }
972 /**
973 * Reads policy reference URIs from PolicyURIs attribute and returns them
974 * as a String array returns null if there is no such attribute. This method
975 * will attempt to check for the attribute in every supported policy namespace.
976 * Resulting array of URIs is concatenation of URIs defined in all found
977 * PolicyURIs attribute version.
978 */
979 private String[] getPolicyURIsFromAttr(final XMLStreamReader reader) {
980 final StringBuilder policyUriBuffer = new StringBuilder();
981 for (NamespaceVersion version : NamespaceVersion.values()) {
982 final String value = reader.getAttributeValue(version.toString(), XmlToken.PolicyUris.toString());
983 if (value != null) {
984 policyUriBuffer.append(value).append(" ");
985 }
986 }
987 return (policyUriBuffer.length() > 0) ? policyUriBuffer.toString().split("[\\n ]+") : null;
988 }
990 }