asked on
ASKER
ASKER
ASKER
ASKER
...
//BROKEN: Removed our custom XmlFilterImpl extension in order to pin down this problem
//BROKEN: Issue seems to be with how we deal with XML transformations instead
//BROKEN: XMLFilter filter = new UrlResolverXmlFilter();
//BROKEN: So just used the base implementation class. This should always work...
XMLFilter filter = new XMLFilterImpl();
XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
filter.setParent(xmlReader);
//then we create a java io pipe, to send the results from the filtering to the escenic import function
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream);
StreamResult streamResult = new StreamResult(pipedOutputStream);
//apply the filter to the inputstream
SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
Transformer transformer = saxTransformerFactory.newTransformer();
SAXSource transformSource = new SAXSource(filter, originalInputSource);
//BROKEN: The program never returns from this call!
transformer.transform(transformSource, streamResult);
return new InputSource(pipedInputStream);
ASKER
1. The original inpustream is coming from a HTTPServletRequest. First of all, this is how I am generating the request using curl:
curl -X PUT -H 'Content-type: multipart/mixed stream' -d @src/xml/sample.xml http://dev-server:8080/myapp/import
2. In my servlet class:
public void doPut(HttpServletRequest request, HttpServletResponse response){
...snip...
ImportAction.doImport(request.getInputStream());
...snip...
}
3. Perform the import, using pipes, XML transformers and SAX parsers:
class ImportAction {
...snip...
public void doImport(InputStream httpRequestInputStream) throws Exception{
ReaderThread readerThread = translateUrls(httpRequestInputStream);
readerThread.start();
importHandler.importData(new InputSource(readerThread.getTransformedInStream()));
readerThread.finish();
}
...snip...
private static ReaderThread translateUrls(InputStream originalInputStream) throws Exception{
//First we create a filterchain
XMLFilter filter = new UrlResolverXmlFilter();
XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
filter.setParent(xmlReader);
//then we create a java io pipe, to send the results from the filtering to the escenic import function
PipedInputStream transformedPipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream(transformedPipedInputStream);
StreamResult streamResult = new StreamResult(pipedOutputStream);
//apply the filter to the inputstream
SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
Transformer transformer = saxTransformerFactory.newTransformer();
InputSource originalInputSource = new InputSource(originalInputStream);
SAXSource originalInputSaxSource = new SAXSource(filter, originalInputSource);
return new ReaderThread(originalInputSaxSource, streamResult, transformer, transformedPipedInputStream);
}
...snip...
}
class ReaderThread extends Thread {
private SAXSource originalInputSaxSource;
private StreamResult streamResult;
private Transformer transformer;
private InputStream transformedInStream;
private boolean finished = false;
...snip...
public void run(){
try {
transformer.transform(originalInputSaxSource, streamResult);
streamResult.getOutputStream().close();
//Hang around until we are sure we are finished, otherwise we will most likely
//get a broken pipe exception from the Escenic parser
while (!finished) try { Thread.sleep(500); } catch (InterruptedException e) {};
} catch (TransformerException e) {
...snip...
} catch (IOException e) {
..snip...
}
finally{
try {
//try it again, just to make sure. The stream could be closed already (see above)!
streamResult.getOutputStream().close();
} catch (IOException e) {
//ignore
}
}
}
public InputStream getTransformedInStream() {
return transformedInStream;
}
public void finish(){
finished = true;
}
...snip...
}//end class
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
ASKER