Skip to content

Commit

Permalink
✨ feat: add unify functions for Request4j #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jul 7, 2024
1 parent 9576257 commit 6055198
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions plugin/src/main/groovy/org/unify4j/common/Request4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unify4j.model.c.Protocol;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -119,6 +121,98 @@ public static Map<String, Object> getHeaders(HttpServletRequest request) {
return headers;
}

/**
* Retrieves all parameters from the HttpServletRequest as a Map.
* This method iterates over the parameter names and adds them to a Map with their corresponding values.
*
* @param request the HttpServletRequest object containing the client request
* @return a Map containing all parameter names and values from the request
*/
public static Map<String, String> getParameters(HttpServletRequest request) {
Map<String, String> params = new HashMap<>();
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String name = parameterNames.nextElement();
params.put(name, request.getParameter(name));
}
return params;
}

/**
* Retrieves the full URL from the HttpServletRequest.
* This method constructs the full URL from the request, including query parameters.
*
* @param request the HttpServletRequest object containing the client request
* @return the full URL as a String
*/
public static String getFullUrl(HttpServletRequest request) {
StringBuilder builder = new StringBuilder(request.getRequestURL().toString());
String queryString = request.getQueryString();
if (queryString != null) {
builder.append('?').append(queryString);
}
return builder.toString();
}

/**
* Retrieves the session ID from the given HttpServletRequest.
* <p>
* This method gets the current HttpSession associated with the request,
* and then extracts the session ID from it. If there is no current session
* and create is false, it returns null.
*
* @param request the HttpServletRequest from which to retrieve the session ID
* @return the session ID, or null if there is no current session
*/
public static String getSessionId(HttpServletRequest request) {
if (request == null) {
return String.valueOf(UniqueId4j.getUniqueId19());
}
HttpSession session = request.getSession(false); // Pass false to prevent creating a new session if one does not exist
return (session != null) ? session.getId() : null;
}

/**
* Retrieves the requested session ID from the HttpServletRequest.
* This method retrieves the session ID from the request, if available.
*
* @param request the HttpServletRequest object containing the client request
* @return the session ID as a String, or null if no session ID is available
*/
public static String getDirectSessionId(HttpServletRequest request) {
if (request == null) {
return String.valueOf(UniqueId4j.getUniqueId19());
}
return request.getRequestedSessionId();
}

/**
* Retrieves the base URL from the HttpServletRequest.
* This method returns the base URL of the request (protocol, server name, and port).
*
* @param request the HttpServletRequest object containing the client request
* @return the base URL of the request as a String
*/
public static String getBaseUrl(HttpServletRequest request) {
if (request == null) {
return "";
}
StringBuilder baseUrl = new StringBuilder();
Protocol HTTP = Protocol.HTTP;
Protocol HTTPS = Protocol.HTTPS;
Protocol protocol = new Protocol(
request.getScheme(),
request.getScheme().toUpperCase(),
request.getServerName(),
request.getServerPort());
baseUrl.append(protocol.getSchemeName()).append("://").append(protocol.getDescription());
if ((HTTP.getSchemeName().equalsIgnoreCase(protocol.getSchemeName()) && HTTP.getDefaultPort() != protocol.getDefaultPort()) ||
(HTTPS.getSchemeName().equalsIgnoreCase(protocol.getSchemeName()) && HTTPS.getDefaultPort() != protocol.getDefaultPort())) {
baseUrl.append(":").append(protocol.getDefaultPort());
}
return baseUrl.toString();
}

/**
* Parses query parameters from the given query string and returns them as a map.
* This function takes a query string and extracts key-value pairs representing query parameters.
Expand Down

0 comments on commit 6055198

Please sign in to comment.