Skip to content

Commit

Permalink
Added and fixed CEF connectivity tests, and refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
evenos committed Oct 31, 2019
1 parent 7880542 commit c2623b7
Show file tree
Hide file tree
Showing 17 changed files with 681 additions and 119 deletions.
22 changes: 6 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>no.difi.oxalis</groupId>
<artifactId>oxalis-outbound</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand Down Expand Up @@ -191,22 +197,6 @@

<build>
<plugins>
<!-- <plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin> -->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/no/difi/oxalis/as4/common/DummyHeaderParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import lombok.extern.slf4j.Slf4j;
import no.difi.oxalis.api.header.HeaderParser;
import no.difi.oxalis.api.util.Type;
import no.difi.vefa.peppol.common.model.Header;
import no.difi.vefa.peppol.common.model.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

@Slf4j
@Singleton
Expand All @@ -26,6 +27,13 @@ public Header parse(InputStream inputStream) {
log.error("IOException while parsing header", e);
}

return new Header();
return Header.of(
ParticipantIdentifier.of("DummySender"),
ParticipantIdentifier.of("DummyReceiver"),
ProcessIdentifier.of("DummyProcess"),
DocumentTypeIdentifier.of("DummyDocument"),
InstanceIdentifier.of("DummyInstance"),
InstanceType.of("Dummy", "InstanceType", "1.0"),
new Date(0L));
}
}
98 changes: 62 additions & 36 deletions src/main/java/no/difi/oxalis/as4/outbound/As4MessageSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import no.difi.oxalis.api.outbound.TransmissionRequest;
import no.difi.oxalis.api.outbound.TransmissionResponse;
import no.difi.oxalis.api.settings.Settings;
import no.difi.oxalis.api.timestamp.TimestampProvider;
import no.difi.oxalis.as4.api.MessageIdGenerator;
import no.difi.oxalis.as4.lang.OxalisAs4TransmissionException;
import no.difi.oxalis.as4.util.CompressionUtil;
import no.difi.oxalis.as4.util.OxalisAlgorithmSuiteLoader;
import no.difi.oxalis.as4.util.Constants;
import no.difi.oxalis.commons.http.HttpConf;
import no.difi.oxalis.commons.security.KeyStoreConf;
import org.apache.cxf.Bus;
import org.apache.cxf.attachment.AttachmentUtil;
import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.ext.logging.LoggingFeature;
import org.apache.cxf.headers.Header;
import org.apache.cxf.jaxb.JAXBDataBinding;
import org.apache.cxf.jaxws.DispatchImpl;
import org.apache.cxf.message.Attachment;
import org.apache.cxf.message.Message;
Expand All @@ -26,13 +26,13 @@
import org.apache.cxf.ws.policy.PolicyConstants;
import org.apache.cxf.ws.policy.WSPolicyFeature;
import org.apache.cxf.ws.security.SecurityConstants;
import org.apache.cxf.ws.security.policy.custom.AlgorithmSuiteLoader;
import org.apache.neethi.Policy;
import org.apache.wss4j.common.ConfigurationConstants;
import org.apache.wss4j.common.crypto.Merlin;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.oasis_open.docs.ebxml_msg.ebms.v3_0.ns.core._200704.Messaging;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.SOAPMessage;
Expand Down Expand Up @@ -68,54 +68,56 @@ public class As4MessageSender {
@Inject
private CompressionUtil compressionUtil;

@Inject
private TimestampProvider timestampProvider;

@Inject
private Settings<HttpConf> httpConfSettings;

@Inject
private TransmissionResponseConverter transmissionResponseConverter;

@Inject
private Bus bus;

public TransmissionResponse send(TransmissionRequest request) throws OxalisAs4TransmissionException {

final String address = request.getEndpoint().getAddress().toString();
private Service service;

Service service = Service.create(SERVICE_NAME, new LoggingFeature(), new WSPolicyFeature());
public As4MessageSender() {
service = Service.create(SERVICE_NAME, new LoggingFeature(), new WSPolicyFeature());
service.addPort(PORT_NAME, SOAPBinding.SOAP12HTTP_BINDING, "BindingProvider.ENDPOINT_ADDRESS_PROPERTY placeholder");
}

public TransmissionResponse send(TransmissionRequest request) throws OxalisAs4TransmissionException {

Dispatch<SOAPMessage> dispatch = createDispatch(request.getEndpoint().getAddress().toString());


Dispatch<SOAPMessage> dispatch = service.createDispatch(PORT_NAME, SOAPMessage.class, Service.Mode.MESSAGE);
dispatch.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, address);
Collection<Attachment> attachments = prepareAttachments(request);
Messaging messaging = messagingProvider.createMessagingHeader(request, attachments);

HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(httpConfSettings.getInt(HttpConf.TIMEOUT_CONNECT));
httpClientPolicy.setReceiveTimeout(httpConfSettings.getInt(HttpConf.TIMEOUT_READ));
((HTTPConduit)((DispatchImpl)dispatch).getClient().getConduit()).setClient(httpClientPolicy);
SoapHeader header = null;
try {
header = new SoapHeader(
Constants.MESSAGING_QNAME,
messaging,
new JAXBDataBinding(Messaging.class),
true);
}catch (JAXBException e){
throw new OxalisAs4TransmissionException("Unable to marshal AS4 header", e);
}


dispatch.getRequestContext().put(Header.HEADER_LIST, new ArrayList(Arrays.asList(header)));
dispatch.getRequestContext().put(Message.ATTACHMENTS, attachments);

HashMap<String, List<String>> headers = new HashMap<>();
headers.put("Content-ID", Collections.singletonList(messageIdGenerator.generate()));
headers.put("CompressionType", Collections.singletonList("application/gzip"));
headers.put("MimeType", Collections.singletonList("application/xml"));

Attachment attachment = null;
try{
attachment = AttachmentUtil.createAttachment(compressionUtil.getCompressedStream(request.getPayload()), headers);
configureSecurity(request, dispatch);

}catch (IOException e){
throw new OxalisAs4TransmissionException("Unable to compress payload", e);
}
Collection<Attachment> attachments = new ArrayList<>(Arrays.asList(attachment));
SOAPMessage response = dispatch.invoke(null);

SoapHeader header = messagingProvider.createMessagingHeader(request, attachments);

return transmissionResponseConverter.convert(request, response);

dispatch.getRequestContext().put(Header.HEADER_LIST, new ArrayList(Arrays.asList(header)));
dispatch.getRequestContext().put(Message.ATTACHMENTS, attachments);
}

private void configureSecurity(TransmissionRequest request, Dispatch<SOAPMessage> dispatch) throws OxalisAs4TransmissionException {

Merlin signatureCrypto = new Merlin();
signatureCrypto.setCryptoProvider(BouncyCastleProvider.PROVIDER_NAME);
Expand All @@ -130,21 +132,45 @@ public TransmissionResponse send(TransmissionRequest request) throws OxalisAs4Tr


try {
Bus bus = ((DispatchImpl) dispatch).getClient().getBus();
bus.setExtension(new OxalisAlgorithmSuiteLoader(bus), AlgorithmSuiteLoader.class);

PolicyBuilder builder = ((DispatchImpl) dispatch).getClient().getBus().getExtension(org.apache.cxf.ws.policy.PolicyBuilder.class);
PolicyBuilder builder = bus.getExtension(PolicyBuilder.class);
Policy policy = builder.getPolicy(getClass().getResourceAsStream("/policy.xml"));
dispatch.getRequestContext().put(PolicyConstants.POLICY_OVERRIDE, policy);
}catch (IOException | ParserConfigurationException | SAXException e){
throw new OxalisAs4TransmissionException("Unable to parse WSPolicy \"/policy.xml\"", e);
}
}

public Collection<Attachment> prepareAttachments(TransmissionRequest request) throws OxalisAs4TransmissionException {

SOAPMessage response = dispatch.invoke(null);
HashMap<String, List<String>> headers = new HashMap<>();
headers.put("Content-ID", Collections.singletonList(messageIdGenerator.generate()));
headers.put("CompressionType", Collections.singletonList("application/gzip"));
headers.put("MimeType", Collections.singletonList("application/xml"));

try{

Attachment attachment = AttachmentUtil.createAttachment(compressionUtil.getCompressedStream(request.getPayload()), headers);
return new ArrayList<>(Arrays.asList(attachment));

}catch (IOException e){

return new TransmissionResponseConverter(request, timestampProvider).convert(response);
throw new OxalisAs4TransmissionException("Unable to compress payload", e);
}
}

private Dispatch<SOAPMessage> createDispatch(String address) {

Dispatch<SOAPMessage> dispatch = service.createDispatch(PORT_NAME, SOAPMessage.class, Service.Mode.MESSAGE);
dispatch.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, address);

HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(httpConfSettings.getInt(HttpConf.TIMEOUT_CONNECT));
httpClientPolicy.setReceiveTimeout(httpConfSettings.getInt(HttpConf.TIMEOUT_READ));

((HTTPConduit)((DispatchImpl)dispatch).getClient().getConduit()).setClient(httpClientPolicy);

return dispatch;
}


Expand Down
22 changes: 16 additions & 6 deletions src/main/java/no/difi/oxalis/as4/outbound/As4OutboundModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import no.difi.oxalis.api.settings.Settings;
import no.difi.oxalis.as4.config.As4Conf;
import no.difi.oxalis.as4.util.CompressionUtil;
import no.difi.oxalis.as4.util.OxalisAlgorithmSuiteLoader;
import no.difi.oxalis.as4.util.PeppolConfiguration;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.ws.security.policy.custom.AlgorithmSuiteLoader;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Security;
Expand All @@ -33,20 +37,26 @@ protected void configure() {

bind(As4MessageSender.class);

bind(TransmissionResponseConverter.class);

}

@Provides
@Singleton
public Bus getBus(){
Bus bus = BusFactory.getThreadDefaultBus(true);
bus.setExtension(new OxalisAlgorithmSuiteLoader(bus), AlgorithmSuiteLoader.class);

return bus;
}

@Provides
@Singleton
protected PeppolConfiguration getPeppolOutboundConfiguration(Settings<As4Conf> settings){
public PeppolConfiguration getPeppolOutboundConfiguration(Settings<As4Conf> settings){

if("cef-connectivity".equalsIgnoreCase(settings.getString(As4Conf.TYPE))){
return new PeppolConfiguration(){

@Override
public String getServiceType() {
return "urn:oasis:names:tc:ebcore:partyid-type:unregistered";
}

@Override
public String getPartyIDType() {
return "urn:oasis:names:tc:ebcore:partyid-type:unregistered";
Expand Down
Loading

0 comments on commit c2623b7

Please sign in to comment.