001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.handler;
038:
039: import com.sun.xml.ws.api.BindingID;
040: import com.sun.xml.ws.api.WSBinding;
041: import com.sun.xml.ws.streaming.XMLStreamReaderUtil;
042: import com.sun.xml.ws.transport.http.DeploymentDescriptorParser;
043: import com.sun.xml.ws.util.HandlerAnnotationInfo;
044: import com.sun.xml.ws.util.JAXWSUtils;
045: import com.sun.xml.ws.util.UtilException;
046:
047: import javax.annotation.PostConstruct;
048: import javax.xml.namespace.QName;
049: import javax.xml.stream.XMLStreamConstants;
050: import javax.xml.stream.XMLStreamReader;
051: import javax.xml.ws.handler.Handler;
052: import javax.xml.ws.handler.PortInfo;
053: import java.lang.reflect.Method;
054: import java.util.ArrayList;
055: import java.util.HashSet;
056: import java.util.List;
057: import java.util.Set;
058: import java.util.StringTokenizer;
059: import java.util.logging.Logger;
060:
061: public class HandlerChainsModel {
062: private static final Logger logger = Logger
063: .getLogger(com.sun.xml.ws.util.Constants.LoggingDomain
064: + ".util");
065:
066: private Class annotatedClass;
067: private List<HandlerChainType> handlerChains;
068: private String id;
069:
070: /** Creates a new instance of HandlerChains */
071: private HandlerChainsModel(Class annotatedClass) {
072: this .annotatedClass = annotatedClass;
073: }
074:
075: private List<HandlerChainType> getHandlerChain() {
076: if (handlerChains == null) {
077: handlerChains = new ArrayList<HandlerChainType>();
078: }
079: return handlerChains;
080: }
081:
082: public String getId() {
083: return id;
084: }
085:
086: public void setId(String value) {
087: this .id = value;
088: }
089:
090: /**
091: * reader should be on <handler-chains> element
092: */
093: public static HandlerChainsModel parseHandlerConfigFile(
094: Class annotatedClass, XMLStreamReader reader) {
095: ensureProperName(reader, QNAME_HANDLER_CHAINS);
096: HandlerChainsModel handlerModel = new HandlerChainsModel(
097: annotatedClass);
098: List<HandlerChainType> hChains = handlerModel.getHandlerChain();
099: XMLStreamReaderUtil.nextElementContent(reader);
100:
101: while (reader.getName().equals(QNAME_HANDLER_CHAIN)) {
102: HandlerChainType hChain = new HandlerChainType();
103: XMLStreamReaderUtil.nextElementContent(reader);
104:
105: if (reader.getName().equals(QNAME_CHAIN_PORT_PATTERN)) {
106: QName portNamePattern = XMLStreamReaderUtil
107: .getElementQName(reader);
108: hChain.setPortNamePattern(portNamePattern);
109: XMLStreamReaderUtil.nextElementContent(reader);
110: } else if (reader.getName().equals(
111: QNAME_CHAIN_PROTOCOL_BINDING)) {
112: String bindingList = XMLStreamReaderUtil
113: .getElementText(reader);
114: StringTokenizer stk = new StringTokenizer(bindingList);
115: while (stk.hasMoreTokens()) {
116: String token = stk.nextToken();
117: // This will convert tokens into Binding URI
118: hChain.addProtocolBinding(token);
119: }
120: XMLStreamReaderUtil.nextElementContent(reader);
121: } else if (reader.getName().equals(
122: QNAME_CHAIN_SERVICE_PATTERN)) {
123: QName serviceNamepattern = XMLStreamReaderUtil
124: .getElementQName(reader);
125: hChain.setServiceNamePattern(serviceNamepattern);
126: XMLStreamReaderUtil.nextElementContent(reader);
127: }
128: List<HandlerType> handlers = hChain.getHandlers();
129: // process all <handler> elements
130: while (reader.getName().equals(QNAME_HANDLER)) {
131: HandlerType handler = new HandlerType();
132:
133: XMLStreamReaderUtil.nextContent(reader);
134: if (reader.getName().equals(QNAME_HANDLER_NAME)) {
135: String handlerName = XMLStreamReaderUtil
136: .getElementText(reader).trim();
137: handler.setHandlerName(handlerName);
138: XMLStreamReaderUtil.nextContent(reader);
139: }
140:
141: // handler class
142: ensureProperName(reader, QNAME_HANDLER_CLASS);
143: String handlerClass = XMLStreamReaderUtil
144: .getElementText(reader).trim();
145: handler.setHandlerClass(handlerClass);
146: XMLStreamReaderUtil.nextContent(reader);
147:
148: // init params (ignored)
149: while (reader.getName().equals(QNAME_HANDLER_PARAM)) {
150: skipInitParamElement(reader);
151: }
152:
153: // headers (ignored)
154: while (reader.getName().equals(QNAME_HANDLER_HEADER)) {
155: skipTextElement(reader);
156: }
157:
158: // roles (not stored per handler)
159: while (reader.getName().equals(QNAME_HANDLER_ROLE)) {
160: List<String> soapRoles = handler.getSoapRoles();
161: soapRoles.add(XMLStreamReaderUtil
162: .getElementText(reader));
163: XMLStreamReaderUtil.nextContent(reader);
164: }
165:
166: handlers.add(handler);
167:
168: // move past </handler>
169: ensureProperName(reader, QNAME_HANDLER);
170: XMLStreamReaderUtil.nextContent(reader);
171: }
172:
173: // move past </handler-chain>
174: ensureProperName(reader, QNAME_HANDLER_CHAIN);
175: hChains.add(hChain);
176: XMLStreamReaderUtil.nextContent(reader);
177: }
178:
179: return handlerModel;
180: }
181:
182: /**
183: * <p>This method is called internally by HandlerAnnotationProcessor,
184: * and by
185: * {@link com.sun.xml.ws.transport.http.DeploymentDescriptorParser}
186: * directly when it reaches the handler chains element in the
187: * descriptor file it is parsing.
188: * @param reader should be on <handler-chains> element
189: * @return A HandlerAnnotationInfo object that stores the
190: * handlers and roles.
191: */
192:
193: public static HandlerAnnotationInfo parseHandlerFile(
194: XMLStreamReader reader, ClassLoader classLoader,
195: QName serviceName, QName portName, WSBinding wsbinding) {
196: ensureProperName(reader, QNAME_HANDLER_CHAINS);
197: String bindingId = wsbinding.getBindingId().toString();
198: HandlerAnnotationInfo info = new HandlerAnnotationInfo();
199:
200: XMLStreamReaderUtil.nextElementContent(reader);
201:
202: List<Handler> handlerChain = new ArrayList<Handler>();
203: Set<String> roles = new HashSet<String>();
204:
205: while (reader.getName().equals(QNAME_HANDLER_CHAIN)) {
206:
207: XMLStreamReaderUtil.nextElementContent(reader);
208:
209: if (reader.getName().equals(QNAME_CHAIN_PORT_PATTERN)) {
210: if (portName == null) {
211: logger
212: .warning("handler chain sepcified for port "
213: + "but port QName passed to parser is null");
214: }
215: boolean parseChain = JAXWSUtils.matchQNames(portName,
216: XMLStreamReaderUtil.getElementQName(reader));
217: if (!parseChain) {
218: skipChain(reader);
219: continue;
220: }
221: XMLStreamReaderUtil.nextElementContent(reader);
222: } else if (reader.getName().equals(
223: QNAME_CHAIN_PROTOCOL_BINDING)) {
224: if (bindingId == null) {
225: logger
226: .warning("handler chain sepcified for bindingId "
227: + "but bindingId passed to parser is null");
228: }
229: String bindingConstraint = XMLStreamReaderUtil
230: .getElementText(reader);
231: boolean skipThisChain = true;
232: StringTokenizer stk = new StringTokenizer(
233: bindingConstraint);
234: List<String> bindingList = new ArrayList<String>();
235: while (stk.hasMoreTokens()) {
236: String tokenOrURI = stk.nextToken();
237: /*
238: Convert short-form tokens to API's binding ids
239: Unknown token, Put it as it is
240: */
241: tokenOrURI = DeploymentDescriptorParser
242: .getBindingIdForToken(tokenOrURI);
243: String binding = BindingID.parse(tokenOrURI)
244: .toString();
245: bindingList.add(binding);
246: }
247: if (bindingList.contains(bindingId)) {
248: skipThisChain = false;
249: }
250:
251: if (skipThisChain) {
252: skipChain(reader);
253: continue;
254: }
255: XMLStreamReaderUtil.nextElementContent(reader);
256: } else if (reader.getName().equals(
257: QNAME_CHAIN_SERVICE_PATTERN)) {
258: if (serviceName == null) {
259: logger
260: .warning("handler chain sepcified for service "
261: + "but service QName passed to parser is null");
262: }
263: boolean parseChain = JAXWSUtils.matchQNames(
264: serviceName, XMLStreamReaderUtil
265: .getElementQName(reader));
266: if (!parseChain) {
267: skipChain(reader);
268: continue;
269: }
270: XMLStreamReaderUtil.nextElementContent(reader);
271: }
272:
273: // process all <handler> elements
274: while (reader.getName().equals(QNAME_HANDLER)) {
275: Handler handler;
276:
277: XMLStreamReaderUtil.nextContent(reader);
278: if (reader.getName().equals(QNAME_HANDLER_NAME)) {
279: skipTextElement(reader);
280: }
281:
282: // handler class
283: ensureProperName(reader, QNAME_HANDLER_CLASS);
284: try {
285: handler = (Handler) loadClass(
286: classLoader,
287: XMLStreamReaderUtil.getElementText(reader)
288: .trim()).newInstance();
289: } catch (InstantiationException ie) {
290: throw new RuntimeException(ie);
291: } catch (IllegalAccessException e) {
292: throw new RuntimeException(e);
293: }
294: XMLStreamReaderUtil.nextContent(reader);
295:
296: // init params (ignored)
297: while (reader.getName().equals(QNAME_HANDLER_PARAM)) {
298: skipInitParamElement(reader);
299: }
300:
301: // headers (ignored)
302: while (reader.getName().equals(QNAME_HANDLER_HEADER)) {
303: skipTextElement(reader);
304: }
305:
306: // roles (not stored per handler)
307: while (reader.getName().equals(QNAME_HANDLER_ROLE)) {
308: roles.add(XMLStreamReaderUtil
309: .getElementText(reader));
310: XMLStreamReaderUtil.nextContent(reader);
311: }
312:
313: // call @PostConstruct method on handler if present
314: for (Method method : handler.getClass().getMethods()) {
315: if (method.getAnnotation(PostConstruct.class) == null) {
316: continue;
317: }
318: try {
319: method.invoke(handler, new Object[0]);
320: break;
321: } catch (Exception e) {
322: throw new RuntimeException(e);
323: }
324: }
325:
326: handlerChain.add(handler);
327:
328: // move past </handler>
329: ensureProperName(reader, QNAME_HANDLER);
330: XMLStreamReaderUtil.nextContent(reader);
331: }
332:
333: // move past </handler-chain>
334: ensureProperName(reader, QNAME_HANDLER_CHAIN);
335: XMLStreamReaderUtil.nextContent(reader);
336: }
337:
338: info.setHandlers(handlerChain);
339: info.setRoles(roles);
340: return info;
341: }
342:
343: public HandlerAnnotationInfo getHandlersForPortInfo(PortInfo info) {
344:
345: HandlerAnnotationInfo handlerInfo = new HandlerAnnotationInfo();
346: List<Handler> handlerClassList = new ArrayList<Handler>();
347: Set<String> roles = new HashSet<String>();
348:
349: for (HandlerChainType hchain : handlerChains) {
350: boolean hchainMatched = false;
351: if ((!hchain.isConstraintSet())
352: || JAXWSUtils.matchQNames(info.getServiceName(),
353: hchain.getServiceNamePattern())
354: || JAXWSUtils.matchQNames(info.getPortName(),
355: hchain.getPortNamePattern())
356: || hchain.getProtocolBindings().contains(
357: info.getBindingID())) {
358: hchainMatched = true;
359:
360: }
361: if (hchainMatched) {
362: for (HandlerType handler : hchain.getHandlers()) {
363: try {
364: Handler handlerClass = (Handler) loadClass(
365: annotatedClass.getClassLoader(),
366: handler.getHandlerClass())
367: .newInstance();
368: callHandlerPostConstruct(handlerClass);
369: handlerClassList.add(handlerClass);
370: } catch (InstantiationException ie) {
371: throw new RuntimeException(ie);
372: } catch (IllegalAccessException e) {
373: throw new RuntimeException(e);
374: }
375:
376: roles.addAll(handler.getSoapRoles());
377: }
378:
379: }
380: }
381:
382: handlerInfo.setHandlers(handlerClassList);
383: handlerInfo.setRoles(roles);
384: return handlerInfo;
385:
386: }
387:
388: private static Class loadClass(ClassLoader loader, String name) {
389: try {
390: return Class.forName(name, true, loader);
391: } catch (ClassNotFoundException e) {
392: throw new UtilException("util.handler.class.not.found",
393: name);
394: }
395: }
396:
397: private static void callHandlerPostConstruct(Object handlerClass) {
398: // call @PostConstruct method on handler if present
399: for (Method method : handlerClass.getClass().getMethods()) {
400: if (method.getAnnotation(PostConstruct.class) == null) {
401: continue;
402: }
403: try {
404: method.invoke(handlerClass, new Object[0]);
405: break;
406: } catch (Exception e) {
407: throw new RuntimeException(e);
408: }
409: }
410: }
411:
412: private static void skipChain(XMLStreamReader reader) {
413: while (XMLStreamReaderUtil.nextContent(reader) != XMLStreamConstants.END_ELEMENT
414: || !reader.getName().equals(QNAME_HANDLER_CHAIN)) {
415: }
416: XMLStreamReaderUtil.nextElementContent(reader);
417: }
418:
419: private static void skipTextElement(XMLStreamReader reader) {
420: XMLStreamReaderUtil.nextContent(reader);
421: XMLStreamReaderUtil.nextElementContent(reader);
422: XMLStreamReaderUtil.nextElementContent(reader);
423: }
424:
425: private static void skipInitParamElement(XMLStreamReader reader) {
426: int state;
427: do {
428: state = XMLStreamReaderUtil.nextContent(reader);
429: } while (state != XMLStreamReader.END_ELEMENT
430: || !reader.getName().equals(QNAME_HANDLER_PARAM));
431: XMLStreamReaderUtil.nextElementContent(reader);
432: }
433:
434: private static void ensureProperName(XMLStreamReader reader,
435: QName expectedName) {
436:
437: if (!reader.getName().equals(expectedName)) {
438: failWithLocalName("util.parser.wrong.element", reader,
439: expectedName.getLocalPart());
440: }
441: }
442:
443: static void ensureProperName(XMLStreamReader reader,
444: String expectedName) {
445: if (!reader.getLocalName().equals(expectedName)) {
446: failWithLocalName("util.parser.wrong.element", reader,
447: expectedName);
448: }
449: }
450:
451: private static void failWithLocalName(String key,
452: XMLStreamReader reader, String arg) {
453: throw new UtilException(key, Integer.toString(reader
454: .getLocation().getLineNumber()), reader.getLocalName(),
455: arg);
456: }
457:
458: public static final String PROTOCOL_SOAP11_TOKEN = "##SOAP11_HTTP";
459: public static final String PROTOCOL_SOAP12_TOKEN = "##SOAP12_HTTP";
460: public static final String PROTOCOL_XML_TOKEN = "##XML_HTTP";
461:
462: public static final String NS_109 = "http://java.sun.com/xml/ns/javaee";
463: public static final QName QNAME_CHAIN_PORT_PATTERN = new QName(
464: NS_109, "port-name-pattern");
465: public static final QName QNAME_CHAIN_PROTOCOL_BINDING = new QName(
466: NS_109, "protocol-bindings");
467: public static final QName QNAME_CHAIN_SERVICE_PATTERN = new QName(
468: NS_109, "service-name-pattern");
469: public static final QName QNAME_HANDLER_CHAIN = new QName(NS_109,
470: "handler-chain");
471: public static final QName QNAME_HANDLER_CHAINS = new QName(NS_109,
472: "handler-chains");
473: public static final QName QNAME_HANDLER = new QName(NS_109,
474: "handler");
475: public static final QName QNAME_HANDLER_NAME = new QName(NS_109,
476: "handler-name");
477: public static final QName QNAME_HANDLER_CLASS = new QName(NS_109,
478: "handler-class");
479: public static final QName QNAME_HANDLER_PARAM = new QName(NS_109,
480: "init-param");
481: public static final QName QNAME_HANDLER_PARAM_NAME = new QName(
482: NS_109, "param-name");
483: public static final QName QNAME_HANDLER_PARAM_VALUE = new QName(
484: NS_109, "param-value");
485: public static final QName QNAME_HANDLER_HEADER = new QName(NS_109,
486: "soap-header");
487: public static final QName QNAME_HANDLER_ROLE = new QName(NS_109,
488: "soap-role");
489:
490: static class HandlerChainType {
491: //constraints
492: QName serviceNamePattern;
493: QName portNamePattern;
494: List<String> protocolBindings;
495:
496: // This flag is set if one of the above constraint is set on handler chain
497: boolean constraintSet = false;
498:
499: List<HandlerType> handlers;
500: String id;
501:
502: /** Creates a new instance of HandlerChain */
503: public HandlerChainType() {
504: protocolBindings = new ArrayList<String>();
505: }
506:
507: public void setServiceNamePattern(QName value) {
508: this .serviceNamePattern = value;
509: constraintSet = true;
510: }
511:
512: public QName getServiceNamePattern() {
513: return serviceNamePattern;
514: }
515:
516: public void setPortNamePattern(QName value) {
517: this .portNamePattern = value;
518: constraintSet = true;
519: }
520:
521: public QName getPortNamePattern() {
522: return portNamePattern;
523: }
524:
525: public List<java.lang.String> getProtocolBindings() {
526: return this .protocolBindings;
527: }
528:
529: public void addProtocolBinding(String tokenOrURI) {
530: /*
531: Convert short-form tokens to API's binding ids
532: Unknown token, Put it as it is
533: */
534: tokenOrURI = DeploymentDescriptorParser
535: .getBindingIdForToken(tokenOrURI);
536: String binding = BindingID.parse(tokenOrURI).toString();
537: protocolBindings.add(binding);
538: constraintSet = true;
539: }
540:
541: public boolean isConstraintSet() {
542: return constraintSet || !protocolBindings.isEmpty();
543: }
544:
545: public java.lang.String getId() {
546: return id;
547: }
548:
549: public void setId(java.lang.String value) {
550: this .id = value;
551: }
552:
553: public List<HandlerType> getHandlers() {
554: if (handlers == null) {
555: handlers = new ArrayList<HandlerType>();
556: }
557: return this .handlers;
558: }
559: }
560:
561: static class HandlerType {
562: String handlerName;
563: String handlerClass;
564: List<String> soapRoles;
565:
566: java.lang.String id;
567:
568: /** Creates a new instance of HandlerComponent */
569: public HandlerType() {
570: }
571:
572: public String getHandlerName() {
573: return handlerName;
574: }
575:
576: public void setHandlerName(String value) {
577: this .handlerName = value;
578: }
579:
580: public String getHandlerClass() {
581: return handlerClass;
582: }
583:
584: public void setHandlerClass(String value) {
585: this .handlerClass = value;
586: }
587:
588: public java.lang.String getId() {
589: return id;
590: }
591:
592: public void setId(java.lang.String value) {
593: this .id = value;
594: }
595:
596: public List<String> getSoapRoles() {
597: if (soapRoles == null) {
598: soapRoles = new ArrayList<String>();
599: }
600: return this.soapRoles;
601: }
602: }
603: }
|