All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.xml.ws.transport.http.server.EndpointImpl Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at
 * https://glassfish.dev.java.net/public/CDDLv1.0.html.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
 */

package com.sun.xml.ws.transport.http.server;

import com.sun.istack.Nullable;
import com.sun.xml.stream.buffer.XMLStreamBufferResult;
import com.sun.xml.ws.api.WSBinding;
import com.sun.xml.ws.api.BindingID;
import com.sun.xml.ws.api.server.WSEndpoint;
import com.sun.xml.ws.binding.BindingImpl;
import com.sun.xml.ws.api.server.InstanceResolver;
import com.sun.xml.ws.api.server.SDDocumentSource;
import com.sun.xml.ws.server.EndpointFactory;
import com.sun.xml.ws.server.ServerRtException;
import com.sun.xml.ws.util.xml.XmlUtil;
import com.sun.istack.NotNull;

import java.net.MalformedURLException;

import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.ws.*;
import javax.xml.ws.wsaddressing.W3CEndpointReference;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;

import org.xml.sax.EntityResolver;
import org.w3c.dom.Element;


/**
 * Implements {@link Endpoint}.
 * 

*

* This class accumulates the information necessary to create * {@link WSEndpoint}, and then when {@link #publish} method * is called it will be created. *

*

* This object also allows accumulated information to be retrieved. * * @author Jitendra Kotamraju */ public class EndpointImpl extends Endpoint { private static final WebServicePermission ENDPOINT_PUBLISH_PERMISSION = new WebServicePermission("publishEndpoint"); /** * Once the service is published, this field will * be set to the {@link HttpEndpoint} instance. *

* But don't declare the type as {@link HttpEndpoint} * to avoid static type dependency that cause the class loading to * fail if the LW HTTP server doesn't exist. */ private Object actualEndpoint; // information accumulated for creating WSEndpoint private final WSBinding binding; private final Object implementor; private List metadata; private Executor executor; private Map properties = Collections.emptyMap(); // always non-null private boolean stopped; public EndpointImpl(@NotNull BindingID bindingId, @NotNull Object impl) { binding = BindingImpl.create(bindingId); implementor = impl; } /** * Wraps an already created {@link WSEndpoint} into an {@link EndpointImpl}, * and immediately publishes it with the given context. * * @deprecated This is a backdoor method. Don't use it unless you know what you are doing. */ public EndpointImpl(WSEndpoint wse, Object serverContext) { actualEndpoint = new HttpEndpoint(wse, executor); ((HttpEndpoint) actualEndpoint).publish(serverContext); binding = wse.getBinding(); implementor = null; // this violates the semantics, but hey, this is a backdoor. } public Binding getBinding() { return binding; } public Object getImplementor() { return implementor; } public void publish(String address) { canPublish(); URL url; try { url = new URL(address); } catch (MalformedURLException ex) { throw new IllegalArgumentException("Cannot create URL for this address " + address); } if (!url.getProtocol().equals("http")) { throw new IllegalArgumentException(url.getProtocol() + " protocol based address is not supported"); } if (!url.getPath().startsWith("/")) { throw new IllegalArgumentException("Incorrect WebService address=" + address + ". The address's path should start with /"); } createEndpoint(); ((HttpEndpoint) actualEndpoint).publish(address); } public void publish(Object serverContext) { canPublish(); if (!com.sun.net.httpserver.HttpContext.class.isAssignableFrom(serverContext.getClass())) { throw new IllegalArgumentException(serverContext.getClass() + " is not a supported context."); } createEndpoint(); ((HttpEndpoint) actualEndpoint).publish(serverContext); } public void stop() { if (isPublished()) { ((HttpEndpoint) actualEndpoint).stop(); actualEndpoint = null; stopped = true; } } public boolean isPublished() { return actualEndpoint != null; } public List getMetadata() { return metadata; } public void setMetadata(java.util.List metadata) { if (isPublished()) { throw new IllegalStateException("Cannot set Metadata. Endpoint is already published"); } this.metadata = metadata; } public Executor getExecutor() { return executor; } public void setExecutor(Executor executor) { this.executor = executor; } public Map getProperties() { return new HashMap(properties); } public void setProperties(Map map) { this.properties = new HashMap(map); } /* * Checks the permission of "publishEndpoint" before accessing HTTP classes. * Also it checks if there is an available HTTP server implementation. */ private void createEndpoint() { // Checks permission for "publishEndpoint" SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(ENDPOINT_PUBLISH_PERMISSION); } // See if HttpServer implementation is available try { Class.forName("com.sun.net.httpserver.HttpServer"); } catch (Exception e) { throw new UnsupportedOperationException("NOT SUPPORTED"); } WSEndpoint wse = WSEndpoint.create( (Class) implementor.getClass(), true, InstanceResolver.createSingleton(implementor).createInvoker(), getProperty(QName.class, Endpoint.WSDL_SERVICE), getProperty(QName.class, Endpoint.WSDL_PORT), null /* no container */, binding, getPrimaryWsdl(), buildDocList(), (EntityResolver) null ); // Don't load HttpEndpoint class before as it may load HttpServer classes actualEndpoint = new HttpEndpoint(wse, executor); } private T getProperty(Class type, String key) { Object o = properties.get(key); if (o == null) return null; if (type.isInstance(o)) return type.cast(o); else throw new IllegalArgumentException("Property " + key + " has to be of type " + type); // i18n } /** * Convert metadata sources using identity transform. So that we can * reuse the Source object multiple times. */ private List buildDocList() { List r = new ArrayList(); if (metadata != null) { Transformer transformer = XmlUtil.newTransformer(); for (Source source : metadata) { try { XMLStreamBufferResult xsbr = new XMLStreamBufferResult(); transformer.transform(source, xsbr); String systemId = source.getSystemId(); r.add(SDDocumentSource.create(new URL(systemId), xsbr.getXMLStreamBuffer())); } catch (TransformerException te) { throw new ServerRtException("server.rt.err", te); } catch (IOException te) { throw new ServerRtException("server.rt.err", te); } } } return r; } /** * Gets wsdl from @WebService or @WebServiceProvider */ private @Nullable SDDocumentSource getPrimaryWsdl() { Class implType = implementor.getClass(); // Takes care of @WebService, @WebServiceProvider's wsdlLocation String wsdlLocation = EndpointFactory.getWsdlLocation(implType); if (wsdlLocation != null) { ClassLoader cl = implType.getClassLoader(); URL url = cl.getResource(wsdlLocation); if (url != null) { return SDDocumentSource.create(url); } throw new ServerRtException("cannot.load.wsdl", wsdlLocation); } return null; } private void canPublish() { if (isPublished()) { throw new IllegalStateException( "Cannot publish this endpoint. Endpoint has been already published."); } if (stopped) { throw new IllegalStateException( "Cannot publish this endpoint. Endpoint has been already stopped."); } } public EndpointReference getEndpointReference(Element...referenceParameters) { return getEndpointReference(W3CEndpointReference.class, referenceParameters); } public T getEndpointReference(Class clazz, Element...referenceParameters) { if (!isPublished()) { throw new WebServiceException("Endpoint is not published yet"); } return ((HttpEndpoint)actualEndpoint).getEndpointReference(clazz,referenceParameters); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy