By Daniel Du
In part3, part 4 and part 5 we introduced how to list the DWG attachments and how to debug in force.com application. in this post, we will go ahead to transfer the DWG attachment to AutoCAD WS storage.
AutoCAD WS announced its API. Currently it is still the fist step, includes connect to AutoCAD WS account, manipulate files on AutoCAD WS storage, and launch a drawing in WS online editor. These APIs rely on the industry standard http based WebDAV protocol which can be used in a variety of programming languages such as C#, C++, Java and JavaScript to access AutoCAD WS’s functionality from within web browsers, desktop applications and server-side components. Today, we will do it in Apex code.
AutoCAD WS team produced two tutorials with full source code samples showing how to use the new set of APIs using either C# or JavaScript. It may helps you to understand the mechanism so that we can create a similar one in Apex.
For our requirement, to transfer DWG attachments to AutoCAD WS and open it in WS online editor, I would not implement all functions like the one in C# or Javascript, Anther difference is the sample tutorial is trying to upload files from local drive to AutoCAD WS storage, it does apply to our scenario. What we want to do is, transfer files from one cloud to another, so we need to this part ourselves.
I created a class named as WebdavManager_WebdavClient. The constructor of the WebdavClient class expects three arguments:
- url: The Webdav server addres. For example, if you want to connect to Autocad WS server, use: ‘https://dav.autocadws.com’
- username: the username of your account in the Webdav server
- password: the password of your account in the Webdav server
The following code creates new WebdavClient class, connecting to Autocad WS Webdav server, with the username ‘someuser’ and the password ’123456′.
WebdavManager_WebdavClient client = new WebdavManager_WebdavClient( 'https://dav.autocadws.com/', 'someuser', '123456');
Next step is to create a method to transfer DWG file stream, it uses HttpRequest object and “PUT” method. The signature is:
public boolean Put(string remoteFilePath,
Attachment attach)
Here is the code to call WebdavManager_WebdavClient.Put method to transfer DWG attachment to AutoCAD WS storage. It is part of transferToWSStorage method, which is introduced in part4.
public void transferToWSStorage()
|
Here is the complete code, I use the user credentials to generate the Authorization header. The Authorization header is sent to the webdav server in each request, and the server uses this header to authorize the user. I also set http header 'Content-Type'
to 'application/x-dwg;', indicating it is a DWG file, and set the HTTP method to “PUT” and send the binary stream of DWG file. Please note that the DWG file should be less than 3MB, due to salesforce.com limit. We may need another way to transfer large files, if you have any suggestions, please do let me know, I really appreciate that.
public with sharing class WebdavManager_WebdavClient {
|
Before you run the code, please check Setup->Security->Remote site settings to enable Salesforce to access remote site, add a new remote website to 'https://dav.autocadws.com/':
OK, with that, you can run your visual force page, and click the “Open in AutoCAD WS” button, the DWG file will be transferred to AutoCAD WS, if you open AutoCAD WS at the same time, you will see a new file is uploaded.
In next post, I will introduce how to open the DWG file in AutoCAD WS editor.
Stay tuned and have fun!