KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > commandline > AbstractCommandLineEnvironment


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.environment.commandline;
17
18 import org.apache.avalon.framework.logger.Logger;
19 import org.apache.cocoon.CascadingIOException;
20 import org.apache.cocoon.Constants;
21 import org.apache.cocoon.ProcessingException;
22 import org.apache.cocoon.components.source.SourceUtil;
23 import org.apache.cocoon.environment.AbstractEnvironment;
24 import org.apache.cocoon.environment.Redirector;
25 import org.apache.excalibur.source.Source;
26 import org.apache.excalibur.source.SourceException;
27 import org.xml.sax.SAXException JavaDoc;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34
35 /**
36  * This environment is used to save the requested file to disk.
37  *
38  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
39  * @version CVS $Id: AbstractCommandLineEnvironment.java 154838 2005-02-22 12:30:44Z upayavira $
40  */

41
42 public abstract class AbstractCommandLineEnvironment
43 extends AbstractEnvironment
44 implements Redirector {
45
46     protected String JavaDoc contentType;
47     protected int contentLength;
48     protected boolean hasRedirected = false;
49     protected int statusCode;
50
51     public AbstractCommandLineEnvironment(String JavaDoc uri,
52                                           String JavaDoc view,
53                                           File JavaDoc context,
54                                           OutputStream JavaDoc stream,
55                                           Logger log)
56     throws MalformedURLException JavaDoc {
57         super(uri, view, context);
58         this.enableLogging(log);
59         this.outputStream = stream;
60         this.statusCode = 0;
61     }
62
63     /**
64      * Redirect the client to a new URL
65      */

66     public void redirect(boolean sessionmode, String JavaDoc newURL)
67     throws IOException {
68
69         this.hasRedirected = true;
70
71         if (sessionmode) {
72             CommandLineSession.getSession(true);
73         }
74
75         // fix all urls created with request.getScheme()+... etc.
76
if (newURL.startsWith("cli:/")) {
77             int pos = newURL.indexOf('/', 6);
78             newURL = newURL.substring(pos+1);
79         }
80
81         // fix all relative urls to use to cocoon: protocol
82
if (newURL.indexOf(":") == -1) {
83             newURL = "cocoon:/" + newURL;
84         }
85
86         // FIXME: this is a hack for the links view
87
if (newURL.startsWith("cocoon:")
88             && this.getView() != null
89             && this.getView().equals(Constants.LINK_VIEW)) {
90
91             // as the internal cocoon protocol is used the last
92
// serializer is removed from it! And therefore
93
// the LinkSerializer is not used.
94
// so we create one without Avalon...
95
org.apache.cocoon.serialization.LinkSerializer ls =
96                 new org.apache.cocoon.serialization.LinkSerializer();
97             ls.setOutputStream(this.outputStream);
98
99             Source redirectSource = null;
100             try {
101                 redirectSource = this.resolveURI(newURL);
102                 SourceUtil.parse( this.manager, redirectSource, ls);
103             } catch (SourceException se) {
104                 throw new CascadingIOException("SourceException: " + se, se);
105             } catch (SAXException JavaDoc se) {
106                 throw new CascadingIOException("SAXException: " + se, se);
107             } catch (ProcessingException pe) {
108                 throw new CascadingIOException("ProcessingException: " + pe, pe);
109             } finally {
110                 this.release( redirectSource );
111             }
112         } else {
113             Source redirectSource = null;
114             try {
115                 redirectSource = this.resolveURI(newURL);
116                 InputStream JavaDoc is = redirectSource.getInputStream();
117                 byte[] buffer = new byte[8192];
118                 int length = -1;
119
120                 while ((length = is.read(buffer)) > -1) {
121                     this.outputStream.write(buffer, 0, length);
122                 }
123             } catch (SourceException se) {
124                 throw new CascadingIOException("SourceException: " + se, se);
125             } finally {
126                 this.release( redirectSource);
127             }
128         }
129     }
130
131     public void sendStatus(int sc) {
132         setStatus(sc);
133         this.hasRedirected = true;
134     }
135
136     public boolean hasRedirected() {
137         return this.hasRedirected;
138     }
139
140     /**
141      * Set the StatusCode
142      */

143     public void setStatus(int statusCode) {
144         this.statusCode = statusCode;
145     }
146
147     /**
148      * Get the StatusCode
149      */

150     public int getStatus() {
151         return statusCode;
152     }
153
154     /**
155      * Set the ContentType
156      */

157     public void setContentType(String JavaDoc contentType) {
158         this.contentType = contentType;
159     }
160
161     /**
162      * Set the ContentLength
163      */

164     public void setContentLength(int contentLength) {
165         this.contentLength = contentLength;
166     }
167
168     /**
169      * Get the ContentType
170      */

171     public String JavaDoc getContentType() {
172         return this.contentType;
173     }
174     
175     /**
176      * Always return <code>true</code>.
177      */

178     public boolean isExternal() {
179         return true;
180     }
181
182     /**
183      * Return an OutputStream, but allow it to be null for when
184      * the pipeline is being streamed to the provided SAX
185      * content handler (using CocoonBean)
186      */

187     public OutputStream JavaDoc getOutputStream(int bufferSize) throws IOException {
188         if (this.outputStream == null) {
189             return null;
190         } else {
191             return super.getOutputStream(bufferSize);
192         }
193     }
194 }
Popular Tags