package com.ctrip.xpipe.redis.metaserver.rest.resource;

import javax.inject.Singleton;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import com.ctrip.xpipe.redis.keeper.entity.Keeper;
import com.ctrip.xpipe.redis.metaserver.MetaServer;

@Path("/api/v1")
@Singleton
@Produces(MediaType.APPLICATION_JSON)
public class MetaResource extends BaseRecource {

	@Path("/{clusterId}/{shardId}")
	@GET
	public Response getActiveKeeper(@PathParam("clusterId") String clusterId, @PathParam("shardId") String shardId,
	      @QueryParam("version") @DefaultValue("0") long version,
	      @QueryParam("format") @DefaultValue("json") String format) {
		MetaServer metaServer = getSpringContext().getBean(MetaServer.class);
		Keeper keeper = metaServer.getActiveKeeper(clusterId, shardId);

		if ("plain".equals(format)) {
			String plainRes = String.format("%s:%s", keeper.getIp(), keeper.getPort());
			return Response.status(Status.OK).entity(plainRes).build();
		} else {
			return Response.status(Status.OK).entity(keeper).build();
		}
	}
}

@@ -138,16 +138,41 @@
 rs.GET;%0A
+import javax.ws.rs.POST;%0A
 import j
@@ -188,16 +188,16 @@
 s.Path;%0A
-
 import j
@@ -389,16 +389,74 @@
 tatus;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import c
@@ -494,24 +494,74 @@
 ity.Keeper;%0A
+import com.ctrip.xpipe.redis.keeper.entity.Redis;%0A
 import com.c
@@ -717,16 +717,230 @@
 urce %7B%0A%0A
+%09private Logger log = LoggerFactory.getLogger(MetaResource.class);%0A%0A%09private Redis master = new Redis();%0A%0A%09public MetaResource() %7B%0A%09%09master.setMaster(true);%0A%09%09master.setIp(%22127.0.0.1%22);%0A%09%09master.setPort(6379);%0A%09%7D%0A%0A
 %09@Path(%22
@@ -961,16 +961,30 @@
 shardId%7D
+/keeper/master
 %22)%0A%09@GET
@@ -1617,13 +1617,1195 @@
 ();%0A
-
 %09%09%7D%0A%09%7D
+%0A%0A%09@Path(%22/%7BclusterId%7D/%7BshardId%7D/redis/master%22)%0A%09@GET%0A%09public Response getRedisMaster(@PathParam(%22clusterId%22) String clusterId, @PathParam(%22shardId%22) String shardId,%0A%09      @QueryParam(%22version%22) @DefaultValue(%220%22) long version,%0A%09      @QueryParam(%22format%22) @DefaultValue(%22json%22) String format) %7B%0A%0A%09%09if (%22plain%22.equals(format)) %7B%0A%09%09%09String plainRes = String.format(%22%25s:%25s%22, master.getIp(), master.getPort());%0A%09%09%09return Response.status(Status.OK).entity(plainRes).build();%0A%09%09%7D else %7B%0A%09%09%09return Response.status(Status.OK).entity(master).build();%0A%09%09%7D%0A%09%7D%0A%0A%09@Path(%22/%7BclusterId%7D/%7BshardId%7D/redis/master%22)%0A%09@POST%0A%09public Response setRedisMaster(@PathParam(%22clusterId%22) String clusterId, @PathParam(%22shardId%22) String shardId,%0A%09      @QueryParam(%22masterIpPort%22) String masterIpPort) %7B%0A%0A%09%09// TODO%0A%09%09try %7B%0A%09%09%09String%5B%5D parts = masterIpPort.split(%22:%22);%0A%09%09%09if (parts.length == 2) %7B%0A%09%09%09%09master.setIp(parts%5B0%5D);%0A%09%09%09%09master.setPort(Integer.parseInt(parts%5B1%5D));%0A%09%09%09%09log.info(%22change redis master to %7B%7D:%7B%7D%22, master.getIp(), master.getPort());%0A%09%09%09%09return Response.status(Status.OK).entity(%22ok%22).build();%0A%09%09%09%7D%0A%09%09%7D catch (Exception e) %7B%0A%09%09%7D%0A%09%09return Response.status(Status.OK).entity(%22error%22).build();%0A%0A%09%7D
 %0A%7D%0A

support get/set redis master