src/share/jaxws_classes/com/sun/xml/internal/ws/client/ClientSchemaValidationTube.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/client/ClientSchemaValidationTube.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,137 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.client;
    1.30 +
    1.31 +import com.sun.xml.internal.ws.api.WSBinding;
    1.32 +import com.sun.xml.internal.ws.api.message.Packet;
    1.33 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
    1.34 +import com.sun.xml.internal.ws.api.pipe.NextAction;
    1.35 +import com.sun.xml.internal.ws.api.pipe.Tube;
    1.36 +import com.sun.xml.internal.ws.api.pipe.TubeCloner;
    1.37 +import com.sun.xml.internal.ws.api.pipe.helper.AbstractTubeImpl;
    1.38 +import com.sun.xml.internal.ws.api.server.SDDocument;
    1.39 +import com.sun.xml.internal.ws.util.MetadataUtil;
    1.40 +import com.sun.xml.internal.ws.util.pipe.AbstractSchemaValidationTube;
    1.41 +import org.xml.sax.SAXException;
    1.42 +
    1.43 +import javax.xml.transform.Source;
    1.44 +import javax.xml.validation.Schema;
    1.45 +import javax.xml.validation.Validator;
    1.46 +import javax.xml.ws.WebServiceException;
    1.47 +import java.util.Map;
    1.48 +import java.util.logging.Logger;
    1.49 +
    1.50 +/**
    1.51 + * {@link Tube} that does the schema validation on the client side.
    1.52 + *
    1.53 + * @author Jitendra Kotamraju
    1.54 + */
    1.55 +public class ClientSchemaValidationTube extends AbstractSchemaValidationTube {
    1.56 +
    1.57 +    private static final Logger LOGGER = Logger.getLogger(ClientSchemaValidationTube.class.getName());
    1.58 +
    1.59 +    private final Schema schema;
    1.60 +    private final Validator validator;
    1.61 +    private final boolean noValidation;
    1.62 +    private final WSDLPort port;
    1.63 +
    1.64 +    public ClientSchemaValidationTube(WSBinding binding, WSDLPort port, Tube next) {
    1.65 +        super(binding, next);
    1.66 +        this.port = port;
    1.67 +        if (port != null) {
    1.68 +            String primaryWsdl = port.getOwner().getParent().getLocation().getSystemId();
    1.69 +            MetadataResolverImpl mdresolver = new MetadataResolverImpl();
    1.70 +            Map<String, SDDocument> docs = MetadataUtil.getMetadataClosure(primaryWsdl, mdresolver, true);
    1.71 +            mdresolver = new MetadataResolverImpl(docs.values());
    1.72 +            Source[] sources = getSchemaSources(docs.values(), mdresolver);
    1.73 +            for(Source source : sources) {
    1.74 +                LOGGER.fine("Constructing client validation schema from = "+source.getSystemId());
    1.75 +                //printDOM((DOMSource)source);
    1.76 +            }
    1.77 +            if (sources.length != 0) {
    1.78 +                noValidation = false;
    1.79 +                sf.setResourceResolver(mdresolver);
    1.80 +                try {
    1.81 +                    schema = sf.newSchema(sources);
    1.82 +                } catch(SAXException e) {
    1.83 +                    throw new WebServiceException(e);
    1.84 +                }
    1.85 +                validator = schema.newValidator();
    1.86 +                return;
    1.87 +            }
    1.88 +        }
    1.89 +        noValidation = true;
    1.90 +        schema = null;
    1.91 +        validator = null;
    1.92 +    }
    1.93 +
    1.94 +    protected Validator getValidator() {
    1.95 +        return validator;
    1.96 +    }
    1.97 +
    1.98 +    protected boolean isNoValidation() {
    1.99 +        return noValidation;
   1.100 +    }
   1.101 +
   1.102 +    protected ClientSchemaValidationTube(ClientSchemaValidationTube that, TubeCloner cloner) {
   1.103 +        super(that,cloner);
   1.104 +        this.port = that.port;
   1.105 +        this.schema = that.schema;
   1.106 +        this.validator = schema.newValidator();
   1.107 +        this.noValidation = that.noValidation;
   1.108 +    }
   1.109 +
   1.110 +    public AbstractTubeImpl copy(TubeCloner cloner) {
   1.111 +        return new ClientSchemaValidationTube(this,cloner);
   1.112 +    }
   1.113 +
   1.114 +    @Override
   1.115 +    public NextAction processRequest(Packet request) {
   1.116 +        if (isNoValidation() || !feature.isOutbound() || !request.getMessage().hasPayload() || request.getMessage().isFault()) {
   1.117 +            return super.processRequest(request);
   1.118 +        }
   1.119 +        try {
   1.120 +            doProcess(request);
   1.121 +        } catch(SAXException se) {
   1.122 +            throw new WebServiceException(se);
   1.123 +        }
   1.124 +        return super.processRequest(request);
   1.125 +    }
   1.126 +
   1.127 +    @Override
   1.128 +    public NextAction processResponse(Packet response) {
   1.129 +        if (isNoValidation() || !feature.isInbound() || response.getMessage() == null || !response.getMessage().hasPayload() || response.getMessage().isFault()) {
   1.130 +            return super.processResponse(response);
   1.131 +        }
   1.132 +        try {
   1.133 +            doProcess(response);
   1.134 +        } catch(SAXException se) {
   1.135 +            throw new WebServiceException(se);
   1.136 +        }
   1.137 +        return super.processResponse(response);
   1.138 +    }
   1.139 +
   1.140 +}

mercurial