package com.sagacloud.filters;

import com.google.common.base.Strings;
import com.sagacloud.utils.accessLog.AccessLogRunnable;

import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.sagacloud.endpointService.pojo.*;

@Provider
@Priority(5)
public class AccessLogFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
        try {
            String path = containerRequestContext.getUriInfo().getPath();
            Map paraMap = containerRequestContext.getUriInfo().getQueryParameters();
            String proId = Strings.nullToEmpty(((List< String >)paraMap.get("projectId"))==null? null: ((List< String >)paraMap.get("projectId")).get(0));
            //String secret = Strings.nullToEmpty(((List< String >)paraMap.get("secret"))==null? null: ((List< String >)paraMap.get("secret")).get(0));
            AccessLog log = new AccessLog();
            log.setPath(path);
            log.setProject(proId.length() < 2 ? proId : proId.substring(2));
            AccessLogRunnable.accessLogQueue.offer(log);
        }catch (Exception ex){

        }
    }
}
