Skip to content
Permalink
Browse files
Consolidating Attachment CallbackHandler into a single class
git-svn-id: https://svn.apache.org/repos/asf/cxf/trunk@1566671 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
coheigea committed Feb 10, 2014
1 parent 9ffaa4d commit d37bdf0de648d868b96b820477eab96e144eea5b
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 159 deletions.
@@ -37,13 +37,13 @@
import org.apache.wss4j.common.ext.AttachmentResultCallback;

/**
* A outbound CallbackHandler to be used to sign/encrypt SOAP Attachments.
* A CallbackHandler to be used to sign/encrypt SOAP Attachments.
*/
public class AttachmentInCallbackHandler implements CallbackHandler {
public class AttachmentCallbackHandler implements CallbackHandler {

private final SoapMessage soapMessage;

public AttachmentInCallbackHandler(SoapMessage soapMessage) {
public AttachmentCallbackHandler(SoapMessage soapMessage) {
this.soapMessage = soapMessage;
}

@@ -57,36 +57,13 @@ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallback
List<org.apache.wss4j.common.ext.Attachment> attachmentList =
new ArrayList<org.apache.wss4j.common.ext.Attachment>();
attachmentRequestCallback.setAttachments(attachmentList);

org.apache.cxf.message.Attachment attachment = null;

final Collection<org.apache.cxf.message.Attachment> attachments =
soapMessage.getAttachments();
// Calling LazyAttachmentCollection.size() here to force it to load the attachments
if (attachments.size() > 0) {
for (Iterator<org.apache.cxf.message.Attachment> iterator = attachments.iterator();
iterator.hasNext();) {
attachment = iterator.next();

if (!attachmentRequestCallback.getAttachmentId().equals(attachment.getId())) {
continue;
}

org.apache.wss4j.common.ext.Attachment att =
new org.apache.wss4j.common.ext.Attachment();
att.setMimeType(attachment.getDataHandler().getContentType());
att.setId(attachment.getId());
att.setSourceStream(attachment.getDataHandler().getInputStream());
Iterator<String> headerIterator = attachment.getHeaderNames();
while (headerIterator.hasNext()) {
String next = headerIterator.next();
att.addHeader(next, attachment.getHeader(next));
}
attachmentList.add(att);

iterator.remove();
}

String attachmentId = attachmentRequestCallback.getAttachmentId();
if ("Attachments".equals(attachmentId)) {
// Load all attachments
attachmentId = null;
}
loadAttachments(attachmentList, attachmentId);
} else if (callback instanceof AttachmentResultCallback) {
AttachmentResultCallback attachmentResultCallback = (AttachmentResultCallback) callback;

@@ -115,5 +92,36 @@ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallback
}
}

private void loadAttachments(
List<org.apache.wss4j.common.ext.Attachment> attachmentList,
String attachmentId
) throws IOException {
final Collection<org.apache.cxf.message.Attachment> attachments = soapMessage.getAttachments();
// Calling LazyAttachmentCollection.size() here to force it to load the attachments
if (attachments != null && attachments.size() > 0) {
for (Iterator<org.apache.cxf.message.Attachment> iterator = attachments.iterator();
iterator.hasNext();) {
org.apache.cxf.message.Attachment attachment = iterator.next();

if (attachmentId != null && !attachmentId.equals(attachment.getId())) {
continue;
}

org.apache.wss4j.common.ext.Attachment att =
new org.apache.wss4j.common.ext.Attachment();
att.setMimeType(attachment.getDataHandler().getContentType());
att.setId(attachment.getId());
att.setSourceStream(attachment.getDataHandler().getInputStream());
Iterator<String> headerIterator = attachment.getHeaderNames();
while (headerIterator.hasNext()) {
String next = headerIterator.next();
att.addHeader(next, attachment.getHeader(next));
}
attachmentList.add(att);

iterator.remove();
}
}
}

}

This file was deleted.

@@ -213,7 +213,7 @@ public void handleMessage(SoapMessage msg) throws Fault {
*/
try {
reqData.setMsgContext(msg);
reqData.setAttachmentCallbackHandler(new AttachmentInCallbackHandler(msg));
reqData.setAttachmentCallbackHandler(new AttachmentCallbackHandler(msg));

setAlgorithmSuites(msg, reqData);

@@ -156,7 +156,7 @@ public void handleMessage(SoapMessage mc) throws Fault {
translateProperties(mc);

reqData.setMsgContext(mc);
reqData.setAttachmentCallbackHandler(new AttachmentOutCallbackHandler(mc));
reqData.setAttachmentCallbackHandler(new AttachmentCallbackHandler(mc));

/*
* The overall try, just to have a finally at the end to perform some
@@ -118,7 +118,7 @@ public void handleMessage(SoapMessage soapMessage) throws Fault {
}

if (secProps.getAttachmentCallbackHandler() == null) {
secProps.setAttachmentCallbackHandler(new AttachmentInCallbackHandler(soapMessage));
secProps.setAttachmentCallbackHandler(new AttachmentCallbackHandler(soapMessage));
}

TokenStoreCallbackHandler callbackHandler =
@@ -139,7 +139,7 @@ public void handleMessage(SoapMessage mc) throws Fault {
}

if (secProps.getAttachmentCallbackHandler() == null) {
secProps.setAttachmentCallbackHandler(new AttachmentOutCallbackHandler(mc));
secProps.setAttachmentCallbackHandler(new AttachmentCallbackHandler(mc));
}

SecurityEventListener securityEventListener =
@@ -72,7 +72,7 @@
import org.apache.cxf.ws.security.SecurityConstants;
import org.apache.cxf.ws.security.tokenstore.SecurityToken;
import org.apache.cxf.ws.security.tokenstore.TokenStore;
import org.apache.cxf.ws.security.wss4j.AttachmentOutCallbackHandler;
import org.apache.cxf.ws.security.wss4j.AttachmentCallbackHandler;
import org.apache.cxf.ws.security.wss4j.WSS4JUtils;
import org.apache.cxf.wsdl.WSDLConstants;
import org.apache.neethi.Assertion;
@@ -1647,7 +1647,7 @@ protected WSSecSignature getSignatureBuilder(
AbstractTokenWrapper wrapper, AbstractToken token, boolean attached, boolean endorse
) throws WSSecurityException {
WSSecSignature sig = new WSSecSignature(wssConfig);
sig.setAttachmentCallbackHandler(new AttachmentOutCallbackHandler(message));
sig.setAttachmentCallbackHandler(new AttachmentCallbackHandler(message));
checkForX509PkiPath(sig, token);
if (token instanceof IssuedToken || token instanceof SamlToken) {
assertPolicy(token);
@@ -41,7 +41,7 @@
import org.apache.cxf.ws.policy.AssertionInfoMap;
import org.apache.cxf.ws.security.SecurityConstants;
import org.apache.cxf.ws.security.tokenstore.SecurityToken;
import org.apache.cxf.ws.security.wss4j.AttachmentOutCallbackHandler;
import org.apache.cxf.ws.security.wss4j.AttachmentCallbackHandler;
import org.apache.wss4j.common.WSEncryptionPart;
import org.apache.wss4j.common.crypto.Crypto;
import org.apache.wss4j.common.ext.WSSecurityException;
@@ -473,7 +473,7 @@ private WSSecBase doEncryption(AbstractTokenWrapper recToken,
} else {
try {
WSSecEncrypt encr = new WSSecEncrypt(wssConfig);
encr.setAttachmentCallbackHandler(new AttachmentOutCallbackHandler(message));
encr.setAttachmentCallbackHandler(new AttachmentCallbackHandler(message));

encr.setDocument(saaj.getSOAPPart());
Crypto crypto = getEncryptionCrypto(recToken);
@@ -40,7 +40,7 @@
import org.apache.cxf.ws.security.SecurityConstants;
import org.apache.cxf.ws.security.tokenstore.SecurityToken;
import org.apache.cxf.ws.security.tokenstore.TokenStore;
import org.apache.cxf.ws.security.wss4j.AttachmentOutCallbackHandler;
import org.apache.cxf.ws.security.wss4j.AttachmentCallbackHandler;
import org.apache.wss4j.common.WSEncryptionPart;
import org.apache.wss4j.common.crypto.Crypto;
import org.apache.wss4j.common.derivedKey.ConversationConstants;
@@ -511,7 +511,7 @@ private WSSecBase doEncryption(AbstractTokenWrapper recToken,
} else {
try {
WSSecEncrypt encr = new WSSecEncrypt(wssConfig);
encr.setAttachmentCallbackHandler(new AttachmentOutCallbackHandler(message));
encr.setAttachmentCallbackHandler(new AttachmentCallbackHandler(message));
String encrTokId = encrTok.getId();
if (attached) {
encrTokId = encrTok.getWsuId();
@@ -752,7 +752,7 @@ private byte[] doSignature(List<WSEncryptionPart> sigs,
return doSignatureDK(sigs, policyAbstractTokenWrapper, policyToken, tok, included);
} else {
WSSecSignature sig = new WSSecSignature(wssConfig);
sig.setAttachmentCallbackHandler(new AttachmentOutCallbackHandler(message));
sig.setAttachmentCallbackHandler(new AttachmentCallbackHandler(message));
// If a EncryptedKeyToken is used, set the correct value type to
// be used in the wsse:Reference in ds:KeyInfo
int type = included ? WSConstants.CUSTOM_SYMM_SIGNING

0 comments on commit d37bdf0

Please sign in to comment.