001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.io.res.type.http;
020
021import java.io.IOException;
022import java.util.Map;
023
024import lucee.commons.io.res.Resource;
025import lucee.commons.io.res.ResourceProvider;
026import lucee.commons.io.res.ResourceProviderPro;
027import lucee.commons.io.res.Resources;
028import lucee.commons.io.res.util.ResourceLockImpl;
029import lucee.commons.io.res.util.ResourceUtil;
030import lucee.commons.lang.StringUtil;
031import lucee.runtime.op.Caster;
032
033public class HTTPResourceProvider implements ResourceProviderPro {
034
035
036        private int lockTimeout=20000;
037        private final ResourceLockImpl lock=new ResourceLockImpl(lockTimeout,false);
038        private String scheme="http";
039        private int clientTimeout=30000;
040        private int socketTimeout=20000;
041        private Map arguments;
042
043        public String getScheme() {
044                return scheme;
045        }
046
047        public String getProtocol() {
048                return scheme;
049        }
050
051        public void setScheme(String scheme) {
052                if(!StringUtil.isEmpty(scheme))this.scheme=scheme;
053        }
054
055        public ResourceProvider init(String scheme, Map arguments) {
056                setScheme(scheme);
057                
058                if(arguments!=null) {
059                        this.arguments=arguments;
060                        // client-timeout
061                        String strTimeout=(String) arguments.get("client-timeout");
062                        if(strTimeout!=null) {
063                                clientTimeout = Caster.toIntValue(strTimeout,clientTimeout);
064                        }
065                        // socket-timeout
066                        strTimeout=(String) arguments.get("socket-timeout");
067                        if(strTimeout!=null) {
068                                socketTimeout=Caster.toIntValue(strTimeout,socketTimeout);
069                        }
070                        // lock-timeout
071                        strTimeout = (String) arguments.get("lock-timeout");
072                        if(strTimeout!=null) {
073                                lockTimeout=Caster.toIntValue(strTimeout,lockTimeout);
074                        }
075                }
076                lock.setLockTimeout(lockTimeout);
077                return this;
078        }
079        
080
081        @Override
082        public Resource getResource(String path) {
083                
084                int indexQ=path.indexOf('?');
085                if(indexQ!=-1){
086                        int indexS=path.lastIndexOf('/');
087                        while((indexS=path.lastIndexOf('/'))>indexQ){
088                                path=path.substring(0,indexS)+"%2F"+path.substring(indexS+1);   
089                        }
090                }
091                
092                path=ResourceUtil.translatePath(ResourceUtil.removeScheme(scheme,path),false,false);
093                
094                return new HTTPResource(this,new HTTPConnectionData(path,getSocketTimeout()));
095        }
096
097        public boolean isAttributesSupported() {
098                return false;
099        }
100
101        public boolean isCaseSensitive() {
102                return false;
103        }
104
105        public boolean isModeSupported() {
106                return false;
107        }
108
109        public void setResources(Resources resources) {
110        }
111
112        @Override
113        public void lock(Resource res) throws IOException {
114                lock.lock(res);
115        }
116
117        @Override
118        public void unlock(Resource res) {
119                lock.unlock(res);
120        }
121
122        @Override
123        public void read(Resource res) throws IOException {
124                lock.read(res);
125        }
126
127        /**
128         * @return the clientTimeout
129         */
130        public int getClientTimeout() {
131                return clientTimeout;
132        }
133
134        /**
135         * @return the lockTimeout
136         */
137        public int getLockTimeout() {
138                return lockTimeout;
139        }
140
141        /**
142         * @return the socketTimeout
143         */
144        public int getSocketTimeout() {
145                return socketTimeout;
146        }
147
148
149        @Override
150        public Map getArguments() {
151                return arguments;
152        }
153        
154        @Override
155        public char getSeparator() {
156                return '/';
157        }
158}