Commit 87d1d8cd authored by Gabriele Civitarese's avatar Gabriele Civitarese

First commit

parents
<component name="libraryTable">
<library name="jackson-core-asl-1.9.2">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/jackson-core-asl-1.9.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jackson-jaxrs-1.9.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jackson-mapper-asl-1.9.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jackson-xc-1.9.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jersey-client-1.19.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jersey-core-1.19.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jersey-json-1.19.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jersey-server-1.19.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jersey-servlet-1.19.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jettison-1.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jsr311-api-1.1.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/EsercizioRest.iml" filepath="$PROJECT_DIR$/EsercizioRest.iml" />
</modules>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jackson-core-asl-1.9.2" level="project" />
</component>
</module>
\ No newline at end of file
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
/**
* Created by civi on 26/04/16.
*/
public class StartServer {
private static final String HOST = "localhost";
private static final int PORT = 1337;
public static void main(String[] args) throws IOException {
HttpServer server = HttpServerFactory.create("http://"+HOST+":"+PORT+"/");
server.start();
System.out.println("Server running!");
System.out.println("Server started on: http://"+HOST+":"+PORT);
System.out.println("Hit return to stop...");
System.in.read();
System.out.println("Stopping server");
server.stop(0);
System.out.println("Server stopped");
}
}
package beans;
/**
* Created by civi on 26/04/16.
*/
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
private String name;
private String surname;
public User(){}
public User(String name, String surname) {
this.name = name;
this.surname = surname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
\ No newline at end of file
package beans;
/**
* Created by civi on 26/04/16.
*/
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType (XmlAccessType.FIELD)
public class Users {
@XmlElement(name="users")
private List<User> userslist;
private static Users instance;
public Users() {
userslist = new ArrayList<User>();
}
//singleton
public synchronized static Users getInstance(){
if(instance==null)
instance = new Users();
return instance;
}
public synchronized List<User> getUserslist() {
return new ArrayList<>(userslist);
}
public void setUserslist(List<User> userslist) {
this.userslist = userslist;
}
public synchronized void add(User u){
userslist.add(u);
}
public User getByName(String name){
List<User> usersCopy = getUserslist();
for(User u: usersCopy)
if(u.getName().toLowerCase().equals(name.toLowerCase()))
return u;
return null;
}
}
\ No newline at end of file
<component name="libraryTable">
<library name="jackson-core-asl-1.9.2">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/jackson-core-asl-1.9.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jackson-jaxrs-1.9.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jackson-mapper-asl-1.9.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jackson-xc-1.9.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jersey-client-1.19.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jersey-core-1.19.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jersey-json-1.19.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jersey-server-1.19.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jersey-servlet-1.19.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jettison-1.1.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jsr311-api-1.1.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/EsercizioRest.iml" filepath="$PROJECT_DIR$/EsercizioRest.iml" />
</modules>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jackson-core-asl-1.9.2" level="project" />
</component>
</module>
\ No newline at end of file
package services;
import beans.User;
import beans.Users;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("users")
public class UsersService {
private Users userslist;
//restituisce la lista di utenti
@GET
@Produces({"application/json", "application/xml"})
public Response getUsersList(){
return Response.ok(Users.getInstance()).build();
}
//permette di inserire un utente (nome e cognome)
@Path("add")
@POST
@Consumes({"application/json", "application/xml"})
public Response addUser(User u){
Users.getInstance().add(u);
return Response.ok().build();
}
//permette di prelevare con un determinato nome
@Path("get/{name}")
@GET
@Produces({"application/json", "application/xml"})
public Response getByName(@PathParam("name") String name){
User u = Users.getInstance().getByName(name);
if(u!=null)
return Response.ok(u).build();
else
return Response.status(Response.Status.NOT_FOUND).build();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment