72a3ae9e2d5074ed36c803a865f397abc338cdaa.svn-base 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.chinacreator.videoalliance.net.util;
  2. import javax.servlet.http.HttpServletRequest;
  3. import org.apache.commons.lang.math.NumberUtils;
  4. import org.apache.log4j.Logger;
  5. public class WebUtil {
  6. private static final String PORT = ":port=";
  7. private static final String SCHEME = ":scheme=";
  8. private static Logger logger = Logger.getLogger("WebUtil.class");
  9. public static String getBasePath(HttpServletRequest request) {
  10. String path = request.getContextPath();
  11. String useragent = request.getHeader("user-agent");
  12. int port = -1;
  13. String scheme = null;
  14. if(useragent != null) {
  15. int index = useragent.indexOf(PORT);
  16. if(index != -1) {
  17. port = NumberUtils.toInt(useragent.substring(index + PORT.length()), -1);
  18. }
  19. int index1 = useragent.indexOf(SCHEME);
  20. if(index1 != -1) {
  21. scheme = useragent.substring(index1 + SCHEME.length(),index);
  22. }
  23. }
  24. if (port == -1) {
  25. port = request.getServerPort();
  26. }
  27. if(scheme == null){
  28. scheme = request.getScheme();
  29. }
  30. System.out.println(request.getHeader("User-Agent"));
  31. if(request.getServerName().equals("ua1.v.wo.cn")){
  32. port = 443;
  33. }
  34. String proxy_port = request.getHeader("X-Proxy-Port");
  35. String proxy_host = request.getHeader("X-Proxy-Host");
  36. String proxy_scheme = request.getHeader("X-Proxy-Scheme");
  37. logger.info("X-Proxy-Port ===> " + proxy_port + ", X-Proxy-Host ==> " + proxy_host + ", X-Proxy-Scheme ==>" + proxy_scheme);
  38. if(proxy_port != null){
  39. port = NumberUtils.toInt(proxy_port, -1);
  40. }
  41. if(proxy_scheme != null){
  42. scheme = proxy_scheme;
  43. }
  44. if(port == 80) {
  45. return scheme+"://" + request.getServerName() + path;
  46. }
  47. return scheme+"://" + request.getServerName() + ":" + port + path;
  48. }
  49. public static void main(String[] args) {
  50. String str = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_2 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14A456 Safari/602.1:scheme=https://ua1.v.wo.cn:443:port=86";
  51. int index1 = str.indexOf(SCHEME);
  52. int index = str.indexOf(PORT);
  53. String scheme = str.substring(index1 + SCHEME.length(),index);
  54. System.out.println(scheme);
  55. }
  56. }