I have created a utility class for getting information from the web environment. To simplify, all the methods are static, so no class creation es necessary.
We can access to the browser, request, response, address, session. Here is the code, and enjoy it
In the comments of the code, you can see where I have got some of the ideas so that you can acquire more information.
I have got a package called openadmin.utils for storing all utility classes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | package openadmin.utils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import com.vaadin.flow.server.VaadinRequest; import com.vaadin.flow.server.VaadinResponse; import com.vaadin.flow.server.VaadinService; import com.vaadin.flow.server.VaadinSession; import com.vaadin.flow.server.WebBrowser; import com.vaadin.flow.server.WrappedSession; public class VaadinUtils { public static WebBrowser getWebBrowser() { return new WebBrowser(); } public static VaadinRequest getRequest () { return VaadinService.getCurrentRequest(); } public static VaadinResponse getResponse() { return VaadinService.getCurrentResponse(); } public static String getClientAddress() { return getWebBrowser().getAddress(); } /** * Gets the remote address from a HttpServletRequest object. It prefers the * `X-Forwarded-For` header, as this is the recommended way to do it (user * may be behind one or more proxies). * * Taken from https://stackoverflow.com/a/38468051/778272 * * @param request - the request object where to get the remote address from * @return a string corresponding to the IP address of the remote machine */ public static String getClientAddress(HttpServletRequest request) { if (request==null) return null; String ipAddress = request.getHeader("X-FORWARDED-FOR"); if (ipAddress != null) { // cares only about the first IP if there is a list ipAddress = ipAddress.replaceFirst(",.*", ""); } else { ipAddress = request.getRemoteAddr(); } return ipAddress; } /* public static String getClientAddress() { return getClientAddress(getRequest()); } */ /** * Get the session object * @param request * @return */ public static HttpSession getSession(HttpServletRequest request) { if (request==null) return null; return request.getSession(); } public static WrappedSession getWrappedSession() { return getRequest().getWrappedSession(); } public static String getSessionId() { return getWrappedSession().getId(); } public static String getUser() { return getRequest().getRemoteUser(); } public static String getUserAgent(HttpServletRequest request) { if (request==null) return null; return request.getHeader("User-Agent"); } public static String getUserAgent() { return ((HttpServletRequest)getRequest()).getHeader("User-Agent"); } public static void setSessionAttribute(String attributeName, Object obj) { getWrappedSession().setAttribute(attributeName, obj); } public static Object getSessionAttribute(String attributeName) { return getWrappedSession().getAttribute(attributeName); } public static VaadinSession getVaadinSession() { return VaadinSession.getCurrent(); } public static void invalidateSession() { getWrappedSession().invalidate(); } public static void main(String[] args) { // TODO Auto-generated method stub } } |
No hay comentarios:
Publicar un comentario