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