Source Code Cross Referenced for PolicyWSDLParserExtension.java in  » 6.0-JDK-Modules-com.sun » wsit » com » sun » xml » ws » policy » jaxws » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules com.sun » wsit » com.sun.xml.ws.policy.jaxws 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003:         * 
0004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005:         * 
0006:         * The contents of this file are subject to the terms of either the GNU
0007:         * General Public License Version 2 only ("GPL") or the Common Development
0008:         * and Distribution License("CDDL") (collectively, the "License").  You
0009:         * may not use this file except in compliance with the License. You can obtain
0010:         * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
0011:         * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
0012:         * language governing permissions and limitations under the License.
0013:         * 
0014:         * When distributing the software, include this License Header Notice in each
0015:         * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
0016:         * Sun designates this particular file as subject to the "Classpath" exception
0017:         * as provided by Sun in the GPL Version 2 section of the License file that
0018:         * accompanied this code.  If applicable, add the following below the License
0019:         * Header, with the fields enclosed by brackets [] replaced by your own
0020:         * identifying information: "Portions Copyrighted [year]
0021:         * [name of copyright owner]"
0022:         * 
0023:         * Contributor(s):
0024:         * 
0025:         * If you wish your version of this file to be governed by only the CDDL or
0026:         * only the GPL Version 2, indicate your decision by adding "[Contributor]
0027:         * elects to include this software in this distribution under the [CDDL or GPL
0028:         * Version 2] license."  If you don't indicate a single choice of license, a
0029:         * recipient has the option to distribute your version of this file under
0030:         * either the CDDL, the GPL Version 2 or to extend the choice of license to
0031:         * its licensees as provided above.  However, if you add GPL Version 2 code
0032:         * and therefore, elected the GPL Version 2 license, then the option applies
0033:         * only if the new code is made subject to such option by the copyright
0034:         * holder.
0035:         */
0036:        package com.sun.xml.ws.policy.jaxws;
0037:
0038:        import com.sun.xml.ws.api.model.wsdl.WSDLBoundFault;
0039:        import com.sun.xml.ws.api.model.wsdl.WSDLBoundOperation;
0040:        import com.sun.xml.ws.api.model.wsdl.WSDLBoundPortType;
0041:        import com.sun.xml.ws.api.model.wsdl.WSDLFault;
0042:        import com.sun.xml.ws.api.model.wsdl.WSDLInput;
0043:        import com.sun.xml.ws.api.model.wsdl.WSDLMessage;
0044:        import com.sun.xml.ws.api.model.wsdl.WSDLObject;
0045:        import com.sun.xml.ws.api.model.wsdl.WSDLOperation;
0046:        import com.sun.xml.ws.api.model.wsdl.WSDLOutput;
0047:        import com.sun.xml.ws.api.model.wsdl.WSDLPort;
0048:        import com.sun.xml.ws.api.model.wsdl.WSDLPortType;
0049:        import com.sun.xml.ws.api.model.wsdl.WSDLService;
0050:        import com.sun.xml.ws.api.wsdl.parser.WSDLParserExtension;
0051:        import com.sun.xml.ws.api.wsdl.parser.WSDLParserExtensionContext;
0052:        import com.sun.xml.ws.policy.EffectivePolicyModifier;
0053:        import com.sun.xml.ws.policy.PolicyConstants;
0054:        import com.sun.xml.ws.policy.PolicyException;
0055:        import com.sun.xml.ws.policy.PolicyMap;
0056:        import com.sun.xml.ws.policy.PolicyMapExtender;
0057:        import com.sun.xml.ws.policy.PolicyMapMutator;
0058:        import com.sun.xml.ws.policy.jaxws.privateutil.LocalizationMessages;
0059:        import com.sun.xml.ws.policy.privateutil.PolicyLogger;
0060:        import com.sun.xml.ws.policy.privateutil.PolicyUtils;
0061:        import com.sun.xml.ws.policy.sourcemodel.PolicyModelUnmarshaller;
0062:        import com.sun.xml.ws.policy.sourcemodel.PolicySourceModel;
0063:        import com.sun.xml.ws.policy.sourcemodel.PolicySourceModelContext;
0064:        import java.io.IOException;
0065:        import java.io.InputStream;
0066:        import java.io.StringReader;
0067:        import java.net.URI;
0068:        import java.net.URISyntaxException;
0069:        import java.net.URL;
0070:        import java.util.List;
0071:        import java.util.HashSet;
0072:        import java.util.ArrayList;
0073:        import java.util.Collection;
0074:        import java.util.HashMap;
0075:        import java.util.LinkedList;
0076:        import java.util.Map;
0077:        import java.util.Set;
0078:        import javax.xml.namespace.QName;
0079:        import javax.xml.stream.XMLStreamConstants;
0080:        import javax.xml.stream.XMLStreamException;
0081:        import javax.xml.stream.XMLStreamReader;
0082:        import javax.xml.stream.XMLInputFactory;
0083:        import javax.xml.ws.WebServiceException;
0084:
0085:        /**
0086:         *
0087:         * @author Jakub Podlesak (jakub.podlesak at sun.com)
0088:         */
0089:        final public class PolicyWSDLParserExtension extends
0090:                WSDLParserExtension {
0091:
0092:            enum HandlerType {
0093:                PolicyUri, AnonymousPolicyId
0094:            }
0095:
0096:            final static class PolicyRecordHandler {
0097:                String handler;
0098:                HandlerType type;
0099:
0100:                PolicyRecordHandler(HandlerType type, String handler) {
0101:                    this .type = type;
0102:                    this .handler = handler;
0103:                }
0104:
0105:                HandlerType getType() {
0106:                    return type;
0107:                }
0108:
0109:                String getHandler() {
0110:                    return handler;
0111:                }
0112:            }
0113:
0114:            final static class PolicyRecord {
0115:                PolicyRecord next;
0116:                String uri;
0117:                PolicySourceModel policyModel;
0118:                Set<String> unresolvedURIs;
0119:
0120:                PolicyRecord() {
0121:                    // nothing to initialize
0122:                }
0123:
0124:                PolicyRecord insert(final PolicyRecord insertedRec) {
0125:                    if (null == insertedRec.unresolvedURIs
0126:                            || insertedRec.unresolvedURIs.isEmpty()) {
0127:                        insertedRec.next = this ;
0128:                        return insertedRec;
0129:                    }
0130:                    final PolicyRecord head = this ;
0131:                    PolicyRecord oneBeforeCurrent = null;
0132:                    PolicyRecord current;
0133:                    for (current = head; null != current.next;) {
0134:                        if ((null != current.unresolvedURIs)
0135:                                && current.unresolvedURIs
0136:                                        .contains(insertedRec.uri)) {
0137:                            if (null == oneBeforeCurrent) {
0138:                                insertedRec.next = current;
0139:                                return insertedRec;
0140:                            } else { // oneBeforeCurrent != null
0141:                                oneBeforeCurrent.next = insertedRec;
0142:                                insertedRec.next = current;
0143:                                return head;
0144:                            } // end-if-else oneBeforeCurrent == null
0145:                        }// end-if current record depends on inserted one
0146:                        if (insertedRec.unresolvedURIs.remove(current.uri)
0147:                                && (insertedRec.unresolvedURIs.isEmpty())) {
0148:                            insertedRec.next = current.next;
0149:                            current.next = insertedRec;
0150:                            return head;
0151:                        } // end-if one of unresolved URIs resolved by current record and thus unresolvedURIs empty
0152:                        oneBeforeCurrent = current;
0153:                        current = current.next;
0154:                    } // end for (current = head; null!=current.next; )
0155:                    insertedRec.next = null;
0156:                    current.next = insertedRec;
0157:                    return head;
0158:                }
0159:
0160:                public String toString() {
0161:                    String result = uri;
0162:                    if (null != next) {
0163:                        result += "->" + next.toString();
0164:                    }
0165:                    return result;
0166:                }
0167:            }
0168:
0169:            private static final PolicyLogger LOGGER = PolicyLogger
0170:                    .getLogger(PolicyWSDLParserExtension.class);
0171:
0172:            //anonymous policy id prefix
0173:            private static final StringBuffer AnonymnousPolicyIdPrefix = new StringBuffer(
0174:                    "#__anonymousPolicy__ID");
0175:
0176:            // anonymous policies count
0177:            private int anonymousPoliciesCount;
0178:
0179:            // are we parsing config file?
0180:            private boolean isForConfigFile = false;
0181:
0182:            // policy queue -- needed for evaluating the right order policy of policy models expansion
0183:            private PolicyRecord expandQueueHead = null;
0184:
0185:            // storage for policy models with an id passed by
0186:            private Map<String, PolicyRecord> policyRecordsPassedBy = null;
0187:            // storage for anonymous policies defined within given WSDL
0188:            private Map<String, PolicySourceModel> anonymousPolicyModels = null;
0189:
0190:            // container for URIs of policies referenced
0191:            private List<String> unresolvedUris = null;
0192:
0193:            // urls of xml docs policies were read from
0194:            final Set<String> urlsRead = new HashSet<String>();
0195:
0196:            // structures for policies really needed to build a map
0197:            private final LinkedList<String> urisNeeded = new LinkedList<String>();
0198:            private final Map<String, PolicySourceModel> modelsNeeded = new HashMap<String, PolicySourceModel>();
0199:
0200:            // lookup tables for Policy attachments found
0201:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4ServiceMap = null;
0202:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4PortMap = null;
0203:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4PortTypeMap = null;
0204:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4BindingMap = null;
0205:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4BoundOperationMap = null;
0206:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4OperationMap = null;
0207:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4MessageMap = null;
0208:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4InputMap = null;
0209:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4OutputMap = null;
0210:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4FaultMap = null;
0211:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4BindingInputOpMap = null;
0212:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4BindingOutputOpMap = null;
0213:            private Map<WSDLObject, Collection<PolicyRecordHandler>> handlers4BindingFaultOpMap = null;
0214:
0215:            private PolicyMapBuilder policyBuilder = new PolicyMapBuilder();
0216:
0217:            private PolicyMapMutator[] externalMutators;
0218:
0219:            private boolean isPolicyProcessed(final String policyUri) {
0220:                return modelsNeeded.containsKey(policyUri);
0221:            }
0222:
0223:            private void addNewPolicyNeeded(final String policyUri,
0224:                    final PolicySourceModel policyModel) {
0225:                if (!modelsNeeded.containsKey(policyUri)) {
0226:                    modelsNeeded.put(policyUri, policyModel);
0227:                    urisNeeded.addFirst(policyUri);
0228:                }
0229:            }
0230:
0231:            private Map<String, PolicySourceModel> getPolicyModels() {
0232:                return modelsNeeded;
0233:            }
0234:
0235:            private Map<String, PolicyRecord> getPolicyRecordsPassedBy() {
0236:                if (null == policyRecordsPassedBy) {
0237:                    policyRecordsPassedBy = new HashMap<String, PolicyRecord>();
0238:                }
0239:                return policyRecordsPassedBy;
0240:            }
0241:
0242:            private Map<String, PolicySourceModel> getAnonymousPolicyModels() {
0243:                if (null == anonymousPolicyModels) {
0244:                    anonymousPolicyModels = new HashMap<String, PolicySourceModel>();
0245:                }
0246:                return anonymousPolicyModels;
0247:            }
0248:
0249:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4ServiceMap() {
0250:                if (null == handlers4ServiceMap) {
0251:                    handlers4ServiceMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0252:                }
0253:                return handlers4ServiceMap;
0254:            }
0255:
0256:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4PortMap() {
0257:                if (null == handlers4PortMap) {
0258:                    handlers4PortMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0259:                }
0260:                return handlers4PortMap;
0261:            }
0262:
0263:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4PortTypeMap() {
0264:                if (null == handlers4PortTypeMap) {
0265:                    handlers4PortTypeMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0266:                }
0267:                return handlers4PortTypeMap;
0268:            }
0269:
0270:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4BindingMap() {
0271:                if (null == handlers4BindingMap) {
0272:                    handlers4BindingMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0273:                }
0274:                return handlers4BindingMap;
0275:            }
0276:
0277:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4OperationMap() {
0278:                if (null == handlers4OperationMap) {
0279:                    handlers4OperationMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0280:                }
0281:                return handlers4OperationMap;
0282:            }
0283:
0284:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4BoundOperationMap() {
0285:                if (null == handlers4BoundOperationMap) {
0286:                    handlers4BoundOperationMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0287:                }
0288:                return handlers4BoundOperationMap;
0289:            }
0290:
0291:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4MessageMap() {
0292:                if (null == handlers4MessageMap) {
0293:                    handlers4MessageMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0294:                }
0295:                return handlers4MessageMap;
0296:            }
0297:
0298:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4InputMap() {
0299:                if (null == handlers4InputMap) {
0300:                    handlers4InputMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0301:                }
0302:                return handlers4InputMap;
0303:            }
0304:
0305:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4OutputMap() {
0306:                if (null == handlers4OutputMap) {
0307:                    handlers4OutputMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0308:                }
0309:                return handlers4OutputMap;
0310:            }
0311:
0312:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4FaultMap() {
0313:                if (null == handlers4FaultMap) {
0314:                    handlers4FaultMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0315:                }
0316:                return handlers4FaultMap;
0317:            }
0318:
0319:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4BindingInputOpMap() {
0320:                if (null == handlers4BindingInputOpMap) {
0321:                    handlers4BindingInputOpMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0322:                }
0323:                return handlers4BindingInputOpMap;
0324:            }
0325:
0326:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4BindingOutputOpMap() {
0327:                if (null == handlers4BindingOutputOpMap) {
0328:                    handlers4BindingOutputOpMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0329:                }
0330:                return handlers4BindingOutputOpMap;
0331:            }
0332:
0333:            private Map<WSDLObject, Collection<PolicyRecordHandler>> getHandlers4BindingFaultOpMap() {
0334:                if (null == handlers4BindingFaultOpMap) {
0335:                    handlers4BindingFaultOpMap = new HashMap<WSDLObject, Collection<PolicyRecordHandler>>();
0336:                }
0337:                return handlers4BindingFaultOpMap;
0338:            }
0339:
0340:            private List<String> getUnresolvedUris(final boolean emptyListNeeded) {
0341:                if ((null == unresolvedUris) || emptyListNeeded) {
0342:                    unresolvedUris = new LinkedList<String>();
0343:                }
0344:                return unresolvedUris;
0345:            }
0346:
0347:            private void policyRecToExpandQueue(final PolicyRecord policyRec) {
0348:                if (null == expandQueueHead) {
0349:                    expandQueueHead = policyRec;
0350:                } else {
0351:                    expandQueueHead = expandQueueHead.insert(policyRec);
0352:                }
0353:            }
0354:
0355:            /**
0356:             * Creates a new instance of PolicyWSDLParserExtension
0357:             */
0358:            public PolicyWSDLParserExtension() {
0359:                this (false, (PolicyMapMutator[]) null);
0360:            }
0361:
0362:            /**
0363:             * Creates a new instance of PolicyWSDLParserExtension.
0364:             * Allows you to register several instances of {@link com.sun.xml.ws.policy.PolicyMapMutator}
0365:             * to the newly populated {@link com.sun.xml.ws.policy.PolicyMap} to make changes to the map later.
0366:             */
0367:            public PolicyWSDLParserExtension(
0368:                    PolicyMapMutator... externalMutators) {
0369:                this (false, externalMutators);
0370:            }
0371:
0372:            /**
0373:             * Creates a new instance of PolicyWSDLParserExtension
0374:             */
0375:            public PolicyWSDLParserExtension(boolean isForConfigFile,
0376:                    PolicyMapMutator... externalMutators) {
0377:                this .isForConfigFile = isForConfigFile;
0378:                if (null != externalMutators) {
0379:                    this .externalMutators = new PolicyMapMutator[externalMutators.length];
0380:                    System.arraycopy(externalMutators, 0,
0381:                            this .externalMutators, 0, externalMutators.length);
0382:                }
0383:            }
0384:
0385:            private PolicyRecordHandler readSinglePolicy(
0386:                    final PolicyRecord policyRec, final boolean inner) {
0387:                PolicyRecordHandler handler = null;
0388:                String policyId = policyRec.policyModel.getPolicyId();
0389:                if (policyId == null) {
0390:                    policyId = policyRec.policyModel.getPolicyName();
0391:                }
0392:                if (policyId != null) { // policy id defined, keep the policy
0393:                    handler = new PolicyRecordHandler(HandlerType.PolicyUri,
0394:                            policyRec.uri);
0395:                    getPolicyRecordsPassedBy().put(policyRec.uri, policyRec);
0396:                    policyRecToExpandQueue(policyRec);
0397:                } else if (inner) { // no id given to the policy --> keep as an annonymous policy model
0398:                    final String anonymousId = AnonymnousPolicyIdPrefix.append(
0399:                            anonymousPoliciesCount++).toString();
0400:                    handler = new PolicyRecordHandler(
0401:                            HandlerType.AnonymousPolicyId, anonymousId);
0402:                    getAnonymousPolicyModels().put(anonymousId,
0403:                            policyRec.policyModel);
0404:                    if (null != policyRec.unresolvedURIs) {
0405:                        getUnresolvedUris(false).addAll(
0406:                                policyRec.unresolvedURIs);
0407:                    }
0408:                }
0409:                return handler;
0410:            }
0411:
0412:            private void addHandlerToMap(
0413:                    final Map<WSDLObject, Collection<PolicyRecordHandler>> map,
0414:                    final WSDLObject key, final PolicyRecordHandler handler) {
0415:                if (map.containsKey(key)) {
0416:                    map.get(key).add(handler);
0417:                } else {
0418:                    final Collection<PolicyRecordHandler> newSet = new LinkedList<PolicyRecordHandler>();
0419:                    newSet.add(handler);
0420:                    map.put(key, newSet);
0421:                }
0422:            }
0423:
0424:            private String getBaseUrl(final String policyUri) {
0425:                if (null == policyUri) {
0426:                    return null;
0427:                }
0428:                // TODO: encoded urls (escaped characters) might be a problem ?
0429:                final int fragmentIdx = policyUri.indexOf('#');
0430:                return (fragmentIdx == -1) ? policyUri : policyUri.substring(0,
0431:                        fragmentIdx);
0432:            }
0433:
0434:            private String relativeToAbsoluteUrl(final String relativeUri,
0435:                    final String baseUri) {
0436:                if ('#' != relativeUri.charAt(0)) { // TODO: escaped char could be an issue?
0437:                    return relativeUri; // absolute already
0438:                }
0439:                return (null == baseUri) ? relativeUri : baseUri + relativeUri;
0440:            }
0441:
0442:            // adding current url even to locally referenced policies
0443:            // in order to distinguish imported policies
0444:            private void processReferenceUri(final String policyUri,
0445:                    final WSDLObject element, final XMLStreamReader reader,
0446:                    final Map<WSDLObject, Collection<PolicyRecordHandler>> map) {
0447:
0448:                if (null == policyUri) {
0449:                    return;
0450:                }
0451:                if ('#' != policyUri.charAt(0)) { // external uri (already)
0452:                    getUnresolvedUris(false).add(policyUri);
0453:                }
0454:
0455:                addHandlerToMap(map, element, new PolicyRecordHandler(
0456:                        HandlerType.PolicyUri, relativeToAbsoluteUrl(policyUri,
0457:                                reader.getLocation().getSystemId())));
0458:            }
0459:
0460:            private boolean processSubelement(final WSDLObject element,
0461:                    final XMLStreamReader reader,
0462:                    final Map<WSDLObject, Collection<PolicyRecordHandler>> map) {
0463:                if (PolicyConstants.POLICY_REFERENCE.equals(reader.getName())) { // "PolicyReference" element interests us
0464:                    processReferenceUri(readPolicyReferenceElement(reader),
0465:                            element, reader, map);
0466:                    return true;
0467:                } else if (PolicyConstants.POLICY.equals(reader.getName())) { // policy could be defined here
0468:                    final PolicyRecordHandler handler = readSinglePolicy(
0469:                            skipPolicyElement(reader, (null == reader
0470:                                    .getLocation().getSystemId()) ? // baseUrl
0471:                            ""
0472:                                    : reader.getLocation().getSystemId()), true);
0473:                    if (null != handler) { // only policies with an Id can work for us
0474:                        addHandlerToMap(map, element, handler);
0475:                    } // endif null != handler
0476:                    return true; // element consumed
0477:                }//end if Policy element found
0478:                return false;
0479:            }
0480:
0481:            private void processAttributes(final WSDLObject element,
0482:                    final XMLStreamReader reader,
0483:                    final Map<WSDLObject, Collection<PolicyRecordHandler>> map) {
0484:                final String[] uriArray = getPolicyURIsFromAttr(reader);
0485:                if (null != uriArray) {
0486:                    for (String policyUri : uriArray) {
0487:                        processReferenceUri(policyUri, element, reader, map);
0488:                    }
0489:                }
0490:            }
0491:
0492:            public boolean portElements(final WSDLPort port,
0493:                    final XMLStreamReader reader) {
0494:                LOGGER.entering();
0495:                final boolean result = processSubelement(port, reader,
0496:                        getHandlers4PortMap());
0497:                LOGGER.exiting();
0498:                return result;
0499:            }
0500:
0501:            public void portAttributes(final WSDLPort port,
0502:                    final XMLStreamReader reader) {
0503:                LOGGER.entering();
0504:                processAttributes(port, reader, getHandlers4PortMap());
0505:                LOGGER.exiting();
0506:            }
0507:
0508:            public boolean serviceElements(final WSDLService service,
0509:                    final XMLStreamReader reader) {
0510:                LOGGER.entering();
0511:                final boolean result = processSubelement(service, reader,
0512:                        getHandlers4ServiceMap());
0513:                LOGGER.exiting();
0514:                return result;
0515:            }
0516:
0517:            public void serviceAttributes(final WSDLService service,
0518:                    final XMLStreamReader reader) {
0519:                LOGGER.entering();
0520:                processAttributes(service, reader, getHandlers4ServiceMap());
0521:                LOGGER.exiting();
0522:            }
0523:
0524:            public boolean definitionsElements(final XMLStreamReader reader) {
0525:                LOGGER.entering();
0526:                if (PolicyConstants.POLICY.equals(reader.getName())) { // Only "Policy" element interests me
0527:                    readSinglePolicy(skipPolicyElement(reader, (null == reader
0528:                            .getLocation().getSystemId()) ? // baseUrl
0529:                    ""
0530:                            : reader.getLocation().getSystemId()), false);
0531:                    LOGGER.exiting();
0532:                    return true;
0533:                }
0534:                LOGGER.exiting();
0535:                return false;
0536:            }
0537:
0538:            public boolean bindingElements(final WSDLBoundPortType binding,
0539:                    final XMLStreamReader reader) {
0540:                LOGGER.entering();
0541:                final boolean result = processSubelement(binding, reader,
0542:                        getHandlers4BindingMap());
0543:                LOGGER.exiting();
0544:                return result;
0545:            }
0546:
0547:            public void bindingAttributes(final WSDLBoundPortType binding,
0548:                    final XMLStreamReader reader) {
0549:                LOGGER.entering();
0550:                processAttributes(binding, reader, getHandlers4BindingMap());
0551:                LOGGER.exiting();
0552:            }
0553:
0554:            public boolean portTypeElements(final WSDLPortType portType,
0555:                    final XMLStreamReader reader) {
0556:                LOGGER.entering();
0557:                final boolean result = processSubelement(portType, reader,
0558:                        getHandlers4PortTypeMap());
0559:                LOGGER.exiting();
0560:                return result;
0561:            }
0562:
0563:            public void portTypeAttributes(final WSDLPortType portType,
0564:                    final XMLStreamReader reader) {
0565:                LOGGER.entering();
0566:                processAttributes(portType, reader, getHandlers4PortTypeMap());
0567:                LOGGER.exiting();
0568:            }
0569:
0570:            public boolean portTypeOperationElements(
0571:                    final WSDLOperation operation, final XMLStreamReader reader) {
0572:                LOGGER.entering();
0573:                final boolean result = processSubelement(operation, reader,
0574:                        getHandlers4OperationMap());
0575:                LOGGER.exiting();
0576:                return result;
0577:            }
0578:
0579:            public void portTypeOperationAttributes(
0580:                    final WSDLOperation operation, final XMLStreamReader reader) {
0581:                LOGGER.entering();
0582:                processAttributes(operation, reader, getHandlers4OperationMap());
0583:                LOGGER.exiting();
0584:            }
0585:
0586:            public boolean bindingOperationElements(
0587:                    final WSDLBoundOperation boundOperation,
0588:                    final XMLStreamReader reader) {
0589:                LOGGER.entering();
0590:                final boolean result = processSubelement(boundOperation,
0591:                        reader, getHandlers4BoundOperationMap());
0592:                LOGGER.exiting();
0593:                return result;
0594:            }
0595:
0596:            public void bindingOperationAttributes(
0597:                    final WSDLBoundOperation boundOperation,
0598:                    final XMLStreamReader reader) {
0599:                LOGGER.entering();
0600:                processAttributes(boundOperation, reader,
0601:                        getHandlers4BoundOperationMap());
0602:                LOGGER.exiting();
0603:            }
0604:
0605:            public boolean messageElements(final WSDLMessage msg,
0606:                    final XMLStreamReader reader) {
0607:                LOGGER.entering();
0608:                final boolean result = processSubelement(msg, reader,
0609:                        getHandlers4MessageMap());
0610:                LOGGER.exiting();
0611:                return result;
0612:            }
0613:
0614:            public void messageAttributes(final WSDLMessage msg,
0615:                    final XMLStreamReader reader) {
0616:                LOGGER.entering();
0617:                processAttributes(msg, reader, getHandlers4MessageMap());
0618:                LOGGER.exiting();
0619:            }
0620:
0621:            public boolean portTypeOperationInputElements(
0622:                    final WSDLInput input, final XMLStreamReader reader) {
0623:                LOGGER.entering();
0624:                final boolean result = processSubelement(input, reader,
0625:                        getHandlers4InputMap());
0626:                LOGGER.exiting();
0627:                return result;
0628:            }
0629:
0630:            public void portTypeOperationInputAttributes(final WSDLInput input,
0631:                    final XMLStreamReader reader) {
0632:                LOGGER.entering();
0633:                processAttributes(input, reader, getHandlers4InputMap());
0634:                LOGGER.exiting();
0635:            }
0636:
0637:            public boolean portTypeOperationOutputElements(
0638:                    final WSDLOutput output, final XMLStreamReader reader) {
0639:                LOGGER.entering();
0640:                final boolean result = processSubelement(output, reader,
0641:                        getHandlers4OutputMap());
0642:                LOGGER.exiting();
0643:                return result;
0644:            }
0645:
0646:            public void portTypeOperationOutputAttributes(
0647:                    final WSDLOutput output, final XMLStreamReader reader) {
0648:                LOGGER.entering();
0649:                processAttributes(output, reader, getHandlers4OutputMap());
0650:                LOGGER.exiting();
0651:            }
0652:
0653:            public boolean portTypeOperationFaultElements(
0654:                    final WSDLFault fault, final XMLStreamReader reader) {
0655:                LOGGER.entering();
0656:                final boolean result = processSubelement(fault, reader,
0657:                        getHandlers4FaultMap());
0658:                LOGGER.exiting();
0659:                return result;
0660:            }
0661:
0662:            public void portTypeOperationFaultAttributes(final WSDLFault fault,
0663:                    final XMLStreamReader reader) {
0664:                LOGGER.entering();
0665:                processAttributes(fault, reader, getHandlers4FaultMap());
0666:                LOGGER.exiting();
0667:            }
0668:
0669:            public boolean bindingOperationInputElements(
0670:                    final WSDLBoundOperation operation,
0671:                    final XMLStreamReader reader) {
0672:                LOGGER.entering();
0673:                final boolean result = processSubelement(operation, reader,
0674:                        getHandlers4BindingInputOpMap());
0675:                LOGGER.exiting();
0676:                return result;
0677:            }
0678:
0679:            public void bindingOperationInputAttributes(
0680:                    final WSDLBoundOperation operation,
0681:                    final XMLStreamReader reader) {
0682:                LOGGER.entering();
0683:                processAttributes(operation, reader,
0684:                        getHandlers4BindingInputOpMap());
0685:                LOGGER.exiting();
0686:            }
0687:
0688:            public boolean bindingOperationOutputElements(
0689:                    final WSDLBoundOperation operation,
0690:                    final XMLStreamReader reader) {
0691:                LOGGER.entering();
0692:                final boolean result = processSubelement(operation, reader,
0693:                        getHandlers4BindingOutputOpMap());
0694:                LOGGER.exiting();
0695:                return result;
0696:            }
0697:
0698:            public void bindingOperationOutputAttributes(
0699:                    final WSDLBoundOperation operation,
0700:                    final XMLStreamReader reader) {
0701:                LOGGER.entering();
0702:                processAttributes(operation, reader,
0703:                        getHandlers4BindingOutputOpMap());
0704:                LOGGER.exiting();
0705:            }
0706:
0707:            public boolean bindingOperationFaultElements(
0708:                    final WSDLBoundFault fault, final XMLStreamReader reader) {
0709:                LOGGER.entering();
0710:                final boolean result = processSubelement(fault, reader,
0711:                        getHandlers4BindingFaultOpMap());
0712:                LOGGER.exiting(result);
0713:                return result;
0714:            }
0715:
0716:            public void bindingOperationFaultAttributes(
0717:                    final WSDLBoundFault fault, final XMLStreamReader reader) {
0718:                LOGGER.entering();
0719:                processAttributes(fault, reader,
0720:                        getHandlers4BindingFaultOpMap());
0721:                LOGGER.exiting();
0722:            }
0723:
0724:            private PolicyMapBuilder getPolicyMapBuilder() {
0725:                if (null == policyBuilder) {
0726:                    policyBuilder = new PolicyMapBuilder();
0727:                }
0728:                return policyBuilder;
0729:            }
0730:
0731:            private Collection<String> getPolicyURIs(
0732:                    final Collection<PolicyRecordHandler> handlers,
0733:                    final PolicySourceModelContext modelContext)
0734:                    throws PolicyException {
0735:                final Collection<String> result = new ArrayList<String>(
0736:                        handlers.size());
0737:                String policyUri;
0738:                for (PolicyRecordHandler handler : handlers) {
0739:                    policyUri = handler.handler;
0740:                    if (HandlerType.AnonymousPolicyId == handler.type) {
0741:                        final PolicySourceModel policyModel = getAnonymousPolicyModels()
0742:                                .get(policyUri);
0743:                        policyModel.expand(modelContext);
0744:                        while (getPolicyModels().containsKey(policyUri)) {
0745:                            policyUri = AnonymnousPolicyIdPrefix.append(
0746:                                    anonymousPoliciesCount++).toString();
0747:                        }
0748:                        getPolicyModels().put(policyUri, policyModel);
0749:                    }
0750:                    result.add(policyUri);
0751:                }
0752:                return result;
0753:            }
0754:
0755:            private boolean readExternalFile(final String fileUrl) {
0756:                InputStream ios = null;
0757:                XMLStreamReader reader = null;
0758:                try {
0759:                    final URL xmlURL = new URL(fileUrl);
0760:                    ios = xmlURL.openStream();
0761:                    reader = XMLInputFactory.newInstance()
0762:                            .createXMLStreamReader(ios);
0763:                    while (reader.hasNext()) {
0764:                        if (reader.isStartElement()
0765:                                && PolicyConstants.POLICY.equals(reader
0766:                                        .getName())) {
0767:                            readSinglePolicy(
0768:                                    skipPolicyElement(reader, fileUrl), false);
0769:                        }
0770:                        reader.next();
0771:                    }
0772:                    return true;
0773:                } catch (IOException ioe) {
0774:                    return false;
0775:                } catch (XMLStreamException xmlse) {
0776:                    return false;
0777:                } finally {
0778:                    PolicyUtils.IO.closeResource(reader);
0779:                    PolicyUtils.IO.closeResource(ios);
0780:                }
0781:            }
0782:
0783:            public void finished(final WSDLParserExtensionContext context) {
0784:                LOGGER.entering();
0785:                // need to make sure proper beginning order of internal policies within unresolvedUris list
0786:                if (null != expandQueueHead) { // any policies found
0787:                    final List<String> externalUris = getUnresolvedUris(false); // protect list of possible external policies
0788:                    getUnresolvedUris(true); // cleaning up the list only
0789:                    final LinkedList<String> baseUnresolvedUris = new LinkedList<String>();
0790:                    for (PolicyRecord currentRec = expandQueueHead; null != currentRec; currentRec = currentRec.next) {
0791:                        baseUnresolvedUris.addFirst(currentRec.uri);
0792:                    }
0793:                    getUnresolvedUris(false).addAll(baseUnresolvedUris);
0794:                    expandQueueHead = null; // cut the queue off
0795:                    getUnresolvedUris(false).addAll(externalUris);
0796:                }
0797:                //        final Set<String> urlsRead = new HashSet<String>();
0798:                //        urlsRead.add("");
0799:                while (!getUnresolvedUris(false).isEmpty()) {
0800:                    final List<String> urisToBeSolvedList = getUnresolvedUris(false);
0801:                    getUnresolvedUris(true); // just cleaning up the list
0802:                    for (String currentUri : urisToBeSolvedList) {
0803:                        if (!isPolicyProcessed(currentUri)) {
0804:                            final PolicyRecord prefetchedRecord = getPolicyRecordsPassedBy()
0805:                                    .get(currentUri);
0806:                            if (null == prefetchedRecord) {
0807:                                if (urlsRead.contains(getBaseUrl(currentUri))) { // big problem --> unresolvable policy
0808:                                    LOGGER
0809:                                            .logSevereException(new PolicyException(
0810:                                                    LocalizationMessages
0811:                                                            .WSP_1042_CAN_NOT_FIND_POLICY(currentUri)));
0812:                                } else {
0813:                                    if (readExternalFile(getBaseUrl(currentUri))) {
0814:                                        getUnresolvedUris(false)
0815:                                                .add(currentUri);
0816:                                    }
0817:                                }
0818:                            } else { // policy has not been yet passed by
0819:                                if (null != prefetchedRecord.unresolvedURIs) {
0820:                                    getUnresolvedUris(false).addAll(
0821:                                            prefetchedRecord.unresolvedURIs);
0822:                                } // end-if null != prefetchedRecord.unresolvedURIs
0823:                                addNewPolicyNeeded(currentUri,
0824:                                        prefetchedRecord.policyModel);
0825:                            }
0826:                        } // end-if policy already processed
0827:                    } // end-foreach unresolved uris
0828:                }
0829:                final PolicySourceModelContext modelContext = PolicySourceModelContext
0830:                        .createContext();
0831:                for (String policyUri : urisNeeded) {
0832:                    final PolicySourceModel sourceModel = modelsNeeded
0833:                            .get(policyUri);
0834:                    try {
0835:                        sourceModel.expand(modelContext);
0836:                        modelContext.addModel(new URI(policyUri), sourceModel);
0837:                    } catch (URISyntaxException e) {
0838:                        LOGGER.logSevereException(e);
0839:                    } catch (PolicyException e) {
0840:                        LOGGER.logSevereException(e);
0841:                    }
0842:                }
0843:
0844:                // Start-preparation of policy map builder
0845:                // iterating over all services and binding all the policies read before
0846:                try {
0847:                    // messageSet holds the handlers for all wsdl:message elements. There
0848:                    // may otherwise be multiple entries for policies that are contained
0849:                    // by fault messages.
0850:                    HashSet<BuilderHandlerMessageScope> messageSet = new HashSet<BuilderHandlerMessageScope>();
0851:                    for (WSDLService service : context.getWSDLModel()
0852:                            .getServices().values()) {
0853:                        if (getHandlers4ServiceMap().containsKey(service)) {
0854:                            getPolicyMapBuilder().registerHandler(
0855:                                    new BuilderHandlerServiceScope(
0856:                                            getPolicyURIs(
0857:                                                    getHandlers4ServiceMap()
0858:                                                            .get(service),
0859:                                                    modelContext),
0860:                                            getPolicyModels(), service, service
0861:                                                    .getName()));
0862:                        }
0863:                        // end service scope
0864:
0865:                        for (WSDLPort port : service.getPorts()) {
0866:                            if (getHandlers4PortMap().containsKey(port)) {
0867:                                getPolicyMapBuilder().registerHandler(
0868:                                        new BuilderHandlerEndpointScope(
0869:                                                getPolicyURIs(
0870:                                                        getHandlers4PortMap()
0871:                                                                .get(port),
0872:                                                        modelContext),
0873:                                                getPolicyModels(), port, port
0874:                                                        .getOwner().getName(),
0875:                                                port.getName()));
0876:                            }
0877:                            if ( // port.getBinding may not be null, but in case ...
0878:                            null != port.getBinding()) {
0879:                                if ( // handler for binding
0880:                                getHandlers4BindingMap().containsKey(
0881:                                        port.getBinding())) {
0882:                                    getPolicyMapBuilder()
0883:                                            .registerHandler(
0884:                                                    new BuilderHandlerEndpointScope(
0885:                                                            getPolicyURIs(
0886:                                                                    getHandlers4BindingMap()
0887:                                                                            .get(
0888:                                                                                    port
0889:                                                                                            .getBinding()),
0890:                                                                    modelContext),
0891:                                                            getPolicyModels(),
0892:                                                            port.getBinding(),
0893:                                                            service.getName(),
0894:                                                            port.getName()));
0895:                                } // endif handler for binding
0896:                                if ( // handler for port type
0897:                                getHandlers4PortTypeMap().containsKey(
0898:                                        port.getBinding().getPortType())) {
0899:                                    getPolicyMapBuilder()
0900:                                            .registerHandler(
0901:                                                    new BuilderHandlerEndpointScope(
0902:                                                            getPolicyURIs(
0903:                                                                    getHandlers4PortTypeMap()
0904:                                                                            .get(
0905:                                                                                    port
0906:                                                                                            .getBinding()
0907:                                                                                            .getPortType()),
0908:                                                                    modelContext),
0909:                                                            getPolicyModels(),
0910:                                                            port
0911:                                                                    .getBinding()
0912:                                                                    .getPortType(),
0913:                                                            service.getName(),
0914:                                                            port.getName()));
0915:                                } // endif handler for port type
0916:                                // end endpoint scope
0917:
0918:                                for (WSDLBoundOperation boundOperation : port
0919:                                        .getBinding().getBindingOperations()) {
0920:
0921:                                    final WSDLOperation operation = boundOperation
0922:                                            .getOperation();
0923:                                    if ( // handler for operation scope -- by boundOperation
0924:                                    getHandlers4BoundOperationMap()
0925:                                            .containsKey(boundOperation)) {
0926:                                        getPolicyMapBuilder()
0927:                                                .registerHandler(
0928:                                                        new BuilderHandlerOperationScope(
0929:                                                                getPolicyURIs(
0930:                                                                        getHandlers4BoundOperationMap()
0931:                                                                                .get(
0932:                                                                                        boundOperation),
0933:                                                                        modelContext),
0934:                                                                getPolicyModels(),
0935:                                                                boundOperation,
0936:                                                                service
0937:                                                                        .getName(),
0938:                                                                port.getName(),
0939:                                                                operation
0940:                                                                        .getName()));
0941:                                    } // endif handler for binding:operation scope
0942:                                    if ( // handler for operation scope -- by operation map
0943:                                    getHandlers4OperationMap().containsKey(
0944:                                            operation)) {
0945:                                        getPolicyMapBuilder()
0946:                                                .registerHandler(
0947:                                                        new BuilderHandlerOperationScope(
0948:                                                                getPolicyURIs(
0949:                                                                        getHandlers4OperationMap()
0950:                                                                                .get(
0951:                                                                                        operation),
0952:                                                                        modelContext),
0953:                                                                getPolicyModels(),
0954:                                                                operation,
0955:                                                                service
0956:                                                                        .getName(),
0957:                                                                port.getName(),
0958:                                                                operation
0959:                                                                        .getName()));
0960:                                    } // endif for portType:operation scope
0961:                                    // end operation scope
0962:
0963:                                    final WSDLInput input = operation
0964:                                            .getInput();
0965:                                    if (null != input) {
0966:                                        WSDLMessage inputMsg = input
0967:                                                .getMessage();
0968:                                        if (inputMsg != null
0969:                                                && getHandlers4MessageMap()
0970:                                                        .containsKey(inputMsg)) {
0971:                                            messageSet
0972:                                                    .add(new BuilderHandlerMessageScope(
0973:                                                            getPolicyURIs(
0974:                                                                    getHandlers4MessageMap()
0975:                                                                            .get(
0976:                                                                                    inputMsg),
0977:                                                                    modelContext),
0978:                                                            getPolicyModels(),
0979:                                                            inputMsg,
0980:                                                            BuilderHandlerMessageScope.Scope.InputMessageScope,
0981:                                                            service.getName(),
0982:                                                            port.getName(),
0983:                                                            operation.getName(),
0984:                                                            null));
0985:                                        }
0986:                                    }
0987:                                    if ( // binding op input msg
0988:                                    getHandlers4BindingInputOpMap()
0989:                                            .containsKey(boundOperation)) {
0990:                                        getPolicyMapBuilder()
0991:                                                .registerHandler(
0992:                                                        new BuilderHandlerMessageScope(
0993:                                                                getPolicyURIs(
0994:                                                                        getHandlers4BindingInputOpMap()
0995:                                                                                .get(
0996:                                                                                        boundOperation),
0997:                                                                        modelContext),
0998:                                                                getPolicyModels(),
0999:                                                                boundOperation,
1000:                                                                BuilderHandlerMessageScope.Scope.InputMessageScope,
1001:                                                                service
1002:                                                                        .getName(),
1003:                                                                port.getName(),
1004:                                                                operation
1005:                                                                        .getName(),
1006:                                                                null));
1007:                                    } // endif binding op input msg
1008:                                    if (null != input // portType op input msg
1009:                                            && getHandlers4InputMap()
1010:                                                    .containsKey(input)) {
1011:                                        getPolicyMapBuilder()
1012:                                                .registerHandler(
1013:                                                        new BuilderHandlerMessageScope(
1014:                                                                getPolicyURIs(
1015:                                                                        getHandlers4InputMap()
1016:                                                                                .get(
1017:                                                                                        input),
1018:                                                                        modelContext),
1019:                                                                getPolicyModels(),
1020:                                                                input,
1021:                                                                BuilderHandlerMessageScope.Scope.InputMessageScope,
1022:                                                                service
1023:                                                                        .getName(),
1024:                                                                port.getName(),
1025:                                                                operation
1026:                                                                        .getName(),
1027:                                                                null));
1028:                                    } // endif portType op input msg
1029:                                    // end input message scope
1030:
1031:                                    final WSDLOutput output = operation
1032:                                            .getOutput();
1033:                                    if (null != output) {
1034:                                        WSDLMessage outputMsg = output
1035:                                                .getMessage();
1036:                                        if (outputMsg != null
1037:                                                && getHandlers4MessageMap()
1038:                                                        .containsKey(outputMsg)) {
1039:                                            messageSet
1040:                                                    .add(new BuilderHandlerMessageScope(
1041:                                                            getPolicyURIs(
1042:                                                                    getHandlers4MessageMap()
1043:                                                                            .get(
1044:                                                                                    outputMsg),
1045:                                                                    modelContext),
1046:                                                            getPolicyModels(),
1047:                                                            outputMsg,
1048:                                                            BuilderHandlerMessageScope.Scope.OutputMessageScope,
1049:                                                            service.getName(),
1050:                                                            port.getName(),
1051:                                                            operation.getName(),
1052:                                                            null));
1053:                                        }
1054:                                    }
1055:                                    if ( // binding op output msg
1056:                                    getHandlers4BindingOutputOpMap()
1057:                                            .containsKey(boundOperation)) {
1058:                                        getPolicyMapBuilder()
1059:                                                .registerHandler(
1060:                                                        new BuilderHandlerMessageScope(
1061:                                                                getPolicyURIs(
1062:                                                                        getHandlers4BindingOutputOpMap()
1063:                                                                                .get(
1064:                                                                                        boundOperation),
1065:                                                                        modelContext),
1066:                                                                getPolicyModels(),
1067:                                                                boundOperation,
1068:                                                                BuilderHandlerMessageScope.Scope.OutputMessageScope,
1069:                                                                service
1070:                                                                        .getName(),
1071:                                                                port.getName(),
1072:                                                                operation
1073:                                                                        .getName(),
1074:                                                                null));
1075:                                    } // endif binding op output msg
1076:                                    if (null != output // portType op output msg
1077:                                            && getHandlers4OutputMap()
1078:                                                    .containsKey(output)) {
1079:                                        getPolicyMapBuilder()
1080:                                                .registerHandler(
1081:                                                        new BuilderHandlerMessageScope(
1082:                                                                getPolicyURIs(
1083:                                                                        getHandlers4OutputMap()
1084:                                                                                .get(
1085:                                                                                        output),
1086:                                                                        modelContext),
1087:                                                                getPolicyModels(),
1088:                                                                output,
1089:                                                                BuilderHandlerMessageScope.Scope.OutputMessageScope,
1090:                                                                service
1091:                                                                        .getName(),
1092:                                                                port.getName(),
1093:                                                                operation
1094:                                                                        .getName(),
1095:                                                                null));
1096:                                    } // endif portType op output msg
1097:                                    // end output message scope
1098:
1099:                                    for (WSDLBoundFault boundFault : boundOperation
1100:                                            .getFaults()) {
1101:                                        final QName faultName = new QName(
1102:                                                boundOperation.getName()
1103:                                                        .getNamespaceURI(),
1104:                                                boundFault.getName());
1105:                                        final WSDLFault fault = boundFault
1106:                                                .getFault();
1107:                                        final WSDLMessage faultMessage = fault
1108:                                                .getMessage();
1109:                                        if (faultMessage != null
1110:                                                && getHandlers4MessageMap()
1111:                                                        .containsKey(
1112:                                                                faultMessage)) {
1113:                                            messageSet
1114:                                                    .add(new BuilderHandlerMessageScope(
1115:                                                            getPolicyURIs(
1116:                                                                    getHandlers4MessageMap()
1117:                                                                            .get(
1118:                                                                                    faultMessage),
1119:                                                                    modelContext),
1120:                                                            getPolicyModels(),
1121:                                                            new WSDLBoundFaultContainer(
1122:                                                                    boundFault,
1123:                                                                    boundOperation),
1124:                                                            BuilderHandlerMessageScope.Scope.FaultMessageScope,
1125:                                                            service.getName(),
1126:                                                            port.getName(),
1127:                                                            operation.getName(),
1128:                                                            faultName));
1129:                                        }
1130:                                        if (fault != null
1131:                                                && getHandlers4FaultMap()
1132:                                                        .containsKey(fault)) {
1133:                                            messageSet
1134:                                                    .add(new BuilderHandlerMessageScope(
1135:                                                            getPolicyURIs(
1136:                                                                    getHandlers4FaultMap()
1137:                                                                            .get(
1138:                                                                                    fault),
1139:                                                                    modelContext),
1140:                                                            getPolicyModels(),
1141:                                                            new WSDLBoundFaultContainer(
1142:                                                                    boundFault,
1143:                                                                    boundOperation),
1144:                                                            BuilderHandlerMessageScope.Scope.FaultMessageScope,
1145:                                                            service.getName(),
1146:                                                            port.getName(),
1147:                                                            operation.getName(),
1148:                                                            faultName));
1149:                                        }
1150:                                        if (getHandlers4BindingFaultOpMap()
1151:                                                .containsKey(boundFault)) {
1152:                                            messageSet
1153:                                                    .add(new BuilderHandlerMessageScope(
1154:                                                            getPolicyURIs(
1155:                                                                    getHandlers4BindingFaultOpMap()
1156:                                                                            .get(
1157:                                                                                    boundFault),
1158:                                                                    modelContext),
1159:                                                            getPolicyModels(),
1160:                                                            new WSDLBoundFaultContainer(
1161:                                                                    boundFault,
1162:                                                                    boundOperation),
1163:                                                            BuilderHandlerMessageScope.Scope.FaultMessageScope,
1164:                                                            service.getName(),
1165:                                                            port.getName(),
1166:                                                            operation.getName(),
1167:                                                            faultName));
1168:                                        }
1169:                                    } // end foreach binding operation fault msg
1170:                                    // end fault message scope
1171:
1172:                                } // end foreach boundOperation in port
1173:                            } // endif port.getBinding() != null
1174:                        } // end foreach port in service
1175:                    } // end foreach service in wsdl
1176:                    // Add handlers for wsdl:message elements
1177:                    for (BuilderHandlerMessageScope scopeHandler : messageSet) {
1178:                        getPolicyMapBuilder().registerHandler(scopeHandler);
1179:                    }
1180:                } catch (PolicyException e) {
1181:                    LOGGER.logSevereException(e);
1182:                }
1183:                // End-preparation of policy map builder
1184:
1185:                // finally register a wrapper for getting WSDL policy map
1186:
1187:                final EffectivePolicyModifier modifier = EffectivePolicyModifier
1188:                        .createEffectivePolicyModifier();
1189:                final PolicyMapExtender extender = PolicyMapExtender
1190:                        .createPolicyMapExtender();
1191:
1192:                try {
1193:                    if (null != externalMutators && externalMutators.length > 0) {
1194:                        PolicyMapMutator[] mutators = new PolicyMapMutator[externalMutators.length + 2];
1195:                        mutators[0] = modifier;
1196:                        mutators[1] = extender;
1197:                        System.arraycopy(externalMutators, 0, mutators, 2,
1198:                                externalMutators.length);
1199:                        context.getWSDLModel().addExtension(
1200:                                new WSDLPolicyMapWrapper(policyBuilder
1201:                                        .getPolicyMap(mutators), modifier,
1202:                                        extender));
1203:                    } else {
1204:                        context.getWSDLModel().addExtension(
1205:                                new WSDLPolicyMapWrapper(policyBuilder
1206:                                        .getPolicyMap(modifier, extender),
1207:                                        modifier, extender));
1208:                    }
1209:                } catch (PolicyException e) {
1210:                    LOGGER.logSevereException(e);
1211:                    throw LOGGER
1212:                            .logSevereException(new WebServiceException(
1213:                                    LocalizationMessages
1214:                                            .WSP_1018_POLICY_EXCEPTION_WHILE_FINISHING_PARSING_WSDL(),
1215:                                    e));
1216:                }
1217:
1218:                LOGGER.exiting();
1219:            }
1220:
1221:            /**
1222:             * Reads policy reference element <wsp:PolicyReference/> and returns referenced policy URI as String
1223:             */
1224:            private String readPolicyReferenceElement(
1225:                    final XMLStreamReader reader) {
1226:                try {
1227:                    if (PolicyConstants.POLICY_REFERENCE.equals(reader
1228:                            .getName())) { // "PolicyReference" element interests me
1229:                        for (int i = 0; i < reader.getAttributeCount(); i++) {
1230:                            if (PolicyConstants.POLICY_URI.getLocalPart()
1231:                                    .equals(
1232:                                            reader.getAttributeName(i)
1233:                                                    .getLocalPart())) {
1234:                                final String uriValue = reader
1235:                                        .getAttributeValue(i);
1236:                                reader.next();
1237:                                return uriValue;
1238:                            }
1239:                        }
1240:                    }
1241:                    reader.next();
1242:                    return null;
1243:                } catch (XMLStreamException e) {
1244:                    throw LOGGER
1245:                            .logSevereException(new WebServiceException(
1246:                                    LocalizationMessages
1247:                                            .WSP_1001_XML_EXCEPTION_WHEN_PROCESSING_POLICY_REFERENCE(),
1248:                                    e));
1249:                }
1250:            }
1251:
1252:            /**
1253:             * Reads policy reference URIs from PolicyURIs attribute and returns them as a String array
1254:             * returns null if there is no such attribute
1255:             */
1256:            private String[] getPolicyURIsFromAttr(final XMLStreamReader reader) {
1257:                final String policyURIs = reader.getAttributeValue(
1258:                        PolicyConstants.POLICY_URIs.getNamespaceURI(),
1259:                        PolicyConstants.POLICY_URIs.getLocalPart());
1260:                return (null == policyURIs) ? null : policyURIs
1261:                        .split("[\\n ]+");
1262:            }
1263:
1264:            /**
1265:             *  skips current element (should be in START_ELEMENT state) and returns its content as String
1266:             */
1267:            private PolicyRecord skipPolicyElement(
1268:                    final XMLStreamReader reader, final String baseUrl) {
1269:                if ((null == reader) || (!reader.isStartElement())) {
1270:                    return null;
1271:                }
1272:                final StringBuffer elementCode = new StringBuffer();
1273:                final PolicyRecord policyRec = new PolicyRecord();
1274:                final QName elementName = reader.getName();
1275:                boolean insidePolicyReferenceAttr;
1276:                int depth = 0;
1277:                try {
1278:                    do {
1279:                        switch (reader.getEventType()) {
1280:                        case XMLStreamConstants.START_ELEMENT: // process start of next element
1281:                            QName curName = reader.getName();
1282:                            insidePolicyReferenceAttr = PolicyConstants.POLICY_REFERENCE
1283:                                    .equals(curName);
1284:                            if (elementName.equals(curName)) { // it is our element !
1285:                                depth++; // we are then deeper
1286:                            }
1287:                            final StringBuffer xmlnsCode = new StringBuffer(); // take care about namespaces as well
1288:                            final Set<String> tmpNsSet = new HashSet<String>();
1289:                            if ((null == curName.getPrefix())
1290:                                    || ("".equals(curName.getPrefix()))) { // no prefix
1291:                                elementCode.append('<') // start tag
1292:                                        .append(curName.getLocalPart());
1293:                                xmlnsCode.append(" xmlns=\"").append(
1294:                                        curName.getNamespaceURI()).append('"');
1295:
1296:                            } else { // prefix presented
1297:                                elementCode.append('<')
1298:                                        // start tag
1299:                                        .append(curName.getPrefix())
1300:                                        .append(':').append(
1301:                                                curName.getLocalPart());
1302:                                xmlnsCode.append(" xmlns:").append(
1303:                                        curName.getPrefix()).append("=\"")
1304:                                        .append(curName.getNamespaceURI())
1305:                                        .append('"');
1306:                                tmpNsSet.add(curName.getPrefix());
1307:                            }
1308:                            final int attrCount = reader.getAttributeCount(); // process element attributes
1309:                            final StringBuffer attrCode = new StringBuffer();
1310:                            for (int i = 0; i < attrCount; i++) {
1311:                                boolean uriAttrFlg = false;
1312:                                if (insidePolicyReferenceAttr
1313:                                        && "URI".equals(reader
1314:                                                .getAttributeName(i)
1315:                                                .getLocalPart())) { // PolicyReference found
1316:                                    uriAttrFlg = true;
1317:                                    if (null == policyRec.unresolvedURIs) { // first such URI found
1318:                                        policyRec.unresolvedURIs = new HashSet<String>(); // initialize URIs set
1319:                                    }
1320:                                    policyRec.unresolvedURIs.add( // add the URI
1321:                                            relativeToAbsoluteUrl(reader
1322:                                                    .getAttributeValue(i),
1323:                                                    baseUrl));
1324:                                } // end-if PolicyReference attribute found
1325:                                if ("xmlns"
1326:                                        .equals(reader.getAttributePrefix(i))
1327:                                        && tmpNsSet.contains(reader
1328:                                                .getAttributeLocalName(i))) {
1329:                                    continue; // do not append already defined ns
1330:                                }
1331:                                if ((null == reader.getAttributePrefix(i))
1332:                                        || ("".equals(reader
1333:                                                .getAttributePrefix(i)))) { // no attribute prefix
1334:                                    attrCode
1335:                                            .append(' ')
1336:                                            .append(
1337:                                                    reader
1338:                                                            .getAttributeLocalName(i))
1339:                                            .append("=\"")
1340:                                            .append(
1341:                                                    uriAttrFlg ? relativeToAbsoluteUrl(
1342:                                                            reader
1343:                                                                    .getAttributeValue(i),
1344:                                                            baseUrl)
1345:                                                            : reader
1346:                                                                    .getAttributeValue(i))
1347:                                            .append('"');
1348:                                } else { // prefix`presented
1349:                                    attrCode
1350:                                            .append(' ')
1351:                                            .append(
1352:                                                    reader
1353:                                                            .getAttributePrefix(i))
1354:                                            .append(':')
1355:                                            .append(
1356:                                                    reader
1357:                                                            .getAttributeLocalName(i))
1358:                                            .append("=\"")
1359:                                            .append(
1360:                                                    uriAttrFlg ? relativeToAbsoluteUrl(
1361:                                                            reader
1362:                                                                    .getAttributeValue(i),
1363:                                                            baseUrl)
1364:                                                            : reader
1365:                                                                    .getAttributeValue(i))
1366:                                            .append('"');
1367:                                    if (!tmpNsSet.contains(reader
1368:                                            .getAttributePrefix(i))) {
1369:                                        xmlnsCode
1370:                                                .append(" xmlns:")
1371:                                                .append(
1372:                                                        reader
1373:                                                                .getAttributePrefix(i))
1374:                                                .append("=\"")
1375:                                                .append(
1376:                                                        reader
1377:                                                                .getAttributeNamespace(i))
1378:                                                .append('"');
1379:                                        tmpNsSet.add(reader
1380:                                                .getAttributePrefix(i));
1381:                                    } // end if prefix already processed
1382:                                }
1383:                            } // end foreach attr
1384:                            elementCode.append(xmlnsCode) // complete the start element tag
1385:                                    .append(attrCode).append('>');
1386:                            break;
1387:                        //case XMLStreamConstants.ATTRIBUTE:   Unreachable (I hope ;-)
1388:                        //    break;
1389:                        //case XMLStreamConstants.NAMESPACE:   Unreachable (I hope ;-)
1390:                        //    break;
1391:                        case XMLStreamConstants.END_ELEMENT:
1392:                            curName = reader.getName();
1393:                            if (elementName.equals(curName)) { // it is our element !
1394:                                depth--; // go up
1395:                            }
1396:                            elementCode
1397:                                    .append("</")
1398:                                    // append appropriate XML code
1399:                                    .append(
1400:                                            "".equals(curName.getPrefix()) ? ""
1401:                                                    : curName.getPrefix() + ':')
1402:                                    .append(curName.getLocalPart()).append('>'); // complete the end element tag
1403:                            break;
1404:                        case XMLStreamConstants.CHARACTERS:
1405:                            elementCode.append(reader.getText()); // append text data
1406:                            break;
1407:                        case XMLStreamConstants.CDATA:
1408:                            elementCode.append("<![CDATA[") // append CDATA delimiters
1409:                                    .append(reader.getText()).append("]]>");
1410:                            break;
1411:                        case XMLStreamConstants.COMMENT: // Ignore any comments
1412:                            break;
1413:                        case XMLStreamConstants.SPACE: // Ignore spaces as well
1414:                            break;
1415:                        }
1416:                        if (reader.hasNext() && depth > 0) {
1417:                            reader.next();
1418:                        }
1419:                    } while (XMLStreamConstants.END_DOCUMENT != reader
1420:                            .getEventType()
1421:                            && depth > 0);
1422:                    policyRec.policyModel = PolicyModelUnmarshaller
1423:                            .getXmlUnmarshaller().unmarshalModel(
1424:                                    new StringReader(elementCode.toString()));
1425:                    if (null != policyRec.policyModel.getPolicyId()) {
1426:                        policyRec.uri = baseUrl + "#"
1427:                                + policyRec.policyModel.getPolicyId();
1428:                    } else if (policyRec.policyModel.getPolicyName() != null) {
1429:                        policyRec.uri = policyRec.policyModel.getPolicyName();
1430:                    }
1431:                } catch (Exception e) {
1432:                    throw LOGGER
1433:                            .logSevereException(new WebServiceException(
1434:                                    LocalizationMessages
1435:                                            .WSP_1033_EXCEPTION_WHEN_READING_POLICY_ELEMENT(elementCode
1436:                                                    .toString()), e));
1437:                }
1438:                urlsRead.add(baseUrl);
1439:                return policyRec;
1440:            }
1441:
1442:            // time to read possible config file and do alternative selection (on client side)
1443:            public void postFinished(final WSDLParserExtensionContext context) {
1444:                LOGGER.entering();
1445:                final WSDLPolicyMapWrapper mapWrapper = context.getWSDLModel()
1446:                        .getExtension(WSDLPolicyMapWrapper.class);
1447:                if (mapWrapper != null) {
1448:                    if (context.isClientSide() && (!isForConfigFile)) {
1449:                        try {
1450:                            final PolicyMap clientPolicyMap = PolicyConfigParser
1451:                                    .parse(
1452:                                            PolicyConstants.CLIENT_CONFIGURATION_IDENTIFIER,
1453:                                            null);
1454:                            if (clientPolicyMap != null) {
1455:                                mapWrapper
1456:                                        .addClientConfigToMap(
1457:                                                PolicyConstants.CLIENT_CONFIGURATION_IDENTIFIER,
1458:                                                clientPolicyMap);
1459:                            } else {
1460:                                LOGGER
1461:                                        .config(LocalizationMessages
1462:                                                .WSP_1040_CLIENT_CONFIG_PROCESSING_SKIPPED());
1463:                            }
1464:                        } catch (PolicyException e) {
1465:                            throw LOGGER
1466:                                    .logSevereException(new WebServiceException(
1467:                                            LocalizationMessages
1468:                                                    .WSP_1017_ERROR_WHILE_PROCESSING_CLIENT_CONFIG(),
1469:                                            e));
1470:                        }
1471:
1472:                        LOGGER
1473:                                .fine(LocalizationMessages
1474:                                        .WSP_1024_INVOKING_CLIENT_POLICY_ALTERNATIVE_SELECTION());
1475:                        try {
1476:                            mapWrapper.doAlternativeSelection();
1477:                        } catch (PolicyException e) {
1478:                            throw LOGGER
1479:                                    .logSevereException(new WebServiceException(
1480:                                            LocalizationMessages
1481:                                                    .WSP_1003_VALID_POLICY_ALTERNATIVE_NOT_FOUND(),
1482:                                            e));
1483:                        }
1484:                    } else if (!context.isClientSide() && !isForConfigFile) { // not client side and not config file => server side
1485:                        try {
1486:                            mapWrapper.validateServerSidePolicies();
1487:                        } catch (PolicyException e) {
1488:                            throw LOGGER
1489:                                    .logSevereException(new WebServiceException(
1490:                                            LocalizationMessages
1491:                                                    .WSP_1050_SERVER_SIDE_POLICY_VALIDATION_FAILED(),
1492:                                            e));
1493:                        }
1494:                    }
1495:                    mapWrapper.configureModel(context.getWSDLModel());
1496:                }
1497:                LOGGER.exiting();
1498:            }
1499:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.