1 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 ; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 import java.net.MalformedURLException ; 34 35 41 42 public abstract class AbstractCommandLineEnvironment 43 extends AbstractEnvironment 44 implements Redirector { 45 46 protected String contentType; 47 protected int contentLength; 48 protected boolean hasRedirected = false; 49 protected int statusCode; 50 51 public AbstractCommandLineEnvironment(String uri, 52 String view, 53 File context, 54 OutputStream stream, 55 Logger log) 56 throws MalformedURLException { 57 super(uri, view, context); 58 this.enableLogging(log); 59 this.outputStream = stream; 60 this.statusCode = 0; 61 } 62 63 66 public void redirect(boolean sessionmode, String newURL) 67 throws IOException { 68 69 this.hasRedirected = true; 70 71 if (sessionmode) { 72 CommandLineSession.getSession(true); 73 } 74 75 if (newURL.startsWith("cli:/")) { 77 int pos = newURL.indexOf('/', 6); 78 newURL = newURL.substring(pos+1); 79 } 80 81 if (newURL.indexOf(":") == -1) { 83 newURL = "cocoon:/" + newURL; 84 } 85 86 if (newURL.startsWith("cocoon:") 88 && this.getView() != null 89 && this.getView().equals(Constants.LINK_VIEW)) { 90 91 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 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 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 143 public void setStatus(int statusCode) { 144 this.statusCode = statusCode; 145 } 146 147 150 public int getStatus() { 151 return statusCode; 152 } 153 154 157 public void setContentType(String contentType) { 158 this.contentType = contentType; 159 } 160 161 164 public void setContentLength(int contentLength) { 165 this.contentLength = contentLength; 166 } 167 168 171 public String getContentType() { 172 return this.contentType; 173 } 174 175 178 public boolean isExternal() { 179 return true; 180 } 181 182 187 public OutputStream 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 |