Kalaimaan

Doing simple things to make great life

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);
}
}

Advertisements

12 Responses to “Create Calander Appointment, EWS, Exchanger server with java stub”

  1. 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

  2. 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

  3. comrad said

    @Juergen: Use Apache Axis2’s wsdl2java.

  4. exchange2010 said

    Has anyone tried this with exchange 2010 beta. I am trying
    to make this work for exchange 2010. I have installed exchange
    2010 but when I try to access ews using https:///ews/exchange.asmx,
    I get the following instead of the Services.wsdl which is quite different from
    exchange 2007.

    “You have created a service.

    To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:

    svcutil.exe https:///EWS/Services.wsdl

    This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:

    C#

    class Test
    {
    static void Main()
    {
    HelloClient client = new HelloClient();

    // Use the ‘client’ variable to call operations on the service.

    // Always close the client.
    client.Close();
    }
    }

    Visual Basic

    Class Test
    Shared Sub Main()
    Dim client As HelloClient = New HelloClient()
    ‘ Use the ‘client’ variable to call operations on the service.

    ‘ Always close the client.
    client.Close()
    End Sub
    End Class

  5. Olindo said

    where i can find net.adaptime.integration.EWSClient.Calendar.MsExchangeCalendarProperty?

    TNX
    Olindo

    • kalaimaan said

      you r welcome
      Pls give some time i will check .. after long back i started again to post new and reply .. will answer u soon

  6. InJeNiErO said

    Hi,

    Thanks for thisp ost, it save my day.

    I used CXF to generate the proxies, there are some differences, specialy in the authentication and in the array tratement.

    CXF doesn’t support NTLM authentcation by itself. In java1.6 it is supported by the JRE, but in 1.5 you are lost. My only chance was to configure IIS to accept basic authentication.

  7. Assad said

    Hi I have seen your code but will you please tell me the required jar file for this code.
    Thanks

  8. nazare said

    К Евро-2012 во Львове, где состоятся несколько матчей турнира, построено ряд новых комфортабельных гостиниц. Маленькие гостиницы Львова легко вписались в архитектуру города.

  9. katadyn hiker pro…

    […]Create Calander Appointment, EWS, Exchanger server with java stub « Kalaimaan[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: