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

com.jk.services.server.JKAbstractRestController Maven / Gradle / Ivy

Go to download

A set wrappers, filters, and API's that enables faster development in microservices in Java

The newest version!
/*
 * Copyright 2002-2023 Dr. Jalal Kiswani. 
 * Email: [email protected]
 * Check out https://j-framework.com for more details
 * 
 * All the opensource projects of Dr. Jalal Kiswani are free for personal and academic use only, 
 * for commercial usage and support, please contact the author.
 *
 * 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 com.jk.services.server;

import com.jk.core.config.JKConfig;
import com.jk.core.config.JKConstants;
import com.jk.core.context.JKContext;
import com.jk.core.context.JKContextFactory;
import com.jk.core.exceptions.JKSecurityException;
import com.jk.core.http.JKHttpStatus;
import com.jk.core.logging.JKLogger;
import com.jk.core.logging.JKLoggerFactory;
import com.jk.core.util.JK;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.NotAuthorizedException;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;

/**
 * This class is a base class for REST controllers that provides common
 * functionality and utility methods.
 * 

* This class includes methods for handling HTTP requests, accessing request * headers, and returning HTTP responses with appropriate status codes. * * @author Dr. Jalal H. Kiswani * @version 1.0 */ @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public class JKAbstractRestController { /** * Represents the class logger. */ protected JKLogger logger = JKLoggerFactory.getLogger(getClass()); /** * This method retrieves the remote IP address of the client making the request. * * @return the remote IP address as a string. */ public String getRemoteIp() { return JKServiceUtil.getRemoteIp(); } /** * This method retrieves the remote user associated with the request. * * @return the remote user. */ public String getRemoteUser() { return JKServiceUtil.getRemoteUser(); } /** * This method retrieves the remote role associated with the request. * * @return the remote role. */ public String getRemoteRole() { return JKServiceUtil.getRemoteRole(); } /** * This method generates an HTTP response with a status code and includes the * provided model as the response entity. * * @param model Specifies the representation entity data. * @return the response with a status code and the provided model as the entity. */ protected Response ok(Object model) { return Response.ok(model).build(); } /** * This method generates an HTTP response with an accepted status code and * includes the provided model as the response entity. * * @param model Specifies the representation entity data. * @return the response with an accepted status code and the provided model as * the entity. */ protected Response accepted(Object model) { return Response.accepted(model).build(); } /** * This method generates an HTTP response with a not found status code (HTTP 404 * Not Found) and includes the provided entity as the response entity. * * @param entity Specifies the representation entity data. * @return the response with a status code and the provided entity. */ protected Response notFound(Object entity) { return status(Status.NOT_FOUND, entity); } /** * This method generates an HTTP response with a bad request status code (HTTP * 400 Bad Request) and includes the provided entity as the response entity. * * @param entity Specifies the representation entity data. * @return the response with a status code and the provided entity. */ protected Response badRequest(Object entity) { return status(Status.BAD_REQUEST, entity); } /** * This method generates an HTTP response with an unauthorized status code (HTTP * 401 Unauthorized) and includes the provided entity as the response entity. * * @param entity Specifies the representation entity data. * @return the response with a status code and the provided entity. */ protected Response unauthorized(Object entity) { return status(Status.UNAUTHORIZED, entity); } /** * This method generates an HTTP response with a forbidden status code (HTTP 403 * Forbidden) and includes the provided entity as the response entity. * * @param entity Specifies the representation entity data. * @return the response with a status code and the provided entity. */ protected Response forbidden(Object entity) { return status(Status.FORBIDDEN, entity); } /** * This method generates an HTTP response with an internal server error status * code (HTTP 500 Internal Server Error) and includes the provided entity as the * response entity. * * @param entity Specifies the representation entity data. * @return the response with a status code and the provided entity. */ protected Response interalServerError(Object entity) { return status(Status.INTERNAL_SERVER_ERROR, entity); } /** * This method generates an HTTP response with the specified HTTP status code * and includes the provided entity as the response entity. * * @param status Specifies the HTTP status code to set for the response. * @param entity Specifies the representation entity data. * @return the response with the provided status code and entity. */ protected Response status(Status status, Object entity) { return Response.status(status).entity(entity).build(); } /** * This method gets the header. * * @param name Specifies the name. * @return the header. */ public String getHeader(String name) { return JKContextFactory.getCurrentContext().getHeadersMap().get(name); } /** * This method gets the tenant identifier (ID). * * @return the tenant identifier (ID). */ public String getTenantId() { return JKContextFactory.getCurrentContext().getTenantId(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy