# Endpoint API

Web Service endpoints can be created and published programmatically using `javax.xml.ws.Endpoint` API. An endpoint consists of a Web Service Implementation object and some configuration information. The implementation hosts the web service endpoint using a light weight http server and clients can access the web service as if the endpoint is deployed in a J2EE container. This means that there is no need to have any J2EE servlet or EJB container to host the endpoint. The `Endpoint` API provides a way to configure the endpoint with the necessary binding, metadata (WSDL and schema documents), handlers etc.

## `Endpoint`

An endpoint can be created using any of the following constructors:

    Endpoint.create(implementor)

    Endpoint.create(bindingId,implementor)

    Endpoint.publish(address, implementor)

Once the `Endpoint` object is created using the first two constructors, it can be published using `Endpoint.publish()`. Any published `Endpoint` can be stopped using `Endpoint.stop()`. `samples/supplychain/src/supplychain/server/WarehouseLightWeight.java` supplychain sample shows creating and publishing an `Endpoint`.

## `Endpoint` and `Properties`

An endpoint can be configured to match service name and port name of WSDL using properties. This overwrites implementor object's serviceName, portName from `@WebService` annotation. The port address for an endpoint is patched only if the corresponding port's service name, and port name in WSDL are matched.

`Endpoint` and `Properties` Example

    Endpoint endpoint = ...

    Map<String, Object> map = new HashMap<String, Object>();
    map.put(Endpoint.WSDL_SERVICE, new QName(...));
    map.put(Endpoint.WSDL_PORT, new QName(...));

    endpoint.setProperties(map);

## `Endpoint` and `Binding`

An endpoint can be configured for different bindings using binding ids. These binding ids are defined in JAX-WS API and endpoint can be configured by specifying `@BindingType` annotation or using binding id in the `Endpoint()` constructors. The parameter in constructor overwrites binding defined by `@BindingType` annotation. If the binding is not specified using `@BindingType` or using a parameter in `Endpoint()` constructor, the default binding is `SOAP1.1`/`HTTP`. Binding object is used to configure MTOM, handler chain etc. SOAP binding object is used to configure SOAP binding specifics like roles.

For example:

`Endpoint` and `Binding` Example

The following configures the endpoint for `XML/HTTP` binding.

    Endpoint endpoint = Endpoint.create(HTTPBinding.HTTP_BINDING,
            implementor);

Working with a `Binding` object:

    // setting MTOM
    SOAPBinding binding = (SOAPBinding) endpoint.getBinding();
    binding.setMTOMEnabled(true);

    // setting SOAP binding roles
    binding.setRoles(...);

    // setting handler chain
    binding.setHandlerChain(...);

## `Endpoint` and `metadata`

When the service endpoint is created using existing java classes, the implementation dynamically generates and publishes WSDL and schema documents. But when the service endpoint is created using existing WSDL documents, the same WSDL documents can be used for publishing using metadata facility. When a `Source` object is created, set `systemId` always and make sure the imports are resolvable w.r.t `systemId`s.

`Endpoint` and `metadata` Example

    // metadata processing for WSDL, schema files
    List<File> metadataFile =...
    List<Source> metadata = new ArrayList<Source>();
    for (File file : metadataFile) {
        Source source = new StreamSource(new FileInputStream(file));
        source.setSystemId(file.toURL().toExternalForm());
        metadata.add(source);
    }
    endpoint.setMetadata(metadata);