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

com.starxmind.boot.utils.HttpUtils Maven / Gradle / Ivy

The newest version!
package com.starxmind.boot.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Http utils
 *
 * @author pizzalord
 * @since 1.0
 */
public abstract class HttpUtils {
    /**
     * Disable browser's caches
     */
    public static void disableBrowserCache() {
        HttpServletResponse response = getHttpServletResponse();
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
    }

    /**
     * Write json into response
     *
     * @param json
     * @throws IOException
     */
    public static void jsonResponse(String json) throws IOException {
        HttpServletResponse response = getHttpServletResponse();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.getWriter().write(json);
    }

    /**
     * Get http response
     *
     * @return
     */
    public static HttpServletResponse getHttpServletResponse() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getResponse();
    }

    /**
     * Get http request
     *
     * @return
     */
    public static HttpServletRequest getHttpServletRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getRequest();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy