com.erudika.scoold.utils.HttpUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.erudika.scoold.utils.HttpUtils.java

Source

/*
 * Copyright 2013-2019 Erudika. http://erudika.com
 *
 * 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.
 *
 * For issues and patches go to: https://github.com/erudika
 */
package com.erudika.scoold.utils;

import com.erudika.para.utils.Config;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;

/**
 * Various utilities for HTTP stuff - cookies, AJAX, etc.
 * @author Alex Bogdanovski [alex@erudika.com]
 */
public final class HttpUtils {

    /**
     * Default private constructor.
     */
    private HttpUtils() {
    }

    /**
     * Checks if a request comes from JavaScript.
     * @param request HTTP request
     * @return true if AJAX
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        return "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))
                || "XMLHttpRequest".equalsIgnoreCase(request.getParameter("X-Requested-With"));
    }

    /////////////////////////////////////////////
    //          COOKIE & STATE UTILS
    /////////////////////////////////////////////

    /**
     * Sets a cookie.
     * @param name the name
     * @param value the value
     * @param req HTTP request
     * @param res HTTP response
     */
    public static void setStateParam(String name, String value, HttpServletRequest req, HttpServletResponse res) {
        setStateParam(name, value, req, res, false);
    }

    /**
     * Sets a cookie.
     * @param name the name
     * @param value the value
     * @param req HTTP request
     * @param res HTTP response
     * @param httpOnly HTTP only flag
     */
    public static void setStateParam(String name, String value, HttpServletRequest req, HttpServletResponse res,
            boolean httpOnly) {
        setRawCookie(name, value, req, res, httpOnly, -1);
    }

    /**
     * Reads a cookie.
     * @param name the name
     * @param req HTTP request
     * @return the cookie value
     */
    public static String getStateParam(String name, HttpServletRequest req) {
        return getCookieValue(req, name);
    }

    /**
     * Deletes a cookie.
     * @param name the name
     * @param req HTTP request
     * @param res HTTP response
     */
    public static void removeStateParam(String name, HttpServletRequest req, HttpServletResponse res) {
        setRawCookie(name, "", req, res, false, 0);
    }

    /**
     * Sets a cookie.
     * @param name the name
     * @param value the value
     * @param req HTTP request
     * @param res HTTP response
     * @param httpOnly HTTP only flag
     * @param maxAge max age
     */
    public static void setRawCookie(String name, String value, HttpServletRequest req, HttpServletResponse res,
            boolean httpOnly, int maxAge) {
        if (StringUtils.isBlank(name) || value == null || req == null || res == null) {
            return;
        }
        Cookie cookie = new Cookie(name, value);
        cookie.setHttpOnly(httpOnly);
        cookie.setMaxAge(maxAge < 0 ? Config.SESSION_TIMEOUT_SEC : maxAge);
        cookie.setPath("/");
        cookie.setSecure(req.isSecure());
        res.addCookie(cookie);
    }

    /**
     * Reads a cookie.
     * @param name the name
     * @param req HTTP request
     * @return the cookie value
     */
    public static String getCookieValue(HttpServletRequest req, String name) {
        if (StringUtils.isBlank(name) || req == null) {
            return null;
        }
        Cookie[] cookies = req.getCookies();
        if (cookies == null) {
            return null;
        }
        //Otherwise, we have to do a linear scan for the cookie.
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(name)) {
                return cookie.getValue();
            }
        }
        return null;
    }
}