package com.fri.http.rest.resources;

import com.fri.http.rest.config.YXDefaultServerConfig;
import com.fri.http.rest.logging.YXLogging;
import com.fri.http.rest.server.YXHttpRestServer;
import com.fri.http.rest.response.YXJSONResponse;
import org.eclipse.jetty.server.ServerConnector;
import org.omg.CORBA.Any;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.logging.Logger;

/**
 * Created by zhangdahui on 2017/5/5.
 */

@Path("server")
public class YXServerStatus {


    private Logger logger = YXLogging.getLogger(YXDefaultServerConfig.LOGGING_NAME_REQUEST_SERVER);

    @GET
    @Path("info")
    @Produces(MediaType.APPLICATION_JSON)
    public String status(@Context HttpServletRequest req, @Context HttpServletResponse resp) throws Exception {

        resp.setCharacterEncoding("UTF-8");
        YXJSONResponse yxResp = new YXJSONResponse();
        HashMap dataMap = new HashMap<String, Any>();
        dataMap.put("ThreadInProcess", YXHttpRestServer.jettyServer.getThreadPool().getThreads());
        dataMap.put("CurrentQueueSize", ((ServerConnector)YXHttpRestServer.jettyServer.getConnectors()[0]).getAcceptQueueSize());
        logger.info(req.getRemoteHost() + ": Status Query");

        return yxResp.outPutSuccess(dataMap).toJSONString();
    }

    @GET
    @Path("error")
    @Produces(MediaType.APPLICATION_JSON)
    public String testError(@Context HttpServletRequest req, @Context HttpServletResponse resp) throws Exception {
        resp.setCharacterEncoding("UTF-8");
        logger.info(req.getRemoteHost() + ":Got Error");
        return new YXJSONResponse().outPutError("0001", "这是一个错误信息测试返回").toJSONString();
    }
}
