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

import java.util.HashMap;
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.Instance;
import cactusServer.entities.Instance.State;
import cactusServer.entities.Plain;
import cactusServer.entities.Player;
import cactusServer.models.Instances;
import net.arnx.jsonic.JSON;

@Path("/instances")
@RequestScoped
public class InstancesRest {
	public static final String INSTANCES_URI = "/CactusServer/rest/instances";

	public InstancesRest() {

	}
	
	@Path("/{instanceId}")
	@GET
	//@Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String getInstance(@PathParam("instanceId") String instanceId) {
		return JSON.encode(Instances.getInstance().getInstance(instanceId));
	}
	
	@Path("{instanceId}/areas")
	@GET
	//@Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String getArea(@PathParam("areaId") String areaId) {
		HashMap<String, Area> areaMap = new HashMap<>();
		return JSON.encode(areaMap);
	}

	@Path("/{instanceId}/areas/{areaId}")
	@GET
	//@Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String getArea(@PathParam("instanceId") String instanceId, @PathParam("areaId") String areaId) {
		Instance instance = Instances.getInstance().getInstance(instanceId);
		if (instance != null) {
			return JSON.encode(instance.getArea(areaId));
		}
		return null;
	}
	
	
	
	@GET
	//@Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String getInstances(){
		return JSON.encode(Instances.getInstance().getInstances());
	}
	
	@POST
	//@Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String createInstance(@FormParam("name") String name, @FormParam("stageID") int stageID) {
		HashMap<String, Instance> idMap = Instances.getInstance().createInstance(name, stageID);
		HashMap<String, Instance> uriMap = new HashMap<>();
		for (String id : idMap.keySet()) {
			String uri = (INSTANCES_URI + "/") + id;
			uriMap.put(uri, idMap.get(id));
		}
		return JSON.encode(uriMap);
	}

	@Path("/{instanceId}/areas")
	@POST
	//@Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public  String createArea(@PathParam("instanceId") String instanceId, @FormParam("name") String name,
			@FormParam("region") Plain[] region, @FormParam("permissions") HashSet<Allowed> permissions) {
		Instance instance = Instances.getInstance().getInstance(instanceId);
		HashMap<String, Area> idMap = instance.createArea(name, region, permissions);
		HashMap<String, Area> uriMap = new HashMap<>();
		for (String id : idMap.keySet()) {
			String uri = (INSTANCES_URI + "/" + instanceId + "/areas/") + id;
			uriMap.put(uri, idMap.get(id));
		}
		return JSON.encode(uriMap);
	}
	
	@Path("/{instanceId}")
	@PUT
	//@Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String updateInstance(@PathParam("instanceId") String instanceId, @FormParam("state") Instance.State state) {
		Instance instance = Instances.getInstance().getInstance(instanceId);
		return JSON.encode(instance.update(state));
	}
	
	
	
	
//	 @Path("/test")
//	 @GET
//	 @Produces(MediaType.TEXT_PLAIN)
//	 public String hoge() {
//	 return "deploy jenkins from tomcat";
//	 }
	
	// @DELETE
	// @Path("/destroyInstance")
	// @Produces(MediaType.APPLICATION_JSON)
	// public Instance destroy(@QueryParam("instanceId") String instanceId) {
	// return Instances.getInstance().destroyInstance(instanceId);
	// }


}