/** * Read items from XML. * * @param reader The reader * @param propertySet The property set * @param destinationList The list in which to add the read items. * @throws javax.xml.stream.XMLStreamException the xML stream exception * @throws ServiceXmlDeserializationException the service xml deserialization exception * @throws Exception the exception */ private void internalReadItemsFromXml( EwsServiceXmlReader reader, PropertySet propertySet, List<TItem> destinationList) throws XMLStreamException, ServiceXmlDeserializationException, Exception { EwsUtilities.EwsAssert( destinationList != null, "FindItemResponse.InternalReadItemsFromXml", "destinationList is null."); reader.readStartElement(XmlNamespace.Types, XmlElementNames.Items); if (!reader.isEmptyElement()) { do { reader.read(); if (reader.getNodeType().nodeType == XmlNodeType.START_ELEMENT) { Item item = EwsUtilities.createEwsObjectFromXmlElementName( Item.class, reader.getService(), reader.getLocalName()); if (item == null) { reader.skipCurrentElement(); } else { item.loadFromXml( reader, true, /* clearPropertyBag */ propertySet, true /* summaryPropertiesOnly */); destinationList.add((TItem) item); } } } while (!reader.isEndElement(XmlNamespace.Types, XmlElementNames.Items)); } else { reader.read(); } }
/** Validates this instance. */ @Override protected void internalValidate() throws Exception { super.internalValidate(); EwsUtilities.validateParam(this.fromAddresses, "FromAddresses"); EwsUtilities.validateParam(this.sentToAddresses, "SentToAddresses"); EwsUtilities.validateParam(this.withinDateRange, "WithinDateRange"); EwsUtilities.validateParam(this.withinSizeRange, "WithinSizeRange"); }
/** * Validate request. * * @throws microsoft.exchange.webservices.data.ServiceLocalException the service local exception * @throws Exception the exception */ @Override protected void validate() throws ServiceLocalException, Exception { super.validate(); EwsUtilities.validateParam(this.name, "name"); EwsUtilities.validateParam(this.parentFolderId, "parentFolderId"); this.getParentFolderId().validate(this.getService().getRequestedServerVersion()); }
@Test public void testParseBoolean() throws ParseException { Boolean input = Boolean.TRUE; assertEquals(input, EwsUtilities.parse(input.getClass(), input.toString())); input = Boolean.FALSE; assertEquals(input, EwsUtilities.parse(input.getClass(), input.toString())); }
@Test public void testEwsAssert() { EwsUtilities.ewsAssert(true, null, null); try { EwsUtilities.ewsAssert(false, "a", "b"); } catch (final RuntimeException ex) { assertEquals("[a] b", ex.getMessage()); } }
@Test public void testParseLong() throws ParseException { Long input = Long.MAX_VALUE; assertEquals(input, EwsUtilities.parse(input.getClass(), input.toString())); input = 0l; assertEquals(input, EwsUtilities.parse(input.getClass(), input.toString())); input = Long.MIN_VALUE; assertEquals(input, EwsUtilities.parse(input.getClass(), input.toString())); }
@Test public void testParseFloat() throws ParseException { Float input = Float.MAX_VALUE; assertEquals(input, EwsUtilities.parse(input.getClass(), input.toString())); input = 0f; assertEquals(input, EwsUtilities.parse(input.getClass(), input.toString())); input = Float.MIN_VALUE; assertEquals(input, EwsUtilities.parse(input.getClass(), input.toString())); }
@Test public void testParseByte() throws ParseException { Byte input = Byte.MAX_VALUE; assertEquals(input, EwsUtilities.parse(input.getClass(), input.toString())); input = 0; assertEquals(input, EwsUtilities.parse(input.getClass(), input.toString())); input = Byte.MIN_VALUE; assertEquals(input, EwsUtilities.parse(input.getClass(), input.toString())); }
/** * * Writes the autodiscover SOAP request. * * @param requestUrl Request URL. * @param memoryStream the memory stream * @return stream The stream. * @throws XMLStreamException the xML stream exception * @throws ServiceXmlSerializationException the service xml serialization exception */ protected void writeSoapRequest(URI requestUrl, ByteArrayOutputStream memoryStream) throws XMLStreamException, ServiceXmlSerializationException { EwsServiceXmlWriter writer = new EwsServiceXmlWriter(this.service, memoryStream); writer.writeStartDocument(); writer.writeStartElement(XmlNamespace.Soap, XmlElementNames.SOAPEnvelopeElementName); writer.writeAttributeValue( "xmlns", EwsUtilities.getNamespacePrefix(XmlNamespace.Soap), EwsUtilities.getNamespaceUri(XmlNamespace.Soap)); writer.writeAttributeValue( "xmlns", EwsUtilities.AutodiscoverSoapNamespacePrefix, EwsUtilities.AutodiscoverSoapNamespace); writer.writeAttributeValue( "xmlns", EwsUtilities.WSAddressingNamespacePrefix, EwsUtilities.WSAddressingNamespace); writer.writeAttributeValue( "xmlns", EwsUtilities.EwsXmlSchemaInstanceNamespacePrefix, EwsUtilities.EwsXmlSchemaInstanceNamespace); writer.writeStartElement(XmlNamespace.Soap, XmlElementNames.SOAPHeaderElementName); if (this.service.getCredentials() != null) { this.service.getCredentials().emitExtraSoapHeaderNamespaceAliases(writer.getInternalWriter()); } writer.writeElementValue( XmlNamespace.Autodiscover, XmlElementNames.RequestedServerVersion, this.service.getRequestedServerVersion().toString()); writer.writeElementValue( XmlNamespace.WSAddressing, XmlElementNames.Action, this.getWsAddressingActionName()); writer.writeElementValue(XmlNamespace.WSAddressing, XmlElementNames.To, requestUrl.toString()); if (this.service.getCredentials() != null) { this.service.getCredentials().serializeWSSecurityHeaders(writer.getInternalWriter()); } this.service.doOnSerializeCustomSoapHeaders(writer.getInternalWriter()); writer.writeEndElement(); // soap:Header writer.writeStartElement(XmlNamespace.Soap, XmlElementNames.SOAPBodyElementName); this.writeBodyToXml(writer); writer.writeEndElement(); // soap:Body writer.writeEndElement(); // soap:Envelope writer.flush(); writer.dispose(); }
/** * Initializes a new instance of the StreamingSubscriptionConnection class. * * @param service The ExchangeService instance this connection uses to connect to the server. * @param lifetime The maximum time, in minutes, the connection will remain open. Lifetime must be * between 1 and 30. * @throws Exception */ public StreamingSubscriptionConnection(ExchangeService service, int lifetime) throws Exception { EwsUtilities.validateParam(service, "service"); EwsUtilities.validateClassVersion( service, ExchangeVersion.Exchange2010_SP1, this.getClass().getName()); if (lifetime < 1 || lifetime > 30) { throw new ArgumentOutOfRangeException("lifetime"); } this.session = service; this.subscriptions = new HashMap<String, StreamingSubscription>(); this.connectionTimeout = lifetime; }
/** * Creates the WS-Security header necessary to send with an outgoing request. * * @param xmlWriter The XML writer to serialize the headers to. * @throws javax.xml.stream.XMLStreamException the xML stream exception */ @Override protected void serializeWSSecurityHeaders(XMLStreamWriter xmlWriter) throws XMLStreamException { EwsUtilities.EwsAssert( this.securityToken != null, "WSSecurityBasedCredentials.SerializeWSSecurityHeaders", "Security token cannot be null!"); // <wsu:Timestamp wsu:Id="_timestamp"> // <wsu:Created>2007-09-20T01:13:10.468Z</wsu:Created> // <wsu:Expires>2007-09-20T01:18:10.468Z</wsu:Expires> // </wsu:Timestamp> // String timestamp = null; if (this.addTimestamp) { Calendar utcNow = Calendar.getInstance(); utcNow.add(Calendar.MINUTE, 5); timestamp = String.format(WSSecurityBasedCredentials.wsuTimeStampFormat, utcNow, utcNow); } // Format the WS-Security header based on all the information we have. String wsSecurityHeader = String.format( WSSecurityBasedCredentials.wsSecurityHeaderFormat, timestamp + this.securityToken); // And write the header out... xmlWriter.writeCharacters(wsSecurityHeader); }
/** * * Writes elements and content to XML. * * @param writer the writer * @throws Exception the exception */ @Override protected void writeElementsToXml(EwsServiceXmlWriter writer) throws Exception { super.writeElementsToXml(writer); // ExchangeVersion ev=writer.getService().getRequestedServerVersion(); if (writer.getService().getRequestedServerVersion().ordinal() > ExchangeVersion.Exchange2007_SP1.ordinal()) { writer.writeElementValue( XmlNamespace.Types, XmlElementNames.IsContactPhoto, this.isContactPhoto); } writer.writeStartElement(XmlNamespace.Types, XmlElementNames.Content); if (!(this.fileName == null || this.fileName.isEmpty())) { File fileStream = new File(this.fileName); FileInputStream fis = null; try { fis = new FileInputStream(fileStream); writer.writeBase64ElementValue(fis); } finally { if (fis != null) { fis.close(); } } } else if (this.contentStream != null) { writer.writeBase64ElementValue(this.contentStream); } else if (this.content != null) { writer.writeBase64ElementValue(this.content); } else { EwsUtilities.EwsAssert( false, "FileAttachment.WriteElementsToXml", "The attachment's content is not set."); } writer.writeEndElement(); }
/** * Validates this view. * * @param request the request * @throws ServiceVersionException the service version exception * @throws ServiceValidationException the service validation exception */ @Override protected void internalValidate(ServiceRequestBase request) throws ServiceVersionException, ServiceValidationException { super.internalValidate(request); EwsUtilities.validateEnumVersionValue( this.traversal, request.getService().getRequestedServerVersion()); }
/** * Initializes a new instance of the class. * * @param referenceItem the reference item * @throws Exception the exception */ protected SuppressReadReceipt(Item referenceItem) throws Exception { super(referenceItem.getService()); EwsUtilities.EwsAssert( referenceItem != null, "SuppressReadReceipt.ctor", "referenceItem is null"); referenceItem.throwIfThisIsNew(); this.referenceItem = referenceItem; }
/** * Initializes a new instance of the FindItemResponse class. * * @param isGrouped if set to true if grouped. * @param propertySet The property Set */ protected FindItemResponse(boolean isGrouped, PropertySet propertySet) { super(); this.isGrouped = isGrouped; this.propertySet = propertySet; EwsUtilities.EwsAssert( this.propertySet != null, "FindItemResponse.ctor", "PropertySet should not be null"); }
/** * * Adds the specified permissions to the collection. * * @param permissions the permissions * @throws Exception the exception */ public void addFolderRange(Iterator<FolderPermission> permissions) throws Exception { EwsUtilities.validateParam(permissions, "permissions"); if (null != permissions) { while (permissions.hasNext()) { this.add(permissions.next()); } } }
/** * Initializes a new instance of the class. * * @param userConfiguration the user configuration */ protected GetUserConfigurationResponse(UserConfiguration userConfiguration) { super(); EwsUtilities.EwsAssert( userConfiguration != null, "GetUserConfigurationResponse.ctor", "userConfiguration is null"); this.userConfiguration = userConfiguration; }
/** * Initializes a new instance of the StreamingSubscriptionConnection class. * * @param service The ExchangeService instance this connection uses to connect to the server. * @param subscriptions Iterable subcriptions * @param lifetime The maximum time, in minutes, the connection will remain open. Lifetime must be * between 1 and 30. * @throws Exception */ public StreamingSubscriptionConnection( ExchangeService service, Iterable<StreamingSubscription> subscriptions, int lifetime) throws Exception { this(service, lifetime); EwsUtilities.validateParamCollection(subscriptions.iterator(), "subscriptions"); for (StreamingSubscription subscription : subscriptions) { this.subscriptions.put(subscription.getId(), subscription); } }
/** * * Reads the SOAP fault. * * @param reader The reader. * @return SOAP fault details. */ private SoapFaultDetails readSoapFault(EwsXmlReader reader) { SoapFaultDetails soapFaultDetails = null; try { reader.read(); if (reader.getNodeType().getNodeType() == XMLNodeType.START_DOCUMENT) { reader.read(); } if (!reader.isStartElement() || (!reader.getLocalName().equals(XmlElementNames.SOAPEnvelopeElementName))) { return soapFaultDetails; } // Get the namespace URI from the envelope element and use it for // the rest of the parsing. // If it's not 1.1 or 1.2, we can't continue. XmlNamespace soapNamespace = EwsUtilities.getNamespaceFromUri(reader.getNamespaceUri()); if (soapNamespace == XmlNamespace.NotSpecified) { return soapFaultDetails; } reader.read(); // Skip SOAP header. if (reader.isStartElement(soapNamespace, XmlElementNames.SOAPHeaderElementName)) { do { reader.read(); } while (!reader.isEndElement(soapNamespace, XmlElementNames.SOAPHeaderElementName)); // Queue up the next read reader.read(); } // Parse the fault element contained within the SOAP body. if (reader.isStartElement(soapNamespace, XmlElementNames.SOAPBodyElementName)) { do { reader.read(); // Parse Fault element if (reader.isStartElement(soapNamespace, XmlElementNames.SOAPFaultElementName)) { soapFaultDetails = SoapFaultDetails.parse(reader, soapNamespace); } } while (!reader.isEndElement(soapNamespace, XmlElementNames.SOAPBodyElementName)); } reader.readEndElement(soapNamespace, XmlElementNames.SOAPEnvelopeElementName); } catch (Exception e) { // If response doesn't contain a valid SOAP fault, just ignore // exception and // return null for SOAP fault details. e.printStackTrace(); } return soapFaultDetails; }
/** * * Validates request. * * @throws Exception the exception */ @Override protected void validate() throws Exception { super.validate(); EwsUtilities.validateParam(this.getPropertySet(), "PropertySet"); if (this.getSyncFolderId() != null) { this.getSyncFolderId().validate(this.getService().getRequestedServerVersion()); } this.getPropertySet().validateForRequest(this, false /* summaryPropertiesOnly */); }
/** * Creates the WS-Addressing headers necessary to send with an outgoing request. * * @param xmlWriter The XML writer to serialize the headers to. * @param webMethodName The Web method being called. * @throws javax.xml.stream.XMLStreamException the xML stream exception */ private void serializeWSAddressingHeaders(XMLStreamWriter xmlWriter, String webMethodName) throws XMLStreamException { EwsUtilities.EwsAssert( webMethodName != null, "WSSecurityBasedCredentials.SerializeWSAddressingHeaders", "Web method name cannot be null!"); EwsUtilities.EwsAssert( this.ewsUrl != null, "WSSecurityBasedCredentials.SerializeWSAddressingHeaders", "EWS Url cannot be null!"); // Format the WS-Addressing headers. String wsAddressingHeaders = String.format( WSSecurityBasedCredentials.wsAddressingHeadersFormat, webMethodName, this.ewsUrl); // And write them out... xmlWriter.writeCharacters(wsAddressingHeaders); }
/** * Removes the specified streaming subscription from the connection. * * @param subscription The subscription to remove. * @throws Exception * @exception Thrown when RemoveSubscription is called while connected. */ public void removeSubscription(StreamingSubscription subscription) throws Exception { this.throwIfDisposed(); EwsUtilities.validateParam(subscription, "subscription"); this.validateConnectionState(false, Strings.CannotRemoveSubscriptionFromLiveConnection); synchronized (this) { this.subscriptions.remove(subscription.getId()); } }
/** * Adds a subscription to this connection. * * @param subscription The subscription to add. * @throws Exception * @exception Thrown when AddSubscription is called while connected. */ public void addSubscription(StreamingSubscription subscription) throws Exception { this.throwIfDisposed(); EwsUtilities.validateParam(subscription, "subscription"); this.validateConnectionState(false, Strings.CannotAddSubscriptionToLiveConnection); synchronized (this) { if (this.subscriptions.containsKey(subscription.getId())) { return; } this.subscriptions.put(subscription.getId(), subscription); } }
/** * * Writes elements to XML. * * @param writer accepts EwsServiceXmlWriter * @throws Exception throws Exception */ @Override protected void writeElementsToXml(EwsServiceXmlWriter writer) throws Exception { if (this.offset != null) { writer.writeElementValue( XmlNamespace.Types, XmlElementNames.Offset, EwsUtilities.getTimeSpanToXSDuration(this.getOffset())); } if (this.recurrence != null) { this.recurrence.writeToXml(writer, XmlElementNames.RelativeYearlyRecurrence); } if (this.absoluteDate != null) { writer.writeElementValue( XmlNamespace.Types, XmlElementNames.AbsoluteDate, EwsUtilities.dateTimeToXSDate(this.getAbsoluteDate())); } if (this.time != null) { writer.writeElementValue(XmlNamespace.Types, XmlElementNames.Time, this.getTime().toXSTime()); } }
/** * Writes the property value to XML. * * @param writer accepts EwsServiceXmlWriter * @param propertyBag accepts PropertyBag * @param isUpdateOperation accepts boolean whether the context is an update operation. * @throws Exception throws Exception */ protected void writePropertyValueToXml( EwsServiceXmlWriter writer, PropertyBag propertyBag, boolean isUpdateOperation) throws Exception { Object value = propertyBag.getObjectFromPropertyDefinition(this); if (value != null) { writer.writeStartElement(XmlNamespace.Types, getXmlElement()); // No need of changing the date time zone to UTC as Java takes // default timezone as UTC Date dateTime = (Date) value; writer.writeValue(EwsUtilities.dateTimeToXSDateTime(dateTime), getName()); writer.writeEndElement(); } }
/** * GetXmlElementName retrieves the XmlElementName of this type based on the EwsObjectDefinition * attribute that decorates it, if present. * * @return The XML element name associated with this type. */ protected String getXmlElementName() { if (this.isNullOrEmpty(this.xmlElementName)) { this.xmlElementName = this.getXmlElementNameOverride(); if (this.isNullOrEmpty(this.xmlElementName)) { synchronized (this.lockObject) { ServiceObjectDefinition annotation = this.getClass().getAnnotation(ServiceObjectDefinition.class); if (null != annotation) { this.xmlElementName = annotation.xmlElementName(); } } } } EwsUtilities.EwsAssert( !isNullOrEmpty(this.xmlElementName), "EwsObject.GetXmlElementName", String.format( "The class %s does not have an " + "associated XML element name.", this.getClass().getName())); return this.xmlElementName; }
/** * * Tries to read element from XML. * * @param reader accepts EwsServiceXmlReader * @return True if element was read * @throws Exception throws Exception */ @Override protected boolean tryReadElementFromXml(EwsServiceXmlReader reader) throws Exception { if (reader.getLocalName().equalsIgnoreCase(XmlElementNames.Offset)) { this.offset = EwsUtilities.getXSDurationToTimeSpan(reader.readElementValue()); return true; } else if (reader.getLocalName().equalsIgnoreCase(XmlElementNames.RelativeYearlyRecurrence)) { this.recurrence = new TimeChangeRecurrence(); this.recurrence.loadFromXml(reader, reader.getLocalName()); return true; } else if (reader.getLocalName().equalsIgnoreCase(XmlElementNames.AbsoluteDate)) { SimpleDateFormat sdfin = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Date tempDate = sdfin.parse(reader.readElementValue()); this.absoluteDate = tempDate; return true; } else if (reader.getLocalName().equalsIgnoreCase(XmlElementNames.Time)) { SimpleDateFormat sdfin = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Date tempDate = sdfin.parse(reader.readElementValue()); this.time = new Time(tempDate); return true; } else { return false; } }
/** * Validate the request. * * @throws ServiceLocalException the service local exception * @throws Exception the exception */ @Override protected void validate() throws ServiceLocalException, Exception { super.validate(); EwsUtilities.validateNonBlankStringParam(this.getSubscriptionId(), "SubscriptionId"); }
/** * Validates request. * * @throws Exception * @throws ServiceLocalException */ @Override protected void validate() throws ServiceLocalException, Exception { super.validate(); EwsUtilities.validateParam(this.getFolderIds(), "FolderIds"); this.getFolderIds().validate(this.getService().getRequestedServerVersion()); }
@Test public void testGetItemTypeFromXmlElementName() { assertEquals(Task.class, EwsUtilities.getItemTypeFromXmlElementName("Task")); assertEquals(EmailMessage.class, EwsUtilities.getItemTypeFromXmlElementName("Message")); assertEquals(PostItem.class, EwsUtilities.getItemTypeFromXmlElementName("PostItem")); assertEquals(SearchFolder.class, EwsUtilities.getItemTypeFromXmlElementName("SearchFolder")); assertEquals(Conversation.class, EwsUtilities.getItemTypeFromXmlElementName("Conversation")); assertEquals(Folder.class, EwsUtilities.getItemTypeFromXmlElementName("Folder")); assertEquals( CalendarFolder.class, EwsUtilities.getItemTypeFromXmlElementName("CalendarFolder")); assertEquals( MeetingMessage.class, EwsUtilities.getItemTypeFromXmlElementName("MeetingMessage")); assertEquals(Contact.class, EwsUtilities.getItemTypeFromXmlElementName("Contact")); assertEquals(Item.class, EwsUtilities.getItemTypeFromXmlElementName("Item")); assertEquals(Appointment.class, EwsUtilities.getItemTypeFromXmlElementName("CalendarItem")); assertEquals( ContactsFolder.class, EwsUtilities.getItemTypeFromXmlElementName("ContactsFolder")); assertEquals( MeetingRequest.class, EwsUtilities.getItemTypeFromXmlElementName("MeetingRequest")); assertEquals(TasksFolder.class, EwsUtilities.getItemTypeFromXmlElementName("TasksFolder")); assertEquals( MeetingCancellation.class, EwsUtilities.getItemTypeFromXmlElementName("MeetingCancellation")); assertEquals( MeetingResponse.class, EwsUtilities.getItemTypeFromXmlElementName("MeetingResponse")); assertEquals( ContactGroup.class, EwsUtilities.getItemTypeFromXmlElementName("DistributionList")); }