001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.source.impl;
018:
019: import org.apache.excalibur.source.Source;
020: import org.apache.excalibur.source.SourceException;
021: import org.apache.excalibur.source.SourceValidity;
022: import org.apache.excalibur.source.SourceNotFoundException;
023:
024: import org.apache.cocoon.servlet.multipart.Part;
025: import org.apache.cocoon.environment.ObjectModelHelper;
026: import org.apache.cocoon.environment.Request;
027: import java.net.MalformedURLException;
028: import java.util.Map;
029: import java.io.IOException;
030: import java.io.InputStream;
031:
032: /**
033: * Implementation of a {@link Source} that gets its content
034: * from a PartOnDisk or PartInMemory held in the Request when
035: * a file is uploaded.
036: *
037: * @author <a href="mailto:paul.crabtree@dna.co.uk">Paul Crabtree</a>
038: * @version CVS $Id: PartSource.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public class PartSource implements Source {
041: /* hold a private ref to the protocol used to call the Source */
042: private String protocol;
043:
044: /* hold a private ref to the full uri */
045: private String uri;
046:
047: /* hold a private ref to the Part which has been uploaded. */
048: private Part part;
049:
050: /**
051: * Builds a PartSource given an URI.
052: * @param uri e.g., upload://formField1
053: * @throws SourceException
054: * @throws MalformedURLException
055: */
056: public PartSource(String uri, Map objectModel)
057: throws MalformedURLException, SourceException {
058: // set the uri for use in getURI()
059: this .uri = uri;
060:
061: int position = uri.indexOf(':') + 1;
062: if (position != 0) {
063: // set the protocol for use in getScheme()
064: this .protocol = uri.substring(0, position - 1);
065: } else {
066: // if the URI is not correctly formatted then throw an excpetion
067: throw new MalformedURLException(
068: "No protocol found for part source in " + uri);
069: }
070:
071: // get the request parameter name: the bit after ://
072: String location = uri.substring(position + 2);
073:
074: // get the cocoon request from the object model.
075: Request request = ObjectModelHelper.getRequest(objectModel);
076:
077: // try and cast the request object to a Part
078: Object obj = request.get(location);
079: if (obj instanceof Part) {
080: part = (Part) obj;
081: } else {
082: throw new SourceException("Request object " + location
083: + " is not an uploaded Part");
084: }
085: }
086:
087: /**
088: * @see org.apache.excalibur.source.Source#getInputStream()
089: */
090: public InputStream getInputStream() throws IOException,
091: SourceNotFoundException {
092: try {
093: return part.getInputStream();
094: } catch (Exception ex) {
095: throw new SourceNotFoundException(
096: "The part source can not be found.");
097: }
098: }
099:
100: /**
101: * @see org.apache.excalibur.source.Source#getMimeType()
102: */
103: public String getMimeType() {
104: return part.getMimeType();
105: }
106:
107: /**
108: * @return true if the resource exists.
109: */
110: public boolean exists() {
111: return part != null;
112: }
113:
114: /*
115: * @see org.apache.excalibur.source.Source#getURI()
116: */
117: public String getURI() {
118: return uri;
119: }
120:
121: /*
122: * @see org.apache.excalibur.source.Source#getScheme()
123: */
124: public String getScheme() {
125: return this .protocol;
126: }
127:
128: /*
129: * Not used, Parts are not cacheable.
130: */
131: public SourceValidity getValidity() {
132: // not sure what happens here.
133: return null;
134: }
135:
136: /**
137: * @see org.apache.excalibur.source.Source#refresh()
138: */
139: public void refresh() {
140: }
141:
142: /**
143: * @see org.apache.excalibur.source.Source#getContentLength()
144: */
145: public long getContentLength() {
146: return part.getSize();
147: }
148:
149: /**
150: * @see org.apache.excalibur.source.Source#getLastModified()
151: */
152: public long getLastModified() {
153: return 0;
154: }
155:
156: public Part getPart() {
157: return this.part;
158: }
159: }
|