-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #449 from bcgov/feature/DPS-232
Feature/dps 232
- Loading branch information
Showing
43 changed files
with
1,987 additions
and
683 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
.../src/main/java/ca/bc/gov/open/pssg/rsbc/dps/dpsemailpoller/email/DpsMSGraphException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ca.bc.gov.open.pssg.rsbc.dps.dpsemailpoller.email; | ||
|
||
public class DpsMSGraphException extends RuntimeException { | ||
|
||
public DpsMSGraphException(String message) { | ||
super(message); | ||
} | ||
|
||
public DpsMSGraphException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...a/ca/bc/gov/open/pssg/rsbc/dps/dpsemailpoller/email/component/GraphServiceClientComp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package ca.bc.gov.open.pssg.rsbc.dps.dpsemailpoller.email.component; | ||
|
||
import ca.bc.gov.open.pssg.rsbc.dps.dpsemailpoller.email.configuration.MSGraphServiceProperties; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.azure.identity.ClientSecretCredential; | ||
import com.azure.identity.ClientSecretCredentialBuilder; | ||
import com.microsoft.graph.serviceclient.GraphServiceClient; | ||
import jakarta.annotation.PostConstruct; | ||
|
||
/** | ||
* | ||
* Provides a GraphServiceClient | ||
* | ||
* Reference: https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=java#client-credentials-provider | ||
* | ||
* If the expiry date of the Secret key is coming up due, send a notification. | ||
* | ||
*/ | ||
@Component | ||
public class GraphServiceClientComp { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(GraphServiceClientComp.class); | ||
|
||
private String clientId; | ||
private String tenantId; | ||
private String clientSecret; | ||
|
||
private GraphServiceClient graphClient = null; | ||
|
||
private MSGraphServiceProperties props; | ||
|
||
public GraphServiceClientComp(MSGraphServiceProperties props) { | ||
this.props = props; | ||
} | ||
|
||
@PostConstruct | ||
private void postConstruct() { | ||
|
||
try { | ||
|
||
this.clientId = props.getMsgClientId(); | ||
this.tenantId = props.getMsgAuthority(); | ||
this.clientSecret = props.getMsgSecretKey(); | ||
|
||
// The client credentials flow requires that you request the | ||
// /.default scope, and pre-configure your permissions on the | ||
// app registration in Azure. An administrator must grant consent | ||
// to those permissions beforehand. | ||
final String[] scopes = new String[] { props.getMsgEndpointHost() }; | ||
|
||
final ClientSecretCredential credential = new ClientSecretCredentialBuilder().clientId(clientId) | ||
.tenantId(tenantId).clientSecret(clientSecret).build(); | ||
|
||
if (null == scopes || null == credential) { | ||
throw new Exception("Unexpected error"); | ||
} | ||
|
||
this.graphClient = new GraphServiceClient(credential, scopes); | ||
|
||
logger.info("MS Graph Service Client created."); | ||
|
||
} catch (Exception ex) { | ||
|
||
logger.error("Unable to create MS Graph Service client. Check CLientId, Authority and/or Secret. Err: " + ex.getMessage()); | ||
} | ||
|
||
} | ||
|
||
public GraphServiceClient getGraphClient() { | ||
return graphClient; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...c/gov/open/pssg/rsbc/dps/dpsemailpoller/email/configuration/MSGraphServiceProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ca.bc.gov.open.pssg.rsbc.dps.dpsemailpoller.email.configuration; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@ConfigurationProperties() | ||
public class MSGraphServiceProperties { | ||
|
||
@Value("${msg.clientId}") | ||
private String msgClientId; | ||
|
||
@Value("${msg.authority}") | ||
private String msgAuthority; | ||
|
||
@Value("${msg.secretKey}") | ||
private String msgSecretKey; | ||
|
||
@Value("${msg.email.account}") | ||
private String msgEmailAccount; | ||
|
||
@Value("${msg.endpointHost}") | ||
private String msgEndpointHost; | ||
|
||
@Value("${msg.email.per.batch}") | ||
private int msgEmailPerBatch; | ||
|
||
// Number of days before MS Graph API Secret Key Expiry Date to start sending notifications. | ||
@Value("${msg.secretKey.expiry.threshold}") | ||
private String msgSecretKeyExpiryThreshold; | ||
|
||
public String getMsgClientId() { | ||
return msgClientId; | ||
} | ||
|
||
public String getMsgAuthority() { | ||
return msgAuthority; | ||
} | ||
|
||
public String getMsgSecretKey() { | ||
return msgSecretKey; | ||
} | ||
|
||
public String getMsgEndpointHost() { | ||
return msgEndpointHost; | ||
} | ||
|
||
public String getMsgEmailAccount() { | ||
return msgEmailAccount; | ||
} | ||
|
||
public int getMsgEmailPerBatch() { | ||
return msgEmailPerBatch; | ||
} | ||
|
||
public String getMsgSecretKeyExpiryThreshold() { | ||
return msgSecretKeyExpiryThreshold; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
.../ca/bc/gov/open/pssg/rsbc/dps/dpsemailpoller/email/services/DpsMSGraphMetadataMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ca.bc.gov.open.pssg.rsbc.dps.dpsemailpoller.email.services; | ||
|
||
import ca.bc.gov.open.pssg.rsbc.models.DpsFileInfo; | ||
import ca.bc.gov.open.pssg.rsbc.models.DpsMetadata; | ||
import com.microsoft.graph.models.Message; | ||
|
||
public interface DpsMSGraphMetadataMapper { | ||
DpsMetadata map(Message emailMessage, DpsFileInfo dpsFileInfo, String tenant); | ||
} |
83 changes: 83 additions & 0 deletions
83
...bc/gov/open/pssg/rsbc/dps/dpsemailpoller/email/services/DpsMSGraphMetadataMapperImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package ca.bc.gov.open.pssg.rsbc.dps.dpsemailpoller.email.services; | ||
|
||
import ca.bc.gov.open.pssg.rsbc.dps.dpsemailpoller.email.DpsMSGraphException; | ||
import ca.bc.gov.open.pssg.rsbc.dps.dpsemailpoller.email.models.DpsEmailContent; | ||
import ca.bc.gov.open.pssg.rsbc.models.DpsFileInfo; | ||
import ca.bc.gov.open.pssg.rsbc.models.DpsMetadata; | ||
import com.microsoft.graph.models.EmailAddress; | ||
import com.microsoft.graph.models.Message; | ||
|
||
import java.util.Date; | ||
|
||
public class DpsMSGraphMetadataMapperImpl implements DpsMSGraphMetadataMapper { | ||
|
||
|
||
public static final String INBOUND = "inbound"; | ||
public static final String FAX = "FAX"; | ||
private final DpsEmailParser dpsEmailParser; | ||
|
||
public DpsMSGraphMetadataMapperImpl(DpsEmailParser dpsEmailParser) { | ||
this.dpsEmailParser = dpsEmailParser; | ||
} | ||
|
||
@Override | ||
public DpsMetadata map(Message emailMessage, DpsFileInfo dpsFileInfo, String tenant) { | ||
|
||
try { | ||
|
||
String body = getEmailBodyText(emailMessage); | ||
|
||
DpsEmailContent dpsEmailContent = dpsEmailParser.parseEmail(body); | ||
|
||
return new DpsMetadata.Builder() | ||
.withEmailId(emailMessage.getId()) | ||
.withApplicationID(tenant) | ||
.withDirection(INBOUND) | ||
.withInboundChannelType(FAX) | ||
.withInboundChannelID(getFirstRecipientEmailLocalPart(emailMessage)) | ||
.withTo(getFirstRecipientEmailAddress(emailMessage)) | ||
.withFrom(getFromEmailAddress(emailMessage)) | ||
.withSubject(emailMessage.getSubject()) | ||
.withRecvdate(Date.from(emailMessage.getReceivedDateTime().toInstant())) | ||
.withSentdate(Date.from(emailMessage.getSentDateTime().toInstant())) | ||
.withBody(emailMessage.getBody().getContent()) | ||
.withFaxJobID(dpsEmailContent.getJobId()) | ||
.withOriginatingNumber(dpsEmailContent.getPhoneNumer()) | ||
.withNumberOfPages(dpsEmailContent.getPageCount()) | ||
.withFileInfo(dpsFileInfo) | ||
.build(); | ||
|
||
} catch (DpsMSGraphException e) { | ||
throw new DpsMSGraphException("exception"); | ||
} | ||
|
||
} | ||
|
||
private String getFirstRecipientEmailLocalPart(Message emailMessage) throws DpsMSGraphException { | ||
String email = getFirstRecipientEmailAddress(emailMessage); | ||
return email.substring(0, email.indexOf('@')); | ||
} | ||
|
||
private String getFirstRecipientEmailAddress(Message emailMessage) throws DpsMSGraphException { | ||
return getFirstRecipients(emailMessage).getAddress(); | ||
|
||
} | ||
|
||
private String getFromEmailAddress(Message emailMessage) throws DpsMSGraphException { | ||
if (emailMessage.getFrom() == null) throw new DpsMSGraphException("From is null."); | ||
return emailMessage.getFrom().getEmailAddress().getAddress(); | ||
} | ||
|
||
|
||
private EmailAddress getFirstRecipients(Message emailMessage) throws DpsMSGraphException { | ||
if (emailMessage.getToRecipients() == null) throw new DpsMSGraphException("Recipient is null."); | ||
if (emailMessage.getToRecipients().isEmpty()) throw new DpsMSGraphException("Recipient is null."); | ||
return emailMessage.getToRecipients().get(0).getEmailAddress(); | ||
} | ||
|
||
|
||
private String getEmailBodyText(Message emailMessage) throws DpsMSGraphException { | ||
return emailMessage.getBody().getContent(); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
.../main/java/ca/bc/gov/open/pssg/rsbc/dps/dpsemailpoller/email/services/MSGraphService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ca.bc.gov.open.pssg.rsbc.dps.dpsemailpoller.email.services; | ||
|
||
import ca.bc.gov.open.pssg.rsbc.dps.dpsemailpoller.email.DpsMSGraphException; | ||
import com.microsoft.graph.models.Attachment; | ||
import com.microsoft.graph.models.Message; | ||
|
||
import java.util.List; | ||
|
||
public interface MSGraphService { | ||
|
||
public List<Message> GetMessages(String filter) throws DpsMSGraphException; | ||
public List<Attachment> getAttachments(Message message) throws DpsMSGraphException; | ||
public byte[] getAttachmentContent(Attachment attachment) throws DpsMSGraphException; | ||
|
||
public void sendMessage(Message message, boolean saveToSentItems) throws DpsMSGraphException; | ||
public void deleteMessage(Message message) throws DpsMSGraphException; | ||
public String getPasswordCredentialsExpiryDate() throws DpsMSGraphException; | ||
|
||
public Message moveToFolder(String messageId, String folderName, boolean createIfNotExist) throws DpsMSGraphException; | ||
} |
Oops, something went wrong.