<commit_before>package app.hongs.normal.serv;

import app.hongs.Cnst;
import app.hongs.action.ActionRunner;
import app.hongs.action.serv.ApisAction;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * REST 接口适配
 * 将 /api/abc/def 转换为 /abc/def.api 的形式
 * 同 API 的规则 , 未来有  URL rewrite 的功能
 * @author Hongs
 */
public class RestAction extends ApisAction {

    private static final String   METHOD_GET    = "GET";
    private static final String   METHOD_PUT    = "PUT";
    private static final String   METHOD_POST   = "POST";
    private static final String   METHOD_PATCH  = "PATCH";
    private static final String   METHOD_DELETE = "DELETE";

    private static final String[] ACTION_GET    = new String[] {"search", "list", "info"};
    private static final String[] ACTION_GOT    = new String[] {"search", "info", "list"};
    private static final String[] ACTION_POST   = new String[] {"create", "save"};
    private static final String[] ACTION_PATCH  = new String[] {"update", "save"};
    private static final String[] ACTION_DELETE = new String[] {"delete"};

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse rsp)
            throws ServletException, IOException {
        switch(req.getMethod()) {
            case METHOD_GET   :
                reset(req, ACTION_GET   );
                super.service( req, rsp );
                break;
            case METHOD_POST  :
                req.setAttribute(Cnst.UPDATE_MODE, false);
                reset(req, ACTION_POST  );
                super.service( req, rsp );
                break;
            case METHOD_PUT   :
                req.setAttribute(Cnst.UPDATE_MODE, false);
                reset(req, ACTION_PATCH );
                super.service( req, rsp );
                break;
            case METHOD_PATCH :
                req.setAttribute(Cnst.UPDATE_MODE, true );
                reset(req, ACTION_PATCH );
                super.service( req, rsp );
                break;
            case METHOD_DELETE:
                reset(req, ACTION_DELETE);
                super.service( req, rsp );
                break;
            default:
                rsp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED,
                req.getMethod() + " is not allowed for this resource!");
        }
    }

    private static  void  reset(HttpServletRequest req, String... mts) {
        /**
         * RestAction 中 ServletPath 为 /api/, PathInfo 为 /abc/def
         * ApisAction 中 ServletPath 为 /abc/def.api, 并无 PathInfo
         * 故需将前者转换为后者的形式, 然后交由 ApisAction 继续处理
         */

        String uri = parse (req, mts);

        req.setAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH , uri);<commit_msg>增加自定义的 ADD 方面<commit_after>package app.hongs.normal.serv;

import app.hongs.Cnst;
import app.hongs.action.ActionRunner;
import app.hongs.action.serv.ApisAction;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * REST 接口适配
 * 将 /api/abc/def 转换为 /abc/def.api 的形式
 * 同 API 的规则 , 未来有  URL rewrite 的功能
 * @author Hongs
 */
public class RestAction extends ApisAction {

    private static final String   METHOD_GET    = "GET";
    private static final String   METHOD_ADD    = "ADD";
    private static final String   METHOD_PUT    = "PUT";
    private static final String   METHOD_POST   = "POST";
    private static final String   METHOD_PATCH  = "PATCH";
    private static final String   METHOD_DELETE = "DELETE";

    private static final String[] ACTION_GET    = new String[] {"search", "list", "info"};
    private static final String[] ACTION_GOT    = new String[] {"search", "info", "list"};
    private static final String[] ACTION_POST   = new String[] {"create", "save"};
    private static final String[] ACTION_PATCH  = new String[] {"update", "save"};
    private static final String[] ACTION_DELETE = new String[] {"delete"};

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse rsp)
            throws ServletException, IOException {
        switch(req.getMethod()) {
            case METHOD_GET   :
                reset(req, ACTION_GET   );
                super.service( req, rsp );
                break;
            case METHOD_ADD   :
            case METHOD_POST  :
                req.setAttribute(Cnst.UPDATE_MODE, false);
                reset(req, ACTION_POST  );
                super.service( req, rsp );
                break;
            case METHOD_PUT   :
            case METHOD_PATCH :
                req.setAttribute(Cnst.UPDATE_MODE, true );
                reset(req, ACTION_PATCH );
                super.service( req, rsp );
                break;
            case METHOD_DELETE:
                reset(req, ACTION_DELETE);
                super.service( req, rsp );
                break;
            default:
                rsp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED,
                req.getMethod() + " is not allowed for this resource!");
        }
    }

    private static  void  reset(HttpServletRequest req, String... mts) {
        /**
         * RestAction 中 ServletPath 为 /api/, PathInfo 为 /abc/def
         * ApisAction 中 ServletPath 为 /abc/def.api, 并无 PathInfo
         * 故需将前者转换为后者的形式, 然后交由 ApisAction 继续处理
         */

        String uri = parse (req, mts);

        req.setAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH , uri);
{'fraction_non_alphanumeric': 0.0973358871669859, 'fraction_numerical': 0.0, 'mean_word_length': 2.2071468453378, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6613399', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1673, 'n_words': 437}