You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2003/09/03 15:35:21 UTC
cvs commit: cocoon-2.1/src/java/org/apache/cocoon/generation StreamGenerator.java
cziegeler 2003/09/03 06:35:21
Modified: src/scratchpad/src/org/apache/cocoon/generation
RequestAttributeGenerator.java
src/java/org/apache/cocoon/generation StreamGenerator.java
Log:
Reducing dependency to the http environment
Revision Changes Path
1.3 +8 -10 cocoon-2.1/src/scratchpad/src/org/apache/cocoon/generation/RequestAttributeGenerator.java
Index: RequestAttributeGenerator.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/scratchpad/src/org/apache/cocoon/generation/RequestAttributeGenerator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- RequestAttributeGenerator.java 16 Mar 2003 18:03:54 -0000 1.2
+++ RequestAttributeGenerator.java 3 Sep 2003 13:35:20 -0000 1.3
@@ -52,14 +52,12 @@
import java.io.StringReader;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
import org.apache.avalon.framework.component.Component;
import org.apache.cocoon.ProcessingException;
-import org.apache.cocoon.environment.http.HttpEnvironment;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.Request;
+import org.apache.cocoon.environment.Response;
import org.apache.excalibur.xml.sax.SAXParser;
-
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -141,9 +139,9 @@
String contentType = null;
InputSource inputSource;
- HttpServletRequest request = (HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
- HttpServletResponse response = (HttpServletResponse) objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
-
+ final Request request = ObjectModelHelper.getRequest(this.objectModel);
+ final Response response = ObjectModelHelper.getResponse(this.objectModel);
+
byte[] xml_data = (byte[]) request.getAttribute(parameter);
if (xml_data == null) {
throw new ProcessingException("request-attribute " +
@@ -192,7 +190,7 @@
* @return The characterEncoding value
* @since 1.0
*/
- protected String getCharacterEncoding(HttpServletResponse res, String contentType) {
+ protected String getCharacterEncoding(Response res, String contentType) {
String charencoding = null;
String charset = "charset=";
if (contentType == null) {
1.3 +28 -36 cocoon-2.1/src/java/org/apache/cocoon/generation/StreamGenerator.java
Index: StreamGenerator.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/generation/StreamGenerator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- StreamGenerator.java 27 Jul 2003 12:52:49 -0000 1.2
+++ StreamGenerator.java 3 Sep 2003 13:35:20 -0000 1.3
@@ -53,6 +53,8 @@
import org.apache.avalon.framework.component.Component;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.ResourceNotFoundException;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.http.HttpEnvironment;
import org.apache.cocoon.util.PostInputStream;
import org.apache.excalibur.xml.sax.SAXParser;
@@ -114,13 +116,14 @@
/**
* Generate XML data out of request InputStream.
*/
- public void generate() throws IOException, SAXException, ProcessingException
- {
+ public void generate()
+ throws IOException, SAXException, ProcessingException {
SAXParser parser = null;
int len = 0;
String contentType = null;
+
+ Request request = ObjectModelHelper.getRequest(this.objectModel);
try {
- HttpServletRequest request = (HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
contentType = request.getContentType();
if (contentType == null) {
contentType = parameters.getParameter("defaultContentType", null);
@@ -145,9 +148,13 @@
contentType.startsWith("text/xml") ||
contentType.startsWith("application/xml")) {
+ HttpServletRequest httpRequest = (HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
+ if ( httpRequest == null ) {
+ throw new ProcessingException("This feature is only available in an http environment.");
+ }
len = request.getContentLength();
if (len > 0) {
- PostInputStream anStream = new PostInputStream(request.getInputStream(), len);
+ PostInputStream anStream = new PostInputStream(httpRequest.getInputStream(), len);
inputSource = new InputSource(anStream);
} else {
throw new IOException("getContentLen() == 0");
@@ -160,8 +167,7 @@
getLogger().debug("processing stream ContentType=" + contentType + " ContentLen=" + len);
}
String charset = getCharacterEncoding(request, contentType) ;
- if( charset != null)
- {
+ if( charset != null) {
this.inputSource.setEncoding(charset);
}
parser = (SAXParser)this.manager.lookup(SAXParser.ROLE);
@@ -193,67 +199,53 @@
*
* @param contentType value associated with Content-Type HTTP header.
*/
- public String getCharacterEncoding(HttpServletRequest req, String contentType)
- {
+ public String getCharacterEncoding(Request req, String contentType) {
String charencoding = null;
String charset = "charset=";
- if (contentType == null)
- {
- return (null);
+ if (contentType == null) {
+ return null;
}
int idx = contentType.indexOf(charset);
- if (idx == -1)
- {
- return (null);
+ if (idx == -1) {
+ return null;
}
- try
- {
+ try {
charencoding = req.getCharacterEncoding();
- if ( charencoding != null)
- {
+ if ( charencoding != null) {
getLogger().debug("charset from container: " + charencoding);
charencoding = charencoding.trim();
- if ((charencoding.length() > 2) && (charencoding.startsWith("\""))&& (charencoding.endsWith("\"")))
- {
+ if ((charencoding.length() > 2) && (charencoding.startsWith("\""))&& (charencoding.endsWith("\""))) {
charencoding = charencoding.substring(1, charencoding.length() - 1);
}
getLogger().debug("charset from container clean: " + charencoding);
- return (charencoding);
- }
- else
- {
-
+ return charencoding;
+ } else {
return extractCharset( contentType, idx );
}
- }
- catch(Throwable e)
- {
+ } catch(Throwable e) {
// We will be there if the container do not implement getCharacterEncoding() method
return extractCharset( contentType, idx );
}
}
- protected String extractCharset(String contentType, int idx)
- {
+ protected String extractCharset(String contentType, int idx) {
String charencoding = null;
String charset = "charset=";
getLogger().debug("charset from extractCharset");
charencoding = contentType.substring(idx + charset.length());
int idxEnd = charencoding.indexOf(";");
- if (idxEnd != -1)
- {
+ if (idxEnd != -1) {
charencoding = charencoding.substring(0, idxEnd);
}
charencoding = charencoding.trim();
- if ((charencoding.length() > 2) && (charencoding.startsWith("\""))&& (charencoding.endsWith("\"")))
- {
+ if ((charencoding.length() > 2) && (charencoding.startsWith("\""))&& (charencoding.endsWith("\""))) {
charencoding = charencoding.substring(1, charencoding.length() - 1);
}
getLogger().debug("charset from extractCharset: " + charencoding);
- return (charencoding.trim());
+ return charencoding.trim();
}
}