From 0c00e4930734724e9cdbfe9579922e738d99764d Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 3 Sep 2010 14:43:10 +0200 Subject: [PATCH] added jersey for rest api --- scm-webapp/pom.xml | 22 +++++ scm-webapp/src/main/java/sonia/scm/Group.java | 3 + .../scm/api/rest/JsonJaxbContextResolver.java | 73 +++++++++++++++++ .../scm/api/rest/UriExtensionsConfig.java | 82 +++++++++++++++++++ scm-webapp/src/main/webapp/WEB-INF/beans.xml | 21 +++++ scm-webapp/src/main/webapp/WEB-INF/web.xml | 41 ++++++++-- scm-webapp/src/main/webapp/index.html | 14 ++++ scm-webapp/src/main/webapp/index.jsp | 13 --- 8 files changed, 251 insertions(+), 18 deletions(-) create mode 100644 scm-webapp/src/main/java/sonia/scm/api/rest/JsonJaxbContextResolver.java create mode 100644 scm-webapp/src/main/java/sonia/scm/api/rest/UriExtensionsConfig.java create mode 100644 scm-webapp/src/main/webapp/WEB-INF/beans.xml create mode 100644 scm-webapp/src/main/webapp/index.html delete mode 100644 scm-webapp/src/main/webapp/index.jsp diff --git a/scm-webapp/pom.xml b/scm-webapp/pom.xml index f0f9ebe8c5..47520b374d 100644 --- a/scm-webapp/pom.xml +++ b/scm-webapp/pom.xml @@ -34,6 +34,28 @@ test + + + com.sun.jersey + jersey-core + 1.1.0-ea + provided + + + + com.sun.jersey + jersey-json + 1.1.0-ea + provided + + + + com.sun.jersey + jersey-bundle + 1.1.0-ea + provided + + diff --git a/scm-webapp/src/main/java/sonia/scm/Group.java b/scm-webapp/src/main/java/sonia/scm/Group.java index 8582d32cb0..935674afb3 100644 --- a/scm-webapp/src/main/java/sonia/scm/Group.java +++ b/scm-webapp/src/main/java/sonia/scm/Group.java @@ -19,10 +19,13 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import javax.xml.bind.annotation.XmlRootElement; + /** * * @author Sebastian Sdorra */ +@XmlRootElement public class Group implements Serializable { diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/JsonJaxbContextResolver.java b/scm-webapp/src/main/java/sonia/scm/api/rest/JsonJaxbContextResolver.java new file mode 100644 index 0000000000..865d07bedc --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/JsonJaxbContextResolver.java @@ -0,0 +1,73 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + + + +package sonia.scm.api.rest; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.Group; + +//~--- JDK imports ------------------------------------------------------------ + +import com.sun.jersey.api.json.JSONConfiguration; +import com.sun.jersey.api.json.JSONJAXBContext; + +import java.util.Arrays; +import java.util.List; + +import javax.ws.rs.ext.ContextResolver; +import javax.ws.rs.ext.Provider; + +import javax.xml.bind.JAXBContext; + +/** + * + * @author Sebastian Sdorra + */ +@Provider +public class JsonJaxbContextResolver implements ContextResolver +{ + + /** + * Constructs ... + * + * + * @throws Exception + */ + public JsonJaxbContextResolver() throws Exception + { + this.context = new JSONJAXBContext( + JSONConfiguration.mapped().rootUnwrapping(true).arrays( + "members").build(), types.toArray(new Class[0])); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param objectType + * + * @return + */ + @Override + public JAXBContext getContext(Class objectType) + { + return (types.contains(objectType)) + ? context + : null; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private JAXBContext context; + + /** Field description */ + private List types = Arrays.asList(new Class[] { Group.class }); +} diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/UriExtensionsConfig.java b/scm-webapp/src/main/java/sonia/scm/api/rest/UriExtensionsConfig.java new file mode 100644 index 0000000000..133eccafd3 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/UriExtensionsConfig.java @@ -0,0 +1,82 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + + + +package sonia.scm.api.rest; + +//~--- JDK imports ------------------------------------------------------------ + +import com.sun.jersey.api.core.PackagesResourceConfig; + +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.core.MediaType; + +/** + * + * @author Sebastian Sdorra + */ +public class UriExtensionsConfig extends PackagesResourceConfig +{ + + /** + * Constructs ... + * + */ + public UriExtensionsConfig() + { + super(); + } + + /** + * Constructs ... + * + * + * @param props + */ + public UriExtensionsConfig(Map props) + { + super(props); + } + + /** + * Constructs ... + * + * + * @param paths + */ + public UriExtensionsConfig(String[] paths) + { + super(paths); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public Map getMediaTypeMappings() + { + if (mediaTypeMap == null) + { + mediaTypeMap = new HashMap(); + mediaTypeMap.put("json", MediaType.APPLICATION_JSON_TYPE); + mediaTypeMap.put("xml", MediaType.APPLICATION_XML_TYPE); + } + + return mediaTypeMap; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private Map mediaTypeMap; +} diff --git a/scm-webapp/src/main/webapp/WEB-INF/beans.xml b/scm-webapp/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000000..bb8ba6ac8f --- /dev/null +++ b/scm-webapp/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,21 @@ + + + + + + + + + diff --git a/scm-webapp/src/main/webapp/WEB-INF/web.xml b/scm-webapp/src/main/webapp/WEB-INF/web.xml index 6ea7250574..62b993fe68 100644 --- a/scm-webapp/src/main/webapp/WEB-INF/web.xml +++ b/scm-webapp/src/main/webapp/WEB-INF/web.xml @@ -4,10 +4,41 @@ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> - - + + Jersey Web Application + com.sun.jersey.spi.container.servlet.ServletContainer + + com.sun.jersey.config.feature.Redirect + true + + + com.sun.jersey.config.feature.ImplicitViewables + true + + + com.sun.jersey.config.property.packages + sonia.scm.api.rest + + + com.sun.jersey.config.property.resourceConfigClass + sonia.scm.api.rest.UriExtensionsConfig + + 1 + + + + Jersey Web Application + /api/rest/* + + + + index.html + + + + 30 - - - + + + diff --git a/scm-webapp/src/main/webapp/index.html b/scm-webapp/src/main/webapp/index.html new file mode 100644 index 0000000000..35110cae59 --- /dev/null +++ b/scm-webapp/src/main/webapp/index.html @@ -0,0 +1,14 @@ + + + + + + + + + TODO write content + + diff --git a/scm-webapp/src/main/webapp/index.jsp b/scm-webapp/src/main/webapp/index.jsp deleted file mode 100644 index ab292c3479..0000000000 --- a/scm-webapp/src/main/webapp/index.jsp +++ /dev/null @@ -1,13 +0,0 @@ -<%@page contentType="text/html" pageEncoding="UTF-8"%> - - - - - - JSP Page - - -

Hello World!

- -