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