Java Examples for microsoft.exchange.webservices.data.core.ExchangeService

The following java examples will help you to understand the usage of microsoft.exchange.webservices.data.core.ExchangeService. These source code samples are taken from different open source projects.

Example 1
Project: ews-java-api-master  File: Recurrence.java View source code
@Override
public void internalWritePropertiesToXml(EwsServiceXmlWriter writer) throws Exception {
    super.internalWritePropertiesToXml(writer);
    this.getDaysOfTheWeek().writeToXml(writer, XmlElementNames.DaysOfWeek);
    if (this.firstDayOfWeek != null) {
        EwsUtilities.validatePropertyVersion((ExchangeService) writer.getService(), ExchangeVersion.Exchange2010_SP1, "FirstDayOfWeek");
        writer.writeElementValue(XmlNamespace.Types, XmlElementNames.FirstDayOfWeek, this.firstDayOfWeek);
    }
}
Example 2
Project: availability-master  File: ExchangeAvailabilityService.java View source code
@Override
@SneakyThrows
public Map<String, Optional<Availability>> getAvailability(List<String> emailAddresses, Date start, Date end) {
    Assert.notEmpty(emailAddresses, "emailAddresses cannot be empty");
    Assert.isTrue(!start.after(end), "start must not be after end");
    final Map<String, Optional<Availability>> ret = new HashMap<>();
    final AvailabilityOptions availabilityOptions = new AvailabilityOptions();
    availabilityOptions.setMeetingDuration(30);
    GetUserAvailabilityResults results;
    try (ExchangeService exchangeService = getExchangeService()) {
        // minimum time frame allowed by API is 24 hours
        results = exchangeService.getUserAvailability(emailAddresses.stream().map(AttendeeInfo::new).collect(Collectors.toList()), new TimeWindow(start, end.before(DateUtils.addDays(start, 1)) ? DateUtils.addDays(start, 1) : end), AvailabilityData.FreeBusyAndSuggestions, availabilityOptions);
    }
    Assert.isTrue(results.getAttendeesAvailability().getCount() == emailAddresses.size());
    for (int attendeesAvailabilityIndex = 0; attendeesAvailabilityIndex < results.getAttendeesAvailability().getCount(); attendeesAvailabilityIndex++) {
        AttendeeAvailability attendeeAvailability;
        try {
            attendeeAvailability = results.getAttendeesAvailability().getResponseAtIndex(attendeesAvailabilityIndex);
            attendeeAvailability.throwIfNecessary();
        } catch (ServiceResponseException e) {
            if (e.getErrorCode() == ServiceError.ErrorMailRecipientNotFound) {
                ret.put(emailAddresses.get(attendeesAvailabilityIndex), Optional.empty());
                continue;
            } else {
                throw e;
            }
        }
        FreeBusyStatus statusAtStart = FreeBusyStatus.FREE;
        final List<CalendarEvent> calendarEvents = new ArrayList<>();
        for (final microsoft.exchange.webservices.data.property.complex.availability.CalendarEvent calendarEvent : attendeeAvailability.getCalendarEvents()) {
            if (start.compareTo(calendarEvent.getEndTime()) < 0 && calendarEvent.getStartTime().compareTo(start) <= 0) {
                switch(calendarEvent.getFreeBusyStatus()) {
                    case Busy:
                        statusAtStart = FreeBusyStatus.BUSY;
                        break;
                    case Free:
                        // do nothing
                        break;
                    case NoData:
                        // do nothing
                        break;
                    case OOF:
                        // do nothing
                        break;
                    case Tentative:
                        if (statusAtStart == FreeBusyStatus.FREE) {
                            statusAtStart = FreeBusyStatus.TENTATIVE;
                        }
                        break;
                }
            }
            if (start.compareTo(calendarEvent.getEndTime()) < 0 && calendarEvent.getStartTime().compareTo(end) < 0) {
                calendarEvents.add(CalendarEvent.builder().start(calendarEvent.getStartTime()).end(calendarEvent.getEndTime()).status(legacyFreeBusyStatusToFreeBusyStatus(calendarEvent.getFreeBusyStatus())).location(calendarEvent.getDetails() == null ? null : calendarEvent.getDetails().getLocation()).subject(calendarEvent.getDetails() == null ? null : calendarEvent.getDetails().getSubject()).id(calendarEvent.getDetails() == null ? null : calendarEvent.getDetails().getStoreId()).build());
            }
        }
        Date nextFree = null;
        for (final Suggestion suggestion : results.getSuggestions()) {
            for (final TimeSuggestion timeSuggestion : suggestion.getTimeSuggestions()) {
                if (nextFree == null || nextFree.after(timeSuggestion.getMeetingTime())) {
                    nextFree = timeSuggestion.getMeetingTime();
                }
            }
        }
        ret.put(emailAddresses.get(attendeesAvailabilityIndex), Optional.of(Availability.builder().statusAtStart(statusAtStart).nextFree(nextFree).calendarEvents(Collections.unmodifiableList(calendarEvents)).build()));
    }
    return Collections.unmodifiableMap(ret);
}
Example 3
Project: nifi-master  File: ConsumeEWS.java View source code
protected ExchangeService initializeIfNecessary(ProcessContext context) throws ProcessException {
    ExchangeVersion ver = ExchangeVersion.valueOf(context.getProperty(EXCHANGE_VERSION).getValue());
    ExchangeService service = new ExchangeService(ver);
    final String timeoutInMillis = String.valueOf(context.getProperty(CONNECTION_TIMEOUT).evaluateAttributeExpressions().asTimePeriod(TimeUnit.MILLISECONDS));
    service.setTimeout(Integer.parseInt(timeoutInMillis));
    String userEmail = context.getProperty(USER).getValue();
    String password = context.getProperty(PASSWORD).getValue();
    ExchangeCredentials credentials = new WebCredentials(userEmail, password);
    service.setCredentials(credentials);
    Boolean useAutodiscover = context.getProperty(USE_AUTODISCOVER).asBoolean();
    if (useAutodiscover) {
        try {
            service.autodiscoverUrl(userEmail, new RedirectionUrlCallback());
        } catch (Exception e) {
            throw new ProcessException("Failure setting Autodiscover URL from email address.", e);
        }
    } else {
        String ewsURL = context.getProperty(EWS_URL).getValue();
        try {
            service.setUrl(new URI(ewsURL));
        } catch (URISyntaxException e) {
            throw new ProcessException("Failure setting EWS URL.", e);
        }
    }
    return service;
}
Example 4
Project: iaf-master  File: ExchangeMailListener.java View source code
public void configure() throws ConfigurationException {
    try {
        exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        CredentialFactory cf = new CredentialFactory(getAuthAlias(), getUserName(), getPassword());
        ExchangeCredentials credentials = new WebCredentials(cf.getUsername(), cf.getPassword());
        exchangeService.setCredentials(credentials);
        if (StringUtils.isNotEmpty(getMailAddress())) {
            exchangeService.autodiscoverUrl(getMailAddress());
        } else {
            exchangeService.setUrl(new URI(getUrl()));
        }
        FolderId inboxId;
        if (StringUtils.isNotEmpty(getMailAddress())) {
            Mailbox mailbox = new Mailbox(getMailAddress());
            inboxId = new FolderId(WellKnownFolderName.Inbox, mailbox);
        } else {
            inboxId = new FolderId(WellKnownFolderName.Inbox);
        }
        FindFoldersResults findFoldersResultsIn;
        FolderView folderViewIn = new FolderView(10);
        if (StringUtils.isNotEmpty(getInputFolder())) {
            SearchFilter searchFilterIn = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, getInputFolder());
            findFoldersResultsIn = exchangeService.findFolders(inboxId, searchFilterIn, folderViewIn);
            if (findFoldersResultsIn.getTotalCount() == 0) {
                throw new ConfigurationException("no (in) folder found with name [" + getInputFolder() + "]");
            } else if (findFoldersResultsIn.getTotalCount() > 1) {
                throw new ConfigurationException("multiple (in) folders found with name [" + getInputFolder() + "]");
            }
        } else {
            findFoldersResultsIn = exchangeService.findFolders(inboxId, folderViewIn);
        }
        folderIn = findFoldersResultsIn.getFolders().get(0);
        if (StringUtils.isNotEmpty(getFilter())) {
            if (!getFilter().equalsIgnoreCase("NDR")) {
                throw new ConfigurationException("illegal value for filter [" + getFilter() + "], must be 'NDR' or empty");
            }
        }
        if (StringUtils.isNotEmpty(getOutputFolder())) {
            SearchFilter searchFilterOut = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, getOutputFolder());
            FolderView folderViewOut = new FolderView(10);
            FindFoldersResults findFoldersResultsOut = exchangeService.findFolders(inboxId, searchFilterOut, folderViewOut);
            if (findFoldersResultsOut.getTotalCount() == 0) {
                throw new ConfigurationException("no (out) folder found with name [" + getOutputFolder() + "]");
            } else if (findFoldersResultsOut.getTotalCount() > 1) {
                throw new ConfigurationException("multiple (out) folders found with name [" + getOutputFolder() + "]");
            }
            folderOut = findFoldersResultsOut.getFolders().get(0);
        }
    } catch (Exception e) {
        throw new ConfigurationException(e);
    }
}