src/share/jaxws_classes/com/sun/xml/internal/ws/server/ServerSchemaValidationTube.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 637
9c07ef4934dd
permissions
-rw-r--r--

merge

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

mercurial