1 17 package org.apache.excalibur.source.impl; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.UnsupportedEncodingException ; 22 import java.net.HttpURLConnection ; 23 import java.net.URL ; 24 import java.net.URLConnection ; 25 import java.net.URLDecoder ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 29 import org.apache.excalibur.source.Source; 30 import org.apache.excalibur.source.SourceException; 31 import org.apache.excalibur.source.SourceParameters; 32 import org.apache.excalibur.source.SourceResolver; 33 import org.apache.excalibur.source.SourceUtil; 34 import org.apache.excalibur.source.SourceValidity; 35 import org.apache.excalibur.source.impl.validity.TimeStampValidity; 36 37 43 public class URLSource extends AbstractSource implements Source 44 { 45 46 47 protected URL m_url; 48 49 50 protected URLConnection m_connection; 51 52 53 protected SourceParameters m_parameters; 54 55 56 protected String m_encoding; 57 58 59 protected boolean m_isPost = false; 60 61 62 protected boolean m_exists = false; 63 64 65 protected SourceValidity m_cachedValidity; 66 67 protected long m_cachedLastModificationDate; 68 69 70 protected String m_mimeType; 71 72 75 public URLSource() 76 { 77 } 78 79 83 public void init(URL url, Map parameters) throws IOException 84 { 85 String systemId = url.toExternalForm(); 86 setSystemId(systemId); 87 setScheme(SourceUtil.getScheme(systemId)); 88 89 m_url = url; 90 m_isPost = false; 91 m_encoding = System.getProperties().getProperty("file.property", "ISO-8859-1"); 93 94 if (null != parameters) 95 { 96 m_parameters = (SourceParameters) parameters.get(SourceResolver.URI_PARAMETERS); 97 final String method = (String ) parameters.get(SourceResolver.METHOD); 98 99 if ("POST".equalsIgnoreCase(method)) 100 m_isPost = true; 101 102 final String encoding = (String ) parameters.get(SourceResolver.URI_ENCODING); 103 if (encoding != null && !"".equals(encoding)) 104 m_encoding = encoding; 105 } 106 107 if (null != m_parameters && m_parameters.hasParameters() && !m_isPost) 108 { 109 StringBuffer urlBuffer = new StringBuffer (systemId); 110 String key; 111 final Iterator i = m_parameters.getParameterNames(); 112 Iterator values; 113 String value; 114 boolean first = (systemId.indexOf('?') == -1); 115 if (first == true) 116 urlBuffer.append('?'); 117 while (i.hasNext()) 118 { 119 key = (String ) i.next(); 120 values = m_parameters.getParameterValues(key); 121 while (values.hasNext() == true) 122 { 123 value = SourceUtil.encode((String ) values.next(), m_encoding); 124 if (first == false) 125 urlBuffer.append('&'); 126 first = false; 127 urlBuffer.append(key); 128 urlBuffer.append('='); 129 urlBuffer.append(value); 130 } 131 } 132 133 m_url = new URL (urlBuffer.toString()); 134 m_parameters = null; 135 } 136 } 137 138 143 protected void getInfos() 144 { 145 m_exists = false; 147 148 if (!m_isPost) 149 { 150 try 151 { 152 if (null == m_connection) 153 { 154 m_connection = m_url.openConnection(); 155 String userInfo = getUserInfo(); 156 if (m_url.getProtocol().startsWith("http") && userInfo != null){ 157 m_connection.setRequestProperty("Authorization", "Basic " + SourceUtil.encodeBASE64(userInfo)); 158 } 159 } 160 setLastModified(m_connection.getLastModified()); 161 setContentLength(m_connection.getContentLength()); 162 m_mimeType = m_connection.getContentType(); 163 m_exists = true; 164 } 165 catch (IOException ignore) 166 { 167 super.getInfos(); 168 } 169 } 170 else 171 { 172 super.getInfos(); 174 } 175 } 176 177 180 public boolean exists() 181 { 182 checkInfos(); 183 return m_exists; 184 } 185 186 193 public InputStream getInputStream() throws IOException , SourceException 194 { 195 checkInfos(); 196 InputStream input = null; 197 if (m_connection == null) 198 { 199 m_connection = m_url.openConnection(); 200 201 String userInfo = getUserInfo(); 202 if (m_url.getProtocol().startsWith("http") && userInfo != null) 203 { 204 m_connection.setRequestProperty("Authorization", "Basic " + SourceUtil.encodeBASE64(userInfo)); 205 } 206 207 if (m_connection instanceof HttpURLConnection && m_isPost) 209 { 210 StringBuffer buffer = new StringBuffer (2000); 211 String key; 212 Iterator i = m_parameters.getParameterNames(); 213 Iterator values; 214 String value; 215 boolean first = true; 216 while (i.hasNext()) 217 { 218 key = (String ) i.next(); 219 values = m_parameters.getParameterValues(key); 220 while (values.hasNext() == true) 221 { 222 value = SourceUtil.encode((String ) values.next(), m_encoding); 223 if (first == false) 224 buffer.append('&'); 225 first = false; 226 buffer.append(key.toString()); 227 buffer.append('='); 228 buffer.append(value); 229 } 230 } 231 HttpURLConnection httpCon = (HttpURLConnection ) m_connection; 232 httpCon.setDoInput(true); 233 234 if (buffer.length() > 1) 235 { String postString = buffer.toString(); 237 httpCon.setRequestMethod("POST"); httpCon.setDoOutput(true); 239 httpCon.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); 240 241 httpCon.setRequestProperty("Content-length", Integer.toString(postString.length())); 243 java.io.OutputStream out = new java.io.BufferedOutputStream (httpCon.getOutputStream()); 244 out.write(postString.getBytes()); 245 out.close(); 246 } 247 input = httpCon.getInputStream(); 248 m_connection = null; return input; 250 } 251 } 252 input = m_connection.getInputStream(); 253 m_connection = null; return input; 255 } 256 257 263 public SourceValidity getValidity() 264 { 265 final long lm = getLastModified(); 266 if (lm > 0) 267 { 268 if (lm == m_cachedLastModificationDate) 269 return m_cachedValidity; 270 271 m_cachedLastModificationDate = lm; 272 m_cachedValidity = new TimeStampValidity(lm); 273 return m_cachedValidity; 274 } 275 return null; 276 } 277 278 282 public void refresh() 283 { 284 m_connection = null; 286 super.refresh(); 287 } 288 289 294 public String getMimeType() 295 { 296 return m_mimeType; 297 } 298 299 303 protected String getUserInfo() 304 { 305 if (m_url == null) return null; 306 String ui = m_url.getUserInfo(); 307 if (ui == null) return null; 308 309 try 310 { 311 ui = URLDecoder.decode(ui,"UTF-8"); 312 } 313 catch (UnsupportedEncodingException e) 314 { 315 } 318 return ui; 319 } 320 } 321 | Popular Tags |