Create Calander Appointment, EWS, Exchanger server with java stub
Posted by kalaimaan on February 10, 2009
The below example is explain how to create Calander appointment in exchange server using Java stub
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import net.adaptime.integration.EWSClient.Calendar.MsExchangeCalendarProperty;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.transport.http.HttpTransportProperties.Authenticator;
import org.apache.commons.httpclient.protocol.Protocol;
import com.microsoft.schemas.exchange.services._2006.messages.CreateItemDocument;
import com.microsoft.schemas.exchange.services._2006.messages.CreateItemResponseDocument;
import com.microsoft.schemas.exchange.services._2006.messages.CreateItemType;
import com.microsoft.schemas.exchange.services._2006.messages.ExchangeWebServiceStub;
import com.microsoft.schemas.exchange.services._2006.messages.ItemInfoResponseMessageType;
import com.microsoft.schemas.exchange.services._2006.types.BodyType;
import com.microsoft.schemas.exchange.services._2006.types.BodyTypeType;
import com.microsoft.schemas.exchange.services._2006.types.CalendarItemCreateOrDeleteOperationType;
import com.microsoft.schemas.exchange.services._2006.types.CalendarItemType;
import com.microsoft.schemas.exchange.services._2006.types.DistinguishedFolderIdNameType;
import com.microsoft.schemas.exchange.services._2006.types.DistinguishedFolderIdType;
import com.microsoft.schemas.exchange.services._2006.types.ImportanceChoicesType;
import com.microsoft.schemas.exchange.services._2006.types.NonEmptyArrayOfAllItemsType;
import com.microsoft.schemas.exchange.services._2006.types.ResponseClassType;
import com.microsoft.schemas.exchange.services._2006.types.TargetFolderIdType;
public class EWSCreateCalender
{
ExchangeWebServiceStub exService;
private static String NTLM_AUT = “_NTLM_DIGEST_BASIC_AUTHENTICATION_”;
private static String NTLM_PRO = “NTLM”;
private static String HTTPS = “https”;
String exchangeURL = null;
String bodyText;
String subject;
Calendar scal;
Calendar ecal;
String loc;
int minRBS;
private String itemId = null;
private String changeKey = null;
public EWSCreateCalender(String userName, String passWord)
{
String exchangeURL = “https://Manager/EWS/Exchange.asmx”;
try
{
exService = new ExchangeWebServiceStub(null, exchangeURL);
}
catch (AxisFault e)
{
e.printStackTrace();
}
checkAuthenticate(userName, passWord);
}
/**
*
*
*/
private void checkAuthenticate(String userName, String passWord)
{
Protocol AlcHttpsProtocol = new Protocol(HTTPS, new EWSSSLSocketFactory(), 443);
Protocol.registerProtocol(HTTPS, AlcHttpsProtocol);
Options options = exService._getServiceClient().getOptions();
Authenticator authenticator = new Authenticator();
List<String> authScheme = new ArrayList<String>();
authScheme.add(NTLM_PRO);
authenticator.setAuthSchemes(authScheme);
authenticator.setUsername(userName);
authenticator.setPassword(passWord);
authenticator.setHost(“Manager”);
authenticator.setDomain(exchangeURL);
authenticator.setPort(Integer.parseInt(“443″));
options.setProperty(NTLM_AUT, authenticator);
options.setUserName(userName);
exService._getServiceClient().setOptions(options);
}
/**
*
*/
public void createCalendarAppointment(String bodyText,String subject,Calendar scal, Calendar ecal,String loc,int minRBS)
{
this.bodyText = bodyText;
this.subject = subject;
this.scal = scal;
this.ecal = ecal;
this.loc = loc;
this.minRBS = minRBS;
// Create the appointment.
CalendarItemType appointment = CalendarItemType.Factory.newInstance();
// Create the CreateItem request.
CreateItemType createItemRequest = CreateItemType.Factory.newInstance();
appointment = prepareAppointment(appointment);
appointment.setItemClass(“IPM.Appointment”);
createItemRequest.setSendMeetingInvitations(CalendarItemCreateOrDeleteOperationType.SEND_TO_ALL_AND_SAVE_COPY);
// Identify the destination folder that will contain the appointment.
DistinguishedFolderIdType folder = DistinguishedFolderIdType.Factory.newInstance();
folder.setId(DistinguishedFolderIdNameType.CALENDAR);
// Create the array of items that will contain the appointment.
NonEmptyArrayOfAllItemsType arrayOfItems = NonEmptyArrayOfAllItemsType.Factory.newInstance();
CalendarItemType[] calender = new CalendarItemType[1];
calender[0] = CalendarItemType.Factory.newInstance();
arrayOfItems.setCalendarItemArray(calender);
arrayOfItems.setCalendarItemArray(0, appointment);
createItemRequest.setSavedItemFolderId(TargetFolderIdType.Factory.newInstance());
createItemRequest.getSavedItemFolderId().setDistinguishedFolderId(folder);
createItemRequest.setItems(arrayOfItems);
CreateItemDocument createItemDocument = CreateItemDocument.Factory.newInstance();
createItemDocument.setCreateItem(createItemRequest);
CreateItemResponseDocument createItemResponseDocument = null;
try
{
createItemResponseDocument = exService.CreateItem(createItemDocument, null, null, null, null);
}
catch (RemoteException e)
{
return;
}
ItemInfoResponseMessageType[] infoResponseMessageType = createItemResponseDocument.getCreateItemResponse()
.getResponseMessages().getCreateItemResponseMessageArray();
for (int i = 0; i < infoResponseMessageType.length; i++)
{
if (infoResponseMessageType[i].getResponseClass() == ResponseClassType.SUCCESS)
{
System.out.println(“Success”);
itemId = infoResponseMessageType[i].getItems().getCalendarItemArray()[0].getItemId().getId();
changeKey = infoResponseMessageType[i].getItems().getCalendarItemArray()[0].getItemId().getChangeKey();
System.out.println(“ItemId : ” + itemId);
System.out.println(“Change Id : ” + changeKey);
}
else if (infoResponseMessageType[i].getResponseClass() == ResponseClassType.ERROR)
System.out.println(“Error”);
}
}
/**
*
* @param appointment
* @param property
* @param type
* @return
*/
private CalendarItemType prepareAppointment(CalendarItemType appointment)
{
if (bodyText != null && bodyText.length() > 0)
{
appointment.setBody(BodyType.Factory.newInstance());
appointment.getBody().setBodyType(BodyTypeType.HTML);
appointment.getBody().setStringValue(bodyText);
}
appointment.setImportance(ImportanceChoicesType.NORMAL);
if (subject != null && subject.trim().length() > 0)
appointment.setSubject(subject);
else
return null;
if (scal != null)
appointment.setStart(scal);
else
return null;
if (ecal != null)
appointment.setEnd(ecal);
else
return null;
if (loc != null && loc.length() > 0)
appointment.setLocation(loc);
appointment.setReminderIsSet(true);
if (minRBS > MsExchangeCalendarProperty.remainder_Null)
{
appointment.setReminderIsSet(Boolean.TRUE);
appointment.setReminderMinutesBeforeStart(minRBS);
}
appointment.setImportance(ImportanceChoicesType.NORMAL);
return appointment;
}
public static void main(String[] args)
{
EWSCreateCalender calender = new EWSCreateCalender(“kalaimaan”, “test01.” );
Calendar cals = Calendar.getInstance();
Calendar cale = Calendar.getInstance();
cals.set(2009, 02, 11, 10,30);
cale.set(2009, 02, 11, 12,00);
calender.createCalendarAppointment(“sample boby test”, “Tomorrow morning we have scheduled for seminer, come without fail”, cals, cale, “Chennai”, 30);
}
}
Juergen said
hi sridhar,
great script. i want to try it but my java knowhow is very very less. where can I find the ewscreatecalender class.
greatings from germany
Juergen
kalaimaan said
you check my all post of exchange server, you dont want to search for a particular EWS.
Every thing is available in single Stub that is EWS stub. It will be create by our self.
I explained in my post how to install and how to create the stub and all, check the details and let me know
comrad said
@Juergen: Use Apache Axis2’s wsdl2java.