Newer
Older
CactusServer / src / main / java / cactusServer / resources / CharactersRest.java
package cactusServer.resources;

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 cactusServer.entities.Character;
import cactusServer.entities.IDAddressedEntity;
import cactusServer.entities.Instance;
import cactusServer.entities.URIAddressedEntity;
import cactusServer.models.Instances;

@Path("/instances/{instanceId}/characters")
public class CharactersRest {
	
	@Path("/{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;
	}
	
	@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((InstancesRest.INSTANCES_URI + "/" + instanceId + "/characters/" + ae.getId()), ae.getBody());
	}
	
	@Path("/{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);
		}
	}
	


}