001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.security.pacl.permission;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.Http;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.net.URL;
024    
025    /**
026     * @author Raymond Aug??
027     */
028    public class PortalSocketPermission {
029    
030            public static void checkConnect(Http.Options options) {
031                    checkConnect(options.getLocation());
032            }
033    
034            public static void checkConnect(String location) {
035                    String domainAndPort = HttpUtil.getDomain(location);
036    
037                    String[] domainAndPortArray = domainAndPort.split(StringPool.COLON);
038    
039                    String domain = domainAndPortArray[0];
040    
041                    int port = -1;
042    
043                    if (domainAndPortArray.length > 1) {
044                            port = GetterUtil.getInteger(domainAndPortArray[1]);
045                    }
046    
047                    String protocol = HttpUtil.getProtocol(location);
048    
049                    checkConnect(domain, port, protocol);
050            }
051    
052            public static void checkConnect(URL url) {
053                    if (url == null) {
054                            return;
055                    }
056    
057                    String domain = url.getHost();
058                    int port = url.getPort();
059                    String protocol = url.getProtocol();
060    
061                    checkConnect(domain, port, protocol);
062            }
063    
064            private static void checkConnect(String domain, int port, String protocol) {
065                    if (Validator.isNull(domain) ||
066                            (!protocol.startsWith(Http.HTTPS) &&
067                             !protocol.startsWith(Http.HTTP))) {
068    
069                            return;
070                    }
071    
072                    if (port == -1) {
073                            protocol = protocol.toLowerCase();
074    
075                            if (protocol.startsWith(Http.HTTPS)) {
076                                    port = Http.HTTPS_PORT;
077                            }
078                            else if (protocol.startsWith(Http.HTTP)) {
079                                    port = Http.HTTP_PORT;
080                            }
081                    }
082    
083                    String location = domain.concat(StringPool.COLON).concat(
084                            String.valueOf(port));
085    
086                    _pacl.checkPermission(location, "connect");
087            }
088    
089            private static PACL _pacl = new NoPACL();
090    
091            private static class NoPACL implements PACL {
092    
093                    @Override
094                    public void checkPermission(String host, String action) {
095                    }
096    
097            }
098    
099            public static interface PACL {
100    
101                    public void checkPermission(String host, String action);
102    
103            }
104    
105    }