Commit 08b8275d authored by Marco Descher's avatar Marco Descher 🏔

[16515] IMessageService, multiple refactorings

parent 356d0593
Pipeline #12182 passed with stages
in 4 minutes and 42 seconds
......@@ -5,7 +5,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.IStatus;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
......@@ -45,7 +45,7 @@ public class MessageService implements IMessageService {
@Override
public List<String> getSupportedUriSchemes(){
return new ArrayList<String>(messageTransporters.keySet());
return new ArrayList<>(messageTransporters.keySet());
}
@Override
......@@ -54,7 +54,7 @@ public class MessageService implements IMessageService {
String receiver = message.getReceiver();
int indexOf = receiver.indexOf(':');
if (indexOf <= 0) {
return new ObjectStatus(Status.ERROR, Bundle.ID,
return new ObjectStatus(IStatus.ERROR, Bundle.ID,
"No transporter uri scheme found in receiver [" + receiver + "]", null);
}
......@@ -67,10 +67,18 @@ public class MessageService implements IMessageService {
}
if (messageTransporter == null) {
return new ObjectStatus(Status.ERROR, Bundle.ID,
return new ObjectStatus(IStatus.ERROR, Bundle.ID,
"No transporter found for uri scheme [" + uriScheme + "]", null);
}
if (messageTransporter.isExternal()) {
if (!message.isAlllowExternal()) {
return new ObjectStatus(IStatus.ERROR, Bundle.ID,
"Selected transporter is external, but message not marked as allowExternal, rejecting send.",
null);
}
}
return new ObjectStatus(messageTransporter.send(message),
messageTransporter.getUriScheme());
}
......
......@@ -2,7 +2,6 @@ package ch.elexis.core.services.eenv;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
......@@ -54,8 +53,8 @@ public class RocketchatMessageTransporter implements IMessageTransporter {
}
private IStatus sendFromStationSender(TransientMessage message){
Optional<String> authorizationToken = contextService
.getNamed(CTX_ROCKETCHAT_STATION_INTEGRATION_TOKEN).map(e -> e.toString());
Optional<String> authorizationToken =
contextService.getNamed(CTX_ROCKETCHAT_STATION_INTEGRATION_TOKEN).map(Object::toString);
if (authorizationToken.isPresent()) {
try {
URL integrationUrl =
......@@ -66,11 +65,11 @@ public class RocketchatMessageTransporter implements IMessageTransporter {
return send(integrationUrl, jsonMessage.getBytes());
} catch (IOException e) {
return new Status(Status.ERROR, Bundle.ID, e.getMessage());
return new Status(IStatus.ERROR, Bundle.ID, e.getMessage());
}
}
return new Status(Status.ERROR, Bundle.ID,
return new Status(IStatus.ERROR, Bundle.ID,
"No webhook integration token [" + CTX_ROCKETCHAT_STATION_INTEGRATION_TOKEN
+ "] found in root context or malformed url.");
}
......@@ -101,7 +100,7 @@ public class RocketchatMessageTransporter implements IMessageTransporter {
return json.toString();
}
private IStatus send(URL url, byte[] postDataBytes) throws MalformedURLException, IOException{
private IStatus send(URL url, byte[] postDataBytes) throws IOException{
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
......@@ -115,7 +114,7 @@ public class RocketchatMessageTransporter implements IMessageTransporter {
if (responseCode == 200) {
return Status.OK_STATUS;
}
return new Status(Status.ERROR, Bundle.ID,
return new Status(IStatus.ERROR, Bundle.ID,
"Error sending, with response code: " + responseCode);
}
......
package ch.elexis.core.services.internal;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.osgi.service.component.annotations.Component;
import ch.elexis.core.model.IMessage;
......@@ -29,10 +28,10 @@ public class InternalDatabaseMessageTransporter implements IMessageTransporter {
boolean save = CoreModelServiceHolder.get().save(idbMessage);
if (save) {
return Status.OK_STATUS;
return ObjectStatus.OK_STATUS(idbMessage.getId(), null);
}
return ObjectStatus.ERROR_STATUS(idbMessage.getId());
return ObjectStatus.ERROR_STATUS("Could not save message", null);
}
@Override
......
......@@ -92,4 +92,9 @@ public class TransientMessage {
this.alllowExternal = alllowExternal;
}
@Override
public String toString(){
return String.format("%s [%s -> %s] %s", createDateTime, sender, receiver, messageText);
}
}
\ No newline at end of file
......@@ -13,7 +13,7 @@ public interface IMessageService {
public static final String INTERNAL_MESSAGE_URI_SCHEME = "internal";
/**
* @return a list of all supported uri schemes
* @return a list of all supported URI schemes
*/
List<String> getSupportedUriSchemes();
......@@ -38,7 +38,9 @@ public interface IMessageService {
* Try to send the message.
*
* @param message
* @return if the message was sent successfully and the explicit transporter uri scheme used
* @return if the message was sent successfully, an optional message id as
* {@link ObjectStatus#getMessage()} and the explicit transporter URI scheme used in
* {@link ObjectStatus#getObject()}
*/
ObjectStatus send(TransientMessage message);
......
......@@ -17,9 +17,10 @@ public interface IMessageTransporter {
*
* @param message
* to send
* @return {@link IStatus#OK} if the message could be sent, {@link IStatus#CANCEL} if message
* not supported by this transporter (e.g. sender is not valid), or
* {@link IStatus#ERROR} if any other failure
* @return {@link ObjectStatus#OK} with the message ID as {@link IStatus#getMessage()} if the
* message could be sent; {@link IStatus#CANCEL} if the message not supported by this
* transporter (e.g. sender is not valid), or {@link IStatus#ERROR} with reason message
* on failure
*/
IStatus send(TransientMessage message);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment