users@wadl.java.net

the beginner

From: George Cong <george.cong_at_conceptwave.com>
Date: Wed, 2 Oct 2013 12:45:07 -0400

Hello, I am the beginner, and I use the wadl2java 1.1.5 core code, I
generate client code, but I don’t know how to use the generated client,
thanks.

* *

*Wadl*

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<application xmlns="http://research.sun.com/wadl/2006/10">

    <doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey:
1.8 06/24/2011 12:17 PM"/>

    <resources base="http://localhost:8080/RESTfulExample/rest/">

        <resource path="/hello">

            <resource path="/{param}">

                <param xmlns:xs="http://www.w3.org/2001/XMLSchema"
name="param" style="template" type="xs:string"/>

                <method id="getMsg" name="GET">

                    <response>

                        <representation mediaType="*/*"/>

                    </response>

                </method>

            </resource>

        </resource>

    </resources>

</application>



The code to generate the client:



        URI wadlDesc = new URI("C:/TEMP/hello.wadl");

        File wadlFile = new File(wadlDesc.getPath());

        Parameters p = new Parameters();

        p.setCustomizations(new ArrayList());

        p.setPkg("testpkg");

        p.setRootDir(new URI("C:/TEMP1/"));

        p.setCodeWriter(new FileCodeWriter(new File("C:/TEMP/")));

        Wadl2Java w = new Wadl2Java(p);

        w.process(wadlFile.toURI());



the generated client:





package testpkg;



import java.net.URI;

import java.util.HashMap;

import java.util.Map;

import javax.annotation.Generated;

import javax.ws.rs.WebApplicationException;

import javax.ws.rs.core.Response;

import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.GenericType;

import com.sun.jersey.api.client.config.ClientConfig;

import com.sun.jersey.api.client.config.DefaultClientConfig;

import com.sun.jersey.api.uri.UriTemplate;



@Generated(value = {

    "wadl|file:/C:/TEMP/hello.wadl"

}, comments = "wadl2java, http://wadl.java.net", date =
"2013-10-02T12:06:38.288-04:00")

public class Localhost_RESTfulExampleRest {



    /**

     * The base URI for the resource represented by this proxy

     *

     */

    public final static URI BASE_URI;



    static {

        URI originalURI = URI.create("
http://localhost:8080/RESTfulExample/rest/");

        // Look up to see if we have any indirection in the local copy

        // of META-INF/java-rs-catalog.xml file, assuming it will be in the

        // oasis:name:tc:entity:xmlns:xml:catalog namespace or similar duck
type

        java.io.InputStream is =
Localhost_RESTfulExampleRest.class.getResourceAsStream("/META-INF/jax-rs-catalog.xml");

        if (is!=null) {

            try {

                // Ignore the namespace in the catalog, can't use wildcard
until

                // we are sure we have XPath 2.0

                String found =
javax.xml.xpath.XPathFactory.newInstance().newXPath().evaluate(

                    "/*[name(.) = 'catalog']/*[name(.) = 'uri' and @name
='" + originalURI +"']/_at_uri",

                    new org.xml.sax.InputSource(is));

                if (found!=null && found.length()>0) {

                    originalURI = java.net.URI.create(found);

                }



            }

            catch (Exception ex) {

                ex.printStackTrace();

            }

            finally {

                try {

                    is.close();

                } catch (java.io.IOException e) {

                }

            }

        }

        BASE_URI = originalURI;

    }



    public static Localhost_RESTfulExampleRest.Hello
hello(com.sun.jersey.api.client.Client client, URI baseURI) {

        return new Localhost_RESTfulExampleRest.Hello(client, baseURI);

    }



    /**

     * Template method to allow tooling to customize the new Client

     *

     */

    private static void customizeClientConfiguration(ClientConfig cc) {

    }



    /**

     * Template method to allow tooling to override Client factory

     *

     */

    private static com.sun.jersey.api.client.Client
createClientInstance(ClientConfig cc) {

        return com.sun.jersey.api.client.Client.create(cc);

    }



    /**

     * Create a new Client instance

     *

     */

    public static com.sun.jersey.api.client.Client createClient() {

        ClientConfig cc = new DefaultClientConfig();

        customizeClientConfiguration(cc);

        return createClientInstance(cc);

    }



    public static Localhost_RESTfulExampleRest.Hello hello() {

        return hello(createClient(), BASE_URI);

    }



    public static Localhost_RESTfulExampleRest.Hello
hello(com.sun.jersey.api.client.Client client) {

        return hello(client, BASE_URI);

    }



    public static class Hello {



        private com.sun.jersey.api.client.Client _client;

        private UriBuilder _uriBuilder;

        private Map<String, Object> _templateAndMatrixParameterValues;



        private Hello(com.sun.jersey.api.client.Client client, UriBuilder
uriBuilder, Map<String, Object> map) {

            _client = client;

            _uriBuilder = uriBuilder.clone();

            _templateAndMatrixParameterValues = map;

        }



        /**

         * Create new instance using existing Client instance, and a base
URI and any parameters

         *

         */

        public Hello(com.sun.jersey.api.client.Client client, URI baseUri) {

            _client = client;

            _uriBuilder = UriBuilder.fromUri(baseUri);

            _uriBuilder = _uriBuilder.path("/hello");

            _templateAndMatrixParameterValues = new HashMap<String,
Object>();

        }



        public Localhost_RESTfulExampleRest.Hello.Param param(String param)
{

            return new Localhost_RESTfulExampleRest.Hello.Param(_client,
_uriBuilder.buildFromMap(_templateAndMatrixParameterValues), param);

        }



        public static class Param {



            private com.sun.jersey.api.client.Client _client;

            private UriBuilder _uriBuilder;

            private Map<String, Object> _templateAndMatrixParameterValues;



            private Param(com.sun.jersey.api.client.Client client,
UriBuilder uriBuilder, Map<String, Object> map) {

                _client = client;

                _uriBuilder = uriBuilder.clone();

                _templateAndMatrixParameterValues = map;

            }



            /**

             * Create new instance using existing Client instance, and a
base URI and any parameters

             *

             */

            public Param(com.sun.jersey.api.client.Client client, URI
baseUri, String param) {

                _client = client;

                _uriBuilder = UriBuilder.fromUri(baseUri);

                _uriBuilder = _uriBuilder.path("/{param}");

                _templateAndMatrixParameterValues = new HashMap<String,
Object>();

                _templateAndMatrixParameterValues.put("param", param);

            }



            /**

             * Create new instance using existing Client instance, and the
URI from which the parameters will be extracted

             *

             */

            public Param(com.sun.jersey.api.client.Client client, URI uri) {

                _client = client;

                StringBuilder template = new
StringBuilder(BASE_URI.toString());

                if (template.charAt((template.length()- 1))!= '/') {

                    template.append("/hello/{param}");

                } else {

                    template.append("hello/{param}");

                }

                _uriBuilder = UriBuilder.fromPath(template.toString());

                _templateAndMatrixParameterValues = new HashMap<String,
Object>();

                UriTemplate uriTemplate = new
UriTemplate(template.toString());

                HashMap<String, String> parameters = new HashMap<String,
String>();

                uriTemplate.match(uri.toString(), parameters);

                _templateAndMatrixParameterValues.putAll(parameters);

            }



            /**

             * Get param

             *

             */

            public String getParam() {

                return ((String)
_templateAndMatrixParameterValues.get("param"));

            }



            /**

             * Duplicate state and set param

             *

             */

            public Localhost_RESTfulExampleRest.Hello.Param setParam(String
param) {

                Map<String, Object> copyMap;

                copyMap = new HashMap<String,
Object>(_templateAndMatrixParameterValues);

                UriBuilder copyUriBuilder = _uriBuilder.clone();

                copyMap.put("param", param);

                return new
Localhost_RESTfulExampleRest.Hello.Param(_client, copyUriBuilder, copyMap);

            }



            public<T >T getAs(GenericType<T> returnType) {

                UriBuilder localUriBuilder = _uriBuilder.clone();

                com.sun.jersey.api.client.WebResource resource =
_client.resource(localUriBuilder.buildFromMap(_templateAndMatrixParameterValues));

                com.sun.jersey.api.client.WebResource.Builder
resourceBuilder = resource.getRequestBuilder();

                resourceBuilder = resourceBuilder.accept("*/*");

                com.sun.jersey.api.client.ClientResponse response;

                response = resourceBuilder.method("GET",
com.sun.jersey.api.client.ClientResponse.class);

                if (response.getStatus()>= 400) {

                    throw new
Localhost_RESTfulExampleRest.WebApplicationExceptionMessage(Response.status(response.getClientResponseStatus()).build());

                }

                return response.getEntity(returnType);

            }



            public<T >T getAs(Class<T> returnType) {

                UriBuilder localUriBuilder = _uriBuilder.clone();

                com.sun.jersey.api.client.WebResource resource =
_client.resource(localUriBuilder.buildFromMap(_templateAndMatrixParameterValues));

                com.sun.jersey.api.client.WebResource.Builder
resourceBuilder = resource.getRequestBuilder();

                resourceBuilder = resourceBuilder.accept("*/*");

                com.sun.jersey.api.client.ClientResponse response;

                response = resourceBuilder.method("GET",
com.sun.jersey.api.client.ClientResponse.class);

                if
(!com.sun.jersey.api.client.ClientResponse.class.isAssignableFrom(returnType))
{

                    if (response.getStatus()>= 400) {

                        throw new
Localhost_RESTfulExampleRest.WebApplicationExceptionMessage(Response.status(response.getClientResponseStatus()).build());

                    }

                }

                if
(!com.sun.jersey.api.client.ClientResponse.class.isAssignableFrom(returnType))
{

                    return response.getEntity(returnType);

                } else {

                    return returnType.cast(response);

                }

            }



        }



    }





    /**

     * Workaround for JAX_RS_SPEC-312

     *

     */

    private static class WebApplicationExceptionMessage

        extends WebApplicationException

    {





        private WebApplicationExceptionMessage(Response response) {

            super(response);

        }



        /**

         * Workaround for JAX_RS_SPEC-312

         *

         */

        public String getMessage() {

            Response response = getResponse();

            Response.Status status =
Response.Status.fromStatusCode(response.getStatus());

            if (status!= null) {

                return (response.getStatus()+(" "+
status.getReasonPhrase()));

            } else {

                return Integer.toString(response.getStatus());

            }

        }



        public String toString() {

            String s = "javax.ws.rs.WebApplicationException";

            String message = getLocalizedMessage();

            return (s +(": "+ message));

        }



    }



}



*George Cong*
Product Developer
ConceptWave Software Inc. (now part of Ericsson)
5935 Airport Road, Suite 1105
Mississauga, ON L4V 1W5 Canada
t: +1.905-293-8538

www.ericsson.com/conceptwave



[image: Inline image 2]




image001.gif
(image/gif attachment: image001.gif)