Friday, June 29, 2012

Microsoft EWS Managed Java API Installation from a POB recipe

It's been a year since I posted about EWS Exchange API and I wanted to give the managed API a try again.

For some reason Microsoft is still reluctant to put their EWS Managed Java API in a repository from where we can check out the source code. The ant script is buggy (won't build out of the box from command line) and even though at the time of this writing the version is 1.1.5 it still generates the resulting jar file as 1.1.0.

With the spirit of automation I am providing here a Plain Old Bash Recipe to build the project. The only thing that you have to do manually is download the zipped file as Microsoft enforces a license agreement every time you try to download (Again no real versioning repository exists with the project content). After running this recipe you will end up with the correct version number in the jar file. No need for Eclipse, just command prompt and ready you are to upload the artifact to your Maven repo.
#!/usr/bin/env bash -ex
#
# @author Nestor Urquiza
#
# Builds Microsoft EWS Managed Java API
# 

EWSJavaAPI_BIN=$1
: ${EWSJavaAPI_BIN:?"Usage: $(basename $0) </path/to/EWSJavaAPI.zip>"}

cp -f $EWSJavaAPI_BIN .
fileNameAndExtension=$(basename "$EWSJavaAPI_BIN")
extension="${fileNameAndExtension##*.}"
fileName="${fileNameAndExtension%.*}"
rm -fR $fileName
unzip $fileNameAndExtension

cd $fileName
sed -i '' 's!<javac srcdir="${src}" destdir="${build}"/>! \
              <javac srcdir="${src}" destdir="${build}"> \
                <compilerarg value="-Xlint"/><classpath> \
                <fileset dir="${basedir}/lib"> \
                    <include name="**/*.jar" /> \
                </fileset> \
                </classpath> \
              </javac>!g' build.xml


cd lib
curl -O http://repo1.maven.org/maven2/commons-codec/commons-codec/1.4/commons-codec-1.4.jar
curl -O http://repo1.maven.org/maven2/commons-httpclient/commons-httpclient/3.1/commons-httpclient-3.1.jar
curl -O http://repo1.maven.org/maven2/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
curl -O http://repo1.maven.org/maven2/jcifs/jcifs/1.3.17/jcifs-1.3.17.jar
cd ../
ant
mv lib/EWSAPI-1.1.0.jar lib/$fileName.jar
I have taken the opportunity to update my project in Google Code repository which now shows a simple SendMail class that sends the members of a given distribution list.

Thursday, June 28, 2012

Test Email over TLS with a POB Recipe thanks to openssl and expect

We already saw how to test connectivity and send an email from a Plain Old Bash script (POB recipe). Here is a recipe to send it over TLS (ideal to use remotely from Remoto-IT to make sure SMTP is working properly in the remote server).

Here is how to invoke the script using Gmail SMTP:
./smtp-tls-test.sh smtp.gmail.com 587  gmailUser@gmail.com gmailUser@gmail.com "From Openssl using authentication over TLS" "Just a test" gmailUser@gmail.com gmailUserPassword
Of course this is offered just as (if you wish) curiosity because mailx will do the work for us:
$ sudo apt install mailutils
$ echo "body" | mailx  -r "sender@sample.com" -s "subject" -S smtp="host:port" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="username" -S smtp-auth-password="password" -S ssl-verify=ignore "receiver@sample.com"

Send Email using TLS from Windows scripting like VBS

The "CDO.Message" used in VBS does not work for TLS. So if no SSL or anonymous access is allowed within your protected network you will be forced to use an external command. Here is when the SendEmail Open Source Project will help. Just download the executable and you will be able to use it to send emails over TLS. Below is an example on how to use it with gmail TLS:
C:\>c:\scripts\sendEmail.exe -f gmailUserAddress -t gmailUserAddress -u "from sendEmail using TLS" -m "Just Testing" -s smtp.gmail.com:587 -o tls=yes -xu gmailUserAddress
.com -xp gmailUserPassword
This is handy if you are trying to monitor your event logs like I have posted before.

encode from echo

Every once in a while I am caught by forgetting echo command sends a trailing new line character. Use "-n" switch every time the result of echo is piped as input data for openssl when encoding. Here is for example the difference illustrated while encoding base64 the word 'hello':
$ echo -n "hello" | openssl enc -base64
aGVsbG8=
However when decoding you must not use the swtich '-n':
$ echo "aGVsbG8=" | openssl base64 -d
hello

Wednesday, June 27, 2012

Send Email from Windows using VBS and optional SSL Authentication

As I have posted before you can configure Windows Servers to send alerts when they detect errors (commonly logged in the Event Log). If your internal SMTP enforces authentication (I hope using SSL) then the scripts will need to be changed a little bit just to add support for it.

Rather than modifying previous posted scripts I am sharing this time a script that will just send an email from VBS. Optionally you can use authentication if you just supply the credentials: A couple of examples showing how to use it to send internal (sampleDomain) non SSL authenticated emails and externals SSL autheticated ones (gmail)
C:\>cscript c:\scripts\events\sendEmail.vbs smtp.company.com 25 corporateUser@company.com anotherCorporateUser@company.com "testing smtp from windows vbs" "Just a test"
C:\>cscript c:\scripts\events\sendEmail.vbs smtp.gmail.com 465 gmailUser@gmail.com corporateUser@company.com "testing smtp from windows vbs" "Just a test" gmailUser@gmail.com gmaiUserPassword

Monday, June 25, 2012

SQL Optimization: Warehouse versus Realtime Reports versus Listing Pages

Sometimes a Real Time Report is assumed to be the equivalent of a listing page. However, while they both consume instant data, Real Time Reporting can take longer than a listing page. This is because a user will happily wait to receive the report run at certain time but waiting a minute for a web page to be rendered will not be a nice experience at all.

On the other hand Warehouse Reports are fast and in many cases even faster than listing pages because they run out of data sources that have been optimized for data retrieval (reads) instead of the regular data sources which are used for reads and writes. However Warehouse reports are valid only if a snapshot of the past is acceptable.

Of course what is fast, or what is old is relative and it is easy to mix Warehouse Reports, Real Time Reports and Web Listing Pages concepts.

From time to time I need to explain the difference to Business people and I thought like writing it down to avoid repeating myself.

There is a reason why we cannot have a transmitter and a receiver that will work in all electromagnetic frequencies and it is the metric called Gain–bandwidth product.

In short if you try to design for higher gain (power to amplify) you will need to compromise the bandwidth (the spectrum of frequencies you will be able to amplify). If you want to build a device that will be an AM/FM radio, a TV receiver, a cellular phone, a WIFI transceiver, a satellite multi frequency transceiver and more you will need multiple circuits (and multiple antennas, more power disipation etc) which will mean a bigger equipment. At least that rule is still there after I learned about the "Gain–bandwidth product" 25 years ago. Technology advances and circuit integration gets better, the space you need gets smaller but the amount of spectrum in use also grows and the demands for different services grows as well. No, you cannot have it all.

Knowing the limitations are important so you can make wise decisions in your architecture. It does not matter if it is hardware or software based.

Architecture is the art of designing a future product. For the product to be real the Architect must design under constraints making sure the client understand the cost involved in the construction. Usability, expected life, maintainability, and other metrics will decide the final cost of the product. As an architect you are the Chief of the Building Process.

How this applies to databases? Let us pick MySQL and think about a typical request: Allow me to search for X in [table A].[field1] where [table B].[field 1] is Y but order by [table C].[field 1]. Of course the three tables are related by keys.

Pretty quick you will find yourself inspecting the query plan (EXPLAIN) just to realize that you can cut in half or more query execution times just applying some indexes in the case you are lucky enough to get composite indexes that relate the filter (WHERE) and the sorting (ORDER BY) however even if you are lucky your query might still not satisfy in some cases the demands for speed.

Here is where the Gain-bandwidth product concept helps. Is it really that we want this listing page in our web interface? Is this long taking query based report to be run real time or actually we are OK with 1 hour old data? Can we at least cache it? Or is it OK to use a typical data-warehouse? Is it so crucial to have this response time that we should forget about normalization for some fields and impact then persistance? Can the query ask for more on the WHERE clause? Can the query be divided in the one to be used by regular employees which commonly are supposed to query for more specific data and a 1 day delay executive report? Too many questions and depending on answers to these and more the Architect should come up with a proposal that makes sense budget and user experience wise.

Business just needs to understand the cost and the Architect for sure will deploy the solution that makes sense for the Enterprise.

As with the "Gain–bandwidth product" there is a "Speed-Normalization product" and depending on the allowed compromise you could even end up deciding for a NoSQL Database approach. So in specifics let us analyze what we are trying to do with the attached query:

Test Email Server POB Recipe

Checking if the email server is working from a particular box is a useful test we need to do from time to time. It makes sense to include that test as part of deployment, monitoring, server configuration and more.

Here is a POB script that can be run locally (or remotely from Remoto-IT for example)

Here is how to call it:
./smtp-test.sh "mail.sample.com" 25 "nurquiza@sample.com" "nurquiza@sample.com" "test from bash" "This is just a test"
In case you need to use TLS check this out.

Followers