1 16 package org.apache.cocoon.components.source.impl; 17 18 import org.apache.excalibur.source.Source; 19 import org.apache.excalibur.source.SourceException; 20 import org.apache.excalibur.source.SourceValidity; 21 import org.apache.excalibur.source.SourceNotFoundException; 22 23 import org.apache.cocoon.servlet.multipart.Part; 24 import org.apache.cocoon.environment.ObjectModelHelper; 25 import org.apache.cocoon.environment.Request; 26 import java.net.MalformedURLException ; 27 import java.util.Map ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 31 32 40 public class PartSource implements Source 41 { 42 43 private String protocol; 44 45 46 private String uri; 47 48 49 private Part part; 50 51 57 public PartSource(String uri, Map objectModel) throws MalformedURLException , SourceException 58 { 59 this.uri = uri; 61 62 int position = uri.indexOf(':') + 1; 63 if (position != 0) 64 { 65 this.protocol = uri.substring(0, position-1); 67 } 68 else 69 { 70 throw new MalformedURLException ("No protocol found for part source in " + uri); 72 } 73 74 String location = uri.substring(position + 2); 76 77 Request request = ObjectModelHelper.getRequest(objectModel); 79 80 Object obj = request.get(location); 82 if (obj instanceof Part) 83 { 84 part = (Part) obj; 85 } 86 else 87 { 88 throw new SourceException("Request object " + location + " is not an uploaded Part"); 89 } 90 } 91 92 95 public InputStream getInputStream() throws IOException , SourceNotFoundException 96 { 97 try 98 { 99 return part.getInputStream(); 100 } 101 catch (Exception ex) 102 { 103 throw new SourceNotFoundException("The part source can not be found."); 104 } 105 } 106 107 110 public String getMimeType() 111 { 112 return part.getMimeType(); 113 } 114 115 118 public boolean exists() 119 { 120 return part != null; 121 } 122 123 126 public String getURI() 127 { 128 return uri; 129 } 130 131 134 public String getScheme() 135 { 136 return this.protocol; 137 } 138 139 142 public SourceValidity getValidity() 143 { 144 return null; 146 } 147 148 151 public void refresh() 152 { 153 } 154 155 158 public long getContentLength() 159 { 160 return part.getSize(); 161 } 162 163 166 public long getLastModified() 167 { 168 return 0; 169 } 170 171 public Part getPart() { 172 return this.part; 173 } 174 } 175 | Popular Tags |