Last active
May 24, 2018 20:51
-
-
Save fmbenhassine/10ae6566f57373691297 to your computer and use it in GitHub Desktop.
Jersey lab #lab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.jeelabs.ws.dictionary; | |
import com.google.gson.Gson; | |
import com.thoughtworks.xstream.XStream; | |
import io.github.benas.jeelabs.ws.dictionary.model.Word; | |
import io.github.benas.jeelabs.ws.dictionary.persistence.InMemoryDictionary; | |
import javax.ws.rs.*; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.core.Response.Status; | |
import javax.xml.bind.JAXBElement; | |
@Path("/dictionary") | |
public class DictionaryResource { | |
private static InMemoryDictionary dictionary = InMemoryDictionary.getInstance(); | |
XStream xStream = new XStream(); | |
Gson gson = new Gson(); | |
@GET | |
@Path("{word}") | |
public Response getWord(@PathParam("word") String name) { | |
Word word = dictionary.getWord(name); | |
if (word == null) | |
return Response.status(Status.NOT_FOUND).build(); | |
return Response.ok(word.toString()).build(); | |
} | |
@GET | |
@Path("{word}") | |
@Produces({MediaType.APPLICATION_XML}) | |
public Response getXmlWord(@PathParam("word") String name) { | |
Word word = dictionary.getWord(name); | |
if (word == null) | |
return Response.status(Status.NOT_FOUND).build(); | |
return Response.ok(xStream.toXML(word)).build(); | |
} | |
@GET | |
@Path("{word}") | |
@Produces({MediaType.APPLICATION_JSON}) | |
public Response getJsonWord(@PathParam("word") String name) { | |
Word word = dictionary.getWord(name); | |
if (word == null) | |
return Response.status(Status.NOT_FOUND).build(); | |
return Response.ok(gson.toJson(word)).build(); | |
} | |
@PUT | |
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) | |
public Response putWord(JAXBElement<Word> word) { | |
dictionary.putWord(word.getValue()); | |
return Response.ok().build(); | |
} | |
@POST | |
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) | |
public Response postWord(JAXBElement<Word> word) { | |
dictionary.putWord(word.getValue()); | |
return Response.ok().build(); | |
} | |
@DELETE | |
@Path("{word}") | |
public Response deleteWord(@PathParam("word") String name) { | |
Word word = dictionary.getWord(name); | |
if (word == null) | |
return Response.status(Status.NOT_FOUND).build(); | |
dictionary.removeWord(name); | |
return Response.ok().build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.jeelabs.ws.dictionary.persistence; | |
import io.github.benas.jeelabs.ws.dictionary.model.Word; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class InMemoryDictionary { | |
private Map<String, Word> words = new HashMap<String, Word>(); | |
private static InMemoryDictionary dictionary; | |
public static InMemoryDictionary getInstance() { | |
if (dictionary == null) | |
dictionary = new InMemoryDictionary(); | |
return dictionary; | |
} | |
private InMemoryDictionary() { | |
Word brie = new Word("brie", "a french cheese"); | |
putWord(brie); | |
} | |
public Word getWord(String name) { | |
return words.get(name); | |
} | |
//serves as add and update | |
public void putWord(Word word) { | |
words.put(word.getName(), word); | |
} | |
public void removeWord(String name) { | |
words.remove(name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.jeelabs.ws.dictionary; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.webapp.WebAppContext; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
String weppAppHome = args[0]; | |
Integer port = 8080; | |
Server server = new Server(port); | |
WebAppContext webapp = new WebAppContext(); | |
webapp.setContextPath("/"); | |
webapp.setCompactPath(true); | |
webapp.setDescriptor(weppAppHome + "/WEB-INF/web.xml"); | |
webapp.setResourceBase(weppAppHome); | |
webapp.setParentLoaderPriority(true); | |
server.setHandler(webapp); | |
server.start(); | |
server.join(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<artifactId>web-services</artifactId> | |
<packaging>war</packaging> | |
<version>1.0</version> | |
<name>Web Services labs</name> | |
<description>labs for JAX-RS web services</description> | |
<dependencies> | |
<dependency> | |
<groupId>javax</groupId> | |
<artifactId>javaee-web-api</artifactId> | |
<version>7.0</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.thoughtworks.xstream</groupId> | |
<artifactId>xstream</artifactId> | |
<version>1.4.7</version> | |
</dependency> | |
<dependency> | |
<groupId>com.google.code.gson</groupId> | |
<artifactId>gson</artifactId> | |
<version>2.2.4</version> | |
</dependency> | |
<dependency> | |
<groupId>com.sun.jersey</groupId> | |
<artifactId>jersey-core</artifactId> | |
<version>1.18.1</version> | |
</dependency> | |
<dependency> | |
<groupId>com.sun.jersey</groupId> | |
<artifactId>jersey-servlet</artifactId> | |
<version>1.18.1</version> | |
</dependency> | |
<dependency> | |
<groupId>com.sun.jersey</groupId> | |
<artifactId>jersey-server</artifactId> | |
<version>1.18.1</version> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-webapp</artifactId> | |
<version>9.1.3.v20140225</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-war-plugin</artifactId> | |
<version>2.4</version> | |
<configuration> | |
<warName>${project.artifactId}</warName> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" | |
version="3.1"> | |
<servlet> | |
<servlet-name>ServletAdaptor</servlet-name> | |
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> | |
<load-on-startup>1</load-on-startup> | |
<init-param> | |
<param-name>com.sun.jersey.config.property.packages</param-name> | |
<param-value>io.github.benas.jeelabs.ws.dictionary</param-value> | |
</init-param> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>ServletAdaptor</servlet-name> | |
<url-pattern>/resources/*</url-pattern> | |
</servlet-mapping> | |
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.jeelabs.ws.dictionary.model; | |
import javax.xml.bind.annotation.XmlRootElement; | |
@XmlRootElement | |
public class Word { | |
private String name; | |
private String definition; | |
public Word() { | |
} | |
public Word(String name) { | |
super(); | |
this.name = name; | |
} | |
public Word(String name, String definition) { | |
super(); | |
this.name = name; | |
this.definition = definition; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getDefinition() { | |
return definition; | |
} | |
public void setDefinition(String definition) { | |
this.definition = definition; | |
} | |
@Override | |
public String toString() { | |
return "Word{" + | |
"name='" + name + '\'' + | |
", definition='" + definition + '\'' + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment