Skip to content

Commit

Permalink
Adding <> to cid
Browse files Browse the repository at this point in the history
  • Loading branch information
FrodeBjerkholt committed Feb 28, 2019
1 parent 4705e42 commit 2d950dd
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private LinkedHashMap<InputStream, As4PayloadHeader> parseAttachments(SOAPMessag

AttachmentPart attachmentPart = attachments.next();
InputStream is = attachmentPart.getDataHandler().getInputStream();
String contentId = attachmentPart.getContentId();
String contentId = AttachmentUtil.cleanContentId(attachmentPart.getContentId());

Map<String, MimeHeader> mimeHeaders = new HashMap<>();
Iterator<MimeHeader> mimeHeaderIterator = attachmentPart.getAllMimeHeaders();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/no/difi/oxalis/as4/outbound/As4Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import no.difi.oxalis.as4.util.CompressionUtil;
import no.difi.oxalis.as4.util.Constants;
import no.difi.oxalis.as4.util.Marshalling;
import no.difi.oxalis.as4.util.MessageIdUtil;
import no.difi.oxalis.commons.security.CertificateUtils;
import org.apache.cxf.attachment.AttachmentUtil;
import org.oasis_open.docs.ebxml_msg.ebms.v3_0.ns.core._200704.*;
import org.springframework.http.MediaType;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
Expand Down Expand Up @@ -54,7 +56,7 @@ public void doWithMessage(WebServiceMessage webServiceMessage) throws IOExceptio
InputStream compressedAttachment = compressionUtil.getCompressedStream(request.getPayload());

// Must be octet-stream for encrypted attachments
message.addAttachment(getPayloadHref(), () -> compressedAttachment, MediaType.APPLICATION_OCTET_STREAM_VALUE);
message.addAttachment(MessageIdUtil.wrap(getPayloadHref()), () -> compressedAttachment, MediaType.APPLICATION_OCTET_STREAM_VALUE);
addEbmsHeader(message);
}

Expand Down Expand Up @@ -82,7 +84,7 @@ private PayloadInfo createPayloadInfo(SoapMessage message) {
ArrayList<PartInfo> partInfos = Lists.newArrayList();
while (attachments.hasNext()) {
Attachment a = attachments.next();
String cid = "cid:" + a.getContentId();
String cid = "cid:" + AttachmentUtil.cleanContentId(a.getContentId());
Property compressionType = Property.builder()
.withName("CompressionType")
.withValue("application/gzip")
Expand Down
215 changes: 109 additions & 106 deletions src/main/java/no/difi/oxalis/as4/outbound/AttachmentCallbackHandler.java
Original file line number Diff line number Diff line change
@@ -1,106 +1,109 @@
package no.difi.oxalis.as4.outbound;

import no.difi.oxalis.as4.util.InputStreamDataSource;
import org.apache.wss4j.common.ext.AttachmentRequestCallback;
import org.apache.wss4j.common.ext.AttachmentResultCallback;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessage;

import javax.activation.DataHandler;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MimeHeader;
import javax.xml.soap.SOAPException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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

private SaajSoapMessage soapMessage;

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

@Override
public void handle (Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof AttachmentRequestCallback) {
AttachmentRequestCallback attachmentRequestCallback = (AttachmentRequestCallback) callback;

List<org.apache.wss4j.common.ext.Attachment> attachmentList = new ArrayList<>();
attachmentRequestCallback.setAttachments(attachmentList);

String attachmentId = attachmentRequestCallback.getAttachmentId();
if ("Attachments".equals(attachmentId)) {
attachmentId = null;
}
loadAttachments(attachmentList, attachmentId, attachmentRequestCallback.isRemoveAttachments());
} else if (callback instanceof AttachmentResultCallback) {
AttachmentResultCallback attachmentResultCallback = (AttachmentResultCallback) callback;
AttachmentPart attachmentPart = soapMessage.getSaajMessage()
.createAttachmentPart(new DataHandler(
new InputStreamDataSource(
attachmentResultCallback.getAttachment()
.getSourceStream(),
attachmentResultCallback.getAttachment()
.getMimeType())));
attachmentPart.setContentId(attachmentResultCallback.getAttachmentId());

Map<String, String> headers = attachmentResultCallback.getAttachment()
.getHeaders();
for (Map.Entry<String, String> entry : headers.entrySet()) {
attachmentPart.addMimeHeader(entry.getKey(), entry.getValue());
}

soapMessage.getSaajMessage()
.addAttachmentPart(attachmentPart);

} else {
throw new UnsupportedCallbackException(callback, "Unsupported callback");
}
}
}

@SuppressWarnings("unchecked")
private void loadAttachments (List<org.apache.wss4j.common.ext.Attachment> attachmentList,
String attachmentId,
boolean removeAttachments)
throws IOException {

Iterator<AttachmentPart> iterator = soapMessage.getSaajMessage().getAttachments();
while (iterator.hasNext()) {
AttachmentPart attachmentPart = iterator.next();
if (attachmentId != null && !attachmentId.equals(attachmentPart.getContentId())) {
continue;
}
org.apache.wss4j.common.ext.Attachment att = new org.apache.wss4j.common.ext.Attachment();
att.setMimeType(attachmentPart.getContentType());
att.setId(attachmentPart.getContentId());
try {
att.setSourceStream(attachmentPart.getDataHandler()
.getInputStream());
} catch (SOAPException e) {
throw new IOException("Soap exception: " + e.getMessage());
}
Iterator<MimeHeader> mimeHeaders = attachmentPart.getAllMimeHeaders();
while (mimeHeaders.hasNext()) {
MimeHeader mimeHeader = mimeHeaders.next();
att.addHeader(mimeHeader.getName(), mimeHeader.getValue());
}
attachmentList.add(att);

if (removeAttachments) {
iterator.remove();
}
}
}
}
package no.difi.oxalis.as4.outbound;

import no.difi.oxalis.as4.util.InputStreamDataSource;
import no.difi.oxalis.as4.util.MessageIdUtil;
import org.apache.cxf.attachment.AttachmentUtil;
import org.apache.wss4j.common.ext.AttachmentRequestCallback;
import org.apache.wss4j.common.ext.AttachmentResultCallback;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessage;

import javax.activation.DataHandler;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MimeHeader;
import javax.xml.soap.SOAPException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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

private SaajSoapMessage soapMessage;

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

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof AttachmentRequestCallback) {
AttachmentRequestCallback attachmentRequestCallback = (AttachmentRequestCallback) callback;

List<org.apache.wss4j.common.ext.Attachment> attachmentList = new ArrayList<>();
attachmentRequestCallback.setAttachments(attachmentList);

String attachmentId = attachmentRequestCallback.getAttachmentId();
if ("Attachments".equals(attachmentId)) {
attachmentId = null;
}
loadAttachments(attachmentList, attachmentId, attachmentRequestCallback.isRemoveAttachments());
} else if (callback instanceof AttachmentResultCallback) {
AttachmentResultCallback attachmentResultCallback = (AttachmentResultCallback) callback;
AttachmentPart attachmentPart = soapMessage.getSaajMessage()
.createAttachmentPart(new DataHandler(
new InputStreamDataSource(
attachmentResultCallback.getAttachment()
.getSourceStream(),
attachmentResultCallback.getAttachment()
.getMimeType())));
String attachmentId = attachmentResultCallback.getAttachmentId();
attachmentPart.setContentId(MessageIdUtil.wrap(attachmentId));

Map<String, String> headers = attachmentResultCallback.getAttachment()
.getHeaders();
for (Map.Entry<String, String> entry : headers.entrySet()) {
attachmentPart.addMimeHeader(entry.getKey(), entry.getValue());
}

soapMessage.getSaajMessage()
.addAttachmentPart(attachmentPart);

} else {
throw new UnsupportedCallbackException(callback, "Unsupported callback");
}
}
}

@SuppressWarnings("unchecked")
private void loadAttachments(List<org.apache.wss4j.common.ext.Attachment> attachmentList,
String attachmentId,
boolean removeAttachments)
throws IOException {

Iterator<AttachmentPart> iterator = soapMessage.getSaajMessage().getAttachments();
while (iterator.hasNext()) {
AttachmentPart attachmentPart = iterator.next();
if (attachmentId != null && !attachmentId.equals(attachmentPart.getContentId())) {
continue;
}
org.apache.wss4j.common.ext.Attachment att = new org.apache.wss4j.common.ext.Attachment();
att.setMimeType(attachmentPart.getContentType());
att.setId(AttachmentUtil.cleanContentId(attachmentPart.getContentId()));
try {
att.setSourceStream(attachmentPart.getDataHandler()
.getInputStream());
} catch (SOAPException e) {
throw new IOException("Soap exception: " + e.getMessage());
}
Iterator<MimeHeader> mimeHeaders = attachmentPart.getAllMimeHeaders();
while (mimeHeaders.hasNext()) {
MimeHeader mimeHeader = mimeHeaders.next();
att.addHeader(mimeHeader.getName(), mimeHeader.getValue());
}
attachmentList.add(att);

if (removeAttachments) {
iterator.remove();
}
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/no/difi/oxalis/as4/util/MessageIdUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ public class MessageIdUtil {
public static boolean verify(String identifier) {
return PATTERN.matcher(identifier).matches();
}

public static String wrap(String cid) {
if (cid == null) {
return null;
}

if (cid.startsWith("<") && cid.endsWith(">")) {
return cid;
}

return String.format("<%s>", cid);
}
}

0 comments on commit 2d950dd

Please sign in to comment.