package cactusServer.resources; import java.util.HashMap; 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.ntlab.radishforandroidstudio.framework.model3D.Position3D; import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D; import org.ntlab.radishforandroidstudio.framework.physics.AngularVelocity3D; import org.ntlab.radishforandroidstudio.framework.physics.Velocity3D; import cactusServer.entities.Instance; import cactusServer.entities.MovableObject; import cactusServer.entities.MovableObject.Attribute; import cactusServer.models.Instances; import net.arnx.jsonic.JSON; @Path("/instances/{instanceId}/objects") public class ObjectsRest { @Path("/{objectId}") @GET // @Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) public String getObject(@PathParam("instanceId") String instanceId, @PathParam("objectId") String objectId) { Instance instance = Instances.getInstance().getInstance(instanceId); if (instance != null) { return JSON.encode(instance.getObject(objectId)); } return null; } @GET // @Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) public String getObjects(@PathParam("instanceId") String instanceId) { return JSON.encode(Instances.getInstance().getInstance(instanceId).getObjects()); } @POST // @Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) public String createObject(@PathParam("instanceId") String instanceId, @FormParam("position") Position3D position, @FormParam("velocity") Velocity3D velocity, @FormParam("angularVelocity") AngularVelocity3D angularVelocity, @FormParam("angle") Quaternion3D angle, @FormParam("attribute") Attribute attribute, @FormParam("model") int modelID) { Instance instance = Instances.getInstance().getInstance(instanceId); HashMap<String, MovableObject> idMap = instance.createObject(position, velocity, angularVelocity, angle, attribute, modelID); HashMap<String, MovableObject> uriMap = new HashMap<>(); for (String id : idMap.keySet()) { String uri = (InstancesRest.INSTANCES_URI + "/" + instanceId + "/objects/") + id; uriMap.put(uri, idMap.get(id)); } return JSON.encode(uriMap); } @Path("/{objectId}") @PUT // @Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) public String updateObject(@PathParam("instanceId") String instanceId, @PathParam("objectId") String objectId, @FormParam("position") Position3D position, @FormParam("velocity") Velocity3D velocity, @FormParam("angularVelocity") AngularVelocity3D angularVelocity, @FormParam("angle") Quaternion3D angle) { Instance instance = Instances.getInstance().getInstance(instanceId); if (instance != null) { MovableObject object = instance.getObject(objectId); if (object != null) { object.setAngle(angle); object.setAngularVelocity(angularVelocity); object.setPosition(position); object.setVelocity(velocity); return JSON.encode(object); } } throw new WebApplicationException(400); } }