001/*
002 *  Copyright 2020 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.exchange;
017
018import java.time.LocalDateTime;
019import java.time.ZoneId;
020import java.time.ZoneOffset;
021import java.time.ZonedDateTime;
022import java.time.format.DateTimeFormatter;
023import java.time.temporal.ChronoUnit;
024import java.util.ArrayList;
025import java.util.Date;
026import java.util.List;
027import java.util.Map;
028import java.util.TreeMap;
029import java.util.stream.Collectors;
030
031import org.apache.avalon.framework.service.ServiceException;
032import org.apache.avalon.framework.service.ServiceManager;
033
034import org.ametys.core.user.UserIdentity;
035import org.ametys.core.util.DateUtils;
036import org.ametys.plugins.extrausermgt.users.aad.GraphClientProvider;
037import org.ametys.plugins.extrausermgt.users.aad.GraphClientProvider.GraphClientException;
038import org.ametys.plugins.messagingconnector.AbstractMessagingConnector;
039import org.ametys.plugins.messagingconnector.CalendarEvent;
040import org.ametys.plugins.messagingconnector.EmailMessage;
041import org.ametys.plugins.messagingconnector.MessagingConnectorException;
042import org.ametys.plugins.messagingconnector.MessagingConnectorException.ExceptionType;
043
044import com.microsoft.graph.core.tasks.PageIterator;
045import com.microsoft.graph.models.Event;
046import com.microsoft.graph.models.EventCollectionResponse;
047import com.microsoft.graph.models.Message;
048import com.microsoft.graph.models.MessageCollectionResponse;
049import com.microsoft.graph.users.item.UserItemRequestBuilder;
050
051/**
052 * The connector used by the messaging connector plugin when connecting to Exchange Online.<br>
053 * Implemented through the Microsoft Graph API.
054 */
055public class GraphConnector extends AbstractMessagingConnector
056{
057    /** The avalon role */
058    public static final String INNER_ROLE = GraphConnector.class.getName();
059    
060    /** The Graph client provider */
061    protected GraphClientProvider _graphClientProvider;
062    
063    @Override
064    public void service(ServiceManager manager) throws ServiceException
065    {
066        super.service(manager);
067        _graphClientProvider = (GraphClientProvider) manager.lookup(GraphClientProvider.ROLE);
068    }
069    
070    private List<Event> _getEvents(UserIdentity userIdentity, int maxDays)
071    {
072        try
073        {
074            UserItemRequestBuilder userRequestBuilder = _graphClientProvider.getUserRequestBuilder(userIdentity);
075            
076            ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.SECONDS);
077            
078            String start = now.format(DateTimeFormatter.ISO_DATE_TIME);
079            String end = now.plusDays(maxDays).format(DateTimeFormatter.ISO_DATE_TIME);
080            
081            List<Event> result = new ArrayList<>();
082
083            EventCollectionResponse eventCollectionResponse = userRequestBuilder.calendar().calendarView().get(requestConfiguration -> {
084                requestConfiguration.queryParameters.startDateTime = start;
085                requestConfiguration.queryParameters.endDateTime = end;
086                requestConfiguration.queryParameters.orderby = new String[] {"start/datetime"};
087                requestConfiguration.queryParameters.select = new String[] {"subject", "start", "end", "location"};
088            });
089            
090            new PageIterator.Builder<Event, EventCollectionResponse>()
091                            .client(_graphClientProvider.getGraphClient())
092                            .collectionPage(eventCollectionResponse)
093                            .collectionPageFactory(EventCollectionResponse::createFromDiscriminatorValue)
094                            .processPageItemCallback(event -> {
095                                result.add(event);
096                                return true;
097                            })
098                            .build()
099                            .iterate();
100            
101            return result;
102        }
103        catch (GraphClientException e)
104        {
105            throw new MessagingConnectorException("Failed to retrieve a Graph client for user " + userIdentity + ". "
106                    + "The user could not be linked to a Entra user", ExceptionType.UNAUTHORIZED);
107        }
108        catch (Exception e)
109        {
110            throw new MessagingConnectorException("An error occured while contacting the graph endpoint for user " + userIdentity, ExceptionType.UNKNOWN);
111        }
112    }
113    
114    @Override
115    protected List<CalendarEvent> internalGetEvents(UserIdentity userIdentity, int maxDays, int maxEvents) throws MessagingConnectorException
116    {
117        List<Event> events = _getEvents(userIdentity, maxDays);
118        
119        Map<Long, CalendarEvent> calendarEvents = new TreeMap<>();
120        for (Event event : events)
121        {
122            LocalDateTime ldt = LocalDateTime.parse(event.getStart().getDateTime(), DateTimeFormatter.ISO_DATE_TIME);
123            ZonedDateTime zdt = ldt.atZone(ZoneId.of(event.getStart().getTimeZone()));
124            zdt = zdt.withZoneSameInstant(ZoneId.systemDefault());
125            
126            long longStart = zdt.toEpochSecond();
127            Date startDate = DateUtils.asDate(zdt);
128            
129            ldt = LocalDateTime.parse(event.getEnd().getDateTime(), DateTimeFormatter.ISO_DATE_TIME);
130            zdt = ldt.atZone(ZoneId.of(event.getEnd().getTimeZone()));
131            zdt = zdt.withZoneSameInstant(ZoneId.systemDefault());
132            
133            Date endDate = DateUtils.asDate(zdt);
134            
135            CalendarEvent newEvent = new CalendarEvent();
136            newEvent.setStartDate(startDate);
137            newEvent.setEndDate(endDate);
138            newEvent.setSubject(event.getSubject());
139            newEvent.setLocation(event.getLocation().getDisplayName());
140            calendarEvents.put(longStart, newEvent);
141        }
142        
143        return calendarEvents.entrySet().stream().limit(maxEvents).map(e -> e.getValue()).collect(Collectors.toList());
144    }
145
146    @Override
147    protected int internalGetEventsCount(UserIdentity userIdentity, int maxDays) throws MessagingConnectorException
148    {
149        return _getEvents(userIdentity, maxDays).size();
150    }
151
152    @Override
153    protected List<EmailMessage> internalGetEmails(UserIdentity userIdentity, int maxEmails) throws MessagingConnectorException
154    {
155        try
156        {
157            UserItemRequestBuilder userRequestBuilder = _graphClientProvider.getUserRequestBuilder(userIdentity);
158            
159            MessageCollectionResponse messageCollectionResponse = userRequestBuilder.mailFolders().byMailFolderId("inbox").messages().get(requestConfiguration -> {
160                requestConfiguration.queryParameters.top = Math.min(maxEmails, 999);
161                requestConfiguration.queryParameters.select = new String[] {"sender", "subject", "bodyPreview"};
162                requestConfiguration.queryParameters.filter = "isRead eq false";
163            });
164            
165            List<EmailMessage> result = new ArrayList<>();
166            
167            new PageIterator.Builder<Message, MessageCollectionResponse>()
168                            .client(_graphClientProvider.getGraphClient())
169                            .collectionPage(messageCollectionResponse)
170                            .collectionPageFactory(MessageCollectionResponse::createFromDiscriminatorValue)
171                            .processPageItemCallback(message -> {
172                                result.add(new EmailMessage(message.getSubject(), message.getSender().getEmailAddress().getAddress(), message.getBodyPreview()));
173                                return result.size() < maxEmails;
174                            })
175                            .build()
176                            .iterate();
177            
178            return result;
179        }
180        catch (GraphClientException e)
181        {
182            throw new MessagingConnectorException("Failed to retrieve a Graph client for user " + userIdentity + ". "
183                    + "The user could not be linked to a Entra user", ExceptionType.UNAUTHORIZED);
184        }
185        catch (Exception e)
186        {
187            throw new MessagingConnectorException("An error occured while contacting the graph endpoint for user " + userIdentity, ExceptionType.UNKNOWN);
188        }
189    }
190
191    @Override
192    protected int internalGetEmailsCount(UserIdentity userIdentity) throws MessagingConnectorException
193    {
194        try
195        {
196            UserItemRequestBuilder userRequestBuilder = _graphClientProvider.getUserRequestBuilder(userIdentity);
197            
198            return userRequestBuilder.mailFolders().byMailFolderId("inbox").get().getUnreadItemCount();
199        }
200        catch (GraphClientException e)
201        {
202            throw new MessagingConnectorException("Failed to retrieve a Graph client for user " + userIdentity + ". "
203                    + "The user could not be linked to a Entra user", ExceptionType.UNAUTHORIZED);
204        }
205    }
206}