001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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    /**
028     * @author Raymond Aug??
029     */
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    }