diff --git a/wadl/wadl-core/pom.xml b/wadl/wadl-core/pom.xml index 1764d7a..e2f5ee0 100644 --- a/wadl/wadl-core/pom.xml +++ b/wadl/wadl-core/pom.xml @@ -44,6 +44,11 @@ javax.xml.bind jaxb-api + + com.sun.jersey + jersey-client + 1.0.1-SNAPSHOT + diff --git a/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/http/ClientConfigurator.java b/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/http/ClientConfigurator.java new file mode 100644 index 0000000..e1fc3ad --- /dev/null +++ b/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/http/ClientConfigurator.java @@ -0,0 +1,69 @@ +/* + * 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 + * http://www.opensource.org/licenses/cddl1.php + * See the License for the specific language governing + * permissions and limitations under the License. + */ + +package org.jvnet.ws.wadl.http; + +import com.sun.jersey.api.client.ClientHandler; +import com.sun.jersey.api.client.config.ClientConfig; +import com.sun.jersey.api.client.config.DefaultClientConfig; + + +/** + Holds configuration information used by the jersey-client API. + + @author jorgew + **/ + +public class ClientConfigurator { + private static ClientHandler handler = null; + private static ClientConfig config = null; + + /** + Gets the current client handler. + + @return The current client handler, null if the default handler + is bing used. + **/ + protected static ClientHandler getClientHandler() { + return handler; + } + + /** + Sets the current client handler. + + @param clientHandler The client handler to use. Set to null to + use the default handler. + **/ + public static void setClientHandler (ClientHandler clientHandler) { + handler = clientHandler; + } + + /** + Gets current client config. + + @return The current client config, null if the default + configuration should be used. + **/ + protected static ClientConfig getClientConfig() { + return config; + } + + /** + Sets the current client config. + + @param clientConfig The client configuration to use. Set to + null to use the default handler. + **/ + public static void setClientConfig (ClientConfig clientConfig) { + config = clientConfig; + } +} diff --git a/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/http/ClientFactory.java b/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/http/ClientFactory.java new file mode 100644 index 0000000..8f6a112 --- /dev/null +++ b/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/http/ClientFactory.java @@ -0,0 +1,77 @@ +/* + * 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 + * http://www.opensource.org/licenses/cddl1.php + * See the License for the specific language governing + * permissions and limitations under the License. + */ + +package org.jvnet.ws.wadl.http; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientHandler; +import com.sun.jersey.api.client.config.ClientConfig; +import com.sun.jersey.api.client.config.DefaultClientConfig; + + +/** + This class is used to create a Jersy client. The client uses the + configuration and handler set in the {@link + org.jvnet.ws.wadl.http.ClientConfigurator}. + + @author jorgew +**/ + +public class ClientFactory { + + // + // We cache the last client to avoid recreating it.Chances are + // we'll be resusing it with the same configuration options over + // and over again... + // + private static Client lastClient; + private static ClientConfig lastConfig; + private static ClientHandler lastHandler; + + /** + Creates a Jersy client. + + @return A client with the configuration and handler set in the + {@link org.jvnet.ws.wadl.http.ClientConfigurator}. + **/ + public static Client createClient() { + + ClientConfig config = ClientConfigurator.getClientConfig(); + ClientHandler handler = ClientConfigurator.getClientHandler(); + Client retClient; + + // + // Should we reuse the cached client? + // + if ((lastClient != null) && + (config == lastConfig) && + (handler == lastHandler)) { + return lastClient; + } + + if (config == null) { + config = new DefaultClientConfig(); + } + + if (handler == null) { + retClient = Client.create(config); + } else { + retClient = new Client (handler, config); + } + + lastConfig = config; + lastHandler = handler; + lastClient = retClient; + + return retClient; + } +} diff --git a/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/util/DSDispatcher.java b/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/util/DSDispatcher.java index b859d05..a561201 100755 --- a/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/util/DSDispatcher.java +++ b/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/util/DSDispatcher.java @@ -19,16 +19,18 @@ package org.jvnet.ws.wadl.util; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; +import java.net.URISyntaxException; +import java.net.URI; import java.util.Map; import javax.activation.DataSource; +import com.sun.jersey.api.client.UniformInterfaceException; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; + +import org.jvnet.ws.wadl.http.ClientFactory; + /** * Comms utility containing methods used by code * generated for WADL methods. @@ -45,175 +47,124 @@ public class DSDispatcher { /** * Perform a HTTP GET on the resource * @return the unmarshalled resource representation. - * @param url the URL of the resource + * @param uri the URI of the resource * @param expectedMimeType the MIME type that will be used in the HTTP Accept header */ - public DataSource doGET(String url, Map httpHeaders, String expectedMimeType) throws MalformedURLException, IOException { - URL u = new URL(url); - URLConnection c = u.openConnection(); - InputStream in = null; - String mediaType = null; - if (c instanceof HttpURLConnection) { - HttpURLConnection h = (HttpURLConnection)c; - h.setRequestMethod("GET"); - setAccept(h, expectedMimeType); - for(String key: httpHeaders.keySet()) - h.setRequestProperty(key, httpHeaders.get(key).toString()); - h.connect(); - mediaType = h.getContentType(); - if (h.getResponseCode() < 400) - in = h.getInputStream(); - else - in = h.getErrorStream(); + public DataSource doGET(String uri, Map httpHeaders, String expectedMimeType) + throws URISyntaxException, UniformInterfaceException { + URI u = new URI(uri); + Client client = ClientFactory.createClient(); + WebResource res=client.resource(u); + WebResource.Builder r=null; + if (expectedMimeType != null) { + r=res.accept(expectedMimeType); } - - return new StreamDataSource(mediaType, in); + for(String key: httpHeaders.keySet()) { + if (r != null) { + r=r.header (key, httpHeaders.get(key)); + } else { + r=res.header (key, httpHeaders.get(key)); + } + } + + return (DataSource) r.get(DataSource.class); } /** * Perform a HTTP DELETE on the resource * @return the unmarshalled resource representation. - * @param url the URL of the resource + * @param uri the URI of the resource * @param expectedMimeType the MIME type that will be used in the HTTP Accept header */ - public DataSource doDELETE(String url, Map httpHeaders, String expectedMimeType) throws MalformedURLException, IOException { - URL u = new URL(url); - URLConnection c = u.openConnection(); - InputStream in = null; - String mediaType = null; - if (c instanceof HttpURLConnection) { - HttpURLConnection h = (HttpURLConnection)c; - h.setRequestMethod("DELETE"); - if (expectedMimeType != null) - h.setRequestProperty("Accept", expectedMimeType); - for(String key: httpHeaders.keySet()) - h.setRequestProperty(key, httpHeaders.get(key).toString()); - h.connect(); - mediaType = h.getContentType(); - if (h.getResponseCode() < 400) - in = h.getInputStream(); - else - in = h.getErrorStream(); + public DataSource doDELETE(String uri, Map httpHeaders, String expectedMimeType) + throws URISyntaxException, UniformInterfaceException { + URI u = new URI(uri); + Client client = ClientFactory.createClient(); + WebResource res = client.resource(u); + WebResource.Builder r=null; + if (expectedMimeType != null) { + r=res.accept(expectedMimeType); } - - return new StreamDataSource(mediaType, in); + for(String key: httpHeaders.keySet()) { + if (r != null) { + r=r.header (key, httpHeaders.get(key)); + } else { + r=res.header (key, httpHeaders.get(key)); + } + } + + return (DataSource) r.delete (DataSource.class); } /** * Perform a HTTP POST on the resource * * @return the unmarshalled resource representation. - * @param url the URL of the resource + * @param uri the URI of the resource * @param input the body of the POST request * @param inputMimeType the MIME type of the body of the POST request * @param expectedMimeType the MIME type that will be used in the HTTP Accept header */ - public DataSource doPOST(DataSource input, String inputMimeType, String url, Map httpHeaders, String expectedMimeType) throws MalformedURLException, IOException { - URL u = new URL(url); - URLConnection c = u.openConnection(); - InputStream in = null; - String mediaType = null; - if (c instanceof HttpURLConnection) { - HttpURLConnection h = (HttpURLConnection)c; - h.setRequestMethod("POST"); - h.setChunkedStreamingMode(-1); - setAccept(h, expectedMimeType); - h.setRequestProperty("Content-Type", inputMimeType); - for(String key: httpHeaders.keySet()) - h.setRequestProperty(key, httpHeaders.get(key).toString()); - h.setDoOutput(true); - h.connect(); - OutputStream out = h.getOutputStream(); - byte buffer[] = new byte[4096]; - int bytes; - InputStream inputStream = input.getInputStream(); - while ((bytes = inputStream.read(buffer)) != -1) { - out.write(buffer, 0, bytes); - } - out.close(); - mediaType = h.getContentType(); - if (h.getResponseCode() < 400) - in = h.getInputStream(); - else - in = h.getErrorStream(); + public DataSource doPOST(DataSource input, String inputMimeType, String uri, Map httpHeaders, String expectedMimeType) + throws URISyntaxException, UniformInterfaceException { + URI u = new URI(uri); + Client client = ClientFactory.createClient(); + WebResource.Builder r = client.resource(u).type(inputMimeType); + if (expectedMimeType != null) { + r=r.accept(expectedMimeType); } - - return new StreamDataSource(mediaType, in); + for(String key: httpHeaders.keySet()) + r = r.header (key, httpHeaders.get(key)); + + return (DataSource) r.post (DataSource.class, input); } /** * Perform a HTTP PUT on the resource * * @return the unmarshalled resource representation. - * @param url the URL of the resource + * @param uri the URI of the resource * @param input the body of the POST request * @param inputMimeType the MIME type of the body of the POST request * @param expectedMimeType the MIME type that will be used in the HTTP Accept header */ - public DataSource doPUT(DataSource input, String inputMimeType, String url, Map httpHeaders, String expectedMimeType) throws MalformedURLException, IOException { - URL u = new URL(url); - URLConnection c = u.openConnection(); - InputStream in = null; - String mediaType = null; - if (c instanceof HttpURLConnection) { - HttpURLConnection h = (HttpURLConnection)c; - h.setRequestMethod("PUT"); - h.setChunkedStreamingMode(-1); - setAccept(h, expectedMimeType); - h.setRequestProperty("Content-Type", inputMimeType); - for(String key: httpHeaders.keySet()) - h.setRequestProperty(key, httpHeaders.get(key).toString()); - h.setDoOutput(true); - h.connect(); - OutputStream out = h.getOutputStream(); - byte buffer[] = new byte[4096]; - int bytes; - InputStream inputStream = input.getInputStream(); - while ((bytes = inputStream.read(buffer)) != -1) { - out.write(buffer, 0, bytes); - } - out.close(); - mediaType = h.getContentType(); - if (h.getResponseCode() < 400) - in = h.getInputStream(); - else - in = h.getErrorStream(); + public DataSource doPUT(DataSource input, String inputMimeType, String uri, Map httpHeaders, String expectedMimeType) + throws URISyntaxException, UniformInterfaceException { + URI u = new URI(uri); + Client client = ClientFactory.createClient(); + WebResource.Builder r = client.resource(u).type(inputMimeType); + if (expectedMimeType != null) { + r=r.accept(expectedMimeType); } - - return new StreamDataSource(mediaType, in); + for(String key: httpHeaders.keySet()) + r = r.header (key, httpHeaders.get(key)); + + return (DataSource) r.put (DataSource.class, input); } /** * Perform a HTTP OPTIONS on the resource * @return the unmarshalled resource representation. - * @param url the URL of the resource + * @param uri the URI of the resource * @param expectedMimeType the MIME type that will be used in the HTTP Accept header */ - public DataSource doOPTIONS(String url, Map httpHeaders, String expectedMimeType) throws MalformedURLException, IOException { - URL u = new URL(url); - URLConnection c = u.openConnection(); - InputStream in = null; - String mediaType = null; - if (c instanceof HttpURLConnection) { - HttpURLConnection h = (HttpURLConnection)c; - h.setRequestMethod("OPTIONS"); - setAccept(h, expectedMimeType); - for(String key: httpHeaders.keySet()) - h.setRequestProperty(key, httpHeaders.get(key).toString()); - h.connect(); - mediaType = h.getContentType(); - if (h.getResponseCode() < 400) - in = h.getInputStream(); - else - in = h.getErrorStream(); + public DataSource doOPTIONS(String uri, Map httpHeaders, String expectedMimeType) + throws URISyntaxException, UniformInterfaceException { + URI u = new URI(uri); + Client client = ClientFactory.createClient(); + WebResource res=client.resource(u); + WebResource.Builder r=null; + if (expectedMimeType != null) { + r=res.accept(expectedMimeType); + } + for(String key: httpHeaders.keySet()) { + if (r != null) { + r=r.header (key, httpHeaders.get(key)); + } else { + r=res.header (key, httpHeaders.get(key)); + } } - - return new StreamDataSource(mediaType, in); - } - - public static void setAccept(HttpURLConnection connection, String expectedMimeType) { - if (expectedMimeType != null) - connection.setRequestProperty("Accept", expectedMimeType); - } + return (DataSource) r.options (DataSource.class); + } } diff --git a/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/util/JAXBDispatcher.java b/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/util/JAXBDispatcher.java index 5a12344..8731433 100755 --- a/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/util/JAXBDispatcher.java +++ b/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/util/JAXBDispatcher.java @@ -19,19 +19,21 @@ package org.jvnet.ws.wadl.util; -import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; +import java.net.URISyntaxException; +import java.net.URI; import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; +import com.sun.jersey.api.client.UniformInterfaceException; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; + +import org.jvnet.ws.wadl.http.ClientFactory; + /** * A wrapper for JAX-WS Dispatch containing methods used by code * generated for WADL methods. @@ -58,32 +60,42 @@ public class JAXBDispatcher { * * * @return the unmarshalled resource representation. - * @param url the URL of the resource + * @param uri the URI of the resource * @param expectedMimeType the MIME type that will be used in the HTTP Accept header */ - public Object doGET(String url, Map httpHeaders, String expectedMimeType) throws MalformedURLException, IOException, JAXBException { - URL u = new URL(url); - URLConnection c = u.openConnection(); - InputStream in = null; - String mediaType = null; - if (c instanceof HttpURLConnection) { - HttpURLConnection h = (HttpURLConnection)c; - h.setRequestMethod("GET"); - DSDispatcher.setAccept(h, expectedMimeType); - for(String key: httpHeaders.keySet()) - h.setRequestProperty(key, httpHeaders.get(key).toString()); - h.connect(); - mediaType = h.getContentType(); - if (h.getResponseCode() < 400) - in = h.getInputStream(); - else - in = h.getErrorStream(); + public Object doGET(String uri, Map httpHeaders, String expectedMimeType) + throws URISyntaxException, UniformInterfaceException, JAXBException { + + InputStream entityStream = null; + + try { + URI u = new URI(uri); + Client client = ClientFactory.createClient(); + WebResource res = client.resource(u); + WebResource.Builder r=null; + if (expectedMimeType != null) { + r=res.accept(expectedMimeType); + } + for(String key: httpHeaders.keySet()){ + if (r != null){ + r=r.header (key, httpHeaders.get(key)); + } else { + r=res.header(key, httpHeaders.get(key)); + } + } + ClientResponse response = r.get(ClientResponse.class); + + Unmarshaller um = jc.createUnmarshaller(); + Object o = um.unmarshal(entityStream = response.getEntityInputStream()); + + return o; + } finally { + if (entityStream != null) + { + try { entityStream.close();} + catch (Exception e){} //ignore + } } - - Unmarshaller um = jc.createUnmarshaller(); - Object o = um.unmarshal(in); - - return o; } /** @@ -91,73 +103,80 @@ public class JAXBDispatcher { * * * @return the unmarshalled resource representation. - * @param url the URL of the resource + * @param uri the URI of the resource * @param expectedMimeType the MIME type that will be used in the HTTP Accept header */ - public Object doDELETE(String url, Map httpHeaders, String expectedMimeType) throws MalformedURLException, IOException, JAXBException { - URL u = new URL(url); - URLConnection c = u.openConnection(); - InputStream in = null; - String mediaType = null; - if (c instanceof HttpURLConnection) { - HttpURLConnection h = (HttpURLConnection)c; - h.setRequestMethod("DELETE"); - DSDispatcher.setAccept(h, expectedMimeType); - for(String key: httpHeaders.keySet()) - h.setRequestProperty(key, httpHeaders.get(key).toString()); - h.connect(); - mediaType = h.getContentType(); - if (h.getResponseCode() < 400) - in = h.getInputStream(); - else - in = h.getErrorStream(); - } + public Object doDELETE(String uri, Map httpHeaders, String expectedMimeType) + throws URISyntaxException, UniformInterfaceException, JAXBException { - Unmarshaller um = jc.createUnmarshaller(); - Object o = um.unmarshal(in); + InputStream entityStream = null; - return o; + try { + URI u = new URI(uri); + Client client = ClientFactory.createClient(); + WebResource res = client.resource(u); + WebResource.Builder r=null; + if (expectedMimeType != null){ + r = res.accept(expectedMimeType); + } + for(String key: httpHeaders.keySet()){ + if (r != null) { + r=r.header (key, httpHeaders.get(key)); + } else { + r=res.header (key, httpHeaders.get(key)); + } + } + ClientResponse response = r.delete(ClientResponse.class); + + Unmarshaller um = jc.createUnmarshaller(); + Object o = um.unmarshal(entityStream = response.getEntityInputStream()); + + return o; + } finally { + if (entityStream != null) + { + try { entityStream.close();} + catch (Exception e){} //ignore + } + } } /** * Perform a HTTP POST on the resource * * @return the unmarshalled resource representation. - * @param url the URL of the resource + * @param uri the URI of the resource * @param input the body of the POST request * @param inputMimeType the MIME type of the body of the POST request * @param expectedMimeType the MIME type that will be used in the HTTP Accept header */ - public Object doPOST(Object input, String inputMimeType, String url, Map httpHeaders, String expectedMimeType) throws MalformedURLException, IOException, JAXBException { - URL u = new URL(url); - URLConnection c = u.openConnection(); - InputStream in = null; - String mediaType = null; - if (c instanceof HttpURLConnection) { - HttpURLConnection h = (HttpURLConnection)c; - h.setRequestMethod("POST"); - h.setChunkedStreamingMode(-1); - DSDispatcher.setAccept(h, expectedMimeType); - h.setRequestProperty("Content-Type", inputMimeType); + public Object doPOST(Object input, String inputMimeType, String uri, Map httpHeaders, String expectedMimeType) + throws URISyntaxException, UniformInterfaceException, JAXBException { + + InputStream entityStream = null; + + try { + URI u = new URI(uri); + Client client = ClientFactory.createClient(); + WebResource.Builder r = client.resource(u).type(inputMimeType); + if (expectedMimeType != null) { + r=r.accept(expectedMimeType); + } for(String key: httpHeaders.keySet()) - h.setRequestProperty(key, httpHeaders.get(key).toString()); - h.setDoOutput(true); - h.connect(); - OutputStream out = h.getOutputStream(); - Marshaller m = jc.createMarshaller(); - m.marshal(input, out); - out.close(); - mediaType = h.getContentType(); - if (h.getResponseCode() < 400) - in = h.getInputStream(); - else - in = h.getErrorStream(); - } + r = r.header (key, httpHeaders.get(key)); + ClientResponse response = r.post(ClientResponse.class, input); - Unmarshaller um = jc.createUnmarshaller(); - Object o = um.unmarshal(in); - - return o; + Unmarshaller um = jc.createUnmarshaller(); + Object o = um.unmarshal(entityStream = response.getEntityInputStream()); + + return o; + } finally { + if (entityStream != null) + { + try { entityStream.close();} + catch (Exception e){} //ignore + } + } } @@ -165,41 +184,38 @@ public class JAXBDispatcher { * Perform a HTTP PUT on the resource * * @return the unmarshalled resource representation. - * @param url the URL of the resource + * @param uri the URI of the resource * @param input the body of the POST request * @param inputMimeType the MIME type of the body of the POST request * @param expectedMimeType the MIME type that will be used in the HTTP Accept header */ - public Object doPUT(Object input, String inputMimeType, String url, Map httpHeaders, String expectedMimeType) throws MalformedURLException, IOException, JAXBException { - URL u = new URL(url); - URLConnection c = u.openConnection(); - InputStream in = null; - String mediaType = null; - if (c instanceof HttpURLConnection) { - HttpURLConnection h = (HttpURLConnection)c; - h.setRequestMethod("PUT"); - h.setChunkedStreamingMode(-1); - DSDispatcher.setAccept(h, expectedMimeType); - h.setRequestProperty("Content-Type", inputMimeType); + public Object doPUT(Object input, String inputMimeType, String uri, Map httpHeaders, String expectedMimeType) + throws URISyntaxException, UniformInterfaceException, JAXBException { + + InputStream entityStream = null; + + try { + URI u = new URI(uri); + Client client = ClientFactory.createClient(); + WebResource.Builder r = client.resource(u).type(inputMimeType); + if (expectedMimeType != null) { + r=r.accept(expectedMimeType); + } for(String key: httpHeaders.keySet()) - h.setRequestProperty(key, httpHeaders.get(key).toString()); - h.setDoOutput(true); - h.connect(); - OutputStream out = h.getOutputStream(); - Marshaller m = jc.createMarshaller(); - m.marshal(input, out); - out.close(); - mediaType = h.getContentType(); - if (h.getResponseCode() < 400) - in = h.getInputStream(); - else - in = h.getErrorStream(); - } + r = r.header (key, httpHeaders.get(key)); + ClientResponse response = r.put(ClientResponse.class, input); - Unmarshaller um = jc.createUnmarshaller(); - Object o = um.unmarshal(in); - - return o; + Unmarshaller um = jc.createUnmarshaller(); + Object o = um.unmarshal(entityStream = response.getEntityInputStream()); + + return o; + } finally { + if (entityStream != null) + { + try { entityStream.close();} + catch (Exception e){} //ignore + } + } } /** @@ -207,32 +223,42 @@ public class JAXBDispatcher { * * * @return the unmarshalled resource representation. - * @param url the URL of the resource + * @param uri the URI of the resource * @param expectedMimeType the MIME type that will be used in the HTTP Accept header */ - public Object doOPTIONS(String url, Map httpHeaders, String expectedMimeType) throws MalformedURLException, IOException, JAXBException { - URL u = new URL(url); - URLConnection c = u.openConnection(); - InputStream in = null; - String mediaType = null; - if (c instanceof HttpURLConnection) { - HttpURLConnection h = (HttpURLConnection)c; - h.setRequestMethod("OPTIONS"); - DSDispatcher.setAccept(h, expectedMimeType); - for(String key: httpHeaders.keySet()) - h.setRequestProperty(key, httpHeaders.get(key).toString()); - h.connect(); - mediaType = h.getContentType(); - if (h.getResponseCode() < 400) - in = h.getInputStream(); - else - in = h.getErrorStream(); + public Object doOPTIONS(String uri, Map httpHeaders, String expectedMimeType) + throws URISyntaxException, UniformInterfaceException, JAXBException { + + InputStream entityStream = null; + + try { + URI u = new URI(uri); + Client client = ClientFactory.createClient(); + WebResource res=client.resource(u); + WebResource.Builder r=null; + if (expectedMimeType != null) { + r=res.accept(expectedMimeType); + } + for(String key: httpHeaders.keySet()) { + if (r != null) { + r=r.header (key, httpHeaders.get(key)); + } else { + r=res.header(key, httpHeaders.get(key)); + } + } + ClientResponse response = r.options(ClientResponse.class); + + Unmarshaller um = jc.createUnmarshaller(); + Object o = um.unmarshal(entityStream = response.getEntityInputStream()); + + return o; + } finally { + if (entityStream != null) + { + try { entityStream.close();} + catch (Exception e){} //ignore + } } - - Unmarshaller um = jc.createUnmarshaller(); - Object o = um.unmarshal(in); - - return o; } } diff --git a/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/util/StreamDataSource.java b/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/util/StreamDataSource.java deleted file mode 100644 index 37181a6..0000000 --- a/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl/util/StreamDataSource.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 - * http://www.opensource.org/licenses/cddl1.php - * See the License for the specific language governing - * permissions and limitations under the License. - */ - -/* - * StreamDataSource.java - * - * Created on April 18, 2007, 3:28 PM - * - */ - -package org.jvnet.ws.wadl.util; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import javax.activation.DataSource; - -/** - * - * @author mh124079 - */ -public class StreamDataSource implements DataSource { - - String mediaType; - InputStream in; - - /** Creates a new instance of StreamDataSource */ - public StreamDataSource(String mediaType, InputStream in) { - this.mediaType = mediaType; - this.in = in; - } - - public String getContentType() { - return mediaType; - } - - public InputStream getInputStream() throws IOException { - return in; - } - - public String getName() { - return "stream"; - } - - public OutputStream getOutputStream() throws IOException { - return null; - } - -} diff --git a/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl2java/ResourceClassGenerator.java b/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl2java/ResourceClassGenerator.java index d0c118e..1d316df 100755 --- a/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl2java/ResourceClassGenerator.java +++ b/wadl/wadl-core/src/main/java/org/jvnet/ws/wadl2java/ResourceClassGenerator.java @@ -35,6 +35,7 @@ import com.sun.codemodel.JMod; import com.sun.codemodel.JPackage; import com.sun.codemodel.JType; import com.sun.codemodel.JVar; +import com.sun.jersey.api.client.UniformInterfaceException; import org.jvnet.ws.wadl.*; import org.jvnet.ws.wadl.util.DSDispatcher; import org.jvnet.ws.wadl.util.JAXBDispatcher; @@ -47,8 +48,7 @@ import org.jvnet.ws.wadl2java.ast.ResourceNode; import org.jvnet.ws.wadl2java.ast.ResourceTypeNode; import com.sun.tools.xjc.api.Mapping; import com.sun.tools.xjc.api.S2JJAXBModel; -import java.io.IOException; -import java.net.MalformedURLException; +import java.net.URISyntaxException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -415,8 +415,8 @@ public class ResourceClassGenerator { // add throws for any required exceptions $genMethod._throws(JAXBException.class); - $genMethod._throws(MalformedURLException.class); - $genMethod._throws(IOException.class); + $genMethod._throws(URISyntaxException.class); + $genMethod._throws(UniformInterfaceException.class); for (JDefinedClass $ex: exceptionMap.values()) { $genMethod._throws($ex); } @@ -509,8 +509,8 @@ public class ResourceClassGenerator { javaDoc.generateReturnDoc(outputRep, $genMethod); // add throws for any required exceptions - $genMethod._throws(MalformedURLException.class); - $genMethod._throws(IOException.class); + $genMethod._throws(URISyntaxException.class); + $genMethod._throws(UniformInterfaceException.class); // add a parameter for the input representation (if required) if (inputType != null) {