com.sun.xml.ws.security.opt.impl.outgoing.SecurityHeader Maven / Gradle / Ivy
Show all versions of webservices-osgi Show documentation
/*
* 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.outgoing;
import com.sun.istack.NotNull;
import com.sun.istack.Nullable;
import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
import com.sun.xml.ws.security.opt.impl.enc.JAXBEncryptedData;
import com.sun.xml.ws.security.opt.impl.enc.JAXBEncryptedKey;
import com.sun.xml.ws.security.opt.impl.message.GSHeaderElement;
import com.sun.xml.ws.security.opt.impl.tokens.Timestamp;
import com.sun.xml.wss.impl.MessageConstants;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.xml.namespace.QName;
import jakarta.xml.soap.SOAPException;
import jakarta.xml.soap.SOAPMessage;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
/**
*
* @author [email protected]
*/
public class SecurityHeader {
public static final int LAYOUT_LAX = 0;
public static final int LAYOUT_STRICT = 1;
public static final int LAYOUT_LAX_TS_FIRST = 2;
public static final int LAYOUT_LAX_TS_LAST = 3;
protected ArrayList secHeaderContent = new ArrayList<>();
protected int headerLayout = LAYOUT_STRICT;
protected String soapVersion = MessageConstants.SOAP_1_1_NS;
private boolean debug = false;
private boolean mustUnderstandValue = true;
/**
* Default constructor
* uses Lax Message Layout and SOAP 1.1 version
*/
public SecurityHeader(){
}
public SecurityHeader(int layout, String soapVersion, boolean muValue){
this.headerLayout = layout;
this.soapVersion = soapVersion;
this.mustUnderstandValue = muValue;
}
public int getHeaderLayout(){
return this.headerLayout;
}
public void setHeaderLayout(int headerLayout){
this.headerLayout = headerLayout;
}
public String getSOAPVersion(){
return this.soapVersion;
}
public void setSOAPVersion(String soapVersion){
this.soapVersion = soapVersion;
}
public SecurityHeaderElement getChildElement(String localName,String uri){
for(SecurityHeaderElement she : secHeaderContent){
if(localName.equals(she.getLocalPart()) && uri.equals(she.getNamespaceURI())){
return she;
}
}
return null;
}
public Iterator getHeaders(final String localName,final String uri){
return new Iterator() {
int idx = 0;
Object next;
@Override
public boolean hasNext() {
if(next==null)
fetch();
return next!=null;
}
@Override
public Object next() {
if(next==null) {
fetch();
if(next==null){
throw new NoSuchElementException();
}
}
Object r = next;
next = null;
return r;
}
private void fetch() {
while(idx
* This is a convenience method that calls into {@link #getAttribute(String, String)}
*
* @param name
* Never null.
*
* @see #getAttribute(String, String)
*/
public @Nullable String getAttribute(@NotNull QName name){
throw new UnsupportedOperationException();
}
/**
* Writes out the header.
*
* @throws XMLStreamException
* if the operation fails for some reason. This leaves the
* writer to an undefined state.
*/
public void writeTo(XMLStreamWriter streamWriter) throws XMLStreamException{
orderHeaders();
if(secHeaderContent.size() >0){
streamWriter.writeStartElement(MessageConstants.WSSE_PREFIX,"Security",MessageConstants.WSSE_NS);
writeMustunderstand(streamWriter);
for( SecurityHeaderElement el : secHeaderContent){
((SecurityElementWriter)el).writeTo(streamWriter);
}
streamWriter.writeEndElement();
}
}
/**
* Writes out the header to the given SOAPMessage.
*
*
* Sometimes a Message needs to produce itself
* as {@link SOAPMessage}, in which case each header needs
* to turn itself into a header.
*
* @throws SOAPException
* if the operation fails for some reason. This leaves the
* writer to an undefined state.
*/
public void writeTo(SOAPMessage saaj) throws SOAPException{
throw new UnsupportedOperationException();
}
/**
* Writes out the header as SAX events.
*
*
* Sometimes a Message needs to produce SAX events,
* and this method is necessary for headers to participate to it.
*
*
* A header is responsible for producing the SAX events for its part,
* including startPrefixMapping
and endPrefixMapping
,
* but not startDocument/endDocument.
*
*
* Note that SAX contract requires that any error that does NOT originate
* from {@link ContentHandler} (meaning any parsing error and etc) must
* be first reported to {@link ErrorHandler}. If the SAX event production
* cannot be continued and the processing needs to abort, the code may
* then throw the same SAXParseException reported to {@link ErrorHandler}.
*
* @param contentHandler
* The {@link ContentHandler} that receives SAX events.
*
* @param errorHandler
* The {@link ErrorHandler} that receives parsing errors.
*/
public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) {
throw new UnsupportedOperationException();
}
private void writeMustunderstand(XMLStreamWriter writer) throws XMLStreamException{
if(mustUnderstandValue){
if(soapVersion == MessageConstants.SOAP_1_1_NS){
writer.writeAttribute("S",MessageConstants.SOAP_1_1_NS,MessageConstants.MUST_UNDERSTAND,"1");
}else if(soapVersion == MessageConstants.SOAP_1_2_NS){
writer.writeAttribute("S",MessageConstants.SOAP_1_2_NS,MessageConstants.MUST_UNDERSTAND,"true");
}
} else{
if(soapVersion == MessageConstants.SOAP_1_1_NS){
writer.writeAttribute("S",MessageConstants.SOAP_1_1_NS,MessageConstants.MUST_UNDERSTAND,"0");
}else if(soapVersion == MessageConstants.SOAP_1_2_NS){
writer.writeAttribute("S",MessageConstants.SOAP_1_2_NS,MessageConstants.MUST_UNDERSTAND,"false");
}
}
}
private void orderHeaders(){
if(headerLayout == LAYOUT_LAX_TS_LAST){
laxTimestampLast();
}else if(headerLayout == LAYOUT_LAX_TS_FIRST){
laxTimestampFirst();
}else{
strict();
}
}
private void laxTimestampLast(){
strict();
SecurityHeaderElement timestamp = this.secHeaderContent.get(0);
if((timestamp instanceof Timestamp)){
this.secHeaderContent.remove(0);
this.secHeaderContent.add(timestamp);
}
}
private void laxTimestampFirst(){
strict();
}
private void print(ArrayList list){
if(!debug){
return;
}
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++");
for(int j=0;j primaryElementList = new ArrayList<>();
ArrayList topElementList = new ArrayList<>();
int len = secHeaderContent.size();
print(secHeaderContent);
SecurityHeaderElement timeStamp = null;
for(int i=0;i tmpList = new ArrayList<>();
for(int i=0;i=0;j--){
SecurityHeaderElement tk = topElementList.get(j);
if(she.refersToSecHdrWithId(tk.getId())){
topElementList.add(j+1,she);
//topElementList.add(j,she);
tmpList.add(she);
break;
}
}
}
}
primaryElementList.removeAll(tmpList);
topElementList = orderList(topElementList);
secHeaderContent.clear();
for(int i=topElementList.size()-1;i>=0;i--){
secHeaderContent.add(topElementList.get(i));
}
for(int i=primaryElementList.size()-1;i>=0;i--){
secHeaderContent.add(primaryElementList.get(i));
}
if(timeStamp != null){
secHeaderContent.add(0,timeStamp);
}
}
private ArrayList orderList(ArrayList list){
ArrayList tmp = new ArrayList<>();
for(int i=0;i