Skip to content

Commit

Permalink
Optimizing sending of AS2 messages and adding support for pluggable g…
Browse files Browse the repository at this point in the history
…eneration of Message-Id. #294
  • Loading branch information
klakegg committed Jun 7, 2018
1 parent cee0138 commit d577999
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2017 Norwegian Agency for Public Management and eGovernment (Difi)
*
* Licensed under the EUPL, Version 1.1 or – as soon they
* will be approved by the European Commission - subsequent
* versions of the EUPL (the "Licence");
*
* You may not use this work except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
*
* https://joinup.ec.europa.eu/community/eupl/og_page/eupl
*
* Unless required by applicable law or agreed to in
* writing, software distributed under the Licence is
* distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the Licence for the specific language governing
* permissions and limitations under the Licence.
*/

package no.difi.oxalis.as2.api;

import no.difi.oxalis.api.outbound.TransmissionRequest;

/**
* @author erlend
* @since 4.0.2
*/
public interface MessageIdGenerator {

String generate(TransmissionRequest transmissionRequest);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
*/
public class As2Header {

public static final String MESSAGE_ID = "Message-Id";

public static final String MIME_VERSION = "MIME-Version";

public static final String CONTENT_TYPE = "Content-Type";

public static final String AS2_VERSION = "AS2-Version";

public static final String AS2_FROM = "AS2-From";
Expand All @@ -38,8 +44,6 @@ public class As2Header {

public static final String SUBJECT = "Subject";

public static final String MESSAGE_ID = "Message-ID";

public static final String DATE = "Date";

public static final String DISPOSITION_NOTIFICATION_TO = "Disposition-Notification-To";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2010-2017 Norwegian Agency for Public Management and eGovernment (Difi)
*
* Licensed under the EUPL, Version 1.1 or – as soon they
* will be approved by the European Commission - subsequent
* versions of the EUPL (the "Licence");
*
* You may not use this work except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
*
* https://joinup.ec.europa.eu/community/eupl/og_page/eupl
*
* Unless required by applicable law or agreed to in
* writing, software distributed under the Licence is
* distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the Licence for the specific language governing
* permissions and limitations under the Licence.
*/

package no.difi.oxalis.as2.common;

import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import no.difi.oxalis.api.settings.Settings;
import no.difi.oxalis.as2.api.MessageIdGenerator;
import no.difi.oxalis.commons.guice.ImplLoader;
import no.difi.oxalis.commons.guice.OxalisModule;

/**
* @author erlend
* @since 4.0.2
*/
public class As2CommonModule extends OxalisModule {

@Override
protected void configure() {
bindTyped(MessageIdGenerator.class, DefaultMessageIdGenerator.class);

bindSettings(As2Conf.class);
}

@Provides
@Singleton
@Named("as2-notification")
public String getNotification(Settings<As2Conf> settings) {
return settings.getString(As2Conf.NOTIFICATION);
}

@Provides
@Singleton
public MessageIdGenerator getMessageIdGenerator(Injector injector, Settings<As2Conf> settings) {
return ImplLoader.get(injector, MessageIdGenerator.class, settings, As2Conf.MSGID_GENERATOR);
}
}
48 changes: 48 additions & 0 deletions oxalis-as2/src/main/java/no/difi/oxalis/as2/common/As2Conf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2017 Norwegian Agency for Public Management and eGovernment (Difi)
*
* Licensed under the EUPL, Version 1.1 or – as soon they
* will be approved by the European Commission - subsequent
* versions of the EUPL (the "Licence");
*
* You may not use this work except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
*
* https://joinup.ec.europa.eu/community/eupl/og_page/eupl
*
* Unless required by applicable law or agreed to in
* writing, software distributed under the Licence is
* distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the Licence for the specific language governing
* permissions and limitations under the Licence.
*/

package no.difi.oxalis.as2.common;

import no.difi.oxalis.api.settings.DefaultValue;
import no.difi.oxalis.api.settings.Path;
import no.difi.oxalis.api.settings.Title;

/**
* @author erlend
* @since 4.0.2
*/
@Title("AS2")
public enum As2Conf {

@Path("oxalis.as2.hostname")
@DefaultValue("")
HOSTNAME,

@Path("oxalis.as2.notification")
@DefaultValue("not.in.use@difi.no")
NOTIFICATION,

@Path("oxalis.as2.msgid_gen")
@DefaultValue("default")
MSGID_GENERATOR,

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2010-2017 Norwegian Agency for Public Management and eGovernment (Difi)
*
* Licensed under the EUPL, Version 1.1 or – as soon they
* will be approved by the European Commission - subsequent
* versions of the EUPL (the "Licence");
*
* You may not use this work except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
*
* https://joinup.ec.europa.eu/community/eupl/og_page/eupl
*
* Unless required by applicable law or agreed to in
* writing, software distributed under the Licence is
* distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the Licence for the specific language governing
* permissions and limitations under the Licence.
*/

package no.difi.oxalis.as2.common;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import no.difi.oxalis.api.outbound.TransmissionRequest;
import no.difi.oxalis.api.settings.Settings;
import no.difi.oxalis.api.util.Type;
import no.difi.oxalis.as2.api.MessageIdGenerator;

import javax.mail.internet.InternetAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.atomic.AtomicLong;

/**
* @author erlend
*/
@Singleton
@Type("default")
public class DefaultMessageIdGenerator implements MessageIdGenerator {

private String hostname;

private AtomicLong atomicLong = new AtomicLong();

@Inject
public DefaultMessageIdGenerator(Settings<As2Conf> settings) {
try {
hostname = settings.getString(As2Conf.HOSTNAME);
if (hostname.trim().isEmpty())
hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
throw new IllegalStateException("Unable to get local hostname.", e);
}
}

@Override
public String generate(TransmissionRequest transmissionRequest) {
return new StringBuilder()
.append('<')
.append(System.currentTimeMillis())
.append('.')
.append(atomicLong.incrementAndGet())
.append('.')
.append(transmissionRequest.hashCode())
.append('.')
.append("Oxalis")
.append('@')
.append(hostname)
.append('>')
.toString();
}
}
Loading

0 comments on commit d577999

Please sign in to comment.