All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.kut3.http.RestAPI Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2019 Kut3Net.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kut3.http;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.kut3.http.servlet.BaseServlet;
import net.kut3.util.StringUtil;

/**
 *
 */
public abstract class RestAPI extends BaseServlet {

    /**
     *
     */
    public static final String REQUESTER = "requester";

    private static final String MODULE = "module";
    private static final String HTTP_METHOD = "httpMethod";
    private static final String OPERATION = "op";
    private static final String REQUEST_ID = "requestId";

    /**
     *
     * @param apiCtx The current API context object
     * @return true or false
     * @throws java.io.IOException Write response error
     */
    protected boolean logReq(APIContext apiCtx) throws IOException {
        apiCtx.log(MODULE, System.getProperty("application.name"))
                .log(HTTP_METHOD, apiCtx.httpMethod())
                .log(OPERATION, apiCtx.reqPath());

        String reqId = apiCtx.header(HttpHeader.X_REQUEST_ID);
        if (StringUtil.isNullOrEmpty(reqId)) {
            apiCtx.badRequest(HttpHeader.X_REQUEST_ID + " nullOrEmpty");
            return false;
        }

        apiCtx.log(REQUEST_ID, reqId);
        return true;
    }

    @Override
    protected final void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        APIContext apiCtx = new APIContext(req, resp);
        try {
            if (this.logReq(apiCtx)) {
                this.doGet(apiCtx);
            }
        } finally {
            apiCtx.log();
        }
    }

    /**
     *
     * @param apiCtx A API context
     * @throws java.io.IOException Write response error
     */
    protected void doGet(APIContext apiCtx) throws IOException {
        apiCtx.methodNotAllowed();
    }

    @Override
    protected final void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        APIContext apiCtx = new APIContext(req, resp);
        try {
            if (this.logReq(apiCtx)) {
                this.doPost(apiCtx);
            }
        } finally {
            apiCtx.log();
        }
    }

    /**
     *
     * @param apiCtx A API context
     * @throws java.io.IOException Write response error
     */
    protected void doPost(APIContext apiCtx) throws IOException {
        apiCtx.methodNotAllowed();
    }

    @Override
    protected final void doPut(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        APIContext apiCtx = new APIContext(req, resp);
        try {
            if (this.logReq(apiCtx)) {
                this.doPut(apiCtx);
            }
        } finally {
            apiCtx.log();
        }
    }

    /**
     *
     * @param apiCtx A API context
     * @throws java.io.IOException Write response error
     */
    protected void doPut(APIContext apiCtx) throws IOException {
        apiCtx.methodNotAllowed();
    }

    @Override
    protected final void doDelete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        APIContext apiCtx = new APIContext(req, resp);
        try {
            if (this.logReq(apiCtx)) {
                this.doDelete(apiCtx);
            }
        } finally {
            apiCtx.log();
        }
    }

    /**
     *
     * @param apiCtx A API context
     * @throws java.io.IOException Write response error
     */
    protected void doDelete(APIContext apiCtx) throws IOException {
        apiCtx.methodNotAllowed();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy