|
1 /* |
|
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.ws.client; |
|
27 |
|
28 import com.sun.xml.internal.ws.api.WSBinding; |
|
29 import com.sun.xml.internal.ws.api.message.Packet; |
|
30 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; |
|
31 import com.sun.xml.internal.ws.api.pipe.NextAction; |
|
32 import com.sun.xml.internal.ws.api.pipe.Tube; |
|
33 import com.sun.xml.internal.ws.api.pipe.TubeCloner; |
|
34 import com.sun.xml.internal.ws.api.pipe.helper.AbstractTubeImpl; |
|
35 import com.sun.xml.internal.ws.api.server.SDDocument; |
|
36 import com.sun.xml.internal.ws.util.MetadataUtil; |
|
37 import com.sun.xml.internal.ws.util.pipe.AbstractSchemaValidationTube; |
|
38 import org.xml.sax.SAXException; |
|
39 |
|
40 import javax.xml.transform.Source; |
|
41 import javax.xml.validation.Schema; |
|
42 import javax.xml.validation.Validator; |
|
43 import javax.xml.ws.WebServiceException; |
|
44 import java.util.Map; |
|
45 import java.util.logging.Logger; |
|
46 |
|
47 /** |
|
48 * {@link Tube} that does the schema validation on the client side. |
|
49 * |
|
50 * @author Jitendra Kotamraju |
|
51 */ |
|
52 public class ClientSchemaValidationTube extends AbstractSchemaValidationTube { |
|
53 |
|
54 private static final Logger LOGGER = Logger.getLogger(ClientSchemaValidationTube.class.getName()); |
|
55 |
|
56 private final Schema schema; |
|
57 private final Validator validator; |
|
58 private final boolean noValidation; |
|
59 private final WSDLPort port; |
|
60 |
|
61 public ClientSchemaValidationTube(WSBinding binding, WSDLPort port, Tube next) { |
|
62 super(binding, next); |
|
63 this.port = port; |
|
64 if (port != null) { |
|
65 String primaryWsdl = port.getOwner().getParent().getLocation().getSystemId(); |
|
66 MetadataResolverImpl mdresolver = new MetadataResolverImpl(); |
|
67 Map<String, SDDocument> docs = MetadataUtil.getMetadataClosure(primaryWsdl, mdresolver, true); |
|
68 mdresolver = new MetadataResolverImpl(docs.values()); |
|
69 Source[] sources = getSchemaSources(docs.values(), mdresolver); |
|
70 for(Source source : sources) { |
|
71 LOGGER.fine("Constructing client validation schema from = "+source.getSystemId()); |
|
72 //printDOM((DOMSource)source); |
|
73 } |
|
74 if (sources.length != 0) { |
|
75 noValidation = false; |
|
76 sf.setResourceResolver(mdresolver); |
|
77 try { |
|
78 schema = sf.newSchema(sources); |
|
79 } catch(SAXException e) { |
|
80 throw new WebServiceException(e); |
|
81 } |
|
82 validator = schema.newValidator(); |
|
83 return; |
|
84 } |
|
85 } |
|
86 noValidation = true; |
|
87 schema = null; |
|
88 validator = null; |
|
89 } |
|
90 |
|
91 protected Validator getValidator() { |
|
92 return validator; |
|
93 } |
|
94 |
|
95 protected boolean isNoValidation() { |
|
96 return noValidation; |
|
97 } |
|
98 |
|
99 protected ClientSchemaValidationTube(ClientSchemaValidationTube that, TubeCloner cloner) { |
|
100 super(that,cloner); |
|
101 this.port = that.port; |
|
102 this.schema = that.schema; |
|
103 this.validator = schema.newValidator(); |
|
104 this.noValidation = that.noValidation; |
|
105 } |
|
106 |
|
107 public AbstractTubeImpl copy(TubeCloner cloner) { |
|
108 return new ClientSchemaValidationTube(this,cloner); |
|
109 } |
|
110 |
|
111 @Override |
|
112 public NextAction processRequest(Packet request) { |
|
113 if (isNoValidation() || !feature.isOutbound() || !request.getMessage().hasPayload() || request.getMessage().isFault()) { |
|
114 return super.processRequest(request); |
|
115 } |
|
116 try { |
|
117 doProcess(request); |
|
118 } catch(SAXException se) { |
|
119 throw new WebServiceException(se); |
|
120 } |
|
121 return super.processRequest(request); |
|
122 } |
|
123 |
|
124 @Override |
|
125 public NextAction processResponse(Packet response) { |
|
126 if (isNoValidation() || !feature.isInbound() || response.getMessage() == null || !response.getMessage().hasPayload() || response.getMessage().isFault()) { |
|
127 return super.processResponse(response); |
|
128 } |
|
129 try { |
|
130 doProcess(response); |
|
131 } catch(SAXException se) { |
|
132 throw new WebServiceException(se); |
|
133 } |
|
134 return super.processResponse(response); |
|
135 } |
|
136 |
|
137 } |