private HttpWebRequest buildEwsHttpWebRequest(HttpWebRequest request) throws Exception { try { service.traceHttpRequestHeaders(TraceFlags.EwsRequestHttpHeaders, request); ByteArrayOutputStream requestStream = (ByteArrayOutputStream) request.getOutputStream(); EwsServiceXmlWriter writer = new EwsServiceXmlWriter(service, requestStream); boolean needSignature = service.getCredentials() != null && service.getCredentials().isNeedSignature(); writer.setRequireWSSecurityUtilityNamespace(needSignature); writeToXml(writer); if (needSignature) { service.getCredentials().sign(requestStream); } service.traceXml(TraceFlags.EwsRequest, requestStream); return request; } catch (IOException e) { // Wrap exception. throw new ServiceRequestException(String.format("The request failed. %s", e.getMessage()), e); } }
/** * @param xmlNode XML node * @param xmlStreamWriter XML stream writer * @throws XMLStreamException the XML stream exception */ public static void writeNode(Node xmlNode, XMLStreamWriter xmlStreamWriter) throws XMLStreamException { if (xmlNode instanceof Element) { addElement((Element) xmlNode, xmlStreamWriter); } else if (xmlNode instanceof Text) { xmlStreamWriter.writeCharacters(xmlNode.getNodeValue()); } else if (xmlNode instanceof CDATASection) { xmlStreamWriter.writeCData(((CDATASection) xmlNode).getData()); } else if (xmlNode instanceof Comment) { xmlStreamWriter.writeComment(((Comment) xmlNode).getData()); } else if (xmlNode instanceof EntityReference) { xmlStreamWriter.writeEntityRef(xmlNode.getNodeValue()); } else if (xmlNode instanceof ProcessingInstruction) { ProcessingInstruction procInst = (ProcessingInstruction) xmlNode; xmlStreamWriter.writeProcessingInstruction(procInst.getTarget(), procInst.getData()); } else if (xmlNode instanceof Document) { writeToDocument((Document) xmlNode, xmlStreamWriter); } }
if (writer.isRequireWSSecurityUtilityNamespace()) { writer.writeAttributeValue("xmlns", EwsUtilities.WSSecurityUtilityNamespacePrefix, EwsUtilities.WSSecurityUtilityNamespace); 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); writer.getInternalWriter()); writer.writeElementValue(XmlNamespace.Autodiscover, XmlElementNames.RequestedServerVersion, this.service .getRequestedServerVersion().toString());
/** * Writes the attribute to XML. * * @param writer the writer * @throws ServiceXmlSerializationException the service xml serialization exception */ @Override public void writeAttributesToXml(EwsServiceXmlWriter writer) throws ServiceXmlSerializationException { // The Name attribute is only supported in Exchange 2010 and above. if (writer.getService().getRequestedServerVersion() != ExchangeVersion.Exchange2007_SP1) { writer.writeAttributeValue(XmlAttributeNames.Name, this.name); } writer.writeAttributeValue(XmlAttributeNames.Id, this.id); }
/** * Writes to XML. * * @param writer The writer * @throws Exception the exception */ public void writeToXml(EwsServiceXmlWriter writer) throws Exception { if (this.id == null || this.id.isEmpty()) { throw new Exception("The Id property must be set."); } writer.writeStartElement(XmlNamespace.Types, XmlElementNames.ExchangeImpersonation); writer.writeStartElement(XmlNamespace.Types, XmlElementNames.ConnectingSID); // For 2007 SP1, use PrimarySmtpAddress for type SmtpAddress String connectingIdTypeLocalName = (this.idType == ConnectingIdType.SmtpAddress) && (writer.getService().getRequestedServerVersion() == ExchangeVersion.Exchange2007_SP1) ? XmlElementNames.PrimarySmtpAddress : this.getIdType().toString(); writer.writeElementValue(XmlNamespace.Types, connectingIdTypeLocalName, this.id); writer.writeEndElement(); // ConnectingSID writer.writeEndElement(); // ExchangeImpersonation }
/** * Writes the element value. * * @param xmlNamespace the XML namespace * @param localName the local name of the element * @param displayName the name that should appear in the exception message when the value can not be serialized * @param value the value * @throws XMLStreamException the XML stream exception * @throws ServiceXmlSerializationException the service xml serialization exception */ public void writeElementValue(XmlNamespace xmlNamespace, String localName, String displayName, Object value) throws XMLStreamException, ServiceXmlSerializationException { String stringValue = null; OutParam<String> strOut = new OutParam<String>(); if (this.tryConvertObjectToString(value, strOut)) { stringValue = strOut.getParam(); if (null != stringValue) { // allow an empty string to create an empty element (like <Value // />). this.writeStartElement(xmlNamespace, localName); this.writeValue(stringValue, displayName); this.writeEndElement(); } } else { throw new ServiceXmlSerializationException(String.format( "Values of type '%s' can't be used for the '%s' element.", value.getClass() .getName(), localName)); } }
if (writer.getService().getRequestedServerVersion() != ExchangeVersion.Exchange2007_SP1) { if (this.periods.size() > 0) { writer.writeStartElement(XmlNamespace.Types, XmlElementNames.Periods); writer.writeEndElement(); // Periods writer.writeStartElement(XmlNamespace.Types, XmlElementNames.TransitionsGroups); for (int i = 0; i < this.transitionGroups.size(); i++) { this.transitionGroups.get(key[i]).writeToXml(writer); writer.writeEndElement(); // TransitionGroups writer.writeStartElement(XmlNamespace.Types, XmlElementNames.Transitions); writer.writeEndElement(); // Transitions
/** * Writes the attribute value. Optionally emits empty string values. * * @param localName the local name of the attribute. * @param alwaysWriteEmptyString always emit the empty string as the value. * @param value the value * @throws ServiceXmlSerializationException the service xml serialization exception */ public void writeAttributeValue(String localName, boolean alwaysWriteEmptyString, Object value) throws ServiceXmlSerializationException { OutParam<String> stringOut = new OutParam<String>(); String stringValue = null; if (this.tryConvertObjectToString(value, stringOut)) { stringValue = stringOut.getParam(); if ((null != stringValue) && (alwaysWriteEmptyString || (stringValue.length() != 0))) { this.writeAttributeString(localName, stringValue); } } else { throw new ServiceXmlSerializationException(String.format( "Values of type '%s' can't be used for the '%s' attribute.", value.getClass() .getName(), localName)); } }
/** * Writes elements to XML. * * @param writer the writer * @throws Exception the exception */ @Override public void writeElementsToXml(EwsServiceXmlWriter writer) throws Exception { writer.writeElementValue(XmlNamespace.Types, XmlElementNames.Name, this .getName()); writer.writeElementValue(XmlNamespace.Types, XmlElementNames.ContentType, this.getContentType()); writer.writeElementValue(XmlNamespace.Types, XmlElementNames.ContentId, this.getContentId()); writer.writeElementValue(XmlNamespace.Types, XmlElementNames.ContentLocation, this.getContentLocation()); if (writer.getService().getRequestedServerVersion().ordinal() > ExchangeVersion.Exchange2007_SP1 .ordinal()) { writer.writeElementValue(XmlNamespace.Types, XmlElementNames.IsInline, this.getIsInline()); } }
/** * Writes to XML. * * @param writer the writer * @throws XMLStreamException the XML stream exception * @throws ServiceXmlSerializationException the service xml serialization exception */ public void writeToXml(EwsServiceXmlWriter writer) throws XMLStreamException, ServiceXmlSerializationException { if (writer.getService().getRequestedServerVersion() == ExchangeVersion.Exchange2007_SP1) { AppointmentSchema.MeetingTimeZone.writeToXml(writer); } else { super.writeToXml(writer); } }
/** * @param document XML document * @param xmlStreamWriter XML stream writer * @throws XMLStreamException the XML stream exception */ public static void writeToDocument(Document document, XMLStreamWriter xmlStreamWriter) throws XMLStreamException { xmlStreamWriter.writeStartDocument(); Element rootElement = document.getDocumentElement(); addElement(rootElement, xmlStreamWriter); xmlStreamWriter.writeEndDocument(); }
/** * Writes to XML. * * @param writer the writer * @param propertyBag the property bag * @param isUpdateOperation the is update operation * @throws Exception the exception */ public void writePropertyValueToXml(EwsServiceXmlWriter writer, PropertyBag propertyBag, boolean isUpdateOperation) throws Exception { TimeZoneDefinition timeZoneDefinition = propertyBag.getObjectFromPropertyDefinition(this); if (timeZoneDefinition != null) { // We emit time zone property only if we have not emitted the time // zone SOAP header // or if this time zone is different from that of the service // through which the request // is being emitted. if (!writer.isTimeZoneHeaderEmitted())// || value != // writer.getService().getTimeZone()) { timeZoneDefinition.writeToXml(writer, this.getXmlElement()); } } }
super.writeElementsToXml(writer); if (writer.getService().getRequestedServerVersion().ordinal() > ExchangeVersion.Exchange2007_SP1 .ordinal()) { writer.writeElementValue(XmlNamespace.Types, XmlElementNames.IsContactPhoto, this.isContactPhoto); writer.writeStartElement(XmlNamespace.Types, XmlElementNames.Content); try { fis = new FileInputStream(fileStream); writer.writeBase64ElementValue(fis); } finally { if (fis != null) { writer.writeBase64ElementValue(this.contentStream); } else if (this.content != null) { writer.writeBase64ElementValue(this.content); } else { EwsUtilities writer.writeEndElement();
/** * Writes the bag's property to XML. * * @param writer The writer to write the property to. * @throws Exception the exception */ public void writeToXml(EwsServiceXmlWriter writer) throws Exception { writer.writeStartElement(XmlNamespace.Types, this.getOwner() .getXmlElementName()); Iterator<PropertyDefinition> it = this.getOwner().getSchema() .iterator(); while (it.hasNext()) { PropertyDefinition propertyDefinition = it.next(); // The following test should not be necessary since the property bag // prevents // property to be set if they don't have the CanSet flag, but it // doesn't hurt... if (propertyDefinition .hasFlag(PropertyDefinitionFlags.CanSet, writer.getService().getRequestedServerVersion())) { if (this.contains(propertyDefinition)) { propertyDefinition.writePropertyValueToXml(writer, this, false /* isUpdateOperation */); } } } writer.writeEndElement(); }
/** * Writes the element value. * * @param xmlNamespace the XML namespace * @param localName the local name of the element * @param displayName the name that should appear in the exception message when the value can not be serialized * @param value the value * @throws XMLStreamException the XML stream exception * @throws ServiceXmlSerializationException the service xml serialization exception */ public void writeElementValue(XmlNamespace xmlNamespace, String localName, String displayName, Object value) throws XMLStreamException, ServiceXmlSerializationException { String stringValue = null; OutParam<String> strOut = new OutParam<String>(); if (this.tryConvertObjectToString(value, strOut)) { stringValue = strOut.getParam(); if (null != stringValue) { // allow an empty string to create an empty element (like <Value // />). this.writeStartElement(xmlNamespace, localName); this.writeValue(stringValue, displayName); this.writeEndElement(); } } else { throw new ServiceXmlSerializationException(String.format( "Values of type '%s' can't be used for the '%s' element.", value.getClass() .getName(), localName)); } }
/** * Writes the attribute to XML. * * @param writer accepts EwsServiceXmlWriter * @throws ServiceXmlSerializationException throws ServiceXmlSerializationException */ @Override public void writeAttributesToXml(EwsServiceXmlWriter writer) throws ServiceXmlSerializationException { super.writeAttributesToXml(writer); if (writer.getService().getRequestedServerVersion().ordinal() > ExchangeVersion.Exchange2007_SP1 .ordinal()) { writer.writeAttributeValue(XmlAttributeNames.Name, this .getEmailAddress().getName()); writer.writeAttributeValue(XmlAttributeNames.RoutingType, this .getEmailAddress().getRoutingType()); if (this.getEmailAddress().getMailboxType() != MailboxType.Unknown) { writer.writeAttributeValue(XmlAttributeNames.MailboxType, this .getEmailAddress().getMailboxType()); } } }
/** * Writes the attribute value. * * @param namespacePrefix the namespace prefix * @param localName the local name of the attribute * @param value the value * @throws ServiceXmlSerializationException the service xml serialization exception */ public void writeAttributeValue(String namespacePrefix, String localName, Object value) throws ServiceXmlSerializationException { OutParam<String> stringOut = new OutParam<String>(); String stringValue = null; if (this.tryConvertObjectToString(value, stringOut)) { stringValue = stringOut.getParam(); if (null != stringValue && !stringValue.isEmpty()) { this.writeAttributeString(namespacePrefix, localName, stringValue); } } else { throw new ServiceXmlSerializationException(String.format( "Values of type '%s' can't be used for the '%s' attribute.", value.getClass() .getName(), localName)); } }
/** * Write property to XML. * * @param writer the writer * @throws Exception the exception */ @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); } }
} else if (dictionaryObject instanceof Date) { dictionaryObjectType = UserConfigurationDictionaryObjectType.DateTime; valueAsString = writer.getService() .convertDateTimeToUniversalDateTimeString( (Date) dictionaryObject);
/** * @param document XML document * @param xmlStreamWriter XML stream writer * @throws XMLStreamException the XML stream exception */ public static void writeToDocument(Document document, XMLStreamWriter xmlStreamWriter) throws XMLStreamException { xmlStreamWriter.writeStartDocument(); Element rootElement = document.getDocumentElement(); addElement(rootElement, xmlStreamWriter); xmlStreamWriter.writeEndDocument(); }