View Javadoc

1   package fr.in2p3.jsaga.impl.url;
2   
3   import fr.in2p3.jsaga.adaptor.data.read.FileAttributes;
4   import fr.in2p3.jsaga.helpers.URLEncoder;
5   
6   import org.ogf.saga.SagaObject;
7   import org.ogf.saga.error.BadParameterException;
8   import org.ogf.saga.error.NoSuccessException;
9   import org.ogf.saga.session.Session;
10  import org.ogf.saga.url.URL;
11  
12  import java.io.IOException;
13  import java.util.regex.Pattern;
14  
15  /* ***************************************************
16  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
17  * ***             http://cc.in2p3.fr/             ***
18  * ***************************************************
19  * File:   RelativeURLImpl
20  * Author: Lionel Schwarz (lionel.schwarz@in2p3.fr)
21  * Date:   4 fév 2011
22  * ***************************************************
23  * Description:                                      */
24  /**
25   *
26   */
27  public class RelativeURLImpl extends AbstractURLImpl implements URL {
28  
29  	protected UniversalFile m_file;
30  	protected String url_query;
31  	protected String url_fragment;
32  	
33  	RelativeURLImpl(String url) throws BadParameterException {
34  		// url is considered as path only (even if contains ? and #)
35  		setPath(url);
36  	}
37  
38      /** Encode the relative path + set the cache */
39  	RelativeURLImpl(FileAttributes cache) throws BadParameterException {
40          this(cache.getRelativePath());
41          this.setCache(cache);
42      }
43  
44      public SagaObject clone() throws CloneNotSupportedException {
45      	RelativeURLImpl clone = (RelativeURLImpl) super.clone();
46          clone.m_file = m_file;
47          clone.url_query = url_query;
48          clone.url_fragment = url_fragment;
49          clone.m_cache = m_cache;
50          clone.m_cache_creation_time = m_cache_creation_time;
51          return clone;
52      }
53  
54      public String getEncodedPathOnly() {
55      	String encoded_path = URLEncoder.encodePathOnly(getPath());
56  		return encoded_path + (url_query == null?"":"?"+url_query) + (url_fragment == null?"":"#"+url_fragment);
57      	
58      }
59      
60      public URL resolve(URL url) throws NoSuccessException {
61      	// if absolute: throw exception
62      	if (url instanceof AbsoluteURLImpl) {
63      		throw new NoSuccessException("The URL cannot be resolved against this relative URL");
64      	}
65      	RelativeURLImpl rel_url = (RelativeURLImpl)url;
66      	// If url is absolute file, return url
67      	if (rel_url.m_file.isAbsolute()) {
68      		return url;
69      	}
70      	String new_url;
71      	// path or parent path
72      	new_url = m_file.isDirectory()?m_file.getPath():m_file.getParent();
73      	// add new path
74      	new_url += rel_url.m_file.getPath();
75      	// add new query or current query
76      	if (rel_url.url_query != null) {
77      		new_url += "?" + rel_url.url_query;
78          	if (rel_url.url_fragment != null) {
79          		new_url += "#" + rel_url.url_fragment;
80          	}
81      	} else {
82      		if (url_query != null) {
83      			new_url += "?" + url_query;
84      		}
85          	if (rel_url.url_fragment != null) {
86          		new_url += "#" + rel_url.url_fragment;
87          	} else if (url_fragment != null) {
88          		new_url += "#" + url_fragment;
89          	}
90      		
91      	}
92      	// return resolved URL
93      	try {
94  			return new RelativeURLImpl(new_url);
95  		} catch (BadParameterException e) {
96  			throw new NoSuccessException(e);
97  		}
98      }
99  
100 	public void setString(String url) throws BadParameterException {
101 		// url is considered as path only (even if contains ? and #)
102 		this.setPath(url);
103 		url_query = url_fragment = null;
104 	}
105 
106 	public String getString() {
107 		return getPath() + (url_query == null?"":"?"+url_query) + (url_fragment == null?"":"#"+url_fragment);
108 	}
109 
110 	public String getEscaped() {
111 		return null;
112 	}
113 
114     public String toString() {
115         return this.normalize().getString();
116     }
117 
118 	public String getFragment() {
119 		if (url_fragment == null) {
120 			throw new RuntimeException("URL not resolved yet");
121 		}
122 		return url_fragment;
123 	}
124 
125 	public void setFragment(String fragment) throws BadParameterException {
126 		url_fragment = fragment;
127 	}
128 
129 	public String getHost() {
130 		return null;
131 	}
132 
133 	public void setHost(String host) throws BadParameterException {
134       	throw new BadParameterException("Operation not supported");
135 	}
136 
137 	public String getPath() {
138 		return m_file.getPath();
139 	}
140 
141 	public void setPath(String path) throws BadParameterException {
142 		if (path != null) {
143 			if (Pattern.matches(AbsoluteURLImpl.ABSOLUTE_URL_REGEXP, path)) {
144 				throw new BadParameterException("path must be relative");
145 			}
146 		} else {
147 			path = "";
148 		}
149 		m_file = new UniversalFile(path);
150 	}
151 
152 	public int getPort() {
153 		return -1;
154 	}
155 
156 	public void setPort(int port) throws BadParameterException {
157       	throw new BadParameterException("Operation not supported");
158 	}
159 
160 	public String getQuery() {
161 		if (url_query == null) {
162 			throw new RuntimeException("URL not resolved yet");
163 		}
164 		return url_query;
165 	}
166 
167 	public void setQuery(String query) throws BadParameterException {
168 		url_query = query;
169 	}
170 
171 	public String getScheme() {
172 		return null;
173 	}
174 
175 	public void setScheme(String scheme) throws BadParameterException {
176       	throw new BadParameterException("Operation not supported");
177 	}
178 
179 	public String getUserInfo() {
180 		return null;
181 	}
182 
183 	public void setUserInfo(String userInfo) throws BadParameterException {
184       	throw new BadParameterException("Operation not supported");
185 	}
186 
187 	public URL translate(String scheme) throws BadParameterException,
188 			NoSuccessException {
189       	throw new BadParameterException("Operation not supported");
190 	}
191 
192 	public URL translate(Session session, String scheme)
193 			throws BadParameterException, NoSuccessException {
194 		return this.translate(scheme);
195 	}
196 
197 	public boolean isAbsolute() {
198 		return false;
199 	}
200 
201 	public URL normalize() {
202 		try {
203 			String canon = m_file.getCanonicalPath();
204 			RelativeURLImpl newURL = new RelativeURLImpl(canon);
205 			newURL.setQuery(url_query);
206 			newURL.setFragment(url_fragment);
207 			return newURL;
208 		} catch (BadParameterException e) {
209             throw new RuntimeException(e);
210 		} catch (IOException e) {
211             throw new RuntimeException(e);
212 		}
213 	}
214 
215 
216 }