Saturday, 29 March 2014

Show private documents to your users from google drive without sharing them

In this post I will write how we can use google api to show content on our page from google drive, without actually changing the sharing settings for the document. We will use owner's oAuth access to proxy the content through our server.

I am using google drive sdk for java in this code snippet:
Given the fileId of a file uploaded on google drive we can query google to get the following info about file:

Alternate link: This is the link to file where the document can be accessed on google drive itself.
Download url : Link to download the original file. This is available only for the contetn uploaded on google drive. It is not available for the content created on google drive like google doc, google spreadsheet, google drive etc.
Export Links: links to export the documents in different formats. These links are only available for the content created on google drive like google doc, google spreadsheet, google drive etc. and are not available for content uploaded on google drive.

All of these links require google authentication and our end user doesn't have access to these documents. We are considering that our web application has been authorized(using oAuth) to access these documents by the document owner. Given these links we can make authenticated get request to these links on owner's behalf(by using access token) and serve this content to our end user by writing it to output stream. To make the authenticated get call we will use following code: Note that we are not reading all content in controller method in one go and then serving it, but we are actually buffering it in 8k chunks.

URL url = new URL(contentUrl)
URLConnection connection = url.openConnection();
connection.setRequestProperty("Authorization", 'Bearer ' + accessToken);

// this will actually buffer/stream the file in 8k chunks instead of reading the entire file into memory., response.outputStream)
Controller method to proxy the data and serve it on our page:
private static HttpTransport httpTransport = new NetHttpTransport()
private static JacksonFactory jsonFactory = new JacksonFactory();

//This is a controller method in our web-app
def viewGDOC(){
  String fileId = params.fileId
  String accessToken // read this from database using session
  String refreshToken // read this from database using session
  GoogleCredential gCredentials = new GoogleCredential.Builder()
                .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
  Drive driveService = new Drive.Builder(httpTransport, jsonFactory, credentials).build(); file = driveService.files().get(chapter.gDriveFileId).execute()
  String contentUrl
  def downloadUrl = file.getDownloadUrl()
  def pdfUrl = file.getExportLinks()?.get("application/pdf")
  if (downloadUrl) {
      contentUrl = downloadUrl
  } else {
      contentUrl = pdfUrl
  URL url = new URL(contentUrl)
  URLConnection connection = url.openConnection();
  connection.setRequestProperty("Authorization", 'Bearer ' + googleCredential.accessToken);
  // this will actually buffer/stream the file in 8k chunks instead of reading the entire file into memory., response.outputStream)
Once we get the content stream the next task is to show it to user. Since browsers can display only some mime types like image, video, pdf, we can't directly show many file other types like MS word documents. To better understand this upload a office format file like ppt to google drive and use the above code to proxy the content. You will see that the browser will not render proxied content  properly but instead download a zip which contains raw structure of file.
The solution is to use some kind of viewer that takes this type of content and converts it to html so that browser can display it. Think how google shows word file when you view it in google drive ? There are many web apps which take the url of file and gives back the html. e.g. : viewer.js, crocodoc, google viewer. We will use the google viewer here:
So if you are using iframe on your page to show the file content instead of pointing iframs's src to /viewGDoc , point it to'serverName/viewGDoc'&embedded=true . Please note that if you are using iframe the 'embedded=true' option is necessary in the url as without this the content that google viewer returns is non iframable(X Frame Options is sameorigin in that case).

Saturday, 20 October 2012

Selenium Web Driver to test websites

In this post I will write how to use selenium web driver to test the websites.Selenium  web driver allows us to drive the browser from the code supporting many languages like java , python etc.While it takes a little setup for chrome and ie,it works with firefox without any setup.I am using java to drive firefox browser.

The main goal of this post is to share code on how to design the test suite to make the code most reusable and also to make the testing reliable irrespective of user's connection speed.On to the design now :

The basic approach to design a test suite should be to create methods which automate elementary tasks and then to use these methods from the testing class and assert the results.This way the code developed for automating elementary tasks can be used by any potential testing client.For example : for website testing, tasks like logging in,logout,using various features of site like create post in blogger etc. can be put in a common class.

The test suite I developed is to test a salesforce app , so I will provide methods which helps doing login,selecting apps from the top right,selecting tabs,asserting page title etc.

Also I have overridden the selenium web driver's default methods like findElement() and some others to wait them for given time for page to load before throwing element not found kind of exceptions resulting from incomplete page load.

Please set the variable TIMEOUT according to connection speed.The logic behind waiting till timeout is the recursive call of the methods each after 1 sec.The methods exit after TIMEOUT if the desired elements are not found.

Monday, 3 September 2012

salesforce lead conversion

This application allows user to search leads based on a keyword and giving fields to search in. User can then go to details page of returned leads, can select multiple leads and then can convert selected leads by providing an account name.  The Outcome will be an account, a contact for each lead and an opportunity for each lead. After successful conversion user will be redirected to newly created account’s detail page.

Video Url:

you can install the unmanaged package from :

App uses one visual force page and one controller :

VisualForce Page :

            Enter Keyword
Search Fields
Lead Displayed as Name(Company) , Follow the link to go to lead's Details page Enter Account Name

Controller :

Salesforce Mashup with google maps

This app helps user to plot  sales on the google map.The sales to be plotted can be filtered based on various criteria like location,size,type,date etc.Clicking on a sale on the map redirects to the corresponding account.
Final application looks like this :
you can install the unmanaged apex package from  here :

Use the following doc for deployment instructions :

Demo video:

The app uses only one visual force page and one controller

Visual Force page :

<apex:page sidebar="false" Controller="salesData" id="page">

    <apex:includeScript value="{!URLFOR($Resource.jquery, 'jquery-1.3.2.min.js')}"/>
    <apex:includeScript value="{!URLFOR($Resource.jquery, 'jquery-ui-1.7.2.custom.min.js')}"/>
    <apex:stylesheet value="{!URLFOR($Resource.jquery,'jquery-ui-1.7.2.custom.css')}"/>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">    
        //This will load as soon as the page is ready and will setup slider
                //to setup slider
                $("#slider-range").slider({ //This line creates a slider on the DIV specified, options are passed arguments,comma separated below
                range: true,
                min: 0, //Min value for slider
                max: 400, //Max value for slider
                values: [0,250], //initial values of slider
                slide: function(event, ui){ //function to execute on slide event
                  document.getElementById('{!$}').value = ui.values[0]; //hidden fields to pass slider values to controller 
                  document.getElementById('{!$}').value = ui.values[1];
                  $("#amountValue").html(ui.values[0] + ' - ' + ui.values[1]); //display slider value as text
                //to display initial values of slider
                $("#amountValue").html($("#slider-range").slider("values", 0) + ' - ' + $("#slider-range").slider("values", 1));
        #map_canvas {
        font-family: Arial;
        line-height:normal !important;
     <apex:form id="form">
         <apex:pageBlock mode="edit" id="block">
               <apex:pageMessages />
               <apex:pageBlockSection columns="7" id="values" >
                  <apex:pageBlockSectionItem >
                    <apex:outputlabel value="* Country"/>
                    <apex:selectList value="{!country}" multiselect="false" required="true" size="1">
                       <apex:selectOptions value="{!countries}"/>
                 <apex:pageBlockSectionItem >
                   <apex:outputlabel value="State"/>
                   <apex:inputtext value="{!state}"/> 
                 <apex:pageBlockSectionItem >  
                   <apex:outputlabel value="City"/>
                   <apex:inputtext value="{!city}"/> 
                 <apex:commandButton action="{! search}" value="Search" reRender="mymap" />  
                 <!-- pass slider values -->
                 <apex:inputhidden value="{!size_low}" id="size_low" /> 
                 <apex:inputhidden value="{!size_high}" id="size_high" />
                 <!-- pass address of clicked marker -->
                 <apex:inputhidden value="{!addressParameter}" id="addressParameter" /> 
               <apex:pageBlockSection title="Filter Results" columns="3">
                     <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Size"/>
                        <!-- This is where our slider will be -->
                        <div id="slider-range" style="font-size: 90%; margin-top: 0.5em;"></div>
                        <div id="amountValue" style="text-align: center;"></div>
                     <apex:pageBlockSectionItem >
                        <apex:outputlabel value="Begining Year"/>
                        <apex:selectList value="{!b_year}" multiselect="false" required="true" size="1">
                           <apex:selectOptions value="{!years}"/>
                     <apex:pageBlockSectionItem >
                        <apex:outputlabel value="End Year"/>
                        <apex:selectList value="{!e_year}" multiselect="false" required="true" size="1">
                           <apex:selectOptions value="{!years}"/>
                     <apex:pageBlockSectionItem >
                        <apex:outputlabel value="Type"/>
                        <apex:selectCheckboxes value="{!type}">
                           <apex:selectOptions value="{!items}"/>
          <apex:pageBlockSection columns="2">
                <apex:outputpanel id="mymap">
                  <div id="map_canvas"></div>
                  <script type="text/javascript">
                     var myOptions = {
                            mapTypeControl: false,
                            mapTypeId: google.maps.MapTypeId.ROADMAP
                     var geocoder = new google.maps.Geocoder();
                    //create map
                     var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
                    function putmarker(map,address,geocoder){
                       geocoder.geocode( { address: address}, function(results, status) {
                        if (status == google.maps.GeocoderStatus.OK && results.length) {
                          if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                              //center map
                             //create marker
                                var marker = new google.maps.Marker({
                                  position: results[0].geometry.location,
                                  map: map,
                             //add click listener on marker
                               google.maps.event.addListener(marker, 'click', function() {
                                  //change addressParameter on click
                                    refreshData(); //to rerender rightpane containing sales data of a location(invokes action function)
                  <apex:repeat value="{! salesAddress}" var="address">
                      <script> putmarker(map,"{!address}",geocoder);</script>
                <apex:dataList value="{! Sales}" var="sale" id="dataView">
                    <!-- extract Record Id(15 character long from begining) to make link and Text to display(from 20th character to end of string) --> 
                    <!-- Instance is auto corrected by if it is not ap1 -->
                        <apex:outputLink value="{!''+left(sale,15)}" target="_blank">
               <!-- Rerender Right pane on marker click -->
               <apex:actionFunction name="refreshData" action="{!refreshData}" rerender="dataView"/>
Controller :

Monday, 4 June 2012

Country database android app

we created this app for bitsmun . The app has database for many countries which includes various country details like area,population,military,politics,constitution taken from Cia-World factbook . This is an Html5 app created using phonegap. you may find the source code  at by viewing the source of webpage.

Android installer :

let me know in comments ,  if you want the source code of phonegap project and I will upload.

Sunday, 29 April 2012

Dynamic SQL query when number of fields in where clause in not known

suppose we have a front end where user can select different filters to narrow down the query results.In this case it is not already known which all fields user will select.
A dynamic query for this case can be constructed using this trick:

here fieldNames is the array containing names of all filters and fields is the array containing values provided by user.

Saturday, 28 April 2012 Eclipse plugin installation problem

Installation fails with error 'Md5 hash not as expected'
original error message :

An error occurred while collecting items to be installed session context was:(profile=SDKProfile, phase=org.eclipse.equinox.internal.p2.engine.phases.Collect, operand=, action=). Problems downloading artifact: osgi.bundle,com.salesforce.ide.api, MD5 hash is not as expected. Expected: 97a6329f82c422a61e9b1bc28be7cace and found ef8b1c2b63c7d04acaa6bf41f4b8570c.

this happens due to corrupted download of jars by eclipse.To fix the problem follow these steps:
1. download all jars manually from and
   you can use firefox plugin 'download them all' to download all links in one click.
2. put them in /features/ and /plugins/ directory of eclipse plugin installation again as you would do normally.It won't download the jars again and it will work without error.

original post: