com.sun.xml.ws.security.opt.impl.incoming.EncryptedContentHeaderParser Maven / Gradle / Ivy
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.ws.security.opt.impl.incoming;
import com.sun.xml.ws.security.opt.impl.JAXBFilterProcessingContext;
import com.sun.xml.wss.XWSSecurityException;
import com.sun.xml.wss.impl.MessageConstants;
import com.sun.xml.wss.impl.c14n.AttributeNS;
import com.sun.xml.wss.impl.c14n.StAXAttr;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Vector;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
/**
*
* @author [email protected]
*/
class EncryptedContentHeaderParser{
XMLStreamReader encContentReader = null;
boolean parsed = false;
String localName;
String uri;
String prefix;
Vector attrList = new Vector();
Vector attrNSList = new Vector();
EncryptedData ed = null;
private HashMap parentNS = null;
private JAXBFilterProcessingContext context = null;
EncryptedContentHeaderParser(XMLStreamReader encContentReader, HashMap parentNS,
JAXBFilterProcessingContext context){
this.encContentReader = encContentReader;
this.parentNS = parentNS;
this.context = context;
}
XMLStreamReader getDecryptedElement(InputStream decryptedIS) throws XMLStreamException, XWSSecurityException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(out);
writeStartElement(writer);
writeEndElement(writer);
writer.flush();
writer.close();
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
String outStr = out.toString();
int pos = outStr.indexOf('>');
String startElem = outStr.substring(0, pos+1);
String endElem = outStr.substring(pos+1);
try {
tmpOut.write(startElem.getBytes());
byte[] buf = new byte[4096];
for(int len=-1;(len=decryptedIS.read(buf))!=-1;)
tmpOut.write(buf,0,len);
tmpOut.write(endElem.getBytes());
} catch (IOException ex) {
ex.printStackTrace();
}
InputStream finalContent = new ByteArrayInputStream(tmpOut.toByteArray());
XMLInputFactory xif = XMLInputFactory.newInstance();
return xif.createXMLStreamReader(finalContent);
}
void writeStartElement(XMLStreamWriter xsw) throws XMLStreamException, XWSSecurityException{
if(!parsed){
parse();
}
xsw.writeStartElement(prefix,localName,uri);
if(parentNS.containsKey(prefix)){
xsw.writeNamespace(prefix, uri);
}
for(int i=0;i