Newer
Older
CactusServer / src / main / java / cactusServer / resources / CharactersRest.java
t-kazehaya on 3 Jul 2018 2 KB Restのエラー解消
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 cactusServer.entities.Character;
import cactusServer.entities.Instance;
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 HashMap<String, Character> 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);
		HashMap<String, Character> idMap = instance.createCharacter(instanceId, accountURI, name, position, angle, modelID);
		HashMap<String, Character> uriMap = new HashMap<>();
		for (String id : idMap.keySet()) {
			String uri = (InstancesRest.INSTANCES_URI + "/" + instanceId + "/characters/") + id;
			uriMap.put(uri, idMap.get(id));
		}
		return uriMap;
	}
	
	@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);
		}
	}
	


}