/**
* Generate XML data out of request InputStream.
*/
public void generate()
throws IOException, SAXException, ProcessingException {
SAXParser parser = null;
int len = 0;
String contentType = null;
Request request = ObjectModelHelper.getRequest(this.objectModel);
try {
contentType = request.getContentType();
if (contentType == null) {
contentType = parameters.getParameter("defaultContentType", null);
if (getLogger().isDebugEnabled()) {
getLogger().debug("no Content-Type header - using contentType parameter: " + contentType);
}
if (contentType == null) {
throw new IOException("both Content-Type header and defaultContentType parameter are not set");
}
}
if (contentType.startsWith("application/x-www-form-urlencoded") ||
contentType.startsWith("multipart/form-data")) {
String parameter = parameters.getParameter(FORM_NAME, null);
if (parameter == null) {
throw new ProcessingException(
"StreamGenerator expects a sitemap parameter called '" +
FORM_NAME + "' for handling form data"
);
}
Object xmlObject = request.get(parameter);
Reader xmlReader = null;
if (xmlObject instanceof String) {
xmlReader = new StringReader((String)xmlObject);
} else if (xmlObject instanceof Part) {
xmlReader = new InputStreamReader(((Part)xmlObject).getInputStream());
} else {
throw new ProcessingException("Unknown request object encountered named " +
parameter + " : " + xmlObject);
}
inputSource = new InputSource(xmlReader);
} else if (contentType.startsWith("text/plain") ||
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(httpRequest.getInputStream(), len);
inputSource = new InputSource(anStream);
} else {
throw new IOException("getContentLen() == 0");
}
} else {
throw new IOException("Unexpected getContentType(): " + request.getContentType());
}
if (getLogger().isDebugEnabled()) {
getLogger().debug("processing stream ContentType=" + contentType + " ContentLen=" + len);
}
String charset = getCharacterEncoding(request, contentType) ;
if( charset != null) {
this.inputSource.setEncoding(charset);
}
parser = (SAXParser)this.manager.lookup(SAXParser.ROLE);
parser.parse(this.inputSource, super.xmlConsumer);
} catch (IOException e) {
getLogger().error("StreamGenerator.generate()", e);
throw new ResourceNotFoundException("StreamGenerator could not find resource", e);
} catch (SAXException e) {
getLogger().error("StreamGenerator.generate()", e);