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

z11.rs.auth.AuthUtil Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version

package z11.rs.auth;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 *
 * @author vietduc
 */
public class AuthUtil {
    public static String getAuthorization(HttpServletRequest request, HttpServletResponse response) {
        String token = "";
        HttpSession session = request.getSession(false);
        if (session == null) {
            token = request.getHeader(HttpHeaders.AUTHORIZATION);
            if (token == null || "".equals(token)) {
                session = request.getSession();
                token = session.getId();
                response.addHeader(HttpHeaders.AUTHORIZATION, token);
            } else {
                response.addHeader(HttpHeaders.SET_COOKIE, "JSESSIONID=" + token + "; Path=/; HttpOnly");
            }
        } else {
            token = session.getId();
            response.addHeader(HttpHeaders.AUTHORIZATION, token);
        }
        return token;
    }
    
    public static String newAuthorization(HttpServletRequest request, HttpServletResponse response) {
        String token = request.getSession().getId();
        response.addHeader(HttpHeaders.AUTHORIZATION, token);
        return token;
    }
    
    public static String checkAuthorization(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            return session.getId();
        }
        String token = request.getHeader(HttpHeaders.AUTHORIZATION);
        if (token != null || !"".equals(token)) {
            return token;
        }
        return null;
    }
    
    public static Response makeTextResponse(Response.Status code, String content) {
        return Response.status(code).entity(content).type(MediaType.TEXT_PLAIN).build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy