Showing posts with label Integration. Show all posts
Showing posts with label Integration. Show all posts

Tuesday, May 21, 2013

Integration with Dropbox including download and upload example.

Easy step to integrate the Drop Box with your applications.

1. Setting up the  dropbox account.

  1. Create a dropbox account https://www.dropbox.com/
  2. Login to the dropbox
  3. Change the current URL to https://www.dropbox.com/developers/apps/create
  4. Select app type as core and permission type as full dropbox.
  5. Here you can create a app where you get the app_key and app_secret
  6. This key will be used for the authentication.
2. Java code.
  1. You will need following jar files to be in classpath.
    1. dropbox-client-1.5.3.jar, httpclient-4.0.jar, httpcore-4.0.jar, json-simple-1.1.jar and commons-logging-1.1.jar

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.DropboxAPI.DropboxInputStream;
import com.dropbox.client2.DropboxAPI.Entry;
import com.dropbox.client2.exception.DropboxException;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.RequestTokenPair;
import com.dropbox.client2.session.WebAuthSession;
import com.dropbox.client2.session.Session.AccessType;

public class DropBoxAuthTest {

// App key & secret that Dropbox's developer website gives your app
// Key Received @ step 1.5
private static final String APP_KEY = "your_key";
// Secret Received @ step 1.5
private static final String APP_SECRET = "your_secret";
// Permission type created @ step 1.4
final static private AccessType ACCESS_TYPE = AccessType.DROPBOX;
private static DropboxAPI<WebAuthSession> mDBApi;

public static void main(String[] args) throws Exception {
  
// Initialize the goods/session
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
WebAuthSession session = new WebAuthSession(appKeys, ACCESS_TYPE);
// Initialize DropboxAPI object
mDBApi = new DropboxAPI<WebAuthSession>(session);
// Get ready for user input
Scanner input = new Scanner(System.in);
// Open file that stores tokens, MUST exist as a blank file
File tokensFile = new File("TOKENS");
System.out.println("Enter 'a' to authenticate, or 'r' to reauthentication, 'd' to download, 'u' to  upload ");
String command = input.next();
if(command.equals("a")){
try {
// Present user with URL to allow app access to Dropbox account on
System.out.println("Please go to this URL and hit \"Allow\": " + DBApi.getSession().getAuthInfo().url);
AccessTokenPair tokenPair = mDBApi.getSession().getAccessTokenPair();
// Wait for user to Allow app in browser
System.out.println("Finished allowing?  Enter 'next' if so: ");
if(input.next().equals("next")){
RequestTokenPair tokens = new RequestTokenPair(tokenPair.key,tokenPair.secret);
mDBApi.getSession().retrieveWebAccessToken(tokens);
PrintWriter tokenWriter = new PrintWriter(tokensFile);
tokenWriter.println(session.getAccessTokenPair().key);
tokenWriter.println(session.getAccessTokenPair().secret);
tokenWriter.close();
System.out.println("Authentication Successful!");
}
} catch (DropboxException e) {
e.printStackTrace();
}
} else if(command.equals("r")){
// Initiate Scanner to read tokens from TOKEN file
Scanner tokenScanner = new Scanner(tokensFile);    
String ACCESS_TOKEN_KEY = tokenScanner.nextLine();    // Read key
String ACCESS_TOKEN_SECRET = tokenScanner.nextLine(); // Read secret
tokenScanner.close(); //Close Scanner
//Re-auth
AccessTokenPair reAuthTokens = new AccessTokenPair(ACCESS_TOKEN_KEY,ACCESS_TOKEN_SECRET);
mDBApi.getSession().setAccessTokenPair(reAuthTokens);
System.out.println("Re-authentication Sucessful!");
//Run test command
System.out.println("Hello there, " + mDBApi.accountInfo().displayName);
} else if(command.equals("d")){
// Initiate Scanner to read tokens from TOKEN file
Scanner tokenScanner = new Scanner(tokensFile);       
String ACCESS_TOKEN_KEY = tokenScanner.nextLine();    // Read key
String ACCESS_TOKEN_SECRET = tokenScanner.nextLine(); // Read secret
tokenScanner.close(); //Close Scanner
//Re-auth
AccessTokenPair reAuthTokens = new AccessTokenPair(ACCESS_TOKEN_KEY,ACCESS_TOKEN_SECRET);
mDBApi.getSession().setAccessTokenPair(reAuthTokens);
Entry entries = mDBApi.metadata("/", 20, null, true, null);
for (Entry e: entries.contents) {
if(!e.isDeleted){
if(e.isDir){
System.out.println("Folder ---> " + e.fileName() );
} else {
// this will download the root level files.
System.out.println("File ---->" + e.fileName());
DropboxInputStream inputStream = mDBApi.getFileStream(e.path,null);
OutputStream out=new FileOutputStream(e.fileName());
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0)
out.write(buf,0,len);
      out.close();
      inputStream.close();
      System.out.println("File is created....");
}
}
}
} else if(command.equals("u")){
// Initiate Scanner to read tokens from TOKEN file
Scanner tokenScanner = new Scanner(tokensFile);    
String ACCESS_TOKEN_KEY = tokenScanner.nextLine();    // Read key
String ACCESS_TOKEN_SECRET = tokenScanner.nextLine(); // Read secret
tokenScanner.close(); //Close Scanner
//Re-auth
AccessTokenPair reAuthTokens = new AccessTokenPair(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET);
mDBApi.getSession().setAccessTokenPair(reAuthTokens);
// put Pic1.jpg file in the current directory.
File f = new File("Pic2.jpg");
InputStream inputStream = new FileInputStream(f);
mDBApi.putFile("/Photos/", inputStream, f.length(), null, null);
inputStream.close();
}
}
}



3. Execution
  1. When you run the above code, in the console you will asked to prompt for the options like a, r, d and u. 
  2. For the very first time you have to type  'a'. After typing 'a'  you will get an URL. Copy the URL and paste in browser and click on the allow button.
  3. Once this process is done you can run the same program again and try different options like 'd', 'u' and 'r' as per you wish.
4) Documentation
  1. Java api documentation is provided in the following location.    https://www.dropbox.com/static/developers/dropbox-java-sdk-1.5-docs/allclasses-noframe.html


Thursday, September 29, 2011

Extjs Applet Integration

         In recent years most of the systems are getting upgraded with the new User Interface and Extjs is once of the most widely used technology for the rich look and feel. In most of the domain mostly in DMS (Document Management System) upload and download functionality is provided. To achieve the performance most of the system uses applets for upload and downloading the content from the systems. One of the major problem with applet is they used different tags (applet, object and embed) for different browsers. I came across such issues so decided to write a blog on it.

To achieve the mention problem i am writing few steps that will be helpful to you guys.

1) Create a main.html page and in the same directory put the ext-base.js and ext-all.js files.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" src="ext-base.js"></script>
<script type="text/javascript" src="ext-all.js"></script>
<script type="text/javascript" src="applet.js"></script>
<script>
Ext.onReady(function(){
my.appletRenderer.getFileSelector();
my.appletRenderer.getUploadApplet();
});
</script>
</head>
<body scroll="no">
<div id="center">
<div id="x-desktop">
<div id="center-panel-container" style="background:transparent;"></div>
</div>
</div>
</body>
</html>



2) Create an applet.js file in the same directory with following contents.


my.appletRenderer = {

getFileSelector : function() {
if (new ActiveXObject("Scripting.Dictionary") != null) {
return this.getActiveXFileSelector();
} else {
return this.getSwingFileSelector();
}
},

getSwingFileSelector : function(){
if (Ext.isIE || Ext.isChrome) {
if (Ext.isIE8 ) {
return new Ext.Panel({
renderTo: 'center-panel-container',
height : 0,
html : '<object ' +
'id = "SwingFileSelectorApplet" ' +
'name="SwingFileSelectorApplet" ' +
'codebase="" ' +
'archive="" ' +
'code="" ' +
'cache_archive="" ' +
'cache_version="" ' +
'width="0" ' +
'height="0" ' +
'mayscript="true"  ' +
'bodyStyle="display:none;"> ' +
'</object>'
});
}else{
return new Ext.Panel({
renderTo: 'center-panel-container',
height : 0,
html : '<applet ' +
'id="SwingFileSelectorApplet" ' +
'name="SwingFileSelectorApplet" ' +
'codebase="" ' +
'archive="" ' +
'code="" ' +
'width=0 ' +
'height=0 ' +
'mayscript="true">' +
'<param name="cache_archive" value="" />'+
'<param name="cache_version" value="" />'+
'</applet>'
});
}
}else{
return new Ext.Panel({
renderTo: 'center-panel-container',
height : 0,
html : '<EMBED '
+ 'id="SwingFileSelectorApplet" '
+ 'name="SwingFileSelectorApplet" '
+ 'codebase="" '
+ 'archive="" '
+ 'code="" '
+ 'type="application/x-java-applet;version=1.4" '
+ 'cache_archive="" '
+ 'cache_version="" '
+ 'EMBEDDED="true" '
+ 'mayscript="true" '
+ 'width="0" '
+ 'height="0"> '
+ '<NOEMBED> No Java 2 SDK, Standard Edition v 1.4.1 support for APPLET!! </NOEMBED> '
+ '</EMBED>'
});
}
},

getActiveXFileSelector : function() {
return new Ext.Panel({
renderTo:'center-panel-container',
height : 0,
html : ' <object classid="" align="baseline" border="0">'
+ '<param name="LPKPath" value="">'
+ '</object>'
+ '<object classid="" codebase="" '
+ 'VIEWASTEXT name="fileDialog" id="CommonDialog1"><param NAME="CancelError" VALUE="0">'
+ '<param NAME="DialogTitle" VALUE="'+ fs.locale.write("docUpload", "selectFile") +'">'
+ '<param NAME="" VALUE=""><param NAME="" VALUE="">'
+'</object>'
});
},

getUploadApplet : function(){
if(Ext.isWindows){
return this.getWindowsUploadApplet();
}
else{
return this.getUnixUploadApplet();
}
},

getWindowsUploadApplet : function(){
if (Ext.isIE) {
if (Ext.isIE8 ) {
return new Ext.Panel({
renderTo:'center-panel-container',
height : 0,
html : '<object '
+ 'id = "FileUpload" '
+ 'name="FileUpload" '
+ 'codebase="" '
+ 'archive="" '
+ 'code="" '
+ 'width=0 '
+ 'height=0 '
+ 'mayscript="true" '
+ 'bodyStyle="display:none;">'
+ '<param name="cache_archive" value="" />'
+ '<param name="cache_version" value="" />'
+ '</object>'
});
}else{
return new Ext.Panel({
renderTo:'center-panel-container',
height : 0,
html : '<applet '
+ 'id = "FileUpload" '
+ 'name="FileUpload" '
+ 'codebase="" '
+ 'archive="" '
+ 'code="" '
+ 'width=0 '
+ 'height=0 '
+ 'mayscript="true" '
+ 'bodyStyle="display:none;">'
+ '<param name="cache_archive" value="" />'
+ '<param name="cache_version" value="" />'
+ '</applet>'
});
}
}else if (Ext.isChrome) {
return new Ext.Panel({
renderTo:'center-panel-container',
height : 0,
html : '<applet '
+ 'id = "FileUpload" '
+ 'name="FileUpload" '
+ 'codebase="" '
+ 'archive="" '
+ 'code="" '
+ 'width=0 '
+ 'height=0 '
+ 'mayscript="true" '
+ 'bodyStyle="display:none;">'
+ '<param name="cache_archive" value="" />'
+ '<param name="cache_version" value="" />'
+ '</applet>'
});
}else{
return new Ext.Panel({
renderTo: 'center-panel-container',
height : 0,
html : '<EMBED ' +
'id="FileUpload" ' +
'name="FileUpload" ' +
'codebase="" ' +
'archive="" ' +
'code="" ' +
'type="application/x-java-applet;version=1.4" ' +
'cache_version="" ' +
'cache_archive="" ' +
'width="0" ' +
'height="0" ' +
'mayscript="true"> ' +
'<NOEMBED> No Java 2 SDK, Standard Edition v 1.4.1 support for APPLET!! </NOEMBED>'+
'</EMBED>'
});
}
},

getUnixUploadApplet : function(){

return new Ext.Panel({
renderTo:'center-panel-container',
height : 0,
html : '<applet '
+ 'id = "FileUpload" '
+ 'name="FileUpload" '
+ 'codebase="" '
+ 'archive="" '
+ 'code="" '
+ 'width=0 '
+ 'height=0 '
+ 'mayscript="true" '
+ 'bodyStyle="display:none;">'
+ '<param name="cache_archive" value="" />'
+ '<param name="cache_version" value="" />'
+ '</applet>'
});
}

}




3) In the applet.js file if you pass the proper values of the attribute which i have kept blank like codebase, archive,code,classid etc. the applets will get render.

4) View the main.html and view source the applets be enabled.

5) And this code will work for almost all the browser and OS. (Only you need to create your own jar files operating system specific)

6) Same check can be applied for the download and other applet.