package cactusServer.resources; import java.util.HashSet; 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.WebApplicationException; import javax.ws.rs.core.MediaType; import org.glassfish.jersey.process.internal.RequestScoped; import org.ntlab.radishforandroidstudio.framework.model3D.Position3D; import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D; import cactusServer.entities.Area; import cactusServer.entities.Area.Allowed; import cactusServer.entities.CameraState; import cactusServer.entities.Character; import cactusServer.entities.EmoteState; import cactusServer.entities.IDAddressedEntity; import cactusServer.entities.Instance; import cactusServer.entities.Instance.State; import cactusServer.entities.Plain; import cactusServer.entities.Player; import cactusServer.entities.URIAddressedEntity; import cactusServer.models.Instances; @Path("/instances") @RequestScoped public class InstancesRest { public static final String INSTANCES_URI = "/CactusServer/rest/instances"; public InstancesRest() { } // @Path("/test") // @GET // @Produces(MediaType.TEXT_PLAIN) // public String hoge() { // return "deploy jenkins from tomcat"; // } @Path("/{instanceId}") @GET @Produces(MediaType.APPLICATION_JSON) public Instance getInstance(@PathParam("instanceId") String instanceId) { return Instances.getInstance().getInstance(instanceId); } @Path("/{instanceId}/areas/{areaId}") @GET @Produces(MediaType.APPLICATION_JSON) public Area getArea(@PathParam("instanceId") String instanceId, @PathParam("areaId") String areaId) { Instance instance = Instances.getInstance().getInstance(instanceId); if (instance != null) { return instance.getArea(areaId); } return null; } @Path("/{instanceId}/characters/{characterId}") @GET @Produces(MediaType.APPLICATION_JSON) public Character getCharacter(@PathParam("instanceId") String instanceId, @PathParam("characterId") String characterId) { Instance instance = Instances.getInstance().getInstance(instanceId); if (instance != null) { return instance.getCharacter(characterId); } return null; } @Path("/players/{playerId}") @GET @Produces(MediaType.APPLICATION_JSON) public Player getPlayer(@PathParam("instanceId") String instanceId, @PathParam("playerId") String playerId) { Instances instances = Instances.getInstance(); if (instances != null) { return instances.getPlayer(playerId); } return null; } @POST @Produces(MediaType.APPLICATION_JSON) public URIAddressedEntity createInstance(@FormParam("name") String name, @FormParam("state") Instance.State state, @FormParam("stageModel") String stageModel) { IDAddressedEntity ae = Instances.getInstance().createInstance(name, state, stageModel); return new URIAddressedEntity((INSTANCES_URI + "/" + ae.getId()), ae.getBody()); } @Path("/{instanceId}/areas") @POST @Produces(MediaType.APPLICATION_JSON) public URIAddressedEntity createArea(@PathParam("instanceId") String instanceId, @FormParam("name") String name, @FormParam("region") Plain[] region, @FormParam("permissions") HashSet<Allowed> permissions) { Instance instance = Instances.getInstance().getInstance(instanceId); IDAddressedEntity ae = instance.createArea(name, region, permissions); return new URIAddressedEntity((INSTANCES_URI + "/" + instanceId + "/areas/" + ae.getId()), ae.getBody()); } @Path("/{instanceId}/characters") @POST @Produces(MediaType.APPLICATION_JSON) public URIAddressedEntity createCharacter(@PathParam("instanceId") String instanceId, @FormParam("accountURI") String accountURI, @FormParam("name") String name, @FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle, @FormParam("modelID") String modelID) { Instance instance = Instances.getInstance().getInstance(instanceId); IDAddressedEntity ae = instance.createCharacter(instanceId, accountURI, name, position, angle, modelID); return new URIAddressedEntity((INSTANCES_URI + "/" + instanceId + "/characters/" + ae.getId()), ae.getBody()); } @Path("/players") @POST @Produces(MediaType.APPLICATION_JSON) public URIAddressedEntity createPlayer(@PathParam("instanceId") String instanceURI, @FormParam("characterURI") String characterURI, @FormParam("cameraState") CameraState cameraState, @FormParam("animationClassToStart") EmoteState.EmoteType animationClassToStart) { Instances instances = Instances.getInstance(); IDAddressedEntity ae = instances.createPlayer(instanceURI, characterURI, cameraState, animationClassToStart); return new URIAddressedEntity((INSTANCES_URI + "/" + instanceURI + "/players/" + ae.getId()), ae.getBody()); } // @PUT // @Produces(MediaType.APPLICATION_JSON) // public Instance updateInstance(@FormParam("state") Instance.State state) { // return Instances.getInstance().updateInstance(name, state, stageID); // } // @Path("/{instanceId}") // @PUT // @Produces(MediaType.APPLICATION_JSON) // public Instance updateInstance(@FormParam("state") State state) { // return Instances.getInstance(name, state, stageID) // } @Path("/players/{playerId}") @PUT @Produces(MediaType.APPLICATION_JSON) public Player updatePlayer(@PathParam("instanceId") String instanceId, @FormParam("playerId") String playerId, @FormParam("characterURI") String characterURI, @FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle, @FormParam("cameraState") CameraState cameraState, @FormParam("animationClassToStart") EmoteState.EmoteType animationClassToStart) { return Instances.getInstance().updatePlayer(playerId, characterURI, position, angle, cameraState, animationClassToStart); } @Path("/characters/{characterId}") @PUT @Produces(MediaType.APPLICATION_JSON) public Character updateCharacter(@FormParam("characterId") String characterId, @FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle, @FormParam("modelID") String modelID) { Character character = updateCharacter(characterId, position, angle, modelID); if (character != null) { throw new WebApplicationException(200); } else { throw new WebApplicationException(400); } } // @DELETE // @Path("/destroyInstance") // @Produces(MediaType.APPLICATION_JSON) // public Instance destroy(@QueryParam("instanceId") String instanceId) { // return Instances.getInstance().destroyInstance(instanceId); // } // // @Path("/{instanceId}") // @PUT // public Instance putInstance(@PathParam("InstanceId") String instanceId, // @FormParam("areaId") String areaId, // @FormParam("objectId") String objectId, @FormParam("characterId") String // characterId) { // // } }