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.reading;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.Map;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.apache.avalon.framework.parameters.Parameters;
026: import org.apache.cocoon.ProcessingException;
027: import org.apache.cocoon.environment.Context;
028: import org.apache.cocoon.environment.ObjectModelHelper;
029: import org.apache.cocoon.environment.Response;
030: import org.apache.cocoon.environment.SourceResolver;
031: import org.apache.cocoon.environment.http.HttpEnvironment;
032: import org.xml.sax.SAXException;
033:
034: /**
035: * The <code>RequestReader</code> component is used to serve binary data
036: * from the Http request
037: */
038: public class RequestReader extends AbstractReader {
039:
040: protected int bufferSize = 512;
041:
042: protected Response response;
043:
044: protected HttpServletRequest httpRequest;
045:
046: /**
047: * Setup the reader The resource is opened to get an
048: * <code>HttpServletRequest</code>
049: */
050: public void setup(SourceResolver resolver, Map objectModel,
051: String src, Parameters par) throws ProcessingException,
052: SAXException, IOException {
053: super .setup(resolver, objectModel, src, par);
054:
055: this .response = ObjectModelHelper.getResponse(objectModel);
056:
057: this .httpRequest = (HttpServletRequest) objectModel
058: .get(HttpEnvironment.HTTP_REQUEST_OBJECT);
059:
060: if (httpRequest == null) {
061: throw new ProcessingException(
062: "This feature is only available in an http environment.");
063: }
064: }
065:
066: /**
067: * Recyclable
068: */
069: public void recycle() {
070: this .httpRequest = null;
071: super .recycle();
072: }
073:
074: protected void processStream(InputStream inputStream)
075: throws IOException, ProcessingException {
076:
077: byte[] buffer = new byte[bufferSize];
078: int length = -1;
079:
080: long contentLength = httpRequest.getContentLength();
081:
082: if (contentLength != -1) {
083: response.setHeader("Content-Length", Long
084: .toString(contentLength));
085: }
086:
087: while ((length = inputStream.read(buffer)) > -1) {
088: out.write(buffer, 0, length);
089: }
090: out.flush();
091: }
092:
093: /**
094: * Generates the requested resource.
095: */
096: public void generate() throws IOException, ProcessingException {
097: try {
098: InputStream inputStream = httpRequest.getInputStream();
099:
100: try {
101: processStream(inputStream);
102: } finally {
103: if (inputStream != null) {
104: inputStream.close();
105: }
106: }
107:
108: } catch (IOException e) {
109: getLogger()
110: .debug(
111: "Received an IOException, assuming client severed connection on purpose");
112: }
113: }
114:
115: /**
116: * Returns the mime-type of the resource in process.
117: */
118: public String getMimeType() {
119: Context ctx = ObjectModelHelper.getContext(objectModel);
120: if (ctx != null) {
121: final String mimeType = ctx.getMimeType(source);
122: if (mimeType != null) {
123: return mimeType;
124: }
125: }
126: return null;
127: }
128: }
|