package com.tohi.common.pwdutil;


import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import java.io.IOException;

public class CheckRequestFilter  implements ContainerRequestFilter {

    private static final String TARGET_URL ="/pitask/getToken";

    public void filter(ContainerRequestContext requestContext) throws IOException {
        String url=requestContext.getUriInfo().getRequestUri().toString();
        if(!url.contains("pitask")){
            requestContext.abortWith(Response
                    .status(Response.Status.UNAUTHORIZED)
                    .entity("用户名或密码错误")
                    .build());
        }else {
            if(!url.endsWith(TARGET_URL)){
                String token = requestContext.getHeaderString("token");
                if(token!=null&&!"".equals(token)){
                    if (!TokenUtils.verify(token)) {
                        requestContext.abortWith(Response
                                .status(Response.Status.UNAUTHORIZED)
                                .entity("用户名或密码错误")
                                .build());
                    }
                }else {
                    requestContext.abortWith(Response
                            .status(Response.Status.UNAUTHORIZED)
                            .entity("用户名或密码错误")
                            .build());
                }
            }
            }
    }
}