use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.
the class InMessageRecorder method handleMessage.
public void handleMessage(Message message) throws Fault {
InputStream is = message.getContent(InputStream.class);
if (is == null) {
return;
}
LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream();
try {
IOUtils.copy(is, bos);
is.close();
bos.close();
byte[] bytes = bos.toByteArray();
synchronized (inbound) {
inbound.add(bytes);
}
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("inbound: " + bos.toString());
}
ByteArrayInputStream bis = bos.createInputStream();
message.setContent(InputStream.class, bis);
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.
the class WSDLToJavaContainer method generateLocalWSDL.
@SuppressWarnings("unchecked")
private void generateLocalWSDL(ToolContext context) {
String outputdir = (String) context.get(ToolConstants.CFG_CLASSDIR);
File wsdlFile = new File(outputdir, (String) context.get(ToolConstants.CFG_WSDLLOCATION));
Definition def = context.get(Definition.class);
try {
// get imported schemas
int xsdCount = 0;
SchemaCollection schemas = (SchemaCollection) context.get(ToolConstants.XML_SCHEMA_COLLECTION);
Map<String, String> sourceMap = new HashMap<>();
for (XmlSchema imp : schemas.getXmlSchemas()) {
if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
String schemaFileName = "schema" + (++xsdCount) + ".xsd";
sourceMap.put(imp.getTargetNamespace(), schemaFileName);
}
}
// get imported wsdls
int wsdlImportCount = 0;
List<Definition> defs = (List<Definition>) context.get(ToolConstants.IMPORTED_DEFINITION);
Map<String, String> importWSDLMap = new HashMap<>();
for (Definition importDef : defs) {
File importedWsdlFile = null;
if (!StringUtils.isEmpty(importDef.getDocumentBaseURI())) {
importedWsdlFile = new File(importDef.getDocumentBaseURI());
} else {
importedWsdlFile = new File(importDef.getQName().getLocalPart() + ".wsdl");
}
if (!FileUtils.isValidFileName(importedWsdlFile.getName())) {
importedWsdlFile = new File("import" + (++wsdlImportCount) + ".wsdl");
}
importWSDLMap.put(importDef.getTargetNamespace(), importedWsdlFile.getName());
}
OutputStreamCreator outputStreamCreator = null;
if (context.get(OutputStreamCreator.class) != null) {
outputStreamCreator = context.get(OutputStreamCreator.class);
} else {
outputStreamCreator = new OutputStreamCreator();
context.put(OutputStreamCreator.class, outputStreamCreator);
}
Writer os = null;
for (XmlSchema imp : schemas.getXmlSchemas()) {
if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
String schemaFileName = sourceMap.get(imp.getTargetNamespace());
File impfile = new File(wsdlFile.getParentFile(), schemaFileName);
Element el = imp.getSchemaDocument().getDocumentElement();
updateImports(el, sourceMap);
os = new FileWriterUtil(impfile.getParent(), context.get(OutputStreamCreator.class)).getWriter(impfile, StandardCharsets.UTF_8.name());
StaxUtils.writeTo(el, os, 2);
os.close();
}
}
// change the import location in wsdl file
OutputStream wsdloutput = new BufferedOutputStream(Files.newOutputStream(wsdlFile.toPath()));
WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
wsdlWriter.writeWSDL(def, bout);
Element defEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
List<Element> xsdElements = DOMUtils.findAllElementsByTagNameNS(defEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
for (Element xsdEle : xsdElements) {
updateImports(xsdEle, sourceMap);
}
updateWSDLImports(defEle, importWSDLMap);
StaxUtils.writeTo(defEle, wsdloutput);
wsdloutput.close();
for (Definition importDef : defs) {
File importWsdlFile = new File(outputdir, importWSDLMap.get(importDef.getTargetNamespace()));
OutputStream wsdlOs = new BufferedOutputStream(Files.newOutputStream(importWsdlFile.toPath()));
bout = new LoadingByteArrayOutputStream();
wsdlWriter.writeWSDL(importDef, bout);
Element importEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
xsdElements = DOMUtils.findAllElementsByTagNameNS(importEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
for (Element xsdEle : xsdElements) {
updateImports(xsdEle, sourceMap);
}
updateWSDLImports(importEle, importWSDLMap);
StaxUtils.writeTo(importEle, wsdlOs);
wsdlOs.close();
}
} catch (Exception ex) {
LOG.log(Level.SEVERE, "FAILED_TO_GEN_LOCAL_WSDL", ex);
Message msg = new Message("FAILED_TO_GEN_LOCAL_WSDL", LOG);
throw new ToolException(msg, ex);
}
}
use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.
the class EndpointReferenceUtils method createSchema.
private static synchronized Schema createSchema(ServiceInfo serviceInfo, Bus b) {
if (b == null) {
b = BusFactory.getThreadDefaultBus(false);
}
Schema schema = serviceInfo.getProperty(Schema.class.getName(), Schema.class);
if (schema == null) {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Map<String, byte[]> schemaSourcesMap = new LinkedHashMap<String, byte[]>();
Map<String, Source> schemaSourcesMap2 = new LinkedHashMap<String, Source>();
XMLStreamWriter writer = null;
try {
for (SchemaInfo si : serviceInfo.getSchemas()) {
Element el = si.getElement();
unsetReadonly(el);
String baseURI = null;
try {
baseURI = el.getBaseURI();
} catch (Exception ex) {
// ignore - not DOM level 3
}
if (baseURI == null) {
baseURI = si.getSystemId();
}
DOMSource ds = new DOMSource(el, baseURI);
schemaSourcesMap2.put(si.getSystemId() + ":" + si.getNamespaceURI(), ds);
LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
writer = StaxUtils.createXMLStreamWriter(out);
StaxUtils.copy(el, writer);
writer.flush();
schemaSourcesMap.put(si.getSystemId() + ":" + si.getNamespaceURI(), out.toByteArray());
}
for (XmlSchema sch : serviceInfo.getXmlSchemaCollection().getXmlSchemas()) {
if (sch.getSourceURI() != null && !schemaSourcesMap.containsKey(sch.getSourceURI() + ":" + sch.getTargetNamespace())) {
InputStream ins = null;
try {
URL url = new URL(sch.getSourceURI());
ins = url.openStream();
} catch (Exception e) {
// ignore, we'll just use what we have. (though
// bugs in XmlSchema could make this less useful)
}
LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
if (ins == null) {
sch.write(out);
} else {
IOUtils.copyAndCloseInput(ins, out);
}
schemaSourcesMap.put(sch.getSourceURI() + ":" + sch.getTargetNamespace(), out.toByteArray());
Source source = new StreamSource(out.createInputStream(), sch.getSourceURI());
schemaSourcesMap2.put(sch.getSourceURI() + ":" + sch.getTargetNamespace(), source);
}
}
factory.setResourceResolver(new SchemaLSResourceResolver(schemaSourcesMap, b));
schema = factory.newSchema(schemaSourcesMap2.values().toArray(new Source[schemaSourcesMap2.size()]));
} catch (Exception ex) {
// Something not right with the schema from the wsdl.
LOG.log(Level.WARNING, "SAXException for newSchema()", ex);
for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
String s = StaxUtils.toString(schemaInfo.getElement(), 4);
LOG.log(Level.INFO, "Schema for: " + schemaInfo.getNamespaceURI() + "\n" + s);
}
} finally {
for (Source src : schemaSourcesMap2.values()) {
if (src instanceof DOMSource) {
Node nd = ((DOMSource) src).getNode();
unsetReadonly(nd);
}
}
StaxUtils.close(writer);
}
serviceInfo.setProperty(Schema.class.getName(), schema);
}
return schema;
}
use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.
the class ColocUtil method convertObjectToSource.
public static void convertObjectToSource(Message message) {
List<Object> content = CastUtils.cast(message.getContent(List.class));
if (content == null || content.size() < 1) {
// nothing to convert
return;
}
// only supporting the wrapped style for now (one pojo <-> one source)
Object object = content.get(0);
DataWriter<OutputStream> writer = message.getExchange().getService().getDataBinding().createWriter(OutputStream.class);
LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream();
writer.write(object, bos);
content.set(0, new StreamSource(bos.createInputStream()));
}
use of org.apache.cxf.helpers.LoadingByteArrayOutputStream in project cxf by apache.
the class CachedOutputStream method resetOut.
/**
* Replace the original stream with the new one, optionally copying the content of the old one
* into the new one.
* When with Attachment, needs to replace the xml writer stream with the stream used by
* AttachmentSerializer or copy the cached output stream to the "real"
* output stream, i.e. onto the wire.
*
* @param out the new output stream
* @param copyOldContent flag indicating if the old content should be copied
* @throws IOException
*/
public void resetOut(OutputStream out, boolean copyOldContent) throws IOException {
if (out == null) {
out = new LoadingByteArrayOutputStream();
}
if (currentStream instanceof CachedOutputStream) {
CachedOutputStream ac = (CachedOutputStream) currentStream;
InputStream in = ac.getInputStream();
IOUtils.copyAndCloseInput(in, out);
} else {
if (inmem) {
if (currentStream instanceof ByteArrayOutputStream) {
ByteArrayOutputStream byteOut = (ByteArrayOutputStream) currentStream;
if (copyOldContent && byteOut.size() > 0) {
byteOut.writeTo(out);
}
} else {
throw new IOException("Unknown format of currentStream");
}
} else {
// read the file
try {
currentStream.close();
if (copyOldContent) {
InputStream fin = createInputStream(tempFile);
IOUtils.copyAndCloseInput(fin, out);
}
} finally {
streamList.remove(currentStream);
deleteTempFile();
inmem = true;
}
}
}
currentStream = out;
outputLocked = false;
}
Aggregations