001
014
015 package com.liferay.portal.kernel.security.pacl.permission;
016
017 import com.liferay.portal.kernel.util.CharPool;
018 import com.liferay.portal.kernel.util.GetterUtil;
019 import com.liferay.portal.kernel.util.Http;
020 import com.liferay.portal.kernel.util.HttpUtil;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.StringUtil;
023 import com.liferay.portal.kernel.util.Validator;
024
025 import java.net.URL;
026
027
030 public class PortalSocketPermission {
031
032 public static void checkConnect(Http.Options options) {
033 checkConnect(options.getLocation());
034 }
035
036 public static void checkConnect(String location) {
037 String domainAndPort = HttpUtil.getDomain(location);
038
039 String[] domainAndPortArray = StringUtil.split(
040 domainAndPort, CharPool.COLON);
041
042 String domain = domainAndPortArray[0];
043
044 int port = -1;
045
046 if (domainAndPortArray.length > 1) {
047 port = GetterUtil.getInteger(domainAndPortArray[1]);
048 }
049
050 String protocol = HttpUtil.getProtocol(location);
051
052 checkConnect(domain, port, protocol);
053 }
054
055 public static void checkConnect(URL url) {
056 if (url == null) {
057 return;
058 }
059
060 String domain = url.getHost();
061 int port = url.getPort();
062 String protocol = url.getProtocol();
063
064 checkConnect(domain, port, protocol);
065 }
066
067 public static interface PACL {
068
069 public void checkPermission(String host, String action);
070
071 }
072
073 private static void checkConnect(String domain, int port, String protocol) {
074 if (Validator.isNull(domain) ||
075 (!protocol.startsWith(Http.HTTPS) &&
076 !protocol.startsWith(Http.HTTP))) {
077
078 return;
079 }
080
081 if (port == -1) {
082 protocol = StringUtil.toLowerCase(protocol);
083
084 if (protocol.startsWith(Http.HTTPS)) {
085 port = Http.HTTPS_PORT;
086 }
087 else if (protocol.startsWith(Http.HTTP)) {
088 port = Http.HTTP_PORT;
089 }
090 }
091
092 String location = domain.concat(StringPool.COLON).concat(
093 String.valueOf(port));
094
095 _pacl.checkPermission(location, "connect");
096 }
097
098 private static PACL _pacl = new NoPACL();
099
100 private static class NoPACL implements PACL {
101
102 @Override
103 public void checkPermission(String host, String action) {
104 }
105
106 }
107
108 }