Thursday, March 25, 2010

Consuming Exchange Web Service from Java

I had to get the Exchange Web Service (Exchange Version: 2010) APIs consumed from a Java program. Here is a set of preconditions, that I had, to start working on this.

  • Need to use Apache Axis2 web service stack, version 1.4
  • Consume mail and calendar APIs from Java

I did some research on any existing work for the same. I couldn’t find anything specific for Axis2. However, I found this great wiki page from imap2exchange project, which explains how to do it from Metro. In this blog series, I am trying to put together the sequence of actions I had to do to get the proxy code working from Axis2.

To get the email from user’s inbox, I had to do the following sequence.

  1. Setup your development environment
  2. Customize the WSDL & Schema to get Axis2 generate the proxy
  3. Fix compilation issues of Axis2 generated proxy
  4. Remove some namespace attaching in the request to avoid EWS complaining about the request format
  5. Edit the schema to resolve bugs in Axis2, regenerate proxy code and fix compilation issues

Back to blogging

Title could be little confusing. When I say back to blogging, I wasn’t blogging so frequently earlier. So that is probably a partially correct sentence. What I want to say is, I am starting to blog again now. :)

Wednesday, October 7, 2009

LINQ / Lambda Examples : Aggregate collection


///////////////////////////////////////////////////////////////////////////////
//
// Copyright © Schakra Inc. 2009. All rights reserved.
//

///////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Schakra.Samples.DotNet.Linq
{
///
/// Sample demonstrating aggregating content of a Collection.
///

public class LinqSample_02
{
public void AggregateRequestParameters()
{
// Generate Data.
Random random = new Random();
List uids = new List();
for (int i = 0; i < 10; i++)
{
uids.Add(random.Next(1, int.MaxValue));
}

// Had to use similar code snippet while working with Facebook APIs.
string uidString = uids.Aggregate(
string.Empty,
(s, uid) => string.Format("{0},{1}", s, uid.ToString())
// (s, uid) => s + "," + uid.ToString())
).Substring(1);

// Had to use similar code while working with OAuth signature creation.
SortedList oauthParams = new SortedList();

// Append all parameters in sorted order.
string sigBaseString = oauthParams.Aggregate(
string.Empty,
(s, kvp) => string.Format(
"{0}&{1}={2}", s, UrlEncode(kvp.Key), UrlEncode(kvp.Value))
).Substring(1);
}

private static Regex hexCharsExp = new Regex("%.{2}", RegexOptions.Compiled);

// Custom encoding in upper case for OAuth String.
private static string UrlEncode(string input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}

return hexCharsExp.Replace(
HttpUtility.UrlEncode(input), m => m.Value.ToUpper());
}
}
}

Tuesday, September 29, 2009

LINQ / Lambda Examples : Convert list of one type to another


///////////////////////////////////////////////////////////////////////////////
//
// Copyright © Schakra Inc. 2009. All rights reserved.
//

///////////////////////////////////////////////////////////////////////////////

using System.Collections.Generic;
using System.Linq;

namespace Schakra.Samples.DotNet.Linq
{
public class LinqSample_01
{
List OutlookContacts { get; set; }

public void ConvertListFromOneTypeToAnother()
{
List winLiveContacts =
OutlookContacts.ConvertAll(
contact => contact.ToWindowsLiveContact());

List outlookContacts =
winLiveContacts.ConvertAll(
contact => new OutlookContact()
{
Name = contact.DisplayName,
Phone = contact.MobilePhone
});

List outlookContacts2 =
(from contact in winLiveContacts
select new OutlookContact()
{
Name = contact.DisplayName,
Phone = contact.MobilePhone
}).ToList();
}
}

public class OutlookContact
{
public string Name { get; set; }

public string Phone { get; set; }

public WindowsLiveContact ToWindowsLiveContact()
{
return
new WindowsLiveContact()
{
DisplayName = this.Name,
MobilePhone = this.Phone
};
}
}

public class WindowsLiveContact
{
public string DisplayName { get; set; }

public string MobilePhone { get; set; }
}
}

Wednesday, July 22, 2009

BlackBerry File Explorer

As part of work, we had to generate code coverage report for the application on BlackBerry. We have used Cobertura for J2ME for generating the code coverage results. However, we had to develop a quick tool to transfer the coverage result file generated on the simulator to desktop. For this purpose, we have developed the file explorer / transfer tool for BlackBerry, which works on USB connection.

We would make this tool available tool for public download soon.

Sunday, July 12, 2009

Overriding ServerCertificateValidation

I found the following code very useful, when trying to debug https calls through fiddler. During this proxying, fiddler creates its own certificates, which will be failed by the programs. In order to override the behavior, while doing debugging, the following code could be used.


//using System.Net;
//using System.Security.Cryptography.X509Certificates;
//using System.Net.Security;

ServicePointManager.ServerCertificateValidationCallback +=
delegate(object sender, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};

Monday, May 18, 2009

Msbuild throwing exception for v3.5 target code

As part of my current project, I had to build solution which contained both C++ and C# projects. C# project was written for target version of .NET 3.5. Whenever, I tried msbuild from command prompt, it complained ‘must declare a body because it is not marked abstract or extern’ for the property declarations in my C# classes. I have done a lot of research (Thanks to Google) to find a solution for this.

1. I ensured that the csproj file contains the target framework version of 3.5

         <targetframeworkversion>v3.5</targetframeworkversion>

2. Tried to override version through msbuild parameters

         /p:Configuration=Debug;TargetFrameworkVersion=v3.5 /toolsversion:3.5 /property:TargetFrameworkVersion=v3.5 

3. Enabled diagnostic log information for msbuild and identified that csc.exe file from v2.0.50727 is picked up during the build (event though Microsoft.Common.targets is picked up from v3.5)

        /v:diag

4. Did some more search and found out this thread from msdn. So the problem was in my environment COMPLUS_VERSION was set to v2.0.50727

I fixed the problem by setting environment variable 'COMPLUS_VERSION' value to empty. (Since my project file was already set for v3.5, clearing the environment variable helped me. Otherwise, you will need to set it to v3.5)