Servlet setStatus被忽略,而是响应代码200被发送

时间:2018-02-22 19:41:31

标签: java

我有一个servlet,它从数据库中获取常规数据,然后将输出发送到客户端点。它应该根据遇到的条件返回不同的响应代码和内容 例如:222是正确的执行,而444显示被捕获的异常

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    LOG.debug("Hit");

    BufferedReader brin = new BufferedReader(request.getReader());

    BufferedWriter brout = new BufferedWriter(response.getWriter());

    JSONObject inputJSON;

    String temp;

    StringBuilder sb = new StringBuilder("");

    int restid = 0;

    Connection conn = null;

    PreparedStatement st = null;

    ResultSet rs = null;

    try {

        if(request.getHeader("Authorization") == null) {
            response.setStatus(401);
            LOG.debug("Unauthorized entry detected");
            return;
        }

        while((temp=brin.readLine())!=null){
            sb.append(temp);
        }

        inputJSON = new JSONObject(sb.toString());

        restid=inputJSON.getInt("restid");

        if(!ValidationTester.isValidRestIdInt(restid)) {
            response.setStatus(444);
            LOG.error("Invalid input received");
            return;
        }

    } catch (Exception e) {
        response.setStatus(444);
        LOG.error("Error reading input "+e.toString());
        return;
    }finally {
        try {
            brin.close();
        }catch(Exception e) {
            //Do nothing
        }
    }

    try {

        String idtoken = request.getHeader("Authorization").split(" ")[1];

        if(restaurantRepoApp == null) {

            restaurantRepoApp = FirebaseApp.getInstance();

        }

        FirebaseToken firebaseToken = FirebaseAuth.getInstance(restaurantRepoApp).verifyIdTokenAsync(idtoken).get(10000,TimeUnit.MILLISECONDS);

        if(firebaseToken == null || firebaseToken.getUid() == null) {

            response.setStatus(444);
            LOG.error("Error occurred while verifying the Id token from auth header, no uid found, token found null? "+(firebaseToken==null));
            return;

        }else {

            LOG.debug("ID token received in header "+idtoken+" found uid from the verified token "+firebaseToken.getUid());

        }

    }catch(Exception e) {

        if(e.toString().contains("expired")) {

            response.setStatus(111);
            LOG.debug("Token expired, ask the client to refresh it");
            return;

        }

        response.setStatus(444);
        LOG.error("Error occurred while verifying the Id token from auth header "+e.toString());
        return;
    }

    try {

        long currentTimeMillis = Calendar.getInstance(TimeZone.getTimeZone("Asia/Kolkata")).getTimeInMillis();

        conn = HikariConnectionGetter.getConnection();
        //This will have a validation timeout of max 5s and connection timeout of 10s
        //Thus if this fails, let the thing fail so as to avoid timeout issues at client side

        st = conn.prepareStatement("select * from foodiniq_provalidity where restid=?");

        st.setInt(1,restid);

        rs = st.executeQuery();

        if(!rs.isBeforeFirst()) {
            response.setStatus(333);
            LOG.debug("The restaurant has no bought pro validity pack or it has been deleted after expiry");
            return;
        }

        long expiryTimeFound = 0L;

        while(rs.next()) {

            if(rs.getLong("expirytime")<=currentTimeMillis) {
                response.setStatus(333);
                LOG.debug("The restaurant has no bought pro validity pack or it has been deleted after expiry");
                return;
            }

            expiryTimeFound = rs.getLong("expirytime");

        }

        response.setStatus(222);

        JSONObject responseObject = new JSONObject();

        responseObject.put("expirytime",expiryTimeFound);

        brout.write(responseObject.toString());

        brout.flush();

    }catch (Exception e) {
        response.setStatus(444);
        LOG.error("Error occurred while entering bookmark "+e.toString());
    }finally{

        try{
            rs.close();
        }catch(Exception ee){
            //Do nothing
        }

        try{
            st.close();
        }catch(Exception ee){
            //Do nothing
        }

        try{
            conn.close();
        }catch(Exception ee){
            //Do nothing
        }

        try{
            brout.close();
        }catch(Exception ee){
            //Do nothing
        }

    }

}

此代码工作正常,直到今天,由于某种原因,tomcat似乎返回了200的响应代码,尽管操作正确,但仍未收到发送的到期时间数据。但是,我的本地计算机上收到了正确的响应和内容。有人可以帮我这个吗?

0 个答案:

没有答案