325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.server;
325N/A
325N/Aimport com.sun.xml.internal.ws.api.SOAPVersion;
325N/Aimport com.sun.xml.internal.ws.api.WSBinding;
325N/Aimport com.sun.xml.internal.ws.api.message.Message;
325N/Aimport com.sun.xml.internal.ws.api.message.Packet;
325N/Aimport com.sun.xml.internal.ws.api.model.SEIModel;
325N/Aimport com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
325N/Aimport com.sun.xml.internal.ws.api.pipe.NextAction;
325N/Aimport com.sun.xml.internal.ws.api.pipe.Tube;
325N/Aimport com.sun.xml.internal.ws.api.pipe.TubeCloner;
325N/Aimport com.sun.xml.internal.ws.api.pipe.helper.AbstractTubeImpl;
325N/Aimport com.sun.xml.internal.ws.api.server.WSEndpoint;
325N/Aimport com.sun.xml.internal.ws.fault.SOAPFaultBuilder;
325N/Aimport com.sun.xml.internal.ws.util.pipe.AbstractSchemaValidationTube;
325N/Aimport org.xml.sax.SAXException;
325N/A
325N/Aimport javax.xml.transform.Source;
325N/Aimport javax.xml.validation.Schema;
325N/Aimport javax.xml.validation.Validator;
325N/Aimport javax.xml.ws.WebServiceException;
325N/Aimport java.util.logging.Level;
325N/Aimport java.util.logging.Logger;
325N/A
325N/A/**
325N/A * {@link Tube} that does the schema validation on the server side.
325N/A *
325N/A * @author Jitendra Kotamraju
325N/A */
325N/Apublic class ServerSchemaValidationTube extends AbstractSchemaValidationTube {
325N/A
325N/A private static final Logger LOGGER = Logger.getLogger(ServerSchemaValidationTube.class.getName());
325N/A
325N/A private final Schema schema;
325N/A private final Validator validator;
325N/A
325N/A private final boolean noValidation;
325N/A private final SEIModel seiModel;
325N/A private final WSDLPort wsdlPort;
325N/A
325N/A public ServerSchemaValidationTube(WSEndpoint endpoint, WSBinding binding,
325N/A SEIModel seiModel, WSDLPort wsdlPort, Tube next) {
325N/A super(binding, next);
325N/A this.seiModel = seiModel;
325N/A this.wsdlPort = wsdlPort;
325N/A
325N/A if (endpoint.getServiceDefinition() != null) {
325N/A MetadataResolverImpl mdresolver = new MetadataResolverImpl(endpoint.getServiceDefinition());
325N/A Source[] sources = getSchemaSources(endpoint.getServiceDefinition(), mdresolver);
325N/A for(Source source : sources) {
325N/A LOGGER.fine("Constructing service validation schema from = "+source.getSystemId());
325N/A //printDOM((DOMSource)source);
325N/A }
325N/A if (sources.length != 0) {
325N/A noValidation = false;
325N/A sf.setResourceResolver(mdresolver);
325N/A try {
325N/A schema = sf.newSchema(sources);
325N/A } catch(SAXException e) {
325N/A throw new WebServiceException(e);
325N/A }
325N/A validator = schema.newValidator();
325N/A return;
325N/A }
325N/A }
325N/A noValidation = true;
325N/A schema = null;
325N/A validator = null;
325N/A }
325N/A
325N/A protected Validator getValidator() {
325N/A return validator;
325N/A }
325N/A
325N/A protected boolean isNoValidation() {
325N/A return noValidation;
325N/A }
325N/A
325N/A @Override
325N/A public NextAction processRequest(Packet request) {
325N/A if (isNoValidation() || !feature.isInbound() || !request.getMessage().hasPayload() || request.getMessage().isFault()) {
325N/A return super.processRequest(request);
325N/A }
325N/A try {
325N/A doProcess(request);
325N/A } catch(SAXException se) {
325N/A LOGGER.log(Level.WARNING, "Client Request doesn't pass Service's Schema Validation", se);
325N/A // Client request is invalid. So sending specific fault code
325N/A // Also converting this to fault message so that handlers may get
325N/A // to see the message.
325N/A SOAPVersion soapVersion = binding.getSOAPVersion();
325N/A Message faultMsg = SOAPFaultBuilder.createSOAPFaultMessage(
325N/A soapVersion, null, se, soapVersion.faultCodeClient);
325N/A return doReturnWith(request.createServerResponse(faultMsg,
325N/A wsdlPort, seiModel, binding));
325N/A }
325N/A return super.processRequest(request);
325N/A }
325N/A
325N/A @Override
325N/A public NextAction processResponse(Packet response) {
325N/A if (isNoValidation() || !feature.isOutbound() || response.getMessage() == null || !response.getMessage().hasPayload() || response.getMessage().isFault()) {
325N/A return super.processResponse(response);
325N/A }
325N/A try {
325N/A doProcess(response);
325N/A } catch(SAXException se) {
325N/A // TODO: Should we convert this to fault Message ??
325N/A throw new WebServiceException(se);
325N/A }
325N/A return super.processResponse(response);
325N/A }
325N/A
325N/A protected ServerSchemaValidationTube(ServerSchemaValidationTube that, TubeCloner cloner) {
325N/A super(that,cloner);
325N/A //this.docs = that.docs;
325N/A this.schema = that.schema; // Schema is thread-safe
325N/A this.validator = schema.newValidator();
325N/A this.noValidation = that.noValidation;
325N/A this.seiModel = that.seiModel;
325N/A this.wsdlPort = that.wsdlPort;
325N/A }
325N/A
325N/A public AbstractTubeImpl copy(TubeCloner cloner) {
325N/A return new ServerSchemaValidationTube(this,cloner);
325N/A }
325N/A
325N/A}