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            @Override
063            public Resource getResource(String path) {
064                    
065                    int indexQ=path.indexOf('?');
066                    if(indexQ!=-1){
067                            int indexS=path.lastIndexOf('/');
068                            while((indexS=path.lastIndexOf('/'))>indexQ){
069                                    path=path.substring(0,indexS)+"%2F"+path.substring(indexS+1);   
070                            }
071                    }
072                    
073                    path=ResourceUtil.translatePath(ResourceUtil.removeScheme(scheme,path),false,false);
074                    
075                    return new HTTPResource(this,new HTTPConnectionData(path,getSocketTimeout()));
076            }
077    
078            public boolean isAttributesSupported() {
079                    return false;
080            }
081    
082            public boolean isCaseSensitive() {
083                    return false;
084            }
085    
086            public boolean isModeSupported() {
087                    return false;
088            }
089    
090            public void setResources(Resources resources) {
091            }
092    
093            @Override
094            public void lock(Resource res) throws IOException {
095                    lock.lock(res);
096            }
097    
098            @Override
099            public void unlock(Resource res) {
100                    lock.unlock(res);
101            }
102    
103            @Override
104            public void read(Resource res) throws IOException {
105                    lock.read(res);
106            }
107    
108            /**
109             * @return the clientTimeout
110             */
111            public int getClientTimeout() {
112                    return clientTimeout;
113            }
114    
115            /**
116             * @return the lockTimeout
117             */
118            public int getLockTimeout() {
119                    return lockTimeout;
120            }
121    
122            /**
123             * @return the socketTimeout
124             */
125            public int getSocketTimeout() {
126                    return socketTimeout;
127            }
128    
129    
130            @Override
131            public Map getArguments() {
132                    return arguments;
133            }
134    }