Java tutorial
/* The MIT License (MIT) Copyright (c) 2015 QAXML Pty Ltd Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.qatickets.common; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * HTTP Utils */ public final class HttpUtils { private static Log log = LogFactory.getLog(HttpUtils.class); public static enum HttpMethod { POST, GET } public static Cookie getByName(Cookie[] cookies, String name) { Cookie myCookie = null; if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals(name)) { myCookie = cookies[i]; break; } } } return myCookie; } // TODO validate ip6 public static boolean isIPValid(String sip) { String[] parts = null; if (sip.contains(".")) { parts = sip.split("\\."); } else if (sip.contains(":")) { parts = sip.split(":"); } if (parts != null) { for (String s : parts) { final int i = Integer.parseInt(s); if (i < 0 || i > 255) { return false; } } } return true; } public static Map<String, String> getHeadersAsMap(HttpServletRequest req) { final Map<String, String> headers = new HashMap<String, String>(); final Enumeration enames = req.getHeaderNames(); while (enames.hasMoreElements()) { final String name = (String) enames.nextElement(); final String value = req.getHeader(name); headers.put(name, value); } return headers; } public static Map<String, String> extractParameters(HttpServletRequest req) { final Map<String, String> params = new HashMap<String, String>(); final Enumeration enames = req.getParameterNames(); while (enames.hasMoreElements()) { final String name = (String) enames.nextElement(); final String value = req.getParameter(name); params.put(name, value); } return params; } public static String getIP(HttpServletRequest req) { if (StringUtils.isNotBlank(req.getHeader("X-Real-IP"))) { return req.getHeader("X-Real-IP"); } return req.getRemoteAddr(); } }