001    package railo.commons.io.res.type.http;
002    
003    import java.io.IOException;
004    import java.util.Map;
005    
006    import railo.commons.io.res.Resource;
007    import railo.commons.io.res.ResourceProvider;
008    import railo.commons.io.res.Resources;
009    import railo.commons.io.res.util.ResourceLockImpl;
010    import railo.commons.io.res.util.ResourceUtil;
011    import railo.commons.lang.StringUtil;
012    import railo.runtime.op.Caster;
013    
014    public class HTTPResourceProvider implements ResourceProvider {
015    
016    
017            private int lockTimeout=20000;
018            private final ResourceLockImpl lock=new ResourceLockImpl(lockTimeout,false);
019            private String scheme="http";
020            private int clientTimeout=30000;
021            private int socketTimeout=20000;
022            private Map arguments;
023    
024            public String getScheme() {
025                    return scheme;
026            }
027    
028            public String getProtocol() {
029                    return scheme;
030            }
031    
032            public void setScheme(String scheme) {
033                    if(!StringUtil.isEmpty(scheme))this.scheme=scheme;
034            }
035    
036            public ResourceProvider init(String scheme, Map arguments) {
037                    setScheme(scheme);
038                    
039                    if(arguments!=null) {
040                            this.arguments=arguments;
041                            // client-timeout
042                            String strTimeout=(String) arguments.get("client-timeout");
043                            if(strTimeout!=null) {
044                                    clientTimeout = Caster.toIntValue(strTimeout,clientTimeout);
045                            }
046                            // socket-timeout
047                            strTimeout=(String) arguments.get("socket-timeout");
048                            if(strTimeout!=null) {
049                                    socketTimeout=Caster.toIntValue(strTimeout,socketTimeout);
050                            }
051                            // lock-timeout
052                            strTimeout = (String) arguments.get("lock-timeout");
053                            if(strTimeout!=null) {
054                                    lockTimeout=Caster.toIntValue(strTimeout,lockTimeout);
055                            }
056                    }
057                    lock.setLockTimeout(lockTimeout);
058                    return this;
059            }
060            
061    
062            /**
063             * @see railo.commons.io.res.ResourceProvider#getResource(java.lang.String)
064             */
065            public Resource getResource(String path) {
066                    
067                    int indexQ=path.indexOf('?');
068                    if(indexQ!=-1){
069                            int indexS=path.lastIndexOf('/');
070                            while((indexS=path.lastIndexOf('/'))>indexQ){
071                                    path=path.substring(0,indexS)+"%2F"+path.substring(indexS+1);   
072                            }
073                    }
074                    
075                    path=ResourceUtil.translatePath(ResourceUtil.removeScheme(scheme,path),false,false);
076                    
077                    return new HTTPResource(this,new HTTPConnectionData(path,getSocketTimeout()));
078            }
079    
080            public boolean isAttributesSupported() {
081                    return false;
082            }
083    
084            public boolean isCaseSensitive() {
085                    return false;
086            }
087    
088            public boolean isModeSupported() {
089                    return false;
090            }
091    
092            public void setResources(Resources resources) {
093            }
094    
095            /**
096             * @throws IOException 
097             * @see railo.commons.io.res.ResourceProvider#lock(railo.commons.io.res.Resource)
098             */
099            public void lock(Resource res) throws IOException {
100                    lock.lock(res);
101            }
102    
103            /**
104             * @see railo.commons.io.res.ResourceProvider#unlock(railo.commons.io.res.Resource)
105             */
106            public void unlock(Resource res) {
107                    lock.unlock(res);
108            }
109    
110            /**
111             * @throws IOException 
112             * @see railo.commons.io.res.ResourceProvider#read(railo.commons.io.res.Resource)
113             */
114            public void read(Resource res) throws IOException {
115                    lock.read(res);
116            }
117    
118            /**
119             * @return the clientTimeout
120             */
121            public int getClientTimeout() {
122                    return clientTimeout;
123            }
124    
125            /**
126             * @return the lockTimeout
127             */
128            public int getLockTimeout() {
129                    return lockTimeout;
130            }
131    
132            /**
133             * @return the socketTimeout
134             */
135            public int getSocketTimeout() {
136                    return socketTimeout;
137            }
138    
139    
140            /**
141             * @see railo.commons.io.res.ResourceProvider#getArguments()
142             */
143            public Map getArguments() {
144                    return arguments;
145            }
146    }