1 16 package org.apache.cocoon.generation; 17 18 import org.apache.cocoon.ProcessingException; 19 import org.apache.cocoon.ResourceNotFoundException; 20 import org.apache.cocoon.environment.ObjectModelHelper; 21 import org.apache.cocoon.environment.Request; 22 import org.apache.cocoon.environment.http.HttpEnvironment; 23 import org.apache.cocoon.servlet.multipart.Part; 24 import org.apache.cocoon.util.PostInputStream; 25 import org.apache.excalibur.xml.sax.SAXParser; 26 import org.xml.sax.InputSource ; 27 import org.xml.sax.SAXException ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 31 import java.io.IOException ; 32 import java.io.InputStreamReader ; 33 import java.io.Reader ; 34 import java.io.StringReader ; 35 36 66 public class StreamGenerator extends ServiceableGenerator 67 { 68 69 70 public static final String FORM_NAME = "form-name"; 71 72 73 private InputSource inputSource; 74 75 79 public void recycle() { 80 super.recycle(); 81 this.inputSource = null; 82 } 83 84 87 public void generate() 88 throws IOException , SAXException , ProcessingException { 89 SAXParser parser = null; 90 int len = 0; 91 String contentType = null; 92 93 Request request = ObjectModelHelper.getRequest(this.objectModel); 94 try { 95 contentType = request.getContentType(); 96 if (contentType == null) { 97 contentType = parameters.getParameter("defaultContentType", null); 98 if (getLogger().isDebugEnabled()) { 99 getLogger().debug("no Content-Type header - using contentType parameter: " + contentType); 100 } 101 if (contentType == null) { 102 throw new IOException ("both Content-Type header and defaultContentType parameter are not set"); 103 } 104 } 105 if (contentType.startsWith("application/x-www-form-urlencoded") || 106 contentType.startsWith("multipart/form-data")) { 107 String parameter = parameters.getParameter(FORM_NAME, null); 108 if (parameter == null) { 109 throw new ProcessingException( 110 "StreamGenerator expects a sitemap parameter called '" + 111 FORM_NAME + "' for handling form data" 112 ); 113 } 114 Object xmlObject = request.get(parameter); 115 Reader xmlReader = null; 116 if (xmlObject instanceof String ) { 117 xmlReader = new StringReader ((String )xmlObject); 118 } else if (xmlObject instanceof Part) { 119 xmlReader = new InputStreamReader (((Part)xmlObject).getInputStream()); 120 } else { 121 throw new ProcessingException("Unknown request object encountered named " + 122 parameter + " : " + xmlObject); 123 } 124 inputSource = new InputSource (xmlReader); 125 } else if (contentType.startsWith("text/plain") || 126 contentType.startsWith("text/xml") || 127 contentType.startsWith("application/xml")) { 128 129 HttpServletRequest httpRequest = (HttpServletRequest ) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT); 130 if ( httpRequest == null ) { 131 throw new ProcessingException("This feature is only available in an http environment."); 132 } 133 len = request.getContentLength(); 134 if (len > 0) { 135 PostInputStream anStream = new PostInputStream(httpRequest.getInputStream(), len); 136 inputSource = new InputSource (anStream); 137 } else { 138 throw new IOException ("getContentLen() == 0"); 139 } 140 } else { 141 throw new IOException ("Unexpected getContentType(): " + request.getContentType()); 142 } 143 144 if (getLogger().isDebugEnabled()) { 145 getLogger().debug("processing stream ContentType=" + contentType + " ContentLen=" + len); 146 } 147 String charset = getCharacterEncoding(request, contentType) ; 148 if( charset != null) { 149 this.inputSource.setEncoding(charset); 150 } 151 parser = (SAXParser)this.manager.lookup(SAXParser.ROLE); 152 parser.parse(this.inputSource, super.xmlConsumer); 153 } catch (IOException e) { 154 getLogger().error("StreamGenerator.generate()", e); 155 throw new ResourceNotFoundException("StreamGenerator could not find resource", e); 156 } catch (SAXException e) { 157 getLogger().error("StreamGenerator.generate()", e); 158 throw(e); 159 } catch (Exception e) { 160 getLogger().error("Could not get parser", e); 161 throw new ProcessingException("Exception in StreamGenerator.generate()", e); 162 } finally { 163 this.manager.release( parser); 164 } 165 } 166 167 180 public String getCharacterEncoding(Request req, String contentType) { 181 String charencoding = null; 182 String charset = "charset="; 183 if (contentType == null) { 184 return null; 185 } 186 int idx = contentType.indexOf(charset); 187 if (idx == -1) { 188 return null; 189 } 190 try { 191 charencoding = req.getCharacterEncoding(); 192 193 if ( charencoding != null) { 194 getLogger().debug("charset from container: " + charencoding); 195 charencoding = charencoding.trim(); 196 if ((charencoding.length() > 2) && (charencoding.startsWith("\""))&& (charencoding.endsWith("\""))) { 197 charencoding = charencoding.substring(1, charencoding.length() - 1); 198 } 199 getLogger().debug("charset from container clean: " + charencoding); 200 return charencoding; 201 } else { 202 return extractCharset( contentType, idx ); 203 } 204 } catch(Throwable e) { 205 return extractCharset( contentType, idx ); 207 } 208 } 209 210 211 protected String extractCharset(String contentType, int idx) { 212 String charencoding = null; 213 String charset = "charset="; 214 215 getLogger().debug("charset from extractCharset"); 216 charencoding = contentType.substring(idx + charset.length()); 217 int idxEnd = charencoding.indexOf(";"); 218 if (idxEnd != -1) { 219 charencoding = charencoding.substring(0, idxEnd); 220 } 221 charencoding = charencoding.trim(); 222 if ((charencoding.length() > 2) && (charencoding.startsWith("\""))&& (charencoding.endsWith("\""))) { 223 charencoding = charencoding.substring(1, charencoding.length() - 1); 224 } 225 getLogger().debug("charset from extractCharset: " + charencoding); 226 return charencoding.trim(); 227 228 } 229 } 230 231 | Popular Tags |