1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
package df.chainedecaracteres.miniprojet;
import javax.validation.constraints.Past;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;
import df.chainedecaracteres.miniprojet.model.DaoPhrases;
import df.chainedecaracteres.miniprojet.model.Chainedecaracteres;
@Path("/chainedecaractères")
public class ChainedecaracteresRessource {
@Context
UriInfo uriInfo;
@Context
Request request;
String id = "1";
public ChainedecaracteresRessource(UriInfo uriInfo, Request request, String id) {
super();
this.uriInfo = uriInfo;
this.request = request;
this.id = id;
}
public ChainedecaracteresRessource(){
}
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Chainedecaracteres getChainedecaracteres() {
Chainedecaracteres chaine = DaoPhrases.instance.getChainedecaracteres().get(id);
if (chaine == null)
throw new RuntimeException("Get: Chaine id " + id + " inexistant");
return chaine;
}
@GET
@Produces({MediaType.TEXT_XML})
public Chainedecaracteres getChainedecaracteresBrowser() {
Chainedecaracteres chaine = DaoPhrases.instance.getChainedecaracteres().get(id);
if (chaine == null)
throw new RuntimeException("Get: Chaine id " + id + " inexistant");
return chaine;
}
@Path("/{nombre}")
@GET
public char getcaractere(@PathParam("nombre") int nombre) {
Chainedecaracteres chaine = DaoPhrases.instance.getChainedecaracteres().get(id);
char caractere;
if (chaine == null) {
throw new RuntimeException("Get: Chaine id " + id + " inexistant"); }
else {
nombre = nombre -1;
String contenu = chaine.getContenu();
int taille = contenu.length();
if(nombre > taille) {
caractere = ' ';}
else {
caractere = contenu.charAt(nombre);
}
}
return caractere;
}
@Path("/set")
@POST
public Chainedecaracteres newContenu(@QueryParam("contenu") String contenu) {
Chainedecaracteres chaine = DaoPhrases.instance.getChainedecaracteres().get(id);
if (chaine == null) {
throw new RuntimeException("Get: Chaine id " + id + " inexistant"); }
else {
chaine.setContenu(contenu); }
return chaine;
}
@Path("/concat")
@POST
@Produces(MediaType.TEXT_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Chainedecaracteres addContenu(@FormParam("contenu") String contenu) {
String newcontenu;
Chainedecaracteres chaine = DaoPhrases.instance.getChainedecaracteres().get(id);
if (chaine == null) {
throw new RuntimeException("Get: Chaine id " + id + " inexistant"); }
else {
newcontenu = chaine.getContenu();
newcontenu = newcontenu.concat(contenu);
chaine.setContenu(newcontenu);
}
return chaine;
}
} |
Partager